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

chore(tree): make remove_before bound inclusive #10559

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
17 changes: 9 additions & 8 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ impl TreeState {
true
}

/// Remove all blocks up to the given block number.
pub(crate) fn remove_before(&mut self, upper_bound: Bound<BlockNumber>) {
/// Remove all blocks up to __and including__ the given block number.
pub(crate) fn remove_before(&mut self, upper_bound: BlockNumber) {
let mut numbers_to_remove = Vec::new();
for (&number, _) in self.blocks_by_number.range((Bound::Unbounded, upper_bound)) {
for (&number, _) in
self.blocks_by_number.range((Bound::Unbounded, Bound::Included(upper_bound)))
{
numbers_to_remove.push(number);
}

Expand Down Expand Up @@ -1019,7 +1021,7 @@ where

// state house keeping after backfill sync
// remove all executed blocks below the backfill height
self.state.tree_state.remove_before(Bound::Included(backfill_height));
self.state.tree_state.remove_before(backfill_height);
self.metrics.executed_blocks.set(self.state.tree_state.block_count() as f64);

// remove all buffered blocks below the backfill height
Expand Down Expand Up @@ -1193,9 +1195,7 @@ where
///
/// Assumes that `finish` has been called on the `persistence_state` at least once
fn on_new_persisted_block(&mut self) {
self.state
.tree_state
.remove_before(Bound::Included(self.persistence_state.last_persisted_block_number));
self.state.tree_state.remove_before(self.persistence_state.last_persisted_block_number);
self.canonical_in_memory_state
.remove_persisted_blocks(self.persistence_state.last_persisted_block_number);
}
Expand Down Expand Up @@ -2656,7 +2656,8 @@ mod tests {
tree_state.insert_executed(block.clone());
}

tree_state.remove_before(Bound::Excluded(3));
// inclusive bound, so we should remove anything up to and including 2
tree_state.remove_before(2);

assert!(!tree_state.blocks_by_hash.contains_key(&blocks[0].block.hash()));
assert!(!tree_state.blocks_by_hash.contains_key(&blocks[1].block.hash()));
Expand Down
Loading