chore: Introduce helper type for evm cfg and env tuple (#13377)

This commit is contained in:
Ayodeji Akinola
2024-12-14 09:53:59 +01:00
committed by GitHub
parent 16f6d7a0c3
commit b525231224
33 changed files with 290 additions and 165 deletions

View File

@ -17,7 +17,7 @@ use alloy_rpc_types_eth::{
};
use futures::Future;
use reth_chainspec::EthChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv};
use reth_node_api::BlockBody;
use reth_primitives_traits::SignedTransaction;
use reth_provider::{BlockIdReader, ChainSpecProvider, ProviderHeader};
@ -86,8 +86,8 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
}
// Build cfg and block env, we'll reuse those.
let (mut cfg, mut block_env, block) =
self.evm_env_at(block.unwrap_or_default()).await?;
let (evm_env, block) = self.evm_env_at(block.unwrap_or_default()).await?;
let EvmEnv { mut cfg_env_with_handler_cfg, mut block_env } = evm_env;
// Gas cap for entire operation
let total_gas_limit = self.call_gas_limit();
@ -97,9 +97,9 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
let mut parent_hash = base_block.hash();
// Only enforce base fee if validation is enabled
cfg.disable_base_fee = !validation;
cfg_env_with_handler_cfg.disable_base_fee = !validation;
// Always disable EIP-3607
cfg.disable_eip3607 = true;
cfg_env_with_handler_cfg.disable_eip3607 = true;
let this = self.clone();
self.spawn_with_state_at_block(block, move |state| {
@ -155,7 +155,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
&mut calls,
validation,
block_env.gas_limit.to(),
cfg.chain_id,
cfg_env_with_handler_cfg.chain_id,
&mut db,
this.tx_resp_builder(),
)?;
@ -165,7 +165,11 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
let mut results = Vec::with_capacity(calls.len());
while let Some(tx) = calls.next() {
let env = this.build_call_evm_env(cfg.clone(), block_env.clone(), tx)?;
let env = this.build_call_evm_env(
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
tx,
)?;
let (res, env) = {
if trace_transfers {
@ -268,10 +272,11 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
.into();
}
let ((cfg, block_env, _), block) = futures::try_join!(
let ((evm_env, _), block) = futures::try_join!(
self.evm_env_at(target_block),
self.block_with_senders(target_block)
)?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
let block = block.ok_or(EthApiError::HeaderNotFound(target_block))?;
@ -302,7 +307,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
let transactions = block.transactions_with_sender().take(num_txs);
for (signer, tx) in transactions {
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg.clone(),
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
RpcNodeCore::evm_config(&this).tx_env(tx, *signer),
);
@ -320,7 +325,13 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
let overrides = EvmOverrides::new(state_overrides, block_overrides.clone());
let env = this
.prepare_call_env(cfg.clone(), block_env.clone(), tx, &mut db, overrides)
.prepare_call_env(
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
tx,
&mut db,
overrides,
)
.map(Into::into)?;
let (res, _) = this.transact(&mut db, env)?;
@ -361,10 +372,11 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
{
async move {
let block_id = block_number.unwrap_or_default();
let (cfg, block, at) = self.evm_env_at(block_id).await?;
let (evm_env, at) = self.evm_env_at(block_id).await?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
self.spawn_blocking_io(move |this| {
this.create_access_list_with(cfg, block, at, request)
this.create_access_list_with(cfg_env_with_handler_cfg, block_env, at, request)
})
.await
}
@ -571,14 +583,21 @@ pub trait Call:
R: Send + 'static,
{
async move {
let (cfg, block_env, at) = self.evm_env_at(at).await?;
let (evm_env, at) = self.evm_env_at(at).await?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
let this = self.clone();
self.spawn_blocking_io(move |_| {
let state = this.state_at_block_id(at)?;
let mut db =
CacheDB::new(StateProviderDatabase::new(StateProviderTraitObjWrapper(&state)));
let env = this.prepare_call_env(cfg, block_env, request, &mut db, overrides)?;
let env = this.prepare_call_env(
cfg_env_with_handler_cfg,
block_env,
request,
&mut db,
overrides,
)?;
f(StateCacheDbRefMutWrapper(&mut db), env)
})
@ -614,7 +633,8 @@ pub trait Call:
};
let (tx, tx_info) = transaction.split();
let (cfg, block_env, _) = self.evm_env_at(block.hash().into()).await?;
let (evm_env, _) = self.evm_env_at(block.hash().into()).await?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
// we need to get the state of the parent block because we're essentially replaying the
// block the transaction is included in
@ -628,14 +648,14 @@ pub trait Call:
// replay all transactions prior to the targeted transaction
this.replay_transactions_until(
&mut db,
cfg.clone(),
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
block_txs,
*tx.tx_hash(),
)?;
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
cfg_env_with_handler_cfg,
block_env,
RpcNodeCore::evm_config(&this).tx_env(tx.as_signed(), tx.signer()),
);

View File

@ -6,6 +6,7 @@ use alloy_primitives::U256;
use alloy_rpc_types_eth::{state::StateOverride, transaction::TransactionRequest, BlockId};
use futures::Future;
use reth_chainspec::MIN_TRANSACTION_GAS;
use reth_evm::env::EvmEnv;
use reth_provider::StateProvider;
use reth_revm::{
database::StateProviderDatabase,
@ -259,13 +260,14 @@ pub trait EstimateCall: Call {
Self: LoadPendingBlock,
{
async move {
let (cfg, block_env, at) = self.evm_env_at(at).await?;
let (evm_env, at) = self.evm_env_at(at).await?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
self.spawn_blocking_io(move |this| {
let state = this.state_at_block_id(at)?;
EstimateCall::estimate_gas_with(
&this,
cfg,
cfg_env_with_handler_cfg,
block_env,
request,
state,

View File

@ -12,8 +12,8 @@ use futures::Future;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_errors::RethError;
use reth_evm::{
state_change::post_block_withdrawals_balance_increments, system_calls::SystemCaller,
ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes,
env::EvmEnv, state_change::post_block_withdrawals_balance_increments,
system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes,
};
use reth_primitives::{BlockExt, InvalidTransactionError, SealedBlockWithSenders};
use reth_primitives_traits::Receipt;
@ -87,13 +87,15 @@ pub trait LoadPendingBlock:
// Note: for the PENDING block we assume it is past the known merge block and
// thus this will not fail when looking up the total
// difficulty value for the blockenv.
let (cfg, block_env) = self
let evm_env = self
.provider()
.env_with_header(block.header(), self.evm_config().clone())
.map_err(Self::Error::from_eth_err)?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
return Ok(PendingBlockEnv::new(
cfg,
cfg_env_with_handler_cfg,
block_env,
PendingBlockEnvOrigin::ActualPending(block, receipts),
));
@ -108,7 +110,7 @@ pub trait LoadPendingBlock:
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(BlockNumberOrTag::Latest.into()))?;
let (cfg, block_env) = self
let EvmEnv { cfg_env_with_handler_cfg, block_env } = self
.evm_config()
.next_cfg_and_block_env(
&latest,
@ -123,7 +125,7 @@ pub trait LoadPendingBlock:
.map_err(Self::Error::from_eth_err)?;
Ok(PendingBlockEnv::new(
cfg,
cfg_env_with_handler_cfg,
block_env,
PendingBlockEnvOrigin::DerivedFromLatest(latest.hash()),
))

View File

@ -10,14 +10,14 @@ use alloy_serde::JsonStorageKey;
use futures::Future;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_errors::RethError;
use reth_evm::ConfigureEvmEnv;
use reth_evm::{env::EvmEnv, ConfigureEvmEnv};
use reth_provider::{
BlockIdReader, BlockNumReader, ChainSpecProvider, EvmEnvProvider as _, ProviderHeader,
StateProvider, StateProviderBox, StateProviderFactory,
};
use reth_rpc_eth_types::{EthApiError, PendingBlockEnv, RpcInvalidTransactionError};
use reth_transaction_pool::TransactionPool;
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId};
use revm_primitives::SpecId;
/// Helper methods for `eth_` methods relating to state (accounts).
pub trait EthState: LoadState + SpawnBlocking {
@ -214,7 +214,7 @@ pub trait LoadState:
fn evm_env_at(
&self,
at: BlockId,
) -> impl Future<Output = Result<(CfgEnvWithHandlerCfg, BlockEnv, BlockId), Self::Error>> + Send
) -> impl Future<Output = Result<(EvmEnv, BlockId), Self::Error>> + Send
where
Self: LoadPendingBlock + SpawnBlocking,
{
@ -222,7 +222,7 @@ pub trait LoadState:
if at.is_pending() {
let PendingBlockEnv { cfg, block_env, origin } =
self.pending_block_env_and_cfg()?;
Ok((cfg, block_env, origin.state_block_id()))
Ok(((cfg, block_env).into(), origin.state_block_id()))
} else {
// Use cached values if there is no pending block
let block_hash = RpcNodeCore::provider(self)
@ -233,11 +233,11 @@ pub trait LoadState:
let header =
self.cache().get_header(block_hash).await.map_err(Self::Error::from_eth_err)?;
let evm_config = self.evm_config().clone();
let (cfg, block_env) = self
let evm_env = self
.provider()
.env_with_header(&header, evm_config)
.map_err(Self::Error::from_eth_err)?;
Ok((cfg, block_env, block_hash.into()))
Ok((evm_env, block_hash.into()))
}
}
}
@ -248,18 +248,19 @@ pub trait LoadState:
fn evm_env_for_raw_block(
&self,
header: &ProviderHeader<Self::Provider>,
) -> impl Future<Output = Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error>> + Send
) -> impl Future<Output = Result<EvmEnv, Self::Error>> + Send
where
Self: LoadPendingBlock + SpawnBlocking,
{
async move {
// get the parent config first
let (cfg, mut block_env, _) = self.evm_env_at(header.parent_hash().into()).await?;
let (evm_env, _) = self.evm_env_at(header.parent_hash().into()).await?;
let EvmEnv { cfg_env_with_handler_cfg, mut block_env } = evm_env;
let after_merge = cfg.handler_cfg.spec_id >= SpecId::MERGE;
let after_merge = cfg_env_with_handler_cfg.handler_cfg.spec_id >= SpecId::MERGE;
self.evm_config().fill_block_env(&mut block_env, header, after_merge);
Ok((cfg, block_env))
Ok((cfg_env_with_handler_cfg, block_env).into())
}
}

View File

@ -8,7 +8,7 @@ use alloy_primitives::B256;
use alloy_rpc_types_eth::{BlockId, TransactionInfo};
use futures::Future;
use reth_chainspec::ChainSpecProvider;
use reth_evm::{system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv};
use reth_evm::{env::EvmEnv, system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::SealedBlockWithSenders;
use reth_primitives_traits::{BlockBody, SignedTransaction};
use reth_provider::{BlockReader, ProviderBlock, ProviderHeader, ProviderTx};
@ -195,7 +195,8 @@ pub trait Trace:
};
let (tx, tx_info) = transaction.split();
let (cfg, block_env, _) = self.evm_env_at(block.hash().into()).await?;
let (evm_env, _) = self.evm_env_at(block.hash().into()).await?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
// we need to get the state of the parent block because we're essentially replaying the
// block the transaction is included in
@ -206,19 +207,24 @@ pub trait Trace:
let mut db = CacheDB::new(StateProviderDatabase::new(state));
let block_txs = block.transactions_with_sender();
this.apply_pre_execution_changes(&block, &mut db, &cfg, &block_env)?;
this.apply_pre_execution_changes(
&block,
&mut db,
&cfg_env_with_handler_cfg,
&block_env,
)?;
// replay all transactions prior to the targeted transaction
this.replay_transactions_until(
&mut db,
cfg.clone(),
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
block_txs,
*tx.tx_hash(),
)?;
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
cfg_env_with_handler_cfg,
block_env,
RpcNodeCore::evm_config(&this).tx_env(tx.as_signed(), tx.signer()),
);
@ -308,8 +314,9 @@ pub trait Trace:
self.block_with_senders(block_id).await
};
let ((cfg, block_env, _), block) =
futures::try_join!(self.evm_env_at(block_id), block)?;
let ((evm_env, _), block) = futures::try_join!(self.evm_env_at(block_id), block)?;
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
let Some(block) = block else { return Ok(None) };
@ -333,7 +340,12 @@ pub trait Trace:
let mut db =
CacheDB::new(StateProviderDatabase::new(StateProviderTraitObjWrapper(&state)));
this.apply_pre_execution_changes(&block, &mut db, &cfg, &block_env)?;
this.apply_pre_execution_changes(
&block,
&mut db,
&cfg_env_with_handler_cfg,
&block_env,
)?;
// prepare transactions, we do everything upfront to reduce time spent with open
// state
@ -362,8 +374,11 @@ pub trait Trace:
.peekable();
while let Some((tx_info, tx)) = transactions.next() {
let env =
EnvWithHandlerCfg::new_with_cfg_env(cfg.clone(), block_env.clone(), tx);
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg_env_with_handler_cfg.clone(),
block_env.clone(),
tx,
);
let mut inspector = inspector_setup();
let (res, _) =