Skip to content

Commit 879d065

Browse files
authored
chore(blockstate): change GetRuntime to take common.Hash as argument (#2759)
1 parent 86b4fcc commit 879d065

30 files changed

+201
-174
lines changed

dot/core/helpers_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,3 @@ func getGssmrRuntimeCode(t *testing.T) (code []byte) {
301301

302302
return trieState.LoadCode()
303303
}
304-
305-
func hashPtr(h common.Hash) *common.Hash { return &h }

dot/core/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type BlockState interface {
7070
SubChain(start, end common.Hash) ([]common.Hash, error)
7171
GetBlockBody(hash common.Hash) (*types.Body, error)
7272
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error
73-
GetRuntime(*common.Hash) (runtime.Instance, error)
73+
GetRuntime(blockHash common.Hash) (instance runtime.Instance, err error)
7474
StoreRuntime(common.Hash, runtime.Instance)
7575
}
7676

dot/core/messages.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func (s *Service) HandleTransactionMessage(peerID peer.ID, msg *network.Transact
6868
return false, err
6969
}
7070

71-
hash := head.Hash()
72-
rt, err := s.blockState.GetRuntime(&hash)
71+
bestBlockHash := head.Hash()
72+
rt, err := s.blockState.GetRuntime(bestBlockHash)
7373
if err != nil {
7474
return false, err
7575
}

dot/core/messages_integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ func TestService_HandleTransactionMessage(t *testing.T) {
157157
genHeader, err := s.blockState.BestBlockHeader()
158158
require.NoError(t, err)
159159

160-
rt, err := s.blockState.GetRuntime(nil)
160+
bestBlockHash := s.blockState.BestBlockHash()
161+
rt, err := s.blockState.GetRuntime(bestBlockHash)
161162
require.NoError(t, err)
162163

163164
ts, err := s.storageState.TrieState(nil)

dot/core/mocks_test.go

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

dot/core/service.go

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
218218
logger.Debugf("imported block %s and stored state trie with root %s",
219219
block.Header.Hash(), state.MustRoot())
220220

221-
rt, err := s.blockState.GetRuntime(&block.Header.ParentHash)
221+
rt, err := s.blockState.GetRuntime(block.Header.ParentHash)
222222
if err != nil {
223223
return err
224224
}
@@ -262,7 +262,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
262262
return fmt.Errorf("%w: for hash %s", ErrEmptyRuntimeCode, hash)
263263
}
264264

265-
rt, err := s.blockState.GetRuntime(&hash)
265+
rt, err := s.blockState.GetRuntime(hash)
266266
if err != nil {
267267
return fmt.Errorf("getting runtime from block state: %w", err)
268268
}
@@ -352,7 +352,8 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
352352
}
353353

354354
// Check transaction validation on the best block.
355-
rt, err := s.blockState.GetRuntime(nil)
355+
bestBlockHash := s.blockState.BestBlockHash()
356+
rt, err := s.blockState.GetRuntime(bestBlockHash)
356357
if err != nil {
357358
return err
358359
}
@@ -425,7 +426,8 @@ func (s *Service) maintainTransactionPool(block *types.Block, bestBlockHash comm
425426
// re-validate transactions in the pool and move them to the queue
426427
txs := s.transactionState.PendingInPool()
427428
for _, tx := range txs {
428-
rt, err := s.blockState.GetRuntime(&bestBlockHash)
429+
bestBlockHash := s.blockState.BestBlockHash()
430+
rt, err := s.blockState.GetRuntime(bestBlockHash)
429431
if err != nil {
430432
return fmt.Errorf("failed to get runtime to re-validate transactions in pool: %s", err)
431433
}
@@ -477,7 +479,8 @@ func (s *Service) HasKey(pubKeyStr, keystoreType string) (bool, error) {
477479

478480
// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
479481
func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
480-
rt, err := s.blockState.GetRuntime(nil)
482+
bestBlockHash := s.blockState.BestBlockHash()
483+
rt, err := s.blockState.GetRuntime(bestBlockHash)
481484
if err != nil {
482485
return nil, err
483486
}
@@ -488,28 +491,10 @@ func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
488491
// GetRuntimeVersion gets the current RuntimeVersion
489492
func (s *Service) GetRuntimeVersion(bhash *common.Hash) (
490493
version runtime.Version, err error) {
491-
var stateRootHash *common.Hash
492-
493-
// If block hash is not nil then fetch the state root corresponding to the block.
494-
if bhash != nil {
495-
var err error
496-
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
497-
if err != nil {
498-
return version, err
499-
}
500-
}
501-
502-
ts, err := s.storageState.TrieState(stateRootHash)
494+
rt, err := prepareRuntime(bhash, s.storageState, s.blockState)
503495
if err != nil {
504-
return version, err
496+
return version, fmt.Errorf("setting up runtime: %w", err)
505497
}
506-
507-
rt, err := s.blockState.GetRuntime(bhash)
508-
if err != nil {
509-
return version, err
510-
}
511-
512-
rt.SetContextStorage(ts)
513498
return rt.Version(), nil
514499
}
515500

@@ -535,7 +520,7 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
535520
return err
536521
}
537522

538-
rt, err := s.blockState.GetRuntime(&bestBlockHash)
523+
rt, err := s.blockState.GetRuntime(bestBlockHash)
539524
if err != nil {
540525
logger.Critical("failed to get runtime")
541526
return err
@@ -564,30 +549,11 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
564549
}
565550

566551
// GetMetadata calls runtime Metadata_metadata function
567-
func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
568-
var (
569-
stateRootHash *common.Hash
570-
err error
571-
)
572-
573-
// If block hash is not nil then fetch the state root corresponding to the block.
574-
if bhash != nil {
575-
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
576-
if err != nil {
577-
return nil, err
578-
}
579-
}
580-
ts, err := s.storageState.TrieState(stateRootHash)
581-
if err != nil {
582-
return nil, err
583-
}
584-
585-
rt, err := s.blockState.GetRuntime(bhash)
552+
func (s *Service) GetMetadata(bhash *common.Hash) (metadata []byte, err error) {
553+
rt, err := prepareRuntime(bhash, s.storageState, s.blockState)
586554
if err != nil {
587-
return nil, err
555+
return nil, fmt.Errorf("setting up runtime: %w", err)
588556
}
589-
590-
rt.SetContextStorage(ts)
591557
return rt.Metadata()
592558
}
593559

@@ -631,3 +597,33 @@ func (s *Service) buildExternalTransaction(rt runtime.Instance, ext types.Extrin
631597
}
632598
return types.Extrinsic(bytes.Join(extrinsicParts, nil)), nil
633599
}
600+
601+
func prepareRuntime(blockHash *common.Hash, storageState StorageState,
602+
blockState BlockState) (instance runtime.Instance, err error) {
603+
var stateRootHash *common.Hash
604+
if blockHash != nil {
605+
stateRootHash, err = storageState.GetStateRootFromBlock(blockHash)
606+
if err != nil {
607+
return nil, fmt.Errorf("getting state root from block hash: %w", err)
608+
}
609+
}
610+
611+
trieState, err := storageState.TrieState(stateRootHash)
612+
if err != nil {
613+
return nil, fmt.Errorf("getting trie state: %w", err)
614+
}
615+
616+
var blockHashValue common.Hash
617+
if blockHash != nil {
618+
blockHashValue = *blockHash
619+
} else {
620+
blockHashValue = blockState.BestBlockHash()
621+
}
622+
instance, err = blockState.GetRuntime(blockHashValue)
623+
if err != nil {
624+
return nil, fmt.Errorf("getting runtime: %w", err)
625+
}
626+
627+
instance.SetContextStorage(trieState)
628+
return instance, nil
629+
}

dot/core/service_integration_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
209209
parent, err := bs.BestBlockHeader()
210210
require.NoError(t, err)
211211

212-
rt, err := s.blockState.GetRuntime(nil)
212+
bestBlockHash := s.blockState.BestBlockHash()
213+
rt, err := s.blockState.GetRuntime(bestBlockHash)
213214
require.NoError(t, err)
214215

215216
block1 := sync.BuildBlock(t, rt, parent, nil)
@@ -299,8 +300,8 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
299300
// we prefix with []byte{2} here since that's the enum index for the old IncludeDataExt extrinsic
300301
tx := append([]byte{2}, enc...)
301302

302-
bhash := s.blockState.BestBlockHash()
303-
rt, err := s.blockState.GetRuntime(&bhash)
303+
bestBlockHash := s.blockState.BestBlockHash()
304+
rt, err := s.blockState.GetRuntime(bestBlockHash)
304305
require.NoError(t, err)
305306

306307
validity, err := rt.ValidateTransaction(tx)
@@ -440,7 +441,8 @@ func TestMaintainTransactionPoolLatestTxnQueue_BlockWithExtrinsics(t *testing.T)
440441

441442
func TestService_GetRuntimeVersion(t *testing.T) {
442443
s := NewTestService(t, nil)
443-
rt, err := s.blockState.GetRuntime(nil)
444+
bestBlockHash := s.blockState.BestBlockHash()
445+
rt, err := s.blockState.GetRuntime(bestBlockHash)
444446
require.NoError(t, err)
445447

446448
rtExpected := rt.Version()
@@ -461,7 +463,8 @@ func TestService_HandleSubmittedExtrinsic(t *testing.T) {
461463
genHeader, err := s.blockState.BestBlockHeader()
462464
require.NoError(t, err)
463465

464-
rt, err := s.blockState.GetRuntime(nil)
466+
bestBlockHash := s.blockState.BestBlockHash()
467+
rt, err := s.blockState.GetRuntime(bestBlockHash)
465468
require.NoError(t, err)
466469

467470
ts, err := s.storageState.TrieState(nil)
@@ -492,7 +495,8 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
492495
)
493496
s := NewTestService(t, nil)
494497

495-
rt, err := s.blockState.GetRuntime(nil)
498+
bestBlockHash := s.blockState.BestBlockHash()
499+
rt, err := s.blockState.GetRuntime(bestBlockHash)
496500
require.NoError(t, err)
497501

498502
v := rt.Version()
@@ -526,7 +530,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
526530
ts, err := s.storageState.TrieState(nil) // Pass genesis root
527531
require.NoError(t, err)
528532

529-
parentRt, err := s.blockState.GetRuntime(&hash)
533+
parentRt, err := s.blockState.GetRuntime(hash)
530534
require.NoError(t, err)
531535

532536
v = parentRt.Version()
@@ -547,13 +551,13 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
547551
require.NoError(t, err)
548552

549553
// bhash1 runtime should not be updated
550-
rt, err = s.blockState.GetRuntime(&bhash1)
554+
rt, err = s.blockState.GetRuntime(bhash1)
551555
require.NoError(t, err)
552556

553557
v = rt.Version()
554558
require.Equal(t, v.SpecVersion, currSpecVersion)
555559

556-
rt, err = s.blockState.GetRuntime(&rtUpdateBhash)
560+
rt, err = s.blockState.GetRuntime(rtUpdateBhash)
557561
require.NoError(t, err)
558562

559563
v = rt.Version()
@@ -574,7 +578,8 @@ func TestService_HandleCodeSubstitutes(t *testing.T) {
574578
blockHash: common.BytesToHex(testRuntime),
575579
}
576580

577-
rt, err := s.blockState.GetRuntime(nil)
581+
bestBlockHash := s.blockState.BestBlockHash()
582+
rt, err := s.blockState.GetRuntime(bestBlockHash)
578583
require.NoError(t, err)
579584

580585
s.blockState.StoreRuntime(blockHash, rt)
@@ -589,7 +594,8 @@ func TestService_HandleCodeSubstitutes(t *testing.T) {
589594
func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
590595
s := NewTestService(t, nil)
591596

592-
parentRt, err := s.blockState.GetRuntime(nil)
597+
bestBlockHash := s.blockState.BestBlockHash()
598+
parentRt, err := s.blockState.GetRuntime(bestBlockHash)
593599
require.NoError(t, err)
594600

595601
codeHashBefore := parentRt.GetCodeHash()
@@ -626,7 +632,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
626632
err = s.blockState.HandleRuntimeChanges(ts, parentRt, rtUpdateBhash)
627633
require.NoError(t, err)
628634

629-
rt, err := s.blockState.GetRuntime(&rtUpdateBhash)
635+
rt, err := s.blockState.GetRuntime(rtUpdateBhash)
630636
require.NoError(t, err)
631637

632638
// codeHash should change after runtime change

0 commit comments

Comments
 (0)