Skip to content

Commit d3ed298

Browse files
author
Roman
committed
update e2e test
1 parent 1ab7491 commit d3ed298

File tree

1 file changed

+52
-59
lines changed

1 file changed

+52
-59
lines changed

tests/e2e_tests/test_commit_reveal_v3.py

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# @pytest.mark.parametrize("local_chain", [True], indirect=True)
1717
@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):
1919
"""
2020
Tests the commit/reveal weights mechanism (CR3)
2121
@@ -30,56 +30,64 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
3030
Raises:
3131
AssertionError: If any of the checks or verifications fail
3232
"""
33+
logging.console.info("Testing `test_commit_and_reveal_weights_cr4`")
34+
3335
BLOCK_TIME = (
3436
0.25 if subtensor.is_fast_blocks() else 12.0
3537
) # 12 for non-fast-block, 0.25 for fast block
36-
netuid = subtensor.get_total_subnets() # 2
3738

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
3942

4043
# Register root as Alice
4144
assert subtensor.register_subnet(alice_wallet), "Unable to register the subnet"
4245

4346
# 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"
4748

48-
logging.console.success(f"Subnet {netuid} is registered")
49+
logging.console.success(f"SN #{netuid} is registered.")
4950

5051
# Enable commit_reveal on the subnet
5152
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}"
5859

5960
# 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}"
6270

6371
# Change the weights rate limit on the subnet
6472
status, error = sudo_set_admin_utils(
65-
local_chain,
66-
alice_wallet,
73+
substrate=local_chain,
74+
wallet=alice_wallet,
6775
call_function="sudo_set_weights_set_rate_limit",
6876
call_params={"netuid": netuid, "weights_set_rate_limit": "0"},
6977
)
7078

71-
assert error is None
7279
assert status is True
80+
assert error is None
7381

7482
# Verify weights rate limit was changed
7583
assert (
7684
subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0
7785
), "Failed to set weights_rate_limit"
7886
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")
8088

8189
# 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
8391
assert (
8492
sudo_set_admin_utils(
8593
local_chain,
@@ -89,9 +97,10 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
8997
)[0]
9098
is True
9199
)
100+
92101
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}")
95104

96105
# Commit-reveal values - setting weights to self
97106
uids = np.array([0], dtype=np.int64)
@@ -107,19 +116,16 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
107116
f"Checking if window is too low with Current block: {current_block}, next tempo: {upcoming_tempo}"
108117
)
109118

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-
113119
# 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:
115121
await wait_interval(
116122
tempo,
117123
subtensor,
118124
netuid=netuid,
119125
reporting_interval=1,
120126
)
121127
current_block = subtensor.get_current_block()
122-
expected_block = current_block
128+
expected_commit_block = current_block + 1
123129
latest_drand_round = subtensor.last_drand_round()
124130
upcoming_tempo = next_tempo(current_block, tempo)
125131
logging.console.info(
@@ -139,73 +145,60 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
139145
)
140146

141147
# Assert committing was a success
142-
assert success is True
148+
assert success is True, message
143149
assert bool(re.match(r"reveal_round:\d+", message))
144150

145151
# Parse expected reveal_round
146152
expected_reveal_round = int(message.split(":")[1])
147-
logging.console.info(
153+
logging.console.success(
148154
f"Successfully set weights: uids {weight_uids}, weights {weight_vals}, reveal_round: {expected_reveal_round}"
149155
)
150156

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-
163157
# Fetch current commits pending on the chain
164158
commits_on_chain = subtensor.get_current_weight_commit_info_v2(netuid=netuid)
165159
address, commit_block, commit, reveal_round = commits_on_chain[0]
166160

167161
# Assert correct values are committed on the chain
168162
assert expected_reveal_round == reveal_round
169163
assert address == alice_wallet.hotkey.ss58_address
170-
assert commit_block == expected_block + 1
164+
assert commit_block == expected_commit_block + 1
171165

172166
# Ensure no weights are available as of now
173167
assert subtensor.weights(netuid=netuid) == []
168+
logging.console.success("No weights are available before next epoch.")
174169

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}"
182175
)
176+
subtensor.wait_for_block(expected_reveal_block)
183177

184178
# Fetch the latest drand pulse
185179
latest_drand_round = subtensor.last_drand_round()
186180
logging.console.info(
187181
f"Latest drand round after waiting for tempo: {latest_drand_round}"
188182
)
189183

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-
194184
# 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)
196186

197-
print("revealed weights", revealed_weights_)
198-
revealed_weights = revealed_weights_[0][1]
187+
revealed_weights = subnet_weights[0][1]
199188
# Assert correct weights were revealed
200189
assert weight_uids[0] == revealed_weights[0][0]
201190
assert weight_vals[0] == revealed_weights[0][1]
202191

192+
logging.console.success(
193+
f"Successfully revealed weights: uids {weight_uids}, weights {weight_vals}"
194+
)
195+
203196
# 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) == []
205198

206199
# 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, (
208201
f"latest_drand_round ({latest_drand_round}) is less than expected_reveal_round ({expected_reveal_round})"
209202
)
210203

211-
logging.console.info("✅ Passed commit_reveal v3")
204+
logging.console.success("✅ Passed `test_commit_and_reveal_weights_cr4`")

0 commit comments

Comments
 (0)