From f646102dfd72005fb0f1b3e0d0d3a6a49b0851b7 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 5 Feb 2025 11:34:53 -0800 Subject: [PATCH 1/5] Adds registration subnet extrinsic --- bittensor/core/async_subtensor.py | 28 ++++++++ .../core/extrinsics/asyncex/registration.py | 64 ++++++++++++++++++- bittensor/core/extrinsics/registration.py | 53 +++++++++++++++ bittensor/core/subtensor.py | 29 ++++++++- 4 files changed, 172 insertions(+), 2 deletions(-) diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index 741f2d2857..5882e1e272 100644 --- a/bittensor/core/async_subtensor.py +++ b/bittensor/core/async_subtensor.py @@ -32,6 +32,7 @@ from bittensor.core.extrinsics.asyncex.registration import ( burned_register_extrinsic, register_extrinsic, + register_subnet_extrinsic, ) from bittensor.core.extrinsics.asyncex.move_stake import ( transfer_stake_extrinsic, @@ -2957,6 +2958,33 @@ async def register( log_verbose=log_verbose, ) + async def register_subnet( + self: "AsyncSubtensor", + wallet: "Wallet", + wait_for_inclusion: bool = False, + wait_for_finalization: bool = True, + ) -> bool: + """ + Registers a new subnetwork on the Bittensor network. + + Args: + wallet (bittensor_wallet.Wallet): The wallet to be used for subnet registration. + wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true, or returns + false if the extrinsic fails to enter the block within the timeout. Default is False. + wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning + true, or returns false if the extrinsic fails to be finalized within the timeout. Default is True. + + Returns: + bool: True if the subnet registration was successful, False otherwise. + + """ + return await register_subnet_extrinsic( + subtensor=self, + wallet=wallet, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + ) + async def reveal_weights( self, wallet: "Wallet", diff --git a/bittensor/core/extrinsics/asyncex/registration.py b/bittensor/core/extrinsics/asyncex/registration.py index d4b5c6a271..599f13ab5c 100644 --- a/bittensor/core/extrinsics/asyncex/registration.py +++ b/bittensor/core/extrinsics/asyncex/registration.py @@ -10,7 +10,7 @@ import asyncio from typing import Optional, Union, TYPE_CHECKING -from bittensor.utils import unlock_key +from bittensor.utils import unlock_key, format_error_message from bittensor.utils.btlogging import logging from bittensor.utils.registration import log_no_torch_error, create_pow_async, torch @@ -379,3 +379,65 @@ async def register_extrinsic( # Failed to register after max attempts. logging.error("[red]No more attempts.[/red]") return False + + +async def register_subnet_extrinsic( + subtensor: "AsyncSubtensor", + wallet: "Wallet", + wait_for_inclusion: bool = False, + wait_for_finalization: bool = True, +) -> bool: + """ + Registers a new subnetwork on the Bittensor blockchain asynchronously. + + Args: + subtensor (AsyncSubtensor): The async subtensor interface to send the extrinsic. + wallet (Wallet): The wallet to be used for subnet registration. + wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true. + wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning true. + + Returns: + bool: True if the subnet registration was successful, False otherwise. + """ + balance = await subtensor.get_balance(wallet.coldkeypub.ss58_address) + burn_cost = await subtensor.get_subnet_burn_cost() + + if burn_cost > balance: + logging.error( + f"Insufficient balance {balance} to register subnet. Current burn cost is {burn_cost} TAO" + ) + return False + + call = await subtensor.substrate.compose_call( + call_module="SubtensorModule", + call_function="register_network", + call_params={ + "hotkey": wallet.hotkey.ss58_address, + "mechid": 1, + }, + ) + + extrinsic = await subtensor.substrate.create_signed_extrinsic( + call=call, keypair=wallet.coldkey + ) + + response = await subtensor.substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + ) + + if not wait_for_finalization and not wait_for_inclusion: + return True + + await response.process_events() + if not await response.is_success: + logging.error( + f"Failed to register subnet: {format_error_message(await response.error_message, subtensor.substrate)}" + ) + return False + + logging.success( + ":white_heavy_check_mark: [green]Successfully registered subnet[/green]" + ) + return True diff --git a/bittensor/core/extrinsics/registration.py b/bittensor/core/extrinsics/registration.py index d5211ee199..501b78d673 100644 --- a/bittensor/core/extrinsics/registration.py +++ b/bittensor/core/extrinsics/registration.py @@ -191,6 +191,59 @@ def _do_pow_register( ) +def register_subnet_extrinsic( + subtensor: "Subtensor", + wallet: "Wallet", + wait_for_inclusion: bool = False, + wait_for_finalization: bool = True, +) -> bool: + """ + Registers a new subnetwork on the Bittensor blockchain. + + Args: + subtensor (SubtensorInterface): The subtensor interface to send the extrinsic. + wallet (Wallet): The wallet to be used for subnet registration. + wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true. + wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning true. + + Returns: + bool: True if the subnet registration was successful, False otherwise. + """ + balance = subtensor.get_balance(wallet.coldkeypub.ss58_address) + burn_cost = subtensor.get_subnet_burn_cost() + + if burn_cost > balance: + logging.error( + f"Insufficient balance {balance} to register subnet. Current burn cost is {burn_cost} TAO" + ) + return False + + call = subtensor.substrate.compose_call( + call_module="SubtensorModule", + call_function="register_network", + call_params={ + "hotkey": wallet.hotkey.ss58_address, + "mechid": 1, + }, + ) + + success, message = subtensor.sign_and_send_extrinsic( + call=call, + wallet=wallet, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + ) + + if success: + logging.success( + ":white_heavy_check_mark: [green]Successfully registered subnet[/green]" + ) + return True + else: + logging.error(f"Failed to register subnet: {message}") + return False + + def register_extrinsic( subtensor: "Subtensor", wallet: "Wallet", diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index f5f2f42331..56c5f62ca1 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -38,6 +38,7 @@ from bittensor.core.extrinsics.registration import ( burned_register_extrinsic, register_extrinsic, + register_subnet_extrinsic, ) from bittensor.core.extrinsics.root import ( root_register_extrinsic, @@ -1281,7 +1282,7 @@ def get_subnet_burn_cost(self, block: Optional[int] = None) -> Optional[int]: block=block, ) - return lock_cost + return Balance.from_rao(lock_cost) def get_subnet_hyperparameters( self, netuid: int, block: Optional[int] = None @@ -2323,6 +2324,32 @@ def register( log_verbose=log_verbose, ) + def register_subnet( + self, + wallet: "Wallet", + wait_for_inclusion: bool = False, + wait_for_finalization: bool = True, + ) -> bool: + """ + Registers a new subnetwork on the Bittensor network. + + Args: + wallet (bittensor_wallet.Wallet): The wallet to be used for subnet registration. + wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true, or returns + false if the extrinsic fails to enter the block within the timeout. Default is False. + wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning + true, or returns false if the extrinsic fails to be finalized within the timeout. Default is True. + + Returns: + bool: True if the subnet registration was successful, False otherwise. + """ + return register_subnet_extrinsic( + subtensor=self, + wallet=wallet, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + ) + def reveal_weights( self, wallet: "Wallet", From b9d43dc7fcb248e0e5c68f4557cd1c7efb19a9ee Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 5 Feb 2025 11:36:47 -0800 Subject: [PATCH 2/5] Reverts lock-cost changes --- bittensor/core/subtensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 56c5f62ca1..f84604b9c7 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1282,7 +1282,7 @@ def get_subnet_burn_cost(self, block: Optional[int] = None) -> Optional[int]: block=block, ) - return Balance.from_rao(lock_cost) + return lock_cost def get_subnet_hyperparameters( self, netuid: int, block: Optional[int] = None From e2d0df74225b61021b6ff286cc740888b4ee80ec Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 5 Feb 2025 11:39:22 -0800 Subject: [PATCH 3/5] Updates docstrings --- bittensor/core/extrinsics/registration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/core/extrinsics/registration.py b/bittensor/core/extrinsics/registration.py index 501b78d673..8505eab15b 100644 --- a/bittensor/core/extrinsics/registration.py +++ b/bittensor/core/extrinsics/registration.py @@ -201,7 +201,7 @@ def register_subnet_extrinsic( Registers a new subnetwork on the Bittensor blockchain. Args: - subtensor (SubtensorInterface): The subtensor interface to send the extrinsic. + subtensor (Subtensor): The subtensor interface to send the extrinsic. wallet (Wallet): The wallet to be used for subnet registration. wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true. wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning true. From bfd1f0060d86d367313738b0ed3a93fafdf35ce2 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 5 Feb 2025 11:42:08 -0800 Subject: [PATCH 4/5] Bumps btwallet --- requirements/prod.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/prod.txt b/requirements/prod.txt index 39c1bb4423..84ab981b3c 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -22,5 +22,5 @@ scalecodec==1.2.11 uvicorn websockets>=14.1 bittensor-commit-reveal>=0.2.0 -bittensor-wallet>=3.0.0 +bittensor-wallet>=3.0.1 async-substrate-interface==1.0.0rc10 From e09178ded50145b36c54c908db3cb097810d3168 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 5 Feb 2025 12:03:59 -0800 Subject: [PATCH 5/5] Updates bittenso-wallet version --- requirements/prod.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/prod.txt b/requirements/prod.txt index 84ab981b3c..39c1bb4423 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -22,5 +22,5 @@ scalecodec==1.2.11 uvicorn websockets>=14.1 bittensor-commit-reveal>=0.2.0 -bittensor-wallet>=3.0.1 +bittensor-wallet>=3.0.0 async-substrate-interface==1.0.0rc10