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

Adds rpc method for anvil to drop all pending transactions #7643

Merged
merged 4 commits into from
Apr 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ pub enum EthRequest {
)]
DropTransaction(B256),

/// Removes transactions from the pool
#[cfg_attr(
feature = "serde",
serde(
rename = "anvil_dropAllTransactions",
alias = "hardhat_dropAllTransactions",
with = "empty_params"
)
)]
DropAllTransactions(),

/// Reset the fork to a fresh forked state, and optionally update the fork config
#[cfg_attr(feature = "serde", serde(rename = "anvil_reset", alias = "hardhat_reset"))]
Reset(#[cfg_attr(feature = "serde", serde(default))] Option<Params<Option<Forking>>>),
Expand Down
13 changes: 13 additions & 0 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ impl EthApi {
EthRequest::DropTransaction(tx) => {
self.anvil_drop_transaction(tx).await.to_rpc_result()
}
EthRequest::DropAllTransactions() => {
self.anvil_drop_all_transactions().await.to_rpc_result()
}
EthRequest::Reset(fork) => {
self.anvil_reset(fork.and_then(|p| p.params)).await.to_rpc_result()
}
Expand Down Expand Up @@ -1572,6 +1575,16 @@ impl EthApi {
Ok(self.pool.drop_transaction(tx_hash).map(|tx| tx.hash()))
}


/// Removes all transactions from the pool
///
/// Handler for RPC call: `anvil_dropAllTransactions`
pub async fn anvil_drop_all_transactions(&self) -> Result<()> {
node_info!("anvil_dropAllTransactions");
self.pool.drop_all_transactions();
Ok(())
}

/// Reset the fork to a fresh forked state, and optionally update the fork config.
///
/// If `forking` is `None` then this will disable forking entirely.
Expand Down
17 changes: 17 additions & 0 deletions crates/anvil/src/eth/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ impl Pool {
dropped
}

pub fn drop_all_transactions(&self) {
let pool = self.inner.write();

let pending_transactions = pool.pending_transactions.transactions();
let ready_transactions = pool.ready_transactions.get_transactions();

pending_transactions.for_each(|pool_transaction| {
let tx_hash = pool_transaction.as_ref().hash();
self.drop_transaction(tx_hash);
});

ready_transactions.for_each(|pool_transaction| {
let tx_hash = pool_transaction.as_ref().hash();
self.drop_transaction(tx_hash);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this works, I want this to be

fn clear(&self) {
let pool = self.inner.write();
pool.clear(); // this clears both tx sets, calls clear on all their internals
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, and this should be more performant too


/// notifies all listeners about the transaction
fn notify_listener(&self, hash: TxHash) {
let mut listener = self.transaction_listener.lock();
Expand Down