|
| 1 | +from typing import Optional, Union, TYPE_CHECKING |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from bittensor_commit_reveal import get_encrypted_commit |
| 5 | +from numpy.typing import NDArray |
| 6 | + |
| 7 | +from bittensor.core.settings import version_as_int |
| 8 | +from bittensor.utils import format_error_message |
| 9 | +from bittensor.utils.btlogging import logging |
| 10 | +from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from bittensor_wallet import Wallet |
| 14 | + from bittensor.core.async_subtensor import AsyncSubtensor |
| 15 | + from bittensor.utils.registration import torch |
| 16 | + |
| 17 | + |
| 18 | +async def _do_commit_reveal_v3( |
| 19 | + subtensor: "AsyncSubtensor", |
| 20 | + wallet: "Wallet", |
| 21 | + netuid: int, |
| 22 | + commit: bytes, |
| 23 | + reveal_round: int, |
| 24 | + wait_for_inclusion: bool = False, |
| 25 | + wait_for_finalization: bool = False, |
| 26 | +) -> tuple[bool, Optional[str]]: |
| 27 | + """ |
| 28 | + Executes the commit-reveal phase 3 for a given netuid and commit, and optionally waits for extrinsic inclusion or finalization. |
| 29 | +
|
| 30 | + Arguments: |
| 31 | + subtensor: An instance of the Subtensor class. |
| 32 | + wallet: Wallet An instance of the Wallet class containing the user's keypair. |
| 33 | + netuid: int The network unique identifier. |
| 34 | + commit bytes The commit data in bytes format. |
| 35 | + reveal_round: int The round number for the reveal phase. |
| 36 | + wait_for_inclusion: bool, optional Flag indicating whether to wait for the extrinsic to be included in a block. |
| 37 | + wait_for_finalization: bool, optional Flag indicating whether to wait for the extrinsic to be finalized. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + A tuple where the first element is a boolean indicating success or failure, and the second element is an optional string containing error message if any. |
| 41 | + """ |
| 42 | + logging.info( |
| 43 | + f"Committing weights hash [blue]{commit.hex()}[/blue] for subnet #[blue]{netuid}[/blue] with " |
| 44 | + f"reveal round [blue]{reveal_round}[/blue]..." |
| 45 | + ) |
| 46 | + |
| 47 | + call = await subtensor.substrate.compose_call( |
| 48 | + call_module="SubtensorModule", |
| 49 | + call_function="commit_crv3_weights", |
| 50 | + call_params={ |
| 51 | + "netuid": netuid, |
| 52 | + "commit": commit, |
| 53 | + "reveal_round": reveal_round, |
| 54 | + }, |
| 55 | + ) |
| 56 | + extrinsic = await subtensor.substrate.create_signed_extrinsic( |
| 57 | + call=call, |
| 58 | + keypair=wallet.hotkey, |
| 59 | + ) |
| 60 | + |
| 61 | + response = await subtensor.substrate.submit_extrinsic( |
| 62 | + subtensor=subtensor, |
| 63 | + extrinsic=extrinsic, |
| 64 | + wait_for_inclusion=wait_for_inclusion, |
| 65 | + wait_for_finalization=wait_for_finalization, |
| 66 | + ) |
| 67 | + |
| 68 | + if not wait_for_finalization and not wait_for_inclusion: |
| 69 | + return True, "Not waiting for finalization or inclusion." |
| 70 | + |
| 71 | + if await response.is_success: |
| 72 | + return True, None |
| 73 | + |
| 74 | + return False, format_error_message(await response.error_message) |
| 75 | + |
| 76 | + |
| 77 | +async def commit_reveal_v3_extrinsic( |
| 78 | + subtensor: "AsyncSubtensor", |
| 79 | + wallet: "Wallet", |
| 80 | + netuid: int, |
| 81 | + uids: Union[NDArray[np.int64], "torch.LongTensor", list], |
| 82 | + weights: Union[NDArray[np.float32], "torch.FloatTensor", list], |
| 83 | + version_key: int = version_as_int, |
| 84 | + wait_for_inclusion: bool = False, |
| 85 | + wait_for_finalization: bool = False, |
| 86 | +) -> tuple[bool, str]: |
| 87 | + """ |
| 88 | + Commits and reveals weights for given subtensor and wallet with provided uids and weights. |
| 89 | +
|
| 90 | + Arguments: |
| 91 | + subtensor: The Subtensor instance. |
| 92 | + wallet: The wallet to use for committing and revealing. |
| 93 | + netuid: The id of the network. |
| 94 | + uids: The uids to commit. |
| 95 | + weights: The weights associated with the uids. |
| 96 | + version_key: The version key to use for committing and revealing. Default is version_as_int. |
| 97 | + wait_for_inclusion: Whether to wait for the inclusion of the transaction. Default is False. |
| 98 | + wait_for_finalization: Whether to wait for the finalization of the transaction. Default is False. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + tuple[bool, str]: A tuple where the first element is a boolean indicating success or failure, and the second element is a message associated with the result. |
| 102 | + """ |
| 103 | + try: |
| 104 | + # Convert uids and weights |
| 105 | + if isinstance(uids, list): |
| 106 | + uids = np.array(uids, dtype=np.int64) |
| 107 | + if isinstance(weights, list): |
| 108 | + weights = np.array(weights, dtype=np.float32) |
| 109 | + |
| 110 | + # Reformat and normalize. |
| 111 | + uids, weights = convert_weights_and_uids_for_emit(uids, weights) |
| 112 | + |
| 113 | + current_block = await subtensor.substrate.get_block_number(None) |
| 114 | + subnet_hyperparameters = await subtensor.get_subnet_hyperparameters(netuid) |
| 115 | + tempo = subnet_hyperparameters.tempo |
| 116 | + subnet_reveal_period_epochs = ( |
| 117 | + subnet_hyperparameters.commit_reveal_weights_interval |
| 118 | + ) |
| 119 | + |
| 120 | + # Encrypt `commit_hash` with t-lock and `get reveal_round` |
| 121 | + commit_for_reveal, reveal_round = get_encrypted_commit( |
| 122 | + uids=uids, |
| 123 | + weights=weights, |
| 124 | + version_key=version_key, |
| 125 | + tempo=tempo, |
| 126 | + current_block=current_block, |
| 127 | + netuid=netuid, |
| 128 | + subnet_reveal_period_epochs=subnet_reveal_period_epochs, |
| 129 | + ) |
| 130 | + |
| 131 | + success, message = await _do_commit_reveal_v3( |
| 132 | + subtensor=subtensor, |
| 133 | + wallet=wallet, |
| 134 | + netuid=netuid, |
| 135 | + commit=commit_for_reveal, |
| 136 | + reveal_round=reveal_round, |
| 137 | + wait_for_inclusion=wait_for_inclusion, |
| 138 | + wait_for_finalization=wait_for_finalization, |
| 139 | + ) |
| 140 | + |
| 141 | + if success is not True: |
| 142 | + logging.error(message) |
| 143 | + return False, message |
| 144 | + |
| 145 | + logging.success( |
| 146 | + f"[green]Finalized![/green] Weights commited with reveal round [blue]{reveal_round}[/blue]." |
| 147 | + ) |
| 148 | + return True, f"reveal_round:{reveal_round}" |
| 149 | + |
| 150 | + except Exception as e: |
| 151 | + logging.error(f":cross_mark: [red]Failed. Error:[/red] {e}") |
| 152 | + return False, str(e) |
0 commit comments