Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use reth_primitives::{
SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned, TxEip4844, B256, U256,
};
use reth_provider::{
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ExecutorFactory,
ProviderFactory, StageCheckpointReader, StateProviderFactory,
providers::BlockchainProvider, BlockExecutor, BlockHashReader, BlockReader, BlockWriter,
ExecutorFactory, ProviderFactory, StageCheckpointReader, StateProviderFactory,
};
use reth_revm::EvmProcessorFactory;
#[cfg(feature = "optimism")]
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_network_api::NetworkInfo;
use reth_node_ethereum::EthEvmConfig;
use reth_primitives::{fs, stage::StageId, BlockHashOrNumber, ChainSpec};
use reth_provider::{
AccountExtReader, BlockWriter, ExecutorFactory, HashingWriter, HeaderProvider,
AccountExtReader, BlockExecutor, BlockWriter, ExecutorFactory, HashingWriter, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
StorageReader,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use reth_primitives::{
BlockHash, BlockNumber, ForkBlock, GotExpected, SealedBlockWithSenders, SealedHeader, U256,
};
use reth_provider::{
providers::BundleStateProvider, BundleStateDataProvider, BundleStateWithReceipts, Chain,
ExecutorFactory, StateRootProvider,
providers::BundleStateProvider, BlockExecutor, BundleStateDataProvider,
BundleStateWithReceipts, Chain, ExecutorFactory, StateRootProvider,
};
use reth_trie::updates::TrieUpdates;
use std::{
Expand Down
6 changes: 3 additions & 3 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl StorageInner {
pub(crate) fn execute<EvmConfig>(
&mut self,
block: &BlockWithSenders,
executor: &mut EVMProcessor<'_, EvmConfig>,
mut executor: EVMProcessor<'_, EvmConfig>,
) -> Result<(BundleStateWithReceipts, u64), BlockExecutionError>
where
EvmConfig: ConfigureEvmEnv,
Expand Down Expand Up @@ -447,9 +447,9 @@ impl StorageInner {
.with_database_boxed(Box::new(StateProviderDatabase::new(client.latest().unwrap())))
.with_bundle_update()
.build();
let mut executor = EVMProcessor::new_with_state(chain_spec.clone(), db, evm_config);
let executor = EVMProcessor::new_with_state(chain_spec.clone(), db, evm_config);

let (bundle_state, gas_used) = self.execute(&block, &mut executor)?;
let (bundle_state, gas_used) = self.execute(&block, executor)?;

let Block { header, body, .. } = block.block;
let body = BlockBody { transactions: body, ommers: vec![], withdrawals: None };
Expand Down
97 changes: 89 additions & 8 deletions crates/consensus/beacon/src/engine/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use reth_node_ethereum::{EthEngineTypes, EthEvmConfig};
use reth_payload_builder::test_utils::spawn_test_payload_service;
use reth_primitives::{BlockNumber, ChainSpec, B256};
use reth_provider::{
providers::BlockchainProvider, test_utils::TestExecutorFactory, BundleStateWithReceipts,
ExecutorFactory, HeaderSyncMode, ProviderFactory, PrunableBlockExecutor,
providers::BlockchainProvider, test_utils::TestExecutorFactory, BlockExecutor,
BundleStateWithReceipts, ExecutorFactory, HeaderSyncMode, ProviderFactory,
PrunableBlockExecutor,
};
use reth_prune::Pruner;
use reth_revm::EvmProcessorFactory;
Expand Down Expand Up @@ -166,18 +167,98 @@ pub enum EitherExecutorFactory<A: ExecutorFactory, B: ExecutorFactory> {
Right(B),
}

/// The type used for associated types in EitherExecutorFactory.
#[derive(Debug)]
pub enum EitherExecutor<A, B> {
/// The first executor variant
Left(A),
/// The second executor variant
Right(B),
}

impl<A, B> BlockExecutor for EitherExecutor<A, B>
where
A: BlockExecutor,
B: BlockExecutor,
{
fn execute(
&mut self,
block: &reth_primitives::BlockWithSenders,
total_difficulty: reth_primitives::U256,
) -> Result<(), reth_interfaces::executor::BlockExecutionError> {
match self {
EitherExecutor::Left(a) => a.execute(block, total_difficulty),
EitherExecutor::Right(b) => b.execute(block, total_difficulty),
}
}

fn size_hint(&self) -> Option<usize> {
match self {
EitherExecutor::Left(a) => a.size_hint(),
EitherExecutor::Right(b) => b.size_hint(),
}
}

fn take_output_state(self) -> BundleStateWithReceipts {
match self {
EitherExecutor::Left(a) => a.take_output_state(),
EitherExecutor::Right(b) => b.take_output_state(),
}
}
fn execute_transactions(
&mut self,
block: &reth_primitives::BlockWithSenders,
total_difficulty: reth_primitives::U256,
) -> Result<(Vec<reth_primitives::Receipt>, u64), reth_interfaces::executor::BlockExecutionError>
{
match self {
EitherExecutor::Left(a) => a.execute_transactions(block, total_difficulty),
EitherExecutor::Right(b) => b.execute_transactions(block, total_difficulty),
}
}
fn execute_and_verify_receipt(
&mut self,
block: &reth_primitives::BlockWithSenders,
total_difficulty: reth_primitives::U256,
) -> Result<(), reth_interfaces::executor::BlockExecutionError> {
match self {
EitherExecutor::Left(a) => a.execute_and_verify_receipt(block, total_difficulty),
EitherExecutor::Right(b) => b.execute_and_verify_receipt(block, total_difficulty),
}
}
}

impl<A, B> PrunableBlockExecutor for EitherExecutor<A, B>
where
A: PrunableBlockExecutor,
B: PrunableBlockExecutor,
{
fn set_tip(&mut self, tip: BlockNumber) {
match self {
EitherExecutor::Left(a) => a.set_tip(tip),
EitherExecutor::Right(b) => b.set_tip(tip),
}
}

fn set_prune_modes(&mut self, prune_modes: reth_primitives::PruneModes) {
match self {
EitherExecutor::Left(a) => a.set_prune_modes(prune_modes),
EitherExecutor::Right(b) => b.set_prune_modes(prune_modes),
}
}
}

impl<A, B> ExecutorFactory for EitherExecutorFactory<A, B>
where
A: ExecutorFactory,
B: ExecutorFactory,
{
fn with_state<'a, SP: reth_provider::StateProvider + 'a>(
&'a self,
sp: SP,
) -> Box<dyn PrunableBlockExecutor + 'a> {
type Executor = EitherExecutor<A::Executor<'static>, B::Executor<'static>>;

fn with_state<SP: reth_provider::StateProvider + 'static>(&self, sp: SP) -> Self::Executor {
match self {
EitherExecutorFactory::Left(a) => a.with_state::<'a, SP>(sp),
EitherExecutorFactory::Right(b) => b.with_state::<'a, SP>(sp),
EitherExecutorFactory::Left(a) => EitherExecutor::Left(a.with_state::<SP>(sp)),
EitherExecutorFactory::Right(b) => EitherExecutor::Right(b.with_state::<SP>(sp)),
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions crates/revm/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use reth_node_api::ConfigureEvmEnv;
use reth_primitives::ChainSpec;
use reth_provider::{ExecutorFactory, PrunableBlockExecutor, StateProvider};
use reth_provider::{ExecutorFactory, StateProvider};
use std::sync::Arc;

/// Factory for creating [EVMProcessor].
Expand Down Expand Up @@ -40,16 +40,15 @@ impl<EvmConfig> ExecutorFactory for EvmProcessorFactory<EvmConfig>
where
EvmConfig: ConfigureEvmEnv + Send + Sync + Clone + 'static,
{
fn with_state<'a, SP: StateProvider + 'a>(
&'a self,
sp: SP,
) -> Box<dyn PrunableBlockExecutor + 'a> {
type Executor = EVMProcessor<'static, EvmConfig>;

fn with_state<SP: StateProvider + 'static>(&self, sp: SP) -> Self::Executor {
let database_state = StateProviderDatabase::new(sp);
let mut evm = Box::new(EVMProcessor::new_with_db(
let mut evm = EVMProcessor::new_with_db(
self.chain_spec.clone(),
database_state,
self.evm_config.clone(),
));
);
if let Some(ref stack) = self.stack {
evm.set_stack(stack.clone());
}
Expand Down
10 changes: 1 addition & 9 deletions crates/revm/src/optimism/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_primitives::{
proofs::calculate_receipt_root_optimism, revm_primitives::ResultAndState, BlockWithSenders,
Bloom, ChainSpec, Hardfork, Receipt, ReceiptWithBloom, TxType, B256, U256,
};
use reth_provider::{BlockExecutor, BlockExecutorStats, BundleStateWithReceipts};
use reth_provider::{BlockExecutor, BundleStateWithReceipts};
use revm::DatabaseCommit;
use std::time::Instant;
use tracing::{debug, trace};
Expand Down Expand Up @@ -195,12 +195,4 @@ where
self.first_block.unwrap_or_default(),
)
}

fn stats(&self) -> BlockExecutorStats {
self.stats.clone()
}

fn size_hint(&self) -> Option<usize> {
Some(self.evm.context.evm.db.bundle_size_hint())
}
}
13 changes: 5 additions & 8 deletions crates/revm/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,19 +499,16 @@ where
Ok((receipts, cumulative_gas_used))
}

fn take_output_state(&mut self) -> BundleStateWithReceipts {
let receipts = std::mem::take(&mut self.receipts);
fn take_output_state(self) -> BundleStateWithReceipts {
// log the stats whenever we take the output state.
self.stats.log_info();
BundleStateWithReceipts::new(
self.evm.context.evm.db.take_bundle(),
receipts,
self.evm.context.evm.db.bundle_state,
self.receipts,
self.first_block.unwrap_or_default(),
)
}

fn stats(&self) -> BlockExecutorStats {
self.stats.clone()
}

fn size_hint(&self) -> Option<usize> {
Some(self.evm.context.evm.db.bundle_size_hint())
}
Expand Down
7 changes: 3 additions & 4 deletions crates/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use reth_primitives::{
BlockNumber, Header, PruneModes, U256,
};
use reth_provider::{
BlockReader, DatabaseProviderRW, ExecutorFactory, HeaderProvider, LatestStateProviderRef,
OriginalValuesKnown, ProviderError, TransactionVariant,
BlockExecutor, BlockReader, DatabaseProviderRW, ExecutorFactory, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderError, PrunableBlockExecutor,
TransactionVariant,
};
use std::{
ops::RangeInclusive,
Expand Down Expand Up @@ -206,8 +207,6 @@ impl<EF: ExecutorFactory> ExecutionStage<EF> {
"Execution time"
);

executor.stats().log_info();

let done = stage_progress == max_block;
Ok(ExecOutput {
checkpoint: StageCheckpoint::new(stage_progress)
Expand Down
21 changes: 8 additions & 13 deletions crates/storage/provider/src/test_utils/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
bundle_state::BundleStateWithReceipts, BlockExecutor, BlockExecutorStats, ExecutorFactory,
PrunableBlockExecutor, StateProvider,
bundle_state::BundleStateWithReceipts, BlockExecutor, ExecutorFactory, PrunableBlockExecutor,
StateProvider,
};
use parking_lot::Mutex;
use reth_interfaces::executor::BlockExecutionError;
Expand Down Expand Up @@ -41,12 +41,8 @@ impl BlockExecutor for TestExecutor {
Err(BlockExecutionError::UnavailableForTest)
}

fn take_output_state(&mut self) -> BundleStateWithReceipts {
self.0.clone().unwrap_or_default()
}

fn stats(&self) -> BlockExecutorStats {
BlockExecutorStats::default()
fn take_output_state(self) -> BundleStateWithReceipts {
self.0.unwrap_or_default()
}

fn size_hint(&self) -> Option<usize> {
Expand Down Expand Up @@ -74,11 +70,10 @@ impl TestExecutorFactory {
}

impl ExecutorFactory for TestExecutorFactory {
fn with_state<'a, SP: StateProvider + 'a>(
&'a self,
_sp: SP,
) -> Box<dyn PrunableBlockExecutor + 'a> {
type Executor = TestExecutor;

fn with_state<SP: StateProvider>(&self, _sp: SP) -> Self::Executor {
let exec_res = self.exec_results.lock().pop();
Box::new(TestExecutor(exec_res))
TestExecutor(exec_res)
}
}
14 changes: 6 additions & 8 deletions crates/storage/provider/src/traits/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use tracing::debug;
///
/// It can be used to mock executor.
pub trait ExecutorFactory: Send + Sync + 'static {
/// Type of block executor to return, this must be an associated type so the executor can be
/// consumed when it's done executing.
type Executor: PrunableBlockExecutor;

/// Executor with [`StateProvider`]
fn with_state<'a, SP: StateProvider + 'a>(
&'a self,
_sp: SP,
) -> Box<dyn PrunableBlockExecutor + 'a>;
fn with_state<SP: StateProvider + 'static>(&self, _sp: SP) -> Self::Executor;
}

/// An executor capable of executing a block.
Expand Down Expand Up @@ -54,10 +55,7 @@ pub trait BlockExecutor {
) -> Result<(Vec<Receipt>, u64), BlockExecutionError>;

/// Return bundle state. This is output of executed blocks.
fn take_output_state(&mut self) -> BundleStateWithReceipts;

/// Internal statistics of execution.
fn stats(&self) -> BlockExecutorStats;
fn take_output_state(self) -> BundleStateWithReceipts;

/// Returns the size hint of current in-memory changes.
fn size_hint(&self) -> Option<usize>;
Expand Down