Skip to content

Commit

Permalink
Prevent weird slot drift
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Sep 9, 2024
1 parent 907a7c0 commit 024843e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions beacon_node/store/src/reconstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ where
let lower_limit_slot = anchor.state_lower_limit;
let upper_limit_slot = std::cmp::min(split.slot, anchor.state_upper_limit);

// If `num_blocks` is not specified iterate all blocks.
// If `num_blocks` is not specified iterate all blocks. Add 1 so that we end on an epoch
// boundary when `num_blocks` is a multiple of an epoch boundary. We want to be *inclusive*
// of the state at slot `lower_limit_slot + num_blocks`.
let block_root_iter = self
.forwards_block_roots_iterator_until(lower_limit_slot, upper_limit_slot - 1, || {
Err(Error::StateShouldNotBeRequired(upper_limit_slot - 1))
})?
.take(num_blocks.unwrap_or(usize::MAX));
.take(num_blocks.map_or(usize::MAX, |n| n + 1));

// The state to be advanced.
let mut state = self
Expand Down Expand Up @@ -104,12 +106,15 @@ where
// Stage state for storage in freezer DB.
self.store_cold_state(&state_root, &state, &mut io_batch)?;

let batch_complete = num_blocks.map_or(false, |n_blocks| {
slot + 1 == lower_limit_slot + n_blocks as u64
});
let batch_complete =
num_blocks.map_or(false, |n_blocks| slot == lower_limit_slot + n_blocks as u64);
let reconstruction_complete = slot + 1 == upper_limit_slot;

// If the slot lies on an epoch boundary, commit the batch and update the anchor.
// Commit the I/O batch if:
//
// - The diff/snapshot for this slot is required for future slots, or
// - The reconstruction batch is complete (we are about to return), or
// - Reconstruction is complete.
if self.hierarchy.should_commit_immediately(slot)?
|| batch_complete
|| reconstruction_complete
Expand Down

0 comments on commit 024843e

Please sign in to comment.