Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: always create Evm through ConfigureEvm #13812

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 43 additions & 90 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use reth_chainspec::ChainSpec;
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::transaction::execute::FillTxEnv;
use reth_revm::{inspector_handle_register, EvmBuilder};
use revm_primitives::{
AnalysisKind, BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, Env, SpecId, TxEnv,
};
Expand Down Expand Up @@ -182,24 +183,57 @@ impl ConfigureEvmEnv for EthEvmConfig {
}
}

impl ConfigureEvm for EthEvmConfig {}
impl ConfigureEvm for EthEvmConfig {
fn evm_with_env<DB: reth_revm::Database>(
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
) -> reth_revm::Evm<'_, (), DB> {
EvmBuilder::default()
.with_db(db)
.with_cfg_env_with_handler_cfg(evm_env.cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.with_tx_env(tx)
.build()
}

fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
inspector: I,
) -> reth_revm::Evm<'_, I, DB>
where
DB: reth_revm::Database,
I: reth_revm::GetInspector<DB>,
{
EvmBuilder::default()
.with_db(db)
.with_external_context(inspector)
.with_cfg_env_with_handler_cfg(evm_env.cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.with_tx_env(tx)
.append_handler_register(inspector_handle_register)
.build()
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_consensus::{constants::KECCAK_EMPTY, Header};
use alloy_consensus::Header;
use alloy_genesis::Genesis;
use alloy_primitives::{B256, U256};
use alloy_primitives::U256;
use reth_chainspec::{Chain, ChainSpec, MAINNET};
use reth_evm::{env::EvmEnv, execute::ProviderError};
use reth_revm::{
db::{CacheDB, EmptyDBTyped},
inspectors::NoOpInspector,
primitives::{BlockEnv, CfgEnv, SpecId},
JournaledState,
};
use revm_primitives::HandlerCfg;
use std::collections::HashSet;

#[test]
fn test_fill_cfg_and_block_env() {
Expand All @@ -226,45 +260,6 @@ mod tests {
assert_eq!(cfg_env_with_handler_cfg.chain_id, chain_spec.chain().id());
}

#[test]
#[allow(clippy::needless_update)]
fn test_evm_configure() {
// Create a default `EthEvmConfig`
let evm_config = EthEvmConfig::new(MAINNET.clone());

// Initialize an empty database wrapped in CacheDB
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();

// Create an EVM instance using the configuration and the database
let evm = evm_config.evm(db);

// Check that the EVM environment is initialized with default values
assert_eq!(evm.context.evm.inner.env, Box::default());

// Latest spec ID and no warm preloaded addresses
assert_eq!(
evm.context.evm.inner.journaled_state,
JournaledState::new(SpecId::LATEST, HashSet::default())
);

// Ensure that the accounts database is empty
assert!(evm.context.evm.inner.db.accounts.is_empty());

// Ensure that the block hashes database is empty
assert!(evm.context.evm.inner.db.block_hashes.is_empty());

// Verify that there are two default contracts in the contracts database
assert_eq!(evm.context.evm.inner.db.contracts.len(), 2);
assert!(evm.context.evm.inner.db.contracts.contains_key(&KECCAK_EMPTY));
assert!(evm.context.evm.inner.db.contracts.contains_key(&B256::ZERO));

// Ensure that the logs database is empty
assert!(evm.context.evm.inner.db.logs.is_empty());

// No Optimism
assert_eq!(evm.handler.cfg, HandlerCfg { spec_id: SpecId::LATEST, ..Default::default() });
}

#[test]
#[allow(clippy::needless_update)]
fn test_evm_with_env_default_spec() {
Expand Down Expand Up @@ -374,57 +369,15 @@ mod tests {
let evm = evm_config.evm_with_env(db, evm_env, Default::default());

// Check that the spec ID is setup properly
assert_eq!(evm.handler.spec_id(), SpecId::CONSTANTINOPLE);
assert_eq!(evm.handler.spec_id(), SpecId::PETERSBURG);

// No Optimism
assert_eq!(
evm.handler.cfg,
HandlerCfg { spec_id: SpecId::CONSTANTINOPLE, ..Default::default() }
HandlerCfg { spec_id: SpecId::PETERSBURG, ..Default::default() }
);
}

#[test]
#[allow(clippy::needless_update)]
fn test_evm_with_inspector() {
let evm_config = EthEvmConfig::new(MAINNET.clone());

let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();

// No operation inspector
let noop = NoOpInspector;

let evm = evm_config.evm_with_inspector(db, noop);

// Check that the inspector is set correctly
assert_eq!(evm.context.external, noop);

// Check that the EVM environment is initialized with default values
assert_eq!(evm.context.evm.inner.env, Box::default());

// Latest spec ID and no warm preloaded addresses
assert_eq!(
evm.context.evm.inner.journaled_state,
JournaledState::new(SpecId::LATEST, HashSet::default())
);

// Ensure that the accounts database is empty
assert!(evm.context.evm.inner.db.accounts.is_empty());

// Ensure that the block hashes database is empty
assert!(evm.context.evm.inner.db.block_hashes.is_empty());

// Verify that there are two default contracts in the contracts database
assert_eq!(evm.context.evm.inner.db.contracts.len(), 2);
assert!(evm.context.evm.inner.db.contracts.contains_key(&KECCAK_EMPTY));
assert!(evm.context.evm.inner.db.contracts.contains_key(&B256::ZERO));

// Ensure that the logs database is empty
assert!(evm.context.evm.inner.db.logs.is_empty());

// No Optimism
assert_eq!(evm.handler.cfg, HandlerCfg { spec_id: SpecId::LATEST, ..Default::default() });
}

#[test]
#[allow(clippy::needless_update)]
fn test_evm_with_env_and_default_inspector() {
Expand Down Expand Up @@ -530,7 +483,7 @@ mod tests {
);

// Check that the spec ID is set properly
assert_eq!(evm.handler.spec_id(), SpecId::CONSTANTINOPLE);
assert_eq!(evm.handler.spec_id(), SpecId::PETERSBURG);
assert_eq!(evm.context.evm.env.block, evm_env.block_env);
assert_eq!(evm.context.evm.env.cfg, evm_env.cfg_env_with_handler_cfg.cfg_env);
assert_eq!(evm.context.evm.env.tx, Default::default());
Expand All @@ -539,7 +492,7 @@ mod tests {
// No Optimism
assert_eq!(
evm.handler.cfg,
HandlerCfg { spec_id: SpecId::CONSTANTINOPLE, ..Default::default() }
HandlerCfg { spec_id: SpecId::PETERSBURG, ..Default::default() }
);
}
}
10 changes: 2 additions & 8 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,7 @@ where

// apply eip-4788 pre block contract call
system_caller
.pre_block_beacon_root_contract_call(
&mut db,
evm_env.cfg_env_with_handler_cfg(),
evm_env.block_env(),
attributes.parent_beacon_block_root,
)
.pre_block_beacon_root_contract_call(&mut db, &evm_env, attributes.parent_beacon_block_root)
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_header.hash(),
Expand All @@ -220,8 +215,7 @@ where
// apply eip-2935 blockhashes update
system_caller.pre_block_blockhashes_contract_call(
&mut db,
evm_env.cfg_env_with_handler_cfg(),
evm_env.block_env(),
&evm_env,
parent_header.hash(),
)
.map_err(|err| {
Expand Down
141 changes: 0 additions & 141 deletions crates/evm/src/builder.rs

This file was deleted.

Loading
Loading