Skip to content

Commit

Permalink
fix: remove empty pending and future transaction lists (#4987)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann authored and fvictorio committed Mar 13, 2024
1 parent 5fe1728 commit 0ec305f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thick-cycles-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Fixed a bug in `hardhat_dropTransaction` where empty queues persisted and caused panics
12 changes: 11 additions & 1 deletion crates/edr_evm/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ impl MemPool {
let mut invalidated_transactions = pending_transactions.split_off(idx + 1);
let removed = pending_transactions.remove(idx);

if pending_transactions.is_empty() {
self.pending_transactions.remove(caller);
}

self.future_transactions
.entry(*caller)
.and_modify(|transactions| {
Expand All @@ -352,7 +356,13 @@ impl MemPool {
.enumerate()
.find(|(_, transaction)| *transaction.hash() == *hash)
{
return Some(future_transactions.remove(idx));
let removed = future_transactions.remove(idx);

if future_transactions.is_empty() {
self.future_transactions.remove(caller);
}

return Some(removed);
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions crates/edr_provider/tests/issue_325.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use edr_eth::{
remote::PreEip1898BlockSpec, transaction::EthTransactionRequest, AccountInfo, Address, SpecId,
B256,
};
use edr_evm::KECCAK_EMPTY;
use edr_provider::{
test_utils::{create_test_config_with_fork, one_ether},
MethodInvocation, MiningConfig, NoopLogger, Provider, ProviderRequest,
};
use tokio::runtime;

#[tokio::test(flavor = "multi_thread")]
async fn issue_325() -> anyhow::Result<()> {
let logger = Box::new(NoopLogger);
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(None);
config.hardfork = SpecId::CANCUN;
config.mining = MiningConfig {
auto_mine: false,
..MiningConfig::default()
};

let impersonated_account = Address::random();
config.genesis_accounts.insert(
impersonated_account,
AccountInfo {
balance: one_ether(),
nonce: 0,
code: None,
code_hash: KECCAK_EMPTY,
},
);

let provider = Provider::new(runtime::Handle::current(), logger, subscriber, config)?;

provider.handle_request(ProviderRequest::Single(
MethodInvocation::ImpersonateAccount(impersonated_account.into()),
))?;

let result = provider.handle_request(ProviderRequest::Single(
MethodInvocation::SendTransaction(EthTransactionRequest {
from: impersonated_account,
to: Some(Address::random()),
..EthTransactionRequest::default()
}),
))?;

let transaction_hash: B256 = serde_json::from_value(result.result)?;

let result = provider.handle_request(ProviderRequest::Single(
MethodInvocation::DropTransaction(transaction_hash),
))?;

let dropped: bool = serde_json::from_value(result.result)?;

assert!(dropped);

provider.handle_request(ProviderRequest::Single(MethodInvocation::GetBlockByNumber(
PreEip1898BlockSpec::pending(),
false,
)))?;

Ok(())
}

0 comments on commit 0ec305f

Please sign in to comment.