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..8505eab15b 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 (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. + + 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..f84604b9c7 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, @@ -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",