15
15
16
16
# @pytest.mark.parametrize("local_chain", [True], indirect=True)
17
17
@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 ):
19
19
"""
20
20
Tests the commit/reveal weights mechanism (CR3)
21
21
@@ -30,56 +30,64 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
30
30
Raises:
31
31
AssertionError: If any of the checks or verifications fail
32
32
"""
33
+ logging .console .info ("Testing `test_commit_and_reveal_weights_cr4`" )
34
+
33
35
BLOCK_TIME = (
34
36
0.25 if subtensor .is_fast_blocks () else 12.0
35
37
) # 12 for non-fast-block, 0.25 for fast block
36
- netuid = subtensor .get_total_subnets () # 2
37
38
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
39
42
40
43
# Register root as Alice
41
44
assert subtensor .register_subnet (alice_wallet ), "Unable to register the subnet"
42
45
43
46
# 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"
47
48
48
- logging .console .success (f"Subnet { netuid } is registered" )
49
+ logging .console .success (f"SN # { netuid } is registered. " )
49
50
50
51
# Enable commit_reveal on the subnet
51
52
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 } "
58
59
59
60
# 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 } "
62
70
63
71
# Change the weights rate limit on the subnet
64
72
status , error = sudo_set_admin_utils (
65
- local_chain ,
66
- alice_wallet ,
73
+ substrate = local_chain ,
74
+ wallet = alice_wallet ,
67
75
call_function = "sudo_set_weights_set_rate_limit" ,
68
76
call_params = {"netuid" : netuid , "weights_set_rate_limit" : "0" },
69
77
)
70
78
71
- assert error is None
72
79
assert status is True
80
+ assert error is None
73
81
74
82
# Verify weights rate limit was changed
75
83
assert (
76
84
subtensor .get_subnet_hyperparameters (netuid = netuid ).weights_rate_limit == 0
77
85
), "Failed to set weights_rate_limit"
78
86
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" )
80
88
81
89
# 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
83
91
assert (
84
92
sudo_set_admin_utils (
85
93
local_chain ,
@@ -89,9 +97,10 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
89
97
)[0 ]
90
98
is True
91
99
)
100
+
92
101
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 } " )
95
104
96
105
# Commit-reveal values - setting weights to self
97
106
uids = np .array ([0 ], dtype = np .int64 )
@@ -107,19 +116,16 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
107
116
f"Checking if window is too low with Current block: { current_block } , next tempo: { upcoming_tempo } "
108
117
)
109
118
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
-
113
119
# 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 :
115
121
await wait_interval (
116
122
tempo ,
117
123
subtensor ,
118
124
netuid = netuid ,
119
125
reporting_interval = 1 ,
120
126
)
121
127
current_block = subtensor .get_current_block ()
122
- expected_block = current_block
128
+ expected_commit_block = current_block + 1
123
129
latest_drand_round = subtensor .last_drand_round ()
124
130
upcoming_tempo = next_tempo (current_block , tempo )
125
131
logging .console .info (
@@ -128,8 +134,8 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
128
134
129
135
# Commit weights
130
136
success , message = subtensor .set_weights (
131
- alice_wallet ,
132
- netuid ,
137
+ wallet = alice_wallet ,
138
+ netuid = netuid ,
133
139
uids = weight_uids ,
134
140
weights = weight_vals ,
135
141
wait_for_inclusion = True ,
@@ -139,73 +145,60 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
139
145
)
140
146
141
147
# Assert committing was a success
142
- assert success is True
148
+ assert success is True , message
143
149
assert bool (re .match (r"reveal_round:\d+" , message ))
144
150
145
151
# Parse expected reveal_round
146
152
expected_reveal_round = int (message .split (":" )[1 ])
147
- logging .console .info (
153
+ logging .console .success (
148
154
f"Successfully set weights: uids { weight_uids } , weights { weight_vals } , reveal_round: { expected_reveal_round } "
149
155
)
150
156
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
-
163
157
# Fetch current commits pending on the chain
164
158
commits_on_chain = subtensor .get_current_weight_commit_info_v2 (netuid = netuid )
165
159
address , commit_block , commit , reveal_round = commits_on_chain [0 ]
166
160
167
161
# Assert correct values are committed on the chain
168
162
assert expected_reveal_round == reveal_round
169
163
assert address == alice_wallet .hotkey .ss58_address
170
- assert commit_block == expected_block + 1
164
+ assert commit_block == expected_commit_block + 1
171
165
172
166
# Ensure no weights are available as of now
173
167
assert subtensor .weights (netuid = netuid ) == []
168
+ logging .console .success ("No weights are available before next epoch." )
174
169
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 } "
182
175
)
176
+ subtensor .wait_for_block (expected_reveal_block )
183
177
184
178
# Fetch the latest drand pulse
185
179
latest_drand_round = subtensor .last_drand_round ()
186
180
logging .console .info (
187
181
f"Latest drand round after waiting for tempo: { latest_drand_round } "
188
182
)
189
183
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
-
194
184
# 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 )
196
186
197
- print ("revealed weights" , revealed_weights_ )
198
- revealed_weights = revealed_weights_ [0 ][1 ]
187
+ revealed_weights = subnet_weights [0 ][1 ]
199
188
# Assert correct weights were revealed
200
189
assert weight_uids [0 ] == revealed_weights [0 ][0 ]
201
190
assert weight_vals [0 ] == revealed_weights [0 ][1 ]
202
191
192
+ logging .console .success (
193
+ f"Successfully revealed weights: uids { weight_uids } , weights { weight_vals } "
194
+ )
195
+
203
196
# 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 ) == []
205
198
206
199
# 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 , (
208
201
f"latest_drand_round ({ latest_drand_round } ) is less than expected_reveal_round ({ expected_reveal_round } )"
209
202
)
210
203
211
- logging .console .info ("✅ Passed commit_reveal v3 " )
204
+ logging .console .success ("✅ Passed `test_commit_and_reveal_weights_cr4` " )
0 commit comments