Skip to content

Commit 4c09715

Browse files
fix(dot/types): *types.Body to be of type []types.Extrinsic (ChainSafe#1807)
1 parent a04deb6 commit 4c09715

32 files changed

+232
-326
lines changed

dot/core/messages_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
103103
ParentHash: s.blockState.BestBlockHash(),
104104
Digest: digest,
105105
},
106-
Body: *types.NewBody([]byte{}),
106+
Body: *types.NewBody([]types.Extrinsic{}),
107107
}
108108

109109
expected := &network.BlockAnnounceMessage{

dot/core/mocks/block_state.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/core/service.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,8 @@ func (s *Service) handleBlocksAsync() {
333333
logger.Warn("failed to re-add transactions to chain upon re-org", "error", err)
334334
}
335335

336-
if err := s.maintainTransactionPool(block); err != nil {
337-
logger.Warn("failed to maintain transaction pool", "error", err)
338-
}
336+
s.maintainTransactionPool(block)
337+
339338
case <-s.ctx.Done():
340339
return
341340
}
@@ -380,14 +379,9 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
380379
continue
381380
}
382381

383-
exts, err := body.AsExtrinsics()
384-
if err != nil {
385-
continue
386-
}
387-
388382
// TODO: decode extrinsic and make sure it's not an inherent.
389383
// currently we are attempting to re-add inherents, causing lots of "'Bad input data provided to validate_transaction" errors.
390-
for _, ext := range exts {
384+
for _, ext := range *body {
391385
logger.Debug("validating transaction on re-org chain", "extrinsic", ext)
392386
encExt, err := scale.Marshal(ext)
393387
if err != nil {
@@ -423,14 +417,9 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
423417
// maintainTransactionPool removes any transactions that were included in the new block, revalidates the transactions in the pool,
424418
// and moves them to the queue if valid.
425419
// See https://github.com/paritytech/substrate/blob/74804b5649eccfb83c90aec87bdca58e5d5c8789/client/transaction-pool/src/lib.rs#L545
426-
func (s *Service) maintainTransactionPool(block *types.Block) error {
427-
exts, err := block.Body.AsExtrinsics()
428-
if err != nil {
429-
return err
430-
}
431-
420+
func (s *Service) maintainTransactionPool(block *types.Block) {
432421
// remove extrinsics included in a block
433-
for _, ext := range exts {
422+
for _, ext := range block.Body {
434423
s.transactionState.RemoveExtrinsic(ext)
435424
}
436425

@@ -457,8 +446,6 @@ func (s *Service) maintainTransactionPool(block *types.Block) error {
457446
s.transactionState.RemoveExtrinsicFromPool(tx.Extrinsic)
458447
logger.Trace("moved transaction to queue", "hash", h)
459448
}
460-
461-
return nil
462449
}
463450

464451
// InsertKey inserts keypair into the account keystore

dot/core/service_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestAnnounceBlock(t *testing.T) {
127127
ParentHash: s.blockState.BestBlockHash(),
128128
Digest: digest,
129129
},
130-
Body: *types.NewBody([]byte{}),
130+
Body: *types.NewBody([]types.Extrinsic{}),
131131
}
132132

133133
expected := &network.BlockAnnounceMessage{
@@ -303,8 +303,6 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
303303
require.NoError(t, err)
304304

305305
// build "re-org" chain
306-
body, err := types.NewBodyFromExtrinsics([]types.Extrinsic{tx})
307-
require.NoError(t, err)
308306

309307
digest := types.NewDigest()
310308
block := &types.Block{
@@ -313,7 +311,7 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
313311
Number: big.NewInt(0).Add(ancestor.Header.Number, big.NewInt(1)),
314312
Digest: digest,
315313
},
316-
Body: *body,
314+
Body: types.Body([]types.Extrinsic{tx}),
317315
}
318316

319317
s.blockState.StoreRuntime(block.Header.Hash(), rt)
@@ -376,10 +374,9 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) {
376374
transactionState: ts,
377375
}
378376

379-
err := s.maintainTransactionPool(&types.Block{
380-
Body: *types.NewBody([]byte{}),
377+
s.maintainTransactionPool(&types.Block{
378+
Body: *types.NewBody([]types.Extrinsic{}),
381379
})
382-
require.NoError(t, err)
383380

384381
res := make([]*transaction.ValidTransaction, len(txs))
385382
for i := range txs {
@@ -422,13 +419,9 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) {
422419
transactionState: ts,
423420
}
424421

425-
body, err := types.NewBodyFromExtrinsics([]types.Extrinsic{txs[0].Extrinsic})
426-
require.NoError(t, err)
427-
428-
err = s.maintainTransactionPool(&types.Block{
429-
Body: *body,
422+
s.maintainTransactionPool(&types.Block{
423+
Body: types.Body([]types.Extrinsic{txs[0].Extrinsic}),
430424
})
431-
require.NoError(t, err)
432425

433426
res := []*transaction.ValidTransaction{}
434427
for {
@@ -514,7 +507,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
514507
ParentHash: hash,
515508
Number: big.NewInt(1),
516509
Digest: types.NewDigest()},
517-
Body: *types.NewBody([]byte("Old Runtime")),
510+
Body: *types.NewBody([]types.Extrinsic{[]byte("Old Runtime")}),
518511
}
519512

520513
newBlockRTUpdate := &types.Block{
@@ -523,7 +516,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
523516
Number: big.NewInt(1),
524517
Digest: digest,
525518
},
526-
Body: *types.NewBody([]byte("Updated Runtime")),
519+
Body: *types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")}),
527520
}
528521

529522
ts, err := s.storageState.TrieState(nil) // Pass genesis root
@@ -598,13 +591,14 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
598591
codeHashBefore := parentRt.GetCodeHash()
599592
blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution
600593

594+
body := types.NewBody([]types.Extrinsic{[]byte("Updated Runtime")})
601595
newBlock := &types.Block{
602596
Header: types.Header{
603597
ParentHash: blockHash,
604598
Number: big.NewInt(1),
605599
Digest: types.NewDigest(),
606600
},
607-
Body: *types.NewBody([]byte("Updated Runtime")),
601+
Body: *body,
608602
}
609603

610604
err = s.handleCodeSubstitution(blockHash)
@@ -647,7 +641,7 @@ func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) {
647641

648642
testBlock := &types.Block{
649643
Header: *header,
650-
Body: *types.NewBody([]byte{}),
644+
Body: *types.NewBody([]types.Extrinsic{}),
651645
}
652646

653647
err = s.blockState.AddBlock(testBlock)
@@ -677,7 +671,7 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) {
677671

678672
testBlock := &types.Block{
679673
Header: *header,
680-
Body: *types.NewBody([]byte{}),
674+
Body: *types.NewBody([]types.Extrinsic{}),
681675
}
682676

683677
err = s.blockState.AddBlock(testBlock)
@@ -702,7 +696,7 @@ func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) {
702696

703697
testBlock := &types.Block{
704698
Header: *header,
705-
Body: *types.NewBody([]byte{}),
699+
Body: *types.NewBody([]types.Extrinsic{}),
706700
}
707701

708702
err = s.blockState.AddBlock(testBlock)
@@ -787,7 +781,7 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by
787781

788782
testBlock := &types.Block{
789783
Header: *header,
790-
Body: *types.NewBody([]byte{}),
784+
Body: *types.NewBody([]types.Extrinsic{}),
791785
}
792786

793787
err = s.blockState.AddBlock(testBlock)

dot/network/message_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ func TestEncodeBlockResponseMessage_WithBody(t *testing.T) {
179179
require.NoError(t, err)
180180

181181
exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}}
182-
body, err := types.NewBodyFromBytes(exts)
183-
require.NoError(t, err)
182+
body := types.NewBody(types.BytesArrayToExtrinsics(exts))
184183

185184
bd := &types.BlockData{
186185
Hash: hash,
@@ -226,8 +225,7 @@ func TestEncodeBlockResponseMessage_WithAll(t *testing.T) {
226225
require.NoError(t, err)
227226

228227
exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}}
229-
body, err := types.NewBodyFromBytes(exts)
230-
require.NoError(t, err)
228+
body := types.NewBody(types.BytesArrayToExtrinsics(exts))
231229

232230
bd := &types.BlockData{
233231
Hash: hash,

dot/network/sync_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ func TestSyncQueue_PushResponse(t *testing.T) {
9090
testHeader := types.NewEmptyHeader()
9191
testHeader.Number = big.NewInt(int64(77 + i))
9292

93+
body := types.NewBody([]types.Extrinsic{[]byte{0}})
9394
msg.BlockData = append(msg.BlockData, &types.BlockData{
9495
Header: testHeader,
95-
Body: types.NewBody([]byte{0}),
96+
Body: body,
9697
})
9798
}
9899

@@ -372,7 +373,7 @@ func TestSyncQueue_handleResponseQueue_responseQueueAhead(t *testing.T) {
372373
q.responses = append(q.responses, &types.BlockData{
373374
Hash: testHeader0.Hash(),
374375
Header: testHeader0,
375-
Body: types.NewBody([]byte{4, 4, 2}),
376+
Body: types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}}),
376377
Receipt: nil,
377378
MessageQueue: nil,
378379
Justification: nil,
@@ -400,7 +401,7 @@ func TestSyncQueue_processBlockResponses(t *testing.T) {
400401
{
401402
Hash: testHeader0.Hash(),
402403
Header: testHeader0,
403-
Body: types.NewBody([]byte{4, 4, 2}),
404+
Body: types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}}),
404405
Receipt: nil,
405406
MessageQueue: nil,
406407
Justification: nil,

dot/network/test_helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ func testBlockResponseMessage() *BlockResponseMessage {
7373
Digest: types.NewDigest(),
7474
}
7575

76+
body := types.NewBody([]types.Extrinsic{[]byte{4, 4, 2}})
77+
7678
msg.BlockData = append(msg.BlockData, &types.BlockData{
7779
Hash: testHeader.Hash(),
7880
Header: testHeader,
79-
Body: types.NewBody([]byte{4, 4, 2}),
81+
Body: body,
8082
MessageQueue: nil,
8183
Receipt: nil,
8284
Justification: nil,

dot/rpc/modules/author.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type ExtrinsicOrHashRequest []ExtrinsicOrHash
6565
// KeyInsertResponse []byte
6666
type KeyInsertResponse []byte
6767

68-
// PendingExtrinsicsResponse is a bi-dimensional array of bytes for allocating the pending extrisics
68+
// PendingExtrinsicsResponse is a bi-dimensional array of bytes for allocating the pending extrinsics
6969
type PendingExtrinsicsResponse []string
7070

7171
// RemoveExtrinsicsResponse is a array of hash used to Remove extrinsics

dot/rpc/modules/chain_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ import (
3434
"github.com/stretchr/testify/require"
3535
)
3636

37+
// test data
38+
var (
39+
sampleBodyBytes = *types.NewBody([]types.Extrinsic{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}})
40+
// sampleBodyString is string conversion of sampleBodyBytes
41+
sampleBodyString = []string{"0x2800010203040506070809"}
42+
)
43+
3744
func TestChainGetHeader_Genesis(t *testing.T) {
3845
state := newTestStateService(t)
3946
svc := NewChainModule(state.Block)
@@ -140,7 +147,7 @@ func TestChainGetBlock_Genesis(t *testing.T) {
140147
expected := &ChainBlockResponse{
141148
Block: ChainBlock{
142149
Header: *expectedHeader,
143-
Body: nil,
150+
Body: sampleBodyString,
144151
},
145152
}
146153

@@ -179,7 +186,7 @@ func TestChainGetBlock_Latest(t *testing.T) {
179186
expected := &ChainBlockResponse{
180187
Block: ChainBlock{
181188
Header: *expectedHeader,
182-
Body: nil,
189+
Body: sampleBodyString,
183190
},
184191
}
185192

@@ -361,12 +368,9 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti
361368
}
362369
// Create blockHash
363370
blockHash0 := header0.Hash()
364-
// BlockBody with fake extrinsics
365-
blockBody0 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
366-
367371
block0 := &types.Block{
368372
Header: *header0,
369-
Body: blockBody0,
373+
Body: sampleBodyBytes,
370374
}
371375

372376
err := bs.AddBlock(block0)
@@ -387,12 +391,9 @@ func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runti
387391
StateRoot: trie.EmptyHash,
388392
}
389393

390-
// Create Block with fake extrinsics
391-
blockBody1 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
392-
393394
block1 := &types.Block{
394395
Header: *header1,
395-
Body: blockBody1,
396+
Body: sampleBodyBytes,
396397
}
397398

398399
// Add the block1 to the DB

dot/rpc/modules/childstate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) {
228228
Number: big.NewInt(0).Add(big.NewInt(1), bb.Header.Number),
229229
StateRoot: stateRoot,
230230
},
231-
Body: []byte{},
231+
Body: types.Body{},
232232
}
233233

234234
err = st.Block.AddBlock(b)

0 commit comments

Comments
 (0)