Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/inspectors/src/tracing/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,10 @@ impl JsInspector {
};
let refund = interp.host().tx_refunded(frame_refund);
debug_assert!(refund >= 0, "transaction-wide refund counter went negative: {refund}");
// An out-of-gas step consumes the whole remainder it started with.
let cost = if interp.result().is_err_and(InstrStop::is_out_of_gas) {
pending.instruction.gas
// Like geth, the hook that reports an exceptional halt sees cost 0, while a `step` before
// a `fault` sees the opcode's cost.
let cost = if error.is_some() && interp.result().is_err_and(InstrStop::is_halt) {
0
} else {
interp.gas().spent().saturating_sub(pending.gas_spent_before)
};
Expand Down
7 changes: 5 additions & 2 deletions crates/inspectors/src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,15 @@ impl CallTraceStep {
depth: u64,
stack: Option<Vec<U256>>,
) -> StructLog {
// A step that halts exceptionally costs 0, like geth and ethereum/execution-apis#986: the
// attempted cost depends on where each client detects the failure.
let halted = self.is_error();
StructLog {
depth,
error: self.as_error(),
gas: self.gas_remaining,
gas_cost: self.gas_cost,
state_gas_cost: self.state_gas_cost(),
gas_cost: if halted { 0 } else { self.gas_cost },
state_gas_cost: self.state_gas_cost().filter(|_| !halted),
state_gas_reservoir: self.state_gas_reservoir,
op: if self.op.is_valid() {
self.op.as_str().into()
Expand Down
45 changes: 0 additions & 45 deletions crates/inspectors/tests/it/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,51 +2415,6 @@ fn test_geth_opcode_limit_ignores_named_tracers() {
assert_eq!(inspector.config().step_limit, None);
}

#[test]
fn test_geth_struct_log_out_of_gas_cost() {
let contract = address!("0xc000000000000000000000000000000000000004");
let caller = address!("0xa000000000000000000000000000000000000004");
let code = [op::PUSH1, 0x01, op::PUSH1, 0x00, op::SSTORE];

let context = Context::mainnet()
.with_db(InMemoryDB::default())
.modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON)
.modify_db_chained(|db| {
db.insert_account_info(
&contract,
AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() },
);
});

let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth());
let mut evm = context.build_mainnet().with_inspector(&mut insp);

// Leaves 5000 gas for the cold zero-to-nonzero SSTORE, which would cost 22100. The step
// consumes the 5000 left.
let res = evm
.inspect_tx(TxEnv {
caller,
gas_limit: 21_000 + 6 + 5_000,
gas_price: 0,
kind: TransactTo::Call(contract),
data: Bytes::default(),
nonce: 0,
..Default::default()
})
.unwrap();
assert!(!res.result.is_success(), "{res:#?}");

let frame = insp.geth_builder().geth_traces(
res.result.tx_gas_used(),
res.result.output().unwrap_or_default().clone(),
GethDefaultTracingOptions::default(),
);
let sstore = frame.struct_logs.last().unwrap();
assert_eq!(sstore.opcode(), "SSTORE");
assert_eq!(sstore.gas, 5_000);
assert_eq!(sstore.gas_cost, 5_000);
}

/// EIP-7708: a transfer log of a reverted call is rolled back and does not consume an index.
#[test]
fn test_geth_calltracer_log_index_eip7708_revert() {
Expand Down
85 changes: 84 additions & 1 deletion crates/inspectors/tests/it/geth_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod values;

use crate::utils::{
AccountInfo, Bytecode, Context, ResultAndState, SpecId, TestDbExt, TransactTo, TxEnv,
deploy_contract,
deploy_contract, op,
};
use alloy_primitives::{Address, U256, address, bytes, hex, map::HashMap};
use alloy_rpc_types_trace::geth::{
Expand Down Expand Up @@ -627,3 +627,86 @@ fn mux_interrupts_its_js_tracers() {
interrupt.interrupt();
assert!(inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).is_err());
}

/// A step that halts exceptionally costs 0 in the struct logs and in the JS hook that reports the
/// halt: `step` before the opcode runs, `fault` while it runs. The values come from
/// `debug_traceTransaction` on go-ethereum `44577c84216d9e3ddcba55e57fcf611b31d2ae67`
/// (ethereum/go-ethereum#36166), which follows ethereum/execution-apis#986.
#[test]
fn test_geth_exceptional_halt_costs_zero() {
let target = address!("0000000000000000000000000000000001000000");
let child_address = address!("0000000000000000000000000000000002000000");
let tracer = r#"{
events: [],
step: function(log) {
if (log.getError()) this.events.push(["step", log.op.toString(), log.getGas(), log.getCost()]);
else this.cost = log.getCost();
},
fault: function(log) {
this.events.push(["fault", log.op.toString(), log.getGas(), log.getCost(), this.cost]);
},
result: function() { return this.events; }
}"#;
// Before ethereum/go-ethereum#36166, geth reported the constant gas of an opcode that fails its
// stack check, such as 32000 for CREATE. A `fault` follows a `step` that has the opcode's cost.
for (call, child_gas, child, opcode, gas, fault_step_cost) in [
(op::CALL, 100_000, &hex!("6001630fffffff52")[..], "MSTORE", 99_994, None),
(op::CALL, 0x400, &hex!("5f5f5f5f5f60445af1")[..], "CALL", 1_009, None),
(op::CALL, 50_000, &hex!("602a5f55")[..], "SSTORE", 49_995, None),
(op::CALL, 100_000, &hex!("01")[..], "ADD", 100_000, None),
(op::CALL, 100_000, &[op::PUSH0; 1025][..], "PUSH0", 97_952, None),
(op::CALL, 100_000, &hex!("f0")[..], "CREATE", 100_000, None),
(op::CALL, 100_000, &hex!("a3")[..], "LOG3", 100_000, None),
(op::CALL, 100_000, &hex!("3b")[..], "EXTCODESIZE", 100_000, None),
(op::CALL, 100_000, &hex!("600556")[..], "JUMP", 99_997, Some(8)),
// Geth's `step` has cost 9: it charges the memory and copy gas before the bounds check,
// which evm2 does first.
(op::CALL, 100_000, &hex!("60015f5f3e")[..], "RETURNDATACOPY", 99_993, Some(3)),
(op::STATICCALL, 100_000, &hex!("5f5fa0")[..], "LOG0", 99_996, Some(375)),
(op::STATICCALL, 100_000, &hex!("5fff")[..], "SELFDESTRUCT", 99_998, None),
] {
// call(child_gas, child, [0,] 0, 0, 0, 0), POP, STOP
let mut code = vec![op::PUSH0; 4 + usize::from(call == op::CALL)];
code.extend([op::PUSH4, 0x02, 0, 0, 0, op::PUSH3]);
code.extend(&u32::to_be_bytes(child_gas)[1..]);
code.extend([call, op::POP, op::STOP]);
for opts in [GethDebugTracingOptions::default(), GethDebugTracingOptions::js_tracer(tracer)]
{
let mut db = InMemoryDB::default();
for (address, code) in [(target, &code[..]), (child_address, child)] {
let code = Some(Bytecode::new_raw(code.to_vec().into()));
db.insert_account_info(&address, AccountInfo { code, ..Default::default() });
}
let mut inspector = DebugInspector::new(opts).unwrap();
// Amsterdam, for the state gas of SSTORE. The other values are the same in Osaka.
let mut evm = Context::mainnet()
.modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM)
.with_db(db)
.build_mainnet()
.with_inspector(&mut inspector);
let tx =
TxEnv { gas_limit: 0x30000, kind: TransactTo::Call(target), ..Default::default() };
let res = evm.inspect_tx(tx).unwrap();
assert!(res.result.is_success(), "{res:#?}");
let (ctx, inspector) = evm.ctx_inspector();
let (tx, block) = (ctx.tx().envelope(), *ctx.block());
match inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap() {
GethTrace::Default(frame) => {
let mut failed = frame.struct_logs.iter().filter(|log| log.error.is_some());
let log = failed.next().unwrap();
assert!(failed.next().is_none(), "{opcode}");
assert_eq!((&*log.op, log.gas, log.gas_cost), (opcode, gas, 0));
assert_eq!(log.state_gas_cost, None, "{opcode}");
}
GethTrace::JS(events) => {
let expected = match fault_step_cost {
Some(cost) => json!([["fault", opcode, gas, 0, cost]]),
None => json!([["step", opcode, gas, 0]]),
};
assert_eq!(events, expected, "{opcode}");
}
trace => panic!("unexpected trace: {trace:?}"),
}
}
}
}
5 changes: 3 additions & 2 deletions crates/inspectors/tests/it/parity/oog_cost.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! The cost of a step that runs out of gas is the gas it consumed, in both trace formats.
//! The cost of a step that runs out of gas is the gas it consumed in the Parity `vmTrace`. Geth
//! struct logs report 0 for it, like for any exceptional halt.

use super::*;
use alloy_primitives::Bytes;
Expand Down Expand Up @@ -47,7 +48,7 @@ fn out_of_gas_step_costs_the_consumed_gas() {
let failed = geth.struct_logs.iter().rfind(|log| log.depth == 2).unwrap();
assert_eq!(failed.opcode(), opcode);
assert_eq!(failed.gas, remaining, "{opcode}");
assert_eq!(failed.gas_cost, cost, "{opcode}");
assert_eq!(failed.gas_cost, 0, "{opcode}");

let trace = inspector.into_parity_builder().vm_trace();
let failed = trace.ops[7].sub.as_ref().unwrap().ops.last().unwrap();
Expand Down