chore: Introduce helper type for evm cfg and env tuple (#13377)

This commit is contained in:
Ayodeji Akinola
2024-12-14 09:53:59 +01:00
committed by GitHub
parent 16f6d7a0c3
commit b525231224
33 changed files with 290 additions and 165 deletions

44
crates/evm/src/env.rs Normal file
View File

@ -0,0 +1,44 @@
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
/// Container type that holds both the configuration and block environment for EVM execution.
#[derive(Debug, Clone)]
pub struct EvmEnv {
/// The configuration environment with handler settings
pub cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg,
/// The block environment containing block-specific data
pub block_env: BlockEnv,
}
impl EvmEnv {
/// Create a new `EvmEnv` from its components.
///
/// # Arguments
///
/// * `cfg_env_with_handler_cfg` - The configuration environment with handler settings
/// * `block` - The block environment containing block-specific data
pub const fn new(cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg, block_env: BlockEnv) -> Self {
Self { cfg_env_with_handler_cfg, block_env }
}
/// Returns a reference to the block environment.
pub const fn block_env(&self) -> &BlockEnv {
&self.block_env
}
/// Returns a reference to the configuration environment.
pub const fn cfg_env_with_handler_cfg(&self) -> &CfgEnvWithHandlerCfg {
&self.cfg_env_with_handler_cfg
}
}
impl From<(CfgEnvWithHandlerCfg, BlockEnv)> for EvmEnv {
fn from((cfg_env_with_handler_cfg, block_env): (CfgEnvWithHandlerCfg, BlockEnv)) -> Self {
Self { cfg_env_with_handler_cfg, block_env }
}
}
impl From<EvmEnv> for (CfgEnvWithHandlerCfg, BlockEnv) {
fn from(env: EvmEnv) -> Self {
(env.cfg_env_with_handler_cfg, env.block_env)
}
}

View File

@ -26,7 +26,11 @@ use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, Sp
pub mod builder;
pub mod either;
/// EVM environment configuration.
pub mod env;
pub mod execute;
use env::EvmEnv;
#[cfg(feature = "std")]
pub mod metrics;
pub mod noop;
@ -179,16 +183,12 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
}
}
/// Creates a new [`CfgEnvWithHandlerCfg`] and [`BlockEnv`] for the given header.
fn cfg_and_block_env(
&self,
header: &Self::Header,
total_difficulty: U256,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
/// Creates a new [`EvmEnv`] for the given header.
fn cfg_and_block_env(&self, header: &Self::Header, total_difficulty: U256) -> EvmEnv {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
self.fill_cfg_and_block_env(&mut cfg, &mut block_env, header, total_difficulty);
(cfg, block_env)
EvmEnv::new(cfg, block_env)
}
/// Convenience function to call both [`fill_cfg_env`](ConfigureEvmEnv::fill_cfg_env) and
@ -207,7 +207,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
self.fill_block_env(block_env, header, after_merge);
}
/// Returns the configured [`CfgEnvWithHandlerCfg`] and [`BlockEnv`] for `parent + 1` block.
/// Returns the configured [`EvmEnv`] for `parent + 1` block.
///
/// This is intended for usage in block building after the merge and requires additional
/// attributes that can't be derived from the parent block: attributes that are determined by
@ -216,7 +216,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error>;
) -> Result<EvmEnv, Self::Error>;
}
/// Represents additional attributes required to configure the next block.

View File

@ -1,24 +1,23 @@
//! Provider trait for populating the EVM environment.
use crate::ConfigureEvmEnv;
use crate::{env::EvmEnv, ConfigureEvmEnv};
use alloy_consensus::Header;
use reth_storage_errors::provider::ProviderResult;
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
/// A provider type that knows chain specific information required to configure a
/// [`CfgEnvWithHandlerCfg`].
/// [`EvmEnv`].
///
/// This type is mainly used to provide required data to configure the EVM environment that is
/// not part of the block and stored separately (on disk), for example the total difficulty.
#[auto_impl::auto_impl(&, Arc)]
pub trait EvmEnvProvider<H = Header>: Send + Sync {
/// Fills the default [`CfgEnvWithHandlerCfg`] and [BlockEnv] fields with values specific to the
/// Fills the default [`EvmEnv`] fields with values specific to the
/// given block header.
fn env_with_header<EvmConfig>(
&self,
header: &H,
evm_config: EvmConfig,
) -> ProviderResult<(CfgEnvWithHandlerCfg, BlockEnv)>
) -> ProviderResult<EvmEnv>
where
EvmConfig: ConfigureEvmEnv<Header = H>;
}

View File

@ -1,6 +1,7 @@
//! Helpers for testing.
use crate::{
env::EvmEnv,
execute::{
BasicBatchExecutor, BasicBlockExecutor, BatchExecutor, BlockExecutionInput,
BlockExecutionOutput, BlockExecutionStrategy, BlockExecutorProvider, Executor,
@ -18,7 +19,7 @@ use reth_primitives::{BlockWithSenders, EthPrimitives, NodePrimitives, Receipt,
use reth_prune_types::PruneModes;
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use revm::State;
use revm_primitives::{db::Database, BlockEnv, CfgEnvWithHandlerCfg};
use revm_primitives::db::Database;
use std::{fmt::Display, sync::Arc};
impl<C: Send + Sync, N: NodePrimitives> EvmEnvProvider<N::BlockHeader>
@ -28,7 +29,7 @@ impl<C: Send + Sync, N: NodePrimitives> EvmEnvProvider<N::BlockHeader>
&self,
header: &N::BlockHeader,
evm_config: EvmConfig,
) -> ProviderResult<(CfgEnvWithHandlerCfg, BlockEnv)>
) -> ProviderResult<EvmEnv>
where
EvmConfig: ConfigureEvmEnv<Header = N::BlockHeader>,
{