Skip to content

Commit 028ef8b

Browse files
author
opendansor
authored
Merge pull request #2225 from opentensor/feature/opendansor/improve_child_hotkeys
Improve child hotkeys QOL
2 parents 98d0af6 + b29dc57 commit 028ef8b

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

bittensor/commands/stake.py

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from rich.prompt import Confirm, Prompt
2525
from rich.table import Table
2626
from rich.console import Console
27+
from rich.text import Text
2728
from tqdm import tqdm
2829

2930
import bittensor
@@ -616,7 +617,13 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
616617
if not cli.config.is_set("hotkey"):
617618
cli.config.hotkey = Prompt.ask("Enter parent hotkey (ss58)")
618619

619-
children = GetChildrenCommand.run(cli)
620+
# display children
621+
GetChildrenCommand.retrieve_children(
622+
subtensor=subtensor,
623+
hotkey=cli.config.hotkey,
624+
netuid=cli.config.netuid,
625+
render_table=True,
626+
)
620627

621628
if not cli.config.is_set("children"):
622629
cli.config.children = Prompt.ask(
@@ -661,6 +668,12 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
661668

662669
# Result
663670
if success:
671+
GetChildrenCommand.retrieve_children(
672+
subtensor=subtensor,
673+
hotkey=cli.config.hotkey,
674+
netuid=cli.config.netuid,
675+
render_table=True,
676+
)
664677
console.print(
665678
":white_heavy_check_mark: [green]Set children hotkeys.[/green]"
666679
)
@@ -765,16 +778,39 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
765778

766779
# Get values if not set.
767780
if not cli.config.is_set("hotkey"):
768-
cli.config.netuid = Prompt.ask("Enter hotkey")
781+
cli.config.hotkey = Prompt.ask("Enter parent hotkey (ss58)")
769782

770783
# Parse from strings
771784
netuid = cli.config.netuid
772785
hotkey = cli.config.hotkey
773786

774787
children = subtensor.get_children(hotkey, netuid)
775788

776-
GetChildrenCommand.render_table(subtensor, hotkey, children, netuid)
789+
GetChildrenCommand.render_table(subtensor, hotkey, children, netuid, True)
790+
791+
return children
792+
793+
@staticmethod
794+
def retrieve_children(
795+
subtensor: "bittensor.subtensor", hotkey: str, netuid: int, render_table: bool
796+
):
797+
"""
798+
799+
Static method to retrieve children for a given subtensor.
800+
801+
Args:
802+
subtensor (bittensor.subtensor): The subtensor object used to interact with the Bittensor network.
803+
hotkey (str): The hotkey of the tensor owner.
804+
netuid (int): The network unique identifier of the subtensor.
805+
render_table (bool): Flag indicating whether to render the retrieved children in a table.
806+
807+
Returns:
808+
List[str]: A list of children hotkeys.
777809
810+
"""
811+
children = subtensor.get_children(hotkey, netuid)
812+
if render_table:
813+
GetChildrenCommand.render_table(subtensor, hotkey, children, netuid, False)
778814
return children
779815

780816
@staticmethod
@@ -803,7 +839,33 @@ def render_table(
803839
hotkey: str,
804840
children: list[Tuple[int, str]],
805841
netuid: int,
842+
prompt: bool,
806843
):
844+
"""
845+
846+
Render a table displaying information about child hotkeys on a particular subnet.
847+
848+
Parameters:
849+
- subtensor: An instance of the "bittensor.subtensor" class.
850+
- hotkey: The hotkey of the parent node.
851+
- children: A list of tuples containing information about child hotkeys. Each tuple should contain:
852+
- The proportion of the child's stake relative to the total stake.
853+
- The hotkey of the child node.
854+
- netuid: The ID of the subnet.
855+
- prompt: A boolean indicating whether to display a prompt for adding a child hotkey.
856+
857+
Returns:
858+
None
859+
860+
Example Usage:
861+
subtensor = bittensor.subtensor_instance
862+
hotkey = "parent_hotkey"
863+
children = [(0.5, "child1_hotkey"), (0.3, "child2_hotkey"), (0.2, "child3_hotkey")]
864+
netuid = 1234
865+
prompt = True
866+
render_table(subtensor, hotkey, children, netuid, prompt)
867+
868+
"""
807869
console = Console()
808870

809871
# Initialize Rich table for pretty printing
@@ -822,12 +884,14 @@ def render_table(
822884

823885
if not children:
824886
console.print(table)
825-
826-
command = f"btcli stake set_children --children <child_hotkey> --hotkey <parent_hotkey> --netuid {netuid} --proportion <float>"
827-
console.print(f"There are currently no child hotkeys on subnet {netuid}.")
828887
console.print(
829-
f"To add a child hotkey you can run the command: [white]{command}[/white]"
888+
f"There are currently no child hotkeys on subnet {netuid} with ParentHotKey {hotkey}."
830889
)
890+
if prompt:
891+
command = f"btcli stake set_children --children <child_hotkey> --hotkey <parent_hotkey> --netuid {netuid} --proportion <float>"
892+
console.print(
893+
f"To add a child hotkey you can run the command: [white]{command}[/white]"
894+
)
831895
return
832896

833897
console.print("ParentHotKey:", style="cyan", no_wrap=True)
@@ -857,10 +921,14 @@ def render_table(
857921

858922
# add the children info to the table
859923
for i, (proportion, hotkey, stake) in enumerate(children_info, 1):
924+
proportion_str = Text(
925+
str(proportion), style="red" if proportion == 0 else ""
926+
)
927+
hotkey = Text(hotkey, style="red" if proportion == 0 else "")
860928
table.add_row(
861929
str(i),
862930
hotkey,
863-
str(proportion),
931+
proportion_str,
864932
str(stake),
865933
)
866934

bittensor/commands/unstake.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,13 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
344344
if not cli.config.is_set("hotkey"):
345345
cli.config.hotkey = Prompt.ask("Enter parent hotkey (ss58)")
346346

347-
# Display current children information
348-
current_children = GetChildrenCommand.run(cli)
347+
# Get and display current children information
348+
current_children = GetChildrenCommand.retrieve_children(
349+
subtensor=subtensor,
350+
hotkey=cli.config.hotkey,
351+
netuid=cli.config.netuid,
352+
render_table=False,
353+
)
349354

350355
# Parse from strings
351356
netuid = cli.config.netuid
@@ -365,6 +370,13 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
365370

366371
# Result
367372
if success:
373+
if cli.config.wait_for_finalization and cli.config.wait_for_inclusion:
374+
GetChildrenCommand.retrieve_children(
375+
subtensor=subtensor,
376+
hotkey=cli.config.hotkey,
377+
netuid=cli.config.netuid,
378+
render_table=True,
379+
)
368380
console.print(
369381
":white_heavy_check_mark: [green]Revoked all children hotkeys.[/green]"
370382
)

bittensor/subtensor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,6 @@ def get_children(self, hotkey, netuid):
46114611
if children:
46124612
return format_children(children)
46134613
else:
4614-
print(" No children found.")
46154614
return []
46164615
except SubstrateRequestException as e:
46174616
print(f"Error querying ChildKeys: {e}")

0 commit comments

Comments
 (0)