feat: default impl for some BlockExecutionStrategy methods (#11941)

This commit is contained in:
Federico Gimenez
2024-10-21 20:47:50 +02:00
committed by GitHub
parent 3f2a41bd3f
commit d9d184d498
4 changed files with 28 additions and 23 deletions

View File

@ -21,7 +21,7 @@ use reth_evm::{
ConfigureEvm,
};
use reth_primitives::{BlockWithSenders, Receipt};
use reth_revm::db::{states::bundle_state::BundleRetention, BundleState, State};
use reth_revm::db::State;
use revm_primitives::{
db::{Database, DatabaseCommit},
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, U256,
@ -264,11 +264,6 @@ where
self.system_caller.with_state_hook(hook);
}
fn finish(&mut self) -> BundleState {
self.state.merge_transitions(BundleRetention::Reverts);
self.state.take_bundle()
}
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,

View File

@ -6,6 +6,7 @@ pub use reth_execution_errors::{
};
pub use reth_execution_types::{BlockExecutionInput, BlockExecutionOutput, ExecutionOutcome};
pub use reth_storage_errors::provider::ProviderError;
use revm::db::states::bundle_state::BundleRetention;
use crate::system_calls::OnStateHook;
use alloc::{boxed::Box, vec::Vec};
@ -176,7 +177,10 @@ pub struct ExecuteOutput {
}
/// Defines the strategy for executing a single block.
pub trait BlockExecutionStrategy<DB> {
pub trait BlockExecutionStrategy<DB>
where
DB: Database,
{
/// The error type returned by this strategy's methods.
type Error: From<ProviderError> + core::error::Error;
@ -209,18 +213,23 @@ pub trait BlockExecutionStrategy<DB> {
fn state_mut(&mut self) -> &mut State<DB>;
/// Sets a hook to be called after each state change during execution.
fn with_state_hook(&mut self, hook: Option<Box<dyn OnStateHook>>);
fn with_state_hook(&mut self, _hook: Option<Box<dyn OnStateHook>>) {}
/// Returns the final bundle state.
fn finish(&mut self) -> BundleState;
fn finish(&mut self) -> BundleState {
self.state_mut().merge_transitions(BundleRetention::Reverts);
self.state_mut().take_bundle()
}
/// Validate a block with regard to execution results.
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,
receipts: &[Receipt],
requests: &Requests,
) -> Result<(), ConsensusError>;
_block: &BlockWithSenders,
_receipts: &[Receipt],
_requests: &Requests,
) -> Result<(), ConsensusError> {
Ok(())
}
}
/// A strategy factory that can create block execution strategies.
@ -293,6 +302,7 @@ where
pub struct BasicBlockExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Block execution strategy.
pub(crate) strategy: S,
@ -302,6 +312,7 @@ where
impl<S, DB> BasicBlockExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Creates a new `BasicBlockExecutor` with the given strategy.
pub const fn new(strategy: S) -> Self {
@ -384,6 +395,7 @@ where
pub struct BasicBatchExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Batch execution strategy.
pub(crate) strategy: S,
@ -395,6 +407,7 @@ where
impl<S, DB> BasicBatchExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Creates a new `BasicBatchExecutor` with the given strategy.
pub const fn new(strategy: S, batch_record: BlockBatchRecord) -> Self {
@ -597,7 +610,10 @@ mod tests {
}
}
impl<DB> BlockExecutionStrategy<DB> for TestExecutorStrategy<DB, TestEvmConfig> {
impl<DB> BlockExecutionStrategy<DB> for TestExecutorStrategy<DB, TestEvmConfig>
where
DB: Database,
{
type Error = BlockExecutionError;
fn apply_pre_execution_changes(

View File

@ -119,6 +119,7 @@ impl<DB> BatchExecutor<DB> for MockExecutorProvider {
impl<S, DB> BasicBlockExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Provides safe read access to the state
pub fn with_state<F, R>(&self, f: F) -> R
@ -140,6 +141,7 @@ where
impl<S, DB> BasicBatchExecutor<S, DB>
where
S: BlockExecutionStrategy<DB>,
DB: Database,
{
/// Provides safe read access to the state
pub fn with_state<F, R>(&self, f: F) -> R

View File

@ -20,10 +20,7 @@ use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_consensus::validate_block_post_execution;
use reth_optimism_forks::OptimismHardfork;
use reth_primitives::{BlockWithSenders, Header, Receipt, TxType};
use reth_revm::{
db::{states::bundle_state::BundleRetention, BundleState},
Database, State,
};
use reth_revm::{Database, State};
use revm_primitives::{
db::DatabaseCommit, BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, U256,
};
@ -271,11 +268,6 @@ where
self.system_caller.with_state_hook(hook);
}
fn finish(&mut self) -> BundleState {
self.state.merge_transitions(BundleRetention::Reverts);
self.state.take_bundle()
}
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,