Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use batch tx validation in eth validator #13490

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use alloy_eips::{
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_primitives::{InvalidTransactionError, SealedBlock};
use reth_primitives_traits::{BlockBody, GotExpected};
use reth_storage_api::{AccountReader, StateProviderFactory};
use reth_storage_api::{AccountReader, StateProvider, StateProviderFactory};
use reth_tasks::TaskSpawner;
use std::{
marker::PhantomData,
Expand Down Expand Up @@ -80,7 +80,7 @@ where
&self,
transactions: Vec<(TransactionOrigin, Tx)>,
) -> Vec<TransactionValidationOutcome<Tx>> {
transactions.into_iter().map(|(origin, tx)| self.validate_one(origin, tx)).collect()
self.inner.validate_batch(transactions)
}
}

Expand Down Expand Up @@ -176,10 +176,11 @@ where
Tx: EthPoolTransaction,
{
/// Validates a single transaction.
fn validate_one(
fn validate_one_with_provider(
mattsse marked this conversation as resolved.
Show resolved Hide resolved
&self,
origin: TransactionOrigin,
mut transaction: Tx,
provider: &mut Option<Box<dyn StateProvider>>,
mattsse marked this conversation as resolved.
Show resolved Hide resolved
) -> TransactionValidationOutcome<Tx> {
// Checks for tx_type
match transaction.tx_type() {
Expand Down Expand Up @@ -349,11 +350,23 @@ where
}
}

let account = match self
.client
.latest()
.and_then(|state| state.basic_account(transaction.sender()))
{
// Get or create provider
let state = if let Some(provider) = provider {
provider
} else {
match self.client.latest() {
Ok(new_provider) => {
*provider = Some(new_provider);
provider.as_ref().unwrap()
}
Err(err) => {
return TransactionValidationOutcome::Error(*transaction.hash(), Box::new(err))
}
}
};

// Use provider to get account info
let account = match state.basic_account(transaction.sender()) {
Ok(account) => account.unwrap_or_default(),
Err(err) => {
return TransactionValidationOutcome::Error(*transaction.hash(), Box::new(err))
Expand Down Expand Up @@ -479,6 +492,28 @@ where
}
}

/// Validates a single transaction.
fn validate_one(
&self,
origin: TransactionOrigin,
transaction: Tx,
) -> TransactionValidationOutcome<Tx> {
let mut provider = None;
self.validate_one_with_provider(origin, transaction, &mut provider)
}

/// Validates all given transactions.
fn validate_batch(
&self,
transactions: Vec<(TransactionOrigin, Tx)>,
) -> Vec<TransactionValidationOutcome<Tx>> {
let mut provider = None;
transactions
.into_iter()
.map(|(origin, tx)| self.validate_one_with_provider(origin, tx, &mut provider))
.collect()
}

fn on_new_head_block<T: BlockHeader>(&self, new_tip_block: &T) {
// update all forks
if self.chain_spec.is_cancun_active_at_timestamp(new_tip_block.timestamp()) {
Expand Down
Loading