Skip to content

Commit

Permalink
Merge pull request #565 from meshplus/fix/appchain-available
Browse files Browse the repository at this point in the history
fix(*): improve interface for determining the available status of appchain
  • Loading branch information
Sandy Zhou authored Aug 20, 2021
2 parents c3478d3 + 0614516 commit 4e4a4a5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 85 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/libp2p/go-libp2p-core v0.5.6
github.com/looplab/fsm v0.2.0
github.com/magiconair/properties v1.8.4
github.com/meshplus/bitxhub-core v1.3.1-0.20210812123923-d9dc47396e0b
github.com/meshplus/bitxhub-core v1.3.1-0.20210819114413-70449f8bd324
github.com/meshplus/bitxhub-kit v1.2.1-0.20210813111404-c50fb6384374
github.com/meshplus/bitxhub-model v1.2.1-0.20210811073359-62d4143881d5
github.com/meshplus/eth-kit v0.0.0-20210813021934-4ed1d1931201
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsO
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/meshplus/bitxhub-core v1.3.1-0.20210812123923-d9dc47396e0b h1:jP8RIwQvm45gKwpAM4BIWX0BeOmJxzmb9uW0fDZFesA=
github.com/meshplus/bitxhub-core v1.3.1-0.20210812123923-d9dc47396e0b/go.mod h1:F20O0rZbjGt25mpHJNQiw20z8whY+GsUof9y82MT4pg=
github.com/meshplus/bitxhub-core v1.3.1-0.20210819114413-70449f8bd324 h1:hebLGxmjVAsI4X+AepSKLR08qCLszRImZMXV8hgWBRM=
github.com/meshplus/bitxhub-core v1.3.1-0.20210819114413-70449f8bd324/go.mod h1:F20O0rZbjGt25mpHJNQiw20z8whY+GsUof9y82MT4pg=
github.com/meshplus/bitxhub-kit v1.1.1/go.mod h1:r4l4iqn0RPJreb/OmoYKfjCjQJrXpZX++6Qc31VG/1k=
github.com/meshplus/bitxhub-kit v1.2.1-0.20210616114532-4849447f09e1 h1:rLkrRvNOduKx+w3sGxNQIUCFf2Qu6d/mvTCGZjsfVZw=
github.com/meshplus/bitxhub-kit v1.2.1-0.20210616114532-4849447f09e1/go.mod h1:wrEdhHp1tktzdwcWb4bOxYsVc+KkcrYL18IYWYeumPQ=
Expand Down
98 changes: 37 additions & 61 deletions internal/executor/contracts/appchain_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,48 +309,36 @@ func (am *AppchainManager) FreezeAppchain(id, reason string) *boltvm.Response {
}

res := am.CrossInvoke(constant.RoleContractAddr.String(), "CheckPermission",
pb.String(string(PermissionSelfAdmin)),
pb.String(string(PermissionAdmin)),
pb.String(addr),
pb.String(am.CurrentCaller()),
pb.Bytes(nil))
if !res.Ok {
return boltvm.Error("check permission error:" + string(res.Result))
}

// 2. Judging the freezing mode
// - Active freeze (appchain admin) does not require voting governance
// - Passive freeze (relay-chain admin) requires voting governance
var event governance.EventType
if am.CurrentCaller() != addr {
event = governance.EventPassiveFreeze
} else {
event = governance.EventActiveFreeze
}

// 3. GovernancePre: check if exist and status
if ok, data := am.AppchainManager.GovernancePre(id, event, nil); !ok {
// 2. GovernancePre: check if exist and status
if ok, data := am.AppchainManager.GovernancePre(id, governance.EventFreeze, nil); !ok {
return boltvm.Error("freeze prepare error: " + string(data))
}

// 4. SubmitProposal(only EventPassiveFreeze require)
if event == governance.EventPassiveFreeze {
res = am.CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal",
pb.String(am.Caller()),
pb.String(string(event)),
pb.String(""),
pb.String(string(AppchainMgr)),
pb.String(id),
pb.String(string(chainInfo.Status)),
pb.String(reason),
pb.Bytes(chainData),
)
if !res.Ok {
return boltvm.Error("submit proposal error:" + string(res.Result))
}
// 3. SubmitProposal
res = am.CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal",
pb.String(am.Caller()),
pb.String(string(governance.EventFreeze)),
pb.String(""),
pb.String(string(AppchainMgr)),
pb.String(id),
pb.String(string(chainInfo.Status)),
pb.String(reason),
pb.Bytes(chainData),
)
if !res.Ok {
return boltvm.Error("submit proposal error:" + string(res.Result))
}

// 5. ChangeStatus
if ok, data := am.AppchainManager.ChangeStatus(id, string(event), string(chainInfo.Status), nil); !ok {
// 4. ChangeStatus
if ok, data := am.AppchainManager.ChangeStatus(id, string(governance.EventFreeze), string(chainInfo.Status), nil); !ok {
return boltvm.Error(string(data))
}

Expand Down Expand Up @@ -384,40 +372,28 @@ func (am *AppchainManager) ActivateAppchain(id, reason string) *boltvm.Response
return boltvm.Error("check permission error:" + string(res.Result))
}

// 2. Judging the activate mode
// - Active activate (appchain admin) : not require voting governance
// - Passive activate (relay-chain admin) : require voting governance
var event governance.EventType
if am.CurrentCaller() != addr {
event = governance.EventPassiveActivate
} else {
event = governance.EventActiveActivate
}

// 3. GovernancePre: check if exist and status
if ok, data := am.AppchainManager.GovernancePre(id, event, nil); !ok {
// 2. GovernancePre: check if exist and status
if ok, data := am.AppchainManager.GovernancePre(id, governance.EventActivate, nil); !ok {
return boltvm.Error("activate prepare error: " + string(data))
}

// 4. SubmitProposal
if event != governance.EventActiveActivate {
res = am.CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal",
pb.String(am.Caller()),
pb.String(string(event)),
pb.String(""),
pb.String(string(AppchainMgr)),
pb.String(id),
pb.String(string(chainInfo.Status)),
pb.String(reason),
pb.Bytes(chainData),
)
if !res.Ok {
return boltvm.Error("submit proposal error:" + string(res.Result))
}
// 3. SubmitProposal
res = am.CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal",
pb.String(am.Caller()),
pb.String(string(governance.EventActivate)),
pb.String(""),
pb.String(string(AppchainMgr)),
pb.String(id),
pb.String(string(chainInfo.Status)),
pb.String(reason),
pb.Bytes(chainData),
)
if !res.Ok {
return boltvm.Error("submit proposal error:" + string(res.Result))
}

// 5. ChangeStatus
if ok, data := am.AppchainManager.ChangeStatus(id, string(event), string(chainInfo.Status), nil); !ok {
// 4. ChangeStatus
if ok, data := am.AppchainManager.ChangeStatus(id, string(governance.EventActivate), string(chainInfo.Status), nil); !ok {
return boltvm.Error(string(data))
}

Expand Down Expand Up @@ -591,10 +567,10 @@ func (am *AppchainManager) IsAvailable(chainId string) *boltvm.Response {

for _, s := range appchainMgr.AppchainAvailableState {
if app.Status == s {
return boltvm.Success(nil)
return boltvm.Success([]byte("true"))
}
}
return boltvm.Error("the appchain status is " + string(app.Status))
return boltvm.Success([]byte("false"))
}

func getAddr(pubKeyStr string) (string, error) {
Expand Down
7 changes: 4 additions & 3 deletions internal/executor/contracts/appchain_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ func TestAppchainManager_IsAvailable(t *testing.T) {
res := am.IsAvailable(chains[0].ID)
assert.Equal(t, true, res.Ok, string(res.Result))
res = am.IsAvailable(chains[1].ID)
assert.Equal(t, false, res.Ok, string(res.Result))
assert.Equal(t, true, res.Ok, string(res.Result))
assert.Equal(t, "false", string(res.Result))
res = am.IsAvailable("errId")
assert.Equal(t, false, res.Ok, string(res.Result))
res = am.IsAvailable("unmarshalErrId")
Expand All @@ -280,7 +281,7 @@ func TestManageChain(t *testing.T) {
availableChain.Status = governance.GovernanceAvailable
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).SetArg(1, *availableChain).Return(true).Times(2)
frozenChain := chains[1]
frozenChain.Status = governance.GovernancePassiveFrozen
frozenChain.Status = governance.GovernanceFrozen
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).SetArg(1, *frozenChain).Return(true).AnyTimes()

// test UpdateAppchain
Expand Down Expand Up @@ -431,7 +432,7 @@ func prepare(t *testing.T) (*AppchainManager, *mock_stub.MockStub, []*appchainMg

var chains []*appchainMgr.Appchain
var chainsData [][]byte
chainType := []string{string(governance.GovernanceAvailable), string(governance.GovernancePassiveFrozen), string(governance.GovernanceUnavailable)}
chainType := []string{string(governance.GovernanceAvailable), string(governance.GovernanceFrozen), string(governance.GovernanceUnavailable)}

chainAdminKeyPath, err := repo.PathRootWithDefault("../../../tester/test_data/appchain1.json")
assert.Nil(t, err)
Expand Down
36 changes: 18 additions & 18 deletions internal/executor/contracts/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ type Ballot struct {
VoteTime int64 `json:"vote_time"`
}

func (g *Governance) GetBallot(voterAddr, proposalId string) *boltvm.Response {
p := &Proposal{}
if !g.GetObject(ProposalKey(proposalId), p) {
return boltvm.Error("proposal does not exist")
}

ballot, ok := p.BallotMap[voterAddr]
if !ok {
return boltvm.Error("administrator of the address has not voted")
}

bData, err := json.Marshal(ballot)
if err != nil {
return boltvm.Error(err.Error())
}
return boltvm.Success(bData)
}

type Proposal struct {
Id string `json:"id"`
Des string `json:"des"`
Expand Down Expand Up @@ -300,6 +282,24 @@ func (g *Governance) WithdrawProposal(id, reason string) *boltvm.Response {
return boltvm.Success(nil)
}

func (g *Governance) GetBallot(voterAddr, proposalId string) *boltvm.Response {
p := &Proposal{}
if !g.GetObject(ProposalKey(proposalId), p) {
return boltvm.Error("proposal does not exist")
}

ballot, ok := p.BallotMap[voterAddr]
if !ok {
return boltvm.Error("administrator of the address has not voted")
}

bData, err := json.Marshal(ballot)
if err != nil {
return boltvm.Error(err.Error())
}
return boltvm.Success(bData)
}

// GetProposal query proposal by id
func (g *Governance) GetProposal(id string) *boltvm.Response {
p := &Proposal{}
Expand Down

0 comments on commit 4e4a4a5

Please sign in to comment.