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

Adds subnet registration extrinsic #2630

Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 28 additions & 0 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
64 changes: 63 additions & 1 deletion bittensor/core/extrinsics/asyncex/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
53 changes: 53 additions & 0 deletions bittensor/core/extrinsics/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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