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

Unify txpool.remove function #6641

Merged
merged 6 commits into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 5 additions & 14 deletions crates/transaction-pool/src/pool/pending.rs
Expand Up @@ -313,27 +313,18 @@ impl<T: TransactionOrdering> PendingPool<T> {
self.by_id.insert(tx_id, tx);
}

/// Removes a _mined_ transaction from the pool.
/// Removes the transaction from the pool.
///
/// If the transaction has a descendant transaction it will advance it to the best queue.
pub(crate) fn prune_transaction(
/// Note: If the transaction has a descendant transaction
/// it will advance it to the best queue.
pub(crate) fn remove_transaction(
&mut self,
id: &TransactionId,
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> {
// mark the next as independent if it exists
if let Some(unlocked) = self.get(&id.descendant()) {
self.independent_transactions.insert(unlocked.clone());
};
self.remove_transaction(id)
}

/// Removes the transaction from the pool.
///
/// Note: this only removes the given transaction.
pub(crate) fn remove_transaction(
&mut self,
id: &TransactionId,
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> {
}
let tx = self.by_id.remove(id)?;
self.size_of -= tx.transaction.size();
self.all.remove(&tx);
Expand Down
27 changes: 26 additions & 1 deletion crates/transaction-pool/src/pool/txpool.rs
Expand Up @@ -678,7 +678,7 @@ impl<T: TransactionOrdering> TxPool<T> {
tx: &TransactionId,
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> {
match pool {
SubPool::Pending => self.pending_pool.prune_transaction(tx),
SubPool::Pending => self.pending_pool.remove_transaction(tx),
SubPool::Queued => self.queued_pool.remove_transaction(tx),
SubPool::BaseFee => self.basefee_pool.remove_transaction(tx),
SubPool::Blob => self.blob_pool.remove_transaction(tx),
Expand Down Expand Up @@ -2818,4 +2818,29 @@ mod tests {
assert!(pool.size().blob <= blob_limit.max_txs);
}
}

#[test]
fn test_transaction_removal() {
let on_chain_balance = U256::from(10_000);
let on_chain_nonce = 0;
let mut f = MockTransactionFactory::default();
let mut pool = TxPool::new(MockOrdering::default(), Default::default());

let tx_0 = MockTransaction::eip1559().set_gas_price(100).inc_limit();
let tx_1 = tx_0.next();

// Create 2 transactions
let v0 = f.validated(tx_0.clone());
let v1 = f.validated(tx_1);

// Add them to the pool
let _res = pool.add_transaction(v0.clone(), on_chain_balance, on_chain_nonce).unwrap();
let _res = pool.add_transaction(v1.clone(), on_chain_balance, on_chain_nonce).unwrap();

assert_eq!(0, pool.queued_transactions().len());
assert_eq!(2, pool.pending_transactions().len());

// Remove first (nonce 0) - simulating that it was taken to be a part of the block.
pool.remove_transactions(vec![*v0.hash()]);
loocapro marked this conversation as resolved.
Show resolved Hide resolved
}
loocapro marked this conversation as resolved.
Show resolved Hide resolved
}