Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix epoch jump when epoch_block is close #12

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ async fn generate_commit(
// Calculate reveal epoch and ensure enough time for SUBTENSOR_PULSE_DELAY pulses
let mut reveal_epoch = current_epoch + subnet_reveal_period_epochs;
let mut reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
let mut blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
let mut blocks_until_reveal = reveal_block_number - current_block;
let mut time_until_reveal = blocks_until_reveal * block_time;

// Ensure at least SUBTENSOR_PULSE_DELAY * period seconds lead time
while time_until_reveal < SUBTENSOR_PULSE_DELAY * period {

if blocks_until_reveal > 1 {
break;
}

reveal_epoch += 1;
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
blocks_until_reveal = reveal_block_number - current_block;
time_until_reveal = blocks_until_reveal * block_time;
}

Expand Down
34 changes: 14 additions & 20 deletions src/tests/test_commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ async def test_generate_commit_various_tempos():
abs(reveal_round - expected_reveal_round) <= 1
), f"Tempo {tempo}: reveal_round {reveal_round} not close to expected {expected_reveal_round}"

computed_reveal_time = (
GENESIS_TIME + (reveal_round + SUBTENSOR_PULSE_DELAY) * PERIOD
)
required_lead_time = SUBTENSOR_PULSE_DELAY * PERIOD
computed_reveal_time = (
GENESIS_TIME + (reveal_round + SUBTENSOR_PULSE_DELAY) * PERIOD
)
required_lead_time = SUBTENSOR_PULSE_DELAY * PERIOD

if time_until_reveal >= required_lead_time:
assert computed_reveal_time - start_time >= required_lead_time, (
f"Tempo {tempo}: Not enough lead time: reveal_time={computed_reveal_time}, "
f"Not enough lead time: reveal_time={computed_reveal_time}, "
f"start_time={start_time}, required={required_lead_time}"
)
else:
pass

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


def compute_expected_reveal_round(
Expand All @@ -171,27 +171,21 @@ def compute_expected_reveal_round(
block_with_offset = current_block + netuid_plus_one
current_epoch = block_with_offset // tempo_plus_one

# Initial guess for reveal_epoch
reveal_epoch = current_epoch + subnet_reveal_period_epochs
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one

# Compute blocks_until_reveal, ensure non-negative
blocks_until_reveal = reveal_block_number - current_block
if blocks_until_reveal < 0:
blocks_until_reveal = 0
blocks_until_reveal = max(reveal_block_number - current_block, 0)
time_until_reveal = blocks_until_reveal * block_time

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

reveal_time = now + time_until_reveal
reveal_round = (
(reveal_time - GENESIS_TIME + PERIOD - 1) // PERIOD
) - SUBTENSOR_PULSE_DELAY
reveal_round = ((reveal_time - GENESIS_TIME + PERIOD - 1) // PERIOD) - SUBTENSOR_PULSE_DELAY
return reveal_round, reveal_time, time_until_reveal
Loading