diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index a916878126..a37acc28da 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -220,7 +220,7 @@ impl Command { let blockchain_db = BlockchainProvider::new(factory, blockchain_tree.clone())?; let transaction_pool = reth_transaction_pool::Pool::eth_pool( - EthTransactionValidator::new( + EthTransactionValidator::with_additional_tasks( blockchain_db.clone(), Arc::clone(&self.chain), ctx.task_executor.clone(), diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index 917c2aecad..46a744cfb8 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -86,6 +86,57 @@ //! [`Pool`](crate::Pool) type is just an `Arc` wrapper around `PoolInner`. This is the usable type //! that provides the `TransactionPool` interface. //! +//! +//! ## Examples +//! +//! Listen for new transactions and print them: +//! +//! ``` +//! use reth_primitives::MAINNET; +//! use reth_provider::StateProviderFactory; +//! use reth_tasks::TokioTaskExecutor; +//! use reth_transaction_pool::{EthTransactionValidator, Pool, TransactionPool}; +//! async fn t(client: C) where C: StateProviderFactory + Clone + 'static{ +//! let pool = Pool::eth_pool( +//! EthTransactionValidator::new(client, MAINNET.clone(), TokioTaskExecutor::default()), +//! Default::default(), +//! ); +//! let mut transactions = pool.pending_transactions_listener(); +//! tokio::task::spawn( async move { +//! while let Some(tx) = transactions.recv().await { +//! println!("New transaction: {:?}", tx); +//! } +//! }); +//! +//! // do something useful with the pool, like RPC integration +//! +//! # } +//! ``` +//! +//! Spawn maintenance task to keep the pool updated +//! +//! ``` +//! use futures_util::Stream; +//! use reth_primitives::MAINNET; +//! use reth_provider::{BlockReaderIdExt, CanonStateNotification, StateProviderFactory}; +//! use reth_tasks::TokioTaskExecutor; +//! use reth_transaction_pool::{EthTransactionValidator, Pool}; +//! use reth_transaction_pool::maintain::maintain_transaction_pool_future; +//! async fn t(client: C, stream: St) +//! where C: StateProviderFactory + BlockReaderIdExt + Clone + 'static, +//! St: Stream + Send + Unpin + 'static, +//! { +//! let pool = Pool::eth_pool( +//! EthTransactionValidator::new(client.clone(), MAINNET.clone(), TokioTaskExecutor::default()), +//! Default::default(), +//! ); +//! +//! // spawn a task that listens for new blocks and updates the pool's transactions, mined transactions etc.. +//! tokio::task::spawn( maintain_transaction_pool_future(client, pool, stream)); +//! +//! # } +//! ``` +//! //! ## Feature Flags //! //! - `serde` (default): Enable serde support @@ -230,6 +281,21 @@ where { /// Returns a new [Pool] that uses the default [EthTransactionValidator] when validating /// [PooledTransaction]s and ords via [GasCostOrdering] + /// + /// # Example + /// + /// ``` + /// use reth_provider::StateProviderFactory; + /// use reth_primitives::MAINNET; + /// use reth_tasks::TokioTaskExecutor; + /// use reth_transaction_pool::{EthTransactionValidator, Pool}; + /// # fn t(client: C) where C: StateProviderFactory + Clone + 'static{ + /// let pool = Pool::eth_pool( + /// EthTransactionValidator::new(client, MAINNET.clone(), TokioTaskExecutor::default()), + /// Default::default(), + /// ); + /// # } + /// ``` pub fn eth_pool( validator: EthTransactionValidator, config: PoolConfig, diff --git a/crates/transaction-pool/src/validate/eth.rs b/crates/transaction-pool/src/validate/eth.rs index ee24c62669..605c7d08a4 100644 --- a/crates/transaction-pool/src/validate/eth.rs +++ b/crates/transaction-pool/src/validate/eth.rs @@ -31,9 +31,19 @@ pub struct EthTransactionValidator { impl EthTransactionValidator { /// Creates a new instance for the given [ChainSpec] /// - /// This will always spawn a validation tasks that perform the actual validation. A will spawn + /// This will spawn a single validation tasks that performs the actual validation. + pub fn new(client: Client, chain_spec: Arc, tasks: T) -> Self + where + T: TaskSpawner, + { + Self::with_additional_tasks(client, chain_spec, tasks, 0) + } + + /// Creates a new instance for the given [ChainSpec] + /// + /// This will always spawn a validation task that performs the actual validation. It will spawn /// `num_additional_tasks` additional tasks. - pub fn new( + pub fn with_additional_tasks( client: Client, chain_spec: Arc, tasks: T,