Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
32 changes: 29 additions & 3 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ revm-inspectors = "0.15.0"
alloy-chains = { version = "0.1.32", default-features = false }
alloy-dyn-abi = "0.8.20"
alloy-eip2124 = { version = "0.1.0", default-features = false }
alloy-evm = { version = "0.1", default-features = false }
alloy-primitives = { version = "0.8.20", default-features = false, features = ["map-foldhash"] }
alloy-rlp = { version = "0.3.10", default-features = false, features = ["core-net"] }
alloy-sol-types = "0.8.20"
Expand Down Expand Up @@ -472,6 +473,7 @@ alloy-transport-ipc = { version = "0.11.1", default-features = false }
alloy-transport-ws = { version = "0.11.1", default-features = false }

# op
alloy-op-evm = { version = "0.1", default-features = false }
op-alloy-rpc-types = { version = "0.10.3", default-features = false }
op-alloy-rpc-types-engine = { version = "0.10.3", default-features = false }
op-alloy-network = { version = "0.10.3", default-features = false }
Expand Down Expand Up @@ -611,7 +613,9 @@ snmalloc-rs = { version = "0.3.7", features = ["build_cc"] }
# See: https://github.com/eira-fransham/crunchy/issues/13
crunchy = "=0.2.2"

# [patch.crates-io]
[patch.crates-io]
alloy-evm = { git = "https://github.com/alloy-rs/evm", rev = "4f5049a" }
alloy-op-evm = { git = "https://github.com/alloy-rs/evm", rev = "4f5049a" }
# alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "cfb13aa" }
# alloy-contract = { git = "https://github.com/alloy-rs/alloy", rev = "cfb13aa" }
# alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "cfb13aa" }
Expand Down
4 changes: 4 additions & 0 deletions book/sources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ reth-exex = { path = "../../crates/exex/exex" }
reth-node-ethereum = { path = "../../crates/ethereum/node" }
reth-tracing = { path = "../../crates/tracing" }
reth-node-api = { path = "../../crates/node/api" }

[patch.crates-io]
alloy-evm = { git = "https://github.com/alloy-rs/evm", rev = "4f5049a" }
alloy-op-evm = { git = "https://github.com/alloy-rs/evm", rev = "4f5049a" }
6 changes: 2 additions & 4 deletions crates/ethereum/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ reth-primitives-traits.workspace = true
# Alloy
alloy-primitives.workspace = true
alloy-eips.workspace = true
alloy-evm.workspace = true
alloy-sol-types.workspace = true
alloy-consensus.workspace = true

# Misc
derive_more.workspace = true

[dev-dependencies]
reth-testing-utils.workspace = true
reth-evm = { workspace = true, features = ["test-utils"] }
Expand Down Expand Up @@ -60,6 +58,6 @@ std = [
"serde_json/std",
"reth-primitives-traits/std",
"reth-chainspec/std",
"derive_more/std",
"reth-execution-types/std",
"alloy-evm/std",
]
136 changes: 13 additions & 123 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

extern crate alloc;

use alloc::{sync::Arc, vec::Vec};
use alloc::sync::Arc;
use alloy_consensus::{BlockHeader, Header};
pub use alloy_evm::EthEvm;
use alloy_evm::EthEvmFactory;
use alloy_primitives::{Address, U256};
use core::{convert::Infallible, fmt::Debug};
use reth_chainspec::{ChainSpec, EthChainSpec, MAINNET};
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, Database, Evm, NextBlockEnvAttributes};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, EvmEnv, NextBlockEnvAttributes};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::transaction::execute::FillTxEnv;
use revm::{inspector_handle_register, EvmBuilder};
use revm_primitives::{
AnalysisKind, BlobExcessGasAndPrice, BlockEnv, Bytes, CfgEnv, CfgEnvWithHandlerCfg, EVMError,
HaltReason, HandlerCfg, ResultAndState, SpecId, TxEnv, TxKind,
AnalysisKind, BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId, TxEnv,
};

mod config;
Expand All @@ -44,90 +44,17 @@ pub mod dao_fork;
/// [EIP-6110](https://eips.ethereum.org/EIPS/eip-6110) handling.
pub mod eip6110;

/// Ethereum EVM implementation.
#[derive(derive_more::Debug, derive_more::Deref, derive_more::DerefMut, derive_more::From)]
#[debug(bound(DB::Error: Debug))]
pub struct EthEvm<'a, EXT, DB: Database>(revm::Evm<'a, EXT, DB>);

impl<EXT, DB: Database> Evm for EthEvm<'_, EXT, DB> {
type DB = DB;
type Tx = TxEnv;
type Error = EVMError<DB::Error>;
type HaltReason = HaltReason;

fn block(&self) -> &BlockEnv {
self.0.block()
}

fn transact(&mut self, tx: Self::Tx) -> Result<ResultAndState, Self::Error> {
*self.tx_mut() = tx;
self.0.transact()
}

fn transact_system_call(
&mut self,
caller: Address,
contract: Address,
data: Bytes,
) -> Result<ResultAndState, Self::Error> {
#[allow(clippy::needless_update)] // side-effect of optimism fields
let tx_env = TxEnv {
caller,
transact_to: TxKind::Call(contract),
// Explicitly set nonce to None so revm does not do any nonce checks
nonce: None,
gas_limit: 30_000_000,
value: U256::ZERO,
data,
// Setting the gas price to zero enforces that no value is transferred as part of the
// call, and that the call will not count against the block's gas limit
gas_price: U256::ZERO,
// The chain ID check is not relevant here and is disabled if set to None
chain_id: None,
// Setting the gas priority fee to None ensures the effective gas price is derived from
// the `gas_price` field, which we need to be zero
gas_priority_fee: None,
access_list: Vec::new(),
// blob fields can be None for this tx
blob_hashes: Vec::new(),
max_fee_per_blob_gas: None,
// TODO remove this once this crate is no longer built with optimism
..Default::default()
};

*self.tx_mut() = tx_env;

let prev_block_env = self.block().clone();

// ensure the block gas limit is >= the tx
self.block_mut().gas_limit = U256::from(self.tx().gas_limit);

// disable the base fee check for this call by setting the base fee to zero
self.block_mut().basefee = U256::ZERO;

let res = self.0.transact();

// re-set the block env
*self.block_mut() = prev_block_env;

res
}

fn db_mut(&mut self) -> &mut Self::DB {
&mut self.context.evm.db
}
}

/// Ethereum-related EVM configuration.
#[derive(Debug, Clone)]
pub struct EthEvmConfig {
chain_spec: Arc<ChainSpec>,
evm_factory: EthEvmFactory,
}

impl EthEvmConfig {
/// Creates a new Ethereum EVM configuration with the given chain spec.
pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
Self { chain_spec }
pub fn new(chain_spec: Arc<ChainSpec>) -> Self {
Self { chain_spec, evm_factory: Default::default() }
}

/// Creates a new Ethereum EVM configuration for the ethereum mainnet.
Expand Down Expand Up @@ -241,47 +168,10 @@ impl ConfigureEvmEnv for EthEvmConfig {
}

impl ConfigureEvm for EthEvmConfig {
type Evm<'a, DB: Database + 'a, I: 'a> = EthEvm<'a, I, DB>;
type EvmError<DBError: core::error::Error + Send + Sync + 'static> = EVMError<DBError>;
type HaltReason = HaltReason;

fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()> {
let cfg_env_with_handler_cfg = CfgEnvWithHandlerCfg {
cfg_env: evm_env.cfg_env,
handler_cfg: HandlerCfg::new(evm_env.spec),
};
EthEvm(
EvmBuilder::default()
.with_db(db)
.with_cfg_env_with_handler_cfg(cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.build(),
)
}
type EvmFactory = EthEvmFactory;

fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
DB: Database,
I: revm::GetInspector<DB>,
{
let cfg_env_with_handler_cfg = CfgEnvWithHandlerCfg {
cfg_env: evm_env.cfg_env,
handler_cfg: HandlerCfg::new(evm_env.spec),
};
EthEvm(
EvmBuilder::default()
.with_db(db)
.with_external_context(inspector)
.with_cfg_env_with_handler_cfg(cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.append_handler_register(inspector_handle_register)
.build(),
)
fn evm_factory(&self) -> &Self::EvmFactory {
&self.evm_factory
}
}

Expand All @@ -292,8 +182,8 @@ mod tests {
use alloy_genesis::Genesis;
use alloy_primitives::U256;
use reth_chainspec::{Chain, ChainSpec, MAINNET};
use reth_evm::{env::EvmEnv, execute::ProviderError};
use revm::{
use reth_evm::{execute::ProviderError, EvmEnv};
use reth_revm::{
db::{CacheDB, EmptyDBTyped},
inspectors::NoOpInspector,
primitives::{BlockEnv, CfgEnv, SpecId},
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_chainspec::{ChainSpec, ChainSpecProvider, EthChainSpec, EthereumHardfor
use reth_errors::RethError;
use reth_ethereum_primitives::{Block, BlockBody, Receipt, TransactionSigned};
use reth_evm::{
env::EvmEnv, system_calls::SystemCaller, ConfigureEvm, Evm, EvmError, InvalidTxError,
system_calls::SystemCaller, ConfigureEvm, Evm, EvmEnv, EvmError, InvalidTxError,
NextBlockEnvAttributes,
};
use reth_evm_ethereum::{eip6110::parse_deposits_from_receipts, EthEvmConfig};
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ revm-primitives.workspace = true
# alloy
alloy-primitives.workspace = true
alloy-eips.workspace = true
alloy-evm.workspace = true
alloy-consensus.workspace = true

auto_impl.workspace = true
Expand Down Expand Up @@ -62,6 +63,7 @@ std = [
"reth-execution-errors/std",
"reth-storage-errors/std",
"reth-execution-types/std",
"alloy-evm/std",
]
test-utils = [
"dep:parking_lot",
Expand Down
6 changes: 6 additions & 0 deletions crates/evm/src/aliases.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Helper aliases when working with [`NodePrimitives`] and the traits in this crate.
use crate::{ConfigureEvm, ConfigureEvmEnv};
use alloy_evm::{EvmEnv, EvmFactory};
use reth_primitives_traits::NodePrimitives;

/// This is a type alias to make type bounds simpler when we have a [`NodePrimitives`] and need a
Expand Down Expand Up @@ -29,3 +30,8 @@ where
C: ConfigureEvm<Header = N::BlockHeader, Transaction = N::SignedTx>,
{
}

/// Helper to access [`EvmFactory::Error`] for a given [`ConfigureEvm`].
pub type EvmErrorFor<Evm, DB> = <<Evm as ConfigureEvm>::EvmFactory as EvmFactory<
EvmEnv<<Evm as ConfigureEvmEnv>::Spec>,
>>::Error<DB>;
Loading