diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index 69a9dc5c31..305652edc0 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -84,7 +84,7 @@ pub use crate::{ use crate::{ error::PoolResult, pool::PoolInner, - traits::{NewTransactionEvent, TransactionOrigin}, + traits::{NewTransactionEvent, PoolStatus, TransactionOrigin}, validate::ValidPoolTransaction, }; use futures::channel::mpsc::Receiver; @@ -174,6 +174,10 @@ where { type Transaction = T::Transaction; + fn status(&self) -> PoolStatus { + self.pool.status() + } + async fn on_new_block(&self, _event: NewBlockEvent) { // TODO perform maintenance: update pool accordingly todo!() diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index 9cf1ae8a56..94f5c48135 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -67,7 +67,7 @@ use crate::{ error::PoolResult, identifier::{SenderId, SenderIdentifiers, TransactionId}, pool::{listener::PoolEventListener, state::SubPool, txpool::TxPool}, - traits::{NewTransactionEvent, PoolTransaction, TransactionOrigin}, + traits::{NewTransactionEvent, PoolStatus, PoolTransaction, TransactionOrigin}, validate::{TransactionValidationOutcome, ValidPoolTransaction}, PoolConfig, TransactionOrdering, TransactionValidator, U256, }; @@ -126,6 +126,11 @@ where } } + /// Returns stats about the pool. + pub(crate) fn status(&self) -> PoolStatus { + self.pool.read().status() + } + /// Returns the internal `SenderId` for this address pub(crate) fn get_sender_id(&self, addr: Address) -> SenderId { self.identifiers.write().sender_id_or_create(addr) diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 618326f20f..8af3d42f26 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -10,6 +10,7 @@ use crate::{ state::{SubPool, TxState}, AddedPendingTransaction, AddedTransaction, }, + traits::PoolStatus, PoolConfig, PoolResult, PoolTransaction, TransactionOrdering, ValidPoolTransaction, U256, }; use fnv::FnvHashMap; @@ -97,6 +98,16 @@ impl TxPool { config, } } + + /// Returns stats about the pool. + pub(crate) fn status(&self) -> PoolStatus { + PoolStatus { + pending: self.pending_pool.len(), + basefee: self.basefee_pool.len(), + queued: self.queued_pool.len(), + } + } + /// Updates the pool based on the changed base fee. /// /// This enforces the dynamic fee requirement. diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 576ef15c53..706c535852 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -13,6 +13,9 @@ pub trait TransactionPool: Send + Sync + 'static { /// The transaction type of the pool type Transaction: PoolTransaction; + /// Returns stats about the pool. + fn status(&self) -> PoolStatus; + /// Event listener for when a new block was mined. /// /// Implementers need to update the pool accordingly. @@ -189,3 +192,14 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + 'static { /// This will return `None` for non-EIP1559 transactions fn max_priority_fee_per_gas(&self) -> Option; } + +/// Represents the current status of the pool. +#[derive(Debug, Clone)] +pub struct PoolStatus { + /// Number of transactions in the _pending_ sub-pool. + pub pending: usize, + /// Number of transactions in the _basefee_ pool. + pub basefee: usize, + /// Number of transactions in the _queued_ sub-pool. + pub queued: usize, +}