Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3464,7 +3464,13 @@ where

{
// Unwind the storage back to the common ancestor first
self.blockchain.storage.write().unwind_to(common_block.header.number(), hash);
let removed_blocks =
self.blockchain.storage.write().unwind_to(common_block.header.number(), hash);

// Clean up in-memory and on-disk states for removed blocks
let removed_hashes: Vec<_> =
removed_blocks.iter().map(|b| b.header.hash_slow()).collect();
self.states.write().remove_block_states(&removed_hashes);

// Set environment back to common block
let mut env = self.evm_env.write();
Expand Down
14 changes: 14 additions & 0 deletions crates/anvil/src/eth/backend/mem/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ impl InMemoryBlockStates {
}
}

/// Removes states for the given block hashes.
///
/// This is used during chain rollback to clean up states for blocks that are no longer part
/// of the canonical chain.
pub fn remove_block_states(&mut self, hashes: &[B256]) {
for hash in hashes {
self.states.remove(hash);
self.on_disk_states.remove(hash);
self.disk_cache.remove(*hash);
}
self.present.retain(|h| !hashes.contains(h));
self.oldest_on_disk.retain(|h| !hashes.contains(h));
}

/// Serialize all states to a list of serializable historical states
pub fn serialized_states(&mut self) -> SerializableHistoricalStates {
// Get in-memory states
Expand Down
Loading