From 3e77a3bb276d84cb88769c70f5fbe22307e20e7d Mon Sep 17 00:00:00 2001 From: bdhimes Date: Mon, 6 Oct 2025 20:57:52 +0200 Subject: [PATCH 1/2] Eliminates a lot of double-checking for "error" in response, and adds error checking for this for `get_chain_head` --- async_substrate_interface/async_substrate.py | 54 ++++++-------------- async_substrate_interface/sync_substrate.py | 38 ++++---------- 2 files changed, 25 insertions(+), 67 deletions(-) diff --git a/async_substrate_interface/async_substrate.py b/async_substrate_interface/async_substrate.py index 2cda046..b2bdd40 100644 --- a/async_substrate_interface/async_substrate.py +++ b/async_substrate_interface/async_substrate.py @@ -1407,7 +1407,8 @@ async def retrieve_pending_extrinsics(self) -> list: runtime = await self.init_runtime() result_data = await self.rpc_request("author_pendingExtrinsics", []) - + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) extrinsics = [] for extrinsic_data in result_data["result"]: @@ -2141,6 +2142,8 @@ async def get_parent_block_hash(self, block_hash) -> str: async def _get_parent_block_hash(self, block_hash) -> str: block_header = await self.rpc_request("chain_getHeader", [block_hash]) + if "error" in block_header: + raise SubstrateRequestException(block_header["error"]["message"]) if block_header["result"] is None: 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: response = await self.rpc_request( "state_getStorage", [storage_key, block_hash] ) - - if "result" in response: - return response.get("result") - elif "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - else: - raise SubstrateRequestException( - "Unknown error occurred during retrieval of events" - ) + return response.get("result") @cached_fetcher(max_size=SUBSTRATE_RUNTIME_CACHE_SIZE) async def get_block_runtime_info(self, block_hash: str) -> dict: @@ -2236,9 +2231,6 @@ async def get_block_metadata( params = [block_hash] response = await self.rpc_request("state_getMetadata", params) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - if (result := response.get("result")) and decode: metadata_decoder = runtime_config.create_scale_object( "MetadataVersioned", data=ScaleBytes(result) @@ -2562,6 +2554,8 @@ async def get_chain_head(self) -> str: ) ] ) + if "error" in result[0]: + raise SubstrateRequestException(result[0]["error"]["message"]) self.last_block_hash = result["rpc_request"][0]["result"] return result["rpc_request"][0]["result"] @@ -2690,9 +2684,6 @@ async def query_multi( runtime=runtime, ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result = [] storage_key_map = {s.to_hex(): s for s in storage_keys} @@ -3044,12 +3035,7 @@ async def get_chain_finalised_head(self): """ response = await self.rpc_request("chain_getFinalizedHead", []) - - if response is not None: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - return response.get("result") + return response["result"] async def _do_runtime_call_old( self, @@ -3092,6 +3078,8 @@ async def _do_runtime_call_old( [f"{api}_{method}", param_data.hex(), block_hash], runtime=runtime, ) + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) result_vec_u8_bytes = hex_to_bytes(result_data["result"]) result_bytes = await self.decode_scale( "Vec", result_vec_u8_bytes, runtime=runtime @@ -3185,6 +3173,8 @@ async def runtime_call( [f"{api}_{method}", param_data.hex(), block_hash], runtime=runtime, ) + if "error" in result_data: + raise SubstrateRequestException(result_data["error"]["message"]) output_type_string = f"scale_info::{runtime_call_def['output']}" # Decode result @@ -3237,6 +3227,8 @@ async def get_account_next_index(self, account_address: str) -> int: nonce_obj = await self.rpc_request( "account_nextIndex", [account_address] ) + if "error" in nonce_obj: + raise SubstrateRequestException(nonce_obj["error"]["message"]) self._nonces[account_address] = nonce_obj["result"] else: self._nonces[account_address] += 1 @@ -3622,9 +3614,6 @@ async def query_map( method="state_getKeys", params=[prefix, block_hash], runtime=runtime ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result_keys = response.get("result") result = [] @@ -3640,8 +3629,6 @@ async def query_map( params=[result_keys, block_hash], runtime=runtime, ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) for result_group in response["result"]: result = decode_query_map( result_group["changes"], @@ -3680,8 +3667,6 @@ async def query_map( ) ) for response in all_responses: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) for result_group in response["result"]: changes.extend(result_group["changes"]) @@ -3905,9 +3890,6 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]: "author_submitExtrinsic", [str(extrinsic.data)] ) - if "result" not in response: - raise SubstrateRequestException(response.get("error")) - result = AsyncExtrinsicReceipt( substrate=self, extrinsic_hash=response["result"] ) @@ -3994,12 +3976,8 @@ async def get_block_number(self, block_hash: Optional[str] = None) -> int: """Async version of `substrateinterface.base.get_block_number` method.""" response = await self.rpc_request("chain_getHeader", [block_hash]) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - elif "result" in response: - if response["result"]: - return int(response["result"]["number"], 16) + if response["result"]: + return int(response["result"]["number"], 16) raise SubstrateRequestException( f"Unable to retrieve block number for {block_hash}" ) diff --git a/async_substrate_interface/sync_substrate.py b/async_substrate_interface/sync_substrate.py index 61efc54..5854ba2 100644 --- a/async_substrate_interface/sync_substrate.py +++ b/async_substrate_interface/sync_substrate.py @@ -1709,8 +1709,6 @@ def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any: if "result" in response: return response.get("result") - elif "error" in response: - raise SubstrateRequestException(response["error"]["message"]) else: raise SubstrateRequestException( "Unknown error occurred during retrieval of events" @@ -1761,9 +1759,6 @@ def get_block_metadata( params = [block_hash] response = self.rpc_request("state_getMetadata", params) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - if (result := response.get("result")) and decode: metadata_decoder = self.runtime_config.create_scale_object( "MetadataVersioned", data=ScaleBytes(result) @@ -2082,6 +2077,8 @@ def get_chain_head(self) -> str: ) ] ) + if "error" in result[0]: + raise SubstrateRequestException(result[0]["error"]["message"]) self.last_block_hash = result["rpc_request"][0]["result"] return result["rpc_request"][0]["result"] @@ -2191,9 +2188,6 @@ def query_multi( "state_queryStorageAt", [[s.to_hex() for s in storage_keys], block_hash] ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result = [] storage_key_map = {s.to_hex(): s for s in storage_keys} @@ -2528,12 +2522,7 @@ def get_chain_finalised_head(self): """ response = self.rpc_request("chain_getFinalizedHead", []) - - if response is not None: - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - return response.get("result") + return response["result"] def _do_runtime_call_old( self, @@ -3051,9 +3040,6 @@ def query_map( params=[prefix, page_size, start_key, block_hash], ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - result_keys = response.get("result") result = [] @@ -3067,9 +3053,6 @@ def query_map( method="state_queryStorageAt", params=[result_keys, block_hash] ) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - for result_group in response["result"]: result = decode_query_map( result_group["changes"], @@ -3376,15 +3359,12 @@ def get_block_number(self, block_hash: Optional[str] = None) -> int: """Async version of `substrateinterface.base.get_block_number` method.""" response = self.rpc_request("chain_getHeader", [block_hash]) - if "error" in response: - raise SubstrateRequestException(response["error"]["message"]) - - elif "result" in response: - if response["result"]: - return int(response["result"]["number"], 16) - raise SubstrateRequestException( - f"Unable to determine block number for {block_hash}" - ) + if response["result"]: + return int(response["result"]["number"], 16) + else: + raise SubstrateRequestException( + f"Unable to determine block number for {block_hash}" + ) def close(self): """ From 9c427d57a9280d9086356c568850865312b46bb1 Mon Sep 17 00:00:00 2001 From: bdhimes Date: Mon, 6 Oct 2025 21:04:38 +0200 Subject: [PATCH 2/2] Oepsie --- async_substrate_interface/async_substrate.py | 11 ++++++----- async_substrate_interface/sync_substrate.py | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/async_substrate_interface/async_substrate.py b/async_substrate_interface/async_substrate.py index b2bdd40..3ccf8af 100644 --- a/async_substrate_interface/async_substrate.py +++ b/async_substrate_interface/async_substrate.py @@ -2545,7 +2545,7 @@ async def _get_block_hash(self, block_id: int) -> str: return (await self.rpc_request("chain_getBlockHash", [block_id]))["result"] async def get_chain_head(self) -> str: - result = await self._make_rpc_request( + response = await self._make_rpc_request( [ self.make_payload( "rpc_request", @@ -2554,10 +2554,11 @@ async def get_chain_head(self) -> str: ) ] ) - if "error" in result[0]: - raise SubstrateRequestException(result[0]["error"]["message"]) - self.last_block_hash = result["rpc_request"][0]["result"] - return result["rpc_request"][0]["result"] + result = response["rpc_request"][0] + if "error" in result: + raise SubstrateRequestException(result["error"]["message"]) + self.last_block_hash = result["result"] + return result["result"] async def compose_call( self, diff --git a/async_substrate_interface/sync_substrate.py b/async_substrate_interface/sync_substrate.py index 5854ba2..1622d78 100644 --- a/async_substrate_interface/sync_substrate.py +++ b/async_substrate_interface/sync_substrate.py @@ -2068,7 +2068,7 @@ def get_block_hash(self, block_id: int) -> str: return self.rpc_request("chain_getBlockHash", [block_id])["result"] def get_chain_head(self) -> str: - result = self._make_rpc_request( + response = self._make_rpc_request( [ self.make_payload( "rpc_request", @@ -2077,10 +2077,11 @@ def get_chain_head(self) -> str: ) ] ) - if "error" in result[0]: - raise SubstrateRequestException(result[0]["error"]["message"]) - self.last_block_hash = result["rpc_request"][0]["result"] - return result["rpc_request"][0]["result"] + result = response["rpc_request"][0] + if "error" in result: + raise SubstrateRequestException(result["error"]["message"]) + self.last_block_hash = result["result"] + return result["result"] def compose_call( self,