Skip to content

Commit

Permalink
perf: clear all transactions if exceeds configured keeper (foundry-rs…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 26, 2023
1 parent 7d12927 commit f345556
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
14 changes: 4 additions & 10 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,19 +969,13 @@ impl Backend {
storage.transactions.insert(mined_tx.info.transaction_hash, mined_tx);
}

// remove old transactions that exceed the transaction block keeper
if let Some(transaction_block_keeper) = self.transaction_block_keeper {
if storage.blocks.len() > transaction_block_keeper {
let n: U64 = block_number
let to_clear = block_number
.as_u64()
.saturating_sub(transaction_block_keeper.try_into().unwrap())
.into();
if let Some(hash) = storage.hashes.get(&n) {
if let Some(block) = storage.blocks.get(hash) {
for tx in block.clone().transactions {
let _ = storage.transactions.remove(&tx.hash());
}
}
}
.saturating_sub(transaction_block_keeper.try_into().unwrap());
storage.remove_block_transactions_by_number(to_clear)
}
}

Expand Down
17 changes: 17 additions & 0 deletions crates/anvil/src/eth/backend/mem/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ impl BlockchainStorage {
total_difficulty: Default::default(),
}
}

/// Removes all stored transactions for the given block number
pub fn remove_block_transactions_by_number(&mut self, num: u64) {
if let Some(hash) = self.hashes.get(&(num.into())).copied() {
self.remove_block_transactions(hash);
}
}

/// Removes all stored transactions for the given block hash
pub fn remove_block_transactions(&mut self, block_hash: H256) {
if let Some(block) = self.blocks.get_mut(&block_hash) {
for tx in block.transactions.iter() {
self.transactions.remove(&tx.hash());
}
block.transactions.clear();
}
}
}

// === impl BlockchainStorage ===
Expand Down

0 comments on commit f345556

Please sign in to comment.