Skip to content

Commit

Permalink
fix(mempool): bug fix in mempool input validation (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
elintul authored Oct 8, 2024
1 parent 12528f1 commit 7ed164b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Mempool {
if self
.tx_queue
.get_nonce(sender_address)
.is_some_and(|queued_nonce| queued_nonce > tx_nonce)
.is_some_and(|queued_nonce| queued_nonce >= tx_nonce)
{
return Err(duplicate_nonce_error);
}
Expand Down
25 changes: 15 additions & 10 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ fn test_add_tx_multi_nonce_success(mut mempool: Mempool) {
}

#[rstest]
fn test_add_tx_with_duplicate_tx(mut mempool: Mempool) {
fn test_add_tx_failure_on_duplicate_tx_hash(mut mempool: Mempool) {
// Setup.
let input = add_tx_input!(tip: 50, tx_hash: 1);
let duplicate_input = input.clone();
let input = add_tx_input!(tx_hash: 1, tx_nonce: 1, account_nonce: 0);
let duplicate_input = input.clone(); // Same hash is possible if signature is different.

// Test.
add_tx(&mut mempool, &input);
Expand Down Expand Up @@ -423,13 +423,18 @@ fn test_add_tx_lower_than_queued_nonce() {
.build_into_mempool();

// Test and assert: original transaction remains.
let lower_nonce_input =
add_tx_input!(tx_hash: 2, sender_address: "0x0", tx_nonce: 0, account_nonce: 0);
add_tx_expect_error(
&mut mempool,
&lower_nonce_input,
MempoolError::DuplicateNonce { address: contract_address!("0x0"), nonce: nonce!(0) },
);
for tx_nonce in [0, 1] {
let invalid_input =
add_tx_input!(tx_hash: 2, sender_address: "0x0", tx_nonce: tx_nonce, account_nonce: 0);
add_tx_expect_error(
&mut mempool,
&invalid_input,
MempoolError::DuplicateNonce {
address: contract_address!("0x0"),
nonce: nonce!(tx_nonce),
},
);
}

let expected_mempool_content = MempoolContentBuilder::new()
.with_pool(pool_txs)
Expand Down

0 comments on commit 7ed164b

Please sign in to comment.