Skip to content

Commit 3e77a3b

Browse files
committed
Eliminates a lot of double-checking for "error" in response, and adds error checking for this for get_chain_head
1 parent a5415d8 commit 3e77a3b

File tree

2 files changed

+25
-67
lines changed

2 files changed

+25
-67
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,8 @@ async def retrieve_pending_extrinsics(self) -> list:
14071407
runtime = await self.init_runtime()
14081408

14091409
result_data = await self.rpc_request("author_pendingExtrinsics", [])
1410-
1410+
if "error" in result_data:
1411+
raise SubstrateRequestException(result_data["error"]["message"])
14111412
extrinsics = []
14121413

14131414
for extrinsic_data in result_data["result"]:
@@ -2141,6 +2142,8 @@ async def get_parent_block_hash(self, block_hash) -> str:
21412142

21422143
async def _get_parent_block_hash(self, block_hash) -> str:
21432144
block_header = await self.rpc_request("chain_getHeader", [block_hash])
2145+
if "error" in block_header:
2146+
raise SubstrateRequestException(block_header["error"]["message"])
21442147

21452148
if block_header["result"] is None:
21462149
raise SubstrateRequestException(f'Block not found for "{block_hash}"')
@@ -2172,15 +2175,7 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
21722175
response = await self.rpc_request(
21732176
"state_getStorage", [storage_key, block_hash]
21742177
)
2175-
2176-
if "result" in response:
2177-
return response.get("result")
2178-
elif "error" in response:
2179-
raise SubstrateRequestException(response["error"]["message"])
2180-
else:
2181-
raise SubstrateRequestException(
2182-
"Unknown error occurred during retrieval of events"
2183-
)
2178+
return response.get("result")
21842179

21852180
@cached_fetcher(max_size=SUBSTRATE_RUNTIME_CACHE_SIZE)
21862181
async def get_block_runtime_info(self, block_hash: str) -> dict:
@@ -2236,9 +2231,6 @@ async def get_block_metadata(
22362231
params = [block_hash]
22372232
response = await self.rpc_request("state_getMetadata", params)
22382233

2239-
if "error" in response:
2240-
raise SubstrateRequestException(response["error"]["message"])
2241-
22422234
if (result := response.get("result")) and decode:
22432235
metadata_decoder = runtime_config.create_scale_object(
22442236
"MetadataVersioned", data=ScaleBytes(result)
@@ -2562,6 +2554,8 @@ async def get_chain_head(self) -> str:
25622554
)
25632555
]
25642556
)
2557+
if "error" in result[0]:
2558+
raise SubstrateRequestException(result[0]["error"]["message"])
25652559
self.last_block_hash = result["rpc_request"][0]["result"]
25662560
return result["rpc_request"][0]["result"]
25672561

@@ -2690,9 +2684,6 @@ async def query_multi(
26902684
runtime=runtime,
26912685
)
26922686

2693-
if "error" in response:
2694-
raise SubstrateRequestException(response["error"]["message"])
2695-
26962687
result = []
26972688

26982689
storage_key_map = {s.to_hex(): s for s in storage_keys}
@@ -3044,12 +3035,7 @@ async def get_chain_finalised_head(self):
30443035
30453036
"""
30463037
response = await self.rpc_request("chain_getFinalizedHead", [])
3047-
3048-
if response is not None:
3049-
if "error" in response:
3050-
raise SubstrateRequestException(response["error"]["message"])
3051-
3052-
return response.get("result")
3038+
return response["result"]
30533039

30543040
async def _do_runtime_call_old(
30553041
self,
@@ -3092,6 +3078,8 @@ async def _do_runtime_call_old(
30923078
[f"{api}_{method}", param_data.hex(), block_hash],
30933079
runtime=runtime,
30943080
)
3081+
if "error" in result_data:
3082+
raise SubstrateRequestException(result_data["error"]["message"])
30953083
result_vec_u8_bytes = hex_to_bytes(result_data["result"])
30963084
result_bytes = await self.decode_scale(
30973085
"Vec<u8>", result_vec_u8_bytes, runtime=runtime
@@ -3185,6 +3173,8 @@ async def runtime_call(
31853173
[f"{api}_{method}", param_data.hex(), block_hash],
31863174
runtime=runtime,
31873175
)
3176+
if "error" in result_data:
3177+
raise SubstrateRequestException(result_data["error"]["message"])
31883178
output_type_string = f"scale_info::{runtime_call_def['output']}"
31893179

31903180
# Decode result
@@ -3237,6 +3227,8 @@ async def get_account_next_index(self, account_address: str) -> int:
32373227
nonce_obj = await self.rpc_request(
32383228
"account_nextIndex", [account_address]
32393229
)
3230+
if "error" in nonce_obj:
3231+
raise SubstrateRequestException(nonce_obj["error"]["message"])
32403232
self._nonces[account_address] = nonce_obj["result"]
32413233
else:
32423234
self._nonces[account_address] += 1
@@ -3622,9 +3614,6 @@ async def query_map(
36223614
method="state_getKeys", params=[prefix, block_hash], runtime=runtime
36233615
)
36243616

3625-
if "error" in response:
3626-
raise SubstrateRequestException(response["error"]["message"])
3627-
36283617
result_keys = response.get("result")
36293618

36303619
result = []
@@ -3640,8 +3629,6 @@ async def query_map(
36403629
params=[result_keys, block_hash],
36413630
runtime=runtime,
36423631
)
3643-
if "error" in response:
3644-
raise SubstrateRequestException(response["error"]["message"])
36453632
for result_group in response["result"]:
36463633
result = decode_query_map(
36473634
result_group["changes"],
@@ -3680,8 +3667,6 @@ async def query_map(
36803667
)
36813668
)
36823669
for response in all_responses:
3683-
if "error" in response:
3684-
raise SubstrateRequestException(response["error"]["message"])
36853670
for result_group in response["result"]:
36863671
changes.extend(result_group["changes"])
36873672

@@ -3905,9 +3890,6 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39053890
"author_submitExtrinsic", [str(extrinsic.data)]
39063891
)
39073892

3908-
if "result" not in response:
3909-
raise SubstrateRequestException(response.get("error"))
3910-
39113893
result = AsyncExtrinsicReceipt(
39123894
substrate=self, extrinsic_hash=response["result"]
39133895
)
@@ -3994,12 +3976,8 @@ async def get_block_number(self, block_hash: Optional[str] = None) -> int:
39943976
"""Async version of `substrateinterface.base.get_block_number` method."""
39953977
response = await self.rpc_request("chain_getHeader", [block_hash])
39963978

3997-
if "error" in response:
3998-
raise SubstrateRequestException(response["error"]["message"])
3999-
4000-
elif "result" in response:
4001-
if response["result"]:
4002-
return int(response["result"]["number"], 16)
3979+
if response["result"]:
3980+
return int(response["result"]["number"], 16)
40033981
raise SubstrateRequestException(
40043982
f"Unable to retrieve block number for {block_hash}"
40053983
)

async_substrate_interface/sync_substrate.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,6 @@ def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
17091709

17101710
if "result" in response:
17111711
return response.get("result")
1712-
elif "error" in response:
1713-
raise SubstrateRequestException(response["error"]["message"])
17141712
else:
17151713
raise SubstrateRequestException(
17161714
"Unknown error occurred during retrieval of events"
@@ -1761,9 +1759,6 @@ def get_block_metadata(
17611759
params = [block_hash]
17621760
response = self.rpc_request("state_getMetadata", params)
17631761

1764-
if "error" in response:
1765-
raise SubstrateRequestException(response["error"]["message"])
1766-
17671762
if (result := response.get("result")) and decode:
17681763
metadata_decoder = self.runtime_config.create_scale_object(
17691764
"MetadataVersioned", data=ScaleBytes(result)
@@ -2082,6 +2077,8 @@ def get_chain_head(self) -> str:
20822077
)
20832078
]
20842079
)
2080+
if "error" in result[0]:
2081+
raise SubstrateRequestException(result[0]["error"]["message"])
20852082
self.last_block_hash = result["rpc_request"][0]["result"]
20862083
return result["rpc_request"][0]["result"]
20872084

@@ -2191,9 +2188,6 @@ def query_multi(
21912188
"state_queryStorageAt", [[s.to_hex() for s in storage_keys], block_hash]
21922189
)
21932190

2194-
if "error" in response:
2195-
raise SubstrateRequestException(response["error"]["message"])
2196-
21972191
result = []
21982192

21992193
storage_key_map = {s.to_hex(): s for s in storage_keys}
@@ -2528,12 +2522,7 @@ def get_chain_finalised_head(self):
25282522
25292523
"""
25302524
response = self.rpc_request("chain_getFinalizedHead", [])
2531-
2532-
if response is not None:
2533-
if "error" in response:
2534-
raise SubstrateRequestException(response["error"]["message"])
2535-
2536-
return response.get("result")
2525+
return response["result"]
25372526

25382527
def _do_runtime_call_old(
25392528
self,
@@ -3051,9 +3040,6 @@ def query_map(
30513040
params=[prefix, page_size, start_key, block_hash],
30523041
)
30533042

3054-
if "error" in response:
3055-
raise SubstrateRequestException(response["error"]["message"])
3056-
30573043
result_keys = response.get("result")
30583044

30593045
result = []
@@ -3067,9 +3053,6 @@ def query_map(
30673053
method="state_queryStorageAt", params=[result_keys, block_hash]
30683054
)
30693055

3070-
if "error" in response:
3071-
raise SubstrateRequestException(response["error"]["message"])
3072-
30733056
for result_group in response["result"]:
30743057
result = decode_query_map(
30753058
result_group["changes"],
@@ -3376,15 +3359,12 @@ def get_block_number(self, block_hash: Optional[str] = None) -> int:
33763359
"""Async version of `substrateinterface.base.get_block_number` method."""
33773360
response = self.rpc_request("chain_getHeader", [block_hash])
33783361

3379-
if "error" in response:
3380-
raise SubstrateRequestException(response["error"]["message"])
3381-
3382-
elif "result" in response:
3383-
if response["result"]:
3384-
return int(response["result"]["number"], 16)
3385-
raise SubstrateRequestException(
3386-
f"Unable to determine block number for {block_hash}"
3387-
)
3362+
if response["result"]:
3363+
return int(response["result"]["number"], 16)
3364+
else:
3365+
raise SubstrateRequestException(
3366+
f"Unable to determine block number for {block_hash}"
3367+
)
33883368

33893369
def close(self):
33903370
"""

0 commit comments

Comments
 (0)