diff --git a/.circleci/config.yml b/.circleci/config.yml index 4ed70d500ab..f9056edad77 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -150,6 +150,11 @@ jobs: esac fi + act_checkout=() + if [ "<< parameters.wf >>" = "compact.yml" ]; then + act_checkout=(--no-skip-checkout) + fi + act_env=(--env "RUSTC_WRAPPER=" --env "GITHUB_BASE_REF=${base_ref}") if [ "<< parameters.wf >>" = "unit.yml" ] && [ "$target_job" = "state" ]; then # EF tests use Rayon internally, so unrestricted nextest processes multiply @@ -165,6 +170,7 @@ jobs: act "$event_name" -W ".github/workflows/<< parameters.wf >>" \ "${act_job[@]}" \ + "${act_checkout[@]}" \ --eventpath "$event_path" \ -P "ubuntu-latest=catthehacker/ubuntu@sha256:b839c14c4410998529ec18f951262bdf87a2b23bc1467304d07b491b9455e074" \ --concurrent-jobs << parameters.concurrent_jobs >> \ diff --git a/Cargo.lock b/Cargo.lock index dcd132cab81..1f22ca9af08 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/mod.rs similarity index 53% rename from crates/rpc/rpc-eth-api/src/helpers/trace.rs rename to crates/rpc/rpc-eth-api/src/helpers/trace/mod.rs index d51921713e5..f3e1ca23f9a 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace/mod.rs @@ -6,19 +6,33 @@ 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, RecoveredBlock}; +use reth_rpc_eth_types::{ + cache::db::{attach_bal_before_tx, StateCacheDb}, + EthApiError, +}; +use reth_storage_api::{HashedPostStateProvider, ProviderBlock, StateRootProvider}; +use revm::{ + context::Block, context_interface::result::ResultAndState, + database::states::bundle_state::BundleRetention, state::bal::Bal as RevmBal, DatabaseCommit, +}; +use revm_inspectors::tracing::{ + DebugInspector, TracingInspector, TracingInspectorConfig, TransactionContext, }; -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 std::sync::Arc; +mod replay; + +#[cfg(test)] +mod replay_tests; + /// Executes CPU heavy tasks. pub trait Trace: LoadState> + Call { /// Executes the [`TxEnvFor`] with [`reth_evm::EvmEnv`] against the given [`StateCacheDb`] @@ -106,14 +120,14 @@ pub trait Trace: LoadState> + Call { None => return Ok(None), Some(res) => res, }; - let (tx, tx_info) = transaction.split(); + 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 let parent_block = block.parent_hash(); self.spawn_with_state_at_block(parent_block, move |this, mut db| { - let (res, _) = this.inspect_transaction_in_block( + let (res, _) = this.inspect_canonical_transaction_in_block( &block, &mut db, &mut inspector, @@ -121,7 +135,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) @@ -206,6 +219,36 @@ pub trait Trace: LoadState> + Call { Ok((res, evm_env)) } + /// Executes the signed transaction recorded at `target_tx_index` in `block` with the + /// configured inspector on the state right before it, see + /// [`Trace::inspect_transaction_in_block`]. + /// + /// Returns [`EthApiError::InvalidParams`] if the block has no transaction at that index. + #[expect(clippy::type_complexity)] + fn inspect_canonical_transaction_in_block<'a>( + &self, + block: &RecoveredBlock>, + db: &'a mut StateCacheDb, + inspector: impl InspectorFor, + target_tx_index: usize, + bal: Option<&DecodedBal>>, + ) -> Result<(ResultAndState>, EvmEnvFor), Self::Error> { + let Some(tx) = block.transactions_recovered().nth(target_tx_index) else { + return Err(Self::Error::from_eth_err(EthApiError::InvalidParams(format!( + "tx_index {target_tx_index} out of bounds for block with {} transactions", + block.transaction_count() + )))) + }; + self.inspect_transaction_in_block( + block, + db, + inspector, + target_tx_index, + self.evm_config().tx_env(tx), + bal, + ) + } + /// 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` @@ -226,7 +269,7 @@ pub trait Trace: LoadState> + Call { TransactionInfo, TracingCtx< '_, - Recovered<&ProviderTx>, + TracingInspector, EvmFor, >, ) -> Result @@ -258,23 +301,19 @@ pub trait Trace: LoadState> + Call { block_id: BlockId, block: Option>>>, highest_index: Option, - mut inspector_setup: Setup, + inspector_setup: Setup, f: F, ) -> impl Future>, Self::Error>> + Send where Self: LoadBlock, F: Fn( TransactionInfo, - TracingCtx< - '_, - Recovered<&ProviderTx>, - EvmFor, - >, + TracingCtx<'_, Insp, EvmFor>, ) -> Result + Send + 'static, Setup: FnMut() -> Insp + Send + 'static, - Insp: Clone + for<'a> InspectorFor, + Insp: for<'a> InspectorFor, R: Send + 'static, { async move { @@ -293,14 +332,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 +342,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 } @@ -363,7 +381,7 @@ pub trait Trace: LoadState> + Call { TransactionInfo, TracingCtx< '_, - Recovered<&ProviderTx>, + TracingInspector, EvmFor, >, ) -> Result @@ -401,16 +419,12 @@ pub trait Trace: LoadState> + Call { // state and db F: Fn( TransactionInfo, - TracingCtx< - '_, - Recovered<&ProviderTx>, - EvmFor, - >, + TracingCtx<'_, Insp, EvmFor>, ) -> Result + Send + 'static, Setup: FnMut() -> Insp + Send + 'static, - Insp: Clone + for<'a> InspectorFor, + Insp: for<'a> InspectorFor, R: Send + 'static, { self.trace_block_until_with_inspector(block_id, block, None, insp_setup, f) @@ -434,4 +448,270 @@ 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`]: `inspector_setup` is invoked once per + /// transaction. 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, + mut inspector_setup: Setup, + f: F, + ) -> Result, Self::Error> + where + F: Fn( + TransactionInfo, + TracingCtx<'_, Insp, EvmFor>, + ) -> Result, + Setup: FnMut() -> Insp, + Insp: 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 evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector_setup()); + replay::trace_transactions( + evm, + block.transactions_recovered().take(max_transactions), + inspector_setup, + |index, tx, context| { + let transaction_info = TransactionInfo { + base_fee: Some(base_fee), + block_hash: Some(block_hash), + block_number: Some(block_number), + block_timestamp: Some(block_timestamp), + hash: Some(*tx.tx_hash()), + index: Some(index as u64), + }; + f(transaction_info, context) + }, + ) + } + + /// Traces every transaction of a block the node has validated, on `db`, which holds the + /// state of the block's parent. + /// + /// Backs `debug_traceBlockByHash` and `debug_traceBlockByNumber`. Chains whose block execution + /// changes state beyond the transactions themselves can override this to replay the block + /// through their block executor, while + /// [`Trace::debug_trace_unvalidated_block_transactions`] keeps serving blocks such an executor + /// could reject. + fn debug_trace_block_transactions( + &self, + db: &mut StateCacheDb, + block: &RecoveredBlock>, + evm_env: EvmEnvFor, + opts: GethDebugTracingOptions, + ) -> Result, Self::Error> { + self.debug_trace_unvalidated_block_transactions(db, block, evm_env, opts) + } + + /// Traces every transaction of a caller-supplied or rejected block on `db`, which holds the + /// state of the block's parent, without block-level validation. + /// + /// Backs `debug_traceBlock` and `debug_traceBadBlock`. + fn debug_trace_unvalidated_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 passed to per-transaction trace callbacks. +/// +/// Explicit callback annotations must import this type instead of alloy-evm's tracing context. +pub struct TracingCtx<'a, Insp, E: Evm> { + /// Execution result and state changes for the transaction. + pub result: &'a ResultAndState, + /// Database pointing to the transaction pre-state. + pub db: &'a mut E::DB, + /// Inspector used while executing the transaction. + pub inspector: Insp, +} + +impl core::fmt::Debug for TracingCtx<'_, Insp, E> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("TracingCtx").finish_non_exhaustive() + } +} + +impl TracingCtx<'_, Insp, E> { + /// Consumes this context and returns the inspector. + pub fn take_inspector(self) -> Insp { + self.inspector + } } diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace/replay.rs b/crates/rpc/rpc-eth-api/src/helpers/trace/replay.rs new file mode 100644 index 00000000000..997f7f7492e --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/helpers/trace/replay.rs @@ -0,0 +1,42 @@ +use super::TracingCtx; +use alloy_evm::{Evm, IntoTxEnv}; +use revm::DatabaseCommit; + +/// Executes `transactions` on `evm` in order and invokes `callback` with each one's index, the +/// transaction and its [`TracingCtx`]. +/// +/// `inspector_setup` provides the inspector for the next transaction once the previous one is +/// handed to the callback. Iteration stops at the first transaction that fails to execute, and +/// the state of the last executed transaction is not committed. +pub(super) fn trace_transactions( + mut evm: E, + transactions: Transactions, + mut inspector_setup: Setup, + mut callback: F, +) -> Result, Error> +where + E: Evm, + Setup: FnMut() -> E::Inspector, + Transactions: IntoIterator>, + F: FnMut(usize, Transactions::Item, TracingCtx<'_, E::Inspector, E>) -> Result, +{ + let mut results = Vec::new(); + let mut transactions = transactions.into_iter().enumerate().peekable(); + while let Some((index, transaction)) = transactions.next() { + let Ok(result) = evm.transact(transaction.clone()) else { + break; + }; + let (database, inspector, _) = evm.components_mut(); + let inspector = core::mem::replace(inspector, inspector_setup()); + let output = callback( + index, + transaction, + TracingCtx { result: &result, db: &mut *database, inspector }, + ); + if transactions.peek().is_some() { + database.commit(result.state); + } + results.push(output?); + } + Ok(results) +} diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace/replay_tests.rs b/crates/rpc/rpc-eth-api/src/helpers/trace/replay_tests.rs new file mode 100644 index 00000000000..dbfe87ef3e2 --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/helpers/trace/replay_tests.rs @@ -0,0 +1,169 @@ +use super::replay::trace_transactions; +use alloy_evm::{ + eth::{EthEvm, EthEvmFactory}, + tracing::TxTracer, + EvmEnv, EvmFactory, +}; +use alloy_primitives::{Address, Bytes, TxKind}; +use revm::{ + bytecode::Bytecode, + context::TxEnv, + context_interface::result::{ExecutionResult, HaltReason}, + database::{CacheDB, EmptyDB}, + interpreter::Interpreter, + state::{AccountInfo, EvmState}, + Database, Inspector, +}; + +const CALLER: Address = Address::repeat_byte(0x11); +const CONTRACT: Address = Address::repeat_byte(0x22); + +type TestDatabase = CacheDB; +type Snapshot = (ExecutionResult, EvmState, usize); + +#[derive(Clone, Debug, Default)] +struct CountingInspector { + calls: usize, + id: usize, +} + +impl Inspector for CountingInspector { + fn initialize_interp(&mut self, _interpreter: &mut Interpreter, _context: &mut Context) { + self.calls += 1; + } +} + +fn database() -> TestDatabase { + let mut database = CacheDB::new(EmptyDB::default()); + database.insert_account_info( + CONTRACT, + AccountInfo::default().with_code(Bytecode::new_legacy(Bytes::from_static(&[0x00]))), + ); + database +} + +fn evm( + database: &mut TestDatabase, + inspector: CountingInspector, +) -> EthEvm<&mut TestDatabase, CountingInspector, alloy_evm::precompiles::PrecompilesMap> { + EthEvmFactory::default().create_evm_with_inspector(database, EvmEnv::default(), inspector) +} + +fn transaction(nonce: u64) -> TxEnv { + TxEnv { + caller: CALLER, + gas_limit: 100_000, + kind: TxKind::Call(CONTRACT), + nonce, + ..Default::default() + } +} + +fn assert_replay_matches_tx_tracer(transaction_count: u64, take_inspector: bool) { + let transactions = (0..transaction_count).map(transaction).collect::>(); + let mut expected_database = database(); + let mut tracer = TxTracer::new(evm(&mut expected_database, CountingInspector::default())); + let expected = tracer + .trace_many(transactions.clone(), |mut context| { + let calls = if take_inspector { + context.take_inspector().calls + } else { + context.inspector.calls + }; + (context.result, context.state.clone(), calls) + }) + .collect::, _>>() + .unwrap(); + + let mut actual_database = database(); + let actual: Result, ()> = trace_transactions( + evm(&mut actual_database, CountingInspector::default()), + transactions, + CountingInspector::default, + |_, _, context| { + let result = context.result.result.clone(); + let state = context.result.state.clone(); + let calls = if take_inspector { + context.take_inspector().calls + } else { + context.inspector.calls + }; + Ok((result, state, calls)) + }, + ); + assert_eq!(actual.unwrap(), expected); + assert_eq!(actual_database.basic(CALLER).unwrap(), expected_database.basic(CALLER).unwrap()); + assert_eq!( + actual_database.basic(CALLER).unwrap().unwrap_or_default().nonce, + transaction_count.saturating_sub(1), + ); + assert!(expected.iter().all(|snapshot| snapshot.2 == 1)); +} + +#[test] +fn replay_matches_tx_tracer_for_empty_input() { + assert_replay_matches_tx_tracer(0, false); +} + +#[test] +fn replay_matches_tx_tracer_for_multiple_transactions() { + assert_replay_matches_tx_tracer(3, false); +} + +#[test] +fn replay_matches_tx_tracer_for_single_transaction() { + assert_replay_matches_tx_tracer(1, false); +} + +#[test] +fn replay_matches_tx_tracer_with_taken_inspector() { + assert_replay_matches_tx_tracer(3, true); +} + +#[test] +fn each_transaction_receives_the_inspector_set_up_for_it() { + let mut database = database(); + let mut created = 0; + let ids: Result, ()> = trace_transactions( + evm(&mut database, CountingInspector { calls: 0, id: 1 }), + (0..3).map(transaction), + move || { + created += 1; + CountingInspector { calls: 0, id: created + 1 } + }, + |_, _, context| Ok(context.take_inspector().id), + ); + assert_eq!(ids, Ok(vec![1, 2, 3])); +} + +#[test] +fn callback_failure_preserves_legacy_commit_order() { + let mut database = database(); + let result: Result, usize> = trace_transactions( + evm(&mut database, CountingInspector::default()), + (0..3).map(transaction), + CountingInspector::default, + |index, _, _| { + if index == 1 { + Err(index) + } else { + Ok(()) + } + }, + ); + assert_eq!(result, Err(1)); + assert_eq!(database.basic(CALLER).unwrap().unwrap().nonce, 2); +} + +#[test] +fn transaction_failure_preserves_legacy_truncation() { + let mut database = database(); + let result: Result, ()> = trace_transactions( + evm(&mut database, CountingInspector::default()), + [transaction(0), transaction(7), transaction(2)], + CountingInspector::default, + |index, _, _| Ok(index), + ); + assert_eq!(result, Ok(vec![0])); + assert_eq!(database.basic(CALLER).unwrap().unwrap().nonce, 1); +} diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 1154f793839..d5bd7dd487e 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,14 +29,13 @@ 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}; use reth_storage_api::{ - BlockIdReader, BlockReaderIdExt, HashedPostStateProvider, HeaderProvider, ProviderBlock, - ReceiptProviderIdExt, StateProviderFactory, StateRootProvider, StorageRootProvider, - TransactionVariant, + BlockIdReader, BlockReaderIdExt, HeaderProvider, ProviderBlock, ReceiptProviderIdExt, + StateProviderFactory, StateRootProvider, StorageRootProvider, TransactionVariant, }; use reth_tasks::{pool::BlockingTaskGuard, Runtime}; use reth_transaction_pool::TransactionPool; @@ -44,7 +43,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 +121,21 @@ 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) - } - } + eth_api.debug_trace_block_transactions(&mut db, &block, evm_env, opts) + }) + .await + } - Ok(results) + /// Traces a caller-supplied or rejected block asynchronously, without block-level validation. + async fn trace_unvalidated_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| { + eth_api.debug_trace_unvalidated_block_transactions(&mut db, &block, evm_env, opts) }) .await } @@ -177,80 +151,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 } @@ -285,7 +186,12 @@ where } .map_err(Eth::Error::from_eth_err)?; - self.trace_block(Arc::new(block.into_recovered_with_signers(senders)), evm_env, opts).await + self.trace_unvalidated_block( + Arc::new(block.into_recovered_with_signers(senders)), + evm_env, + opts, + ) + .await } /// Replays a block and returns the trace of each transaction. @@ -335,12 +241,11 @@ 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( + let (res, evm_env) = eth_api.inspect_canonical_transaction_in_block( &block, &mut db, &mut inspector, index, - tx_env.clone(), bal.as_deref(), )?; @@ -814,33 +719,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 } @@ -1416,7 +1295,7 @@ where .to_rpc_result()?; let opts = opts.map(|o| o.tracing_options).unwrap_or_default(); - self.trace_block(entry.block.clone(), evm_env, opts).await.map_err(Into::into) + self.trace_unvalidated_block(entry.block.clone(), evm_env, opts).await.map_err(Into::into) } } diff --git a/crates/rpc/rpc/src/otterscan.rs b/crates/rpc/rpc/src/otterscan.rs index 7d43540b7fe..a3f493764f8 100644 --- a/crates/rpc/rpc/src/otterscan.rs +++ b/crates/rpc/rpc/src/otterscan.rs @@ -340,7 +340,7 @@ where num.into(), None, TracingInspectorConfig::default_parity(), - |tx_info, mut ctx| { + |tx_info, ctx| { Ok(ctx .take_inspector() .into_parity_builder() diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index c79b087b169..bf587654c07 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -34,7 +34,7 @@ use revm::DatabaseCommit; use revm_inspectors::{ opcode::OpcodeGasInspector, storage::StorageInspector, - tracing::{parity::populate_state_diff, TracingInspector, TracingInspectorConfig}, + tracing::{TracingInspector, TracingInspectorConfig}, }; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -443,7 +443,7 @@ where Some(block.clone()), None, TracingInspectorConfig::default_parity(), - move |tx_info, mut ctx| { + move |tx_info, ctx| { // Keep the block replay permit inside the spawned replay task. let _block_replay_permit = &permit; let mut traces = ctx @@ -525,7 +525,7 @@ where block_id, Some(block.clone()), TracingInspectorConfig::default_parity(), - |tx_info, mut ctx| { + |tx_info, ctx| { let traces = ctx .take_inspector() .into_parity_builder() @@ -561,18 +561,12 @@ where block_id, None, TracingInspectorConfig::from_parity_config(&trace_types), - move |tx_info, mut ctx| { - let mut full_trace = ctx - .take_inspector() + move |tx_info, ctx| { + let full_trace = ctx + .inspector .into_parity_builder() - .into_trace_results(&ctx.result, &trace_types); - - // If statediffs were requested, populate them with the account balance and - // nonce from pre-state - if let Some(ref mut state_diff) = full_trace.state_diff { - populate_state_diff(state_diff, &ctx.db, ctx.state.iter()) - .map_err(Eth::Error::from_eth_err)?; - } + .into_trace_results_with_state(ctx.result, &trace_types, &**ctx.db) + .map_err(Eth::Error::from_eth_err)?; let trace = TraceResultsWithTransactionHash { transaction_hash: tx_info.hash.expect("tx hash is set"), @@ -638,7 +632,7 @@ where block_id, Some(block.clone()), StorageInspector::default, - move |tx_info, mut ctx| { + move |tx_info, ctx| { let unique_loads = ctx.inspector.unique_loads(); let warm_loads = ctx.inspector.warm_loads(); let trace = TransactionStorageAccess {