From 0d9a4f55620a6b5066bc911db88168bf42ac3fc9 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Wed, 14 Aug 2024 15:51:33 +0200 Subject: [PATCH] Error handling and correctly handling bool return from `get_children` method. --- src/commands/stake.py | 14 ++++++++++---- src/subtensor_interface.py | 12 ++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/commands/stake.py b/src/commands/stake.py index 63f85b02..79a07b2e 100644 --- a/src/commands/stake.py +++ b/src/commands/stake.py @@ -1537,9 +1537,9 @@ async def render_table( console.print(table) async with subtensor: - err, children = await subtensor.get_children(wallet.hotkey, netuid) - if err: - err_console.print(f"Failed to get children from subtensor. {children[0]}") + success, children, err_mg = await subtensor.get_children(wallet.hotkey, netuid) + if not success: + err_console.print(f"Failed to get children from subtensor. {children[0]}: {err_mg}") if not children: console.print("[yellow]No children found.[/yellow]") @@ -1618,11 +1618,17 @@ async def revoke_children( >>> revoke_children(wallet, subtensor, 12345, wait_for_inclusion=True) """ # print table with diff prompts - status, current_children = await subtensor.get_children(wallet.hotkey.ss58_address, netuid) + async with subtensor: + success, current_children, err_msg = await subtensor.get_children(wallet.hotkey.ss58_address, netuid) + if not success: + await subtensor.substrate.close() + err_console.print(f"[red]Error retrieving children[/red]: {err_msg}") + return # Validate children SS58 addresses for child in current_children: if not is_valid_ss58_address(child): err_console.print(f":cross_mark:[red] Invalid SS58 address: {child}[/red]") + await subtensor.substrate.close() return # Prepare children with zero proportions diff --git a/src/subtensor_interface.py b/src/subtensor_interface.py index e57db73c..f730f968 100644 --- a/src/subtensor_interface.py +++ b/src/subtensor_interface.py @@ -781,7 +781,7 @@ async def sign_and_send_extrinsic( else: return False, format_error_message(response.error_message) - async def get_children(self, hotkey, netuid) -> tuple[bool, list]: + async def get_children(self, hotkey, netuid) -> tuple[bool, list, str]: """ This method retrieves the children of a given hotkey and netuid. It queries the SubtensorModule's ChildKeys storage function to get the children and formats them before returning as a tuple. @@ -789,8 +789,8 @@ async def get_children(self, hotkey, netuid) -> tuple[bool, list]: :param hotkey: The hotkey value. :param netuid: The netuid value. - :return: A tuple containing a boolean indicating success or failure, and a list of formatted children. - If an error occurs, the list will contain an exception object. + :return: A tuple containing a boolean indicating success or failure, a list of formatted children, and an error + message (if applicable) """ try: children = await self.substrate.query( @@ -808,8 +808,8 @@ async def get_children(self, hotkey, netuid) -> tuple[bool, list]: else int(proportion) ) formatted_children.append((int_proportion, child.value)) - return True, formatted_children + return True, formatted_children, "" else: - return True, [] + return True, [], "" except SubstrateRequestException as e: - return False, [e] # TODO: find a more elegant way of returning error message + return False, [], str(e)