Skip to content

Commit 8edef50

Browse files
Merge pull request #12 from opentensor/spiigot/fix-epoch-jump
fix epoch jump when epoch_block is close
2 parents 51a3c28 + 0c799c2 commit 8edef50

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,19 @@ async fn generate_commit(
5858
// Calculate reveal epoch and ensure enough time for SUBTENSOR_PULSE_DELAY pulses
5959
let mut reveal_epoch = current_epoch + subnet_reveal_period_epochs;
6060
let mut reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
61-
let mut blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
61+
let mut blocks_until_reveal = reveal_block_number - current_block;
6262
let mut time_until_reveal = blocks_until_reveal * block_time;
6363

6464
// Ensure at least SUBTENSOR_PULSE_DELAY * period seconds lead time
6565
while time_until_reveal < SUBTENSOR_PULSE_DELAY * period {
66+
67+
if blocks_until_reveal > 1 {
68+
break;
69+
}
70+
6671
reveal_epoch += 1;
6772
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
68-
blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
73+
blocks_until_reveal = reveal_block_number - current_block;
6974
time_until_reveal = blocks_until_reveal * block_time;
7075
}
7176

src/tests/test_commit_reveal.py

+14-20
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@ async def test_generate_commit_various_tempos():
143143
abs(reveal_round - expected_reveal_round) <= 1
144144
), f"Tempo {tempo}: reveal_round {reveal_round} not close to expected {expected_reveal_round}"
145145

146-
computed_reveal_time = (
147-
GENESIS_TIME + (reveal_round + SUBTENSOR_PULSE_DELAY) * PERIOD
148-
)
149-
required_lead_time = SUBTENSOR_PULSE_DELAY * PERIOD
146+
computed_reveal_time = (
147+
GENESIS_TIME + (reveal_round + SUBTENSOR_PULSE_DELAY) * PERIOD
148+
)
149+
required_lead_time = SUBTENSOR_PULSE_DELAY * PERIOD
150150

151+
if time_until_reveal >= required_lead_time:
151152
assert computed_reveal_time - start_time >= required_lead_time, (
152-
f"Tempo {tempo}: Not enough lead time: reveal_time={computed_reveal_time}, "
153+
f"Not enough lead time: reveal_time={computed_reveal_time}, "
153154
f"start_time={start_time}, required={required_lead_time}"
154155
)
156+
else:
157+
pass
155158

156-
assert (
157-
time_until_reveal >= SUBTENSOR_PULSE_DELAY * PERIOD
158-
), f"Tempo {tempo}: time_until_reveal {time_until_reveal} is less than required {SUBTENSOR_PULSE_DELAY * PERIOD}"
159159

160160

161161
def compute_expected_reveal_round(
@@ -171,27 +171,21 @@ def compute_expected_reveal_round(
171171
block_with_offset = current_block + netuid_plus_one
172172
current_epoch = block_with_offset // tempo_plus_one
173173

174-
# Initial guess for reveal_epoch
175174
reveal_epoch = current_epoch + subnet_reveal_period_epochs
176175
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one
177176

178-
# Compute blocks_until_reveal, ensure non-negative
179-
blocks_until_reveal = reveal_block_number - current_block
180-
if blocks_until_reveal < 0:
181-
blocks_until_reveal = 0
177+
blocks_until_reveal = max(reveal_block_number - current_block, 0)
182178
time_until_reveal = blocks_until_reveal * block_time
183179

184-
# Adjust until we have enough lead time (at least SUBTENSOR_PULSE_DELAY pulses * period seconds)
185180
while time_until_reveal < SUBTENSOR_PULSE_DELAY * PERIOD:
181+
# If there's at least one block until the reveal, break early and don't force more lead time
182+
if blocks_until_reveal > 0:
183+
break
186184
reveal_epoch += 1
187185
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one
188-
blocks_until_reveal = reveal_block_number - current_block
189-
if blocks_until_reveal < 0:
190-
blocks_until_reveal = 0
186+
blocks_until_reveal = max(reveal_block_number - current_block, 0)
191187
time_until_reveal = blocks_until_reveal * block_time
192188

193189
reveal_time = now + time_until_reveal
194-
reveal_round = (
195-
(reveal_time - GENESIS_TIME + PERIOD - 1) // PERIOD
196-
) - SUBTENSOR_PULSE_DELAY
190+
reveal_round = ((reveal_time - GENESIS_TIME + PERIOD - 1) // PERIOD) - SUBTENSOR_PULSE_DELAY
197191
return reveal_round, reveal_time, time_until_reveal

0 commit comments

Comments
 (0)