@@ -807,7 +807,15 @@ async def initialize(self):
807807 if not self ._chain :
808808 chain = await self .rpc_request ("system_chain" , [])
809809 self ._chain = chain .get ("result" )
810- await self .init_runtime ()
810+ runtime = await self .init_runtime ()
811+ if self .ss58_format is None :
812+ # Check and apply runtime constants
813+ ss58_prefix_constant = await self .get_constant (
814+ "System" , "SS58Prefix" , runtime = runtime
815+ )
816+
817+ if ss58_prefix_constant :
818+ self .ss58_format = ss58_prefix_constant
811819 self .initialized = True
812820 self ._initializing = False
813821
@@ -962,27 +970,6 @@ async def decode_scale(
962970 else :
963971 return obj
964972
965- def load_runtime (self , runtime ):
966- # Update type registry
967- runtime .reload_type_registry (use_remote_preset = False , auto_discover = True )
968-
969- runtime .runtime_config .set_active_spec_version_id (runtime .runtime_version )
970- if runtime .implements_scaleinfo :
971- logger .debug ("Adding PortableRegistry from metadata to type registry" )
972- runtime .runtime_config .add_portable_registry (runtime .metadata )
973- # Set runtime compatibility flags
974- try :
975- _ = runtime .runtime_config .create_scale_object (
976- "sp_weights::weight_v2::Weight"
977- )
978- runtime .config ["is_weight_v2" ] = True
979- runtime .runtime_config .update_type_registry_types (
980- {"Weight" : "sp_weights::weight_v2::Weight" }
981- )
982- except NotImplementedError :
983- runtime .config ["is_weight_v2" ] = False
984- runtime .runtime_config .update_type_registry_types ({"Weight" : "WeightV1" })
985-
986973 async def init_runtime (
987974 self , block_hash : Optional [str ] = None , block_id : Optional [int ] = None
988975 ) -> Runtime :
@@ -1006,37 +993,27 @@ async def init_runtime(
1006993 raise ValueError ("Cannot provide block_hash and block_id at the same time" )
1007994
1008995 if block_id is not None :
996+ if runtime := self .runtime_cache .retrieve (block = block_id ):
997+ return runtime
1009998 block_hash = await self .get_block_hash (block_id )
1010999
10111000 if not block_hash :
10121001 block_hash = await self .get_chain_head ()
1002+ else :
1003+ self .last_block_hash = block_hash
1004+ if runtime := self .runtime_cache .retrieve (block_hash = block_hash ):
1005+ return runtime
10131006
10141007 runtime_version = await self .get_block_runtime_version_for (block_hash )
10151008 if runtime_version is None :
10161009 raise SubstrateRequestException (
10171010 f"No runtime information for block '{ block_hash } '"
10181011 )
10191012
1020- if self .runtime and runtime_version == self .runtime .runtime_version :
1021- return self .runtime
1022-
1023- runtime = self .runtime_cache .retrieve (runtime_version = runtime_version )
1024- if not runtime :
1025- self .last_block_hash = block_hash
1026-
1027- runtime = await self .get_runtime_for_version (runtime_version , block_hash )
1028-
1029- self .load_runtime (runtime )
1030-
1031- if self .ss58_format is None :
1032- # Check and apply runtime constants
1033- ss58_prefix_constant = await self .get_constant (
1034- "System" , "SS58Prefix" , block_hash = block_hash , runtime = runtime
1035- )
1036-
1037- if ss58_prefix_constant :
1038- self .ss58_format = ss58_prefix_constant
1039- return runtime
1013+ if runtime := self .runtime_cache .retrieve (runtime_version = runtime_version ):
1014+ return runtime
1015+ else :
1016+ return await self .get_runtime_for_version (runtime_version , block_hash )
10401017
10411018 @cached_fetcher (max_size = 16 , cache_key_index = 0 )
10421019 async def get_runtime_for_version (
@@ -1056,7 +1033,9 @@ async def get_runtime_for_version(
10561033 async def _get_runtime_for_version (
10571034 self , runtime_version : int , block_hash : Optional [str ] = None
10581035 ) -> Runtime :
1059- runtime_block_hash = await self .get_parent_block_hash (block_hash )
1036+ runtime_block_hash , block_number = await asyncio .gather (
1037+ self .get_parent_block_hash (block_hash ), self .get_block_number (block_hash )
1038+ )
10601039 runtime_info , metadata , (metadata_v15 , registry ) = await asyncio .gather (
10611040 self .get_block_runtime_info (runtime_block_hash ),
10621041 self .get_block_metadata (block_hash = runtime_block_hash , decode = True ),
@@ -1080,7 +1059,12 @@ async def _get_runtime_for_version(
10801059 runtime_info = runtime_info ,
10811060 registry = registry ,
10821061 )
1083- self .runtime_cache .add_item (runtime_version = runtime_version , runtime = runtime )
1062+ self .runtime_cache .add_item (
1063+ block = block_number ,
1064+ block_hash = block_hash ,
1065+ runtime_version = runtime_version ,
1066+ runtime = runtime ,
1067+ )
10841068 return runtime
10851069
10861070 async def create_storage_key (
0 commit comments