refactor: use EvmEnv when setting up Evm (#13800)

This commit is contained in:
Arsenii Kulikov
2025-01-15 20:09:03 +04:00
committed by GitHub
parent f2bf02413f
commit d5978a78b4
16 changed files with 551 additions and 585 deletions

View File

@ -9,6 +9,19 @@ pub struct EvmEnv {
pub block_env: BlockEnv,
}
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 {
/// Create a new `EvmEnv` from its components.
///

View File

@ -18,19 +18,18 @@
extern crate alloc;
use crate::builder::RethEvmBuilder;
use alloc::boxed::Box;
use alloy_consensus::BlockHeader as _;
use alloy_primitives::{Address, Bytes, B256, U256};
use reth_primitives_traits::{BlockHeader, SignedTransaction};
use revm::{Database, Evm, GetInspector};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, SpecId, TxEnv};
pub mod builder;
pub mod either;
/// EVM environment configuration.
pub mod env;
pub mod execute;
use env::EvmEnv;
pub use env::EvmEnv;
#[cfg(feature = "std")]
pub mod metrics;
@ -53,13 +52,14 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
}
/// Returns a new EVM with the given database configured with the given environment settings,
/// including the spec id.
/// including the spec id and transaction environment.
///
/// This will preserve any handler modifications
fn evm_with_env<DB: Database>(&self, db: DB, env: EnvWithHandlerCfg) -> Evm<'_, (), DB> {
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv, tx: TxEnv) -> Evm<'_, (), DB> {
let mut evm = self.evm(db);
evm.modify_spec_id(env.spec_id());
evm.context.evm.env = env.env;
evm.modify_spec_id(evm_env.cfg_env_with_handler_cfg.handler_cfg.spec_id);
evm.context.evm.env =
Env::boxed(evm_env.cfg_env_with_handler_cfg.cfg_env, evm_env.block_env, tx);
evm
}
@ -71,17 +71,8 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
///
/// This does not initialize the tx environment.
fn evm_for_block<DB: Database>(&self, db: DB, header: &Self::Header) -> Evm<'_, (), DB> {
let EvmEnv {
cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg { cfg_env, handler_cfg },
block_env,
} = self.cfg_and_block_env(header);
self.evm_with_env(
db,
EnvWithHandlerCfg {
env: Box::new(Env { cfg: cfg_env, block: block_env, tx: Default::default() }),
handler_cfg,
},
)
let evm_env = self.cfg_and_block_env(header);
self.evm_with_env(db, evm_env, Default::default())
}
/// Returns a new EVM with the given database configured with the given environment settings,
@ -93,7 +84,8 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
env: EnvWithHandlerCfg,
evm_env: EvmEnv,
tx: TxEnv,
inspector: I,
) -> Evm<'_, I, DB>
where
@ -101,8 +93,9 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
I: GetInspector<DB>,
{
let mut evm = self.evm_with_inspector(db, inspector);
evm.modify_spec_id(env.spec_id());
evm.context.evm.env = env.env;
evm.modify_spec_id(evm_env.cfg_env_with_handler_cfg.handler_cfg.spec_id);
evm.context.evm.env =
Env::boxed(evm_env.cfg_env_with_handler_cfg.cfg_env, evm_env.block_env, tx);
evm
}
@ -133,21 +126,22 @@ where
(*self).evm_for_block(db, header)
}
fn evm_with_env<DB: Database>(&self, db: DB, env: EnvWithHandlerCfg) -> Evm<'_, (), DB> {
(*self).evm_with_env(db, env)
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv, tx: TxEnv) -> Evm<'_, (), DB> {
(*self).evm_with_env(db, evm_env, tx)
}
fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
env: EnvWithHandlerCfg,
evm_env: EvmEnv,
tx_env: TxEnv,
inspector: I,
) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
(*self).evm_with_env_and_inspector(db, env, inspector)
(*self).evm_with_env_and_inspector(db, evm_env, tx_env, inspector)
}
fn evm_with_inspector<DB, I>(&self, db: DB, inspector: I) -> Evm<'_, I, DB>