feat(txpool): add PoolStatus Api (#91)

This commit is contained in:
Matthias Seitz
2022-10-18 21:56:53 +02:00
committed by GitHub
parent 522a4e689e
commit 2009b764c0
4 changed files with 36 additions and 2 deletions

View File

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

View File

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

View File

@ -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<T: TransactionOrdering> TxPool<T> {
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.

View File

@ -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<U256>;
}
/// 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,
}