Skip to content

Commit b56b4b1

Browse files
committed
support all tempos
1 parent efaa176 commit b56b4b1

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/lib.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,39 @@ async fn generate_commit(
5454
.unwrap()
5555
.as_secs();
5656

57-
// Compute the current epoch index
57+
// Compute current epoch
5858
let tempo_plus_one = tempo + 1;
5959
let netuid_plus_one = (netuid as u64) + 1;
6060
let block_with_offset = current_block + netuid_plus_one;
61+
let current_epoch = block_with_offset / tempo_plus_one;
6162

62-
// If at an exact epoch boundary, treat this as the new epoch start.
63-
let is_epoch_boundary = (block_with_offset % tempo_plus_one) == 0;
64-
let base_epoch = block_with_offset / tempo_plus_one;
65-
let current_epoch = if is_epoch_boundary {
66-
base_epoch + 1
67-
} else {
68-
base_epoch
69-
};
63+
// Compute initial reveal epoch
64+
let mut reveal_epoch = current_epoch + subnet_reveal_period_epochs;
7065

71-
// Compute the reveal epoch
72-
let reveal_epoch = current_epoch + subnet_reveal_period_epochs;
66+
// Compute the block when the reveal epoch starts
67+
let mut reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
7368

74-
// Compute the block number when the reveal epoch starts
75-
let reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
69+
// Compute how many blocks until reveal
70+
let mut blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
7671

77-
// Compute the number of blocks until the reveal epoch
78-
let blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
72+
// Compute time until reveal in seconds
73+
let mut time_until_reveal = blocks_until_reveal * block_time;
7974

80-
// Compute the time until the reveal in seconds
81-
let time_until_reveal = blocks_until_reveal * block_time;
75+
// Since SUBTENSOR_PULSE_DELAY is in pulses, we must ensure that we have at least
76+
// SUBTENSOR_PULSE_DELAY pulses worth of time before reveal.
77+
// Each pulse is 'period' seconds, so we need at least SUBTENSOR_PULSE_DELAY * period seconds.
78+
while time_until_reveal < SUBTENSOR_PULSE_DELAY * period {
79+
// Not enough lead time, push to next epoch
80+
reveal_epoch += 1;
81+
reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
82+
blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
83+
time_until_reveal = blocks_until_reveal * block_time;
84+
}
8285

8386
// Compute the reveal time in seconds since UNIX_EPOCH
8487
let reveal_time = now + time_until_reveal;
8588

86-
// Compute the reveal round, ensuring we round up
89+
// Compute the reveal round, ensure we pick an older Drand round by subtracting SUBTENSOR_PULSE_DELAY pulses.
8790
let reveal_round = ((reveal_time - genesis_time + period - 1) / period) - SUBTENSOR_PULSE_DELAY;
8891

8992
// 3. Deserialize the public key

0 commit comments

Comments
 (0)