refactor(txpool): simplify validator trait (#60)

* refactor(txpool): simplify validator trait

* fix docs
This commit is contained in:
Matthias Seitz
2022-10-13 20:36:15 +02:00
committed by GitHub
parent 55768a534d
commit 6b71460e7e
4 changed files with 36 additions and 50 deletions

View File

@ -1,10 +0,0 @@
//! Provides access to the chain's storage
use crate::{error::PoolError, validate::TransactionValidator};
/// The interface used to interact with the blockchain and access storage.
#[async_trait::async_trait]
pub trait PoolClient: Send + Sync + TransactionValidator {
/// Error type that can be converted to the crate's internal Error.
type Error: Into<PoolError>;
}

View File

@ -76,7 +76,6 @@
//! that provides the `TransactionPool` interface.
pub use crate::{
client::PoolClient,
config::PoolConfig,
ordering::TransactionOrdering,
traits::{BestTransactions, NewBlockEvent, PoolTransaction, TransactionPool},
@ -89,7 +88,6 @@ use futures::channel::mpsc::Receiver;
use reth_primitives::{BlockID, TxHash, U256, U64};
use std::{collections::HashMap, sync::Arc};
mod client;
mod config;
pub mod error;
mod identifier;
@ -102,33 +100,33 @@ mod validate;
mod test_util;
/// A shareable, generic, customizable `TransactionPool` implementation.
pub struct Pool<P: PoolClient, T: TransactionOrdering> {
pub struct Pool<V: TransactionValidator, T: TransactionOrdering> {
/// Arc'ed instance of the pool internals
pool: Arc<PoolInner<P, T>>,
pool: Arc<PoolInner<V, T>>,
}
// === impl Pool ===
impl<P, T> Pool<P, T>
impl<V, T> Pool<V, T>
where
P: PoolClient,
T: TransactionOrdering<Transaction = <P as TransactionValidator>::Transaction>,
V: TransactionValidator,
T: TransactionOrdering<Transaction = <V as TransactionValidator>::Transaction>,
{
/// Create a new transaction pool instance.
pub fn new(client: Arc<P>, ordering: Arc<T>, config: PoolConfig) -> Self {
pub fn new(client: Arc<V>, ordering: Arc<T>, config: PoolConfig) -> Self {
Self { pool: Arc::new(PoolInner::new(client, ordering, config)) }
}
/// Returns the wrapped pool.
pub(crate) fn inner(&self) -> &PoolInner<P, T> {
pub(crate) fn inner(&self) -> &PoolInner<V, T> {
&self.pool
}
/// Returns future that validates all transaction in the given iterator.
async fn validate_all(
&self,
transactions: impl IntoIterator<Item = P::Transaction>,
) -> PoolResult<HashMap<TxHash, TransactionValidationOutcome<P::Transaction>>> {
transactions: impl IntoIterator<Item = V::Transaction>,
) -> PoolResult<HashMap<TxHash, TransactionValidationOutcome<V::Transaction>>> {
let outcome =
futures::future::join_all(transactions.into_iter().map(|tx| self.validate(tx)))
.await
@ -141,12 +139,12 @@ where
/// Validates the given transaction
async fn validate(
&self,
transaction: P::Transaction,
) -> (TxHash, TransactionValidationOutcome<P::Transaction>) {
transaction: V::Transaction,
) -> (TxHash, TransactionValidationOutcome<V::Transaction>) {
let hash = *transaction.hash();
// TODO(mattsse): this is where additional validate checks would go, like banned senders
// etc...
let outcome = self.pool.client().validate_transaction(transaction).await;
let outcome = self.pool.validator().validate_transaction(transaction).await;
(hash, outcome)
}
@ -164,10 +162,10 @@ where
/// implements the `TransactionPool` interface for various transaction pool API consumers.
#[async_trait::async_trait]
impl<P, T> TransactionPool for Pool<P, T>
impl<V, T> TransactionPool for Pool<V, T>
where
P: PoolClient,
T: TransactionOrdering<Transaction = <P as TransactionValidator>::Transaction>,
V: TransactionValidator,
T: TransactionOrdering<Transaction = <V as TransactionValidator>::Transaction>,
{
type Transaction = T::Transaction;
@ -216,7 +214,7 @@ where
}
}
impl<P: PoolClient, O: TransactionOrdering> Clone for Pool<P, O> {
impl<V: TransactionValidator, O: TransactionOrdering> Clone for Pool<V, O> {
fn clone(&self) -> Self {
Self { pool: Arc::clone(&self.pool) }
}

View File

@ -69,7 +69,7 @@ use crate::{
pool::{listener::PoolEventListener, state::SubPool, txpool::TxPool},
traits::{NewTransactionEvent, PoolTransaction},
validate::{TransactionValidationOutcome, ValidPoolTransaction},
PoolClient, PoolConfig, TransactionOrdering, TransactionValidator, U256,
PoolConfig, TransactionOrdering, TransactionValidator, U256,
};
use best::BestTransactions;
pub use events::TransactionEvent;
@ -89,11 +89,11 @@ mod transaction;
pub mod txpool;
/// Transaction pool internals.
pub struct PoolInner<P: PoolClient, T: TransactionOrdering> {
pub struct PoolInner<V: TransactionValidator, T: TransactionOrdering> {
/// Internal mapping of addresses to plain ints.
identifiers: RwLock<SenderIdentifiers>,
/// Chain/Storage access.
client: Arc<P>,
/// Transaction validation.
validator: Arc<V>,
/// The internal pool that manages all transactions.
pool: RwLock<TxPool<T>>,
/// Pool settings.
@ -108,16 +108,16 @@ pub struct PoolInner<P: PoolClient, T: TransactionOrdering> {
// === impl PoolInner ===
impl<P: PoolClient, T: TransactionOrdering> PoolInner<P, T>
impl<V: TransactionValidator, T: TransactionOrdering> PoolInner<V, T>
where
P: PoolClient,
T: TransactionOrdering<Transaction = <P as TransactionValidator>::Transaction>,
V: TransactionValidator,
T: TransactionOrdering<Transaction = <V as TransactionValidator>::Transaction>,
{
/// Create a new transaction pool instance.
pub fn new(client: Arc<P>, ordering: Arc<T>, config: PoolConfig) -> Self {
pub fn new(client: Arc<V>, ordering: Arc<T>, config: PoolConfig) -> Self {
Self {
identifiers: Default::default(),
client,
validator: client,
config,
event_listener: Default::default(),
pool: RwLock::new(TxPool::new(ordering)),
@ -136,9 +136,9 @@ where
self.pool.write().update_base_fee(base_fee);
}
/// Get client reference.
pub fn client(&self) -> &P {
&self.client
/// Get the validator reference.
pub fn validator(&self) -> &V {
&self.validator
}
/// Adds a new transaction listener to the pool that gets notified about every new _ready_

View File

@ -39,9 +39,7 @@ pub trait TransactionValidator: Send + Sync {
async fn validate_transaction(
&self,
_transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction> {
unimplemented!()
}
) -> TransactionValidationOutcome<Self::Transaction>;
}
/// A valida transaction in the pool.