Skip to content

Commit 0c1c998

Browse files
authored
Merge commit from fork
The epoch duration subtraction in get_block_epoch was unchecked. A miner can craft a chain where the previous epoch tail has a higher timestamp than the next epoch tail (consensus-valid since only median-time is enforced, not monotonicity). The unchecked subtraction then underflows in release builds (overflow-checks = true), causing a panic. Using saturating_sub yields 0 for non-monotonic timestamps, which the downstream next_epoch_ext already clamps to at least 1 second via cmp::max(epoch_duration / 1000, 1).
1 parent 8f6cacf commit 0c1c998

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

traits/src/epoch_provider.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ pub trait EpochProvider {
3333
.get_block_ext(&last_block_hash_in_previous_epoch)
3434
.expect("stored block ext")
3535
.total_uncles_count;
36-
let epoch_duration_in_milliseconds = header.timestamp()
37-
- self
38-
.get_block_header(&last_block_hash_in_previous_epoch)
36+
let epoch_duration_in_milliseconds = header.timestamp().saturating_sub(
37+
self.get_block_header(&last_block_hash_in_previous_epoch)
3938
.expect("stored block header")
40-
.timestamp();
39+
.timestamp(),
40+
);
4141

4242
BlockEpoch::TailBlock {
4343
epoch,

0 commit comments

Comments
 (0)