feat(rpc): Add tx_hash to debug_traceBlockByX (#5743)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Devon Bear
2023-12-13 13:36:24 -05:00
committed by GitHub
parent 5062b7ea86
commit 013b4c93db
3 changed files with 51 additions and 6 deletions

View File

@ -1,14 +1,36 @@
//! Types used by tracing backends //! Types used by tracing backends
use alloy_primitives::TxHash;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// The result of a single transaction trace. /// The result of a single transaction trace.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged, rename_all = "camelCase")]
#[allow(missing_docs)]
pub enum TraceResult<Ok, Err> { pub enum TraceResult<Ok, Err> {
/// Untagged success variant /// 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 /// 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,
}
}
} }

View File

@ -559,4 +559,20 @@ mod tests {
let input = serde_json::from_str::<serde_json::Value>(s).unwrap(); let input = serde_json::from_str::<serde_json::Value>(s).unwrap();
similar_asserts::assert_eq!(input, val); 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();
}
} }

View File

@ -102,12 +102,19 @@ where
let mut transactions = transactions.into_iter().peekable(); let mut transactions = transactions.into_iter().peekable();
while let Some(tx) = transactions.next() { while let Some(tx) = transactions.next() {
let tx_hash = tx.hash;
let tx = tx_env_with_recovered(&tx); let tx = tx_env_with_recovered(&tx);
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx }; let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
let (result, state_changes) = let (result, state_changes) =
this.trace_transaction(opts.clone(), env, at, &mut db)?; this.trace_transaction(opts.clone(), env, at, &mut db).map_err(|err| {
results.push(TraceResult::Success { result }); 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() { if transactions.peek().is_some() {
// need to apply the state changes of this transaction before executing the // need to apply the state changes of this transaction before executing the
// next transaction // next transaction