From c0bbf1a9d7e556c59ede08b9ccce57334eaadbc7 Mon Sep 17 00:00:00 2001 From: geoknee Date: Tue, 20 Oct 2026 15:45:59 +0100 Subject: [PATCH] fix(rpc): replay block transactions through the block executor The RPC helpers that replay a block's own transactions (debug_trace*, trace_*, eth_callMany/debug_traceCall at an index/debug_traceCallMany, debug_intermediateRoots, ots) ran them on a bare EVM. Per-transaction logic that lives in the block executor was skipped, so on OP Stack SDM blocks traces reported pre-refund gas and unsettled balances, and the intermediate roots never reached the header's state root. Prefix replays now go through `executor_for_block`, and traced transactions run on an executor built over the inspected EVM via `execute_transaction_without_commit`. alloy-evm's `TxTracer` is replaced by an executor loop with a local `TracingCtx` of the same public shape. `trace_chain_block` is ported the same way and still reports a failing transaction as a per-transaction error. `replay_block_until` keeps the cached-BAL shortcut, since computed BALs are built by the executor and hold settled state. `inspect_transaction_in_block` and `spawn_replay_transaction` no longer use it: attaching the BAL ran the target on a bare EVM, and `--rpc-cache.prewarm-bals` caches a BAL for every canonical block on any chain. Ref: ethereum-optimism/protocol-team#485 Co-Authored-By: Claude Opus 5.5 --- crates/rpc/rpc-eth-api/src/helpers/call.rs | 50 ++-- crates/rpc/rpc-eth-api/src/helpers/trace.rs | 273 +++++++++++++------- crates/rpc/rpc/src/debug.rs | 78 +++--- 3 files changed, 242 insertions(+), 159 deletions(-) diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 48249a286fa..122b0d3438d 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -21,8 +21,10 @@ use futures::Future; use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks}; use reth_errors::{ProviderError, RethError}; use reth_evm::{ - block::BlockExecutor, env::BlockEnvironment, execute::BlockBuilder, ConfigureEvm, Evm, - EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, TransactionEnvMut, TxEnvFor, + block::{BlockExecutor, TxResult}, + env::BlockEnvironment, + execute::BlockBuilder, + ConfigureEvm, Evm, EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, TransactionEnvMut, TxEnvFor, }; use reth_node_api::BlockBody; use reth_primitives_traits::Recovered; @@ -36,7 +38,6 @@ use reth_revm::{ }; use reth_rpc_convert::{RpcConvert, RpcTxReq}; use reth_rpc_eth_types::{ - cache::db::attach_bal_before_tx, error::{AsEthApiError, FromEthApiError}, simulate::{self, EthSimulateError}, EthApiError, StateCacheDb, @@ -702,13 +703,12 @@ pub trait Call: /// Retrieves the transaction if it exists and executes it. /// - /// Before the transaction is executed, the state is positioned right before the transaction, - /// either by attaching the block's cached BAL or by executing all previous transactions in the - /// block. + /// Before the transaction is executed, all previous transactions in the block are executed + /// through the block executor, which then executes the transaction itself, so the result + /// includes any chain-specific per-transaction settlement. A cached BAL is not used, because + /// the target would then run outside an executor that has replayed the block up to it. /// The callback `f` is invoked with the [`ResultAndState`] after the transaction was executed - /// and the database that points to the beginning of the transaction. The database may have - /// the block's BAL attached and must only be used for reads, because an attached BAL takes - /// read precedence over state committed on top, see [`attach_bal_before_tx`]. + /// and the database that points to the beginning of the transaction. /// /// Note: Implementers should use a threadpool where blocking is allowed, such as /// [`BlockingTaskPool`](reth_tasks::pool::BlockingTaskPool). @@ -729,11 +729,10 @@ pub trait Call: R: Send + 'static, { async move { - let (transaction, block, bal) = - match self.transaction_and_block_and_maybe_bal(hash).await? { - None => return Ok(None), - Some(res) => res, - }; + let (transaction, block) = match self.transaction_and_block(hash).await? { + None => return Ok(None), + Some(res) => res, + }; let (tx, tx_info) = transaction.split(); // we need to get the state of the parent block because we're essentially replaying the @@ -741,15 +740,6 @@ pub trait Call: let parent_block = block.parent_hash(); self.spawn_with_state_at_block(parent_block, move |this, mut db| { - if let Some((bal, tx_index)) = bal.zip(tx_info.index) { - attach_bal_before_tx(&mut db, &bal, tx_index as usize); - - let evm_env = this.evm_env_for_header(block.sealed_block().sealed_header())?; - let tx_env = RpcNodeCore::evm_config(&this).tx_env(tx); - let res = this.transact(&mut db, evm_env, tx_env)?; - return f(tx_info, res, db) - } - let block_txs = block.transactions_recovered(); let mut executor = RpcNodeCore::evm_config(&this) @@ -766,9 +756,10 @@ pub trait Call: executor.execute_transaction(block_tx).map_err(Self::Error::from_eth_err)?; } - let tx_env = RpcNodeCore::evm_config(&this).tx_env(tx); - - let res = executor.evm_mut().transact(tx_env).map_err(Self::Error::from_evm_err)?; + let res = executor + .execute_transaction_without_commit(tx) + .map_err(Self::Error::from_eth_err)? + .into_result(); drop(executor); f(tx_info, res, db) }) @@ -781,8 +772,11 @@ pub trait Call: /// /// This executes on a caller provided EVM, so the target transaction can then be run on the /// same EVM, keeping any block-scoped EVM state intact. The EVM's inspector configuration is - /// left untouched; see - /// [`Trace::inspect_transaction_in_block`] to replay without inspection and trace the target. + /// left untouched. + /// + /// This bypasses the block executor, so it misses any chain-specific per-transaction + /// settlement the executor applies. To replay a block's own transactions, use + /// [`Trace::replay_block_until`] or [`Trace::inspect_transaction_in_block`] instead. /// /// If the target index is greater than or equal to the iterator length, all transactions are /// replayed. diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index d51921713e5..8d5a5ef6d24 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs @@ -9,13 +9,20 @@ use alloy_rpc_types_eth::{BlockId, TransactionInfo}; use futures::Future; use reth_errors::RethError; use reth_evm::{ - block::BlockExecutor, evm::EvmFactoryExt, tracing::TracingCtx, ConfigureEvm, Evm, EvmEnvFor, - EvmFor, HaltReasonFor, InspectorFor, IntoTxEnv, TxEnvFor, + block::{BlockExecutor, BlockExecutorFor, TxResult}, + ConfigureEvm, Evm, EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, IntoTxEnv, TxEnvFor, }; use reth_primitives_traits::{BlockBody, BlockTy, Recovered, RecoveredBlock}; -use reth_rpc_eth_types::cache::db::{attach_bal_before_tx, StateCacheDb}; +use reth_rpc_eth_types::{ + cache::db::{attach_bal_before_tx, StateCacheDb}, + EthApiError, +}; use reth_storage_api::{ProviderBlock, ProviderTx}; -use revm::{context::Block, context_interface::result::ResultAndState, state::bal::Bal as RevmBal}; +use revm::{ + context::Block, + context_interface::result::{ExecutionResult, ResultAndState}, + state::{bal::Bal as RevmBal, EvmState}, +}; use revm_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; use std::sync::Arc; @@ -38,13 +45,10 @@ pub trait Trace: LoadState> + Call { /// Retrieves the transaction if it exists and returns its trace. /// - /// Before the transaction is traced, the state is positioned right before the transaction, - /// either by attaching the block's cached BAL or by executing all previous transactions in - /// the block. + /// Before the transaction is traced, all previous transactions in the block are executed + /// through the block executor, see [`Trace::inspect_transaction_in_block`]. /// The callback `f` is invoked with the [`ResultAndState`] after the transaction was executed - /// and the database that points to the beginning of the transaction. The database may have - /// the block's BAL attached and must only be used for reads, because an attached BAL takes - /// read precedence over state committed on top, see [`attach_bal_before_tx`]. + /// and the database that points to the beginning of the transaction. /// /// Note: Implementers should use a threadpool where blocking is allowed, such as /// [`BlockingTaskPool`](reth_tasks::pool::BlockingTaskPool). @@ -71,13 +75,10 @@ pub trait Trace: LoadState> + Call { /// Retrieves the transaction if it exists and returns its trace. /// - /// Before the transaction is traced, the state is positioned right before the transaction, - /// either by attaching the block's cached BAL or by executing all previous transactions in - /// the block. + /// Before the transaction is traced, all previous transactions in the block are executed + /// through the block executor, see [`Trace::inspect_transaction_in_block`]. /// The callback `f` is invoked with the [`ResultAndState`] after the transaction was executed - /// and the database that points to the beginning of the transaction. The database may have - /// the block's BAL attached and must only be used for reads, because an attached BAL takes - /// read precedence over state committed on top, see [`attach_bal_before_tx`]. + /// and the database that points to the beginning of the transaction. /// /// Note: Implementers should use a threadpool where blocking is allowed, such as /// [`BlockingTaskPool`](reth_tasks::pool::BlockingTaskPool). @@ -101,12 +102,11 @@ pub trait Trace: LoadState> + Call { R: Send + 'static, { async move { - let (transaction, block, bal) = - match self.transaction_and_block_and_maybe_bal(hash).await? { - None => return Ok(None), - Some(res) => res, - }; - let (tx, tx_info) = transaction.split(); + let (transaction, block) = match self.transaction_and_block(hash).await? { + None => return Ok(None), + Some(res) => res, + }; + let (_, tx_info) = transaction.split(); // we need to get the state of the parent block because we're essentially replaying the // block the transaction is included in @@ -121,8 +121,6 @@ pub trait Trace: LoadState> + Call { // returns transactions included in a block tx_info.index.expect("transaction_and_block only returns block transactions") as usize, - tx, - bal.as_deref(), )?; f(tx_info, inspector, res, db) }) @@ -134,9 +132,10 @@ pub trait Trace: LoadState> + Call { /// Positions the state of `db` right before the transaction at the target index. /// /// If the block's cached BAL is given, it is attached to the database at the target index and - /// no transactions are executed, see [`attach_bal_before_tx`]. Otherwise all transactions - /// before the target transaction are executed and their changes are written to the - /// _runtime_ db ([`StateCacheDb`]). + /// no transactions are executed, see [`attach_bal_before_tx`]. Otherwise the block's + /// pre-execution changes and all transactions before the target transaction are executed + /// through the block executor, and their changes are written to the _runtime_ db + /// ([`StateCacheDb`]). /// /// If the target index is greater than or equal to the block's transaction count, all /// transactions are replayed. @@ -152,23 +151,28 @@ pub trait Trace: LoadState> + Call { return Ok(()) } - self.apply_pre_execution_changes(block, db)?; + let mut executor = self + .evm_config() + .executor_for_block(db, block.sealed_block()) + .map_err(RethError::other) + .map_err(Self::Error::from_eth_err)?; + executor.apply_pre_execution_changes().map_err(Self::Error::from_eth_err)?; - let evm_env = self.evm_env_for_header(block.sealed_block().sealed_header())?; - let mut evm = self.evm_config().evm_with_env(db, evm_env); - self.replay_transactions_until_with_evm( - &mut evm, - block.transactions_recovered(), - target_tx_index, - ) + for tx in block.transactions_recovered().take(target_tx_index) { + executor.execute_transaction(tx).map_err(Self::Error::from_eth_err)?; + } + Ok(()) } - /// Executes the target transaction with the configured inspector on the state right before - /// the transaction. + /// Replays all transactions before the target transaction without inspection, then executes + /// the block's transaction at the target index with the configured inspector, all through the + /// same block executor. + /// + /// The returned [`ResultAndState`] is the executor's, so it includes any chain-specific + /// per-transaction settlement. It is not committed to `db`. /// - /// If the block's cached BAL is given, the state is positioned by attaching the BAL at the - /// target index, see [`attach_bal_before_tx`]. Otherwise all transactions before the target - /// transaction are replayed without inspection first. + /// This never positions the state with a cached BAL: the target would then run on an executor + /// that has not replayed the block up to it, and miss settlement keyed by its position. #[expect(clippy::type_complexity)] fn inspect_transaction_in_block<'a>( &self, @@ -176,36 +180,68 @@ pub trait Trace: LoadState> + Call { db: &'a mut StateCacheDb, inspector: impl InspectorFor, target_tx_index: usize, - target_tx_env: impl IntoTxEnv>, - bal: Option<&DecodedBal>>, ) -> Result<(ResultAndState>, EvmEnvFor), Self::Error> { - if let Some(bal) = bal { - // the BAL also covers the block's pre-execution changes - attach_bal_before_tx(db, bal, target_tx_index); - } else { - self.apply_pre_execution_changes(block, db)?; - } - let evm_env = self.evm_env_for_header(block.sealed_block().sealed_header())?; - let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector); - - if bal.is_none() { - evm.disable_inspector(); - self.replay_transactions_until_with_evm( - &mut evm, - block.transactions_recovered(), - target_tx_index, - )?; - evm.enable_inspector(); - } + let mut executor = self.inspected_block_executor(block, db, evm_env.clone(), inspector)?; - let res = evm.transact(target_tx_env).map_err(Self::Error::from_evm_err)?; + let mut block_txs = block.transactions_recovered(); + for tx in block_txs.by_ref().take(target_tx_index) { + executor.execute_transaction(tx).map_err(Self::Error::from_eth_err)?; + } + let target_tx = block_txs.next().ok_or_else(|| { + Self::Error::from_eth_err(EthApiError::InvalidParams(format!( + "tx_index {target_tx_index} out of bounds for block with {} transactions", + block.transaction_count() + ))) + })?; - let (_, evm_env) = evm.finish(); + executor.evm_mut().enable_inspector(); + let res = executor + .execute_transaction_without_commit(target_tx) + .map_err(Self::Error::from_eth_err)? + .into_result(); Ok((res, evm_env)) } + /// Creates a block executor for `block` whose EVM runs the given inspector, and applies the + /// block's pre-execution changes. The inspector is left disabled. + /// + /// Block transactions must be replayed through the executor rather than its EVM: the executor + /// tracks the transaction's position in the block and applies chain-specific per-transaction + /// settlement, such as OP Stack SDM refunds, that a bare EVM replay misses. + #[expect(clippy::type_complexity)] + fn inspected_block_executor<'a, 'db, I>( + &'a self, + block: &'a RecoveredBlock>, + db: &'db mut StateCacheDb, + evm_env: EvmEnvFor, + inspector: I, + ) -> Result< + BlockExecutorFor< + 'a, + ::BlockExecutorFactory, + &'db mut StateCacheDb, + I, + >, + Self::Error, + > + where + I: InspectorFor, + { + let evm_config = self.evm_config(); + let ctx = evm_config + .context_for_block(block.sealed_block()) + .map_err(RethError::other) + .map_err(Self::Error::from_eth_err)?; + let mut evm = evm_config.evm_with_env_and_inspector(db, evm_env, inspector); + evm.disable_inspector(); + + let mut executor = evm_config.create_executor_with_state(evm, ctx); + executor.apply_pre_execution_changes().map_err(Self::Error::from_eth_err)?; + Ok(executor) + } + /// Executes all transactions of a block up to a given index. /// /// If a `highest_index` is given, this will only execute the first `highest_index` @@ -299,38 +335,64 @@ pub trait Trace: LoadState> + Call { let block_timestamp = evm_env.block_env.timestamp().saturating_to(); let base_fee = evm_env.block_env.basefee(); - this.apply_pre_execution_changes(&block, &mut db)?; - // prepare transactions, we do everything upfront to reduce time spent with open // state - let max_transactions = highest_index.map_or_else( - || block.body().transaction_count(), - |highest| { - // we need + 1 because the index is 0-based - highest as usize + 1 - }, - ); - - let mut idx = 0; - - let results = this - .evm_config() - .evm_factory() - .create_tracer(&mut db, evm_env, inspector_setup()) - .try_trace_many(block.transactions_recovered().take(max_transactions), |ctx| { - let tx_info = TransactionInfo { - hash: Some(*ctx.tx.tx_hash()), - index: Some(idx), - block_hash: Some(block_hash), - block_number: Some(block_number), - block_timestamp: Some(block_timestamp), - base_fee: Some(base_fee), - }; - idx += 1; - - f(tx_info, ctx) - }) - .collect::>()?; + let max_transactions = highest_index + .map_or_else( + || block.body().transaction_count(), + |highest| { + // we need + 1 because the index is 0-based + (highest as usize).saturating_add(1) + }, + ) + .min(block.body().transaction_count()); + + let inspector = inspector_setup(); + let fused_inspector = inspector.clone(); + let mut executor = + this.inspected_block_executor(&block, &mut db, evm_env, inspector)?; + executor.evm_mut().enable_inspector(); + + let mut results = Vec::with_capacity(max_transactions); + let mut transactions = + block.transactions_recovered().take(max_transactions).enumerate().peekable(); + while let Some((idx, tx)) = transactions.next() { + let output = executor + .execute_transaction_without_commit(tx) + .map_err(Self::Error::from_eth_err)?; + let ResultAndState { result, state } = output.result(); + + let tx_info = TransactionInfo { + hash: Some(*tx.tx_hash()), + index: Some(idx as u64), + block_hash: Some(block_hash), + block_number: Some(block_number), + block_timestamp: Some(block_timestamp), + base_fee: Some(base_fee), + }; + let mut was_fused = false; + let (db, inspector, _) = executor.evm_mut().components_mut(); + results.push(f( + tx_info, + TracingCtx { + tx, + result: result.clone(), + state, + inspector, + db, + fused_inspector: &fused_inspector, + was_fused: &mut was_fused, + }, + )?); + if !was_fused { + *executor.evm_mut().inspector_mut() = fused_inspector.clone(); + } + + // the state of the last traced transaction is not needed + if transactions.peek().is_some() { + executor.commit_transaction(output); + } + } Ok(Some(results)) }) @@ -435,3 +497,30 @@ pub trait Trace: LoadState> + Call { Ok(()) } } + +/// Context for one transaction of a block traced by [`Trace::trace_block_until_with_inspector`]. +#[derive(Debug)] +pub struct TracingCtx<'a, T, E: Evm> { + /// The transaction that was just executed. + pub tx: T, + /// Result of transaction execution. + pub result: ExecutionResult, + /// State changes after transaction. + pub state: &'a EvmState, + /// Inspector state after transaction. + pub inspector: &'a mut E::Inspector, + /// Database used when executing the transaction, _before_ committing the state changes. + pub db: &'a mut E::DB, + /// Inspector state before any transaction was executed. + fused_inspector: &'a E::Inspector, + /// Whether the inspector was already reset by [`TracingCtx::take_inspector`]. + was_fused: &'a mut bool, +} + +impl> TracingCtx<'_, T, E> { + /// Returns the inspector state of this transaction, resetting the inspector for the next one. + pub fn take_inspector(&mut self) -> E::Inspector { + *self.was_fused = true; + core::mem::replace(self.inspector, self.fused_inspector.clone()) + } +} diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 1154f793839..fd1ac2c3d65 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -20,7 +20,11 @@ use parking_lot::RwLock; use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks}; use reth_engine_primitives::ConsensusEngineEvent; use reth_errors::RethError; -use reth_evm::{block::BlockExecutor, execute::Executor, ConfigureEvm, EvmEnvFor}; +use reth_evm::{ + block::{BlockExecutor, TxResult}, + execute::Executor, + ConfigureEvm, EvmEnvFor, +}; use reth_primitives_traits::{ Block as BlockTrait, BlockBody, BlockTy, ReceiptWithBloom, RecoveredBlock, }; @@ -29,7 +33,7 @@ use reth_rpc_api::DebugApiServer; use reth_rpc_convert::RpcTxReq; use reth_rpc_eth_api::{ helpers::{EthTransactions, TraceExt}, - AsEthApiError, FromEthApiError, FromEvmError, RpcConvert, RpcNodeCore, + AsEthApiError, FromEthApiError, RpcConvert, RpcNodeCore, }; use reth_rpc_eth_types::{EthApiError, StateCacheDb}; use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult}; @@ -124,20 +128,22 @@ where .spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| { let mut results = Vec::with_capacity(block.body().transactions().len()); - eth_api.apply_pre_execution_changes(&block, &mut db)?; - let block_env = evm_env.block_env.clone(); - let mut transactions = block.transactions_recovered().enumerate().peekable(); let inspector = DebugInspector::new(opts).map_err(Eth::Error::from_eth_err)?; - let mut evm = - eth_api.evm_config().evm_with_env_and_inspector(&mut db, evm_env, inspector); + let mut executor = + eth_api.inspected_block_executor(&block, &mut db, evm_env, inspector)?; + executor.evm_mut().enable_inspector(); + + let mut transactions = block.transactions_recovered().enumerate().peekable(); while let Some((index, tx)) = transactions.next() { let tx_env = eth_api.evm_config().tx_env(tx); - let res = evm.transact(tx_env.clone()).map_err(Eth::Error::from_evm_err)?; + let output = executor + .execute_transaction_without_commit(tx) + .map_err(Eth::Error::from_eth_err)?; - let (db, inspector, _) = evm.components_mut(); + let (db, inspector, _) = executor.evm_mut().components_mut(); let result = inspector .get_result( Some(TransactionContext { @@ -147,7 +153,7 @@ where }), &tx_env, &block_env, - &res, + output.result(), db, ) .map_err(Eth::Error::from_eth_err)?; @@ -157,7 +163,7 @@ where inspector.fuse().map_err(Eth::Error::from_eth_err)?; // need to apply the state changes of this transaction before executing the // next transaction - db.commit(res.state) + executor.commit_transaction(output); } } @@ -180,8 +186,6 @@ where let tx_count = block.body().transactions().len(); let mut results = Vec::with_capacity(tx_count); - eth_api.apply_pre_execution_changes(&block, &mut db)?; - let block_env = evm_env.block_env.clone(); let mut transactions = block.transactions_recovered().enumerate().peekable(); let inspector = match DebugInspector::new(opts) { @@ -197,14 +201,15 @@ where return Ok(results) } }; - let mut evm = - eth_api.evm_config().evm_with_env_and_inspector(&mut db, evm_env, inspector); + let mut executor = + eth_api.inspected_block_executor(&block, &mut db, evm_env, inspector)?; + executor.evm_mut().enable_inspector(); while let Some((index, tx)) = transactions.next() { let tx_hash = *tx.tx_hash(); let tx_env = eth_api.evm_config().tx_env(tx); - let res = match evm.transact(tx_env.clone()) { - Ok(res) => res, + let output = match executor.execute_transaction_without_commit(tx) { + Ok(output) => output, Err(err) => { results.push(Some(TraceResult::Error { error: err.to_string(), @@ -214,7 +219,7 @@ where } }; - let (db, inspector, _) = evm.components_mut(); + let (db, inspector, _) = executor.evm_mut().components_mut(); let result = match inspector.get_result( Some(TransactionContext { block_hash: Some(block.hash()), @@ -223,7 +228,7 @@ where }), &tx_env, &block_env, - &res, + output.result(), db, ) { Ok(result) => result, @@ -245,7 +250,7 @@ where })); break } - db.commit(res.state); + executor.commit_transaction(output); } } @@ -316,11 +321,10 @@ where tx_hash: B256, opts: GethDebugTracingOptions, ) -> Result { - let (transaction, block, bal) = - match self.eth_api().transaction_and_block_and_maybe_bal(tx_hash).await? { - None => return Err(EthApiError::TracingTransactionNotFound.into()), - Some(res) => res, - }; + let (transaction, block) = match self.eth_api().transaction_and_block(tx_hash).await? { + None => return Err(EthApiError::TracingTransactionNotFound.into()), + Some(res) => res, + }; self.eth_api() .spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| { @@ -335,14 +339,8 @@ where let mut inspector = DebugInspector::new(opts).map_err(Eth::Error::from_eth_err)?; let tx_env = eth_api.evm_config().tx_env(&tx); - let (res, evm_env) = eth_api.inspect_transaction_in_block( - &block, - &mut db, - &mut inspector, - index, - tx_env.clone(), - bal.as_deref(), - )?; + let (res, evm_env) = + eth_api.inspect_transaction_in_block(&block, &mut db, &mut inspector, index)?; let trace = inspector .get_result( @@ -810,22 +808,24 @@ where .recovered_block(block_hash.into()) .await? .ok_or(EthApiError::HeaderNotFound(block_hash.into()))?; - let evm_env = self.eth_api().evm_env_for_header(block.sealed_block().sealed_header())?; self.eth_api() .spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| { // Enable transition tracking so that merge_transitions works db.transition_state = Some(Default::default()); - eth_api.apply_pre_execution_changes(&block, &mut db)?; + let mut executor = eth_api + .evm_config() + .executor_for_block(&mut db, block.sealed_block()) + .map_err(RethError::other) + .map_err(Eth::Error::from_eth_err)?; + executor.apply_pre_execution_changes().map_err(Eth::Error::from_eth_err)?; let mut roots = Vec::with_capacity(block.body().transactions().len()); - let mut evm = eth_api.evm_config().evm_with_env(&mut db, evm_env); for tx in block.transactions_recovered() { - let tx_env = eth_api.evm_config().tx_env(tx); - evm.transact_commit(tx_env).map_err(Eth::Error::from_evm_err)?; + executor.execute_transaction(tx).map_err(Eth::Error::from_eth_err)?; - let state = evm.db_mut(); + let state = executor.evm_mut().db_mut(); // Merge transitions into cumulative bundle_state state.merge_transitions(BundleRetention::PlainState); // Compute state root from the accumulated state changes