Skip to content

Commit

Permalink
fix(dot/types): *types.Body to be of type []types.Extrinsic (#1807)
Browse files Browse the repository at this point in the history
  • Loading branch information
kishansagathiya authored Oct 5, 2021
1 parent a04deb6 commit 4c09715
Show file tree
Hide file tree
Showing 32 changed files with 232 additions and 326 deletions.
2 changes: 1 addition & 1 deletion dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions dot/core/mocks/block_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 5 additions & 18 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
34 changes: 14 additions & 20 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions dot/network/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions dot/network/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion dot/network/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -140,7 +147,7 @@ func TestChainGetBlock_Genesis(t *testing.T) {
expected := &ChainBlockResponse{
Block: ChainBlock{
Header: *expectedHeader,
Body: nil,
Body: sampleBodyString,
},
}

Expand Down Expand Up @@ -179,7 +186,7 @@ func TestChainGetBlock_Latest(t *testing.T) {
expected := &ChainBlockResponse{
Block: ChainBlock{
Header: *expectedHeader,
Body: nil,
Body: sampleBodyString,
},
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 4c09715

Please sign in to comment.