@@ -727,13 +727,7 @@ async def initialize(self):
727
727
if not self ._chain :
728
728
chain = await self .rpc_request ("system_chain" , [])
729
729
self ._chain = chain .get ("result" )
730
- init_load = await asyncio .gather (
731
- self .init_runtime (),
732
- return_exceptions = True ,
733
- )
734
- for potential_exception in init_load :
735
- if isinstance (potential_exception , Exception ):
736
- raise potential_exception
730
+ await self .init_runtime ()
737
731
self .initialized = True
738
732
self ._initializing = False
739
733
@@ -775,7 +769,9 @@ async def name(self):
775
769
self ._name = (await self .rpc_request ("system_name" , [])).get ("result" )
776
770
return self ._name
777
771
778
- async def get_storage_item (self , module : str , storage_function : str , block_hash : str = None ):
772
+ async def get_storage_item (
773
+ self , module : str , storage_function : str , block_hash : str = None
774
+ ):
779
775
await self .init_runtime (block_hash = block_hash )
780
776
metadata_pallet = self .runtime .metadata .get_metadata_pallet (module )
781
777
storage_item = metadata_pallet .get_storage_function (storage_function )
@@ -792,7 +788,9 @@ async def _get_current_block_hash(
792
788
return self .last_block_hash
793
789
return block_hash
794
790
795
- async def _load_registry_at_block (self , block_hash : Optional [str ]) -> MetadataV15 :
791
+ async def _load_registry_at_block (
792
+ self , block_hash : Optional [str ]
793
+ ) -> tuple [MetadataV15 , PortableRegistry ]:
796
794
# Should be called for any block that fails decoding.
797
795
# Possibly the metadata was different.
798
796
try :
@@ -817,7 +815,7 @@ async def _load_registry_at_block(self, block_hash: Optional[str]) -> MetadataV1
817
815
metadata = MetadataV15 .decode_from_metadata_option (metadata_option_bytes )
818
816
registry = PortableRegistry .from_metadata_v15 (metadata )
819
817
self ._load_registry_type_map (registry )
820
- return metadata ,registry
818
+ return metadata , registry
821
819
822
820
async def _wait_for_registry (self , _attempt : int = 1 , _retries : int = 3 ) -> None :
823
821
async def _waiter ():
@@ -899,7 +897,7 @@ async def decode_scale(
899
897
else :
900
898
return obj
901
899
902
- async def load_runtime (self ,runtime ):
900
+ async def load_runtime (self , runtime ):
903
901
self .runtime = runtime
904
902
905
903
# Update type registry
@@ -965,39 +963,31 @@ async def init_runtime(
965
963
966
964
runtime_info = await self .get_block_runtime_info (runtime_block_hash )
967
965
968
- # TODO when someone gets time, someone'd like to add this and the metadata v15 as tasks with callbacks
969
- # TODO to update the caches, but someone doesn't have time now.
970
- metadata = await self .get_block_metadata (
971
- block_hash = runtime_block_hash , decode = True
966
+ metadata , (metadata_v15 , registry ) = await asyncio .gather (
967
+ self .get_block_metadata (block_hash = runtime_block_hash , decode = True ),
968
+ self ._load_registry_at_block (block_hash = runtime_block_hash ),
972
969
)
973
970
if metadata is None :
974
971
# does this ever happen?
975
972
raise SubstrateRequestException (
976
973
f"No metadata for block '{ runtime_block_hash } '"
977
974
)
978
975
logger .debug (
979
- "Retrieved metadata for {} from Substrate node" .format (
980
- runtime_version
981
- )
982
- )
983
-
984
- metadata_v15 ,registry = await self ._load_registry_at_block (block_hash = runtime_block_hash )
985
- logger .debug (
986
- "Retrieved metadata v15 for {} from Substrate node" .format (
987
- runtime_version
988
- )
976
+ f"Retrieved metadata and metadata v15 for { runtime_version } from Substrate node"
989
977
)
990
978
991
979
runtime = Runtime (
992
- chain = self .chain ,
993
- runtime_config = self .runtime_config ,
994
- metadata = metadata ,
995
- type_registry = self .type_registry ,
996
- metadata_v15 = metadata_v15 ,
997
- runtime_info = runtime_info ,
998
- registry = registry ,
999
- )
1000
- self .runtime_cache .add_item (runtime_version = runtime_version , runtime = runtime )
980
+ chain = self .chain ,
981
+ runtime_config = self .runtime_config ,
982
+ metadata = metadata ,
983
+ type_registry = self .type_registry ,
984
+ metadata_v15 = metadata_v15 ,
985
+ runtime_info = runtime_info ,
986
+ registry = registry ,
987
+ )
988
+ self .runtime_cache .add_item (
989
+ runtime_version = runtime_version , runtime = runtime
990
+ )
1001
991
1002
992
await self .load_runtime (runtime )
1003
993
@@ -1669,30 +1659,28 @@ def convert_event_data(data):
1669
1659
events .append (convert_event_data (item ))
1670
1660
return events
1671
1661
1672
- @a .lru_cache (maxsize = 16 ) # small cache with large items
1673
- async def get_parent_block_hash (self ,block_hash ):
1662
+ @a .lru_cache (maxsize = 512 ) # large cache with small items
1663
+ async def get_parent_block_hash (self , block_hash ):
1674
1664
block_header = await self .rpc_request ("chain_getHeader" , [block_hash ])
1675
1665
1676
1666
if block_header ["result" ] is None :
1677
- raise SubstrateRequestException (
1678
- f'Block not found for "{ block_hash } "'
1679
- )
1667
+ raise SubstrateRequestException (f'Block not found for "{ block_hash } "' )
1680
1668
parent_block_hash : str = block_header ["result" ]["parentHash" ]
1681
1669
1682
- if int (parent_block_hash ,16 ) == 0 :
1670
+ if int (parent_block_hash , 16 ) == 0 :
1683
1671
# "0x0000000000000000000000000000000000000000000000000000000000000000"
1684
1672
return block_hash
1685
1673
return parent_block_hash
1686
1674
1687
- @a .lru_cache (maxsize = 16 ) # small cache with large items
1675
+ @a .lru_cache (maxsize = 16 ) # small cache with large items
1688
1676
async def get_block_runtime_info (self , block_hash : str ) -> dict :
1689
1677
"""
1690
1678
Retrieve the runtime info of given block_hash
1691
1679
"""
1692
1680
response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
1693
1681
return response .get ("result" )
1694
1682
1695
- @a .lru_cache (maxsize = 512 ) # large cache with small items
1683
+ @a .lru_cache (maxsize = 512 ) # large cache with small items
1696
1684
async def get_block_runtime_version_for (self , block_hash : str ):
1697
1685
"""
1698
1686
Retrieve the runtime version of the parent of a given block_hash
@@ -1997,7 +1985,7 @@ async def rpc_request(
1997
1985
else :
1998
1986
raise SubstrateRequestException (result [payload_id ][0 ])
1999
1987
2000
- @a .lru_cache (maxsize = 512 ) # block_id->block_hash does not change
1988
+ @a .lru_cache (maxsize = 512 ) # block_id->block_hash does not change
2001
1989
async def get_block_hash (self , block_id : int ) -> str :
2002
1990
return (await self .rpc_request ("chain_getBlockHash" , [block_id ]))["result" ]
2003
1991
@@ -2177,7 +2165,7 @@ async def create_scale_object(
2177
2165
if "metadata" not in kwargs :
2178
2166
kwargs ["metadata" ] = self .runtime .metadata
2179
2167
2180
- return runtime .runtime_config .create_scale_object (
2168
+ return self . runtime .runtime_config .create_scale_object (
2181
2169
type_string , data = data , ** kwargs
2182
2170
)
2183
2171
@@ -2358,7 +2346,7 @@ async def create_signed_extrinsic(
2358
2346
The signed Extrinsic
2359
2347
"""
2360
2348
# only support creating extrinsics for current block
2361
- await self .init_runtime (block_id = self .get_block_number ())
2349
+ await self .init_runtime (block_id = await self .get_block_number ())
2362
2350
2363
2351
# Check requirements
2364
2352
if not isinstance (call , GenericCall ):
0 commit comments