Skip to content

Commit fc47114

Browse files
Merge pull request #2768 from opentensor/fix/zyzniewski/test_set_weights
Fix E2E Tests: wait for new nonce
2 parents 7c62606 + 04e153a commit fc47114

File tree

3 files changed

+76
-46
lines changed

3 files changed

+76
-46
lines changed

tests/e2e_tests/test_commit_weights.py

+39-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from tests.e2e_tests.utils.chain_interactions import (
88
sudo_set_admin_utils,
99
sudo_set_hyperparameter_bool,
10+
use_and_wait_for_next_nonce,
1011
wait_epoch,
1112
)
1213

@@ -227,42 +228,44 @@ async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wall
227228
salt3[0] += 2 # Increment the first byte to produce a different commit hash
228229

229230
# Commit all three salts
230-
success, message = subtensor.commit_weights(
231-
alice_wallet,
232-
netuid,
233-
salt=salt,
234-
uids=weight_uids,
235-
weights=weight_vals,
236-
wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool
237-
wait_for_finalization=False,
238-
)
239-
240-
assert success is True
241-
242-
success, message = subtensor.commit_weights(
243-
alice_wallet,
244-
netuid,
245-
salt=salt2,
246-
uids=weight_uids,
247-
weights=weight_vals,
248-
wait_for_inclusion=False,
249-
wait_for_finalization=False,
250-
)
251-
252-
assert success is True
253-
254-
# Commit the third salt
255-
success, message = subtensor.commit_weights(
256-
alice_wallet,
257-
netuid,
258-
salt=salt3,
259-
uids=weight_uids,
260-
weights=weight_vals,
261-
wait_for_inclusion=False,
262-
wait_for_finalization=False,
263-
)
264-
265-
assert success is True
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,
240+
)
241+
242+
assert success is True
243+
244+
async with use_and_wait_for_next_nonce(subtensor, alice_wallet):
245+
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+
)
254+
255+
assert success is True
256+
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+
)
267+
268+
assert success is True
266269

267270
# Wait a few blocks
268271
await asyncio.sleep(10) # Wait for the txs to be included in the chain

tests/e2e_tests/test_set_weights.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from tests.e2e_tests.utils.chain_interactions import (
77
sudo_set_hyperparameter_bool,
88
sudo_set_admin_utils,
9+
use_and_wait_for_next_nonce,
910
wait_epoch,
1011
)
1112

@@ -108,16 +109,17 @@ async def test_set_weights_uses_next_nonce(local_chain, subtensor, alice_wallet)
108109

109110
# Set weights for each subnet
110111
for netuid in netuids:
111-
success, message = subtensor.set_weights(
112-
alice_wallet,
113-
netuid,
114-
uids=weight_uids,
115-
weights=weight_vals,
116-
wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool
117-
wait_for_finalization=False,
118-
)
119-
120-
assert success is True, f"Failed to set weights for subnet {netuid}"
112+
async with use_and_wait_for_next_nonce(subtensor, alice_wallet):
113+
success, message = subtensor.set_weights(
114+
alice_wallet,
115+
netuid,
116+
uids=weight_uids,
117+
weights=weight_vals,
118+
wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool
119+
wait_for_finalization=False,
120+
)
121+
122+
assert success is True, message
121123

122124
# Wait for the txs to be included in the chain
123125
await wait_epoch(subtensor, netuid=netuids[-1], times=4)

tests/e2e_tests/utils/chain_interactions.py

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import asyncio
7+
import contextlib
78
import unittest.mock
89
from typing import Union, Optional, TYPE_CHECKING
910

@@ -150,6 +151,30 @@ async def wait_interval(
150151
)
151152

152153

154+
@contextlib.asynccontextmanager
155+
async def use_and_wait_for_next_nonce(
156+
subtensor: "Subtensor",
157+
wallet: "Wallet",
158+
sleep: float = 0.25,
159+
timeout: float = 15.0,
160+
):
161+
"""
162+
ContextManager that makes sure the Nonce has been consumed after sending Extrinsic.
163+
"""
164+
165+
nonce = subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address)
166+
167+
yield
168+
169+
async def wait_for_new_nonce():
170+
while nonce == subtensor.substrate.get_account_next_index(
171+
wallet.hotkey.ss58_address
172+
):
173+
await asyncio.sleep(sleep)
174+
175+
await asyncio.wait_for(wait_for_new_nonce(), timeout)
176+
177+
153178
# Helper to execute sudo wrapped calls on the chain
154179
def sudo_set_admin_utils(
155180
substrate: "SubstrateInterface",

0 commit comments

Comments
 (0)