From 1d6c5fd466fee7a02c76fd76e7d9421ce4d11dcb Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 30 Sep 2024 22:58:42 +0200 Subject: [PATCH 1/5] Set archive network if network is not specified as well as if network specified as finney (through option or config) --- bittensor_cli/cli.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index ea272b22e..15889f5db 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -897,7 +897,9 @@ def set_config( if valid_endpoint: if valid_endpoint in Constants.network_map.values(): known_network = next( - key for key, value in Constants.network_map.items() if value == network + key + for key, value in Constants.network_map.items() + if value == network ) args["network"] = known_network if not Confirm.ask( @@ -2802,7 +2804,7 @@ def root_my_delegates( wallet_path, wallet_hotkey, ask_for=([WO.NAME] if not all_wallets else [WO.PATH]), - validate=WV.WALLET if not all_wallets else WV.NONE + validate=WV.WALLET if not all_wallets else WV.NONE, ) self._run_command( root.my_delegates(wallet, self.initialize_chain(network), all_wallets) @@ -2860,17 +2862,17 @@ def root_list_delegates( [green]$[/green] btcli root list_delegates --subtensor.network finney # can also be `test` or `local` - [blue bold]NOTE[/blue bold]: This commmand is intended for use within a + [blue bold]NOTE[/blue bold]: This command is intended for use within a console application. It prints directly to the console and does not return any value. """ self.verbosity_handler(quiet, verbose) - if network: - if network == "finney": - network = "wss://archive.chain.opentensor.ai:443" - elif self.config.get("network"): - if self.config.get("network") == "finney": - network = "wss://archive.chain.opentensor.ai:443" + if network and network == "finney": + network = "wss://archive.chain.opentensor.ai:443" + elif self.config.get("network") == "finney": + network = "wss://archive.chain.opentensor.ai:443" + elif not network: + network = "wss://archive.chain.opentensor.ai:443" sub = self.initialize_chain(network) return self._run_command(root.list_delegates(sub)) From c12575d8994680ed905876f09ba7c0520c72932e Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 30 Sep 2024 23:11:15 +0200 Subject: [PATCH 2/5] Corrected the logic --- bittensor_cli/cli.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 15889f5db..0bc60825f 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -2867,11 +2867,14 @@ def root_list_delegates( """ self.verbosity_handler(quiet, verbose) - if network and network == "finney": + if network: + if network == "finney": + network = "wss://archive.chain.opentensor.ai:443" + elif (conf_net := self.config.get("network")) == "finney": network = "wss://archive.chain.opentensor.ai:443" - elif self.config.get("network") == "finney": - network = "wss://archive.chain.opentensor.ai:443" - elif not network: + elif conf_net: + network = conf_net + else: network = "wss://archive.chain.opentensor.ai:443" sub = self.initialize_chain(network) From 0e93148de4a7b2846708b79bca6aa8b7b779a2bc Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 30 Sep 2024 23:50:52 +0200 Subject: [PATCH 3/5] Update tests to remove dual-use of network/chain, since they call the same thing. --- bittensor_cli/cli.py | 6 +++--- tests/e2e_tests/test_root.py | 28 +++++++++++----------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 0bc60825f..5ceeb0b44 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -712,11 +712,11 @@ def initialize_chain( network: Optional[str] = None, ) -> SubtensorInterface: """ - Intelligently initializes a connection to the chain, depending on the supplied (or in config) values. Set's the + Intelligently initializes a connection to the chain, depending on the supplied (or in config) values. Sets the `self.not_subtensor` object to this created connection. - :param network: Network name (e.g. finney, test, etc.) - :param chain: the chain endpoint (e.g. ws://127.0.0.1:9945, wss://entrypoint-finney.opentensor.ai:443, etc.) + :param network: Network name (e.g. finney, test, etc.) or + chain endpoint (e.g. ws://127.0.0.1:9945, wss://entrypoint-finney.opentensor.ai:443) """ if not self.not_subtensor: if network: diff --git a/tests/e2e_tests/test_root.py b/tests/e2e_tests/test_root.py index 7fe4432a9..aaac49a5a 100644 --- a/tests/e2e_tests/test_root.py +++ b/tests/e2e_tests/test_root.py @@ -70,7 +70,7 @@ def test_root_commands(local_chain, wallet_setup): command="root", sub_command="list", extra_args=[ - "--chain", + "--network", "ws://127.0.0.1:9945", ], ) @@ -93,7 +93,7 @@ def test_root_commands(local_chain, wallet_setup): command="root", sub_command="list-delegates", extra_args=[ - "--chain", + "--network", "ws://127.0.0.1:9945", ], ) @@ -131,7 +131,7 @@ def test_root_commands(local_chain, wallet_setup): extra_args=[ "--wallet-path", wallet_path_bob, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet_bob.name, @@ -150,7 +150,7 @@ def test_root_commands(local_chain, wallet_setup): command="root", sub_command="list-delegates", extra_args=[ - "--chain", + "--network", "ws://127.0.0.1:9945", ], ) @@ -168,7 +168,7 @@ def test_root_commands(local_chain, wallet_setup): extra_args=[ "--wallet-path", wallet_path_alice, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet_alice.name, @@ -206,7 +206,7 @@ def test_root_commands(local_chain, wallet_setup): extra_args=[ "--wallet-path", wallet_path_alice, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet_alice.name, @@ -239,14 +239,12 @@ def test_root_commands(local_chain, wallet_setup): extra_args=[ "--wallet-path", wallet_path_alice, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet_alice.name, "--delegate-ss58key", wallet_bob.hotkey.ss58_address, - "--network", - "local", "--all", "--no-prompt", ], @@ -278,7 +276,7 @@ def test_root_commands(local_chain, wallet_setup): extra_args=[ "--wallet-path", wallet_path_alice, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet_alice.name, @@ -309,12 +307,10 @@ def check_my_delegates(exec_command, wallet, delegate_ss58key, delegate_amount): extra_args=[ "--wallet-path", wallet.path, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", wallet.name, - "--network", - "local", ], ) # First row are headers, records start from second row @@ -345,12 +341,10 @@ def check_balance(exec_command, wallet, expected_balance): extra_args=[ "--wallet-path", wallet.path, - "--chain", + "--network", "ws://127.0.0.1:9945", "--wallet-name", - wallet.name, - "--network", - "local", + wallet.name ], ) From ae9ba26c754c8fd5dae2b63633d63df9498b3ae3 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 30 Sep 2024 23:51:04 +0200 Subject: [PATCH 4/5] Ruff --- .../src/bittensor/subtensor_interface.py | 6 ++-- bittensor_cli/src/commands/root.py | 25 +++++++++++------ tests/e2e_tests/test_root.py | 28 +++++++++++++++---- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/bittensor_cli/src/bittensor/subtensor_interface.py b/bittensor_cli/src/bittensor/subtensor_interface.py index 46a3a2c9c..246c01a67 100644 --- a/bittensor_cli/src/bittensor/subtensor_interface.py +++ b/bittensor_cli/src/bittensor/subtensor_interface.py @@ -34,7 +34,7 @@ console, err_console, decode_hex_identity_dict, - validate_chain_endpoint + validate_chain_endpoint, ) @@ -84,7 +84,9 @@ def __init__(self, network): self.chain_endpoint = network if network in Constants.network_map.values(): self.network = next( - key for key, value in Constants.network_map.items() if value == network + key + for key, value in Constants.network_map.items() + if value == network ) else: self.network = "custom" diff --git a/bittensor_cli/src/commands/root.py b/bittensor_cli/src/commands/root.py index 716602af6..c9d8f9241 100644 --- a/bittensor_cli/src/commands/root.py +++ b/bittensor_cli/src/commands/root.py @@ -490,13 +490,19 @@ async def _do_delegation(staking_balance_: Balance) -> tuple[bool, str]: call = await subtensor.substrate.compose_call( call_module="SubtensorModule", call_function="add_stake", - call_params={"hotkey": delegate_ss58, "amount_staked": staking_balance_.rao}, + call_params={ + "hotkey": delegate_ss58, + "amount_staked": staking_balance_.rao, + }, ) else: call = await subtensor.substrate.compose_call( call_module="SubtensorModule", call_function="remove_stake", - call_params={"hotkey": delegate_ss58, "amount_unstaked": staking_balance_.rao}, + call_params={ + "hotkey": delegate_ss58, + "amount_unstaked": staking_balance_.rao, + }, ) return await subtensor.sign_and_send_extrinsic( call, wallet, wait_for_inclusion, wait_for_finalization @@ -576,7 +582,7 @@ async def get_stake_for_coldkey_and_hotkey( staking_balance = Balance.from_tao(amount) # Check enough balance to stake. - if delegate_string == "delegate" and staking_balance > my_prev_coldkey_balance: + if delegate_string == "delegate" and staking_balance > my_prev_coldkey_balance: err_console.print( ":cross_mark: [red]Not enough balance to stake[/red]:\n" f" [bold blue]current balance[/bold blue]:{my_prev_coldkey_balance}\n" @@ -726,21 +732,22 @@ async def _get_list() -> tuple: ) return sm, rn, di, ts - with console.status( f":satellite: Syncing with chain: [white]{subtensor}[/white] ...", spinner="aesthetic", ): - senate_members, root_neurons, delegate_info, total_stakes = await _get_list() - total_tao = sum(float(Balance.from_rao(total_stakes[neuron.hotkey])) for neuron in root_neurons) + total_tao = sum( + float(Balance.from_rao(total_stakes[neuron.hotkey])) + for neuron in root_neurons + ) table = Table( Column( "[bold white]UID", style="dark_orange", no_wrap=True, - footer=f"[bold]{len(root_neurons)}[/bold]" + footer=f"[bold]{len(root_neurons)}[/bold]", ), Column( "[bold white]NAME", @@ -757,7 +764,7 @@ async def _get_list() -> tuple: justify="right", style="light_goldenrod2", no_wrap=True, - footer=f"{total_tao:.2f} (\u03c4) " + footer=f"{total_tao:.2f} (\u03c4) ", ), Column( "[bold white]SENATOR", @@ -777,7 +784,7 @@ async def _get_list() -> tuple: f"[red]Error: No neurons detected on the network:[/red] [white]{subtensor}" ) raise typer.Exit() - + sorted_root_neurons = sorted( root_neurons, key=lambda neuron: float(Balance.from_rao(total_stakes[neuron.hotkey])), diff --git a/tests/e2e_tests/test_root.py b/tests/e2e_tests/test_root.py index aaac49a5a..e179329ec 100644 --- a/tests/e2e_tests/test_root.py +++ b/tests/e2e_tests/test_root.py @@ -185,13 +185,17 @@ def test_root_commands(local_chain, wallet_setup): exec_command=exec_command_alice, wallet=wallet_alice, delegate_ss58key=wallet_bob.hotkey.ss58_address, - delegate_amount=10 + delegate_amount=10, ) check_balance( exec_command=exec_command_alice, wallet=wallet_alice, - expected_balance={'free_balance': 999990.0, 'staked_balance': 10.0, 'total_balance': 1000000.0}, + expected_balance={ + "free_balance": 999990.0, + "staked_balance": 10.0, + "total_balance": 1000000.0, + }, ) # TODO: Ask nucleus the rate limit and wait epoch @@ -224,7 +228,11 @@ def test_root_commands(local_chain, wallet_setup): check_balance( exec_command=exec_command_alice, wallet=wallet_alice, - expected_balance={'free_balance': 1000000.0, 'staked_balance': 0.0, 'total_balance': 1000000.0}, + expected_balance={ + "free_balance": 1000000.0, + "staked_balance": 0.0, + "total_balance": 1000000.0, + }, ) # TODO: Ask nucleus the rate limit and wait epoch @@ -261,7 +269,11 @@ def test_root_commands(local_chain, wallet_setup): check_balance( exec_command=exec_command_alice, wallet=wallet_alice, - expected_balance={'free_balance': 0.0000005, 'staked_balance': 999999.9999995, 'total_balance': 1000000.0}, + expected_balance={ + "free_balance": 0.0000005, + "staked_balance": 999999.9999995, + "total_balance": 1000000.0, + }, ) # TODO: Ask nucleus the rate limit and wait epoch @@ -293,7 +305,11 @@ def test_root_commands(local_chain, wallet_setup): check_balance( exec_command=exec_command_alice, wallet=wallet_alice, - expected_balance={'free_balance': 1000000.0, 'staked_balance': 0.0, 'total_balance': 1000000.0}, + expected_balance={ + "free_balance": 1000000.0, + "staked_balance": 0.0, + "total_balance": 1000000.0, + }, ) print("✅ Passed Root commands") @@ -344,7 +360,7 @@ def check_balance(exec_command, wallet, expected_balance): "--network", "ws://127.0.0.1:9945", "--wallet-name", - wallet.name + wallet.name, ], ) From 6abfd0200ade2559154b1ac90c251937da5fb3f9 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 1 Oct 2024 00:09:18 +0200 Subject: [PATCH 5/5] tests --- tests/e2e_tests/test_root.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/e2e_tests/test_root.py b/tests/e2e_tests/test_root.py index e179329ec..26485c259 100644 --- a/tests/e2e_tests/test_root.py +++ b/tests/e2e_tests/test_root.py @@ -175,7 +175,7 @@ def test_root_commands(local_chain, wallet_setup): "--delegate-ss58key", wallet_bob.hotkey.ss58_address, "--amount", - f"10", + "10", "--no-prompt", ], ) @@ -216,8 +216,6 @@ def test_root_commands(local_chain, wallet_setup): wallet_alice.name, "--delegate-ss58key", wallet_bob.hotkey.ss58_address, - "--network", - "local", "--amount", f"10", "--no-prompt", @@ -294,8 +292,6 @@ def test_root_commands(local_chain, wallet_setup): wallet_alice.name, "--delegate-ss58key", wallet_bob.hotkey.ss58_address, - "--network", - "local", "--all", "--no-prompt", ],