diff --git a/go.mod b/go.mod index 95cbf01..b18b92a 100644 --- a/go.mod +++ b/go.mod @@ -15,14 +15,12 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/golang/mock v1.4.3 github.com/grpc-ecosystem/grpc-gateway v1.13.0 - github.com/hashicorp/go-version v1.2.0 // indirect github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e github.com/libp2p/go-libp2p-core v0.3.0 github.com/magiconair/properties v1.8.1 - github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200429064226-95effbd4b140 + github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200507061023-08d568ad983d github.com/meshplus/bitxhub-kit v1.0.1-0.20200429033154-8736253c8460 github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200427024612-57b479c1d610 - github.com/miekg/pkcs11 v1.0.3 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/multiformats/go-multiaddr v0.2.0 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index c3e20a5..3181fb7 100644 --- a/go.sum +++ b/go.sum @@ -262,8 +262,8 @@ github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7 github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/hyperledger/fabric v2.0.1+incompatible h1:7W+yG0gLKTC7NLcWPT3vfpnaseztPpH9wXGfAW7yvBs= github.com/hyperledger/fabric v2.0.1+incompatible/go.mod h1:tGFAOCT696D3rG0Vofd2dyWYLySHlh0aQjf7Q1HAju0= -github.com/hyperledger/fabric-amcl v0.0.0-20200424173818-327c9e2cf77a h1:JAKZdGuUIjVmES0X31YUD7UqMR2rz/kxLluJuGvsXPk= -github.com/hyperledger/fabric-amcl v0.0.0-20200424173818-327c9e2cf77a/go.mod h1:X+DIyUsaTmalOpmpQfIvFZjKHQedrURQ5t4YqquX7lE= +github.com/hyperledger/fabric-amcl v0.0.0-20200128223036-d1aa2665426a h1:HgdNn3UYz8PdcZrLEk0IsSU4LRHp7yY2rgjIKcSiJaA= +github.com/hyperledger/fabric-amcl v0.0.0-20200128223036-d1aa2665426a/go.mod h1:X+DIyUsaTmalOpmpQfIvFZjKHQedrURQ5t4YqquX7lE= github.com/hyperledger/fabric-protos-go v0.0.0-20200330074707-cfe579e86986 h1:g9tgYXQPZcxRryp2/rutvfSCiiJzHNoyX7JaoXeGkZ8= github.com/hyperledger/fabric-protos-go v0.0.0-20200330074707-cfe579e86986/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= @@ -495,8 +495,8 @@ github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= 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 v0.1.0-rc1.0.20200429064226-95effbd4b140 h1:AQofFnvW0A8QJ5C/bm1ZzxV1rwdiUS7slgerNM+xn/Y= -github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200429064226-95effbd4b140/go.mod h1:4T05X9Rk5Qr7zA5NkXvy/7GmUh4BkrOMDbuDShfZOFg= +github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200507061023-08d568ad983d h1:HpdrdcFI4UPzPsPOcKPM79YWCuhgelPIJjoUMftjK6M= +github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200507061023-08d568ad983d/go.mod h1:0iCI1ACHiEaVpt3C68NcgDq47dH8DI5FI7Lh9eHQAiw= github.com/meshplus/bitxhub-kit v1.0.0-rc1 h1:gNi8IFU5CMHT3KE2I4ACj5alMW9h/4cV8xOxn7wSmtA= github.com/meshplus/bitxhub-kit v1.0.0-rc1/go.mod h1:ra/AhOkPvpElI+wXrB9G6DjdcrdxFU3vMwA5MYKr9D0= github.com/meshplus/bitxhub-kit v1.0.1-0.20200429033154-8736253c8460 h1:bL+s+LBhGr4g+28DNtqyj4WoVkYdZ5CPoGjdF6Ll0sk= diff --git a/internal/constant/constant.go b/internal/constant/constant.go index 8b934bf..2451ae6 100644 --- a/internal/constant/constant.go +++ b/internal/constant/constant.go @@ -9,6 +9,7 @@ const ( StoreContractAddr BoltContractAddress = "0x000000000000000000000000000000000000000b" RuleManagerContractAddr BoltContractAddress = "0x000000000000000000000000000000000000000c" RoleContractAddr BoltContractAddress = "0x000000000000000000000000000000000000000d" + AppchainMgrContractAddr BoltContractAddress = "0x000000000000000000000000000000000000000e" ) func (addr BoltContractAddress) Address() types.Address { diff --git a/internal/executor/contracts/appchain_manager.go b/internal/executor/contracts/appchain_manager.go new file mode 100644 index 0000000..cc4e1b0 --- /dev/null +++ b/internal/executor/contracts/appchain_manager.go @@ -0,0 +1,116 @@ +package contracts + +import ( + "fmt" + "strconv" + + appchainMgr "github.com/meshplus/bitxhub-core/appchain-mgr" + + "github.com/meshplus/bitxhub-model/pb" + "github.com/meshplus/bitxhub/internal/constant" + "github.com/meshplus/bitxhub/pkg/vm/boltvm" +) + +type AppchainManager struct { + boltvm.Stub + appchainMgr.AppchainManager +} + +// Register appchain managers registers appchain info caller is the appchain +// manager address return appchain id and error +func (am *AppchainManager) Register(validators string, consensusType int32, chainType, name, desc, version, pubkey string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + res := am.CrossInvoke(constant.InterchainContractAddr.String(), "Register") + if !res.Ok { + return res + } + return responseWrapper(am.AppchainManager.Register(validators, consensusType, chainType, name, desc, version, pubkey)) +} + +// UpdateAppchain updates approved appchain +func (am *AppchainManager) UpdateAppchain(validators string, consensusType int32, chainType, name, desc, version, pubkey string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.UpdateAppchain(validators, consensusType, chainType, name, desc, version, pubkey)) +} + +//FetchAuditRecords fetches audit records by appchain id +func (am *AppchainManager) FetchAuditRecords(id string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.FetchAuditRecords(id)) +} + +// CountApprovedAppchains counts all approved appchains +func (am *AppchainManager) CountApprovedAppchains() *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.CountApprovedAppchains()) +} + +// CountAppchains counts all appchains including approved, rejected or registered +func (am *AppchainManager) CountAppchains() *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.CountAppchains()) +} + +// Appchains returns all appchains +func (am *AppchainManager) Appchains() *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.Appchains()) +} + +// Appchain returns appchain info +func (am *AppchainManager) Appchain() *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.Appchain()) +} + +func (am *AppchainManager) GetAppchain(id string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.GetAppchain(id)) +} + +// GetPubKeyByChainID can get aim chain's public key using aim chain ID +func (am *AppchainManager) GetPubKeyByChainID(id string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + return responseWrapper(am.AppchainManager.GetPubKeyByChainID(id)) +} + +// Audit bitxhub manager audit appchain register info +func (am *AppchainManager) Audit(proposer string, isApproved int32, desc string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + if res := am.IsAdmin(); !res.Ok { + return res + } + return responseWrapper(am.AppchainManager.Audit(proposer, isApproved, desc)) +} + +func (am *AppchainManager) DeleteAppchain(cid string) *boltvm.Response { + am.AppchainManager.Persister = am.Stub + if res := am.IsAdmin(); !res.Ok { + return res + } + res := am.CrossInvoke(constant.InterchainContractAddr.String(), "DeleteInterchain", pb.String(cid)) + if !res.Ok { + return res + } + return responseWrapper(am.AppchainManager.DeleteAppchain(cid)) +} + +func (am *AppchainManager) IsAdmin() *boltvm.Response { + ret := am.CrossInvoke(constant.RoleContractAddr.String(), "IsAdmin", pb.String(am.Caller())) + is, err := strconv.ParseBool(string(ret.Result)) + if err != nil { + return boltvm.Error(fmt.Errorf("judge caller type: %w", err).Error()) + } + + if !is { + return boltvm.Error("caller is not an admin account") + } + return boltvm.Success([]byte("1")) +} + +func responseWrapper(ok bool, data []byte) *boltvm.Response { + if ok { + return boltvm.Success(data) + } + return boltvm.Error(string(data)) +} diff --git a/internal/executor/contracts/interchain.go b/internal/executor/contracts/interchain.go index 8665f37..298b68f 100755 --- a/internal/executor/contracts/interchain.go +++ b/internal/executor/contracts/interchain.go @@ -3,52 +3,29 @@ package contracts import ( "encoding/json" "fmt" - "strconv" "strings" - "github.com/meshplus/bitxhub/internal/constant" - + appchainMgr "github.com/meshplus/bitxhub-core/appchain-mgr" "github.com/meshplus/bitxhub-kit/types" "github.com/meshplus/bitxhub-model/pb" + + "github.com/meshplus/bitxhub/internal/constant" "github.com/meshplus/bitxhub/pkg/vm/boltvm" - "github.com/sirupsen/logrus" ) -const ( - prefix = "appchain-" - - registered = 0 - approved = 1 -) - -type Interchain struct { +type InterchainManager struct { boltvm.Stub } -type appchain struct { - ID string `json:"id"` - Name string `json:"name"` - Validators string `json:"validators"` - ConsensusType int32 `json:"consensus_type"` - // 0 => registered, 1 => approved, -1 => rejected - Status int32 `json:"status"` - ChainType string `json:"chain_type"` - Desc string `json:"desc"` - Version string `json:"version"` - PublicKey string `json:"public_key"` +type Interchain struct { + ID string `json:"id"` InterchainCounter map[string]uint64 `json:"interchain_counter,omitempty"` ReceiptCounter map[string]uint64 `json:"receipt_counter,omitempty"` SourceReceiptCounter map[string]uint64 `json:"source_receipt_counter,omitempty"` } -type auditRecord struct { - Appchain *appchain `json:"appchain"` - IsApproved bool `json:"is_approved"` - Desc string `json:"desc"` -} - -func (chain *appchain) UnmarshalJSON(data []byte) error { - type alias appchain +func (i *Interchain) UnmarshalJSON(data []byte) error { + type alias Interchain t := &alias{} if err := json.Unmarshal(data, t); err != nil { return err @@ -66,38 +43,19 @@ func (chain *appchain) UnmarshalJSON(data []byte) error { t.SourceReceiptCounter = make(map[string]uint64) } - *chain = appchain(*t) + *i = Interchain(*t) return nil } -// Register appchain manager registers appchain info caller is the appchain -// manager address return appchain id and error -func (x *Interchain) Register(validators string, consensusType int32, chainType, name, desc, version, pubkey string) *boltvm.Response { - chain := &appchain{ - ID: x.Caller(), - Name: name, - Validators: validators, - ConsensusType: consensusType, - ChainType: chainType, - Desc: desc, - Version: version, - PublicKey: pubkey, - } - +func (x *InterchainManager) Register() *boltvm.Response { + interchain := &Interchain{ID: x.Caller()} ok := x.Has(x.appchainKey(x.Caller())) if ok { - x.Stub.Logger().WithFields(logrus.Fields{ - "id": x.Caller(), - }).Debug("Appchain has registered") - x.GetObject(x.appchainKey(x.Caller()), chain) + x.GetObject(x.appchainKey(x.Caller()), interchain) } else { - // logger.Info(x.Caller()) - x.SetObject(x.appchainKey(x.Caller()), chain) - x.Logger().WithFields(logrus.Fields{ - "id": x.Caller(), - }).Info("Appchain register successfully") + x.SetObject(x.appchainKey(x.Caller()), interchain) } - body, err := json.Marshal(chain) + body, err := json.Marshal(interchain) if err != nil { return boltvm.Error(err.Error()) } @@ -105,167 +63,12 @@ func (x *Interchain) Register(validators string, consensusType int32, chainType, return boltvm.Success(body) } -func (x *Interchain) UpdateAppchain(validators string, consensusType int32, chainType, name, desc, version, pubkey string) *boltvm.Response { - ok := x.Has(x.appchainKey(x.Caller())) - if !ok { - return boltvm.Error("register appchain firstly") - } - - chain := &appchain{} - x.GetObject(x.appchainKey(x.Caller()), chain) - - if chain.Status == registered { - return boltvm.Error("this appchain is being audited") - } - - chain = &appchain{ - ID: x.Caller(), - Name: name, - Validators: validators, - ConsensusType: consensusType, - ChainType: chainType, - Desc: desc, - Version: version, - PublicKey: pubkey, - } - - x.SetObject(x.appchainKey(x.Caller()), chain) - +func (x *InterchainManager) DeleteInterchain(id string) *boltvm.Response { + x.Delete(x.appchainKey(id)) return boltvm.Success(nil) } -// Audit bitxhub manager audit appchain register info -// caller is the bitxhub manager address -// proposer is the appchain manager address -func (x *Interchain) Audit(proposer string, isApproved int32, desc string) *boltvm.Response { - ret := x.CrossInvoke(constant.RoleContractAddr.String(), "IsAdmin", pb.String(x.Caller())) - is, err := strconv.ParseBool(string(ret.Result)) - if err != nil { - return boltvm.Error(fmt.Errorf("judge caller type: %w", err).Error()) - } - - if !is { - return boltvm.Error("caller is not an admin account") - } - - chain := &appchain{} - ok := x.GetObject(x.appchainKey(proposer), chain) - if !ok { - return boltvm.Error(fmt.Errorf("this appchain does not exist").Error()) - } - - chain.Status = isApproved - - record := &auditRecord{ - Appchain: chain, - IsApproved: isApproved == approved, - Desc: desc, - } - - var records []*auditRecord - x.GetObject(x.auditRecordKey(proposer), &records) - records = append(records, record) - - x.SetObject(x.auditRecordKey(proposer), records) - x.SetObject(x.appchainKey(proposer), chain) - - return boltvm.Success([]byte(fmt.Sprintf("audit %s successfully", proposer))) -} - -func (x *Interchain) FetchAuditRecords(id string) *boltvm.Response { - var records []*auditRecord - x.GetObject(x.auditRecordKey(id), &records) - - body, err := json.Marshal(records) - if err != nil { - return boltvm.Error(err.Error()) - } - - return boltvm.Success(body) -} - -// CountApprovedAppchains counts all approved appchains -func (x *Interchain) CountApprovedAppchains() *boltvm.Response { - ok, value := x.Query(prefix) - if !ok { - return boltvm.Success([]byte("0")) - } - - count := 0 - for _, v := range value { - a := &appchain{} - if err := json.Unmarshal(v, a); err != nil { - return boltvm.Error(fmt.Sprintf("unmarshal json error: %v", err)) - } - if a.Status == approved { - count++ - } - } - - return boltvm.Success([]byte(strconv.Itoa(count))) -} - -// CountAppchains counts all appchains including approved, rejected or registered -func (x *Interchain) CountAppchains() *boltvm.Response { - ok, value := x.Query(prefix) - if !ok { - return boltvm.Success([]byte("0")) - } - - return boltvm.Success([]byte(strconv.Itoa(len(value)))) -} - -// Appchains returns all appchains -func (x *Interchain) Appchains() *boltvm.Response { - ok, value := x.Query(prefix) - if !ok { - return boltvm.Success(nil) - } - - ret := make([]*appchain, 0) - for _, data := range value { - chain := &appchain{} - if err := json.Unmarshal(data, chain); err != nil { - return boltvm.Error(err.Error()) - } - ret = append(ret, chain) - } - - data, err := json.Marshal(ret) - if err != nil { - return boltvm.Error(err.Error()) - } - - return boltvm.Success(data) -} - -func (x *Interchain) DeleteAppchain(cid string) *boltvm.Response { - ret := x.CrossInvoke(constant.RoleContractAddr.String(), "IsAdmin", pb.String(x.Caller())) - is, err := strconv.ParseBool(string(ret.Result)) - if err != nil { - return boltvm.Error(fmt.Errorf("judge caller type: %w", err).Error()) - } - - if !is { - return boltvm.Error("caller is not an admin account") - } - - x.Delete(prefix + cid) - x.Logger().Infof("delete appchain:%s", cid) - - return boltvm.Success(nil) -} - -func (x *Interchain) Appchain() *boltvm.Response { - ok, data := x.Get(x.appchainKey(x.Caller())) - if !ok { - return boltvm.Error(fmt.Errorf("this appchain does not exist").Error()) - } - - return boltvm.Success(data) -} - -func (x *Interchain) HandleIBTP(data []byte) *boltvm.Response { +func (x *InterchainManager) HandleIBTP(data []byte) *boltvm.Response { ok := x.Has(x.appchainKey(x.Caller())) if !ok { return boltvm.Error("this appchain does not exist") @@ -279,17 +82,22 @@ func (x *Interchain) HandleIBTP(data []byte) *boltvm.Response { if ibtp.To == "" { return boltvm.Error("empty destination chain id") } - ok = x.Has(x.appchainKey(ibtp.To)) if !ok { x.Logger().WithField("chain_id", ibtp.To).Warn("target appchain does not exist") } - app := &appchain{} - x.GetObject(x.appchainKey(ibtp.From), &app) + interchain := &Interchain{} + x.GetObject(x.appchainKey(ibtp.From), &interchain) + app := &appchainMgr.Appchain{} + res := x.CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(ibtp.From)) + err := json.Unmarshal(res.Result, app) + if err != nil { + return boltvm.Error(err.Error()) + } // get validation rule contract address - res := x.CrossInvoke(constant.RuleManagerContractAddr.String(), "GetRuleAddress", pb.String(ibtp.From), pb.String(app.ChainType)) + res = x.CrossInvoke(constant.RuleManagerContractAddr.String(), "GetRuleAddress", pb.String(ibtp.From), pb.String(app.ChainType)) if !res.Ok { return boltvm.Error("this appchain don't register rule") } @@ -310,13 +118,13 @@ func (x *Interchain) HandleIBTP(data []byte) *boltvm.Response { return boltvm.Error("ibtp from != caller") } - idx := app.InterchainCounter[ibtp.To] + idx := interchain.InterchainCounter[ibtp.To] if idx+1 != ibtp.Index { return boltvm.Error(fmt.Sprintf("wrong index, required %d, but %d", idx+1, ibtp.Index)) } - app.InterchainCounter[ibtp.To]++ - x.SetObject(x.appchainKey(ibtp.From), app) + interchain.InterchainCounter[ibtp.To]++ + x.SetObject(x.appchainKey(ibtp.From), interchain) x.SetObject(x.indexMapKey(ibtp.ID()), x.GetTxHash()) m := make(map[string]uint64) m[ibtp.To] = x.GetTxIndex() @@ -326,22 +134,22 @@ func (x *Interchain) HandleIBTP(data []byte) *boltvm.Response { return boltvm.Error("ibtp from != caller") } - idx := app.ReceiptCounter[ibtp.To] + idx := interchain.ReceiptCounter[ibtp.To] if idx+1 != ibtp.Index { - if app.SourceReceiptCounter[ibtp.To]+1 != ibtp.Index { + if interchain.SourceReceiptCounter[ibtp.To]+1 != ibtp.Index { return boltvm.Error(fmt.Sprintf("wrong receipt index, required %d, but %d", idx+1, ibtp.Index)) } } - app.ReceiptCounter[ibtp.To] = ibtp.Index - x.SetObject(x.appchainKey(ibtp.From), app) + interchain.ReceiptCounter[ibtp.To] = ibtp.Index + x.SetObject(x.appchainKey(ibtp.From), interchain) m := make(map[string]uint64) m[ibtp.From] = x.GetTxIndex() - ac := &appchain{} - x.GetObject(x.appchainKey(ibtp.To), &ac) - ac.SourceReceiptCounter[ibtp.From] = ibtp.Index - x.SetObject(x.appchainKey(ibtp.To), ac) + ic := &Interchain{} + x.GetObject(x.appchainKey(ibtp.To), &ic) + ic.SourceReceiptCounter[ibtp.From] = ibtp.Index + x.SetObject(x.appchainKey(ibtp.To), ic) x.PostInterchainEvent(m) } @@ -349,7 +157,7 @@ func (x *Interchain) HandleIBTP(data []byte) *boltvm.Response { return boltvm.Success(nil) } -func (x *Interchain) GetIBTPByID(id string) *boltvm.Response { +func (x *InterchainManager) GetIBTPByID(id string) *boltvm.Response { arr := strings.Split(id, "-") if len(arr) != 3 { return boltvm.Error("wrong ibtp id") @@ -370,26 +178,10 @@ func (x *Interchain) GetIBTPByID(id string) *boltvm.Response { return boltvm.Success(hash.Bytes()) } -// GetPubKeyByChainID can get aim chain's public key using aim chain ID -func (x *Interchain) GetPubKeyByChainID(id string) *boltvm.Response { - ok := x.Has(x.appchainKey(id)) - if !ok { - return boltvm.Error("chain is not existed") - } else { - chain := &appchain{} - x.GetObject(x.appchainKey(id), chain) - return boltvm.Success([]byte(chain.PublicKey)) - } +func (x *InterchainManager) appchainKey(id string) string { + return appchainMgr.PREFIX + id } -func (x *Interchain) appchainKey(id string) string { - return prefix + id -} - -func (x *Interchain) auditRecordKey(id string) string { - return "audit-record-" + id -} - -func (x *Interchain) indexMapKey(id string) string { +func (x *InterchainManager) indexMapKey(id string) string { return fmt.Sprintf("index-tx-%s", id) } diff --git a/internal/executor/contracts/role.go b/internal/executor/contracts/role.go index 36f5001..8b152b0 100644 --- a/internal/executor/contracts/role.go +++ b/internal/executor/contracts/role.go @@ -5,7 +5,6 @@ import ( "strconv" "github.com/meshplus/bitxhub/internal/constant" - "github.com/meshplus/bitxhub/pkg/vm/boltvm" ) @@ -27,7 +26,7 @@ func (r *Role) GetRole() *boltvm.Response { } } - res := r.CrossInvoke(constant.InterchainContractAddr.String(), "Appchain") + res := r.CrossInvoke(constant.AppchainMgrContractAddr.String(), "Appchain") if !res.Ok { return boltvm.Success([]byte("none")) } diff --git a/internal/executor/contracts/rule_manager.go b/internal/executor/contracts/rule_manager.go index fb96569..8702b02 100644 --- a/internal/executor/contracts/rule_manager.go +++ b/internal/executor/contracts/rule_manager.go @@ -3,7 +3,9 @@ package contracts import ( "fmt" + appchainMgr "github.com/meshplus/bitxhub-core/appchain-mgr" "github.com/meshplus/bitxhub-core/validator" + "github.com/meshplus/bitxhub/pkg/vm/boltvm" ) @@ -62,7 +64,7 @@ func (r *RuleManager) Audit(id string, isApproved int32, desc string) *boltvm.Re record := &ruleRecord{ Rule: rl, - IsApproved: isApproved == approved, + IsApproved: isApproved == appchainMgr.APPROVED, Desc: desc, } diff --git a/internal/executor/contracts/store.go b/internal/executor/contracts/store.go index 50c0571..e8a02ad 100644 --- a/internal/executor/contracts/store.go +++ b/internal/executor/contracts/store.go @@ -16,7 +16,7 @@ func (s *Store) Set(key string, value string) *boltvm.Response { func (s *Store) Get(key string) *boltvm.Response { var v string - ok := s.Stub.GetObject(key, &v) + ok := s.GetObject(key, &v) if !ok { return boltvm.Error("there is not exist key") } diff --git a/internal/executor/executor.go b/internal/executor/executor.go index cdf8e3b..d919c02 100755 --- a/internal/executor/executor.go +++ b/internal/executor/executor.go @@ -4,13 +4,14 @@ import ( "context" "fmt" + "github.com/meshplus/bitxhub/internal/executor/contracts" + "github.com/ethereum/go-ethereum/event" "github.com/meshplus/bitxhub-core/validator" "github.com/meshplus/bitxhub-kit/cache" "github.com/meshplus/bitxhub-kit/types" "github.com/meshplus/bitxhub-model/pb" "github.com/meshplus/bitxhub/internal/constant" - "github.com/meshplus/bitxhub/internal/executor/contracts" "github.com/meshplus/bitxhub/internal/ledger" "github.com/meshplus/bitxhub/internal/model/events" "github.com/meshplus/bitxhub/pkg/vm/boltvm" @@ -120,9 +121,9 @@ func registerBoltContracts() map[string]boltvm.Contract { boltContracts := []*boltvm.BoltContract{ { Enabled: true, - Name: "appchain manager contract", + Name: "interchain manager contract", Address: constant.InterchainContractAddr.String(), - Contract: &contracts.Interchain{}, + Contract: &contracts.InterchainManager{}, }, { Enabled: true, @@ -142,6 +143,12 @@ func registerBoltContracts() map[string]boltvm.Contract { Address: constant.RoleContractAddr.String(), Contract: &contracts.Role{}, }, + { + Enabled: true, + Name: "appchain manager service", + Address: constant.AppchainMgrContractAddr.String(), + Contract: &contracts.AppchainManager{}, + }, } return boltvm.Register(boltContracts) diff --git a/tester/case002_appchain_test.go b/tester/case002_appchain_test.go index c0bd570..a7be8bb 100755 --- a/tester/case002_appchain_test.go +++ b/tester/case002_appchain_test.go @@ -45,7 +45,7 @@ func (suite *RegisterAppchain) TestRegisterAppchain() { pb.String(string(pub)), } - ret, err := invokeBVMContract(suite.api, suite.privKey, constant.InterchainContractAddr.Address(), "Register", args...) + ret, err := invokeBVMContract(suite.api, suite.privKey, constant.AppchainMgrContractAddr.Address(), "Register", args...) suite.Require().Nil(err) suite.Require().True(ret.IsSuccess(), string(ret.Ret)) suite.Require().Equal("hyperchain", gjson.Get(string(ret.Ret), "chain_type").String()) @@ -71,7 +71,7 @@ func (suite *RegisterAppchain) TestFetchAppchains() { pb.String("1.8"), pb.String(string(pub1)), } - ret, err := invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "Register", args...) + ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", args...) suite.Require().Nil(err) suite.Require().True(ret.IsSuccess(), string(ret.Ret)) @@ -85,15 +85,15 @@ func (suite *RegisterAppchain) TestFetchAppchains() { pb.String(string(pub2)), } - ret, err = invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "Register", args...) + ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register", args...) suite.Require().True(ret.IsSuccess(), string(ret.Ret)) suite.Require().Nil(err) - ret, err = invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "Appchains") + ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Appchains") suite.Require().Nil(err) suite.Require().True(ret.IsSuccess()) - rec, err := invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "CountAppchains") + rec, err := invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "CountAppchains") suite.Require().Nil(err) suite.Require().True(ret.IsSuccess()) num, err := strconv.Atoi(string(rec.Ret)) @@ -101,7 +101,7 @@ func (suite *RegisterAppchain) TestFetchAppchains() { result := gjson.Parse(string(ret.Ret)) suite.Require().GreaterOrEqual(num, len(result.Array())) - ret, err = invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "CountApprovedAppchains") + ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "CountApprovedAppchains") suite.Require().Nil(err) suite.Require().True(ret.IsSuccess()) num, err = strconv.Atoi(string(ret.Ret)) diff --git a/tester/case003_interchain_test.go b/tester/case003_interchain_test.go index adf8328..f49b7ae 100644 --- a/tester/case003_interchain_test.go +++ b/tester/case003_interchain_test.go @@ -35,7 +35,7 @@ func (suite *Interchain) TestHandleIBTP() { pub2, err := k2.PublicKey().Bytes() suite.Require().Nil(err) - ret, err := invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "Register", + ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", pb.String(""), pb.Int32(0), pb.String("hyperchain"), @@ -47,7 +47,7 @@ func (suite *Interchain) TestHandleIBTP() { suite.Require().Nil(err) suite.Require().True(ret.IsSuccess(), string(ret.Ret)) - ret, err = invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "Register", + ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register", pb.String(""), pb.Int32(0), pb.String("fabric"), @@ -97,7 +97,7 @@ func (suite *Interchain) TestGetIBTPByID() { confByte, err := ioutil.ReadFile("./test_data/validator") suite.Require().Nil(err) - ret, err := invokeBVMContract(suite.api, k1, constant.InterchainContractAddr.Address(), "Register", + ret, err := invokeBVMContract(suite.api, k1, constant.AppchainMgrContractAddr.Address(), "Register", pb.String(string(confByte)), pb.Int32(0), pb.String("hyperchain"), @@ -109,7 +109,7 @@ func (suite *Interchain) TestGetIBTPByID() { suite.Require().Nil(err) suite.Require().True(ret.IsSuccess(), string(ret.Ret)) - ret, err = invokeBVMContract(suite.api, k2, constant.InterchainContractAddr.Address(), "Register", + ret, err = invokeBVMContract(suite.api, k2, constant.AppchainMgrContractAddr.Address(), "Register", pb.String(""), pb.Int32(0), pb.String("fabric"), @@ -161,7 +161,7 @@ func (suite *Interchain) TestAudit() { k, err := ecdsa.GenerateKey(ecdsa.Secp256r1) suite.Require().Nil(err) - ret, err := invokeBVMContract(suite.api, k, constant.InterchainContractAddr.Address(), "Audit", + ret, err := invokeBVMContract(suite.api, k, constant.AppchainMgrContractAddr.Address(), "Audit", pb.String("0x123"), pb.Int32(1), pb.String("通过"), diff --git a/tester/case004_role_test.go b/tester/case004_role_test.go index 7d319a9..96c9c44 100644 --- a/tester/case004_role_test.go +++ b/tester/case004_role_test.go @@ -30,7 +30,7 @@ func (suite *Role) SetupSuite() { func (suite *Role) TestGetRole() { pubKey, err := suite.pubKey.Bytes() suite.Assert().Nil(err) - _, err = invokeBVMContract(suite.api, suite.privKey, constant.InterchainContractAddr.Address(), "Register", + _, err = invokeBVMContract(suite.api, suite.privKey, constant.AppchainMgrContractAddr.Address(), "Register", pb.String(""), pb.Int32(0), pb.String("hyperchain"),