From c8673e2016793de8a29bdb860693bb135fa0b97d Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Thu, 22 Oct 2026 15:45:54 +0000 Subject: [PATCH 1/6] Test stateGas fields propagation --- .changeset/debug-trace-gas-dimensions.md | 5 + .../edr_provider/tests/integration/eip8037.rs | 234 ++++++++++++++++-- 2 files changed, 221 insertions(+), 18 deletions(-) create mode 100644 .changeset/debug-trace-gas-dimensions.md diff --git a/.changeset/debug-trace-gas-dimensions.md b/.changeset/debug-trace-gas-dimensions.md new file mode 100644 index 0000000000..c1aef8b2fd --- /dev/null +++ b/.changeset/debug-trace-gas-dimensions.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/edr": patch +--- + +Added the EIP-8037 gas dimensions to `debug_traceTransaction` and `debug_traceCall` responses from the Amsterdam hardfork: the default tracer's top-level frame gains `executionGasUsed`, `stateGasUsed` and `gasRefund`, each of its `structLogs` entries gains `stateGasCost` and `stateGasReservoir`, and the `callTracer`'s root call frame gains `executionGasUsed`, `stateGasUsed` and `gasRefund`. diff --git a/crates/edr_provider/tests/integration/eip8037.rs b/crates/edr_provider/tests/integration/eip8037.rs index 4d60d41ebe..93bd676d87 100644 --- a/crates/edr_provider/tests/integration/eip8037.rs +++ b/crates/edr_provider/tests/integration/eip8037.rs @@ -15,6 +15,7 @@ use std::num::NonZeroU64; +use alloy_rpc_types_trace::geth::{CallConfig, GethDebugTracingOptions}; use edr_block_api::Block as _; use edr_block_header::HeaderOverrides; use edr_chain_l1::{ @@ -27,7 +28,7 @@ use edr_chain_spec_evm::result::ResultGas; use edr_eip7825::OSAKA_TRANSACTION_GAS_CAP; use edr_eip8037::TX_MAX_TOTAL_GAS_LIMIT; use edr_mem_pool::MemPoolAddTransactionError; -use edr_primitives::{address, Address, Bytecode, Bytes, U256}; +use edr_primitives::{address, Address, Bytecode, Bytes, B256, U256}; use edr_provider::{ config::{ConfigOption, ProviderConfig}, observability::EvmObservedData, @@ -35,7 +36,7 @@ use edr_provider::{ create_test_config, one_ether, set_genesis_state_with_owned_accounts, ProviderTestFixture, }, AccountOverride, MethodInvocation, MineBlockResultWithMetadataForChainSpec, Provider, - ProviderError, ProviderRequest, + ProviderError, ProviderRequest, ResponseWithCallTraces, }; use edr_receipt::ExecutionReceipt as _; use edr_test_utils::secret_key::secret_key_from_str; @@ -44,7 +45,10 @@ use tokio::runtime; use crate::common::{ bytecode::{opcode, BytecodeBuilder}, - provider::{estimate_gas, new_provider_with_config, send_transaction, transaction_receipt}, + provider::{ + estimate_gas, new_provider_with_config, quantity_field, send_transaction, + transaction_receipt, + }, }; /// Contract creating [`FRESH_SLOTS`] new storage slots: state gas dominates. @@ -755,26 +759,29 @@ async fn estimate_gas_accepts_gas_above_cap_from_amsterdam() -> anyhow::Result<( Ok(()) } +/// Provider on `hardfork` with the slot-creating contract at genesis. +fn new_slot_creator_provider( + hardfork: edr_chain_l1::Hardfork, +) -> anyhow::Result> { + new_provider_with_config(|config| { + config.hardfork = hardfork; + config.genesis_state.insert( + SLOT_CREATOR, + AccountOverride { + code: Some(Bytecode::new_raw(slot_creator_code())), + ..AccountOverride::default() + }, + ); + }) +} + /// This EIP makes state creation more expensive, so the same slot-creating /// transaction costs more on Amsterdam than on Osaka, and `eth_estimateGas` /// must account for it: its estimate is higher and funds the transaction. #[tokio::test(flavor = "multi_thread")] async fn estimate_gas_covers_state_gas() -> anyhow::Result<()> { - let new_provider = |hardfork| { - new_provider_with_config(|config| { - config.hardfork = hardfork; - config.genesis_state.insert( - SLOT_CREATOR, - AccountOverride { - code: Some(Bytecode::new_raw(slot_creator_code())), - ..AccountOverride::default() - }, - ); - }) - }; - - let amsterdam = new_provider(edr_chain_l1::Hardfork::Amsterdam)?; - let osaka = new_provider(edr_chain_l1::Hardfork::Osaka)?; + let amsterdam = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + let osaka = new_slot_creator_provider(edr_chain_l1::Hardfork::Osaka)?; let send_with_gas = |gas: u64| -> anyhow::Result { let transaction_hash = send_transaction( @@ -822,3 +829,194 @@ async fn estimate_gas_covers_state_gas() -> anyhow::Result<()> { Ok(()) } + +/// State gas of one fresh slot: 64 state bytes at 1530 gas each. +const SLOT_CREATION_STATE_GAS: u64 = 97_920; + +/// Mines a slot-creating transaction and returns its hash. +fn send_slot_creation(provider: &Provider) -> anyhow::Result { + send_transaction( + provider, + TransactionRequest { + from: caller(provider), + to: Some(SLOT_CREATOR), + ..TransactionRequest::default() + }, + ) +} + +fn slot_creation_call(provider: &Provider) -> L1CallRequest { + L1CallRequest { + from: Some(caller(provider)), + to: Some(SLOT_CREATOR), + ..L1CallRequest::default() + } +} + +/// Serialized `debug_trace*` result, as a client would see it. +fn debug_trace( + provider: &Provider, + method: MethodInvocation, +) -> anyhow::Result { + let response: ResponseWithCallTraces = + provider.handle_request(ProviderRequest::with_single(method))?; + Ok(response.deserialize_result()?) +} + +/// Asserts the top-level EIP-8037 fields of a default or `callTracer` frame +/// for a transaction creating [`FRESH_SLOTS`] slots. +fn assert_gas_dimensions(frame: &serde_json::Value, gas_used_key: &str) -> anyhow::Result<()> { + let execution_gas_used = quantity_field(frame, "executionGasUsed")?; + let state_gas_used = quantity_field(frame, "stateGasUsed")?; + + assert_eq!( + state_gas_used, + u64::from(FRESH_SLOTS) * SLOT_CREATION_STATE_GAS, + "state gas should be the slot creations alone: {frame}" + ); + assert_eq!( + execution_gas_used + state_gas_used, + quantity_field(frame, gas_used_key)?, + "the dimensions should add up to the transaction's gas: {frame}" + ); + assert_eq!( + quantity_field(frame, "gasRefund")?, + 0, + "creating slots refunds nothing: {frame}" + ); + + Ok(()) +} + +/// From Amsterdam, `debug_traceTransaction` reports execution and state gas +/// separately: per transaction on the frame and per opcode in the struct logs. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyhow::Result<()> { + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + let transaction_hash = send_slot_creation(&provider)?; + + let trace = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction(transaction_hash, None), + )?; + + assert_gas_dimensions(&trace, "gas")?; + assert_eq!( + quantity_field(&trace, "gas")?, + transaction_receipt(&provider, transaction_hash)?.gas_used, + "the frame's gas should match the receipt" + ); + + let struct_logs = trace["structLogs"] + .as_array() + .expect("structLogs is an array"); + let sstores: Vec<_> = struct_logs + .iter() + .filter(|log| log["op"] == "SSTORE") + .collect(); + assert_eq!(sstores.len(), usize::from(FRESH_SLOTS)); + for log in &sstores { + assert_eq!( + log["stateGasCost"], + serde_json::json!(SLOT_CREATION_STATE_GAS), + "a fresh slot should cost its state gas: {log}" + ); + assert!( + log["stateGasReservoir"].is_u64(), + "every step reports the reservoir: {log}" + ); + } + // A step that creates no state reports the reservoir but no cost. + let push = struct_logs.first().expect("the trace has steps"); + assert_eq!(push["op"], "PUSH1", "the contract starts with a PUSH1"); + assert!(push["stateGasReservoir"].is_u64()); + assert!(push.get("stateGasCost").is_none(), "{push}"); + + Ok(()) +} + +/// From Amsterdam, the `callTracer` reports both dimensions on the root frame. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_transaction_call_tracer_reports_gas_dimensions_from_amsterdam( +) -> anyhow::Result<()> { + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + let transaction_hash = send_slot_creation(&provider)?; + + let frame = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction( + transaction_hash, + Some(GethDebugTracingOptions::call_tracer(CallConfig::default())), + ), + )?; + + assert_gas_dimensions(&frame, "gasUsed")?; + + Ok(()) +} + +/// From Amsterdam, `debug_traceCall` reports both dimensions as well. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_call_reports_gas_dimensions_from_amsterdam() -> anyhow::Result<()> { + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + + let trace = debug_trace( + &provider, + MethodInvocation::DebugTraceCall(slot_creation_call(&provider), None, None), + )?; + + assert_gas_dimensions(&trace, "gas")?; + + Ok(()) +} + +/// Before Amsterdam, gas has one dimension and the responses are unchanged: +/// none of the EIP-8037 fields are present. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_omits_gas_dimensions_before_amsterdam() -> anyhow::Result<()> { + const FRAME_FIELDS: [&str; 3] = ["executionGasUsed", "stateGasUsed", "gasRefund"]; + const STEP_FIELDS: [&str; 2] = ["stateGasCost", "stateGasReservoir"]; + + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Osaka)?; + let transaction_hash = send_slot_creation(&provider)?; + + let default_trace = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction(transaction_hash, None), + )?; + let call_frame = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction( + transaction_hash, + Some(GethDebugTracingOptions::call_tracer(CallConfig::default())), + ), + )?; + let call_trace = debug_trace( + &provider, + MethodInvocation::DebugTraceCall(slot_creation_call(&provider), None, None), + )?; + + for frame in [&default_trace, &call_frame, &call_trace] { + for field in FRAME_FIELDS { + assert!( + frame.get(field).is_none(), + "unexpected `{field}` in {frame}" + ); + } + } + // The step that creates state is the one that would carry the fields. + let sstore = default_trace["structLogs"] + .as_array() + .expect("structLogs is an array") + .iter() + .find(|log| log["op"] == "SSTORE") + .expect("the transaction stores"); + for field in STEP_FIELDS { + assert!( + sstore.get(field).is_none(), + "unexpected `{field}` in {sstore}" + ); + } + + Ok(()) +} From b85ed4db0d64935842bbcde9e95544e29aaadc4e Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 23 Oct 2026 14:33:28 +0000 Subject: [PATCH 2/6] Apply PR feedback --- .../edr_provider/tests/integration/eip8037.rs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/edr_provider/tests/integration/eip8037.rs b/crates/edr_provider/tests/integration/eip8037.rs index 93bd676d87..c3d6dee578 100644 --- a/crates/edr_provider/tests/integration/eip8037.rs +++ b/crates/edr_provider/tests/integration/eip8037.rs @@ -41,6 +41,7 @@ use edr_provider::{ use edr_receipt::ExecutionReceipt as _; use edr_test_utils::secret_key::secret_key_from_str; use edr_transaction::{request::TransactionRequestAndSender, TxKind}; +use revm_primitives::eip8037::{CPSB_GLAMSTERDAM, SSTORE_SET_BYTES}; use tokio::runtime; use crate::common::{ @@ -830,8 +831,8 @@ async fn estimate_gas_covers_state_gas() -> anyhow::Result<()> { Ok(()) } -/// State gas of one fresh slot: 64 state bytes at 1530 gas each. -const SLOT_CREATION_STATE_GAS: u64 = 97_920; +/// State gas of one fresh slot. +const SLOT_CREATION_STATE_GAS: u64 = SSTORE_SET_BYTES * CPSB_GLAMSTERDAM; /// Mines a slot-creating transaction and returns its hash. fn send_slot_creation(provider: &Provider) -> anyhow::Result { @@ -866,24 +867,22 @@ fn debug_trace( /// Asserts the top-level EIP-8037 fields of a default or `callTracer` frame /// for a transaction creating [`FRESH_SLOTS`] slots. fn assert_gas_dimensions(frame: &serde_json::Value, gas_used_key: &str) -> anyhow::Result<()> { + let gas_used = quantity_field(frame, gas_used_key)?; let execution_gas_used = quantity_field(frame, "executionGasUsed")?; let state_gas_used = quantity_field(frame, "stateGasUsed")?; + let gas_refund = quantity_field(frame, "gasRefund")?; + let expected_state_gas_used = u64::from(FRESH_SLOTS) * SLOT_CREATION_STATE_GAS; assert_eq!( - state_gas_used, - u64::from(FRESH_SLOTS) * SLOT_CREATION_STATE_GAS, + state_gas_used, expected_state_gas_used, "state gas should be the slot creations alone: {frame}" ); assert_eq!( execution_gas_used + state_gas_used, - quantity_field(frame, gas_used_key)?, - "the dimensions should add up to the transaction's gas: {frame}" - ); - assert_eq!( - quantity_field(frame, "gasRefund")?, - 0, - "creating slots refunds nothing: {frame}" + gas_used + gas_refund, + "the dimensions should add up to the gas before refunds: {frame}" ); + assert_eq!(gas_refund, 0, "creating slots refunds nothing: {frame}"); Ok(()) } From f5e82b19812b745859131aa8e9fd843bfec6c4bd Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 23 Oct 2026 14:44:31 +0000 Subject: [PATCH 3/6] split gas_dimensions helper from assert_slot_creation_gas_dimensions --- .../edr_provider/tests/integration/eip8037.rs | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/crates/edr_provider/tests/integration/eip8037.rs b/crates/edr_provider/tests/integration/eip8037.rs index c3d6dee578..a4f9207464 100644 --- a/crates/edr_provider/tests/integration/eip8037.rs +++ b/crates/edr_provider/tests/integration/eip8037.rs @@ -864,25 +864,51 @@ fn debug_trace( Ok(response.deserialize_result()?) } -/// Asserts the top-level EIP-8037 fields of a default or `callTracer` frame -/// for a transaction creating [`FRESH_SLOTS`] slots. -fn assert_gas_dimensions(frame: &serde_json::Value, gas_used_key: &str) -> anyhow::Result<()> { - let gas_used = quantity_field(frame, gas_used_key)?; - let execution_gas_used = quantity_field(frame, "executionGasUsed")?; - let state_gas_used = quantity_field(frame, "stateGasUsed")?; - let gas_refund = quantity_field(frame, "gasRefund")?; +/// The EIP-8037 gas dimensions a default or `callTracer` frame reports. +struct GasDimensions { + gas_used: u64, + execution_gas_used: u64, + state_gas_used: u64, + gas_refund: u64, +} + +/// Reads the EIP-8037 gas dimensions of a frame, asserting that they add up to +/// its gas before refunds. +fn gas_dimensions(frame: &serde_json::Value, gas_used_key: &str) -> anyhow::Result { + let dimensions = GasDimensions { + gas_used: quantity_field(frame, gas_used_key)?, + execution_gas_used: quantity_field(frame, "executionGasUsed")?, + state_gas_used: quantity_field(frame, "stateGasUsed")?, + gas_refund: quantity_field(frame, "gasRefund")?, + }; + + assert_eq!( + dimensions.execution_gas_used + dimensions.state_gas_used, + dimensions.gas_used + dimensions.gas_refund, + "the dimensions should add up to the gas before refunds: {frame}" + ); + + Ok(dimensions) +} + +/// Asserts the gas dimensions of a frame executing a transaction that creates +/// [`FRESH_SLOTS`] slots: all of its state gas is the creations, nothing is +/// refunded. +fn assert_slot_creation_gas_dimensions( + frame: &serde_json::Value, + gas_used_key: &str, +) -> anyhow::Result<()> { + let dimensions = gas_dimensions(frame, gas_used_key)?; let expected_state_gas_used = u64::from(FRESH_SLOTS) * SLOT_CREATION_STATE_GAS; assert_eq!( - state_gas_used, expected_state_gas_used, + dimensions.state_gas_used, expected_state_gas_used, "state gas should be the slot creations alone: {frame}" ); assert_eq!( - execution_gas_used + state_gas_used, - gas_used + gas_refund, - "the dimensions should add up to the gas before refunds: {frame}" + dimensions.gas_refund, 0, + "creating slots refunds nothing: {frame}" ); - assert_eq!(gas_refund, 0, "creating slots refunds nothing: {frame}"); Ok(()) } @@ -899,7 +925,7 @@ async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyh MethodInvocation::DebugTraceTransaction(transaction_hash, None), )?; - assert_gas_dimensions(&trace, "gas")?; + assert_slot_creation_gas_dimensions(&trace, "gas")?; assert_eq!( quantity_field(&trace, "gas")?, transaction_receipt(&provider, transaction_hash)?.gas_used, @@ -949,7 +975,7 @@ async fn debug_trace_transaction_call_tracer_reports_gas_dimensions_from_amsterd ), )?; - assert_gas_dimensions(&frame, "gasUsed")?; + assert_slot_creation_gas_dimensions(&frame, "gasUsed")?; Ok(()) } @@ -964,7 +990,7 @@ async fn debug_trace_call_reports_gas_dimensions_from_amsterdam() -> anyhow::Res MethodInvocation::DebugTraceCall(slot_creation_call(&provider), None, None), )?; - assert_gas_dimensions(&trace, "gas")?; + assert_slot_creation_gas_dimensions(&trace, "gas")?; Ok(()) } From 75084f25bd6e8ea8a8e0c3162abceba7f920f952 Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 23 Oct 2026 15:41:19 +0000 Subject: [PATCH 4/6] Fix debug_trace_transaction_reports_gas_dimensions_from_amsterdam Assert against expected gas_state and reservoir for SSTORE opcodes --- .../edr_provider/tests/integration/eip8037.rs | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/crates/edr_provider/tests/integration/eip8037.rs b/crates/edr_provider/tests/integration/eip8037.rs index a4f9207464..a82de75e99 100644 --- a/crates/edr_provider/tests/integration/eip8037.rs +++ b/crates/edr_provider/tests/integration/eip8037.rs @@ -760,12 +760,16 @@ async fn estimate_gas_accepts_gas_above_cap_from_amsterdam() -> anyhow::Result<( Ok(()) } -/// Provider on `hardfork` with the slot-creating contract at genesis. +/// Provider on `hardfork` with the slot-creating contract at genesis and the +/// protocol's gas cap. Requests without `gas` get their block's bound, so from +/// Amsterdam they carry a state gas reservoir. fn new_slot_creator_provider( hardfork: edr_chain_l1::Hardfork, ) -> anyhow::Result> { new_provider_with_config(|config| { config.hardfork = hardfork; + config.transaction_gas_cap = ConfigOption::Default; + config.default_transaction_gas_limit = None; config.genesis_state.insert( SLOT_CREATOR, AccountOverride { @@ -935,27 +939,43 @@ async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyh let struct_logs = trace["structLogs"] .as_array() .expect("structLogs is an array"); - let sstores: Vec<_> = struct_logs - .iter() - .filter(|log| log["op"] == "SSTORE") - .collect(); - assert_eq!(sstores.len(), usize::from(FRESH_SLOTS)); - for log in &sstores { - assert_eq!( - log["stateGasCost"], - serde_json::json!(SLOT_CREATION_STATE_GAS), - "a fresh slot should cost its state gas: {log}" - ); - assert!( - log["stateGasReservoir"].is_u64(), - "every step reports the reservoir: {log}" - ); + let reservoir = |log: &serde_json::Value| -> u64 { + log["stateGasReservoir"] + .as_u64() + .unwrap_or_else(|| panic!("every step reports the reservoir: {log}")) + }; + let (sstores, other_opcodes): (Vec<_>, Vec<_>) = + struct_logs.iter().partition(|log| log["op"] == "SSTORE"); + + // The SSTOREs are the only state-creating opcodes in `slot_creator_code`, + // and only state-creating steps report a cost. + for log in other_opcodes { + assert!(log.get("stateGasCost").is_none(), "{log}"); } - // A step that creates no state reports the reservoir but no cost. - let push = struct_logs.first().expect("the trace has steps"); - assert_eq!(push["op"], "PUSH1", "the contract starts with a PUSH1"); - assert!(push["stateGasReservoir"].is_u64()); - assert!(push.get("stateGasCost").is_none(), "{push}"); + + // A step reports the reservoir before executing. + let initial_reservoir = reservoir(sstores[0]); + assert!( + initial_reservoir > 0, + "the protocol gas cap funds a reservoir" + ); + // Each slot creation drains the reservoir its predecessor saw by one + // `SLOT_CREATION_STATE_GAS`. + let expected_reservoirs: Vec = (0..u64::from(FRESH_SLOTS)) + .map(|created| initial_reservoir - created * SLOT_CREATION_STATE_GAS) + .collect(); + let reservoirs: Vec = sstores.iter().map(|log| reservoir(log)).collect(); + assert_eq!(reservoirs, expected_reservoirs); + + // Every slot is fresh, so every SSTORE costs the same state gas. + let expected_costs = vec![SLOT_CREATION_STATE_GAS; sstores.len()]; + let state_gas_cost = |log: &serde_json::Value| -> u64 { + log["stateGasCost"] + .as_u64() + .unwrap_or_else(|| panic!("an SSTORE reports its state gas cost: {log}")) + }; + let costs: Vec = sstores.iter().map(|log| state_gas_cost(log)).collect(); + assert_eq!(costs, expected_costs); Ok(()) } From 228b7d327ea609f13d7893952a4f340ba23909a7 Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 23 Oct 2026 19:20:35 +0000 Subject: [PATCH 5/6] Add tests for uncovered scenarios --- .../edr_provider/tests/integration/eip8037.rs | 282 +++++++++++++++--- 1 file changed, 248 insertions(+), 34 deletions(-) diff --git a/crates/edr_provider/tests/integration/eip8037.rs b/crates/edr_provider/tests/integration/eip8037.rs index a82de75e99..57ce6c75b6 100644 --- a/crates/edr_provider/tests/integration/eip8037.rs +++ b/crates/edr_provider/tests/integration/eip8037.rs @@ -760,26 +760,37 @@ async fn estimate_gas_accepts_gas_above_cap_from_amsterdam() -> anyhow::Result<( Ok(()) } -/// Provider on `hardfork` with the slot-creating contract at genesis and the -/// protocol's gas cap. Requests without `gas` get their block's bound, so from -/// Amsterdam they carry a state gas reservoir. -fn new_slot_creator_provider( +/// Provider on `hardfork` with `contracts` at genesis and the protocol's gas +/// cap. Requests without `gas` get their block's bound, so from Amsterdam they +/// carry a state gas reservoir. +fn new_provider_with_contracts( hardfork: edr_chain_l1::Hardfork, + contracts: impl IntoIterator, ) -> anyhow::Result> { - new_provider_with_config(|config| { + let contracts: Vec<_> = contracts.into_iter().collect(); + new_provider_with_config(move |config| { config.hardfork = hardfork; config.transaction_gas_cap = ConfigOption::Default; config.default_transaction_gas_limit = None; - config.genesis_state.insert( - SLOT_CREATOR, - AccountOverride { - code: Some(Bytecode::new_raw(slot_creator_code())), - ..AccountOverride::default() - }, - ); + for (address, code) in contracts { + config.genesis_state.insert( + address, + AccountOverride { + code: Some(Bytecode::new_raw(code)), + ..AccountOverride::default() + }, + ); + } }) } +/// Provider on `hardfork` with the slot-creating contract at genesis. +fn new_slot_creator_provider( + hardfork: edr_chain_l1::Hardfork, +) -> anyhow::Result> { + new_provider_with_contracts(hardfork, [(SLOT_CREATOR, slot_creator_code())]) +} + /// This EIP makes state creation more expensive, so the same slot-creating /// transaction costs more on Amsterdam than on Osaka, and `eth_estimateGas` /// must account for it: its estimate is higher and funds the transaction. @@ -838,13 +849,26 @@ async fn estimate_gas_covers_state_gas() -> anyhow::Result<()> { /// State gas of one fresh slot. const SLOT_CREATION_STATE_GAS: u64 = SSTORE_SET_BYTES * CPSB_GLAMSTERDAM; -/// Mines a slot-creating transaction and returns its hash. -fn send_slot_creation(provider: &Provider) -> anyhow::Result { +/// The EIP-8037 fields of a default or `callTracer` frame. +const FRAME_FIELDS: [&str; 3] = ["executionGasUsed", "stateGasUsed", "gasRefund"]; + +/// Asserts that `frame` reports none of the EIP-8037 fields. +fn assert_no_gas_dimensions(frame: &serde_json::Value) { + for field in FRAME_FIELDS { + assert!( + frame.get(field).is_none(), + "unexpected `{field}` in {frame}" + ); + } +} + +/// Mines a transaction calling `to` without data and returns its hash. +fn send_to(provider: &Provider, to: Address) -> anyhow::Result { send_transaction( provider, TransactionRequest { from: caller(provider), - to: Some(SLOT_CREATOR), + to: Some(to), ..TransactionRequest::default() }, ) @@ -868,6 +892,14 @@ fn debug_trace( Ok(response.deserialize_result()?) } +/// The `stateGasCost` a struct log reports: positive for a state charge, +/// negative for a refill. Panics if the step reports none. +fn state_gas_cost(log: &serde_json::Value) -> i64 { + log["stateGasCost"] + .as_i64() + .unwrap_or_else(|| panic!("the step reports no state gas cost: {log}")) +} + /// The EIP-8037 gas dimensions a default or `callTracer` frame reports. struct GasDimensions { gas_used: u64, @@ -922,7 +954,7 @@ fn assert_slot_creation_gas_dimensions( #[tokio::test(flavor = "multi_thread")] async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyhow::Result<()> { let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; - let transaction_hash = send_slot_creation(&provider)?; + let transaction_hash = send_to(&provider, SLOT_CREATOR)?; let trace = debug_trace( &provider, @@ -964,17 +996,12 @@ async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyh let expected_reservoirs: Vec = (0..u64::from(FRESH_SLOTS)) .map(|created| initial_reservoir - created * SLOT_CREATION_STATE_GAS) .collect(); - let reservoirs: Vec = sstores.iter().map(|log| reservoir(log)).collect(); + let reservoirs: Vec = sstores.iter().copied().map(reservoir).collect(); assert_eq!(reservoirs, expected_reservoirs); // Every slot is fresh, so every SSTORE costs the same state gas. - let expected_costs = vec![SLOT_CREATION_STATE_GAS; sstores.len()]; - let state_gas_cost = |log: &serde_json::Value| -> u64 { - log["stateGasCost"] - .as_u64() - .unwrap_or_else(|| panic!("an SSTORE reports its state gas cost: {log}")) - }; - let costs: Vec = sstores.iter().map(|log| state_gas_cost(log)).collect(); + let expected_costs = vec![i64::try_from(SLOT_CREATION_STATE_GAS)?; sstores.len()]; + let costs: Vec = sstores.iter().copied().map(state_gas_cost).collect(); assert_eq!(costs, expected_costs); Ok(()) @@ -985,7 +1012,7 @@ async fn debug_trace_transaction_reports_gas_dimensions_from_amsterdam() -> anyh async fn debug_trace_transaction_call_tracer_reports_gas_dimensions_from_amsterdam( ) -> anyhow::Result<()> { let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; - let transaction_hash = send_slot_creation(&provider)?; + let transaction_hash = send_to(&provider, SLOT_CREATOR)?; let frame = debug_trace( &provider, @@ -1019,11 +1046,10 @@ async fn debug_trace_call_reports_gas_dimensions_from_amsterdam() -> anyhow::Res /// none of the EIP-8037 fields are present. #[tokio::test(flavor = "multi_thread")] async fn debug_trace_omits_gas_dimensions_before_amsterdam() -> anyhow::Result<()> { - const FRAME_FIELDS: [&str; 3] = ["executionGasUsed", "stateGasUsed", "gasRefund"]; const STEP_FIELDS: [&str; 2] = ["stateGasCost", "stateGasReservoir"]; let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Osaka)?; - let transaction_hash = send_slot_creation(&provider)?; + let transaction_hash = send_to(&provider, SLOT_CREATOR)?; let default_trace = debug_trace( &provider, @@ -1040,14 +1066,22 @@ async fn debug_trace_omits_gas_dimensions_before_amsterdam() -> anyhow::Result<( &provider, MethodInvocation::DebugTraceCall(slot_creation_call(&provider), None, None), )?; + let call_trace_call_frame = debug_trace( + &provider, + MethodInvocation::DebugTraceCall( + slot_creation_call(&provider), + None, + Some(GethDebugTracingOptions::call_tracer(CallConfig::default())), + ), + )?; - for frame in [&default_trace, &call_frame, &call_trace] { - for field in FRAME_FIELDS { - assert!( - frame.get(field).is_none(), - "unexpected `{field}` in {frame}" - ); - } + for frame in [ + &default_trace, + &call_frame, + &call_trace, + &call_trace_call_frame, + ] { + assert_no_gas_dimensions(frame); } // The step that creates state is the one that would carry the fields. let sstore = default_trace["structLogs"] @@ -1065,3 +1099,183 @@ async fn debug_trace_omits_gas_dimensions_before_amsterdam() -> anyhow::Result<( Ok(()) } + +/// From Amsterdam, the `stateGasTracer` reports the transaction's gas in both +/// dimensions. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_transaction_state_gas_tracer_reports_gas_dimensions_from_amsterdam( +) -> anyhow::Result<()> { + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + let transaction_hash = send_to(&provider, SLOT_CREATOR)?; + + let trace = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction( + transaction_hash, + Some(GethDebugTracingOptions::state_gas_tracer()), + ), + )?; + + assert_slot_creation_gas_dimensions(&trace, "gasUsed")?; + + Ok(()) +} + +/// Before Amsterdam, the `stateGasTracer` still answers, with no state gas: +/// all the gas before refunds is execution gas. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_transaction_state_gas_tracer_reports_no_state_gas_before_amsterdam( +) -> anyhow::Result<()> { + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Osaka)?; + let transaction_hash = send_to(&provider, SLOT_CREATOR)?; + + let trace = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction( + transaction_hash, + Some(GethDebugTracingOptions::state_gas_tracer()), + ), + )?; + + let dimensions = gas_dimensions(&trace, "gasUsed")?; + assert_eq!( + dimensions.state_gas_used, 0, + "no state gas before Amsterdam: {trace}" + ); + assert_eq!( + dimensions.execution_gas_used, + dimensions.gas_used + dimensions.gas_refund, + "all the gas before refunds is execution gas before Amsterdam: {trace}" + ); + + Ok(()) +} + +/// Contract writing one fresh slot and clearing it again in the same +/// transaction. +const SLOT_RESTORER: Address = address!("0x0000000000000000000000000000000000000f0f"); + +/// `PUSH1 1; PUSH1 1; SSTORE; PUSH1 0; PUSH1 1; SSTORE`. +fn slot_restorer_code() -> Bytes { + let mut code = BytecodeBuilder::default(); + code.push1(1).push1(1).opcode(opcode::SSTORE); + code.push1(0).push1(1).opcode(opcode::SSTORE); + code.opcode(opcode::STOP); + code.runtime() +} + +/// Clearing a slot created in the same transaction refills its state gas: the +/// step reports a negative `stateGasCost`, the transaction's `stateGasUsed` +/// nets to zero and the clear earns a refund. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_transaction_reports_state_gas_refill_from_amsterdam() -> anyhow::Result<()> { + let provider = new_provider_with_contracts( + edr_chain_l1::Hardfork::Amsterdam, + [(SLOT_RESTORER, slot_restorer_code())], + )?; + let transaction_hash = send_to(&provider, SLOT_RESTORER)?; + + let trace = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction(transaction_hash, None), + )?; + + let slot_creation_state_gas = i64::try_from(SLOT_CREATION_STATE_GAS)?; + let state_gas_costs: Vec = trace["structLogs"] + .as_array() + .expect("structLogs is an array") + .iter() + .filter(|log| log["op"] == "SSTORE") + .map(state_gas_cost) + .collect(); + + assert_eq!( + state_gas_costs, + [slot_creation_state_gas, -slot_creation_state_gas], + "creating the slot costs its state gas and clearing it refills it" + ); + + let dimensions = gas_dimensions(&trace, "gas")?; + assert_eq!( + dimensions.state_gas_used, 0, + "the refill nets the transaction's state gas to zero: {trace}" + ); + assert!( + dimensions.gas_refund > 0, + "clearing the slot earns a refund: {trace}" + ); + + Ok(()) +} + +/// From Amsterdam, `debug_traceCall` with the `callTracer` reports both +/// dimensions on the root frame, like `debug_traceTransaction` does. +#[tokio::test(flavor = "multi_thread")] +async fn debug_trace_call_call_tracer_reports_gas_dimensions_from_amsterdam() -> anyhow::Result<()> +{ + let provider = new_slot_creator_provider(edr_chain_l1::Hardfork::Amsterdam)?; + + let frame = debug_trace( + &provider, + MethodInvocation::DebugTraceCall( + slot_creation_call(&provider), + None, + Some(GethDebugTracingOptions::call_tracer(CallConfig::default())), + ), + )?; + + assert_slot_creation_gas_dimensions(&frame, "gasUsed")?; + + Ok(()) +} + +/// Contract forwarding all its gas to [`SLOT_CREATOR`] in a nested `CALL`. +const SLOT_CREATOR_CALLER: Address = address!("0x000000000000000000000000000000000000ca11"); + +/// `CALL` into [`SLOT_CREATOR`] with all remaining gas, no value and no data. +fn slot_creator_caller_code() -> Bytes { + let mut code = BytecodeBuilder::default(); + // retSize, retOffset, argsSize, argsOffset, value + code.push1(0).push1(0).push1(0).push1(0).push1(0); + code.opcode(opcode::PUSH20); + for byte in SLOT_CREATOR { + code.opcode(byte); + } + code.opcode(opcode::GAS) + .opcode(opcode::CALL) + .opcode(opcode::POP); + code.opcode(opcode::STOP); + code.runtime() +} + +/// The `callTracer` reports the dimensions on the root frame only. +#[tokio::test(flavor = "multi_thread")] +async fn call_tracer_reports_gas_dimensions_on_root_frame_only() -> anyhow::Result<()> { + let provider = new_provider_with_contracts( + edr_chain_l1::Hardfork::Amsterdam, + [ + (SLOT_CREATOR, slot_creator_code()), + (SLOT_CREATOR_CALLER, slot_creator_caller_code()), + ], + )?; + let transaction_hash = send_to(&provider, SLOT_CREATOR_CALLER)?; + + let root = debug_trace( + &provider, + MethodInvocation::DebugTraceTransaction( + transaction_hash, + Some(GethDebugTracingOptions::call_tracer(CallConfig::default())), + ), + )?; + + assert_slot_creation_gas_dimensions(&root, "gasUsed")?; + + let child = root["calls"] + .as_array() + .and_then(|calls| calls.first()) + .expect("the caller makes a call"); + + assert_no_gas_dimensions(child); + + Ok(()) +} From 8ed1967e476308d3c3f497e1644dbf142ea2e744 Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 23 Oct 2026 19:36:18 +0000 Subject: [PATCH 6/6] Improve changeset --- .changeset/debug-trace-gas-dimensions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/debug-trace-gas-dimensions.md b/.changeset/debug-trace-gas-dimensions.md index c1aef8b2fd..dfb612c97d 100644 --- a/.changeset/debug-trace-gas-dimensions.md +++ b/.changeset/debug-trace-gas-dimensions.md @@ -2,4 +2,5 @@ "@nomicfoundation/edr": patch --- -Added the EIP-8037 gas dimensions to `debug_traceTransaction` and `debug_traceCall` responses from the Amsterdam hardfork: the default tracer's top-level frame gains `executionGasUsed`, `stateGasUsed` and `gasRefund`, each of its `structLogs` entries gains `stateGasCost` and `stateGasReservoir`, and the `callTracer`'s root call frame gains `executionGasUsed`, `stateGasUsed` and `gasRefund`. +- Added the EIP-8037 gas dimensions to `debug_traceTransaction` and `debug_traceCall` responses from the Amsterdam hardfork: the default tracer's top-level frame and the `callTracer`'s root call frame gain `executionGasUsed`, `stateGasUsed` and `gasRefund`, and each `structLogs` entry gains `stateGasReservoir`, plus a signed `stateGasCost` on the steps that charge or refill state gas. +- Added the `stateGasTracer`, which reports a transaction's gas in both dimensions on every hardfork, with zero state gas before Amsterdam.