diff --git a/README.md b/README.md index 35c18c1958..5825b95a7d 100644 --- a/README.md +++ b/README.md @@ -183,18 +183,18 @@ For example: ```bash btcli subnets --help -usage: btcli subnets [-h] {list,metagraph,lock_cost,create,register,recycle_register,hyperparameters} ... +usage: btcli subnets [-h] {list,metagraph,lock_cost,create,register,pow_register,hyperparameters} ... positional arguments: - {list,metagraph,lock_cost,create,register,recycle_register,hyperparameters} + {list,metagraph,lock_cost,create,register,pow_register,hyperparameters} Commands for managing and viewing subnetworks. - list List all subnets on the network + list List all subnets on the network. metagraph View a subnet metagraph information. - lock_cost Return the lock cost to register a subnet + lock_cost Return the lock cost to register a subnet. create Create a new bittensor subnetwork on this chain. register Register a wallet to a network. - recycle_register Register a wallet to a network. - hyperparameters View subnet hyperparameters + register Register a wallet to a network using PoW. + hyperparameters View subnet hyperparameters. options: -h, --help show this help message and exit diff --git a/bittensor/__init__.py b/bittensor/__init__.py index 600e5e8797..93f01c9d84 100644 --- a/bittensor/__init__.py +++ b/bittensor/__init__.py @@ -87,7 +87,7 @@ def debug(on: bool = True): # Wallet ss58 address length __ss58_address_length__ = 48 -__networks__ = ["local", "finney"] +__networks__ = ["local", "finney", "test"] __finney_entrypoint__ = "wss://entrypoint-finney.opentensor.ai:443" diff --git a/bittensor/cli.py b/bittensor/cli.py index 673fa31419..e4513f5ec0 100644 --- a/bittensor/cli.py +++ b/bittensor/cli.py @@ -53,8 +53,8 @@ "metagraph": MetagraphCommand, "lock_cost": SubnetLockCostCommand, "create": RegisterSubnetworkCommand, + "pow_register": PowRegisterCommand, "register": RegisterCommand, - "recycle_register": RecycleRegisterCommand, "hyperparameters": SubnetHyperparamsCommand, }, }, diff --git a/bittensor/commands/__init__.py b/bittensor/commands/__init__.py index b9e7721547..86e41f91a8 100644 --- a/bittensor/commands/__init__.py +++ b/bittensor/commands/__init__.py @@ -21,7 +21,7 @@ { "netuid": 1, "subtensor": {"network": "finney", "chain_endpoint": None, "_mock": False}, - "register": { + "pow_register": { "num_processes": None, "update_interval": 50000, "output_in_place": True, @@ -65,7 +65,7 @@ from .stake import StakeCommand, StakeShow from .unstake import UnStakeCommand from .overview import OverviewCommand -from .register import RegisterCommand, RecycleRegisterCommand, RunFaucetCommand +from .register import PowRegisterCommand, RegisterCommand, RunFaucetCommand from .delegates import ( NominateCommand, ListDelegatesCommand, diff --git a/bittensor/commands/register.py b/bittensor/commands/register.py index 2fc615fd26..32c7f97fc4 100644 --- a/bittensor/commands/register.py +++ b/bittensor/commands/register.py @@ -27,6 +27,88 @@ class RegisterCommand: + @staticmethod + def run(cli): + r"""Register neuron by recycling some TAO.""" + wallet = bittensor.wallet(config=cli.config) + subtensor = bittensor.subtensor(config=cli.config) + + # Verify subnet exists + if not subtensor.subnet_exists(netuid=cli.config.netuid): + bittensor.__console__.print( + f"[red]Subnet {cli.config.netuid} does not exist[/red]" + ) + sys.exit(1) + + # Check current recycle amount + current_recycle = subtensor.burn(netuid=cli.config.netuid) + balance = subtensor.get_balance(address=wallet.coldkeypub.ss58_address) + + # Check balance is sufficient + if balance < current_recycle: + bittensor.__console__.print( + f"[red]Insufficient balance {balance} to register neuron. Current recycle is {current_recycle} TAO[/red]" + ) + sys.exit(1) + + if not cli.config.no_prompt: + if ( + Confirm.ask( + f"Your balance is: [bold green]{balance}[/bold green]\nThe cost to register by recycle is [bold red]{current_recycle}[/bold red]\nDo you want to continue?", + default=False, + ) + == False + ): + sys.exit(1) + + subtensor.burned_register( + wallet=wallet, netuid=cli.config.netuid, prompt=not cli.config.no_prompt + ) + + @staticmethod + def add_args(parser: argparse.ArgumentParser): + register_parser = parser.add_parser( + "register", help="""Register a wallet to a network.""" + ) + register_parser.add_argument( + "--netuid", + type=int, + help="netuid for subnet to serve this neuron on", + default=argparse.SUPPRESS, + ) + + bittensor.wallet.add_args(register_parser) + bittensor.subtensor.add_args(register_parser) + + @staticmethod + def check_config(config: "bittensor.config"): + if ( + not config.is_set("subtensor.network") + and not config.is_set("subtensor.chain_endpoint") + and not config.no_prompt + ): + config.subtensor.network = Prompt.ask( + "Enter subtensor network", + choices=bittensor.__networks__, + default=defaults.subtensor.network, + ) + _, endpoint = bittensor.subtensor.determine_chain_endpoint_and_network( + config.subtensor.network + ) + config.subtensor.chain_endpoint = endpoint + + check_netuid_set(config, subtensor=bittensor.subtensor(config=config)) + + if not config.is_set("wallet.name") and not config.no_prompt: + wallet_name = Prompt.ask("Enter wallet name", default=defaults.wallet.name) + config.wallet.name = str(wallet_name) + + if not config.is_set("wallet.hotkey") and not config.no_prompt: + hotkey = Prompt.ask("Enter hotkey name", default=defaults.wallet.hotkey) + config.wallet.hotkey = str(hotkey) + + +class PowRegisterCommand: @staticmethod def run(cli): r"""Register neuron.""" @@ -44,23 +126,25 @@ def run(cli): wallet=wallet, netuid=cli.config.netuid, prompt=not cli.config.no_prompt, - TPB=cli.config.register.cuda.get("TPB", None), - update_interval=cli.config.register.get("update_interval", None), - num_processes=cli.config.register.get("num_processes", None), - cuda=cli.config.register.cuda.get( - "use_cuda", defaults.register.cuda.use_cuda + TPB=cli.config.pow_register.cuda.get("TPB", None), + update_interval=cli.config.pow_register.get("update_interval", None), + num_processes=cli.config.pow_register.get("num_processes", None), + cuda=cli.config.pow_register.cuda.get( + "use_cuda", defaults.pow_register.cuda.use_cuda ), - dev_id=cli.config.register.cuda.get("dev_id", None), - output_in_place=cli.config.register.get( - "output_in_place", defaults.register.output_in_place + dev_id=cli.config.pow_register.cuda.get("dev_id", None), + output_in_place=cli.config.pow_register.get( + "output_in_place", defaults.pow_register.output_in_place + ), + log_verbose=cli.config.pow_register.get( + "verbose", defaults.pow_register.verbose ), - log_verbose=cli.config.register.get("verbose", defaults.register.verbose), ) @staticmethod def add_args(parser: argparse.ArgumentParser): register_parser = parser.add_parser( - "register", help="""Register a wallet to a network.""" + "pow_register", help="""Register a wallet to a network using PoW.""" ) register_parser.add_argument( "--netuid", @@ -69,75 +153,75 @@ def add_args(parser: argparse.ArgumentParser): default=argparse.SUPPRESS, ) register_parser.add_argument( - "--register.num_processes", + "--pow_register.num_processes", "-n", - dest="register.num_processes", + dest="pow_register.num_processes", help="Number of processors to use for POW registration", type=int, - default=defaults.register.num_processes, + default=defaults.pow_register.num_processes, ) register_parser.add_argument( - "--register.update_interval", - "--register.cuda.update_interval", + "--pow_register.update_interval", + "--pow_register.cuda.update_interval", "--cuda.update_interval", "-u", help="The number of nonces to process before checking for next block during registration", type=int, - default=defaults.register.update_interval, + default=defaults.pow_register.update_interval, ) register_parser.add_argument( - "--register.no_output_in_place", + "--pow_register.no_output_in_place", "--no_output_in_place", - dest="register.output_in_place", + dest="pow_register.output_in_place", help="Whether to not ouput the registration statistics in-place. Set flag to disable output in-place.", action="store_false", required=False, - default=defaults.register.output_in_place, + default=defaults.pow_register.output_in_place, ) register_parser.add_argument( - "--register.verbose", + "--pow_register.verbose", help="Whether to ouput the registration statistics verbosely.", action="store_true", required=False, - default=defaults.register.verbose, + default=defaults.pow_register.verbose, ) ## Registration args for CUDA registration. register_parser.add_argument( - "--register.cuda.use_cuda", + "--pow_register.cuda.use_cuda", "--cuda", "--cuda.use_cuda", - dest="register.cuda.use_cuda", - default=defaults.register.cuda.use_cuda, + dest="pow_register.cuda.use_cuda", + default=defaults.pow_register.cuda.use_cuda, help="""Set flag to use CUDA to register.""", action="store_true", required=False, ) register_parser.add_argument( - "--register.cuda.no_cuda", + "--pow_register.cuda.no_cuda", "--no_cuda", "--cuda.no_cuda", - dest="register.cuda.use_cuda", - default=not defaults.register.cuda.use_cuda, + dest="pow_register.cuda.use_cuda", + default=not defaults.pow_register.cuda.use_cuda, help="""Set flag to not use CUDA for registration""", action="store_false", required=False, ) register_parser.add_argument( - "--register.cuda.dev_id", + "--pow_register.cuda.dev_id", "--cuda.dev_id", type=int, nargs="+", - default=defaults.register.cuda.dev_id, + default=defaults.pow_register.cuda.dev_id, help="""Set the CUDA device id(s). Goes by the order of speed. (i.e. 0 is the fastest).""", required=False, ) register_parser.add_argument( - "--register.cuda.TPB", + "--pow_register.cuda.TPB", "--cuda.TPB", type=int, - default=defaults.register.cuda.TPB, + default=defaults.pow_register.cuda.TPB, help="""Set the number of Threads Per Block for CUDA.""", required=False, ) @@ -145,76 +229,6 @@ def add_args(parser: argparse.ArgumentParser): bittensor.wallet.add_args(register_parser) bittensor.subtensor.add_args(register_parser) - @staticmethod - def check_config(config: "bittensor.config"): - check_netuid_set(config, subtensor=bittensor.subtensor(config=config)) - - if not config.is_set("wallet.name") and not config.no_prompt: - wallet_name = Prompt.ask("Enter wallet name", default=defaults.wallet.name) - config.wallet.name = str(wallet_name) - - if not config.is_set("wallet.hotkey") and not config.no_prompt: - hotkey = Prompt.ask("Enter hotkey name", default=defaults.wallet.hotkey) - config.wallet.hotkey = str(hotkey) - - if not config.no_prompt: - check_for_cuda_reg_config(config) - - -class RecycleRegisterCommand: - @staticmethod - def run(cli): - r"""Register neuron by recycling some TAO.""" - wallet = bittensor.wallet(config=cli.config) - subtensor = bittensor.subtensor(config=cli.config) - - # Verify subnet exists - if not subtensor.subnet_exists(netuid=cli.config.netuid): - bittensor.__console__.print( - f"[red]Subnet {cli.config.netuid} does not exist[/red]" - ) - sys.exit(1) - - # Check current recycle amount - current_recycle = subtensor.burn(netuid=cli.config.netuid) - balance = subtensor.get_balance(address=wallet.coldkeypub.ss58_address) - - # Check balance is sufficient - if balance < current_recycle: - bittensor.__console__.print( - f"[red]Insufficient balance {balance} to register neuron. Current recycle is {current_recycle} TAO[/red]" - ) - sys.exit(1) - - if not cli.config.no_prompt: - if ( - Confirm.ask( - f"Your balance is: [bold green]{balance}[/bold green]\nThe cost to register by recycle is [bold red]{current_recycle}[/bold red]\nDo you want to continue?", - default=False, - ) - == False - ): - sys.exit(1) - - subtensor.burned_register( - wallet=wallet, netuid=cli.config.netuid, prompt=not cli.config.no_prompt - ) - - @staticmethod - def add_args(parser: argparse.ArgumentParser): - recycle_register_parser = parser.add_parser( - "recycle_register", help="""Register a wallet to a network.""" - ) - recycle_register_parser.add_argument( - "--netuid", - type=int, - help="netuid for subnet to serve this neuron on", - default=argparse.SUPPRESS, - ) - - bittensor.wallet.add_args(recycle_register_parser) - bittensor.subtensor.add_args(recycle_register_parser) - @staticmethod def check_config(config: "bittensor.config"): if ( @@ -227,6 +241,10 @@ def check_config(config: "bittensor.config"): choices=bittensor.__networks__, default=defaults.subtensor.network, ) + _, endpoint = bittensor.subtensor.determine_chain_endpoint_and_network( + config.subtensor.network + ) + config.subtensor.chain_endpoint = endpoint check_netuid_set(config, subtensor=bittensor.subtensor(config=config)) @@ -238,6 +256,9 @@ def check_config(config: "bittensor.config"): hotkey = Prompt.ask("Enter hotkey name", default=defaults.wallet.hotkey) config.wallet.hotkey = str(hotkey) + if not config.no_prompt: + check_for_cuda_reg_config(config) + class RunFaucetCommand: @staticmethod @@ -248,17 +269,19 @@ def run(cli): subtensor.run_faucet( wallet=wallet, prompt=not cli.config.no_prompt, - TPB=cli.config.register.cuda.get("TPB", None), - update_interval=cli.config.register.get("update_interval", None), - num_processes=cli.config.register.get("num_processes", None), - cuda=cli.config.register.cuda.get( - "use_cuda", defaults.register.cuda.use_cuda + TPB=cli.config.pow_register.cuda.get("TPB", None), + update_interval=cli.config.pow_register.get("update_interval", None), + num_processes=cli.config.pow_register.get("num_processes", None), + cuda=cli.config.pow_register.cuda.get( + "use_cuda", defaults.pow_register.cuda.use_cuda ), - dev_id=cli.config.register.cuda.get("dev_id", None), - output_in_place=cli.config.register.get( - "output_in_place", defaults.register.output_in_place + dev_id=cli.config.pow_register.cuda.get("dev_id", None), + output_in_place=cli.config.pow_register.get( + "output_in_place", defaults.pow_register.output_in_place + ), + log_verbose=cli.config.pow_register.get( + "verbose", defaults.pow_register.verbose ), - log_verbose=cli.config.register.get("verbose", defaults.register.verbose), ) @staticmethod @@ -267,74 +290,74 @@ def add_args(parser: argparse.ArgumentParser): "faucet", help="""Perform PoW to receieve test TAO in your wallet.""" ) run_faucet_parser.add_argument( - "--register.num_processes", + "--faucet.num_processes", "-n", - dest="register.num_processes", + dest="pow_register.num_processes", help="Number of processors to use for POW registration", type=int, - default=defaults.register.num_processes, + default=defaults.pow_register.num_processes, ) run_faucet_parser.add_argument( - "--register.update_interval", - "--register.cuda.update_interval", + "--faucet.update_interval", + "--faucet.cuda.update_interval", "--cuda.update_interval", "-u", help="The number of nonces to process before checking for next block during registration", type=int, - default=defaults.register.update_interval, + default=defaults.pow_register.update_interval, ) run_faucet_parser.add_argument( - "--register.no_output_in_place", + "--faucet.no_output_in_place", "--no_output_in_place", - dest="register.output_in_place", + dest="pow_register.output_in_place", help="Whether to not ouput the registration statistics in-place. Set flag to disable output in-place.", action="store_false", required=False, - default=defaults.register.output_in_place, + default=defaults.pow_register.output_in_place, ) run_faucet_parser.add_argument( - "--register.verbose", + "--faucet.verbose", help="Whether to ouput the registration statistics verbosely.", action="store_true", required=False, - default=defaults.register.verbose, + default=defaults.pow_register.verbose, ) ## Registration args for CUDA registration. run_faucet_parser.add_argument( - "--register.cuda.use_cuda", + "--faucet.cuda.use_cuda", "--cuda", "--cuda.use_cuda", - dest="register.cuda.use_cuda", - default=defaults.register.cuda.use_cuda, - help="""Set flag to use CUDA to register.""", + dest="pow_register.cuda.use_cuda", + default=defaults.pow_register.cuda.use_cuda, + help="""Set flag to use CUDA to pow_register.""", action="store_true", required=False, ) run_faucet_parser.add_argument( - "--register.cuda.no_cuda", + "--faucet.cuda.no_cuda", "--no_cuda", "--cuda.no_cuda", - dest="register.cuda.use_cuda", - default=not defaults.register.cuda.use_cuda, + dest="pow_register.cuda.use_cuda", + default=not defaults.pow_register.cuda.use_cuda, help="""Set flag to not use CUDA for registration""", action="store_false", required=False, ) run_faucet_parser.add_argument( - "--register.cuda.dev_id", + "--faucet.cuda.dev_id", "--cuda.dev_id", type=int, nargs="+", - default=defaults.register.cuda.dev_id, + default=defaults.pow_register.cuda.dev_id, help="""Set the CUDA device id(s). Goes by the order of speed. (i.e. 0 is the fastest).""", required=False, ) run_faucet_parser.add_argument( - "--register.cuda.TPB", + "--faucet.cuda.TPB", "--cuda.TPB", type=int, - default=defaults.register.cuda.TPB, + default=defaults.pow_register.cuda.TPB, help="""Set the number of Threads Per Block for CUDA.""", required=False, ) diff --git a/bittensor/commands/utils.py b/bittensor/commands/utils.py index b54053622a..732d43d68a 100644 --- a/bittensor/commands/utils.py +++ b/bittensor/commands/utils.py @@ -80,15 +80,15 @@ def check_for_cuda_reg_config(config: "bittensor.config") -> None: """Checks, when CUDA is available, if the user would like to register with their CUDA device.""" if torch.cuda.is_available(): if not config.no_prompt: - if config.register.cuda.get("use_cuda") == None: # flag not set + if config.pow_register.cuda.get("use_cuda") == None: # flag not set # Ask about cuda registration only if a CUDA device is available. cuda = Confirm.ask("Detected CUDA device, use CUDA for registration?\n") - config.register.cuda.use_cuda = cuda + config.pow_register.cuda.use_cuda = cuda # Only ask about which CUDA device if the user has more than one CUDA device. if ( - config.register.cuda.use_cuda - and config.register.cuda.get("dev_id") is None + config.pow_register.cuda.use_cuda + and config.pow_register.cuda.get("dev_id") is None ): devices: List[str] = [str(x) for x in range(torch.cuda.device_count())] device_names: List[str] = [ @@ -122,11 +122,11 @@ def check_for_cuda_reg_config(config: "bittensor.config") -> None: ) ) sys.exit(1) - config.register.cuda.dev_id = dev_id + config.pow_register.cuda.dev_id = dev_id else: # flag was not set, use default value. - if config.register.cuda.get("use_cuda") is None: - config.register.cuda.use_cuda = defaults.register.cuda.use_cuda + if config.pow_register.cuda.get("use_cuda") is None: + config.pow_register.cuda.use_cuda = defaults.pow_register.cuda.use_cuda def get_hotkey_wallets_for_wallet(wallet) -> List["bittensor.wallet"]: diff --git a/bittensor/utils/__init__.py b/bittensor/utils/__init__.py index 6324461965..dea833d72b 100644 --- a/bittensor/utils/__init__.py +++ b/bittensor/utils/__init__.py @@ -26,7 +26,7 @@ from substrateinterface.utils import ss58 as ss58 from .wallet_utils import * -from .registration import create_pow as create_pow, __reregister_wallet as reregister +from .registration import create_pow as create_pow RAOPERTAO = 1e9 U16_MAX = 65535 diff --git a/bittensor/utils/registration.py b/bittensor/utils/registration.py index d86f7d85fc..29a1d5d7f3 100644 --- a/bittensor/utils/registration.py +++ b/bittensor/utils/registration.py @@ -1087,51 +1087,3 @@ def create_pow( ) return solution - - -def __reregister_wallet( - netuid: int, - wallet: "bittensor.wallet", - subtensor: "bittensor.subtensor", - reregister: bool = False, - prompt: bool = False, - **registration_args: Any, -) -> Optional["bittensor.wallet"]: - """Re-register this a wallet on the chain, or exits. - Exits if the wallet is not registered on the chain AND - reregister is set to False. - Args: - netuid (int): - The network uid of the subnet to register on. - wallet( 'bittensor.wallet' ): - Bittensor wallet to re-register - reregister (bool, default=False): - If true, re-registers the wallet on the chain. - Exits if False and the wallet is not registered on the chain. - prompt (bool): - If true, the call waits for confirmation from the user before proceeding. - **registration_args (Any): - The registration arguments to pass to the subtensor register function. - Return: - wallet (bittensor.wallet): - The wallet - - Raises: - SytemExit(0): - If the wallet is not registered on the chain AND - the config.subtensor.reregister flag is set to False. - """ - wallet.hotkey - - if not subtensor.is_hotkey_registered_on_subnet( - hotkey_ss58=wallet.hotkey.ss58_address, netuid=netuid - ): - # Check if the wallet should reregister - if not reregister: - sys.exit(0) - - subtensor.register( - wallet=wallet, netuid=netuid, prompt=prompt, **registration_args - ) - - return wallet diff --git a/tests/integration_tests/test_cli.py b/tests/integration_tests/test_cli.py index ffc973d78c..4dfb4caba9 100644 --- a/tests/integration_tests/test_cli.py +++ b/tests/integration_tests/test_cli.py @@ -1913,31 +1913,6 @@ def test_register(self, _): config = self.config config.command = "subnets" config.subcommand = "register" - config.register.num_processes = 1 - config.register.update_interval = 50_000 - config.no_prompt = True - - mock_wallet = generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - with patch("bittensor.wallet", return_value=mock_wallet) as mock_create_wallet: - with patch( - "bittensor.extrinsics.registration.POWSolution.is_stale", - side_effect=MockException, - ) as mock_is_stale: - with pytest.raises(MockException): - cli = bittensor.cli(config) - cli.run() - mock_create_wallet.assert_called_once() - - self.assertEqual(mock_is_stale.call_count, 1) - - def test_recycle_register(self, _): - config = self.config - config.command = "subnets" - config.subcommand = "recycle_register" config.no_prompt = True mock_wallet = generate_wallet(hotkey=_get_mock_keypair(100, self.id())) @@ -1961,6 +1936,31 @@ def test_recycle_register(self, _): self.assertTrue(registered) + def test_pow_register(self, _): + config = self.config + config.command = "subnets" + config.subcommand = "pow_register" + config.pow_register.num_processes = 1 + config.pow_register.update_interval = 50_000 + config.no_prompt = True + + mock_wallet = generate_wallet(hotkey=_get_mock_keypair(100, self.id())) + + class MockException(Exception): + pass + + with patch("bittensor.wallet", return_value=mock_wallet) as mock_create_wallet: + with patch( + "bittensor.extrinsics.registration.POWSolution.is_stale", + side_effect=MockException, + ) as mock_is_stale: + with pytest.raises(MockException): + cli = bittensor.cli(config) + cli.run() + mock_create_wallet.assert_called_once() + + self.assertEqual(mock_is_stale.call_count, 1) + def test_stake(self, _): amount_to_stake: Balance = Balance.from_tao(0.5) config = self.config diff --git a/tests/integration_tests/test_cli_no_network.py b/tests/integration_tests/test_cli_no_network.py index 5aa8571211..8fc551cde3 100644 --- a/tests/integration_tests/test_cli_no_network.py +++ b/tests/integration_tests/test_cli_no_network.py @@ -338,7 +338,7 @@ def test_btcli_help(self, _, __): def test_register_cuda_use_cuda_flag(self, _, __, patched_sub): base_args = [ "subnets", - "register", + "pow_register", "--wallet.path", "tmp/walletpath", "--wallet.name", @@ -358,24 +358,24 @@ def test_register_cuda_use_cuda_flag(self, _, __, patched_sub): # Should be able to set true without argument args = base_args + [ - "--register.cuda.use_cuda", # should be True without any arugment + "--pow_register.cuda.use_cuda", # should be True without any arugment ] with pytest.raises(MockException): cli = bittensor.cli(args=args) cli.run() - self.assertEqual(cli.config.register.cuda.get("use_cuda"), True) + self.assertEqual(cli.config.pow_register.cuda.get("use_cuda"), True) # Should be able to set to false with no argument args = base_args + [ - "--register.cuda.no_cuda", + "--pow_register.cuda.no_cuda", ] with pytest.raises(MockException): cli = bittensor.cli(args=args) cli.run() - self.assertEqual(cli.config.register.cuda.get("use_cuda"), False) + self.assertEqual(cli.config.pow_register.cuda.get("use_cuda"), False) def return_mock_sub_2(*args, **kwargs): diff --git a/tests/integration_tests/test_subtensor_integration.py b/tests/integration_tests/test_subtensor_integration.py index 8448f9098b..e0c633e3b2 100644 --- a/tests/integration_tests/test_subtensor_integration.py +++ b/tests/integration_tests/test_subtensor_integration.py @@ -586,12 +586,11 @@ class ExitEarly(Exception): msg="only tries to submit once, then exits", ) + def test_defaults_to_finney(self): + sub = bittensor.subtensor() + assert sub.network == "finney" + assert sub.chain_endpoint == bittensor.__finney_entrypoint__ -# # This test was flaking, please check to_defaults before reactiving the test -# def _test_defaults_to_finney(): -# sub = bittensor.subtensor() -# assert sub.network == 'finney' -# assert sub.chain_endpoint == bittensor.__finney_entrypoint__ if __name__ == "__main__": unittest.main() diff --git a/tests/unit_tests/utils/test_utils.py b/tests/unit_tests/utils/test_utils.py index 307891a13c..cad54216c4 100644 --- a/tests/unit_tests/utils/test_utils.py +++ b/tests/unit_tests/utils/test_utils.py @@ -762,222 +762,5 @@ def test_get_explorer_url_for_network_by_network_and_block_hash( ) -class TestWalletReregister(unittest.TestCase): - _mock_subtensor: MockSubtensor - - def setUp(self): - self.subtensor = MockSubtensor() # own instance per test - - @classmethod - def setUpClass(cls) -> None: - # Keeps the same mock network for all tests. This stops the network from being re-setup for each test. - cls._mock_subtensor = MockSubtensor() - - cls._do_setup_subnet() - - @classmethod - def _do_setup_subnet(cls): - # reset the mock subtensor - cls._mock_subtensor.reset() - # Setup the mock subnet 3 - cls._mock_subtensor.create_subnet(netuid=3) - - def test_wallet_reregister_reregister_false(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - # Determine the correct string for patch based on Python version - if sys.version_info >= (3, 11): - patch_string = "bittensor.subtensor.subtensor.register" - else: - patch_string = "bittensor.subtensor.register" - - with patch(patch_string, side_effect=MockException) as mock_register: - with pytest.raises(SystemExit): # should exit because it's not registered - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - reregister=False, - ) - - mock_register.assert_not_called() # should not call register - - def test_wallet_reregister_reregister_false_and_registered_already(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - self._mock_subtensor.force_register_neuron( - netuid=3, - hotkey=mock_wallet.hotkey.ss58_address, - coldkey=mock_wallet.coldkeypub.ss58_address, - ) - self.assertTrue( - self._mock_subtensor.is_hotkey_registered_on_subnet( - netuid=3, - hotkey_ss58=mock_wallet.hotkey.ss58_address, - ) - ) - - # Determine the correct string for patch based on Python version - if sys.version_info >= (3, 11): - patch_string = "bittensor.subtensor.subtensor.register" - else: - patch_string = "bittensor.subtensor.register" - - with patch(patch_string, side_effect=MockException) as mock_register: - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - reregister=False, - ) # Should not exit because it's registered - - mock_register.assert_not_called() # should not call register - - def test_wallet_reregister_reregister_true_and_registered_already(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - self._mock_subtensor.force_register_neuron( - netuid=3, - hotkey=mock_wallet.hotkey.ss58_address, - coldkey=mock_wallet.coldkeypub.ss58_address, - ) - self.assertTrue( - self._mock_subtensor.is_hotkey_registered_on_subnet( - netuid=3, - hotkey_ss58=mock_wallet.hotkey.ss58_address, - ) - ) - - # Determine the correct string for patch based on Python version - if sys.version_info >= (3, 11): - patch_string = "bittensor.subtensor.subtensor.register" - else: - patch_string = "bittensor.subtensor.register" - - with patch(patch_string, side_effect=MockException) as mock_register: - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - reregister=True, - ) # Should not exit because it's registered - - mock_register.assert_not_called() # should not call register - - def test_wallet_reregister_no_params(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - # Determine the correct string for patch based on Python version - if sys.version_info >= (3, 11): - patch_string = "bittensor.subtensor.subtensor.register" - else: - patch_string = "bittensor.subtensor.register" - - with patch(patch_string, side_effect=MockException) as mock_register: - # Should be able to set without argument - with pytest.raises(MockException): - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - reregister=True, - # didn't pass any register params - ) - - mock_register.assert_called_once() # should call register once - - def test_wallet_reregister_use_cuda_flag_true(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - with patch("torch.cuda.is_available", return_value=True) as mock_cuda_available: - with patch( - "bittensor.extrinsics.registration.create_pow", - side_effect=MockException, - ) as mock_create_pow: - # Should be able to set without argument - with pytest.raises(MockException): - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - dev_id=0, - cuda=True, - reregister=True, - ) - - call_args = mock_create_pow.call_args - _, kwargs = call_args - - mock_create_pow.assert_called_once() - self.assertIn("cuda", kwargs) - self.assertEqual(kwargs["cuda"], True) - - def test_wallet_reregister_use_cuda_flag_false(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - with patch( - "bittensor.extrinsics.registration.create_pow", side_effect=MockException - ) as mock_create_pow: - # Should be able to set without argument - with pytest.raises(MockException): - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - dev_id=0, - cuda=False, - reregister=True, - ) - - call_args = mock_create_pow.call_args - _, kwargs = call_args - - mock_create_pow.assert_called_once() - self.assertEqual(kwargs["cuda"], False) - - def test_wallet_reregister_cuda_arg_not_specified_should_be_false(self): - mock_wallet = _generate_wallet(hotkey=_get_mock_keypair(100, self.id())) - - class MockException(Exception): - pass - - with patch( - "bittensor.extrinsics.registration.create_pow", side_effect=MockException - ) as mock_create_pow: - # Should be able to set without argument - with pytest.raises(MockException): - bittensor.utils.reregister( - wallet=mock_wallet, - subtensor=self._mock_subtensor, - netuid=3, - dev_id=0, - reregister=True, - ) - - call_args = mock_create_pow.call_args - _, kwargs = call_args - - mock_create_pow.assert_called_once() - self.assertEqual(kwargs["cuda"], False) # should be False by default - - if __name__ == "__main__": unittest.main()