adjust unit tests for did
This commit is contained in:
parent
55f64f4142
commit
c2e5c5ea96
|
@ -289,12 +289,6 @@ func (am *AppchainManager) Appchains() *boltvm.Response {
|
|||
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())
|
||||
}
|
||||
|
||||
// GetAppchain returns appchain info by appchain id
|
||||
func (am *AppchainManager) GetAppchain(id string) *boltvm.Response {
|
||||
am.AppchainManager.Persister = am.Stub
|
||||
|
|
|
@ -2,8 +2,10 @@ package contracts
|
|||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -24,7 +26,16 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var caller = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
var (
|
||||
caller = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
appchainMethod = "did:bitxhub:appchain1:."
|
||||
appchainMethod2 = "did:bitxhub:appchain2:."
|
||||
appchainAdminDID = "did:bitxhub:appchain1:0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
relayAdminDID = "did:bitxhub:relay:0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
docAddr = "/ipfs/QmQVxzUqN2Yv2UHUQXYwH8dSNkM8ReJ9qPqwJsf8zzoNUi"
|
||||
docHash = "QmQVxzUqN2Yv2UHUQXYwH8dSNkM8ReJ9qPqwJsf8zzoNUi"
|
||||
fakeSig = []byte("fake signature")
|
||||
)
|
||||
|
||||
func TestAppchainManager_Appchain(t *testing.T) {
|
||||
mockCtl := gomock.NewController(t)
|
||||
|
@ -50,18 +61,18 @@ func TestAppchainManager_Appchain(t *testing.T) {
|
|||
o1 := mockStub.EXPECT().Caller().Return(addr0)
|
||||
o2 := mockStub.EXPECT().Caller().Return(addr1)
|
||||
gomock.InOrder(o1, o2)
|
||||
mockStub.EXPECT().Get("appchain-"+addr0).Return(true, data)
|
||||
mockStub.EXPECT().Get("appchain-"+addr1).Return(false, nil)
|
||||
mockStub.EXPECT().Get("appchain-"+appchainMethod).Return(true, data)
|
||||
mockStub.EXPECT().Get("appchain-"+appchainMethod2).Return(false, nil)
|
||||
|
||||
am := &AppchainManager{
|
||||
Stub: mockStub,
|
||||
}
|
||||
|
||||
res := am.Appchain()
|
||||
res := am.GetAppchain(appchainMethod)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
assert.Equal(t, data, res.Result)
|
||||
|
||||
res = am.Appchain()
|
||||
res = am.GetAppchain(appchainMethod2)
|
||||
assert.Equal(t, false, res.Ok)
|
||||
}
|
||||
|
||||
|
@ -103,6 +114,12 @@ func TestAppchainManager_Appchains(t *testing.T) {
|
|||
mockStub.EXPECT().Logger().Return(logger).AnyTimes()
|
||||
|
||||
// test for register
|
||||
mockStub.EXPECT().CrossInvoke(constant.InterchainContractAddr.String(), "Register", pb.String(appchainMethod)).Return(registerResponse)
|
||||
mockStub.EXPECT().CrossInvoke(constant.MethodRegistryContractAddr.String(), "Register",
|
||||
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(registerResponse)
|
||||
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(false).MaxTimes(3)
|
||||
am.Register(appchainAdminDID, appchainMethod, fakeSig, docAddr, docHash,
|
||||
chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
|
||||
mockStub.EXPECT().Get(gomock.Any()).Return(true, chainsData[0]).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).MaxTimes(3)
|
||||
|
@ -141,6 +158,42 @@ func TestAppchainManager_Appchains(t *testing.T) {
|
|||
func TestAppchainManager_Register(t *testing.T) {
|
||||
am, mockStub, chains, chainsData := prepare(t)
|
||||
|
||||
func TestApply(t *testing.T) {
|
||||
am, mockStub, _, _ := prepare(t)
|
||||
// test for Apply
|
||||
mockStub.EXPECT().Caller().Return(caller).AnyTimes()
|
||||
|
||||
approveRes := &boltvm.Response{
|
||||
Ok: true,
|
||||
Result: []byte("true"),
|
||||
}
|
||||
mockStub.EXPECT().CrossInvoke(constant.MethodRegistryContractAddr.String(), "Apply",
|
||||
pb.String(appchainAdminDID), pb.String(appchainMethod), pb.Bytes(fakeSig)).Return(approveRes)
|
||||
//mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Return(true).AnyTimes()
|
||||
|
||||
res := am.Apply(appchainAdminDID, appchainMethod, fakeSig)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
}
|
||||
|
||||
func TestAuditApply(t *testing.T) {
|
||||
am, mockStub, _, _ := prepare(t)
|
||||
// test for Apply
|
||||
mockStub.EXPECT().Caller().Return(caller).AnyTimes()
|
||||
|
||||
approveRes := &boltvm.Response{
|
||||
Ok: true,
|
||||
Result: []byte("true"),
|
||||
}
|
||||
mockStub.EXPECT().CrossInvoke(constant.MethodRegistryContractAddr.String(), "AuditApply",
|
||||
pb.String(relayAdminDID), pb.String(appchainMethod), pb.Int32(1), pb.Bytes(fakeSig)).Return(approveRes)
|
||||
mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "IsAdmin", gomock.Any()).Return(approveRes)
|
||||
|
||||
res := am.AuditApply(relayAdminDID, appchainMethod, 1, fakeSig)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
}
|
||||
|
||||
func TestAudit(t *testing.T) {
|
||||
am, mockStub, _, _ := prepare(t)
|
||||
logger := log.NewWithModule("contracts")
|
||||
|
||||
mockStub.EXPECT().Caller().Return(caller).AnyTimes()
|
||||
|
@ -255,6 +308,7 @@ func TestManageChain(t *testing.T) {
|
|||
mockStub.EXPECT().Caller().Return(caller).AnyTimes()
|
||||
mockStub.EXPECT().CurrentCaller().Return(caller).AnyTimes()
|
||||
mockStub.EXPECT().Has(gomock.Any()).Return(true).AnyTimes()
|
||||
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(true)
|
||||
mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()
|
||||
mockStub.EXPECT().Logger().Return(logger).AnyTimes()
|
||||
mockStub.EXPECT().Get(AppchainKey(caller)).Return(true, chainsData[0]).AnyTimes()
|
||||
|
@ -264,6 +318,9 @@ func TestManageChain(t *testing.T) {
|
|||
|
||||
// test UpdateAppchain
|
||||
res := am.UpdateAppchain(chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
|
||||
// test UpdateAppchain without register
|
||||
mockStub.EXPECT().GetObject(AppchainKey(appchainMethod), gomock.Any()).Return(true)
|
||||
res := am.UpdateAppchain(appchainMethod, chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
|
||||
chains[0].Name, chains[0].Desc, chains[0].Version, chains[0].PublicKey)
|
||||
assert.Equal(t, true, res.Ok, string(res.Result))
|
||||
// test FreezeAppchain
|
||||
|
@ -284,6 +341,18 @@ func TestManageChain_WithoutPermission(t *testing.T) {
|
|||
mockStub.EXPECT().CrossInvoke(constant.GovernanceContractAddr.String(), "SubmitProposal", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "CheckPermission", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Error("")).AnyTimes()
|
||||
|
||||
assert.Equal(t, false, res.Ok)
|
||||
// test UpdateAppchain with register
|
||||
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(true)
|
||||
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Do(
|
||||
func(key string, ret interface{}) bool {
|
||||
chain := ret.(*appchainMgr.Appchain)
|
||||
chain.Status = appchainMgr.APPROVED
|
||||
chain.PublicKey = chains[0].PublicKey
|
||||
assert.Equal(t, key, AppchainKey(appchainMethod))
|
||||
return true
|
||||
})
|
||||
res = am.UpdateAppchain(appchainMethod, chains[0].Validators, chains[0].ConsensusType, chains[0].ChainType,
|
||||
// test FreezeAppchain
|
||||
res := am.FreezeAppchain("addr")
|
||||
assert.Equal(t, false, res.Ok, string(res.Result))
|
||||
|
@ -354,6 +423,11 @@ func TestDeleteAppchain(t *testing.T) {
|
|||
mockStub.EXPECT().CrossInvoke(constant.InterchainContractAddr.String(), "DeleteInterchain",
|
||||
gomock.Any()).Return(approveRes).AnyTimes()
|
||||
mockStub.EXPECT().Delete(AppchainKey(caller)).Return()
|
||||
gomock.Any()).Return(approveRes)
|
||||
mockStub.EXPECT().CrossInvoke(constant.MethodRegistryContractAddr.String(), "Delete",
|
||||
gomock.Any(), gomock.Any(), gomock.Any()).Return(approveRes)
|
||||
mockStub.EXPECT().GetObject(AppchainKey(appchainMethod), gomock.Any()).Return(true)
|
||||
mockStub.EXPECT().Delete(AppchainKey(appchainMethod)).Return()
|
||||
|
||||
// judge caller type error
|
||||
res := am.DeleteAppchain(caller)
|
||||
|
@ -366,23 +440,24 @@ func TestDeleteAppchain(t *testing.T) {
|
|||
assert.Equal(t, false, res.Ok)
|
||||
|
||||
res = am.DeleteAppchain(caller)
|
||||
res := am.DeleteAppchain(relayAdminDID, appchainMethod, fakeSig)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
}
|
||||
|
||||
func TestGetPubKeyByChainID(t *testing.T) {
|
||||
am, mockStub, chains, _ := prepare(t)
|
||||
// test for GetPubKeyByChainID
|
||||
mockStub.EXPECT().Has(AppchainKey(caller)).Return(true)
|
||||
mockStub.EXPECT().Has(AppchainKey(appchainMethod)).Return(true)
|
||||
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Do(
|
||||
func(key string, ret interface{}) bool {
|
||||
chain := ret.(*appchainMgr.Appchain)
|
||||
chain.Status = appchainMgr.AppchainAvailable
|
||||
chain.PublicKey = chains[0].PublicKey
|
||||
assert.Equal(t, key, AppchainKey(caller))
|
||||
assert.Equal(t, key, AppchainKey(appchainMethod))
|
||||
fmt.Printf("chain is %v", chain)
|
||||
return true
|
||||
}).AnyTimes()
|
||||
res := am.GetPubKeyByChainID(caller)
|
||||
res := am.GetPubKeyByChainID(appchainMethod)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
assert.Equal(t, chains[0].PublicKey, string(res.Result))
|
||||
}
|
||||
|
@ -429,50 +504,51 @@ func TestInterchainManager_Register(t *testing.T) {
|
|||
addr := types.NewAddress([]byte{0}).String()
|
||||
//mockStub.EXPECT().Caller().Return(addr).AnyTimes()
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(false, nil)
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(false, nil)
|
||||
|
||||
interchain := pb.Interchain{
|
||||
ID: addr,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
interchain.InterchainCounter[addr] = 1
|
||||
interchain.ReceiptCounter[addr] = 1
|
||||
interchain.SourceReceiptCounter[addr] = 1
|
||||
interchain.InterchainCounter[appchainMethod] = 1
|
||||
interchain.ReceiptCounter[appchainMethod] = 1
|
||||
interchain.SourceReceiptCounter[appchainMethod] = 1
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(true, data0)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data0)
|
||||
|
||||
interchain = pb.Interchain{
|
||||
ID: addr,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
data1, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
o3 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(true, data1)
|
||||
o3 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data1)
|
||||
gomock.InOrder(o1, o2, o3)
|
||||
|
||||
im := &InterchainManager{mockStub}
|
||||
|
||||
res := im.Register(appchainMethod)
|
||||
res := im.Register(addr)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
|
||||
ic := &pb.Interchain{}
|
||||
err = ic.Unmarshal(res.Result)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, addr, ic.ID)
|
||||
assert.Equal(t, appchainMethod, ic.ID)
|
||||
assert.Equal(t, 0, len(ic.InterchainCounter))
|
||||
assert.Equal(t, 0, len(ic.ReceiptCounter))
|
||||
assert.Equal(t, 0, len(ic.SourceReceiptCounter))
|
||||
|
||||
res = im.Register(addr)
|
||||
res = im.Register(appchainMethod)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
assert.Equal(t, data0, res.Result)
|
||||
|
||||
res = im.Register(addr)
|
||||
res = im.Register(appchainMethod)
|
||||
assert.Equal(t, true, res.Ok)
|
||||
assert.Equal(t, data1, res.Result)
|
||||
}
|
||||
|
@ -484,7 +560,7 @@ func TestInterchainManager_Interchain(t *testing.T) {
|
|||
addr := types.NewAddress([]byte{0}).String()
|
||||
mockStub.EXPECT().Caller().Return(addr).AnyTimes()
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(false, nil)
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(false, nil)
|
||||
|
||||
interchain := pb.Interchain{
|
||||
ID: addr,
|
||||
|
@ -497,15 +573,15 @@ func TestInterchainManager_Interchain(t *testing.T) {
|
|||
interchain.SourceReceiptCounter[addr] = 1
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(true, data0)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data0)
|
||||
gomock.InOrder(o1, o2)
|
||||
|
||||
im := &InterchainManager{mockStub}
|
||||
|
||||
res := im.Interchain()
|
||||
res := im.Interchain(appchainMethod)
|
||||
assert.False(t, res.Ok)
|
||||
|
||||
res = im.Interchain()
|
||||
res = im.Interchain(appchainMethod)
|
||||
assert.True(t, res.Ok)
|
||||
assert.Equal(t, data0, res.Result)
|
||||
}
|
||||
|
@ -514,30 +590,29 @@ func TestInterchainManager_GetInterchain(t *testing.T) {
|
|||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
addr := types.NewAddress([]byte{0}).String()
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(false, nil)
|
||||
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(false, nil)
|
||||
|
||||
interchain := pb.Interchain{
|
||||
ID: addr,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
interchain.InterchainCounter[addr] = 1
|
||||
interchain.ReceiptCounter[addr] = 1
|
||||
interchain.SourceReceiptCounter[addr] = 1
|
||||
interchain.InterchainCounter[appchainMethod] = 1
|
||||
interchain.ReceiptCounter[appchainMethod] = 1
|
||||
interchain.SourceReceiptCounter[appchainMethod] = 1
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(true, data0)
|
||||
o2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data0)
|
||||
gomock.InOrder(o1, o2)
|
||||
|
||||
im := &InterchainManager{mockStub}
|
||||
|
||||
res := im.GetInterchain(addr)
|
||||
res := im.GetInterchain(appchainMethod)
|
||||
assert.False(t, res.Ok)
|
||||
|
||||
res = im.GetInterchain(addr)
|
||||
res = im.GetInterchain(appchainMethod)
|
||||
assert.True(t, res.Ok)
|
||||
assert.Equal(t, data0, res.Result)
|
||||
}
|
||||
|
@ -546,29 +621,45 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
|
|||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
fromPrivKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
||||
assert.Nil(t, err)
|
||||
fromPubKey := fromPrivKey.PublicKey()
|
||||
from, err := fromPubKey.Address()
|
||||
assert.Nil(t, err)
|
||||
rawFromPubKeyBytes, err := fromPubKey.Bytes()
|
||||
assert.Nil(t, err)
|
||||
fromPubKeyBytes := base64.StdEncoding.EncodeToString(rawFromPubKeyBytes)
|
||||
|
||||
toPrivKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
||||
assert.Nil(t, err)
|
||||
toPubKey := toPrivKey.PublicKey()
|
||||
to, err := toPubKey.Address()
|
||||
assert.Nil(t, err)
|
||||
rawToPubKeyBytes, err := toPubKey.Bytes()
|
||||
assert.Nil(t, err)
|
||||
toPubKeyBytes := base64.StdEncoding.EncodeToString(rawToPubKeyBytes)
|
||||
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
f1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+from).Return(false, nil)
|
||||
f1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(false, nil)
|
||||
|
||||
interchain := pb.Interchain{
|
||||
ID: from,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
interchain.InterchainCounter[to] = 1
|
||||
interchain.ReceiptCounter[to] = 1
|
||||
interchain.SourceReceiptCounter[to] = 1
|
||||
interchain.InterchainCounter[appchainMethod2] = 1
|
||||
interchain.ReceiptCounter[appchainMethod2] = 1
|
||||
interchain.SourceReceiptCounter[appchainMethod2] = 1
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
|
||||
f2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+from).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+to).Return(true, data0).AnyTimes()
|
||||
f2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod2).Return(true, data0).AnyTimes()
|
||||
|
||||
appchain := &appchainMgr.Appchain{
|
||||
ID: "",
|
||||
ID: appchainMethod,
|
||||
Name: "Relay1",
|
||||
Validators: "",
|
||||
ConsensusType: "",
|
||||
|
@ -576,13 +667,27 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
|
|||
ChainType: "appchain",
|
||||
Desc: "Relay1",
|
||||
Version: "1",
|
||||
PublicKey: "",
|
||||
PublicKey: fromPubKeyBytes,
|
||||
}
|
||||
appchainData, err := json.Marshal(appchain)
|
||||
require.Nil(t, err)
|
||||
|
||||
dstAppchain := &appchainMgr.Appchain{
|
||||
ID: appchainMethod2,
|
||||
Name: "Relay2",
|
||||
Validators: "",
|
||||
ConsensusType: 0,
|
||||
ChainType: "appchain",
|
||||
Desc: "Relay2",
|
||||
Version: "1",
|
||||
PublicKey: toPubKeyBytes,
|
||||
}
|
||||
dstAppchainData, err := json.Marshal(dstAppchain)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// mockStub.EXPECT().IsRelayIBTP(gomock.Any()).Return(true).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Eq("GetAppchain"), gomock.Any()).Return(boltvm.Success(appchainData)).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Eq("GetAppchain"), pb.String(appchainMethod)).Return(boltvm.Success(appchainData)).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Eq("GetAppchain"), pb.String(appchainMethod2)).Return(boltvm.Success(dstAppchainData)).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Not("GetAppchain"), gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()
|
||||
mockStub.EXPECT().AddObject(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().GetTxIndex().Return(uint64(1)).AnyTimes()
|
||||
|
@ -593,7 +698,7 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
|
|||
im := &InterchainManager{mockStub}
|
||||
|
||||
ibtp := &pb.IBTP{
|
||||
From: from,
|
||||
From: appchainMethod,
|
||||
}
|
||||
|
||||
res := im.HandleIBTP(ibtp)
|
||||
|
@ -605,24 +710,23 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
|
|||
assert.Equal(t, "empty destination chain id", string(res.Result))
|
||||
|
||||
ibtp = &pb.IBTP{
|
||||
From: from,
|
||||
To: to,
|
||||
From: appchainMethod,
|
||||
To: appchainMethod2,
|
||||
Index: 0,
|
||||
Type: pb.IBTP_INTERCHAIN,
|
||||
Timestamp: 0,
|
||||
Proof: nil,
|
||||
Payload: nil,
|
||||
Version: "",
|
||||
Extra: nil,
|
||||
}
|
||||
mockStub.EXPECT().Caller().Return(ibtp.To)
|
||||
|
||||
mockStub.EXPECT().Caller().Return(from.String()).MaxTimes(7)
|
||||
ibtp.From = appchainMethod2
|
||||
res = im.HandleIBTP(ibtp)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "ibtp from != caller", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().Caller().Return(ibtp.From).MaxTimes(6)
|
||||
assert.Equal(t, true, strings.HasPrefix(string(res.Result), "caller is not bind to ibtp from"))
|
||||
|
||||
ibtp.From = appchainMethod
|
||||
res = im.HandleIBTP(ibtp)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "index already exists, required 2, but 0", string(res.Result))
|
||||
|
@ -646,10 +750,9 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
|
|||
ibtp.Type = pb.IBTP_RECEIPT_SUCCESS
|
||||
res = im.HandleIBTP(ibtp)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "ibtp to != caller", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().Caller().Return(ibtp.To).AnyTimes()
|
||||
assert.Equal(t, "caller is not bind to ibtp to", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().Caller().Return(to.String()).AnyTimes()
|
||||
res = im.HandleIBTP(ibtp)
|
||||
assert.True(t, res.Ok)
|
||||
|
||||
|
@ -667,9 +770,8 @@ func TestInterchainManager_GetIBTPByID(t *testing.T) {
|
|||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
|
||||
validID := fmt.Sprintf("%s-%s-1", from, to)
|
||||
validID := fmt.Sprintf("%s-%s-1", appchainMethod, appchainMethod2)
|
||||
|
||||
mockStub.EXPECT().Caller().Return(from).AnyTimes()
|
||||
im := &InterchainManager{mockStub}
|
||||
|
@ -680,14 +782,14 @@ func TestInterchainManager_GetIBTPByID(t *testing.T) {
|
|||
|
||||
res = im.GetIBTPByID("abc-def-10")
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "The caller does not have access to this ibtp", string(res.Result))
|
||||
assert.Equal(t, "invalid format of appchain method", string(res.Result))
|
||||
|
||||
unexistId := fmt.Sprintf("%s-%s-10", from, to)
|
||||
unexistId := fmt.Sprintf("%s-%s-10", appchainMethod, appchainMethod2)
|
||||
mockStub.EXPECT().GetObject(fmt.Sprintf("index-tx-%s", unexistId), gomock.Any()).Return(false)
|
||||
|
||||
res = im.GetIBTPByID(unexistId)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "this id is not existed", string(res.Result))
|
||||
assert.Equal(t, "this ibtp id is not existed", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().GetObject(fmt.Sprintf("index-tx-%s", validID), gomock.Any()).Return(true)
|
||||
res = im.GetIBTPByID(validID)
|
||||
|
@ -698,36 +800,62 @@ func TestInterchainManager_HandleIBTPs(t *testing.T) {
|
|||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
interchain := pb.Interchain{
|
||||
ID: caller,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
interchain.InterchainCounter[to] = 1
|
||||
interchain.ReceiptCounter[to] = 1
|
||||
interchain.SourceReceiptCounter[to] = 1
|
||||
interchain.InterchainCounter[appchainMethod2] = 1
|
||||
interchain.ReceiptCounter[appchainMethod2] = 1
|
||||
interchain.SourceReceiptCounter[appchainMethod2] = 1
|
||||
|
||||
mockStub.EXPECT().Caller().Return(caller).AnyTimes()
|
||||
mockStub.EXPECT().Has(gomock.Any()).Return(true).AnyTimes()
|
||||
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Do(
|
||||
func(key string, ret interface{}) bool {
|
||||
assert.Equal(t, key, AppchainKey(caller))
|
||||
assert.Equal(t, key, AppchainKey(appchainMethod))
|
||||
meta := ret.(*pb.Interchain)
|
||||
meta.ID = caller
|
||||
meta.ID = appchainMethod
|
||||
meta.SourceReceiptCounter = interchain.SourceReceiptCounter
|
||||
meta.ReceiptCounter = interchain.InterchainCounter
|
||||
meta.InterchainCounter = interchain.InterchainCounter
|
||||
return true
|
||||
}).AnyTimes()
|
||||
|
||||
fromPrivKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
||||
assert.Nil(t, err)
|
||||
fromPubKey := fromPrivKey.PublicKey()
|
||||
from, err := fromPubKey.Address()
|
||||
assert.Nil(t, err)
|
||||
rawFromPubKeyBytes, err := fromPubKey.Bytes()
|
||||
assert.Nil(t, err)
|
||||
fromPubKeyBytes := base64.StdEncoding.EncodeToString(rawFromPubKeyBytes)
|
||||
|
||||
appchain := &appchainMgr.Appchain{
|
||||
ID: appchainMethod,
|
||||
Name: "Relay1",
|
||||
Validators: "",
|
||||
ConsensusType: 0,
|
||||
ChainType: "appchain",
|
||||
Desc: "Relay1",
|
||||
Version: "1",
|
||||
PublicKey: fromPubKeyBytes,
|
||||
}
|
||||
appchainData, err := json.Marshal(appchain)
|
||||
require.Nil(t, err)
|
||||
|
||||
mockStub.EXPECT().Caller().Return(from.String()).AnyTimes()
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
f1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(false, nil)
|
||||
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
|
||||
f2 := mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod2).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(appchainMethod)).Return(boltvm.Success(appchainData)).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()
|
||||
appchain := &appchainMgr.Appchain{
|
||||
ID: "",
|
||||
Name: "Relay1",
|
||||
|
@ -754,8 +882,8 @@ func TestInterchainManager_HandleIBTPs(t *testing.T) {
|
|||
im := &InterchainManager{mockStub}
|
||||
|
||||
ibtp := &pb.IBTP{
|
||||
From: caller,
|
||||
To: to,
|
||||
From: appchainMethod,
|
||||
To: appchainMethod2,
|
||||
Index: 2,
|
||||
Type: pb.IBTP_INTERCHAIN,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
|
@ -774,6 +902,8 @@ func TestInterchainManager_HandleIBTPs(t *testing.T) {
|
|||
data, err := ibtps.Marshal()
|
||||
assert.Nil(t, err)
|
||||
res := im.HandleIBTPs(data)
|
||||
//fmt.Printf("result is %v", string(res.Result))
|
||||
assert.Equal(t, true, res.Ok)
|
||||
fmt.Printf("result is %v", string(res.Result))
|
||||
assert.Equal(t, true, res.Ok, string(res.Result))
|
||||
}
|
||||
|
@ -783,24 +913,26 @@ func TestInterchainManager_HandleUnionIBTP(t *testing.T) {
|
|||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().Has(gomock.Any()).Return(true).AnyTimes()
|
||||
|
||||
interchain := pb.Interchain{
|
||||
ID: from,
|
||||
ID: appchainMethod,
|
||||
InterchainCounter: make(map[string]uint64),
|
||||
ReceiptCounter: make(map[string]uint64),
|
||||
SourceReceiptCounter: make(map[string]uint64),
|
||||
}
|
||||
interchain.InterchainCounter[to] = 1
|
||||
interchain.ReceiptCounter[to] = 1
|
||||
interchain.SourceReceiptCounter[to] = 1
|
||||
interchain.InterchainCounter[appchainMethod2] = 1
|
||||
interchain.ReceiptCounter[appchainMethod2] = 1
|
||||
interchain.SourceReceiptCounter[appchainMethod2] = 1
|
||||
data0, err := interchain.Marshal()
|
||||
assert.Nil(t, err)
|
||||
|
||||
relayChain := &appchainMgr.Appchain{
|
||||
Status: appchainMgr.APPROVED,
|
||||
ID: appchainMethod,
|
||||
Name: "appchain" + appchainMethod,
|
||||
Status: appchainMgr.AppchainAvailable,
|
||||
ID: from,
|
||||
Name: "appchain" + from,
|
||||
|
@ -832,6 +964,7 @@ func TestInterchainManager_HandleUnionIBTP(t *testing.T) {
|
|||
data, err := json.Marshal(relayChain)
|
||||
assert.Nil(t, err)
|
||||
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+appchainMethod+"-"+appchainMethod).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+from).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().Get(appchainMgr.PREFIX+from+"-"+from).Return(true, data0).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(data)).AnyTimes()
|
||||
|
@ -843,8 +976,8 @@ func TestInterchainManager_HandleUnionIBTP(t *testing.T) {
|
|||
im := &InterchainManager{mockStub}
|
||||
|
||||
ibtp := &pb.IBTP{
|
||||
From: from + "-" + from,
|
||||
To: to,
|
||||
From: appchainMethod + "-" + appchainMethod,
|
||||
To: appchainMethod2,
|
||||
Index: 0,
|
||||
Type: pb.IBTP_INTERCHAIN,
|
||||
Timestamp: 0,
|
||||
|
@ -854,7 +987,7 @@ func TestInterchainManager_HandleUnionIBTP(t *testing.T) {
|
|||
Extra: nil,
|
||||
}
|
||||
|
||||
mockStub.EXPECT().Caller().Return(ibtp.From).AnyTimes()
|
||||
mockStub.EXPECT().Caller().Return(from).AnyTimes()
|
||||
|
||||
res := im.handleUnionIBTP(ibtp)
|
||||
assert.False(t, res.Ok)
|
||||
|
@ -1413,13 +1546,9 @@ func TestTransactionManager_BeginMultiTXs(t *testing.T) {
|
|||
func TestAssetExchange_Init(t *testing.T) {
|
||||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
|
||||
ae := &AssetExchange{mockStub}
|
||||
|
||||
res := ae.Init(from, to, []byte{1})
|
||||
res := ae.Init(appchainMethod, appchainMethod2, []byte{1})
|
||||
assert.False(t, res.Ok)
|
||||
|
||||
aei := pb.AssetExchangeInfo{
|
||||
|
@ -1435,12 +1564,12 @@ func TestAssetExchange_Init(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
|
||||
mockStub.EXPECT().Has(AssetExchangeKey(aei.Id)).Return(true).MaxTimes(1)
|
||||
res = ae.Init(from, to, info)
|
||||
res = ae.Init(appchainMethod, appchainMethod2, info)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "asset exhcange id already exists", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().Has(AssetExchangeKey(aei.Id)).Return(false).AnyTimes()
|
||||
res = ae.Init(from, to, info)
|
||||
res = ae.Init(appchainMethod, appchainMethod2, info)
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "illegal asset exchange info", string(res.Result))
|
||||
|
||||
|
@ -1449,13 +1578,13 @@ func TestAssetExchange_Init(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
|
||||
aer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeInit,
|
||||
Info: aei,
|
||||
}
|
||||
mockStub.EXPECT().SetObject(AssetExchangeKey(aei.Id), aer)
|
||||
res = ae.Init(from, to, info)
|
||||
res = ae.Init(appchainMethod, appchainMethod2, info)
|
||||
assert.True(t, res.Ok)
|
||||
}
|
||||
|
||||
|
@ -1463,9 +1592,6 @@ func TestAssetExchange_Redeem(t *testing.T) {
|
|||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
|
||||
ae := &AssetExchange{mockStub}
|
||||
|
||||
aei := pb.AssetExchangeInfo{
|
||||
|
@ -1479,36 +1605,36 @@ func TestAssetExchange_Redeem(t *testing.T) {
|
|||
}
|
||||
|
||||
aer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeRedeem,
|
||||
Info: aei,
|
||||
}
|
||||
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).Return(false).MaxTimes(1)
|
||||
res := ae.Redeem(from, to, []byte(aei.Id))
|
||||
res := ae.Redeem(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "asset exchange record does not exist", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).SetArg(1, aer).Return(true).MaxTimes(1)
|
||||
res = ae.Redeem(from, to, []byte(aei.Id))
|
||||
res = ae.Redeem(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "asset exchange status for this id is not 'Init'", string(res.Result))
|
||||
|
||||
aer.Status = AssetExchangeInit
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).SetArg(1, aer).Return(true).AnyTimes()
|
||||
res = ae.Redeem(to, to, []byte(aei.Id))
|
||||
res = ae.Redeem(appchainMethod2, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, fmt.Sprintf("invalid participator of asset exchange id %s", aei.Id), string(res.Result))
|
||||
|
||||
expAer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeRedeem,
|
||||
Info: aei,
|
||||
}
|
||||
mockStub.EXPECT().SetObject(AssetExchangeKey(aei.Id), expAer)
|
||||
res = ae.Redeem(from, to, []byte(aei.Id))
|
||||
res = ae.Redeem(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.True(t, res.Ok)
|
||||
}
|
||||
|
||||
|
@ -1516,9 +1642,6 @@ func TestAssetExchange_Refund(t *testing.T) {
|
|||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
|
||||
ae := &AssetExchange{mockStub}
|
||||
|
||||
aei := pb.AssetExchangeInfo{
|
||||
|
@ -1532,36 +1655,36 @@ func TestAssetExchange_Refund(t *testing.T) {
|
|||
}
|
||||
|
||||
aer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeRedeem,
|
||||
Info: aei,
|
||||
}
|
||||
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).Return(false).MaxTimes(1)
|
||||
res := ae.Refund(from, to, []byte(aei.Id))
|
||||
res := ae.Refund(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "asset exchange record does not exist", string(res.Result))
|
||||
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).SetArg(1, aer).Return(true).MaxTimes(1)
|
||||
res = ae.Refund(from, to, []byte(aei.Id))
|
||||
res = ae.Refund(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, "asset exchange status for this id is not 'Init'", string(res.Result))
|
||||
|
||||
aer.Status = AssetExchangeInit
|
||||
mockStub.EXPECT().GetObject(AssetExchangeKey(aei.Id), gomock.Any()).SetArg(1, aer).Return(true).AnyTimes()
|
||||
res = ae.Refund(to, to, []byte(aei.Id))
|
||||
res = ae.Refund(appchainMethod2, appchainMethod2, []byte(aei.Id))
|
||||
assert.False(t, res.Ok)
|
||||
assert.Equal(t, fmt.Sprintf("invalid participator of asset exchange id %s", aei.Id), string(res.Result))
|
||||
|
||||
expAer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeRefund,
|
||||
Info: aei,
|
||||
}
|
||||
mockStub.EXPECT().SetObject(AssetExchangeKey(aei.Id), expAer)
|
||||
res = ae.Refund(from, to, []byte(aei.Id))
|
||||
res = ae.Refund(appchainMethod, appchainMethod2, []byte(aei.Id))
|
||||
assert.True(t, res.Ok)
|
||||
}
|
||||
|
||||
|
@ -1579,11 +1702,9 @@ func TestAssetExchange_GetStatus(t *testing.T) {
|
|||
AssetOnDst: 100,
|
||||
}
|
||||
|
||||
from := types.NewAddress([]byte{0}).String()
|
||||
to := types.NewAddress([]byte{1}).String()
|
||||
aer := AssetExchangeRecord{
|
||||
Chain0: from,
|
||||
Chain1: to,
|
||||
Chain0: appchainMethod,
|
||||
Chain1: appchainMethod2,
|
||||
Status: AssetExchangeRedeem,
|
||||
Info: aei,
|
||||
}
|
||||
|
@ -1633,8 +1754,8 @@ func TestInterRelayBroker_InvokeInterRelayContract(t *testing.T) {
|
|||
require.True(t, res.Ok)
|
||||
|
||||
ibtp := &pb.IBTP{
|
||||
From: "123",
|
||||
To: "123",
|
||||
From: appchainMethod,
|
||||
To: appchainMethod2,
|
||||
Type: pb.IBTP_INTERCHAIN,
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package contracts
|
|||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
@ -10,6 +11,7 @@ import (
|
|||
"github.com/meshplus/bitxhub-core/boltvm"
|
||||
"github.com/meshplus/bitxhub-kit/crypto"
|
||||
"github.com/meshplus/bitxhub-kit/crypto/asym"
|
||||
"github.com/meshplus/bitxhub-kit/crypto/asym/ecdsa"
|
||||
"github.com/meshplus/bitxhub-kit/types"
|
||||
"github.com/meshplus/bitxhub-model/constant"
|
||||
"github.com/meshplus/bitxhub-model/pb"
|
||||
|
@ -185,10 +187,19 @@ func (x *InterchainManager) checkIBTP(ibtp *pb.IBTP, interchain *pb.Interchain)
|
|||
x.Logger().WithField("chain_id", ibtp.To).Debug("target appchain does not exist")
|
||||
}
|
||||
|
||||
srcChainInfo, err := x.getAppchainInfo(ibtp.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pb.IBTP_INTERCHAIN == ibtp.Type ||
|
||||
pb.IBTP_ASSET_EXCHANGE_INIT == ibtp.Type ||
|
||||
pb.IBTP_ASSET_EXCHANGE_REDEEM == ibtp.Type ||
|
||||
pb.IBTP_ASSET_EXCHANGE_REFUND == ibtp.Type {
|
||||
if srcChainInfo.ChainType != appchainMgr.RelaychainType {
|
||||
if err := x.checkPubKeyAndCaller(srcChainInfo.PublicKey); err != nil {
|
||||
return fmt.Errorf("caller is not bind to ibtp from: %w", err)
|
||||
}
|
||||
}
|
||||
idx := interchain.InterchainCounter[ibtp.To]
|
||||
if ibtp.Index <= idx {
|
||||
return fmt.Errorf(fmt.Sprintf("index already exists, required %d, but %d", idx+1, ibtp.Index))
|
||||
|
@ -197,6 +208,15 @@ func (x *InterchainManager) checkIBTP(ibtp *pb.IBTP, interchain *pb.Interchain)
|
|||
return fmt.Errorf(fmt.Sprintf("wrong index, required %d, but %d", idx+1, ibtp.Index))
|
||||
}
|
||||
} else {
|
||||
if srcChainInfo.ChainType != appchainMgr.RelaychainType {
|
||||
destChainInfo, err := x.getAppchainInfo(ibtp.To)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := x.checkPubKeyAndCaller(destChainInfo.PublicKey); err != nil {
|
||||
return fmt.Errorf("caller is not bind to ibtp to")
|
||||
}
|
||||
}
|
||||
idx := interchain.ReceiptCounter[ibtp.To]
|
||||
if ibtp.Index <= idx {
|
||||
return fmt.Errorf(fmt.Sprintf("receipt index already exists, required %d, but %d", idx+1, ibtp.Index))
|
||||
|
@ -210,6 +230,25 @@ func (x *InterchainManager) checkIBTP(ibtp *pb.IBTP, interchain *pb.Interchain)
|
|||
return nil
|
||||
}
|
||||
|
||||
func (x *InterchainManager) checkPubKeyAndCaller(pub string) error {
|
||||
pubKeyBytes, err := base64.StdEncoding.DecodeString(pub)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pubKey, err := ecdsa.UnmarshalPublicKey(pubKeyBytes, crypto.Secp256k1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt registerd public key error: %w", err)
|
||||
}
|
||||
addr, err := pubKey.Address()
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt registerd public key error: %w", err)
|
||||
}
|
||||
if addr.String() != x.Caller() {
|
||||
return fmt.Errorf("chain pub key derived address != caller")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *InterchainManager) checkAppchain(id string) (*pb.Interchain, *appchainMgr.Appchain, error) {
|
||||
interchain, ok := x.getInterchain(id)
|
||||
if !ok {
|
||||
|
@ -234,13 +273,13 @@ func (x *InterchainManager) checkAppchain(id string) (*pb.Interchain, *appchainM
|
|||
}
|
||||
|
||||
// isRelayIBTP returns whether ibtp.from is relaychain type
|
||||
func (x *InterchainManager) isRelayIBTP(from string) bool {
|
||||
func (x *InterchainManager) getAppchainInfo(chainMethod string) (*appchainMgr.Appchain, error) {
|
||||
srcChain := &appchainMgr.Appchain{}
|
||||
res := x.CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(from))
|
||||
res := x.CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(chainMethod))
|
||||
if err := json.Unmarshal(res.Result, srcChain); err != nil {
|
||||
return false
|
||||
return nil, fmt.Errorf("unmarshal appchain info error: %w", err)
|
||||
}
|
||||
return srcChain.ChainType == appchainMgr.RelaychainType
|
||||
return srcChain, nil
|
||||
}
|
||||
|
||||
func (x *InterchainManager) ProcessIBTP(ibtp *pb.IBTP, interchain *pb.Interchain) {
|
||||
|
|
|
@ -31,8 +31,9 @@ import (
|
|||
|
||||
const (
|
||||
keyPassword = "bitxhub"
|
||||
srcMethod = "did:bitxhub:appchain1:."
|
||||
dstMethod = "did:bitxhub:appchain2:."
|
||||
from = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
to = "0x000018f7c3a6e5e4c0b877fe3e688ab08840b997"
|
||||
executorType = "serial"
|
||||
)
|
||||
|
||||
|
@ -182,9 +183,7 @@ func TestBlockExecutor_ApplyReadonlyTransactions(t *testing.T) {
|
|||
privKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
||||
assert.Nil(t, err)
|
||||
|
||||
addr, err := privKey.PublicKey().Address()
|
||||
assert.Nil(t, err)
|
||||
id := fmt.Sprintf("%s-%s-%d", addr.String(), to, 1)
|
||||
id := fmt.Sprintf("%s-%s-%d", srcMethod, dstMethod, 1)
|
||||
|
||||
hash := types.NewHash([]byte{1})
|
||||
val, err := json.Marshal(hash)
|
||||
|
|
|
@ -20,10 +20,13 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
from = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b991"
|
||||
to = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b992"
|
||||
did = "did:bitxhub:appchain001:0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b992"
|
||||
other = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b993"
|
||||
srcMethod = "did:bitxhub:appchain1:."
|
||||
dstMethod = "did:bitxhub:appchain2:."
|
||||
otherMethod = "did:bitxhub:appchain3:."
|
||||
from = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b991"
|
||||
to = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b992"
|
||||
did = "did:bitxhub:appchain001:0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b992"
|
||||
other = "0x3f9d18f7c3a6e5e4c0b877fe3e688ab08840b993"
|
||||
)
|
||||
|
||||
func TestInterchainRouter_GetInterchainTxWrappers(t *testing.T) {
|
||||
|
@ -36,7 +39,7 @@ func TestInterchainRouter_GetInterchainTxWrappers(t *testing.T) {
|
|||
|
||||
m := make(map[string]*pb.Uint64Slice, 0)
|
||||
|
||||
m[to] = &pb.Uint64Slice{
|
||||
m[dstMethod] = &pb.Uint64Slice{
|
||||
Slice: []uint64{0},
|
||||
}
|
||||
im := &pb.InterchainMeta{
|
||||
|
@ -63,13 +66,13 @@ func TestInterchainRouter_GetInterchainTxWrappers(t *testing.T) {
|
|||
wrappersCh3 := make(chan *pb.InterchainTxWrappers, 1)
|
||||
wrappersCh4 := make(chan *pb.InterchainTxWrappers, 1)
|
||||
|
||||
err = router.GetInterchainTxWrappers(to, 1, 1, wrappersCh1)
|
||||
err = router.GetInterchainTxWrappers(dstMethod, 1, 1, wrappersCh1)
|
||||
require.Nil(t, err)
|
||||
err = router.GetInterchainTxWrappers(to, 2, 2, wrappersCh2)
|
||||
err = router.GetInterchainTxWrappers(dstMethod, 2, 2, wrappersCh2)
|
||||
require.NotNil(t, err)
|
||||
err = router.GetInterchainTxWrappers(to, 3, 3, wrappersCh3)
|
||||
err = router.GetInterchainTxWrappers(dstMethod, 3, 3, wrappersCh3)
|
||||
require.NotNil(t, err)
|
||||
err = router.GetInterchainTxWrappers(other, 1, 1, wrappersCh4)
|
||||
err = router.GetInterchainTxWrappers(otherMethod, 1, 1, wrappersCh4)
|
||||
require.Nil(t, err)
|
||||
|
||||
select {
|
||||
|
@ -126,7 +129,7 @@ func TestInterchainRouter_AddPier(t *testing.T) {
|
|||
isUnion := false
|
||||
router := testStartRouter(t)
|
||||
|
||||
interchainWrappersC, err := router.AddPier(did, to, isUnion)
|
||||
interchainWrappersC, err := router.AddPier(dstMethod, to, isUnion)
|
||||
require.Nil(t, err)
|
||||
|
||||
var txs []*pb.Transaction
|
||||
|
@ -138,7 +141,7 @@ func TestInterchainRouter_AddPier(t *testing.T) {
|
|||
|
||||
m := make(map[string]*pb.Uint64Slice, 0)
|
||||
|
||||
m[to] = &pb.Uint64Slice{
|
||||
m[dstMethod] = &pb.Uint64Slice{
|
||||
Slice: []uint64{0},
|
||||
}
|
||||
im := &pb.InterchainMeta{
|
||||
|
@ -157,7 +160,7 @@ func TestInterchainRouter_AddPier(t *testing.T) {
|
|||
require.Errorf(t, fmt.Errorf("not found interchainWrappers"), "")
|
||||
}
|
||||
|
||||
router.RemovePier(did, to, isUnion)
|
||||
router.RemovePier(dstMethod, to, isUnion)
|
||||
|
||||
require.Nil(t, router.Stop())
|
||||
}
|
||||
|
@ -166,7 +169,7 @@ func TestInterchainRouter_AddNonexistentPier(t *testing.T) {
|
|||
isUnion := false
|
||||
router := testStartRouter(t)
|
||||
|
||||
interchainWrappersC, err := router.AddPier(did, to, isUnion)
|
||||
interchainWrappersC, err := router.AddPier(srcMethod, to, isUnion)
|
||||
require.Nil(t, err)
|
||||
|
||||
var txs []*pb.Transaction
|
||||
|
@ -179,7 +182,7 @@ func TestInterchainRouter_AddNonexistentPier(t *testing.T) {
|
|||
m := make(map[string]*pb.Uint64Slice, 0)
|
||||
|
||||
// pier of other is not added
|
||||
m[other] = &pb.Uint64Slice{
|
||||
m[otherMethod] = &pb.Uint64Slice{
|
||||
Slice: []uint64{0},
|
||||
}
|
||||
im := &pb.InterchainMeta{
|
||||
|
@ -197,7 +200,7 @@ func TestInterchainRouter_AddNonexistentPier(t *testing.T) {
|
|||
require.Errorf(t, fmt.Errorf("not found interchainWrappers"), "")
|
||||
}
|
||||
|
||||
router.RemovePier(did, to, isUnion)
|
||||
router.RemovePier(srcMethod, to, isUnion)
|
||||
|
||||
require.Nil(t, router.Stop())
|
||||
}
|
||||
|
@ -206,7 +209,7 @@ func TestInterchainRouter_AddUnionPier(t *testing.T) {
|
|||
isUnion := true
|
||||
router := testStartRouter(t)
|
||||
|
||||
interchainWrappersC, err := router.AddPier(did, to, isUnion)
|
||||
interchainWrappersC, err := router.AddPier(srcMethod, to, isUnion)
|
||||
require.Nil(t, err)
|
||||
|
||||
var txs []*pb.Transaction
|
||||
|
@ -218,7 +221,7 @@ func TestInterchainRouter_AddUnionPier(t *testing.T) {
|
|||
|
||||
m := make(map[string]*pb.Uint64Slice, 0)
|
||||
|
||||
m[other] = &pb.Uint64Slice{
|
||||
m[otherMethod] = &pb.Uint64Slice{
|
||||
Slice: []uint64{0},
|
||||
}
|
||||
im := &pb.InterchainMeta{
|
||||
|
@ -237,7 +240,7 @@ func TestInterchainRouter_AddUnionPier(t *testing.T) {
|
|||
require.Errorf(t, fmt.Errorf("not found interchainWrappers"), "")
|
||||
}
|
||||
|
||||
router.RemovePier(did, to, isUnion)
|
||||
router.RemovePier(srcMethod, to, isUnion)
|
||||
|
||||
require.Nil(t, router.Stop())
|
||||
}
|
||||
|
@ -338,8 +341,8 @@ func mockIBTP(t *testing.T, index uint64, typ pb.IBTP_Type) *pb.IBTP {
|
|||
assert.Nil(t, err)
|
||||
|
||||
return &pb.IBTP{
|
||||
From: from,
|
||||
To: other,
|
||||
From: srcMethod,
|
||||
To: otherMethod,
|
||||
Payload: ibtppd,
|
||||
Index: index,
|
||||
Type: typ,
|
||||
|
|
Loading…
Reference in New Issue