feat: add Spec generic for EvmEnv (#13975)

This commit is contained in:
Arsenii Kulikov
2025-01-24 19:59:55 +04:00
committed by GitHub
parent 621b30f037
commit 203fed0f64
19 changed files with 223 additions and 293 deletions

View File

@ -93,9 +93,9 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
let mut parent_hash = base_block.hash();
// Only enforce base fee if validation is enabled
evm_env.cfg_env_with_handler_cfg.disable_base_fee = !validation;
evm_env.cfg_env.disable_base_fee = !validation;
// Always disable EIP-3607
evm_env.cfg_env_with_handler_cfg.disable_eip3607 = true;
evm_env.cfg_env.disable_eip3607 = true;
let this = self.clone();
self.spawn_with_state_at_block(block, move |state| {
@ -178,7 +178,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
call,
validation,
default_gas_limit,
evm_env.cfg_env_with_handler_cfg.chain_id,
evm_env.cfg_env.chain_id,
&mut db,
this.tx_resp_builder(),
)?;
@ -387,7 +387,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
/// [`BlockId`].
fn create_access_list_with(
&self,
mut evm_env: EvmEnv,
mut evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
at: BlockId,
mut request: TransactionRequest,
) -> Result<AccessListResult, Self::Error>
@ -400,12 +400,12 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
// we want to disable this in eth_createAccessList, since this is common practice used by
// other node impls and providers <https://github.com/foundry-rs/foundry/issues/4388>
evm_env.cfg_env_with_handler_cfg.disable_block_gas_limit = true;
evm_env.cfg_env.disable_block_gas_limit = true;
// The basefee should be ignored for eth_createAccessList
// See:
// <https://github.com/ethereum/go-ethereum/blob/8990c92aea01ca07801597b00c0d83d4e2d9b811/internal/ethapi/api.go#L1476-L1476>
evm_env.cfg_env_with_handler_cfg.disable_base_fee = true;
evm_env.cfg_env.disable_base_fee = true;
let mut db = CacheDB::new(StateProviderDatabase::new(state));
@ -427,7 +427,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
// can consume the list since we're not using the request anymore
let initial = request.access_list.take().unwrap_or_default();
let precompiles = get_precompiles(evm_env.cfg_env_with_handler_cfg.handler_cfg.spec_id);
let precompiles = get_precompiles(evm_env.spec.into());
let mut inspector = AccessListInspector::new(initial, from, to, precompiles);
let (result, (evm_env, mut tx_env)) =
@ -495,16 +495,21 @@ pub trait Call:
fn transact<DB>(
&self,
db: DB,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
) -> Result<(ResultAndState, (EvmEnv, <Self::Evm as ConfigureEvmEnv>::TxEnv)), Self::Error>
) -> Result<
(
ResultAndState,
(EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>, <Self::Evm as ConfigureEvmEnv>::TxEnv),
),
Self::Error,
>
where
DB: Database,
EthApiError: From<DB::Error>,
{
let mut evm = self.evm_config().evm_with_env(db, evm_env);
let mut evm = self.evm_config().evm_with_env(db, evm_env.clone());
let res = evm.transact(tx_env.clone()).map_err(Self::Error::from_evm_err)?;
let evm_env = evm.into_env();
Ok((res, (evm_env, tx_env)))
}
@ -515,17 +520,22 @@ pub trait Call:
fn transact_with_inspector<DB>(
&self,
db: DB,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
inspector: impl GetInspector<DB>,
) -> Result<(ResultAndState, (EvmEnv, <Self::Evm as ConfigureEvmEnv>::TxEnv)), Self::Error>
) -> Result<
(
ResultAndState,
(EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>, <Self::Evm as ConfigureEvmEnv>::TxEnv),
),
Self::Error,
>
where
DB: Database,
EthApiError: From<DB::Error>,
{
let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector);
let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env.clone(), inspector);
let res = evm.transact(tx_env.clone()).map_err(Self::Error::from_evm_err)?;
let evm_env = evm.into_env();
Ok((res, (evm_env, tx_env)))
}
@ -539,7 +549,13 @@ pub trait Call:
overrides: EvmOverrides,
) -> impl Future<
Output = Result<
(ResultAndState, (EvmEnv, <Self::Evm as ConfigureEvmEnv>::TxEnv)),
(
ResultAndState,
(
EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
<Self::Evm as ConfigureEvmEnv>::TxEnv,
),
),
Self::Error,
>,
> + Send
@ -594,7 +610,7 @@ pub trait Call:
Self: LoadPendingBlock,
F: FnOnce(
StateCacheDbRefMutWrapper<'_, '_>,
EvmEnv,
EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
<Self::Evm as ConfigureEvmEnv>::TxEnv,
) -> Result<R, Self::Error>
+ Send
@ -680,7 +696,7 @@ pub trait Call:
fn replay_transactions_until<'a, DB, I>(
&self,
db: &mut DB,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
transactions: I,
target_tx_hash: B256,
) -> Result<usize, Self::Error>
@ -728,13 +744,17 @@ pub trait Call:
/// - `nonce` is set to `None`
///
/// In addition, this changes the block's gas limit to the configured [`Self::call_gas_limit`].
#[expect(clippy::type_complexity)]
fn prepare_call_env<DB>(
&self,
mut evm_env: EvmEnv,
mut evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
mut request: TransactionRequest,
db: &mut CacheDB<DB>,
overrides: EvmOverrides,
) -> Result<(EvmEnv, <Self::Evm as ConfigureEvmEnv>::TxEnv), Self::Error>
) -> Result<
(EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>, <Self::Evm as ConfigureEvmEnv>::TxEnv),
Self::Error,
>
where
DB: DatabaseRef,
EthApiError: From<<DB as DatabaseRef>::Error>,
@ -751,12 +771,12 @@ pub trait Call:
// Disabled because eth_call is sometimes used with eoa senders
// See <https://github.com/paradigmxyz/reth/issues/1959>
evm_env.cfg_env_with_handler_cfg.disable_eip3607 = true;
evm_env.cfg_env.disable_eip3607 = true;
// The basefee should be ignored for eth_call
// See:
// <https://github.com/ethereum/go-ethereum/blob/ee8e83fa5f6cb261dad2ed0a7bbcde4930c41e6c/internal/ethapi/api.go#L985>
evm_env.cfg_env_with_handler_cfg.disable_base_fee = true;
evm_env.cfg_env.disable_base_fee = true;
// set nonce to None so that the correct nonce is chosen by the EVM
request.nonce = None;

View File

@ -36,7 +36,7 @@ pub trait EstimateCall: Call {
/// - `nonce` is set to `None`
fn estimate_gas_with<S>(
&self,
mut evm_env: EvmEnv,
mut evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
mut request: TransactionRequest,
state: S,
state_override: Option<StateOverride>,
@ -46,12 +46,12 @@ pub trait EstimateCall: Call {
{
// Disabled because eth_estimateGas is sometimes used with eoa senders
// See <https://github.com/paradigmxyz/reth/issues/1959>
evm_env.cfg_env_with_handler_cfg.disable_eip3607 = true;
evm_env.cfg_env.disable_eip3607 = true;
// The basefee should be ignored for eth_estimateGas and similar
// See:
// <https://github.com/ethereum/go-ethereum/blob/ee8e83fa5f6cb261dad2ed0a7bbcde4930c41e6c/internal/ethapi/api.go#L985>
evm_env.cfg_env_with_handler_cfg.disable_base_fee = true;
evm_env.cfg_env.disable_base_fee = true;
// set nonce to None so that the correct nonce is chosen by the EVM
request.nonce = None;
@ -281,7 +281,7 @@ pub trait EstimateCall: Call {
fn map_out_of_gas_err<DB>(
&self,
env_gas_limit: U256,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
mut tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
db: &mut DB,
) -> Self::Error

View File

@ -69,7 +69,11 @@ pub trait LoadPendingBlock:
fn pending_block_env_and_cfg(
&self,
) -> Result<
PendingBlockEnv<ProviderBlock<Self::Provider>, ProviderReceipt<Self::Provider>>,
PendingBlockEnv<
ProviderBlock<Self::Provider>,
ProviderReceipt<Self::Provider>,
<Self::Evm as ConfigureEvmEnv>::Spec,
>,
Self::Error,
> {
if let Some(block) =
@ -234,7 +238,7 @@ pub trait LoadPendingBlock:
#[expect(clippy::type_complexity)]
fn build_block(
&self,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
parent_hash: B256,
) -> Result<
(RecoveredBlock<ProviderBlock<Self::Provider>>, Vec<ProviderReceipt<Self::Provider>>),

View File

@ -210,10 +210,13 @@ pub trait LoadState:
/// for.
/// If the [`BlockId`] is pending, this will return the "Pending" tag, otherwise this returns
/// the hash of the exact block.
#[expect(clippy::type_complexity)]
fn evm_env_at(
&self,
at: BlockId,
) -> impl Future<Output = Result<(EvmEnv, BlockId), Self::Error>> + Send
) -> impl Future<
Output = Result<(EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>, BlockId), Self::Error>,
> + Send
where
Self: LoadPendingBlock + SpawnBlocking,
{

View File

@ -37,18 +37,23 @@ pub trait Trace:
fn inspect<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
inspector: I,
) -> Result<(ResultAndState, (EvmEnv, <Self::Evm as ConfigureEvmEnv>::TxEnv)), Self::Error>
) -> Result<
(
ResultAndState,
(EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>, <Self::Evm as ConfigureEvmEnv>::TxEnv),
),
Self::Error,
>
where
DB: Database,
EthApiError: From<DB::Error>,
I: GetInspector<DB>,
{
let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector);
let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env.clone(), inspector);
let res = evm.transact(tx_env.clone()).map_err(Self::Error::from_evm_err)?;
let evm_env = evm.into_env();
Ok((res, (evm_env, tx_env)))
}
@ -61,7 +66,7 @@ pub trait Trace:
/// Caution: this is blocking
fn trace_at<F, R>(
&self,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
config: TracingInspectorConfig,
at: BlockId,
@ -88,7 +93,7 @@ pub trait Trace:
/// the configured [`EvmEnv`] was inspected.
fn spawn_trace_at_with_state<F, R>(
&self,
evm_env: EvmEnv,
evm_env: EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Self::Evm as ConfigureEvmEnv>::TxEnv,
config: TracingInspectorConfig,
at: BlockId,
@ -444,7 +449,7 @@ pub trait Trace:
&self,
block: &RecoveredBlock<ProviderBlock<Self::Provider>>,
db: &mut DB,
evm_env: &EvmEnv,
evm_env: &EvmEnv<<Self::Evm as ConfigureEvmEnv>::Spec>,
) -> Result<(), Self::Error> {
let mut system_caller =
SystemCaller::new(self.evm_config().clone(), self.provider().chain_spec());

View File

@ -14,9 +14,9 @@ use reth_primitives_traits::Block;
/// Configured [`EvmEnv`] for a pending block.
#[derive(Debug, Clone, Constructor)]
pub struct PendingBlockEnv<B: Block = reth_primitives::Block, R = Receipt> {
pub struct PendingBlockEnv<B: Block, R, Spec> {
/// Configured [`EvmEnv`] for the pending block.
pub evm_env: EvmEnv,
pub evm_env: EvmEnv<Spec>,
/// Origin block for the config
pub origin: PendingBlockEnvOrigin<B, R>,
}

View File

@ -95,7 +95,7 @@ where
async fn trace_block(
&self,
block: Arc<RecoveredBlock<ProviderBlock<Eth::Provider>>>,
evm_env: EvmEnv,
evm_env: EvmEnv<<Eth::Evm as ConfigureEvmEnv>::Spec>,
opts: GethDebugTracingOptions,
) -> Result<Vec<TraceResult>, Eth::Error> {
// replay all transactions of the block
@ -446,7 +446,7 @@ where
&mut inspector,
)?;
let env = revm_primitives::Env::boxed(
evm_env.cfg_env_with_handler_cfg.cfg_env,
evm_env.cfg_env,
evm_env.block_env,
tx_env.into(),
);
@ -650,7 +650,7 @@ where
fn trace_transaction(
&self,
opts: &GethDebugTracingOptions,
evm_env: EvmEnv,
evm_env: EvmEnv<<Eth::Evm as ConfigureEvmEnv>::Spec>,
tx_env: <Eth::Evm as ConfigureEvmEnv>::TxEnv,
db: &mut StateCacheDb<'_>,
transaction_context: Option<TransactionContext>,
@ -781,7 +781,7 @@ where
let state = res.state.clone();
let env = revm_primitives::Env::boxed(
evm_env.cfg_env_with_handler_cfg.cfg_env,
evm_env.cfg_env,
evm_env.block_env,
tx_env.into(),
);

View File

@ -1,18 +1,16 @@
//! `Eth` bundle implementation and helpers.
use alloy_consensus::{BlockHeader, EnvKzgSettings, Transaction as _};
use alloy_consensus::{EnvKzgSettings, Transaction as _};
use alloy_eips::eip4844::MAX_DATA_GAS_PER_BLOCK;
use alloy_primitives::{Keccak256, U256};
use alloy_rpc_types_mev::{EthCallBundle, EthCallBundleResponse, EthCallBundleTransactionResult};
use jsonrpsee::core::RpcResult;
use reth_chainspec::EthChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, Evm};
use reth_primitives_traits::SignedTransaction;
use reth_provider::{ChainSpecProvider, HeaderProvider};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_eth_api::{
helpers::{Call, EthTransactions, LoadPendingBlock},
EthCallBundleApiServer, FromEthApiError, FromEvmError, RpcNodeCore,
EthCallBundleApiServer, FromEthApiError, FromEvmError,
};
use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError, RpcInvalidTransactionError};
use reth_tasks::pool::BlockingTaskGuard;
@ -23,7 +21,6 @@ use revm::{
db::{CacheDB, DatabaseCommit, DatabaseRef},
primitives::ResultAndState,
};
use revm_primitives::SpecId;
use std::sync::Arc;
/// `Eth` bundle implementation.
@ -132,21 +129,6 @@ where
if let Some(base_fee) = base_fee {
evm_env.block_env.basefee = U256::from(base_fee);
} else if evm_env.cfg_env_with_handler_cfg.handler_cfg.spec_id.is_enabled_in(SpecId::LONDON)
{
let parent_block = evm_env.block_env.number.saturating_to::<u64>();
// here we need to fetch the _next_ block's basefee based on the parent block <https://github.com/flashbots/mev-geth/blob/fddf97beec5877483f879a77b7dea2e58a58d653/internal/ethapi/api.go#L2130>
let parent = RpcNodeCore::provider(self.eth_api())
.header_by_number(parent_block)
.map_err(Eth::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(parent_block.into()))?;
if let Some(base_fee) = parent.next_block_base_fee(
RpcNodeCore::provider(self.eth_api())
.chain_spec()
.base_fee_params_at_block(parent_block),
) {
evm_env.block_env.basefee = U256::from(base_fee);
}
}
let state_block_number = evm_env.block_env.number;

View File

@ -1,6 +1,5 @@
//! `Eth` Sim bundle implementation and helpers.
use alloy_consensus::BlockHeader;
use alloy_eips::BlockNumberOrTag;
use alloy_primitives::U256;
use alloy_rpc_types_eth::BlockId;
@ -9,21 +8,20 @@ use alloy_rpc_types_mev::{
SimBundleOverrides, SimBundleResponse, Validity,
};
use jsonrpsee::core::RpcResult;
use reth_chainspec::EthChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, Evm};
use reth_provider::{ChainSpecProvider, HeaderProvider, ProviderTx};
use reth_provider::ProviderTx;
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::MevSimApiServer;
use reth_rpc_eth_api::{
helpers::{Call, EthTransactions, LoadPendingBlock},
FromEthApiError, RpcNodeCore,
FromEthApiError,
};
use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError};
use reth_tasks::pool::BlockingTaskGuard;
use reth_transaction_pool::{PoolConsensusTx, PoolPooledTx, PoolTransaction, TransactionPool};
use revm::{
db::CacheDB,
primitives::{Address, ResultAndState, SpecId},
primitives::{Address, ResultAndState},
DatabaseCommit, DatabaseRef,
};
use std::{sync::Arc, time::Duration};
@ -246,15 +244,6 @@ where
let block_id = parent_block.unwrap_or(BlockId::Number(BlockNumberOrTag::Pending));
let (mut evm_env, current_block) = self.eth_api().evm_env_at(block_id).await?;
let parent_header = RpcNodeCore::provider(&self.inner.eth_api)
.header_by_number(evm_env.block_env.number.saturating_to::<u64>())
.map_err(EthApiError::from_eth_err)? // Explicitly map the error
.ok_or_else(|| {
EthApiError::HeaderNotFound(
(evm_env.block_env.number.saturating_to::<u64>()).into(),
)
})?;
// apply overrides
if let Some(block_number) = block_number {
evm_env.block_env.number = U256::from(block_number);
@ -274,15 +263,6 @@ where
if let Some(base_fee) = base_fee {
evm_env.block_env.basefee = U256::from(base_fee);
} else if evm_env.cfg_env_with_handler_cfg.handler_cfg.spec_id.is_enabled_in(SpecId::LONDON)
{
if let Some(base_fee) = parent_header.next_block_base_fee(
RpcNodeCore::provider(&self.inner.eth_api)
.chain_spec()
.base_fee_params_at_block(evm_env.block_env.number.saturating_to::<u64>()),
) {
evm_env.block_env.basefee = U256::from(base_fee);
}
}
let eth_api = self.inner.eth_api.clone();