-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathtest_commit_weights.py
284 lines (237 loc) · 8.96 KB
/
test_commit_weights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import asyncio
import numpy as np
import pytest
from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit
from tests.e2e_tests.utils.chain_interactions import (
sudo_set_admin_utils,
sudo_set_hyperparameter_bool,
sudo_set_hyperparameter_values,
wait_epoch,
)
@pytest.mark.asyncio
async def test_commit_and_reveal_weights_legacy(local_chain, subtensor, alice_wallet):
"""
Tests the commit/reveal weights mechanism with subprocess disabled (CR1.0)
Steps:
1. Register a subnet through Alice
2. Enable commit-reveal mechanism on the subnet
3. Lower the commit_reveal interval and rate limit
4. Commit weights and verify
5. Wait interval & reveal weights and verify
Raises:
AssertionError: If any of the checks or verifications fail
"""
netuid = 2
print("Testing test_commit_and_reveal_weights")
# Register root as Alice
assert subtensor.register_subnet(alice_wallet), "Unable to register the subnet"
# Verify subnet 2 created successfully
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
# Enable commit_reveal on the subnet
assert sudo_set_hyperparameter_bool(
local_chain,
alice_wallet,
"sudo_set_commit_reveal_weights_enabled",
True,
netuid,
), "Unable to enable commit reveal on the subnet"
assert subtensor.get_subnet_hyperparameters(
netuid=netuid,
).commit_reveal_weights_enabled, "Failed to enable commit/reveal"
assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
), "Failed to set commit/reveal periods"
assert (
subtensor.weights_rate_limit(netuid=netuid) > 0
), "Weights rate limit is below 0"
# Lower the rate limit
assert sudo_set_hyperparameter_values(
local_chain,
alice_wallet,
call_function="sudo_set_weights_set_rate_limit",
call_params={"netuid": netuid, "weights_set_rate_limit": "0"},
return_error_message=True,
)
assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0
), "Failed to set weights_rate_limit"
assert subtensor.weights_rate_limit(netuid=netuid) == 0
# Increase subnet tempo so we have enough time to commit and reveal weights
sudo_set_admin_utils(
local_chain,
alice_wallet,
call_function="sudo_set_tempo",
call_params={
"netuid": netuid,
"tempo": 100,
},
return_error_message=True,
)
# Commit-reveal values
uids = np.array([0], dtype=np.int64)
weights = np.array([0.1], dtype=np.float32)
salt = [18, 179, 107, 0, 165, 211, 141, 197]
weight_uids, weight_vals = convert_weights_and_uids_for_emit(
uids=uids, weights=weights
)
# Commit weights
success, message = subtensor.commit_weights(
alice_wallet,
netuid,
salt=salt,
uids=weight_uids,
weights=weight_vals,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert success is True
weight_commits = subtensor.query_module(
module="SubtensorModule",
name="WeightCommits",
params=[netuid, alice_wallet.hotkey.ss58_address],
)
# Assert that the committed weights are set correctly
assert weight_commits is not None, "Weight commit not found in storage"
commit_hash, commit_block, reveal_block, expire_block = weight_commits[0]
assert commit_block > 0, f"Invalid block number: {commit_block}"
# Query the WeightCommitRevealInterval storage map
reveal_periods = subtensor.query_module(
module="SubtensorModule", name="RevealPeriodEpochs", params=[netuid]
)
periods = reveal_periods
assert periods > 0, "Invalid RevealPeriodEpochs"
# Wait until the reveal block range
await wait_epoch(subtensor, netuid)
# Reveal weights
success, message = subtensor.reveal_weights(
alice_wallet,
netuid,
uids=weight_uids,
weights=weight_vals,
salt=salt,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert success is True
# Query the Weights storage map
revealed_weights = subtensor.query_module(
module="SubtensorModule",
name="Weights",
params=[netuid, 0], # netuid and uid
)
# Assert that the revealed weights are set correctly
assert revealed_weights is not None, "Weight reveal not found in storage"
assert (
weight_vals[0] == revealed_weights[0][1]
), f"Incorrect revealed weights. Expected: {weights[0]}, Actual: {revealed_weights[0][1]}"
print("✅ Passed test_commit_and_reveal_weights")
@pytest.mark.asyncio
async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wallet):
"""
Tests that commiting weights doesn't re-use a nonce in the transaction pool.
Steps:
1. Register a subnet through Alice
2. Register Alice's neuron and add stake
3. Enable commit-reveal mechanism on the subnet
4. Lower the commit_reveal interval and rate limit
5. Commit weights three times
6. Assert that all commits succeeded
Raises:
AssertionError: If any of the checks or verifications fail
"""
# Wait for 2 tempos to pass as CR3 only reveals weights after 2 tempos
subtensor.wait_for_block(20)
netuid = 2
print("Testing test_commit_and_reveal_weights")
# Register root as Alice
assert subtensor.register_subnet(alice_wallet), "Unable to register the subnet"
# Verify subnet 1 created successfully
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
# Enable commit_reveal on the subnet
assert sudo_set_hyperparameter_bool(
local_chain,
alice_wallet,
"sudo_set_commit_reveal_weights_enabled",
True,
netuid,
), "Unable to enable commit reveal on the subnet"
assert subtensor.get_subnet_hyperparameters(
netuid=netuid,
).commit_reveal_weights_enabled, "Failed to enable commit/reveal"
assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
), "Failed to set commit/reveal periods"
assert (
subtensor.weights_rate_limit(netuid=netuid) > 0
), "Weights rate limit is below 0"
# Lower the rate limit
assert sudo_set_hyperparameter_values(
local_chain,
alice_wallet,
call_function="sudo_set_weights_set_rate_limit",
call_params={"netuid": netuid, "weights_set_rate_limit": "0"},
return_error_message=True,
)
assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0
), "Failed to set weights_rate_limit"
assert subtensor.weights_rate_limit(netuid=netuid) == 0
# Commit-reveal values
uids = np.array([0], dtype=np.int64)
weights = np.array([0.1], dtype=np.float32)
salt = [18, 179, 107, 0, 165, 211, 141, 197]
weight_uids, weight_vals = convert_weights_and_uids_for_emit(
uids=uids, weights=weights
)
# Make a second salt
salt2 = salt.copy()
salt2[0] += 1 # Increment the first byte to produce a different commit hash
# Make a third salt
salt3 = salt.copy()
salt3[0] += 2 # Increment the first byte to produce a different commit hash
# Commit all three salts
success, message = subtensor.commit_weights(
alice_wallet,
netuid,
salt=salt,
uids=weight_uids,
weights=weight_vals,
wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool
wait_for_finalization=False,
)
assert success is True
success, message = subtensor.commit_weights(
alice_wallet,
netuid,
salt=salt2,
uids=weight_uids,
weights=weight_vals,
wait_for_inclusion=False,
wait_for_finalization=False,
)
assert success is True
# Commit the third salt
success, message = subtensor.commit_weights(
alice_wallet,
netuid,
salt=salt3,
uids=weight_uids,
weights=weight_vals,
wait_for_inclusion=False,
wait_for_finalization=False,
)
assert success is True
# Wait a few blocks
await asyncio.sleep(2) # Wait for the txs to be included in the chain
# Query the WeightCommits storage map for all three salts
weight_commits = subtensor.query_module(
module="SubtensorModule",
name="WeightCommits",
params=[netuid, alice_wallet.hotkey.ss58_address],
)
# Assert that the committed weights are set correctly
assert weight_commits.value is not None, "Weight commit not found in storage"
commit_hash, commit_block, reveal_block, expire_block = weight_commits.value[0]
assert commit_block > 0, f"Invalid block number: {commit_block}"
# Check for three commits in the WeightCommits storage map
assert len(weight_commits.value) == 3, "Expected 3 weight commits"