refactor(txpool): get rid of block number requirement (#56)

This commit is contained in:
Matthias Seitz
2022-10-13 01:24:19 +02:00
committed by GitHub
parent 6d4e39deef
commit 74b04dc81a
4 changed files with 12 additions and 52 deletions

View File

@ -1,24 +1,10 @@
//! Provides access to the chain's storage
use crate::{
error::{PoolError, PoolResult},
validate::TransactionValidator,
};
use reth_primitives::{BlockID, U64};
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>;
/// Returns the block number for the given block identifier.
fn convert_block_id(&self, block_id: &BlockID) -> PoolResult<Option<U64>>;
/// Same as [`PoolClient::convert_block_id()`] but returns an error if no matching block number
/// was found
fn ensure_block_number(&self, block_id: &BlockID) -> PoolResult<U64> {
self.convert_block_id(block_id)
.and_then(|number| number.ok_or_else(|| PoolError::BlockNumberNotFound(*block_id)))
}
}

View File

@ -84,7 +84,6 @@ pub use crate::{
};
use crate::{error::PoolResult, pool::PoolInner, validate::ValidPoolTransaction};
use futures::channel::mpsc::Receiver;
use reth_primitives::{BlockID, TxHash, U256, U64};
use std::{collections::HashMap, sync::Arc};
@ -123,24 +122,14 @@ where
&self.pool
}
/// Returns the actual block number for the block id
fn resolve_block_number(&self, block_id: &BlockID) -> PoolResult<U64> {
self.pool.client().ensure_block_number(block_id)
}
/// Returns future that validates all transaction in the given iterator at the block the
/// `block_id` points to.
async fn validate_all(
&self,
block_id: &BlockID,
transactions: impl IntoIterator<Item = P::Transaction>,
) -> PoolResult<HashMap<TxHash, TransactionValidationOutcome<P::Transaction>>> {
// get the actual block number which is required to validate the transactions
let block_number = self.resolve_block_number(block_id)?;
let outcome = futures::future::join_all(
transactions.into_iter().map(|tx| self.validate(block_id, block_number, tx)),
)
let outcome =
futures::future::join_all(transactions.into_iter().map(|tx| self.validate(tx)))
.await
.into_iter()
.collect::<HashMap<_, _>>();
@ -151,13 +140,11 @@ where
/// Validates the given transaction at the given block
async fn validate(
&self,
block_id: &BlockID,
_block_number: U64,
transaction: P::Transaction,
) -> (TxHash, TransactionValidationOutcome<P::Transaction>) {
let _hash = *transaction.hash();
// TODO this is where additional validate checks would go, like banned senders etc...
let _res = self.pool.client().validate_transaction(block_id, transaction).await;
let _res = self.pool.client().validate_transaction(transaction).await;
// TODO blockstamp the transaction
@ -189,23 +176,16 @@ where
todo!()
}
async fn add_transaction(
&self,
block_id: BlockID,
transaction: Self::Transaction,
) -> PoolResult<TxHash> {
self.add_transactions(block_id, vec![transaction])
.await?
.pop()
.expect("transaction exists; qed")
async fn add_transaction(&self, transaction: Self::Transaction) -> PoolResult<TxHash> {
let (_, tx) = self.validate(transaction).await;
self.pool.add_transactions(std::iter::once(tx)).pop().expect("exists; qed")
}
async fn add_transactions(
&self,
block_id: BlockID,
transactions: Vec<Self::Transaction>,
) -> PoolResult<Vec<PoolResult<TxHash>>> {
let validated = self.validate_all(&block_id, transactions).await?;
let validated = self.validate_all(transactions).await?;
let transactions = self.pool.add_transactions(validated.into_values());
Ok(transactions)
}

View File

@ -23,11 +23,7 @@ pub trait TransactionPool: Send + Sync {
/// Adds an _unvalidated_ transaction into the pool.
///
/// Consumer: RPC
async fn add_transaction(
&self,
block_id: BlockID,
transaction: Self::Transaction,
) -> PoolResult<TxHash>;
async fn add_transaction(&self, transaction: Self::Transaction) -> PoolResult<TxHash>;
/// Adds the given _unvalidated_ transaction into the pool.
///
@ -36,7 +32,6 @@ pub trait TransactionPool: Send + Sync {
/// Consumer: RPC
async fn add_transactions(
&self,
block_id: BlockID,
transactions: Vec<Self::Transaction>,
) -> PoolResult<Vec<PoolResult<TxHash>>>;

View File

@ -38,7 +38,6 @@ pub trait TransactionValidator: Send + Sync {
/// transactions for the sender.
async fn validate_transaction(
&self,
_block_id: &BlockID,
_transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction> {
unimplemented!()