Skip to content

Commit

Permalink
Error handling and correctly handling bool return from get_children
Browse files Browse the repository at this point in the history
… method.
  • Loading branch information
thewhaleking committed Aug 14, 2024
1 parent c6810ae commit 0d9a4f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/commands/stake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/subtensor_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,16 +781,16 @@ 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.
: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(
Expand All @@ -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)

0 comments on commit 0d9a4f5

Please sign in to comment.