@@ -1089,6 +1089,8 @@ def __init__(
1089
1089
else :
1090
1090
self .query_map_result_cls = QueryMapResult
1091
1091
self .extrinsic_receipt_cls = AsyncExtrinsicReceipt
1092
+ self .reload_type_registry ()
1093
+ self ._initializing = False
1092
1094
1093
1095
async def __aenter__ (self ):
1094
1096
await self .initialize ()
@@ -1099,13 +1101,14 @@ async def initialize(self):
1099
1101
Initialize the connection to the chain.
1100
1102
"""
1101
1103
async with self ._lock :
1104
+ self ._initializing = True
1102
1105
if not self .initialized :
1103
1106
if not self .__chain :
1104
1107
chain = await self .rpc_request ("system_chain" , [])
1105
1108
self .__chain = chain .get ("result" )
1106
- self .reload_type_registry ()
1107
1109
await asyncio .gather (self .load_registry (), self ._init_init_runtime ())
1108
1110
self .initialized = True
1111
+ self ._initializing = False
1109
1112
1110
1113
async def __aexit__ (self , exc_type , exc_val , exc_tb ):
1111
1114
pass
@@ -1248,19 +1251,28 @@ async def _wait_for_registry():
1248
1251
if scale_bytes == b"\x00 " :
1249
1252
obj = None
1250
1253
else :
1251
- if not self .registry :
1252
- await asyncio .wait_for (_wait_for_registry (), timeout = 10 )
1253
1254
try :
1255
+ if not self .registry :
1256
+ await asyncio .wait_for (_wait_for_registry (), timeout = 10 )
1254
1257
obj = decode_by_type_string (type_string , self .registry , scale_bytes )
1255
1258
except TimeoutError :
1256
1259
# indicates that registry was never loaded
1257
- if _attempt < _retries :
1260
+ if not self ._initializing :
1261
+ raise AttributeError (
1262
+ "Registry was never loaded. This did not occur during initialization, which usually indicates "
1263
+ "you must first initialize the AsyncSubstrateInterface object, either with "
1264
+ "`await AsyncSubstrateInterface.initialize()` or running with `async with`"
1265
+ )
1266
+ elif _attempt < _retries :
1258
1267
await self .load_registry ()
1259
1268
return await self .decode_scale (
1260
1269
type_string , scale_bytes , _attempt + 1
1261
1270
)
1262
1271
else :
1263
- raise ValueError ("Registry was never loaded." )
1272
+ raise AttributeError (
1273
+ "Registry was never loaded. This occurred during initialization, which usually indicates a "
1274
+ "connection or node error."
1275
+ )
1264
1276
if return_scale_obj :
1265
1277
return ScaleObj (obj )
1266
1278
else :
@@ -2471,7 +2483,7 @@ async def get_block_runtime_version(self, block_hash: str) -> dict:
2471
2483
2472
2484
async def get_block_metadata (
2473
2485
self , block_hash : Optional [str ] = None , decode : bool = True
2474
- ) -> Union [dict , ScaleType ]:
2486
+ ) -> Optional [ Union [dict , ScaleType ] ]:
2475
2487
"""
2476
2488
A pass-though to existing JSONRPC method `state_getMetadata`.
2477
2489
@@ -2480,7 +2492,8 @@ async def get_block_metadata(
2480
2492
decode: Whether to decode the metadata or present it raw
2481
2493
2482
2494
Returns:
2483
- metadata, either as a dict (not decoded) or ScaleType (decoded)
2495
+ metadata, either as a dict (not decoded) or ScaleType (decoded); None if there was no response
2496
+ from the server
2484
2497
"""
2485
2498
params = None
2486
2499
if decode and not self .runtime_config :
@@ -2495,15 +2508,15 @@ async def get_block_metadata(
2495
2508
if "error" in response :
2496
2509
raise SubstrateRequestException (response ["error" ]["message" ])
2497
2510
2498
- if response .get ("result" ) and decode :
2511
+ if ( result := response .get ("result" ) ) and decode :
2499
2512
metadata_decoder = self .runtime_config .create_scale_object (
2500
- "MetadataVersioned" , data = ScaleBytes (response . get ( " result" ) )
2513
+ "MetadataVersioned" , data = ScaleBytes (result )
2501
2514
)
2502
2515
metadata_decoder .decode ()
2503
2516
2504
2517
return metadata_decoder
2505
-
2506
- return response
2518
+ else :
2519
+ return result
2507
2520
2508
2521
async def _preprocess (
2509
2522
self ,
0 commit comments