feat: introduce external context GAT in ConfigureEvm (#7842)

This commit is contained in:
Dan Cline
2024-04-30 22:06:37 -04:00
committed by GitHub
parent bf9d9745ed
commit 074c5c3013
5 changed files with 36 additions and 14 deletions

View File

@ -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.
@ -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 {

View File

@ -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.
@ -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;
@ -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,

View File

@ -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()
}
@ -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]

View File

@ -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
@ -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()
}

View File

@ -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