feat: add Spec generic for EvmEnv (#13975)

This commit is contained in:
Arsenii Kulikov
2025-01-24 19:59:55 +04:00
committed by GitHub
parent 621b30f037
commit 203fed0f64
19 changed files with 223 additions and 293 deletions

View File

@ -1,36 +1,27 @@
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
use revm_primitives::{CfgEnv, SpecId};
/// Container type that holds both the configuration and block environment for EVM execution.
#[derive(Debug, Clone)]
pub struct EvmEnv {
#[derive(Debug, Clone, Default)]
pub struct EvmEnv<Spec = SpecId> {
/// The configuration environment with handler settings
pub cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg,
pub cfg_env: CfgEnv,
/// The block environment containing block-specific data
pub block_env: BlockEnv,
/// The spec id of the chain. Specifies which hardfork is currently active, `Spec` type will
/// most likely be an enum over hardforks.
pub spec: Spec,
}
impl Default for EvmEnv {
fn default() -> Self {
Self {
cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg {
cfg_env: Default::default(),
// Will set `is_optimism` if `revm/optimism-default-handler` is enabled.
handler_cfg: Default::default(),
},
block_env: BlockEnv::default(),
}
}
}
impl EvmEnv {
impl<Spec> EvmEnv<Spec> {
/// 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 }
pub const fn new(cfg_env: CfgEnv, block_env: BlockEnv, spec: Spec) -> Self {
Self { cfg_env, spec, block_env }
}
/// Returns a reference to the block environment.
@ -39,19 +30,19 @@ impl EvmEnv {
}
/// Returns a reference to the configuration environment.
pub const fn cfg_env_with_handler_cfg(&self) -> &CfgEnvWithHandlerCfg {
&self.cfg_env_with_handler_cfg
pub const fn cfg_env(&self) -> &CfgEnv {
&self.cfg_env
}
/// Returns the spec id of the chain
pub const fn spec_id(&self) -> &Spec {
&self.spec
}
}
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)
let CfgEnvWithHandlerCfg { cfg_env, handler_cfg } = cfg_env_with_handler_cfg;
Self { cfg_env, spec: handler_cfg.spec_id, block_env }
}
}

View File

@ -57,9 +57,6 @@ pub trait Evm {
/// Reference to [`BlockEnv`].
fn block(&self) -> &BlockEnv;
/// Consumes the type and returns the underlying [`EvmEnv`].
fn into_env(self) -> EvmEnv;
/// Executes the given transaction.
fn transact(&mut self, tx: Self::Tx) -> Result<ResultAndState, Self::Error>;
@ -99,7 +96,11 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
/// including the spec id and transaction environment.
///
/// This will preserve any handler modifications
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()>;
fn evm_with_env<DB: Database>(
&self,
db: DB,
evm_env: EvmEnv<Self::Spec>,
) -> Self::Evm<'_, DB, ()>;
/// Returns a new EVM with the given database configured with `cfg` and `block_env`
/// configuration derived from the given header. Relies on
@ -122,7 +123,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
evm_env: EvmEnv<Self::Spec>,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
@ -133,7 +134,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
impl<'b, T> ConfigureEvm for &'b T
where
T: ConfigureEvm,
&'b T: ConfigureEvmEnv<Header = T::Header, TxEnv = T::TxEnv>,
&'b T: ConfigureEvmEnv<Header = T::Header, TxEnv = T::TxEnv, Spec = T::Spec>,
{
type Evm<'a, DB: Database + 'a, I: 'a> = T::Evm<'a, DB, I>;
@ -141,14 +142,18 @@ where
(*self).evm_for_block(db, header)
}
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()> {
fn evm_with_env<DB: Database>(
&self,
db: DB,
evm_env: EvmEnv<Self::Spec>,
) -> Self::Evm<'_, DB, ()> {
(*self).evm_with_env(db, evm_env)
}
fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
evm_env: EvmEnv<Self::Spec>,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
@ -177,11 +182,14 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
/// The error type that is returned by [`Self::next_evm_env`].
type Error: core::error::Error + Send + Sync;
/// Identifier of the EVM specification.
type Spec: Into<revm_primitives::SpecId> + Debug + Copy + Send + Sync;
/// Returns a [`TxEnv`] from a transaction and [`Address`].
fn tx_env(&self, transaction: &Self::Transaction, signer: Address) -> Self::TxEnv;
/// Creates a new [`EvmEnv`] for the given header.
fn evm_env(&self, header: &Self::Header) -> EvmEnv;
fn evm_env(&self, header: &Self::Header) -> EvmEnv<Self::Spec>;
/// Returns the configured [`EvmEnv`] for `parent + 1` block.
///
@ -192,7 +200,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<EvmEnv, Self::Error>;
) -> Result<EvmEnv<Self::Spec>, Self::Error>;
}
/// Represents additional attributes required to configure the next block.

View File

@ -123,7 +123,7 @@ where
pub fn pre_block_blockhashes_contract_call<DB>(
&mut self,
db: &mut DB,
evm_env: &EvmEnv,
evm_env: &EvmEnv<EvmConfig::Spec>,
parent_block_hash: B256,
) -> Result<(), BlockExecutionError>
where
@ -173,7 +173,7 @@ where
pub fn pre_block_beacon_root_contract_call<DB>(
&mut self,
db: &mut DB,
evm_env: &EvmEnv,
evm_env: &EvmEnv<EvmConfig::Spec>,
parent_beacon_block_root: Option<B256>,
) -> Result<(), BlockExecutionError>
where
@ -223,7 +223,7 @@ where
pub fn post_block_withdrawal_requests_contract_call<DB>(
&mut self,
db: &mut DB,
evm_env: &EvmEnv,
evm_env: &EvmEnv<EvmConfig::Spec>,
) -> Result<Bytes, BlockExecutionError>
where
DB: Database + DatabaseCommit,
@ -256,7 +256,7 @@ where
pub fn post_block_consolidation_requests_contract_call<DB>(
&mut self,
db: &mut DB,
evm_env: &EvmEnv,
evm_env: &EvmEnv<EvmConfig::Spec>,
) -> Result<Bytes, BlockExecutionError>
where
DB: Database + DatabaseCommit,