|
| 1 | +from typing import TYPE_CHECKING, Optional |
| 2 | +from bittensor.utils import float_to_u64, unlock_key |
| 3 | + |
| 4 | +if TYPE_CHECKING: |
| 5 | + from bittensor_wallet import Wallet |
| 6 | + from bittensor.core.async_subtensor import AsyncSubtensor |
| 7 | + |
| 8 | + |
| 9 | +async def set_children_extrinsic( |
| 10 | + subtensor: "AsyncSubtensor", |
| 11 | + wallet: "Wallet", |
| 12 | + hotkey: str, |
| 13 | + netuid: int, |
| 14 | + children: list[tuple[float, str]], |
| 15 | + wait_for_inclusion: bool = True, |
| 16 | + wait_for_finalization: bool = False, |
| 17 | + raise_error: bool = False, |
| 18 | + period: Optional[int] = None, |
| 19 | +): |
| 20 | + """ |
| 21 | + Allows a coldkey to set children-keys. |
| 22 | +
|
| 23 | + Arguments: |
| 24 | + subtensor: bittensor subtensor. |
| 25 | + wallet: bittensor wallet instance. |
| 26 | + hotkey: The ``SS58`` address of the neuron's hotkey. |
| 27 | + netuid: The netuid value. |
| 28 | + children: A list of children with their proportions. |
| 29 | + wait_for_inclusion: Waits for the transaction to be included in a block. |
| 30 | + wait_for_finalization: Waits for the transaction to be finalized on the blockchain. |
| 31 | + raise_error: Raises a relevant exception rather than returning `False` if unsuccessful. |
| 32 | + period: The number of blocks during which the transaction will remain valid after it's submitted. If the |
| 33 | + transaction is not included in a block within that number of blocks, it will expire and be rejected. You can |
| 34 | + think of it as an expiration date for the transaction. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + tuple[bool, str]: A tuple where the first element is a boolean indicating success or failure of the operation, |
| 38 | + and the second element is a message providing additional information. |
| 39 | +
|
| 40 | + Raises: |
| 41 | + DuplicateChild: There are duplicates in the list of children. |
| 42 | + InvalidChild: Child is the hotkey. |
| 43 | + NonAssociatedColdKey: The coldkey does not own the hotkey or the child is the same as the hotkey. |
| 44 | + NotEnoughStakeToSetChildkeys: Parent key doesn't have minimum own stake. |
| 45 | + ProportionOverflow: The sum of the proportions does exceed uint64. |
| 46 | + RegistrationNotPermittedOnRootSubnet: Attempting to register a child on the root network. |
| 47 | + SubNetworkDoesNotExist: Attempting to register to a non-existent network. |
| 48 | + TooManyChildren: Too many children in request. |
| 49 | + TxRateLimitExceeded: Hotkey hit the rate limit. |
| 50 | + bittensor_wallet.errors.KeyFileError: Failed to decode keyfile data. |
| 51 | + bittensor_wallet.errors.PasswordError: Decryption failed or wrong password for decryption provided. |
| 52 | + """ |
| 53 | + unlock = unlock_key(wallet, raise_error=raise_error) |
| 54 | + |
| 55 | + if not unlock.success: |
| 56 | + return False, unlock.message |
| 57 | + |
| 58 | + async with subtensor.substrate as substrate: |
| 59 | + call = await substrate.compose_call( |
| 60 | + call_module="SubtensorModule", |
| 61 | + call_function="set_children", |
| 62 | + call_params={ |
| 63 | + "children": [ |
| 64 | + ( |
| 65 | + float_to_u64(proportion), |
| 66 | + child_hotkey, |
| 67 | + ) |
| 68 | + for proportion, child_hotkey in children |
| 69 | + ], |
| 70 | + "hotkey": hotkey, |
| 71 | + "netuid": netuid, |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + success, message = await subtensor.sign_and_send_extrinsic( |
| 76 | + call=call, |
| 77 | + wallet=wallet, |
| 78 | + wait_for_inclusion=wait_for_inclusion, |
| 79 | + wait_for_finalization=wait_for_finalization, |
| 80 | + raise_error=raise_error, |
| 81 | + period=period, |
| 82 | + ) |
| 83 | + |
| 84 | + if not wait_for_finalization and not wait_for_inclusion: |
| 85 | + return True, message |
| 86 | + |
| 87 | + if success: |
| 88 | + return True, "Success with `set_children_extrinsic` response." |
| 89 | + |
| 90 | + return True, message |
| 91 | + |
| 92 | + |
| 93 | +async def root_set_pending_childkey_cooldown_extrinsic( |
| 94 | + subtensor: "AsyncSubtensor", |
| 95 | + wallet: "Wallet", |
| 96 | + cooldown: int, |
| 97 | + wait_for_inclusion: bool = True, |
| 98 | + wait_for_finalization: bool = False, |
| 99 | + period: Optional[int] = None, |
| 100 | +) -> tuple[bool, str]: |
| 101 | + """ |
| 102 | + Allows a coldkey to set children-keys. |
| 103 | + """ |
| 104 | + unlock = unlock_key(wallet) |
| 105 | + |
| 106 | + if not unlock.success: |
| 107 | + return False, unlock.message |
| 108 | + |
| 109 | + async with subtensor.substrate as substrate: |
| 110 | + call = await substrate.compose_call( |
| 111 | + call_module="SubtensorModule", |
| 112 | + call_function="set_pending_childkey_cooldown", |
| 113 | + call_params={"cooldown": cooldown}, |
| 114 | + ) |
| 115 | + |
| 116 | + sudo_call = await substrate.compose_call( |
| 117 | + call_module="Sudo", |
| 118 | + call_function="sudo", |
| 119 | + call_params={"call": call}, |
| 120 | + ) |
| 121 | + |
| 122 | + success, message = await subtensor.sign_and_send_extrinsic( |
| 123 | + call=sudo_call, |
| 124 | + wallet=wallet, |
| 125 | + wait_for_inclusion=wait_for_inclusion, |
| 126 | + wait_for_finalization=wait_for_finalization, |
| 127 | + period=period, |
| 128 | + ) |
| 129 | + |
| 130 | + if not wait_for_finalization and not wait_for_inclusion: |
| 131 | + return True, message |
| 132 | + |
| 133 | + if success: |
| 134 | + return ( |
| 135 | + True, |
| 136 | + "Success with `root_set_pending_childkey_cooldown_extrinsic` response.", |
| 137 | + ) |
| 138 | + |
| 139 | + return True, message |
0 commit comments