Skip to content

Commit

Permalink
Child Hotkey refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
opendansor committed Aug 20, 2024
1 parent 6dbc1f1 commit d07ee71
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
42 changes: 29 additions & 13 deletions bittensor/commands/stake.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,9 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):

netuid = cli.config.netuid
total_subnets = subtensor.get_total_subnets()
if total_subnets is not None and total_subnets <= netuid <= 0:
raise ValueError("Netuid is outside the current subnet range")
if total_subnets is not None and not (0 <= netuid <= total_subnets):
console.print("Netuid is outside the current subnet range")
return

if not cli.config.is_set("hotkey"):
cli.config.hotkey = Prompt.ask("Enter parent hotkey (ss58)")
Expand All @@ -644,10 +645,11 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
netuid=cli.config.netuid,
render_table=True,
)
raise ValueError(
console.print(
f"There are already children hotkeys under parent hotkey {cli.config.hotkey}. "
f"Call revoke_children command before attempting to set_children again, or call the get_children command to view them."
)
return

if not cli.config.is_set("children"):
cli.config.children = Prompt.ask(
Expand All @@ -660,13 +662,15 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
if not wallet_utils.is_valid_ss58_address(child):
console.print(f":cross_mark:[red] Invalid SS58 address: {child}[/red]")
return

console.print(f"the number of children is {len(children)}")
if (
len(children) == 1
): # if only one child, then they have full proportion by default
cli.config.proportions = 1.0
console.print(f"setting proportion to 1: {cli.config.proportions}")

if not cli.config.is_set("proportions"):
console.print(f"the proportion val is {cli.config.proportions}")
cli.config.proportions = Prompt.ask(
"Enter the percentage of proportion for each child as comma-separated values (total must equal 1)"
)
Expand All @@ -675,11 +679,16 @@ def _run(cli: "bittensor.cli", subtensor: "bittensor.subtensor"):
proportions = [float(x) for x in re.split(r"[ ,]+", cli.config.proportions)]
total_proposed = sum(proportions)
if total_proposed != 1:
raise ValueError(
f"Invalid proportion: The sum of all proportions must equal 1 (representing 100% of the allocation). Proposed sum of proportions is {total_proposed}."
)
console.print(f"Invalid proportion: The sum of all proportions must equal 1 (representing 100% of the allocation). Proposed sum of proportions is {total_proposed}.")
return

if len(proportions) != len(children):
console.print("Invalid proportion and children length: The count of children and number of proportion values entered do not match.")
return

children_with_proportions = list(zip(proportions, children))

SetChildrenCommand.print_current_stake(subtensor=subtensor, children=children, hotkey=cli.config.hotkey)

success, message = subtensor.set_children(
wallet=wallet,
Expand Down Expand Up @@ -737,7 +746,7 @@ def add_args(parser: argparse.ArgumentParser):
"--wait_for_inclusion",
dest="wait_for_inclusion",
action="store_true",
default=False,
default=True,
help="""Wait for the transaction to be included in a block.""",
)
set_children_parser.add_argument(
Expand All @@ -751,12 +760,22 @@ def add_args(parser: argparse.ArgumentParser):
"--prompt",
dest="prompt",
action="store_true",
default=False,
default=True,
help="""Prompt for confirmation before proceeding.""",
)
bittensor.wallet.add_args(set_children_parser)
bittensor.subtensor.add_args(set_children_parser)

@staticmethod
def print_current_stake(subtensor, children, hotkey):
parent_stake = subtensor.get_total_stake_for_hotkey(
ss58_address=hotkey
)
console.print(f"Parent Hotkey: {hotkey} | Total Parent Stake: {parent_stake}τ")
for child in children:
child_stake = subtensor.get_total_stake_for_hotkey(child)
console.print(f"Child Hotkey: {child} | Current Child Stake: {child_stake}τ")


class GetChildrenCommand:
"""
Expand Down Expand Up @@ -918,9 +937,8 @@ def render_table(
table.add_column("Index", style="cyan", no_wrap=True, justify="right")
table.add_column("ChildHotkey", style="cyan", no_wrap=True)
table.add_column("Proportion", style="cyan", no_wrap=True, justify="right")
table.add_column("Child Stake", style="cyan", no_wrap=True, justify="right")
table.add_column(
"Total Stake Weight", style="cyan", no_wrap=True, justify="right"
"New Stake Weight", style="cyan", no_wrap=True, justify="right"
)

if not children:
Expand Down Expand Up @@ -981,7 +999,6 @@ def render_table(
str(i),
hotkey,
proportion_str,
str(stake.tao),
str(stake_weight),
)

Expand All @@ -990,7 +1007,6 @@ def render_table(
"",
"Total",
f"{total_proportion}%",
f"{total_stake}τ",
f"{total_stake_weight}τ",
)
console.print(table)
6 changes: 3 additions & 3 deletions bittensor/commands/unstake.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,21 +405,21 @@ def add_args(parser: argparse.ArgumentParser):
"--wait_for_inclusion",
dest="wait_for_inclusion",
action="store_true",
default=False,
default=True,
help="""Wait for the transaction to be included in a block.""",
)
parser.add_argument(
"--wait_for_finalization",
dest="wait_for_finalization",
action="store_true",
default=False,
default=True,
help="""Wait for the transaction to be finalized.""",
)
parser.add_argument(
"--prompt",
dest="prompt",
action="store_true",
default=False,
default=True,
help="""Prompt for confirmation before proceeding.""",
)
bittensor.wallet.add_args(parser)
Expand Down
7 changes: 6 additions & 1 deletion bittensor/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

from bittensor.utils.balance import Balance

console = bittensor.__console__


def _check_threshold_amount(
subtensor: "bittensor.subtensor", stake_balance: Balance
Expand Down Expand Up @@ -606,6 +608,7 @@ def set_children_extrinsic(
if not all_revoked
else children_with_proportions
)
console.print(f"setting children with values {normalized_children}")

success, error_message = subtensor._do_set_children(
wallet=wallet,
Expand All @@ -615,7 +618,9 @@ def set_children_extrinsic(
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

console.print(
f"wait_for_inclusion: {wait_for_inclusion} wait_for_finalization: {wait_for_finalization}"
)
if not wait_for_finalization and not wait_for_inclusion:
return (
True,
Expand Down

0 comments on commit d07ee71

Please sign in to comment.