diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 48249a286fa..76fb647b2a0 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -21,11 +21,12 @@ 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, HaltReasonFor, InspectorFor, TransactionEnvMut, TxEnvFor, }; use reth_node_api::BlockBody; -use reth_primitives_traits::Recovered; use reth_revm::{ cancelled::CancelOnDrop, database::StateProviderDatabase, @@ -41,7 +42,7 @@ use reth_rpc_eth_types::{ simulate::{self, EthSimulateError}, EthApiError, StateCacheDb, }; -use reth_storage_api::{BlockIdReader, ProviderTx}; +use reth_storage_api::BlockIdReader; use revm::{ context::Block, context_interface::{result::ResultAndState, Cfg, Transaction}, @@ -704,7 +705,7 @@ pub trait Call: /// /// 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. + /// block through the block executor, which then also executes the transaction. /// 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 @@ -766,9 +767,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) }) @@ -777,38 +779,6 @@ pub trait Call: } } - /// Replays all transactions before the target transaction index on the given EVM. - /// - /// 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. - /// - /// If the target index is greater than or equal to the iterator length, all transactions are - /// replayed. - fn replay_transactions_until_with_evm<'a, DB, I, Txs>( - &self, - evm: &mut EvmFor, - transactions: Txs, - target_tx_index: usize, - ) -> Result<(), Self::Error> - where - DB: Database> + DatabaseCommit + core::fmt::Debug, - I: InspectorFor, - Txs: IntoIterator>>, - { - for (index, tx) in transactions.into_iter().enumerate() { - if index == target_tx_index { - // reached the target transaction - break - } - - let tx_env = self.evm_config().tx_env(tx); - evm.transact_commit(tx_env).map_err(Self::Error::from_evm_err)?; - } - Ok(()) - } - /// /// All `TxEnv` fields are derived from the given [`RpcTxReq`], if fields are /// `None`, they fall back to the [`reth_evm::EvmEnv`]'s settings. diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index d51921713e5..8aabda279a4 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; @@ -40,7 +47,7 @@ pub trait Trace: LoadState> + Call { /// /// 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. + /// the block, 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 @@ -73,7 +80,7 @@ pub trait Trace: LoadState> + Call { /// /// 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. + /// the block, 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 @@ -134,9 +141,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 +160,29 @@ 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. /// /// 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. + /// target index, see [`attach_bal_before_tx`], and `target_tx_env` is executed on its own, so + /// per-transaction changes the block executor applies are skipped. Otherwise all transactions + /// before the target transaction are replayed without inspection through the block + /// executor, which then executes the block's transaction at the target index, so the result + /// includes any per-transaction changes the executor applies. The result is not committed + /// to `db`. #[expect(clippy::type_complexity)] fn inspect_transaction_in_block<'a>( &self, @@ -179,33 +193,77 @@ pub trait Trace: LoadState> + Call { target_tx_env: impl IntoTxEnv>, bal: Option<&DecodedBal>>, ) -> Result<(ResultAndState>, EvmEnvFor), Self::Error> { + let evm_env = self.evm_env_for_header(block.sealed_block().sealed_header())?; + 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 mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector); + let res = evm.transact(target_tx_env).map_err(Self::Error::from_evm_err)?; + let (_, evm_env) = evm.finish(); + return Ok((res, evm_env)) } - 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's post-execution gas refunds, that a bare EVM replay misses. + #[expect(clippy::type_complexity, reason = "the executor type is spelled out by its factory")] + 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 +357,63 @@ 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, + state, + inspector, + db, + fused_inspector: &fused_inspector, + was_fused: &mut was_fused, + }, + )?); + // The inspector and state of the last traced transaction are not needed. + if transactions.peek().is_some() { + if !was_fused { + *executor.evm_mut().inspector_mut() = fused_inspector.clone(); + } + executor.commit_transaction(output); + } + } Ok(Some(results)) }) @@ -435,3 +518,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: &'a 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..80c83c98cac 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, }; @@ -113,7 +117,10 @@ where self.inner.blocking_task_guard.clone().acquire_owned().await } - /// Trace the entire block asynchronously + /// Trace the entire block asynchronously. + /// + /// Transactions run on a bare EVM, so a block the executor would reject, such as a bad block, + /// is still traced. async fn trace_block( &self, block: Arc>>, @@ -166,6 +173,62 @@ where .await } + /// Traces a canonical block like [`Self::trace_block`], but replays it through the block + /// executor, so the traces include the per-transaction changes the executor applies. + async fn trace_canonical_block( + &self, + block: Arc>>, + evm_env: EvmEnvFor, + opts: GethDebugTracingOptions, + ) -> Result, Eth::Error> { + self.eth_api() + .spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| { + let mut results = Vec::with_capacity(block.body().transactions().len()); + + let block_env = evm_env.block_env.clone(); + + let inspector = DebugInspector::new(opts).map_err(Eth::Error::from_eth_err)?; + 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 output = executor + .execute_transaction_without_commit(tx) + .map_err(Eth::Error::from_eth_err)?; + + let (db, inspector, _) = executor.evm_mut().components_mut(); + let result = inspector + .get_result( + Some(TransactionContext { + block_hash: Some(block.hash()), + tx_hash: Some(*tx.tx_hash()), + tx_index: Some(index), + }), + &tx_env, + &block_env, + output.result(), + db, + ) + .map_err(Eth::Error::from_eth_err)?; + + results.push(TraceResult::Success { result, tx_hash: Some(*tx.tx_hash()) }); + if transactions.peek().is_some() { + inspector.fuse().map_err(Eth::Error::from_eth_err)?; + // need to apply the state changes of this transaction before executing the + // next transaction + executor.commit_transaction(output); + } + } + + Ok(results) + }) + .await + } + /// Traces a block for `traceChain`, preserving transaction-level tracing failures in the /// subscription result instead of failing the entire range. async fn trace_chain_block( @@ -180,8 +243,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 +258,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 +276,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 +285,7 @@ where }), &tx_env, &block_env, - &res, + output.result(), db, ) { Ok(result) => result, @@ -245,7 +307,7 @@ where })); break } - db.commit(res.state); + executor.commit_transaction(output); } } @@ -305,7 +367,7 @@ where } let evm_env = self.eth_api().evm_env_for_header(block.sealed_block().sealed_header())?; - self.trace_block(block, evm_env, opts).await + self.trace_canonical_block(block, evm_env, opts).await } /// Trace the transaction according to the provided options. @@ -810,22 +872,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 diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index c79b087b169..c3199cb0a45 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -565,7 +565,7 @@ where let mut full_trace = ctx .take_inspector() .into_parity_builder() - .into_trace_results(&ctx.result, &trace_types); + .into_trace_results(ctx.result, &trace_types); // If statediffs were requested, populate them with the account balance and // nonce from pre-state