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

chore: introduce ExecuteOutput #11929

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
6 changes: 3 additions & 3 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
execute::{
BasicBlockExecutorProvider, BlockExecutionError, BlockExecutionStrategy,
BlockExecutionStrategyFactory, BlockValidationError, ProviderError,
BlockExecutionStrategyFactory, BlockValidationError, ExecuteOutput, ProviderError,
},
state_change::post_block_balance_increments,
system_calls::{OnStateHook, SystemCaller},
Expand Down Expand Up @@ -152,7 +152,7 @@ where
&mut self,
block: &BlockWithSenders,
total_difficulty: U256,
) -> Result<(Vec<Receipt>, u64), Self::Error> {
) -> Result<ExecuteOutput, Self::Error> {
let env = self.evm_env_for_block(&block.header, total_difficulty);
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);

Expand Down Expand Up @@ -203,7 +203,7 @@ where
},
);
}
Ok((receipts, cumulative_gas_used))
Ok(ExecuteOutput { receipts, gas_used: cumulative_gas_used })
}

fn apply_post_execution_changes(
Expand Down
32 changes: 23 additions & 9 deletions crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static {
DB: Database<Error: Into<ProviderError> + Display>;
}

/// Helper type for the output of executing a block.
#[derive(Debug, Clone)]
pub struct ExecuteOutput {
/// Receipts obtained after executing a block.
pub receipts: Vec<Receipt>,
/// Cumulative gas used in the block execution.
pub gas_used: u64,
}

/// Defines the strategy for executing a single block.
pub trait BlockExecutionStrategy<DB> {
/// The error type returned by this strategy's methods.
Expand All @@ -183,7 +192,7 @@ pub trait BlockExecutionStrategy<DB> {
&mut self,
block: &BlockWithSenders,
total_difficulty: U256,
) -> Result<(Vec<Receipt>, u64), Self::Error>;
) -> Result<ExecuteOutput, Self::Error>;

/// Applies any necessary changes after executing the block's transactions.
fn apply_post_execution_changes(
Expand Down Expand Up @@ -313,7 +322,8 @@ where
let BlockExecutionInput { block, total_difficulty } = input;

self.strategy.apply_pre_execution_changes(block, total_difficulty)?;
let (receipts, gas_used) = self.strategy.execute_transactions(block, total_difficulty)?;
let ExecuteOutput { receipts, gas_used } =
self.strategy.execute_transactions(block, total_difficulty)?;
let requests =
self.strategy.apply_post_execution_changes(block, total_difficulty, &receipts)?;
let state = self.strategy.finish();
Expand All @@ -332,7 +342,8 @@ where
let BlockExecutionInput { block, total_difficulty } = input;

self.strategy.apply_pre_execution_changes(block, total_difficulty)?;
let (receipts, gas_used) = self.strategy.execute_transactions(block, total_difficulty)?;
let ExecuteOutput { receipts, gas_used } =
self.strategy.execute_transactions(block, total_difficulty)?;
let requests =
self.strategy.apply_post_execution_changes(block, total_difficulty, &receipts)?;

Expand All @@ -356,7 +367,8 @@ where
self.strategy.with_state_hook(Some(Box::new(state_hook)));

self.strategy.apply_pre_execution_changes(block, total_difficulty)?;
let (receipts, gas_used) = self.strategy.execute_transactions(block, total_difficulty)?;
let ExecuteOutput { receipts, gas_used } =
self.strategy.execute_transactions(block, total_difficulty)?;
let requests =
self.strategy.apply_post_execution_changes(block, total_difficulty, &receipts)?;

Expand Down Expand Up @@ -407,7 +419,8 @@ where
}

self.strategy.apply_pre_execution_changes(block, total_difficulty)?;
let (receipts, _gas_used) = self.strategy.execute_transactions(block, total_difficulty)?;
let ExecuteOutput { receipts, .. } =
self.strategy.execute_transactions(block, total_difficulty)?;
let requests =
self.strategy.apply_post_execution_changes(block, total_difficulty, &receipts)?;

Expand Down Expand Up @@ -545,14 +558,14 @@ mod tests {
_chain_spec: Arc<ChainSpec>,
_evm_config: EvmConfig,
state: State<DB>,
execute_transactions_result: (Vec<Receipt>, u64),
execute_transactions_result: ExecuteOutput,
apply_post_execution_changes_result: Requests,
finish_result: BundleState,
}

#[derive(Clone)]
struct TestExecutorStrategyFactory {
execute_transactions_result: (Vec<Receipt>, u64),
execute_transactions_result: ExecuteOutput,
apply_post_execution_changes_result: Requests,
finish_result: BundleState,
}
Expand Down Expand Up @@ -599,7 +612,7 @@ mod tests {
&mut self,
_block: &BlockWithSenders,
_total_difficulty: U256,
) -> Result<(Vec<Receipt>, u64), Self::Error> {
) -> Result<ExecuteOutput, Self::Error> {
Ok(self.execute_transactions_result.clone())
}

Expand Down Expand Up @@ -651,7 +664,8 @@ mod tests {
fn test_strategy() {
let expected_gas_used = 10;
let expected_receipts = vec![Receipt::default()];
let expected_execute_transactions_result = (expected_receipts.clone(), expected_gas_used);
let expected_execute_transactions_result =
ExecuteOutput { receipts: expected_receipts.clone(), gas_used: expected_gas_used };
let expected_apply_post_execution_changes_result = Requests::new(vec![bytes!("deadbeef")]);
let expected_finish_result = BundleState::default();

Expand Down
6 changes: 3 additions & 3 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_consensus::ConsensusError;
use reth_evm::{
execute::{
BasicBlockExecutorProvider, BlockExecutionError, BlockExecutionStrategy,
BlockExecutionStrategyFactory, BlockValidationError, ProviderError,
BlockExecutionStrategyFactory, BlockValidationError, ExecuteOutput, ProviderError,
},
state_change::post_block_balance_increments,
system_calls::{OnStateHook, SystemCaller},
Expand Down Expand Up @@ -155,7 +155,7 @@ where
&mut self,
block: &BlockWithSenders,
total_difficulty: U256,
) -> Result<(Vec<Receipt>, u64), Self::Error> {
) -> Result<ExecuteOutput, Self::Error> {
let env = self.evm_env_for_block(&block.header, total_difficulty);
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);

Expand Down Expand Up @@ -240,7 +240,7 @@ where
});
}

Ok((receipts, cumulative_gas_used))
Ok(ExecuteOutput { receipts, gas_used: cumulative_gas_used })
}

fn apply_post_execution_changes(
Expand Down
Loading