From 3cc169fcfefc7060a4cece89530e48852c4b3223 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 10 Sep 2023 10:04:20 +0200 Subject: [PATCH] fix: set trace results root trace's gas used to execution results gas (#4506) --- .../src/tracing/builder/parity.rs | 11 +++++- crates/rpc/rpc-types/src/eth/trace/parity.rs | 39 +++++++++++++++++++ crates/rpc/rpc/src/trace.rs | 3 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/crates/revm/revm-inspectors/src/tracing/builder/parity.rs b/crates/revm/revm-inspectors/src/tracing/builder/parity.rs index 014aca08e9..3c5bafe36f 100644 --- a/crates/revm/revm-inspectors/src/tracing/builder/parity.rs +++ b/crates/revm/revm-inspectors/src/tracing/builder/parity.rs @@ -136,6 +136,7 @@ impl ParityTraceBuilder { res: ExecutionResult, trace_types: &HashSet, ) -> TraceResults { + let gas_used = res.gas_used(); let output = match res { ExecutionResult::Success { output, .. } => output.into_data(), ExecutionResult::Revert { output, .. } => output, @@ -144,12 +145,18 @@ impl ParityTraceBuilder { let (trace, vm_trace, state_diff) = self.into_trace_type_traces(trace_types); - TraceResults { + let mut trace = TraceResults { output: output.into(), trace: trace.unwrap_or_default(), vm_trace, state_diff, - } + }; + + // we're setting the gas used of the root trace explicitly to the gas used of the execution + // result + trace.set_root_trace_gas_used(gas_used); + + trace } /// Consumes the inspector and returns the trace results according to the configured trace diff --git a/crates/rpc/rpc-types/src/eth/trace/parity.rs b/crates/rpc/rpc-types/src/eth/trace/parity.rs index 64a4ba7c6c..da71213da8 100644 --- a/crates/rpc/rpc-types/src/eth/trace/parity.rs +++ b/crates/rpc/rpc-types/src/eth/trace/parity.rs @@ -39,6 +39,21 @@ pub struct TraceResults { pub vm_trace: Option, } +// === impl TraceResults === + +impl TraceResults { + /// Sets the gas used of the root trace. + /// + /// The root trace's gasUsed should mirror the actual gas used by the transaction. + /// + /// This allows setting int manually by consuming the execution result's gas for example. + pub fn set_root_trace_gas_used(&mut self, gas_used: u64) { + if let Some(r) = self.trace.first_mut().and_then(|t| t.result.as_mut()) { + r.set_gas_used(gas_used) + } + } +} + /// A `FullTrace` with an additional transaction hash #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -240,13 +255,37 @@ pub struct CreateOutput { pub address: Address, } +/// Represents the output of a trace. #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(untagged)] pub enum TraceOutput { + /// Output of a regular call transaction. Call(CallOutput), + /// Output of a CREATE transaction. Create(CreateOutput), } +// === impl TraceOutput === + +impl TraceOutput { + /// Returns the gas used by this trace. + pub fn gas_used(&self) -> U64 { + match self { + TraceOutput::Call(call) => call.gas_used, + TraceOutput::Create(create) => create.gas_used, + } + } + + /// Sets the gas used by this trace. + pub fn set_gas_used(&mut self, gas_used: u64) { + match self { + TraceOutput::Call(call) => call.gas_used = U64::from(gas_used), + TraceOutput::Create(create) => create.gas_used = U64::from(gas_used), + } + } +} + +/// A parity style trace of a transaction. #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct TransactionTrace { diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index bca7059b75..73c20187a7 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -268,7 +268,8 @@ where .await } - /// Executes all transactions of a block and returns a list of callback results. + /// Executes all transactions of a block and returns a list of callback results invoked for each + /// transaction in the block. /// /// This /// 1. fetches all transactions of the block