Skip to content

Commit

Permalink
feat: introduce external context GAT in ConfigureEvm (paradigmxyz#7842)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored May 1, 2024
1 parent bf9d974 commit 074c5c3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
12 changes: 11 additions & 1 deletion crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use reth_primitives::{
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
Address, ChainSpec, Head, Header, Transaction, U256,
};
use reth_revm::{Database, EvmBuilder};
pub mod execute;

/// Ethereum-related EVM configuration.
Expand Down Expand Up @@ -55,7 +56,16 @@ impl ConfigureEvmEnv for EthEvmConfig {
}
}

impl ConfigureEvm for EthEvmConfig {}
impl ConfigureEvm for EthEvmConfig {
type DefaultExternalContext<'a> = ();

fn evm<'a, DB: Database + 'a>(
&self,
db: DB,
) -> reth_revm::Evm<'a, Self::DefaultExternalContext<'a>, DB> {
EvmBuilder::default().with_db(db).build()
}
}

#[cfg(test)]
mod tests {
Expand Down
11 changes: 7 additions & 4 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ pub mod execute;

/// Trait for configuring the EVM for executing full blocks.
pub trait ConfigureEvm: ConfigureEvmEnv {
/// Associated type for the default external context that should be configured for the EVM.
type DefaultExternalContext<'a>;

/// 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<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, (), DB> {
EvmBuilder::default().with_db(db).build()
}
fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, Self::DefaultExternalContext<'a>, DB>;

/// Returns a new EVM with the given database configured with the given environment settings,
/// including the spec id.
Expand All @@ -33,7 +34,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
&self,
db: DB,
env: EnvWithHandlerCfg,
) -> Evm<'a, (), DB> {
) -> Evm<'a, Self::DefaultExternalContext<'a>, DB> {
let mut evm = self.evm(db);
evm.modify_spec_id(env.spec_id());
evm.context.evm.env = env.env;
Expand All @@ -43,6 +44,8 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
/// 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<'a, DB, I>(
&self,
Expand Down
6 changes: 4 additions & 2 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
}

impl ConfigureEvm for OptimismEvmConfig {
fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, (), DB> {
type DefaultExternalContext<'a> = ();

fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, Self::DefaultExternalContext<'a>, DB> {
EvmBuilder::default().with_db(db).optimism().build()
}

Expand All @@ -83,7 +85,7 @@ impl ConfigureEvm for OptimismEvmConfig {
mod tests {
use super::*;
use reth_primitives::revm_primitives::{BlockEnv, CfgEnv};
use reth_revm::primitives::SpecId;
use revm_primitives::SpecId;

#[test]
#[ignore]
Expand Down
17 changes: 11 additions & 6 deletions crates/revm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use std::collections::HashMap;
#[cfg(feature = "optimism")]
use {
reth_primitives::revm::env::fill_op_tx_env,
revm::{
inspector_handle_register,
primitives::{HandlerCfg, SpecId},
Database, Evm, EvmBuilder, GetInspector,
},
revm::{inspector_handle_register, GetInspector},
};

use revm::{
primitives::{HandlerCfg, SpecId},
Database, Evm, EvmBuilder,
};

/// Mock state for testing
Expand Down Expand Up @@ -158,9 +159,13 @@ impl ConfigureEvmEnv for TestEvmConfig {
}

impl ConfigureEvm for TestEvmConfig {
#[cfg(feature = "optimism")]
type DefaultExternalContext<'a> = ();

fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, (), DB> {
#[cfg(feature = "optimism")]
let handler_cfg = HandlerCfg { spec_id: SpecId::LATEST, is_optimism: true };
#[cfg(not(feature = "optimism"))]
let handler_cfg = HandlerCfg { spec_id: SpecId::LATEST };
EvmBuilder::default().with_db(db).with_handler_cfg(handler_cfg).build()
}

Expand Down
4 changes: 3 additions & 1 deletion examples/custom-evm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ impl ConfigureEvmEnv for MyEvmConfig {
}

impl ConfigureEvm for MyEvmConfig {
fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, (), DB> {
type DefaultExternalContext<'a> = ();

fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, Self::DefaultExternalContext<'a>, DB> {
EvmBuilder::default()
.with_db(db)
// add additional precompiles
Expand Down

0 comments on commit 074c5c3

Please sign in to comment.