Skip to content

Commit 59778da

Browse files
committed
Porting
1 parent c3f3c2b commit 59778da

File tree

2 files changed

+167
-2
lines changed

2 files changed

+167
-2
lines changed

bittensor/core/async_subtensor.py

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

0 commit comments

Comments
 (0)