Skip to content

Commit 98bc956

Browse files
fix(anvil): remove transactions before block/hash in unwind_to to prevent leaks (#12178)
* fix(anvil): remove transactions before block/hash in unwind_to to prevent leaks * Update storage.rs --------- Co-authored-by: grandizzy <[email protected]>
1 parent 040be03 commit 98bc956

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

crates/anvil/src/eth/backend/mem/storage.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,15 @@ impl BlockchainStorage {
337337
let mut removed = vec![];
338338
let best_num: u64 = self.best_number;
339339
for i in (block_number + 1)..=best_num {
340-
if let Some(hash) = self.hashes.remove(&i)
341-
&& let Some(block) = self.blocks.remove(&hash)
342-
{
343-
self.remove_block_transactions_by_number(block.header.number);
344-
removed.push(block);
340+
if let Some(hash) = self.hashes.get(&i).copied() {
341+
// First remove the block's transactions while the mappings still exist
342+
self.remove_block_transactions_by_number(i);
343+
344+
// Now remove the block from storage (may already be empty of txs) and drop mapping
345+
if let Some(block) = self.blocks.remove(&hash) {
346+
removed.push(block);
347+
}
348+
self.hashes.remove(&i);
345349
}
346350
}
347351
self.best_hash = block_hash;

0 commit comments

Comments
 (0)