mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-02-04 12:05:12 -05:00
feat: add evm_for_block helper to simplify EVM setup (#13787)
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
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};
|
||||
@@ -41,7 +42,6 @@ pub mod system_calls;
|
||||
pub mod test_utils;
|
||||
|
||||
/// Trait for configuring the EVM for executing full blocks.
|
||||
#[auto_impl::auto_impl(&, Arc)]
|
||||
pub trait ConfigureEvm: ConfigureEvmEnv {
|
||||
/// Associated type for the default external context that should be configured for the EVM.
|
||||
type DefaultExternalContext<'a>;
|
||||
@@ -70,6 +70,31 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
|
||||
evm
|
||||
}
|
||||
|
||||
/// Returns a new EVM with the given database configured with `cfg` and `block_env`
|
||||
/// configuration derived from the given header. Relies on
|
||||
/// [`ConfigureEvmEnv::cfg_and_block_env`].
|
||||
///
|
||||
/// # Caution
|
||||
///
|
||||
/// This does not initialize the tx environment.
|
||||
fn evm_for_block<DB: Database>(
|
||||
&self,
|
||||
db: DB,
|
||||
header: &Self::Header,
|
||||
) -> Evm<'_, Self::DefaultExternalContext<'_>, 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,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns a new EVM with the given database configured with the given environment settings,
|
||||
/// including the spec id.
|
||||
///
|
||||
@@ -109,6 +134,59 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
|
||||
fn default_external_context<'a>(&self) -> Self::DefaultExternalContext<'a>;
|
||||
}
|
||||
|
||||
impl<'b, T> ConfigureEvm for &'b T
|
||||
where
|
||||
T: ConfigureEvm,
|
||||
&'b T: ConfigureEvmEnv<Header = T::Header>,
|
||||
{
|
||||
type DefaultExternalContext<'a> = T::DefaultExternalContext<'a>;
|
||||
|
||||
fn default_external_context<'a>(&self) -> Self::DefaultExternalContext<'a> {
|
||||
(*self).default_external_context()
|
||||
}
|
||||
|
||||
fn evm<DB: Database>(&self, db: DB) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
|
||||
(*self).evm(db)
|
||||
}
|
||||
|
||||
fn evm_for_block<DB: Database>(
|
||||
&self,
|
||||
db: DB,
|
||||
header: &Self::Header,
|
||||
) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
|
||||
(*self).evm_for_block(db, header)
|
||||
}
|
||||
|
||||
fn evm_with_env<DB: Database>(
|
||||
&self,
|
||||
db: DB,
|
||||
env: EnvWithHandlerCfg,
|
||||
) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
|
||||
(*self).evm_with_env(db, env)
|
||||
}
|
||||
|
||||
fn evm_with_env_and_inspector<DB, I>(
|
||||
&self,
|
||||
db: DB,
|
||||
env: EnvWithHandlerCfg,
|
||||
inspector: I,
|
||||
) -> Evm<'_, I, DB>
|
||||
where
|
||||
DB: Database,
|
||||
I: GetInspector<DB>,
|
||||
{
|
||||
(*self).evm_with_env_and_inspector(db, env, inspector)
|
||||
}
|
||||
|
||||
fn evm_with_inspector<DB, I>(&self, db: DB, inspector: I) -> Evm<'_, I, DB>
|
||||
where
|
||||
DB: Database,
|
||||
I: GetInspector<DB>,
|
||||
{
|
||||
(*self).evm_with_inspector(db, inspector)
|
||||
}
|
||||
}
|
||||
|
||||
/// This represents the set of methods used to configure the EVM's environment before block
|
||||
/// execution.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user