From 963d8b95a486da5e7961e60d714468eb0b90be3a Mon Sep 17 00:00:00 2001 From: int88 <106391185+int88@users.noreply.github.com> Date: Thu, 27 Mar 2025 19:45:30 +0800 Subject: [PATCH] integration test: txpool listener could get replaced event (#15318) --- crates/transaction-pool/tests/it/listeners.rs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/transaction-pool/tests/it/listeners.rs b/crates/transaction-pool/tests/it/listeners.rs index ad4361fa13..759bb25b5b 100644 --- a/crates/transaction-pool/tests/it/listeners.rs +++ b/crates/transaction-pool/tests/it/listeners.rs @@ -28,6 +28,43 @@ async fn txpool_listener_by_hash() { assert_matches!(events.next().await, Some(TransactionEvent::Discarded)); } +#[tokio::test(flavor = "multi_thread")] +async fn txpool_listener_replace_event() { + let txpool = TestPoolBuilder::default(); + let mut mock_tx_factory = MockTransactionFactory::default(); + let transaction = mock_tx_factory.create_eip1559(); + + let mut all_tx_events = txpool.all_transactions_event_listener(); + + let old_transaction = transaction.transaction.clone(); + let mut result = txpool + .add_transaction_and_subscribe(TransactionOrigin::External, old_transaction.clone()) + .await; + assert_matches!(result, Ok(_)); + + let mut events = result.unwrap(); + assert_matches!(events.next().await, Some(TransactionEvent::Pending)); + assert_matches!(all_tx_events.next().await, Some(FullTransactionEvent::Pending(hash)) if hash == *old_transaction.get_hash()); + + // add replace tx. + let replace_transaction = transaction.transaction.clone().rng_hash().inc_price(); + result = txpool + .add_transaction_and_subscribe(TransactionOrigin::External, replace_transaction.clone()) + .await; + assert_matches!(result, Ok(_)); + + let mut new_events = result.unwrap(); + assert_matches!(new_events.next().await, Some(TransactionEvent::Pending)); + + // The listener of old transaction should receive replaced event. + assert_matches!(events.next().await, Some(TransactionEvent::Replaced(hash)) if hash == *replace_transaction.get_hash()); + + // The listener of all should receive one pending event of new transaction and one replaced + // event of old transaction. + assert_matches!(all_tx_events.next().await, Some(FullTransactionEvent::Pending(hash)) if hash == *replace_transaction.get_hash()); + assert_matches!(all_tx_events.next().await, Some(FullTransactionEvent::Replaced { transaction, replaced_by }) if *transaction.transaction.get_hash() == *old_transaction.get_hash() && replaced_by == *replace_transaction.get_hash()); +} + #[tokio::test(flavor = "multi_thread")] async fn txpool_listener_all() { let txpool = TestPoolBuilder::default();