From 9dd327ab3f4e6fef57f387f6f8a943f8339a5869 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 28 Sep 2021 14:09:46 +0530 Subject: [PATCH 1/8] BodyExtrinsics instead of Body Create a new BodyExtrinsics struct with its own methods and use it instead of Body --- dot/core/interface.go | 2 +- dot/core/messages_test.go | 2 +- dot/core/mocks/block_state.go | 25 +---- dot/core/service.go | 14 +-- dot/core/service_test.go | 39 ++++--- dot/digest/digest_test.go | 2 +- dot/network/message.go | 2 +- dot/network/message_test.go | 6 +- dot/network/sync_test.go | 7 +- dot/network/test_helpers.go | 6 +- dot/rpc/modules/chain_test.go | 14 +-- dot/rpc/modules/state_test.go | 2 +- dot/rpc/modules/system_test.go | 2 +- dot/rpc/subscription/listeners_test.go | 5 +- dot/state/block.go | 14 ++- dot/state/block_data_test.go | 7 +- dot/state/block_test.go | 35 +++--- dot/state/storage_test.go | 5 +- dot/state/test_helpers.go | 13 ++- dot/sync/interface.go | 2 +- dot/sync/message_test.go | 2 +- dot/sync/syncer.go | 26 ++--- dot/sync/syncer_test.go | 6 +- dot/sync/test_helpers.go | 4 +- dot/types/block.go | 18 +-- dot/types/block_data.go | 5 +- dot/types/block_data_test.go | 9 +- dot/types/block_test.go | 26 ++--- dot/types/body.go | 12 +- dot/types/body_extrinsics.go | 147 +++++++++++++++++++++++++ lib/babe/build.go | 9 +- lib/babe/build_test.go | 11 +- lib/babe/median_test.go | 4 +- lib/grandpa/message_handler_test.go | 15 ++- lib/grandpa/message_test.go | 2 +- lib/grandpa/message_tracker_test.go | 4 +- lib/grandpa/network_test.go | 2 +- lib/grandpa/round_test.go | 2 +- lib/runtime/life/exports_test.go | 17 +-- lib/runtime/wasmer/exports_test.go | 18 +-- tests/stress/stress_test.go | 3 +- tests/utils/chain.go | 2 +- 42 files changed, 333 insertions(+), 215 deletions(-) create mode 100644 dot/types/body_extrinsics.go diff --git a/dot/core/interface.go b/dot/core/interface.go index 3cdeb0c326..8a12e728bf 100644 --- a/dot/core/interface.go +++ b/dot/core/interface.go @@ -48,7 +48,7 @@ type BlockState interface { UnregisterFinalisedChannel(id byte) HighestCommonAncestor(a, b common.Hash) (common.Hash, error) SubChain(start, end common.Hash) ([]common.Hash, error) - GetBlockBody(hash common.Hash) (*types.Body, error) + GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error GetRuntime(*common.Hash) (runtime.Instance, error) StoreRuntime(common.Hash, runtime.Instance) diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index b471014c1a..3e610896c8 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -103,7 +103,7 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) { ParentHash: s.blockState.BestBlockHash(), Digest: digest, }, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } expected := &network.BlockAnnounceMessage{ diff --git a/dot/core/mocks/block_state.go b/dot/core/mocks/block_state.go index 7686c23dab..5170f7cf0e 100644 --- a/dot/core/mocks/block_state.go +++ b/dot/core/mocks/block_state.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -6,7 +6,6 @@ import ( big "math/big" common "github.com/ChainSafe/gossamer/lib/common" - mock "github.com/stretchr/testify/mock" runtime "github.com/ChainSafe/gossamer/lib/runtime" @@ -176,15 +175,15 @@ func (_m *MockBlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash { } // GetBlockBody provides a mock function with given fields: hash -func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { +func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) { ret := _m.Called(hash) - var r0 *types.Body - if rf, ok := ret.Get(0).(func(common.Hash) *types.Body); ok { + var r0 *types.BodyExtrinsics + if rf, ok := ret.Get(0).(func(common.Hash) *types.BodyExtrinsics); ok { r0 = rf(hash) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*types.Body) + r0 = ret.Get(0).(*types.BodyExtrinsics) } } @@ -390,20 +389,6 @@ func (_m *MockBlockState) RegisterImportedChannel(ch chan<- *types.Block) (byte, return r0, r1 } -// SetFinalisedHash provides a mock function with given fields: _a0, _a1, _a2 -func (_m *MockBlockState) SetFinalisedHash(_a0 common.Hash, _a1 uint64, _a2 uint64) error { - ret := _m.Called(_a0, _a1, _a2) - - var r0 error - if rf, ok := ret.Get(0).(func(common.Hash, uint64, uint64) error); ok { - r0 = rf(_a0, _a1, _a2) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // StoreRuntime provides a mock function with given fields: _a0, _a1 func (_m *MockBlockState) StoreRuntime(_a0 common.Hash, _a1 runtime.Instance) { _m.Called(_a0, _a1) diff --git a/dot/core/service.go b/dot/core/service.go index b0890a4e76..7841c626b1 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -380,14 +380,9 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error { continue } - exts, err := body.AsExtrinsics() - if err != nil { - continue - } - // TODO: decode extrinsic and make sure it's not an inherent. // currently we are attempting to re-add inherents, causing lots of "'Bad input data provided to validate_transaction" errors. - for _, ext := range exts { + for _, ext := range *body { logger.Debug("validating transaction on re-org chain", "extrinsic", ext) encExt, err := scale.Marshal(ext) if err != nil { @@ -424,13 +419,8 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error { // and moves them to the queue if valid. // See https://github.com/paritytech/substrate/blob/74804b5649eccfb83c90aec87bdca58e5d5c8789/client/transaction-pool/src/lib.rs#L545 func (s *Service) maintainTransactionPool(block *types.Block) error { - exts, err := block.Body.AsExtrinsics() - if err != nil { - return err - } - // remove extrinsics included in a block - for _, ext := range exts { + for _, ext := range block.Body { s.transactionState.RemoveExtrinsic(ext) } diff --git a/dot/core/service_test.go b/dot/core/service_test.go index 5988da2393..49477e7236 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -26,7 +26,6 @@ import ( "time" "github.com/ChainSafe/gossamer/dot/core/mocks" - coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/sync" @@ -65,7 +64,7 @@ func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, dept Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))), Digest: types.NewDigest(), }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } previousHash = block.Header.Hash() @@ -107,7 +106,7 @@ func TestStartService(t *testing.T) { } func TestAnnounceBlock(t *testing.T) { - net := new(coremocks.MockNetwork) + net := new(mocks.MockNetwork) cfg := &Config{ Network: net, } @@ -128,7 +127,7 @@ func TestAnnounceBlock(t *testing.T) { ParentHash: s.blockState.BestBlockHash(), Digest: digest, }, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } expected := &network.BlockAnnounceMessage{ @@ -304,8 +303,6 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) { require.NoError(t, err) // build "re-org" chain - body, err := types.NewBodyFromExtrinsics([]types.Extrinsic{tx}) - require.NoError(t, err) digest := types.NewDigest() block := &types.Block{ @@ -314,7 +311,7 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) { Number: big.NewInt(0).Add(ancestor.Header.Number, big.NewInt(1)), Digest: digest, }, - Body: *body, + Body: types.BodyExtrinsics([]types.Extrinsic{tx}), } s.blockState.StoreRuntime(block.Header.Hash(), rt) @@ -378,7 +375,7 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) { } err := s.maintainTransactionPool(&types.Block{ - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), }) require.NoError(t, err) @@ -423,11 +420,8 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) { transactionState: ts, } - body, err := types.NewBodyFromExtrinsics([]types.Extrinsic{txs[0].Extrinsic}) - require.NoError(t, err) - - err = s.maintainTransactionPool(&types.Block{ - Body: *body, + err := s.maintainTransactionPool(&types.Block{ + Body: types.BodyExtrinsics([]types.Extrinsic{txs[0].Extrinsic}), }) require.NoError(t, err) @@ -510,21 +504,25 @@ func TestService_HandleRuntimeChanges(t *testing.T) { }) require.NoError(t, err) + body1 := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Old Runtime")}) newBlock1 := &types.Block{ Header: types.Header{ ParentHash: hash, Number: big.NewInt(1), Digest: types.NewDigest()}, - Body: *types.NewBody([]byte("Old Runtime")), + Body: *body1, } + body2 := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Updated Runtime")}) + require.NoError(t, err) + newBlockRTUpdate := &types.Block{ Header: types.Header{ ParentHash: hash, Number: big.NewInt(1), Digest: digest, }, - Body: *types.NewBody([]byte("Updated Runtime")), + Body: *body2, } ts, err := s.storageState.TrieState(nil) // Pass genesis root @@ -599,13 +597,14 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) { codeHashBefore := parentRt.GetCodeHash() blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution + body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Updated Runtime")}) newBlock := &types.Block{ Header: types.Header{ ParentHash: blockHash, Number: big.NewInt(1), Digest: types.NewDigest(), }, - Body: *types.NewBody([]byte("Updated Runtime")), + Body: *body, } err = s.handleCodeSubstitution(blockHash) @@ -648,7 +647,7 @@ func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -678,7 +677,7 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -703,7 +702,7 @@ func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -788,7 +787,7 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) diff --git a/dot/digest/digest_test.go b/dot/digest/digest_test.go index 39ac8896cb..51bf32dcf6 100644 --- a/dot/digest/digest_test.go +++ b/dot/digest/digest_test.go @@ -58,7 +58,7 @@ func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, dept Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } previousHash = block.Header.Hash() diff --git a/dot/network/message.go b/dot/network/message.go index a6671acaa1..18a563c9d3 100644 --- a/dot/network/message.go +++ b/dot/network/message.go @@ -326,7 +326,7 @@ func protobufToBlockData(pbd *pb.BlockData) (*types.BlockData, error) { } if pbd.Body != nil { - body, err := types.NewBodyFromEncodedBytes(pbd.Body) + body, err := types.NewBodyExtrinsicsFromEncodedBytes(pbd.Body) if err != nil { return nil, err } diff --git a/dot/network/message_test.go b/dot/network/message_test.go index aedcebb58a..0ae46fe56b 100644 --- a/dot/network/message_test.go +++ b/dot/network/message_test.go @@ -179,8 +179,7 @@ func TestEncodeBlockResponseMessage_WithBody(t *testing.T) { require.NoError(t, err) exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}} - body, err := types.NewBodyFromBytes(exts) - require.NoError(t, err) + body := types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) bd := &types.BlockData{ Hash: hash, @@ -226,8 +225,7 @@ func TestEncodeBlockResponseMessage_WithAll(t *testing.T) { require.NoError(t, err) exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}} - body, err := types.NewBodyFromBytes(exts) - require.NoError(t, err) + body := types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) bd := &types.BlockData{ Hash: hash, diff --git a/dot/network/sync_test.go b/dot/network/sync_test.go index c979c648c1..be26d9be80 100644 --- a/dot/network/sync_test.go +++ b/dot/network/sync_test.go @@ -90,9 +90,10 @@ func TestSyncQueue_PushResponse(t *testing.T) { testHeader := types.NewEmptyHeader() testHeader.Number = big.NewInt(int64(77 + i)) + body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0}}) msg.BlockData = append(msg.BlockData, &types.BlockData{ Header: testHeader, - Body: types.NewBody([]byte{0}), + Body: body, }) } @@ -372,7 +373,7 @@ func TestSyncQueue_handleResponseQueue_responseQueueAhead(t *testing.T) { q.responses = append(q.responses, &types.BlockData{ Hash: testHeader0.Hash(), Header: testHeader0, - Body: types.NewBody([]byte{4, 4, 2}), + Body: types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}), Receipt: nil, MessageQueue: nil, Justification: nil, @@ -400,7 +401,7 @@ func TestSyncQueue_processBlockResponses(t *testing.T) { { Hash: testHeader0.Hash(), Header: testHeader0, - Body: types.NewBody([]byte{4, 4, 2}), + Body: types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}), Receipt: nil, MessageQueue: nil, Justification: nil, diff --git a/dot/network/test_helpers.go b/dot/network/test_helpers.go index cf0a31dca0..0289c7a07e 100644 --- a/dot/network/test_helpers.go +++ b/dot/network/test_helpers.go @@ -73,10 +73,14 @@ func testBlockResponseMessage() *BlockResponseMessage { Digest: types.NewDigest(), } + // body, err := types.NewBodyExtrinsicsFromBytes([]byte{4, 4, 2}) + // logger.Error("Failed to get extrinsics from bytes:", err) + body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}) + msg.BlockData = append(msg.BlockData, &types.BlockData{ Hash: testHeader.Hash(), Header: testHeader, - Body: types.NewBody([]byte{4, 4, 2}), + Body: body, MessageQueue: nil, Receipt: nil, Justification: nil, diff --git a/dot/rpc/modules/chain_test.go b/dot/rpc/modules/chain_test.go index 73254f5849..6ade88fcc3 100644 --- a/dot/rpc/modules/chain_test.go +++ b/dot/rpc/modules/chain_test.go @@ -140,7 +140,7 @@ func TestChainGetBlock_Genesis(t *testing.T) { expected := &ChainBlockResponse{ Block: ChainBlock{ Header: *expectedHeader, - Body: nil, + Body: []string{"0x2800010203040506070809"}, }, } @@ -179,7 +179,7 @@ func TestChainGetBlock_Latest(t *testing.T) { expected := &ChainBlockResponse{ Block: ChainBlock{ Header: *expectedHeader, - Body: nil, + Body: []string{"0x2800010203040506070809"}, }, } @@ -360,12 +360,9 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti } // Create blockHash blockHash0 := header0.Hash() - // BlockBody with fake extrinsics - blockBody0 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - block0 := &types.Block{ Header: *header0, - Body: blockBody0, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err := bs.AddBlock(block0) @@ -386,12 +383,9 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti StateRoot: trie.EmptyHash, } - // Create Block with fake extrinsics - blockBody1 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - block1 := &types.Block{ Header: *header1, - Body: blockBody1, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block1 to the DB diff --git a/dot/rpc/modules/state_test.go b/dot/rpc/modules/state_test.go index c5cf4b169f..e6ffb01414 100644 --- a/dot/rpc/modules/state_test.go +++ b/dot/rpc/modules/state_test.go @@ -498,7 +498,7 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) { Number: big.NewInt(2), StateRoot: sr1, }, - Body: *types.NewBody([]byte{}), + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{}}), } err = chain.Block.AddBlock(b) diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index efcd0c7318..a70263d552 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -312,7 +312,7 @@ func setupSystemModule(t *testing.T) *SystemModule { ParentHash: chain.Block.BestBlockHash(), StateRoot: ts.MustRoot(), }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, }) require.NoError(t, err) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index 589b044751..21d8e35e93 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -111,7 +111,7 @@ func TestBlockListener_Listen(t *testing.T) { } //block := types.NewEmptyBlock() - block := types.NewBlock(*types.NewEmptyHeader(), *new(types.Body)) + block := types.NewBlock(*types.NewEmptyHeader(), *new(types.BodyExtrinsics)) block.Header.Number = big.NewInt(1) go bl.Listen() @@ -213,8 +213,7 @@ func TestExtrinsicSubmitListener_Listen(t *testing.T) { header := types.NewEmptyHeader() exts := []types.Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - body, err := types.NewBodyFromExtrinsics(exts) - require.NoError(t, err) + body := types.NewBodyExtrinsics(exts) block := &types.Block{ Header: *header, diff --git a/dot/state/block.go b/dot/state/block.go index 1be58b1cb8..6a41cb4a04 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -40,7 +40,8 @@ var blockPrefix = "block" const pruneKeyBufferSize = 1000 -// BlockState defines fields for manipulating the state of blocks, such as BlockTree, BlockDB and Header +// BlockState defines fields for manipulating the state of blocks, such as BlockTree, +// BlockDB and Header type BlockState struct { bt *blocktree.BlockTree baseState *BaseState @@ -351,13 +352,13 @@ func (bs *BlockState) HasBlockBody(hash common.Hash) (bool, error) { } // GetBlockBody will return Body for a given hash -func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { +func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) { data, err := bs.db.Get(blockBodyKey(hash)) if err != nil { return nil, err } - return types.NewBody(data), nil + return types.NewBodyExtrinsicsFromBytes(data) } // SetBlockBody will add a block body to the db @@ -432,7 +433,12 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti } } - err = bs.SetBlockBody(block.Header.Hash(), types.NewBody(block.Body)) + encodedBody, err := block.Body.AsSCALEEncodedBody() + if err != nil { + return err + } + + err = bs.SetBlockBody(block.Header.Hash(), &encodedBody) if err != nil { return err } diff --git a/dot/state/block_data_test.go b/dot/state/block_data_test.go index 73fc146374..96abea8375 100644 --- a/dot/state/block_data_test.go +++ b/dot/state/block_data_test.go @@ -38,8 +38,6 @@ func TestGetSet_ReceiptMessageQueue_Justification(t *testing.T) { } hash := common.NewHash([]byte{0}) - body := types.NewBody([]byte{0xa, 0xb, 0xc, 0xd}) - parentHash := genesisHeader.Hash() stateRoot, err := common.HexToHash("0x2747ab7c0dc38b7f2afba82bd5e2d6acef8c31e09800f660b75ec84a7005099f") @@ -59,10 +57,13 @@ func TestGetSet_ReceiptMessageQueue_Justification(t *testing.T) { a := []byte("asdf") b := []byte("ghjkl") c := []byte("qwerty") + body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + require.NoError(t, err) + bds := []*types.BlockData{{ Hash: header.Hash(), Header: header, - Body: types.NewBody([]byte{}), + Body: body, Receipt: nil, MessageQueue: nil, Justification: nil, diff --git a/dot/state/block_test.go b/dot/state/block_test.go index e36714387e..2cc1430db9 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -93,7 +93,7 @@ func TestGetBlockByNumber(t *testing.T) { block := &types.Block{ Header: *blockHeader, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // AddBlock also sets mapping [blockNumber : hash] in DB @@ -116,12 +116,9 @@ func TestAddBlock(t *testing.T) { } // Create blockHash blockHash0 := header0.Hash() - // BlockBody with fake extrinsics - blockBody0 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - block0 := &types.Block{ Header: *header0, - Body: blockBody0, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block0 to the DB @@ -136,12 +133,9 @@ func TestAddBlock(t *testing.T) { } blockHash1 := header1.Hash() - // Create Block with fake extrinsics - blockBody1 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - block1 := &types.Block{ Header: *header1, - Body: blockBody1, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block1 to the DB @@ -191,7 +185,7 @@ func TestGetSlotForBlock(t *testing.T) { Number: big.NewInt(int64(1)), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } err = bs.AddBlock(block) @@ -257,7 +251,7 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) { ParentHash: bestHash, Number: big.NewInt(0).Add(bestHeader.Number, big.NewInt(1)), }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } err = bs.AddBlock(newBlock) @@ -292,7 +286,7 @@ func TestFinalizedHash(t *testing.T) { err = bs.AddBlock(&types.Block{ Header: *header, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, }) require.NoError(t, err) @@ -391,7 +385,7 @@ func TestGetHashByNumber(t *testing.T) { block := &types.Block{ Header: *header, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block) @@ -411,9 +405,10 @@ func TestAddBlock_WithReOrg(t *testing.T) { ParentHash: testGenesisHeader.Hash(), } + blockbody1a := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}) block1a := &types.Block{ Header: *header1a, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *blockbody1a, } err := bs.AddBlock(block1a) @@ -430,9 +425,13 @@ func TestAddBlock_WithReOrg(t *testing.T) { ExtrinsicsRoot: common.Hash{99}, } + blockbody1b := types.NewBodyExtrinsics( + []types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, + ) + block1b := &types.Block{ Header: *header1b, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *blockbody1b, } err = bs.AddBlock(block1b) @@ -452,7 +451,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2b := &types.Block{ Header: *header2b, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block2b) @@ -475,7 +474,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2a := &types.Block{ Header: *header2a, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block2a) @@ -489,7 +488,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block3a := &types.Block{ Header: *header3a, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block3a) diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index b33cb88c4f..49d44088f2 100644 --- a/dot/state/storage_test.go +++ b/dot/state/storage_test.go @@ -57,13 +57,16 @@ func TestStorage_GetStorageByBlockHash(t *testing.T) { err = storage.StoreTrie(ts, nil) require.NoError(t, err) + body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + require.NoError(t, err) + block := &types.Block{ Header: types.Header{ ParentHash: testGenesisHeader.Hash(), Number: big.NewInt(1), StateRoot: root, }, - Body: *types.NewBody([]byte{}), + Body: *body, } err = storage.blockState.AddBlock(block) require.NoError(t, err) diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index 1ec2500a07..137e33f82c 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -83,7 +83,7 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int) ([]*types StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } currentChain = append(currentChain, &block.Header) @@ -123,7 +123,7 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int) ([]*types StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } branchChains = append(branchChains, &block.Header) @@ -162,7 +162,7 @@ func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, dep Number: big.NewInt(int64(i)), StateRoot: trie.EmptyHash, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } hash := block.Header.Hash() @@ -203,7 +203,7 @@ func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, dep StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } hash := block.Header.Hash() @@ -236,13 +236,16 @@ func generateBlockWithRandomTrie(t *testing.T, serv *Service, parent *common.Has parent = &bb } + body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + require.NoError(t, err) + block := &types.Block{ Header: types.Header{ ParentHash: *parent, Number: big.NewInt(bNum), StateRoot: trieStateRoot, }, - Body: *types.NewBody([]byte{}), + Body: *body, } return block, trieState } diff --git a/dot/sync/interface.go b/dot/sync/interface.go index 5136591afb..e574cdf790 100644 --- a/dot/sync/interface.go +++ b/dot/sync/interface.go @@ -35,7 +35,7 @@ type BlockState interface { CompareAndSetBlockData(bd *types.BlockData) error GetBlockByNumber(*big.Int) (*types.Block, error) HasBlockBody(hash common.Hash) (bool, error) - GetBlockBody(common.Hash) (*types.Body, error) + GetBlockBody(common.Hash) (*types.BodyExtrinsics, error) SetHeader(*types.Header) error GetHeader(common.Hash) (*types.Header, error) HasHeader(hash common.Hash) (bool, error) diff --git a/dot/sync/message_test.go b/dot/sync/message_test.go index a2f37ca6b6..363cf6e610 100644 --- a/dot/sync/message_test.go +++ b/dot/sync/message_test.go @@ -26,7 +26,7 @@ func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) { StateRoot: trie.EmptyHash, Digest: types.NewDigest(), }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } previousHash = block.Header.Hash() diff --git a/dot/sync/syncer.go b/dot/sync/syncer.go index 1123248272..4b12efed7f 100644 --- a/dot/sync/syncer.go +++ b/dot/sync/syncer.go @@ -153,8 +153,9 @@ func (s *Service) ProcessJustification(data []*types.BlockData) (int, error) { return 0, nil } -// ProcessBlockData processes the BlockData from a BlockResponse and returns the index of the last BlockData it handled on success, -// or the index of the block data that errored on failure. +// ProcessBlockData processes the BlockData from a BlockResponse and returns the +// index of the last BlockData it handled on success, or the index of the block data +// that errored on failure. func (s *Service) ProcessBlockData(data []*types.BlockData) (int, error) { if len(data) == 0 { return 0, ErrNilBlockData @@ -229,10 +230,7 @@ func (s *Service) ProcessBlockData(data []*types.BlockData) (int, error) { logger.Trace("processing body", "hash", bd.Hash) - err = s.handleBody(body) - if err != nil { - return i, err - } + s.handleBody(body) logger.Trace("body processed", "hash", bd.Hash) } @@ -277,22 +275,14 @@ func (s *Service) handleHeader(header *types.Header) error { return nil } -// handleHeader handles block bodies included in BlockResponses -func (s *Service) handleBody(body *types.Body) error { - exts, err := body.AsExtrinsics() - if err != nil { - logger.Error("cannot parse body as extrinsics", "error", err) - return err - } - - for _, ext := range exts { +// handleBody handles block bodies included in BlockResponses +func (s *Service) handleBody(body *types.BodyExtrinsics) { + for _, ext := range *body { s.transactionState.RemoveExtrinsic(ext) } - - return err } -// handleHeader handles blocks (header+body) included in BlockResponses +// handleBlock handles blocks (header+body) included in BlockResponses func (s *Service) handleBlock(block *types.Block) error { if block == nil || block.Empty() || block.Header.Empty() { return errors.New("block, header, or body is nil") diff --git a/dot/sync/syncer_test.go b/dot/sync/syncer_test.go index 7747635bb3..6b307ffd12 100644 --- a/dot/sync/syncer_test.go +++ b/dot/sync/syncer_test.go @@ -158,9 +158,7 @@ func TestRemoveIncludedExtrinsics(t *testing.T) { require.NoError(t, err) exts := []types.Extrinsic{ext} - body, err := types.NewBodyFromExtrinsics(exts) - require.NoError(t, err) - + body := types.NewBodyExtrinsics(exts) bd := &types.BlockData{ Body: body, } @@ -266,7 +264,7 @@ func TestSyncer_HandleJustification(t *testing.T) { err = syncer.blockState.AddBlock(&types.Block{ Header: *header, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, }) require.NoError(t, err) diff --git a/dot/sync/test_helpers.go b/dot/sync/test_helpers.go index 7833b55343..eea10fba60 100644 --- a/dot/sync/test_helpers.go +++ b/dot/sync/test_helpers.go @@ -180,7 +180,7 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e inExt := exts - var body *types.Body + var body *types.BodyExtrinsics if ext != nil { var txn *transaction.Validity externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, ext...)) @@ -195,7 +195,7 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e require.NoError(t, err) } else { - body = types.NewBody(inherentExts) + body = types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) } // apply each inherent extrinsic diff --git a/dot/types/block.go b/dot/types/block.go index 1a5e5a1cc5..a0b4c9d314 100644 --- a/dot/types/block.go +++ b/dot/types/block.go @@ -23,11 +23,11 @@ import ( // Block defines a state block type Block struct { Header Header - Body Body + Body BodyExtrinsics } // NewBlock returns a new Block -func NewBlock(header Header, body Body) Block { +func NewBlock(header Header, body BodyExtrinsics) Block { return Block{ Header: header, Body: body, @@ -38,7 +38,7 @@ func NewBlock(header Header, body Body) Block { func NewEmptyBlock() Block { return Block{ Header: *NewEmptyHeader(), - Body: *NewBody(nil), + Body: BodyExtrinsics(nil), } } @@ -54,8 +54,12 @@ func (b *Block) Encode() ([]byte, error) { return nil, err } - // block body is already SCALE encoded - return append(enc, []byte(b.Body)...), nil + // get a SCALE encoded block body + encodedBody, err := b.Body.AsSCALEEncodedBody() + if err != nil { + return nil, err + } + return append(enc, []byte(encodedBody)...), nil } // MustEncode returns the SCALE encoded block and panics if it fails to encode @@ -69,14 +73,12 @@ func (b *Block) MustEncode() []byte { // DeepCopy returns a copy of the block func (b *Block) DeepCopy() (Block, error) { - bc := make([]byte, len(b.Body)) - copy(bc, b.Body) head, err := b.Header.DeepCopy() if err != nil { return Block{}, err } return Block{ Header: *head, - Body: *NewBody(bc), + Body: b.Body.DeepCopy(), }, nil } diff --git a/dot/types/block_data.go b/dot/types/block_data.go index 50f9138c42..b14ca07c89 100644 --- a/dot/types/block_data.go +++ b/dot/types/block_data.go @@ -24,11 +24,12 @@ import ( ) // BlockData is stored within the BlockDB -// The BlockData fields are optionals and thus are represented as pointers to ensure correct encoding +// The BlockData fields are optionals and thus are represented as pointers to ensure +// correct encoding type BlockData struct { Hash common.Hash Header *Header - Body *Body + Body *BodyExtrinsics Receipt *[]byte MessageQueue *[]byte Justification *[]byte diff --git a/dot/types/block_data_test.go b/dot/types/block_data_test.go index bd4d0b6c8d..a61ec84d36 100644 --- a/dot/types/block_data_test.go +++ b/dot/types/block_data_test.go @@ -126,13 +126,13 @@ func TestBlockDataEncodeAndDecodeHeader(t *testing.T) { } func TestBlockDataEncodeAndDecodeBody(t *testing.T) { - expected, err := common.HexToBytes("0x00000000000000000000000000000000000000000000000000000000000000000001100a0b0c0d000000") + expected, err := common.HexToBytes("0x0000000000000000000000000000000000000000000000000000000000000000000104100a0b0c0d000000") require.NoError(t, err) bd := BlockData{ Hash: common.NewHash([]byte{0}), Header: nil, - Body: NewBody([]byte{0xa, 0xb, 0xc, 0xd}), + Body: NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), Receipt: nil, MessageQueue: nil, Justification: nil, @@ -156,12 +156,11 @@ func TestBlockDataEncodeAndDecodeBody(t *testing.T) { } func TestBlockDataEncodeAndDecodeAll(t *testing.T) { - expected, err := common.HexToBytes("0x7d0000000000000000000000000000000000000000000000000000000000000001000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f04000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f0806424142450c0102030542414245100405060701100a0b0c0d010401010402010403") + expected, err := common.HexToBytes("0x7d0000000000000000000000000000000000000000000000000000000000000001000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f04000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f0806424142450c010203054241424510040506070104100a0b0c0d010401010402010403") require.NoError(t, err) hash := common.NewHash([]byte{125}) testHash := common.NewHash([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}) - body := NewBody([]byte{0xa, 0xb, 0xc, 0xd}) headerVdt, err := NewHeader(testHash, testHash, testHash, big.NewInt(1), testDigest) require.NoError(t, err) @@ -169,7 +168,7 @@ func TestBlockDataEncodeAndDecodeAll(t *testing.T) { bd := BlockData{ Hash: hash, Header: headerVdt, - Body: body, + Body: NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), Receipt: &[]byte{1}, MessageQueue: &[]byte{2}, Justification: &[]byte{3}, diff --git a/dot/types/block_test.go b/dot/types/block_test.go index 157f3ad3c4..0ac3c681bb 100644 --- a/dot/types/block_test.go +++ b/dot/types/block_test.go @@ -32,7 +32,7 @@ func TestEmptyBlock(t *testing.T) { isEmpty := block.Empty() require.True(t, isEmpty) - block = NewBlock(*NewEmptyHeader(), Body{}) + block = NewBlock(*NewEmptyHeader(), BodyExtrinsics{}) isEmpty = block.Empty() require.True(t, isEmpty) @@ -48,20 +48,18 @@ func TestEmptyBlock(t *testing.T) { header, err := NewHeader(parentHash, stateRoot, extrinsicsRoot, big.NewInt(1), NewDigest()) require.NoError(t, err) - block = NewBlock(*header, Body{}) + block = NewBlock(*header, BodyExtrinsics{}) isEmpty = block.Empty() require.False(t, isEmpty) - block = NewBlock(*NewEmptyHeader(), *NewBody([]byte{4, 1})) + block = NewBlock(*NewEmptyHeader(), *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) isEmpty = block.Empty() require.False(t, isEmpty) } func TestEncodeAndDecodeBlock(t *testing.T) { - expected := []byte{69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 4, 39, 71, 171, 124, 13, 195, 139, 127, 42, 251, 168, 43, 213, 226, 214, 172, 239, 140, 49, 224, 152, 0, - 246, 96, 183, 94, 200, 74, 112, 5, 9, 159, 3, 23, 10, 46, 117, 151, 183, 183, 227, 216, 76, 5, 57, 29, 19, - 154, 98, 177, 87, 231, 135, 134, 216, 192, 130, 242, 157, 207, 76, 17, 19, 20, 0, 8, 4, 1} + expected, err := common.HexToBytes("0x4545454545454545454545454545454545454545454545454545454545454545042747ab7c0dc38b7f2afba82bd5e2d6acef8c31e09800f660b75ec84a7005099f03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c1113140004080401") + require.NoError(t, err) parentHash, err := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545") require.NoError(t, err) @@ -75,16 +73,14 @@ func TestEncodeAndDecodeBlock(t *testing.T) { header, err := NewHeader(parentHash, stateRoot, extrinsicsRoot, big.NewInt(1), NewDigest()) require.NoError(t, err) - body := NewBody([]byte{4, 1}) - - block := NewBlock(*header, *body) + block := NewBlock(*header, *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) enc, err := scale.Marshal(block) require.NoError(t, err) require.Equal(t, expected, enc) - dec := NewBlock(*NewEmptyHeader(), *new(Body)) + dec := NewBlock(*NewEmptyHeader(), *new(BodyExtrinsics)) err = scale.Unmarshal(enc, &dec) require.NoError(t, err) if dec.Header.Number != nil { @@ -95,7 +91,7 @@ func TestEncodeAndDecodeBlock(t *testing.T) { func TestDeepCopyBlock(t *testing.T) { data := []byte{69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 4, 39, 71, 171, 124, 13, 195, 139, 127, 42, 251, 168, 43, 213, 226, 214, 172, 239, 140, 49, 224, 152, 0, 246, 96, 183, 94, 200, 74, 112, 5, 9, 159, 3, 23, 10, 46, 117, 151, 183, 183, 227, 216, 76, 5, 57, 29, 19, 154, 98, 177, 87, 231, 135, 134, 216, 192, 130, 242, 157, 207, 76, 17, 19, 20, 0, 0} - block := NewBlock(*NewEmptyHeader(), *new(Body)) + block := NewBlock(*NewEmptyHeader(), *new(BodyExtrinsics)) err := scale.Unmarshal(data, &block) if err != nil { @@ -111,13 +107,15 @@ func TestDeepCopyBlock(t *testing.T) { func TestMustEncodeBlock(t *testing.T) { h1, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), NewDigest()) require.NoError(t, err) - b1 := NewBlock(*h1, *NewBody([]byte{})) + + b1 := NewBlock(*h1, *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) enc, err := b1.Encode() require.NoError(t, err) h2, err := NewHeader(common.Hash{0x1, 0x2}, common.Hash{}, common.Hash{}, big.NewInt(0), NewDigest()) require.NoError(t, err) - b2 := NewBlock(*h2, *NewBody([]byte{0xa, 0xb})) + + b2 := NewBlock(*h2, *NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb}})) enc2, err := b2.Encode() require.NoError(t, err) diff --git a/dot/types/body.go b/dot/types/body.go index 3b6eb38dde..0b2cb9fc46 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -36,7 +36,7 @@ func NewBody(b []byte) *Body { return &body } -// NewBodyFromBytes returns a new Body from a slice of byte slices +// NewBodyFromBytes returns a new Body from a slice of byte slices (not encoded extrinsics) func NewBodyFromBytes(exts [][]byte) (*Body, error) { enc, err := scale.Encode(exts) if err != nil { @@ -47,8 +47,13 @@ func NewBodyFromBytes(exts [][]byte) (*Body, error) { return &body, nil } -// NewBodyFromEncodedBytes returns a new Body from a slice of byte slices that are SCALE encoded extrinsics +// NewBodyFromEncodedBytes returns a new Body from a slice of byte slices that are +// SCALE encoded extrinsics func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { + // A collection of same-typed values is encoded, prefixed with a compact + // encoding of the number of items, followed by each item's encoding + // concatenated in turn. + // https://substrate.dev/docs/en/knowledgebase/advanced/codec#vectors-lists-series-sets enc, err := scale.Encode(big.NewInt(int64(len(exts)))) if err != nil { return nil, err @@ -73,7 +78,8 @@ func NewBodyFromExtrinsics(exts []Extrinsic) (*Body, error) { return &body, nil } -// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded 0x-prefixed strings. +// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded +// 0x-prefixed strings. func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { exts := [][]byte{} for _, s := range ss { diff --git a/dot/types/body_extrinsics.go b/dot/types/body_extrinsics.go new file mode 100644 index 0000000000..f813e599fe --- /dev/null +++ b/dot/types/body_extrinsics.go @@ -0,0 +1,147 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + +package types + +import ( + "bytes" + "fmt" + "math/big" + + "github.com/ChainSafe/gossamer/lib/common" + scale2 "github.com/ChainSafe/gossamer/pkg/scale" +) + +// BodyExtrinsics is the extrinsics(not encoded) inside a state block. +type BodyExtrinsics []Extrinsic + +// NewBodyExtrinsics returns a BodyExtrinsics from an Extrinsic array. +func NewBodyExtrinsics(e []Extrinsic) *BodyExtrinsics { + body := BodyExtrinsics(e) + return &body +} + +// NewBodyExtrinsics returns a BodyExtrinsics from a SCALE encoded byte array. +func NewBodyExtrinsicsFromBytes(b []byte) (*BodyExtrinsics, error) { + exts := [][]byte{} + + if len(b) == 0 { + return NewBodyExtrinsics([]Extrinsic{}), nil + } + + err := scale2.Unmarshal(b, &exts) + if err != nil { + return nil, err + } + + return NewBodyExtrinsics(BytesArrayToExtrinsics(exts)), nil +} + +// NewBodyExtrinsicsFromEncodedBytes returns a new Body from a slice of byte slices that are +// SCALE encoded extrinsics +func NewBodyExtrinsicsFromEncodedBytes(exts [][]byte) (*BodyExtrinsics, error) { + // A collection of same-typed values is encoded, prefixed with a compact + // encoding of the number of items, followed by each item's encoding + // concatenated in turn. + // https://substrate.dev/docs/en/knowledgebase/advanced/codec#vectors-lists-series-sets + enc, err := scale2.Marshal(big.NewInt(int64(len(exts)))) + if err != nil { + return nil, err + } + + for _, ext := range exts { + enc = append(enc, ext...) + } + + return NewBodyExtrinsicsFromBytes(enc) +} + +// NewBodyExtrinsicsFromExtrinsicStrings creates a block body given an array of hex-encoded +// 0x-prefixed strings. +func NewBodyExtrinsicsFromExtrinsicStrings(ss []string) (*BodyExtrinsics, error) { + exts := [][]byte{} + for _, s := range ss { + b, err := common.HexToBytes(s) + if err == common.ErrNoPrefix { + b = []byte(s) + } else if err != nil { + return nil, err + } + exts = append(exts, b) + } + + return NewBodyExtrinsics(BytesArrayToExtrinsics(exts)), nil +} + +func (b *BodyExtrinsics) AsSCALEEncodedBody() (Body, error) { + encodedBody, err := scale2.Marshal(ExtrinsicsArrayToBytesArray(*b)) + if err != nil { + return nil, err + } + + return Body(encodedBody), nil +} + +func (b *BodyExtrinsics) DeepCopy() BodyExtrinsics { + newExtrinsics := make([]Extrinsic, len([]Extrinsic(*b))) + copy(newExtrinsics, []Extrinsic(*b)) + + return BodyExtrinsics(newExtrinsics) +} + +// HasExtrinsic returns true if body contains target Extrisic +func (b *BodyExtrinsics) HasExtrinsic(target Extrinsic) (bool, error) { + exts := *b + + // goes through the decreasing order due to the fact that extrinsicsToBody + // func (lib/babe/build.go) appends the valid transaction extrinsic on the end of the body + for i := len(exts) - 1; i >= 0; i-- { + currext := exts[i] + + // if current extrinsic is equal the target then returns true + if bytes.Equal(target, currext) { + return true, nil + } + + //otherwise try to encode and compare + encext, err := scale2.Marshal(currext) + if err != nil { + return false, fmt.Errorf("fail while scale encode: %w", err) + } + + if len(encext) >= len(target) && bytes.Equal(target, encext[:len(target)]) { + return true, nil + } + } + + return false, nil +} + +// AsEncodedExtrinsics decodes the body into an array of SCALE encoded extrinsics +func (b *BodyExtrinsics) AsEncodedExtrinsics() ([]Extrinsic, error) { + decodedExts := *b + ret := make([][]byte, len(decodedExts)) + var err error + + for i, ext := range decodedExts { + ret[i], err = scale2.Marshal(ext) + if err != nil { + return nil, err + } + } + + return BytesArrayToExtrinsics(ret), nil +} diff --git a/lib/babe/build.go b/lib/babe/build.go index 5110665957..e6768fd53c 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -359,7 +359,7 @@ func hasSlotEnded(slot Slot) bool { } // ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. -func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { +func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.BodyExtrinsics, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { @@ -371,10 +371,5 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( extrinsics = append(extrinsics, decExt) } - enc, err := scale.Marshal(extrinsics) - if err != nil { - return nil, err - } - body := types.Body(enc) - return &body, nil + return types.NewBodyExtrinsics(extrinsics), nil } diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index 7977ddc081..3a7f8f7c53 100644 --- a/lib/babe/build_test.go +++ b/lib/babe/build_test.go @@ -206,10 +206,7 @@ func TestBuildBlock_ok(t *testing.T) { require.Equal(t, *preDigest, block.Header.Digest.Types[0].Value()) // confirm block body is correct - extsRes, err := block.Body.AsExtrinsics() - require.NoError(t, err) - - extsBytes := types.ExtrinsicsArrayToBytesArray(extsRes) + extsBytes := types.ExtrinsicsArrayToBytesArray(block.Body) require.Equal(t, 1, len(extsBytes)) } @@ -460,11 +457,7 @@ func TestDecodeExtrinsicBody(t *testing.T) { body, err := ExtrinsicsToBody(inh, []*transaction.ValidTransaction{vtx}) require.Nil(t, err) require.NotNil(t, body) - - bodyext, err := body.AsExtrinsics() - require.Nil(t, err) - require.NotNil(t, bodyext) - require.Len(t, bodyext, 3) + require.Len(t, *body, 3) contains, err := body.HasExtrinsic(ext) require.Nil(t, err) diff --git a/lib/babe/median_test.go b/lib/babe/median_test.go index 2a1c51f021..b25a027dd1 100644 --- a/lib/babe/median_test.go +++ b/lib/babe/median_test.go @@ -107,7 +107,7 @@ func addBlocksToState(t *testing.T, babeService *Service, depth int, blockState Number: big.NewInt(int64(i)), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } arrivalTime := previousAT.Add(duration) @@ -176,7 +176,7 @@ func TestEstimateCurrentSlot(t *testing.T) { Number: big.NewInt(int64(1)), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } arrivalTime := time.Now().UnixNano() - slot.duration.Nanoseconds() diff --git a/lib/grandpa/message_handler_test.go b/lib/grandpa/message_handler_test.go index 8dce03d9c5..edfa77cef4 100644 --- a/lib/grandpa/message_handler_test.go +++ b/lib/grandpa/message_handler_test.go @@ -204,13 +204,17 @@ func TestMessageHandler_NeighbourMessage(t *testing.T) { digest := types.NewDigest() err = digest.Add(types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()) require.NoError(t, err) + + body, err := types.NewBodyExtrinsicsFromBytes([]byte{0}) + require.NoError(t, err) + block := &types.Block{ Header: types.Header{ Number: big.NewInt(2), ParentHash: st.Block.GenesisHash(), Digest: digest, }, - Body: types.Body{0}, + Body: *body, } err = st.Block.AddBlock(block) @@ -265,7 +269,7 @@ func TestMessageHandler_CommitMessage_NoCatchUpRequest_ValidSig(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } err = st.Block.AddBlock(block) @@ -358,7 +362,7 @@ func TestMessageHandler_CatchUpRequest_WithResponse(t *testing.T) { Number: big.NewInt(2), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } err = st.Block.AddBlock(block) @@ -533,9 +537,12 @@ func TestMessageHandler_VerifyBlockJustification(t *testing.T) { err := st.Grandpa.SetNextChange(auths, big.NewInt(1)) require.NoError(t, err) + body, err := types.NewBodyExtrinsicsFromBytes([]byte{0}) + require.NoError(t, err) + block := &types.Block{ Header: *testHeader, - Body: types.Body{0}, + Body: *body, } err = st.Block.AddBlock(block) diff --git a/lib/grandpa/message_test.go b/lib/grandpa/message_test.go index 6e9824715c..96d3423d80 100644 --- a/lib/grandpa/message_test.go +++ b/lib/grandpa/message_test.go @@ -113,7 +113,7 @@ func TestNewCatchUpResponse(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } hash := block.Header.Hash() diff --git a/lib/grandpa/message_tracker_test.go b/lib/grandpa/message_tracker_test.go index 9d1e3644c3..7cb18a540a 100644 --- a/lib/grandpa/message_tracker_test.go +++ b/lib/grandpa/message_tracker_test.go @@ -90,7 +90,7 @@ func TestMessageTracker_SendMessage(t *testing.T) { err = gs.blockState.(*state.BlockState).AddBlock(&types.Block{ Header: *next, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, }) require.NoError(t, err) @@ -136,7 +136,7 @@ func TestMessageTracker_ProcessMessage(t *testing.T) { err = gs.blockState.(*state.BlockState).AddBlock(&types.Block{ Header: *next, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, }) require.NoError(t, err) diff --git a/lib/grandpa/network_test.go b/lib/grandpa/network_test.go index 98a9f5ed90..6f5f27cc00 100644 --- a/lib/grandpa/network_test.go +++ b/lib/grandpa/network_test.go @@ -99,7 +99,7 @@ func TestSendNeighbourMessage(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } err = st.Block.AddBlock(block) diff --git a/lib/grandpa/round_test.go b/lib/grandpa/round_test.go index 3666770489..e7df7ee32b 100644 --- a/lib/grandpa/round_test.go +++ b/lib/grandpa/round_test.go @@ -388,7 +388,7 @@ func TestPlayGrandpaRound_VaryingChain(t *testing.T) { time.Sleep(time.Millisecond * 10) block := &types.Block{ Header: *h, - Body: types.Body{}, + Body: types.BodyExtrinsics{}, } gs.blockState.(*state.BlockState).AddBlock(block) } diff --git a/lib/runtime/life/exports_test.go b/lib/runtime/life/exports_test.go index a3f3df9685..f59b9b4cd6 100644 --- a/lib/runtime/life/exports_test.go +++ b/lib/runtime/life/exports_test.go @@ -11,7 +11,6 @@ import ( "github.com/ChainSafe/gossamer/lib/keystore" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/runtime/storage" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/trie" scale2 "github.com/ChainSafe/gossamer/pkg/scale" @@ -188,7 +187,7 @@ func buildBlock(t *testing.T, instance runtime.Instance) *types.Block { return &types.Block{ Header: *res, - Body: *types.NewBody(inherentExts), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } } @@ -237,9 +236,10 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { // block data is received from querying a polkadot node body := []byte{8, 40, 4, 2, 0, 11, 144, 17, 14, 179, 110, 1, 16, 4, 20, 0, 0} - exts, err := scale.Decode(body, [][]byte{}) + var exts [][]byte + err = scale2.Unmarshal(body, &exts) require.NoError(t, err) - require.Equal(t, 2, len(exts.([][]byte))) + require.Equal(t, 2, len(exts)) // digest from polkadot.js digestBytes := common.MustHexToBytes("0x0c0642414245340201000000ef55a50f00000000044241424549040118ca239392960473fe1bc65f94ee27d890a49c1b200c006ff5dcc525330ecc16770100000000000000b46f01874ce7abbb5220e8fd89bede0adad14c73039d91e28e881823433e723f0100000000000000d684d9176d6eb69887540c9a89fa6097adea82fc4b0ff26d1062b488f352e179010000000000000068195a71bdde49117a616424bdc60a1733e96acb1da5aeab5d268cf2a572e94101000000000000001a0575ef4ae24bdfd31f4cb5bd61239ae67c12d4e64ae51ac756044aa6ad8200010000000000000018168f2aad0081a25728961ee00627cfe35e39833c805016632bf7c14da5800901000000000000000000000000000000000000000000000000000000000000000000000000000000054241424501014625284883e564bc1e4063f5ea2b49846cdddaa3761d04f543b698c1c3ee935c40d25b869247c36c6b8a8cbbd7bb2768f560ab7c276df3c62df357a7e3b1ec8d") @@ -256,7 +256,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xa35fb7f7616f5c979d48222b3d2fa7cb2331ef73954726714d91ca945cc34fd8"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -286,9 +286,10 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { // block data is received from querying a polkadot node body := []byte{8, 40, 4, 3, 0, 11, 80, 149, 160, 81, 114, 1, 16, 4, 20, 0, 0} - exts, err := scale.Decode(body, [][]byte{}) + var exts [][]byte + err = scale2.Unmarshal(body, &exts) require.NoError(t, err) - require.Equal(t, 2, len(exts.([][]byte))) + require.Equal(t, 2, len(exts)) // digest data received from querying polkadot node digestBytes := common.MustHexToBytes("0x0c0642414245b501010000000093decc0f00000000362ed8d6055645487fe42e9c8640be651f70a3a2a03658046b2b43f021665704501af9b1ca6e974c257e3d26609b5f68b5b0a1da53f7f252bbe5d94948c39705c98ffa4b869dd44ac29528e3723d619cc7edf1d3f7b7a57a957f6a7e9bdb270a044241424549040118fa3437b10f6e7af8f31362df3a179b991a8c56313d1bcd6307a4d0c734c1ae310100000000000000d2419bc8835493ac89eb09d5985281f5dff4bc6c7a7ea988fd23af05f301580a0100000000000000ccb6bef60defc30724545d57440394ed1c71ea7ee6d880ed0e79871a05b5e40601000000000000005e67b64cf07d4d258a47df63835121423551712844f5b67de68e36bb9a21e12701000000000000006236877b05370265640c133fec07e64d7ca823db1dc56f2d3584b3d7c0f1615801000000000000006c52d02d95c30aa567fda284acf25025ca7470f0b0c516ddf94475a1807c4d250100000000000000000000000000000000000000000000000000000000000000000000000000000005424142450101d468680c844b19194d4dfbdc6697a35bf2b494bda2c5a6961d4d4eacfbf74574379ba0d97b5bb650c2e8670a63791a727943bcb699dc7a228bdb9e0a98c9d089") @@ -305,7 +306,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x9a87f6af64ef97aff2d31bebfdd59f8fe2ef6019278b634b2515a38f1c4c2420"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, _ = instance.ExecuteBlock(block) // TODO: fix diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 853d77b249..5d096fa300 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -529,7 +529,7 @@ func buildBlockVdt(t *testing.T, instance runtime.Instance, parentHash common.Ha return &types.Block{ Header: *res, - Body: *types.NewBody(inherentExts), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } } @@ -681,7 +681,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x9a87f6af64ef97aff2d31bebfdd59f8fe2ef6019278b634b2515a38f1c4c2420"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -732,7 +732,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xa35fb7f7616f5c979d48222b3d2fa7cb2331ef73954726714d91ca945cc34fd8"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -778,7 +778,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock3784(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x52b7d4852fc648cb8f908901e1e36269593c25050c31718454bca74b69115d12"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -824,7 +824,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock901442(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x13483a4c148fff5f072e86b5af52bf031556514e9c87ea19f9e31e7b13c0c414"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -870,7 +870,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1377831(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x7f3ea0ed63b4053d9b75e7ee3e5b3f6ce916e8f59b7b6c5e966b7a56ea0a563a"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -917,7 +917,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1482003(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xdf5da95780b77e83ad0bf820d5838f07a0d5131aa95a75f8dfbd01fbccb300bd"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -961,7 +961,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock4939774(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x5d887e118ee6320aca38e49cbd98adc25472c6efbf77a695ab0d6c476a4ec6e9"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -1006,7 +1006,7 @@ func TestInstance_ExecuteBlock_PolkadotBlock1089328(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x950173af1d9fdcd0be5428fc3eaf05d5f34376bd3882d9a61b348fa2dc641012"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) diff --git a/tests/stress/stress_test.go b/tests/stress/stress_test.go index f3d8613e79..bcd4e163e3 100644 --- a/tests/stress/stress_test.go +++ b/tests/stress/stress_test.go @@ -467,8 +467,7 @@ func TestSync_SubmitExtrinsic(t *testing.T) { logger.Debug("got block from node", "header", header, "body", block.Body, "node", nodes[idx].Key) if block.Body != nil { - resExts, err = block.Body.AsExtrinsics() - require.NoError(t, err, block.Body) + resExts = block.Body logger.Debug("extrinsics", "exts", resExts) if len(resExts) >= 2 { diff --git a/tests/utils/chain.go b/tests/utils/chain.go index 636d6690d2..41ddfcc45d 100644 --- a/tests/utils/chain.go +++ b/tests/utils/chain.go @@ -137,7 +137,7 @@ func GetBlock(t *testing.T, node *Node, hash common.Hash) *types.Block { h, err := types.NewHeader(parentHash, stateRoot, extrinsicsRoot, number, rpcLogsToDigest(t, header.Digest.Logs)) require.NoError(t, err) - b, err := types.NewBodyFromExtrinsicStrings(block.Block.Body) + b, err := types.NewBodyExtrinsicsFromExtrinsicStrings(block.Block.Body) require.NoError(t, err, fmt.Sprintf("%v", block.Block.Body)) return &types.Block{ From 70cd6d1070f2d08d0a80970b14f62ee205d57807 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 28 Sep 2021 16:10:05 +0530 Subject: [PATCH 2/8] Tests for BodyExtrinsics, Removed old Body struct --- dot/state/block.go | 17 ++- dot/types/body.go | 182 ------------------------------ dot/types/body_extrinsics.go | 27 +++-- dot/types/body_extrinsics_test.go | 81 +++++++++++++ dot/types/body_test.go | 113 ------------------- 5 files changed, 108 insertions(+), 312 deletions(-) delete mode 100644 dot/types/body.go create mode 100644 dot/types/body_extrinsics_test.go delete mode 100644 dot/types/body_test.go diff --git a/dot/state/block.go b/dot/state/block.go index 603c35a0a8..51ec1d8ff2 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -119,7 +119,7 @@ func NewBlockStateFromGenesis(db chaindb.Database, header *types.Header) (*Block return nil, err } - if err := bs.SetBlockBody(header.Hash(), types.NewBody([]byte{})); err != nil { + if err := bs.SetBlockBody(header.Hash(), types.NewBodyExtrinsics([]types.Extrinsic{})); err != nil { return nil, err } @@ -359,8 +359,12 @@ func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, err } // SetBlockBody will add a block body to the db -func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.Body) error { - return bs.db.Put(blockBodyKey(hash), body.AsOptional().Value()) +func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.BodyExtrinsics) error { + optionalBody, err := body.AsOptional() + if err != nil { + return err + } + return bs.db.Put(blockBodyKey(hash), optionalBody.Value()) } // CompareAndSetBlockData will compare empty fields and set all elements in a block data to db @@ -430,12 +434,7 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti } } - encodedBody, err := block.Body.AsSCALEEncodedBody() - if err != nil { - return err - } - - err = bs.SetBlockBody(block.Header.Hash(), &encodedBody) + err = bs.SetBlockBody(block.Header.Hash(), &block.Body) if err != nil { return err } diff --git a/dot/types/body.go b/dot/types/body.go deleted file mode 100644 index 0b2cb9fc46..0000000000 --- a/dot/types/body.go +++ /dev/null @@ -1,182 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package types - -import ( - "bytes" - "fmt" - "math/big" - - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" -) - -// Body is the encoded extrinsics inside a state block -type Body []byte - -// NewBody returns a Body from a byte array -func NewBody(b []byte) *Body { - body := Body(b) - return &body -} - -// NewBodyFromBytes returns a new Body from a slice of byte slices (not encoded extrinsics) -func NewBodyFromBytes(exts [][]byte) (*Body, error) { - enc, err := scale.Encode(exts) - if err != nil { - return nil, err - } - - body := Body(enc) - return &body, nil -} - -// NewBodyFromEncodedBytes returns a new Body from a slice of byte slices that are -// SCALE encoded extrinsics -func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { - // A collection of same-typed values is encoded, prefixed with a compact - // encoding of the number of items, followed by each item's encoding - // concatenated in turn. - // https://substrate.dev/docs/en/knowledgebase/advanced/codec#vectors-lists-series-sets - enc, err := scale.Encode(big.NewInt(int64(len(exts)))) - if err != nil { - return nil, err - } - - for _, ext := range exts { - enc = append(enc, ext...) - } - - body := Body(enc) - return &body, nil -} - -// NewBodyFromExtrinsics creates a block body given an array of extrinsics. -func NewBodyFromExtrinsics(exts []Extrinsic) (*Body, error) { - enc, err := scale.Encode(ExtrinsicsArrayToBytesArray(exts)) - if err != nil { - return nil, err - } - - body := Body(enc) - return &body, nil -} - -// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded -// 0x-prefixed strings. -func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { - exts := [][]byte{} - for _, s := range ss { - b, err := common.HexToBytes(s) - if err == common.ErrNoPrefix { - b = []byte(s) - } else if err != nil { - return nil, err - } - exts = append(exts, b) - } - - enc, err := scale.Encode(exts) - if err != nil { - return nil, err - } - - body := Body(enc) - return &body, nil -} - -// AsExtrinsics decodes the body into an array of extrinsics -func (b *Body) AsExtrinsics() ([]Extrinsic, error) { - exts := [][]byte{} - - if len(*b) == 0 { - return []Extrinsic{}, nil - } - - dec, err := scale.Decode(*b, exts) - if err != nil { - return nil, err - } - - return BytesArrayToExtrinsics(dec.([][]byte)), nil -} - -// AsEncodedExtrinsics decodes the body into an array of SCALE encoded extrinsics -func (b *Body) AsEncodedExtrinsics() ([]Extrinsic, error) { - exts := [][]byte{} - - if len(*b) == 0 { - return []Extrinsic{}, nil - } - - err := scale2.Unmarshal(*b, &exts) - if err != nil { - return nil, err - } - - decodedExts := exts - ret := make([][]byte, len(decodedExts)) - - for i, ext := range decodedExts { - ret[i], err = scale2.Marshal(ext) - if err != nil { - return nil, err - } - } - - return BytesArrayToExtrinsics(ret), nil -} - -// AsOptional returns the Body as an optional.Body -func (b *Body) AsOptional() *optional.Body { - ob := optional.CoreBody([]byte(*b)) - return optional.NewBody(true, ob) -} - -// HasExtrinsic returns true if body contains target Extrisic -// returns error when fails to encode decoded extrinsic on body -func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { - exts, err := b.AsExtrinsics() - if err != nil { - return false, err - } - - // goes through the decreasing order due to the fact that extrinsicsToBody func (lib/babe/build.go) - // appends the valid transaction extrinsic on the end of the body - for i := len(exts) - 1; i >= 0; i-- { - currext := exts[i] - - // if current extrinsic is equal the target then returns true - if bytes.Equal(target, currext) { - return true, nil - } - - //otherwise try to encode and compare - encext, err := scale.Encode(currext) - if err != nil { - return false, fmt.Errorf("fail while scale encode: %w", err) - } - - if len(encext) >= len(target) && bytes.Equal(target, encext[:len(target)]) { - return true, nil - } - } - - return false, nil -} diff --git a/dot/types/body_extrinsics.go b/dot/types/body_extrinsics.go index f813e599fe..fc8ae24670 100644 --- a/dot/types/body_extrinsics.go +++ b/dot/types/body_extrinsics.go @@ -22,7 +22,8 @@ import ( "math/big" "github.com/ChainSafe/gossamer/lib/common" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/lib/common/optional" + "github.com/ChainSafe/gossamer/pkg/scale" ) // BodyExtrinsics is the extrinsics(not encoded) inside a state block. @@ -42,7 +43,7 @@ func NewBodyExtrinsicsFromBytes(b []byte) (*BodyExtrinsics, error) { return NewBodyExtrinsics([]Extrinsic{}), nil } - err := scale2.Unmarshal(b, &exts) + err := scale.Unmarshal(b, &exts) if err != nil { return nil, err } @@ -57,7 +58,7 @@ func NewBodyExtrinsicsFromEncodedBytes(exts [][]byte) (*BodyExtrinsics, error) { // encoding of the number of items, followed by each item's encoding // concatenated in turn. // https://substrate.dev/docs/en/knowledgebase/advanced/codec#vectors-lists-series-sets - enc, err := scale2.Marshal(big.NewInt(int64(len(exts)))) + enc, err := scale.Marshal(big.NewInt(int64(len(exts)))) if err != nil { return nil, err } @@ -86,13 +87,13 @@ func NewBodyExtrinsicsFromExtrinsicStrings(ss []string) (*BodyExtrinsics, error) return NewBodyExtrinsics(BytesArrayToExtrinsics(exts)), nil } -func (b *BodyExtrinsics) AsSCALEEncodedBody() (Body, error) { - encodedBody, err := scale2.Marshal(ExtrinsicsArrayToBytesArray(*b)) +func (b *BodyExtrinsics) AsSCALEEncodedBody() ([]byte, error) { + encodedBody, err := scale.Marshal(ExtrinsicsArrayToBytesArray(*b)) if err != nil { return nil, err } - return Body(encodedBody), nil + return encodedBody, nil } func (b *BodyExtrinsics) DeepCopy() BodyExtrinsics { @@ -117,7 +118,7 @@ func (b *BodyExtrinsics) HasExtrinsic(target Extrinsic) (bool, error) { } //otherwise try to encode and compare - encext, err := scale2.Marshal(currext) + encext, err := scale.Marshal(currext) if err != nil { return false, fmt.Errorf("fail while scale encode: %w", err) } @@ -137,7 +138,7 @@ func (b *BodyExtrinsics) AsEncodedExtrinsics() ([]Extrinsic, error) { var err error for i, ext := range decodedExts { - ret[i], err = scale2.Marshal(ext) + ret[i], err = scale.Marshal(ext) if err != nil { return nil, err } @@ -145,3 +146,13 @@ func (b *BodyExtrinsics) AsEncodedExtrinsics() ([]Extrinsic, error) { return BytesArrayToExtrinsics(ret), nil } + +// AsOptional returns the Body as an optional.Body +func (b *BodyExtrinsics) AsOptional() (*optional.Body, error) { + encodedBody, err := b.AsSCALEEncodedBody() + if err != nil { + return nil, err + } + ob := optional.CoreBody([]byte(encodedBody)) + return optional.NewBody(true, ob), nil +} diff --git a/dot/types/body_extrinsics_test.go b/dot/types/body_extrinsics_test.go new file mode 100644 index 0000000000..3e3c3525b9 --- /dev/null +++ b/dot/types/body_extrinsics_test.go @@ -0,0 +1,81 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + +package types + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/stretchr/testify/require" +) + +func TestBodyExtrinsicsToSCALEEncodedBody(t *testing.T) { + exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} + + bodyExtrinsicsBefore := NewBodyExtrinsics(exts) + scaleEncodedBody, err := bodyExtrinsicsBefore.AsSCALEEncodedBody() + require.NoError(t, err) + + bodyExtrinsicsAfter, err := NewBodyExtrinsicsFromBytes(scaleEncodedBody) + require.NoError(t, err) + + require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) +} + +func TestHasExtrinsics(t *testing.T) { + exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} + + bodyExtrinsics := NewBodyExtrinsics(exts) + + found, err := bodyExtrinsics.HasExtrinsic(Extrinsic{1, 2, 3}) + require.NoError(t, err) + require.True(t, found) +} + +func TestBodyExtrinsicsFromEncodedBytes(t *testing.T) { + exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} + + bodyExtrinsicsBefore := NewBodyExtrinsics(exts) + + encodeExtrinsics, err := bodyExtrinsicsBefore.AsEncodedExtrinsics() + require.NoError(t, err) + + encodedBytes := ExtrinsicsArrayToBytesArray(encodeExtrinsics) + + bodyExtrinsicsAfter, err := NewBodyExtrinsicsFromEncodedBytes(encodedBytes) + require.NoError(t, err) + + require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) +} + +func TestBodyExtrinsicsFromExtrinsicStrings(t *testing.T) { + exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} + extStrings := []string{} + + for _, ext := range exts { + extStrings = append(extStrings, common.BytesToHex(ext)) + } + + fmt.Println(extStrings) + + bodyFromByteExtrinsics := NewBodyExtrinsics(exts) + bodyFromStringExtrinsics, err := NewBodyExtrinsicsFromExtrinsicStrings(extStrings) + require.NoError(t, err) + + require.Equal(t, bodyFromByteExtrinsics, bodyFromStringExtrinsics) +} diff --git a/dot/types/body_test.go b/dot/types/body_test.go deleted file mode 100644 index d34b22716b..0000000000 --- a/dot/types/body_test.go +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package types - -import ( - "testing" - - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/scale" - "github.com/stretchr/testify/require" -) - -func TestBodyToExtrinsics(t *testing.T) { - exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - - body, err := NewBodyFromExtrinsics(exts) - require.NoError(t, err) - - res, err := body.AsExtrinsics() - require.NoError(t, err) - require.Equal(t, exts, res) -} - -func TestNewBodyFromExtrinsicStrings(t *testing.T) { - strs := []string{"0xabcd", "0xff9988", "0x7654acdf"} - body, err := NewBodyFromExtrinsicStrings(strs) - require.NoError(t, err) - - exts, err := body.AsExtrinsics() - require.NoError(t, err) - - for i, e := range exts { - b, err := common.HexToBytes(strs[i]) - require.NoError(t, err) - require.Equal(t, []byte(e), b) - } -} - -func TestNewBodyFromExtrinsicStrings_Mixed(t *testing.T) { - strs := []string{"0xabcd", "0xff9988", "noot"} - body, err := NewBodyFromExtrinsicStrings(strs) - require.NoError(t, err) - - exts, err := body.AsExtrinsics() - require.NoError(t, err) - - for i, e := range exts { - b, err := common.HexToBytes(strs[i]) - if err == common.ErrNoPrefix { - b = []byte(strs[i]) - } else if err != nil { - t.Fatal(err) - } - require.Equal(t, []byte(e), b) - } -} - -func TestBody_EncodedExtrinsics(t *testing.T) { - exts := [][]byte{{16, 1, 3, 5, 7}, {12, 9, 1, 2}, {12, 3, 4, 5}} - body, err := NewBodyFromEncodedBytes(exts) - require.NoError(t, err) - - res, err := body.AsEncodedExtrinsics() - require.NoError(t, err) - require.Equal(t, BytesArrayToExtrinsics(exts), res) -} - -func TestBody_FindEncodedExtrinsic(t *testing.T) { - target := Extrinsic([]byte{0x1, 0x2, 0x3, 0x4, 0x5}) - - body1, err := NewBodyFromExtrinsics([]Extrinsic{}) - require.Nil(t, err) - - decodedTarget, err := scale.Decode(target, []byte{}) - require.Nil(t, err) - - body2, err := NewBodyFromExtrinsics([]Extrinsic{decodedTarget.([]byte)}) - require.Nil(t, err) - - tests := []struct { - body *Body - expect bool - }{ - { - body: body1, - expect: false, - }, - { - body: body2, - expect: true, - }, - } - - for _, test := range tests { - res, err := test.body.HasExtrinsic(target) - require.Nil(t, err) - require.Equal(t, test.expect, res) - } -} From 51cbe83b6a24d935a04c6df8ca35d4415dd68b8f Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 28 Sep 2021 17:37:55 +0530 Subject: [PATCH 3/8] Rename BodyExtrinsics to Body Rename BodyExtrinsics to Body and accordingly rename its methods --- dot/core/interface.go | 2 +- dot/core/messages_test.go | 2 +- dot/core/mocks/block_state.go | 8 ++--- dot/core/service_test.go | 24 +++++++-------- dot/digest/digest_test.go | 2 +- dot/network/message.go | 2 +- dot/network/message_test.go | 4 +-- dot/network/sync_test.go | 6 ++-- dot/network/test_helpers.go | 4 +-- dot/rpc/modules/chain_test.go | 4 +-- dot/rpc/modules/childstate_test.go | 2 +- dot/rpc/modules/state_test.go | 2 +- dot/rpc/modules/system_test.go | 2 +- dot/rpc/subscription/listeners_test.go | 4 +-- dot/state/block.go | 8 ++--- dot/state/block_data_test.go | 2 +- dot/state/block_test.go | 24 +++++++-------- dot/state/storage_test.go | 2 +- dot/state/test_helpers.go | 10 +++--- dot/sync/interface.go | 2 +- dot/sync/message_test.go | 2 +- dot/sync/syncer.go | 2 +- dot/sync/syncer_test.go | 4 +-- dot/sync/test_helpers.go | 4 +-- dot/types/block.go | 6 ++-- dot/types/block_data.go | 2 +- dot/types/block_data_test.go | 4 +-- dot/types/block_test.go | 16 +++++----- dot/types/body_extrinsics.go | 42 +++++++++++++------------- dot/types/body_extrinsics_test.go | 14 ++++----- lib/babe/build.go | 4 +-- lib/babe/median_test.go | 4 +-- lib/grandpa/message_handler_test.go | 8 ++--- lib/grandpa/message_test.go | 2 +- lib/grandpa/message_tracker_test.go | 4 +-- lib/grandpa/network_test.go | 2 +- lib/grandpa/round_test.go | 2 +- lib/runtime/life/exports_test.go | 6 ++-- lib/runtime/wasmer/exports_test.go | 18 +++++------ tests/utils/chain.go | 2 +- 40 files changed, 131 insertions(+), 133 deletions(-) diff --git a/dot/core/interface.go b/dot/core/interface.go index cfac78e810..dd017a96d5 100644 --- a/dot/core/interface.go +++ b/dot/core/interface.go @@ -48,7 +48,7 @@ type BlockState interface { UnregisterFinalisedChannel(id byte) HighestCommonAncestor(a, b common.Hash) (common.Hash, error) SubChain(start, end common.Hash) ([]common.Hash, error) - GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) + GetBlockBody(hash common.Hash) (*types.Body, error) HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error GetRuntime(*common.Hash) (runtime.Instance, error) StoreRuntime(common.Hash, runtime.Instance) diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 3e610896c8..8f6e244019 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -103,7 +103,7 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) { ParentHash: s.blockState.BestBlockHash(), Digest: digest, }, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } expected := &network.BlockAnnounceMessage{ diff --git a/dot/core/mocks/block_state.go b/dot/core/mocks/block_state.go index 8c81fad6c3..061fc2db47 100644 --- a/dot/core/mocks/block_state.go +++ b/dot/core/mocks/block_state.go @@ -180,15 +180,15 @@ func (_m *MockBlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash { } // GetBlockBody provides a mock function with given fields: hash -func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) { +func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { ret := _m.Called(hash) - var r0 *types.BodyExtrinsics - if rf, ok := ret.Get(0).(func(common.Hash) *types.BodyExtrinsics); ok { + var r0 *types.Body + if rf, ok := ret.Get(0).(func(common.Hash) *types.Body); ok { r0 = rf(hash) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*types.BodyExtrinsics) + r0 = ret.Get(0).(*types.Body) } } diff --git a/dot/core/service_test.go b/dot/core/service_test.go index 49477e7236..12c0d34b10 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -64,7 +64,7 @@ func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, dept Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))), Digest: types.NewDigest(), }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } previousHash = block.Header.Hash() @@ -127,7 +127,7 @@ func TestAnnounceBlock(t *testing.T) { ParentHash: s.blockState.BestBlockHash(), Digest: digest, }, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } expected := &network.BlockAnnounceMessage{ @@ -311,7 +311,7 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) { Number: big.NewInt(0).Add(ancestor.Header.Number, big.NewInt(1)), Digest: digest, }, - Body: types.BodyExtrinsics([]types.Extrinsic{tx}), + Body: types.Body([]types.Extrinsic{tx}), } s.blockState.StoreRuntime(block.Header.Hash(), rt) @@ -375,7 +375,7 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) { } err := s.maintainTransactionPool(&types.Block{ - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), }) require.NoError(t, err) @@ -421,7 +421,7 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) { } err := s.maintainTransactionPool(&types.Block{ - Body: types.BodyExtrinsics([]types.Extrinsic{txs[0].Extrinsic}), + Body: types.Body([]types.Extrinsic{txs[0].Extrinsic}), }) require.NoError(t, err) @@ -504,7 +504,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) { }) require.NoError(t, err) - body1 := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Old Runtime")}) + body1 := types.NewBody([]types.Extrinsic{[]byte("Old Runtime")}) newBlock1 := &types.Block{ Header: types.Header{ ParentHash: hash, @@ -513,7 +513,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) { Body: *body1, } - body2 := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Updated Runtime")}) + body2 := types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}) require.NoError(t, err) newBlockRTUpdate := &types.Block{ @@ -597,7 +597,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) { codeHashBefore := parentRt.GetCodeHash() blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution - body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte("Updated Runtime")}) + body := types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}) newBlock := &types.Block{ Header: types.Header{ ParentHash: blockHash, @@ -647,7 +647,7 @@ func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -677,7 +677,7 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -702,7 +702,7 @@ func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -787,7 +787,7 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by testBlock := &types.Block{ Header: *header, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) diff --git a/dot/digest/digest_test.go b/dot/digest/digest_test.go index 51bf32dcf6..39ac8896cb 100644 --- a/dot/digest/digest_test.go +++ b/dot/digest/digest_test.go @@ -58,7 +58,7 @@ func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, dept Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } previousHash = block.Header.Hash() diff --git a/dot/network/message.go b/dot/network/message.go index 18a563c9d3..a6671acaa1 100644 --- a/dot/network/message.go +++ b/dot/network/message.go @@ -326,7 +326,7 @@ func protobufToBlockData(pbd *pb.BlockData) (*types.BlockData, error) { } if pbd.Body != nil { - body, err := types.NewBodyExtrinsicsFromEncodedBytes(pbd.Body) + body, err := types.NewBodyFromEncodedBytes(pbd.Body) if err != nil { return nil, err } diff --git a/dot/network/message_test.go b/dot/network/message_test.go index 0ae46fe56b..cdb4fbd39b 100644 --- a/dot/network/message_test.go +++ b/dot/network/message_test.go @@ -179,7 +179,7 @@ func TestEncodeBlockResponseMessage_WithBody(t *testing.T) { require.NoError(t, err) exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}} - body := types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) + body := types.NewBody(types.BytesArrayToExtrinsics(exts)) bd := &types.BlockData{ Hash: hash, @@ -225,7 +225,7 @@ func TestEncodeBlockResponseMessage_WithAll(t *testing.T) { require.NoError(t, err) exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}} - body := types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) + body := types.NewBody(types.BytesArrayToExtrinsics(exts)) bd := &types.BlockData{ Hash: hash, diff --git a/dot/network/sync_test.go b/dot/network/sync_test.go index be26d9be80..f816b55d6d 100644 --- a/dot/network/sync_test.go +++ b/dot/network/sync_test.go @@ -90,7 +90,7 @@ func TestSyncQueue_PushResponse(t *testing.T) { testHeader := types.NewEmptyHeader() testHeader.Number = big.NewInt(int64(77 + i)) - body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0}}) + body := types.NewBody([]types.Extrinsic{[]byte{0}}) msg.BlockData = append(msg.BlockData, &types.BlockData{ Header: testHeader, Body: body, @@ -373,7 +373,7 @@ func TestSyncQueue_handleResponseQueue_responseQueueAhead(t *testing.T) { q.responses = append(q.responses, &types.BlockData{ Hash: testHeader0.Hash(), Header: testHeader0, - Body: types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}), + Body: types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}}), Receipt: nil, MessageQueue: nil, Justification: nil, @@ -401,7 +401,7 @@ func TestSyncQueue_processBlockResponses(t *testing.T) { { Hash: testHeader0.Hash(), Header: testHeader0, - Body: types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}), + Body: types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}}), Receipt: nil, MessageQueue: nil, Justification: nil, diff --git a/dot/network/test_helpers.go b/dot/network/test_helpers.go index 0289c7a07e..8083043a65 100644 --- a/dot/network/test_helpers.go +++ b/dot/network/test_helpers.go @@ -73,9 +73,7 @@ func testBlockResponseMessage() *BlockResponseMessage { Digest: types.NewDigest(), } - // body, err := types.NewBodyExtrinsicsFromBytes([]byte{4, 4, 2}) - // logger.Error("Failed to get extrinsics from bytes:", err) - body := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{4, 4, 2}}) + body := types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}}) msg.BlockData = append(msg.BlockData, &types.BlockData{ Hash: testHeader.Hash(), diff --git a/dot/rpc/modules/chain_test.go b/dot/rpc/modules/chain_test.go index 6ade88fcc3..77f59530a7 100644 --- a/dot/rpc/modules/chain_test.go +++ b/dot/rpc/modules/chain_test.go @@ -362,7 +362,7 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti blockHash0 := header0.Hash() block0 := &types.Block{ Header: *header0, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err := bs.AddBlock(block0) @@ -385,7 +385,7 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti block1 := &types.Block{ Header: *header1, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block1 to the DB diff --git a/dot/rpc/modules/childstate_test.go b/dot/rpc/modules/childstate_test.go index 9189c35461..e0e1ba71fa 100644 --- a/dot/rpc/modules/childstate_test.go +++ b/dot/rpc/modules/childstate_test.go @@ -101,7 +101,7 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) { Number: big.NewInt(0).Add(big.NewInt(1), bb.Header.Number), StateRoot: stateRoot, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = st.Block.AddBlock(b) diff --git a/dot/rpc/modules/state_test.go b/dot/rpc/modules/state_test.go index e6ffb01414..a3470274f1 100644 --- a/dot/rpc/modules/state_test.go +++ b/dot/rpc/modules/state_test.go @@ -498,7 +498,7 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) { Number: big.NewInt(2), StateRoot: sr1, }, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{}}), } err = chain.Block.AddBlock(b) diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index ffaf7cfbe3..7239d317ca 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -312,7 +312,7 @@ func setupSystemModule(t *testing.T) *SystemModule { ParentHash: chain.Block.BestBlockHash(), StateRoot: ts.MustRoot(), }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, }) require.NoError(t, err) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index f74feb8be7..437a76f44c 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -111,7 +111,7 @@ func TestBlockListener_Listen(t *testing.T) { } //block := types.NewEmptyBlock() - block := types.NewBlock(*types.NewEmptyHeader(), *new(types.BodyExtrinsics)) + block := types.NewBlock(*types.NewEmptyHeader(), *new(types.Body)) block.Header.Number = big.NewInt(1) go bl.Listen() @@ -213,7 +213,7 @@ func TestExtrinsicSubmitListener_Listen(t *testing.T) { header := types.NewEmptyHeader() exts := []types.Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - body := types.NewBodyExtrinsics(exts) + body := types.NewBody(exts) block := &types.Block{ Header: *header, diff --git a/dot/state/block.go b/dot/state/block.go index 51ec1d8ff2..7ad93a3ee2 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -119,7 +119,7 @@ func NewBlockStateFromGenesis(db chaindb.Database, header *types.Header) (*Block return nil, err } - if err := bs.SetBlockBody(header.Hash(), types.NewBodyExtrinsics([]types.Extrinsic{})); err != nil { + if err := bs.SetBlockBody(header.Hash(), types.NewBody([]types.Extrinsic{})); err != nil { return nil, err } @@ -349,17 +349,17 @@ func (bs *BlockState) HasBlockBody(hash common.Hash) (bool, error) { } // GetBlockBody will return Body for a given hash -func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.BodyExtrinsics, error) { +func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { data, err := bs.db.Get(blockBodyKey(hash)) if err != nil { return nil, err } - return types.NewBodyExtrinsicsFromBytes(data) + return types.NewBodyFromBytes(data) } // SetBlockBody will add a block body to the db -func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.BodyExtrinsics) error { +func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.Body) error { optionalBody, err := body.AsOptional() if err != nil { return err diff --git a/dot/state/block_data_test.go b/dot/state/block_data_test.go index 96abea8375..76278053d3 100644 --- a/dot/state/block_data_test.go +++ b/dot/state/block_data_test.go @@ -57,7 +57,7 @@ func TestGetSet_ReceiptMessageQueue_Justification(t *testing.T) { a := []byte("asdf") b := []byte("ghjkl") c := []byte("qwerty") - body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + body, err := types.NewBodyFromBytes([]byte{}) require.NoError(t, err) bds := []*types.BlockData{{ diff --git a/dot/state/block_test.go b/dot/state/block_test.go index 2cc1430db9..76046a2232 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -93,7 +93,7 @@ func TestGetBlockByNumber(t *testing.T) { block := &types.Block{ Header: *blockHeader, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // AddBlock also sets mapping [blockNumber : hash] in DB @@ -118,7 +118,7 @@ func TestAddBlock(t *testing.T) { blockHash0 := header0.Hash() block0 := &types.Block{ Header: *header0, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block0 to the DB @@ -135,7 +135,7 @@ func TestAddBlock(t *testing.T) { block1 := &types.Block{ Header: *header1, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } // Add the block1 to the DB @@ -185,7 +185,7 @@ func TestGetSlotForBlock(t *testing.T) { Number: big.NewInt(int64(1)), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = bs.AddBlock(block) @@ -251,7 +251,7 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) { ParentHash: bestHash, Number: big.NewInt(0).Add(bestHeader.Number, big.NewInt(1)), }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = bs.AddBlock(newBlock) @@ -286,7 +286,7 @@ func TestFinalizedHash(t *testing.T) { err = bs.AddBlock(&types.Block{ Header: *header, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, }) require.NoError(t, err) @@ -385,7 +385,7 @@ func TestGetHashByNumber(t *testing.T) { block := &types.Block{ Header: *header, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block) @@ -405,7 +405,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { ParentHash: testGenesisHeader.Hash(), } - blockbody1a := types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}) + blockbody1a := types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}) block1a := &types.Block{ Header: *header1a, Body: *blockbody1a, @@ -425,7 +425,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { ExtrinsicsRoot: common.Hash{99}, } - blockbody1b := types.NewBodyExtrinsics( + blockbody1b := types.NewBody( []types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, ) @@ -451,7 +451,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2b := &types.Block{ Header: *header2b, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block2b) @@ -474,7 +474,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2a := &types.Block{ Header: *header2a, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block2a) @@ -488,7 +488,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block3a := &types.Block{ Header: *header3a, - Body: *types.NewBodyExtrinsics([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), } err = bs.AddBlock(block3a) diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index 49d44088f2..3a78043bf9 100644 --- a/dot/state/storage_test.go +++ b/dot/state/storage_test.go @@ -57,7 +57,7 @@ func TestStorage_GetStorageByBlockHash(t *testing.T) { err = storage.StoreTrie(ts, nil) require.NoError(t, err) - body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + body, err := types.NewBodyFromBytes([]byte{}) require.NoError(t, err) block := &types.Block{ diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index 137e33f82c..64e8f87a60 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -83,7 +83,7 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int) ([]*types StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } currentChain = append(currentChain, &block.Header) @@ -123,7 +123,7 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int) ([]*types StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } branchChains = append(branchChains, &block.Header) @@ -162,7 +162,7 @@ func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, dep Number: big.NewInt(int64(i)), StateRoot: trie.EmptyHash, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } hash := block.Header.Hash() @@ -203,7 +203,7 @@ func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, dep StateRoot: trie.EmptyHash, Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } hash := block.Header.Hash() @@ -236,7 +236,7 @@ func generateBlockWithRandomTrie(t *testing.T, serv *Service, parent *common.Has parent = &bb } - body, err := types.NewBodyExtrinsicsFromBytes([]byte{}) + body, err := types.NewBodyFromBytes([]byte{}) require.NoError(t, err) block := &types.Block{ diff --git a/dot/sync/interface.go b/dot/sync/interface.go index e574cdf790..5136591afb 100644 --- a/dot/sync/interface.go +++ b/dot/sync/interface.go @@ -35,7 +35,7 @@ type BlockState interface { CompareAndSetBlockData(bd *types.BlockData) error GetBlockByNumber(*big.Int) (*types.Block, error) HasBlockBody(hash common.Hash) (bool, error) - GetBlockBody(common.Hash) (*types.BodyExtrinsics, error) + GetBlockBody(common.Hash) (*types.Body, error) SetHeader(*types.Header) error GetHeader(common.Hash) (*types.Header, error) HasHeader(hash common.Hash) (bool, error) diff --git a/dot/sync/message_test.go b/dot/sync/message_test.go index 363cf6e610..a2f37ca6b6 100644 --- a/dot/sync/message_test.go +++ b/dot/sync/message_test.go @@ -26,7 +26,7 @@ func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) { StateRoot: trie.EmptyHash, Digest: types.NewDigest(), }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } previousHash = block.Header.Hash() diff --git a/dot/sync/syncer.go b/dot/sync/syncer.go index 4b12efed7f..8942344316 100644 --- a/dot/sync/syncer.go +++ b/dot/sync/syncer.go @@ -276,7 +276,7 @@ func (s *Service) handleHeader(header *types.Header) error { } // handleBody handles block bodies included in BlockResponses -func (s *Service) handleBody(body *types.BodyExtrinsics) { +func (s *Service) handleBody(body *types.Body) { for _, ext := range *body { s.transactionState.RemoveExtrinsic(ext) } diff --git a/dot/sync/syncer_test.go b/dot/sync/syncer_test.go index 6b307ffd12..30974babb9 100644 --- a/dot/sync/syncer_test.go +++ b/dot/sync/syncer_test.go @@ -158,7 +158,7 @@ func TestRemoveIncludedExtrinsics(t *testing.T) { require.NoError(t, err) exts := []types.Extrinsic{ext} - body := types.NewBodyExtrinsics(exts) + body := types.NewBody(exts) bd := &types.BlockData{ Body: body, } @@ -264,7 +264,7 @@ func TestSyncer_HandleJustification(t *testing.T) { err = syncer.blockState.AddBlock(&types.Block{ Header: *header, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, }) require.NoError(t, err) diff --git a/dot/sync/test_helpers.go b/dot/sync/test_helpers.go index fbc1844f2b..06f15a5377 100644 --- a/dot/sync/test_helpers.go +++ b/dot/sync/test_helpers.go @@ -190,7 +190,7 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e inExt := exts - var body *types.BodyExtrinsics + var body *types.Body if ext != nil { var txn *transaction.Validity externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, ext...)) @@ -205,7 +205,7 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e require.NoError(t, err) } else { - body = types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)) + body = types.NewBody(types.BytesArrayToExtrinsics(exts)) } // apply each inherent extrinsic diff --git a/dot/types/block.go b/dot/types/block.go index a0b4c9d314..d4f10cd091 100644 --- a/dot/types/block.go +++ b/dot/types/block.go @@ -23,11 +23,11 @@ import ( // Block defines a state block type Block struct { Header Header - Body BodyExtrinsics + Body Body } // NewBlock returns a new Block -func NewBlock(header Header, body BodyExtrinsics) Block { +func NewBlock(header Header, body Body) Block { return Block{ Header: header, Body: body, @@ -38,7 +38,7 @@ func NewBlock(header Header, body BodyExtrinsics) Block { func NewEmptyBlock() Block { return Block{ Header: *NewEmptyHeader(), - Body: BodyExtrinsics(nil), + Body: Body(nil), } } diff --git a/dot/types/block_data.go b/dot/types/block_data.go index b14ca07c89..6f91c55c84 100644 --- a/dot/types/block_data.go +++ b/dot/types/block_data.go @@ -29,7 +29,7 @@ import ( type BlockData struct { Hash common.Hash Header *Header - Body *BodyExtrinsics + Body *Body Receipt *[]byte MessageQueue *[]byte Justification *[]byte diff --git a/dot/types/block_data_test.go b/dot/types/block_data_test.go index a61ec84d36..df1babc8d3 100644 --- a/dot/types/block_data_test.go +++ b/dot/types/block_data_test.go @@ -132,7 +132,7 @@ func TestBlockDataEncodeAndDecodeBody(t *testing.T) { bd := BlockData{ Hash: common.NewHash([]byte{0}), Header: nil, - Body: NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), + Body: NewBody([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), Receipt: nil, MessageQueue: nil, Justification: nil, @@ -168,7 +168,7 @@ func TestBlockDataEncodeAndDecodeAll(t *testing.T) { bd := BlockData{ Hash: hash, Header: headerVdt, - Body: NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), + Body: NewBody([]Extrinsic{[]byte{0xa, 0xb, 0xc, 0xd}}), Receipt: &[]byte{1}, MessageQueue: &[]byte{2}, Justification: &[]byte{3}, diff --git a/dot/types/block_test.go b/dot/types/block_test.go index 0ac3c681bb..4580c862b4 100644 --- a/dot/types/block_test.go +++ b/dot/types/block_test.go @@ -32,7 +32,7 @@ func TestEmptyBlock(t *testing.T) { isEmpty := block.Empty() require.True(t, isEmpty) - block = NewBlock(*NewEmptyHeader(), BodyExtrinsics{}) + block = NewBlock(*NewEmptyHeader(), Body{}) isEmpty = block.Empty() require.True(t, isEmpty) @@ -48,11 +48,11 @@ func TestEmptyBlock(t *testing.T) { header, err := NewHeader(parentHash, stateRoot, extrinsicsRoot, big.NewInt(1), NewDigest()) require.NoError(t, err) - block = NewBlock(*header, BodyExtrinsics{}) + block = NewBlock(*header, Body{}) isEmpty = block.Empty() require.False(t, isEmpty) - block = NewBlock(*NewEmptyHeader(), *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) + block = NewBlock(*NewEmptyHeader(), *NewBody([]Extrinsic{[]byte{4, 1}})) isEmpty = block.Empty() require.False(t, isEmpty) } @@ -73,14 +73,14 @@ func TestEncodeAndDecodeBlock(t *testing.T) { header, err := NewHeader(parentHash, stateRoot, extrinsicsRoot, big.NewInt(1), NewDigest()) require.NoError(t, err) - block := NewBlock(*header, *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) + block := NewBlock(*header, *NewBody([]Extrinsic{[]byte{4, 1}})) enc, err := scale.Marshal(block) require.NoError(t, err) require.Equal(t, expected, enc) - dec := NewBlock(*NewEmptyHeader(), *new(BodyExtrinsics)) + dec := NewBlock(*NewEmptyHeader(), *new(Body)) err = scale.Unmarshal(enc, &dec) require.NoError(t, err) if dec.Header.Number != nil { @@ -91,7 +91,7 @@ func TestEncodeAndDecodeBlock(t *testing.T) { func TestDeepCopyBlock(t *testing.T) { data := []byte{69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 4, 39, 71, 171, 124, 13, 195, 139, 127, 42, 251, 168, 43, 213, 226, 214, 172, 239, 140, 49, 224, 152, 0, 246, 96, 183, 94, 200, 74, 112, 5, 9, 159, 3, 23, 10, 46, 117, 151, 183, 183, 227, 216, 76, 5, 57, 29, 19, 154, 98, 177, 87, 231, 135, 134, 216, 192, 130, 242, 157, 207, 76, 17, 19, 20, 0, 0} - block := NewBlock(*NewEmptyHeader(), *new(BodyExtrinsics)) + block := NewBlock(*NewEmptyHeader(), *new(Body)) err := scale.Unmarshal(data, &block) if err != nil { @@ -108,14 +108,14 @@ func TestMustEncodeBlock(t *testing.T) { h1, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), NewDigest()) require.NoError(t, err) - b1 := NewBlock(*h1, *NewBodyExtrinsics([]Extrinsic{[]byte{4, 1}})) + b1 := NewBlock(*h1, *NewBody([]Extrinsic{[]byte{4, 1}})) enc, err := b1.Encode() require.NoError(t, err) h2, err := NewHeader(common.Hash{0x1, 0x2}, common.Hash{}, common.Hash{}, big.NewInt(0), NewDigest()) require.NoError(t, err) - b2 := NewBlock(*h2, *NewBodyExtrinsics([]Extrinsic{[]byte{0xa, 0xb}})) + b2 := NewBlock(*h2, *NewBody([]Extrinsic{[]byte{0xa, 0xb}})) enc2, err := b2.Encode() require.NoError(t, err) diff --git a/dot/types/body_extrinsics.go b/dot/types/body_extrinsics.go index fc8ae24670..23d6014238 100644 --- a/dot/types/body_extrinsics.go +++ b/dot/types/body_extrinsics.go @@ -26,21 +26,21 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -// BodyExtrinsics is the extrinsics(not encoded) inside a state block. -type BodyExtrinsics []Extrinsic +// Body is the extrinsics(not encoded) inside a state block. +type Body []Extrinsic -// NewBodyExtrinsics returns a BodyExtrinsics from an Extrinsic array. -func NewBodyExtrinsics(e []Extrinsic) *BodyExtrinsics { - body := BodyExtrinsics(e) +// NewBody returns a BodyExtrinsics from an Extrinsic array. +func NewBody(e []Extrinsic) *Body { + body := Body(e) return &body } -// NewBodyExtrinsics returns a BodyExtrinsics from a SCALE encoded byte array. -func NewBodyExtrinsicsFromBytes(b []byte) (*BodyExtrinsics, error) { +// NewBodyFromBytes returns a BodyExtrinsics from a SCALE encoded byte array. +func NewBodyFromBytes(b []byte) (*Body, error) { exts := [][]byte{} if len(b) == 0 { - return NewBodyExtrinsics([]Extrinsic{}), nil + return NewBody([]Extrinsic{}), nil } err := scale.Unmarshal(b, &exts) @@ -48,12 +48,12 @@ func NewBodyExtrinsicsFromBytes(b []byte) (*BodyExtrinsics, error) { return nil, err } - return NewBodyExtrinsics(BytesArrayToExtrinsics(exts)), nil + return NewBody(BytesArrayToExtrinsics(exts)), nil } -// NewBodyExtrinsicsFromEncodedBytes returns a new Body from a slice of byte slices that are +// NewBodyFromEncodedBytes returns a new Body from a slice of byte slices that are // SCALE encoded extrinsics -func NewBodyExtrinsicsFromEncodedBytes(exts [][]byte) (*BodyExtrinsics, error) { +func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { // A collection of same-typed values is encoded, prefixed with a compact // encoding of the number of items, followed by each item's encoding // concatenated in turn. @@ -67,12 +67,12 @@ func NewBodyExtrinsicsFromEncodedBytes(exts [][]byte) (*BodyExtrinsics, error) { enc = append(enc, ext...) } - return NewBodyExtrinsicsFromBytes(enc) + return NewBodyFromBytes(enc) } -// NewBodyExtrinsicsFromExtrinsicStrings creates a block body given an array of hex-encoded +// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded // 0x-prefixed strings. -func NewBodyExtrinsicsFromExtrinsicStrings(ss []string) (*BodyExtrinsics, error) { +func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { exts := [][]byte{} for _, s := range ss { b, err := common.HexToBytes(s) @@ -84,10 +84,10 @@ func NewBodyExtrinsicsFromExtrinsicStrings(ss []string) (*BodyExtrinsics, error) exts = append(exts, b) } - return NewBodyExtrinsics(BytesArrayToExtrinsics(exts)), nil + return NewBody(BytesArrayToExtrinsics(exts)), nil } -func (b *BodyExtrinsics) AsSCALEEncodedBody() ([]byte, error) { +func (b *Body) AsSCALEEncodedBody() ([]byte, error) { encodedBody, err := scale.Marshal(ExtrinsicsArrayToBytesArray(*b)) if err != nil { return nil, err @@ -96,15 +96,15 @@ func (b *BodyExtrinsics) AsSCALEEncodedBody() ([]byte, error) { return encodedBody, nil } -func (b *BodyExtrinsics) DeepCopy() BodyExtrinsics { +func (b *Body) DeepCopy() Body { newExtrinsics := make([]Extrinsic, len([]Extrinsic(*b))) copy(newExtrinsics, []Extrinsic(*b)) - return BodyExtrinsics(newExtrinsics) + return Body(newExtrinsics) } // HasExtrinsic returns true if body contains target Extrisic -func (b *BodyExtrinsics) HasExtrinsic(target Extrinsic) (bool, error) { +func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { exts := *b // goes through the decreasing order due to the fact that extrinsicsToBody @@ -132,7 +132,7 @@ func (b *BodyExtrinsics) HasExtrinsic(target Extrinsic) (bool, error) { } // AsEncodedExtrinsics decodes the body into an array of SCALE encoded extrinsics -func (b *BodyExtrinsics) AsEncodedExtrinsics() ([]Extrinsic, error) { +func (b *Body) AsEncodedExtrinsics() ([]Extrinsic, error) { decodedExts := *b ret := make([][]byte, len(decodedExts)) var err error @@ -148,7 +148,7 @@ func (b *BodyExtrinsics) AsEncodedExtrinsics() ([]Extrinsic, error) { } // AsOptional returns the Body as an optional.Body -func (b *BodyExtrinsics) AsOptional() (*optional.Body, error) { +func (b *Body) AsOptional() (*optional.Body, error) { encodedBody, err := b.AsSCALEEncodedBody() if err != nil { return nil, err diff --git a/dot/types/body_extrinsics_test.go b/dot/types/body_extrinsics_test.go index 3e3c3525b9..ed03773b48 100644 --- a/dot/types/body_extrinsics_test.go +++ b/dot/types/body_extrinsics_test.go @@ -27,11 +27,11 @@ import ( func TestBodyExtrinsicsToSCALEEncodedBody(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsicsBefore := NewBodyExtrinsics(exts) + bodyExtrinsicsBefore := NewBody(exts) scaleEncodedBody, err := bodyExtrinsicsBefore.AsSCALEEncodedBody() require.NoError(t, err) - bodyExtrinsicsAfter, err := NewBodyExtrinsicsFromBytes(scaleEncodedBody) + bodyExtrinsicsAfter, err := NewBodyFromBytes(scaleEncodedBody) require.NoError(t, err) require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) @@ -40,7 +40,7 @@ func TestBodyExtrinsicsToSCALEEncodedBody(t *testing.T) { func TestHasExtrinsics(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsics := NewBodyExtrinsics(exts) + bodyExtrinsics := NewBody(exts) found, err := bodyExtrinsics.HasExtrinsic(Extrinsic{1, 2, 3}) require.NoError(t, err) @@ -50,14 +50,14 @@ func TestHasExtrinsics(t *testing.T) { func TestBodyExtrinsicsFromEncodedBytes(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsicsBefore := NewBodyExtrinsics(exts) + bodyExtrinsicsBefore := NewBody(exts) encodeExtrinsics, err := bodyExtrinsicsBefore.AsEncodedExtrinsics() require.NoError(t, err) encodedBytes := ExtrinsicsArrayToBytesArray(encodeExtrinsics) - bodyExtrinsicsAfter, err := NewBodyExtrinsicsFromEncodedBytes(encodedBytes) + bodyExtrinsicsAfter, err := NewBodyFromEncodedBytes(encodedBytes) require.NoError(t, err) require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) @@ -73,8 +73,8 @@ func TestBodyExtrinsicsFromExtrinsicStrings(t *testing.T) { fmt.Println(extStrings) - bodyFromByteExtrinsics := NewBodyExtrinsics(exts) - bodyFromStringExtrinsics, err := NewBodyExtrinsicsFromExtrinsicStrings(extStrings) + bodyFromByteExtrinsics := NewBody(exts) + bodyFromStringExtrinsics, err := NewBodyFromExtrinsicStrings(extStrings) require.NoError(t, err) require.Equal(t, bodyFromByteExtrinsics, bodyFromStringExtrinsics) diff --git a/lib/babe/build.go b/lib/babe/build.go index e6768fd53c..95eb6e9bfe 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -359,7 +359,7 @@ func hasSlotEnded(slot Slot) bool { } // ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. -func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.BodyExtrinsics, error) { +func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { @@ -371,5 +371,5 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( extrinsics = append(extrinsics, decExt) } - return types.NewBodyExtrinsics(extrinsics), nil + return types.NewBody(extrinsics), nil } diff --git a/lib/babe/median_test.go b/lib/babe/median_test.go index b25a027dd1..2a1c51f021 100644 --- a/lib/babe/median_test.go +++ b/lib/babe/median_test.go @@ -107,7 +107,7 @@ func addBlocksToState(t *testing.T, babeService *Service, depth int, blockState Number: big.NewInt(int64(i)), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } arrivalTime := previousAT.Add(duration) @@ -176,7 +176,7 @@ func TestEstimateCurrentSlot(t *testing.T) { Number: big.NewInt(int64(1)), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } arrivalTime := time.Now().UnixNano() - slot.duration.Nanoseconds() diff --git a/lib/grandpa/message_handler_test.go b/lib/grandpa/message_handler_test.go index edfa77cef4..e4ec01bc1c 100644 --- a/lib/grandpa/message_handler_test.go +++ b/lib/grandpa/message_handler_test.go @@ -205,7 +205,7 @@ func TestMessageHandler_NeighbourMessage(t *testing.T) { err = digest.Add(types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()) require.NoError(t, err) - body, err := types.NewBodyExtrinsicsFromBytes([]byte{0}) + body, err := types.NewBodyFromBytes([]byte{0}) require.NoError(t, err) block := &types.Block{ @@ -269,7 +269,7 @@ func TestMessageHandler_CommitMessage_NoCatchUpRequest_ValidSig(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = st.Block.AddBlock(block) @@ -362,7 +362,7 @@ func TestMessageHandler_CatchUpRequest_WithResponse(t *testing.T) { Number: big.NewInt(2), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = st.Block.AddBlock(block) @@ -537,7 +537,7 @@ func TestMessageHandler_VerifyBlockJustification(t *testing.T) { err := st.Grandpa.SetNextChange(auths, big.NewInt(1)) require.NoError(t, err) - body, err := types.NewBodyExtrinsicsFromBytes([]byte{0}) + body, err := types.NewBodyFromBytes([]byte{0}) require.NoError(t, err) block := &types.Block{ diff --git a/lib/grandpa/message_test.go b/lib/grandpa/message_test.go index 96d3423d80..6e9824715c 100644 --- a/lib/grandpa/message_test.go +++ b/lib/grandpa/message_test.go @@ -113,7 +113,7 @@ func TestNewCatchUpResponse(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } hash := block.Header.Hash() diff --git a/lib/grandpa/message_tracker_test.go b/lib/grandpa/message_tracker_test.go index d9073f00bf..b4d3671a46 100644 --- a/lib/grandpa/message_tracker_test.go +++ b/lib/grandpa/message_tracker_test.go @@ -88,7 +88,7 @@ func TestMessageTracker_SendMessage(t *testing.T) { err = gs.blockState.(*state.BlockState).AddBlock(&types.Block{ Header: *next, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, }) require.NoError(t, err) @@ -134,7 +134,7 @@ func TestMessageTracker_ProcessMessage(t *testing.T) { err = gs.blockState.(*state.BlockState).AddBlock(&types.Block{ Header: *next, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, }) require.NoError(t, err) diff --git a/lib/grandpa/network_test.go b/lib/grandpa/network_test.go index 6f5f27cc00..98a9f5ed90 100644 --- a/lib/grandpa/network_test.go +++ b/lib/grandpa/network_test.go @@ -99,7 +99,7 @@ func TestSendNeighbourMessage(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } err = st.Block.AddBlock(block) diff --git a/lib/grandpa/round_test.go b/lib/grandpa/round_test.go index e7df7ee32b..3666770489 100644 --- a/lib/grandpa/round_test.go +++ b/lib/grandpa/round_test.go @@ -388,7 +388,7 @@ func TestPlayGrandpaRound_VaryingChain(t *testing.T) { time.Sleep(time.Millisecond * 10) block := &types.Block{ Header: *h, - Body: types.BodyExtrinsics{}, + Body: types.Body{}, } gs.blockState.(*state.BlockState).AddBlock(block) } diff --git a/lib/runtime/life/exports_test.go b/lib/runtime/life/exports_test.go index f59b9b4cd6..55fb294546 100644 --- a/lib/runtime/life/exports_test.go +++ b/lib/runtime/life/exports_test.go @@ -187,7 +187,7 @@ func buildBlock(t *testing.T, instance runtime.Instance) *types.Block { return &types.Block{ Header: *res, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } } @@ -256,7 +256,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xa35fb7f7616f5c979d48222b3d2fa7cb2331ef73954726714d91ca945cc34fd8"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -306,7 +306,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x9a87f6af64ef97aff2d31bebfdd59f8fe2ef6019278b634b2515a38f1c4c2420"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, _ = instance.ExecuteBlock(block) // TODO: fix diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index cbf0c361c9..03b260c64b 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -532,7 +532,7 @@ func buildBlockVdt(t *testing.T, instance runtime.Instance, parentHash common.Ha return &types.Block{ Header: *res, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } } @@ -684,7 +684,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x9a87f6af64ef97aff2d31bebfdd59f8fe2ef6019278b634b2515a38f1c4c2420"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -735,7 +735,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xa35fb7f7616f5c979d48222b3d2fa7cb2331ef73954726714d91ca945cc34fd8"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -781,7 +781,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock3784(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x52b7d4852fc648cb8f908901e1e36269593c25050c31718454bca74b69115d12"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -827,7 +827,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock901442(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x13483a4c148fff5f072e86b5af52bf031556514e9c87ea19f9e31e7b13c0c414"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -873,7 +873,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1377831(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x7f3ea0ed63b4053d9b75e7ee3e5b3f6ce916e8f59b7b6c5e966b7a56ea0a563a"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -920,7 +920,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1482003(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xdf5da95780b77e83ad0bf820d5838f07a0d5131aa95a75f8dfbd01fbccb300bd"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -964,7 +964,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock4939774(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x5d887e118ee6320aca38e49cbd98adc25472c6efbf77a695ab0d6c476a4ec6e9"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -1009,7 +1009,7 @@ func TestInstance_ExecuteBlock_PolkadotBlock1089328(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x950173af1d9fdcd0be5428fc3eaf05d5f34376bd3882d9a61b348fa2dc641012"), Digest: digest, }, - Body: *types.NewBodyExtrinsics(types.BytesArrayToExtrinsics(exts)), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) diff --git a/tests/utils/chain.go b/tests/utils/chain.go index 41ddfcc45d..636d6690d2 100644 --- a/tests/utils/chain.go +++ b/tests/utils/chain.go @@ -137,7 +137,7 @@ func GetBlock(t *testing.T, node *Node, hash common.Hash) *types.Block { h, err := types.NewHeader(parentHash, stateRoot, extrinsicsRoot, number, rpcLogsToDigest(t, header.Digest.Logs)) require.NoError(t, err) - b, err := types.NewBodyExtrinsicsFromExtrinsicStrings(block.Block.Body) + b, err := types.NewBodyFromExtrinsicStrings(block.Block.Body) require.NoError(t, err, fmt.Sprintf("%v", block.Block.Body)) return &types.Block{ From 3caeae9cbabbfe739fa3f5b318cc49361be419ba Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Sep 2021 12:33:39 +0530 Subject: [PATCH 4/8] Addressed some reviews and left over renaming --- dot/state/block.go | 5 ++- dot/types/block.go | 4 +- dot/types/{body_extrinsics.go => body.go} | 41 +++++++------------ .../{body_extrinsics_test.go => body_test.go} | 27 ++++++------ 4 files changed, 33 insertions(+), 44 deletions(-) rename dot/types/{body_extrinsics.go => body.go} (80%) rename dot/types/{body_extrinsics_test.go => body_test.go} (70%) diff --git a/dot/state/block.go b/dot/state/block.go index 7ad93a3ee2..66af3fd150 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -360,11 +360,12 @@ func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { // SetBlockBody will add a block body to the db func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.Body) error { - optionalBody, err := body.AsOptional() + encodedBody, err := scale.Marshal(*body) if err != nil { return err } - return bs.db.Put(blockBodyKey(hash), optionalBody.Value()) + + return bs.db.Put(blockBodyKey(hash), encodedBody) } // CompareAndSetBlockData will compare empty fields and set all elements in a block data to db diff --git a/dot/types/block.go b/dot/types/block.go index d4f10cd091..ae3b577f54 100644 --- a/dot/types/block.go +++ b/dot/types/block.go @@ -55,11 +55,11 @@ func (b *Block) Encode() ([]byte, error) { } // get a SCALE encoded block body - encodedBody, err := b.Body.AsSCALEEncodedBody() + encodedBody, err := scale.Marshal(b.Body) if err != nil { return nil, err } - return append(enc, []byte(encodedBody)...), nil + return append(enc, encodedBody...), nil } // MustEncode returns the SCALE encoded block and panics if it fails to encode diff --git a/dot/types/body_extrinsics.go b/dot/types/body.go similarity index 80% rename from dot/types/body_extrinsics.go rename to dot/types/body.go index 23d6014238..20ad2e0502 100644 --- a/dot/types/body_extrinsics.go +++ b/dot/types/body.go @@ -22,20 +22,19 @@ import ( "math/big" "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/common/optional" "github.com/ChainSafe/gossamer/pkg/scale" ) // Body is the extrinsics(not encoded) inside a state block. type Body []Extrinsic -// NewBody returns a BodyExtrinsics from an Extrinsic array. +// NewBody returns a Body from an Extrinsic array. func NewBody(e []Extrinsic) *Body { body := Body(e) return &body } -// NewBodyFromBytes returns a BodyExtrinsics from a SCALE encoded byte array. +// NewBodyFromBytes returns a Body from a SCALE encoded byte array. func NewBodyFromBytes(b []byte) (*Body, error) { exts := [][]byte{} @@ -73,7 +72,7 @@ func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { // NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded // 0x-prefixed strings. func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { - exts := [][]byte{} + exts := []Extrinsic{} for _, s := range ss { b, err := common.HexToBytes(s) if err == common.ErrNoPrefix { @@ -84,21 +83,19 @@ func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { exts = append(exts, b) } - return NewBody(BytesArrayToExtrinsics(exts)), nil -} - -func (b *Body) AsSCALEEncodedBody() ([]byte, error) { - encodedBody, err := scale.Marshal(ExtrinsicsArrayToBytesArray(*b)) - if err != nil { - return nil, err - } - - return encodedBody, nil + return NewBody(exts), nil } +// DeepCopy creates a new copy of the body. func (b *Body) DeepCopy() Body { newExtrinsics := make([]Extrinsic, len([]Extrinsic(*b))) - copy(newExtrinsics, []Extrinsic(*b)) + + for _, e := range []Extrinsic(*b) { + temp := make([]byte, len(e)) + copy(temp, e) + + newExtrinsics = append(newExtrinsics, temp) + } return Body(newExtrinsics) } @@ -134,7 +131,7 @@ func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { // AsEncodedExtrinsics decodes the body into an array of SCALE encoded extrinsics func (b *Body) AsEncodedExtrinsics() ([]Extrinsic, error) { decodedExts := *b - ret := make([][]byte, len(decodedExts)) + ret := make([]Extrinsic, len(decodedExts)) var err error for i, ext := range decodedExts { @@ -144,15 +141,5 @@ func (b *Body) AsEncodedExtrinsics() ([]Extrinsic, error) { } } - return BytesArrayToExtrinsics(ret), nil -} - -// AsOptional returns the Body as an optional.Body -func (b *Body) AsOptional() (*optional.Body, error) { - encodedBody, err := b.AsSCALEEncodedBody() - if err != nil { - return nil, err - } - ob := optional.CoreBody([]byte(encodedBody)) - return optional.NewBody(true, ob), nil + return ret, nil } diff --git a/dot/types/body_extrinsics_test.go b/dot/types/body_test.go similarity index 70% rename from dot/types/body_extrinsics_test.go rename to dot/types/body_test.go index ed03773b48..fdc9a817b1 100644 --- a/dot/types/body_extrinsics_test.go +++ b/dot/types/body_test.go @@ -21,49 +21,50 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) -func TestBodyExtrinsicsToSCALEEncodedBody(t *testing.T) { +func TestBodyToSCALEEncodedBody(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsicsBefore := NewBody(exts) - scaleEncodedBody, err := bodyExtrinsicsBefore.AsSCALEEncodedBody() + bodyBefore := NewBody(exts) + scaleEncodedBody, err := scale.Marshal(*bodyBefore) require.NoError(t, err) - bodyExtrinsicsAfter, err := NewBodyFromBytes(scaleEncodedBody) + bodyAfter, err := NewBodyFromBytes(scaleEncodedBody) require.NoError(t, err) - require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) + require.Equal(t, bodyBefore, bodyAfter) } func TestHasExtrinsics(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsics := NewBody(exts) + body := NewBody(exts) - found, err := bodyExtrinsics.HasExtrinsic(Extrinsic{1, 2, 3}) + found, err := body.HasExtrinsic(Extrinsic{1, 2, 3}) require.NoError(t, err) require.True(t, found) } -func TestBodyExtrinsicsFromEncodedBytes(t *testing.T) { +func TestBodyFromEncodedBytes(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyExtrinsicsBefore := NewBody(exts) + bodyBefore := NewBody(exts) - encodeExtrinsics, err := bodyExtrinsicsBefore.AsEncodedExtrinsics() + encodeExtrinsics, err := bodyBefore.AsEncodedExtrinsics() require.NoError(t, err) encodedBytes := ExtrinsicsArrayToBytesArray(encodeExtrinsics) - bodyExtrinsicsAfter, err := NewBodyFromEncodedBytes(encodedBytes) + bodyAfter, err := NewBodyFromEncodedBytes(encodedBytes) require.NoError(t, err) - require.Equal(t, bodyExtrinsicsBefore, bodyExtrinsicsAfter) + require.Equal(t, bodyBefore, bodyAfter) } -func TestBodyExtrinsicsFromExtrinsicStrings(t *testing.T) { +func TestBodyFromExtrinsicStrings(t *testing.T) { exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} extStrings := []string{} From 5d04036dbc7be8ec26fee26412ce0a5333bb6e8a Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Sep 2021 21:44:54 +0530 Subject: [PATCH 5/8] Fixing a bug --- dot/types/body.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/types/body.go b/dot/types/body.go index 20ad2e0502..69f76bb94c 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -88,7 +88,7 @@ func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { // DeepCopy creates a new copy of the body. func (b *Body) DeepCopy() Body { - newExtrinsics := make([]Extrinsic, len([]Extrinsic(*b))) + newExtrinsics := []Extrinsic{} for _, e := range []Extrinsic(*b) { temp := make([]byte, len(e)) From 17016ec72624904b5dfab94056744d1c141f9ea0 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 30 Sep 2021 11:31:43 +0530 Subject: [PATCH 6/8] Addressed some more minor comments --- dot/core/service_test.go | 8 ++------ dot/rpc/modules/author.go | 2 +- dot/rpc/modules/chain_test.go | 15 +++++++++++---- dot/state/block_test.go | 22 ++++++++++------------ dot/sync/syncer_test.go | 4 +--- dot/types/block_test.go | 1 + dot/types/body.go | 2 +- dot/types/body_test.go | 9 ++------- 8 files changed, 29 insertions(+), 34 deletions(-) diff --git a/dot/core/service_test.go b/dot/core/service_test.go index 12c0d34b10..97c11d5b7d 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -504,25 +504,21 @@ func TestService_HandleRuntimeChanges(t *testing.T) { }) require.NoError(t, err) - body1 := types.NewBody([]types.Extrinsic{[]byte("Old Runtime")}) newBlock1 := &types.Block{ Header: types.Header{ ParentHash: hash, Number: big.NewInt(1), Digest: types.NewDigest()}, - Body: *body1, + Body: *types.NewBody([]types.Extrinsic{[]byte("Old Runtime")}), } - body2 := types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}) - require.NoError(t, err) - newBlockRTUpdate := &types.Block{ Header: types.Header{ ParentHash: hash, Number: big.NewInt(1), Digest: digest, }, - Body: *body2, + Body: *types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}), } ts, err := s.storageState.TrieState(nil) // Pass genesis root diff --git a/dot/rpc/modules/author.go b/dot/rpc/modules/author.go index 51394a2bab..023b9ad9cb 100644 --- a/dot/rpc/modules/author.go +++ b/dot/rpc/modules/author.go @@ -65,7 +65,7 @@ type ExtrinsicOrHashRequest []ExtrinsicOrHash // KeyInsertResponse []byte type KeyInsertResponse []byte -// PendingExtrinsicsResponse is a bi-dimensional array of bytes for allocating the pending extrisics +// PendingExtrinsicsResponse is a bi-dimensional array of bytes for allocating the pending extrinsics type PendingExtrinsicsResponse []string // RemoveExtrinsicsResponse is a array of hash used to Remove extrinsics diff --git a/dot/rpc/modules/chain_test.go b/dot/rpc/modules/chain_test.go index 77f59530a7..205c22b801 100644 --- a/dot/rpc/modules/chain_test.go +++ b/dot/rpc/modules/chain_test.go @@ -34,6 +34,13 @@ import ( "github.com/stretchr/testify/require" ) +// test data +var ( + sampleBodyBytes = *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}) + // sampleBodyString is string conversion of sampleBodyBytes + sampleBodyString = []string{"0x2800010203040506070809"} +) + func TestChainGetHeader_Genesis(t *testing.T) { state := newTestStateService(t) svc := NewChainModule(state.Block) @@ -140,7 +147,7 @@ func TestChainGetBlock_Genesis(t *testing.T) { expected := &ChainBlockResponse{ Block: ChainBlock{ Header: *expectedHeader, - Body: []string{"0x2800010203040506070809"}, + Body: sampleBodyString, }, } @@ -179,7 +186,7 @@ func TestChainGetBlock_Latest(t *testing.T) { expected := &ChainBlockResponse{ Block: ChainBlock{ Header: *expectedHeader, - Body: []string{"0x2800010203040506070809"}, + Body: sampleBodyString, }, } @@ -362,7 +369,7 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti blockHash0 := header0.Hash() block0 := &types.Block{ Header: *header0, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBodyBytes, } err := bs.AddBlock(block0) @@ -385,7 +392,7 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti block1 := &types.Block{ Header: *header1, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBodyBytes, } // Add the block1 to the DB diff --git a/dot/state/block_test.go b/dot/state/block_test.go index 76046a2232..60dff019de 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -29,6 +29,8 @@ import ( "github.com/stretchr/testify/require" ) +var sampleBlockBody = *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}) + var testGenesisHeader = &types.Header{ Number: big.NewInt(0), StateRoot: trie.EmptyHash, @@ -93,7 +95,7 @@ func TestGetBlockByNumber(t *testing.T) { block := &types.Block{ Header: *blockHeader, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } // AddBlock also sets mapping [blockNumber : hash] in DB @@ -118,7 +120,7 @@ func TestAddBlock(t *testing.T) { blockHash0 := header0.Hash() block0 := &types.Block{ Header: *header0, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } // Add the block0 to the DB @@ -135,7 +137,7 @@ func TestAddBlock(t *testing.T) { block1 := &types.Block{ Header: *header1, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } // Add the block1 to the DB @@ -385,7 +387,7 @@ func TestGetHashByNumber(t *testing.T) { block := &types.Block{ Header: *header, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } err = bs.AddBlock(block) @@ -425,13 +427,9 @@ func TestAddBlock_WithReOrg(t *testing.T) { ExtrinsicsRoot: common.Hash{99}, } - blockbody1b := types.NewBody( - []types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, - ) - block1b := &types.Block{ Header: *header1b, - Body: *blockbody1b, + Body: sampleBlockBody, } err = bs.AddBlock(block1b) @@ -451,7 +449,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2b := &types.Block{ Header: *header2b, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } err = bs.AddBlock(block2b) @@ -474,7 +472,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block2a := &types.Block{ Header: *header2a, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } err = bs.AddBlock(block2a) @@ -488,7 +486,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block3a := &types.Block{ Header: *header3a, - Body: *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}), + Body: sampleBlockBody, } err = bs.AddBlock(block3a) diff --git a/dot/sync/syncer_test.go b/dot/sync/syncer_test.go index 30974babb9..5c5f6cc4f2 100644 --- a/dot/sync/syncer_test.go +++ b/dot/sync/syncer_test.go @@ -157,10 +157,8 @@ func TestRemoveIncludedExtrinsics(t *testing.T) { _, err := syncer.transactionState.(*state.TransactionState).Push(tx) require.NoError(t, err) - exts := []types.Extrinsic{ext} - body := types.NewBody(exts) bd := &types.BlockData{ - Body: body, + Body: types.NewBody([]types.Extrinsic{ext}), } msg := &network.BlockResponseMessage{ diff --git a/dot/types/block_test.go b/dot/types/block_test.go index 4580c862b4..21d9afe43b 100644 --- a/dot/types/block_test.go +++ b/dot/types/block_test.go @@ -58,6 +58,7 @@ func TestEmptyBlock(t *testing.T) { } func TestEncodeAndDecodeBlock(t *testing.T) { + // SCALE encoding of the block, NewBlock(*header, *NewBody([]Extrinsic{[]byte{4, 1}})) expected, err := common.HexToBytes("0x4545454545454545454545454545454545454545454545454545454545454545042747ab7c0dc38b7f2afba82bd5e2d6acef8c31e09800f660b75ec84a7005099f03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c1113140004080401") require.NoError(t, err) diff --git a/dot/types/body.go b/dot/types/body.go index 69f76bb94c..7db1878bdf 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -100,7 +100,7 @@ func (b *Body) DeepCopy() Body { return Body(newExtrinsics) } -// HasExtrinsic returns true if body contains target Extrisic +// HasExtrinsic returns true if body contains target Extrinsic func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { exts := *b diff --git a/dot/types/body_test.go b/dot/types/body_test.go index fdc9a817b1..1bad93b3fe 100644 --- a/dot/types/body_test.go +++ b/dot/types/body_test.go @@ -25,9 +25,9 @@ import ( "github.com/stretchr/testify/require" ) -func TestBodyToSCALEEncodedBody(t *testing.T) { - exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} +var exts = []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} +func TestBodyToSCALEEncodedBody(t *testing.T) { bodyBefore := NewBody(exts) scaleEncodedBody, err := scale.Marshal(*bodyBefore) require.NoError(t, err) @@ -39,8 +39,6 @@ func TestBodyToSCALEEncodedBody(t *testing.T) { } func TestHasExtrinsics(t *testing.T) { - exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - body := NewBody(exts) found, err := body.HasExtrinsic(Extrinsic{1, 2, 3}) @@ -49,8 +47,6 @@ func TestHasExtrinsics(t *testing.T) { } func TestBodyFromEncodedBytes(t *testing.T) { - exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} - bodyBefore := NewBody(exts) encodeExtrinsics, err := bodyBefore.AsEncodedExtrinsics() @@ -65,7 +61,6 @@ func TestBodyFromEncodedBytes(t *testing.T) { } func TestBodyFromExtrinsicStrings(t *testing.T) { - exts := []Extrinsic{{1, 2, 3}, {7, 8, 9, 0}, {0xa, 0xb}} extStrings := []string{} for _, ext := range exts { From 376848c32d73d7f435ea091cc8e70f4894eb63fe Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 30 Sep 2021 12:57:48 +0530 Subject: [PATCH 7/8] Optimized slice intialization --- dot/types/body.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/types/body.go b/dot/types/body.go index 7db1878bdf..304ce04bef 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -88,7 +88,7 @@ func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { // DeepCopy creates a new copy of the body. func (b *Body) DeepCopy() Body { - newExtrinsics := []Extrinsic{} + newExtrinsics := make([]Extrinsic, 0, len([]Extrinsic(*b))) for _, e := range []Extrinsic(*b) { temp := make([]byte, len(e)) From e3c856d2462cad059decf16efce27e9446655ca6 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Fri, 1 Oct 2021 14:21:30 +0530 Subject: [PATCH 8/8] Fix a linting issue --- dot/core/service.go | 9 +++------ dot/core/service_test.go | 6 ++---- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/dot/core/service.go b/dot/core/service.go index 7841c626b1..d1921ff60e 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -333,9 +333,8 @@ func (s *Service) handleBlocksAsync() { logger.Warn("failed to re-add transactions to chain upon re-org", "error", err) } - if err := s.maintainTransactionPool(block); err != nil { - logger.Warn("failed to maintain transaction pool", "error", err) - } + s.maintainTransactionPool(block) + case <-s.ctx.Done(): return } @@ -418,7 +417,7 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error { // maintainTransactionPool removes any transactions that were included in the new block, revalidates the transactions in the pool, // and moves them to the queue if valid. // See https://github.com/paritytech/substrate/blob/74804b5649eccfb83c90aec87bdca58e5d5c8789/client/transaction-pool/src/lib.rs#L545 -func (s *Service) maintainTransactionPool(block *types.Block) error { +func (s *Service) maintainTransactionPool(block *types.Block) { // remove extrinsics included in a block for _, ext := range block.Body { s.transactionState.RemoveExtrinsic(ext) @@ -447,8 +446,6 @@ func (s *Service) maintainTransactionPool(block *types.Block) error { s.transactionState.RemoveExtrinsicFromPool(tx.Extrinsic) logger.Trace("moved transaction to queue", "hash", h) } - - return nil } // InsertKey inserts keypair into the account keystore diff --git a/dot/core/service_test.go b/dot/core/service_test.go index 97c11d5b7d..a12afe323f 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -374,10 +374,9 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) { transactionState: ts, } - err := s.maintainTransactionPool(&types.Block{ + s.maintainTransactionPool(&types.Block{ Body: *types.NewBody([]types.Extrinsic{}), }) - require.NoError(t, err) res := make([]*transaction.ValidTransaction, len(txs)) for i := range txs { @@ -420,10 +419,9 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) { transactionState: ts, } - err := s.maintainTransactionPool(&types.Block{ + s.maintainTransactionPool(&types.Block{ Body: types.Body([]types.Extrinsic{txs[0].Extrinsic}), }) - require.NoError(t, err) res := []*transaction.ValidTransaction{} for {