Skip to content

Commit

Permalink
chore(tree): make remove_before bound inclusive (#10559)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Aug 27, 2024
1 parent 16577e0 commit b1d6e27
Showing 1 changed file with 9 additions and 8 deletions.
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

0 comments on commit b1d6e27

Please sign in to comment.