From 26e5f108e80973adadbfd582c45885a04f24ce41 Mon Sep 17 00:00:00 2001 From: geoknee Date: Fri, 23 Oct 2026 16:14:28 +0100 Subject: [PATCH] refactor(rpc): make block replays overridable Move the per-block replay loops behind defaulted `Trace` methods so an `EthApi` whose block executor keeps per-transaction state can replay through it: `trace_block_transactions` (backing `trace_block_until_with_inspector`), `debug_trace_block_transactions`, `debug_trace_chain_block_transactions` and `block_intermediate_roots`. The bodies are unchanged. Callbacks get a reth-owned `TracingCtx` that can be constructed outside the crate, since alloy-evm's can't; it borrows the execution result. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/rpc/rpc-eth-api/Cargo.toml | 1 + crates/rpc/rpc-eth-api/src/helpers/trace.rs | 350 ++++++++++++++++++-- crates/rpc/rpc/src/debug.rs | 150 +-------- crates/rpc/rpc/src/trace.rs | 2 +- 5 files changed, 324 insertions(+), 180 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0af8e7f9822..e9ca4c7f1d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10268,6 +10268,7 @@ dependencies = [ "alloy-rlp", "alloy-rpc-types-eth", "alloy-rpc-types-mev", + "alloy-rpc-types-trace", "alloy-serde", "async-trait", "auto_impl", diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index f3b12654670..bbbb5464993 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -44,6 +44,7 @@ alloy-network.workspace = true alloy-primitives.workspace = true alloy-rpc-types-eth.workspace = true alloy-rpc-types-mev.workspace = true +alloy-rpc-types-trace.workspace = true alloy-consensus.workspace = true # rpc diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index d51921713e5..4f7bcd3ccc9 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs @@ -6,17 +6,26 @@ use alloy_consensus::{transaction::TxHashRef, BlockHeader}; use alloy_eip7928::bal::DecodedBal; use alloy_primitives::B256; use alloy_rpc_types_eth::{BlockId, TransactionInfo}; +use alloy_rpc_types_trace::geth::{GethDebugTracingOptions, TraceResult}; 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, 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_storage_api::{ProviderBlock, ProviderTx}; -use revm::{context::Block, context_interface::result::ResultAndState, state::bal::Bal as RevmBal}; -use revm_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; +use reth_storage_api::{HashedPostStateProvider, ProviderBlock, ProviderTx, StateRootProvider}; +use revm::{ + context::Block, + context_interface::result::{ExecutionResult, ResultAndState}, + database::states::bundle_state::BundleRetention, + state::{bal::Bal as RevmBal, EvmState}, + DatabaseCommit, +}; +use revm_inspectors::tracing::{ + DebugInspector, TracingInspector, TracingInspectorConfig, TransactionContext, +}; use std::sync::Arc; /// Executes CPU heavy tasks. @@ -293,14 +302,6 @@ pub trait Trace: LoadState> + Call { // we need to get the state of the parent block because we're replaying this block // on top of its parent block's state self.spawn_with_state_at_block(block.parent_hash(), move |this, mut db| { - let block_hash = block.hash(); - - let block_number = evm_env.block_env.number().saturating_to(); - 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( @@ -311,28 +312,15 @@ pub trait Trace: LoadState> + Call { }, ); - 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::>()?; - - Ok(Some(results)) + this.trace_block_transactions( + &mut db, + &block, + evm_env, + max_transactions, + inspector_setup(), + f, + ) + .map(Some) }) .await } @@ -434,4 +422,296 @@ pub trait Trace: LoadState> + Call { .map_err(Self::Error::from_eth_err)?; Ok(()) } + + /// Executes the first `max_transactions` transactions of `block` on `db`, which holds the + /// state of the block's parent, and invokes `f` with each transaction's [`TracingCtx`]. + /// + /// Backs [`Trace::trace_block_until_with_inspector`]. Iteration stops at the first transaction + /// that fails to execute, and the state of the last executed transaction is not committed. + fn trace_block_transactions( + &self, + db: &mut StateCacheDb, + block: &RecoveredBlock>, + evm_env: EvmEnvFor, + max_transactions: usize, + inspector: Insp, + f: F, + ) -> Result, Self::Error> + where + F: Fn( + TransactionInfo, + TracingCtx< + '_, + Recovered<&ProviderTx>, + EvmFor, + >, + ) -> Result, + Insp: Clone + for<'a> InspectorFor, + { + let block_hash = block.hash(); + let block_number = evm_env.block_env.number().saturating_to(); + let block_timestamp = evm_env.block_env.timestamp().saturating_to(); + let base_fee = evm_env.block_env.basefee(); + + self.apply_pre_execution_changes(block, db)?; + + let fused_inspector = inspector.clone(); + let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, 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 Ok(ResultAndState { result, state }) = evm.transact(tx) else { break }; + + 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, _) = evm.components_mut(); + let output = f( + tx_info, + TracingCtx::new( + tx, + &result, + &state, + &mut *inspector, + &mut *db, + &fused_inspector, + &mut was_fused, + ), + ); + if transactions.peek().is_some() { + db.commit(state); + } + if !was_fused { + *inspector = fused_inspector.clone(); + } + results.push(output?); + } + Ok(results) + } + + /// Traces every transaction of `block` with a [`DebugInspector`] configured by `opts`, on `db`, + /// which holds the state of the block's parent. + /// + /// Backs `debug_traceBlockByHash`, `debug_traceBlockByNumber`, `debug_traceBlock` and + /// `debug_traceBadBlock`. + fn debug_trace_block_transactions( + &self, + db: &mut StateCacheDb, + block: &RecoveredBlock>, + evm_env: EvmEnvFor, + opts: GethDebugTracingOptions, + ) -> Result, Self::Error> { + let mut results = Vec::with_capacity(block.body().transactions().len()); + + self.apply_pre_execution_changes(block, db)?; + + let block_env = evm_env.block_env.clone(); + + let mut transactions = block.transactions_recovered().enumerate().peekable(); + let inspector = DebugInspector::new(opts).map_err(Self::Error::from_eth_err)?; + let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector); + while let Some((index, tx)) = transactions.next() { + let tx_env = self.evm_config().tx_env(tx); + + let res = evm.transact(tx_env.clone()).map_err(Self::Error::from_evm_err)?; + + let (db, inspector, _) = evm.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, + &res, + db, + ) + .map_err(Self::Error::from_eth_err)?; + + results.push(TraceResult::Success { result, tx_hash: Some(*tx.tx_hash()) }); + if transactions.peek().is_some() { + inspector.fuse().map_err(Self::Error::from_eth_err)?; + // need to apply the state changes of this transaction before executing the + // next transaction + db.commit(res.state) + } + } + + Ok(results) + } + + /// Traces every transaction of `block` like [`Trace::debug_trace_block_transactions`], for + /// `debug_traceChain`: a transaction that fails to trace is reported as a + /// [`TraceResult::Error`] and ends the block, and the transactions after it are `None`. + fn debug_trace_chain_block_transactions( + &self, + db: &mut StateCacheDb, + block: &RecoveredBlock>, + evm_env: EvmEnvFor, + opts: GethDebugTracingOptions, + ) -> Result>, Self::Error> { + let tx_count = block.body().transactions().len(); + let mut results = Vec::with_capacity(tx_count); + + self.apply_pre_execution_changes(block, db)?; + + let block_env = evm_env.block_env.clone(); + let mut transactions = block.transactions_recovered().enumerate().peekable(); + let inspector = match DebugInspector::new(opts) { + Ok(inspector) => inspector, + Err(err) => { + if let Some((_, tx)) = transactions.peek() { + results.push(Some(TraceResult::Error { + error: err.to_string(), + tx_hash: Some(*tx.tx_hash()), + })); + } + results.resize(tx_count, None); + return Ok(results) + } + }; + let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector); + + while let Some((index, tx)) = transactions.next() { + let tx_hash = *tx.tx_hash(); + let tx_env = self.evm_config().tx_env(tx); + let res = match evm.transact(tx_env.clone()) { + Ok(res) => res, + Err(err) => { + results.push(Some(TraceResult::Error { + error: err.to_string(), + tx_hash: Some(tx_hash), + })); + break + } + }; + + let (db, inspector, _) = evm.components_mut(); + let result = match inspector.get_result( + Some(TransactionContext { + block_hash: Some(block.hash()), + tx_hash: Some(tx_hash), + tx_index: Some(index), + }), + &tx_env, + &block_env, + &res, + db, + ) { + Ok(result) => result, + Err(err) => { + results.push(Some(TraceResult::Error { + error: err.to_string(), + tx_hash: Some(tx_hash), + })); + break + } + }; + + results.push(Some(TraceResult::Success { result, tx_hash: Some(tx_hash) })); + if let Some((_, next_tx)) = transactions.peek() { + if let Err(err) = inspector.fuse() { + results.push(Some(TraceResult::Error { + error: err.to_string(), + tx_hash: Some(*next_tx.tx_hash()), + })); + break + } + db.commit(res.state); + } + } + + results.resize(tx_count, None); + Ok(results) + } + + /// Executes every transaction of `block` on `db`, which holds the state of the block's + /// parent, and returns the state root after each one. + /// + /// Backs `debug_intermediateRoots`. + fn block_intermediate_roots( + &self, + db: &mut StateCacheDb, + block: &RecoveredBlock>, + evm_env: EvmEnvFor, + ) -> Result, Self::Error> { + // Enable transition tracking so that merge_transitions works + db.transition_state = Some(Default::default()); + + self.apply_pre_execution_changes(block, db)?; + + let mut roots = Vec::with_capacity(block.body().transactions().len()); + let mut evm = self.evm_config().evm_with_env(db, evm_env); + for tx in block.transactions_recovered() { + let tx_env = self.evm_config().tx_env(tx); + evm.transact_commit(tx_env).map_err(Self::Error::from_evm_err)?; + + let state = evm.db_mut(); + // Merge transitions into cumulative bundle_state + state.merge_transitions(BundleRetention::PlainState); + // Compute state root from the accumulated state changes + let hashed_state = state + .database + .hashed_post_state(&state.bundle_state) + .map_err(Self::Error::from_eth_err)?; + let root = + state.database.state_root(hashed_state).map_err(Self::Error::from_eth_err)?; + roots.push(root); + } + + Ok(roots) + } +} + +/// Context for one transaction of a block traced by [`Trace::trace_block_transactions`]. +#[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<'a, T, E: Evm> TracingCtx<'a, T, E> { + /// Creates the context for a transaction. `fused_inspector` is the inspector state before any + /// transaction was executed; `was_fused` is set when the callback takes the inspector, and the + /// caller resets the inspector to `fused_inspector` otherwise. + pub const fn new( + tx: T, + result: &'a ExecutionResult, + state: &'a EvmState, + inspector: &'a mut E::Inspector, + db: &'a mut E::DB, + fused_inspector: &'a E::Inspector, + was_fused: &'a mut bool, + ) -> Self { + Self { tx, result, state, inspector, db, fused_inspector, was_fused } + } +} + +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..9200ea70938 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -1,6 +1,6 @@ use alloy_consensus::{constants::KECCAK_EMPTY, transaction::TxHashRef, BlockHeader}; use alloy_eips::{eip2718::Encodable2718, BlockId, BlockNumberOrTag}; -use alloy_evm::{env::BlockEnvironment, Evm}; +use alloy_evm::env::BlockEnvironment; use alloy_genesis::ChainConfig; use alloy_primitives::{hex::decode, uint, Address, Bytes, B256, U256, U64}; use alloy_rlp::{Decodable, Encodable}; @@ -29,7 +29,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}; @@ -44,7 +44,7 @@ use reth_trie_common::{ root::storage_root_unsorted, updates::TrieUpdates, ExecutionWitnessMode, HashedPostState, HashedStorage, }; -use revm::{database::states::bundle_state::BundleRetention, Database, DatabaseCommit}; +use revm::{Database, DatabaseCommit}; use revm_inspectors::tracing::{DebugInspector, TransactionContext}; use serde::{Deserialize, Serialize}; use std::{collections::VecDeque, sync::Arc}; @@ -122,46 +122,7 @@ where ) -> 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()); - - 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); - 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 (db, inspector, _) = evm.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, - &res, - 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 - db.commit(res.state) - } - } - - Ok(results) + eth_api.debug_trace_block_transactions(&mut db, &block, evm_env, opts) }) .await } @@ -177,80 +138,7 @@ where self.eth_api() .spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| { - 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) { - Ok(inspector) => inspector, - Err(err) => { - if let Some((_, tx)) = transactions.peek() { - results.push(Some(TraceResult::Error { - error: err.to_string(), - tx_hash: Some(*tx.tx_hash()), - })); - } - results.resize(tx_count, None); - return Ok(results) - } - }; - let mut evm = - eth_api.evm_config().evm_with_env_and_inspector(&mut db, evm_env, 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, - Err(err) => { - results.push(Some(TraceResult::Error { - error: err.to_string(), - tx_hash: Some(tx_hash), - })); - break - } - }; - - let (db, inspector, _) = evm.components_mut(); - let result = match inspector.get_result( - Some(TransactionContext { - block_hash: Some(block.hash()), - tx_hash: Some(tx_hash), - tx_index: Some(index), - }), - &tx_env, - &block_env, - &res, - db, - ) { - Ok(result) => result, - Err(err) => { - results.push(Some(TraceResult::Error { - error: err.to_string(), - tx_hash: Some(tx_hash), - })); - break - } - }; - - results.push(Some(TraceResult::Success { result, tx_hash: Some(tx_hash) })); - if let Some((_, next_tx)) = transactions.peek() { - if let Err(err) = inspector.fuse() { - results.push(Some(TraceResult::Error { - error: err.to_string(), - tx_hash: Some(*next_tx.tx_hash()), - })); - break - } - db.commit(res.state); - } - } - - results.resize(tx_count, None); - Ok(results) + eth_api.debug_trace_chain_block_transactions(&mut db, &block, evm_env, opts) }) .await } @@ -814,33 +702,7 @@ where 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 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)?; - - let state = evm.db_mut(); - // Merge transitions into cumulative bundle_state - state.merge_transitions(BundleRetention::PlainState); - // Compute state root from the accumulated state changes - let hashed_state = state - .database - .hashed_post_state(&state.bundle_state) - .map_err(Eth::Error::from_eth_err)?; - let root = state - .database - .state_root(hashed_state) - .map_err(Eth::Error::from_eth_err)?; - roots.push(root); - } - - Ok(roots) + eth_api.block_intermediate_roots(&mut db, &block, evm_env) }) .await } 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