Skip to content

Commit ad195e7

Browse files
authored
Merge pull request #6 from opentensor/feat/thewhaleking/warning-for-uninitialised
Exception for uninitialised
2 parents 161e3b8 + 95a0d42 commit ad195e7

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

async_substrate_interface/substrate_interface.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ def __init__(
10891089
else:
10901090
self.query_map_result_cls = QueryMapResult
10911091
self.extrinsic_receipt_cls = AsyncExtrinsicReceipt
1092+
self.reload_type_registry()
1093+
self._initializing = False
10921094

10931095
async def __aenter__(self):
10941096
await self.initialize()
@@ -1099,13 +1101,14 @@ async def initialize(self):
10991101
Initialize the connection to the chain.
11001102
"""
11011103
async with self._lock:
1104+
self._initializing = True
11021105
if not self.initialized:
11031106
if not self.__chain:
11041107
chain = await self.rpc_request("system_chain", [])
11051108
self.__chain = chain.get("result")
1106-
self.reload_type_registry()
11071109
await asyncio.gather(self.load_registry(), self._init_init_runtime())
11081110
self.initialized = True
1111+
self._initializing = False
11091112

11101113
async def __aexit__(self, exc_type, exc_val, exc_tb):
11111114
pass
@@ -1248,19 +1251,28 @@ async def _wait_for_registry():
12481251
if scale_bytes == b"\x00":
12491252
obj = None
12501253
else:
1251-
if not self.registry:
1252-
await asyncio.wait_for(_wait_for_registry(), timeout=10)
12531254
try:
1255+
if not self.registry:
1256+
await asyncio.wait_for(_wait_for_registry(), timeout=10)
12541257
obj = decode_by_type_string(type_string, self.registry, scale_bytes)
12551258
except TimeoutError:
12561259
# 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:
12581267
await self.load_registry()
12591268
return await self.decode_scale(
12601269
type_string, scale_bytes, _attempt + 1
12611270
)
12621271
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+
)
12641276
if return_scale_obj:
12651277
return ScaleObj(obj)
12661278
else:
@@ -2471,7 +2483,7 @@ async def get_block_runtime_version(self, block_hash: str) -> dict:
24712483

24722484
async def get_block_metadata(
24732485
self, block_hash: Optional[str] = None, decode: bool = True
2474-
) -> Union[dict, ScaleType]:
2486+
) -> Optional[Union[dict, ScaleType]]:
24752487
"""
24762488
A pass-though to existing JSONRPC method `state_getMetadata`.
24772489
@@ -2480,7 +2492,8 @@ async def get_block_metadata(
24802492
decode: Whether to decode the metadata or present it raw
24812493
24822494
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
24842497
"""
24852498
params = None
24862499
if decode and not self.runtime_config:
@@ -2495,15 +2508,15 @@ async def get_block_metadata(
24952508
if "error" in response:
24962509
raise SubstrateRequestException(response["error"]["message"])
24972510

2498-
if response.get("result") and decode:
2511+
if (result := response.get("result")) and decode:
24992512
metadata_decoder = self.runtime_config.create_scale_object(
2500-
"MetadataVersioned", data=ScaleBytes(response.get("result"))
2513+
"MetadataVersioned", data=ScaleBytes(result)
25012514
)
25022515
metadata_decoder.decode()
25032516

25042517
return metadata_decoder
2505-
2506-
return response
2518+
else:
2519+
return result
25072520

25082521
async def _preprocess(
25092522
self,

0 commit comments

Comments
 (0)