@@ -218,7 +218,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
218
218
logger .Debugf ("imported block %s and stored state trie with root %s" ,
219
219
block .Header .Hash (), state .MustRoot ())
220
220
221
- rt , err := s .blockState .GetRuntime (& block .Header .ParentHash )
221
+ rt , err := s .blockState .GetRuntime (block .Header .ParentHash )
222
222
if err != nil {
223
223
return err
224
224
}
@@ -262,7 +262,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
262
262
return fmt .Errorf ("%w: for hash %s" , ErrEmptyRuntimeCode , hash )
263
263
}
264
264
265
- rt , err := s .blockState .GetRuntime (& hash )
265
+ rt , err := s .blockState .GetRuntime (hash )
266
266
if err != nil {
267
267
return fmt .Errorf ("getting runtime from block state: %w" , err )
268
268
}
@@ -352,7 +352,8 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
352
352
}
353
353
354
354
// 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 )
356
357
if err != nil {
357
358
return err
358
359
}
@@ -425,7 +426,8 @@ func (s *Service) maintainTransactionPool(block *types.Block, bestBlockHash comm
425
426
// re-validate transactions in the pool and move them to the queue
426
427
txs := s .transactionState .PendingInPool ()
427
428
for _ , tx := range txs {
428
- rt , err := s .blockState .GetRuntime (& bestBlockHash )
429
+ bestBlockHash := s .blockState .BestBlockHash ()
430
+ rt , err := s .blockState .GetRuntime (bestBlockHash )
429
431
if err != nil {
430
432
return fmt .Errorf ("failed to get runtime to re-validate transactions in pool: %s" , err )
431
433
}
@@ -477,7 +479,8 @@ func (s *Service) HasKey(pubKeyStr, keystoreType string) (bool, error) {
477
479
478
480
// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
479
481
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 )
481
484
if err != nil {
482
485
return nil , err
483
486
}
@@ -488,28 +491,10 @@ func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
488
491
// GetRuntimeVersion gets the current RuntimeVersion
489
492
func (s * Service ) GetRuntimeVersion (bhash * common.Hash ) (
490
493
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 )
503
495
if err != nil {
504
- return version , err
496
+ return version , fmt . Errorf ( "setting up runtime: %w" , err )
505
497
}
506
-
507
- rt , err := s .blockState .GetRuntime (bhash )
508
- if err != nil {
509
- return version , err
510
- }
511
-
512
- rt .SetContextStorage (ts )
513
498
return rt .Version (), nil
514
499
}
515
500
@@ -535,7 +520,7 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
535
520
return err
536
521
}
537
522
538
- rt , err := s .blockState .GetRuntime (& bestBlockHash )
523
+ rt , err := s .blockState .GetRuntime (bestBlockHash )
539
524
if err != nil {
540
525
logger .Critical ("failed to get runtime" )
541
526
return err
@@ -564,30 +549,11 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
564
549
}
565
550
566
551
// 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 )
586
554
if err != nil {
587
- return nil , err
555
+ return nil , fmt . Errorf ( "setting up runtime: %w" , err )
588
556
}
589
-
590
- rt .SetContextStorage (ts )
591
557
return rt .Metadata ()
592
558
}
593
559
@@ -631,3 +597,33 @@ func (s *Service) buildExternalTransaction(rt runtime.Instance, ext types.Extrin
631
597
}
632
598
return types .Extrinsic (bytes .Join (extrinsicParts , nil )), nil
633
599
}
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
+ }
0 commit comments