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

perf: clear all transactions if exceeds configured keeper #6127

Merged
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
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