feat(cmd): add init and addAdmin command registry contract

This commit is contained in:
Alexader 2021-04-13 16:21:19 +08:00
parent 6d97564b5c
commit b43ecf0198
4 changed files with 130 additions and 39 deletions

View File

@ -26,6 +26,7 @@ var clientCMD = cli.Command{
validatorsCMD(),
delVPNodeCMD(),
governanceCMD(),
didCMD(),
},
}

119
cmd/bitxhub/client/did.go Normal file
View File

@ -0,0 +1,119 @@
package client
import (
"fmt"
"github.com/fatih/color"
"github.com/meshplus/bitxhub-model/constant"
"github.com/meshplus/bitxhub-model/pb"
"github.com/meshplus/bitxhub/internal/repo"
"github.com/urfave/cli"
)
const relaychainDIDPrefix = "did:bitxhub:relayroot:"
func didCMD() cli.Command {
return cli.Command{
Name: "did",
Usage: "did command",
Subcommands: cli.Commands{
cli.Command{
Name: "init",
Usage: "init method-registry contract",
Action: initAdminDID,
},
cli.Command{
Name: "addAdmin",
Usage: "add admin role for method-registry contract",
Flags: []cli.Flag{
cli.StringFlag{
Name: "addr",
Usage: "Specify node address derived from node public key " +
"to add method-registry contract",
Required: true,
},
},
Action: addAdmin,
},
},
}
}
func initAdminDID(ctx *cli.Context) error {
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
keyPath := repo.GetKeyPath(repoRoot)
key, err := repo.LoadKey(keyPath)
if err != nil {
return fmt.Errorf("wrong key: %w", err)
}
from, err := key.PrivKey.PublicKey().Address()
if err != nil {
return fmt.Errorf("wrong private key: %w", err)
}
relayAdminDID := genRelaychainDID(from.String())
// init method registry with this admin key
receipt, err := invokeBVMContract(ctx,
constant.MethodRegistryContractAddr.String(),
"Init", pb.String(relayAdminDID),
)
if err != nil {
return fmt.Errorf("invoke bvm contract: %w", err)
}
if !receipt.IsSuccess() {
return fmt.Errorf("method registery init faild: %s", string(receipt.Ret))
}
// init did registry with this admin key
receipt, err = invokeBVMContract(ctx,
constant.DIDRegistryContractAddr.String(),
"Init", pb.String(relayAdminDID),
)
if err != nil {
return fmt.Errorf("invoke bvm contract: %w", err)
}
if !receipt.IsSuccess() {
return fmt.Errorf("did registery init faild: %s", string(receipt.Ret))
}
return nil
}
func addAdmin(ctx *cli.Context) error {
addr := ctx.String("addr")
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
keyPath := repo.GetKeyPath(repoRoot)
key, err := repo.LoadKey(keyPath)
if err != nil {
return fmt.Errorf("wrong key: %w", err)
}
from, err := key.PrivKey.PublicKey().Address()
if err != nil {
return fmt.Errorf("wrong private key: %w", err)
}
adminDID := genRelaychainDID(from.String())
toAddAdminDID := genRelaychainDID(addr)
receipt, err := invokeBVMContract(ctx, constant.MethodRegistryContractAddr.String(), "AddAdmin",
pb.String(adminDID), pb.String(toAddAdminDID))
if err != nil {
return err
}
if receipt.IsSuccess() {
color.Green("Add admin for method-registry successfully!\n")
} else {
color.Red("Add admin for method-registry error: %s\n", string(receipt.Ret))
}
return nil
}
func genRelaychainDID(addr string) string {
return relaychainDIDPrefix + addr
}

View File

@ -86,6 +86,11 @@ func (am *AppchainManager) Manager(des string, proposalResult string, extra []by
func (am *AppchainManager) Register(appchainAdminDID, appchainMethod string, docAddr, docHash, validators string,
consensusType, chainType, name, desc, version, pubkey string) *boltvm.Response {
am.AppchainManager.Persister = am.Stub
res := am.CrossInvoke(constant.MethodRegistryContractAddr.String(), "Apply",
pb.String(appchainAdminDID), pb.String(appchainMethod), pb.Bytes(nil))
if !res.Ok {
return res
}
ok, idData := am.AppchainManager.Register(appchainMethod, appchainAdminDID, docAddr, docHash, validators, consensusType,
chainType, name, desc, version, pubkey)
if ok {
@ -97,11 +102,6 @@ func (am *AppchainManager) Register(appchainAdminDID, appchainMethod string, doc
return boltvm.Error("get appchain error: " + string(data))
}
res := am.CrossInvoke(constant.MethodRegistryContractAddr.String(), "Apply",
pb.String(appchainAdminDID), pb.String(appchainMethod), pb.Bytes(nil))
if !res.Ok {
return res
}
res = am.CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal",
pb.String(am.Caller()),
pb.String(appchainMgr.EventRegister),

View File

@ -172,50 +172,21 @@ func TestAppchainManager_Register(t *testing.T) {
mockStub.EXPECT().Logger().Return(logger).AnyTimes()
mockStub.EXPECT().CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil))
mockStub.EXPECT().CrossInvoke(constant.MethodRegistryContractAddr.String(), "Apply",
gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil))
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(false).Times(1)
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(true).AnyTimes()
gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil)).Times(2)
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(false)
res := am.Register(appchainAdminDID, appchainMethod, docAddr, docHash,
chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
chains[0].Name, chains[0].Desc, chains[0].Version, chains[0].PublicKey)
assert.True(t, res.Ok)
// test for repeated register
am.Register(appchainAdminDID, appchainMethod, docAddr, docHash,
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(true)
res = am.Register(appchainAdminDID, appchainMethod, docAddr, docHash,
chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
chains[0].Name, chains[0].Desc, chains[0].Version, chains[0].PublicKey)
assert.True(t, res.Ok)
assert.False(t, res.Ok)
}
//func TestAudit(t *testing.T) {
// am, mockStub, _, _ := prepare(t)
// logger := log.NewWithModule("contracts")
//
// mockStub.EXPECT().Caller().Return(caller).AnyTimes()
// mockStub.EXPECT().Get(gomock.Any()).Return(true, chainsData[0]).AnyTimes()
// mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Do(
// func(key string, ret interface{}) bool {
// chain := ret.(*appchainMgr.Appchain)
// chain.ID = chains[0].ID
// return true
// }).Return(true).AnyTimes()
// mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()
// mockStub.EXPECT().Logger().Return(logger).AnyTimes()
// mockStub.EXPECT().CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil))
// mockStub.EXPECT().Has(AppchainKey(caller)).Return(false).Times(1)
// mockStub.EXPECT().Has(AppchainKey(caller)).Return(true).AnyTimes()
// res := am.Register(appchainAdminDID, appchainMethod, docAddr, docHash,
// chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
// chains[0].Name, chains[0].Desc, chains[0].Version, chains[0].PublicKey)
// assert.True(t, res.Ok)
//
// // test for repeated register
// am.Register(appchainAdminDID, appchainMethod, docAddr, docHash,
// chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
// chains[0].Name, chains[0].Desc, chains[0].Version, chains[0].PublicKey)
// assert.True(t, res.Ok)
//}
func TestAppchainManager_Manager(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)