6161
6262ResultHandler = Callable [[dict , Any ], Awaitable [tuple [dict , bool ]]]
6363
64+ logger = logging .getLogger ("async_substrate_interface" )
65+
6466
6567class AsyncExtrinsicReceipt :
6668 """
@@ -734,9 +736,13 @@ async def initialize(self):
734736 if not self ._chain :
735737 chain = await self .rpc_request ("system_chain" , [])
736738 self ._chain = chain .get ("result" )
737- await asyncio .gather (
738- self .load_registry (), self ._first_initialize_runtime ()
739+ init_load = await asyncio .gather (
740+ self .load_registry (), self ._first_initialize_runtime (),
741+ return_exceptions = True
739742 )
743+ for potential_exception in init_load :
744+ if isinstance (potential_exception , Exception ):
745+ raise potential_exception
740746 self .initialized = True
741747 self ._initializing = False
742748
@@ -1020,7 +1026,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10201026 if not self ._metadata :
10211027 if self .runtime_version in self ._metadata_cache :
10221028 # Get metadata from cache
1023- logging .debug (
1029+ logger .debug (
10241030 "Retrieved metadata for {} from memory" .format (
10251031 self .runtime_version
10261032 )
@@ -1034,7 +1040,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10341040 metadata = self ._metadata = await self .get_block_metadata (
10351041 block_hash = runtime_block_hash , decode = True
10361042 )
1037- logging .debug (
1043+ logger .debug (
10381044 "Retrieved metadata for {} from Substrate node" .format (
10391045 self .runtime_version
10401046 )
@@ -1047,7 +1053,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10471053
10481054 if self .runtime_version in self ._metadata_v15_cache :
10491055 # Get metadata v15 from cache
1050- logging .debug (
1056+ logger .debug (
10511057 "Retrieved metadata v15 for {} from memory" .format (
10521058 self .runtime_version
10531059 )
@@ -1059,7 +1065,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10591065 metadata_v15 = (
10601066 self ._old_metadata_v15
10611067 ) = await self ._load_registry_at_block (block_hash = runtime_block_hash )
1062- logging .debug (
1068+ logger .debug (
10631069 "Retrieved metadata v15 for {} from Substrate node" .format (
10641070 self .runtime_version
10651071 )
@@ -1072,7 +1078,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10721078 self .reload_type_registry (use_remote_preset = False , auto_discover = True )
10731079
10741080 if self .implements_scaleinfo :
1075- logging .debug ("Add PortableRegistry from metadata to type registry" )
1081+ logger .debug ("Add PortableRegistry from metadata to type registry" )
10761082 self .runtime_config .add_portable_registry (metadata )
10771083
10781084 # Set active runtime version
@@ -1108,6 +1114,11 @@ async def get_runtime(block_hash, block_id) -> Runtime:
11081114 if block_id and block_hash :
11091115 raise ValueError ("Cannot provide block_hash and block_id at the same time" )
11101116
1117+ if not self .metadata_v15 :
1118+ raise SubstrateRequestException (
1119+ "Metadata V15 was not loaded. This usually indicates that you did not correctly initialize"
1120+ " the AsyncSubstrateInterface class with `async with` or by calling `initialize()`"
1121+ )
11111122 if (
11121123 not (runtime := self .runtime_cache .retrieve (block_id , block_hash ))
11131124 or runtime .metadata is None
@@ -1991,14 +2002,14 @@ async def _make_rpc_request(
19912002 break
19922003 if time .time () - self .ws .last_received >= self .retry_timeout :
19932004 if attempt >= self .max_retries :
1994- logging .warning (
2005+ logger .warning (
19952006 f"Timed out waiting for RPC requests { attempt } times. Exiting."
19962007 )
19972008 raise SubstrateRequestException ("Max retries reached." )
19982009 else :
19992010 self .ws .last_received = time .time ()
20002011 await self .ws .connect (force = True )
2001- logging .error (
2012+ logger .error (
20022013 f"Timed out waiting for RPC requests. "
20032014 f"Retrying attempt { attempt + 1 } of { self .max_retries } "
20042015 )
@@ -2070,7 +2081,7 @@ async def rpc_request(
20702081 "Failed to get runtime version"
20712082 in result [payload_id ][0 ]["error" ]["message" ]
20722083 ):
2073- logging .warning (
2084+ logger .warning (
20742085 "Failed to get runtime. Re-fetching from chain, and retrying."
20752086 )
20762087 await self .init_runtime ()
@@ -2547,7 +2558,7 @@ async def _do_runtime_call_old(
25472558 params : Optional [Union [list , dict ]] = None ,
25482559 block_hash : Optional [str ] = None ,
25492560 ) -> ScaleType :
2550- logging .debug (
2561+ logger .debug (
25512562 f"Decoding old runtime call: { api } .{ method } with params: { params } at block hash: { block_hash } "
25522563 )
25532564 runtime_call_def = _TYPE_REGISTRY ["runtime_api" ][api ]["methods" ][method ]
@@ -2585,7 +2596,7 @@ async def _do_runtime_call_old(
25852596 # Get correct type
25862597 result_decoded = runtime_call_def ["decoder" ](bytes (result_bytes ))
25872598 as_dict = _bt_decode_to_dict_or_list (result_decoded )
2588- logging .debug ("Decoded old runtime call result: " , as_dict )
2599+ logger .debug ("Decoded old runtime call result: " , as_dict )
25892600 result_obj = ScaleObj (as_dict )
25902601
25912602 return result_obj
0 commit comments