chore: trim ConfigureEvm trait (#13807)

This commit is contained in:
Arsenii Kulikov
2025-01-15 22:31:16 +04:00
committed by GitHub
parent d5978a78b4
commit fd52b634ed
6 changed files with 105 additions and 386 deletions

View File

@ -1,141 +0,0 @@
//! Builder for creating an EVM with a database and environment.
use alloc::boxed::Box;
use revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
use revm_primitives::EnvWithHandlerCfg;
/// Builder for creating an EVM with a database and environment.
///
/// Wrapper around [`EvmBuilder`] that allows for setting the database and environment for the EVM.
///
/// This is useful for creating an EVM with a custom database and environment without having to
/// necessarily rely on Revm inspector.
#[derive(Debug)]
pub struct RethEvmBuilder<DB, EXT = ()> {
/// The database to use for the EVM.
db: DB,
/// The environment to use for the EVM.
env: Option<Box<EnvWithHandlerCfg>>,
/// The external context for the EVM.
external_context: EXT,
}
impl<DB> RethEvmBuilder<DB> {
/// Create a new EVM builder with the given database.
pub const fn new(db: DB) -> Self {
Self { db, env: None, external_context: () }
}
}
impl<DB, EXT> RethEvmBuilder<DB, EXT>
where
DB: Database,
{
/// Set the environment for the EVM.
pub fn with_env(mut self, env: Box<EnvWithHandlerCfg>) -> Self {
self.env = Some(env);
self
}
/// Set the external context for the EVM.
pub fn with_external_context<EXT1>(self, external_context: EXT1) -> RethEvmBuilder<DB, EXT1> {
RethEvmBuilder { db: self.db, env: self.env, external_context }
}
/// Build the EVM with the given database and environment.
pub fn build<'a>(self) -> Evm<'a, EXT, DB> {
let mut builder =
EvmBuilder::default().with_db(self.db).with_external_context(self.external_context);
if let Some(env) = self.env {
builder = builder.with_spec_id(env.spec_id());
builder = builder.with_env(env.env);
}
builder.build()
}
/// Build the EVM with the given database and environment, using the given inspector.
pub fn build_with_inspector<'a, I>(self, inspector: I) -> Evm<'a, I, DB>
where
I: GetInspector<DB>,
EXT: 'a,
{
let mut builder =
EvmBuilder::default().with_db(self.db).with_external_context(self.external_context);
if let Some(env) = self.env {
builder = builder.with_spec_id(env.spec_id());
builder = builder.with_env(env.env);
}
builder
.with_external_context(inspector)
.append_handler_register(inspector_handle_register)
.build()
}
}
/// Trait for configuring an EVM builder.
pub trait ConfigureEvmBuilder {
/// The type of EVM builder that this trait can configure.
type Builder<'a, DB: Database>: EvmFactory;
}
/// Trait for configuring the EVM for executing full blocks.
pub trait EvmFactory {
/// Returns new EVM with the given database
///
/// This does not automatically configure the EVM with [`crate::ConfigureEvmEnv`] methods. It is
/// up to the caller to call an appropriate method to fill the transaction and block
/// environment before executing any transactions using the provided EVM.
fn evm<DB: Database>(self, db: DB) -> Evm<'static, (), DB>
where
Self: Sized,
{
RethEvmBuilder::new(db).build()
}
/// Returns a new EVM with the given database configured with the given environment settings,
/// including the spec id.
///
/// This will preserve any handler modifications
fn evm_with_env<'a, DB: Database + 'a>(
&self,
db: DB,
env: EnvWithHandlerCfg,
) -> Evm<'a, (), DB> {
RethEvmBuilder::new(db).with_env(env.into()).build()
}
/// Returns a new EVM with the given database configured with the given environment settings,
/// including the spec id.
///
/// This will use the given external inspector as the EVM external context.
///
/// This will preserve any handler modifications
fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
env: EnvWithHandlerCfg,
inspector: I,
) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
RethEvmBuilder::new(db).with_env(env.into()).build_with_inspector(inspector)
}
/// Returns a new EVM with the given inspector.
///
/// Caution: This does not automatically configure the EVM with [`crate::ConfigureEvmEnv`]
/// methods. It is up to the caller to call an appropriate method to fill the transaction
/// and block environment before executing any transactions using the provided EVM.
fn evm_with_inspector<DB, I>(&self, db: DB, inspector: I) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
RethEvmBuilder::new(db).build_with_inspector(inspector)
}
}
impl<DB: Database, EXT: Clone> EvmFactory for RethEvmBuilder<DB, EXT> {}

View File

@ -17,14 +17,12 @@
extern crate alloc;
use crate::builder::RethEvmBuilder;
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, SpecId, TxEnv};
pub mod builder;
pub mod either;
/// EVM environment configuration.
pub mod env;
@ -42,26 +40,11 @@ pub mod test_utils;
/// Trait for configuring the EVM for executing full blocks.
pub trait ConfigureEvm: ConfigureEvmEnv {
/// Returns new EVM with the given database
///
/// This does not automatically configure the EVM with [`ConfigureEvmEnv`] methods. It is up to
/// the caller to call an appropriate method to fill the transaction and block environment
/// before executing any transactions using the provided EVM.
fn evm<DB: Database>(&self, db: DB) -> Evm<'_, (), DB> {
RethEvmBuilder::new(db).build()
}
/// Returns a new EVM with the given database configured with the given environment settings,
/// 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, tx: TxEnv) -> Evm<'_, (), DB> {
let mut evm = self.evm(db);
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
}
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv, tx: TxEnv) -> Evm<'_, (), DB>;
/// Returns a new EVM with the given database configured with `cfg` and `block_env`
/// configuration derived from the given header. Relies on
@ -90,27 +73,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
let mut evm = self.evm_with_inspector(db, inspector);
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
}
/// Returns a new EVM with the given inspector.
///
/// Caution: This does not automatically configure the EVM with [`ConfigureEvmEnv`] methods. It
/// is up to the caller to call an appropriate method to fill the transaction and block
/// environment before executing any transactions using the provided EVM.
fn evm_with_inspector<DB, I>(&self, db: DB, inspector: I) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
RethEvmBuilder::new(db).build_with_inspector(inspector)
}
I: GetInspector<DB>;
}
impl<'b, T> ConfigureEvm for &'b T
@ -118,10 +81,6 @@ where
T: ConfigureEvm,
&'b T: ConfigureEvmEnv<Header = T::Header>,
{
fn evm<DB: Database>(&self, db: DB) -> Evm<'_, (), DB> {
(*self).evm(db)
}
fn evm_for_block<DB: Database>(&self, db: DB, header: &Self::Header) -> Evm<'_, (), DB> {
(*self).evm_for_block(db, header)
}
@ -143,14 +102,6 @@ where
{
(*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>
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