test(net): more txpool testing support for network (#711)

This commit is contained in:
Matthias Seitz
2023-01-04 13:36:51 +01:00
committed by GitHub
parent 6f3eb42d32
commit 5933014fba
12 changed files with 119 additions and 20 deletions

View File

@ -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 }

View File

@ -503,3 +503,44 @@ pub enum NetworkTransactionEvent {
response: oneshot::Sender<RequestResult<PooledTransactions>>,
},
}
#[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());
}
}

View File

@ -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};

View File

@ -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"]

View File

@ -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)]

View File

@ -78,7 +78,7 @@ mod tests {
use super::*;
use crate::{
pool::pending::PendingPool,
test_util::{MockOrdering, MockTransaction, MockTransactionFactory},
test_utils::{MockOrdering, MockTransaction, MockTransactionFactory},
};
#[test]

View File

@ -448,7 +448,7 @@ impl<T: TransactionOrdering> TxPool<T> {
}
// Additional test impls
#[cfg(test)]
#[cfg(any(test, feature = "test-utils"))]
#[allow(missing_docs)]
impl<T: TransactionOrdering> TxPool<T> {
pub(crate) fn pending(&self) -> &PendingPool<T> {
@ -1121,7 +1121,7 @@ impl SenderInfo {
mod tests {
use super::*;
use crate::{
test_util::{MockTransaction, MockTransactionFactory},
test_utils::{MockTransaction, MockTransactionFactory},
traits::TransactionOrigin,
};

View File

@ -1,7 +0,0 @@
//! Internal helpers for testing.
#![allow(missing_docs, unused, missing_debug_implementations, unreachable_pub)]
mod mock;
mod pool;
pub use mock::*;

View File

@ -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<MockOrdering>;
pub(crate) type MockTxPool = TxPool<MockOrdering>;
pub type MockValidTx = ValidPoolTransaction<MockTransaction>;
/// 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,

View File

@ -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<NoopTransactionValidator<MockTransaction>, 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<T>(PhantomData<T>);
#[async_trait::async_trait]
impl<T: PoolTransaction> TransactionValidator for NoopTransactionValidator<T> {
type Transaction = T;
async fn validate_transaction(
&self,
origin: TransactionOrigin,
transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction> {
TransactionValidationOutcome::Valid {
balance: Default::default(),
state_nonce: 0,
transaction,
}
}
}
impl<T> Default for NoopTransactionValidator<T> {
fn default() -> Self {
NoopTransactionValidator(PhantomData::default())
}
}

View File

@ -3,7 +3,7 @@
use crate::{
error::PoolResult,
pool::{txpool::TxPool, AddedTransaction},
test_util::{
test_utils::{
MockOrdering, MockTransaction, MockTransactionDistribution, MockTransactionFactory,
},
TransactionOrdering,

View File

@ -27,7 +27,7 @@ pub enum TransactionValidationOutcome<T: PoolTransaction> {
/// 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;