Skip to content

Commit 013b4c9

Browse files
Devon Bearmattsse
Devon Bear
andauthored
feat(rpc): Add tx_hash to debug_traceBlockByX (paradigmxyz#5743)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent 5062b7e commit 013b4c9

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
+26-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
//! Types used by tracing backends
22
3+
use alloy_primitives::TxHash;
34
use serde::{Deserialize, Serialize};
45

56
/// The result of a single transaction trace.
67
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
7-
#[serde(untagged)]
8-
#[allow(missing_docs)]
8+
#[serde(untagged, rename_all = "camelCase")]
99
pub enum TraceResult<Ok, Err> {
1010
/// Untagged success variant
11-
Success { result: Ok },
11+
Success {
12+
/// Trace results produced by the tracer
13+
result: Ok,
14+
/// transaction hash
15+
#[serde(skip_serializing_if = "Option::is_none")]
16+
tx_hash: Option<TxHash>,
17+
},
1218
/// Untagged error variant
13-
Error { error: Err },
19+
Error {
20+
/// Trace failure produced by the tracer
21+
error: Err,
22+
/// transaction hash
23+
#[serde(skip_serializing_if = "Option::is_none")]
24+
tx_hash: Option<TxHash>,
25+
},
26+
}
27+
28+
impl<Ok, Err> TraceResult<Ok, Err> {
29+
/// Returns the hash of the transaction that was traced.
30+
pub fn tx_hash(&self) -> Option<TxHash> {
31+
*match self {
32+
TraceResult::Success { tx_hash, .. } => tx_hash,
33+
TraceResult::Error { tx_hash, .. } => tx_hash,
34+
}
35+
}
1436
}

crates/rpc/rpc-types/src/eth/trace/geth/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,20 @@ mod tests {
559559
let input = serde_json::from_str::<serde_json::Value>(s).unwrap();
560560
similar_asserts::assert_eq!(input, val);
561561
}
562+
563+
#[test]
564+
fn test_trace_result_serde() {
565+
let s = r#" {
566+
"result": {
567+
"from": "0xccc5499e15fedaaeaba68aeb79b95b20f725bc56",
568+
"gas": "0x186a0",
569+
"gasUsed": "0xdb91",
570+
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
571+
"input": "0xa9059cbb000000000000000000000000e3f85a274c1edbea2f2498cf5978f41961cf8b5b0000000000000000000000000000000000000000000000000000000068c8f380",
572+
"value": "0x0",
573+
"type": "CALL"
574+
}
575+
}"#;
576+
let _result: TraceResult = serde_json::from_str(s).unwrap();
577+
}
562578
}

crates/rpc/rpc/src/debug.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,19 @@ where
102102

103103
let mut transactions = transactions.into_iter().peekable();
104104
while let Some(tx) = transactions.next() {
105+
let tx_hash = tx.hash;
105106
let tx = tx_env_with_recovered(&tx);
106107
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
107108
let (result, state_changes) =
108-
this.trace_transaction(opts.clone(), env, at, &mut db)?;
109-
results.push(TraceResult::Success { result });
110-
109+
this.trace_transaction(opts.clone(), env, at, &mut db).map_err(|err| {
110+
results.push(TraceResult::Error {
111+
error: err.to_string(),
112+
tx_hash: Some(tx_hash),
113+
});
114+
err
115+
})?;
116+
117+
results.push(TraceResult::Success { result, tx_hash: Some(tx_hash) });
111118
if transactions.peek().is_some() {
112119
// need to apply the state changes of this transaction before executing the
113120
// next transaction

0 commit comments

Comments
 (0)