|
1 | | -from bittensor.core import subtensor as subtensor_module |
2 | | -from bittensor.core.chain_data import SubnetHyperparameters |
3 | | -from bittensor.core.subtensor import Subtensor |
4 | 1 | from bittensor.core.extrinsics import commit_reveal |
5 | | -import pytest |
6 | | -import torch |
7 | | -import numpy as np |
8 | 2 |
|
9 | 3 |
|
10 | | -@pytest.fixture |
11 | | -def subtensor(mocker): |
12 | | - fake_substrate = mocker.MagicMock() |
13 | | - fake_substrate.websocket.sock.getsockopt.return_value = 0 |
14 | | - mocker.patch.object( |
15 | | - subtensor_module, "SubstrateInterface", return_value=fake_substrate |
16 | | - ) |
17 | | - yield Subtensor() |
18 | | - |
19 | | - |
20 | | -@pytest.fixture |
21 | | -def hyperparams(): |
22 | | - yield SubnetHyperparameters( |
23 | | - rho=0, |
24 | | - kappa=0, |
25 | | - immunity_period=0, |
26 | | - min_allowed_weights=0, |
27 | | - max_weight_limit=0.0, |
28 | | - tempo=0, |
29 | | - min_difficulty=0, |
30 | | - max_difficulty=0, |
31 | | - weights_version=0, |
32 | | - weights_rate_limit=0, |
33 | | - adjustment_interval=0, |
34 | | - activity_cutoff=0, |
35 | | - registration_allowed=False, |
36 | | - target_regs_per_interval=0, |
37 | | - min_burn=0, |
38 | | - max_burn=0, |
39 | | - bonds_moving_avg=0, |
40 | | - max_regs_per_block=0, |
41 | | - serving_rate_limit=0, |
42 | | - max_validators=0, |
43 | | - adjustment_alpha=0, |
44 | | - difficulty=0, |
45 | | - commit_reveal_weights_interval=0, |
46 | | - commit_reveal_weights_enabled=True, |
47 | | - alpha_high=0, |
48 | | - alpha_low=0, |
49 | | - liquid_alpha_enabled=False, |
50 | | - ) |
51 | | - |
52 | | - |
53 | | -def test_do_commit_reveal_v3_success(mocker, subtensor): |
54 | | - """Test successful commit-reveal with wait for finalization.""" |
55 | | - # Preps |
56 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
57 | | - fake_netuid = 1 |
58 | | - fake_commit = b"fake_commit" |
59 | | - fake_reveal_round = 1 |
60 | | - |
61 | | - mocked_compose_call = mocker.patch.object(subtensor.substrate, "compose_call") |
62 | | - mocked_create_signed_extrinsic = mocker.patch.object( |
63 | | - subtensor.substrate, "create_signed_extrinsic" |
64 | | - ) |
65 | | - mocked_submit_extrinsic = mocker.patch.object(commit_reveal, "submit_extrinsic") |
66 | | - |
67 | | - # Call |
68 | | - result = commit_reveal._do_commit_reveal_v3( |
69 | | - self=subtensor, |
70 | | - wallet=fake_wallet, |
71 | | - netuid=fake_netuid, |
72 | | - commit=fake_commit, |
73 | | - reveal_round=fake_reveal_round, |
74 | | - ) |
75 | | - |
76 | | - # Asserts |
77 | | - mocked_compose_call.assert_called_once_with( |
78 | | - call_module="SubtensorModule", |
79 | | - call_function="commit_crv3_weights", |
80 | | - call_params={ |
81 | | - "netuid": fake_netuid, |
82 | | - "commit": fake_commit, |
83 | | - "reveal_round": fake_reveal_round, |
84 | | - }, |
85 | | - ) |
86 | | - mocked_create_signed_extrinsic.assert_called_once_with( |
87 | | - call=mocked_compose_call.return_value, keypair=fake_wallet.hotkey |
88 | | - ) |
89 | | - mocked_submit_extrinsic.assert_called_once_with( |
90 | | - subtensor=subtensor, |
91 | | - extrinsic=mocked_create_signed_extrinsic.return_value, |
92 | | - wait_for_inclusion=False, |
93 | | - wait_for_finalization=False, |
94 | | - ) |
95 | | - assert result == (True, "Not waiting for finalization or inclusion.") |
96 | | - |
97 | | - |
98 | | -def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): |
99 | | - """Test commit-reveal fails due to an error in submission.""" |
100 | | - # Preps |
101 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
102 | | - fake_netuid = 1 |
103 | | - fake_commit = b"fake_commit" |
104 | | - fake_reveal_round = 1 |
105 | | - |
106 | | - mocked_compose_call = mocker.patch.object(subtensor.substrate, "compose_call") |
107 | | - mocked_create_signed_extrinsic = mocker.patch.object( |
108 | | - subtensor.substrate, "create_signed_extrinsic" |
109 | | - ) |
110 | | - mocked_submit_extrinsic = mocker.patch.object( |
111 | | - commit_reveal, |
112 | | - "submit_extrinsic", |
113 | | - return_value=mocker.Mock(is_success=False, error_message="Mocked error"), |
114 | | - ) |
115 | | - mocked_format_error_message = mocker.patch.object( |
116 | | - commit_reveal, "format_error_message", return_value="Formatted error" |
117 | | - ) |
118 | | - |
119 | | - # Call |
120 | | - result = commit_reveal._do_commit_reveal_v3( |
121 | | - self=subtensor, |
122 | | - wallet=fake_wallet, |
123 | | - netuid=fake_netuid, |
124 | | - commit=fake_commit, |
125 | | - reveal_round=fake_reveal_round, |
126 | | - wait_for_inclusion=True, |
127 | | - wait_for_finalization=True, |
128 | | - ) |
129 | | - |
130 | | - # Asserts |
131 | | - mocked_compose_call.assert_called_once_with( |
132 | | - call_module="SubtensorModule", |
133 | | - call_function="commit_crv3_weights", |
134 | | - call_params={ |
135 | | - "netuid": fake_netuid, |
136 | | - "commit": fake_commit, |
137 | | - "reveal_round": fake_reveal_round, |
138 | | - }, |
139 | | - ) |
140 | | - mocked_create_signed_extrinsic.assert_called_once_with( |
141 | | - call=mocked_compose_call.return_value, keypair=fake_wallet.hotkey |
142 | | - ) |
143 | | - mocked_submit_extrinsic.assert_called_once_with( |
144 | | - subtensor=subtensor, |
145 | | - extrinsic=mocked_create_signed_extrinsic.return_value, |
146 | | - wait_for_inclusion=True, |
147 | | - wait_for_finalization=True, |
148 | | - ) |
149 | | - mocked_format_error_message.assert_called_once_with("Mocked error") |
150 | | - assert result == (False, "Formatted error") |
151 | | - |
152 | | - |
153 | | -def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperparams): |
154 | | - """Test successful commit-reveal with torch tensors.""" |
155 | | - # Preps |
156 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
157 | | - fake_netuid = 1 |
158 | | - fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) |
159 | | - fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) |
160 | | - fake_commit_for_reveal = b"mock_commit_for_reveal" |
161 | | - fake_reveal_round = 1 |
162 | | - |
163 | | - # Mocks |
164 | | - |
165 | | - mocked_uids = mocker.Mock() |
166 | | - mocked_weights = mocker.Mock() |
167 | | - mocked_convert_weights_and_uids_for_emit = mocker.patch.object( |
168 | | - commit_reveal, |
169 | | - "convert_weights_and_uids_for_emit", |
170 | | - return_value=(mocked_uids, mocked_weights), |
171 | | - ) |
172 | | - mocked_get_subnet_reveal_period_epochs = mocker.patch.object( |
173 | | - subtensor, "get_subnet_reveal_period_epochs" |
174 | | - ) |
175 | | - mocked_get_encrypted_commit = mocker.patch.object( |
176 | | - commit_reveal, |
177 | | - "get_encrypted_commit", |
178 | | - return_value=(fake_commit_for_reveal, fake_reveal_round), |
179 | | - ) |
180 | | - mock_do_commit_reveal_v3 = mocker.patch.object( |
181 | | - commit_reveal, "_do_commit_reveal_v3", return_value=(True, "Success") |
182 | | - ) |
183 | | - mock_block = mocker.patch.object(subtensor, "get_current_block", return_value=1) |
184 | | - mock_hyperparams = mocker.patch.object( |
185 | | - subtensor, |
186 | | - "get_subnet_hyperparameters", |
187 | | - return_value=hyperparams, |
188 | | - ) |
189 | | - |
190 | | - # Call |
191 | | - success, message = commit_reveal.commit_reveal_v3_extrinsic( |
192 | | - subtensor=subtensor, |
193 | | - wallet=fake_wallet, |
194 | | - netuid=fake_netuid, |
195 | | - uids=fake_uids, |
196 | | - weights=fake_weights, |
197 | | - wait_for_inclusion=True, |
198 | | - wait_for_finalization=True, |
199 | | - ) |
200 | | - |
201 | | - # Asserts |
202 | | - assert success is True |
203 | | - assert message == "reveal_round:1" |
204 | | - mocked_convert_weights_and_uids_for_emit.assert_called_once_with( |
205 | | - fake_uids, fake_weights |
206 | | - ) |
207 | | - mocked_get_encrypted_commit.assert_called_once_with( |
208 | | - uids=mocked_uids, |
209 | | - weights=mocked_weights, |
210 | | - subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_weights_interval, |
211 | | - version_key=commit_reveal.version_as_int, |
212 | | - tempo=mock_hyperparams.return_value.tempo, |
213 | | - netuid=fake_netuid, |
214 | | - current_block=mock_block.return_value, |
215 | | - ) |
216 | | - mock_do_commit_reveal_v3.assert_called_once_with( |
217 | | - self=subtensor, |
218 | | - wallet=fake_wallet, |
219 | | - netuid=fake_netuid, |
220 | | - commit=fake_commit_for_reveal, |
221 | | - reveal_round=fake_reveal_round, |
222 | | - wait_for_inclusion=True, |
223 | | - wait_for_finalization=True, |
224 | | - ) |
225 | | - |
226 | | - |
227 | | -def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperparams): |
228 | | - """Test successful commit-reveal with numpy arrays.""" |
229 | | - # Preps |
230 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
231 | | - fake_netuid = 1 |
232 | | - fake_uids = np.array([1, 2, 3], dtype=np.int64) |
233 | | - fake_weights = np.array([0.1, 0.2, 0.7], dtype=np.float32) |
234 | | - |
235 | | - mock_convert = mocker.patch.object( |
236 | | - commit_reveal, |
237 | | - "convert_weights_and_uids_for_emit", |
238 | | - return_value=(fake_uids, fake_weights), |
239 | | - ) |
240 | | - mock_encode_drand = mocker.patch.object( |
241 | | - commit_reveal, "get_encrypted_commit", return_value=(b"commit", 0) |
242 | | - ) |
243 | | - mock_do_commit = mocker.patch.object( |
244 | | - commit_reveal, "_do_commit_reveal_v3", return_value=(True, "Committed!") |
245 | | - ) |
246 | | - mocker.patch.object(subtensor, "get_current_block", return_value=1) |
247 | | - mocker.patch.object( |
248 | | - subtensor, |
249 | | - "get_subnet_hyperparameters", |
250 | | - return_value=hyperparams, |
251 | | - ) |
252 | | - |
253 | | - # Call |
254 | | - success, message = commit_reveal.commit_reveal_v3_extrinsic( |
255 | | - subtensor=subtensor, |
256 | | - wallet=fake_wallet, |
257 | | - netuid=fake_netuid, |
258 | | - uids=fake_uids, |
259 | | - weights=fake_weights, |
260 | | - wait_for_inclusion=False, |
261 | | - wait_for_finalization=False, |
262 | | - ) |
263 | | - |
264 | | - # Asserts |
265 | | - assert success is True |
266 | | - assert message == "reveal_round:0" |
267 | | - mock_convert.assert_called_once_with(fake_uids, fake_weights) |
268 | | - mock_encode_drand.assert_called_once() |
269 | | - mock_do_commit.assert_called_once() |
270 | | - |
271 | | - |
272 | | -def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparams): |
273 | | - """Test unsuccessful commit-reveal with torch.""" |
| 4 | +def test_commit_reveal_v3_extrinsic(mocker): |
| 5 | + """"Verify that sync `commit_reveal_v3_extrinsic` method calls proper async method.""" |
274 | 6 | # Preps |
275 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
276 | | - fake_netuid = 1 |
277 | | - fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) |
278 | | - fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) |
279 | | - fake_commit_for_reveal = b"mock_commit_for_reveal" |
280 | | - fake_reveal_round = 1 |
281 | | - |
282 | | - # Mocks |
283 | | - mocker.patch.object( |
284 | | - commit_reveal, |
285 | | - "convert_weights_and_uids_for_emit", |
286 | | - return_value=(fake_uids, fake_weights), |
287 | | - ) |
288 | | - mocker.patch.object( |
289 | | - commit_reveal, |
290 | | - "get_encrypted_commit", |
291 | | - return_value=(fake_commit_for_reveal, fake_reveal_round), |
292 | | - ) |
293 | | - mock_do_commit_reveal_v3 = mocker.patch.object( |
294 | | - commit_reveal, "_do_commit_reveal_v3", return_value=(False, "Failed") |
295 | | - ) |
296 | | - mocker.patch.object(subtensor, "get_current_block", return_value=1) |
297 | | - mocker.patch.object( |
298 | | - subtensor, |
299 | | - "get_subnet_hyperparameters", |
300 | | - return_value=hyperparams, |
301 | | - ) |
| 7 | + fake_subtensor = mocker.Mock() |
| 8 | + fake_wallet = mocker.Mock() |
| 9 | + netuid = 1 |
| 10 | + uids = [1, 2, 3, 4] |
| 11 | + weights = [0.1, 0.2, 0.3, 0.4] |
| 12 | + version_key = 2 |
| 13 | + wait_for_inclusion = True |
| 14 | + wait_for_finalization = True |
| 15 | + |
| 16 | + mocked_execute_coroutine = mocker.patch.object(commit_reveal, "execute_coroutine") |
| 17 | + mocked_commit_reveal_v3_extrinsic = mocker.Mock() |
| 18 | + commit_reveal.async_commit_reveal_v3_extrinsic = mocked_commit_reveal_v3_extrinsic |
302 | 19 |
|
303 | 20 | # Call |
304 | | - success, message = commit_reveal.commit_reveal_v3_extrinsic( |
305 | | - subtensor=subtensor, |
| 21 | + result = commit_reveal.commit_reveal_v3_extrinsic( |
| 22 | + subtensor=fake_subtensor, |
306 | 23 | wallet=fake_wallet, |
307 | | - netuid=fake_netuid, |
308 | | - uids=fake_uids, |
309 | | - weights=fake_weights, |
310 | | - wait_for_inclusion=True, |
311 | | - wait_for_finalization=True, |
| 24 | + netuid=netuid, |
| 25 | + uids=uids, |
| 26 | + weights=weights, |
| 27 | + version_key=version_key, |
| 28 | + wait_for_inclusion=wait_for_inclusion, |
| 29 | + wait_for_finalization=wait_for_finalization |
312 | 30 | ) |
313 | 31 |
|
314 | 32 | # Asserts |
315 | | - assert success is False |
316 | | - assert message == "Failed" |
317 | | - mock_do_commit_reveal_v3.assert_called_once_with( |
318 | | - self=subtensor, |
319 | | - wallet=fake_wallet, |
320 | | - netuid=fake_netuid, |
321 | | - commit=fake_commit_for_reveal, |
322 | | - reveal_round=fake_reveal_round, |
323 | | - wait_for_inclusion=True, |
324 | | - wait_for_finalization=True, |
325 | | - ) |
326 | | - |
327 | | - |
328 | | -def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor): |
329 | | - """Test exception handling in commit-reveal.""" |
330 | | - # Preps |
331 | | - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) |
332 | | - fake_netuid = 1 |
333 | | - fake_uids = [1, 2, 3] |
334 | | - fake_weights = [0.1, 0.2, 0.7] |
335 | 33 |
|
336 | | - mocker.patch.object( |
337 | | - commit_reveal, |
338 | | - "convert_weights_and_uids_for_emit", |
339 | | - side_effect=Exception("Test Error"), |
| 34 | + mocked_execute_coroutine.assert_called_once_with( |
| 35 | + coroutine=mocked_commit_reveal_v3_extrinsic.return_value, |
| 36 | + event_loop=fake_subtensor.event_loop |
340 | 37 | ) |
341 | | - |
342 | | - # Call |
343 | | - success, message = commit_reveal.commit_reveal_v3_extrinsic( |
344 | | - subtensor=subtensor, |
| 38 | + mocked_commit_reveal_v3_extrinsic.assert_called_once_with( |
| 39 | + subtensor=fake_subtensor.async_subtensor, |
345 | 40 | wallet=fake_wallet, |
346 | | - netuid=fake_netuid, |
347 | | - uids=fake_uids, |
348 | | - weights=fake_weights, |
| 41 | + netuid=netuid, |
| 42 | + uids=uids, |
| 43 | + weights=weights, |
| 44 | + version_key=version_key, |
| 45 | + wait_for_inclusion=wait_for_inclusion, |
| 46 | + wait_for_finalization=wait_for_finalization |
349 | 47 | ) |
350 | | - |
351 | | - # Asserts |
352 | | - assert success is False |
353 | | - assert "Test Error" in message |
| 48 | + assert result == mocked_execute_coroutine.return_value |
0 commit comments