Skip to content

Commit

Permalink
feat(rpc): Add tx_hash to debug_traceBlockByX (#5743)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Devon Bear and mattsse authored Dec 13, 2023
1 parent 5062b7e commit 013b4c9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
30 changes: 26 additions & 4 deletions crates/rpc/rpc-types/src/eth/trace/common.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
//! Types used by tracing backends
use alloy_primitives::TxHash;
use serde::{Deserialize, Serialize};

/// The result of a single transaction trace.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[allow(missing_docs)]
#[serde(untagged, rename_all = "camelCase")]
pub enum TraceResult<Ok, Err> {
/// Untagged success variant
Success { result: Ok },
Success {
/// Trace results produced by the tracer
result: Ok,
/// transaction hash
#[serde(skip_serializing_if = "Option::is_none")]
tx_hash: Option<TxHash>,
},
/// Untagged error variant
Error { error: Err },
Error {
/// Trace failure produced by the tracer
error: Err,
/// transaction hash
#[serde(skip_serializing_if = "Option::is_none")]
tx_hash: Option<TxHash>,
},
}

impl<Ok, Err> TraceResult<Ok, Err> {
/// Returns the hash of the transaction that was traced.
pub fn tx_hash(&self) -> Option<TxHash> {
*match self {
TraceResult::Success { tx_hash, .. } => tx_hash,
TraceResult::Error { tx_hash, .. } => tx_hash,
}
}
}
16 changes: 16 additions & 0 deletions crates/rpc/rpc-types/src/eth/trace/geth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,20 @@ mod tests {
let input = serde_json::from_str::<serde_json::Value>(s).unwrap();
similar_asserts::assert_eq!(input, val);
}

#[test]
fn test_trace_result_serde() {
let s = r#" {
"result": {
"from": "0xccc5499e15fedaaeaba68aeb79b95b20f725bc56",
"gas": "0x186a0",
"gasUsed": "0xdb91",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"input": "0xa9059cbb000000000000000000000000e3f85a274c1edbea2f2498cf5978f41961cf8b5b0000000000000000000000000000000000000000000000000000000068c8f380",
"value": "0x0",
"type": "CALL"
}
}"#;
let _result: TraceResult = serde_json::from_str(s).unwrap();
}
}
13 changes: 10 additions & 3 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,19 @@ where

let mut transactions = transactions.into_iter().peekable();
while let Some(tx) = transactions.next() {
let tx_hash = tx.hash;
let tx = tx_env_with_recovered(&tx);
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
let (result, state_changes) =
this.trace_transaction(opts.clone(), env, at, &mut db)?;
results.push(TraceResult::Success { result });

this.trace_transaction(opts.clone(), env, at, &mut db).map_err(|err| {
results.push(TraceResult::Error {
error: err.to_string(),
tx_hash: Some(tx_hash),
});
err
})?;

results.push(TraceResult::Success { result, tx_hash: Some(tx_hash) });
if transactions.peek().is_some() {
// need to apply the state changes of this transaction before executing the
// next transaction
Expand Down

0 comments on commit 013b4c9

Please sign in to comment.