1- import asyncio
2-
31import numpy as np
42import pytest
3+ import retry
54
5+ from bittensor .utils .btlogging import logging
66from bittensor .utils .weight_utils import convert_weights_and_uids_for_emit
77from tests .e2e_tests .utils .chain_interactions import (
88 sudo_set_admin_utils ,
99 sudo_set_hyperparameter_bool ,
10- use_and_wait_for_next_nonce ,
10+ execute_and_wait_for_next_nonce ,
1111 wait_epoch ,
1212)
1313
@@ -165,17 +165,30 @@ async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wall
165165 Raises:
166166 AssertionError: If any of the checks or verifications fail
167167 """
168+ subnet_tempo = 50
169+ netuid = 2
170+
168171 # Wait for 2 tempos to pass as CR3 only reveals weights after 2 tempos
169- subtensor .wait_for_block (20 )
172+ subtensor .wait_for_block (subnet_tempo * 2 + 1 )
170173
171- netuid = 2
172174 print ("Testing test_commit_and_reveal_weights" )
173175 # Register root as Alice
174176 assert subtensor .register_subnet (alice_wallet ), "Unable to register the subnet"
175177
176178 # Verify subnet 1 created successfully
177179 assert subtensor .subnet_exists (netuid ), "Subnet wasn't created successfully"
178180
181+ # weights sensitive to epoch changes
182+ assert sudo_set_admin_utils (
183+ local_chain ,
184+ alice_wallet ,
185+ call_function = "sudo_set_tempo" ,
186+ call_params = {
187+ "netuid" : netuid ,
188+ "tempo" : subnet_tempo ,
189+ },
190+ )
191+
179192 # Enable commit_reveal on the subnet
180193 assert sudo_set_hyperparameter_bool (
181194 local_chain ,
@@ -203,72 +216,67 @@ async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wall
203216 call_params = {"netuid" : netuid , "weights_set_rate_limit" : "0" },
204217 )
205218
206- assert error is None
207- assert status is True
219+ assert error is None and status is True , f"Failed to set rate limit: { error } "
208220
209221 assert (
210222 subtensor .get_subnet_hyperparameters (netuid = netuid ).weights_rate_limit == 0
211223 ), "Failed to set weights_rate_limit"
212224 assert subtensor .weights_rate_limit (netuid = netuid ) == 0
213225
214- # Commit-reveal values
215- uids = np .array ([0 ], dtype = np .int64 )
216- weights = np .array ([0.1 ], dtype = np .float32 )
217- salt = [18 , 179 , 107 , 0 , 165 , 211 , 141 , 197 ]
218- weight_uids , weight_vals = convert_weights_and_uids_for_emit (
219- uids = uids , weights = weights
220- )
221-
222- # Make a second salt
223- salt2 = salt .copy ()
224- salt2 [0 ] += 1 # Increment the first byte to produce a different commit hash
225-
226- # Make a third salt
227- salt3 = salt .copy ()
228- salt3 [0 ] += 2 # Increment the first byte to produce a different commit hash
229-
230- # Commit all three salts
231- async with use_and_wait_for_next_nonce (subtensor , alice_wallet ):
232- success , message = subtensor .commit_weights (
233- alice_wallet ,
234- netuid ,
235- salt = salt ,
236- uids = weight_uids ,
237- weights = weight_vals ,
238- wait_for_inclusion = False , # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool
239- wait_for_finalization = False ,
226+ # wait while weights_rate_limit changes applied.
227+ subtensor .wait_for_block (subnet_tempo + 1 )
228+
229+ # create different commited data to avoid coming into pool black list with the error
230+ # Failed to commit weights: Subtensor returned `Custom type(1012)` error. This means: `Transaction is temporarily
231+ # banned`.Failed to commit weights: Subtensor returned `Custom type(1012)` error. This means: `Transaction is
232+ # temporarily banned`.`
233+ def get_weights_and_salt (counter : int ):
234+ # Commit-reveal values
235+ salt_ = [18 , 179 , 107 , counter , 165 , 211 , 141 , 197 ]
236+ uids_ = np .array ([0 ], dtype = np .int64 )
237+ weights_ = np .array ([counter / 10 ], dtype = np .float32 )
238+ weight_uids_ , weight_vals_ = convert_weights_and_uids_for_emit (
239+ uids = uids_ , weights = weights_
240240 )
241+ return salt_ , weight_uids_ , weight_vals_
241242
242- assert success is True
243+ logging .console .info (
244+ f"[orange]Nonce before first commit_weights: "
245+ f"{ subtensor .substrate .get_account_next_index (alice_wallet .hotkey .ss58_address )} [/orange]"
246+ )
243247
244- async with use_and_wait_for_next_nonce (subtensor , alice_wallet ):
248+ # 3 time doing call if nonce wasn't updated, then raise error
249+ @retry .retry (exceptions = Exception , tries = 3 , delay = 1 )
250+ @execute_and_wait_for_next_nonce (subtensor = subtensor , wallet = alice_wallet )
251+ def send_commit (salt_ , weight_uids_ , weight_vals_ ):
245252 success , message = subtensor .commit_weights (
246- alice_wallet ,
247- netuid ,
248- salt = salt2 ,
249- uids = weight_uids ,
250- weights = weight_vals ,
251- wait_for_inclusion = False ,
252- wait_for_finalization = False ,
253+ wallet = alice_wallet ,
254+ netuid = netuid ,
255+ salt = salt_ ,
256+ uids = weight_uids_ ,
257+ weights = weight_vals_ ,
258+ wait_for_inclusion = True ,
259+ wait_for_finalization = True ,
253260 )
261+ assert success is True , message
254262
255- assert success is True
263+ # send some amount of commit weights
264+ AMOUNT_OF_COMMIT_WEIGHTS = 3
265+ for call in range (AMOUNT_OF_COMMIT_WEIGHTS ):
266+ weight_uids , weight_vals , salt = get_weights_and_salt (call )
256267
257- async with use_and_wait_for_next_nonce (subtensor , alice_wallet ):
258- success , message = subtensor .commit_weights (
259- alice_wallet ,
260- netuid ,
261- salt = salt3 ,
262- uids = weight_uids ,
263- weights = weight_vals ,
264- wait_for_inclusion = False ,
265- wait_for_finalization = False ,
266- )
268+ send_commit (salt , weight_uids , weight_vals )
267269
268- assert success is True
270+ # let's wait for 3 (12 fast blocks) seconds between transactions
271+ subtensor .wait_for_block (subtensor .block + 12 )
272+
273+ logging .console .info (
274+ f"[orange]Nonce after third commit_weights: "
275+ f"{ subtensor .substrate .get_account_next_index (alice_wallet .hotkey .ss58_address )} [/orange]"
276+ )
269277
270278 # Wait a few blocks
271- await asyncio . sleep ( 10 ) # Wait for the txs to be included in the chain
279+ subtensor . wait_for_block ( subtensor . block + subtensor . tempo ( netuid ) * 2 )
272280
273281 # Query the WeightCommits storage map for all three salts
274282 weight_commits = subtensor .query_module (
@@ -282,4 +290,6 @@ async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wall
282290 assert commit_block > 0 , f"Invalid block number: { commit_block } "
283291
284292 # Check for three commits in the WeightCommits storage map
285- assert len (weight_commits .value ) == 3 , "Expected 3 weight commits"
293+ assert (
294+ len (weight_commits .value ) == AMOUNT_OF_COMMIT_WEIGHTS
295+ ), "Expected exact list of weight commits"
0 commit comments