From c20737e282e1b6e28b67452d6c344321ce3a8e8f Mon Sep 17 00:00:00 2001 From: JohnReedV <87283488+JohnReedV@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:36:38 -0800 Subject: [PATCH] fix epoch jump when epoch_block is close --- src/lib.rs | 9 +++++++-- src/tests/test_commit_reveal.py | 34 ++++++++++++++------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0fe5eee..f964574 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 > 0 { + 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; } diff --git a/src/tests/test_commit_reveal.py b/src/tests/test_commit_reveal.py index fd07524..136ddff 100644 --- a/src/tests/test_commit_reveal.py +++ b/src/tests/test_commit_reveal.py @@ -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( @@ -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