refactor(*): use new Transaction/Address/Hash in executor and ledger

This commit is contained in:
zhourong 2020-10-21 13:50:17 +08:00
parent 840935f15c
commit 9a78f3f7f2
9 changed files with 72 additions and 56 deletions

4
go.mod
View File

@ -19,8 +19,8 @@ require (
github.com/libp2p/go-libp2p-core v0.5.6
github.com/magiconair/properties v1.8.1
github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20201016031620-9d2d859c4069
github.com/meshplus/bitxhub-kit v1.0.1-0.20201020090511-52fcd9cba5dc
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20201009112846-79d2e6ddf10d
github.com/meshplus/bitxhub-kit v1.1.1
github.com/meshplus/bitxhub-model v1.1.1
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.2.2

8
go.sum
View File

@ -579,10 +579,18 @@ github.com/meshplus/bitxhub-kit v1.0.1-0.20200914065214-5161497a783c h1:H5RvXIJK
github.com/meshplus/bitxhub-kit v1.0.1-0.20200914065214-5161497a783c/go.mod h1:Whtgcr25HOF6iJv0Ib5/BPnEXq9iNFO89j8JQkElISk=
github.com/meshplus/bitxhub-kit v1.0.1-0.20201020090511-52fcd9cba5dc h1:rB9K68YrxFS4R2JbweF2zDh8Vwk30U00YLgZuiUSUpY=
github.com/meshplus/bitxhub-kit v1.0.1-0.20201020090511-52fcd9cba5dc/go.mod h1:Whtgcr25HOF6iJv0Ib5/BPnEXq9iNFO89j8JQkElISk=
github.com/meshplus/bitxhub-kit v1.0.1-0.20201020110439-74ad3b22c086/go.mod h1:Mh3JQsgqthl8koBma9yeu9Wtqob1U1l0jFgLtVqhX+M=
github.com/meshplus/bitxhub-kit v1.0.1-0.20201020122917-96cb5c0ee687/go.mod h1:ZCtDTcNk8rs4Phzkj8SOeJDyDGcVOUlNL4iAqFBFgGo=
github.com/meshplus/bitxhub-kit v1.1.1 h1:vkPO88oA3+Kpc0N8lIgfj/U52KBuI+633hPbMYt1xm8=
github.com/meshplus/bitxhub-kit v1.1.1/go.mod h1:r4l4iqn0RPJreb/OmoYKfjCjQJrXpZX++6Qc31VG/1k=
github.com/meshplus/bitxhub-model v1.0.0-rc3/go.mod h1:ZCctQIYTlE3vJ8Lhkrgs9bWwNA+Dw4JzojOSIzLVU6E=
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200514093243-7e8ae60d1c19/go.mod h1:QK8aACbxtZEA3Hk1BOCirW0uxMWLsMrLDpWz9FweIKM=
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20201009112846-79d2e6ddf10d h1:q4Vig+IVhBvARLyeOsFw8gAdor0ajiBl6lGch1N65sk=
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20201009112846-79d2e6ddf10d/go.mod h1:QK8aACbxtZEA3Hk1BOCirW0uxMWLsMrLDpWz9FweIKM=
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20201020115514-3292017ce485/go.mod h1:QGCNACpZJ16GuSA9rudqG/9EQ1Gc18H7ztiFkU3RBZE=
github.com/meshplus/bitxhub-model v1.1.0/go.mod h1:QGCNACpZJ16GuSA9rudqG/9EQ1Gc18H7ztiFkU3RBZE=
github.com/meshplus/bitxhub-model v1.1.1 h1:/0Si29e14YW1GUbkJbCL8A70yXzxyiV/u36kxFC+gqI=
github.com/meshplus/bitxhub-model v1.1.1/go.mod h1:lUl9vPZXM9tP+B0ABRW/2eOW/6KCmjFTdoiTj5Vut/A=
github.com/meshplus/go-bitxhub-client v1.0.0-rc3/go.mod h1:FpiCyf6KhydcqthrHdvvPhbPIcD92b+Ju8T7WvQtSyM=
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54 h1:5Ip5AB7SxxQHg5SRtf2cCOI2wy1p75MQB12soPtPyf8=
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54/go.mod h1:G89UJaeqCQFxFdp8wzy1AdKfMtDEhpySau0pjDNeeaw=

View File

@ -14,7 +14,7 @@ const (
AssetExchangeContractAddr BoltContractAddress = "0x0000000000000000000000000000000000000010"
)
func (addr BoltContractAddress) Address() types.Address {
func (addr BoltContractAddress) Address() *types.Address {
return types.String2Address(string(addr))
}

View File

@ -20,8 +20,8 @@ func TestAppchainManager_Appchain(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addr0 := types.Address{0}.String()
addr1 := types.Address{1}.String()
addr0 := types.Bytes2Address([]byte{0}).String()
addr1 := types.Bytes2Address([]byte{1}).String()
chain := &appchainMgr.Appchain{
ID: addr0,
@ -62,7 +62,7 @@ func TestAppchainManager_Appchains(t *testing.T) {
var chains []*appchainMgr.Appchain
var chainsData [][]byte
for i := 0; i < 2; i++ {
addr := types.Address{byte(i)}.String()
addr := types.Bytes2Address([]byte{byte(i)}).String()
chain := &appchainMgr.Appchain{
ID: addr,
@ -109,7 +109,7 @@ func TestInterchainManager_Register(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addr := types.Address{}.String()
addr := types.Bytes2Address([]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)
@ -164,7 +164,7 @@ func TestInterchainManager_Interchain(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addr := types.Address{}.String()
addr := types.Bytes2Address([]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)
@ -197,7 +197,7 @@ func TestInterchainManager_GetInterchain(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addr := types.Address{}.String()
addr := types.Bytes2Address([]byte{0}).String()
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
o1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+addr).Return(false, nil)
@ -229,8 +229,8 @@ func TestInterchainManager_HandleIBTP(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
from := types.Address{0}.String()
to := types.Address{1}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
mockStub.EXPECT().Set(gomock.Any(), gomock.Any()).AnyTimes()
f1 := mockStub.EXPECT().Get(appchainMgr.PREFIX+from).Return(false, nil)
@ -362,8 +362,8 @@ func TestInterchainManager_GetIBTPByID(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
from := types.Address{}.String()
to := types.Address{byte(1)}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
validID := fmt.Sprintf("%s-%s-1", from, to)
@ -394,10 +394,10 @@ func TestRole_GetRole(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addrs := []string{types.Address{0}.String(), types.Address{1}.String()}
addrs := []string{types.Bytes2Address([]byte{0}).String(), types.Bytes2Address([]byte{1}).String()}
mockStub.EXPECT().GetObject(adminRolesKey, gomock.Any()).SetArg(1, addrs).AnyTimes()
mockStub.EXPECT().Caller().Return(types.Address{0}.String())
mockStub.EXPECT().Caller().Return(types.Bytes2Address([]byte{0}).String())
im := &Role{mockStub}
@ -405,7 +405,7 @@ func TestRole_GetRole(t *testing.T) {
assert.True(t, res.Ok)
assert.Equal(t, "admin", string(res.Result))
mockStub.EXPECT().Caller().Return(types.Address{2}.String()).AnyTimes()
mockStub.EXPECT().Caller().Return(types.Bytes2Address([]byte{2}).String()).AnyTimes()
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Error(""))
res = im.GetRole()
@ -423,7 +423,7 @@ func TestRole_IsAdmin(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addrs := []string{types.Address{0}.String(), types.Address{1}.String()}
addrs := []string{types.Bytes2Address([]byte{0}).String(), types.Bytes2Address([]byte{1}).String()}
mockStub.EXPECT().GetObject(adminRolesKey, gomock.Any()).SetArg(1, addrs).AnyTimes()
@ -433,7 +433,7 @@ func TestRole_IsAdmin(t *testing.T) {
assert.True(t, res.Ok)
assert.Equal(t, "true", string(res.Result))
res = im.IsAdmin(types.Address{2}.String())
res = im.IsAdmin(types.Bytes2Address([]byte{2}).String())
assert.True(t, res.Ok)
assert.Equal(t, "false", string(res.Result))
}
@ -442,7 +442,7 @@ func TestRole_GetAdminRoles(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addrs := []string{types.Address{0}.String(), types.Address{1}.String()}
addrs := []string{types.Bytes2Address([]byte{0}).String(), types.Bytes2Address([]byte{1}).String()}
mockStub.EXPECT().GetObject(adminRolesKey, gomock.Any()).SetArg(1, addrs).AnyTimes()
@ -464,7 +464,7 @@ func TestRole_SetAdminRoles(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
addrs := []string{types.Address{0}.String(), types.Address{1}.String()}
addrs := []string{types.Bytes2Address([]byte{0}).String(), types.Bytes2Address([]byte{1}).String()}
mockStub.EXPECT().SetObject(adminRolesKey, addrs).AnyTimes()
im := &Role{mockStub}
@ -480,8 +480,8 @@ func TestRuleManager_RegisterRule(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
id0 := types.Address{0}.String()
id1 := types.Address{1}.String()
id0 := types.Bytes2Address([]byte{0}).String()
id1 := types.Bytes2Address([]byte{1}).String()
mockStub.EXPECT().CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(id0)).Return(boltvm.Success(nil))
mockStub.EXPECT().CrossInvoke(constant.AppchainMgrContractAddr.String(), "GetAppchain", pb.String(id1)).Return(boltvm.Error(""))
@ -489,7 +489,7 @@ func TestRuleManager_RegisterRule(t *testing.T) {
im := &RuleManager{mockStub}
addr := types.Address{2}.String()
addr := types.Bytes2Address([]byte{2}).String()
res := im.RegisterRule(id0, addr)
assert.True(t, res.Ok)
@ -502,8 +502,8 @@ func TestRuleManager_GetRuleAddress(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
id0 := types.Address{0}.String()
id1 := types.Address{1}.String()
id0 := types.Bytes2Address([]byte{0}).String()
id1 := types.Bytes2Address([]byte{1}).String()
rule := Rule{
Address: "123",
Status: 1,
@ -531,8 +531,8 @@ func TestRuleManager_Audit(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
id0 := types.Address{0}.String()
id1 := types.Address{1}.String()
id0 := types.Bytes2Address([]byte{0}).String()
id1 := types.Bytes2Address([]byte{1}).String()
rule := Rule{
Address: "123",
Status: 1,
@ -591,7 +591,7 @@ func TestTransactionManager_Begin(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
id := types.Hash{}.String()
id := types.Bytes2Hash([]byte{0}).String()
mockStub.EXPECT().AddObject(fmt.Sprintf("%s-%s", PREFIX, id), pb.TransactionStatus_BEGIN)
im := &TransactionManager{mockStub}
@ -778,8 +778,8 @@ func TestAssetExchange_Init(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
from := types.Address{0}.String()
to := types.Address{1}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
ae := &AssetExchange{mockStub}
@ -827,8 +827,8 @@ func TestAssetExchange_Redeem(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
from := types.Address{0}.String()
to := types.Address{1}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
ae := &AssetExchange{mockStub}
@ -880,8 +880,8 @@ func TestAssetExchange_Refund(t *testing.T) {
mockCtl := gomock.NewController(t)
mockStub := mock_stub.NewMockStub(mockCtl)
from := types.Address{0}.String()
to := types.Address{1}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
ae := &AssetExchange{mockStub}
@ -943,8 +943,8 @@ func TestAssetExchange_GetStatus(t *testing.T) {
AssetOnDst: 100,
}
from := types.Address{0}.String()
to := types.Address{1}.String()
from := types.Bytes2Address([]byte{0}).String()
to := types.Bytes2Address([]byte{1}).String()
aer := AssetExchangeRecord{
Chain0: from,
Chain1: to,

View File

@ -382,14 +382,15 @@ func (x *InterchainManager) checkUnionIBTP(app *appchainMgr.Appchain, ibtp *pb.I
threshold := (len(validators.Addresses) - 1) / 3
counter := 0
hash := sha256.Sum256([]byte(ibtp.Hash().String()))
ibtpHash := ibtp.Hash()
hash := sha256.Sum256([]byte(ibtpHash.String()))
for v, sign := range signs.Sign {
if _, ok := m[v]; !ok {
return fmt.Errorf("wrong validator: %s", v)
}
delete(m, v)
addr := types.String2Address(v)
ok, _ := asym.Verify(crypto.Secp256k1, sign, hash[:], addr)
ok, _ := asym.Verify(crypto.Secp256k1, sign, hash[:], *addr)
if ok {
counter++
}

View File

@ -120,7 +120,7 @@ func (exec *BlockExecutor) buildTxMerkleTree(txs []*pb.Transaction) (types.Hash,
txHashes := make([]merkletree.Content, 0, len(txIndexes))
for _, txIndex := range txIndexes {
txHashes = append(txHashes, pb.TransactionHash(txs[txIndex].TransactionHash.Bytes()))
txHashes = append(txHashes, &txs[txIndex].TransactionHash)
}
hash, err := calcMerkleRoot(txHashes)
@ -137,7 +137,7 @@ func (exec *BlockExecutor) buildTxMerkleTree(txs []*pb.Transaction) (types.Hash,
txHashes := make([]merkletree.Content, 0, len(exec.txsExecutor.GetNormalTxs()))
for _, txHash := range exec.txsExecutor.GetNormalTxs() {
txHashes = append(txHashes, pb.TransactionHash(txHash.Bytes()))
txHashes = append(txHashes, &txHash)
}
hash, err := calcMerkleRoot(txHashes)
@ -160,7 +160,7 @@ func (exec *BlockExecutor) buildTxMerkleTree(txs []*pb.Transaction) (types.Hash,
contents := make([]merkletree.Content, 0, groupCnt)
for _, l2Root := range l2Roots {
contents = append(contents, pb.TransactionHash(l2Root.Bytes()))
contents = append(contents, &l2Root)
}
root, err := calcMerkleRoot(contents)
if err != nil {
@ -254,22 +254,27 @@ func (exec *BlockExecutor) postBlockEvent(block *pb.Block, interchainMeta *pb.In
}
func (exec *BlockExecutor) applyTransaction(i int, tx *pb.Transaction, opt *agency.TxOpt) ([]byte, error) {
if tx.Data == nil {
if tx.Payload == nil {
return nil, fmt.Errorf("empty transaction data")
}
switch tx.Data.Type {
data := &pb.TransactionData{}
if err := data.Unmarshal(tx.Payload); err != nil {
return nil, err
}
switch data.Type {
case pb.TransactionData_NORMAL:
err := exec.transfer(tx.From, tx.To, tx.Data.Amount)
err := exec.transfer(tx.From, tx.To, data.Amount)
return nil, err
default:
var instance vm.VM
switch tx.Data.VmType {
switch data.VmType {
case pb.TransactionData_BVM:
ctx := vm.NewContext(tx, uint64(i), tx.Data, exec.ledger, exec.logger)
ctx := vm.NewContext(tx, uint64(i), data, exec.ledger, exec.logger)
instance = boltvm.New(ctx, exec.validationEngine, exec.getContracts(opt))
case pb.TransactionData_XVM:
ctx := vm.NewContext(tx, uint64(i), tx.Data, exec.ledger, exec.logger)
ctx := vm.NewContext(tx, uint64(i), data, exec.ledger, exec.logger)
imports, err := wasm.EmptyImports()
if err != nil {
return nil, err
@ -282,7 +287,7 @@ func (exec *BlockExecutor) applyTransaction(i int, tx *pb.Transaction, opt *agen
return nil, fmt.Errorf("wrong vm type")
}
return instance.Run(tx.Data.Payload)
return instance.Run(data.Payload)
}
}
@ -313,7 +318,8 @@ func (exec *BlockExecutor) calcReceiptMerkleRoot(receipts []*pb.Receipt) (types.
receiptHashes := make([]merkletree.Content, 0, len(receipts))
for _, receipt := range receipts {
receiptHashes = append(receiptHashes, pb.TransactionHash(receipt.Hash().Bytes()))
hash := receipt.Hash()
receiptHashes = append(receiptHashes, &hash)
}
receiptRoot, err := calcMerkleRoot(receiptHashes)
if err != nil {
@ -335,7 +341,7 @@ func calcMerkleRoot(contents []merkletree.Content) (types.Hash, error) {
return types.Hash{}, err
}
return types.Bytes2Hash(tree.MerkleRoot()), nil
return *types.Bytes2Hash(tree.MerkleRoot()), nil
}
func (exec *BlockExecutor) getContracts(opt *agency.TxOpt) map[string]agency.Contract {

View File

@ -254,7 +254,8 @@ func (o *Account) getStateJournalAndComputeHash() map[string][]byte {
dirtyVal, _ := o.dirtyState.Load(key)
dirtyStateData = append(dirtyStateData, dirtyVal.([]byte)...)
}
o.dirtyStateHash = sha256.Sum256(dirtyStateData)
hash := sha256.Sum256(dirtyStateData)
o.dirtyStateHash = *types.Bytes2Hash(hash[:])
return prevStates
}
@ -272,7 +273,7 @@ func (o *Account) getDirtyData() []byte {
dirtyData = append(dirtyData, data...)
}
return append(dirtyData, o.dirtyStateHash[:]...)
return append(dirtyData, o.dirtyStateHash.Bytes()...)
}
func innerAccountChanged(account0 *innerAccount, account1 *innerAccount) bool {

View File

@ -28,7 +28,7 @@ func TestAccount_GetState(t *testing.T) {
h := hexutil.Encode(bytesutil.LeftPadBytes([]byte{11}, 20))
addr := types.String2Address(h)
account := newAccount(ledger.ldb, ledger.accountCache, addr)
account := newAccount(ledger.ldb, ledger.accountCache, *addr)
account.SetState([]byte("a"), []byte("b"))
ok, v := account.GetState([]byte("a"))

View File

@ -140,15 +140,15 @@ func (l *ChainLedger) FlushDirtyDataAndComputeJournal() (map[types.Address]*Acco
for _, addr := range sortedAddr {
dirtyAccountData = append(dirtyAccountData, accountData[addr]...)
}
dirtyAccountData = append(dirtyAccountData, l.prevJnlHash[:]...)
dirtyAccountData = append(dirtyAccountData, l.prevJnlHash.Bytes()...)
journalHash := sha256.Sum256(dirtyAccountData)
blockJournal := &BlockJournal{
Journals: journals,
ChangedHash: journalHash,
ChangedHash: *types.Bytes2Hash(journalHash[:]),
}
l.prevJnlHash = journalHash
l.prevJnlHash = blockJournal.ChangedHash
l.Clear()
l.accountCache.add(dirtyAccounts)