mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor(txpool): get rid of block number requirement (#56)
This commit is contained in:
@ -1,24 +1,10 @@
|
|||||||
//! Provides access to the chain's storage
|
//! Provides access to the chain's storage
|
||||||
|
|
||||||
use crate::{
|
use crate::{error::PoolError, validate::TransactionValidator};
|
||||||
error::{PoolError, PoolResult},
|
|
||||||
validate::TransactionValidator,
|
|
||||||
};
|
|
||||||
use reth_primitives::{BlockID, U64};
|
|
||||||
|
|
||||||
/// The interface used to interact with the blockchain and access storage.
|
/// The interface used to interact with the blockchain and access storage.
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait PoolClient: Send + Sync + TransactionValidator {
|
pub trait PoolClient: Send + Sync + TransactionValidator {
|
||||||
/// Error type that can be converted to the crate's internal Error.
|
/// Error type that can be converted to the crate's internal Error.
|
||||||
type Error: Into<PoolError>;
|
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)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -84,7 +84,6 @@ pub use crate::{
|
|||||||
};
|
};
|
||||||
use crate::{error::PoolResult, pool::PoolInner, validate::ValidPoolTransaction};
|
use crate::{error::PoolResult, pool::PoolInner, validate::ValidPoolTransaction};
|
||||||
use futures::channel::mpsc::Receiver;
|
use futures::channel::mpsc::Receiver;
|
||||||
|
|
||||||
use reth_primitives::{BlockID, TxHash, U256, U64};
|
use reth_primitives::{BlockID, TxHash, U256, U64};
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
@ -123,27 +122,17 @@ where
|
|||||||
&self.pool
|
&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
|
/// Returns future that validates all transaction in the given iterator at the block the
|
||||||
/// `block_id` points to.
|
/// `block_id` points to.
|
||||||
async fn validate_all(
|
async fn validate_all(
|
||||||
&self,
|
&self,
|
||||||
block_id: &BlockID,
|
|
||||||
transactions: impl IntoIterator<Item = P::Transaction>,
|
transactions: impl IntoIterator<Item = P::Transaction>,
|
||||||
) -> PoolResult<HashMap<TxHash, TransactionValidationOutcome<P::Transaction>>> {
|
) -> PoolResult<HashMap<TxHash, TransactionValidationOutcome<P::Transaction>>> {
|
||||||
// get the actual block number which is required to validate the transactions
|
let outcome =
|
||||||
let block_number = self.resolve_block_number(block_id)?;
|
futures::future::join_all(transactions.into_iter().map(|tx| self.validate(tx)))
|
||||||
|
.await
|
||||||
let outcome = futures::future::join_all(
|
.into_iter()
|
||||||
transactions.into_iter().map(|tx| self.validate(block_id, block_number, tx)),
|
.collect::<HashMap<_, _>>();
|
||||||
)
|
|
||||||
.await
|
|
||||||
.into_iter()
|
|
||||||
.collect::<HashMap<_, _>>();
|
|
||||||
|
|
||||||
Ok(outcome)
|
Ok(outcome)
|
||||||
}
|
}
|
||||||
@ -151,13 +140,11 @@ where
|
|||||||
/// Validates the given transaction at the given block
|
/// Validates the given transaction at the given block
|
||||||
async fn validate(
|
async fn validate(
|
||||||
&self,
|
&self,
|
||||||
block_id: &BlockID,
|
|
||||||
_block_number: U64,
|
|
||||||
transaction: P::Transaction,
|
transaction: P::Transaction,
|
||||||
) -> (TxHash, TransactionValidationOutcome<P::Transaction>) {
|
) -> (TxHash, TransactionValidationOutcome<P::Transaction>) {
|
||||||
let _hash = *transaction.hash();
|
let _hash = *transaction.hash();
|
||||||
// TODO this is where additional validate checks would go, like banned senders etc...
|
// 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
|
// TODO blockstamp the transaction
|
||||||
|
|
||||||
@ -189,23 +176,16 @@ where
|
|||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_transaction(
|
async fn add_transaction(&self, transaction: Self::Transaction) -> PoolResult<TxHash> {
|
||||||
&self,
|
let (_, tx) = self.validate(transaction).await;
|
||||||
block_id: BlockID,
|
self.pool.add_transactions(std::iter::once(tx)).pop().expect("exists; qed")
|
||||||
transaction: Self::Transaction,
|
|
||||||
) -> PoolResult<TxHash> {
|
|
||||||
self.add_transactions(block_id, vec![transaction])
|
|
||||||
.await?
|
|
||||||
.pop()
|
|
||||||
.expect("transaction exists; qed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_transactions(
|
async fn add_transactions(
|
||||||
&self,
|
&self,
|
||||||
block_id: BlockID,
|
|
||||||
transactions: Vec<Self::Transaction>,
|
transactions: Vec<Self::Transaction>,
|
||||||
) -> PoolResult<Vec<PoolResult<TxHash>>> {
|
) -> 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());
|
let transactions = self.pool.add_transactions(validated.into_values());
|
||||||
Ok(transactions)
|
Ok(transactions)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,11 +23,7 @@ pub trait TransactionPool: Send + Sync {
|
|||||||
/// Adds an _unvalidated_ transaction into the pool.
|
/// Adds an _unvalidated_ transaction into the pool.
|
||||||
///
|
///
|
||||||
/// Consumer: RPC
|
/// Consumer: RPC
|
||||||
async fn add_transaction(
|
async fn add_transaction(&self, transaction: Self::Transaction) -> PoolResult<TxHash>;
|
||||||
&self,
|
|
||||||
block_id: BlockID,
|
|
||||||
transaction: Self::Transaction,
|
|
||||||
) -> PoolResult<TxHash>;
|
|
||||||
|
|
||||||
/// Adds the given _unvalidated_ transaction into the pool.
|
/// Adds the given _unvalidated_ transaction into the pool.
|
||||||
///
|
///
|
||||||
@ -36,7 +32,6 @@ pub trait TransactionPool: Send + Sync {
|
|||||||
/// Consumer: RPC
|
/// Consumer: RPC
|
||||||
async fn add_transactions(
|
async fn add_transactions(
|
||||||
&self,
|
&self,
|
||||||
block_id: BlockID,
|
|
||||||
transactions: Vec<Self::Transaction>,
|
transactions: Vec<Self::Transaction>,
|
||||||
) -> PoolResult<Vec<PoolResult<TxHash>>>;
|
) -> PoolResult<Vec<PoolResult<TxHash>>>;
|
||||||
|
|
||||||
|
|||||||
@ -38,7 +38,6 @@ pub trait TransactionValidator: Send + Sync {
|
|||||||
/// transactions for the sender.
|
/// transactions for the sender.
|
||||||
async fn validate_transaction(
|
async fn validate_transaction(
|
||||||
&self,
|
&self,
|
||||||
_block_id: &BlockID,
|
|
||||||
_transaction: Self::Transaction,
|
_transaction: Self::Transaction,
|
||||||
) -> TransactionValidationOutcome<Self::Transaction> {
|
) -> TransactionValidationOutcome<Self::Transaction> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
|
|||||||
Reference in New Issue
Block a user