Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for root + enhancements #60

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import rich
import typer
import numpy as np
from bittensor_wallet import Wallet
from git import Repo, GitError
from rich.prompt import Confirm, FloatPrompt, Prompt, IntPrompt
Expand Down Expand Up @@ -43,6 +44,8 @@

_epilog = "Made with [bold red]:heart:[/bold red] by Openτensor Foundaτion"

np.set_printoptions(precision=8, suppress=True, floatmode="fixed")


class Options:
"""
Expand Down Expand Up @@ -3059,8 +3062,8 @@ def stake_get_children(

# Example usage:

[green]$[/green]btcli stake get_children --netuid 1
[green]$[/green]btcli stake get_children --all-netuids
[green]$[/green] btcli stake get_children --netuid 1
[green]$[/green] btcli stake get_children --all-netuids

[italic]Note[/italic]: This command is for users who wish to see child hotkeys among different neurons (hotkeys) on the network.
"""
Expand Down Expand Up @@ -3093,6 +3096,7 @@ def stake_set_children(
proportions: list[float] = typer.Option(
[],
"--proportions",
"-prop",
help="Enter proportions for children as (sum less than 1)",
prompt=False,
),
Expand All @@ -3117,10 +3121,9 @@ def stake_set_children(

# Example usage:

[green]$[/green] btcli stake child set - <child_hotkey> -c <child_hotkey> --hotkey <parent_hotkey> --netuid 1
-p 0.3 -p 0.3
[green]$[/green] btcli stake child set - <child_hotkey> -c <child_hotkey> --hotkey <parent_hotkey> --netuid 1 -prop 0.3 -prop 0.3

[italic]]Note[/italic]: This command is critical for users who wish to delegate children hotkeys among different
[italic]Note[/italic]: This command is critical for users who wish to delegate children hotkeys among different
neurons (hotkeys) on the network. It allows for a strategic allocation of authority to enhance network participation and influence.
"""
self.verbosity_handler(quiet, verbose)
Expand Down
24 changes: 16 additions & 8 deletions bittensor_cli/src/bittensor/extrinsics/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
from numpy.typing import NDArray
from rich.prompt import Confirm
from rich.table import Table, Column
from scalecodec import ScaleBytes, U16, Vec
from substrateinterface import Keypair

Expand Down Expand Up @@ -432,18 +433,25 @@ async def _do_set_weights():
print_verbose("Normalizing weights")
formatted_weights = normalize_max_weight(x=weights, limit=max_weight_limit)
console.print(
f"\nRaw Weights -> Normalized weights: \n\t{weights} -> \n\t{formatted_weights}\n"
f"\nRaw weights -> Normalized weights: \n\t{weights} -> \n\t{formatted_weights}\n"
)

# Ask before moving on.
if prompt:
if not Confirm.ask(
"Do you want to set the following root weights?:\n"
f"[bold white]"
f" weights: {formatted_weights}\n"
f" uids: {netuids}"
"[/bold white]?"
):
table = Table(
Column("[dark_orange]Netuid", justify="center", style="bold green"),
Column(
"[dark_orange]Weight", justify="center", style="bold light_goldenrod2"
),
expand=False,
show_edge=False,
)

for netuid, weight in zip(netuids, formatted_weights):
table.add_row(str(netuid), f"{weight:.8f}")

console.print(table)
if not Confirm.ask("\nDo you want to set these root weights?"):
return False

try:
Expand Down
78 changes: 49 additions & 29 deletions bittensor_cli/src/commands/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ async def _get_list() -> tuple:
style="dark_sea_green",
no_wrap=True,
),
title="[underline dark_orange]Root Network",
title=f"[underline dark_orange]Root Network[/underline dark_orange]\n[dark_orange]Network {subtensor.network}",
show_footer=True,
show_edge=False,
expand=False,
Expand Down Expand Up @@ -815,6 +815,7 @@ async def set_weights(
"""Set weights for root network."""
netuids_ = np.array(netuids, dtype=np.int64)
weights_ = np.array(weights, dtype=np.float32)
console.print(f"Setting weights in [dark_orange]network: {subtensor.network}")

# Run the set weights operation.

Expand Down Expand Up @@ -958,11 +959,17 @@ async def _get_my_weights(
),
)
my_weights: list[tuple[int, int]] = my_weights_.value
for i, w in enumerate(my_weights):

print_verbose("Fetching current weights")
for _, w in enumerate(my_weights):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we need to enumerate this?

if w:
print(i, w)
print_verbose(f"{w}")
total_subnets: int = total_subnets_.value

# If setting weights for the first time, pass 0 root weights
if not my_weights:
my_weights = [(0, 0)]

uids, values = zip(*my_weights)
weight_array = convert_weight_uids_and_vals_to_tensor(total_subnets, uids, values)
return weight_array
Expand All @@ -976,6 +983,7 @@ async def set_boost(
prompt: bool,
):
"""Boosts weight of a given netuid for root network."""
console.print(f"Boosting weights in [dark_orange]network: {subtensor.network}")
print_verbose(f"Fetching uid of hotkey on root: {wallet.hotkey_str}")
my_uid = (
await subtensor.substrate.query(
Expand All @@ -989,16 +997,18 @@ async def set_boost(

print_verbose("Fetching current weights")
my_weights = await _get_my_weights(subtensor, wallet.hotkey.ss58_address, my_uid)
prev_weight = my_weights[netuid]
new_weight = prev_weight + amount
prev_weights = my_weights.copy()
my_weights[netuid] += amount
all_netuids = np.arange(len(my_weights))

console.print(
f"Boosting weight for netuid {netuid} from {prev_weight} -> {new_weight}"
f"Boosting weight for netuid {netuid}\n\tfrom {prev_weights[netuid]} to {my_weights[netuid]}\n"
)
console.print(
f"Previous weights -> Raw weights: \n\t{prev_weights} -> \n\t{my_weights}"
)
my_weights[netuid] = new_weight
all_netuids = np.arange(len(my_weights))

console.print("all netuids", all_netuids)
print_verbose(f"All netuids: {all_netuids}")
await set_root_weights_extrinsic(
subtensor=subtensor,
wallet=wallet,
Expand All @@ -1019,6 +1029,7 @@ async def set_slash(
prompt: bool,
):
"""Slashes weight"""
console.print(f"Slashing weights in [dark_orange]network: {subtensor.network}")
print_verbose(f"Fetching uid of hotkey on root: {wallet.hotkey_str}")
my_uid = (
await subtensor.substrate.query(
Expand All @@ -1036,19 +1047,23 @@ async def set_slash(
my_weights[my_weights < 0] = 0 # Ensure weights don't go negative
all_netuids = np.arange(len(my_weights))

console.print(f"Slash weights from {prev_weights} -> {my_weights}")
console.print(
f"Slashing weight for netuid {netuid}\n\tfrom {prev_weights[netuid]} to {my_weights[netuid]}\n"
)
console.print(
f"Previous weights -> Raw weights: \n\t{prev_weights} -> \n\t{my_weights}"
)

with console.status("Setting root weights..."):
await set_root_weights_extrinsic(
subtensor=subtensor,
wallet=wallet,
netuids=all_netuids,
weights=my_weights,
version_key=0,
wait_for_inclusion=True,
wait_for_finalization=True,
prompt=prompt,
)
await set_root_weights_extrinsic(
subtensor=subtensor,
wallet=wallet,
netuids=all_netuids,
weights=my_weights,
version_key=0,
wait_for_inclusion=True,
wait_for_finalization=True,
prompt=prompt,
)


async def senate_vote(
Expand Down Expand Up @@ -1076,7 +1091,7 @@ async def senate_vote(
wallet.unlock_hotkey()
wallet.unlock_coldkey()

print_verbose("Fetching vote data")
console.print(f"Fetching proposals in [dark_orange]network: {subtensor.network}")
vote_data = await _get_vote_data(subtensor, proposal_hash, reuse_block=True)
if not vote_data:
err_console.print(":cross_mark: [red]Failed[/red]: Proposal not found.")
Expand Down Expand Up @@ -1122,7 +1137,7 @@ async def get_senate(subtensor: SubtensorInterface):
style="bright_magenta",
no_wrap=True,
),
title="[underline dark_orange]Senate",
title=f"[underline dark_orange]Senate[/underline dark_orange]\n[dark_orange]Network: {subtensor.network}\n",
show_footer=True,
show_edge=False,
expand=False,
Expand All @@ -1141,6 +1156,8 @@ async def get_senate(subtensor: SubtensorInterface):
async def register(wallet: Wallet, subtensor: SubtensorInterface, prompt: bool):
"""Register neuron by recycling some TAO."""

console.print(f"Registering on [dark_orange]network: {subtensor.network}")

# Check current recycle amount
print_verbose("Fetching recycle amount & balance")
recycle_call, balance_ = await asyncio.gather(
Expand Down Expand Up @@ -1216,7 +1233,7 @@ async def proposals(subtensor: SubtensorInterface):
),
Column("[white]END", style="bright_cyan"),
Column("[white]CALLDATA", style="dark_sea_green"),
title=f"\n[dark_orange]Proposals\t\t\nActive Proposals: {len(all_proposals)}\t\tSenate Size: {len(senate_members)}",
title=f"\n[dark_orange]Proposals\t\t\nActive Proposals: {len(all_proposals)}\t\tSenate Size: {len(senate_members)}\nNetwork: {subtensor.network}",
show_footer=True,
box=box.SIMPLE_HEAVY,
pad_edge=False,
Expand Down Expand Up @@ -1287,6 +1304,7 @@ async def _do_set_take() -> bool:
)
return True

console.print(f"Setting take on [dark_orange]network: {subtensor.network}")
# Unlock the wallet.
wallet.unlock_hotkey()
wallet.unlock_coldkey()
Expand All @@ -1304,7 +1322,7 @@ async def delegate_stake(
prompt: bool,
):
"""Delegates stake to a chain delegate."""

console.print(f"Delegating stake on [dark_orange]network: {subtensor.network}")
await delegate_extrinsic(
subtensor,
wallet,
Expand All @@ -1324,6 +1342,7 @@ async def delegate_unstake(
prompt: bool,
):
"""Undelegates stake from a chain delegate."""
console.print(f"Undelegating stake on [dark_orange]network: {subtensor.network}")
await delegate_extrinsic(
subtensor,
wallet,
Expand Down Expand Up @@ -1385,11 +1404,11 @@ async def wallet_to_delegates(
style="light_goldenrod2",
no_wrap=True,
),
Column("[white]SUBNETS", justify="right", style="white", no_wrap=True),
Column("[white]VPERMIT", justify="right", no_wrap=True),
Column("[white]SUBNETS", justify="right", style="white"),
Column("[white]VPERMIT", justify="right"),
Column("[white]24h/k\u03c4", style="rgb(42,161,152)", justify="center"),
Column("[white]Desc", style="rgb(50,163,219)"),
title="[underline dark_orange]My Delegates\n",
title=f"[underline dark_orange]My Delegates[/underline dark_orange]\n[dark_orange]Network: {subtensor.network}\n",
show_footer=True,
show_edge=False,
expand=False,
Expand Down Expand Up @@ -1564,7 +1583,7 @@ async def list_delegates(subtensor: SubtensorInterface):
style="dark_sea_green",
),
Column("[white]Desc", style="rgb(50,163,219)", max_width=30),
title="[underline dark_orange]Root Delegates\n",
title=f"[underline dark_orange]Root Delegates[/underline dark_orange]\n[dark_orange]Network: {subtensor.network}\n",
show_footer=True,
width=table_width,
pad_edge=False,
Expand Down Expand Up @@ -1650,6 +1669,7 @@ async def list_delegates(subtensor: SubtensorInterface):
async def nominate(wallet: Wallet, subtensor: SubtensorInterface, prompt: bool):
"""Nominate wallet."""

console.print(f"Nominating on [dark_orange]network: {subtensor.network}")
# Unlock the wallet.
wallet.unlock_hotkey()
wallet.unlock_coldkey()
Expand Down
18 changes: 9 additions & 9 deletions bittensor_cli/src/commands/stake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,9 +1695,10 @@ async def get_total_stake_for_hk(hotkey: str, parent: bool = False):
)
if parent:
console.print(
f"\nYour Hotkey: {hotkey} | ", style="cyan", end="", no_wrap=True
f"\nYour Hotkey: [bright_magenta]{hotkey}[/bright_magenta] | Total Stake: [dark_orange]{stake}t[/dark_orange]\n",
end="",
no_wrap=True,
)
console.print(f"Total Stake: {stake}τ")

return stake

Expand Down Expand Up @@ -1727,18 +1728,17 @@ async def _render_table(
):
# Initialize Rich table for pretty printing
table = Table(
show_header=True,
header_style="bold magenta",
border_style="blue",
header_style="bold white",
border_style="bright_black",
style="dim",
)

# Add columns to the table with specific styles
table.add_column("Index", style="bold yellow", no_wrap=True, justify="center")
table.add_column("Child Hotkey", style="bold green")
table.add_column("Index", style="bold white", no_wrap=True, justify="center")
table.add_column("Child Hotkey", style="bold bright_magenta")
table.add_column("Proportion", style="bold cyan", no_wrap=True, justify="right")
table.add_column(
"Childkey Take", style="bold blue", no_wrap=True, justify="right"
"Childkey Take", style="light_goldenrod2", no_wrap=True, justify="right"
)
table.add_column(
"Current Stake Weight", style="bold red", no_wrap=True, justify="right"
Expand Down Expand Up @@ -1789,7 +1789,7 @@ async def _render_table(
key=lambda x: x[0], reverse=True
) # sorting by proportion (highest first)

console.print(f"\nChildren for netuid: {netuid} ", style="cyan")
console.print(f"\n[dark_orange]Children for netuid: {netuid} ")

# add the children info to the table
for idx, (proportion, hotkey, stake, child_take) in enumerate(children_info, 1):
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e_tests/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_root_commands(local_chain, wallet_setup):

# Capture root information and assert correct values
# First two rows are labels, entries start from the third row
bob_root_info = check_root_list.stdout.splitlines()[3].split()
bob_root_info = check_root_list.stdout.splitlines()[4].split()

# UID: First uid is always 0
assert bob_root_info[0] == "0"
Expand All @@ -102,7 +102,7 @@ def test_root_commands(local_chain, wallet_setup):

# Capture delegate information and assert correct values
# First row are labels, entries start from the second row
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()
bob_delegate_info = check_delegates.stdout.splitlines()[4].split()

# INDEX: First uid is always 0
assert bob_delegate_info[0] == "0"
Expand Down Expand Up @@ -161,7 +161,7 @@ def test_root_commands(local_chain, wallet_setup):
],
)
# Capture delegate information after setting take
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()
bob_delegate_info = check_delegates.stdout.splitlines()[4].split()

# Take percentage: This should be 18% by default
take_percentage = float(bob_delegate_info[6].strip("%")) / 100
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_root_commands(local_chain, wallet_setup):
],
)
# First row are headers, records start from second row
alice_delegates_info = alice_delegates.stdout.splitlines()[4].split()
alice_delegates_info = alice_delegates.stdout.splitlines()[5].split()

# WALLET: Wallet name of Alice
assert alice_delegates_info[0] == wallet_alice.name
Expand All @@ -224,7 +224,7 @@ def test_root_commands(local_chain, wallet_setup):
# Total delegated Tao: This is listed at the bottom of the information
# Since Alice has only delegated to Bob, total should be 10 TAO
total_delegated_tao = Balance.from_tao(
float(alice_delegates.stdout.splitlines()[7].split()[3].strip("τ"))
float(alice_delegates.stdout.splitlines()[8].split()[3].strip("τ"))
)
assert total_delegated_tao == Balance.from_tao(10)

Expand Down
Loading