docs: add transaction pool example (#3596)

Co-authored-by: BrazilRaw <138177568+BrazilRaw@users.noreply.github.com>
This commit is contained in:
Matthias Seitz
2023-07-05 15:39:12 +02:00
committed by GitHub
parent 5b246fe29d
commit 7d8f0c7f87
3 changed files with 79 additions and 3 deletions

View File

@ -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(),

View File

@ -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<C>(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<C, St>(client: C, stream: St)
//! where C: StateProviderFactory + BlockReaderIdExt + Clone + 'static,
//! St: Stream<Item = CanonStateNotification> + 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<C>(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<Client, PooledTransaction>,
config: PoolConfig,

View File

@ -31,9 +31,19 @@ pub struct EthTransactionValidator<Client, T> {
impl<Client, Tx> EthTransactionValidator<Client, Tx> {
/// 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<T>(client: Client, chain_spec: Arc<ChainSpec>, 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<T>(
pub fn with_additional_tasks<T>(
client: Client,
chain_spec: Arc<ChainSpec>,
tasks: T,