Skip to content

Commit 35a2779

Browse files
committed
Merge branch 'staging' into fix/thewhaleking/fix-test-breakages
2 parents 81af4fd + ee0f422 commit 35a2779

File tree

7 files changed

+83
-72
lines changed

7 files changed

+83
-72
lines changed

bittensor/core/extrinsics/asyncex/commit_reveal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
from bittensor.utils.registration import torch
1717

1818

19+
# TODO: Merge this logic with `commit_reveal_extrinsic` in SDKv10 bc this is not CRv3 anymore.
1920
async def _do_commit_reveal_v3(
2021
subtensor: "AsyncSubtensor",
2122
wallet: "Wallet",
2223
netuid: int,
2324
commit: bytes,
2425
reveal_round: int,
26+
commit_reveal_version: int = 4,
2527
wait_for_inclusion: bool = False,
2628
wait_for_finalization: bool = False,
2729
period: Optional[int] = None,
@@ -35,6 +37,7 @@ async def _do_commit_reveal_v3(
3537
netuid: int The network unique identifier.
3638
commit: bytes The commit data in bytes format.
3739
reveal_round: int The round number for the reveal phase.
40+
commit_reveal_version: The version of the chain commit-reveal protocol to use. Default is ``4``.
3841
wait_for_inclusion: bool, optional Flag indicating whether to wait for the extrinsic to be included in a block.
3942
wait_for_finalization: bool, optional Flag indicating whether to wait for the extrinsic to be finalized.
4043
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted. If
@@ -52,11 +55,12 @@ async def _do_commit_reveal_v3(
5255

5356
call = await subtensor.substrate.compose_call(
5457
call_module="SubtensorModule",
55-
call_function="commit_crv3_weights",
58+
call_function="commit_timelocked_weights",
5659
call_params={
5760
"netuid": netuid,
5861
"commit": commit,
5962
"reveal_round": reveal_round,
63+
"commit_reveal_version": commit_reveal_version,
6064
},
6165
)
6266
return await subtensor.sign_and_send_extrinsic(
@@ -69,6 +73,7 @@ async def _do_commit_reveal_v3(
6973
)
7074

7175

76+
# TODO: rename this extrinsic to `commit_reveal_extrinsic` in SDK.v10
7277
async def commit_reveal_v3_extrinsic(
7378
subtensor: "AsyncSubtensor",
7479
wallet: "Wallet",
@@ -122,6 +127,7 @@ async def commit_reveal_v3_extrinsic(
122127
netuid=netuid,
123128
subnet_reveal_period_epochs=subnet_reveal_period_epochs,
124129
block_time=block_time,
130+
hotkey=wallet.hotkey.public_key,
125131
)
126132

127133
success, message = await _do_commit_reveal_v3(
@@ -135,7 +141,7 @@ async def commit_reveal_v3_extrinsic(
135141
period=period,
136142
)
137143

138-
if success is not True:
144+
if not success:
139145
logging.error(message)
140146
return False, message
141147

bittensor/core/extrinsics/commit_reveal.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@
1616
from bittensor.utils.registration import torch
1717

1818

19+
# TODO: Merge this logic with `commit_reveal_extrinsic` in SDKv10 bc this is not CRv3 anymore.
1920
def _do_commit_reveal_v3(
2021
subtensor: "Subtensor",
2122
wallet: "Wallet",
2223
netuid: int,
2324
commit: bytes,
2425
reveal_round: int,
26+
commit_reveal_version: int = 4,
2527
wait_for_inclusion: bool = False,
2628
wait_for_finalization: bool = False,
2729
period: Optional[int] = None,
2830
) -> tuple[bool, str]:
2931
"""
30-
Executes commit-reveal phase 3 for a given netuid and commit, and optionally waits for extrinsic inclusion or
31-
finalization.
32+
Executes commit-reveal extrinsic for a given netuid, commit, reveal_round, and commit_reveal_version.
3233
3334
Arguments:
3435
subtensor: An instance of the Subtensor class.
3536
wallet: Wallet An instance of the Wallet class containing the user's keypair.
3637
netuid: int The network unique identifier.
3738
commit: bytes The commit data in bytes format.
3839
reveal_round: int The round number for the reveal phase.
40+
commit_reveal_version: The version of the chain commit-reveal protocol to use. Default is ``4``.
3941
wait_for_inclusion: bool, optional Flag indicating whether to wait for the extrinsic to be included in a block.
4042
wait_for_finalization: bool, optional Flag indicating whether to wait for the extrinsic to be finalized.
4143
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted. If
@@ -53,11 +55,12 @@ def _do_commit_reveal_v3(
5355

5456
call = subtensor.substrate.compose_call(
5557
call_module="SubtensorModule",
56-
call_function="commit_crv3_weights",
58+
call_function="commit_timelocked_weights",
5759
call_params={
5860
"netuid": netuid,
5961
"commit": commit,
6062
"reveal_round": reveal_round,
63+
"commit_reveal_version": commit_reveal_version,
6164
},
6265
)
6366
return subtensor.sign_and_send_extrinsic(
@@ -70,6 +73,7 @@ def _do_commit_reveal_v3(
7073
)
7174

7275

76+
# TODO: rename this extrinsic to `commit_reveal_extrinsic` in SDK.v10
7377
def commit_reveal_v3_extrinsic(
7478
subtensor: "Subtensor",
7579
wallet: "Wallet",
@@ -123,6 +127,7 @@ def commit_reveal_v3_extrinsic(
123127
netuid=netuid,
124128
subnet_reveal_period_epochs=subnet_reveal_period_epochs,
125129
block_time=block_time,
130+
hotkey=wallet.hotkey.public_key,
126131
)
127132

128133
success, message = _do_commit_reveal_v3(
@@ -136,7 +141,7 @@ def commit_reveal_v3_extrinsic(
136141
period=period,
137142
)
138143

139-
if success is not True:
144+
if not success:
140145
logging.error(message)
141146
return False, message
142147

bittensor/core/subtensor_api/subnets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(self, subtensor: Union["_Subtensor", "_AsyncSubtensor"]):
1212
self.blocks_since_last_step = subtensor.blocks_since_last_step
1313
self.blocks_since_last_update = subtensor.blocks_since_last_update
1414
self.bonds = subtensor.bonds
15+
self.commit_reveal_enabled = subtensor.commit_reveal_enabled
1516
self.difficulty = subtensor.difficulty
1617
self.get_all_subnets_info = subtensor.get_all_subnets_info
1718
self.get_parents = subtensor.get_parents

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies = [
3434
"pydantic>=2.3, <3",
3535
"scalecodec==1.2.11",
3636
"uvicorn",
37-
"bittensor-drand>=0.5.0",
37+
"bittensor-drand>=1.0.0",
3838
"bittensor-wallet>=4.0.0,<5.0",
3939
"async-substrate-interface>=1.4.2"
4040
]

tests/e2e_tests/test_commit_reveal_v3.py

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# @pytest.mark.parametrize("local_chain", [True], indirect=True)
1717
@pytest.mark.asyncio
18-
async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_wallet):
18+
async def test_commit_and_reveal_weights_cr4(local_chain, subtensor, alice_wallet):
1919
"""
2020
Tests the commit/reveal weights mechanism (CR3)
2121
@@ -30,56 +30,64 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
3030
Raises:
3131
AssertionError: If any of the checks or verifications fail
3232
"""
33+
logging.console.info("Testing `test_commit_and_reveal_weights_cr4`")
34+
3335
BLOCK_TIME = (
3436
0.25 if subtensor.is_fast_blocks() else 12.0
3537
) # 12 for non-fast-block, 0.25 for fast block
36-
netuid = subtensor.get_total_subnets() # 2
3738

38-
logging.console.info("Testing test_commit_and_reveal_weights")
39+
logging.console.info(f"Using block time: {BLOCK_TIME}")
40+
41+
netuid = subtensor.get_total_subnets() # 2
3942

4043
# Register root as Alice
4144
assert subtensor.register_subnet(alice_wallet), "Unable to register the subnet"
4245

4346
# Verify subnet 2 created successfully
44-
assert subtensor.subnet_exists(netuid), (
45-
f"Subnet {netuid} wasn't created successfully"
46-
)
47+
assert subtensor.subnet_exists(netuid), f"SN #{netuid} wasn't created successfully"
4748

48-
logging.console.success(f"Subnet {netuid} is registered")
49+
logging.console.success(f"SN #{netuid} is registered.")
4950

5051
# Enable commit_reveal on the subnet
5152
assert sudo_set_hyperparameter_bool(
52-
local_chain,
53-
alice_wallet,
54-
"sudo_set_commit_reveal_weights_enabled",
55-
True,
56-
netuid,
57-
), "Unable to enable commit reveal on the subnet"
53+
substrate=local_chain,
54+
wallet=alice_wallet,
55+
call_function="sudo_set_commit_reveal_weights_enabled",
56+
value=True,
57+
netuid=netuid,
58+
), f"Unable to enable commit reveal on the SN #{netuid}"
5859

5960
# Verify commit_reveal was enabled
60-
assert subtensor.commit_reveal_enabled(netuid), "Failed to enable commit/reveal"
61-
logging.console.info("Commit reveal enabled")
61+
assert subtensor.subnets.commit_reveal_enabled(netuid), (
62+
"Failed to enable commit/reveal"
63+
)
64+
logging.console.success("Commit reveal enabled")
65+
66+
cr_version = subtensor.substrate.query(
67+
module="SubtensorModule", storage_function="CommitRevealWeightsVersion"
68+
)
69+
assert cr_version == 4, f"Commit reveal version is not 3, got {cr_version}"
6270

6371
# Change the weights rate limit on the subnet
6472
status, error = sudo_set_admin_utils(
65-
local_chain,
66-
alice_wallet,
73+
substrate=local_chain,
74+
wallet=alice_wallet,
6775
call_function="sudo_set_weights_set_rate_limit",
6876
call_params={"netuid": netuid, "weights_set_rate_limit": "0"},
6977
)
7078

71-
assert error is None
7279
assert status is True
80+
assert error is None
7381

7482
# Verify weights rate limit was changed
7583
assert (
7684
subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0
7785
), "Failed to set weights_rate_limit"
7886
assert subtensor.weights_rate_limit(netuid=netuid) == 0
79-
logging.console.info("sudo_set_weights_set_rate_limit executed: set to 0")
87+
logging.console.success("sudo_set_weights_set_rate_limit executed: set to 0")
8088

8189
# Change the tempo of the subnet
82-
tempo_set = 50 if subtensor.is_fast_blocks() else 10
90+
tempo_set = 100 if subtensor.is_fast_blocks() else 10
8391
assert (
8492
sudo_set_admin_utils(
8593
local_chain,
@@ -89,9 +97,10 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
8997
)[0]
9098
is True
9199
)
100+
92101
tempo = subtensor.get_subnet_hyperparameters(netuid=netuid).tempo
93-
assert tempo_set == tempo
94-
logging.console.info(f"sudo_set_tempo executed: set to {tempo_set}")
102+
assert tempo_set == tempo, "SN tempos has not been changed."
103+
logging.console.success(f"SN #{netuid} tempo set to {tempo_set}")
95104

96105
# Commit-reveal values - setting weights to self
97106
uids = np.array([0], dtype=np.int64)
@@ -107,19 +116,16 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
107116
f"Checking if window is too low with Current block: {current_block}, next tempo: {upcoming_tempo}"
108117
)
109118

110-
# Wait for 2 tempos to pass as CR3 only reveals weights after 2 tempos + 1
111-
subtensor.wait_for_block(tempo_set * 2 + 1)
112-
113119
# Lower than this might mean weights will get revealed before we can check them
114-
if upcoming_tempo - current_block < 3:
120+
if upcoming_tempo - current_block < 6:
115121
await wait_interval(
116122
tempo,
117123
subtensor,
118124
netuid=netuid,
119125
reporting_interval=1,
120126
)
121127
current_block = subtensor.get_current_block()
122-
expected_block = current_block
128+
expected_commit_block = current_block + 1
123129
latest_drand_round = subtensor.last_drand_round()
124130
upcoming_tempo = next_tempo(current_block, tempo)
125131
logging.console.info(
@@ -128,8 +134,8 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
128134

129135
# Commit weights
130136
success, message = subtensor.set_weights(
131-
alice_wallet,
132-
netuid,
137+
wallet=alice_wallet,
138+
netuid=netuid,
133139
uids=weight_uids,
134140
weights=weight_vals,
135141
wait_for_inclusion=True,
@@ -139,73 +145,60 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
139145
)
140146

141147
# Assert committing was a success
142-
assert success is True
148+
assert success is True, message
143149
assert bool(re.match(r"reveal_round:\d+", message))
144150

145151
# Parse expected reveal_round
146152
expected_reveal_round = int(message.split(":")[1])
147-
logging.console.info(
153+
logging.console.success(
148154
f"Successfully set weights: uids {weight_uids}, weights {weight_vals}, reveal_round: {expected_reveal_round}"
149155
)
150156

151-
current_block = subtensor.get_current_block()
152-
latest_drand_round = subtensor.last_drand_round()
153-
upcoming_tempo = next_tempo(current_block, tempo)
154-
logging.console.info(
155-
f"After setting weights: Current block: {current_block}, next tempo: {upcoming_tempo}, drand: {latest_drand_round}"
156-
)
157-
158-
# Ensure the expected drand round is well in the future
159-
assert expected_reveal_round >= latest_drand_round + 1, (
160-
"Revealed drand pulse is older than the drand pulse right after setting weights"
161-
)
162-
163157
# Fetch current commits pending on the chain
164158
commits_on_chain = subtensor.get_current_weight_commit_info_v2(netuid=netuid)
165159
address, commit_block, commit, reveal_round = commits_on_chain[0]
166160

167161
# Assert correct values are committed on the chain
168162
assert expected_reveal_round == reveal_round
169163
assert address == alice_wallet.hotkey.ss58_address
170-
assert commit_block == expected_block + 1
164+
assert commit_block == expected_commit_block + 1
171165

172166
# Ensure no weights are available as of now
173167
assert subtensor.weights(netuid=netuid) == []
168+
logging.console.success("No weights are available before next epoch.")
174169

175-
# Wait for the next tempo so weights can be revealed
176-
await wait_interval(
177-
subtensor.get_subnet_hyperparameters(netuid=netuid).tempo,
178-
subtensor,
179-
netuid=netuid,
180-
reporting_interval=1,
181-
sleep=BLOCK_TIME,
170+
expected_reveal_block = (
171+
subtensor.subnets.get_next_epoch_start_block(netuid) + 5
172+
) # 5 is safety drand offset
173+
logging.console.info(
174+
f"Waiting for the next epoch to ensure weights are revealed: block {expected_reveal_block}"
182175
)
176+
subtensor.wait_for_block(expected_reveal_block)
183177

184178
# Fetch the latest drand pulse
185179
latest_drand_round = subtensor.last_drand_round()
186180
logging.console.info(
187181
f"Latest drand round after waiting for tempo: {latest_drand_round}"
188182
)
189183

190-
# wait until last_drand_round is the same or greeter than expected_reveal_round with sleep 3 second (as Drand round period)
191-
while expected_reveal_round >= subtensor.last_drand_round():
192-
time.sleep(3)
193-
194184
# Fetch weights on the chain as they should be revealed now
195-
revealed_weights_ = subtensor.weights(netuid=netuid)
185+
subnet_weights = subtensor.weights(netuid=netuid)
196186

197-
print("revealed weights", revealed_weights_)
198-
revealed_weights = revealed_weights_[0][1]
187+
revealed_weights = subnet_weights[0][1]
199188
# Assert correct weights were revealed
200189
assert weight_uids[0] == revealed_weights[0][0]
201190
assert weight_vals[0] == revealed_weights[0][1]
202191

192+
logging.console.success(
193+
f"Successfully revealed weights: uids {weight_uids}, weights {weight_vals}"
194+
)
195+
203196
# Now that the commit has been revealed, there shouldn't be any pending commits
204-
assert subtensor.get_current_weight_commit_info_v2(netuid=netuid) == []
197+
assert subtensor.commitments.get_current_weight_commit_info_v2(netuid=netuid) == []
205198

206199
# Ensure the drand_round is always in the positive w.r.t expected when revealed
207-
assert latest_drand_round - expected_reveal_round >= 0, (
200+
assert latest_drand_round - expected_reveal_round >= -3, (
208201
f"latest_drand_round ({latest_drand_round}) is less than expected_reveal_round ({expected_reveal_round})"
209202
)
210203

211-
logging.console.info("✅ Passed commit_reveal v3")
204+
logging.console.success("✅ Passed `test_commit_and_reveal_weights_cr4`")

0 commit comments

Comments
 (0)