diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index b471014c1a..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.NewBody([]byte{}), + 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 efb499f96f..6e5387bae4 100644 --- a/dot/core/mocks/block_state.go +++ b/dot/core/mocks/block_state.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. 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" diff --git a/dot/core/service.go b/dot/core/service.go index b466aa3712..3f8f72ba47 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 } @@ -380,14 +379,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 { @@ -423,14 +417,9 @@ 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 { - exts, err := block.Body.AsExtrinsics() - if err != nil { - return err - } - +func (s *Service) maintainTransactionPool(block *types.Block) { // remove extrinsics included in a block - for _, ext := range exts { + for _, ext := range block.Body { s.transactionState.RemoveExtrinsic(ext) } @@ -457,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 9774857e25..53bf84cfcf 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -127,7 +127,7 @@ func TestAnnounceBlock(t *testing.T) { ParentHash: s.blockState.BestBlockHash(), Digest: digest, }, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{}), } expected := &network.BlockAnnounceMessage{ @@ -303,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{ @@ -313,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.Body([]types.Extrinsic{tx}), } s.blockState.StoreRuntime(block.Header.Hash(), rt) @@ -376,10 +374,9 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) { transactionState: ts, } - err := s.maintainTransactionPool(&types.Block{ - Body: *types.NewBody([]byte{}), + s.maintainTransactionPool(&types.Block{ + Body: *types.NewBody([]types.Extrinsic{}), }) - require.NoError(t, err) res := make([]*transaction.ValidTransaction, len(txs)) for i := range txs { @@ -422,13 +419,9 @@ 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, + s.maintainTransactionPool(&types.Block{ + Body: types.Body([]types.Extrinsic{txs[0].Extrinsic}), }) - require.NoError(t, err) res := []*transaction.ValidTransaction{} for { @@ -514,7 +507,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) { ParentHash: hash, Number: big.NewInt(1), Digest: types.NewDigest()}, - Body: *types.NewBody([]byte("Old Runtime")), + Body: *types.NewBody([]types.Extrinsic{[]byte("Old Runtime")}), } newBlockRTUpdate := &types.Block{ @@ -523,7 +516,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) { Number: big.NewInt(1), Digest: digest, }, - Body: *types.NewBody([]byte("Updated Runtime")), + Body: *types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}), } ts, err := s.storageState.TrieState(nil) // Pass genesis root @@ -598,13 +591,14 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) { codeHashBefore := parentRt.GetCodeHash() blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution + body := types.NewBody([]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) @@ -647,7 +641,7 @@ func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -677,7 +671,7 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -702,7 +696,7 @@ func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) { testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) @@ -787,7 +781,7 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by testBlock := &types.Block{ Header: *header, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{}), } err = s.blockState.AddBlock(testBlock) diff --git a/dot/network/message_test.go b/dot/network/message_test.go index aedcebb58a..cdb4fbd39b 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.NewBody(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.NewBody(types.BytesArrayToExtrinsics(exts)) bd := &types.BlockData{ Hash: hash, diff --git a/dot/network/sync_test.go b/dot/network/sync_test.go index c979c648c1..f816b55d6d 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.NewBody([]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.NewBody([]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.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 cf0a31dca0..8083043a65 100644 --- a/dot/network/test_helpers.go +++ b/dot/network/test_helpers.go @@ -73,10 +73,12 @@ func testBlockResponseMessage() *BlockResponseMessage { Digest: types.NewDigest(), } + body := types.NewBody([]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/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 141f7cdb11..9f3412df17 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: nil, + Body: sampleBodyString, }, } @@ -179,7 +186,7 @@ func TestChainGetBlock_Latest(t *testing.T) { expected := &ChainBlockResponse{ Block: ChainBlock{ Header: *expectedHeader, - Body: nil, + Body: sampleBodyString, }, } @@ -361,12 +368,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: sampleBodyBytes, } err := bs.AddBlock(block0) @@ -387,12 +391,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: sampleBodyBytes, } // Add the block1 to the DB diff --git a/dot/rpc/modules/childstate_test.go b/dot/rpc/modules/childstate_test.go index 05f868661f..535d4134a9 100644 --- a/dot/rpc/modules/childstate_test.go +++ b/dot/rpc/modules/childstate_test.go @@ -228,7 +228,7 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) { Number: big.NewInt(0).Add(big.NewInt(1), bb.Header.Number), StateRoot: stateRoot, }, - Body: []byte{}, + 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 5bbbc40e1a..f79079053f 100644 --- a/dot/rpc/modules/state_test.go +++ b/dot/rpc/modules/state_test.go @@ -545,7 +545,7 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) { Number: big.NewInt(2), StateRoot: sr1, }, - Body: *types.NewBody([]byte{}), + Body: *types.NewBody([]types.Extrinsic{[]byte{}}), } err = chain.Block.AddBlock(b) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index d6119237a9..a192c27b94 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -214,8 +214,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.NewBody(exts) block := &types.Block{ Header: *header, diff --git a/dot/state/block.go b/dot/state/block.go index dc980d19d0..7f3690fe5c 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 @@ -118,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.NewBody([]types.Extrinsic{})); err != nil { return nil, err } @@ -354,12 +355,17 @@ func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { return nil, err } - return types.NewBody(data), nil + return types.NewBodyFromBytes(data) } // 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()) + encodedBody, err := scale.Marshal(*body) + if err != nil { + return err + } + + return bs.db.Put(blockBodyKey(hash), encodedBody) } // CompareAndSetBlockData will compare empty fields and set all elements in a block data to db @@ -429,7 +435,7 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti } } - err = bs.SetBlockBody(block.Header.Hash(), types.NewBody(block.Body)) + err = bs.SetBlockBody(block.Header.Hash(), &block.Body) if err != nil { return err } diff --git a/dot/state/block_data_test.go b/dot/state/block_data_test.go index 73fc146374..76278053d3 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.NewBodyFromBytes([]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..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.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: sampleBlockBody, } // AddBlock also sets mapping [blockNumber : hash] in DB @@ -116,12 +118,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: sampleBlockBody, } // Add the block0 to the DB @@ -136,12 +135,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: sampleBlockBody, } // Add the block1 to the DB @@ -391,7 +387,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: sampleBlockBody, } err = bs.AddBlock(block) @@ -411,9 +407,10 @@ func TestAddBlock_WithReOrg(t *testing.T) { ParentHash: testGenesisHeader.Hash(), } + blockbody1a := types.NewBody([]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) @@ -432,7 +429,7 @@ func TestAddBlock_WithReOrg(t *testing.T) { block1b := &types.Block{ Header: *header1b, - Body: types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Body: sampleBlockBody, } err = bs.AddBlock(block1b) @@ -452,7 +449,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: sampleBlockBody, } err = bs.AddBlock(block2b) @@ -475,7 +472,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: sampleBlockBody, } err = bs.AddBlock(block2a) @@ -489,7 +486,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: sampleBlockBody, } err = bs.AddBlock(block3a) diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index b33cb88c4f..3a78043bf9 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.NewBodyFromBytes([]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..64e8f87a60 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -236,13 +236,16 @@ func generateBlockWithRandomTrie(t *testing.T, serv *Service, parent *common.Has parent = &bb } + body, err := types.NewBodyFromBytes([]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/syncer.go b/dot/sync/syncer.go index 1123248272..8942344316 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.Body) { + 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..5c5f6cc4f2 100644 --- a/dot/sync/syncer_test.go +++ b/dot/sync/syncer_test.go @@ -157,12 +157,8 @@ func TestRemoveIncludedExtrinsics(t *testing.T) { _, err := syncer.transactionState.(*state.TransactionState).Push(tx) require.NoError(t, err) - exts := []types.Extrinsic{ext} - body, err := types.NewBodyFromExtrinsics(exts) - require.NoError(t, err) - bd := &types.BlockData{ - Body: body, + Body: types.NewBody([]types.Extrinsic{ext}), } msg := &network.BlockResponseMessage{ diff --git a/dot/sync/test_helpers.go b/dot/sync/test_helpers.go index 781113320b..06f15a5377 100644 --- a/dot/sync/test_helpers.go +++ b/dot/sync/test_helpers.go @@ -205,7 +205,7 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e require.NoError(t, err) } else { - body = types.NewBody(inherentExts) + body = types.NewBody(types.BytesArrayToExtrinsics(exts)) } // apply each inherent extrinsic diff --git a/dot/types/block.go b/dot/types/block.go index 1a5e5a1cc5..ae3b577f54 100644 --- a/dot/types/block.go +++ b/dot/types/block.go @@ -38,7 +38,7 @@ func NewBlock(header Header, body Body) Block { func NewEmptyBlock() Block { return Block{ Header: *NewEmptyHeader(), - Body: *NewBody(nil), + Body: Body(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 := scale.Marshal(b.Body) + if err != nil { + return nil, err + } + return append(enc, 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..6f91c55c84 100644 --- a/dot/types/block_data.go +++ b/dot/types/block_data.go @@ -24,7 +24,8 @@ 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 diff --git a/dot/types/block_data_test.go b/dot/types/block_data_test.go index bd4d0b6c8d..df1babc8d3 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: NewBody([]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: 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 157f3ad3c4..21d9afe43b 100644 --- a/dot/types/block_test.go +++ b/dot/types/block_test.go @@ -52,16 +52,15 @@ func TestEmptyBlock(t *testing.T) { isEmpty = block.Empty() require.False(t, isEmpty) - block = NewBlock(*NewEmptyHeader(), *NewBody([]byte{4, 1})) + block = NewBlock(*NewEmptyHeader(), *NewBody([]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} + // SCALE encoding of the block, NewBlock(*header, *NewBody([]Extrinsic{[]byte{4, 1}})) + expected, err := common.HexToBytes("0x4545454545454545454545454545454545454545454545454545454545454545042747ab7c0dc38b7f2afba82bd5e2d6acef8c31e09800f660b75ec84a7005099f03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c1113140004080401") + require.NoError(t, err) parentHash, err := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545") require.NoError(t, err) @@ -75,9 +74,7 @@ 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, *NewBody([]Extrinsic{[]byte{4, 1}})) enc, err := scale.Marshal(block) require.NoError(t, err) @@ -111,13 +108,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, *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, *NewBody([]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.go b/dot/types/body.go index 3b6eb38dde..304ce04bef 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -22,34 +22,42 @@ import ( "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" + "github.com/ChainSafe/gossamer/pkg/scale" ) -// Body is the encoded extrinsics inside a state block -type Body []byte +// Body is the extrinsics(not encoded) inside a state block. +type Body []Extrinsic -// NewBody returns a Body from a byte array -func NewBody(b []byte) *Body { - body := Body(b) +// NewBody returns a Body from an Extrinsic array. +func NewBody(e []Extrinsic) *Body { + body := Body(e) return &body } -// NewBodyFromBytes returns a new Body from a slice of byte slices -func NewBodyFromBytes(exts [][]byte) (*Body, error) { - enc, err := scale.Encode(exts) +// NewBodyFromBytes returns a Body from a SCALE encoded byte array. +func NewBodyFromBytes(b []byte) (*Body, error) { + exts := [][]byte{} + + if len(b) == 0 { + return NewBody([]Extrinsic{}), nil + } + + err := scale.Unmarshal(b, &exts) if err != nil { return nil, err } - body := Body(enc) - return &body, nil + return NewBody(BytesArrayToExtrinsics(exts)), 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) { - enc, err := scale.Encode(big.NewInt(int64(len(exts)))) + // 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.Marshal(big.NewInt(int64(len(exts)))) if err != nil { return nil, err } @@ -58,24 +66,13 @@ func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { 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 + return NewBodyFromBytes(enc) } -// 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{} + exts := []Extrinsic{} for _, s := range ss { b, err := common.HexToBytes(s) if err == common.ErrNoPrefix { @@ -86,73 +83,29 @@ func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { exts = append(exts, b) } - enc, err := scale.Encode(exts) - if err != nil { - return nil, err - } - - body := Body(enc) - return &body, nil + return NewBody(exts), nil } -// AsExtrinsics decodes the body into an array of extrinsics -func (b *Body) AsExtrinsics() ([]Extrinsic, error) { - exts := [][]byte{} +// DeepCopy creates a new copy of the body. +func (b *Body) DeepCopy() Body { + newExtrinsics := make([]Extrinsic, 0, len([]Extrinsic(*b))) - if len(*b) == 0 { - return []Extrinsic{}, nil - } + for _, e := range []Extrinsic(*b) { + temp := make([]byte, len(e)) + copy(temp, e) - dec, err := scale.Decode(*b, exts) - if err != nil { - return nil, err + newExtrinsics = append(newExtrinsics, temp) } - return BytesArrayToExtrinsics(dec.([][]byte)), nil + return Body(newExtrinsics) } -// 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 +// HasExtrinsic returns true if body contains target Extrinsic func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { - exts, err := b.AsExtrinsics() - if err != nil { - return false, err - } + 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 + // 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] @@ -162,7 +115,7 @@ func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { } //otherwise try to encode and compare - encext, err := scale.Encode(currext) + encext, err := scale.Marshal(currext) if err != nil { return false, fmt.Errorf("fail while scale encode: %w", err) } @@ -174,3 +127,19 @@ func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) { return false, nil } + +// AsEncodedExtrinsics decodes the body into an array of SCALE encoded extrinsics +func (b *Body) AsEncodedExtrinsics() ([]Extrinsic, error) { + decodedExts := *b + ret := make([]Extrinsic, len(decodedExts)) + var err error + + for i, ext := range decodedExts { + ret[i], err = scale.Marshal(ext) + if err != nil { + return nil, err + } + } + + return ret, nil +} diff --git a/dot/types/body_test.go b/dot/types/body_test.go index d34b22716b..1bad93b3fe 100644 --- a/dot/types/body_test.go +++ b/dot/types/body_test.go @@ -17,97 +17,61 @@ package types import ( + "fmt" "testing" "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) -func TestBodyToExtrinsics(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}} - body, err := NewBodyFromExtrinsics(exts) +func TestBodyToSCALEEncodedBody(t *testing.T) { + bodyBefore := NewBody(exts) + scaleEncodedBody, err := scale.Marshal(*bodyBefore) require.NoError(t, err) - res, err := body.AsExtrinsics() + bodyAfter, err := NewBodyFromBytes(scaleEncodedBody) require.NoError(t, err) - require.Equal(t, exts, res) + + require.Equal(t, bodyBefore, bodyAfter) } -func TestNewBodyFromExtrinsicStrings(t *testing.T) { - strs := []string{"0xabcd", "0xff9988", "0x7654acdf"} - body, err := NewBodyFromExtrinsicStrings(strs) - require.NoError(t, err) +func TestHasExtrinsics(t *testing.T) { + body := NewBody(exts) - exts, err := body.AsExtrinsics() + found, err := body.HasExtrinsic(Extrinsic{1, 2, 3}) 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) - } + require.True(t, found) } -func TestNewBodyFromExtrinsicStrings_Mixed(t *testing.T) { - strs := []string{"0xabcd", "0xff9988", "noot"} - body, err := NewBodyFromExtrinsicStrings(strs) - require.NoError(t, err) +func TestBodyFromEncodedBytes(t *testing.T) { + bodyBefore := NewBody(exts) - exts, err := body.AsExtrinsics() + encodeExtrinsics, err := bodyBefore.AsEncodedExtrinsics() 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) - } -} + encodedBytes := ExtrinsicsArrayToBytesArray(encodeExtrinsics) -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) + bodyAfter, err := NewBodyFromEncodedBytes(encodedBytes) require.NoError(t, err) - res, err := body.AsEncodedExtrinsics() - require.NoError(t, err) - require.Equal(t, BytesArrayToExtrinsics(exts), res) + require.Equal(t, bodyBefore, bodyAfter) } -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, - }, - } +func TestBodyFromExtrinsicStrings(t *testing.T) { + extStrings := []string{} - for _, test := range tests { - res, err := test.body.HasExtrinsic(target) - require.Nil(t, err) - require.Equal(t, test.expect, res) + for _, ext := range exts { + extStrings = append(extStrings, common.BytesToHex(ext)) } + + fmt.Println(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 5110665957..95eb6e9bfe 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -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.NewBody(extrinsics), nil } diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index cb0dd91468..ed0fef874f 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)) } @@ -457,11 +454,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/grandpa/message_handler_test.go b/lib/grandpa/message_handler_test.go index ecd89f1011..8cdcd171c4 100644 --- a/lib/grandpa/message_handler_test.go +++ b/lib/grandpa/message_handler_test.go @@ -203,13 +203,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.NewBodyFromBytes([]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) @@ -532,9 +536,12 @@ func TestMessageHandler_VerifyBlockJustification(t *testing.T) { err := st.Grandpa.SetNextChange(auths, big.NewInt(1)) require.NoError(t, err) + body, err := types.NewBodyFromBytes([]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/runtime/life/exports_test.go b/lib/runtime/life/exports_test.go index a3f3df9685..55fb294546 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.NewBody(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.NewBody(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.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 32e6501ec8..1a3c3eeee8 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -563,7 +563,7 @@ func buildBlockVdt(t *testing.T, instance runtime.Instance, parentHash common.Ha return &types.Block{ Header: *res, - Body: *types.NewBody(inherentExts), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } } @@ -715,7 +715,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x9a87f6af64ef97aff2d31bebfdd59f8fe2ef6019278b634b2515a38f1c4c2420"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -766,7 +766,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xa35fb7f7616f5c979d48222b3d2fa7cb2331ef73954726714d91ca945cc34fd8"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -812,7 +812,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock3784(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x52b7d4852fc648cb8f908901e1e36269593c25050c31718454bca74b69115d12"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -858,7 +858,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock901442(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x13483a4c148fff5f072e86b5af52bf031556514e9c87ea19f9e31e7b13c0c414"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -904,7 +904,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1377831(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x7f3ea0ed63b4053d9b75e7ee3e5b3f6ce916e8f59b7b6c5e966b7a56ea0a563a"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -951,7 +951,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1482003(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0xdf5da95780b77e83ad0bf820d5838f07a0d5131aa95a75f8dfbd01fbccb300bd"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -995,7 +995,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock4939774(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x5d887e118ee6320aca38e49cbd98adc25472c6efbf77a695ab0d6c476a4ec6e9"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), } _, err = instance.ExecuteBlock(block) @@ -1040,7 +1040,7 @@ func TestInstance_ExecuteBlock_PolkadotBlock1089328(t *testing.T) { ExtrinsicsRoot: common.MustHexToHash("0x950173af1d9fdcd0be5428fc3eaf05d5f34376bd3882d9a61b348fa2dc641012"), Digest: digest, }, - Body: *types.NewBody(body), + Body: *types.NewBody(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 {