diff --git a/crates/net/network/Cargo.toml b/crates/net/network/Cargo.toml index a8230d655..fd6044864 100644 --- a/crates/net/network/Cargo.toml +++ b/crates/net/network/Cargo.toml @@ -61,6 +61,8 @@ reth-discv4 = { path = "../discv4", features = ["test-utils"] } reth-interfaces = { path = "../../interfaces", features = ["test-utils"] } reth-provider = { path = "../../storage/provider", features = ["test-utils"] } reth-tracing = { path = "../../tracing" } +reth-transaction-pool = { path = "../../transaction-pool", features = ["test-utils"] } + ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false } ethers-providers = { git = "https://github.com/gakonst/ethers-rs", default-features = false } diff --git a/crates/net/network/src/transactions.rs b/crates/net/network/src/transactions.rs index a3314e9cd..77d1510c6 100644 --- a/crates/net/network/src/transactions.rs +++ b/crates/net/network/src/transactions.rs @@ -503,3 +503,44 @@ pub enum NetworkTransactionEvent { response: oneshot::Sender>, }, } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{NetworkConfig, NetworkManager}; + use reth_interfaces::sync::{SyncState, SyncStateUpdater}; + use reth_provider::test_utils::NoopProvider; + use reth_transaction_pool::test_utils::testing_pool; + use secp256k1::SecretKey; + + #[tokio::test(flavor = "multi_thread")] + async fn test_ignored_tx_broadcasts_while_syncing() { + reth_tracing::init_tracing(); + + let secret_key = SecretKey::new(&mut rand::thread_rng()); + + let client = Arc::new(NoopProvider::default()); + let pool = testing_pool(); + let config = NetworkConfig::builder(Arc::clone(&client), secret_key).build(); + let (handle, network, mut transactions, _) = NetworkManager::new(config) + .await + .unwrap() + .into_builder() + .transactions(pool.clone()) + .split_with_handle(); + + tokio::task::spawn(network); + + handle.update_sync_state(SyncState::Downloading { target_block: 100 }); + assert!(handle.is_syncing()); + + let peer_id = PeerId::random(); + + transactions.on_network_tx_event(NetworkTransactionEvent::IncomingTransactions { + peer_id, + msg: Transactions(vec![TransactionSigned::default()]), + }); + + assert!(pool.is_empty()); + } +} diff --git a/crates/net/network/tests/it/connect.rs b/crates/net/network/tests/it/connect.rs index e1c0642f2..995fbcfe5 100644 --- a/crates/net/network/tests/it/connect.rs +++ b/crates/net/network/tests/it/connect.rs @@ -1,8 +1,7 @@ //! Connection tests -use crate::{NetworkEventStream, PeerConfig}; - use super::testnet::Testnet; +use crate::{NetworkEventStream, PeerConfig}; use enr::{k256::ecdsa::SigningKey, Enr, EnrPublicKey}; use ethers_core::utils::Geth; use ethers_providers::{Http, Middleware, Provider}; diff --git a/crates/transaction-pool/Cargo.toml b/crates/transaction-pool/Cargo.toml index 617ca1e72..7e4569d3f 100644 --- a/crates/transaction-pool/Cargo.toml +++ b/crates/transaction-pool/Cargo.toml @@ -42,6 +42,14 @@ bitflags = "1.3" # Using the uint! requires the crate to be imported ruint = "1.7.0" +# testing +rand = { version = "0.8", optional = true } +paste = { version = "1.0", optional = true } + [dev-dependencies] paste = "1.0" rand = "0.8" + + +[features] +test-utils = ["rand", "paste"] \ No newline at end of file diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index 376fca8ad..284a3ee70 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -107,8 +107,9 @@ pub mod pool; mod traits; mod validate; -#[cfg(test)] -mod test_util; +#[cfg(any(test, feature = "test-utils"))] +/// Common test helpers for mocking A pool +pub mod test_utils; /// A shareable, generic, customizable `TransactionPool` implementation. #[derive(Debug)] diff --git a/crates/transaction-pool/src/pool/best.rs b/crates/transaction-pool/src/pool/best.rs index 954538e4a..1b315bd78 100644 --- a/crates/transaction-pool/src/pool/best.rs +++ b/crates/transaction-pool/src/pool/best.rs @@ -78,7 +78,7 @@ mod tests { use super::*; use crate::{ pool::pending::PendingPool, - test_util::{MockOrdering, MockTransaction, MockTransactionFactory}, + test_utils::{MockOrdering, MockTransaction, MockTransactionFactory}, }; #[test] diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 9e9c2faba..0c5956280 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -448,7 +448,7 @@ impl TxPool { } // Additional test impls -#[cfg(test)] +#[cfg(any(test, feature = "test-utils"))] #[allow(missing_docs)] impl TxPool { pub(crate) fn pending(&self) -> &PendingPool { @@ -1121,7 +1121,7 @@ impl SenderInfo { mod tests { use super::*; use crate::{ - test_util::{MockTransaction, MockTransactionFactory}, + test_utils::{MockTransaction, MockTransactionFactory}, traits::TransactionOrigin, }; diff --git a/crates/transaction-pool/src/test_util/mod.rs b/crates/transaction-pool/src/test_util/mod.rs deleted file mode 100644 index ad126940f..000000000 --- a/crates/transaction-pool/src/test_util/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! Internal helpers for testing. -#![allow(missing_docs, unused, missing_debug_implementations, unreachable_pub)] - -mod mock; -mod pool; - -pub use mock::*; diff --git a/crates/transaction-pool/src/test_util/mock.rs b/crates/transaction-pool/src/test_utils/mock.rs similarity index 97% rename from crates/transaction-pool/src/test_util/mock.rs rename to crates/transaction-pool/src/test_utils/mock.rs index 85f611464..11b1ceec8 100644 --- a/crates/transaction-pool/src/test_util/mock.rs +++ b/crates/transaction-pool/src/test_utils/mock.rs @@ -12,17 +12,17 @@ use rand::{ prelude::Distribution, }; use reth_primitives::{ - Address, FromRecoveredTransaction, Transaction, TransactionSignedEcRecovered, TxEip1559, - TxHash, TxLegacy, H256, U256, + Address, FromRecoveredTransaction, IntoRecoveredTransaction, Transaction, + TransactionSignedEcRecovered, TxEip1559, TxHash, TxLegacy, H256, U256, }; use std::{ops::Range, sync::Arc, time::Instant}; -pub type MockTxPool = TxPool; +pub(crate) type MockTxPool = TxPool; pub type MockValidTx = ValidPoolTransaction; /// Create an empty `TxPool` -pub fn mock_tx_pool() -> MockTxPool { +pub(crate) fn mock_tx_pool() -> MockTxPool { MockTxPool::new(Arc::new(Default::default()), Default::default()) } @@ -384,6 +384,12 @@ impl FromRecoveredTransaction for MockTransaction { } } +impl IntoRecoveredTransaction for MockTransaction { + fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { + todo!() + } +} + #[derive(Default)] pub struct MockTransactionFactory { pub(crate) ids: SenderIdentifiers, diff --git a/crates/transaction-pool/src/test_utils/mod.rs b/crates/transaction-pool/src/test_utils/mod.rs new file mode 100644 index 000000000..233ed1bec --- /dev/null +++ b/crates/transaction-pool/src/test_utils/mod.rs @@ -0,0 +1,49 @@ +//! Internal helpers for testing. +#![allow(missing_docs, unused, missing_debug_implementations, unreachable_pub)] + +mod mock; +mod pool; + +use crate::{ + Pool, PoolTransaction, TransactionOrigin, TransactionValidationOutcome, TransactionValidator, +}; +use async_trait::async_trait; +pub use mock::*; +use std::{marker::PhantomData, sync::Arc}; + +/// Returns a new [Pool] used for testing purposes +pub fn testing_pool() -> Pool, MockOrdering> { + Pool::new( + Arc::new(NoopTransactionValidator::default()), + Arc::new(MockOrdering::default()), + Default::default(), + ) +} + +// A [`TransactionValidator`] that does nothing. +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct NoopTransactionValidator(PhantomData); + +#[async_trait::async_trait] +impl TransactionValidator for NoopTransactionValidator { + type Transaction = T; + + async fn validate_transaction( + &self, + origin: TransactionOrigin, + transaction: Self::Transaction, + ) -> TransactionValidationOutcome { + TransactionValidationOutcome::Valid { + balance: Default::default(), + state_nonce: 0, + transaction, + } + } +} + +impl Default for NoopTransactionValidator { + fn default() -> Self { + NoopTransactionValidator(PhantomData::default()) + } +} diff --git a/crates/transaction-pool/src/test_util/pool.rs b/crates/transaction-pool/src/test_utils/pool.rs similarity index 99% rename from crates/transaction-pool/src/test_util/pool.rs rename to crates/transaction-pool/src/test_utils/pool.rs index a90868a9d..9e6f3cdb9 100644 --- a/crates/transaction-pool/src/test_util/pool.rs +++ b/crates/transaction-pool/src/test_utils/pool.rs @@ -3,7 +3,7 @@ use crate::{ error::PoolResult, pool::{txpool::TxPool, AddedTransaction}, - test_util::{ + test_utils::{ MockOrdering, MockTransaction, MockTransactionDistribution, MockTransactionFactory, }, TransactionOrdering, diff --git a/crates/transaction-pool/src/validate.rs b/crates/transaction-pool/src/validate.rs index 78881ee6b..3170ece56 100644 --- a/crates/transaction-pool/src/validate.rs +++ b/crates/transaction-pool/src/validate.rs @@ -27,7 +27,7 @@ pub enum TransactionValidationOutcome { /// Provides support for validating transaction at any given state of the chain #[async_trait::async_trait] -pub trait TransactionValidator: Send + Sync + 'static { +pub trait TransactionValidator: Send + Sync { /// The transaction type to validate. type Transaction: PoolTransaction;