|
| 1 | +use crate::{ |
| 2 | + DebugError, RpcCtx, |
| 3 | + utils::{await_handler, response_tri}, |
| 4 | +}; |
| 5 | +use ajj::{HandlerCtx, ResponsePayload}; |
| 6 | +use alloy::{consensus::BlockHeader, eips::BlockId, primitives::B256}; |
| 7 | +use itertools::Itertools; |
| 8 | +use reth::rpc::{ |
| 9 | + server_types::eth::EthApiError, |
| 10 | + types::{ |
| 11 | + TransactionInfo, |
| 12 | + trace::geth::{GethDebugTracingOptions, GethTrace, TraceResult}, |
| 13 | + }, |
| 14 | +}; |
| 15 | +use reth_node_api::FullNodeComponents; |
| 16 | +use signet_evm::EvmErrored; |
| 17 | +use signet_node_types::Pnt; |
| 18 | +use signet_types::MagicSig; |
| 19 | +use tracing::Instrument; |
| 20 | + |
| 21 | +/// Params for the `debug_traceBlockByNumber` and `debug_traceBlockByHash` |
| 22 | +/// endpoints. |
| 23 | +#[derive(Debug, serde::Deserialize)] |
| 24 | +pub(super) struct TraceBlockParams<T>(T, #[serde(default)] Option<GethDebugTracingOptions>); |
| 25 | + |
| 26 | +/// Params type for `debug_traceTransaction`.` |
| 27 | +#[derive(Debug, serde::Deserialize)] |
| 28 | +pub(super) struct TraceTransactionParams(B256, #[serde(default)] Option<GethDebugTracingOptions>); |
| 29 | + |
| 30 | +/// `debug_traceBlockByNumber` and `debug_traceBlockByHash` endpoint handler. |
| 31 | +pub(super) async fn trace_block<T, Host, Signet>( |
| 32 | + hctx: HandlerCtx, |
| 33 | + TraceBlockParams(id, opts): TraceBlockParams<T>, |
| 34 | + ctx: RpcCtx<Host, Signet>, |
| 35 | +) -> ResponsePayload<Vec<TraceResult>, DebugError> |
| 36 | +where |
| 37 | + T: Into<BlockId>, |
| 38 | + Host: FullNodeComponents, |
| 39 | + Signet: Pnt, |
| 40 | +{ |
| 41 | + let _permit = response_tri!( |
| 42 | + ctx.acquire_tracing_permit() |
| 43 | + .await |
| 44 | + .map_err(|_| DebugError::rpc_error("Failed to acquire tracing permit".into())) |
| 45 | + ); |
| 46 | + |
| 47 | + let id = id.into(); |
| 48 | + let span = tracing::debug_span!("traceBlock", ?id, tracer = ?opts.as_ref().and_then(|o| o.tracer.as_ref())); |
| 49 | + |
| 50 | + let fut = async move { |
| 51 | + // Fetch the block by ID |
| 52 | + let Some((hash, block)) = response_tri!(ctx.signet().raw_block(id).await) else { |
| 53 | + return ResponsePayload::internal_error_message( |
| 54 | + EthApiError::HeaderNotFound(id).to_string().into(), |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + tracing::debug!(number = block.number(), "Loaded block"); |
| 59 | + |
| 60 | + // Allocate space for the frames |
| 61 | + let mut frames = Vec::with_capacity(block.transaction_count()); |
| 62 | + |
| 63 | + // Instantiate the EVM with the block |
| 64 | + let mut trevm = response_tri!(ctx.trevm(crate::LoadState::Before, block.header())); |
| 65 | + |
| 66 | + // Apply all transactions in the block up, tracing each one |
| 67 | + let opts = opts.unwrap_or_default(); |
| 68 | + |
| 69 | + tracing::trace!(?opts, "Tracing block transactions"); |
| 70 | + |
| 71 | + let mut txns = block.body().transactions().enumerate().peekable(); |
| 72 | + for (idx, tx) in txns |
| 73 | + .by_ref() |
| 74 | + .peeking_take_while(|(_, t)| MagicSig::try_from_signature(t.signature()).is_none()) |
| 75 | + { |
| 76 | + let tx_info = TransactionInfo { |
| 77 | + hash: Some(*tx.hash()), |
| 78 | + index: Some(idx as u64), |
| 79 | + block_hash: Some(hash), |
| 80 | + block_number: Some(block.header().number()), |
| 81 | + base_fee: block.header().base_fee_per_gas(), |
| 82 | + }; |
| 83 | + |
| 84 | + let t = trevm.fill_tx(tx); |
| 85 | + |
| 86 | + let frame; |
| 87 | + (frame, trevm) = response_tri!(crate::debug::tracer::trace(t, &opts, tx_info)); |
| 88 | + frames.push(TraceResult::Success { result: frame, tx_hash: Some(*tx.hash()) }); |
| 89 | + |
| 90 | + tracing::debug!(tx_index = idx, tx_hash = ?tx.hash(), "Traced transaction"); |
| 91 | + } |
| 92 | + |
| 93 | + ResponsePayload::Success(frames) |
| 94 | + } |
| 95 | + .instrument(span); |
| 96 | + |
| 97 | + await_handler!(@response_option hctx.spawn_blocking(fut)) |
| 98 | +} |
| 99 | + |
| 100 | +/// Handle for `debug_traceTransaction`. |
| 101 | +pub(super) async fn trace_transaction<Host, Signet>( |
| 102 | + hctx: HandlerCtx, |
| 103 | + TraceTransactionParams(tx_hash, opts): TraceTransactionParams, |
| 104 | + ctx: RpcCtx<Host, Signet>, |
| 105 | +) -> ResponsePayload<GethTrace, DebugError> |
| 106 | +where |
| 107 | + Host: FullNodeComponents, |
| 108 | + Signet: Pnt, |
| 109 | +{ |
| 110 | + let _permit = response_tri!( |
| 111 | + ctx.acquire_tracing_permit() |
| 112 | + .await |
| 113 | + .map_err(|_| DebugError::rpc_error("Failed to acquire tracing permit".into())) |
| 114 | + ); |
| 115 | + |
| 116 | + let span = tracing::debug_span!("traceTransaction", %tx_hash, tracer = ?opts.as_ref().and_then(|o| o.tracer.as_ref())); |
| 117 | + |
| 118 | + let fut = async move { |
| 119 | + // Load the transaction by hash |
| 120 | + let (tx, meta) = response_tri!( |
| 121 | + response_tri!(ctx.signet().raw_transaction_by_hash(tx_hash)) |
| 122 | + .ok_or(EthApiError::TransactionNotFound) |
| 123 | + ); |
| 124 | + |
| 125 | + tracing::debug!("Loaded transaction metadata"); |
| 126 | + |
| 127 | + // Load the block containing the transaction |
| 128 | + let res = response_tri!(ctx.signet().raw_block(meta.block_hash).await); |
| 129 | + let (_, block) = |
| 130 | + response_tri!(res.ok_or_else(|| EthApiError::HeaderNotFound(meta.block_hash.into()))); |
| 131 | + |
| 132 | + tracing::debug!(number = block.number(), "Loaded containing block"); |
| 133 | + |
| 134 | + // Load trevm at the start of the block (i.e. before any transactions are applied) |
| 135 | + let mut trevm = response_tri!(ctx.trevm(crate::LoadState::Before, block.header())); |
| 136 | + |
| 137 | + // Apply all transactions in the block up to (but not including) the |
| 138 | + // target one |
| 139 | + let mut txns = block.body().transactions().enumerate().peekable(); |
| 140 | + for (_idx, tx) in txns.by_ref().peeking_take_while(|(_, t)| t.hash() != tx.hash()) { |
| 141 | + if MagicSig::try_from_signature(tx.signature()).is_some() { |
| 142 | + return ResponsePayload::internal_error_message( |
| 143 | + EthApiError::TransactionNotFound.to_string().into(), |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + trevm = response_tri!(trevm.run_tx(tx).map_err(EvmErrored::into_error)).accept_state(); |
| 148 | + } |
| 149 | + |
| 150 | + let (index, tx) = response_tri!(txns.next().ok_or(EthApiError::TransactionNotFound)); |
| 151 | + |
| 152 | + let trevm = trevm.fill_tx(tx); |
| 153 | + |
| 154 | + let tx_info = TransactionInfo { |
| 155 | + hash: Some(*tx.hash()), |
| 156 | + index: Some(index as u64), |
| 157 | + block_hash: Some(block.hash()), |
| 158 | + block_number: Some(block.header().number()), |
| 159 | + base_fee: block.header().base_fee_per_gas(), |
| 160 | + }; |
| 161 | + |
| 162 | + let res = |
| 163 | + response_tri!(crate::debug::tracer::trace(trevm, &opts.unwrap_or_default(), tx_info)).0; |
| 164 | + |
| 165 | + ResponsePayload::Success(res) |
| 166 | + } |
| 167 | + .instrument(span); |
| 168 | + |
| 169 | + await_handler!(@response_option hctx.spawn_blocking(fut)) |
| 170 | +} |
0 commit comments