Skip to content

Commit 8ef40ec

Browse files
authored
Merge pull request #2630 from opentensor/adds-extrinsic/register-subnet
Adds subnet registration extrinsic
2 parents 93ba99b + e09178d commit 8ef40ec

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed

bittensor/core/async_subtensor.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from bittensor.core.extrinsics.asyncex.registration import (
3333
burned_register_extrinsic,
3434
register_extrinsic,
35+
register_subnet_extrinsic,
3536
)
3637
from bittensor.core.extrinsics.asyncex.move_stake import (
3738
transfer_stake_extrinsic,
@@ -2959,6 +2960,33 @@ async def register(
29592960
log_verbose=log_verbose,
29602961
)
29612962

2963+
async def register_subnet(
2964+
self: "AsyncSubtensor",
2965+
wallet: "Wallet",
2966+
wait_for_inclusion: bool = False,
2967+
wait_for_finalization: bool = True,
2968+
) -> bool:
2969+
"""
2970+
Registers a new subnetwork on the Bittensor network.
2971+
2972+
Args:
2973+
wallet (bittensor_wallet.Wallet): The wallet to be used for subnet registration.
2974+
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true, or returns
2975+
false if the extrinsic fails to enter the block within the timeout. Default is False.
2976+
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
2977+
true, or returns false if the extrinsic fails to be finalized within the timeout. Default is True.
2978+
2979+
Returns:
2980+
bool: True if the subnet registration was successful, False otherwise.
2981+
2982+
"""
2983+
return await register_subnet_extrinsic(
2984+
subtensor=self,
2985+
wallet=wallet,
2986+
wait_for_inclusion=wait_for_inclusion,
2987+
wait_for_finalization=wait_for_finalization,
2988+
)
2989+
29622990
async def reveal_weights(
29632991
self,
29642992
wallet: "Wallet",

bittensor/core/extrinsics/asyncex/registration.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import asyncio
1111
from typing import Optional, Union, TYPE_CHECKING
1212

13-
from bittensor.utils import unlock_key
13+
from bittensor.utils import unlock_key, format_error_message
1414
from bittensor.utils.btlogging import logging
1515
from bittensor.utils.registration import log_no_torch_error, create_pow_async, torch
1616

@@ -379,3 +379,65 @@ async def register_extrinsic(
379379
# Failed to register after max attempts.
380380
logging.error("[red]No more attempts.[/red]")
381381
return False
382+
383+
384+
async def register_subnet_extrinsic(
385+
subtensor: "AsyncSubtensor",
386+
wallet: "Wallet",
387+
wait_for_inclusion: bool = False,
388+
wait_for_finalization: bool = True,
389+
) -> bool:
390+
"""
391+
Registers a new subnetwork on the Bittensor blockchain asynchronously.
392+
393+
Args:
394+
subtensor (AsyncSubtensor): The async subtensor interface to send the extrinsic.
395+
wallet (Wallet): The wallet to be used for subnet registration.
396+
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true.
397+
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning true.
398+
399+
Returns:
400+
bool: True if the subnet registration was successful, False otherwise.
401+
"""
402+
balance = await subtensor.get_balance(wallet.coldkeypub.ss58_address)
403+
burn_cost = await subtensor.get_subnet_burn_cost()
404+
405+
if burn_cost > balance:
406+
logging.error(
407+
f"Insufficient balance {balance} to register subnet. Current burn cost is {burn_cost} TAO"
408+
)
409+
return False
410+
411+
call = await subtensor.substrate.compose_call(
412+
call_module="SubtensorModule",
413+
call_function="register_network",
414+
call_params={
415+
"hotkey": wallet.hotkey.ss58_address,
416+
"mechid": 1,
417+
},
418+
)
419+
420+
extrinsic = await subtensor.substrate.create_signed_extrinsic(
421+
call=call, keypair=wallet.coldkey
422+
)
423+
424+
response = await subtensor.substrate.submit_extrinsic(
425+
extrinsic,
426+
wait_for_inclusion=wait_for_inclusion,
427+
wait_for_finalization=wait_for_finalization,
428+
)
429+
430+
if not wait_for_finalization and not wait_for_inclusion:
431+
return True
432+
433+
await response.process_events()
434+
if not await response.is_success:
435+
logging.error(
436+
f"Failed to register subnet: {format_error_message(await response.error_message, subtensor.substrate)}"
437+
)
438+
return False
439+
440+
logging.success(
441+
":white_heavy_check_mark: [green]Successfully registered subnet[/green]"
442+
)
443+
return True

bittensor/core/extrinsics/registration.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,59 @@ def _do_pow_register(
191191
)
192192

193193

194+
def register_subnet_extrinsic(
195+
subtensor: "Subtensor",
196+
wallet: "Wallet",
197+
wait_for_inclusion: bool = False,
198+
wait_for_finalization: bool = True,
199+
) -> bool:
200+
"""
201+
Registers a new subnetwork on the Bittensor blockchain.
202+
203+
Args:
204+
subtensor (Subtensor): The subtensor interface to send the extrinsic.
205+
wallet (Wallet): The wallet to be used for subnet registration.
206+
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true.
207+
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning true.
208+
209+
Returns:
210+
bool: True if the subnet registration was successful, False otherwise.
211+
"""
212+
balance = subtensor.get_balance(wallet.coldkeypub.ss58_address)
213+
burn_cost = subtensor.get_subnet_burn_cost()
214+
215+
if burn_cost > balance:
216+
logging.error(
217+
f"Insufficient balance {balance} to register subnet. Current burn cost is {burn_cost} TAO"
218+
)
219+
return False
220+
221+
call = subtensor.substrate.compose_call(
222+
call_module="SubtensorModule",
223+
call_function="register_network",
224+
call_params={
225+
"hotkey": wallet.hotkey.ss58_address,
226+
"mechid": 1,
227+
},
228+
)
229+
230+
success, message = subtensor.sign_and_send_extrinsic(
231+
call=call,
232+
wallet=wallet,
233+
wait_for_inclusion=wait_for_inclusion,
234+
wait_for_finalization=wait_for_finalization,
235+
)
236+
237+
if success:
238+
logging.success(
239+
":white_heavy_check_mark: [green]Successfully registered subnet[/green]"
240+
)
241+
return True
242+
else:
243+
logging.error(f"Failed to register subnet: {message}")
244+
return False
245+
246+
194247
def register_extrinsic(
195248
subtensor: "Subtensor",
196249
wallet: "Wallet",

bittensor/core/subtensor.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from bittensor.core.extrinsics.registration import (
3939
burned_register_extrinsic,
4040
register_extrinsic,
41+
register_subnet_extrinsic,
4142
)
4243
from bittensor.core.extrinsics.root import (
4344
root_register_extrinsic,
@@ -2326,6 +2327,32 @@ def register(
23262327
log_verbose=log_verbose,
23272328
)
23282329

2330+
def register_subnet(
2331+
self,
2332+
wallet: "Wallet",
2333+
wait_for_inclusion: bool = False,
2334+
wait_for_finalization: bool = True,
2335+
) -> bool:
2336+
"""
2337+
Registers a new subnetwork on the Bittensor network.
2338+
2339+
Args:
2340+
wallet (bittensor_wallet.Wallet): The wallet to be used for subnet registration.
2341+
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning true, or returns
2342+
false if the extrinsic fails to enter the block within the timeout. Default is False.
2343+
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
2344+
true, or returns false if the extrinsic fails to be finalized within the timeout. Default is True.
2345+
2346+
Returns:
2347+
bool: True if the subnet registration was successful, False otherwise.
2348+
"""
2349+
return register_subnet_extrinsic(
2350+
subtensor=self,
2351+
wallet=wallet,
2352+
wait_for_inclusion=wait_for_inclusion,
2353+
wait_for_finalization=wait_for_finalization,
2354+
)
2355+
23292356
def reveal_weights(
23302357
self,
23312358
wallet: "Wallet",

0 commit comments

Comments
 (0)