13
13
ss58_encode ,
14
14
MultiAccountId ,
15
15
)
16
- from scalecodec .base import RuntimeConfigurationObject , ScaleBytes , ScaleType
16
+ from scalecodec .base import ScaleBytes , ScaleType
17
17
from websockets .sync .client import connect , ClientConnection
18
18
from websockets .exceptions import ConnectionClosed
19
19
@@ -553,6 +553,13 @@ def initialize(self):
553
553
chain = self .rpc_request ("system_chain" , [])
554
554
self ._chain = chain .get ("result" )
555
555
self .init_runtime ()
556
+ if self .ss58_format is None :
557
+ # Check and apply runtime constants
558
+ ss58_prefix_constant = self .get_constant (
559
+ "System" , "SS58Prefix" , block_hash = self .last_block_hash
560
+ )
561
+ if ss58_prefix_constant :
562
+ self .ss58_format = ss58_prefix_constant
556
563
self .initialized = True
557
564
558
565
def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -721,11 +728,19 @@ def init_runtime(
721
728
if block_id and block_hash :
722
729
raise ValueError ("Cannot provide block_hash and block_id at the same time" )
723
730
724
- if block_id :
731
+ if block_id is not None :
732
+ if runtime := self .runtime_cache .retrieve (block = block_id ):
733
+ self .runtime = runtime
734
+ return runtime
725
735
block_hash = self .get_block_hash (block_id )
726
736
727
737
if not block_hash :
728
738
block_hash = self .get_chain_head ()
739
+ else :
740
+ self .last_block_hash = block_hash
741
+ if runtime := self .runtime_cache .retrieve (block_hash = block_hash ):
742
+ self .runtime = runtime
743
+ return runtime
729
744
730
745
runtime_version = self .get_block_runtime_version_for (block_hash )
731
746
if runtime_version is None :
@@ -736,56 +751,62 @@ def init_runtime(
736
751
if self .runtime and runtime_version == self .runtime .runtime_version :
737
752
return self .runtime
738
753
739
- runtime = self .runtime_cache .retrieve (runtime_version = runtime_version )
740
- if not runtime :
741
- self .last_block_hash = block_hash
742
-
743
- runtime_block_hash = self .get_parent_block_hash (block_hash )
744
-
745
- runtime_info = self .get_block_runtime_info (runtime_block_hash )
754
+ if runtime := self .runtime_cache .retrieve (runtime_version = runtime_version ):
755
+ self .runtime = runtime
756
+ return runtime
757
+ else :
758
+ return self .get_runtime_for_version (runtime_version , block_hash )
746
759
747
- metadata = self .get_block_metadata (
748
- block_hash = runtime_block_hash , decode = True
749
- )
750
- if metadata is None :
751
- # does this ever happen?
752
- raise SubstrateRequestException (
753
- f"No metadata for block '{ runtime_block_hash } '"
754
- )
755
- logger .debug (
756
- "Retrieved metadata for {} from Substrate node" .format (runtime_version )
757
- )
760
+ def get_runtime_for_version (
761
+ self , runtime_version : int , block_hash : Optional [str ] = None
762
+ ) -> Runtime :
763
+ """
764
+ Retrieves the `Runtime` for a given runtime version at a given block hash.
765
+ Args:
766
+ runtime_version: version of the runtime (from `get_block_runtime_version_for`)
767
+ block_hash: hash of the block to query
758
768
759
- metadata_v15 , registry = self ._load_registry_at_block (
760
- block_hash = runtime_block_hash
761
- )
762
- logger .debug (
763
- f"Retrieved metadata v15 for { runtime_version } from Substrate node"
764
- )
769
+ Returns:
770
+ Runtime object for the given runtime version
771
+ """
772
+ if not block_hash :
773
+ block_hash = self .get_chain_head ()
774
+ runtime_block_hash = self .get_parent_block_hash (block_hash )
775
+ block_number = self .get_block_number (block_hash )
776
+ runtime_info = self .get_block_runtime_info (runtime_block_hash )
765
777
766
- runtime = Runtime (
767
- chain = self .chain ,
768
- runtime_config = self .runtime_config ,
769
- metadata = metadata ,
770
- type_registry = self .type_registry ,
771
- metadata_v15 = metadata_v15 ,
772
- runtime_info = runtime_info ,
773
- registry = registry ,
774
- )
775
- self .runtime_cache .add_item (
776
- runtime_version = runtime_version , runtime = runtime
778
+ metadata = self .get_block_metadata (block_hash = runtime_block_hash , decode = True )
779
+ if metadata is None :
780
+ # does this ever happen?
781
+ raise SubstrateRequestException (
782
+ f"No metadata for block '{ runtime_block_hash } '"
777
783
)
784
+ logger .debug (
785
+ "Retrieved metadata for {} from Substrate node" .format (runtime_version )
786
+ )
778
787
779
- self .load_runtime (runtime )
780
-
781
- if self .ss58_format is None :
782
- # Check and apply runtime constants
783
- ss58_prefix_constant = self .get_constant (
784
- "System" , "SS58Prefix" , block_hash = block_hash
785
- )
788
+ metadata_v15 , registry = self ._load_registry_at_block (
789
+ block_hash = runtime_block_hash
790
+ )
791
+ logger .debug (
792
+ f"Retrieved metadata v15 for { runtime_version } from Substrate node"
793
+ )
786
794
787
- if ss58_prefix_constant :
788
- self .ss58_format = ss58_prefix_constant
795
+ runtime = Runtime (
796
+ chain = self .chain ,
797
+ runtime_config = self .runtime_config ,
798
+ metadata = metadata ,
799
+ type_registry = self .type_registry ,
800
+ metadata_v15 = metadata_v15 ,
801
+ runtime_info = runtime_info ,
802
+ registry = registry ,
803
+ )
804
+ self .runtime_cache .add_item (
805
+ block = block_number ,
806
+ block_hash = block_hash ,
807
+ runtime_version = runtime_version ,
808
+ runtime = runtime ,
809
+ )
789
810
return runtime
790
811
791
812
def create_storage_key (
@@ -1061,6 +1082,7 @@ def get_metadata_runtime_call_function(
1061
1082
Args:
1062
1083
api: Name of the runtime API e.g. 'TransactionPaymentApi'
1063
1084
method: Name of the method e.g. 'query_fee_details'
1085
+ block_hash: block hash whose metadata to query
1064
1086
1065
1087
Returns:
1066
1088
runtime call function
@@ -1087,41 +1109,6 @@ def get_metadata_runtime_call_function(
1087
1109
1088
1110
return runtime_call_def_obj
1089
1111
1090
- def get_metadata_runtime_call_function (
1091
- self , api : str , method : str
1092
- ) -> GenericRuntimeCallDefinition :
1093
- """
1094
- Get details of a runtime API call
1095
-
1096
- Args:
1097
- api: Name of the runtime API e.g. 'TransactionPaymentApi'
1098
- method: Name of the method e.g. 'query_fee_details'
1099
-
1100
- Returns:
1101
- GenericRuntimeCallDefinition
1102
- """
1103
- self .init_runtime ()
1104
-
1105
- try :
1106
- runtime_call_def = self .runtime_config .type_registry ["runtime_api" ][api ][
1107
- "methods"
1108
- ][method ]
1109
- runtime_call_def ["api" ] = api
1110
- runtime_call_def ["method" ] = method
1111
- runtime_api_types = self .runtime_config .type_registry ["runtime_api" ][
1112
- api
1113
- ].get ("types" , {})
1114
- except KeyError :
1115
- raise ValueError (f"Runtime API Call '{ api } .{ method } ' not found in registry" )
1116
-
1117
- # Add runtime API types to registry
1118
- self .runtime_config .update_type_registry_types (runtime_api_types )
1119
-
1120
- runtime_call_def_obj = self .create_scale_object ("RuntimeCallDefinition" )
1121
- runtime_call_def_obj .encode (runtime_call_def )
1122
-
1123
- return runtime_call_def_obj
1124
-
1125
1112
def _get_block_handler (
1126
1113
self ,
1127
1114
block_hash : str ,
@@ -2887,7 +2874,6 @@ def query_map(
2887
2874
Returns:
2888
2875
QueryMapResult object
2889
2876
"""
2890
- hex_to_bytes_ = hex_to_bytes
2891
2877
params = params or []
2892
2878
block_hash = self ._get_current_block_hash (block_hash , reuse_block_hash )
2893
2879
if block_hash :
0 commit comments