|
1 | 1 | import copy
|
2 | 2 | from datetime import datetime, timezone
|
3 |
| - |
4 | 3 | from functools import lru_cache
|
5 | 4 | from typing import TYPE_CHECKING, Any, Iterable, Optional, Union, cast
|
6 | 5 |
|
7 | 6 | import numpy as np
|
8 | 7 | import scalecodec
|
9 | 8 | from async_substrate_interface.errors import SubstrateRequestException
|
10 |
| -from async_substrate_interface.types import ScaleObj |
11 | 9 | from async_substrate_interface.sync_substrate import SubstrateInterface
|
| 10 | +from async_substrate_interface.types import ScaleObj |
| 11 | +from bittensor_commit_reveal import get_encrypted_commitment |
12 | 12 | from numpy.typing import NDArray
|
13 | 13 |
|
14 | 14 | from bittensor.core.async_subtensor import ProposalVoteData
|
|
28 | 28 | decode_account_id,
|
29 | 29 | )
|
30 | 30 | from bittensor.core.chain_data.chain_identity import ChainIdentity
|
31 |
| -from bittensor.core.chain_data.utils import decode_metadata |
| 31 | +from bittensor.core.chain_data.utils import ( |
| 32 | + decode_metadata, |
| 33 | + decode_revealed_commitment, |
| 34 | + decode_revealed_commitment_with_hotkey, |
| 35 | +) |
32 | 36 | from bittensor.core.config import Config
|
33 | 37 | from bittensor.core.errors import ChainError
|
34 | 38 | from bittensor.core.extrinsics.commit_reveal import commit_reveal_v3_extrinsic
|
@@ -803,6 +807,54 @@ def get_all_commitments(
|
803 | 807 | result[decode_account_id(id_[0])] = decode_metadata(value)
|
804 | 808 | return result
|
805 | 809 |
|
| 810 | + def get_revealed_commitment( |
| 811 | + self, |
| 812 | + netuid: int, |
| 813 | + hotkey_ss58_address: Optional[str] = None, |
| 814 | + block: Optional[int] = None, |
| 815 | + ) -> Optional[tuple[int, str]]: |
| 816 | + """Returns hotkey related revealed commitment for a given netuid. |
| 817 | +
|
| 818 | + Arguments: |
| 819 | + netuid (int): The unique identifier of the subnetwork. |
| 820 | + block (Optional[int]): The block number to retrieve the commitment from. Default is ``None``. |
| 821 | + hotkey_ss58_address (str): The ss58 address of the committee member. |
| 822 | +
|
| 823 | + Returns: |
| 824 | + result (tuple[int, str): A tuple of reveal block and commitment message. |
| 825 | + """ |
| 826 | + query = self.query_module( |
| 827 | + module="Commitments", |
| 828 | + name="RevealedCommitments", |
| 829 | + params=[netuid, hotkey_ss58_address], |
| 830 | + block=block, |
| 831 | + ) |
| 832 | + return decode_revealed_commitment(query) |
| 833 | + |
| 834 | + def get_all_revealed_commitments( |
| 835 | + self, netuid: int, block: Optional[int] = None |
| 836 | + ) -> dict[str, tuple[int, str]]: |
| 837 | + """Returns all revealed commitments for a given netuid. |
| 838 | +
|
| 839 | + Arguments: |
| 840 | + netuid (int): The unique identifier of the subnetwork. |
| 841 | + block (Optional[int]): The block number to retrieve the commitment from. Default is ``None``. |
| 842 | +
|
| 843 | + Returns: |
| 844 | + result (dict): A dictionary of all revealed commitments in view {ss58_address: (reveal block, commitment message)}. |
| 845 | + """ |
| 846 | + query = self.query_map( |
| 847 | + module="Commitments", |
| 848 | + name="RevealedCommitments", |
| 849 | + params=[netuid], |
| 850 | + block=block, |
| 851 | + ) |
| 852 | + |
| 853 | + result = {} |
| 854 | + for item in query: |
| 855 | + result.update(decode_revealed_commitment_with_hotkey(item)) |
| 856 | + return result |
| 857 | + |
806 | 858 | def get_current_weight_commit_info(
|
807 | 859 | self, netuid: int, block: Optional[int] = None
|
808 | 860 | ) -> list:
|
@@ -2191,6 +2243,46 @@ def recycle(self, netuid: int, block: Optional[int] = None) -> Optional[Balance]
|
2191 | 2243 | call = self.get_hyperparameter(param_name="Burn", netuid=netuid, block=block)
|
2192 | 2244 | return None if call is None else Balance.from_rao(int(call))
|
2193 | 2245 |
|
| 2246 | + def set_reveal_commitment( |
| 2247 | + self, |
| 2248 | + wallet, |
| 2249 | + netuid: int, |
| 2250 | + data: str, |
| 2251 | + blocks_until_reveal: int = 360, |
| 2252 | + block_time: Union[int, float] = 12, |
| 2253 | + ) -> tuple[bool, int]: |
| 2254 | + """ |
| 2255 | + Commits arbitrary data to the Bittensor network by publishing metadata. |
| 2256 | +
|
| 2257 | + Arguments: |
| 2258 | + wallet (bittensor_wallet.Wallet): The wallet associated with the neuron committing the data. |
| 2259 | + netuid (int): The unique identifier of the subnetwork. |
| 2260 | + data (str): The data to be committed to the network. |
| 2261 | + blocks_until_reveal (int): The number of blocks from now after which the data will be revealed. Defaults to `360`. |
| 2262 | + Then amount of blocks in one epoch. |
| 2263 | + block_time (Union[int, float]): The number of seconds between each block. Defaults to `12`. |
| 2264 | +
|
| 2265 | + Returns: |
| 2266 | + bool: `True` if the commitment was successful, `False` otherwise. |
| 2267 | +
|
| 2268 | + Note: A commitment can be set once per subnet epoch and is reset at the next epoch in the chain automatically. |
| 2269 | + """ |
| 2270 | + |
| 2271 | + encrypted, reveal_round = get_encrypted_commitment( |
| 2272 | + data, blocks_until_reveal, block_time |
| 2273 | + ) |
| 2274 | + |
| 2275 | + # increase reveal_round in return + 1 because we want to fetch data from the chain after that round was revealed |
| 2276 | + # and stored. |
| 2277 | + data_ = {"encrypted": encrypted, "reveal_round": reveal_round} |
| 2278 | + return publish_metadata( |
| 2279 | + subtensor=self, |
| 2280 | + wallet=wallet, |
| 2281 | + netuid=netuid, |
| 2282 | + data_type=f"TimelockEncrypted", |
| 2283 | + data=data_, |
| 2284 | + ), reveal_round |
| 2285 | + |
2194 | 2286 | def subnet(self, netuid: int, block: Optional[int] = None) -> Optional[DynamicInfo]:
|
2195 | 2287 | """
|
2196 | 2288 | Retrieves the subnet information for a single subnet in the network.
|
|
0 commit comments