From 300e7c9813e98c528385d723f7372d9f9d1d295a Mon Sep 17 00:00:00 2001 From: RealiCZ Date: Thu, 29 Oct 2026 12:33:40 -0400 Subject: [PATCH 001/159] fix(evm): count reservoir-funded state gas in system call totals (#842) ## Summary Count the state gas a system call pays from its EIP-8037 reservoir in `total_gas_spent`. The reservoir is funded on top of `gas_limit`, but `total_gas_spent` was `gas_limit - remaining`, so it could be smaller than `state_gas_spent` and `execution_gas_spent()` saturated to zero. Fixes #841. ## Changes - `Evm::execute_system_call`: add the consumed reservoir (`reservoir - result.gas.reservoir()`) to `total_gas_spent`. - Test `system_call_total_includes_reservoir_funded_state_gas`: a system call that writes one new slot on Amsterdam reports `state_gas_spent` = `SstoreSetState`, `total_gas_spent` = regular + state gas, and `execution_gas_spent()` = regular gas. Without the fix it reports `total_gas_spent` 12,105 instead of 110,025. ## Validation - `cargo test -p evm2 --lib`: 717 passed. - `cargo fmt --all --check` and `cargo clippy -p evm2 --all-targets --all-features`: clean. - Workspace-wide `cargo cl` / `cargo docs` need LLVM for the JIT crate and were not run locally. --- crates/evm2/src/evm/system.rs | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index 68a7c49bb..1711a121e 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -167,7 +167,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { )?; let result = execute_initial_frame(self, &tx_env, frame, &mut tx_gas, gas_limit, reservoir)?; - let gas_spent = gas_limit.saturating_sub(result.gas.remaining()); + // The reservoir is funded on top of `gas_limit`, so the state gas it paid for is added + // back; otherwise `total_gas_spent` would not cover `state_gas_spent`. + let gas_spent = gas_limit + .saturating_sub(result.gas.remaining()) + .saturating_add(reservoir.saturating_sub(result.gas.reservoir())); let gas_refunded = if result.stop.is_success() && result.gas.refunded() > 0 { result.gas.refunded() as u64 } else { @@ -520,4 +524,36 @@ mod tests { assert_eq!(result.result.total_gas_spent, 3_000); assert!(!result.pending_state.is_changed()); } + + #[test] + fn system_call_total_includes_reservoir_funded_state_gas() { + let contract = Address::from([0x42; 20]); + let code = Bytecode::new_legacy(Bytes::from_static(&[ + op::PUSH1, + 1, + op::PUSH0, + op::SSTORE, + op::STOP, + ])); + let mut database = InMemoryDB::default(); + database.insert_account_info(&contract, AccountInfo::default().with_code(code)); + let mut evm = TestEvm::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + TxRegistry::new(), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + + let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().discard(); + + // One new slot: its state gas comes from the system call's reservoir, the regular gas + // (PUSH1, PUSH0, cold SSTORE) from `gas_limit`. + let state_gas = evm.version().gas_params.get(GasId::SstoreSetState); + let regular_gas = 3 + 2 + 2_100 + 10_000; + assert!(result.status); + assert_eq!(result.state_gas_spent, state_gas); + assert_eq!(result.total_gas_spent, regular_gas + state_gas); + assert_eq!(result.execution_gas_spent(), regular_gas); + } } From 10937705e4ee7c98f6dd15469208b7f93063fd9b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 29 Oct 2026 17:41:10 +0100 Subject: [PATCH 002/159] fix(inspectors): include forwarded gas in call opcode costs (#744) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The step cost the tracing inspector records for a call or create step was the gas consumed over the whole opcode, measured after the child returned its unused gas. evm2 runs the child inside the opcode, so a CALL that forwards 65535 gas and gets all of it back reported 2600, where geth logs 68135, and struct logs and Parity `vmTrace` both read that value. This PR copies geth's model: the interpreter hands the inspector the cost the step charged, and the inspector records it. Geth captures the gas at the top of each step, charges the constant gas, meters the dynamic gas, and passes the resulting cost to the tracer before it runs the opcode. The CALL family's dynamic gas function adds the forwarded gas but not the stipend, so a call's cost includes the gas it forwards. CREATE and CREATE2 take their 63/64 share while the opcode executes, so their cost does not include it. In evm2, `Inspector::step` is geth's capture point, the interpreter's static and dynamic gas charges are the same charges, and the child call runs between `step` and `step_end`. The interpreter now keeps the gas at the start of an inspected step, and a call or create marks the point where it finished charging: right after it spends the forwarded gas in a call, before the stipend is added, and in a create after it spends the creation cost and the EIP-8037 account creation state gas, which spills into regular gas when the reservoir is short, before the child gas split. `Interpreter::step_cost()` returns the gas at the start of the step minus the gas at that mark, or minus the gas at the end of the step for any other opcode. `TracingInspector` stores it as `gas_cost`, and struct logs and `vmTrace` report the same value. The Parity `ex.used` of a call or create step reports the gas after the child returns. A call cost no longer shows that gas, so the inspector records the gas balance after a call or a gas credit whenever steps are enabled, and only the memory and storage deltas depend on `record_step_deltas`. Out-of-gas steps still report the gas delta as on main, and `step_cost()` can take failing charges in a follow-up. The costs below change on a Prague fixture. The main column is the gas consumed over the opcode, the reth column is the previous reth tracer on a parent with 1M gas, and the geth column is what this PR reports and what geth 1.17.10 `evm run --trace` logs for the same code. | Step | main | previous reth tracer | geth and this PR | | --- | --- | --- | --- | | CALL, CALLCODE, DELEGATECALL or STATICCALL, child returns | 2600 | 68135 | 68135 | | CALL, child spends 4 gas | 2604 | 68135 | 68135 | | cold CALL forwarding 1024 gas | 2600 | not measured | 3624 | | CALL to precompile 0x04 forwarding 65535 | 115 | 65635 | 65635 | | value CALL that cannot be funded, target has code | 9300 | 12600 | 12600 | | value CALL that cannot be funded, empty target | 34300 | 37600 | 37600 | | CREATE, empty initcode | 32000 | 984870 | 32000 | | CREATE2, empty initcode | 32000 | 984868 | 32000 | | CREATE, initcode that spends 8 gas | 32010 | 984857 | 32002 | | CREATE that cannot be funded | 32000 | 984869 | 32000 | CREATE and CREATE2 costs that include memory expansion, initcode words or keccak words (32023 and 32008 in the tests) are unchanged from main. Only the gas a CREATE's initcode spends leaves its cost. The interpreter stores the mark unconditionally when a call or create finishes charging, with no inspector check. Counting instructions with callgrind over the `usdc_proxy` and `universal_router` benchmark suites, `prepare_call` costs 41k instructions more than on main in both, and the whole run changes by +0.0014% and +0.013%. Two identical runs of one binary differ by about 70k instructions. Found while comparing paradigmxyz/reth#25002 against main. # PR stack Review in order: 1. #744 — forwarded CALL/CREATE trace gas — this PR 2. #746 — underfunded creation nonce 3. #747 — empty-code inspection 4. #748 — normal REVERT struct logs 5. #750 — uncharged gas in opcode totals --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- .../src/interpreter/instructions/system.rs | 2 + crates/evm2/src/interpreter/runtime.rs | 32 +++ crates/inspectors/src/tracing/config.rs | 8 +- crates/inspectors/src/tracing/mod.rs | 44 ++--- crates/inspectors/src/tracing/types.rs | 7 +- crates/inspectors/tests/it/parity.rs | 186 ++++++++++++++++-- 6 files changed, 236 insertions(+), 43 deletions(-) diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 4abf1dabe..08323fde5 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -140,6 +140,7 @@ fn load_acc_and_calc_gas( stack_gas_limit }; gas.spend(gas_limit)?; + state.finish_step_charge(gas.remaining()); if transfers_value { gas_limit = gas_limit.saturating_add(state.gas_params().get(GasId::CallStipend)); @@ -367,6 +368,7 @@ fn create_inner( charged_create_state_gas = true; } } + state.finish_step_charge(gas.remaining()); let gas_limit = if state.feature(EvmFeatures::EIP150) { state.gas_params().call_stipend_reduction(gas.remaining()) } else { diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 258138f66..c1b2bf95c 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -42,6 +42,10 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) stack: Box, pub(in crate::interpreter) gas: Gas, + /// Remaining gas at the start of the current inspected step. + step_gas_before: u64, + /// Remaining gas once the current step finished charging, if it marked that point. + step_gas_after_charge: Option, pub(in crate::interpreter) result: Result, error: Option, spec: SpecId, @@ -83,6 +87,8 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { bytecode_ref: None, stack_len: 0, gas: Gas::new(0), + step_gas_before: 0, + step_gas_after_charge: None, memory: Memory::new(), result: Ok(()), error: None, @@ -237,6 +243,23 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.gas = gas; } + /// Returns the gas the current step charged for its execution, before its effects. + /// + /// This is the gas remaining at the start of the step minus the gas remaining once the step + /// finished charging. Call and create steps finish charging right before they hand gas to + /// the child, so their cost includes the forwarded gas for calls and excludes it for creates. + /// Any other step finishes charging when it ends, which makes its cost the gas it consumed. + /// + /// Only meaningful in [`Inspector::step_end`](crate::inspector::Inspector::step_end). + #[inline] + pub const fn step_cost(&self) -> u64 { + let remaining = match self.step_gas_after_charge { + Some(remaining) => remaining, + None => self.gas.remaining(), + }; + self.step_gas_before.saturating_sub(remaining) + } + /// Returns the current linear memory. #[inline] pub const fn memory(&self) -> &Memory { @@ -700,6 +723,8 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { pub(crate) fn inspect_step(&mut self, pc: Pc, stack_len: usize) { self.0.pc = pc.as_ptr(); self.0.stack_len = stack_len; + self.0.step_gas_before = self.0.gas.remaining(); + self.0.step_gas_after_charge = None; unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step(&mut self.0); @@ -721,6 +746,13 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { } } + /// Marks that the current step finished charging gas, so [`Interpreter::step_cost`] stops + /// counting gas after this point. + #[inline] + pub(crate) const fn finish_step_charge(&mut self, remaining: u64) { + self.0.step_gas_after_charge = Some(remaining); + } + #[inline] pub(crate) fn inspect_selfdestruct( &mut self, diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 89b262597..57a87060f 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -87,11 +87,11 @@ pub struct TracingInspectorConfig { pub record_logs: bool, /// Whether to record immediate bytes for opcodes. pub record_immediate_bytes: bool, - /// Whether to record the deltas of each step: the memory it writes or an `MLOAD` reads, the - /// storage an `SSTORE` writes, and the remaining gas after returning from a call or gaining - /// gas. + /// Whether to record the memory each step writes or an `MLOAD` reads, and the storage an + /// `SSTORE` writes. /// - /// Required for parity `vmTrace`. + /// Required for parity `vmTrace` memory and storage. The remaining gas after returning from a + /// call or gaining gas is recorded whenever steps are enabled, independently of this option. pub record_step_deltas: bool, } diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index f5f545159..0312203b5 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -651,13 +651,12 @@ impl TracingInspector { }; } - // The gas cost is the difference between the recorded gas remaining at the start of the - // step and the remaining gas here, at the end of the step. An out-of-gas step consumes - // the whole remainder, except the reentrancy sentry, which halts before any charge. + // An out-of-gas step consumes the whole remainder, except the reentrancy sentry, which + // halts before any charge. step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { 0 } else { - step.gas_remaining.saturating_sub(interp.gas().remaining()) + interp.step_cost() }; let state_gas_delta = interp.gas().state_gas_spent().saturating_sub(step.state_gas_spent); if step.state_gas_reservoir.is_some() && state_gas_delta != 0 { @@ -667,24 +666,25 @@ impl TracingInspector { // set the status step.status = interp.result().err(); - if self.config.record_step_deltas { - if step.status.is_some_and(|status| status.is_halt()) { - return; - } - // Call results, returned memory and gas are already available in evm2's `step_end`. - // Gas credits cannot be recovered from the saturated unsigned gas cost. - let gas_remaining_after = (step.is_call_like_op() - || interp.gas().remaining() > step.gas_remaining) - .then_some(interp.gas().remaining()); - if gas_remaining_after.is_some() - && node.trace.step_deltas.last().is_none_or(|delta| delta.step != step_idx) - { - node.trace.step_deltas.push(StepDelta { step: step_idx, ..Default::default() }); - } - if let Some(delta) = - node.trace.step_deltas.last_mut().filter(|delta| delta.step == step_idx) - { - delta.gas_remaining_after = gas_remaining_after; + if step.status.is_some_and(|status| status.is_halt()) { + return; + } + // The gas a child returns and gas credits cannot be recovered from the step cost, so + // record the balance after the step even without memory and storage deltas. + let gas_remaining_after = (step.is_call_like_op() + || interp.gas().remaining() > step.gas_remaining) + .then_some(interp.gas().remaining()); + if gas_remaining_after.is_some() + && node.trace.step_deltas.last().is_none_or(|delta| delta.step != step_idx) + { + node.trace.step_deltas.push(StepDelta { step: step_idx, ..Default::default() }); + } + if let Some(delta) = + node.trace.step_deltas.last_mut().filter(|delta| delta.step == step_idx) + { + delta.gas_remaining_after = gas_remaining_after; + // Call results and returned memory are already available in evm2's `step_end`. + if self.config.record_step_deltas { if step.is_call_like_op() && let Some(range) = &mut delta.write_range { diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 9ae3645f7..09c7edcb9 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -116,8 +116,8 @@ pub struct CallTrace { pub steps: Vec, /// The deltas recorded for [`Self::steps`], in step order. /// - /// Only steps that report memory or storage, make a call or gain gas have an entry, and only - /// if [`record_step_deltas`] is enabled. + /// Only steps that report memory or storage, make a call or gain gas have an entry. Memory + /// and storage require [`record_step_deltas`]; gas balances are recorded independently. /// /// [`record_step_deltas`]: crate::tracing::TracingInspectorConfig::record_step_deltas #[cfg_attr(feature = "serde", serde(default))] @@ -763,7 +763,8 @@ impl CallTraceStep { /// The deltas a [`CallTraceStep`] produced, as reported by parity's `vmTrace`. /// /// Recorded in [`CallTrace::step_deltas`] for the steps that report memory or storage, make a -/// call or gain gas, when [`record_step_deltas`] is enabled. +/// call or gain gas. Memory and storage require [`record_step_deltas`]; gas balances are recorded +/// independently. /// /// [`record_step_deltas`]: crate::tracing::TracingInspectorConfig::record_step_deltas #[derive(Clone, Debug, Default, PartialEq, Eq)] diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index ff440f8cf..d86a7b564 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -524,21 +524,23 @@ fn vmtrace_reports_remaining_gas_after_execution() { #[test] fn vmtrace_reports_remaining_gas_after_storage_credit() { // Restore a newly written slot to zero, crediting gas spilled from the state gas reservoir. - let inspector = inspect_code( - &hex!("60015f555f5f5500"), - &[], - SpecId::AMSTERDAM, - TracingInspectorConfig::parity_vm_trace(), - ); - let steps = &inspector.traces().nodes()[0].trace.steps; - let gas_after = steps[6].gas_remaining; - assert!(gas_after > steps[5].gas_remaining); - let trace = inspector.into_parity_builder().vm_trace(); - assert_eq!(trace.ops[5].ex.as_ref().unwrap().used, gas_after); + for deltas in [false, true] { + let inspector = inspect_code( + &hex!("60015f555f5f5500"), + &[], + SpecId::AMSTERDAM, + TracingInspectorConfig::parity_vm_trace().set_step_deltas(deltas), + ); + let steps = &inspector.traces().nodes()[0].trace.steps; + let gas_after = steps[6].gas_remaining; + assert!(gas_after > steps[5].gas_remaining); + let trace = inspector.into_parity_builder().vm_trace(); + assert_eq!(trace.ops[5].ex.as_ref().unwrap().used, gas_after); + } } #[test] -fn non_vm_traces_do_not_record_step_deltas() { +fn non_vm_traces_only_record_gas_deltas() { // Exercise a memory write, a call and a storage gas credit. let code = hex!("602a5f525f5f5f5f5f604361fffff15060015f555f5f5500"); for config in [ @@ -547,7 +549,20 @@ fn non_vm_traces_do_not_record_step_deltas() { TracingInspectorConfig::from_geth_call_config(&Default::default()), ] { let inspector = inspect_code(&code, &hex!("00"), SpecId::AMSTERDAM, config); - assert!(inspector.traces().nodes().iter().all(|node| node.trace.step_deltas.is_empty())); + for node in inspector.traces().nodes() { + for delta in &node.trace.step_deltas { + assert!(delta.gas_remaining_after.is_some()); + assert!(delta.memory.is_none()); + assert!(delta.store.is_none()); + } + } + if config.record_steps { + assert_eq!(inspector.traces().nodes()[0].trace.step_deltas.len(), 2); + } else { + assert!( + inspector.traces().nodes().iter().all(|node| node.trace.step_deltas.is_empty()) + ); + } } } @@ -792,7 +807,9 @@ fn vmtrace_terminal_call_finalizes_delta_at_capture_limit() { let mem = ex.mem.as_ref().unwrap(); assert_eq!(mem.off, 32); assert_eq!(mem.data.as_ref(), &[42]); - assert_eq!(ex.used, trace.ops[6].ex.as_ref().unwrap().used - call.cost); + // The cost includes the forwarded gas. The child spends 16 gas and returns the rest. + assert_eq!(call.cost, 2_600 + 6 + 65_535); + assert_eq!(ex.used, trace.ops[6].ex.as_ref().unwrap().used - call.cost + 65_535 - 16); } } @@ -1134,6 +1151,147 @@ fn precompile_exclusion_uses_original_callee_with_delegation() { } } +/// The cost and used gas of the call or create step before the final `STOP`, as reported by +/// parity `vmTrace` and geth struct logs under each config. +struct CallStep { + /// Gas remaining before the step. + gas_remaining: u64, + /// Gas limit of the child, if one started. + child_gas_limit: Option, + /// Parity cost, which equals the geth cost. + cost: u64, + /// Parity used gas, with and without step deltas. + used: u64, +} + +fn call_step(code: &[u8], child: &[u8], spec: SpecId) -> CallStep { + let vm_trace = |config| { + let inspector = inspect_code(code, child, spec, config); + let nodes = inspector.traces().nodes(); + let step = &nodes[0].trace.steps[nodes[0].trace.steps.len() - 2]; + let step = (step.gas_remaining, nodes.get(1).map(|node| node.trace.gas_limit)); + let trace = inspector.into_parity_builder().vm_trace(); + let call = &trace.ops[trace.ops.len() - 2]; + (step, call.cost, call.ex.as_ref().unwrap().used) + }; + let (step, cost, used) = vm_trace(TracingInspectorConfig::parity_vm_trace()); + let (_, cost_without_deltas, used_without_deltas) = + vm_trace(TracingInspectorConfig::parity_vm_trace().set_step_deltas(false)); + assert_eq!(used_without_deltas, used); + let geth = inspect_code(code, child, spec, TracingInspectorConfig::default_geth()) + .geth_builder() + .geth_traces(0, Default::default(), Default::default()); + let log = geth.struct_logs.iter().rev().find(|log| log.gas == step.0).unwrap(); + assert_eq!(cost_without_deltas, cost); + assert_eq!(log.gas_cost, cost); + CallStep { gas_remaining: step.0, child_gas_limit: step.1, cost, used } +} + +#[test] +fn call_cost_includes_forwarded_gas() { + for (prefix, opcode) in + [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] + { + for (child, spent) in [(&hex!("00")[..], 0), (&hex!("5f5ffd")[..], 4), (&[][..], 0)] { + let code = hex::decode(format!("{prefix}604361ffff{opcode}00")).unwrap(); + let step = call_step(&code, child, SpecId::PRAGUE); + assert_eq!(step.cost, 2_600 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - spent); + } + } +} + +#[test] +fn call_cost_excludes_stipend() { + // A cold CALL forwarding 1024 gas. + let step = call_step(&hex!("5f5f5f5f5f6043610400f100"), &hex!("00"), SpecId::PRAGUE); + assert_eq!(step.cost, 2_600 + 1_024); + assert_eq!(step.used, step.gas_remaining - step.cost + 1_024); +} + +#[test] +fn precompile_call_cost_includes_forwarded_gas() { + let step = call_step(&hex!("5f5f5f5f5f600461fffff100"), &[], SpecId::PRAGUE); + assert_eq!(step.cost, 100 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - 15); +} + +#[test] +fn unfunded_call_cost_includes_forwarded_gas() { + // The child fails with out of funds and returns its gas, including the stipend. The cost + // has the base, cold, value and, for an empty target, new account charges plus the + // forwarded 1000 gas, without the stipend. + for (child, cost) in [(&hex!("00")[..], 2_600 + 9_000 + 1_000), (&[][..], 37_600)] { + let step = call_step(&hex!("5f5f5f5f600160436103e8f100"), child, SpecId::PRAGUE); + assert_eq!(step.child_gas_limit, Some(1_000 + 2_300)); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - cost + 3_300); + } +} + +#[test] +fn create_cost_excludes_forwarded_gas() { + for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { + let step = call_step(code, &[], SpecId::PRAGUE); + let available = step.gas_remaining - 32_000; + assert_eq!(step.cost, 32_000); + assert_eq!(step.child_gas_limit, Some(available - available / 64)); + assert_eq!(step.used, available); + } +} + +#[test] +fn create_cost_counts_all_gas_charged_before_the_child_split() { + // Amsterdam charges the account creation state gas, which spills into regular gas when the + // reservoir is empty. + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { + let step = call_step(code, &[], spec); + let remaining = step.gas_remaining - step.cost; + assert_eq!(step.child_gas_limit, Some(remaining - remaining / 64)); + } + } +} + +#[test] +fn create_cost_counts_initcode_memory_and_hash_words() { + // The initcode `5f5f5f5f00` spends 8 gas. + for (code, cost) in [ + (&hex!("645f5f5f5f005f526005601b5ff000")[..], 32_002), + (&hex!("645f5f5f5f005f526005601b5f5ff500")[..], 32_008), + (&hex!("6001600052600160ff5ff000")[..], 32_023), + ] { + let step = call_step(code, &[], SpecId::PRAGUE); + assert_eq!(step.cost, cost); + } + let step = call_step(&hex!("645f5f5f5f005f526005601b5ff000"), &[], SpecId::PRAGUE); + assert_eq!(step.used, step.gas_remaining - step.cost - 8); +} + +#[test] +fn unfunded_create_cost_excludes_forwarded_gas() { + let code = hex!("5f5f6001f000"); + assert!(trace_vm_code(&code, &[]).ops[3].sub.is_none()); + let step = call_step(&code, &[], SpecId::PRAGUE); + assert_eq!(step.cost, 32_000); + assert_eq!(step.used, step.gas_remaining - 32_000); +} + +#[test] +fn nested_calls_keep_each_forwarded_cost() { + let trace = trace_vm_code(&hex!("5f5f5f5f5f604361fffff100"), &hex!("5f5f5f5f5f60046103e8f100")); + let outer = &trace.ops[7]; + let child = outer.sub.as_ref().unwrap(); + let inner = &child.ops[7]; + assert_eq!(outer.cost, 2_600 + 65_535); + assert_eq!(inner.cost, 100 + 1_000); + assert_eq!( + outer.ex.as_ref().unwrap().used, + trace.ops[6].ex.as_ref().unwrap().used - 2_600 - 16 - 100 - 15 + ); + assert_eq!(inner.ex.as_ref().unwrap().used, child.ops[6].ex.as_ref().unwrap().used - 100 - 15); +} + #[path = "parity/vm_bytecode.rs"] mod vm_bytecode; From 34209d5853ba92063ffbf1dbfed54dd472b90ca3 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:42:31 +0100 Subject: [PATCH 003/159] perf(jit): disable debug objects and perf map in the runtime (#848) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The JIT runtime built its compilers with the default `BackendConfig`, which enables `debug_support` and `simple_perf`. Both apply once per process to the global LLJIT. `debug_support` keeps a full copy of every linked object in memory for debugger registration until the code is freed, and `simple_perf` appends every JIT symbol to `/tmp/perf-.map` forever, which its own docs call unsuitable for long-running programs. The worker compilers in `create_compiler` and the backend-thread `JitObjectLinker` now turn both off. `BackendConfig::default()` is unchanged, so the CLI and the benches keep the perf map for profiling. Generated code does not change. Measured by linking 1000 distinct small programs through `JitObjectLinker` and reading `VmRSS` before and after (three runs each): | | RSS per resident program | | --- | --- | | before | 22.5 KiB 🔴 | | after | 17.9 KiB 🟢 | The programs are tiny (about 217 bytes of JIT memory each), so larger contracts save more; the debug copy scales with object size. --- crates/jit/runtime/src/runtime/backend.rs | 13 ++++++++++++- crates/jit/runtime/src/runtime/worker.rs | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index ecb410c69..005bd20b9 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -33,6 +33,8 @@ use std::{ #[cfg(feature = "llvm")] use crate::llvm::jit_memory_usage; #[cfg(feature = "llvm")] +use evm2_jit_backend::{Backend, BackendConfig}; +#[cfg(feature = "llvm")] use evm2_jit_context::RawEvmCompilerFn; /// The resident map type: code_hash+spec_id → compiled program. @@ -85,7 +87,16 @@ impl JitObjectLinker { ) -> eyre::Result<(EvmCompilerFn, Arc)> { let backend = match &mut self.backend { Some(backend) => backend, - None => self.backend.insert(crate::EvmLlvmBackend::new(false)?), + None => { + let mut backend = crate::EvmLlvmBackend::new(false)?; + // Long-running nodes cannot afford debug object copies or the append-only perf map. + backend.apply_config(BackendConfig { + debug_support: false, + simple_perf: false, + ..backend.config().clone() + }); + self.backend.insert(backend) + } }; let symbol_name = CString::new(success.symbol_name.clone())?; diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index 4a7d1793d..fecc7e157 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -638,6 +638,9 @@ pub(super) fn create_compiler( } else { config.tuning.jit_opt_level }); + // Long-running nodes cannot afford the debug object copies or the append-only perf map. + compiler.set_debug_support(false); + compiler.set_simple_perf(false); compiler.set_debug_assertions(config.debug_assertions); compiler.set_single_error(config.single_error); compiler.set_dedup(!config.no_dedup); From 24ac747199efb395f55f65efe06c469305b638a2 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:46:34 +0100 Subject: [PATCH 004/159] fix(state): check BAL coverage for zero-valued created-account slot reads (#844) A slot of an account created in the transaction reads zero without a database read, since the creation discards the address's old storage. That shortcut also skipped the strict BAL coverage check that `CacheDB::get_storage` performs, so a BAL that covered the account but omitted the slot was accepted for `SLOAD` and `storage_slot_untracked`, although rebuilding the BAL records the slot. Both reads now run the same coverage check before returning zero; the zero value itself is unchanged. The regression test attaches a BAL that covers a created account with one slot and expects a `BalError` for an uncovered slot on both read paths, while the covered slot still reads zero. It fails without the fix. Found by the Cyclops audit of #562 (rakita), which flagged this path in main's existing created-account read. --- crates/evm2/src/evm/db/cache.rs | 13 +++++++++ crates/evm2/src/evm/state/mod.rs | 6 +++-- crates/evm2/src/evm/state/storage.rs | 40 +++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 4300e99e6..38d297130 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -251,6 +251,19 @@ impl CacheDB { pub fn insert_block_hash(&mut self, number: &Word, hash: &B256) { self.cache.block_hashes.insert(*number, *hash); } + + /// Checks that the attached read BAL, if any, covers storage slot `key`. + /// + /// Used by reads that know the slot's value without consulting storage, such as a slot of an + /// account created in the transaction, so they keep the coverage check of + /// [`DynDatabase::get_storage`]. + #[inline] + pub(crate) fn check_bal_storage(&self, address: &Address, key: &Word) -> DbResult<()> { + match self.bal_context.bal_storage(address, key) { + Ok(_) => Ok(()), + Err(err) => Err(DatabaseError::new(err, false)), + } + } } /// Applies streamed changes to the cache and forwards every callback to [`BalContext`]'s own sink diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index b1eaff64d..bb68a0676 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -665,8 +665,9 @@ impl<'a> State<'a> { /// This is the storage-side mirror of [`Self::account_info_untracked`]: a non-loading peek that /// returns the overlay slot value when one has been loaded or written this transaction, /// otherwise it reads the backing database directly without caching the result in the overlay. - /// A slot of a wiped or created account that has not been rewritten reads as zero. Use - /// [`Self::storage`] when the slot should be loaded and preserved. + /// A slot of a wiped or created account that has not been rewritten reads as zero, though an + /// attached strict BAL must still cover it. Use [`Self::storage`] when the slot should be + /// loaded and preserved. #[inline(never)] pub fn storage_slot_untracked(&mut self, address: &Address, key: &Word) -> DbResult { if let Some(account) = self.accounts.get(address) { @@ -674,6 +675,7 @@ impl<'a> State<'a> { return Ok(slot.value.current); } if account.storage.wiped || account.discards_storage() { + self.database.check_bal_storage(address, key)?; return Ok(Word::ZERO); } } diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index e43242f2b..81d5a154b 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -242,6 +242,8 @@ impl<'a, 'db> StorageHandle<'a, 'db> { // Like revm, an unloaded slot of a created account reads zero without a database // read, since the creation discards the address's old storage. let value = if storage.wiped || created { + // The zero needs no database read, but a strict BAL must still cover the slot. + inner.database.check_bal_storage(address, &key)?; Word::ZERO } else { inner.database.get_storage(address, &key)? @@ -397,10 +399,12 @@ mod tests { SpecId, Version, evm::{ CacheDB, + bal::{AccountBal, Bal, BalChanges, BalError, BlockAccessIndex}, state::{AccountInfo, State}, }, }; - use alloc::vec; + use alloc::{sync::Arc, vec}; + use alloy_eip7928::StorageChange; use alloy_primitives::Address; #[test] @@ -611,4 +615,38 @@ mod tests { assert!(slot.is_warm()); assert!(!slot.warm(), "base-prewarmed slot must not report a cold transition"); } + + #[test] + fn created_account_slot_reads_keep_strict_bal_coverage() { + let creator = Address::with_last_byte(0x50); + let address = Address::with_last_byte(0x51); + let (covered, uncovered) = (Word::from(7), Word::from(9)); + let mut account = AccountBal::default(); + account.storage.storage.insert( + covered, + BalChanges::new(vec![StorageChange::new(BlockAccessIndex::new(1), Word::from(42))]), + ); + let mut database = CacheDB::default(); + database.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + database.insert_account_storage(&address, &uncovered, &Word::from(20)); + let mut state = State::new(database); + state.set_bal(Arc::new(Bal::from_iter([(address, account)]))); + state.set_bal_index(BlockAccessIndex::new(2)); + let version = Version::base(SpecId::LONDON); + state.create_account(&creator, &address, &Word::ZERO, version.features).unwrap().unwrap(); + + // The created account reads zero, but a slot missing from the BAL is still invalid. + let error = state.storage_slot(&address, uncovered).unwrap_err(); + assert!(error.downcast_ref::().is_some()); + let error = state.storage_slot_untracked(&address, &uncovered).unwrap_err(); + assert!(error.downcast_ref::().is_some()); + assert!(!state.accounts[&address].storage.slots.contains_key(&uncovered)); + + assert_eq!(state.storage_slot(&address, covered).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot_untracked(&address, &covered).unwrap(), Word::ZERO); + + // With database fallback enabled, an uncovered slot is allowed and still reads zero. + state.set_allow_bal_db_fallback(true); + assert_eq!(state.storage_slot(&address, uncovered).unwrap().current(), Word::ZERO); + } } From f52033757dfb7cdbaca5b65cd9fcd4f49e879d19 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:48:03 +0100 Subject: [PATCH 005/159] fix(state): reload slots read as zero under a rolled-back creation (#845) A slot first loaded while its account is created in the transaction caches zero without a database read. Rolling the creation back restored the account flags but left that zero in the overlay, so later reads in the transaction returned zero instead of the backing value, and the slot reported zero as its original. This is reachable when a creation targets an existing account with nonce zero, empty code and nonzero storage and the constructor reads such a slot before reverting. When a rollback ends an account's storage discard, `State::rollback` now rereads the overlay slots that still hold zero from the database, after every journal entry of the rolled-back scope is reverted. Slots keep their entries, so their reads and warmth stay visible to the BAL; a slot whose read fails is evicted so its next load reports the error. This departs from the earlier behavior of keeping the cached zero. A slot already loaded before a retried creation keeps its value, as slots loaded before a creation already did. `created_account_reads_old_storage_as_zero` asserted the stale zero after the rollback, so it now expects the backing value, and the new `rollback_of_creation_reloads_slots_read_as_zero` and `rollback_reloads_slots_written_through_the_creating_handle` fail without the fix. Found by the Cyclops audit of #562 (rakita), which flagged this in main's existing created-account read path. --- crates/evm2/src/evm/state/mod.rs | 117 +++++++++++++++++++++++++-- crates/evm2/src/evm/state/storage.rs | 20 ++++- 2 files changed, 131 insertions(+), 6 deletions(-) diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index bb68a0676..a47a2ad37 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -832,6 +832,9 @@ impl<'a> State<'a> { // Restoring an older snapshot can leave active cursors past the current lengths. Preserve // shorter prefixes and revert only entries beyond each cursor. self.logs.truncate(checkpoint.logs_len); + // Accounts whose creation was undone. Their slot reloads wait until every slot write of + // the rolled-back scope is reverted, which can happen after the account entry is. + let mut uncreated = Vec::new(); while self.journal.len() > checkpoint.journal_len { let Some(entry) = self.journal.pop() else { unreachable!("journal length is checked above") @@ -850,6 +853,7 @@ impl<'a> State<'a> { self.selfdestructs.insert(address); } if let Some(entry) = self.accounts.get_mut(&address) { + let discarded_storage = entry.discards_storage(); entry.present = previous; // EIP-161 preserves the historical Yellow Paper K.1 precompile-3 touch. let keep_touch = features.contains(EvmFeatures::EIP161) @@ -862,6 +866,9 @@ impl<'a> State<'a> { kept |= AccountFlags::TOUCHED; } entry.flags = (previous_flags - kept) | (entry.flags & kept); + if discarded_storage && !entry.discards_storage() { + uncreated.push(address); + } } } JournalEntry::StorageChange { address, key, previous } => { @@ -892,6 +899,13 @@ impl<'a> State<'a> { } } } + for address in uncreated { + if let Some(entry) = self.accounts.get_mut(&address) + && !entry.discards_storage() + { + entry.storage.reload_zeroed(&address, &mut self.inner.database); + } + } } fn materialize_empty_account_for_finalization(&mut self, address: &Address) -> DbResult<()> { @@ -1150,12 +1164,13 @@ mod tests { fn created_account_reads_old_storage_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (loaded, unloaded) = (Word::from(1), Word::from(2)); + let (loaded, unloaded, fresh) = (Word::from(1), Word::from(2), Word::from(3)); let features = EvmFeatures::empty(); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); db.insert_account_storage(&address, &loaded, &Word::from(5)); db.insert_account_storage(&address, &unloaded, &Word::from(6)); + db.insert_account_storage(&address, &fresh, &Word::from(7)); let mut state = State::new(db); assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); @@ -1170,13 +1185,14 @@ mod tests { state.storage_slot(&address, unloaded).unwrap().write(Word::from(9)); state.rollback(checkpoint, features); - // Like revm, rolling back the creation keeps the zero it loaded, so a retried creation - // still reads zero. + // Rolling back the creation reloads the slot it read as zero from the backing database. assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); - assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::from(6)); let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); - assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot(&address, fresh).unwrap().current(), Word::ZERO); + // Like `loaded`, the slot was already loaded before this creation, so it keeps its value. + assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::from(6)); state.rollback(checkpoint, features); assert!(!state.take_pending_state().is_changed()); } @@ -1311,6 +1327,97 @@ mod tests { assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); } + #[test] + fn rollback_of_creation_reloads_slots_read_as_zero() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let (read, written, empty) = (Word::from(1), Word::from(2), Word::from(3)); + let version = Version::base(crate::SpecId::CANCUN); + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &read, &Word::from(6)); + db.insert_account_storage(&address, &written, &Word::from(7)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::ZERO); + state.storage_slot(&address, written).unwrap().write(Word::from(9)); + assert_eq!(state.storage_slot(&address, empty).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, version.features); + + assert!(!state.accounts[&address].discards_storage()); + for (key, value) in [(read, 6), (written, 7), (empty, 0)] { + let value = Word::from(value); + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), value); + let slot = state.storage_slot(&address, key).unwrap(); + assert_eq!((slot.original(), slot.current()), (value, value)); + } + assert!(!state.take_pending_state().is_changed()); + } + + #[test] + fn rollback_reloads_zeroed_slots_only_when_the_creation_is_undone() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let key = Word::from(1); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let outer = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + let inner = state.checkpoint(); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.rollback(inner, features); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.rollback(outer, features); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); + } + + #[test] + fn rollback_of_creation_keeping_storage_leaves_slots_alone() { + let address = Address::with_last_byte(2); + let (read, zero) = (Word::from(1), Word::from(2)); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &read, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.account(&address).unwrap().mark_created_keeping_storage(); + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); + } + + #[test] + fn rollback_reloads_slots_written_through_the_creating_handle() { + let address = Address::with_last_byte(2); + let key = Word::from(1); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + let mut account = state.account(&address).unwrap(); + account.mark_created(); + // The slot write is journaled before the handle flushes its creation entry on drop. + account.storage().into_slot(key).unwrap().write(Word::from(9)); + drop(account); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); + } + #[test] fn later_transactions_read_old_storage_of_created_account_as_zero() { let caller = Address::with_last_byte(1); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index 81d5a154b..d69a4b6dd 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -58,6 +58,23 @@ impl StorageOverlay { pub fn is_changed(&self) -> bool { self.wiped || self.changed_slots().next().is_some() } + + /// Rereads the slots that hold zero in the overlay after the account's creation was rolled + /// back. + /// + /// A slot first loaded under the creation cached zero instead of its backing value, and the + /// rollback restores its zero current value. A slot that fails to reload is evicted, so its + /// next load reports the error. + pub(super) fn reload_zeroed(&mut self, address: &Address, database: &mut dyn DynDatabase) { + self.slots.retain(|key, slot| { + if !slot.value.original.is_zero() || !slot.value.current.is_zero() { + return true; + } + let Ok(value) = database.get_storage(address, key) else { return false }; + slot.value = Tracked::new(value); + true + }); + } } /// Persistent storage slot cached by [`super::State`]. @@ -186,7 +203,8 @@ impl<'a, 'db> StorageHandle<'a, 'db> { /// slot's [`StorageSlot::is_warm`] is seeded from the base warm set (EIP-2930 access list), /// mirroring how [`account_raw`](super::State) seeds an account's warmth. The load itself /// records no revert entry: the cached value's original equals its current, so it is left in - /// place by [`State::rollback`](super::State::rollback) as a harmless cache. Used by + /// place by [`State::rollback`](super::State::rollback) as a harmless cache, except that a + /// creation's rollback reloads the slots it read as zero. Used by /// [`State::storage_slot`](super::State::storage_slot) to reach a single slot directly. #[inline] pub fn into_slot_with_skip( From a76ff17d2e4905289a39f8ffaf83448ddeadf938 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 29 Oct 2026 19:08:56 +0100 Subject: [PATCH 006/159] test(evm): pin underfunded top-level value transfers on both sides of BALANCE_CHECK (#746) Pins how a top-level value transfer that the sender cannot fund behaves on each side of `BALANCE_CHECK`, end to end through `transact`. The change is test-only; no behavior changes. With `BALANCE_CHECK` on, `validate_sender_and_bump_nonce` already rejects a transaction whose balance is below `max_upfront`, and every transaction type passes the gas cost plus the value (plus blob gas for EIP-4844) as `max_upfront`. This matches go-ethereum's pre-check together with its top-call affordability check (`core/state_transition.go:765-773` at `a0cba163b1f17f6044df84502ff13036dffb5de1`): an underfunded call or create is rejected with `InsufficientFunds`, the nonce is unchanged and no state changes. With `BALANCE_CHECK` and `BALANCE_TOP_UP` off, as in `eth_call`-style simulation and custom fee systems, the transaction still executes. The transfer halts with `OutOfFunds` and the sender nonce is consumed, so the same signed transaction cannot be replayed. Embedders depend on this opt-out, so it stays as it is. The new matrix test covers create and call, values 0, 1 and 2 against a balance of 1, legacy, EIP-2930 and EIP-1559 transactions, Prague and Amsterdam, and fee charging on and off. Found while comparing paradigmxyz/reth#25002 against main with trace-interop. Checked against go-ethereum master `a0cba163` with the geth parity harness from #859, run in a scratch copy because #859 is not on main yet. The case sends a create and a call whose value (2) exceeds what the sender has left after paying for gas (balance covers gas plus 1), followed by a create with value 1. Geth rejects the first two with `insufficient funds for gas * price + value` and runs the third; evm2 rejects and runs the same transactions, with the same receipts, state root and post state on Cancun, Prague, Osaka and Amsterdam. The case moves into `crates/cli/testdata/geth-parity/cases/` once #859 is on main. ``` $ scripts/geth_parity.py tx_rejected_underfunded_value 4 case/fork pairs, 8 comparisons: 0 differences (0 known, 0 new), 0 errors, 0 stale known entries, 0 skipped because both tools reject the input ``` --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/evm2/src/ethereum/mod.rs | 112 +++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 758075801..18bdbd34b 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -919,7 +919,7 @@ mod tests { interpreter::{Host, InstrStop, op}, registry::TxRegistry, }; - use alloc::{string::ToString, vec}; + use alloc::{format, string::ToString, vec}; use alloy_consensus::{TxEip2930, transaction::Recovered}; use alloy_eips::eip2930::AccessList; @@ -1450,4 +1450,114 @@ mod tests { assert_eq!(floor, base + floor_calldata_gas); } } + + #[rstest::rstest] + fn underfunded_top_level_value_depends_on_balance_check( + #[values(false, true)] balance_check: bool, + #[values(false, true)] fee_charge: bool, + ) { + let caller = Address::with_last_byte(0xaa); + let recipient = Address::with_last_byte(0xbb); + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + for to in [TxKind::Create, TxKind::Call(recipient)] { + for value in [0, 1, 2] { + for input in [ + Bytes::from_static(&[op::STOP]), + Bytes::from_static(&[op::PUSH0, op::PUSH0, op::REVERT]), + ] { + for tx_type in 0..=2 { + let mut version = Version::new(spec); + version.features.remove(EvmFeatures::BALANCE_TOP_UP); + version.features.set(EvmFeatures::BALANCE_CHECK, balance_check); + version.features.set(EvmFeatures::FEE_CHARGE, fee_charge); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo::default().with_nonce(10).with_balance(U256::ONE), + ); + database.insert_account_info( + &recipient, + AccountInfo::default() + .with_code(Bytecode::new_legacy(input.clone())), + ); + let tx = match tx_type { + 0 => TxEnvelope::Legacy(TxLegacy { + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + 1 => TxEnvelope::Eip2930(TxEip2930 { + chain_id: version.chain_id, + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + _ => TxEnvelope::Eip1559(TxEip1559 { + chain_id: version.chain_id, + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + }; + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnvExt { + gas_limit: U256::from(10_000_000), + ..Default::default() + }, + ethereum_tx_registry(spec), + database, + Precompiles::base(spec), + ); + let tx = Recovered::new_unchecked(tx, caller); + let case = format!("{spec:?} {to:?} value={value} type={tx_type}"); + let caller_before = evm.state.account_info_untracked(&caller).unwrap(); + let recipient_before = + evm.state.account_info_untracked(&recipient).unwrap(); + if balance_check && value == 2 { + assert_eq!( + evm.transact(&tx).map(crate::evm::ExecutedTx::discard), + Err(HandlerError::InsufficientFunds { + fee: U256::from(2), + balance: U256::ONE + }), + "{case}" + ); + assert_eq!( + evm.state.account_info_untracked(&caller).unwrap(), + caller_before, + "{case}" + ); + assert_eq!( + evm.state.account_info_untracked(&recipient).unwrap(), + recipient_before, + "{case}" + ); + continue; + } + let result = evm.transact(&tx).unwrap().commit(); + if value == 2 { + assert_eq!(result.stop, InstrStop::OutOfFunds, "{case}"); + } else { + assert_eq!(result.status, input[0] == op::STOP, "{case}"); + } + let nonce = + evm.state.account_info_untracked(&caller).unwrap().unwrap().nonce; + assert_eq!(nonce, 11, "{case}"); + } + } + } + } + } + } } From a29a283f6cd4bc65e44f5813de5e9f43553c5fda Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:21:20 +0100 Subject: [PATCH 007/159] feat(evm)!: let the EVM own the transaction environment (#866) The transaction environment was a handler-local value that every frame borrowed, so nothing outside the handler could change it once a transaction started. This moves it into `Evm` next to the block environment. Handlers and system calls write it before the initial frame, the interpreter and compiled code read it through `Host::tx_env`, and `Evm::tx_env_mut` lets an inspector change it during execution, the same way `Evm::set_block` already works for the block environment. Validation, fees, and nonces never read it, so a change affects only what the running code observes. This replaces the use case of #838. Instead of a dedicated origin override, Foundry sets the origin of a nested transaction from its top-level call or create hook: ```rust interp.host().tx_env_mut().origin = outer_origin; ``` The same call overrides every other field that opcodes read: the gas price (`GASPRICE`), the chain ID (`CHAINID`), the blob hashes (`BLOBHASH`), and the type-specific `ext`, such as a fee token. Cheatcodes that change these mid-transaction no longer need a step hook. `Host::execute_message` and `Interpreter::new` no longer take a transaction environment, and `Host` gains `tx_env`. Network handlers built on evm2 write `*host.tx_env_mut()` where they used to build a local environment. --- crates/evm2/examples/custom_evm/tx.rs | 5 +- crates/evm2/examples/precompile_subcall.rs | 6 +- crates/evm2/src/ethereum/eip1559.rs | 12 +- crates/evm2/src/ethereum/eip2930.rs | 12 +- crates/evm2/src/ethereum/eip4844.rs | 12 +- crates/evm2/src/ethereum/eip7702.rs | 4 +- crates/evm2/src/ethereum/legacy.rs | 12 +- crates/evm2/src/ethereum/mod.rs | 8 +- crates/evm2/src/evm/inspector.rs | 17 +- crates/evm2/src/evm/mod.rs | 192 ++++++++++++------ crates/evm2/src/evm/registry.rs | 5 +- crates/evm2/src/evm/system.rs | 5 +- crates/evm2/src/interpreter/host.rs | 7 +- .../src/interpreter/instructions/block.rs | 4 +- .../evm2/src/interpreter/instructions/env.rs | 4 +- .../interpreter/instructions/macro_tests.rs | 3 +- .../src/interpreter/instructions/memory.rs | 7 +- .../src/interpreter/instructions/system.rs | 6 +- crates/evm2/src/interpreter/runtime.rs | 41 +--- crates/evm2/src/test_utils.rs | 10 +- crates/evm2/src/tests.rs | 10 +- crates/inspectors/src/opcode.rs | 8 +- .../src/tracing/js/interrupt_tests.rs | 8 +- crates/jit/builtins/src/lib.rs | 36 +--- crates/jit/codegen/src/tests/runner.rs | 7 +- crates/jit/context/src/lib.rs | 19 +- crates/jit/runtime/src/evm2_evm.rs | 49 ++--- 27 files changed, 245 insertions(+), 264 deletions(-) diff --git a/crates/evm2/examples/custom_evm/tx.rs b/crates/evm2/examples/custom_evm/tx.rs index 4357cb7b9..ed102995e 100644 --- a/crates/evm2/examples/custom_evm/tx.rs +++ b/crates/evm2/examples/custom_evm/tx.rs @@ -66,8 +66,9 @@ pub fn execute_code( mut message: MessageExt, ) -> HandlerResult> { // The transaction handler owns policy; the interpreter still executes a normal message. - let tx_env = TxEnvExt { ext: CustomTxEnvExt { label: "execute-code" }, ..TxEnvExt::default() }; - let mut result = req.host.execute_message(&tx_env, &mut message)?; + *req.host.tx_env_mut() = + TxEnvExt { ext: CustomTxEnvExt { label: "execute-code" }, ..TxEnvExt::default() }; + let mut result = req.host.execute_message(&mut message)?; result.ext = CustomMessageResultExt { handled_custom_message: true }; Ok(evm2::TxResult:: { status: result.stop.is_success(), diff --git a/crates/evm2/examples/precompile_subcall.rs b/crates/evm2/examples/precompile_subcall.rs index 2da982aa3..1ce49f3ea 100644 --- a/crates/evm2/examples/precompile_subcall.rs +++ b/crates/evm2/examples/precompile_subcall.rs @@ -4,7 +4,7 @@ use alloy_primitives::{Address, Bytes, U256}; use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{AccountInfo, InMemoryDB, precompile::PrecompileOutput}, interpreter::{GasTracker, Host, InstrStop, Message, MessageExt, MessageKind, Word, op}, precompiles::{Precompile, PrecompileError, PrecompileHalt, PrecompileId, PrecompileResult}, @@ -27,7 +27,7 @@ fn main() { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); assert_eq!(result.output.len(), 32); @@ -86,7 +86,7 @@ fn staticcall_precompile( ..MessageExt::default() }; - let result = Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + let result = Host::execute_message(evm, &mut child)?; gas.merge_child_gas(result.gas, result.stop); match result.stop { diff --git a/crates/evm2/src/ethereum/eip1559.rs b/crates/evm2/src/ethereum/eip1559.rs index 0bffc0178..0f26de59b 100644 --- a/crates/evm2/src/ethereum/eip1559.rs +++ b/crates/evm2/src/ethereum/eip1559.rs @@ -98,7 +98,7 @@ pub fn execute_prepared>( let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -108,14 +108,8 @@ pub fn execute_prepared>( GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); let frame = prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( - req.host, - &tx_env, - frame, - &mut tx_gas, - execution_gas_limit, - reservoir, - )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/eip2930.rs b/crates/evm2/src/ethereum/eip2930.rs index 655a6370d..7f04108d6 100644 --- a/crates/evm2/src/ethereum/eip2930.rs +++ b/crates/evm2/src/ethereum/eip2930.rs @@ -92,7 +92,7 @@ pub fn execute_prepared>( let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -102,14 +102,8 @@ pub fn execute_prepared>( GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); let frame = prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( - req.host, - &tx_env, - frame, - &mut tx_gas, - execution_gas_limit, - reservoir, - )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/eip4844.rs b/crates/evm2/src/ethereum/eip4844.rs index 1c9812a99..36c76f8c5 100644 --- a/crates/evm2/src/ethereum/eip4844.rs +++ b/crates/evm2/src/ethereum/eip4844.rs @@ -111,7 +111,7 @@ pub fn execute_prepared>( let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -130,14 +130,8 @@ pub fn execute_prepared>( tx.value, &mut tx_gas, )?; - let result = execute_initial_frame( - req.host, - &tx_env, - frame, - &mut tx_gas, - execution_gas_limit, - reservoir, - )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/eip7702.rs b/crates/evm2/src/ethereum/eip7702.rs index c40ca1414..3f5019848 100644 --- a/crates/evm2/src/ethereum/eip7702.rs +++ b/crates/evm2/src/ethereum/eip7702.rs @@ -108,7 +108,7 @@ pub fn execute_prepared>( let tx = req.tx.inner(); let envelope = req.envelope; let chain_id = req.host.version().chain_id; - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(chain_id), @@ -202,7 +202,7 @@ pub fn execute_prepared>( // applied delegations, which stay) inside `execute_message`. The settle merges the frame gas // into `tx_gas`, which carries the authorization state gas into the block state-gas // accounting. - let mut result = req.host.execute_message(&tx_env, &mut message)?; + let mut result = req.host.execute_message(&mut message)?; settle_initial_frame_gas(&mut tx_gas, &mut result, charged_state_gas); settle(req.host, result) } diff --git a/crates/evm2/src/ethereum/legacy.rs b/crates/evm2/src/ethereum/legacy.rs index 13f0f667b..12eec802c 100644 --- a/crates/evm2/src/ethereum/legacy.rs +++ b/crates/evm2/src/ethereum/legacy.rs @@ -83,7 +83,7 @@ pub fn execute_prepared>( let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -93,14 +93,8 @@ pub fn execute_prepared>( GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); let frame = prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( - req.host, - &tx_env, - frame, - &mut tx_gas, - execution_gas_limit, - reservoir, - )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 18bdbd34b..b33c2ca3f 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -17,7 +17,6 @@ pub use lazy_eip7702::{LazyAuthorization, LazyTxEip7702}; use crate::{ Evm, EvmFeatures, EvmTypes, HostError, SpecId, TxResult, TxResultExt, Version, bytecode::Bytecode, - env::TxEnv, evm::{ AccountInfo, handler::{DefaultTxHandlerHooks, GasSettlement}, @@ -708,7 +707,6 @@ pub const fn settle_initial_frame_gas( /// Executes the prepared first frame and settles its gas into the transaction-level tracker. pub fn execute_initial_frame( host: &mut Evm<'_, T>, - tx_env: &TxEnv, frame: Option>, tx_gas: &mut GasTracker, execution_gas_limit: u64, @@ -720,7 +718,7 @@ pub fn execute_initial_frame( // Failed execution has already been rolled back to the message's own checkpoint inside // `execute_message`; the settle merges the frame gas into the transaction-level gas. - let mut result = host.execute_message(tx_env, &mut message)?; + let mut result = host.execute_message(&mut message)?; settle_initial_frame_gas(tx_gas, &mut result, charged_state_gas); Ok(result) } @@ -914,7 +912,7 @@ mod tests { use super::*; use crate::{ BaseEvmTypes, ExecutionConfig, Precompiles, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::InMemoryDB, interpreter::{Host, InstrStop, op}, registry::TxRegistry, @@ -1356,7 +1354,7 @@ mod tests { assert_eq!(message.code_address, delegated); assert_eq!(charged_state_gas, 0); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); assert_eq!(result.output.len(), 32); diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 0e9e70b0d..af4cf0f0c 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -147,7 +147,7 @@ mod tests { Version, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, evm::{AccountInfo, EmptyDB, InMemoryDB, SYSTEM_ADDRESS, State, StateCheckpoint}, interpreter::{ @@ -390,10 +390,9 @@ mod tests { Precompiles::base(spec_id), ); evm.set_inspector(inspector); - let tx_env = TxEnvExt::default(); let bytecode = legacy_bytecode(code); let mut message = MessageExt { gas_limit, code: bytecode, ..message.clone() }; - let result = Host::execute_message(&mut evm, &tx_env, &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); let inspector = evm.clear_inspector_as::().unwrap(); (result, inspector, evm) } @@ -770,8 +769,7 @@ mod tests { code: legacy_bytecode(code.clone()), ..Default::default() }; - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); let inspector = evm.clear_inspector_as::().unwrap(); assert_matches!(result.stop, InstrStop::Stop); assert_eq!(inspector.call_depths, [depth, depth + 1]); @@ -883,10 +881,9 @@ mod tests { Precompiles::base(SpecId::OSAKA), ); evm.set_inspector(MutateCallInspector { destination: replacement }); - let tx_env = TxEnvExt::default(); let bytecode = legacy_bytecode(code); let mut message = MessageExt { gas_limit: 100_000, code: bytecode, ..Default::default() }; - let result = Host::execute_message(&mut evm, &tx_env, &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_matches!(result.stop, InstrStop::Stop); // The redirected call transferred the value to the replacement, not the target. @@ -1228,13 +1225,12 @@ mod tests { push(&mut code, address_to_word(&target)); code.push(op::SELFDESTRUCT); - let tx_env = TxEnvExt::default(); let message = Message:: { gas_limit: 10_000, code: legacy_bytecode(code), ..Default::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let stop = interp.run_inspect(&mut host, &mut inspector); assert_matches!(stop, Err(crate::ExecutionError::Database(error)) if error.is_fatal()); @@ -1848,13 +1844,12 @@ mod tests { (Vec::from([op::PUSH1, 0]), 2, InstrStop::OutOfGas), (Vec::from([op::INVALID]), 10_000, InstrStop::InvalidFEOpcode), ] { - let tx_env = TxEnvExt::default(); let message = Message:: { gas_limit, code: legacy_bytecode(code), ..Default::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let stop = interp.run_inspect(&mut TestHost::default(), &mut GasEdit); assert_eq!(stop, Ok(expected)); diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index fb1b6b400..4dcc53577 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -211,6 +211,7 @@ pub struct Evm<'a, T: EvmTypesHost> { execution_config: ExecutionConfig, features: EvmFeatures, pub(crate) block: BlockEnv, + tx_env: TxEnv, registry: TxRegistry>, #[derive_where(skip)] pub(crate) state: State<'a>, @@ -344,6 +345,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { features: execution_config.version().features, execution_config, block, + tx_env: TxEnv::::default(), registry, state: State::new_mono(database), ext, @@ -478,6 +480,27 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.block = block; } + /// Returns the active transaction environment. + /// + /// Transaction handlers and system calls replace it before executing their initial frame, and + /// it keeps the last transaction's values afterwards. + #[inline] + pub const fn tx_env(&self) -> &TxEnv { + &self.tx_env + } + + /// Returns the active transaction environment mutably, including during execution. + /// + /// The current frame and nested frames observe changes on subsequent reads, such as `ORIGIN` + /// or `GASPRICE`. Validation, fees, and nonces do not read it, so an inspector can override, + /// for example, the origin of a running transaction from its top-level call or create hook + /// without affecting how the transaction is charged. Handlers overwrite it when the next + /// transaction starts executing. + #[inline] + pub const fn tx_env_mut(&mut self) -> &mut TxEnv { + &mut self.tx_env + } + /// Replaces the active execution specification and its fork-dependent dispatch state. /// /// `execution_config` must have the same base specification as `spec_id`. The transaction @@ -1267,17 +1290,14 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] fn execute_message_impl( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { let mut result = match message.kind { - MessageKind::Create | MessageKind::Create2 => { - self.execute_create_message(tx_env, message) - } + MessageKind::Create | MessageKind::Create2 => self.execute_create_message(message), MessageKind::Call | MessageKind::CallCode | MessageKind::DelegateCall - | MessageKind::StaticCall => self.execute_call_message(tx_env, message), + | MessageKind::StaticCall => self.execute_call_message(message), }?; // Settle the returning frame's gas for its stop reason at this single exit, // rather than in each result builder, so every consumer (parent @@ -1293,12 +1313,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn execute_message_inspected<'frame>( &mut self, - tx_env: &'frame TxEnv, message: &'frame mut Message, ) -> Result, ExecutionError> { let guard = self.enter_execution(); let Some(inspector) = guard.evm.active_inspector() else { - return guard.evm.execute_message_impl(tx_env, message); + return guard.evm.execute_message_impl(message); }; // SAFETY: The inspector is stored in `self`; the execution guard prevents inspector // replacement while the hooks are running. @@ -1323,7 +1342,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { None => { // SAFETY: The message outlives the frame, which is returned to the pool below. let frame_message = unsafe { trustme::decouple_lt(&*message) }; - let frame = top_frame.insert(guard.evm.interpreter_pool.pop(tx_env, frame_message)); + let frame = top_frame.insert(guard.evm.interpreter_pool.pop(frame_message)); top_code = message.code.clone(); // SAFETY: `top_code` owns a clone of the code, so the frame's pointers into it // stay valid even if a hook replaces `message.code`. It is declared before @@ -1346,7 +1365,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { let mut result = if let Some(error) = frame.take_error() { Err(error) } else { - inspected.map(Ok).unwrap_or_else(|| guard.evm.execute_message_impl(tx_env, message)) + inspected.map(Ok).unwrap_or_else(|| guard.evm.execute_message_impl(message)) }; if let Ok(result) = &mut result { @@ -1371,7 +1390,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn execute_create_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { if message.depth > self.version().max_call_depth() { @@ -1401,7 +1419,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } message.code_address = message.destination; let input = core::mem::take(&mut message.input); - let result = self.run_frame(tx_env, message, checkpoint.clone()); + let result = self.run_frame(message, checkpoint.clone()); message.input = input; let stop = result?; self.finish_create_message_run(checkpoint, &message.destination, stop) @@ -1538,7 +1556,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn execute_call_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { if message.depth > self.version().max_call_depth() { @@ -1577,7 +1594,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ..MessageResultExt::default() }); } - let stop = self.run_frame(tx_env, message, checkpoint.clone())?; + let stop = self.run_frame(message, checkpoint.clone())?; Ok(self.finish_call_message_run(checkpoint, stop)) } @@ -1662,12 +1679,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] fn run_frame( &mut self, - tx_env: &TxEnv, message: &Message, checkpoint: StateCheckpoint, ) -> Result { let previous = self.frame_checkpoint.replace(checkpoint); - let stop = self.run_interpreter(tx_env, message); + let stop = self.run_interpreter(message); self.frame_checkpoint = previous; stop } @@ -1675,12 +1691,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn run_interpreter<'frame>( &mut self, - tx_env: &'frame TxEnv, message: &'frame Message, ) -> Result { let guard = self.enter_execution(); - let mut interp: Box> = - guard.evm.interpreter_pool.pop(tx_env, message); + let mut interp: Box> = guard.evm.interpreter_pool.pop(message); let interp_ref = interp.as_mut(); guard.evm.inspect_initialize_interp(interp_ref); let inspector = guard.evm.active_inspector().map(|inspector| { @@ -1733,6 +1747,10 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { &self.block } + fn tx_env(&mut self) -> &TxEnv { + &self.tx_env + } + fn load_account( &mut self, address: &Address, @@ -1858,14 +1876,13 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { #[inline] fn execute_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { let checkpoint = self.state.checkpoint(); let result = if self.inspector.is_some() { - self.execute_message_inspected(tx_env, message) + self.execute_message_inspected(message) } else { - self.execute_message_impl(tx_env, message) + self.execute_message_impl(message) }; if result.is_err() { self.state.rollback(checkpoint, self.features); @@ -2056,7 +2073,7 @@ mod tests { use crate::{ BaseEvmConfigSelector, BaseEvmTypes, NoopInspector, Precompiles, SpecId, Version, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, interpreter::{GasTracker, Interpreter, Message, MessageExt, MessageKind, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileMap}, @@ -2151,14 +2168,13 @@ mod tests { Precompiles::base(SpecId::OSAKA), ); evm.set_interpreter_runner(TestInterpreterRunner { stop, calls: Arc::clone(&calls) }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 30_000, code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), ..Default::default() }; - let stop = evm.run_interpreter(&tx_env, &message).unwrap(); + let stop = evm.run_interpreter(&message).unwrap(); assert_eq!(calls.load(Ordering::Relaxed), 1); stop @@ -2261,7 +2277,7 @@ mod tests { evm.set_inspector(LogInspector::default()); let mut message = precompile_message(TEST_PRECOMPILE); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); let inspector = evm.clear_inspector_as::().unwrap(); @@ -2285,7 +2301,7 @@ mod tests { let mut child = precompile_message(INNER_TEST_PRECOMPILE); child.depth = message.depth + 1; child.input = message.input.clone(); - Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + Host::execute_message(evm, &mut child)?; evm.log(Log { address: TEST_PRECOMPILE, data: LogData::default() }); Ok(PrecompileOutput::new(Bytes::new())) }), @@ -2314,8 +2330,7 @@ mod tests { if revert { message.input = Bytes::from_static(b"revert"); } - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); let inspector = evm.clear_inspector_as::().unwrap(); @@ -2361,8 +2376,7 @@ mod tests { for address in [TEST_PRECOMPILE, INNER_TEST_PRECOMPILE] { let mut message = precompile_message(address); - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::PrecompileOOG); assert_eq!(result.gas.remaining(), 0); @@ -2418,7 +2432,7 @@ mod tests { test_precompile(TEST_PRECOMPILE, |evm, _, _| { let mut child = precompile_message(INNER_TEST_PRECOMPILE); child.depth = 1; - Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + Host::execute_message(evm, &mut child)?; panic!("execution must abort on a database error"); }), test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { @@ -2436,11 +2450,7 @@ mod tests { InMemoryDB::default(), precompiles, ); - let result = Host::execute_message( - &mut evm, - &TxEnvExt::default(), - &mut precompile_message(TEST_PRECOMPILE), - ); + let result = Host::execute_message(&mut evm, &mut precompile_message(TEST_PRECOMPILE)); let ExecutionError::Database(error) = result.unwrap_err() else { panic!("expected database error") }; @@ -2534,7 +2544,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let result = Host::execute_message(&mut evm, &mut message); let ExecutionError::Fatal(error) = result.unwrap_err() else { panic!("expected fatal precompile error") @@ -2592,7 +2602,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Stop); evm.state.finalize_transaction_(Version::base(SpecId::OSAKA)); @@ -2899,8 +2909,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[test] @@ -2925,9 +2934,8 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[test] @@ -2983,12 +2991,70 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); assert_eq!(evm.block().number, Word::from(123)); assert_eq!(evm.block().timestamp, Word::from(124)); } + #[test] + fn tx_env_mut_during_transaction_overrides_origin() { + struct OriginInspector(Address); + + impl Inspector for OriginInspector { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth == 0 { + interp.host().tx_env_mut().origin = self.0; + } + None + } + } + + let caller = Address::repeat_byte(0xaa); + let origin = Address::repeat_byte(0xbb); + let target = Address::repeat_byte(0x11); + let inner = Address::repeat_byte(0x22); + // SSTORE(0, ORIGIN), then CALL(inner). + let mut code = vec![op::ORIGIN, op::PUSH0, op::SSTORE]; + code.extend([op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH20]); + code.extend(inner.0); + code.extend([op::GAS, op::CALL, op::STOP]); + // SSTORE(1, ORIGIN) + let inner_code = vec![op::ORIGIN, op::PUSH1, 1, op::SSTORE, op::STOP]; + let mut database = InMemoryDB::default(); + for (address, code) in [(target, code), (inner, inner_code)] { + database.insert_account_info( + &address, + AccountInfo::default().with_nonce(1).with_code(legacy_bytecode(code)), + ); + } + let mut evm = Evm::::new( + SpecId::CANCUN, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::CANCUN), + database, + Precompiles::base(SpecId::CANCUN), + ); + let tx = Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { + to: TxKind::Call(target), + gas_limit: 500_000, + ..Default::default() + }), + caller, + ); + let executed = evm.transact_with_inspector(&tx, &mut OriginInspector(origin)).unwrap(); + assert!(executed.commit().status); + + let origin_word = Word::from_be_slice(origin.as_slice()); + assert_eq!(evm.state.storage_slot_untracked(&target, &Word::ZERO).unwrap(), origin_word); + assert_eq!(evm.state.storage_slot_untracked(&inner, &Word::from(1)).unwrap(), origin_word); + assert_eq!(evm.state.account_info_untracked(&caller).unwrap().unwrap().nonce, 1); + } + #[test] #[should_panic(expected = "execution config cannot be modified during EVM execution")] fn set_execution_config_panics_during_execution() { @@ -3018,8 +3084,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[derive(Clone, Copy)] @@ -3071,11 +3136,11 @@ mod tests { } TopLevelInspectorHook::Call => { let mut message = MessageExt { kind: MessageKind::Call, ..Default::default() }; - let _ = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let _ = Host::execute_message(&mut evm, &mut message); } TopLevelInspectorHook::Create => { let mut message = MessageExt { kind: MessageKind::Create, ..Default::default() }; - let _ = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let _ = Host::execute_message(&mut evm, &mut message); } } } @@ -3591,7 +3656,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); } @@ -3656,7 +3721,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let result = Host::execute_message(&mut evm, &mut message); let ExecutionError::Database(error) = result.unwrap_err() else { panic!("expected database error") @@ -3665,10 +3730,7 @@ mod tests { assert!(error.downcast_ref::().is_some()); assert_eq!(error.to_string(), "storage read failed"); message.code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); - assert_eq!( - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap().stop, - InstrStop::Stop - ); + assert_eq!(Host::execute_message(&mut evm, &mut message).unwrap().stop, InstrStop::Stop); } #[test] @@ -3693,7 +3755,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert!(!evm.state.storage(&contract).unwrap().is_warm(&key)); @@ -3775,7 +3837,7 @@ mod tests { code: selfdestruct_to_code(&target), ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert_eq!(target_reads.load(Ordering::SeqCst), 0); @@ -3805,7 +3867,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::FRONTIER)); @@ -3857,7 +3919,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert!(result.output.is_empty()); @@ -3899,7 +3961,7 @@ mod tests { code, ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::CreateContractStartingWithEF); assert!(result.output.is_empty()); @@ -3934,7 +3996,7 @@ mod tests { code, ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::CreateContractSizeLimit); assert!(result.output.is_empty()); @@ -3961,7 +4023,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); @@ -3996,7 +4058,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); @@ -4062,7 +4124,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); let version = *evm.version(); @@ -4707,7 +4769,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - Host::execute_message(req.host, &TxEnvExt::default(), &mut message)?; + Host::execute_message(req.host, &mut message)?; Ok(TxResultExt::default()) }, ), @@ -4825,7 +4887,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - Host::execute_message(req.host, &TxEnvExt::default(), &mut message)?; + Host::execute_message(req.host, &mut message)?; Ok(TxResultExt::default()) }, ), diff --git a/crates/evm2/src/evm/registry.rs b/crates/evm2/src/evm/registry.rs index 196e20c56..e03e67b27 100644 --- a/crates/evm2/src/evm/registry.rs +++ b/crates/evm2/src/evm/registry.rs @@ -489,6 +489,10 @@ mod tests { &self.block } + fn tx_env(&mut self) -> &TxEnv { + unimplemented!() + } + fn load_account( &mut self, _address: &Address, @@ -543,7 +547,6 @@ mod tests { fn execute_message( &mut self, - _tx_env: &TxEnv, _message: &mut Message, ) -> Result, crate::ExecutionError> { unimplemented!() diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index 1711a121e..a0d0a64b3 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -139,7 +139,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn execute_system_call(&mut self, tx: SystemTx) -> HandlerResult> { let SystemTx { caller, system_contract_address, data, gas_limit, .. } = tx; self.state.prewarm(&system_contract_address); - let tx_env = TxEnvExt { + self.tx_env = TxEnvExt { origin: caller, gas_price: U256::ZERO, chain_id: U256::from(self.version().chain_id), @@ -165,8 +165,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { U256::ZERO, &mut tx_gas, )?; - let result = - execute_initial_frame(self, &tx_env, frame, &mut tx_gas, gas_limit, reservoir)?; + let result = execute_initial_frame(self, frame, &mut tx_gas, gas_limit, reservoir)?; // The reservoir is funded on top of `gas_limit`, so the state gas it paid for is added // back; otherwise `total_gas_spent` would not cover `state_gas_spent`. let gas_spent = gas_limit diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index c7722002d..ffcd414ff 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -127,6 +127,12 @@ pub trait Host { /// must keep it at a stable address and unchanged while the interpreter runs. fn block_env(&mut self) -> &BlockEnv; + /// Returns the transaction environment. + /// + /// Compiled code keeps a pointer to the returned environment for the whole run, so the host + /// must keep it at a stable address while the interpreter runs. + fn tx_env(&mut self) -> &TxEnv; + /// Loads account information. fn load_account( &mut self, @@ -177,7 +183,6 @@ pub trait Host { /// Executes a message inside this host. fn execute_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError>; diff --git a/crates/evm2/src/interpreter/instructions/block.rs b/crates/evm2/src/interpreter/instructions/block.rs index fd27fbc6d..7579c1ed7 100644 --- a/crates/evm2/src/interpreter/instructions/block.rs +++ b/crates/evm2/src/interpreter/instructions/block.rs @@ -51,7 +51,7 @@ pub fn gaslimit(cx: _) -> out { #[instruction] pub fn chainid(cx: _) -> Result { - *out = cx.state.tx().chain_id; + *out = cx.state.host().tx_env().chain_id; } #[instruction] @@ -73,7 +73,7 @@ pub fn basefee(cx: _) -> Result { #[instruction] pub fn blobhash(cx: _, [index]: [Word]) -> Result { let index = index.to_usize_saturated(); - *out = cx.state.tx().blob_hashes.get(index).copied().unwrap_or_default(); + *out = cx.state.host().tx_env().blob_hashes.get(index).copied().unwrap_or_default(); } #[instruction] diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index d88807e6b..085de3c03 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -56,7 +56,7 @@ pub fn balance(cx: _, [addr]: [Word]) -> Result { #[instruction] pub fn origin(cx: _) -> out { - out.write(address_to_word(&cx.state.tx().origin)); + out.write(address_to_word(&cx.state.host().tx_env().origin)); } #[instruction] @@ -107,7 +107,7 @@ pub fn codecopy(cx: _, [memory_offset, code_offset, len]: [Word]) -> Result { #[instruction] pub fn gasprice(cx: _) -> out { - *out = cx.state.tx().gas_price; + *out = cx.state.host().tx_env().gas_price; } #[instruction(dynamic_gas)] diff --git a/crates/evm2/src/interpreter/instructions/macro_tests.rs b/crates/evm2/src/interpreter/instructions/macro_tests.rs index a1a466956..eee94e981 100644 --- a/crates/evm2/src/interpreter/instructions/macro_tests.rs +++ b/crates/evm2/src/interpreter/instructions/macro_tests.rs @@ -80,10 +80,11 @@ fn run(config: RunConfig<'_>) -> TestInterpreter { let execution_config = ExecutionConfig::::for_config::(); let RunConfig { code, host, spec_id, tx_env, message, gas_limit, return_data } = config; let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; - let mut inner = Interpreter::::new(&tx_env, &message); + let mut inner = Interpreter::::new(&message); *inner.return_data_mut() = return_data; let mut default_host = TestHost::default(); let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; host.spec_id = spec_id; host.execution_config = execution_config; let err = inner.run(host).unwrap(); diff --git a/crates/evm2/src/interpreter/instructions/memory.rs b/crates/evm2/src/interpreter/instructions/memory.rs index 12c2c2c23..f6131e490 100644 --- a/crates/evm2/src/interpreter/instructions/memory.rs +++ b/crates/evm2/src/interpreter/instructions/memory.rs @@ -43,7 +43,6 @@ pub fn mcopy(cx: _, [dst, src, len]: [Word]) -> Result { mod tests { use crate::{ ExecutionConfig, SpecId, Version, - env::TxEnvExt, interpreter::{InstrStop, Interpreter, MessageExt, Word, op}, test_utils::{RunConfig, TestHost, TestTypes, legacy_bytecode, push, run, run_stack}, version::GasId, @@ -101,10 +100,9 @@ mod tests { code.push(op::MSTORE); code.push(op::STOP); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 10_000, code: legacy_bytecode(code), ..MessageExt::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let mut host = TestHost { execution_config: config, ..TestHost::default() }; let err = interp.run(&mut host).unwrap(); @@ -123,10 +121,9 @@ mod tests { code.push(op::MSTORE); code.push(op::STOP); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 17, code: legacy_bytecode(code), ..MessageExt::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let mut host = TestHost { execution_config: config, ..TestHost::default() }; let err = interp.run(&mut host).unwrap(); diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 08323fde5..f9ba38dcf 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -238,9 +238,8 @@ fn call_inner( let new_account_state_gas = prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range)?; - let tx_env = state.tx(); let mut result = - state.host().execute_message(tx_env, &mut message).map_err(|error| state.fail(error))?; + state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; if result.stop.is_fatal() { return Err(result.stop); } @@ -376,7 +375,6 @@ fn create_inner( }; gas.spend(gas_limit)?; - let tx_env = state.tx(); let mut message = MessageExt { kind, depth, @@ -396,7 +394,7 @@ fn create_inner( _non_exhaustive: (), }; let mut result = - state.host().execute_message(tx_env, &mut message).map_err(|error| state.fail(error))?; + state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; if result.stop.is_fatal() { return Err(result.stop); } diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index c1b2bf95c..f316612ea 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -5,7 +5,6 @@ use super::{ use crate::{ EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, Version, bytecode::Bytecode, - env::TxEnv, evm::inspector::Inspector, interpreter::dispatch, trustme, @@ -32,8 +31,6 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) pc: *const u8, output: Range, #[derive_where(skip)] - tx_env: Option<&'frame TxEnv>, - #[derive_where(skip)] message: Option<&'frame Message>, host: Option>>, inspector: Option + 'host>>, @@ -62,17 +59,15 @@ unsafe impl Send for Interpreter<'_, '_, T> where T: EvmTypesHost, T::MessageExt: Sync, - T::TxEnvExt: Sync, { } impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { - /// Creates an interpreter from a transaction-global environment and a frame-local message, - /// which carries the bytecode to run. - pub fn new(tx_env: &'frame TxEnv, message: &'frame Message) -> Self { + /// Creates an interpreter from a frame-local message, which carries the bytecode to run. + pub fn new(message: &'frame Message) -> Self { // SAFETY: Calling init right after. let mut interp = unsafe { Self::uninit() }; - interp.init(tx_env, message); + interp.init(message); interp } @@ -93,7 +88,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { result: Ok(()), error: None, output: 0..0, - tx_env: None, message: None, is_static: false, return_data: Bytes::new(), @@ -107,7 +101,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { } /// Initializes this interpreter for a new frame, retaining reusable allocations. - fn init(&mut self, tx_env: &'frame TxEnv, message: &'frame Message) { + fn init(&mut self, message: &'frame Message) { let gas_limit = message.gas_limit; let is_static = message.is_static(); self.set_bytecode(&message.code); @@ -117,7 +111,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.result = Ok(()); self.error = None; self.output = 0..0; - self.tx_env = Some(tx_env); self.message = Some(message); self.is_static = is_static; self.return_data = Bytes::new(); @@ -133,7 +126,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { pub(crate) fn clear_frame_refs(&mut self) { self.bytecode = Default::default(); self.bytecode_ref = None; - self.tx_env = None; self.message = None; self.host = None; self.inspector = None; @@ -303,13 +295,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { core::mem::offset_of!(Self, message) } - /// Returns the cached transaction-global environment. - #[inline] - pub const fn tx_env(&self) -> &'frame TxEnv { - // SAFETY: `tx_env` is initialized before execution starts. - unsafe { self.tx_env.unwrap_unchecked() } - } - /// Returns return data from the last call-like operation. #[inline] pub const fn return_data(&self) -> &Bytes { @@ -596,14 +581,6 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { self.0.stack_len = stack_len; } - /// Returns the cached transaction-global environment. - #[inline] - pub const fn tx(&self) -> &'frame TxEnv { - // SAFETY: `tx_env` is initialized at the beginning of `run` and remains set for - // instruction execution. - unsafe { self.0.tx_env.unwrap_unchecked() } - } - /// Returns the active base specification ID. #[inline] pub const fn spec(&self) -> SpecId { @@ -781,15 +758,14 @@ impl InterpreterPool { pub(crate) fn pop<'frame, 'host>( &mut self, - tx_env: &'frame TxEnv, message: &'frame Message, ) -> Box> { let frame = match self.frames.pop() { Some(mut frame) => { - frame.init(tx_env, message); + frame.init(message); frame } - None => Box::new(Interpreter::new(tx_env, message)), + None => Box::new(Interpreter::new(message)), }; // SAFETY: Frames stored in the pool have their frame-local references cleared before they // are erased to `'static`. Rebinding the lifetime is only used to initialize the next @@ -822,13 +798,12 @@ impl InterpreterPool { #[cfg(test)] mod owned_error_tests { use super::*; - use crate::{DatabaseError, env::TxEnvExt, interpreter::MessageExt, test_utils::TestTypes}; + use crate::{DatabaseError, interpreter::MessageExt, test_utils::TestTypes}; #[test] fn owned_error_is_taken_on_exit() { - let tx = TxEnvExt::default(); let message = MessageExt::default(); - let mut interpreter = Interpreter::::new(&tx, &message); + let mut interpreter = Interpreter::::new(&message); let error = DatabaseError::new(core::fmt::Error, false); let stop = interpreter.fail(error.clone()); let result = interpreter.finish_run(stop); diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index 9f5cac91f..25dd584ef 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -35,6 +35,7 @@ pub(crate) struct TestHost { pub(crate) spec_id: SpecId, pub(crate) execution_config: ExecutionConfig, pub(crate) block: BlockEnv, + pub(crate) tx_env: TxEnv, pub(crate) code_hash: B256, pub(crate) code: Bytes, pub(crate) exists: bool, @@ -64,6 +65,7 @@ impl Default for TestHost { SpecId::OSAKA, ), block: BlockEnvExt::default(), + tx_env: TxEnvExt::default(), code_hash: B256::ZERO, code: Bytes::new(), exists: true, @@ -103,6 +105,10 @@ impl Host for TestHost { &self.block } + fn tx_env(&mut self) -> &TxEnv { + &self.tx_env + } + fn load_account( &mut self, address: &Address, @@ -204,7 +210,6 @@ impl Host for TestHost { fn execute_message( &mut self, - _tx_env: &TxEnv, message: &mut Message, ) -> Result, crate::ExecutionError> { // Mimics the depth limit enforced by the real host. @@ -341,10 +346,11 @@ impl Default for RunConfig<'_> { pub(crate) fn run(config: RunConfig<'_>) -> TestInterpreter { let RunConfig { code, host, spec_id, tx_env, message, gas_limit, return_data } = config; let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; - let mut inner = Interpreter::::new(&tx_env, &message); + let mut inner = Interpreter::::new(&message); *inner.return_data_mut() = return_data; let mut default_host = TestHost::default(); let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; host.spec_id = spec_id; host.execution_config = ExecutionConfig::for_base_spec::(spec_id); let (err, execution_error) = match inner.run(host) { diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 3a431aa6f..985166b39 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -1,7 +1,7 @@ use crate::{ BaseEvmTypes, Evm, EvmFeatures, ExecutionConfig, Inspector, Precompiles, SpecId, Version, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::{ LazyAuthorization, LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, eip7702::{AuthRefunds, Eip7702Authorization, RuntimeAuthCharges, apply_auth_list}, @@ -28,7 +28,7 @@ fn run_tx(evm: &mut TestEvm, destination: Address, code: impl Into>) { code: legacy_bytecode(code), ..Default::default() }; - let result = Host::execute_message(evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(evm, &mut message).unwrap(); assert!(result.stop.is_success()); } @@ -136,7 +136,7 @@ fn evm_propagates_child_sstore_negative_refund() { code: legacy_bytecode(parent_code), ..Default::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert!(result.stop.is_success()); assert_eq!(result.gas.refunded(), 0); @@ -159,7 +159,7 @@ fn evm_reports_invalid_transaction_execution() { code: legacy_bytecode([op::PUSH1, 0x01, op::SSTORE]), ..Default::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::StackUnderflow); } @@ -563,7 +563,7 @@ fn precompile_dispatch_uses_original_callee( ) .unwrap() .unwrap(); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut frame.message).unwrap(); + let result = Host::execute_message(&mut evm, &mut frame.message).unwrap(); assert!(result.stop.is_success(), "{:?}", result.stop); assert_eq!(result.output.as_ref(), expected_output); diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index 9b58632c0..e899d33e7 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -169,7 +169,7 @@ pub fn immediate_size(opcode: u8) -> u8 { mod tests { use super::*; use alloy_primitives::Bytes; - use evm2::{BaseEvmTypes, bytecode::Bytecode, env::TxEnv}; + use evm2::{BaseEvmTypes, bytecode::Bytecode}; #[test] fn test_opcode_counter_inspector() { @@ -177,12 +177,11 @@ mod tests { let opcodes = [op::ADD, op::ADD, op::ADD, op::BYTE]; - let tx_env = TxEnv::::default(); let message = Message:: { code: Bytecode::new_legacy(Bytes::from(opcodes)), ..Message::::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); for _ in &opcodes { opcode_counter.step(&mut interp); @@ -195,12 +194,11 @@ mod tests { let opcodes = [op::PUSH1, op::PUSH1, op::ADD, op::PUSH1, op::SSTORE, op::STOP]; - let tx_env = TxEnv::::default(); let message = Message:: { code: Bytecode::new_legacy(Bytes::from(opcodes)), ..Message::::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); for _ in &opcodes { opcode_counter.step(&mut interp); diff --git a/crates/inspectors/src/tracing/js/interrupt_tests.rs b/crates/inspectors/src/tracing/js/interrupt_tests.rs index dcf134087..b9d56644e 100644 --- a/crates/inspectors/src/tracing/js/interrupt_tests.rs +++ b/crates/inspectors/src/tracing/js/interrupt_tests.rs @@ -8,7 +8,7 @@ use boa_gc::{Finalize, Trace}; use evm2::{ BaseEvmTypes, Precompiles, SpecId, bytecode::Bytecode, - env::{BlockEnvExt, TxEnv}, + env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, evm::{AccountInfo, CacheDB, EmptyDB}, registry::HandlerError, @@ -66,9 +66,8 @@ fn interrupt_stops_steps_without_js_step_callback() { let mut inspector = JsInspector::new(TRACER.into(), serde_json::Value::Null) .unwrap() .with_interrupt(interrupt.clone()); - let tx_env = TxEnv::::default(); let message = Message::::default(); - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); // Signal from another thread after the inspector has been created. std::thread::spawn(move || interrupt.interrupt()).join().unwrap(); inspector.step(&mut interp); @@ -127,9 +126,8 @@ fn interrupt_before_step_end_skips_callback_and_preserves_error() { ) .unwrap() .with_interrupt(interrupt.clone()); - let tx_env = TxEnv::::default(); let message = Message::::default(); - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); inspector.step(&mut interp); assert!(inspector.pending_steps[0].active); let error = if preserve_error { diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 5b3296ae7..0a14f6fea 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -681,7 +681,6 @@ pub unsafe fn __revmc_builtin_create( } ecx.gas.spend(gas_limit)?; - let tx_env = ecx.tx_env(); let mut message = MessageExt { kind, depth, @@ -701,7 +700,7 @@ pub unsafe fn __revmc_builtin_create( }; let mut result = ecx .host() - .execute_message(tx_env, &mut message) + .execute_message(&mut message) .map_err(|error| ecx.interpreter_mut().fail(error))?; if result.stop.is_fatal() { return Err(result.stop.into()); @@ -816,10 +815,9 @@ pub unsafe fn __revmc_builtin_call( _non_exhaustive: (), }; - let tx_env = ecx.tx_env(); let mut result = ecx .host() - .execute_message(tx_env, &mut message) + .execute_message(&mut message) .map_err(|error| ecx.interpreter_mut().fail(error))?; if result.stop.is_fatal() { return Err(result.stop.into()); @@ -1107,7 +1105,7 @@ mod tests { use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, constants::CALL_DEPTH_LIMIT, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::ethereum_tx_registry, evm::{AccountInfo, EmptyDB, InMemoryDB, inspector::Inspector}, interpreter::{GasTracker, Message, MessageResult, MessageResultExt, op}, @@ -1222,15 +1220,13 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination: caller, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1275,7 +1271,6 @@ mod tests { let stack_value = Word::from(0x12); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1284,8 +1279,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1315,7 +1309,6 @@ mod tests { let current_value = Word::from(0x99); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1324,8 +1317,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1354,7 +1346,6 @@ mod tests { let caller = Address::from([0x11; 20]); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1363,8 +1354,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1400,14 +1390,12 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1445,14 +1433,12 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1499,7 +1485,6 @@ mod tests { Precompiles::base(SpecId::AMSTERDAM), ); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { depth, gas_limit: 100_000, @@ -1507,8 +1492,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index ee042b578..65f354eda 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -359,6 +359,7 @@ fn prepare_host(spec_id: SpecId) -> Evm<'static, BaseEvmTypes> { def_database(), Precompiles::base(spec_id), ); + *evm.tx_env_mut() = def_tx_env(); for address in [Address::ZERO, DEF_ADDR, DEF_CALLER, OTHER_ADDR, Address::with_last_byte(0x69)] { evm.state_mut().prewarm(&address); @@ -396,7 +397,6 @@ fn with_evm_context_and_host_mut< modify_message: Option)>, f: F, ) -> R { - let tx_env = def_tx_env(); let mut message = def_message(DEF_GAS_LIMIT); if let Some(modify_message) = modify_message { modify_message(&mut message); @@ -405,7 +405,7 @@ fn with_evm_context_and_host_mut< code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), ..message }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); interpreter.prepare_run(host); let (mut ecx, stack, stack_len) = @@ -512,7 +512,6 @@ fn run_compiled_test_case_with_context( } // Interpreter - run evm2 as the oracle - let tx_env = def_tx_env(); let mut message = def_message(gas_limit); if let Some(modify_message) = modify_message { modify_message(&mut message); @@ -522,7 +521,7 @@ fn run_compiled_test_case_with_context( code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), ..message }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut int_host = prepare_host(spec_id); let int_stop = interpreter.run(&mut int_host).unwrap(); let int_result = int_stop; diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index c04e25ca7..4c27cc36b 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -64,8 +64,8 @@ pub struct EvmContext<'ctx, 'frame, 'host, T: EvmTypesHost = BaseEvmTypes> { /// The host's block environment, cached for block opcodes. pub block_env: NonNull>, - /// The transaction environment, cached for transaction opcodes. - pub tx_env: &'frame TxEnv, + /// The host's transaction environment, cached for transaction opcodes. + pub tx_env: NonNull>, _marker: PhantomData<&'ctx mut Interpreter<'frame, 'host, T>>, } @@ -96,7 +96,7 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { let interpreter_ptr = ptr::from_mut(&mut *interpreter); let interpreter_ptr = unsafe { NonNull::new_unchecked(interpreter_ptr) }; let block_env = NonNull::from(interpreter.host().block_env()); - let tx_env = interpreter.tx_env(); + let tx_env = NonNull::from(interpreter.host().tx_env()); let message = interpreter.message(); let gas = interpreter.gas(); let calldatasize = message.input.len(); @@ -180,10 +180,10 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { unsafe { self.block_env.as_ref() } } - /// Returns the transaction-global environment. + /// Returns the current transaction environment. #[inline] - pub const fn tx_env(&self) -> &'frame TxEnv { - self.tx_env + pub const fn tx_env(&self) -> &TxEnv { + unsafe { self.tx_env.as_ref() } } /// Returns active runtime version data. @@ -831,7 +831,7 @@ mod tests { use evm2::{ DatabaseError, Evm, ExecutionError, InterpreterRunner, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{EmptyDB, precompile::NoPrecompiles}, interpreter::MessageExt, registry::TxRegistry, @@ -864,7 +864,6 @@ mod tests { .interpreter_mut() .fail(DatabaseError::new(core::fmt::Error, false)) } - let tx = TxEnvExt::default(); let mut message = MessageExt { gas_limit: 30_000, code: Bytecode::new_legacy(Bytes::from_static(&[0x00])), @@ -878,12 +877,12 @@ mod tests { NoPrecompiles::default(), ); host.set_interpreter_runner(TestRunner(EvmCompilerFn::new(fail))); - let error = host.execute_message(&tx, &mut message).unwrap_err(); + let error = host.execute_message(&mut message).unwrap_err(); let ExecutionError::Database(error) = error else { panic!("expected database error") }; assert!(!error.is_fatal()); assert!(error.downcast_ref::().is_some()); host.set_interpreter_runner(TestRunner(EvmCompilerFn::new(__test_fn))); - assert_eq!(host.execute_message(&tx, &mut message).unwrap().stop, InstrStop::Stop); + assert_eq!(host.execute_message(&mut message).unwrap().stop, InstrStop::Stop); } #[test] diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 69e704bbd..31ad7c6b1 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -78,7 +78,7 @@ mod tests { use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::ethereum_tx_registry, evm::EmptyDB, interpreter::{MessageExt, op}, @@ -138,8 +138,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(code)), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let stop = if with_jit { run_interpreter(&backend, &mut interpreter, &mut host).unwrap() } else { @@ -254,13 +253,12 @@ mod tests { #[test] fn disabled_backend_falls_back_to_interpreter() { - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..Default::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut host = Evm::::new( SpecId::CANCUN, BlockEnvExt::default(), @@ -275,13 +273,12 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn gas_above_i64_max_falls_back_to_interpreter() { - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: i64::MAX as u64 + 1, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..Default::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut host = Evm::::new( SpecId::CANCUN, BlockEnvExt::default(), @@ -314,7 +311,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); // The call output overwrites the word stored before it. let mut code = vec![op::PUSH1, 0x69, op::PUSH0, op::MSTORE]; code.extend([op::PUSH1, 0x20, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0]); @@ -339,7 +335,7 @@ mod tests { code: Bytecode::new_legacy(code.into()), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -361,7 +357,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = [ op::PUSH10, 0x60, @@ -397,7 +392,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -432,7 +427,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = vec![ op::PUSH1, 0x20, @@ -458,7 +452,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -502,7 +496,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = vec![ op::PUSH1, 0x69, @@ -528,7 +521,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Stop)); let beneficiary_balance = host.read_account_info(&beneficiary).unwrap().unwrap().balance; @@ -555,8 +548,6 @@ mod tests { code_address: outer, ..MessageExt::default() }; - let tx_env = TxEnvExt { gas_price: Word::from(10), ..TxEnvExt::default() }; - let run = |with_jit: bool| { let mut database = InMemoryDB::default(); database @@ -582,12 +573,13 @@ mod tests { database, Precompiles::base(SpecId::CANCUN), ); + host.tx_env_mut().gas_price = Word::from(10); host.state_mut().prewarm(&caller); host.state_mut().prewarm(&outer); if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); } - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let stop = if with_jit { run_interpreter(&backend, &mut interpreter, &mut host).unwrap() } else { @@ -669,8 +661,7 @@ mod tests { if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(blocking_backend())); } - Host::execute_message(&mut host, &TxEnvExt::default(), &mut message.clone()) - .unwrap() + Host::execute_message(&mut host, &mut message.clone()).unwrap() }; let interpreted = run(false); let compiled = run(true); @@ -800,9 +791,7 @@ mod tests { code: code.clone(), ..Default::default() }; - let result = - Host::execute_message(&mut host, &TxEnvExt::default(), &mut message) - .unwrap(); + let result = Host::execute_message(&mut host, &mut message).unwrap(); let destination_journaled = host.state_mut().journal().iter().any(|entry| { matches!( @@ -853,7 +842,7 @@ mod custom_host_tests { BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, OpcodeConfig, SpecId, bytecode::Bytecode, config::BaseEvmConfigSelector, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{EmptyDB, precompile::NoPrecompiles}, interpreter::{MessageExt, Word, instructions::sstore_with, op}, registry::TxRegistry, @@ -892,7 +881,6 @@ mod custom_host_tests { EmptyDB::default(), NoPrecompiles::default(), ); - let tx = TxEnvExt::default(); // Exercise all direct message-field loads plus host and memory builtins. let message = MessageExt { gas_limit: 100_000, @@ -928,17 +916,17 @@ mod custom_host_tests { ])), ..Default::default() }; - let mut interpreted = Interpreter::::new(&tx, &message); + let mut interpreted = Interpreter::::new(&message); assert_eq!(interpreted.run(&mut host).unwrap(), InstrStop::Return); let expected_output = interpreted.output().to_vec(); let expected_gas = interpreted.gas(); - let mut compiled = Interpreter::::new(&tx, &message); + let mut compiled = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut compiled, &mut host), Some(InstrStop::Return)); assert_eq!(compiled.output(), expected_output); assert_eq!(compiled.gas(), expected_gas); let wrong = JitBackend::new(RuntimeConfig { blocking: true, ..Default::default() }).unwrap(); - let mut fresh = Interpreter::::new(&tx, &message); + let mut fresh = Interpreter::::new(&message); assert_eq!(run_interpreter(&wrong, &mut fresh, &mut host), None); assert_eq!(fresh.run(&mut host).unwrap(), InstrStop::Return); assert_eq!(fresh.output(), expected_output); @@ -1069,16 +1057,15 @@ mod custom_host_tests { NoPrecompiles::default(), ) }; - let tx = Box::leak(Box::new(TxEnvExt::default())); let message = Box::leak(Box::new(MessageExt { gas_limit: 100_000, caller: Address::repeat_byte(0x11), code: Bytecode::new_legacy(Bytes::from_static(code)), ..Default::default() })); - let mut interpreted = Interpreter::::new(tx, message); + let mut interpreted = Interpreter::::new(message); let stop = interpreted.run(&mut new_host()).unwrap(); - let mut compiled = Interpreter::::new(tx, message); + let mut compiled = Interpreter::::new(message); let compiled_stop = run_interpreter(&backend, &mut compiled, &mut new_host()); (compiled_stop, compiled, stop, interpreted) } From cbb7c7cc8d771976953b97c719f76f1fc4a89bd2 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:11:43 +0100 Subject: [PATCH 008/159] fix(jit): bump ABI_VERSION for the Interpreter layout change (#869) fix(jit): bump ABI_VERSION for the Interpreter layout change --- crates/jit/codegen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index 1bd60d924..6577bb464 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -19,7 +19,7 @@ mod linker; pub use linker::{Linker, shared_library_path}; /// ABI version of compiled artifacts. Bump when the calling convention or execution layout changes. -pub const ABI_VERSION: u32 = 3; +pub const ABI_VERSION: u32 = 4; /// Internal tests and testing utilities. Not public API. #[cfg(test)] From a09d0999c7c94689254f821d55db6bc11fa457c4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:55:33 +0100 Subject: [PATCH 009/159] test(eest): run the tests@v21.0.1 execution-specs fixtures (#860) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switches the default EEST fixtures from the glamsterdam devnet-8 release (`tests-glamsterdam-devnet@v8.1.4`) to the mainline execution-specs release `tests@v21.0.1`, the first mainline release that includes Amsterdam. It is the fixture version go-ethereum master pins. The setup script now extracts it to `test-fixtures/eest` and leaves out the engine, sync and transaction test formats, which the runner does not execute. `EEST_VERSION`, `EEST_TAR` and `EEST_BASE_URL` replace the `DEVNET_*` overrides, and `EVM2_STATETEST_EEST_ONLY` replaces `EVM2_STATETEST_DEVNET_ONLY`. The unused `devnet` branches of the CI fixture step are removed. Of 23368 tests in the new roots, 18 fail. They are skipped here, each with a comment, so the suite stays green: | Skipped fixtures | Cause | | --- | --- | | `frontier/validation/transaction/unrecoverable_signature.json` (15 forks) | The state-test runner takes the sender from the fixture and recovers no signatures, like the already skipped `bad_v_r_s.json`. | | `eip8037…/block_2d_gas_accounting/tx_total_gas_limit_cap.json` | The Amsterdam total gas cap of 2^32-1 is not enforced yet. The PR that enforces it deletes the entry. | | `eip8037…/state_gas_system_calls/system_call_execution_boundary.json`, `eip7928…/block_access_lists_cross_index/bal_pre_execution_call_failure_keeps_read_drops_write.json` | The runner treats a failed EIP-4788 call as an invalid block; the spec runs it unchecked. A follow-up fixes the runner. | With the skips, the interpreter run over the new roots plus the legacy suites passes 28468 tests. The larger corpus also compiles more distinct programs in the JIT run than a default `vm.max_map_count` allows, so the last compilations failed and fell back to the interpreter. The compiled runs now cap resident programs with an 8 MiB code budget, which keeps about 7000 resident. --- .github/workflows/ci.yml | 16 +------ AGENTS.md | 17 ++++--- crates/eest/README.md | 17 +++---- crates/eest/src/blockchaintest/env.rs | 6 +-- crates/eest/src/blockchaintest/runner.rs | 5 +++ crates/eest/src/env.rs | 10 ++--- crates/eest/src/fixtures.rs | 4 +- crates/eest/src/jit.rs | 4 ++ crates/eest/src/runner.rs | 5 +++ crates/jit/AGENTS.md | 4 +- scripts/eest.sh | 2 +- scripts/setup_test_fixtures.py | 56 +++++++++++++----------- 12 files changed, 75 insertions(+), 71 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9a753bce..96ad1563a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -107,22 +107,12 @@ jobs: if: ${{ matrix.kind == 'eest' }} - name: Download EEST fixtures if: ${{ matrix.kind == 'eest' }} - shell: bash - env: - INPUT_FIXTURE: ${{ matrix.fixture }} - run: | - if [[ "${INPUT_FIXTURE}" == "devnet" ]]; then - export DEVNET_VERSION="tests-glamsterdam-devnet%40v8.1.4" - export DEVNET_TAR="fixtures_glamsterdam-devnet.tar.gz" - export EVM2_STATETEST_DEVNET_ONLY=1 - fi - ./scripts/setup_test_fixtures.py + run: ./scripts/setup_test_fixtures.py - name: ${{ matrix.kind }} if: ${{ !matrix.command || matrix.command == 'nextest' }} shell: bash env: INPUT_CXX: ${{ matrix.cxx }} - INPUT_FIXTURE: ${{ matrix.fixture }} INPUT_FLAGS: ${{ matrix.flags }} INPUT_KIND: ${{ matrix.kind }} INPUT_OS: ${{ matrix.os }} @@ -144,10 +134,6 @@ jobs: --exclude evm2-jit-* ) fi - if [[ "${INPUT_FIXTURE}" == "devnet" ]]; then - export EVM2_STATETEST_ROOT="test-fixtures/devnet/state_tests" - export EVM2_BLOCKCHAINTEST_ROOT="test-fixtures/devnet/blockchain_tests" - fi cargo nextest --profile ci run "${package_flags[@]}" --no-fail-fast "${matrix_flags[@]}" "${extra_flags[@]}" # Fetch on the protected host; cross containers do not inherit Aegis routing. - name: Fetch cross-test dependencies through Aegis diff --git a/AGENTS.md b/AGENTS.md index eb52af7e9..a1e9c49c3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,15 +38,14 @@ EVM2_DISPATCH_BACKEND=packed cargo nextest run -p evm2-eest --test eest --ignore `./scripts/setup_test_fixtures.py` downloads fixtures into `test-fixtures/`. If fixtures are already available in another worktree, symlink `test-fixtures` to that directory instead of re-downloading them. -By default it downloads the glamsterdam (Amsterdam) devnet fixtures plus legacy -Cancun/Constantinople state tests. The devnet release defaults to -`tests-glamsterdam-devnet@v8.1.4` / `fixtures_glamsterdam-devnet.tar.gz` -(from the `ethereum/execution-specs` repo, base overridable via `DEVNET_BASE_URL`); -override `DEVNET_VERSION` and `DEVNET_TAR` to select a different devnet release, or -clear either to disable devnet. The glamsterdam devnet fixtures cover -frontier..amsterdam, so the EEST `main` develop suite is now opt-in via -`EVM2_STATETEST_MAIN=1` (stable with `EVM2_STATETEST_STABLE=1`). Add -`EVM2_STATETEST_DEVNET_ONLY=1` to skip legacy fixtures. Use `EVM2_STATETEST_ROOT` +By default it downloads the mainline execution-specs fixtures plus legacy +Cancun/Constantinople state tests. The release defaults to +`tests@v21.0.1` / `fixtures.tar.gz` (from the `ethereum/execution-specs` repo, +base overridable via `EEST_BASE_URL`); override `EEST_VERSION` and `EEST_TAR` +to select a different release (for example a devnet), or clear either to disable +it. The release covers frontier..amsterdam, so the EEST `main` develop suite is +opt-in via `EVM2_STATETEST_MAIN=1` (stable with `EVM2_STATETEST_STABLE=1`). Add +`EVM2_STATETEST_EEST_ONLY=1` to skip legacy fixtures. Use `EVM2_STATETEST_ROOT` or `EVM2_BLOCKCHAINTEST_ROOT` for a single explicit root. Compiled EEST runs use a default subset when the full corpus is too expensive: diff --git a/crates/eest/README.md b/crates/eest/README.md index 173e41a94..fa65d56c2 100644 --- a/crates/eest/README.md +++ b/crates/eest/README.md @@ -9,7 +9,7 @@ test-fixtures/ ├── main/ │ ├── stable/{state_tests,blockchain_tests}/... │ └── develop/{state_tests,blockchain_tests}/... -├── devnet/ +├── eest/ │ └── {state_tests,blockchain_tests}/... └── legacytests/ ├── Cancun/GeneralStateTests/... @@ -22,16 +22,17 @@ Populate it with: ./scripts/setup_test_fixtures.py ``` -By default this downloads the glamsterdam devnet fixtures plus the legacy -Cancun and Constantinople state tests. Set `EVM2_STATETEST_MAIN=1` to also +By default this downloads the mainline execution-specs release fixtures +(`tests@v21.0.1`, Frontier through Amsterdam) plus the legacy Cancun and +Constantinople state tests. Set `EVM2_STATETEST_MAIN=1` to also download the EEST `main` develop suite (stable with `EVM2_STATETEST_STABLE=1`), -or `EVM2_STATETEST_DEVNET_ONLY=1` to skip the legacy fixtures. +or `EVM2_STATETEST_EEST_ONLY=1` to skip the legacy fixtures. The legacy fixtures come from the `ethereum/legacytests` commit that the `ethereum/tests` v17.2 `LegacyTests` submodule pins; override it with -`LEGACY_VERSION`. The runner fails when a default devnet or legacy root is +`LEGACY_VERSION`. The runner fails when a default execution-specs or legacy root is missing, unless the other suite got an explicit root. The `main` roots are -optional. If you clear `DEVNET_VERSION`, set `EVM2_STATETEST_ROOT` and +optional. If you clear `EEST_VERSION`, set `EVM2_STATETEST_ROOT` and `EVM2_BLOCKCHAINTEST_ROOT`. Run all discovered EEST fixtures with the single `eest` test binary: @@ -89,7 +90,7 @@ Run all discovered state tests with nextest: cargo nextest run -p evm2-eest --test eest --ignore-default-filter statetests ``` -This runs every downloaded state-test root: `devnet/state_tests`, the legacy +This runs every downloaded state-test root: `eest/state_tests`, the legacy Cancun and Constantinople state tests, and `main/develop/state_tests` when the `main` suite has been downloaded. EEST develop includes the stable fixtures, so stable is not run separately. @@ -105,7 +106,7 @@ Run all discovered blockchain tests with nextest: cargo nextest run -p evm2-eest --test eest --ignore-default-filter blockchain_tests ``` -This runs every downloaded blockchain-test root: `devnet/blockchain_tests`, +This runs every downloaded blockchain-test root: `eest/blockchain_tests`, plus `main/develop/blockchain_tests` when the `main` suite has been downloaded. Transition forks are skipped. Blocks with a `blockAccessList` build the block access list and check it against the fixture, including the header hash. diff --git a/crates/eest/src/blockchaintest/env.rs b/crates/eest/src/blockchaintest/env.rs index e92c87a79..bd070afbe 100644 --- a/crates/eest/src/blockchaintest/env.rs +++ b/crates/eest/src/blockchaintest/env.rs @@ -60,9 +60,9 @@ pub(crate) fn default_blockchain_test_roots() -> Vec { }; let mut roots = vec![BlockchainTestRoot { - name: "blockchain_tests::devnet".to_string(), - label: "execution-spec-tests devnet", - path: fixtures.join("devnet/blockchain_tests"), + name: "blockchain_tests::eest".to_string(), + label: "execution-specs release", + path: fixtures.join("eest/blockchain_tests"), }]; if main_path.is_dir() { diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index 6acb5b4f5..b9c0b47d2 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -210,6 +210,11 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "prague/eip7251_consolidations", "prague/eip7685_general_purpose_el_requests", + // A failed EIP-4788 or EIP-2935 system call does not invalidate the block, but the runner treats + // every failed system call as an invalid block. + "eip8037_state_creation_gas_cost_increase/state_gas_system_calls/system_call_execution_boundary.json", + "eip7928_block_level_access_lists/block_access_lists_cross_index/bal_pre_execution_call_failure_keeps_read_drops_write.json", + // This fixture has a block gas limit below the transaction intrinsic gas and belongs to block validation. "static/state_tests/stEIP1559/lowGasLimit.json", ]; diff --git a/crates/eest/src/env.rs b/crates/eest/src/env.rs index eea444fa2..05dcc8724 100644 --- a/crates/eest/src/env.rs +++ b/crates/eest/src/env.rs @@ -1,7 +1,7 @@ use crate::{ blockchaintest::env::explicit_blockchain_test_root_from_env, fixtures::{ - DEVNET_ONLY_ENV, EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, + EEST_ONLY_ENV, EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, fixtures_root, workspace_relative, workspace_root, }, harness::TestRoot, @@ -65,9 +65,9 @@ pub(crate) fn default_state_test_roots() -> Vec { fixtures.join("main/develop/state_tests") }; let mut roots = vec![StateTestRoot { - name: "statetests::devnet".to_string(), - label: "execution-spec-tests devnet", - path: fixtures.join("devnet/state_tests"), + name: "statetests::eest".to_string(), + label: "execution-specs release", + path: fixtures.join("eest/state_tests"), }]; if eest_path.is_dir() { @@ -78,7 +78,7 @@ pub(crate) fn default_state_test_roots() -> Vec { }); } - if !env_flag(DEVNET_ONLY_ENV) { + if !env_flag(EEST_ONLY_ENV) { roots.push(StateTestRoot { name: "legacy::cancun".to_string(), label: "legacy Cancun", diff --git a/crates/eest/src/fixtures.rs b/crates/eest/src/fixtures.rs index 56aa86bd3..a334b122f 100644 --- a/crates/eest/src/fixtures.rs +++ b/crates/eest/src/fixtures.rs @@ -10,8 +10,8 @@ pub(crate) const TEST_FIXTURES_ENV: &str = "EVM2_TEST_FIXTURES"; /// Generic EEST stable selector. pub(crate) const EEST_STABLE_ENV: &str = "EVM2_EEST_STABLE"; -/// Selector for devnet-only runs, where the setup script skips the legacy fixtures. -pub(crate) const DEVNET_ONLY_ENV: &str = "EVM2_STATETEST_DEVNET_ONLY"; +/// Selector for execution-specs-only runs, where the setup script skips the legacy fixtures. +pub(crate) const EEST_ONLY_ENV: &str = "EVM2_STATETEST_EEST_ONLY"; /// Repo-relative fixture root used by the setup script and CI. pub(crate) const DEFAULT_FIXTURES_PATH: &str = "test-fixtures"; diff --git a/crates/eest/src/jit.rs b/crates/eest/src/jit.rs index 16bb02b82..b65535f51 100644 --- a/crates/eest/src/jit.rs +++ b/crates/eest/src/jit.rs @@ -22,6 +22,10 @@ pub(crate) fn make_backend(aot: bool) -> Result { jit_worker_count: cpus, jit_opt_level: OptimizationLevel::None, aot_opt_level: OptimizationLevel::None, + // Every resident program holds memory mappings, and the full corpus compiles more + // distinct programs than a default `vm.max_map_count` allows. A small budget evicts old + // programs. + resident_code_cache_bytes: 8 * 1024 * 1024, ..Default::default() }, ..Default::default() diff --git a/crates/eest/src/runner.rs b/crates/eest/src/runner.rs index b51d73505..266d0d6b9 100644 --- a/crates/eest/src/runner.rs +++ b/crates/eest/src/runner.rs @@ -140,7 +140,12 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ // The state-test runner takes the sender from the fixture and does not recover or // validate legacy-tx signatures, so v/r/s and chain-id rejection tests cannot pass. "frontier/validation/transaction/bad_v_r_s.json", + "frontier/validation/transaction/unrecoverable_signature.json", "london/eip1559_fee_market_change/tx_type/invalid_chain_id.json", + + // The Amsterdam total transaction gas cap of 2^32-1 (EIP-8037) is not enforced yet. The PR that + // enforces it removes this entry. + "eip8037_state_creation_gas_cost_increase/block_2d_gas_accounting/tx_total_gas_limit_cap.json", ]; fn should_ignore(name: &str) -> bool { diff --git a/crates/jit/AGENTS.md b/crates/jit/AGENTS.md index 3c0991fa7..848306393 100644 --- a/crates/jit/AGENTS.md +++ b/crates/jit/AGENTS.md @@ -8,8 +8,8 @@ Use the root commands for lint, format, and docs. Targeted JIT commands: cargo nextest run -p evm2-jit-codegen --test codegen # compiler tests EVM2_JIT_TEST_DUMP=1 cargo nextest run -p evm2-jit-codegen --test codegen "test_name" # dump single compiler test cargo nextest run -p evm2-jit-runtime # runtime tests -cargo st "statetests::devnet::jit" # EEST JIT state tests -cargo st "blockchain_tests::devnet::jit" # EEST JIT blockchain tests +cargo st "statetests::eest::jit" # EEST JIT state tests +cargo st "blockchain_tests::eest::jit" # EEST JIT blockchain tests ``` ## Architecture diff --git a/scripts/eest.sh b/scripts/eest.sh index c9ae1e29c..e2457a7a2 100755 --- a/scripts/eest.sh +++ b/scripts/eest.sh @@ -8,7 +8,7 @@ # fixture found under it runs as one auto-detecting suite (state vs blockchain # kind is detected per file), so no test-name filter is needed. Extra arguments # are forwarded to `cargo nextest run`, for example: -# scripts/eest.sh ./test-fixtures/devnet/state_tests -E 'test(precompile)' +# scripts/eest.sh ./test-fixtures/eest/state_tests -E 'test(precompile)' set -euo pipefail if [[ $# -lt 1 ]]; then diff --git a/scripts/setup_test_fixtures.py b/scripts/setup_test_fixtures.py index ef6397f85..c24001437 100755 --- a/scripts/setup_test_fixtures.py +++ b/scripts/setup_test_fixtures.py @@ -39,16 +39,15 @@ # own repository. LEGACY_VERSION = os.environ.get("LEGACY_VERSION", "1f581b8ccdc4c63acf5f2c5c1b155c690c32a8eb") BASE_URL = "https://github.com/ethereum/execution-spec-tests/releases/download" -# Fork-specific devnet fixtures are published from the execution-specs repo. -DEVNET_BASE_URL = os.environ.get( - "DEVNET_BASE_URL", +# Mainline execution-specs fixtures cover Frontier..Amsterdam. The release is a URL path component, so +# the `@` is percent-encoded. Override EEST_VERSION/EEST_TAR/EEST_BASE_URL to select another release, +# for example a fork-specific devnet release. +EEST_BASE_URL = os.environ.get( + "EEST_BASE_URL", "https://github.com/ethereum/execution-specs/releases/download", ) -# Default fork-specific devnet fixtures: glamsterdam (Amsterdam) devnet-8. Downloaded by default so -# the Amsterdam tests run without extra configuration; override DEVNET_VERSION/DEVNET_TAR to select a -# different devnet release. The version is a URL path component, so the `@` is percent-encoded. -DEFAULT_DEVNET_VERSION = "tests-glamsterdam-devnet%40v8.1.4" -DEFAULT_DEVNET_TAR = "fixtures_glamsterdam-devnet.tar.gz" +DEFAULT_EEST_VERSION = "tests%40v21.0.1" +DEFAULT_EEST_TAR = "fixtures.tar.gz" LEGACY_URL = f"https://github.com/ethereum/legacytests/archive/{LEGACY_VERSION}.tar.gz" SCRIPT_DIR = Path(__file__).resolve().parent @@ -57,7 +56,7 @@ MAIN_STABLE_DIR = FIXTURES_DIR / "main" / "stable" MAIN_DEVELOP_DIR = FIXTURES_DIR / "main" / "develop" -DEVNET_DIR = FIXTURES_DIR / "devnet" +EEST_DIR = FIXTURES_DIR / "eest" LEGACY_DIR = FIXTURES_DIR / "legacytests" console = Console() @@ -87,6 +86,8 @@ class Fixture: url: str dest: Path exists_paths: tuple[Path, ...] + # tar member patterns to skip while extracting. + excludes: tuple[str, ...] = () def env_flag(name: str) -> bool: @@ -124,37 +125,39 @@ def legacy_fixture() -> Fixture: ) -def devnet_fixture() -> Fixture | None: - version = os.environ.get("DEVNET_VERSION", DEFAULT_DEVNET_VERSION) - tar_name = os.environ.get("DEVNET_TAR", DEFAULT_DEVNET_TAR) +def eest_fixture() -> Fixture | None: + version = os.environ.get("EEST_VERSION", DEFAULT_EEST_VERSION) + tar_name = os.environ.get("EEST_TAR", DEFAULT_EEST_TAR) - # An explicitly cleared version or tar disables the devnet fixtures. + # An explicitly cleared version or tar disables the execution-specs fixtures. if not version or not tar_name: return None return Fixture( - label="devnet", - url=f"{DEVNET_BASE_URL}/{version}/{tar_name}", - dest=DEVNET_DIR, - exists_paths=(DEVNET_DIR / "state_tests", DEVNET_DIR / "blockchain_tests"), + label="eest", + url=f"{EEST_BASE_URL}/{version}/{tar_name}", + dest=EEST_DIR, + exists_paths=(EEST_DIR / "state_tests", EEST_DIR / "blockchain_tests"), + # The runner executes only the state and blockchain test formats. + excludes=("*/blockchain_tests_engine*", "*/blockchain_tests_sync", "*/transaction_tests"), ) def fixture_plan() -> list[Fixture]: fixtures: list[Fixture] = [] - # The glamsterdam devnet fixtures cover frontier..amsterdam, superseding the main develop suite - # by fork coverage, so main develop/stable is now opt-in via EVM2_STATETEST_MAIN. + # The execution-specs release covers frontier..amsterdam, superseding the main develop suite by + # fork coverage, so main develop/stable is opt-in via EVM2_STATETEST_MAIN. if env_flag("EVM2_STATETEST_MAIN"): fixtures.append(main_fixture()) - # Legacy hand-written GeneralStateTests, unless a devnet-only run was requested. - if not env_flag("EVM2_STATETEST_DEVNET_ONLY"): + # Legacy hand-written GeneralStateTests, unless an execution-specs-only run was requested. + if not env_flag("EVM2_STATETEST_EEST_ONLY"): fixtures.append(legacy_fixture()) - # Glamsterdam (Amsterdam) devnet fixtures: the default suite. - if devnet := devnet_fixture(): - fixtures.append(devnet) + # Mainline execution-specs fixtures: the default suite. + if eest := eest_fixture(): + fixtures.append(eest) return fixtures @@ -192,6 +195,7 @@ async def pipe_to_tar( "tar", "xzf", "-", + *(f"--exclude={pattern}" for pattern in fixture.excludes), "--strip-components=1", "-C", str(fixture.dest), @@ -354,8 +358,8 @@ def print_summary() -> None: MAIN_STABLE_DIR / "blockchain_tests", MAIN_DEVELOP_DIR / "state_tests", MAIN_DEVELOP_DIR / "blockchain_tests", - DEVNET_DIR / "state_tests", - DEVNET_DIR / "blockchain_tests", + EEST_DIR / "state_tests", + EEST_DIR / "blockchain_tests", LEGACY_DIR / "Cancun" / "GeneralStateTests", LEGACY_DIR / "Constantinople" / "GeneralStateTests", ]: From 0b75a2bb4bb3fd41b6db14ef6d9a1db4f4c8873a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:07 +0100 Subject: [PATCH 010/159] feat(inspectors): add the keccak256PreimageTracer (#867) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geth's `keccak256PreimageTracer` returns a JSON object that maps the hash of every KECCAK256 operation of a transaction to its preimage (`eth/tracers/native/keccak256_preimage.go`). evm2 did not know the tracer: `debug_traceTransaction` with it, standalone or inside a `muxTracer`, treated the name as JavaScript and failed with `ReferenceError: keccak256PreimageTracer is not defined`. `Keccak256PreimageInspector` records the preimage on each KECCAK256 step with geth's operand rules: offset and size are truncated to 64 bits, a range that reaches past memory is zero padded, and a range that reaches more than 1 MiB past the end of memory is skipped. `alloy-rpc-types-trace` has no built-in variant for the tracer, so the name arrives as `GethDebugTracerType::JsTracer`. Like geth's tracer directory, `DebugInspector` and `MuxInspector` look the name up before they evaluate the string as JavaScript, and the result is a `GethTrace::JS` object. geth ignores the tracer config, and so does this. Like geth, it records a KECCAK256 that then fails, and `MEMORY_PAD_LIMIT` (1 MiB, geth's `memoryPadLimit`) bounds the zero padding per operation: a preimage that needs more padding is skipped, as geth does. geth also retains every preimage without a limit, which a transaction can use to make the tracer hold gigabytes; the inspector counts the retained bytes against `TraceLimits`, 64 MiB by default (`DEFAULT_MAX_RECORDED_BYTES`), and stops recording once the limit is reached. The output therefore differs from geth only after 64 MiB of retained preimages. `limit_exceeded()` reports it, and `json_result()` then fails with `LimitExceeded` instead of returning a truncated map: `DebugInspector` and `MuxInspector` return it as an error, and `MuxInspector::try_into_mux_frame` now returns the mux `Error`, which has a database variant. Checked against go-ethereum master `a0cba163` with the parity harness of the `dani/geth-parity-harness` branch (a new case with an in-memory, an empty, a padded, a past-memory and an oversized range on Cancun, Prague, Osaka and Amsterdam, run standalone and in the mux config of the harness with the tracer added): | Output of `keccak256PreimageTracer` | geth master | origin/main | this PR | | --- | --- | --- | --- | | standalone, the new case on 4 forks | preimage map | 🔴 `ReferenceError: keccak256PreimageTracer is not defined` | 🟢 identical | | inside a mux, all 311 case and fork pairs of the harness | preimage map | 🔴 the mux rejects the tracer | 🟢 identical | The harness is not merged yet, so its case is not part of this PR; I will add it once it lands. Rust tests cover the zero padding, the skipped oversize range, the padding limit and the byte above it, the byte limit, and the mux key. --- crates/inspectors/src/tracing/debug.rs | 22 +++-- crates/inspectors/src/tracing/keccak.rs | 125 ++++++++++++++++++++++++ crates/inspectors/src/tracing/mod.rs | 6 ++ crates/inspectors/src/tracing/mux.rs | 39 ++++++-- crates/inspectors/tests/it/geth.rs | 124 ++++++++++++++++++++++- 5 files changed, 302 insertions(+), 14 deletions(-) create mode 100644 crates/inspectors/src/tracing/keccak.rs diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index da5707b39..52b25441c 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -1,6 +1,6 @@ use crate::tracing::{ - FourByteInspector, MuxInspector, TracingInspector, TracingInspectorConfig, TransactionContext, - final_refunded, + FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MuxInspector, + TracingInspector, TracingInspectorConfig, TransactionContext, final_refunded, }; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; @@ -48,6 +48,8 @@ pub enum DebugInspector { FlatCallTracer(TracingInspector), /// Erc7562Tracer Erc7562Tracer(TracingInspector, Erc7562Config), + /// Keccak256PreimageTracer + Keccak256Preimage(Keccak256PreimageInspector), /// Default tracer Default(TracingInspector, GethDefaultTracingOptions), /// JS tracer @@ -71,6 +73,7 @@ impl DebugInspector { Self::Erc7562Tracer(inspector, config) => { Self::Erc7562Tracer(inspector.clone(), config.clone()) } + Self::Keccak256Preimage(inspector) => Self::Keccak256Preimage(inspector.clone()), Self::Default(inspector, config) => Self::Default(inspector.clone(), *config), #[cfg(feature = "js-tracer")] Self::Js(inspector) => Self::Js(inspector.try_clone()?.into()), @@ -159,6 +162,10 @@ impl DebugInspector { return Err(DebugInspectorError::UnsupportedTracer); } }, + // geth looks a tracer up by name before it evaluates the string as JavaScript. + GethDebugTracerType::JsTracer(name) if name == KECCAK256_PREIMAGE_TRACER => { + Self::Keccak256Preimage(Keccak256PreimageInspector::default()) + } #[cfg(not(feature = "js-tracer"))] GethDebugTracerType::JsTracer(_) => { return Err(DebugInspectorError::JsTracerNotEnabled); @@ -196,6 +203,7 @@ impl DebugInspector { | Self::FlatCallTracer(inspector) | Self::Erc7562Tracer(inspector, _) | Self::Default(inspector, _) => inspector.fuse(), + Self::Keccak256Preimage(inspector) => inspector.clear(), Self::Noop(_) | Self::StateGasTracer(_) => {} Self::Mux(inspector, config) => { *inspector = MuxInspector::try_from_config(config.clone())?; @@ -276,10 +284,7 @@ impl DebugInspector { .into(), Self::Mux(inspector, _) => { inspector.finalize_transaction(tx, &res.result); - inspector - .try_into_mux_frame(res, db, tx_info) - .map_err(DebugInspectorError::Database)? - .into() + inspector.try_into_mux_frame(res, db, tx_info)?.into() } Self::FlatCallTracer(inspector) => { inspector.recover_runtime_oog_root(tx, &res.result); @@ -301,6 +306,7 @@ impl DebugInspector { .map_err(DebugInspectorError::Database)? .into() } + Self::Keccak256Preimage(inspector) => GethTrace::JS(inspector.json_result()?), Self::Default(inspector, config) => { inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); @@ -352,6 +358,7 @@ macro_rules! delegate { Self::FlatCallTracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Erc7562Tracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::Default($insp, _) => Inspector::::$method($insp, $($arg),*), + Self::Keccak256Preimage($insp) => Inspector::::$method($insp, $($arg),*), Self::Noop($insp) => Inspector::::$method($insp, $($arg),*), Self::StateGasTracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Mux($insp, _) => Inspector::::$method($insp, $($arg),*), @@ -438,6 +445,9 @@ pub enum DebugInspectorError { /// Error from MuxInspector #[error(transparent)] MuxInspector(#[from] crate::tracing::MuxError), + /// The keccak256 preimage tracer reached its limit of recorded bytes + #[error(transparent)] + Keccak256Preimage(#[from] crate::tracing::Keccak256PreimageLimitExceeded), /// Error from JS inspector #[cfg(feature = "js-tracer")] #[error(transparent)] diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs new file mode 100644 index 000000000..fe6117d79 --- /dev/null +++ b/crates/inspectors/src/tracing/keccak.rs @@ -0,0 +1,125 @@ +//! Keccak256 preimage tracing inspector +//! +//! The keccak256PreimageTracer collects the preimage of every `KECCAK256` operation executed in +//! the lifetime of a transaction. The result is a map of hash to preimage. +//! +//! Like geth, it records an operation that goes on to fail. [`MEMORY_PAD_LIMIT`] bounds the work +//! per operation, and the [`TraceLimits`] of the inspector bound the bytes it retains, which geth +//! does not. +//! +//! See also + +use crate::tracing::{TraceLimits, limits::TraceBudget}; +use alloc::{borrow::Cow, vec}; +use alloy_primitives::{B256, Bytes, hex, keccak256, map::HashMap}; +use evm2::{ + EvmTypes, Inspector, + interpreter::{Interpreter, opcode::op}, +}; +use serde_json::Value; +use thiserror::Error; + +/// The name of the tracer in the `tracer` option of `debug_trace*` and in a mux tracer config. +pub const KECCAK256_PREIMAGE_TRACER: &str = "keccak256PreimageTracer"; + +/// The most zero padding, in bytes, a preimage that reaches past the end of memory may need. geth +/// records nothing for a preimage that needs more. +pub const MEMORY_PAD_LIMIT: u64 = 1024 * 1024; + +/// The default limit on the bytes of all recorded preimages, 64 MiB. geth has no limit. +pub const DEFAULT_MAX_RECORDED_BYTES: usize = 64 * 1024 * 1024; + +/// The result of a [`Keccak256PreimageInspector`] would miss preimages: it reached its byte limit. +#[derive(Clone, Copy, Debug, Error)] +#[error("keccak256PreimageTracer reached its limit of recorded preimage bytes")] +pub struct LimitExceeded; + +/// Inspector that records the preimage of every `KECCAK256` operation. +#[derive(Clone, Debug)] +pub struct Keccak256PreimageInspector { + /// The map of hash to preimage. + inner: HashMap, + /// The bytes of the recorded preimages. + budget: TraceBudget, +} + +impl Default for Keccak256PreimageInspector { + fn default() -> Self { + Self::new(TraceLimits::default().set_max_recorded_bytes(Some(DEFAULT_MAX_RECORDED_BYTES))) + } +} + +impl Keccak256PreimageInspector { + /// Returns an inspector that records preimages up to the given limits, which default to + /// [`DEFAULT_MAX_RECORDED_BYTES`]. + pub fn new(limits: TraceLimits) -> Self { + Self { inner: HashMap::default(), budget: TraceBudget { limits, ..Default::default() } } + } + + /// Returns whether a preimage was omitted after reaching the byte limit. + pub const fn limit_exceeded(&self) -> bool { + self.budget.exceeded() + } + + /// Clears the recorded preimages. + pub fn clear(&mut self) { + self.inner.clear(); + self.budget.recorded = 0; + self.budget.exceeded = false; + } + + /// Returns the result of the tracer: a JSON object mapping each hash to its preimage. + /// + /// Fails if the byte limit was reached, because the object would miss preimages. + pub fn json_result(&self) -> Result { + if self.budget.exceeded() { + return Err(LimitExceeded); + } + Ok(Value::Object( + self.inner + .iter() + .map(|(hash, preimage)| { + (hex::encode_prefixed(hash), hex::encode_prefixed(preimage).into()) + }) + .collect(), + )) + } +} + +impl Inspector for Keccak256PreimageInspector { + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if interp.opcode() != op::KECCAK256 || self.budget.exceeded() { + return; + } + // The stack can be too small if the operation is about to fail. + let &[.., length, offset] = interp.stack().as_slice() else { return }; + // The operands are truncated to 64 bits. + let (offset, length) = (offset.as_limbs()[0], length.as_limbs()[0]); + let memory = interp.memory().as_slice(); + if let Some(preimage) = memory_copy_padded(memory, offset, length) { + let hash = keccak256(&preimage); + if !self.inner.contains_key(&hash) && self.budget.reserve(preimage.len()) { + self.inner.insert(hash, preimage.into_owned().into()); + } + } + self.budget.halt(interp, true); + } +} + +/// Returns the `size` bytes of `memory` at `offset`, zero padded past its end. +/// +/// Returns `None` if the range reaches more than [`MEMORY_PAD_LIMIT`] past the end of memory. +fn memory_copy_padded(memory: &[u8], offset: u64, size: u64) -> Option> { + let end = offset.saturating_add(size); + if end.saturating_sub(memory.len() as u64) > MEMORY_PAD_LIMIT { + return None; + } + if let Some(preimage) = memory.get(offset as usize..end as usize) { + return Some(Cow::Borrowed(preimage)); + } + let mut preimage = vec![0; size as usize]; + if let Some(overlap) = memory.get(offset as usize..) { + preimage[..overlap.len()].copy_from_slice(overlap); + } + Some(Cow::Owned(preimage)) +} diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 0312203b5..59824c776 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -40,6 +40,12 @@ pub use config::{OpcodeFilter, StackSnapshotType, TracingInspectorConfig}; mod fourbyte; pub use fourbyte::FourByteInspector; +mod keccak; +pub use keccak::{ + DEFAULT_MAX_RECORDED_BYTES, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, + LimitExceeded as Keccak256PreimageLimitExceeded, MEMORY_PAD_LIMIT, +}; + mod limits; use limits::TraceBudget; pub use limits::{TraceLimitBehavior, TraceLimits}; diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index 66deafd1f..0280f4366 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -1,16 +1,19 @@ -use crate::tracing::{FourByteInspector, TracingInspector, TracingInspectorConfig, final_refunded}; +use crate::tracing::{ + FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, + Keccak256PreimageLimitExceeded, TracingInspector, TracingInspectorConfig, final_refunded, +}; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256, map::HashMap}; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, - NoopFrame, PreStateConfig, StateGasTrace, + GethTrace, NoopFrame, PreStateConfig, StateGasTrace, mux::{MuxConfig, MuxFrame}, }; use evm2::{ - EvmTypes, EvmTypesHost, Inspector, TxResultExt, TxResultWithState, - evm::{DbResult, DynDatabase}, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, TxResultExt, TxResultWithState, + evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, }; use thiserror::Error; @@ -20,6 +23,8 @@ use thiserror::Error; pub struct MuxInspector { /// An instance of FourByteInspector that can be reused four_byte: Option, + /// An instance of Keccak256PreimageInspector that can be reused + keccak256_preimage: Option, /// An instance of TracingInspector that can be reused tracing: Option, /// Configurations for different Geth trace types @@ -40,6 +45,7 @@ impl MuxInspector { /// Try creating a new instance of [MuxInspector] from the given [MuxConfig]. pub fn try_from_config(config: MuxConfig) -> Result { let mut four_byte = None; + let mut keccak256_preimage = None; let mut inspector_config = TracingInspectorConfig::none(); let mut configs = Vec::new(); @@ -47,6 +53,10 @@ impl MuxInspector { for (tracer_type, tracer_config) in config.0 { let builtin = match tracer_type { GethDebugTracerType::BuiltInTracer(b) => b, + GethDebugTracerType::JsTracer(name) if name == KECCAK256_PREIMAGE_TRACER => { + keccak256_preimage = Some(Keccak256PreimageInspector::default()); + continue; + } _ => return Err(Error::UnsupportedTracerType(tracer_type)), }; #[allow(unreachable_patterns)] @@ -95,7 +105,7 @@ impl MuxInspector { let tracing = (!configs.is_empty()).then(|| TracingInspector::new(inspector_config)); - Ok(Self { four_byte, tracing, configs }) + Ok(Self { four_byte, keccak256_preimage, tracing, configs }) } /// Sets the index the next recorded log receives, see @@ -112,7 +122,7 @@ impl MuxInspector { result: &TxResultWithState, db: &mut dyn DynDatabase, tx_info: TransactionInfo, - ) -> DbResult { + ) -> Result { let mut frame = HashMap::with_capacity_and_hasher(self.configs.len(), Default::default()); for (tracer_type, config) in &self.configs { @@ -170,6 +180,14 @@ impl MuxInspector { ); } + // Add keccak256 preimage trace if inspector exists + if let Some(inspector) = &self.keccak256_preimage { + frame.insert( + GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()), + GethTrace::JS(inspector.json_result()?), + ); + } + Ok(MuxFrame(frame)) } @@ -203,6 +221,9 @@ impl Inspector for MuxInspector { if let Some(ref mut inspector) = self.four_byte { inspector.step(interp); } + if let Some(ref mut inspector) = self.keccak256_preimage { + inspector.step(interp); + } if let Some(ref mut inspector) = self.tracing { inspector.step(interp); } @@ -318,6 +339,12 @@ pub enum Error { /// Unsupported tracer type (e.g. JS tracer) #[error("unsupported tracer type: '{0:?}'")] UnsupportedTracerType(GethDebugTracerType), + /// A tracer reached its limit of recorded bytes + #[error(transparent)] + Keccak256Preimage(#[from] Keccak256PreimageLimitExceeded), + /// Database operation failed + #[error("database error {0:?}")] + Database(#[from] DatabaseError), /// Error when deserializing the config #[error("error deserializing config: {0}")] InvalidConfig(#[from] serde_json::Error), diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index c0147e4e6..1c84c01fa 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -7,7 +7,7 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, ETH_TRANSFER_LOG_ADDRESS, EmptyDB, SpecId, TransactTo, TxEnv, deploy_contract, op, }; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, map::HashMap}; +use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, keccak256, map::HashMap}; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, @@ -19,7 +19,8 @@ use evm2::{ ethereum::ethereum_tx_registry, }; use evm2_inspectors::tracing::{ - DebugInspector, MuxInspector, TracingInspector, TracingInspectorConfig, + DebugInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MEMORY_PAD_LIMIT, + MuxInspector, TraceLimits, TracingInspector, TracingInspectorConfig, }; #[test] @@ -2192,3 +2193,122 @@ fn test_geth_failed_root_logs() { } } } + +fn contract_with_code(code: &[u8]) -> (Address, CacheDB) { + let contract = Address::with_last_byte(0x42); + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), + ); + (contract, db) +} + +#[test] +fn keccak256_preimage_tracer_collects_padded_preimages() { + let (contract, db) = contract_with_code(&hex!( + // MSTORE(0, 0x2a), then KECCAK256 of (0, 32), of (0, 0), of (0x10, 0x40) and of (0, 2 MiB). + "602a5f52" "60205f2050" "5f5f2050" "6040601020" "50" "62200000" "5f2050" + )); + let mut inspector = Keccak256PreimageInspector::default(); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 10_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let mut word = [0; 32]; + word[31] = 0x2a; + let mut padded = [0; 0x40]; + padded[..0x10].copy_from_slice(&word[0x10..]); + let expected = serde_json::Value::Object( + [&word[..], &[], &padded] + .into_iter() + .map(|preimage| { + (hex::encode_prefixed(keccak256(preimage)), hex::encode_prefixed(preimage).into()) + }) + .collect(), + ); + assert_eq!(inspector.json_result().unwrap(), expected); +} + +#[test] +fn keccak256_preimage_tracer_runs_standalone_and_in_a_mux() { + let options: GethDebugTracingOptions = + serde_json::from_value(serde_json::json!({"tracer": "keccak256PreimageTracer"})).unwrap(); + assert!(matches!(DebugInspector::new(options).unwrap(), DebugInspector::Keccak256Preimage(_))); + + let (contract, db) = contract_with_code(&hex!("602a5f5260205f2050")); + let key = GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()); + let config = MuxConfig(HashMap::from_iter([(key.clone(), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let frame = inspector + .try_into_mux_frame(&res.tx_result, ctx.db_mut(), TransactionInfo::default()) + .unwrap(); + let GethTrace::JS(preimages) = &frame.0[&key] else { panic!("expected a JSON object") }; + assert_eq!(preimages.as_object().unwrap().len(), 1); +} + +#[test] +fn keccak256_preimage_tracer_bounds_the_padding_like_geth() { + // KECCAK256 of `MEMORY_PAD_LIMIT` and of one byte more at offset 0 of empty memory. + for (size, recorded) in [(MEMORY_PAD_LIMIT, 1), (MEMORY_PAD_LIMIT + 1, 0)] { + // PUSH3 size, PUSH0, KECCAK256. + let code = [&[0x62][..], &size.to_be_bytes()[5..], &hex!("5f20")].concat(); + let (contract, db) = contract_with_code(&code); + let mut inspector = Keccak256PreimageInspector::default(); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 10_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert_eq!(inspector.json_result().unwrap().as_object().unwrap().len(), recorded); + } +} + +#[test] +fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { + // MSTORE(0, 0x2a), then KECCAK256 of 32 and of 64 bytes. + let code = hex!("602a5f52" "60205f2050" "60405f2050"); + for (limit, recorded, exceeded) in [(40, 1, true), (96, 2, false)] { + let (contract, db) = contract_with_code(&code); + let mut inspector = Keccak256PreimageInspector::new( + TraceLimits::default().set_max_recorded_bytes(Some(limit)), + ); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert_eq!(inspector.limit_exceeded(), exceeded); + match inspector.json_result() { + Ok(preimages) => { + assert_eq!((preimages.as_object().unwrap().len(), exceeded), (recorded, false)) + } + Err(_) => assert!(exceeded), + } + } +} From 1deb08b39c07be97459c07ae324fd1b9db4e07d6 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:16 +0100 Subject: [PATCH 011/159] fix(inspectors): require five stack items to read a call target (#863) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `AccessListInspector` read the target of CALL, CALLCODE, DELEGATECALL and STATICCALL from the second stack item whenever two items were present. go-ethereum's access list tracer (`eth/tracers/logger/access_list_tracer.go`) reads it only when the stack holds at least five items. Its hook also runs for an opcode that is about to fail, so a call that underflows with fewer than five items must add nothing, while one that underflows with five or more items still adds the address. evm2 added whatever sat in the second slot: the address for `736000f1`, address zero for three zero pushes followed by an address and `CALL`. The inspector now reads the target only from five items. ## Parity run geth `a0cba163` (`geth --dev`, Osaka), `eth_createAccessList` with state overrides, call from `0x…1001`, 30 cases. The same inputs and geth's lists are in `crates/inspectors/tests/it/accesslist.rs` (`access_list_matches_geth`). Like geth, the test reruns the call with the list found so far, as both the transaction access list and the inspector seed, until the list stops changing. The cases cover the five-item rule for all four call opcodes (two to six items), the single-item opcodes (BALANCE, EXTCODE*, SELFDESTRUCT, SLOAD, SSTORE) with an empty or short stack, out-of-gas and static-context failures, DELEGATECALL and CALLCODE storage, reverted frames, a create transaction and a nested CREATE, a seeded request list with excluded entries, and exclusion of the sender, callee and precompiles. The other opcode branches already match geth: their guards (one item for the single-item opcodes) are the same, and the hook also fires for opcodes that fail on stack underflow or gas. | case | geth | evm2 main | this PR | | --- | --- | --- | --- | | `PUSH20 X; PUSH0; CALL` (2 items) | `[]` | `[X]` 🔴 | `[]` ✅ | | 3 pushes, `PUSH20 X`, `CALL` (4 items, second slot is zero) | `[]` | `[0x0]` 🔴 | `[]` ✅ | | STATICCALL with 4 items | `[]` | `[X]` 🔴 | `[]` ✅ | | CALLCODE with 4 items | `[]` | `[X]` 🔴 | `[]` ✅ | | CALL with 5 or 6 items, CALLCODE, STATICCALL and DELEGATECALL with 5 items (underflow) | `[X]` | `[X]` | `[X]` ✅ | | 21 other cases | | match | match ✅ | geth's raw output for the first case (`stateOverrides: {0x…c1: {code: 0x73…b0016000f1}}`, call `{from: 0x…1001, to: 0x…c1}`): ```json {"accessList":[],"error":"stack underflow (2 <=> 7)","gasUsed":"0x4c4b40"} ``` --- crates/inspectors/src/access_list.rs | 4 +- crates/inspectors/tests/it/accesslist.rs | 506 ++++++++++++++++++++++- crates/inspectors/tests/it/compat.rs | 20 +- 3 files changed, 527 insertions(+), 3 deletions(-) diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 5b7672198..cf571eef4 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -146,7 +146,9 @@ impl Inspector for AccessListInspector { } } op::DELEGATECALL | op::CALL | op::STATICCALL | op::CALLCODE => { - if let Some([_, slot]) = interp.stack().peekn() { + // `go-ethereum` reads the target once five items are on the stack, even if the call + // then underflows. + if let Some([_, slot, _, _, _]) = interp.stack().peekn() { let addr = Address::from_word(B256::from(slot.to_be_bytes())); if !self.excluded.contains(&addr) { self.touched_slots.entry(addr).or_default(); diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index 98f16de92..ad921f04b 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -4,7 +4,7 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; -use alloy_primitives::{Address, B256, Signature, U256, address, hex}; +use alloy_primitives::{Address, B256, Signature, U256, address, b256, hex}; use evm2::ethereum::TxEnvelope; use evm2_inspectors::access_list::AccessListInspector; @@ -138,3 +138,507 @@ fn test_access_list_authorization_exclusions() { // The unrecoverable authorization has no authority and therefore adds nothing. assert_eq!(excluded.len(), 2); } + +const fn a(n: u64) -> Address { + let mut bytes = [0; 20]; + let (_, low) = bytes.split_at_mut(12); + let n = n.to_be_bytes(); + let mut i = 0; + while i < 8 { + low[i] = n[i]; + i += 1; + } + Address::new(bytes) +} + +/// A call run through `go-ethereum`'s `eth_createAccessList`, and the list it returned. +struct AccessListCase { + name: &'static str, + /// Address, code and balance. + accounts: &'static [(Address, &'static [u8], u64)], + to: Option
, + input: &'static [u8], + gas_limit: u64, + /// The access list of the request. + seed: &'static [(Address, &'static [B256])], + expected: &'static [(Address, &'static [B256])], +} + +fn access_list(items: &[(Address, &[B256])]) -> AccessList { + AccessList( + items + .iter() + .map(|&(address, storage_keys)| AccessListItem { + address, + storage_keys: storage_keys.to_vec(), + }) + .collect(), + ) +} + +/// Lists returned by `go-ethereum` a0cba163 (`geth --dev`, Osaka) for the same calls. +#[test] +fn access_list_matches_geth() { + let sender = a(0x1001); + for case in CASES { + let mut db = CacheDB::::default(); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(10u128.pow(18)), ..Default::default() }, + ); + for &(address, code, balance) in case.accounts { + db.insert_account_info( + &address, + AccountInfo { + balance: U256::from(balance), + code: Some(Bytecode::new_raw(code.to_vec().into())), + ..Default::default() + }, + ); + } + let context = Context::mainnet().with_db(db); + + // Like `go-ethereum`, run again with the list found so far until it stops growing. + let mut list = access_list(case.seed); + let actual = loop { + let mut inspector = AccessListInspector::new(list.clone()); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); + evm.inspect_tx(TxEnv { + caller: sender, + gas_limit: case.gas_limit, + kind: case.to.map_or(TransactTo::Create, TransactTo::Call), + data: case.input.to_vec().into(), + nonce: 0, + access_list: list.clone(), + ..Default::default() + }) + .unwrap(); + let next = inspector.access_list(); + if next == list { + break next; + } + list = next; + }; + + assert_eq!(actual, access_list(case.expected), "{}", case.name); + } +} + +const CASES: &[AccessListCase] = &[ + AccessListCase { + name: "call_underflow_2", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0016000f1"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_underflow_4", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b001f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_underflow_6", + accounts: &[( + a(0xc1), + &hex!("600060006000600073000000000000000000000000000000000000b0016000f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "call_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "staticcall_underflow_4", + accounts: &[( + a(0xc1), + &hex!("6000600073000000000000000000000000000000000000b0016000fa"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "staticcall_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000fa"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "delegatecall_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f4"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "callcode_underflow_4", + accounts: &[( + a(0xc1), + &hex!("6000600073000000000000000000000000000000000000b0016000f2"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_ok", + accounts: &[( + a(0xc1), + &hex!("6000600060006000600073000000000000000000000000000000000000b00161fffff150"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "balance_ok", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013100"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "balance_empty_stack", + accounts: &[(a(0xc1), &hex!("31"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "extcodecopy_underflow", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013c"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sload_slot", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + )], + }, + AccessListCase { + name: "sstore_underflow", + accounts: &[(a(0xc1), &hex!("600755"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000007")], + )], + }, + AccessListCase { + name: "balance_oog", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013100"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 23453, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sload_oog", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 25353, + seed: &[], + expected: &[( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + )], + }, + AccessListCase { + name: "sstore_oog_sentry", + accounts: &[(a(0xc1), &hex!("600160095500"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 27300, + seed: &[], + expected: &[( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], + )], + }, + AccessListCase { + name: "call_oog", + accounts: &[( + a(0xc1), + &hex!("6000600060006000600073000000000000000000000000000000000000b00161fffff150"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 23481, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sstore_static", + accounts: &[ + ( + a(0xc1), + &hex!("60006000600060007300000000000000000000000000000000000000c261fffffa50"), + 0, + ), + (a(0xc2), &hex!("6001600355"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[( + a(0xc2), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000003")], + )], + }, + AccessListCase { + name: "delegatecall_sload", + accounts: &[ + ( + a(0xc1), + &hex!("60006000600060007300000000000000000000000000000000000000c261fffff450"), + 0, + ), + (a(0xc2), &hex!("60045400"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[ + ( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000004")], + ), + (a(0xc2), &[]), + ], + }, + AccessListCase { + name: "callcode_sload", + accounts: &[ + ( + a(0xc1), + &hex!("600060006000600060007300000000000000000000000000000000000000c261fffff250"), + 0, + ), + (a(0xc2), &hex!("60045400"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[ + ( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000004")], + ), + (a(0xc2), &[]), + ], + }, + AccessListCase { + name: "selfdestruct", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b001ff"), 1)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "selfdestruct_underflow", + accounts: &[(a(0xc1), &hex!("ff"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "excluded_sender_precompile", + accounts: &[( + a(0xc1), + &hex!( + "73000000000000000000000000000000000000100131507300000000000000000000000000000000000000c13150730000000000000000000000000000000000000002315073000000000000000000000000000000000000b0013100" + ), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "callcode_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f2"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "create_tx_sload", + accounts: &[], + to: None, + input: &hex!("6005545f5ff3"), + gas_limit: 5000000, + seed: &[], + expected: &[( + address!("0x3817e247023b4f489352758397040b1fd33b300a"), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + )], + }, + AccessListCase { + name: "create_nested", + accounts: &[( + a(0xc1), + &hex!("606060005360056001536054600253605f600353605f60045360f3600553600660006000f050"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[( + address!("0x724572a7f7b8eb3aa9763a66f2511269ec3764d5"), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + )], + }, + AccessListCase { + name: "seeded", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[ + ( + a(0xd1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], + ), + ( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000007")], + ), + ( + a(0x1001), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000001")], + ), + (a(0x2), &[]), + ], + expected: &[ + ( + a(0xc1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + ), + ( + a(0xd1), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], + ), + ], + }, + AccessListCase { + name: "reverted_frame_touch", + accounts: &[ + ( + a(0xc1), + &hex!("600060006000600060007300000000000000000000000000000000000000c261fffff150"), + 0, + ), + (a(0xc2), &hex!("73000000000000000000000000000000000000b001315060065460006000fd"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[ + ( + a(0xc2), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000006")], + ), + (a(0xb001), &[]), + ], + }, + AccessListCase { + name: "extcode_ops", + accounts: &[( + a(0xc1), + &hex!( + "7300000000000000000000000000000000000000e13b507300000000000000000000000000000000000000e23f506000600060007300000000000000000000000000000000000000e33c00" + ), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xe1), &[]), (a(0xe2), &[]), (a(0xe3), &[])], + }, +]; diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index 8becc1584..ce914266d 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -1,6 +1,7 @@ //! API shim so that the test files can stay as close to upstream as possible. -use alloy_consensus::{TxEip4844, TxLegacy, transaction::Recovered}; +use alloy_consensus::{TxEip2930, TxEip4844, TxLegacy, transaction::Recovered}; +use alloy_eip2930::AccessList; use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; use evm2::{ BaseEvmTypes, Evm, EvmTypesHost, Inspector, NoopInspector, Precompiles, TxResult, @@ -36,6 +37,7 @@ pub struct TxEnv { pub chain_id: Option, pub blob_hashes: Vec, pub max_fee_per_blob_gas: u128, + pub access_list: AccessList, } impl Default for TxEnv { @@ -52,6 +54,7 @@ impl Default for TxEnv { chain_id: None, blob_hashes: Vec::new(), max_fee_per_blob_gas: 0, + access_list: AccessList::default(), } } } @@ -338,6 +341,21 @@ impl TxEnv { self.caller, ); } + if !self.access_list.is_empty() { + return Recovered::new_unchecked( + TxEnvelope::Eip2930(TxEip2930 { + chain_id: self.chain_id.unwrap_or(1), + nonce: self.nonce, + gas_price: self.gas_price, + gas_limit: self.gas_limit, + to: self.kind, + value: self.value, + access_list: self.access_list.clone(), + input: self.data.clone(), + }), + self.caller, + ); + } Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { chain_id: self.chain_id, From ec116672078de909c1089af8a9a8b6c110d4877e Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:21 +0100 Subject: [PATCH 012/159] fix(evm): skip the interpreter for creates with empty initcode (#861) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A create with empty initcode, either a create transaction with empty data or `CREATE` and `CREATE2` with size 0, ran its frame anyway. The empty bytecode's padding `STOP` executed, so a step-recording inspector saw an extra `STOP` step at depth one more than the creator and an `initialize_interp` hook for a frame that has no code. go-ethereum's `Run` returns at once on empty code (`core/vm/interpreter.go:112-115`), so no step is logged, and the create still succeeds and deploys an empty contract. Calls to accounts without code already skip the interpreter the same way (`execute_call_message`), and this PR does the same for creates. `execute_create_message` now completes an empty-initcode create with a `Stop` result, the full gas and reservoir, and an empty output, then runs the usual `finish_create_message_run` (code size and deposit checks, nonce and account creation, commit). `finish_create_message_run` takes the gas and output as arguments instead of reading them from the interpreter pool, so both paths share it. Gas, state and logs do not change: the `STOP` cost nothing before and the result is the same. A new test asserts that a create transaction and a `CREATE` of size 0 record no steps and use no gas. One existing evm2 test used `CREATE(0, 0, 0)` to observe the create frame's checkpoint through `initialize_interp`; it now creates a one-byte `STOP` initcode so it still runs a frame. Parity harness (#859, geth master `a0cba163`), new case `create_empty_initcode` (an empty create transaction, then a call to a factory that runs `CREATE` and `CREATE2` with empty initcode, on Cancun, Prague, Osaka and Amsterdam). The differences that disappear, by tracer and field, with the same case run on main and on this branch: | tracer | field | before | after | | --- | --- | --- | --- | | `jsonl` | `steps[]`, `steps[].op`, `.opName`, `.pc`, `.depth`, `.gas`, `.stack[]` | 🔴 extra `STOP` step per empty create, every later step shifted | 🟢 equal | | `structlog` | `structLogs[]`, `.op`, `.pc`, `.depth`, `.gas`, `.stack[]`, `.storage` | 🔴 extra `STOP` entry | 🟢 equal | | `js-opcount` | `STOP` | 🔴 evm2 counts 1 and 5, geth none and 3 | 🟢 equal | The remaining differences of the case (`gasCost` of the `CREATE` step, EIP-8037 gas fields, prestate code hashes, `ctx.gasPrice`) are the existing known classes and not part of this change. The case is not committed here because the harness is not on main yet; I add it with the known-diffs entries for those classes once #859 lands. Benchmarks, `cargo bench -p evm2-cli --bench evm` interpreter shard, pinned to one core, median of three runs, main against this branch: | bench | main | this PR | ratio | | --- | --- | --- | --- | | weth | 1.974 µs | 1.959 µs | 0.992 | | erc20_transfer | 1307.6 µs | 1308.9 µs | 1.001 | | uniswap_v2_pair | 1.402 µs | 1.355 µs | 0.966 | | univ2_router | 1.376 µs | 1.379 µs | 1.003 | | seaport | 1.579 µs | 1.538 µs | 0.974 | | usdc_proxy | 1.500 µs | 1.528 µs | 1.019 | | universal_router | 30.051 µs | 29.948 µs | 0.997 | | uniswap_v3_pool | 9.255 µs | 9.161 µs | 0.990 | | snailtracer | 30262 µs | 29707 µs | 0.982 | | factorial | 28.605 µs | 28.648 µs | 1.002 | None of these benchmarks creates a contract, and the ratios stay within ±3.4% with no consistent sign, which is the run-to-run noise on this machine. --- crates/evm2/src/evm/inspector.rs | 14 ++++++++++-- crates/evm2/src/evm/mod.rs | 33 +++++++++++++++++----------- crates/inspectors/tests/it/parity.rs | 22 +++++++++++++++++++ 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index af4cf0f0c..4edc3d372 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -1497,8 +1497,18 @@ mod tests { let caller = Address::from([0xaa; 20]); let target = Address::from([0xbb; 20]); - // CREATE(0, 0, 0), then STOP. - let code = vec![op::PUSH0, op::PUSH0, op::PUSH0, op::CREATE, op::STOP]; + // MSTORE8(0, 0), CREATE(0, 0, 1) with a one byte STOP initcode, then STOP. + let code = vec![ + op::PUSH0, + op::PUSH0, + op::MSTORE8, + op::PUSH1, + 1, + op::PUSH0, + op::PUSH0, + op::CREATE, + op::STOP, + ]; let mut database = InMemoryDB::default(); database.insert_account_info( &caller, diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 4dcc53577..6a8d74ade 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1418,11 +1418,22 @@ impl<'a, T: EvmTypes> Evm<'a, T> { }; } message.code_address = message.destination; - let input = core::mem::take(&mut message.input); - let result = self.run_frame(message, checkpoint.clone()); - message.input = input; - let stop = result?; - self.finish_create_message_run(checkpoint, &message.destination, stop) + // Empty initcode deploys an empty contract without executing the bytecode's padding STOP. + let (stop, gas, output) = if message.code.is_empty() { + ( + InstrStop::Stop, + Gas::new_with_execution_gas_and_reservoir(message.gas_limit, message.reservoir), + Bytes::new(), + ) + } else { + let input = core::mem::take(&mut message.input); + let result = self.run_frame(message, checkpoint.clone()); + message.input = input; + let stop = result?; + let interp = self.interpreter_pool.last_mut().unwrap(); + (stop, interp.gas(), Bytes::copy_from_slice(interp.output())) + }; + self.finish_create_message_run(checkpoint, &message.destination, stop, gas, output) } #[inline(never)] @@ -1472,11 +1483,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { checkpoint: StateCheckpoint, address: &Address, stop: InstrStop, + mut gas: Gas, + mut output: Bytes, ) -> Result, ExecutionError> { - let interp = self.interpreter_pool.last_mut().unwrap(); - let mut gas = interp.gas(); - let output = if stop.is_success() { - let mut output = Bytes::copy_from_slice(interp.output()); + if stop.is_success() { if let Err(stop) = self.validate_create_output(&mut gas, &mut output) { self.state.rollback(checkpoint, self.features); return Ok(MessageResultExt { @@ -1497,12 +1507,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.state.rollback(checkpoint, self.features); return Err(code.into()); } - - output } else { self.state.rollback(checkpoint, self.features); - Bytes::copy_from_slice(interp.output()) - }; + } Ok(MessageResultExt { stop, diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index d86a7b564..55903c692 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -1108,6 +1108,28 @@ fn create_nonce_overflow_is_a_failed_frame() { } } +#[test] +fn create_with_empty_initcode_runs_no_steps() { + let factory = Address::with_last_byte(0x42); + for (kind, depth_nodes) in [(TransactTo::Create, 1), (TransactTo::Call(factory), 2)] { + let mut db = CacheDB::::default(); + db.insert_account_info( + &factory, + AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())), + ); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { gas_limit: 1_000_000, kind, ..Default::default() }).unwrap(); + let nodes = evm.inspector.traces().nodes(); + assert_eq!(nodes.len(), depth_nodes); + let create = nodes.last().unwrap(); + assert!(create.kind().is_any_create()); + assert!(create.trace.success && create.trace.steps.is_empty()); + assert_eq!(create.trace.gas_used, 0); + } +} + #[test] fn precompile_exclusion_uses_original_callee_with_delegation() { let parent = Address::from([0x11; 20]); From cdadae0fd5eff1c239e5f527c1bb758489c6009d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:26 +0100 Subject: [PATCH 013/159] fix(inspectors): omit a zero refund and empty memory and return data from struct logs (#864) Struct logs now omit what geth's struct logger omits: a zero `refund`, an empty `memory` and an empty `returnData`. Before, a step with no refund printed `"refund":0`, a step before the first memory write printed `"memory":[]`, and a step with empty return data printed `"returnData":"0x"` when `enableReturnData` was set. geth's JSON struct omits each of them when empty (`eth/tracers/logger/logger.go`, `structLogLegacy`), and I confirmed on a `geth --dev` node at master a0cba163 that `debug_traceCall` with `enableMemory` and `enableReturnData` prints neither field on steps where they are empty. `storage` has no change here: geth always records the slot of an `SLOAD` or `SSTORE` in the snapshot, so it is never empty, and its snapshot rules are a separate change. The parity harness normalises these fields away on both sides (`OMIT_EMPTY` in `scripts/geth_parity.py`), so it could not show the difference. I ran it offline on #859 plus this branch with `OMIT_EMPTY` reduced to `storage`, with an empty known-diffs file and only the `structlog` tracer. Lines about `refund`, `memory` and `returnData` fall from 15940 to 177. What remains is the refund timing (geth logs the refund after the gas function of `SSTORE`, so evm2 lags one step; that belongs to the refund change) and the `stateGasReservoir`, `stateGasCost`, `executionGasUsed` and `gasRefund` fields that evm2 prints and geth master does not. No `memory` or `returnData` difference remains. Once #859 is on main, this PR drops `memory`, `returnData` and `refund` from `OMIT_EMPTY` in the script and removes their known-diff entries. --- crates/inspectors/src/tracing/builder/geth.rs | 2 +- .../src/tracing/builder/geth/steps_tests.rs | 15 +++++++++++++++ crates/inspectors/src/tracing/types.rs | 8 ++++++-- crates/inspectors/tests/it/geth.rs | 8 +++----- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 2484fd650..e0c62cb18 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -132,7 +132,7 @@ impl<'a> GethTraceBuilder<'a> { } } - if opts.is_return_data_enabled() { + if opts.is_return_data_enabled() && !step.returndata.is_empty() { log.return_data = Some(step.returndata.clone()); } diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index a4ca15a60..18518c152 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -56,6 +56,21 @@ fn struct_log_errors_exclude_reverts() { } } +#[test] +fn struct_log_omits_zero_refund_and_empty_memory() { + let opts = GethDefaultTracingOptions::default().enable_memory(); + let mut step = test_step(0, op::PUSH0); + step.memory = Some(RecordedMemory::new(&[])); + let log = step.convert_to_geth_struct_log(&opts, 1, None); + assert_eq!((log.refund_counter, log.memory), (None, None)); + + step.gas_refund_counter = 4800; + step.memory = Some(RecordedMemory::new(&[1])); + let log = step.convert_to_geth_struct_log(&opts, 1, None); + assert_eq!(log.refund_counter, Some(4800)); + assert_eq!(log.memory.map(|memory| memory.len()), Some(1)); +} + #[test] fn erc7562_keccak_preimages_require_successful_steps() { for status in [ diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 09c7edcb9..4e75fca5a 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -711,10 +711,14 @@ impl CallTraceStep { format!("opcode {:#x} not defined", self.op.get()).into() }, pc: self.pc as u64, - refund_counter: Some(self.gas_refund_counter), + // geth omits a zero refund and empty memory and return data. + refund_counter: (self.gas_refund_counter != 0).then_some(self.gas_refund_counter), stack, memory: if opts.is_memory_enabled() { - self.memory.as_ref().map(RecordedMemory::memory_chunks) + self.memory + .as_ref() + .map(RecordedMemory::memory_chunks) + .filter(|memory| !memory.is_empty()) } else { None }, diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 1c84c01fa..f35ee8473 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -1259,7 +1259,7 @@ fn test_geth_prestate_diff_selfdestruct_cancun() { } #[test] -fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { +fn test_geth_default_tracer_omits_empty_return_data() { let contract = address!("0xc000000000000000000000000000000000000003"); let caller = address!("0xa000000000000000000000000000000000000003"); let code = hex!("00"); @@ -1305,7 +1305,7 @@ fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { ); assert!(!frame.struct_logs.is_empty(), "Expected struct logs for STOP execution"); - assert!(frame.struct_logs.iter().all(|log| log.return_data == Some(Bytes::default()))); + assert!(frame.struct_logs.iter().all(|log| log.return_data.is_none())); let struct_logs = serde_json::to_value(&frame) .unwrap() @@ -1314,9 +1314,7 @@ fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { .cloned() .unwrap(); - assert!(struct_logs.iter().all(|log| { - log.get("returnData") == Some(&serde_json::Value::String("0x".to_string())) - })); + assert!(struct_logs.iter().all(|log| log.get("returnData").is_none())); } fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { From 77549685b8909bd7ff4e30e44a03d0096919bf51 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:32 +0100 Subject: [PATCH 014/159] fix(inspectors): match go-ethereum's transfer logs (#854) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `TransferInspector::with_logs` now behaves like go-ethereum's `eth_simulateV1` tracer (`internal/ethapi/logtracer.go`). The inspector keeps one log list per open call or create frame, adds the logs the EVM emits (through the `log` hook) and the `0xeee…eee` transfer logs, drops a frame's list when the frame fails, and merges it into its parent otherwise. The result is read with `logs()` / `take_logs()`. Nothing is written to the EVM's own logs any more. Differences from go-ethereum found by running both on the same inputs, and fixed here: - **Receipts.** evm2 inserted the transfer logs into the EVM's log list, so the transaction result, receipt and logs bloom contained logs that geth's receipts do not (every simulated block's `logsBloom` is empty when only transfers happen). They now stay in the inspector. - **SELFDESTRUCT.** geth's `OnEnter` for SELFDESTRUCT carries the contract's balance, so a non-zero balance yields a transfer log (also when the beneficiary is the contract itself). evm2 never emitted one, and kept zero-balance selfdestructs in `transfers()`; both now follow the `value > 0` rule. - **CALLCODE.** geth skips CALLCODE (and DELEGATECALL) in `OnEnter`. evm2 recorded a `X → X` transfer for every CALLCODE with value. Merged #531 (dropping transfers of failed frames) matches geth: the cleared-frame cases below (reverted and invalid children, failed and nonce-overflowing creates, insufficient balance, reverted top level, oversized code) give the same result on main and on this branch. ## Parity run geth `a0cba163` (`geth --dev`, Osaka), `eth_simulateV1` with `traceTransfers: true`, `validation: false`, state overrides for the accounts, call from `0x…1001` (balance 1e18). 23 cases; the same inputs and geth's logs are in `crates/inspectors/tests/it/transfer.rs` (`simulate_transfer_logs_match_geth`), which runs them through evm2 on Osaka and also checks that `transfers()` equals the transfer logs and that the receipt logs hold only the emitted logs. `T(a→b, v)` is a log from `0xeee…eee` with topics `Transfer, a, b` and data `v`. | case | geth | evm2 main | this PR | | --- | --- | --- | --- | | CALLCODE with value 3 to `0xa003` | none | `T(c2→c2, 3)` 🔴 | none ✅ | | contract with balance 7 selfdestructs to `0xa002` | `T(c6→a002, 7)` | none 🔴 | `T(c6→a002, 7)` ✅ | | same, tx value 5 | `T(1001→c6, 5)`, `T(c6→a002, 12)` | `T(1001→c6, 5)` 🔴 | same as geth ✅ | | selfdestruct to self, balance 4 | `T(c8→c8, 4)` | none 🔴 | `T(c8→c8, 4)` ✅ | | CALLCODE (value 1) into code that selfdestructs, balance 50 | `T(d4→a002, 50)` | `T(d4→d4, 1)` 🔴 | `T(d4→a002, 50)` ✅ | | 18 other cases (tx value, zero value, zero-balance selfdestruct, interleaved emitted logs, reverted/invalid/oversized frames, CREATE/CREATE2, precompile, EOA, STATICCALL, DELEGATECALL, nonce overflow, insufficient balance) | | match | match ✅ | On main the selfdestruct-in-reverted-frame case only matched because selfdestructs were never logged; it now exercises the clearing. geth's raw output for the tx-value selfdestruct case (`selfdestruct_tx_value`), the evm2 logs after this PR are identical except for `logIndex`, which evm2 does not number: ```json [{"address":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000000000000000000000000000000000000001001","0x00000000000000000000000000000000000000000000000000000000000000c6"],"data":"0x0000000000000000000000000000000000000000000000000000000000000005","logIndex":"0x0"}, {"address":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000000000000000000000000000000000000000000c6","0x000000000000000000000000000000000000000000000000000000000000a002"],"data":"0x000000000000000000000000000000000000000000000000000000000000000c","logIndex":"0x1"}] ``` Input: `stateOverrides: {0x…c6: {code: 0x73…a002ff, balance: 0x7}}`, call `{from: 0x…1001, to: 0x…c6, value: 0x5}`. Once EIP-7708 is active the EVM emits the transfer logs itself, so the inspector then collects only those and synthesizes none, as geth's `eth_simulateV1` turns its transfer tracing off at Amsterdam for the same reason. The EIP-7708 case is covered by `simulate_transfer_logs_use_eip7708_logs`; geth's dev chain does not run Amsterdam, so that case follows geth's source rather than a node run. --- crates/inspectors/src/transfer.rs | 130 +++++-- crates/inspectors/tests/it/transfer.rs | 495 ++++++++++++++++++++++++- 2 files changed, 573 insertions(+), 52 deletions(-) diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 006f939cf..02c3f5c17 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -4,7 +4,7 @@ use alloc::{vec, vec::Vec}; use alloy_primitives::{Address, B256, Log, U256, address, b256}; use alloy_sol_types::SolValue; use evm2::{ - Evm, EvmTypes, Inspector, + Evm, EvmFeatures, EvmTypes, Inspector, interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; @@ -22,19 +22,27 @@ pub const TRANSFER_EVENT_TOPIC: B256 = /// Transfers made by a call or create that fails, including transfers in its subcalls, are /// discarded together with their logs. /// +/// As in `go-ethereum`, zero-value transfers, `CALLCODE` and `DELEGATECALL` are not transfers, and +/// a `SELFDESTRUCT` is one only if the contract holds a balance. +/// +/// Like `go-ethereum`'s `eth_simulateV1` tracer, the logs are kept by the inspector and are never +/// added to the EVM's own logs, so receipts and log blooms do not include them. +/// /// This can be used to construct an `eth_simulateV1` response. For `ots_getInternalOperations`, /// use [InternalOperationsInspector](crate::otterscan::InternalOperationsInspector). #[derive(Debug, Default, Clone)] pub struct TransferInspector { internal_only: bool, transfers: Vec, - /// Checkpoints of the call and create frames currently executing. - checkpoints: Vec, - /// If enabled, will insert ERC20-style transfer logs emitted by [TRANSFER_LOG_EMITTER] for - /// each ETH transfer. + /// The call and create frames currently executing, innermost last. + frames: Vec, + /// If enabled, collects the emitted logs together with ERC20-style transfer logs emitted by + /// [TRANSFER_LOG_EMITTER] for each ETH transfer. /// /// Can be used for [eth_simulateV1](https://github.com/ethereum/execution-apis/pull/484) execution. - insert_logs: bool, + collect_logs: bool, + /// Logs of the top-level frames that succeeded. + logs: Vec, } impl TransferInspector { @@ -43,7 +51,13 @@ impl TransferInspector { /// If `internal_only` is set to `true`, only internal transfers are collected, in other words, /// the top level call is ignored. pub const fn new(internal_only: bool) -> Self { - Self { internal_only, transfers: Vec::new(), checkpoints: Vec::new(), insert_logs: false } + Self { + internal_only, + transfers: Vec::new(), + frames: Vec::new(), + collect_logs: false, + logs: Vec::new(), + } } /// Creates a new transfer inspector that only collects internal transfers. @@ -56,12 +70,25 @@ impl TransferInspector { self.transfers } - /// Sets whether to insert ERC20-style transfer logs. - pub const fn with_logs(mut self, insert_logs: bool) -> Self { - self.insert_logs = insert_logs; + /// Sets whether to collect the emitted logs and the ERC20-style transfer logs. + /// + /// Once EIP-7708 is active the EVM emits the transfer logs itself, so only those are + /// collected, as in `eth_simulateV1`. + pub const fn with_logs(mut self, collect_logs: bool) -> Self { + self.collect_logs = collect_logs; self } + /// Returns a reference to the logs of the succeeded top-level frames, in emission order. + pub fn logs(&self) -> &[Log] { + &self.logs + } + + /// Takes the logs of the succeeded top-level frames, in emission order. + pub fn take_logs(&mut self) -> Vec { + core::mem::take(&mut self.logs) + } + /// Returns a reference to the collected transfers. pub fn transfers(&self) -> &[TransferOperation] { &self.transfers @@ -73,11 +100,14 @@ impl TransferInspector { } fn on_transfer(&mut self, message: &Message, host: &mut Evm<'_, T>) { - self.checkpoints - .push(TransferCheckpoint { transfers: self.transfers.len(), logs: host.logs().len() }); + // A transaction that stopped on an execution error never ran its end hooks. + if message.depth == host.version().initial_call_depth { + self.frames.clear(); + } + self.frames.push(TransferFrame { transfers: self.transfers.len(), logs: Vec::new() }); let kind = match message.kind { - MessageKind::Call | MessageKind::CallCode => TransferKind::Call, + MessageKind::Call => TransferKind::Call, MessageKind::Create => TransferKind::Create, MessageKind::Create2 => TransferKind::Create2, _ => return, @@ -87,40 +117,63 @@ impl TransferInspector { if self.internal_only && message.depth == host.version().initial_call_depth { return; } + + self.record_transfer(kind, message.caller, message.destination, message.value, host); + } + + fn on_transfer_end(&mut self, failed: bool) { + let Some(frame) = self.frames.pop() else { return }; + if failed { + self.transfers.truncate(frame.transfers); + } else if let Some(parent) = self.frames.last_mut() { + parent.logs.extend(frame.logs); + } else { + self.logs.extend(frame.logs); + } + } + + fn record_transfer( + &mut self, + kind: TransferKind, + from: Address, + to: Address, + value: U256, + host: &Evm<'_, T>, + ) { // skip zero transfers - let value = message.value; if value.is_zero() { return; } - let from = message.caller; - let to = message.destination; self.transfers.push(TransferOperation { kind, from, to, value }); - if self.insert_logs { + // Once EIP-7708 is active the EVM emits the transfer logs itself. + if self.collect_logs + && !host.feature(EvmFeatures::EIP7708) + && let Some(frame) = self.frames.last_mut() + { let from = B256::from_slice(&from.abi_encode()); let to = B256::from_slice(&to.abi_encode()); let data = value.abi_encode(); - host.log(Log::new_unchecked( + frame.logs.push(Log::new_unchecked( TRANSFER_LOG_EMITTER, vec![TRANSFER_EVENT_TOPIC, from, to], data.into(), )); } } +} - fn on_transfer_end(&mut self, failed: bool, host: &mut Evm<'_, T>) { - if let Some(checkpoint) = self.checkpoints.pop() - && failed +impl Inspector for TransferInspector { + fn log(&mut self, log: &Log, _host: &mut T::Host<'_>) { + if self.collect_logs + && let Some(frame) = self.frames.last_mut() { - self.transfers.truncate(checkpoint.transfers); - host.logs_mut().truncate(checkpoint.logs); + frame.logs.push(log.clone()); } } -} -impl Inspector for TransferInspector { fn call( &mut self, interp: &mut Interpreter<'_, '_, T>, @@ -132,11 +185,11 @@ impl Inspector for TransferInspector { fn call_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, _message: &Message, result: &mut MessageResult, ) { - self.on_transfer_end::(!result.is_success(), interp.host()); + self.on_transfer_end(!result.is_success()); } fn create( @@ -150,14 +203,14 @@ impl Inspector for TransferInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, _message: &Message, result: &mut MessageResult, ) { // A create that stops early without a created address, such as on nonce overflow, also // counts as failed even though its stop is `Return`. let failed = !result.is_success() || result.created_address.is_none(); - self.on_transfer_end::(failed, interp.host()); + self.on_transfer_end(failed); } fn selfdestruct( @@ -165,14 +218,9 @@ impl Inspector for TransferInspector { contract: &Address, target: &Address, value: &U256, - _host: &mut T::Host<'_>, + host: &mut T::Host<'_>, ) { - self.transfers.push(TransferOperation { - kind: TransferKind::SelfDestruct, - from: *contract, - to: *target, - value: *value, - }); + self.record_transfer(TransferKind::SelfDestruct, *contract, *target, *value, host); } } @@ -203,11 +251,11 @@ pub enum TransferKind { SelfDestruct, } -/// Transfer state at the start of a call or create frame. -#[derive(Clone, Copy, Debug)] -struct TransferCheckpoint { +/// A call or create frame currently executing. +#[derive(Clone, Debug)] +struct TransferFrame { /// Number of transfers recorded before the frame. transfers: usize, - /// Number of logs emitted before the frame. - logs: usize, + /// Logs of the frame and of its succeeded subframes. + logs: Vec, } diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index 85d08bf2a..b074f494a 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -4,10 +4,13 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, SpecId, TestDbExt, TransactTo, TxEnv, }; -use alloy_primitives::{Address, U256, address, hex}; +use alloy_primitives::{Address, B256, Log, U256, address, b256, hex}; use evm2_inspectors::{ tracing::{TracingInspector, TracingInspectorConfig}, - transfer::{TransferInspector, TransferKind, TransferOperation}, + transfer::{ + TRANSFER_EVENT_TOPIC, TRANSFER_LOG_EMITTER, TransferInspector, TransferKind, + TransferOperation, + }, }; #[test] @@ -194,11 +197,10 @@ fn discards_failed_call_transfers_and_logs() { }, ] ); - let logs = &res.tx_result.result.logs; - assert_eq!(logs.len(), 2); - assert_eq!(logs[0].topics()[2], caller.into_word()); - assert_eq!(logs[1].topics()[1], caller.into_word()); - assert_eq!(logs[1].topics()[2], recipient.into_word()); + assert_eq!( + evm.inspector().logs(), + &[transfer(deployer, caller, 10), transfer(caller, recipient, 2)] + ); } #[test] @@ -239,9 +241,7 @@ fn discards_failed_internal_create_transfer_and_log() { value: U256::from(10), }] ); - let logs = &res.tx_result.result.logs; - assert_eq!(logs.len(), 1); - assert_eq!(logs[0].topics()[2], factory.into_word()); + assert_eq!(evm.inspector().logs(), &[transfer(deployer, factory, 10)]); } #[test] @@ -286,5 +286,478 @@ fn discards_internal_create_transfer_on_nonce_overflow() { value: U256::from(10), }] ); - assert_eq!(res.tx_result.result.logs.len(), 1); + assert_eq!(evm.inspector().logs(), &[transfer(deployer, factory, 10)]); +} + +const fn a(n: u64) -> Address { + let mut bytes = [0; 20]; + let (_, low) = bytes.split_at_mut(12); + let n = n.to_be_bytes(); + let mut i = 0; + while i < 8 { + low[i] = n[i]; + i += 1; + } + Address::new(bytes) +} + +fn transfer(from: Address, to: Address, value: u64) -> Log { + Log::new_unchecked( + TRANSFER_LOG_EMITTER, + vec![TRANSFER_EVENT_TOPIC, from.into_word(), to.into_word()], + U256::from(value).to_be_bytes::<32>().into(), + ) +} + +/// A log returned by `go-ethereum`. +enum ExpectedLog { + Transfer(Address, Address, u64), + Emitted(Address, &'static [B256], &'static [u8]), +} + +impl ExpectedLog { + fn to_log(&self) -> Log { + match *self { + Self::Transfer(from, to, value) => transfer(from, to, value), + Self::Emitted(address, topics, data) => { + Log::new_unchecked(address, topics.to_vec(), data.to_vec().into()) + } + } + } +} + +/// A call executed on `go-ethereum`'s `eth_simulateV1` with `traceTransfers` enabled, and the logs +/// it returned. +struct TransferCase { + name: &'static str, + /// Address, code, balance and nonce. + accounts: &'static [(Address, &'static [u8], u64, u64)], + to: Option
, + value: u64, + input: &'static [u8], + expected: &'static [ExpectedLog], +} + +/// Logs returned by `go-ethereum` a0cba163 (`geth --dev`, Osaka) for the same calls. +#[test] +fn simulate_transfer_logs_match_geth() { + let sender = a(0x1001); + for case in CASES { + let mut db = CacheDB::new(EmptyDB::default()); + db.load_account(sender).info.balance = U256::from(10u128.pow(18)); + for &(address, code, balance, nonce) in case.accounts { + db.insert_account_info( + &address, + AccountInfo { + balance: U256::from(balance), + nonce, + code: Some(Bytecode::new_legacy(code.to_vec().into())), + ..Default::default() + }, + ); + } + + let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::OSAKA); + let mut evm = + context.build_mainnet_with_inspector(TransferInspector::new(false).with_logs(true)); + let res = evm + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 5_000_000, + kind: case.to.map_or(TransactTo::Create, TransactTo::Call), + value: U256::from(case.value), + data: case.input.to_vec().into(), + nonce: 0, + ..Default::default() + }) + .unwrap(); + + let expected = case.expected.iter().map(ExpectedLog::to_log).collect::>(); + assert_eq!(evm.inspector().logs(), expected, "{}", case.name); + let transfers = case + .expected + .iter() + .filter_map(|log| match *log { + ExpectedLog::Transfer(from, to, value) => Some((from, to, U256::from(value))), + ExpectedLog::Emitted(..) => None, + }) + .collect::>(); + assert_eq!( + evm.inspector().iter().map(|t| (t.from, t.to, t.value)).collect::>(), + transfers, + "{}", + case.name + ); + // `go-ethereum` keeps the transfer logs in the tracer, so they are not in the receipt. + let receipt_logs = expected + .into_iter() + .filter(|log| log.address != TRANSFER_LOG_EMITTER) + .collect::>(); + assert_eq!(res.tx_result.result.logs, receipt_logs, "{}", case.name); + } +} + +/// Once EIP-7708 is active the EVM logs the transfers itself, and `go-ethereum` stops tracing them. +#[test] +fn simulate_transfer_logs_use_eip7708_logs() { + let sender = a(0x1001); + let contract = a(0xc6); + let mut db = CacheDB::new(EmptyDB::default()); + db.load_account(sender).info.balance = U256::from(10u128.pow(18)); + db.insert_account_info( + &contract, + AccountInfo { + balance: U256::from(7), + code: Some(Bytecode::new_legacy( + hex!("73000000000000000000000000000000000000a002ff").into(), + )), + ..Default::default() + }, + ); + + let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::AMSTERDAM); + let mut evm = + context.build_mainnet_with_inspector(TransferInspector::new(false).with_logs(true)); + let res = evm + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 5_000_000, + kind: TransactTo::Call(contract), + value: U256::from(5), + nonce: 0, + ..Default::default() + }) + .unwrap(); + + assert!(res.result.is_success()); + assert_eq!(evm.inspector().transfers().len(), 2); + assert_eq!(res.tx_result.result.logs.len(), 2); + assert_eq!(evm.inspector().logs(), res.tx_result.result.logs); } + +const CASES: &[TransferCase] = &[ + TransferCase { + name: "tx_value", + accounts: &[(a(0xc1), &hex!(""), 0, 0)], + to: Some(a(0xc1)), + value: 10, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0x1001), a(0xc1), 10)], + }, + TransferCase { + name: "tx_value_zero", + accounts: &[(a(0xc1), &hex!(""), 0, 0)], + to: Some(a(0xc1)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "callcode_value", + accounts: &[( + a(0xc2), + &hex!("6000600060006000600373000000000000000000000000000000000000a00361fffff25000"), + 100, + 0, + )], + to: Some(a(0xc2)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "delegatecall_log", + accounts: &[ + ( + a(0xc4), + &hex!("60006000600060007300000000000000000000000000000000000000c561fffff450"), + 0, + 0, + ), + (a(0xc5), &hex!("601160006000a1"), 0, 0), + ], + to: Some(a(0xc4)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted( + a(0xc4), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000011")], + &hex!(""), + )], + }, + TransferCase { + name: "selfdestruct_balance", + accounts: &[(a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0)], + to: Some(a(0xc6)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc6), a(0xa002), 7)], + }, + TransferCase { + name: "selfdestruct_tx_value", + accounts: &[(a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0)], + to: Some(a(0xc6)), + value: 5, + input: &hex!(""), + expected: &[ + ExpectedLog::Transfer(a(0x1001), a(0xc6), 5), + ExpectedLog::Transfer(a(0xc6), a(0xa002), 12), + ], + }, + TransferCase { + name: "selfdestruct_zero", + accounts: &[(a(0xc7), &hex!("73000000000000000000000000000000000000a002ff"), 0, 0)], + to: Some(a(0xc7)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "selfdestruct_self", + accounts: &[(a(0xc8), &hex!("7300000000000000000000000000000000000000c8ff"), 4, 0)], + to: Some(a(0xc8)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc8), a(0xc8), 4)], + }, + TransferCase { + name: "selfdestruct_reverted_frame", + accounts: &[ + ( + a(0xc9), + &hex!( + "600060006000600060027300000000000000000000000000000000000000ca61fffff1506000600060006000600173000000000000000000000000000000000000a00161fffff15000" + ), + 50, + 0, + ), + ( + a(0xca), + &hex!( + "600060006000600060007300000000000000000000000000000000000000c661fffff15060006000fd" + ), + 50, + 0, + ), + (a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0), + ], + to: Some(a(0xc9)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc9), a(0xa001), 1)], + }, + TransferCase { + name: "logs_interleaved", + accounts: &[ + ( + a(0xcb), + &hex!( + "602160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000a0600060006000600060017300000000000000000000000000000000000000cc61fffff1506000600060006000600273000000000000000000000000000000000000a00161fffff150602260006000a1" + ), + 50, + 0, + ), + ( + a(0xcc), + &hex!( + "602360006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000fd" + ), + 50, + 0, + ), + ], + to: Some(a(0xcb)), + value: 0, + input: &hex!(""), + expected: &[ + ExpectedLog::Emitted( + a(0xcb), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000021")], + &hex!(""), + ), + ExpectedLog::Transfer(a(0xcb), a(0xa001), 1), + ExpectedLog::Emitted(a(0xcb), &[], &hex!("")), + ExpectedLog::Transfer(a(0xcb), a(0xa001), 2), + ExpectedLog::Emitted( + a(0xcb), + &[b256!("0x0000000000000000000000000000000000000000000000000000000000000022")], + &hex!(""), + ), + ], + }, + TransferCase { + name: "toplevel_revert", + accounts: &[( + a(0xcd), + &hex!( + "603160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000fd" + ), + 50, + 0, + )], + to: Some(a(0xcd)), + value: 3, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "create_tx_value", + accounts: &[], + to: None, + value: 10, + input: &hex!("600060005360016000f3"), + expected: &[ExpectedLog::Transfer( + a(0x1001), + address!("0x3817e247023b4f489352758397040b1fd33b300a"), + 10, + )], + }, + TransferCase { + name: "nested_create", + accounts: &[(a(0xce), &hex!("6000600053600160006005f05000"), 50, 0)], + to: Some(a(0xce)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer( + a(0xce), + address!("0x351888951695dc68547cfda22ae5d8bae8ab3b7a"), + 5, + )], + }, + TransferCase { + name: "nested_create2", + accounts: &[(a(0xcf), &hex!("60006000536007600160006003f55000"), 50, 0)], + to: Some(a(0xcf)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer( + a(0xcf), + address!("0xbc77ab833843214da7525df5458cc0a1392deee5"), + 3, + )], + }, + TransferCase { + name: "nested_create_failed", + accounts: &[(a(0xd0), &hex!("60fe600053600160006005f05060006000a000"), 50, 0)], + to: Some(a(0xd0)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd0), &[], &hex!(""))], + }, + TransferCase { + name: "insufficient_balance_call", + accounts: &[( + a(0xd1), + &hex!( + "60006000600060006103e873000000000000000000000000000000000000a00161fffff15060006000a0" + ), + 50, + 0, + )], + to: Some(a(0xd1)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd1), &[], &hex!(""))], + }, + TransferCase { + name: "precompile_value", + accounts: &[( + a(0xd2), + &hex!("6000600060006000600173000000000000000000000000000000000000000461fffff15000"), + 50, + 0, + )], + to: Some(a(0xd2)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd2), a(0x4), 1)], + }, + TransferCase { + name: "eoa_and_zero", + accounts: &[( + a(0xd3), + &hex!( + "6000600060006000600073000000000000000000000000000000000000a00161fffff1506000600060006000600173000000000000000000000000000000000000a00161fffff150600060006000600073000000000000000000000000000000000000a00161fffffa5000" + ), + 50, + 0, + )], + to: Some(a(0xd3)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd3), a(0xa001), 1)], + }, + TransferCase { + name: "callcode_selfdestruct_value", + accounts: &[ + ( + a(0xd4), + &hex!( + "600060006000600060017300000000000000000000000000000000000000c662030000f25000" + ), + 50, + 0, + ), + (a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0), + (a(0xa002), &hex!(""), 1, 0), + ], + to: Some(a(0xd4)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd4), a(0xa002), 50)], + }, + TransferCase { + name: "create_tx_failed", + accounts: &[], + to: None, + value: 10, + input: &hex!("fe"), + expected: &[], + }, + TransferCase { + name: "nested_create_nonce_overflow", + accounts: &[( + a(0xd5), + &hex!("6000600053600160006005f05060006000a000"), + 50, + 18446744073709551615, + )], + to: Some(a(0xd5)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd5), &[], &hex!(""))], + }, + TransferCase { + name: "invalid_child", + accounts: &[ + ( + a(0xd6), + &hex!( + "600060006000600060007300000000000000000000000000000000000000d761fffff15060006000a000" + ), + 50, + 0, + ), + ( + a(0xd7), + &hex!( + "604160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff150fe" + ), + 50, + 0, + ), + ], + to: Some(a(0xd6)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd6), &[], &hex!(""))], + }, + TransferCase { + name: "create_tx_code_too_large", + accounts: &[], + to: None, + value: 10, + input: &hex!("6160015ff3"), + expected: &[], + }, +]; From ce69c8d6cff80250f564cbcb2c2c875f966ec2d9 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:26:40 +0100 Subject: [PATCH 015/159] fix(inspectors): match geth's JS tracer setup and ctx (#846) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The JS tracer's constructor and `ctx` object differed from geth's goja tracer. This makes them match: `setup()` receives the config as a JSON string (`{}` when there is none), a tracer that exposes only one of `enter()` and `exit()` is rejected with geth's message, and a non-callable `setup` is ignored. `ctx.gasPrice` is the effective tip (`min(tip, maxFee - baseFee)`) as a bigint instead of the effective gas price as a number. `ctx.intrinsicGas` and `ctx.time` do not exist in geth, so they are removed. `ctx.txIndex` and `ctx.txHash` are set only for a non-zero block hash and a non-zero transaction hash, as in geth's constructor. `isPrecompiled` exists from construction and returns false until the transaction starts, instead of being registered on the first call hook with the error discarded. I ran each tracer through geth master (`a0cba163`, evm 1.17.11-unstable) `evm t8n --trace.tracer` (Cancun, one EIP-1559 tx with maxFee 10 gwei, tip 1 gwei, base fee 5 gwei, calling a contract with code `00`) and through this branch and `origin/main` on the same transaction. The `--trace.jsonconfig` column is the config passed to the tracer. | Tracer | geth master | `origin/main` | this PR | | --- | --- | --- | --- | | `setup(c){this.t=typeof c+":"+c}`, config `{"a":1}` | `"string:{\"a\":1}"` | 🔴 `"object:[object Object]"` | 🟢 `"string:{\"a\":1}"` | | same, no config | `"string:{}"` | 🔴 `"object:null"` | 🟢 `"string:{}"` | | `{enter(){},fault(){},result(){}}` | `trace object must expose either both or none of enter() and exit()` | 🔴 constructs | 🟢 same error | | `[c.intrinsicGas, c.time, c.gasPrice.toString()]` | `[null,null,"1000000000"]` | 🔴 `[0,"1","6000000000"]` | 🟢 `[null,null,"1000000000"]` | | `Object.keys(ctx)` | `type, from, to, input, gas, gasUsed, gasPrice, value, block, coinbase, output` | 🔴 also `intrinsicGas`, `time` | 🟢 same set | | `isPrecompiled("0x01")` in `setup`, then in `step` | `[false,true]` | 🔴 `["ReferenceError: isPrecompiled is not defined",true]` | 🟢 `[false,true]` | `t8n` passes no tracer context, so the `blockHash`/`txIndex`/`txHash` rules from `eth/tracers/js/goja.go` (L168-181 at `a0cba163`) are covered by a unit test rather than by a geth run. --- crates/inspectors/src/tracing/js/bindings.rs | 28 +-- crates/inspectors/src/tracing/js/builtins.rs | 38 +++- .../src/tracing/js/interrupt_tests.rs | 3 +- crates/inspectors/src/tracing/js/mod.rs | 209 +++++++++++++----- crates/inspectors/tests/it/geth_js.rs | 6 +- 5 files changed, 201 insertions(+), 83 deletions(-) diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 905c10ddc..9cc85d57d 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -1047,10 +1047,8 @@ pub(crate) struct JsEvmContext { pub(crate) gas: u64, /// Number, amount of gas used in executing the transaction (excludes txdata costs) pub(crate) gas_used: u64, - /// Number, gas price configured in the transaction being executed - pub(crate) gas_price: u64, - /// Number, intrinsic gas for the transaction being executed - pub(crate) intrinsic_gas: u64, + /// big.int, effective gas tip of the transaction being executed + pub(crate) gas_price: U256, /// big.int Amount to be transferred in wei pub(crate) value: U256, /// Number, block number @@ -1058,8 +1056,6 @@ pub(crate) struct JsEvmContext { /// Address, miner of the block pub(crate) coinbase: Address, pub(crate) output: Bytes, - /// Number, block timestamp - pub(crate) time: String, pub(crate) transaction_ctx: TransactionContext, /// returns information about the error if one occurred, otherwise returns undefined pub(crate) error: Option, @@ -1075,12 +1071,10 @@ impl JsEvmContext { gas, gas_used, gas_price, - intrinsic_gas, value, block, coinbase, output, - time, transaction_ctx, error, } = self; @@ -1099,21 +1093,19 @@ impl JsEvmContext { obj.set(js_string!("input"), to_uint8_array(input, ctx)?, false, ctx)?; obj.set(js_string!("gas"), gas, false, ctx)?; obj.set(js_string!("gasUsed"), gas_used, false, ctx)?; - obj.set(js_string!("gasPrice"), gas_price, false, ctx)?; - obj.set(js_string!("intrinsicGas"), intrinsic_gas, false, ctx)?; + obj.set(js_string!("gasPrice"), to_bigint(gas_price), false, ctx)?; obj.set(js_string!("value"), to_bigint(value), false, ctx)?; obj.set(js_string!("block"), block, false, ctx)?; obj.set(js_string!("coinbase"), address_to_uint8_array(coinbase, ctx)?, false, ctx)?; obj.set(js_string!("output"), to_uint8_array(output, ctx)?, false, ctx)?; - obj.set(js_string!("time"), js_string!(time), false, ctx)?; - if let Some(block_hash) = transaction_ctx.block_hash { + // Like geth, the transaction fields are only set for a non-zero block and transaction hash. + if let Some(block_hash) = transaction_ctx.block_hash.filter(|hash| !hash.is_zero()) { obj.set(js_string!("blockHash"), to_uint8_array(block_hash, ctx)?, false, ctx)?; - } - if let Some(tx_index) = transaction_ctx.tx_index { - obj.set(js_string!("txIndex"), tx_index as u64, false, ctx)?; - } - if let Some(tx_hash) = transaction_ctx.tx_hash { - obj.set(js_string!("txHash"), to_uint8_array(tx_hash, ctx)?, false, ctx)?; + if let Some(tx_hash) = transaction_ctx.tx_hash.filter(|hash| !hash.is_zero()) { + let tx_index = transaction_ctx.tx_index.unwrap_or_default(); + obj.set(js_string!("txIndex"), tx_index as u64, false, ctx)?; + obj.set(js_string!("txHash"), to_uint8_array(tx_hash, ctx)?, false, ctx)?; + } } if let Some(error) = error { obj.set(js_string!("error"), js_string!(error), false, ctx)?; diff --git a/crates/inspectors/src/tracing/js/builtins.rs b/crates/inspectors/src/tracing/js/builtins.rs index 16a96386d..ab9e64cdb 100644 --- a/crates/inspectors/src/tracing/js/builtins.rs +++ b/crates/inspectors/src/tracing/js/builtins.rs @@ -1,7 +1,7 @@ //! Builtin functions use crate::tracing::types::CallKind; -use alloc::{borrow::Cow, format, vec::Vec}; +use alloc::{borrow::Cow, format, rc::Rc, vec::Vec}; use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::HashSet}; use boa_engine::{ Context, JsArgs, JsBigInt, JsError, JsNativeError, JsResult, JsString, JsValue, NativeFunction, @@ -15,7 +15,7 @@ use boa_engine::{ property::Attribute, }; use boa_gc::{Finalize, Trace, empty_trace}; -use core::borrow::Borrow; +use core::{borrow::Borrow, cell::RefCell}; /// Converts the given `JsValue` to a `serde_json::Value`. /// @@ -66,7 +66,7 @@ pub(crate) fn json_stringify(val: JsValue, ctx: &mut Context) -> JsResult JsResult<()> { let big_int = ctx.global_object().get(js_string!("BigInt"), ctx)?; // Add toJSON method and geth-compatible polyfill shims to BigInt prototype. @@ -447,27 +447,45 @@ pub(crate) fn slice(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResul } } -/// A container for all precompile addresses used for the `isPrecompiled` global callable. -#[derive(Clone, Debug)] -pub(crate) struct PrecompileList(pub(crate) HashSet
); +/// The active precompile addresses behind the `isPrecompiled` global callable. +/// +/// Unset until the transaction starts, like geth's list of active precompiles. +#[derive(Clone, Debug, Default)] +pub(crate) struct PrecompileList(Rc>>>); impl PrecompileList { - /// Registers the global callable `isPrecompiled` - pub(crate) fn register_callable(self, ctx: &mut Context) -> JsResult<()> { + /// Registers the global callable `isPrecompiled`, which reads this list. + pub(crate) fn register_callable(&self, ctx: &mut Context) -> JsResult<()> { let is_precompiled = NativeFunction::from_copy_closure_with_captures( move |_this, args, precompiles, ctx| { let val = args.get_or_undefined(0).clone(); let buf = bytes_from_value(val, ctx)?; let addr = bytes_to_address(&buf); - Ok(precompiles.0.contains(&addr).into()) + let precompiles = RefCell::borrow(&precompiles.0); + Ok(precompiles.as_ref().is_some_and(|list| list.contains(&addr)).into()) }, - self, + self.clone(), ); ctx.register_global_callable(js_string!("isPrecompiled"), 1, is_precompiled)?; Ok(()) } + + /// Returns true if the active precompile addresses have been set. + pub(crate) fn is_set(&self) -> bool { + RefCell::borrow(&self.0).is_some() + } + + /// Sets the active precompile addresses. + pub(crate) fn set(&self, addresses: impl IntoIterator) { + *self.0.borrow_mut() = Some(addresses.into_iter().collect()); + } + + /// Unsets the active precompile addresses. + pub(crate) fn clear(&self) { + *self.0.borrow_mut() = None; + } } impl Finalize for PrecompileList {} diff --git a/crates/inspectors/src/tracing/js/interrupt_tests.rs b/crates/inspectors/src/tracing/js/interrupt_tests.rs index b9d56644e..4bdcc0978 100644 --- a/crates/inspectors/src/tracing/js/interrupt_tests.rs +++ b/crates/inspectors/src/tracing/js/interrupt_tests.rs @@ -105,8 +105,9 @@ fn interrupt_during_enter_and_exit_aborts() { // SELFDESTRUCT invokes enter/exit outside of step_end. &hex!("6043ff")[..], ] { + let other = if callback == "enter" { "exit" } else { "enter" }; let script = format!( - "{{ {callback}: function() {{ interrupt(); }}, + "{{ {callback}: function() {{ interrupt(); }}, {other}: function() {{}}, fault: function() {{}}, result: function() {{ return 42; }} }}" ); let (_, result) = run(interruptible(&script), code, TxKind::Call(TARGET)); diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 628dda8fd..2fb62d33e 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -22,7 +22,7 @@ use alloc::{ vec::Vec, }; use alloy_consensus::{Transaction, transaction::Recovered}; -use alloy_primitives::{Address, Bytes, TxKind, U256, map::HashSet}; +use alloy_primitives::{Address, Bytes, TxKind, U256}; use boa_engine::{ Context, JsError, JsNativeError, JsObject, JsResult, JsValue, Script, Source, builtins::error::Error, error::EngineError, js_string, object::builtins::JsArray, @@ -118,8 +118,8 @@ pub struct JsInspector { call_stack: Vec, /// Monotonically increasing ID used to refresh callback contract input. next_call_id: u64, - /// Marker to track whether the precompiles have been registered. - precompiles_registered: bool, + /// The precompiles read by `isPrecompiled`, set on the first call hook of a transaction. + precompiles: PrecompileList, /// Pre-execution states captured in `step()` to be processed in `step_end()`. pending_steps: Vec, /// Depth of the root frame, which can be non-zero for a nested transaction root. @@ -152,9 +152,10 @@ impl JsInspector { /// - `fault`: a function that will be called when the transaction fails. /// /// Optional functions are invoked during inspection: - /// - `setup`: a function that will be called before the inspection starts. - /// - `enter`: a function that will be called when the execution enters a new call. - /// - `exit`: a function that will be called when the execution exits a call. + /// - `setup`: a function that will be called before the inspection starts, with the config as a + /// JSON string. + /// - `enter` and `exit`: functions that will be called when the execution enters and exits a + /// call. Both must be present or both absent. /// - `step`: a function that will be called when the execution steps to the next instruction. /// /// This also accepts a sender half of a channel to communicate with the database service so the @@ -181,6 +182,8 @@ impl JsInspector { ctx.runtime_limits_mut().set_recursion_limit(RECURSION_LIMIT); register_builtins(&mut ctx)?; + let precompiles = PrecompileList::default(); + precompiles.register_callable(&mut ctx)?; // parse the code let wrapped = format!("({code})"); @@ -219,7 +222,7 @@ impl JsInspector { reusable_db, call_stack: Default::default(), next_call_id: 1, - precompiles_registered: false, + precompiles, pending_steps: Vec::new(), root_depth: 0, hook_error: None, @@ -248,6 +251,8 @@ impl JsInspector { /// wrappers are recreated so their own properties do not carry over between transactions. pub fn fuse(&mut self) -> Result<(), JsInspectorError> { self.ensure_not_interrupted()?; + // `setup` of the fresh tracer must not see the previous transaction's precompiles. + self.precompiles.clear(); let JsTracerObject { obj, result_fn, fault_fn, enter_fn, exit_fn, step_fn } = JsTracerObject::evaluate(&self.script, &self.config, &mut self.ctx)?; // Callback objects are mutable JS objects: replacing their Rust state does not remove @@ -273,7 +278,6 @@ impl JsInspector { self.exit_fn = exit_fn; self.step_fn = step_fn; self.call_stack.clear(); - self.precompiles_registered = false; self.pending_steps.clear(); self.root_depth = 0; self.hook_error = None; @@ -351,6 +355,7 @@ impl JsInspector { }); let base_fee = block.basefee.try_into().unwrap_or(u64::MAX); + let gas_tip = tx.effective_tip_per_gas(base_fee).unwrap_or_default(); let ctx = JsEvmContext { r#type: match kind { @@ -363,13 +368,11 @@ impl JsInspector { input: tx.input().clone(), gas: tx.gas_limit(), gas_used: result.tx_gas_used(), - gas_price: tx.effective_gas_price(Some(base_fee)).try_into().unwrap_or(u64::MAX), + gas_price: U256::from(gas_tip), value: tx.value(), block: block.number.try_into().unwrap_or(u64::MAX), coinbase: block.beneficiary, output: output_bytes.unwrap_or_default(), - time: block.timestamp.to_string(), - intrinsic_gas: 0, transaction_ctx: self.transaction_context, error, }; @@ -449,16 +452,11 @@ impl JsInspector { self.active_call() } - /// Registers the precompiles in the JS context + /// Sets the active precompiles read by `isPrecompiled`. fn register_precompiles(&mut self, host: &Evm<'_, T>) { - if self.precompiles_registered { - return; + if !self.precompiles.is_set() { + self.precompiles.set(host.precompiles().addresses()); } - let precompiles = PrecompileList(HashSet::from_iter(host.precompiles().addresses())); - - let _ = precompiles.register_callable(&mut self.ctx); - - self.precompiles_registered = true } /// Sets a shared handle that cooperatively interrupts JavaScript tracing. @@ -969,17 +967,21 @@ impl JsTracerObject { let exit_fn = obj.get(js_string!("exit"), ctx)?.as_object().filter(|o| o.is_callable()); let step_fn = obj.get(js_string!("step"), ctx)?.as_object().filter(|o| o.is_callable()); - let js_config_value = - JsValue::from_json(config, ctx).map_err(JsInspectorError::InvalidJsonConfig)?; - - if let Some(setup_fn) = obj.get(js_string!("setup"), ctx)?.as_object() { - if !setup_fn.is_callable() { - return Err(JsInspectorError::SetupFunctionNotCallable); - } + if enter_fn.is_some() != exit_fn.is_some() { + return Err(JsInspectorError::EnterExitMismatch); + } - // call setup() + // Like geth, `setup` receives the config as a JSON string, `{}` if there is none, and a + // non-callable `setup` is ignored. + if let Some(setup_fn) = obj.get(js_string!("setup"), ctx)?.as_object() + && setup_fn.is_callable() + { + let config = match config { + serde_json::Value::Null => js_string!("{}"), + config => js_string!(config.to_string()), + }; setup_fn - .call(&(obj.clone().into()), core::slice::from_ref(&js_config_value), ctx) + .call(&(obj.clone().into()), &[config.into()], ctx) .map_err(JsInspectorError::SetupCallFailed)?; } @@ -1022,18 +1024,14 @@ pub enum JsInspectorError { #[error("trace object must expose a function fault()")] FaultFunctionMissing, - /// The setup object must be a callable function. - #[error("setup object must be a function")] - SetupFunctionNotCallable, + /// The trace object must expose either both or none of `enter()` and `exit()`. + #[error("trace object must expose either both or none of enter() and exit()")] + EnterExitMismatch, /// Failure during the invocation of the `setup()` function. #[error("failed to call setup(): {0}")] SetupCallFailed(JsError), - /// Invalid JSON configuration encountered. - #[error("invalid JSON config: {0}")] - InvalidJsonConfig(JsError), - /// Tracing was cancelled through the shared interrupt flag. #[error("JavaScript tracing interrupted")] Interrupted, @@ -1127,8 +1125,8 @@ fn with_message_suffix(err: JsError, suffix: &str, ctx: &mut Context) -> JsError mod tests { use super::*; use alloc::{string::ToString, vec, vec::Vec}; - use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{Address, Bytes, TxKind, U256, bytes, hex}; + use alloy_consensus::{TxEip1559, TxLegacy, transaction::Recovered}; + use alloy_primitives::{Address, B256, Bytes, TxKind, U256, bytes, hex}; use boa_engine::error::{EngineError, RuntimeLimitError}; use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, Version, @@ -1166,6 +1164,108 @@ mod tests { assert!(matches!(result, Err(JsInspectorError::FaultFunctionMissing))); } + #[test] + fn test_setup_receives_config_json_string() { + let code = |expected: &str| { + format!( + r#"{{ + setup: function(config) {{ + if (typeof config !== "string" || config !== '{expected}') {{ + throw new Error(typeof config + ":" + config); + }} + }}, + fault: function() {{}}, + result: function() {{}} + }}"# + ) + }; + JsInspector::new(code(r#"{"a":1}"#), json!({"a": 1})).unwrap(); + JsInspector::new(code("{}"), serde_json::Value::Null).unwrap(); + let result = JsInspector::new(code("{}"), json!({"a": 1})); + assert!(matches!(result, Err(JsInspectorError::SetupCallFailed(_)))); + } + + #[test] + fn test_enter_exit_must_come_together() { + for functions in ["enter: function() {},", "exit: function() {},"] { + let code = format!("{{{functions} fault: function() {{}}, result: function() {{}}}}"); + let result = JsInspector::new(code, serde_json::Value::Null); + assert!(matches!(result, Err(JsInspectorError::EnterExitMismatch))); + } + let code = "{enter: function() {}, exit: function() {}, fault: function() {}, result: function() {}}"; + JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + } + + #[test] + fn test_ctx_matches_geth() { + let code = r#"{ + fault: function() {}, + result: function(ctx) { + return [ctx.gasPrice.toString(), ctx.intrinsicGas, ctx.time]; + } + }"#; + // The tip is min(max_priority_fee, max_fee - base_fee). + for (max_fee, expected) in + [(10_000_000_000u128, "1000000000"), (5_500_000_000, "500000000")] + { + let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + let tx = TxEnvelope::Eip1559(TxEip1559 { + chain_id: 1, + gas_limit: 1_000_000, + max_fee_per_gas: max_fee, + max_priority_fee_per_gas: 1_000_000_000, + to: TxKind::Call(Address::repeat_byte(0x01)), + ..Default::default() + }); + let res = try_run_tx(insp, None, true, U256::from(5_000_000_000u64), tx).unwrap(); + assert_eq!(res, json!([expected, null, null])); + } + } + + #[test] + fn test_ctx_transaction_fields_need_non_zero_hashes() { + let code = r#"{ + fault: function() {}, + result: function(ctx) { + return [ctx.blockHash !== undefined, ctx.txIndex, ctx.txHash !== undefined]; + } + }"#; + let hash = Some(B256::repeat_byte(1)); + let zero = Some(B256::ZERO); + for (block_hash, tx_hash, tx_index, expected) in [ + (None, None, None, json!([false, null, false])), + (hash, None, Some(3), json!([true, null, false])), + (zero, hash, Some(3), json!([false, null, false])), + (hash, zero, Some(3), json!([true, null, false])), + (hash, hash, Some(3), json!([true, 3, true])), + (hash, hash, None, json!([true, 0, true])), + ] { + let tx_context = TransactionContext { block_hash, tx_index, tx_hash }; + let insp = JsInspector::with_transaction_context( + code.to_string(), + serde_json::Value::Null, + tx_context, + ) + .unwrap(); + let res = try_run_tx(insp, None, true, U256::ZERO, legacy_tx(1024)).unwrap(); + assert_eq!(res, expected); + } + } + + #[test] + fn test_is_precompiled_is_false_in_setup() { + let code = r#"{ + before: false, + setup: function() { this.before = isPrecompiled("0x01"); }, + fault: function() {}, + result: function() { return this.before; } + }"#; + let mut insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + insp.precompiles.set([Address::with_last_byte(1)]); + insp.fuse().unwrap(); + assert_eq!(try_run_tx(insp, None, true, U256::ZERO, legacy_tx(0)).unwrap(), json!(false)); + } + fn run_trace(code: &str, contract: Option, success: bool) -> serde_json::Value { try_run_trace(code, contract, success).unwrap() } @@ -1174,6 +1274,26 @@ mod tests { code: &str, contract: Option, success: bool, + ) -> Result { + let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + try_run_tx(insp, contract, success, U256::ZERO, legacy_tx(1024)) + } + + fn legacy_tx(gas_price: u128) -> TxEnvelope { + TxEnvelope::Legacy(TxLegacy { + gas_price, + gas_limit: 1_000_000, + to: TxKind::Call(Address::repeat_byte(0x01)), + ..Default::default() + }) + } + + fn try_run_tx( + insp: JsInspector, + contract: Option, + success: bool, + basefee: U256, + tx: TxEnvelope, ) -> Result { let addr = Address::repeat_byte(0x01); let mut db = CacheDB::new(EmptyDB::default()); @@ -1195,25 +1315,16 @@ mod tests { }, ); - let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); let mut evm = Evm::::new( SpecId::CANCUN, - evm2::env::BlockEnvExt::default(), + evm2::env::BlockEnvExt { basefee, ..Default::default() }, ethereum_tx_registry(SpecId::CANCUN), db, Precompiles::base(SpecId::CANCUN), ); evm.set_inspector(insp); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_price: 1024, - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = Recovered::new_unchecked(tx, Address::ZERO); let res = evm.transact(&tx).expect("pass without error").detach(); assert_eq!(res.result.status, success); @@ -1729,8 +1840,6 @@ mod tests { let code = r#"{ counts: { log: 0, db: 0, enter: 0, exit: 0 }, setup: function(config) { - if (config.used) throw new Error("config was reused"); - config.used = true; this.ready = true; }, mark: function(object, kind) { diff --git a/crates/inspectors/tests/it/geth_js.rs b/crates/inspectors/tests/it/geth_js.rs index 3bd78492e..d71341f31 100644 --- a/crates/inspectors/tests/it/geth_js.rs +++ b/crates/inspectors/tests/it/geth_js.rs @@ -471,10 +471,9 @@ fn test_geth_debug_inspector_jstracer() { exists: db.exists(ctx.to), codeLength: db.getCode(ctx.to).length, gas: ctx.gas, - gasPrice: ctx.gasPrice, + gasPrice: ctx.gasPrice.toString(), value: ctx.value.toString(), block: ctx.block, - time: ctx.time, coinbase: toHex(ctx.coinbase), error: !!ctx.error }; @@ -500,10 +499,9 @@ fn test_geth_debug_inspector_jstracer() { assert_eq!(result["exists"], json!(true)); assert_eq!(result["codeLength"], json!(5)); assert_eq!(result["gas"], json!(1_000_000)); - assert_eq!(result["gasPrice"], json!(17)); + assert_eq!(result["gasPrice"], json!("17")); assert_eq!(result["value"], json!("3")); assert_eq!(result["block"], json!(42)); - assert_eq!(result["time"], json!("1234")); assert_eq!(result["coinbase"], json!(coinbase)); assert_eq!(result["error"], json!(false)); } From 84810818064d5ed7a73336f3d68fb449eea45d48 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:33:21 +0100 Subject: [PATCH 016/159] fix(evm): cap EIP-8037 transaction gas limit at 2^32-1 (#862) EIP-8037 caps the total transaction gas limit, including the state-gas reservoir, at 2^32-1 under Amsterdam (execution-specs `TX_MAX_TOTAL_GAS_LIMIT`, checked in `validate_transaction`). evm2 left `tx.gas` uncapped when EIP-8037 is active and only bounded the required execution gas by 2^24. `Version::tx_gas_caps` now returns 2^32-1 as the `tx.gas` bound under EIP-8037, so the transaction handlers (`validate_tx_gas_limit_cap`) and stateless callers such as a transaction pool reject larger gas limits with `TxGasLimitGreaterThanCap`. The 2^24 bound on `max(intrinsic, floor)` and the execution/reservoir split are unchanged. Block-level gas accounting uses `u64` and already holds twice the cap. The `tx_total_gas_limit_cap` state fixture in `tests@v21.0.1` fails without this change (the above-cap transactions are accepted) and passes with it, so the EEST runner no longer ignores it. --- crates/eest/src/runner.rs | 4 ---- crates/evm2/src/constants.rs | 5 ++++ crates/evm2/src/ethereum/mod.rs | 42 +++++++++++++++++++++++++++++++++ crates/evm2/src/version/mod.rs | 12 ++++++---- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/crates/eest/src/runner.rs b/crates/eest/src/runner.rs index 266d0d6b9..860bce687 100644 --- a/crates/eest/src/runner.rs +++ b/crates/eest/src/runner.rs @@ -142,10 +142,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "frontier/validation/transaction/bad_v_r_s.json", "frontier/validation/transaction/unrecoverable_signature.json", "london/eip1559_fee_market_change/tx_type/invalid_chain_id.json", - - // The Amsterdam total transaction gas cap of 2^32-1 (EIP-8037) is not enforced yet. The PR that - // enforces it removes this entry. - "eip8037_state_creation_gas_cost_increase/block_2d_gas_accounting/tx_total_gas_limit_cap.json", ]; fn should_ignore(name: &str) -> bool { diff --git a/crates/evm2/src/constants.rs b/crates/evm2/src/constants.rs index d99737f27..e44d46e7e 100644 --- a/crates/evm2/src/constants.rs +++ b/crates/evm2/src/constants.rs @@ -21,6 +21,11 @@ pub const MAX_CODE_SIZE_AMSTERDAM: usize = 0x10000; /// Maximum contract creation initcode size since Amsterdam. pub const MAX_INITCODE_SIZE_AMSTERDAM: usize = 2 * MAX_CODE_SIZE_AMSTERDAM; +/// Maximum transaction gas limit, including the state-gas reservoir, since Amsterdam. +/// +/// EIP-8037 caps `tx.gas` at `2^32 - 1` (execution-specs `TX_MAX_TOTAL_GAS_LIMIT`). +pub const MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM: u64 = (1 << 32) - 1; + /// Amsterdam blob base fee update fraction (BPO2 blob schedule). pub const BLOB_BASE_FEE_UPDATE_FRACTION_AMSTERDAM: u64 = 11_684_671; diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index b33c2ca3f..d9902ee73 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -912,6 +912,7 @@ mod tests { use super::*; use crate::{ BaseEvmTypes, ExecutionConfig, Precompiles, + constants::MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, env::BlockEnvExt, evm::InMemoryDB, interpreter::{Host, InstrStop, op}, @@ -1377,6 +1378,14 @@ mod tests { }) ); assert_eq!(validate_tx_gas_limit_cap(amsterdam, tx_gas_limit), Ok(())); + assert_eq!(validate_tx_gas_limit_cap(amsterdam, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM), Ok(())); + assert_eq!( + validate_tx_gas_limit_cap(amsterdam, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + }) + ); assert_eq!( validate_execution_gas_limit_cap(amsterdam, tx_gas_limit, intrinsic, floor_gas), Ok(()) @@ -1405,6 +1414,39 @@ mod tests { ); } + #[rstest::rstest] + fn amsterdam_handler_rejects_gas_limit_above_total_cap( + #[values(MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1)] + gas_limit: u64, + ) { + let mut evm = Evm::::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::AMSTERDAM), + InMemoryDB::default(), + Precompiles::base(SpecId::AMSTERDAM), + ); + let tx = Recovered::new_unchecked( + TxEnvelope::Eip2930(TxEip2930 { + chain_id: 1, + gas_limit, + to: TxKind::Call(Address::with_last_byte(0xbb)), + ..Default::default() + }), + Address::with_last_byte(0xaa), + ); + + let expected = if gas_limit > MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM { + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, + }) + } else { + Ok(()) + }; + assert_eq!(evm.validate_tx(&tx), expected); + } + #[rstest::rstest] #[case::call_zero_value(TxKind::Call(Address::ZERO), U256::ZERO, 15_000, 12_700)] #[case::call_with_value(TxKind::Call(Address::ZERO), U256::from(1), 21_000, 18_700)] diff --git a/crates/evm2/src/version/mod.rs b/crates/evm2/src/version/mod.rs index f194210af..3962602da 100644 --- a/crates/evm2/src/version/mod.rs +++ b/crates/evm2/src/version/mod.rs @@ -4,7 +4,7 @@ use crate::{ EvmConfig, EvmTypesHost, SpecId, constants::{ CALL_DEPTH_LIMIT, MAX_CODE_SIZE, MAX_CODE_SIZE_AMSTERDAM, MAX_INITCODE_SIZE, - MAX_INITCODE_SIZE_AMSTERDAM, + MAX_INITCODE_SIZE_AMSTERDAM, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, }, interpreter::{instructions as instr, op}, }; @@ -86,7 +86,8 @@ impl Version { /// /// Without EIP-8037, [`tx_gas_limit_cap`](Self::tx_gas_limit_cap) (EIP-7825) bounds `tx.gas` /// itself, which also bounds the execution gas. With EIP-8037, `tx.gas` may exceed it because - /// the excess funds the state-gas reservoir, so it only bounds the required execution gas. + /// the excess funds the state-gas reservoir, so it only bounds the required execution gas, and + /// `tx.gas` is bounded by [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`] instead. /// /// The Ethereum transaction handlers enforce these caps through /// [`validate_tx_gas_limit_cap`](crate::ethereum::validate_tx_gas_limit_cap) and @@ -96,7 +97,7 @@ impl Version { #[inline] pub const fn tx_gas_caps(&self) -> (u64, u64) { if self.feature(EvmFeatures::EIP8037) { - (u64::MAX, self.tx_gas_limit_cap) + (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, self.tx_gas_limit_cap) } else { (self.tx_gas_limit_cap, u64::MAX) } @@ -389,7 +390,10 @@ mod tests { assert_eq!(osaka.tx_gas_caps(), (MAX_TX_GAS_LIMIT_OSAKA, u64::MAX)); let amsterdam = Version::base(SpecId::AMSTERDAM); - assert_eq!(amsterdam.tx_gas_caps(), (u64::MAX, MAX_TX_GAS_LIMIT_OSAKA)); + assert_eq!( + amsterdam.tx_gas_caps(), + (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_GAS_LIMIT_OSAKA) + ); let mut amsterdam_without_eip8037 = Version::new(SpecId::AMSTERDAM); amsterdam_without_eip8037.features.remove(EvmFeatures::EIP8037); From 2bed9e941df2edb9acd6e2f5df09d5f3e377f27b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:33:38 +0100 Subject: [PATCH 017/159] fix(inspectors): snapshot struct log storage like geth's struct logger (#865) The default tracer now builds the struct log `storage` snapshot the way geth's struct logger does. Before, it inferred the slot and value from the last journal entry of the step. A warm `SLOAD` (the slot is in the access list, or an earlier opcode touched it) adds no journal entry and got no snapshot, an `SSTORE` that writes the unchanged value got none either, and a step that fails before it runs, such as an `SSTORE` that runs out of gas, still printed a snapshot because the journal had already recorded the slot. Now the inspector records the key and value of every `SLOAD` and `SSTORE` on the step: the key and written value come from the stack before the opcode runs, and the value an `SLOAD` read comes from the state when the opcode ends (it does not change the slot). The builder adds that slot to one map per contract for the whole transaction, without undoing it on a revert, and snapshots the map at each such step, except at a step that fails before it runs. This is what `OnOpcode` does in `eth/tracers/logger/logger.go` at master a0cba163. On the parity harness (offline, #859 plus this branch, `structlog` tracer only, empty known-diffs file), `structLogs[].storage` differences fall from 17 lines in 5 cases (`call_revert_gas_accounting`, `eip7702_delegation`, `oog_cold_sstore`, `static_call_violation`, `tx_access_list`, across forks) to none. For example, before the change `oog_cold_sstore` printed a snapshot on the failed `SSTORE` that geth omits, and `tx_access_list` printed no snapshot for a warm `SLOAD`. The regression test `struct_log_storage_snapshots_match_geth` covers a cold `SLOAD`, a warm `SLOAD`, an `SSTORE` of the unchanged value, an `SSTORE` of a new value, and a failing `SSTORE`. Once #859 is on main, this PR removes the `storage` entry from `OMIT_EMPTY` in `scripts/geth_parity.py` and the matching known-diff entries. --- crates/inspectors/src/tracing/builder/geth.rs | 16 +-- .../src/tracing/builder/geth/steps_tests.rs | 30 ++--- crates/inspectors/src/tracing/mod.rs | 28 +++++ crates/inspectors/src/tracing/types.rs | 5 + crates/inspectors/tests/it/geth.rs | 117 ++++++++++++++++++ 5 files changed, 172 insertions(+), 24 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index e0c62cb18..0bcdca8ac 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -117,19 +117,15 @@ impl<'a> GethTraceBuilder<'a> { let mut log = step.convert_to_geth_struct_log(opts, trace_node.trace.depth as u64 + 1, stack); - // Only touch the storage cache when updating it or emitting a storage snapshot. + // geth snapshots the storage of the contract, with the slots of all its earlier + // SLOADs and SSTOREs, unless the opcode fails before it runs. if opts.is_storage_enabled() - && (step.storage_change.is_some() - || matches!(step.op.get(), op::SLOAD | op::SSTORE)) + && !step.is_error() + && let Some((key, value)) = step.storage_slot.as_deref().copied() { let contract_storage = storage.entry(trace_node.execution_address()).or_default(); - if let Some(change) = &step.storage_change { - contract_storage.insert(change.key.into(), change.value.into()); - } - - if matches!(step.op.get(), op::SLOAD | op::SSTORE) { - log.storage = Some(contract_storage.clone()); - } + contract_storage.insert(key.into(), value.into()); + log.storage = Some(contract_storage.clone()); } if opts.is_return_data_enabled() && !step.returndata.is_empty() { diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index 18518c152..bb1f537c4 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -30,6 +30,7 @@ fn test_step(pc: usize, op: u8) -> CallTraceStep { reason: StorageChangeReason::SSTORE, }) }), + storage_slot: (op == op::SSTORE).then(|| Box::new((U256::ZERO, U256::from(pc)))), status: (op == op::REVERT).then_some(InstrStop::Revert), immediate_bytes: None, decoded: None, @@ -206,6 +207,8 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { }) .collect(); nodes[0].trace.steps.last_mut().unwrap().status = Some(InstrStop::OutOfGas); + // The SLOAD reads the value its delegatecall child wrote. + nodes[2].trace.steps[2].storage_slot = Some(Box::new((U256::ZERO, U256::from(30)))); let builder = GethTraceBuilder::new(nodes); let expected = [ (0, 1), @@ -284,18 +287,17 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { } #[test] -fn storage_snapshots_keep_empty_reads_and_custom_changes() { - let mut custom_change = test_step(2, op::SSTORE); - // Public trace nodes can carry a storage change on a different opcode. - custom_change.op = OpCode::new_or_unknown(op::ADD); +fn storage_snapshots_accumulate_and_skip_failed_steps() { + let mut failed = test_step(4, op::SSTORE); + failed.status = Some(InstrStop::OutOfGas); + let mut sload = test_step(1, op::SLOAD); + sload.storage_slot = Some(Box::new((U256::ZERO, U256::from(1)))); + // A step that is not an SLOAD or SSTORE has no slot. + let mut add = test_step(2, op::ADD); + add.storage_slot = None; let builder = GethTraceBuilder::new(vec![CallTraceNode { trace: CallTrace { - steps: vec![ - test_step(0, op::PUSH0), - test_step(1, op::SLOAD), - custom_change, - test_step(3, op::SLOAD), - ], + steps: vec![test_step(0, op::PUSH0), sload, add, failed], ..Default::default() }, ..Default::default() @@ -311,11 +313,11 @@ fn storage_snapshots_keep_empty_reads_and_custom_changes() { ); assert_eq!(frame.struct_logs.len(), 4); assert!(frame.struct_logs[0].storage.is_none()); - assert_eq!(frame.struct_logs[1].storage, (!disable_storage).then(BTreeMap::new)); - assert!(frame.struct_logs[2].storage.is_none()); assert_eq!( - frame.struct_logs[3].storage, - (!disable_storage).then(|| { BTreeMap::from([(B256::ZERO, U256::from(2).into())]) }) + frame.struct_logs[1].storage, + (!disable_storage).then(|| BTreeMap::from([(B256::ZERO, U256::from(1).into())])) ); + assert!(frame.struct_logs[2].storage.is_none()); + assert!(frame.struct_logs[3].storage.is_none()); } } diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 59824c776..2fbaa9c06 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -536,6 +536,12 @@ impl TracingInspector { self.last_journal_len = interp.host().state().journal().len(); + let storage_slot = if self.config.record_state_diff { + storage_slot(interp.stack().as_slice(), op.get()) + } else { + None + }; + let step_idx = node.trace.steps.len(); node.trace.steps.push(CallTraceStep { pc: interp.pc(), @@ -558,6 +564,7 @@ impl TracingInspector { .then_some(interp.gas().reservoir()), state_gas_spent: interp.gas().state_gas_spent(), storage_change: None, + storage_slot, status: None, // This is never populated in `TracingInspector`. @@ -622,6 +629,7 @@ impl TracingInspector { ); } + let address = interp.message().destination; let host = interp.host(); let journal = host.state().journal(); @@ -657,6 +665,13 @@ impl TracingInspector { }; } + // The slot an SLOAD read is loaded now, and the SLOAD did not change it. + if let Some(slot) = &mut step.storage_slot + && step.op.get() == op::SLOAD + { + slot.1 = host.state().get_storage(&address, &slot.0).unwrap_or_default(); + } + // An out-of-gas step consumes the whole remainder, except the reentrancy sentry, which // halts before any charge. step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { @@ -954,6 +969,19 @@ impl From for TransactionContext { } } +/// Returns the key and value of the storage slot an SSTORE is about to write, or the key of the +/// slot an SLOAD is about to read, with a zero value that `step_end` replaces. +fn storage_slot(stack: &[U256], op: u8) -> Option> { + match op { + op::SLOAD => Some(Box::new((*stack.last()?, U256::ZERO))), + op::SSTORE => { + let [.., value, key] = stack else { return None }; + Some(Box::new((*key, *value))) + } + _ => None, + } +} + /// Returns the memory range whose contents after execution the opcode reports, derived from its /// inputs on the stack. /// diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 4e75fca5a..22f3088f3 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -678,6 +678,11 @@ pub struct CallTraceStep { pub state_gas_spent: i64, /// Change of the contract state after step execution (effect of the SLOAD/SSTORE instructions) pub storage_change: Option>, + /// The key and value a geth struct log snapshots for an SLOAD or SSTORE: the value the slot + /// holds before an SLOAD, or the value an SSTORE writes. + /// + /// Only set if state diffs are recorded. + pub storage_slot: Option>, /// Final status of the step /// /// This is set after the step was executed. diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index f35ee8473..59e548ec5 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -22,6 +22,7 @@ use evm2_inspectors::tracing::{ DebugInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimits, TracingInspector, TracingInspectorConfig, }; +use std::collections::BTreeMap; #[test] fn test_calltracer_invalid_opcode_errors() { @@ -2310,3 +2311,119 @@ fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { } } } + +/// The expected snapshots come from go-ethereum master a0cba163: the struct logger adds the slot +/// of every SLOAD and SSTORE that does not fail before it runs, with the value an SLOAD reads or an +/// SSTORE writes, and snapshots all slots of the contract so far. +#[test] +fn struct_log_storage_snapshots_match_geth() { + let account = address!("1000000000000000000000000000000000000001"); + let slot = |slot: u8| B256::with_last_byte(slot); + let value = |value: u8| U256::from(value).into(); + // SLOAD 5 (holds 7), SSTORE 5 = 7 (unchanged), SLOAD 5 (warm), SSTORE 6 = 1, SLOAD 6, and an + // SSTORE that runs out of gas, which fails before it runs. + let code = hex!("60055450600760055560055450600160065560065450600160075500"); + let mut db = CacheDB::::default(); + db.insert_account_info( + &account, + AccountInfo { + code: Some(Bytecode::new_raw(Bytes::from(code.to_vec()))), + ..Default::default() + }, + ); + db.insert_account_storage(&account, &U256::from(5), &U256::from(7)); + let mut inspector = DebugInspector::new(Default::default()).unwrap(); + let mut evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .build_mainnet() + .with_inspector(&mut inspector); + let result = evm + .inspect_tx(TxEnv { + gas_limit: 21000 + 3 * 1000 + 30000, + kind: TransactTo::Call(account), + ..Default::default() + }) + .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &result.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Default(frame) = trace else { panic!("expected a default trace") }; + + let snapshots: Vec<_> = frame + .struct_logs + .iter() + .filter(|log| matches!(log.op.as_ref(), "SLOAD" | "SSTORE")) + .map(|log| (log.op.to_string(), log.storage.clone(), log.error.is_some())) + .collect(); + let after_first = BTreeMap::from([(slot(5), value(7))]); + let after_sstore = BTreeMap::from([(slot(5), value(7)), (slot(6), value(1))]); + assert_eq!(snapshots[0], ("SLOAD".into(), Some(after_first.clone()), false)); + assert_eq!(snapshots[1], ("SSTORE".into(), Some(after_first.clone()), false)); + assert_eq!(snapshots[2], ("SLOAD".into(), Some(after_first), false)); + assert_eq!(snapshots[3].1, Some(after_sstore.clone())); + assert_eq!(snapshots[4], ("SLOAD".into(), Some(after_sstore), false)); + assert_eq!((snapshots[5].1.clone(), snapshots[5].2), (None, true)); +} + +/// A delegatecall child writes to the storage of its caller, and an SSTORE that fails in a static +/// call leaves no snapshot. +#[test] +fn struct_log_storage_snapshots_follow_the_storage_context() { + let parent = address!("1000000000000000000000000000000000000001"); + let writer = address!("00000000000000000000000000000000000000c0"); + let static_writer = address!("00000000000000000000000000000000000000d0"); + // The parent delegatecalls the writer, loads the slot it wrote, then static calls the second + // writer. + let parent_code = hex!( + "600060006000600060c05af450" + "60015450" + "600060006000600060d05afa5000" + ); + let mut db = CacheDB::::default(); + for (address, code) in [ + (parent, parent_code.to_vec()), + (writer, hex!("6002600155").to_vec()), + (static_writer, hex!("6003600455").to_vec()), + ] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + let mut inspector = DebugInspector::new(Default::default()).unwrap(); + let mut evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .build_mainnet() + .with_inspector(&mut inspector); + let result = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(parent), + ..Default::default() + }) + .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &result.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Default(frame) = trace else { panic!("expected a default trace") }; + + let snapshot = BTreeMap::from([(B256::with_last_byte(1), U256::from(2).into())]); + let logs: Vec<_> = frame + .struct_logs + .iter() + .filter(|log| matches!(log.op.as_ref(), "SLOAD" | "SSTORE")) + .map(|log| (log.depth, log.op.to_string(), log.storage.clone())) + .collect(); + assert_eq!( + logs, + [ + (2, "SSTORE".to_string(), Some(snapshot.clone())), + (1, "SLOAD".to_string(), Some(snapshot)), + (2, "SSTORE".to_string(), None), + ] + ); +} From dc4a776676229a174a8217776a49cfcc2daac19a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:33:44 +0100 Subject: [PATCH 018/159] fix(inspectors): leave state gas out of step costs (#852) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #744. The geth reference is now go-ethereum master at `a0cba163b1f17f6044df84502ff13036dffb5de1` (`evm 1.17.11-unstable-a0cba163`, Amsterdam-capable) instead of 1.17.10. On master the interpreter passes each opcode's execution gas and state gas separately (`core/vm/interpreter.go`), and the struct logger records the execution gas only (`core/tracing/hooks.go`, `EmitOpcode`). State gas is never part of the reported cost: `gasCreateEip8037` leaves it out and `chargeAccountCreation` charges it inside `opCreate` after the tracer hook (`core/vm/evm.go`, `core/vm/instructions.go`), and `makeCallVariantGasCallEIP8037` charges the new-account state gas directly and leaves it out of `totalCost` (`core/vm/operations_acl.go`). `Interpreter::step_cost()` took the delta of the remaining gas, so on Amsterdam it also counted state gas drawn from regular gas when the reservoir was short, and missed state gas refilled into it. It now counts the remaining gas together with the spilled state gas (the new public `Gas::remaining_excluding_state_spill` and `GasTracker::remaining_excluding_state_spill`), at step start and at the charge mark, so spills and refills cancel for every opcode. The state gas still shows in the step's `state_gas_cost`. The CREATE and CALL marks keep their place; the state gas of both stays out of the cost because of this change, not because of where the mark sits. Every geth-derived value in `parity.rs` was rerun against the pinned build on Prague (unchanged from 1.17.10) and Amsterdam, and the call, call-family, precompile, unfunded and create cases now assert both forks in vmTrace and struct logs. A new `state_gas_cost` module covers SSTORE to a new slot, a restoring SSTORE, SELFDESTRUCT to a new account, value CALL to a new account and CREATE, each with an empty, a short (100,000 gas) and a full reservoir. The Cyclops finding on #744 "Parity vmTrace ex.used understates remaining gas after calls when step deltas are off" no longer holds on main: a new test builds the vmTrace from `default_geth()`, `parity_vm_trace()`, the same without step deltas and `all()`, and checks that a call or create step's `ex.used` equals the parent's gas after the child returns, for CALL, CREATE and CREATE2 on Prague and Amsterdam and for a nested call. It passes on main as well, so no code change was needed. Cyclops should recheck it. Step costs that change against main, all on Amsterdam with an empty reservoir (limit 1,000,000), against geth a0cba163: | Step | main | this PR | geth a0cba163 | | --- | --- | --- | --- | | SSTORE to a new slot | 110,020 🔴 | 12,100 | 12,100 | | restoring SSTORE (0→x→0) | 0 🔴 | 100 | 100 | | SELFDESTRUCT to a new account with balance | 200,600 🔴 | 17,000 | 17,000 | | CALL with value 1 to a new account, forwarding 0xffff | 263,435 🔴 | 79,835 | 79,835 | | CALL with value 1 to a new account without funds, forwarding 1000 | 198,900 🔴 | 15,300 | 15,300 | | CREATE / CREATE2 with empty initcode | 195,600 🔴 | 12,000 | 12,000 | With a 100,000 gas reservoir the main values drop by the part the reservoir covers (for example CREATE 95,600 → 12,000); with a reservoir above the charge nothing changes. Prague costs and the Amsterdam costs of calls that need no state gas (cold CALL forwarding 1024 = 4,024; CREATE with initcode `5f5f5f5f00` = 12,002) are the same as on main. The interpreter change is one add in the call and create charge mark and one add at the start of an inspected step. The `evm` criterion bench (interpreter shard, no inspector, one pinned core, median of six interleaved runs against main 34209d58) shows no change beyond run-to-run noise: | case | main | this PR | | --- | --- | --- | | push0_proxy | 1362 ns | 1381 ns | | usdc_proxy | 1502 ns | 1508 ns | | uniswap_v2_pair | 1374 ns | 1379 ns | | univ2_router | 1407 ns | 1401 ns | | seaport | 1556 ns | 1550 ns | #859 (`scripts/geth_parity.py`, geth a0cba163) is not on main yet, so I ran it in a scratch copy of this branch and of main, each with `origin/dani/geth-parity-harness` merged, over all 317 case and fork pairs and 4591 comparisons. Main reports 9774 differences, all known, 0 new, 212 stale known entries. This branch reports 9672 differences, all known, 0 new, 265 stale entries. The 53 additional stale entries are Amsterdam `steps[].gasCost` (jsonl) and `structLogs[].gasCost` (structlog) differences of call_basic, call_nested, call_new_account, call_stipend, callcode_basic, delegatecall_basic, staticcall_basic, create_code_rules, create_init_revert, create_selfdestruct_in_init, create_success, selfdestruct_existing, selfdestruct_new_account, selfdestruct_same_tx, sstore_sload_refund, tx_multi, tx_blob and the precompile cases; the PR that lands after #859 deletes them from `known-diffs.json`. No new difference appears. --- crates/evm2/src/interpreter/gas.rs | 35 +++ .../src/interpreter/instructions/system.rs | 5 +- crates/evm2/src/interpreter/runtime.rs | 30 ++- crates/inspectors/src/tracing/mod.rs | 10 +- crates/inspectors/src/tracing/types.rs | 3 +- crates/inspectors/tests/it/parity.rs | 222 +++++++++++++----- .../tests/it/parity/state_gas_cost.rs | 120 ++++++++++ 7 files changed, 351 insertions(+), 74 deletions(-) create mode 100644 crates/inspectors/tests/it/parity/state_gas_cost.rs diff --git a/crates/evm2/src/interpreter/gas.rs b/crates/evm2/src/interpreter/gas.rs index 6689d3d0e..f314cdc7b 100644 --- a/crates/evm2/src/interpreter/gas.rs +++ b/crates/evm2/src/interpreter/gas.rs @@ -265,6 +265,16 @@ impl GasTracker { self.state_gas_spilled } + /// Returns the remaining gas plus the state gas drawn from it, which is the remaining gas had + /// the reservoir covered every state charge. + /// + /// The difference between two readings is the execution gas charged in between: a spill of + /// state gas into the remaining gas cancels out, as does a refill of it. + #[inline] + pub const fn remaining_excluding_state_spill(&self) -> u64 { + self.remaining.saturating_add(self.state_gas_spilled) + } + /// Adds `delta` to the spilled state gas, saturating. /// /// Used to merge a successful child frame's spilled state gas into this @@ -629,6 +639,13 @@ impl Gas { self.tracker.state_gas_spilled() } + /// Returns the remaining gas plus the state gas drawn from it. + /// See [`GasTracker::remaining_excluding_state_spill`]. + #[inline] + pub const fn remaining_excluding_state_spill(&self) -> u64 { + self.tracker.remaining_excluding_state_spill() + } + /// Adds `delta` to the spilled state gas, saturating. /// See [`GasTracker::add_state_gas_spilled`]. #[inline] @@ -832,6 +849,24 @@ mod tests { assert_eq!((gas.state_gas_spilled(), gas.remaining()), (400, 600)); } + #[test] + fn test_remaining_excluding_state_spill() { + let mut gas = Gas::new_with_execution_gas_and_reservoir(1000, 500); + assert_eq!(gas.remaining_excluding_state_spill(), 1000); + + // A charge the reservoir covers and a spill into the remaining gas do not move it. + assert!(gas.spend_state(300).is_ok()); + assert!(gas.spend_state(500).is_ok()); + assert_eq!((gas.remaining(), gas.state_gas_spilled()), (700, 300)); + assert_eq!(gas.remaining_excluding_state_spill(), 1000); + + // Execution gas does, and a refill of the spill does not. + assert!(gas.spend(100).is_ok()); + assert_eq!(gas.remaining_excluding_state_spill(), 900); + gas.refill_reservoir(300); + assert_eq!(gas.remaining_excluding_state_spill(), 900); + } + #[test] fn test_rollback_state_gas_no_spill() { // Pure-reservoir spend: unwind restores the reservoir, leaving execution gas alone. diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index f9ba38dcf..848a5389c 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -140,7 +140,7 @@ fn load_acc_and_calc_gas( stack_gas_limit }; gas.spend(gas_limit)?; - state.finish_step_charge(gas.remaining()); + state.finish_step_charge(gas); if transfers_value { gas_limit = gas_limit.saturating_add(state.gas_params().get(GasId::CallStipend)); @@ -367,7 +367,8 @@ fn create_inner( charged_create_state_gas = true; } } - state.finish_step_charge(gas.remaining()); + // The state gas charged above spills into regular gas but stays out of the step cost. + state.finish_step_charge(gas); let gas_limit = if state.feature(EvmFeatures::EIP150) { state.gas_params().call_stipend_reduction(gas.remaining()) } else { diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index f316612ea..88bc91664 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -39,9 +39,10 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) stack: Box, pub(in crate::interpreter) gas: Gas, - /// Remaining gas at the start of the current inspected step. + /// Remaining gas plus spilled state gas at the start of the current inspected step. step_gas_before: u64, - /// Remaining gas once the current step finished charging, if it marked that point. + /// Remaining gas plus spilled state gas once the current step finished charging, if it + /// marked that point. step_gas_after_charge: Option, pub(in crate::interpreter) result: Result, error: Option, @@ -235,21 +236,24 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.gas = gas; } - /// Returns the gas the current step charged for its execution, before its effects. + /// Returns the execution gas the current step charged, before its effects. /// /// This is the gas remaining at the start of the step minus the gas remaining once the step - /// finished charging. Call and create steps finish charging right before they hand gas to - /// the child, so their cost includes the forwarded gas for calls and excludes it for creates. - /// Any other step finishes charging when it ends, which makes its cost the gas it consumed. + /// finished charging, leaving out state gas that spills into the remaining gas because the + /// reservoir was short or is refilled from it. State gas is not part of the execution cost; + /// [`Gas::state_gas_spent`] reports it. Call and create steps finish charging right before + /// they hand gas to the child, so their cost includes the forwarded gas for calls and + /// excludes it for creates. Any other step finishes charging when it ends, which makes its + /// cost the execution gas it consumed. /// /// Only meaningful in [`Inspector::step_end`](crate::inspector::Inspector::step_end). #[inline] pub const fn step_cost(&self) -> u64 { - let remaining = match self.step_gas_after_charge { - Some(remaining) => remaining, - None => self.gas.remaining(), + let after = match self.step_gas_after_charge { + Some(after) => after, + None => self.gas.remaining_excluding_state_spill(), }; - self.step_gas_before.saturating_sub(remaining) + self.step_gas_before.saturating_sub(after) } /// Returns the current linear memory. @@ -700,7 +704,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { pub(crate) fn inspect_step(&mut self, pc: Pc, stack_len: usize) { self.0.pc = pc.as_ptr(); self.0.stack_len = stack_len; - self.0.step_gas_before = self.0.gas.remaining(); + self.0.step_gas_before = self.0.gas.remaining_excluding_state_spill(); self.0.step_gas_after_charge = None; unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); @@ -726,8 +730,8 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { /// Marks that the current step finished charging gas, so [`Interpreter::step_cost`] stops /// counting gas after this point. #[inline] - pub(crate) const fn finish_step_charge(&mut self, remaining: u64) { - self.0.step_gas_after_charge = Some(remaining); + pub(crate) const fn finish_step_charge(&mut self, gas: &Gas) { + self.0.step_gas_after_charge = Some(gas.remaining_excluding_state_spill()); } #[inline] diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 2fbaa9c06..d6a541529 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -690,11 +690,13 @@ impl TracingInspector { if step.status.is_some_and(|status| status.is_halt()) { return; } - // The gas a child returns and gas credits cannot be recovered from the step cost, so - // record the balance after the step even without memory and storage deltas. + // The gas a child returns, gas credits and state gas drawn from the remaining gas cannot be + // recovered from the step cost, so record the balance after the step even without memory + // and storage deltas. + let remaining = interp.gas().remaining(); let gas_remaining_after = (step.is_call_like_op() - || interp.gas().remaining() > step.gas_remaining) - .then_some(interp.gas().remaining()); + || remaining != step.gas_remaining.saturating_sub(step.gas_cost)) + .then_some(remaining); if gas_remaining_after.is_some() && node.trace.step_deltas.last().is_none_or(|delta| delta.step != step_idx) { diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 22f3088f3..bdb8a2e3f 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -786,7 +786,8 @@ pub struct StepDelta { /// The storage written by an `SSTORE`, taken from its operands, so it is also set when the /// value does not change. pub store: Option, - /// The remaining gas after a call-like step or an instruction that gained gas. + /// The remaining gas after a call-like step or a step whose remaining gas after execution is + /// not `gas_remaining - gas_cost`, such as one that gained gas or drew state gas from it. /// /// For all other steps the remaining gas after execution is `gas_remaining - gas_cost`. pub gas_remaining_after: Option, diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index 55903c692..52b358aec 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -477,27 +477,37 @@ fn inspect_code( child: &[u8], spec: SpecId, config: TracingInspectorConfig, +) -> TracingInspector { + inspect_funded_code(code, child, spec, config, 0, 1_000_000) +} + +/// Runs `code` at `0x42` with `balance` and a transaction gas limit of `gas_limit`. +fn inspect_funded_code( + code: &[u8], + child: &[u8], + spec: SpecId, + config: TracingInspectorConfig, + balance: u64, + gas_limit: u64, ) -> TracingInspector { use crate::utils::Bytecode; let target = Address::with_last_byte(0x42); let child_address = Address::with_last_byte(0x43); let mut db = CacheDB::::default(); - for (address, code) in [(target, code), (child_address, child)] { + for (address, code, balance) in [(target, code, balance), (child_address, child, 0)] { db.insert_account_info( &address, - AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), + AccountInfo::default() + .with_code(Bytecode::new_raw(code.to_vec().into())) + .with_balance(U256::from(balance)), ); } let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) .with_db(db) .build_mainnet_with_inspector(TracingInspector::new(config)); - evm.inspect_tx(TxEnv { - gas_limit: 1_000_000, - kind: TransactTo::Call(target), - ..Default::default() - }) - .unwrap(); + evm.inspect_tx(TxEnv { gas_limit, kind: TransactTo::Call(target), ..Default::default() }) + .unwrap(); evm.into_inspector() } @@ -541,7 +551,8 @@ fn vmtrace_reports_remaining_gas_after_storage_credit() { #[test] fn non_vm_traces_only_record_gas_deltas() { - // Exercise a memory write, a call and a storage gas credit. + // Exercise a memory write, a call, a storage write that draws state gas from the remaining + // gas and a storage gas credit. let code = hex!("602a5f525f5f5f5f5f604361fffff15060015f555f5f5500"); for config in [ TracingInspectorConfig::default_geth(), @@ -557,7 +568,7 @@ fn non_vm_traces_only_record_gas_deltas() { } } if config.record_steps { - assert_eq!(inspector.traces().nodes()[0].trace.step_deltas.len(), 2); + assert_eq!(inspector.traces().nodes()[0].trace.step_deltas.len(), 3); } else { assert!( inspector.traces().nodes().iter().all(|node| node.trace.step_deltas.is_empty()) @@ -1173,8 +1184,16 @@ fn precompile_exclusion_uses_original_callee_with_delegation() { } } +/// Gas of an Amsterdam account creation: 120 state bytes at 1530 gas per byte. +const NEW_ACCOUNT_STATE_GAS: u64 = 183_600; + /// The cost and used gas of the call or create step before the final `STOP`, as reported by /// parity `vmTrace` and geth struct logs under each config. +/// +/// The expected costs in the tests below come from running the same bytecode on go-ethereum +/// `a0cba163b1f17f6044df84502ff13036dffb5de1` (`evm 1.17.11-unstable-a0cba163`) with +/// `evm run --prestate --trace --trace.format=json`. Its interpreter reports each +/// opcode's execution gas only (core/vm/interpreter.go): state gas is not part of the cost. struct CallStep { /// Gas remaining before the step. gas_remaining: u64, @@ -1187,8 +1206,18 @@ struct CallStep { } fn call_step(code: &[u8], child: &[u8], spec: SpecId) -> CallStep { + call_step_funded(code, child, spec, 0, 1_000_000) +} + +fn call_step_funded( + code: &[u8], + child: &[u8], + spec: SpecId, + balance: u64, + gas_limit: u64, +) -> CallStep { let vm_trace = |config| { - let inspector = inspect_code(code, child, spec, config); + let inspector = inspect_funded_code(code, child, spec, config, balance, gas_limit); let nodes = inspector.traces().nodes(); let step = &nodes[0].trace.steps[nodes[0].trace.steps.len() - 2]; let step = (step.gas_remaining, nodes.get(1).map(|node| node.trace.gas_limit)); @@ -1200,9 +1229,16 @@ fn call_step(code: &[u8], child: &[u8], spec: SpecId) -> CallStep { let (_, cost_without_deltas, used_without_deltas) = vm_trace(TracingInspectorConfig::parity_vm_trace().set_step_deltas(false)); assert_eq!(used_without_deltas, used); - let geth = inspect_code(code, child, spec, TracingInspectorConfig::default_geth()) - .geth_builder() - .geth_traces(0, Default::default(), Default::default()); + let geth = inspect_funded_code( + code, + child, + spec, + TracingInspectorConfig::default_geth(), + balance, + gas_limit, + ) + .geth_builder() + .geth_traces(0, Default::default(), Default::default()); let log = geth.struct_logs.iter().rev().find(|log| log.gas == step.0).unwrap(); assert_eq!(cost_without_deltas, cost); assert_eq!(log.gas_cost, cost); @@ -1211,40 +1247,57 @@ fn call_step(code: &[u8], child: &[u8], spec: SpecId) -> CallStep { #[test] fn call_cost_includes_forwarded_gas() { - for (prefix, opcode) in - [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] - { - for (child, spent) in [(&hex!("00")[..], 0), (&hex!("5f5ffd")[..], 4), (&[][..], 0)] { - let code = hex::decode(format!("{prefix}604361ffff{opcode}00")).unwrap(); - let step = call_step(&code, child, SpecId::PRAGUE); - assert_eq!(step.cost, 2_600 + 65_535); - assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - spent); + for (spec, cold) in [(SpecId::PRAGUE, 2_600), (SpecId::AMSTERDAM, 3_000)] { + for (prefix, opcode) in + [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] + { + for (child, spent) in [(&hex!("00")[..], 0), (&hex!("5f5ffd")[..], 4), (&[][..], 0)] { + let code = hex::decode(format!("{prefix}604361ffff{opcode}00")).unwrap(); + let step = call_step(&code, child, spec); + assert_eq!(step.cost, cold + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - spent); + } } } } #[test] fn call_cost_excludes_stipend() { - // A cold CALL forwarding 1024 gas. - let step = call_step(&hex!("5f5f5f5f5f6043610400f100"), &hex!("00"), SpecId::PRAGUE); - assert_eq!(step.cost, 2_600 + 1_024); - assert_eq!(step.used, step.gas_remaining - step.cost + 1_024); + // A cold call forwarding 1024 gas. + for (spec, cost) in [(SpecId::PRAGUE, 3_624), (SpecId::AMSTERDAM, 4_024)] { + for (prefix, opcode) in + [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] + { + let code = hex::decode(format!("{prefix}6043610400{opcode}00")).unwrap(); + let step = call_step(&code, &hex!("00"), spec); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - step.cost + 1_024); + } + } } #[test] fn precompile_call_cost_includes_forwarded_gas() { - let step = call_step(&hex!("5f5f5f5f5f600461fffff100"), &[], SpecId::PRAGUE); - assert_eq!(step.cost, 100 + 65_535); - assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - 15); + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + let step = call_step(&hex!("5f5f5f5f5f600461fffff100"), &[], spec); + assert_eq!(step.cost, 100 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - 15); + } } #[test] fn unfunded_call_cost_includes_forwarded_gas() { // The child fails with out of funds and returns its gas, including the stipend. The cost - // has the base, cold, value and, for an empty target, new account charges plus the - // forwarded 1000 gas, without the stipend. - for (child, cost) in [(&hex!("00")[..], 2_600 + 9_000 + 1_000), (&[][..], 37_600)] { - let step = call_step(&hex!("5f5f5f5f600160436103e8f100"), child, SpecId::PRAGUE); + // has the base, cold, value and, before Amsterdam, new account charges plus the forwarded + // 1000 gas, without the stipend. Amsterdam charges the new account as state gas, which the + // failed call gives back. + for (spec, child, cost) in [ + (SpecId::PRAGUE, &hex!("00")[..], 2_600 + 9_000 + 1_000), + (SpecId::PRAGUE, &[][..], 37_600), + (SpecId::AMSTERDAM, &hex!("00")[..], 3_000 + 11_300 + 1_000), + (SpecId::AMSTERDAM, &[][..], 3_000 + 11_300 + 1_000), + ] { + let step = call_step(&hex!("5f5f5f5f600160436103e8f100"), child, spec); assert_eq!(step.child_gas_limit, Some(1_000 + 2_300)); assert_eq!(step.cost, cost); assert_eq!(step.used, step.gas_remaining - cost + 3_300); @@ -1253,23 +1306,28 @@ fn unfunded_call_cost_includes_forwarded_gas() { #[test] fn create_cost_excludes_forwarded_gas() { - for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { - let step = call_step(code, &[], SpecId::PRAGUE); - let available = step.gas_remaining - 32_000; - assert_eq!(step.cost, 32_000); - assert_eq!(step.child_gas_limit, Some(available - available / 64)); - assert_eq!(step.used, available); + for (spec, cost, state_gas) in + [(SpecId::PRAGUE, 32_000, 0), (SpecId::AMSTERDAM, 12_000, NEW_ACCOUNT_STATE_GAS)] + { + for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { + let step = call_step(code, &[], spec); + let available = step.gas_remaining - cost - state_gas; + assert_eq!(step.cost, cost); + assert_eq!(step.child_gas_limit, Some(available - available / 64)); + assert_eq!(step.used, available); + } } } #[test] fn create_cost_counts_all_gas_charged_before_the_child_split() { // Amsterdam charges the account creation state gas, which spills into regular gas when the - // reservoir is empty. - for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + // reservoir is empty. It is not part of the cost but the child gets 63/64 of what is left + // after it. + for (spec, state_gas) in [(SpecId::PRAGUE, 0), (SpecId::AMSTERDAM, NEW_ACCOUNT_STATE_GAS)] { for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { let step = call_step(code, &[], spec); - let remaining = step.gas_remaining - step.cost; + let remaining = step.gas_remaining - step.cost - state_gas; assert_eq!(step.child_gas_limit, Some(remaining - remaining / 64)); } } @@ -1278,25 +1336,78 @@ fn create_cost_counts_all_gas_charged_before_the_child_split() { #[test] fn create_cost_counts_initcode_memory_and_hash_words() { // The initcode `5f5f5f5f00` spends 8 gas. - for (code, cost) in [ - (&hex!("645f5f5f5f005f526005601b5ff000")[..], 32_002), - (&hex!("645f5f5f5f005f526005601b5f5ff500")[..], 32_008), - (&hex!("6001600052600160ff5ff000")[..], 32_023), - ] { - let step = call_step(code, &[], SpecId::PRAGUE); - assert_eq!(step.cost, cost); + for (spec, base, state_gas) in + [(SpecId::PRAGUE, 32_000, 0), (SpecId::AMSTERDAM, 12_000, NEW_ACCOUNT_STATE_GAS)] + { + for (code, cost) in [ + (&hex!("645f5f5f5f005f526005601b5ff000")[..], base + 2), + (&hex!("645f5f5f5f005f526005601b5f5ff500")[..], base + 8), + (&hex!("6001600052600160ff5ff000")[..], base + 23), + ] { + let step = call_step(code, &[], spec); + assert_eq!(step.cost, cost); + } + let step = call_step(&hex!("645f5f5f5f005f526005601b5ff000"), &[], spec); + assert_eq!(step.used, step.gas_remaining - step.cost - state_gas - 8); } - let step = call_step(&hex!("645f5f5f5f005f526005601b5ff000"), &[], SpecId::PRAGUE); - assert_eq!(step.used, step.gas_remaining - step.cost - 8); } #[test] fn unfunded_create_cost_excludes_forwarded_gas() { let code = hex!("5f5f6001f000"); assert!(trace_vm_code(&code, &[]).ops[3].sub.is_none()); - let step = call_step(&code, &[], SpecId::PRAGUE); - assert_eq!(step.cost, 32_000); - assert_eq!(step.used, step.gas_remaining - 32_000); + for (spec, cost) in [(SpecId::PRAGUE, 32_000), (SpecId::AMSTERDAM, 12_000)] { + let step = call_step(&code, &[], spec); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - cost); + } +} + +#[test] +fn call_like_step_used_gas_is_the_gas_after_the_child_returns() { + // `ex.used` of a call or create step is the parent's gas once the child returned, which the + // next step reports as its remaining gas, whatever the config records. + let codes = [&hex!("5f5f5f5f5f604361fffff100")[..], &hex!("5f5f5ff000"), &hex!("5f5f5f5ff500")]; + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + for code in codes { + for child in [&hex!("00")[..], &hex!("5f5ffd"), &[]] { + for config in [ + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + TracingInspectorConfig::all(), + ] { + let inspector = inspect_code(code, child, spec, config); + let after = + inspector.traces().nodes()[0].trace.steps.last().unwrap().gas_remaining; + let trace = inspector.into_parity_builder().vm_trace(); + let used = trace.ops[trace.ops.len() - 2].ex.as_ref().unwrap().used; + assert_eq!(used, after, "{code:x?} {child:x?} {spec:?}"); + } + } + } + } +} + +#[test] +fn nested_call_step_used_gas_is_the_gas_after_the_child_returns() { + let code = hex!("5f5f5f5f5f604361fffff100"); + let child = hex!("5f5f5f5f5f600461fffff100"); + for config in [ + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let inspector = inspect_code(&code, &child, SpecId::AMSTERDAM, config); + let nodes = inspector.traces().nodes(); + let after = + [&nodes[0], &nodes[1]].map(|node| node.trace.steps.last().unwrap().gas_remaining); + let trace = inspector.into_parity_builder().vm_trace(); + let outer = &trace.ops[trace.ops.len() - 2]; + let inner = &outer.sub.as_ref().unwrap().ops; + let inner = &inner[inner.len() - 2]; + assert_eq!(outer.ex.as_ref().unwrap().used, after[0]); + assert_eq!(inner.ex.as_ref().unwrap().used, after[1]); + } } #[test] @@ -1328,3 +1439,6 @@ mod state_diff_selfdestruct; #[path = "parity/oog_cost.rs"] mod oog_cost; + +#[path = "parity/state_gas_cost.rs"] +mod state_gas_cost; diff --git a/crates/inspectors/tests/it/parity/state_gas_cost.rs b/crates/inspectors/tests/it/parity/state_gas_cost.rs new file mode 100644 index 000000000..fff29f163 --- /dev/null +++ b/crates/inspectors/tests/it/parity/state_gas_cost.rs @@ -0,0 +1,120 @@ +//! Amsterdam step costs leave out state gas, in both trace formats. +//! +//! The expected values come from go-ethereum `a0cba163b1f17f6044df84502ff13036dffb5de1` +//! (`evm 1.17.11-unstable-a0cba163`): the interpreter passes each opcode's execution and state +//! gas separately (core/vm/interpreter.go) and the struct logger records the execution gas only +//! (core/tracing/hooks.go, `EmitOpcode`). + +use super::*; + +/// Transaction gas limit that leaves a reservoir of 100,000 gas, below the 183,600 gas of an +/// account creation: the gas above the 2^24 transaction cap. +const SHORT_RESERVOIR_GAS_LIMIT: u64 = 16_777_216 + 100_000; +/// Transaction gas limit that leaves a reservoir larger than any state charge below. +const FULL_RESERVOIR_GAS_LIMIT: u64 = 20_000_000; + +/// Returns the cost of each step of `code` in parity `vmTrace` and in geth struct logs, which +/// must agree, and the state gas of each step. +fn step_costs(code: &[u8], balance: u64, gas_limit: u64) -> (Vec, Vec>) { + let inspect = + |config| inspect_funded_code(code, &[], SpecId::AMSTERDAM, config, balance, gas_limit); + let inspector = inspect(TracingInspectorConfig::all()); + let steps = &inspector.traces().nodes()[0].trace.steps; + let costs = steps.iter().map(|step| step.gas_cost).collect::>(); + let state_costs = steps.iter().map(|step| step.state_gas_cost).collect(); + for config in [ + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let vm_trace = inspect(config).into_parity_builder().vm_trace(); + assert_eq!(vm_trace.ops.iter().map(|op| op.cost).collect::>(), costs); + // The gas left after a step is the gas the next step starts with. + for (op, next) in vm_trace.ops.iter().zip(steps.iter().skip(1)) { + assert_eq!(op.ex.as_ref().unwrap().used, next.gas_remaining); + } + } + let geth = inspect(TracingInspectorConfig::default_geth()).geth_builder().geth_traces( + 0, + Default::default(), + Default::default(), + ); + assert_eq!(geth.struct_logs.iter().map(|log| log.gas_cost).collect::>(), costs); + (costs, state_costs) +} + +#[test] +fn sstore_cost_leaves_out_state_gas() { + // A new slot costs 12,100 gas and 64 state bytes, drawn from the remaining gas when the + // reservoir is empty. + for gas_limit in [1_000_000, FULL_RESERVOIR_GAS_LIMIT] { + let (costs, state_costs) = step_costs(&hex!("602a5f5500"), 0, gas_limit); + assert_eq!(costs[2], 12_100); + assert_eq!(state_costs[2], Some(64 * 1_530)); + } +} + +#[test] +fn restoring_sstore_cost_leaves_out_state_gas_refill() { + // Writing the original zero back refills the state gas of the first write, which returns + // to the remaining gas when it spilled into it. The cost stays 100. + for gas_limit in [1_000_000, SHORT_RESERVOIR_GAS_LIMIT, FULL_RESERVOIR_GAS_LIMIT] { + let (costs, _) = step_costs(&hex!("602a5f555f5f5500"), 0, gas_limit); + assert_eq!(costs[2], 12_100); + assert_eq!(costs[5], 100); + } +} + +#[test] +fn selfdestruct_cost_leaves_out_state_gas() { + // Sending the balance to a new account costs 17,000 gas plus the account creation state + // gas, which the remaining gas pays when the reservoir is short. + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let code = hex!("6044ff"); + let (costs, state_costs) = step_costs(&code, 16, gas_limit); + assert_eq!(costs[1], 17_000); + assert_eq!(state_costs[1], Some(NEW_ACCOUNT_STATE_GAS as i64)); + for config in [ + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let inspector = + inspect_funded_code(&code, &[], SpecId::AMSTERDAM, config, 16, gas_limit); + let remaining = inspector.traces().nodes()[0].trace.steps[1].gas_remaining; + let trace = inspector.into_parity_builder().vm_trace(); + assert_eq!(trace.ops[1].ex.as_ref().unwrap().used, remaining - 17_000 - spilled); + } + } +} + +#[test] +fn value_call_to_new_account_cost_leaves_out_state_gas() { + // A cold CALL with value forwarding 0xffff gas to a new account. The child returns its gas + // and the stipend. + let code = hex!("5f5f5f5f6001604461fffff100"); + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let step = call_step_funded(&code, &[], SpecId::AMSTERDAM, 16, gas_limit); + assert_eq!(step.cost, 3_000 + 11_300 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost - spilled + 65_535 + 2_300); + } +} + +#[test] +fn create_cost_leaves_out_state_gas_spilled_from_a_short_reservoir() { + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let step = call_step_funded(&hex!("5f5f5ff000"), &[], SpecId::AMSTERDAM, 0, gas_limit); + assert_eq!(step.cost, 12_000); + assert_eq!(step.used, step.gas_remaining - 12_000 - spilled); + } +} From 87979ac036c5fda8fdd53a9a553945fa595c490f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:38:22 +0100 Subject: [PATCH 019/159] fix(evm): report nested create nonce overflow as a failed frame (#850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A nested `CREATE` or `CREATE2` whose creator nonce is `u64::MAX` returned `InstrStop::Return` from the EVM as a stand-in, and `TracingInspector::create_end` rebuilt `NonceOverflow` from `depth > initial && created_address.is_none() && stop == Return`. Every other inspector (the JavaScript tracer, any custom `create_end`) saw a successful create with no error. go-ethereum fails the frame with `ErrNonceUintOverflow` from `createFramePreCheck` (`core/vm/evm.go:506-518`): `OnExit` fires with that error and `reverted = true`, no gas is used, the gas goes back to the parent, and the return data buffer is cleared. `NonceOverflow` is now a real stop and counts as a revert-class precheck failure beside `CallTooDeep` and `OutOfFunds`, so `merge_child_gas` still returns the whole forwarded gas and `CREATE` still clears return data. Gas, state and logs do not change. The inspector patch and the matching `NonceOverflow` special case in the call frame builder are gone. Since Amsterdam, `chargeAccountCreation` runs the same precheck in the parent frame (`core/vm/evm.go:530-538`) and fires no child frame; evm2 already does this in `create_inner` (depth, balance, nonce, then clear return data and push zero), and a new test pins it. With the parity harness of #859 (geth master `a0cba163`), the `create_nonce_overflow` case (two nested creates from a creator at nonce `u64::MAX`, run on Cancun, Prague, Osaka and Amsterdam) compares equal for the JavaScript tracer after this change. Before it, evm2 gave the two create frames no `error`, where geth reports `nonce uint64 overflow`, so #859 lists the case under the known `js-frames` `frames[].error` differences. With this change the script reports: ``` STALE known diff no longer occurs, delete it: create_nonce_overflow Cancun js-frames frames[].error STALE known diff no longer occurs, delete it: create_nonce_overflow Prague js-frames frames[].error STALE known diff no longer occurs, delete it: create_nonce_overflow Osaka js-frames frames[].error ``` The Amsterdam run has no difference either: the parent runs the precheck and opens no child frame in both. This PR removes the three entries once #859 is on main. The call, geth and parity trace outputs of `TracingInspector` do not change, because the old patch produced the same values. Other differences of that case (`gasPrice`, JSONL memory prefix, CREATE step `gasCost`) are unrelated and belong to other work. Benchmarks, `cargo bench -p evm2-cli --bench evm` interpreter shard, pinned to one core, median of three runs per commit, main against this branch: | bench | main | this PR | ratio | | --- | --- | --- | --- | | weth | 1.937 µs | 1.904 µs | 0.983 | | erc20_transfer | 1289.3 µs | 1291.3 µs | 1.001 | | uniswap_v2_pair | 1.359 µs | 1.395 µs | 1.026 | | univ2_router | 1.370 µs | 1.375 µs | 1.003 | | seaport | 1.593 µs | 1.546 µs | 0.970 | | usdc_proxy | 1.465 µs | 1.501 µs | 1.024 | | universal_router | 30.033 µs | 29.935 µs | 0.997 | | uniswap_v3_pool | 9.115 µs | 9.145 µs | 1.003 | | snailtracer | 29713 µs | 29716 µs | 1.000 | | factorial | 28.613 µs | 28.659 µs | 1.002 | Ratios stay within ±3% in both directions with no consistent sign, which is the run-to-run noise on this machine. --- crates/evm2/examples/precompile_subcall.rs | 2 +- crates/evm2/src/evm/mod.rs | 6 ++--- crates/evm2/src/evm/tx.rs | 17 +++++++++----- crates/evm2/src/interpreter/gas.rs | 2 +- crates/evm2/src/interpreter/mod.rs | 20 ++++++++++++---- crates/inspectors/src/tracing/js/mod.rs | 2 +- crates/inspectors/src/tracing/mod.rs | 15 ++---------- crates/inspectors/src/tracing/types.rs | 5 +--- crates/inspectors/src/transfer.rs | 5 +--- crates/inspectors/tests/it/compat.rs | 2 +- crates/inspectors/tests/it/parity.rs | 27 ++++++++++++++++++++++ crates/jit/codegen/src/lib.rs | 5 ++-- 12 files changed, 67 insertions(+), 41 deletions(-) diff --git a/crates/evm2/examples/precompile_subcall.rs b/crates/evm2/examples/precompile_subcall.rs index 1ce49f3ea..48c8aa382 100644 --- a/crates/evm2/examples/precompile_subcall.rs +++ b/crates/evm2/examples/precompile_subcall.rs @@ -91,7 +91,7 @@ fn staticcall_precompile( match result.stop { stop if stop.is_success() => Ok(PrecompileOutput::new(result.output)), - stop if stop.is_revert() => Err(PrecompileError::Revert(result.output)), + stop if stop.is_frame_revert() => Err(PrecompileError::Revert(result.output)), stop => Err(PrecompileHalt::Other(format!("subcall halted with {stop:?}").into()).into()), } } diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 6a8d74ade..7c4a0c2aa 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1449,10 +1449,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { return Err(InstrStop::OutOfFunds.into()); } - // EIP-2681 caps account nonces at u64::MAX; CREATE/CREATE2 return zero instead of - // wrapping or saturating the creator nonce. + // EIP-2681 caps account nonces at u64::MAX; CREATE/CREATE2 fail the frame and return + // zero instead of wrapping or saturating the creator nonce. if nested && info.as_ref().is_some_and(|info| info.nonce == u64::MAX) { - return Err(InstrStop::Return.into()); + return Err(InstrStop::NonceOverflow.into()); } // `destination` already holds the contract address (derived when the message was diff --git a/crates/evm2/src/evm/tx.rs b/crates/evm2/src/evm/tx.rs index c304d5664..c9dcc3f50 100644 --- a/crates/evm2/src/evm/tx.rs +++ b/crates/evm2/src/evm/tx.rs @@ -98,12 +98,12 @@ impl TxResultExt { /// Returns whether execution ended with the `REVERT` opcode. /// - /// Unlike [`InstrStop::is_revert`], this does not count [`InstrStop::CallTooDeep`] or - /// [`InstrStop::OutOfFunds`]. Those are call-frame failures that the interpreter treats as - /// reverts, but a transaction result reports them as halts. + /// Unlike [`InstrStop::is_frame_revert`], this does not count [`InstrStop::CallTooDeep`], + /// [`InstrStop::OutOfFunds`] or [`InstrStop::NonceOverflow`]. Those are call-frame failures + /// that the interpreter treats as reverts, but a transaction result reports them as halts. #[inline] pub const fn is_revert(&self) -> bool { - !self.status && matches!(self.stop, InstrStop::Revert) + !self.status && self.stop.is_revert() } /// Returns whether execution failed for a reason other than the `REVERT` opcode. @@ -428,8 +428,13 @@ mod tests { let revert = with_stop(false, InstrStop::Revert); assert!(!revert.is_success() && revert.is_revert() && !revert.is_halt()); - // Counted by `InstrStop::is_revert`, but not a top-level `REVERT`. - for stop in [InstrStop::OutOfFunds, InstrStop::CallTooDeep, InstrStop::OutOfGas] { + // Counted by `InstrStop::is_frame_revert`, but not a top-level `REVERT`. + for stop in [ + InstrStop::OutOfFunds, + InstrStop::CallTooDeep, + InstrStop::NonceOverflow, + InstrStop::OutOfGas, + ] { let halt = with_stop(false, stop); assert!(!halt.is_success() && !halt.is_revert() && halt.is_halt(), "{stop:?}"); } diff --git a/crates/evm2/src/interpreter/gas.rs b/crates/evm2/src/interpreter/gas.rs index f314cdc7b..0153bbb07 100644 --- a/crates/evm2/src/interpreter/gas.rs +++ b/crates/evm2/src/interpreter/gas.rs @@ -469,7 +469,7 @@ impl GasTracker { /// revert/halt the child's state changes roll back, so it contributes none. #[inline] pub const fn merge_child_gas(&mut self, child: Self, stop: InstrStop) { - if stop.is_success() || stop.is_revert() { + if stop.is_success() || stop.is_frame_revert() { self.erase_cost(child.remaining); } if stop.is_success() { diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index 1f13453a8..1312bcf73 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -65,6 +65,8 @@ pub enum InstrStop { CallTooDeep, /// Insufficient funds for transfer. OutOfFunds, + /// Nonce overflow. + NonceOverflow, // Halt Codes /// Out of gas error. @@ -101,8 +103,6 @@ pub enum InstrStop { CreateCollision, /// Error in precompiled contract execution. PrecompileError, - /// Nonce overflow. - NonceOverflow, /// Exceeded contract size limit during creation. CreateContractSizeLimit, /// Created contract starts with invalid bytes (`0xEF`). @@ -123,16 +123,26 @@ impl InstrStop { matches!(self, Self::Stop | Self::Return | Self::SelfDestruct) } - /// Returns whether execution reverted without an exceptional halt. + /// Returns whether execution ended with the `REVERT` opcode. #[inline] pub const fn is_revert(self) -> bool { - matches!(self, Self::Revert | Self::CallTooDeep | Self::OutOfFunds) + matches!(self, Self::Revert) + } + + /// Returns whether the frame failed without an exceptional halt, so its unused gas returns to + /// the caller. + /// + /// This is the `REVERT` opcode and the failures of a call or create before its frame ran: + /// [`Self::CallTooDeep`], [`Self::OutOfFunds`] and [`Self::NonceOverflow`]. + #[inline] + pub const fn is_frame_revert(self) -> bool { + matches!(self, Self::Revert | Self::CallTooDeep | Self::OutOfFunds | Self::NonceOverflow) } /// Returns whether execution halted exceptionally. #[inline] pub const fn is_halt(self) -> bool { - !self.is_success() && !self.is_revert() + !self.is_success() && !self.is_frame_revert() } /// Returns whether execution ran out of gas or exceeded the memory limit. diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 2fb62d33e..88c7718c6 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -339,7 +339,7 @@ impl JsInspector { if result.status { output_bytes = Some(result.output.clone()); - } else if result.stop.is_revert() { + } else if result.stop.is_frame_revert() { error = Some("execution reverted".to_string()); output_bytes = Some(result.output.clone()); } else { diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index d6a541529..fcfb8f69e 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -891,22 +891,11 @@ impl Inspector for TracingInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, - message: &Message, + _interp: &mut Interpreter<'_, '_, T>, + _message: &Message, result: &mut MessageResult, ) { - let trace_idx = self.last_trace_idx(); self.fill_trace_on_call_end(result); - // A nested CREATE whose creator nonce cannot be incremented returns successfully but - // without an address, leaving its gas unspent. - if message.depth > interp.version().initial_call_depth - && result.created_address.is_none() - && result.stop == InstrStop::Return - { - let trace = &mut self.traces.arena[trace_idx].trace; - trace.status = Some(InstrStop::NonceOverflow); - trace.success = false; - } } fn selfdestruct( diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index bdb8a2e3f..04d06f1cb 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -445,10 +445,7 @@ impl CallTraceNode { call_frame.to = None; } - if !self - .status() - .is_some_and(|status| status.is_revert() || status == InstrStop::NonceOverflow) - { + if !self.status().is_some_and(InstrStop::is_frame_revert) { call_frame.gas_used = U256::from(self.trace.gas_limit); call_frame.output = None; } diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 02c3f5c17..e72f2b2ab 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -207,10 +207,7 @@ impl Inspector for TransferInspector { _message: &Message, result: &mut MessageResult, ) { - // A create that stops early without a created address, such as on nonce overflow, also - // counts as failed even though its stop is `Return`. - let failed = !result.is_success() || result.created_address.is_none(); - self.on_transfer_end(failed); + self.on_transfer_end(!result.is_success()); } fn selfdestruct( diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index ce914266d..2de77fce2 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -406,7 +406,7 @@ impl ExecutionResult { Output::Call(result.output) }; Self::Success { output, gas_used } - } else if result.stop.is_revert() { + } else if result.stop.is_frame_revert() { Self::Revert { output: result.output, gas_used } } else { Self::Halt { reason: result.stop, gas_used } diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index 52b358aec..d7c317b1e 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -1119,6 +1119,33 @@ fn create_nonce_overflow_is_a_failed_frame() { } } +#[test] +fn create_nonce_overflow_runs_no_frame_since_amsterdam() { + let factory = Address::with_last_byte(0x42); + let mut db = CacheDB::::default(); + db.insert_account_info( + &factory, + AccountInfo { + nonce: u64::MAX, + ..AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())) + }, + ); + let mut evm = Context::mainnet() + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .with_db(db) + .build_mainnet_with_inspector(TracingInspector::new( + TracingInspectorConfig::default_parity(), + )); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(factory), + ..Default::default() + }) + .unwrap(); + let traces = evm.inspector.into_parity_builder().into_transaction_traces(); + assert_eq!(traces.len(), 1); +} + #[test] fn create_with_empty_initcode_runs_no_steps() { let factory = Address::with_last_byte(0x42); diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index 6577bb464..39a8611a8 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -18,8 +18,9 @@ pub use compiler::{CompileTimings, EvmCompiler, EvmCompilerInput, HostLayout}; mod linker; pub use linker::{Linker, shared_library_path}; -/// ABI version of compiled artifacts. Bump when the calling convention or execution layout changes. -pub const ABI_VERSION: u32 = 4; +/// ABI version of compiled artifacts. Bump when the calling convention, the execution layout or the +/// `InstrStop` discriminants change. +pub const ABI_VERSION: u32 = 5; /// Internal tests and testing utilities. Not public API. #[cfg(test)] From 8be6fe726931dd67685b99fd5a16cc96f22a275f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:41:46 +0100 Subject: [PATCH 020/159] fix(inspectors): omit empty accounts from the prestate diff's pre state (#853) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In diff mode, geth removes every account that was empty before the transaction from `pre` (OnTxEnd in `prestate.go`). We removed only the accounts the transaction created, so a plain transfer to a fresh account, or a fresh coinbase receiving the tip, was listed in `pre` as `{"balance":"0x0"}`. `pre` now drops every account that was empty before the transaction, and `post` is still diffed against the empty state first. The case below sends 5 wei to an empty account with an empty coinbase and a non-zero tip. geth is `evm t8n --trace.tracer prestateTracer --trace.jsonconfig '{"diffMode":true}'` at go-ethereum `a0cba163`. | Account | geth `pre` | `main` `pre` | this PR `pre` | | :-- | :-- | :-- | :-- | | sender | balance `0x16345785d8a0000` | same | same | | recipient (empty) | absent | `{"balance":"0x0"}` 🔴 | absent 🟢 | | coinbase (empty) | absent | `{"balance":"0x0"}` 🔴 | absent 🟢 | `post` is identical in all three. I also checked that an access-list-only account is not listed (matches geth) and that a non-empty coinbase with a zero tip stays listed in non-diff mode (matches geth). Two prestate options are not covered here and stay known differences until alloy releases them: `includeEmpty` (alloy-rs/alloy#4467 adds the config field) and `codeHash` in pre and post (in alloy `main`, not in a release yet). --- crates/inspectors/src/tracing/builder/geth.rs | 55 +++++----------- crates/inspectors/tests/it/geth.rs | 65 +++++++++++++++++++ 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 0bcdca8ac..155fd3ca2 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -8,11 +8,11 @@ use crate::tracing::{ use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, format, vec, vec::Vec}; use alloy_primitives::{ Address, B256, Bytes, U256, - map::{Entry, HashMap}, + map::{AddressSet, Entry, HashMap}, }; use alloy_rpc_types_trace::geth::{ - AccountChangeKind, AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, - GethDefaultTracingOptions, PreStateConfig, PreStateFrame, PreStateMode, StructLog, + AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, GethDefaultTracingOptions, + PreStateConfig, PreStateFrame, PreStateMode, StructLog, erc7562::{AccessedSlots, CallFrameType, ContractSize, Erc7562Config, Erc7562Frame}, }; use evm2::{ @@ -386,8 +386,7 @@ impl<'a> GethTraceBuilder<'a> { storage_enabled: bool, ) -> DbResult { let mut state_diff = DiffMode::default(); - let mut account_change_kinds = - HashMap::with_capacity_and_hasher(state.accounts.len(), Default::default()); + let mut empty_before = AddressSet::default(); for (addr, changed_acc) in state.accounts.iter() { let addr = *addr; let db_acc = db.get_account(&addr)?.unwrap_or_default(); @@ -425,23 +424,13 @@ impl<'a> GethTraceBuilder<'a> { state_diff.pre.insert(addr, pre_state); - // determine the change type - // if the account was created _and_ not empty we need to treat it as modified, - // so that it is retained later in `diff_traces` + // Geth drops every account that was empty before the transaction from the pre state, + // whether the transaction created it or only funded it. // See // - let pre_change = if changed_acc.created && db_acc.is_empty() { - AccountChangeKind::Create - } else { - AccountChangeKind::Modify - }; - let post_change = if changed_acc.selfdestructed { - AccountChangeKind::SelfDestruct - } else { - AccountChangeKind::Modify - }; - - account_change_kinds.insert(addr, (pre_change, post_change)); + if db_acc.is_empty() { + empty_before.insert(addr); + } // Don't insert selfdestructed accounts into post state if !changed_acc.selfdestructed { @@ -452,36 +441,28 @@ impl<'a> GethTraceBuilder<'a> { // ensure we're only keeping changed entries state_diff.retain_changed().remove_zero_storage_values(); - self.diff_traces(&mut state_diff.pre, &mut state_diff.post, account_change_kinds); + self.diff_traces(&mut state_diff.pre, &mut state_diff.post, &empty_before); Ok(PreStateFrame::Diff(state_diff)) } - /// Returns the difference between the pre and post state of the transaction depending on the - /// kind of changes of that account (pre,post) + /// Returns the difference between the pre and post state of the transaction. + /// + /// `empty_before` holds the accounts that were empty before the transaction. They are removed + /// from `pre` after `post` is diffed against them. fn diff_traces( &self, pre: &mut BTreeMap, post: &mut BTreeMap, - change_type: HashMap, + empty_before: &AddressSet, ) { - post.retain(|addr, post_state| { - // Don't keep destroyed accounts in the post state - if change_type.get(addr).map(|ty| ty.1.is_selfdestruct()).unwrap_or(false) { - return false; - } + for (addr, post_state) in post.iter_mut() { if let Some(pre_state) = pre.get(addr) { // remove any unchanged account info post_state.remove_matching_account_info(pre_state); } + } - true - }); - - // Don't keep created accounts the pre state - pre.retain(|addr, _pre_state| { - // only keep accounts that are not created - change_type.get(addr).map(|ty| !ty.0.is_created()).unwrap_or(true) - }); + pre.retain(|addr, _pre_state| !empty_before.contains(addr)); } /// Traces ERC-7562 calls using the call tracer. diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 59e548ec5..e31b161bc 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -2427,3 +2427,68 @@ fn struct_log_storage_snapshots_follow_the_storage_context() { ] ); } + +/// Geth omits every account that was empty before the transaction from the pre state, including +/// accounts that the transaction only funds, such as a fresh recipient and a fresh coinbase. +#[test] +fn test_geth_prestate_diff_omits_empty_accounts() { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let coinbase = address!("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"); + let recipient = address!("2222222222222222222222222222222222222222"); + + let mut db = CacheDB::new(EmptyDB::default()); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .modify_block_chained(|block| { + block.beneficiary = coinbase; + block.basefee = U256::from(8); + }) + .build_mainnet(); + + let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + gas_price: 0x600, + kind: TransactTo::Call(recipient), + value: U256::from(5), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + + // Output of geth's `evm t8n --trace.tracer prestateTracer --trace.jsonconfig + // '{"diffMode":true}'`. + assert_eq!( + serde_json::to_value(frame).unwrap(), + serde_json::json!({ + "post": { + "0x2222222222222222222222222222222222222222": { "balance": "0x5" }, + "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba": { "balance": "0x1e99fc0" }, + "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": { + "balance": "0x16345785b9dcffb", + "nonce": 1 + } + }, + "pre": { + "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": { "balance": "0x16345785d8a0000" } + } + }) + ); +} From cc86b27d66d5c95f059d6a91370549e0394b3bdc Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:45:08 +0100 Subject: [PATCH 021/159] fix(inspectors): report cleared code in the prestate diff's post state (#858) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In diff mode, geth sets `post.code` whenever the code after the transaction differs from the code before it, and writes `"code":"0x"` when the new code is empty (`processDiffState` in `prestate.go`). We built the post state with `AccountState::from_account_info`, which drops empty code, so an account whose code a transaction cleared had no `code` field in `post` and a reader could not tell the code was gone. `post` now carries empty code when the account had code before the transaction. The case is an EIP-7702 transaction whose authorization to the zero address clears an account's delegation. geth is `evm t8n --state.fork Prague --trace.tracer prestateTracer --trace.jsonconfig '{"diffMode":true}'` at go-ethereum `a0cba163`; the table shows the delegating account's entry. | | geth `post` | `main` `post` | this PR `post` | | :-- | :-- | :-- | :-- | | `code` | `"0x"` | absent 🔴 | `"0x"` 🟢 | | `nonce` | `1` | `1` | `1` | | `pre` | `{"balance":"0x1","code":"0xef0100…aaaa"}` | same | same | geth also prints `codeHash` for these accounts. alloy-rs/alloy#4467 added `include_empty` to the prestate config but no release has `AccountState.code_hash` or `include_empty` yet, so `codeHash` and `includeEmpty` remain known differences until evm2 bumps to a release that has both. --- crates/inspectors/src/tracing/builder/geth.rs | 5 ++ crates/inspectors/tests/it/compat.rs | 26 ++++++- crates/inspectors/tests/it/geth.rs | 67 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 155fd3ca2..6dfe8f4f8 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -414,6 +414,11 @@ impl<'a> GethTraceBuilder<'a> { let mut pre_state = AccountState::from_account_info(db_acc.nonce, db_acc.balance, pre_code); + // Geth reports `"code":"0x"` when the transaction clears the code the account had. + if pre_state.code.is_some() && post_state.code.is_none() { + post_state.code = Some(Bytes::new()); + } + // handle storage changes if storage_enabled { for (key, slot) in changed_acc.changed_storage() { diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index 2de77fce2..e6e890dea 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -1,7 +1,8 @@ //! API shim so that the test files can stay as close to upstream as possible. -use alloy_consensus::{TxEip2930, TxEip4844, TxLegacy, transaction::Recovered}; +use alloy_consensus::{TxEip2930, TxEip4844, TxEip7702, TxLegacy, transaction::Recovered}; use alloy_eip2930::AccessList; +use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; use evm2::{ BaseEvmTypes, Evm, EvmTypesHost, Inspector, NoopInspector, Precompiles, TxResult, @@ -37,6 +38,7 @@ pub struct TxEnv { pub chain_id: Option, pub blob_hashes: Vec, pub max_fee_per_blob_gas: u128, + pub authorization_list: Vec, pub access_list: AccessList, } @@ -54,6 +56,7 @@ impl Default for TxEnv { chain_id: None, blob_hashes: Vec::new(), max_fee_per_blob_gas: 0, + authorization_list: Vec::new(), access_list: AccessList::default(), } } @@ -341,6 +344,27 @@ impl TxEnv { self.caller, ); } + if !self.authorization_list.is_empty() { + let TxKind::Call(to) = self.kind else { panic!("EIP-7702 transactions must be calls") }; + return Recovered::new_unchecked( + TxEnvelope::Eip7702( + TxEip7702 { + chain_id: self.chain_id.unwrap_or(1), + nonce: self.nonce, + gas_limit: self.gas_limit, + max_fee_per_gas: self.gas_price, + max_priority_fee_per_gas: self.gas_priority_fee.unwrap_or(self.gas_price), + to, + value: self.value, + access_list: Default::default(), + authorization_list: self.authorization_list.clone(), + input: self.data.clone(), + } + .into(), + ), + self.caller, + ); + } if !self.access_list.is_empty() { return Recovered::new_unchecked( TxEnvelope::Eip2930(TxEip2930 { diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index e31b161bc..81eda26fa 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -7,6 +7,8 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, ETH_TRANSFER_LOG_ADDRESS, EmptyDB, SpecId, TransactTo, TxEnv, deploy_contract, op, }; +use alloy_consensus::crypto::secp256k1::sign_message; +use alloy_eips::eip7702::Authorization; use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, keccak256, map::HashMap}; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ @@ -2492,3 +2494,68 @@ fn test_geth_prestate_diff_omits_empty_accounts() { }) ); } + +/// Geth reports `"code":"0x"` in the post state when the transaction clears an account's code, as +/// an EIP-7702 authorization to the zero address does with a delegation. +#[test] +fn test_geth_prestate_diff_reports_cleared_code() { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let authority = address!("7e5f4552091a69125d5dfcb7b8c2659029395bdf"); + let delegation = Bytes::from(hex!("ef0100000000000000000000000000000000000000aaaa")); + + let mut db = CacheDB::new(EmptyDB::default()); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + db.insert_account_info( + &authority, + AccountInfo { + balance: U256::from(1), + code_hash: alloy_primitives::keccak256(&delegation), + code: Some(Bytecode::new_raw(delegation.clone())), + ..Default::default() + }, + ); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::PRAGUE) + .build_mainnet(); + + // Signed by the key of `authority`; delegates to the zero address, which clears the code. + let authorization = Authorization { chain_id: U256::from(1), address: Address::ZERO, nonce: 0 }; + let signature = sign_message(B256::with_last_byte(1), authorization.signature_hash()).unwrap(); + let authorization = authorization.into_signed(signature); + assert_eq!(authorization.recover_authority().unwrap(), authority); + + let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + kind: TransactTo::Call(address!("2222222222222222222222222222222222222222")), + authorization_list: vec![authorization], + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; + + assert_eq!(diff.pre[&authority].code, Some(delegation)); + assert_eq!(diff.post[&authority].code, Some(Bytes::new())); + assert_eq!(diff.post[&authority].nonce, Some(1)); + assert_eq!( + serde_json::to_value(&diff.post[&authority]).unwrap(), + serde_json::json!({ "code": "0x", "nonce": 1 }) + ); +} From 7c5fdb6120a98b3cbec2b2c55c23ee68645f3221 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:56:22 +0100 Subject: [PATCH 022/159] fix(eest): run the EIP-4788 and EIP-2935 calls unchecked (#877) The blockchain runner treated every failed system call to a contract as an invalid block. In the spec only the withdrawal, consolidation and builder calls are checked; the EIP-4788 beacon roots and EIP-2935 history storage calls run unchecked, so a failure never invalidates the block and the accesses of the call stay in the block access list as reads. The runner now commits the recorded access of a failed unchecked call and keeps the old behaviour for the checked ones. This removes the skips for `system_call_execution_boundary.json` and `bal_pre_execution_call_failure_keeps_read_drops_write.json`, which both run a block whose beacon roots call runs out of gas. No evm2 change is needed. --- crates/eest/src/blockchaintest/execute.rs | 20 +++++++++++++++++++- crates/eest/src/blockchaintest/runner.rs | 5 ----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 38e5abf8e..fdf719968 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -851,6 +851,7 @@ fn pre_block_system_calls( BEACON_ROOTS_ADDRESS, Bytes::copy_from_slice(root.as_slice()), "eip4788", + false, )?; } if spec.enables(SpecId::PRAGUE) @@ -862,6 +863,7 @@ fn pre_block_system_calls( HISTORY_STORAGE_ADDRESS, Bytes::copy_from_slice(hash.as_slice()), "eip2935", + false, )?; } Ok(()) @@ -910,13 +912,21 @@ fn post_block_transition( } if spec.enables(SpecId::PRAGUE) { - run_system_call(evm, block_state, WITHDRAWAL_REQUEST_ADDRESS, Bytes::new(), "eip7002")?; + run_system_call( + evm, + block_state, + WITHDRAWAL_REQUEST_ADDRESS, + Bytes::new(), + "eip7002", + true, + )?; run_system_call( evm, block_state, evm2::evm::CONSOLIDATION_REQUEST_ADDRESS, Bytes::new(), "eip7251", + true, )?; } @@ -928,6 +938,7 @@ fn post_block_transition( evm2::evm::BUILDER_DEPOSIT_REQUEST_ADDRESS, Bytes::new(), "eip8282_deposit", + true, )?; run_system_call( evm, @@ -935,6 +946,7 @@ fn post_block_transition( evm2::evm::BUILDER_EXIT_REQUEST_ADDRESS, Bytes::new(), "eip8282_exit", + true, )?; } Ok(()) @@ -946,9 +958,15 @@ fn run_system_call( address: Address, data: Bytes, label: &'static str, + checked: bool, ) -> Result<(), TestErrorKind> { let executed = evm.system_call(SystemTx::new(address, data))?; if !executed.result().status { + // A failed unchecked call keeps the state accesses it recorded, which a BAL lists as reads. + if !checked { + let _ = executed.commit_to(block_state); + return Ok(()); + } let _ = executed.discard(); let has_code = match evm.read_account_info(&address) { Ok(info) => info.is_some_and(|info| !info.is_empty_code_hash()), diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index b9c0b47d2..6acb5b4f5 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -210,11 +210,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "prague/eip7251_consolidations", "prague/eip7685_general_purpose_el_requests", - // A failed EIP-4788 or EIP-2935 system call does not invalidate the block, but the runner treats - // every failed system call as an invalid block. - "eip8037_state_creation_gas_cost_increase/state_gas_system_calls/system_call_execution_boundary.json", - "eip7928_block_level_access_lists/block_access_lists_cross_index/bal_pre_execution_call_failure_keeps_read_drops_write.json", - // This fixture has a block gas limit below the transaction intrinsic gas and belongs to block validation. "static/state_tests/stEIP1559/lowGasLimit.json", ]; From 8bc035a2716f96a6c338f5198086f6a5f5eb31d8 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:56:29 +0100 Subject: [PATCH 023/159] test(eest): run the transition fork blockchain tests (#879) The blockchain runner skipped every transition fork, such as `BPO2ToAmsterdamAtTime15k`, so fixtures like `tx_total_gas_limit_cap_at_transition.json` never ran. The runner now maps a transition fork to the spec before it and the spec after it, with the activation at block 5 or at timestamp 15000, and picks the spec of each block from its header. The DAO fork and the BPO blob schedule forks (Osaka to BPO1 through BPO3 to BPO4) stay skipped: the first needs the DAO state change, and the others share a spec with the fork before them, which evm2 distinguishes only by the mainnet timestamps in `blob_params_for_timestamp`. This runs about 90 more fixture files across Paris to Shanghai, Shanghai to Cancun, Cancun to Prague, Prague to Osaka and BPO2 to Amsterdam, and the 4 invalid-header fixtures among them join the header validation skips. --- crates/eest/src/blockchaintest/execute.rs | 173 +++++++++++++++++----- crates/eest/src/blockchaintest/runner.rs | 7 + crates/eest/src/blockchaintest/types.rs | 25 ---- 3 files changed, 141 insertions(+), 64 deletions(-) diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index fdf719968..2b45dd4b4 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -210,8 +210,7 @@ fn execute_suite_with_resources( let mut summary = ExecuteSummary::default(); for (name, test_case) in &suite.0 { if !test_filter.matches(name) - || test_case.network.is_transition() - || is_blockchain_fork_skipped(fork_to_spec_id(test_case.network)) + || fork_schedule(test_case.network).is_none_or(|schedule| schedule.is_skipped()) { summary.skipped += 1; continue; @@ -261,12 +260,17 @@ fn execute_case( hook: &mut dyn Hook, resources: &ExecutionResources, ) -> Result, TestError> { - let spec = fork_to_spec_id(test_case.network); + let schedule = fork_schedule(test_case.network).expect("unsupported forks are skipped"); + let spec = schedule.initial; let mut parent_block_hash = Some(test_case.genesis_block_header.hash); let mut parent_excess_blob_gas = test_case.genesis_block_header.excess_blob_gas.unwrap_or_default().saturating_to::(); - let mut block_env = - block_env_from_header(&test_case.genesis_block_header, parent_excess_blob_gas, spec); + let mut block_env = block_env_from_header( + &test_case.genesis_block_header, + parent_excess_blob_gas, + &schedule, + spec, + ); let total_blocks = test_case.blocks.len(); let mut db_stats_counts = DbStatsCounts::default(); // `evm2 capture` keeps only the accounts the blocks touch and zeroes the genesis state @@ -280,6 +284,8 @@ fn execute_case( let block_number = block_number(block); let block_gas_used = block_gas_used(block); let total_transactions = block_transactions(block).len(); + let spec = block_header(block) + .map_or(spec, |header| schedule.spec_at(header.number, header.timestamp)); hook.block_started(BlockStarted { block_index, total_blocks, @@ -295,6 +301,7 @@ fn execute_case( block_number, total_transactions, block, + &schedule, spec, database, &mut block_env, @@ -382,6 +389,7 @@ fn execute_block( block_number: Option, total_transactions: usize, block: &Block, + schedule: &ForkSchedule, spec: SpecId, database: &mut InMemoryDB, block_env: &mut BlockEnv, @@ -419,7 +427,7 @@ fn execute_block( if let Some(header) = block_header(block) { block_hash = Some(header.hash); beacon_root = header.parent_beacon_block_root; - next_block_env = block_env_from_header(header, *parent_excess_blob_gas, spec); + next_block_env = block_env_from_header(header, *parent_excess_blob_gas, schedule, spec); this_excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()); expected_state_root = Some(header.state_root); } @@ -1036,7 +1044,12 @@ fn parse_state( Ok(database) } -fn block_env_from_header(header: &BlockHeader, excess_blob_gas: u64, spec: SpecId) -> BlockEnv { +fn block_env_from_header( + header: &BlockHeader, + excess_blob_gas: u64, + schedule: &ForkSchedule, + spec: SpecId, +) -> BlockEnv { let excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()).unwrap_or(excess_blob_gas); BlockEnvExt { @@ -1052,7 +1065,7 @@ fn block_env_from_header(header: &BlockHeader, excess_blob_gas: u64, spec: SpecI U256::ZERO }, blob_basefee: U256::from( - blob_params_for_timestamp(header.timestamp, spec).calc_blob_fee(excess_blob_gas), + schedule.blob_params(header.timestamp, spec).calc_blob_fee(excess_blob_gas), ), slot_num: header.slot_number.unwrap_or_default(), ext: (), @@ -1358,43 +1371,125 @@ fn is_blockchain_fork_skipped(spec: SpecId) -> bool { is_fork_skipped(spec) } -fn fork_to_spec_id(fork: ForkSpec) -> SpecId { - match fork { - ForkSpec::Frontier => SpecId::FRONTIER, - ForkSpec::Homestead => SpecId::HOMESTEAD, - ForkSpec::EIP150 => SpecId::TANGERINE, - ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON, - ForkSpec::Byzantium => SpecId::BYZANTIUM, - ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => SpecId::PETERSBURG, - ForkSpec::Istanbul => SpecId::ISTANBUL, - ForkSpec::Berlin => SpecId::BERLIN, - ForkSpec::London => SpecId::LONDON, +/// The specs a fork runs, in order. +struct ForkSchedule { + initial: SpecId, + /// The spec that a transition fork switches to, and when. + transition: Option<(Activation, SpecId)>, + /// Whether the blob schedule is BPO2 throughout, as before the BPO2 to Amsterdam transition. + bpo2_blobs: bool, +} + +/// When a transition fork activates. +#[derive(Clone, Copy)] +enum Activation { + Block(u64), + Timestamp(u64), +} + +impl ForkSchedule { + const fn fixed(spec: SpecId) -> Self { + Self { initial: spec, transition: None, bpo2_blobs: false } + } + + const fn transition(initial: SpecId, activation: Activation, spec: SpecId) -> Self { + Self { initial, transition: Some((activation, spec)), bpo2_blobs: false } + } + + const fn with_bpo2_blobs(mut self) -> Self { + self.bpo2_blobs = true; + self + } + + /// Returns the blob parameters of the block with this timestamp and spec. + fn blob_params(&self, timestamp: U256, spec: SpecId) -> BlobParams { + if self.bpo2_blobs { + BlobParams::bpo2() + } else { + blob_params_for_timestamp(timestamp, spec) + } + } + + /// Returns the spec of the block with this number and timestamp. + fn spec_at(&self, number: U256, timestamp: U256) -> SpecId { + match self.transition { + Some((Activation::Block(at), spec)) if number >= U256::from(at) => spec, + Some((Activation::Timestamp(at), spec)) if timestamp >= U256::from(at) => spec, + _ => self.initial, + } + } + + /// Returns whether any spec of the fork is skipped. + fn is_skipped(&self) -> bool { + is_blockchain_fork_skipped(self.initial) + || self.transition.is_some_and(|(_, spec)| is_blockchain_fork_skipped(spec)) + } +} + +/// Returns the specs of a fork, or `None` for the transitions that the runner does not support: +/// the DAO fork, and the BPO blob schedule forks, which share a spec. +const fn fork_schedule(fork: ForkSpec) -> Option { + use Activation::{Block, Timestamp}; + const AT_5: Activation = Block(5); + const AT_15K: Activation = Timestamp(15_000); + + Some(match fork { + ForkSpec::Frontier => ForkSchedule::fixed(SpecId::FRONTIER), + ForkSpec::Homestead => ForkSchedule::fixed(SpecId::HOMESTEAD), + ForkSpec::EIP150 => ForkSchedule::fixed(SpecId::TANGERINE), + ForkSpec::EIP158 => ForkSchedule::fixed(SpecId::SPURIOUS_DRAGON), + ForkSpec::Byzantium => ForkSchedule::fixed(SpecId::BYZANTIUM), + ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => { + ForkSchedule::fixed(SpecId::PETERSBURG) + } + ForkSpec::Istanbul => ForkSchedule::fixed(SpecId::ISTANBUL), + ForkSpec::Berlin => ForkSchedule::fixed(SpecId::BERLIN), + ForkSpec::London => ForkSchedule::fixed(SpecId::LONDON), ForkSpec::Paris | ForkSpec::MergeEOF | ForkSpec::MergeMeterInitCode - | ForkSpec::MergePush0 => SpecId::MERGE, - ForkSpec::Shanghai => SpecId::SHANGHAI, - ForkSpec::Cancun => SpecId::CANCUN, - ForkSpec::Prague => SpecId::PRAGUE, - ForkSpec::Osaka => SpecId::OSAKA, - ForkSpec::Amsterdam => SpecId::AMSTERDAM, - ForkSpec::FrontierToHomesteadAt5 - | ForkSpec::HomesteadToDaoAt5 - | ForkSpec::HomesteadToEIP150At5 - | ForkSpec::EIP158ToByzantiumAt5 - | ForkSpec::ByzantiumToConstantinopleAt5 - | ForkSpec::ByzantiumToConstantinopleFixAt5 - | ForkSpec::BerlinToLondonAt5 - | ForkSpec::ParisToShanghaiAtTime15k - | ForkSpec::ShanghaiToCancunAtTime15k - | ForkSpec::CancunToPragueAtTime15k - | ForkSpec::PragueToOsakaAtTime15k + | ForkSpec::MergePush0 => ForkSchedule::fixed(SpecId::MERGE), + ForkSpec::Shanghai => ForkSchedule::fixed(SpecId::SHANGHAI), + ForkSpec::Cancun => ForkSchedule::fixed(SpecId::CANCUN), + ForkSpec::Prague => ForkSchedule::fixed(SpecId::PRAGUE), + ForkSpec::Osaka => ForkSchedule::fixed(SpecId::OSAKA), + ForkSpec::Amsterdam => ForkSchedule::fixed(SpecId::AMSTERDAM), + ForkSpec::FrontierToHomesteadAt5 => { + ForkSchedule::transition(SpecId::FRONTIER, AT_5, SpecId::HOMESTEAD) + } + ForkSpec::HomesteadToEIP150At5 => { + ForkSchedule::transition(SpecId::HOMESTEAD, AT_5, SpecId::TANGERINE) + } + ForkSpec::EIP158ToByzantiumAt5 => { + ForkSchedule::transition(SpecId::SPURIOUS_DRAGON, AT_5, SpecId::BYZANTIUM) + } + ForkSpec::ByzantiumToConstantinopleAt5 | ForkSpec::ByzantiumToConstantinopleFixAt5 => { + ForkSchedule::transition(SpecId::BYZANTIUM, AT_5, SpecId::PETERSBURG) + } + ForkSpec::BerlinToLondonAt5 => { + ForkSchedule::transition(SpecId::BERLIN, AT_5, SpecId::LONDON) + } + ForkSpec::ParisToShanghaiAtTime15k => { + ForkSchedule::transition(SpecId::MERGE, AT_15K, SpecId::SHANGHAI) + } + ForkSpec::ShanghaiToCancunAtTime15k => { + ForkSchedule::transition(SpecId::SHANGHAI, AT_15K, SpecId::CANCUN) + } + ForkSpec::CancunToPragueAtTime15k => { + ForkSchedule::transition(SpecId::CANCUN, AT_15K, SpecId::PRAGUE) + } + ForkSpec::PragueToOsakaAtTime15k => { + ForkSchedule::transition(SpecId::PRAGUE, AT_15K, SpecId::OSAKA) + } + ForkSpec::BPO2ToAmsterdamAtTime15k => { + ForkSchedule::transition(SpecId::OSAKA, AT_15K, SpecId::AMSTERDAM).with_bpo2_blobs() + } + ForkSpec::HomesteadToDaoAt5 | ForkSpec::OsakaToBPO1AtTime15k | ForkSpec::BPO1ToBPO2AtTime15k | ForkSpec::BPO2ToBPO3AtTime15k - | ForkSpec::BPO3ToBPO4AtTime15k - | ForkSpec::BPO2ToAmsterdamAtTime15k => unreachable!("transition forks are skipped"), - } + | ForkSpec::BPO3ToBPO4AtTime15k => return None, + }) } #[cfg(test)] diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index 6acb5b4f5..3660fdb7d 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -204,6 +204,13 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "london/validation/test_invalid_header.json", "shanghai/eip4895_withdrawals/test_withdrawals_root.json", + // These check which header fields a block carries on either side of a fork, which is + // consensus-level block validation. + "eip7843_slotnum/fork_transition/invalid_post_fork_block_without_slot_number.json", + "eip7843_slotnum/fork_transition/invalid_pre_fork_block_with_slot_number.json", + "eip7928_block_level_access_lists/fork_transition/invalid_post_fork_block_without_bal_hash_field.json", + "eip7928_block_level_access_lists/fork_transition/invalid_pre_fork_block_with_bal_hash_field.json", + // Prague request/deposit fixtures validate EL request extraction and system-contract block processing. "prague/eip6110_deposits", "prague/eip7002_el_triggerable_withdrawals", diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 0bd17bd7c..440ffdec0 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -325,31 +325,6 @@ pub enum ForkSpec { Amsterdam, } -impl ForkSpec { - /// Returns true if this fork name represents a transition fork. - pub(crate) const fn is_transition(self) -> bool { - matches!( - self, - Self::FrontierToHomesteadAt5 - | Self::HomesteadToDaoAt5 - | Self::HomesteadToEIP150At5 - | Self::EIP158ToByzantiumAt5 - | Self::ByzantiumToConstantinopleAt5 - | Self::ByzantiumToConstantinopleFixAt5 - | Self::BerlinToLondonAt5 - | Self::ParisToShanghaiAtTime15k - | Self::ShanghaiToCancunAtTime15k - | Self::CancunToPragueAtTime15k - | Self::PragueToOsakaAtTime15k - | Self::OsakaToBPO1AtTime15k - | Self::BPO1ToBPO2AtTime15k - | Self::BPO2ToBPO3AtTime15k - | Self::BPO3ToBPO4AtTime15k - | Self::BPO2ToAmsterdamAtTime15k - ) - } -} - /// Possible seal engines. #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] pub enum SealEngine { From 1345cc1660678306c9a49d4357f738c5267c578a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:59:42 +0100 Subject: [PATCH 024/159] fix(inspectors): count struct logger limit in JSON bytes like geth (#868) The struct logger `limit` option now counts bytes of JSON, as in geth. Before, evm2 read it as a number of steps, so `{"limit": 1000}` kept 1000 struct logs where geth keeps about ten. geth's `StructLogger.OnOpcode` skips a step once the JSON it has written is over the limit (`l.resultSize > l.cfg.Limit`), and each logged step adds the length of its compact legacy JSON entry to `resultSize` (`eth/tracers/logger/logger.go` at master a0cba163). So a step is logged while the earlier entries total at most `limit` bytes, and the step that crosses the limit is the last one. The builder now sums `struct_log_json_len` of each log, which reproduces that JSON size field by field: the field order and omitted fields, the minimal hex of stack words, 66-byte memory words, 64-byte storage keys and values, and the six-byte `\u003c` and `\u003e` escapes geth writes for the `<=>` in `stack underflow (0 <=> 2)`. A zero limit still means unlimited. Because every entry takes at least 48 bytes, capture stops after `limit / 48 + 1` steps instead of recording the whole transaction. The size leaves out the fields geth omits when empty or zero (`refund`, `returnData`, `memory`, `storage`, `error`), whether or not the struct log carries them. I compared a bytecode with memory writes and a `KECCAK256` on a `geth --dev` node at master a0cba163 and on this branch merged with #864 (for the error text of failing steps the byte count also needs #849): the number of logs for limits 1, 50, 100, 150, 200, 250, 300, 400, 500, 600, 800, 1000 and 100000 is 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8 and 17 on both. A unit test checks the length function against entries written by geth, including an error with escaped characters, a memory snapshot, a storage snapshot and a stack of 256-bit words, and the existing limit tests now count bytes. --- crates/inspectors/src/tracing/builder/geth.rs | 73 ++++++++++- .../src/tracing/builder/geth/steps_tests.rs | 119 +++++++++++++++++- crates/inspectors/src/tracing/config.rs | 10 +- crates/inspectors/tests/it/geth.rs | 73 +++++++++-- 4 files changed, 254 insertions(+), 21 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 6dfe8f4f8..355bceade 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -21,6 +21,67 @@ use evm2::{ interpreter::{InstrStop, op}, }; +/// The smallest size in bytes of the JSON of a struct log, +/// `{"pc":0,"op":"OR","gas":0,"gasCost":0,"depth":1}`. +pub(crate) const MIN_STRUCT_LOG_JSON_LEN: u64 = 48; + +/// Returns the size in bytes of the JSON that geth's struct logger writes for the log. +/// +/// geth counts these bytes against the `limit` option. The JSON is compact, orders the fields as +/// `pc`, `op`, `gas`, `gasCost`, `depth`, `error`, `stack`, `returnData`, `memory`, `storage` and +/// `refund`, and omits the optional ones that are unset, empty or zero. It also counts the +/// `stateGasCost` and `stateGasReservoir` fields that evm2 adds after EIP-8037. It escapes `<`, `>` +/// and `&` in strings as `\u00xx`. +/// +/// See +fn struct_log_json_len(log: &StructLog) -> usize { + const fn digits(value: u64) -> usize { + match value.checked_ilog10() { + Some(log) => log as usize + 1, + None => 1, + } + } + // The quoted `0x`-prefixed minimal hex of a word. + fn word(value: &U256) -> usize { + 2 + 2 + value.bit_len().div_ceil(4).max(1) + } + // The quoted `0x`-prefixed hex of a 32-byte hash. + const HASH: usize = 2 + 2 + 64; + + // `{"pc":`, `,"op":"`, `","gas":`, `,"gasCost":`, `,"depth":` and `}`. + let mut len = 6 + digits(log.pc) + 7 + log.op.len() + 1 + 7 + digits(log.gas); + len += 11 + digits(log.gas_cost) + 9 + digits(log.depth) + 1; + // The fields after EIP-8037 that evm2 adds to the struct log: `,"stateGasCost":` and + // `,"stateGasReservoir":`. + if let Some(cost) = log.state_gas_cost { + len += 16 + usize::from(cost < 0) + digits(cost.unsigned_abs()); + } + if let Some(reservoir) = log.state_gas_reservoir { + len += 21 + digits(reservoir); + } + if let Some(error) = log.error.as_ref().filter(|error| !error.is_empty()) { + let escaped = error.bytes().filter(|byte| matches!(byte, b'<' | b'>' | b'&')).count(); + len += 10 + error.len() + 5 * escaped + 1; + } + if let Some(stack) = &log.stack { + len += 10 + stack.iter().map(word).sum::() + stack.len().saturating_sub(1) + 1; + } + if let Some(return_data) = log.return_data.as_ref().filter(|data| !data.is_empty()) { + len += 15 + 2 + 2 * return_data.len() + 1; + } + if let Some(memory) = log.memory.as_ref().filter(|memory| !memory.is_empty()) { + len += 11 + memory.iter().map(|chunk| chunk.len() + 2).sum::(); + len += memory.len().saturating_sub(1) + 1; + } + if let Some(storage) = log.storage.as_ref().filter(|storage| !storage.is_empty()) { + len += 12 + storage.len() * (HASH + 1 + HASH) + storage.len().saturating_sub(1) + 1; + } + if let Some(refund) = log.refund_counter.filter(|refund| *refund != 0) { + len += 10 + digits(refund); + } + len +} + /// A frame awaiting tree assembly, with visibility inherited by children in the forward pass. struct PendingCallFrame { /// Whether this call and its ancestors permit descendant logs. Independent of whether @@ -92,9 +153,11 @@ impl<'a> GethTraceBuilder<'a> { // call depth rather than step count, and a trace without subcalls needs no allocation. let mut steps = main_trace_node.steps_with_children(); let mut parents = Vec::new(); + let mut json_len = 0u64; loop { - if opts.limit.is_some_and(|limit| struct_logs.len() as u64 >= limit) { + // geth stops logging once the JSON written so far is over the limit. + if opts.limit.is_some_and(|limit| json_len > limit) { break; } let Some(CallTraceStepStackItem { trace_node, step, call_child_id }) = steps.next() @@ -133,6 +196,9 @@ impl<'a> GethTraceBuilder<'a> { } // Add step to geth trace + if opts.limit.is_some() { + json_len = json_len.saturating_add(struct_log_json_len(&log) as u64); + } struct_logs.push(log); // Visit the child immediately after its call opcode, then resume the parent. @@ -194,7 +260,10 @@ impl<'a> GethTraceBuilder<'a> { GethDefaultTracingOptions { limit: opts.limit.filter(|limit| *limit != 0), ..opts }; let capacity = opts.limit.map_or_else( || self.trace_step_count(), - |limit| self.trace_step_count().min(usize::try_from(limit).unwrap_or(usize::MAX)), + |limit| { + let steps = limit / MIN_STRUCT_LOG_JSON_LEN + 1; + self.trace_step_count().min(usize::try_from(steps).unwrap_or(usize::MAX)) + }, ); let mut struct_logs = Vec::with_capacity(capacity); let mut storage = HashMap::default(); diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index bb1f537c4..b2bdc2457 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -2,6 +2,7 @@ use super::*; use crate::tracing::types::{ CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, }; +use alloc::string::String; use evm2::{ evm::EmptyDB, interpreter::{InstrStop, opcode::OpCode}, @@ -256,20 +257,35 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { .map(|value| { BTreeMap::from([(B256::ZERO, U256::from(value).into())]) }) ); } - for limit in [0, 1, 3, 6, 9, 99] { + let sizes: Vec<_> = frame.struct_logs.iter().map(struct_log_json_len).collect(); + let total: usize = sizes.iter().sum(); + for limit in + [0, 1, sizes[0], sizes[0] + sizes[1], sizes[..5].iter().sum(), total, 2 * total] + { let limited = builder.geth_traces( 123, Bytes::new(), GethDefaultTracingOptions { - limit: Some(limit), + limit: Some(limit as u64), disable_storage: Some(disable_storage), enable_return_data: Some(true), ..Default::default() }, ); - let len = - if limit == 0 { expected.len() } else { (limit as usize).min(expected.len()) }; - assert_eq!(limited.struct_logs, frame.struct_logs[..len]); + let mut written = 0; + let len = if limit == 0 { + expected.len() + } else { + sizes + .iter() + .take_while(|size| { + let logged = written <= limit; + written += **size; + logged + }) + .count() + }; + assert_eq!(limited.struct_logs, frame.struct_logs[..len], "limit {limit}"); } } assert!( @@ -321,3 +337,96 @@ fn storage_snapshots_accumulate_and_skip_failed_steps() { assert!(frame.struct_logs[3].storage.is_none()); } } + +/// The entries are what go-ethereum master's struct logger writes (`debug_traceCall`). +#[test] +fn struct_log_json_len_matches_geth() { + for json in [ + r#"{"pc":0,"op":"ADD","gas":979000,"gasCost":3,"depth":1,"error":"stack underflow (0 \u003c=\u003e 2)","stack":[]}"#, + r#"{"pc":4,"op":"SSTORE","gas":978994,"gasCost":12100,"depth":1,"stack":["0x1","0x0"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000001"}}"#, + r#"{"pc":7,"op":"SLOAD","gas":868971,"gasCost":100,"depth":1,"stack":["0x0"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000001"}}"#, + r#"{"pc":11,"op":"PUSH1","gas":868862,"gasCost":3,"depth":1,"stack":[],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":13,"op":"MLOAD","gas":868859,"gasCost":3,"depth":1,"stack":["0x0"],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":14,"op":"PUSH1","gas":868856,"gasCost":3,"depth":1,"stack":["0x1"],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":0,"op":"PUSH1","gas":979000,"gasCost":3,"depth":1,"stack":[]}"#, + r#"{"pc":4,"op":"SSTORE","gas":978994,"gasCost":12100,"depth":1,"stack":["0x0","0x5"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000005":"0x0000000000000000000000000000000000000000000000000000000000000000"},"refund":11616}"#, + r#"{"pc":33,"op":"STOP","gas":978997,"gasCost":0,"depth":1,"stack":["0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff60"]}"#, + r#"{"pc":1,"op":"STOP","gas":5,"gasCost":0,"depth":1,"returnData":"0xdeadbeef","refund":4800}"#, + r#"{"pc":20,"op":"opcode 0xc not defined","gas":5,"gasCost":0,"depth":12,"error":"gas uint64 overflow \u0026 more"}"#, + ] { + let log: StructLog = serde_json::from_str(json).unwrap(); + assert_eq!(struct_log_json_len(&log), json.len(), "{json}"); + } +} + +#[test] +fn struct_log_json_len_skips_the_fields_geth_omits() { + let log = StructLog { + pc: 1, + op: "STOP".into(), + gas: 5, + depth: 1, + error: Some(String::new()), + return_data: Some(Bytes::new()), + memory: Some(vec![]), + storage: Some(BTreeMap::new()), + refund_counter: Some(0), + ..Default::default() + }; + let json = r#"{"pc":1,"op":"STOP","gas":5,"gasCost":0,"depth":1}"#; + assert_eq!(struct_log_json_len(&log), json.len()); +} + +#[test] +fn struct_log_json_len_counts_the_state_gas_fields() { + for (cost, reservoir) in [(None, None), (Some(0), Some(0)), (Some(-1234), Some(99_999))] { + let log = StructLog { + op: "SSTORE".into(), + gas: 100, + depth: 2, + state_gas_cost: cost, + state_gas_reservoir: reservoir, + ..Default::default() + }; + assert_eq!( + struct_log_json_len(&log), + serde_json::to_string(&log).unwrap().len(), + "{cost:?} {reservoir:?}" + ); + } +} + +#[test] +fn struct_log_limit_counts_json_bytes() { + let steps = [op::PUSH1, op::PUSH1, op::SSTORE, op::STOP] + .iter() + .enumerate() + .map(|(pc, &op)| test_step(pc, op)) + .collect(); + let builder = GethTraceBuilder::new(vec![CallTraceNode { + trace: CallTrace { steps, ..Default::default() }, + ..Default::default() + }]); + let unlimited = builder.geth_traces(0, Bytes::new(), Default::default()).struct_logs; + let sizes: Vec<_> = unlimited.iter().map(struct_log_json_len).collect(); + assert_eq!(unlimited.len(), 4); + // geth logs a step while the bytes written so far do not exceed the limit, so the step that + // crosses the limit is the last one. + for limit in [1, sizes[0], sizes[0] + 1, sizes[0] + sizes[1], sizes[..3].iter().sum(), 10_000] { + let mut written = 0; + let expected = sizes + .iter() + .take_while(|size| { + let logged = written <= limit; + written += **size; + logged + }) + .count(); + let frame = builder.geth_traces( + 0, + Bytes::new(), + GethDefaultTracingOptions { limit: Some(limit as u64), ..Default::default() }, + ); + assert_eq!(frame.struct_logs.len(), expected, "limit {limit}"); + } +} diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 57a87060f..1db883705 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -1,3 +1,4 @@ +use crate::tracing::builder::geth::MIN_STRUCT_LOG_JSON_LEN; use alloy_primitives::{U256, map::HashSet}; use alloy_rpc_types_trace::{ geth::{ @@ -221,7 +222,12 @@ impl TracingInspectorConfig { #[inline] pub fn from_geth_config(config: &GethDefaultTracingOptions) -> Self { Self { - step_limit: config.limit.and_then(NonZeroU64::new), + // The limit counts bytes of JSON, and a struct log takes at least that many, so the + // trace never needs more steps. + step_limit: config + .limit + .filter(|limit| *limit != 0) + .and_then(|limit| NonZeroU64::new(limit / MIN_STRUCT_LOG_JSON_LEN + 1)), record_memory_snapshots: config.is_memory_enabled(), record_stack_snapshots: if config.disable_stack.unwrap_or_default() { StackSnapshotType::None @@ -569,7 +575,7 @@ mod tests { #[test] fn merge_step_limits_preserves_each_consumers_capture() { let limited = TracingInspectorConfig::from_geth_config(&GethDefaultTracingOptions { - limit: Some(2), + limit: Some(2 * MIN_STRUCT_LOG_JSON_LEN - 1), ..Default::default() }); let larger = TracingInspectorConfig { step_limit: NonZeroU64::new(4), ..limited }; diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 81eda26fa..078318217 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -1719,11 +1719,15 @@ fn test_geth_opcode_limit_end_to_end() { assert_eq!(steps[8]["depth"], 2); assert_eq!(steps[11]["depth"], 1); - // The cap must count steps across call frames, including a cap inside the child. - for limit in [2, 9, 12] { + // The limit counts bytes of JSON across call frames, including a cap inside the child. + // geth logs a step while the JSON written so far is within the limit, so the bytes of the + // first `steps - 1` steps let `steps` steps through. + let sizes: Vec<_> = steps.iter().map(geth_json_len).collect(); + for steps in [2, 9, 12] { + let limit: usize = sizes[..steps - 1].iter().sum(); let (limited, limited_gas) = run(json!({ "limit": limit })); let mut expected = full.clone(); - expected["structLogs"].as_array_mut().unwrap().truncate(limit); + expected["structLogs"].as_array_mut().unwrap().truncate(steps); assert_eq!(limited, expected); assert_eq!(limited_gas, gas_used); } @@ -1736,6 +1740,24 @@ fn test_geth_opcode_limit_end_to_end() { } } +/// Returns the bytes of the JSON geth writes for a struct log, which leaves out a zero refund and +/// empty return data, memory and storage. +fn geth_json_len(log: &serde_json::Value) -> usize { + let mut log = log.clone(); + let fields = log.as_object_mut().unwrap(); + for (name, empty) in [ + ("refund", serde_json::json!(0)), + ("returnData", serde_json::json!("0x")), + ("memory", serde_json::json!([])), + ("storage", serde_json::json!({})), + ] { + if fields.get(name) == Some(&empty) { + fields.remove(name); + } + } + log.to_string().len() +} + /// Execute real bytecode and compare capped capture with the complete execution. #[test] fn test_geth_opcode_limit() { @@ -1786,11 +1808,34 @@ fn test_geth_opcode_limit() { full.return_value, if terminal == op::INVALID { Bytes::new() } else { hex!("2a").into() } ); - // Includes caps before CALL, on CALL, in the child, and after returning to the parent. - for limit in [None, Some(0), Some(2), Some(8), Some(9), Some(12), Some(u64::MAX)] { + // Includes caps before CALL, on CALL, in the child, and after returning to the parent. The + // limit counts bytes of JSON, and the first `steps - 1` steps let `steps` steps through. + let sizes: Vec<_> = full + .struct_logs + .iter() + .map(|log| geth_json_len(&serde_json::to_value(log).unwrap())) + .collect(); + let step_limit = |steps: usize| sizes[..steps - 1].iter().sum::() as u64; + for limit in [ + None, + Some(0), + Some(step_limit(2)), + Some(step_limit(8)), + Some(step_limit(9)), + Some(step_limit(12)), + Some(u64::MAX), + ] { let opts = GethDefaultTracingOptions { limit, ..Default::default() }; - let expected_len = limit.filter(|n| *n != 0).map_or(full.struct_logs.len(), |n| { - full.struct_logs.len().min(usize::try_from(n).unwrap_or(usize::MAX)) + let expected_len = limit.filter(|n| *n != 0).map_or(full.struct_logs.len(), |limit| { + let mut written = 0; + sizes + .iter() + .take_while(|size| { + let logged = written as u64 <= limit; + written += **size; + logged + }) + .count() }); let mut expected = full.clone(); expected.struct_logs.truncate(expected_len); @@ -1799,10 +1844,14 @@ fn test_geth_opcode_limit() { for _ in 0..2 { let actual = run(&mut inspector, opts); assert_eq!(actual, expected, "limit {limit:?}, terminal {terminal}"); - assert_eq!( - inspector.traces().nodes().iter().map(|n| n.trace.steps.len()).sum::(), - expected_len - ); + // Capture stops at the most steps the limit can hold, at least a byte count's worth + // of the smallest log each. + let recorded = + inspector.traces().nodes().iter().map(|n| n.trace.steps.len()).sum::(); + assert!(recorded >= expected_len); + if let Some(limit) = limit.filter(|limit| *limit != 0) { + assert!(recorded as u64 <= limit / 48 + 1); + } inspector.fuse(); } // Builder options also apply to already collected unlimited traces. @@ -2053,7 +2102,7 @@ fn test_debug_default_tracer_moves_stacks() { caller_code.extend_from_slice(&hex!("5af150600700")); for disable_stack in [false, true] { - for limit in [None, Some(0), Some(10)] { + for limit in [None, Some(0), Some(1000)] { let config = GethDefaultTracingOptions { disable_stack: Some(disable_stack), limit, From 5b1f75164a16e7dee23dd39b9e38868c62c5ebef Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:05:29 +0100 Subject: [PATCH 025/159] fix(state): keep slots whose rollback reload fails and reload only creation zeros (#855) Follow-up to #845, which merged while its Cyclops audit still had two findings open. `reload_zeroed` evicted a slot whose reread failed. With a BAL attached and database fallback off, a slot the BAL omits fails the reread, so the slot and its read vanished from the overlay and a rebuilt BAL matched the malformed one. The slot now stays loaded with its zero, so the read reaches the BAL, and its next load, or the next `storage_slot_untracked`, rereads it and reports the error instead of serving the zero. The reload also rescanned every zero-valued slot of the account on each rolled-back creation, so retrying a creation at one address M times with N slots cost about N times M database lookups. Slots first loaded under a creation are now marked with `StorageSlot::created_zero`, and a rollback rereads only those, once each. Two new tests pin the failed-reload path and that slots loaded outside the creation are left alone. --- crates/evm2/src/evm/state/block.rs | 1 + crates/evm2/src/evm/state/mod.rs | 101 ++++++++++++++++++++++++++- crates/evm2/src/evm/state/pending.rs | 1 + crates/evm2/src/evm/state/storage.rs | 48 +++++++++---- 4 files changed, 133 insertions(+), 18 deletions(-) diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 75ce25ab3..1187c464b 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -323,6 +323,7 @@ mod tests { StorageSlot { value: Tracked::from_parts(original, current), is_warm: false, + created_zero: false, _non_exhaustive: (), }, )]) diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index a47a2ad37..bbb1ced7a 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -672,9 +672,11 @@ impl<'a> State<'a> { pub fn storage_slot_untracked(&mut self, address: &Address, key: &Word) -> DbResult { if let Some(account) = self.accounts.get(address) { if let Some(slot) = account.storage.slots.get(key) { - return Ok(slot.value.current); - } - if account.storage.wiped || account.discards_storage() { + // A slot a failed rollback reload left at its cached zero reads the database. + if !slot.created_zero || account.discards_storage() { + return Ok(slot.value.current); + } + } else if account.storage.wiped || account.discards_storage() { self.database.check_bal_storage(address, key)?; return Ok(Word::ZERO); } @@ -1098,6 +1100,7 @@ impl<'a> State<'a> { #[cfg(test)] mod tests { use super::*; + use crate::evm::bal::{AccountBal, Bal, BlockAccessIndex}; use alloc::vec; #[test] @@ -1344,9 +1347,11 @@ mod tests { assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::ZERO); state.storage_slot(&address, written).unwrap().write(Word::from(9)); assert_eq!(state.storage_slot(&address, empty).unwrap().current(), Word::ZERO); + assert!(state.accounts[&address].storage.slots[&empty].created_zero); state.rollback(checkpoint, version.features); assert!(!state.accounts[&address].discards_storage()); + assert!(state.accounts[&address].storage.slots.values().all(|slot| !slot.created_zero)); for (key, value) in [(read, 6), (written, 7), (empty, 0)] { let value = Word::from(value); assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), value); @@ -1397,6 +1402,96 @@ mod tests { assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); } + #[test] + fn rollback_of_creation_keeps_slot_whose_reload_fails() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let key = Word::from(1); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + let bal = Arc::new(Bal::from_iter([(address, AccountBal::default())])); + + // The creation's own read checks the BAL, so detach it there. The BAL covers the account + // but omits the slot, so rereading the slot during the rollback fails. + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.set_bal(bal); + state.set_bal_index(BlockAccessIndex::new(1)); + state.rollback(checkpoint, features); + + // The slot stays loaded, so its read reaches the BAL, and later loads report the failure + // instead of serving the zero. + assert!(state.accounts[&address].storage.slots[&key].created_zero); + // An unaffordable cold access is skipped before the pending reload reads the database. + assert!(matches!( + state.storage(&address).unwrap().into_slot_with_skip(key, true), + Err(LoadError::ColdLoadSkipped) + )); + assert!(state.storage_slot(&address, key).is_err()); + assert!(state.storage_slot_untracked(&address, &key).is_err()); + assert!(state.accounts[&address].storage.slots[&key].created_zero); + + state.clear_bal(); + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::from(6)); + let slot = state.storage_slot(&address, key).unwrap(); + assert_eq!((slot.original(), slot.current()), (Word::from(6), Word::from(6))); + assert!(!state.accounts[&address].storage.slots[&key].created_zero); + } + + #[test] + fn finalization_wipe_clears_the_marker_of_a_slot_whose_reload_failed() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let key = Word::from(1); + let version = Version::base(crate::SpecId::LONDON); + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.set_bal(Arc::new(Bal::from_iter([(address, AccountBal::default())]))); + state.set_bal_index(BlockAccessIndex::new(1)); + state.rollback(checkpoint, version.features); + state.clear_bal(); + + state.account(&address).unwrap().mark_destructed(); + state.finalize_transaction_(version); + + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::ZERO); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + } + + #[test] + fn rollback_of_creation_reloads_only_slots_loaded_under_it() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let (before, under) = (Word::from(1), Word::from(2)); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + let mut state = State::new(db); + assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); + // Only a reread can observe these values. + for key in [before, under] { + state.overlay_db_mut().insert_account_storage(&address, &key, &Word::from(5)); + } + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::from(5)); + } + #[test] fn rollback_reloads_slots_written_through_the_creating_handle() { let address = Address::with_last_byte(2); diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 42bc64fc7..186242ae1 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -84,6 +84,7 @@ impl PendingState { StorageSlot { value: Tracked::from_parts(original, current), is_warm: false, + created_zero: false, _non_exhaustive: (), }, ); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index d69a4b6dd..ac262387b 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -59,21 +59,18 @@ impl StorageOverlay { self.wiped || self.changed_slots().next().is_some() } - /// Rereads the slots that hold zero in the overlay after the account's creation was rolled + /// Rereads the slots first loaded under the account's creation after that creation was rolled /// back. /// - /// A slot first loaded under the creation cached zero instead of its backing value, and the - /// rollback restores its zero current value. A slot that fails to reload is evicted, so its - /// next load reports the error. + /// Such a slot cached zero instead of its backing value, and the rollback restores its zero + /// current value. A slot that fails to reload keeps its zero and its marker, so its read stays + /// in the overlay for the BAL and its next load rereads it and reports the error. pub(super) fn reload_zeroed(&mut self, address: &Address, database: &mut dyn DynDatabase) { - self.slots.retain(|key, slot| { - if !slot.value.original.is_zero() || !slot.value.current.is_zero() { - return true; + for (key, slot) in &mut self.slots { + if slot.created_zero { + let _ = slot.reload_created_zero(address, key, database); } - let Ok(value) = database.get_storage(address, key) else { return false }; - slot.value = Tracked::new(value); - true - }); + } } } @@ -88,6 +85,9 @@ pub struct StorageSlot { pub value: Tracked, /// Whether the slot was warmed during execution this transaction, for EIP-2929 gas accounting. pub is_warm: bool, + /// Whether the slot was first loaded while its account was created in the transaction, so it + /// cached zero instead of its backing value. Rolling that creation back reloads the slot. + pub(crate) created_zero: bool, #[doc(hidden)] // Not public API. Please use an existing constructor. pub _non_exhaustive: (), } @@ -99,11 +99,24 @@ impl StorageSlot { if wiped { !self.value.current.is_zero() } else { self.value.is_changed() } } + /// Rereads a zero cached under a creation that was rolled back. + #[cold] + pub(super) fn reload_created_zero( + &mut self, + address: &Address, + key: &Word, + database: &mut dyn DynDatabase, + ) -> DbResult<()> { + self.value = Tracked::new(database.get_storage(address, key)?); + self.created_zero = false; + Ok(()) + } + /// Creates a freshly loaded slot whose original and current values are `value`, with the given - /// EIP-2929 warmth. + /// EIP-2929 warmth. `created_zero` marks a zero cached without a database read. #[inline] - fn loaded(value: Word, is_warm: bool) -> Self { - Self { value: Tracked::new(value), is_warm, _non_exhaustive: () } + fn loaded(value: Word, is_warm: bool, created_zero: bool) -> Self { + Self { value: Tracked::new(value), is_warm, created_zero, _non_exhaustive: () } } } @@ -250,6 +263,10 @@ impl<'a, 'db> StorageHandle<'a, 'db> { if skip_cold_load && is_cold { return Err(LoadError::ColdLoadSkipped); } + // A rollback that failed to reload the slot leaves it for this load to retry. + if slot.created_zero && !created { + slot.reload_created_zero(address, &key, &mut inner.database)?; + } (slot, is_cold) } hash_map::Entry::Vacant(entry) => { @@ -266,7 +283,7 @@ impl<'a, 'db> StorageHandle<'a, 'db> { } else { inner.database.get_storage(address, &key)? }; - (entry.insert(StorageSlot::loaded(value, is_warm)), !is_warm) + (entry.insert(StorageSlot::loaded(value, is_warm, created)), !is_warm) } }; if WARM && is_cold { @@ -298,6 +315,7 @@ impl<'a, 'db> StorageHandle<'a, 'db> { pub(crate) fn zero_loaded(&mut self) { self.storage.slots.iter_mut().for_each(|(_, slot)| { slot.value.set_current(Word::ZERO); + slot.created_zero = false; }); } } From 9539ffea8441916196d6dc1e8750ccaa66062ff4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:07:54 +0100 Subject: [PATCH 026/159] feat(inspectors): let the mux run nested mux, erc7562 and JavaScript tracers (#882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geth's `muxTracer` creates each of its tracers through the tracer directory: a name is looked up first, and anything else is evaluated as JavaScript (`eth/tracers/native/mux.go`, `eth/tracers/dir.go`). The mux of evm2 accepted only 4byte, call, prestate, flatCall, noop and stateGas tracers, plus the keccak256 preimage tracer from #867. A nested `muxTracer`, an `erc7562Tracer` or a JavaScript source in a mux config failed with `unsupported tracer type` or `unexpected config`. A tracer that does not share the mux's recording now runs as a child `DebugInspector` that receives every hook and reports under its own key: nested `muxTracer`, `erc7562Tracer` and JavaScript sources. `keccak256PreimageTracer` goes through the same path, so the mux keeps no special field for it. Because a JavaScript tracer needs the transaction, the block and the transaction context to produce its result, `MuxInspector::try_into_mux_frame` now takes them, derives the `TransactionInfo` itself, and recovers the transaction root itself instead of leaving that to `DebugInspector`. `MuxInspector` is no longer `Clone`, since a JavaScript child cannot be cloned; it has `try_clone` and `fuse` that clone and reset its children instead, and `set_js_interrupt` hands a `JsInspectorInterrupt` to the JavaScript tracers of the mux and of nested muxes. The mux error enum drops `UnexpectedConfig` and gains a variant for the error of a child. Checked against go-ethereum master `a0cba163` with the parity harness of the `dani/geth-parity-harness` branch, running a mux of callTracer, a nested mux of noopTracer and 4byteTracer, keccak256PreimageTracer, erc7562Tracer and a JavaScript step counter on all 334 case and fork pairs: | Mux config with a nested mux and a JavaScript tracer | geth master | origin/main | this PR | | --- | --- | --- | --- | | nested `muxTracer` result | nested object | 🔴 `unsupported tracer type` | 🟢 identical on all pairs | | JavaScript source result | step count | 🔴 `unsupported tracer type` | 🟢 identical on all pairs | | `keccak256PreimageTracer` result | preimage map | 🟢 identical | 🟢 identical | | `erc7562Tracer` result | frame | 🔴 `unexpected config` | 🔴 runs, but its frame differs from geth in `usedOpcodes`, `contractSize` and `outOfGas`, the same differences as the standalone tracer, which another PR owns | The remaining differences of the callTracer entry in this mux are the Amsterdam gas fields and error strings that other PRs handle. The harness is not merged yet, so its mux case is not part of this PR; I will add it once it lands. Rust tests cover a nested mux with an erc7562Tracer and a JavaScript tracer next to a built-in one. --- crates/inspectors/src/tracing/debug.rs | 34 ++-- crates/inspectors/src/tracing/js/mod.rs | 8 +- crates/inspectors/src/tracing/mux.rs | 245 +++++++++++++----------- crates/inspectors/src/tracing/utils.rs | 22 ++- crates/inspectors/tests/it/geth.rs | 58 +++++- crates/inspectors/tests/it/geth_js.rs | 74 ++++++- 6 files changed, 302 insertions(+), 139 deletions(-) diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index 52b25441c..5b7217e37 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -1,11 +1,11 @@ use crate::tracing::{ FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MuxInspector, TracingInspector, TracingInspectorConfig, TransactionContext, final_refunded, + utils::transaction_info, }; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256}; -use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ CallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, NoopFrame, PreStateConfig, @@ -68,7 +68,7 @@ impl DebugInspector { } Self::Noop(inspector) => Self::Noop(inspector.clone()), Self::StateGasTracer(inspector) => Self::StateGasTracer(inspector.clone()), - Self::Mux(inspector, config) => Self::Mux(inspector.clone(), config.clone()), + Self::Mux(inspector, config) => Self::Mux(inspector.try_clone()?, config.clone()), Self::FlatCallTracer(inspector) => Self::FlatCallTracer(inspector.clone()), Self::Erc7562Tracer(inspector, config) => { Self::Erc7562Tracer(inspector.clone(), config.clone()) @@ -205,9 +205,7 @@ impl DebugInspector { | Self::Default(inspector, _) => inspector.fuse(), Self::Keccak256Preimage(inspector) => inspector.clear(), Self::Noop(_) | Self::StateGasTracer(_) => {} - Self::Mux(inspector, config) => { - *inspector = MuxInspector::try_from_config(config.clone())?; - } + Self::Mux(inspector, _) => inspector.fuse()?, #[cfg(feature = "js-tracer")] Self::Js(inspector) => inspector.fuse()?, } @@ -215,9 +213,20 @@ impl DebugInspector { Ok(()) } + /// Sets the handle that interrupts the JavaScript tracers of this inspector, including those + /// in a mux tracer, see [`JsInspector::with_interrupt`](crate::tracing::js::JsInspector). + #[cfg(feature = "js-tracer")] + pub fn set_js_interrupt(&mut self, interrupt: &crate::tracing::js::JsInspectorInterrupt) { + match self { + Self::Js(inspector) => inspector.set_interrupt(interrupt.clone()), + Self::Mux(inspector, _) => inspector.set_js_interrupt(interrupt), + _ => {} + } + } + /// Sets the index the next recorded log receives, see /// [`TracingInspector::set_next_log_index`]. - pub const fn set_next_log_index(&mut self, index: usize) { + pub fn set_next_log_index(&mut self, index: usize) { match self { Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) @@ -245,15 +254,7 @@ impl DebugInspector { res: &TxResultWithState, db: &mut dyn DynDatabase, ) -> Result { - #[allow(clippy::needless_update)] - let tx_info = TransactionInfo { - hash: tx_context.as_ref().and_then(|c| c.tx_hash), - index: tx_context.as_ref().and_then(|c| c.tx_index.map(|i| i as u64)), - block_hash: tx_context.as_ref().and_then(|c| c.block_hash), - block_number: Some(block_env.number.saturating_to()), - base_fee: Some(block_env.basefee.saturating_to()), - ..Default::default() - }; + let tx_info = transaction_info::(tx_context.as_ref(), block_env); let res = match self { Self::FourByte(inspector) => FourByteFrame::from(&*inspector).into(), @@ -283,8 +284,7 @@ impl DebugInspector { } .into(), Self::Mux(inspector, _) => { - inspector.finalize_transaction(tx, &res.result); - inspector.try_into_mux_frame(res, db, tx_info)?.into() + inspector.try_into_mux_frame(res, tx, block_env, tx_context, db)?.into() } Self::FlatCallTracer(inspector) => { inspector.recover_runtime_oog_root(tx, &res.result); diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 88c7718c6..a02c64580 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -489,10 +489,16 @@ impl JsInspector { /// ``` #[must_use] pub fn with_interrupt(mut self, interrupt: JsInspectorInterrupt) -> Self { - self.interrupt = Some(interrupt); + self.set_interrupt(interrupt); self } + /// Sets the shared handle that cooperatively interrupts JavaScript tracing, see + /// [`Self::with_interrupt`]. + pub fn set_interrupt(&mut self, interrupt: JsInspectorInterrupt) { + self.interrupt = Some(interrupt); + } + #[inline] fn is_interrupted(&self) -> bool { self.interrupt.as_ref().is_some_and(JsInspectorInterrupt::is_interrupted) diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index 0280f4366..c34a4eaf7 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -1,34 +1,34 @@ use crate::tracing::{ - FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, - Keccak256PreimageLimitExceeded, TracingInspector, TracingInspectorConfig, final_refunded, + DebugInspector, DebugInspectorError, FourByteInspector, TracingInspector, + TracingInspectorConfig, TransactionContext, final_refunded, utils::transaction_info, }; -use alloc::vec::Vec; +use alloc::{boxed::Box, vec::Vec}; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256, map::HashMap}; -use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ - CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, - GethTrace, NoopFrame, PreStateConfig, StateGasTrace, + CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerConfig, + GethDebugTracerType, GethDebugTracingOptions, NoopFrame, PreStateConfig, StateGasTrace, mux::{MuxConfig, MuxFrame}, }; use evm2::{ - DatabaseError, EvmTypes, EvmTypesHost, Inspector, TxResultExt, TxResultWithState, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, TxResultWithState, + env::BlockEnv, evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, }; use thiserror::Error; /// Mux tracing inspector that runs and collects results of multiple inspectors at once. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct MuxInspector { /// An instance of FourByteInspector that can be reused four_byte: Option, - /// An instance of Keccak256PreimageInspector that can be reused - keccak256_preimage: Option, /// An instance of TracingInspector that can be reused tracing: Option, /// Configurations for different Geth trace types configs: Vec<(GethDebugBuiltInTracerType, TraceConfig)>, + /// Tracers that run their own inspector, keyed by their tracer type. + children: Vec<(GethDebugTracerType, DebugInspector)>, } /// Holds all Geth supported trace configurations @@ -45,19 +45,19 @@ impl MuxInspector { /// Try creating a new instance of [MuxInspector] from the given [MuxConfig]. pub fn try_from_config(config: MuxConfig) -> Result { let mut four_byte = None; - let mut keccak256_preimage = None; let mut inspector_config = TracingInspectorConfig::none(); let mut configs = Vec::new(); + let mut children = Vec::new(); // Process each tracer configuration for (tracer_type, tracer_config) in config.0 { - let builtin = match tracer_type { - GethDebugTracerType::BuiltInTracer(b) => b, - GethDebugTracerType::JsTracer(name) if name == KECCAK256_PREIMAGE_TRACER => { - keccak256_preimage = Some(Keccak256PreimageInspector::default()); + let builtin = match &tracer_type { + GethDebugTracerType::BuiltInTracer(builtin) => *builtin, + // geth evaluates a name that is not a built-in tracer as JavaScript. + GethDebugTracerType::JsTracer(_) => { + children.push(Self::child(tracer_type, tracer_config)?); continue; } - _ => return Err(Error::UnsupportedTracerType(tracer_type)), }; #[allow(unreachable_patterns)] match builtin { @@ -93,36 +93,108 @@ impl MuxInspector { .merge(TracingInspectorConfig::from_flat_call_config(&flatcall_config)); configs.push((builtin, TraceConfig::FlatCall(flatcall_config))); } - GethDebugBuiltInTracerType::MuxTracer => { - return Err(Error::UnexpectedConfig(builtin)); + GethDebugBuiltInTracerType::MuxTracer + | GethDebugBuiltInTracerType::Erc7562Tracer => { + children.push(Self::child(tracer_type, tracer_config)?); } _ => { // keep this so that new variants can be supported - return Err(Error::UnexpectedConfig(builtin)); + return Err(Error::UnsupportedTracerType(tracer_type)); } } } let tracing = (!configs.is_empty()).then(|| TracingInspector::new(inspector_config)); - Ok(Self { four_byte, keccak256_preimage, tracing, configs }) + Ok(Self { four_byte, tracing, configs, children }) + } + + /// Creates the inspector of a tracer that does not share the recording of the mux. + fn child( + tracer_type: GethDebugTracerType, + tracer_config: Option, + ) -> Result<(GethDebugTracerType, DebugInspector), Error> { + let options = GethDebugTracingOptions { + tracer: Some(tracer_type.clone()), + tracer_config: tracer_config.unwrap_or_default(), + ..Default::default() + }; + Ok((tracer_type, DebugInspector::new(options).map_err(Box::new)?)) + } + + /// Creates a copy of this inspector, with copies of the inspectors of its nested tracers. + /// + /// A JavaScript tracer shares its interrupt handle with its copy. + pub fn try_clone(&self) -> Result { + let children = self + .children + .iter() + .map(|(tracer_type, child)| { + Ok((tracer_type.clone(), child.try_clone().map_err(Box::new)?)) + }) + .collect::>()?; + Ok(Self { + four_byte: self.four_byte.clone(), + tracing: self.tracing.clone(), + configs: self.configs.clone(), + children, + }) + } + + /// Prepares the inspector for executing the next transaction. This removes any state from the + /// previous transaction. + pub fn fuse(&mut self) -> Result<(), Error> { + if let Some(inspector) = &mut self.four_byte { + core::mem::take(inspector); + } + if let Some(inspector) = &mut self.tracing { + inspector.fuse(); + } + for (_, child) in &mut self.children { + child.fuse().map_err(Box::new)?; + } + Ok(()) + } + + /// Sets the handle that interrupts the JavaScript tracers of this mux, including those of + /// nested mux tracers, see [`JsInspector::with_interrupt`](crate::tracing::js::JsInspector). + #[cfg(feature = "js-tracer")] + pub fn set_js_interrupt(&mut self, interrupt: &crate::tracing::js::JsInspectorInterrupt) { + for (_, child) in &mut self.children { + child.set_js_interrupt(interrupt); + } } /// Sets the index the next recorded log receives, see /// [`TracingInspector::set_next_log_index`]. - pub const fn set_next_log_index(&mut self, index: usize) { + pub fn set_next_log_index(&mut self, index: usize) { if let Some(tracing) = &mut self.tracing { tracing.set_next_log_index(index); } + for (_, child) in &mut self.children { + child.set_next_log_index(index); + } } /// Try converting this [MuxInspector] into a [MuxFrame]. - pub fn try_into_mux_frame( - &self, + /// + /// Call traces recover the transaction root when runtime gas charging halts before evm2 + /// invokes the call/create hooks. Recovered roots report `out of gas`. + pub fn try_into_mux_frame>( + &mut self, result: &TxResultWithState, + tx: &Recovered, + block_env: &BlockEnv, + tx_context: Option, db: &mut dyn DynDatabase, - tx_info: TransactionInfo, ) -> Result { + if let Some(inspector) = &mut self.tracing { + inspector.recover_runtime_oog_root(tx, &result.result); + inspector.set_transaction_gas_limit(tx.gas_limit()); + inspector.set_transaction_caller(tx.signer()); + } + let tx_info = transaction_info::(tx_context.as_ref(), block_env); + let mut frame = HashMap::with_capacity_and_hasher(self.configs.len(), Default::default()); for (tracer_type, config) in &self.configs { @@ -180,73 +252,50 @@ impl MuxInspector { ); } - // Add keccak256 preimage trace if inspector exists - if let Some(inspector) = &self.keccak256_preimage { - frame.insert( - GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()), - GethTrace::JS(inspector.json_result()?), - ); + for (tracer_type, child) in &mut self.children { + let trace = + child.get_result(tx_context, tx, block_env, result, db).map_err(Box::new)?; + frame.insert(tracer_type.clone(), trace); } Ok(MuxFrame(frame)) } +} - /// Finalizes the transaction root for tracers sharing the tracing inspector. - pub(crate) fn finalize_transaction( - &mut self, - tx: &Recovered, - result: &TxResultExt, - ) { - if let Some(inspector) = &mut self.tracing { - inspector.recover_runtime_oog_root(tx, result); - inspector.set_transaction_gas_limit(tx.gas_limit()); - inspector.set_transaction_caller(tx.signer()); +/// Calls a hook on every inspector of the mux. +macro_rules! fan_out { + ($mux:ident.$hook:ident($($arg:expr),*)) => {{ + for (_, child) in &mut $mux.children { + Inspector::::$hook(child, $($arg),*); } - } + if let Some(inspector) = &mut $mux.four_byte { + Inspector::::$hook(inspector, $($arg),*); + } + if let Some(inspector) = &mut $mux.tracing { + Inspector::::$hook(inspector, $($arg),*); + } + }}; } impl Inspector for MuxInspector { #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.initialize_interp(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.initialize_interp(interp); - } + fan_out!(self.initialize_interp(interp)); } #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.step(interp); - } - if let Some(ref mut inspector) = self.keccak256_preimage { - inspector.step(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.step(interp); - } + fan_out!(self.step(interp)); } #[inline] fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.step_end(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.step_end(interp); - } + fan_out!(self.step_end(interp)); } #[inline] fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { - if let Some(ref mut inspector) = self.four_byte { - >::log(inspector, log, host); - } - if let Some(ref mut inspector) = self.tracing { - >::log(inspector, log, host); - } + fan_out!(self.log(log, host)); } #[inline] @@ -255,13 +304,13 @@ impl Inspector for MuxInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - if let Some(ref mut inspector) = self.four_byte { - let _ = inspector.call(interp, message); + for (_, child) in &mut self.children { + let _ = child.call(interp, message); } - if let Some(ref mut inspector) = self.tracing { - return inspector.call(interp, message); + if let Some(inspector) = &mut self.four_byte { + let _ = inspector.call(interp, message); } - None + self.tracing.as_mut().and_then(|inspector| inspector.call(interp, message)) } #[inline] @@ -271,12 +320,7 @@ impl Inspector for MuxInspector { message: &Message, result: &mut MessageResult, ) { - if let Some(ref mut inspector) = self.four_byte { - inspector.call_end(interp, message, result); - } - if let Some(ref mut inspector) = self.tracing { - inspector.call_end(interp, message, result); - } + fan_out!(self.call_end(interp, message, result)); } #[inline] @@ -285,13 +329,13 @@ impl Inspector for MuxInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - if let Some(ref mut inspector) = self.four_byte { - let _ = inspector.create(interp, message); + for (_, child) in &mut self.children { + let _ = child.create(interp, message); } - if let Some(ref mut inspector) = self.tracing { - return inspector.create(interp, message); + if let Some(inspector) = &mut self.four_byte { + let _ = inspector.create(interp, message); } - None + self.tracing.as_mut().and_then(|inspector| inspector.create(interp, message)) } #[inline] @@ -301,12 +345,7 @@ impl Inspector for MuxInspector { message: &Message, result: &mut MessageResult, ) { - if let Some(ref mut inspector) = self.four_byte { - inspector.create_end(interp, message, result); - } - if let Some(ref mut inspector) = self.tracing { - inspector.create_end(interp, message, result); - } + fan_out!(self.create_end(interp, message, result)); } #[inline] @@ -317,35 +356,23 @@ impl Inspector for MuxInspector { value: &U256, host: &mut T::Host<'_>, ) { - if let Some(ref mut inspector) = self.four_byte { - >::selfdestruct( - inspector, contract, target, value, host, - ); - } - if let Some(ref mut inspector) = self.tracing { - >::selfdestruct( - inspector, contract, target, value, host, - ); - } + fan_out!(self.selfdestruct(contract, target, value, host)); } } /// Error type for [MuxInspector] #[derive(Debug, Error)] pub enum Error { - /// Config was provided for a tracer that does not expect it - #[error("unexpected config for tracer '{0:?}'")] - UnexpectedConfig(GethDebugBuiltInTracerType), - /// Unsupported tracer type (e.g. JS tracer) + /// Unsupported tracer type #[error("unsupported tracer type: '{0:?}'")] UnsupportedTracerType(GethDebugTracerType), - /// A tracer reached its limit of recorded bytes + /// Error when deserializing the config + #[error("error deserializing config: {0}")] + InvalidConfig(#[from] serde_json::Error), + /// Error from the inspector of a nested tracer #[error(transparent)] - Keccak256Preimage(#[from] Keccak256PreimageLimitExceeded), + Child(#[from] Box), /// Database operation failed #[error("database error {0:?}")] Database(#[from] DatabaseError), - /// Error when deserializing the config - #[error("error deserializing config: {0}")] - InvalidConfig(#[from] serde_json::Error), } diff --git a/crates/inspectors/src/tracing/utils.rs b/crates/inspectors/src/tracing/utils.rs index 7eab85038..4d8c2e55c 100644 --- a/crates/inspectors/src/tracing/utils.rs +++ b/crates/inspectors/src/tracing/utils.rs @@ -1,14 +1,16 @@ //! Utility functions for evm2 related ops. -use crate::tracing::config::TraceStyle; +use crate::tracing::{TransactionContext, config::TraceStyle}; use alloc::{ format, string::{String, ToString}, vec::Vec, }; use alloy_primitives::{Bytes, hex}; +use alloy_rpc_types_eth::TransactionInfo; use alloy_sol_types::{ContractError, GenericRevertReason}; use evm2::{ - AccountInfo, SpecId, + AccountInfo, EvmTypesHost, SpecId, + env::BlockEnv, evm::{DbResult, DynDatabase}, interpreter::InstrStop, }; @@ -176,6 +178,22 @@ pub(crate) fn maybe_revert_reason(output: &[u8]) -> Option { } } +/// Returns the [`TransactionInfo`] of a traced transaction. +pub(crate) fn transaction_info( + tx_context: Option<&TransactionContext>, + block_env: &BlockEnv, +) -> TransactionInfo { + #[allow(clippy::needless_update)] + TransactionInfo { + hash: tx_context.and_then(|c| c.tx_hash), + index: tx_context.and_then(|c| c.tx_index.map(|i| i as u64)), + block_hash: tx_context.and_then(|c| c.block_hash), + block_number: Some(block_env.number.saturating_to()), + base_fee: Some(block_env.basefee.saturating_to()), + ..Default::default() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 078318217..6d2f39dee 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -10,7 +10,6 @@ use crate::utils::{ use alloy_consensus::crypto::secp256k1::sign_message; use alloy_eips::eip7702::Authorization; use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, keccak256, map::HashMap}; -use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, @@ -696,9 +695,9 @@ fn test_geth_mux_tracer() { assert!(res.result.is_success()); let (ctx, inspector) = evm.ctx_inspector(); - let frame = inspector - .try_into_mux_frame(&res.tx_result, ctx.db_mut(), TransactionInfo::default()) - .unwrap(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); assert_eq!(frame.0.len(), 5); assert!(frame.0.contains_key(&GethDebugTracerType::BuiltInTracer( @@ -2307,9 +2306,9 @@ fn keccak256_preimage_tracer_runs_standalone_and_in_a_mux() { .unwrap(); let (ctx, inspector) = evm.ctx_inspector(); - let frame = inspector - .try_into_mux_frame(&res.tx_result, ctx.db_mut(), TransactionInfo::default()) - .unwrap(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); let GethTrace::JS(preimages) = &frame.0[&key] else { panic!("expected a JSON object") }; assert_eq!(preimages.as_object().unwrap().len(), 1); } @@ -2363,6 +2362,51 @@ fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { } } +#[test] +fn mux_runs_nested_mux_and_erc7562_tracers() { + let (contract, db) = contract_with_code(&hex!("5f5f00")); + let nested = serde_json::json!({"callTracer": {}, "noopTracer": {}}); + let nested_key = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::MuxTracer); + let config = MuxConfig(HashMap::from_iter([ + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FourByteTracer), None), + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::Erc7562Tracer), None), + (nested_key.clone(), Some(GethDebugTracerConfig(nested))), + ])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + assert_eq!(frame.0.len(), 3); + assert!(matches!( + frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::Erc7562Tracer)], + GethTrace::Erc7562Tracer(_) + )); + let GethTrace::MuxTracer(nested) = &frame.0[&nested_key] else { + panic!("expected a nested mux frame"); + }; + assert_eq!(nested.0.len(), 2); + assert!(matches!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)], + GethTrace::CallTracer(_) + )); + assert!(matches!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::NoopTracer)], + GethTrace::NoopTracer(_) + )); +} + /// The expected snapshots come from go-ethereum master a0cba163: the struct logger adds the slot /// of every SLOAD and SSTORE that does not fail before it runs, with the value an SLOAD reads or an /// SSTORE writes, and snapshots all slots of the contract so far. diff --git a/crates/inspectors/tests/it/geth_js.rs b/crates/inspectors/tests/it/geth_js.rs index d71341f31..7596bff11 100644 --- a/crates/inspectors/tests/it/geth_js.rs +++ b/crates/inspectors/tests/it/geth_js.rs @@ -7,9 +7,15 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, EmptyDB, ResultAndState, SpecId, TestDbExt, TransactTo, TxEnv, deploy_contract, }; -use alloy_primitives::{Address, U256, address, hex}; -use alloy_rpc_types_trace::geth::{GethDebugTracingOptions, GethTrace}; -use evm2_inspectors::tracing::{DebugInspector, js::JsInspector}; +use alloy_primitives::{Address, U256, address, hex, map::HashMap}; +use alloy_rpc_types_trace::geth::{ + GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethTrace, + mux::MuxConfig, +}; +use evm2_inspectors::tracing::{ + DebugInspector, MuxInspector, + js::{JsInspector, JsInspectorInterrupt}, +}; use serde_json::json; fn js_result( @@ -505,3 +511,65 @@ fn test_geth_debug_inspector_jstracer() { assert_eq!(result["coinbase"], json!(coinbase)); assert_eq!(result["error"], json!(false)); } + +#[test] +fn mux_runs_a_js_tracer_next_to_a_built_in_tracer() { + let code = "{ steps: 0, step: function() { this.steps++; }, fault: function() {}, \ + result: function() { return this.steps; } }"; + let contract = address!("0000000000000000000000000000000000000042"); + let mut db = CacheDB::new(EmptyDB::default()); + db.insert_account_info( + &contract, + AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f00").into())), + ); + let js_key = GethDebugTracerType::JsTracer(code.to_string()); + let call_key = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer); + let config = MuxConfig(HashMap::from_iter([(js_key.clone(), None), (call_key.clone(), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + let GethTrace::JS(steps) = &frame.0[&js_key] else { panic!("expected a JS result") }; + assert_eq!(steps.as_u64(), Some(3)); + assert!(matches!(frame.0[&call_key], GethTrace::CallTracer(_))); +} + +#[test] +fn mux_interrupts_its_js_tracers() { + let code = "{ fault: function() {}, result: function() { return 1; } }"; + let mut db = CacheDB::new(EmptyDB::default()); + db.insert_account_info(&Address::ZERO, AccountInfo::default()); + let config = + MuxConfig(HashMap::from_iter([(GethDebugTracerType::JsTracer(code.into()), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(Address::ZERO), + ..Default::default() + }) + .unwrap(); + + let interrupt = JsInspectorInterrupt::new(); + let (ctx, inspector) = evm.ctx_inspector(); + inspector.set_js_interrupt(&interrupt); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + assert!(inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).is_ok()); + + interrupt.interrupt(); + assert!(inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).is_err()); +} From 46235b639916c487af50f010abd382bf41f55137 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:14:07 +0100 Subject: [PATCH 027/159] fix(inspectors): match geth's JS tracer value conversions (#856) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several JS tracer values differed from geth's goja tracer. `log.memory.getUint(offset)` returns a bigint, not a byte array. `frame.getValue()` is `undefined` for STATICCALL, where geth passes no value. Arguments to `memory.slice`, `memory.getUint` and `stack.peek` now convert the way goja converts them for geth's `int64` Go parameters: `undefined`, `null`, NaN and infinities become 0, fractions truncate toward zero, and values outside the `int64` range become the minimum `int64`. Out-of-bounds memory errors use geth's texts (`offset %d, end %d` for `slice`, `available %d, offset %d, size 32` for `getUint`), and `stack.peek` reports the signed index. I ran each tracer through geth master (`a0cba163`, evm 1.17.11-unstable) `evm t8n --trace.tracer` (Cancun, code `602a6000526001600200`, so memory holds 42 at word 0 and the stack holds `[1, 2]` at STOP) and through `origin/main` and this branch on the same transaction. All 30 `memory`/`stack` expressions I tried now match geth; the table shows a sample. | Expression at STOP | geth master | `origin/main` | this PR | | --- | --- | --- | --- | | `memory.getUint(0).toString()` | `"42"` | 🔴 `"0,0,…,42"` (byte array) | 🟢 `"42"` | | `memory.getUint(NaN).toString()` | `"42"` | 🔴 error `invalid memory offset: NaN` | 🟢 `"42"` | | `memory.getUint(33)` | `tracer accessed out of bound memory: available 32, offset 33, size 32` | 🔴 `invalid memory offset: 33` | 🟢 same as geth | | `memory.slice(5, null)` | `tracer accessed out of bound memory: offset 5, end 0` | 🔴 `…available 32, offset 5, size 0` | 🟢 same as geth | | `toHex(memory.slice(0, Infinity))` | `"0x"` | 🔴 error `invalid memory end: inf` | 🟢 `"0x"` | | `memory.slice(0, 1e30)` | `…offset 0, end -9223372036854775808` | 🔴 `RangeError: Index must be between 0 and 2^53 - 1` | 🟢 same as geth | | `stack.peek(NaN).toString()` | `"2"` | 🔴 error `…index NaN` | 🟢 `"2"` | | `stack.peek(-1)` | `…size 2, index -1` | 🟢 same | 🟢 same | | STATICCALL `frame.getValue()` | `undefined` | 🔴 `0n` | 🟢 `undefined` | Two differences remain in how errors surface, and I did not change them here: evm2 prefixes the message with the error kind (`TypeError: …`) while geth prints the bare message, and a `try`/`catch` in the tracer can catch these errors while geth's interrupt cannot be caught. Opcode names for undefined and EOF opcodes are left for after #849, which adds the geth name table. --- crates/inspectors/src/tracing/js/bindings.rs | 144 +++++++--------- crates/inspectors/src/tracing/js/mod.rs | 165 +++++++++++-------- 2 files changed, 154 insertions(+), 155 deletions(-) diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 9cc85d57d..3ecc123b1 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -312,8 +312,15 @@ impl ReusableCallFrame { let get_to = state_fn(ctx, state.clone(), 0, |state, _, ctx| { address_to_uint8_array_value(state.borrow().contract, ctx) }); - let get_value = - state_fn(ctx, state.clone(), 0, |state, _, _| Ok(to_bigint(state.borrow().value))); + // Geth passes no value for STATICCALL, which `getValue()` reports as undefined. + let get_value = state_fn(ctx, state.clone(), 0, |state, _, _| { + let state = state.borrow(); + Ok(if matches!(state.kind, FrameKind::Call(CallKind::StaticCall)) { + JsValue::undefined() + } else { + to_bigint(state.value) + }) + }); let get_input = state_fn(ctx, state.clone(), 0, |state, _, ctx| { to_uint8_array_value(state.borrow().input.clone(), ctx) }); @@ -726,16 +733,18 @@ impl StackView { } /// Returns the item `idx` positions from the top of the pre-execution stack. - fn peek(&self, idx: usize) -> JsResult { + fn peek(&self, idx: i64) -> JsResult { let Some(post) = self.post else { return Err(type_error("tracer accessed stack outside of a callback".to_string())); }; let saved = self.saved.len(); - let item = if idx < saved { - Some(self.saved[saved - 1 - idx]) - } else { - (idx < self.len).then(|| post.get(self.len - 1 - idx).copied()).flatten() - }; + let item = usize::try_from(idx).ok().and_then(|idx| { + if idx < saved { + Some(self.saved[saved - 1 - idx]) + } else { + (idx < self.len).then(|| post.get(self.len - 1 - idx).copied()).flatten() + } + }); item.ok_or_else(|| { type_error(format!( "tracer accessed out of bound stack: size {}, index {idx}", @@ -865,8 +874,7 @@ fn build_step_stack_object(state: Shared, ctx: &mut Context) -> Js Ok(JsValue::from(state.borrow().stack.len())) }); let peek = state_fn(ctx, state, 1, |state, args, ctx| { - let len = state.borrow().stack.len(); - let idx = parse_stack_index(args.get_or_undefined(0), len, ctx)?; + let idx = to_int64(args.get_or_undefined(0), ctx)?; Ok(to_bigint(state.borrow().stack.peek(idx)?)) }); @@ -881,37 +889,35 @@ fn build_step_memory_object(state: Shared, ctx: &mut Context) -> J Ok(JsValue::from(state.borrow().memory.len() as u64)) }); let slice = state_fn(ctx, state.clone(), 2, |state, args, ctx| { - let len = state.borrow().memory.len(); - let start = parse_memory_offset(args.get_or_undefined(0), "start", ctx)?; - let end = parse_memory_offset(args.get_or_undefined(1), "end", ctx)?; - if end == start { + let begin = to_int64(args.get_or_undefined(0), ctx)?; + let end = to_int64(args.get_or_undefined(1), ctx)?; + if end == begin { return uint8_array_from_block(AlignedVec::from_iter(0, core::iter::empty()), ctx); } - if end < start { - return Err(memory_out_of_bounds_error(len, start, end.saturating_sub(start))); + if end < begin || begin < 0 { + return Err(type_error(format!( + "tracer accessed out of bound memory: offset {begin}, end {end}" + ))); } - if end > len { - let padding = end - len; - if padding > MEMORY_PAD_LIMIT { - return Err(type_error(format!( - "reached limit for padding memory slice: {padding}" - ))); - } + let len = state.borrow().memory.len(); + let padding = end - len as i64; + if padding > MEMORY_PAD_LIMIT { + return Err(type_error(format!("reached limit for padding memory slice: {padding}"))); } - let bytes = state.borrow().memory.bytes(start..end)?; + let bytes = state.borrow().memory.bytes(begin as usize..end as usize)?; uint8_array_from_block(bytes, ctx) }); let get_uint = state_fn(ctx, state, 1, |state, args, ctx| { let len = state.borrow().memory.len(); - let offset = parse_memory_index(args.get_or_undefined(0), "offset", len, ctx)?; - let Some(end) = offset.checked_add(32) else { - return Err(memory_out_of_bounds_error(len, offset, 32)); - }; - if end > len { - return Err(memory_out_of_bounds_error(len, offset, 32)); + let offset = to_int64(args.get_or_undefined(0), ctx)?; + if offset < 0 || offset.saturating_add(32) > len as i64 { + return Err(type_error(format!( + "tracer accessed out of bound memory: available {len}, offset {offset}, size 32" + ))); } - let bytes = state.borrow().memory.bytes(offset..end)?; - uint8_array_from_block(bytes, ctx) + let offset = offset as usize; + let bytes = state.borrow().memory.bytes(offset..offset + 32)?; + Ok(to_bigint(U256::from_be_slice(bytes.as_slice()))) }); obj.set(js_string!("slice"), slice, false, ctx)?; @@ -944,60 +950,28 @@ fn build_step_contract_object( Ok(obj) } -fn parse_stack_index(value: &JsValue, len: usize, ctx: &mut Context) -> JsResult { - let index = value.to_numeric_number(ctx)?; - if !index.is_finite() || index < 0. || index > usize::MAX as f64 { - return Err(type_error(format!( - "tracer accessed out of bound stack: size {len}, index {index}" - ))); - } - Ok(index as usize) -} - -fn invalid_memory_index_error(name: &str, index: impl core::fmt::Display) -> JsError { - type_error(format!("invalid memory {name}: {index}")) -} - -const MEMORY_PAD_LIMIT: usize = 1024 * 1024; - -fn memory_out_of_bounds_error(len: usize, offset: usize, size: usize) -> JsError { - type_error(format!( - "tracer accessed out of bound memory: available {len}, offset {offset}, size {size}" - )) -} - -fn parse_memory_index( - value: &JsValue, - name: &str, - len: usize, - ctx: &mut Context, -) -> JsResult { - let index = parse_memory_offset(value, name, ctx)?; - if index > len { - return Err(invalid_memory_index_error(name, index)); - } - Ok(index) -} - -fn parse_memory_offset(value: &JsValue, name: &str, ctx: &mut Context) -> JsResult { - if value.is_undefined() { - return Err(invalid_memory_index_error(name, "undefined")); - } - if let Some(index) = value.as_number() - && (!index.is_finite() || index < 0.) - { - return Err(invalid_memory_index_error(name, index)); - } - if let Some(index) = value.as_bigint() { - // Boa's `ToIndex` rejects BigInt, but stack-derived tracer values are BigInt. - let index = index.to_string(); - index.parse::().map_err(|_| invalid_memory_index_error(name, &index)) +/// Converts a JS argument to the `int64` goja passes to a Go callback. +/// +/// NaN and infinities become 0, fractions truncate toward zero and values outside the `int64` +/// range become `i64::MIN`, as Go's float conversion does on amd64. +fn to_int64(value: &JsValue, ctx: &mut Context) -> JsResult { + // Boa's `ToNumber` rejects BigInt, but stack-derived tracer values are BigInt. Geth's bigint + // objects reach goja through their `Number` value, so go through a float as well. + let number = match value.as_bigint() { + Some(value) => value.to_f64(), + None => value.to_number(ctx)?, + }; + Ok(if !number.is_finite() { + 0 + } else if (-(2f64.powi(63))..2f64.powi(63)).contains(&number) { + number as i64 } else { - let index = value.to_index(ctx)?; - usize::try_from(index).map_err(|_| invalid_memory_index_error(name, index)) - } + i64::MIN + }) } +const MEMORY_PAD_LIMIT: i64 = 1024 * 1024; + /// Represents the contract object #[derive(Clone, Debug, Default)] pub(crate) struct Contract { @@ -1550,7 +1524,7 @@ mod tests { db.with_changes_scope(&state, &mut database, || { with_step(&log, opcode::STOP, &stack, &memory, &contract, || { assert!(ctx.eval(Source::from_bytes( - "savedLog.stack.peek(0) === 42n && savedLog.memory.getUint(0)[0] === 7 && savedDb.getBalance('00') === 0n" + "savedLog.stack.peek(0) === 42n && savedLog.memory.getUint(0) >> 248n === 7n && savedDb.getBalance('00') === 0n" )).unwrap().as_boolean().unwrap()); match outcome { 0 => Ok(JsValue::undefined()), @@ -1603,7 +1577,7 @@ mod tests { view.post = Some(post); assert_eq!(view.len(), 3); for (idx, expected) in pre.iter().rev().enumerate() { - assert_eq!(view.peek(idx).unwrap(), *expected, "op {op:#x} idx {idx}"); + assert_eq!(view.peek(idx as i64).unwrap(), *expected, "op {op:#x} idx {idx}"); } assert!(view.peek(3).is_err()); } diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index a02c64580..03ac6c61f 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -1364,27 +1364,20 @@ mod tests { } #[test] - fn test_memory_slice_rejects_non_finite_indexes() { + fn test_memory_slice_non_finite_indexes_are_zero() { let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.memory.slice(Infinity, NaN)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_memory_slice_rejects_non_finite_end() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.memory.slice(0, Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + this.res.push(log.memory.slice(Infinity, NaN).length, log.memory.slice(NaN, 2).length); + this.res.push(log.memory.slice(0, Infinity).length); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + let res = run_trace(code, None, true); + assert_eq!(res, json!([0, 2, 0])); } #[test] @@ -1412,75 +1405,107 @@ mod tests { } #[test] - fn test_stack_peek() { + fn test_memory_get_uint_returns_bigint() { let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(-1)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_stack_peek_nan() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(NaN)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_stack_peek_infinity() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + var word = log.memory.getUint(0); + this.res.push(typeof word, word.toString(), log.memory.getUint(NaN).toString()); + this.res.push(log.memory.getUint(Infinity).toString()); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + // PUSH1 42, PUSH1 0, MSTORE, STOP + let res = run_trace(code, Some(bytes!("0x602a60005200")), true); + assert_eq!(res, json!(["bigint", "42", "42", "42"])); } #[test] - fn test_memory_get_uint() { - let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(-64)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + fn test_memory_and_stack_errors_match_geth() { + // PUSH1 42, PUSH1 0, MSTORE, PUSH1 1, PUSH1 2, STOP + let contract = Some(bytes!("0x602a600052600160020000")); + for (expression, expected) in [ + ("log.memory.slice(5, 2)", "tracer accessed out of bound memory: offset 5, end 2"), + ("log.memory.slice(-1, 4)", "tracer accessed out of bound memory: offset -1, end 4"), + ( + "log.memory.slice(0, 1e30)", + "tracer accessed out of bound memory: offset 0, end -9223372036854775808", + ), + ( + "log.memory.slice(0, 1048577 + 32)", + "reached limit for padding memory slice: 1048577", + ), + ( + "log.memory.getUint(1)", + "tracer accessed out of bound memory: available 32, offset 1, size 32", + ), + ( + "log.memory.getUint(-1)", + "tracer accessed out of bound memory: available 32, offset -1, size 32", + ), + ("log.stack.peek(2)", "tracer accessed out of bound stack: size 2, index 2"), + ("log.stack.peek(-1)", "tracer accessed out of bound stack: size 2, index -1"), + ( + "log.stack.peek(9223372036854775807n)", + "tracer accessed out of bound stack: size 2, index -9223372036854775808", + ), + ] { + let code = format!( + "{{step: function(log) {{ if (log.op.toString() === 'STOP') {expression}; }}, fault: function() {{}}, result: function() {{}}}}" + ); + let JsInspectorError::JsError(error) = + try_run_trace(&code, contract.clone(), true).unwrap_err() + else { + panic!("{expression}: unexpected error"); + }; + assert_eq!( + error.as_native().unwrap().message(), + format!("{expected} in server-side tracer function 'step'"), + "{expression}" + ); + } } #[test] - fn test_memory_get_uint_rejects_non_finite_offset() { + fn test_stack_peek_index_conversion() { let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + this.res.push( + log.stack.peek(NaN).toString(), + log.stack.peek(Infinity).toString(), + log.stack.peek(1.5).toString(), + log.stack.peek('1').toString(), + log.stack.peek(1n).toString() + ); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + let res = run_trace(code, Some(bytes!("0x600160020000")), true); + assert_eq!(res, json!(["2", "2", "1", "1", "1"])); } #[test] - fn test_memory_get_uint_rejects_nan_offset() { + fn test_frame_value_is_undefined_for_staticcall() { let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(NaN)); }, + res: [], + enter: function(frame) { this.res.push([frame.getType(), String(frame.getValue())]); }, + exit: function() {}, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + // CALL and STATICCALL of the identity precompile. + let calls = bytes!("0x6000600060006000600060046101fff1600060006000600060046101fffa00"); + assert_eq!( + run_trace(code, Some(calls), true), + json!([["CALL", "0"], ["STATICCALL", "undefined"]]) + ); } #[test] @@ -1775,7 +1800,7 @@ mod tests { if (op === 'MSTORE8' || op === 'STOP') { this.res.push(log.memory.length()); if (log.memory.length() > 0) { - this.res.push(log.memory.getUint(0)[0]); + this.res.push(Number(log.memory.getUint(0) >> 248n)); this.res.push(log.memory.slice(0, 2)[0]); } } From cfdec8bd1cc25b029556f3dc0e359336d2bf96c9 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:17:54 +0100 Subject: [PATCH 028/159] perf(jit-runtime): link AOT artifacts in process (#851) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AOT mode spawned `cc -shared -fuse-ld=lld` for every artifact and then `dlopen`ed the result. The link costs a constant ~37M user instructions and 8–21 ms whatever the contract size, because almost all of it is starting `cc` and `ld.lld`. For small contracts it costs as much as the compile, and it requires `cc` and `lld` on the host. An AOT artifact is now the relocatable object that the out-of-process JIT path already produces. The artifact store persists it, and loading an artifact (after a compile, from a persisted artifact on a miss, and in the startup preload) links it with JITLink through the existing `JitObjectLinker`. The builtin symbols are derived from `Builtin::ALL` at load time, so the manifest does not change. The store API says `object` instead of `dylib` (`object_bytes`, `object_path`, `.o` files), and the runtime drops `LoadedLibrary` and `libloading`. Linked AOT code now counts in `jit_total_bytes()`, so the memory budget evicts AOT entries as well (they reload from the store), and `ABI_VERSION` is bumped because stored artifacts are objects now. Linked AOT code is anonymous memory, so it no longer shows up in `/proc/maps` or perf by file name. The artifact key, manifest checks, atomic persist, and preload semantics are unchanged, and `evm2_jit_codegen::Linker` and the CLI `--aot` path still produce shared libraries. Measured on one runner with a scratch test around `JitBackend::prepare_aot_sync` (AOT config, one worker, fresh process, `perf stat -e instructions:u` minus a run that only creates the backend, median of 5; times are medians of the same runs on a shared machine): | bytecode | bytes | compile + link + load, before | after | prepare time, before → after | | :-- | --: | --: | --: | --: | | push0_proxy | 44 | 59.0M | 26.3M 🟢 | 12.3 ms → 4.3 ms | | counter | 367 | 78.9M | 46.2M 🟢 | 14.4 ms → 7.0 ms | | usdc_proxy | 2186 | 272.6M | 240.1M 🟢 | 30.7 ms → 24.2 ms | | weth | 3124 | 569.4M | 536.9M 🟢 | 57.3 ms → 48.0 ms | | erc20_transfer | 3446 | 609.0M | 576.5M 🟢 | 59.8 ms → 51.7 ms | | uniswap_v2_pair | 11293 | 2054.3M | 2022.9M 🟢 | 174.1 ms → 162.9 ms | | seaport | 24367 | 9903.0M | 9872.8M 🟢 | not stable | Every compile saves about 32M instructions. Loading a persisted artifact costs more, because JITLink replaces `dlopen`: restarting a backend over one stored artifact (preload) goes from 0.2M instructions and 0.2 ms to 2.2–2.8M and 0.4–0.8 ms 🔴, which includes creating the linker's LLVM backend once per preload. --- Cargo.lock | 1 - crates/jit/codegen/src/lib.rs | 6 +- crates/jit/runtime/Cargo.toml | 1 - crates/jit/runtime/src/runtime/api.rs | 37 +--- crates/jit/runtime/src/runtime/backend.rs | 210 ++++++++++------------ crates/jit/runtime/src/runtime/config.rs | 2 +- crates/jit/runtime/src/runtime/mod.rs | 29 +-- crates/jit/runtime/src/runtime/storage.rs | 49 ++--- crates/jit/runtime/src/runtime/tests.rs | 35 +++- crates/jit/runtime/src/runtime/worker.rs | 81 ++++----- 10 files changed, 205 insertions(+), 246 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d15558309..505533a5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2816,7 +2816,6 @@ dependencies = [ "evm2-macros", "eyre", "libc", - "libloading", "paste", "quanta", "rayon", diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index 39a8611a8..f42914052 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -18,9 +18,9 @@ pub use compiler::{CompileTimings, EvmCompiler, EvmCompilerInput, HostLayout}; mod linker; pub use linker::{Linker, shared_library_path}; -/// ABI version of compiled artifacts. Bump when the calling convention, the execution layout or the -/// `InstrStop` discriminants change. -pub const ABI_VERSION: u32 = 5; +/// ABI version of compiled artifacts. Bump when the calling convention, the execution layout, the +/// `InstrStop` discriminants, or the stored artifact format change. +pub const ABI_VERSION: u32 = 6; /// Internal tests and testing utilities. Not public API. #[cfg(test)] diff --git a/crates/jit/runtime/Cargo.toml b/crates/jit/runtime/Cargo.toml index a5e3f346f..05147768b 100644 --- a/crates/jit/runtime/Cargo.toml +++ b/crates/jit/runtime/Cargo.toml @@ -35,7 +35,6 @@ dashmap = "6" derive_more = { version = "2", default-features = false, features = ["debug"] } eyre.workspace = true libc.workspace = true -libloading = "0.9" tempfile = "3.10" quanta.workspace = true rayon.workspace = true diff --git a/crates/jit/runtime/src/runtime/api.rs b/crates/jit/runtime/src/runtime/api.rs index 73ff1b8c4..abfcb6dd5 100644 --- a/crates/jit/runtime/src/runtime/api.rs +++ b/crates/jit/runtime/src/runtime/api.rs @@ -47,8 +47,8 @@ pub struct CompiledProgram { pub key: RuntimeCacheKey, /// Whether this is an AOT or JIT program. pub kind: ProgramKind, - /// Keeps the backing memory (shared library / JIT module) alive. - _backing: ProgramBacking, + /// Keeps the linked machine code alive. + _backing: Arc, } impl std::fmt::Debug for CompiledProgram { @@ -62,13 +62,13 @@ impl std::fmt::Debug for CompiledProgram { } impl CompiledProgram { - /// Creates a new compiled program backed by a loaded shared library. + /// Creates a new compiled program backed by a linked AOT object. pub(crate) const fn new_aot( key: RuntimeCacheKey, func: EvmCompilerFn, - library: Arc, + backing: Arc, ) -> Self { - Self { key, kind: ProgramKind::Aot, func, _backing: ProgramBacking::LoadedLibrary(library) } + Self { key, kind: ProgramKind::Aot, func, _backing: backing } } /// Creates a new compiled program backed by a JIT module's @@ -78,7 +78,7 @@ impl CompiledProgram { func: EvmCompilerFn, backing: Arc, ) -> Self { - Self { key, kind: ProgramKind::Jit, func, _backing: ProgramBacking::JitModule(backing) } + Self { key, kind: ProgramKind::Jit, func, _backing: backing } } } @@ -101,28 +101,3 @@ pub enum ProgramKind { /// Compiled at runtime by the JIT. Jit, } - -/// Backing storage that keeps compiled code alive. -#[expect(dead_code, reason = "variant fields are held alive for Drop")] -enum ProgramBacking { - /// A dynamically loaded shared library (AOT). - LoadedLibrary(Arc), - /// An ORCv2 ResourceTracker that owns JIT machine code. - /// Dropping the last reference calls `tracker.remove()`, freeing the code. - JitModule(Arc), -} - -/// Owns a loaded shared library. -/// -/// Dropping this unloads the library. -pub(crate) struct LoadedLibrary { - /// The loaded library. - _library: libloading::Library, -} - -impl LoadedLibrary { - /// Creates a new loaded library wrapper. - pub(crate) const fn new(library: libloading::Library) -> Self { - Self { _library: library } - } -} diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index 005bd20b9..dea7df93f 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -1,15 +1,17 @@ use crate::{ - EvmCompilerFn, eyre, + EvmCompilerFn, + eyre::{self, WrapErr}, runtime::{ LookupRequest, - api::{CompiledProgram, LoadedLibrary, ProgramKind}, + api::{CompiledProgram, ProgramKind}, config::{CompilationEvent, CompilationKind, RuntimeConfig, RuntimeTuning}, storage::{ - ArtifactKey, ArtifactManifest, ArtifactStore, RuntimeCacheKey, compile_config_hash, + ArtifactKey, ArtifactManifest, ArtifactStore, RuntimeCacheKey, StoredArtifact, + compile_config_hash, }, worker::{ AotSuccess, CompileJob, JitCodeBacking, JitObjectSuccess, SyncNotifier, WorkerPool, - WorkerResult, WorkerSuccess, + WorkerResult, WorkerSuccess, builtin_symbol_names, }, }, }; @@ -24,7 +26,7 @@ use quanta::Instant; #[cfg(feature = "llvm")] use std::ffi::CString; use std::{ - mem, + fs, mem, ops::ControlFlow, sync::{Arc, atomic::Ordering}, time::{SystemTime, UNIX_EPOCH}, @@ -66,24 +68,26 @@ fn jit_total_bytes() -> usize { } #[cfg(feature = "llvm")] -struct JitObjectLinker { +pub(crate) struct JitObjectLinker { backend: Option, /// Resolves the builtins of a custom host, which the helper leaves as symbols. resolver: Option usize>, } #[cfg(not(feature = "llvm"))] -struct JitObjectLinker; +pub(crate) struct JitObjectLinker; #[cfg(feature = "llvm")] impl JitObjectLinker { - const fn new(resolver: Option usize>) -> Self { + pub(crate) const fn new(resolver: Option usize>) -> Self { Self { backend: None, resolver } } fn link( &mut self, - success: &JitObjectSuccess, + symbol_name: &str, + object: &[u8], + builtin_symbols: &[String], ) -> eyre::Result<(EvmCompilerFn, Arc)> { let backend = match &mut self.backend { Some(backend) => backend, @@ -99,9 +103,8 @@ impl JitObjectLinker { } }; - let symbol_name = CString::new(success.symbol_name.clone())?; - let builtin_symbols = success - .builtin_symbols + let symbol_name = CString::new(symbol_name)?; + let builtin_symbols = builtin_symbols .iter() .map(|name| { // The helper suffixed the symbols with the resolver's address. @@ -118,11 +121,8 @@ impl JitObjectLinker { Ok((CString::new(name.as_str())?, addr)) }) .collect::>>()?; - let (addr, tracker, jd_guard) = backend.link_jit_object_in_fresh_dylib( - &symbol_name, - &success.object_bytes, - &builtin_symbols, - )?; + let (addr, tracker, jd_guard) = + backend.link_jit_object_in_fresh_dylib(&symbol_name, object, &builtin_symbols)?; let func = EvmCompilerFn::new(unsafe { std::mem::transmute::(addr) }); Ok((func, Arc::new(JitCodeBacking::new(tracker, jd_guard)))) @@ -131,18 +131,36 @@ impl JitObjectLinker { #[cfg(not(feature = "llvm"))] impl JitObjectLinker { - const fn new(_resolver: Option usize>) -> Self { + pub(crate) const fn new(_resolver: Option usize>) -> Self { Self } fn link( &mut self, - _success: &JitObjectSuccess, + _symbol_name: &str, + _object: &[u8], + _builtin_symbols: &[String], ) -> eyre::Result<(EvmCompilerFn, Arc)> { eyre::bail!("LLVM backend not available"); } } +impl JitObjectLinker { + /// Reads and links the object of a stored AOT artifact. + pub(crate) fn link_stored( + &mut self, + key: RuntimeCacheKey, + stored: &StoredArtifact, + ) -> eyre::Result { + let object = fs::read(&stored.object_path) + .wrap_err_with(|| format!("read {:?}", stored.object_path))?; + // The object may reference any builtin of the base host. + let (func, backing) = + self.link(&stored.manifest.symbol_name, &object, &builtin_symbol_names(None))?; + Ok(CompiledProgram::new_aot(key, func, backing)) + } +} + /// Commands sent to the backend thread on the bounded command channel. /// /// Lookup-observed events are NOT carried here — they go through the @@ -479,39 +497,25 @@ impl BackendState { ); false } - Ok(Some(stored)) => { - match (|| -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .map_err(|e| eyre::eyre!("dlopen {:?}: {e}", stored.dylib_path))?; - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = - library.get(stored.manifest.symbol_name.as_bytes()).map_err(|e| { - eyre::eyre!("symbol '{}': {e}", stored.manifest.symbol_name) - })?; - *sym - }; - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(*key, func, library)) - })() { - Ok(program) => { - debug!( - code_hash = %key.code_hash, - spec_id = ?key.spec_id, - "loaded existing AOT artifact from store, skipping recompilation", - ); - self.insert_resident(*key, Arc::new(program)); - true - } - Err(e) => { - warn!( - code_hash = %key.code_hash, - error = %e, - "failed to load persisted AOT artifact, will recompile", - ); - false - } + Ok(Some(stored)) => match self.jit_object_linker.link_stored(*key, &stored) { + Ok(program) => { + debug!( + code_hash = %key.code_hash, + spec_id = ?key.spec_id, + "loaded existing AOT artifact from store, skipping recompilation", + ); + self.insert_resident(*key, Arc::new(program)); + true } - } + Err(e) => { + warn!( + code_hash = %key.code_hash, + error = %e, + "failed to load persisted AOT artifact, will recompile", + ); + false + } + }, Ok(None) => false, Err(e) => { warn!( @@ -657,7 +661,11 @@ impl BackendState { success: JitObjectSuccess, compile_duration: std::time::Duration, ) { - match self.jit_object_linker.link(&success) { + match self.jit_object_linker.link( + &success.symbol_name, + &success.object_bytes, + &success.builtin_symbols, + ) { Ok((func, backing)) => { let program = Arc::new(CompiledProgram::new_jit(key, func, backing)); self.insert_resident(key, program); @@ -689,13 +697,13 @@ impl BackendState { fn handle_aot_success(&mut self, key: RuntimeCacheKey, success: AotSuccess) { let artifact_key = self.artifact_key(key); - let content_hash = keccak256(&success.dylib_bytes).0; + let content_hash = keccak256(&success.object_bytes).0; let manifest = ArtifactManifest { artifact_key, symbol_name: success.symbol_name.clone(), bytecode_len: success.bytecode_len, - artifact_len: success.dylib_bytes.len(), + artifact_len: success.object_bytes.len(), created_at_unix_secs: SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_secs()) @@ -705,7 +713,7 @@ impl BackendState { // Persist to store if available. if let Some(store) = &self.store { - if let Err(e) = store.store(&artifact_key, &manifest, &success.dylib_bytes) { + if let Err(e) = store.store(&artifact_key, &manifest, &success.object_bytes) { warn!( code_hash = %key.code_hash, error = %e, @@ -719,49 +727,35 @@ impl BackendState { debug!( code_hash = %key.code_hash, spec_id = ?key.spec_id, - dylib_len = success.dylib_bytes.len(), + object_len = success.object_bytes.len(), "AOT artifact persisted to store", ); - // Load from store to get the canonical path, then dlopen. + // Load from store to get the canonical path, then link. match store.load(&artifact_key) { - Ok(Some(stored)) => { - match (|| -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .map_err(|e| eyre::eyre!("dlopen {:?}: {e}", stored.dylib_path))?; - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = - library.get(success.symbol_name.as_bytes()).map_err(|e| { - eyre::eyre!("symbol '{}': {e}", success.symbol_name) - })?; - *sym - }; - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(key, func, library)) - })() { - Ok(program) => { - self.insert_resident(key, Arc::new(program)); - self.remove_entry(&key); - self.inner.stats.compilations_succeeded.fetch_add(1, Ordering::Relaxed); - - debug!( - code_hash = %key.code_hash, - spec_id = ?key.spec_id, - "AOT program loaded into resident map", - ); - } - Err(e) => { - warn!( - code_hash = %key.code_hash, - error = %e, - "failed to load persisted AOT artifact", - ); - // Persisted successfully but couldn't load — remove so JIT can retry. - self.remove_entry(&key); - self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); - } + Ok(Some(stored)) => match self.jit_object_linker.link_stored(key, &stored) { + Ok(program) => { + self.insert_resident(key, Arc::new(program)); + self.remove_entry(&key); + self.inner.stats.compilations_succeeded.fetch_add(1, Ordering::Relaxed); + + debug!( + code_hash = %key.code_hash, + spec_id = ?key.spec_id, + "AOT program linked into resident map", + ); } - } + Err(e) => { + warn!( + code_hash = %key.code_hash, + error = %e, + "failed to link persisted AOT artifact", + ); + // Persisted successfully but couldn't link — remove so JIT can retry. + self.remove_entry(&key); + self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); + } + }, Ok(None) => { warn!( code_hash = %key.code_hash, @@ -845,18 +839,13 @@ impl BackendState { } } - // Phase 3: enforce memory budget by evicting LRU JIT entries. + // Phase 3: enforce memory budget by evicting LRU entries. if budget > 0 && jit_total_bytes() > budget { - // Collect JIT entries sorted by last_hit_at ascending (oldest first). - // AOT entries are excluded because they don't contribute to `jit_total_bytes()`. - let mut entries: Vec<(RuntimeCacheKey, Instant)> = self - .resident_meta - .iter() - .filter(|(key, _)| { - self.inner.resident.get(key).is_some_and(|p| matches!(p.kind, ProgramKind::Jit)) - }) - .map(|(key, meta)| (*key, meta.last_hit_at)) - .collect(); + // Collect entries sorted by last_hit_at ascending (oldest first). AOT entries count + // too: their linked objects are part of `jit_total_bytes()`, and an evicted one + // reloads from the store. + let mut entries: Vec<(RuntimeCacheKey, Instant)> = + self.resident_meta.iter().map(|(key, meta)| (*key, meta.last_hit_at)).collect(); entries.sort_by_key(|(_, t)| *t); for (key, _) in entries { @@ -1609,16 +1598,13 @@ mod tests { #[test] fn jit_object_linker_relinks_live_symbol_name() { let success = compile_jit_object("jit_duplicate_symbol"); - let success2 = JitObjectSuccess { - symbol_name: success.symbol_name.clone(), - object_bytes: success.object_bytes.clone(), - builtin_symbols: success.builtin_symbols.clone(), - }; let mut linker = JitObjectLinker::new(None); - let (_first_func, _first_backing) = linker.link(&success).unwrap(); - let (_second_func, _second_backing) = linker - .link(&success2) + let link = |linker: &mut JitObjectLinker| { + linker.link(&success.symbol_name, &success.object_bytes, &success.builtin_symbols) + }; + let (_first_func, _first_backing) = link(&mut linker).unwrap(); + let (_second_func, _second_backing) = link(&mut linker) .expect("same symbol should link while previous backing remains alive"); } } diff --git a/crates/jit/runtime/src/runtime/config.rs b/crates/jit/runtime/src/runtime/config.rs index 58fd4b33a..7826c5d97 100644 --- a/crates/jit/runtime/src/runtime/config.rs +++ b/crates/jit/runtime/src/runtime/config.rs @@ -162,7 +162,7 @@ pub struct CompilationEvent { pub enum CompilationKind { /// JIT compilation (in-memory function pointer). Jit, - /// AOT compilation (shared library artifact). + /// AOT compilation (relocatable object artifact). Aot, } diff --git a/crates/jit/runtime/src/runtime/mod.rs b/crates/jit/runtime/src/runtime/mod.rs index 06f4c290a..a3b410e12 100644 --- a/crates/jit/runtime/src/runtime/mod.rs +++ b/crates/jit/runtime/src/runtime/mod.rs @@ -5,13 +5,11 @@ //! - Fire-and-forget lookup-observed events to the backend thread. //! - Background JIT compilation for hot keys (threshold-based promotion). -use crate::{ - EvmCompilerFn, - eyre::{self, WrapErr}, -}; +use crate::eyre::{self, WrapErr}; use alloy_primitives::B256; -use api::LoadedLibrary; -use backend::{Command, CompileJitRequest, EventQueue, PrepareAotRequest, ResidentMap}; +use backend::{ + Command, CompileJitRequest, EventQueue, JitObjectLinker, PrepareAotRequest, ResidentMap, +}; use crossbeam_channel as chan; use crossbeam_queue::ArrayQueue; use evm2::SpecId; @@ -657,6 +655,7 @@ impl JitBackend { info!(count = artifacts.len(), "loading AOT artifacts"); let config_hash = storage::compile_config_hash(config); + let mut linker = JitObjectLinker::new(config.host.resolver); let mut out: Vec<(RuntimeCacheKey, Arc)> = Vec::with_capacity(artifacts.len()); let mut loaded = 0u64; @@ -677,7 +676,7 @@ impl JitBackend { continue; } - match Self::load_artifact(&artifact_key, &stored) { + match linker.link_stored(key, &stored) { Ok(program) => { out.push((key, Arc::new(program))); loaded += 1; @@ -697,22 +696,6 @@ impl JitBackend { Ok(out) } - /// Loads a single artifact: `dlopen`s the dylib path and resolves the symbol. - fn load_artifact(key: &ArtifactKey, stored: &StoredArtifact) -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .wrap_err_with(|| format!("dlopen {:?}", stored.dylib_path))?; - - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = library - .get(stored.manifest.symbol_name.as_bytes()) - .wrap_err_with(|| format!("symbol '{}'", stored.manifest.symbol_name))?; - *sym - }; - - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(key.runtime, func, library)) - } - /// Applies a clear before startup, or queues it after the backend takes ownership. /// The startup lock serializes this decision with AOT preload and thread creation. fn send_clear(&self, command: Command) { diff --git a/crates/jit/runtime/src/runtime/storage.rs b/crates/jit/runtime/src/runtime/storage.rs index cbcbfc8fd..8210a611c 100644 --- a/crates/jit/runtime/src/runtime/storage.rs +++ b/crates/jit/runtime/src/runtime/storage.rs @@ -73,13 +73,13 @@ pub(crate) fn compile_config_hash(config: &RuntimeConfig) -> B256 { keccak256(bytes) } -/// A stored artifact consisting of a manifest and a path to the compiled dylib. +/// A stored artifact consisting of a manifest and a path to the relocatable object. #[derive(Clone, Debug)] pub struct StoredArtifact { /// Metadata about the artifact. pub manifest: ArtifactManifest, - /// Path to the shared library on disk. The store owns and manages these files. - pub dylib_path: PathBuf, + /// Path to the relocatable object on disk. The store owns and manages these files. + pub object_path: PathBuf, } /// Metadata for a stored artifact. @@ -87,7 +87,7 @@ pub struct StoredArtifact { pub struct ArtifactManifest { /// The full artifact key. pub artifact_key: ArtifactKey, - /// The symbol name to look up in the loaded library. + /// The symbol name of the compiled function in the object. pub symbol_name: String, /// Length of the original bytecode. pub bytecode_len: usize, @@ -95,7 +95,7 @@ pub struct ArtifactManifest { pub artifact_len: usize, /// Creation timestamp (unix seconds). pub created_at_unix_secs: u64, - /// Keccak-256 digest of the dylib bytes. + /// Keccak-256 digest of the object bytes. pub content_hash: [u8; 32], } @@ -111,8 +111,8 @@ pub enum BackendSelection { /// Trait for loading and storing compiled artifacts. /// -/// Implementations manage artifact files on the filesystem. The store owns the dylib files and -/// returns paths to them. The backend loads shared libraries directly from these paths. +/// Implementations manage artifact files on the filesystem. The store owns the object files and +/// returns paths to them. The backend links the objects in process from these paths. pub trait ArtifactStore: Send + Sync + 'static { /// Loads all available artifacts from storage. fn load_all(&self) -> eyre::Result>; @@ -120,13 +120,13 @@ pub trait ArtifactStore: Send + Sync + 'static { /// Loads a single artifact by key. fn load(&self, key: &ArtifactKey) -> eyre::Result>; - /// Stores an artifact. The `dylib_bytes` are the raw shared-library bytes to persist. + /// Stores an artifact. The `object_bytes` are the raw relocatable object bytes to persist. /// The store writes them to disk and the returned path (via subsequent `load`) points there. fn store( &self, key: &ArtifactKey, manifest: &ArtifactManifest, - dylib_bytes: &[u8], + object_bytes: &[u8], ) -> eyre::Result<()>; /// Deletes an artifact by key. @@ -136,7 +136,7 @@ pub trait ArtifactStore: Send + Sync + 'static { fn clear(&self) -> eyre::Result<()>; } -/// In-memory artifact index backed by dylib files in a temporary directory. +/// In-memory artifact index backed by object files in a temporary directory. #[derive(Debug)] pub struct RuntimeArtifactStore { dir: tempfile::TempDir, @@ -161,7 +161,7 @@ impl RuntimeArtifactStore { fn artifact_path(&self, key: &ArtifactKey) -> PathBuf { self.dir.path().join(format!( - "{:x}_{:?}_{:?}_{:?}_v{}_{:x}.so", + "{:x}_{:?}_{:?}_{:?}_v{}_{:x}.o", key.runtime.code_hash, key.runtime.spec_id, key.backend, @@ -185,28 +185,29 @@ impl ArtifactStore for RuntimeArtifactStore { &self, key: &ArtifactKey, manifest: &ArtifactManifest, - dylib_bytes: &[u8], + object_bytes: &[u8], ) -> eyre::Result<()> { let path = self.artifact_path(key); - // Write to a unique file and rename it over the target: a previous artifact for the key - // may be mapped, and rewriting it in place would corrupt the mapping. + // Write to a unique file and rename it over the target, so a reader never sees a partial + // artifact. let mut file = tempfile::NamedTempFile::new_in(self.dir.path())?; - file.write_all(dylib_bytes)?; + file.write_all(object_bytes)?; file.persist(&path).map_err(|err| err.error)?; self.artifacts - .insert(*key, StoredArtifact { manifest: manifest.clone(), dylib_path: path }); + .insert(*key, StoredArtifact { manifest: manifest.clone(), object_path: path }); Ok(()) } fn delete(&self, key: &ArtifactKey) -> eyre::Result<()> { if let Some((_, artifact)) = self.artifacts.remove(key) { - let _ = fs::remove_file(artifact.dylib_path); + let _ = fs::remove_file(artifact.object_path); } Ok(()) } fn clear(&self) -> eyre::Result<()> { - let paths = self.artifacts.iter().map(|entry| entry.dylib_path.clone()).collect::>(); + let paths = + self.artifacts.iter().map(|entry| entry.object_path.clone()).collect::>(); self.artifacts.clear(); for path in paths { let _ = fs::remove_file(path); @@ -262,13 +263,13 @@ mod tests { let key = artifact_key(B256::with_last_byte(1)); let manifest = manifest(key, 4); - store.store(&key, &manifest, b"dylib").unwrap(); + store.store(&key, &manifest, b"object").unwrap(); assert_eq!(store.len(), 1); let loaded = store.load(&key).unwrap().unwrap(); assert_eq!(loaded.manifest.symbol_name, "main"); assert_eq!(loaded.manifest.artifact_len, 4); - assert_eq!(std::fs::read(&loaded.dylib_path).unwrap(), b"dylib"); + assert_eq!(std::fs::read(&loaded.object_path).unwrap(), b"object"); let all = store.load_all().unwrap(); assert_eq!(all.len(), 1); @@ -287,7 +288,7 @@ mod tests { assert_eq!(store.len(), 1); let loaded = store.load(&key).unwrap().unwrap(); assert_eq!(loaded.manifest.artifact_len, 5); - assert_eq!(std::fs::read(&loaded.dylib_path).unwrap(), b"three"); + assert_eq!(std::fs::read(&loaded.object_path).unwrap(), b"three"); } #[test] @@ -296,7 +297,7 @@ mod tests { let key = artifact_key(B256::with_last_byte(5)); store.store(&key, &manifest(key, 3), b"one").unwrap(); - let mut open = std::fs::File::open(store.load(&key).unwrap().unwrap().dylib_path).unwrap(); + let mut open = std::fs::File::open(store.load(&key).unwrap().unwrap().object_path).unwrap(); store.store(&key, &manifest(key, 5), b"three").unwrap(); let mut contents = Vec::new(); @@ -312,8 +313,8 @@ mod tests { store.store(&first, &manifest(first, 1), b"a").unwrap(); store.store(&second, &manifest(second, 1), b"b").unwrap(); - let first_path = store.load(&first).unwrap().unwrap().dylib_path; - let second_path = store.load(&second).unwrap().unwrap().dylib_path; + let first_path = store.load(&first).unwrap().unwrap().object_path; + let second_path = store.load(&second).unwrap().unwrap().object_path; store.delete(&first).unwrap(); assert!(store.load(&first).unwrap().is_none()); diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index c8f1f314b..d9e1ec47d 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -164,7 +164,7 @@ impl ArtifactStore for EmptyStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -194,7 +194,7 @@ impl ArtifactStore for FailingStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -226,7 +226,7 @@ impl ArtifactStore for CountingStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -1132,6 +1132,31 @@ fn memory_budget_eviction() { }); } +#[test] +#[cfg(feature = "llvm")] +fn memory_budget_evicts_aot_entries() { + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + store: Some(Arc::new(RuntimeArtifactStore::new().unwrap())), + tuning: RuntimeTuning { + jit_worker_count: 1, + resident_code_cache_bytes: 1, + eviction_sweep_interval: std::time::Duration::from_millis(50), + ..Default::default() + }, + ..Default::default() + }); + + tb.prepare_aot_sync(AotRequest { + code_hash: alloy_primitives::keccak256(BYTECODE_RET42), + code: Bytes::copy_from_slice(BYTECODE_RET42), + spec_id: SpecId::CANCUN, + }) + .unwrap(); + + poll_until(std::time::Duration::from_secs(10), || (tb.stats().evictions >= 1).then_some(())); +} + // =========================================================================== // Tests: concurrency. // =========================================================================== @@ -1519,7 +1544,7 @@ fn preload_aot_skips_stale_abi_version() { let stale_manifest = ArtifactManifest { artifact_key: stale_key, ..stored.manifest.clone() }; let stale_store = Arc::new(RuntimeArtifactStore::new().unwrap()); stale_store - .store(&stale_key, &stale_manifest, &std::fs::read(&stored.dylib_path).unwrap()) + .store(&stale_key, &stale_manifest, &std::fs::read(&stored.object_path).unwrap()) .unwrap(); let tb = TestBackend::new(RuntimeConfig { @@ -1557,7 +1582,7 @@ fn preload_aot_skips_manifest_key_mismatch() { let stale_key = ArtifactKey { abi_version: crate::ABI_VERSION - 1, ..key }; let manifest = ArtifactManifest { artifact_key: stale_key, ..stored.manifest.clone() }; let mismatched_store = Arc::new(RuntimeArtifactStore::new().unwrap()); - mismatched_store.store(&key, &manifest, &std::fs::read(&stored.dylib_path).unwrap()).unwrap(); + mismatched_store.store(&key, &manifest, &std::fs::read(&stored.object_path).unwrap()).unwrap(); let tb = TestBackend::new(RuntimeConfig { enabled: true, diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index fecc7e157..2fd2dbc8d 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -21,7 +21,7 @@ use alloy_primitives::Bytes; use crossbeam_channel as chan; use rayon::{ThreadPool, ThreadPoolBuilder}; #[cfg(feature = "llvm")] -use std::{cell::RefCell, fs::File, io::Read, time::Instant}; +use std::{cell::RefCell, time::Instant}; use std::{ sync::{ Arc, @@ -33,7 +33,7 @@ use std::{ #[cfg(feature = "llvm")] use crate::{ - EvmCompiler, EvmLlvmBackend, Linker, + EvmCompiler, EvmLlvmBackend, llvm::{JitDylibGuard, orc::ResourceTracker}, runtime::config::JitMode, }; @@ -120,7 +120,7 @@ pub(crate) struct WorkerResult { pub(crate) enum WorkerSuccess { /// JIT compilation produced an in-memory function pointer. Jit(JitSuccess), - /// AOT compilation produced shared-library bytes. + /// AOT compilation produced relocatable object bytes to persist and link in process. Aot(AotSuccess), /// JIT compilation produced relocatable object bytes to link in the parent. JitObject(JitObjectSuccess), @@ -147,10 +147,10 @@ pub(crate) struct JitObjectSuccess { } pub(crate) struct AotSuccess { - /// The symbol name in the shared library. + /// The symbol name in the object file. pub(crate) symbol_name: String, - /// The raw shared-library bytes (.so / .dylib). - pub(crate) dylib_bytes: Vec, + /// The raw relocatable object bytes. + pub(crate) object_bytes: Vec, /// Length of the original bytecode. pub(crate) bytecode_len: usize, } @@ -688,20 +688,28 @@ fn compile_jit_artifact( } } -/// Compiles a single bytecode to a shared library and returns the raw bytes. +/// Compiles a single bytecode to a relocatable object and returns the raw bytes. #[cfg(feature = "llvm")] -pub(super) fn compile_jit_object_artifact( +fn compile_object( job: &CompileJob, compiler: &mut EvmCompiler, -) -> Result { +) -> Result, String> { compiler .translate(&job.symbol_name, &job.bytecode[..], job.key.spec_id) - .map_err(|e| format!("JIT object translate failed: {e}"))?; + .map_err(|e| format!("object translate failed: {e}"))?; let mut object_bytes = Vec::new(); - compiler - .write_object(&mut object_bytes) - .map_err(|e| format!("JIT object write failed: {e}"))?; + compiler.write_object(&mut object_bytes).map_err(|e| format!("object write failed: {e}"))?; + Ok(object_bytes) +} + +/// Compiles a single bytecode to a relocatable object that the parent links as JIT code. +#[cfg(feature = "llvm")] +pub(super) fn compile_jit_object_artifact( + job: &CompileJob, + compiler: &mut EvmCompiler, +) -> Result { + let object_bytes = compile_object(job, compiler)?; let mut builtin_symbols: Vec = compiler .backend() .pending_symbol_names() @@ -709,14 +717,7 @@ pub(super) fn compile_jit_object_artifact( .map(|name| name.to_string_lossy().into_owned()) .collect(); if builtin_symbols.is_empty() { - let suffix = compiler.builtin_symbol_suffix(); - builtin_symbols = evm2_jit_builtins::Builtin::ALL - .iter() - .map(|builtin| match suffix { - Some(suffix) => format!("{}_{suffix:x}", builtin.name()), - None => builtin.name().to_string(), - }) - .collect(); + builtin_symbols = builtin_symbol_names(compiler.builtin_symbol_suffix()); } debug!( @@ -732,43 +733,33 @@ pub(super) fn compile_jit_object_artifact( })) } +/// Returns the names of all builtin symbols that an object compiled with `suffix` may reference. +pub(super) fn builtin_symbol_names(suffix: Option) -> Vec { + evm2_jit_builtins::Builtin::ALL + .iter() + .map(|builtin| match suffix { + Some(suffix) => format!("{}_{suffix:x}", builtin.name()), + None => builtin.name().to_string(), + }) + .collect() +} + #[cfg(feature = "llvm")] fn compile_aot_artifact( job: &CompileJob, compiler: &mut EvmCompiler, ) -> Result { - compiler - .translate(&job.symbol_name, &job.bytecode[..], job.key.spec_id) - .map_err(|e| format!("AOT translate failed: {e}"))?; - - let tmp_dir = tempfile::tempdir().map_err(|e| format!("failed to create temp dir: {e}"))?; - - let obj_path = tmp_dir.path().join("a.o"); - let so_path = tmp_dir.path().join("a.so"); - - compiler - .write_object_to_file(&obj_path) - .map_err(|e| format!("AOT write object failed: {e}"))?; - - let linker = Linker::new(); - linker - .link(&so_path, [obj_path.to_str().unwrap()]) - .map_err(|e| format!("AOT link failed: {e}"))?; - - let mut dylib_bytes = Vec::new(); - File::open(&so_path) - .and_then(|mut f| f.read_to_end(&mut dylib_bytes)) - .map_err(|e| format!("failed to read linked .so: {e}"))?; + let object_bytes = compile_object(job, compiler)?; debug!( bytecode_len = job.bytecode.len(), - dylib_len = dylib_bytes.len(), + object_len = object_bytes.len(), "AOT compilation succeeded", ); Ok(WorkerSuccess::Aot(AotSuccess { symbol_name: job.symbol_name.clone(), - dylib_bytes, + object_bytes, bytecode_len: job.bytecode.len(), })) } From abfd112245e473333823514fd83cea8030f0b684 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:20:13 +0100 Subject: [PATCH 029/159] fix(inspectors): keep read slots in a selfdestructed account's prestate diff (#881) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In diff mode, geth does not diff an account that selfdestructed in the transaction (`processDiffState` skips `deleted` accounts in `prestate.go`), so its `pre` entry stays as the tracer first looked it up: balance, nonce, code and every storage slot an SLOAD or SSTORE touched, including slots that did not change and slots that held zero. We kept only the changed non-zero slots of such an account. `pre` now lists every slot the transaction read for a selfdestructed account, with its value before the transaction, and the zero-slot filter no longer applies to it. Other accounts keep the old filtering. The case is a London contract that reads slot 1 (`0x2a`) and slot 2 (zero) and then selfdestructs. London selfdestructs every contract; from Cancun only a contract created in the same transaction selfdestructs, and that contract is empty before the transaction, so `pre` already drops it. geth is `evm t8n --state.fork London --trace.tracer prestateTracer --trace.jsonconfig '{"diffMode":true}'` at go-ethereum `a0cba163`; the table shows the contract's `pre.storage`. | | geth | `main` | this PR | | :-- | :-- | :-- | :-- | | slot 1 | `0x2a` | absent 🔴 | `0x2a` 🟢 | | slot 2 | `0x0` | absent 🔴 | `0x0` 🟢 | | `post` entry | none | none | none | The parity harness has no London cases yet, so the geth output above comes from running `evm t8n` by hand on the same alloc and transaction as the regression test. --- crates/inspectors/src/tracing/builder/geth.rs | 20 ++++-- crates/inspectors/tests/it/geth.rs | 72 +++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 355bceade..432ac173f 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -490,9 +490,21 @@ impl<'a> GethTraceBuilder<'a> { // handle storage changes if storage_enabled { - for (key, slot) in changed_acc.changed_storage() { - pre_state.storage.insert((*key).into(), slot.original.into()); - post_state.storage.insert((*key).into(), slot.current.into()); + // Geth keeps the pre state of a selfdestructed account as it looked it up, with + // every slot the transaction touched, unchanged and zero ones included. + if changed_acc.selfdestructed { + for key in changed_acc.storage.keys() { + pre_state.storage.insert((*key).into(), db.get_storage(&addr, key)?.into()); + } + } else { + for (key, slot) in changed_acc.changed_storage() { + if !slot.original.is_zero() { + pre_state.storage.insert((*key).into(), slot.original.into()); + } + if !slot.current.is_zero() { + post_state.storage.insert((*key).into(), slot.current.into()); + } + } } } @@ -513,7 +525,7 @@ impl<'a> GethTraceBuilder<'a> { } // ensure we're only keeping changed entries - state_diff.retain_changed().remove_zero_storage_values(); + state_diff.retain_changed(); self.diff_traces(&mut state_diff.pre, &mut state_diff.post, &empty_before); Ok(PreStateFrame::Diff(state_diff)) diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 6d2f39dee..5cb727f08 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -2652,3 +2652,75 @@ fn test_geth_prestate_diff_reports_cleared_code() { serde_json::json!({ "code": "0x", "nonce": 1 }) ); } + +/// Geth keeps the pre state of a selfdestructed account whole, with every slot the transaction +/// read, including unchanged and zero ones. +#[test] +fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let contract = address!("1111111111111111111111111111111111111111"); + let beneficiary = address!("3333333333333333333333333333333333333333"); + + // SLOAD 1, SLOAD 2, SELFDESTRUCT to the beneficiary. + let code = Bytes::from(hex!("6001545060025450733333333333333333333333333333333333333333ff")); + let mut db = CacheDB::new(EmptyDB::default()); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + db.insert_account_info( + &contract, + AccountInfo { + balance: U256::from(7), + nonce: 1, + code_hash: alloy_primitives::keccak256(&code), + code: Some(Bytecode::new_raw(code)), + ..Default::default() + }, + ); + db.insert_account_storage(&contract, &U256::from(1), &U256::from(0x2a)); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) + .build_mainnet(); + + let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; + + // Output of geth's `evm t8n --state.fork London` with the prestate tracer in diff mode. + assert!(!diff.post.contains_key(&contract)); + assert_eq!(diff.post[&beneficiary].balance, Some(U256::from(7))); + assert_eq!( + serde_json::to_value(&diff.pre[&contract]).unwrap(), + serde_json::json!({ + "balance": "0x7", + "code": "0x6001545060025450733333333333333333333333333333333333333333ff", + "nonce": 1, + "storage": { + "0x0000000000000000000000000000000000000000000000000000000000000001": + "0x000000000000000000000000000000000000000000000000000000000000002a", + "0x0000000000000000000000000000000000000000000000000000000000000002": + "0x0000000000000000000000000000000000000000000000000000000000000000" + } + }) + ); +} From 5d311311ab3a1c5a0c8b5dd5fd994300bbfe47dc Mon Sep 17 00:00:00 2001 From: RealiCZ Date: Fri, 30 Oct 2026 09:49:18 -0400 Subject: [PATCH 030/159] feat(interpreter): add CALL and CREATE extension helpers (#887) ## Summary Adds `call_with` and `create_with`, the `sstore_with` (#771) shape for the CALL family and CREATE/CREATE2, as proposed in #843. Custom call or create instructions can now reuse the base bodies instead of copying `load_acc_and_calc_gas` / `prepare_call` / `create_inner`. Closes #843. ## Changes - `call_with(stack, gas, state, kind, before, after)`: the body of `call` / `callcode` / `delegatecall` / `staticcall`. - `before` runs after every charge of the opcode (the EIP-8037 new-account state gas included) and after the forward is taken, right before `execute_message`. It may charge or credit `gas`, edit the child message (`gas_limit`, `reservoir`, `ext`) or fail the instruction. - `after` runs after `merge_child_gas` and the refund of the upfront state gas for a failed child, before the return data is copied and the success word pushed. A fatal child result returns before it. - `create_with(stack, gas, state, is_create2, before, after)`: the same for CREATE/CREATE2. `before` does not run when EIP-8037's balance/nonce/depth checks refuse the creation before the forward. - `call_inner` / `create_inner` call them with no-op closures; the checks, comments and charges are unchanged. - Gas charged by the hooks is not part of the step cost reported to inspectors (it happens after `finish_step_charge`); documented on both helpers. - Tests (in `system.rs`, a test opcode table that installs hooked CALL/CREATE next to the base ones, on Amsterdam): - the hooks can keep part of the forward in the caller and charge after the child (child `gas_limit` and caller gas compared with the base opcode); - `before` sees the new-account state gas already charged for a value call to an empty account; - `after` can fail the instruction; - CREATE with both hooks; - CREATE refused before the forward runs neither hook. ## A related point, raised here rather than added The issue proposed two callbacks, and this PR implements exactly those. We have one more need that the issue did not mention: a point **right before the opcode's own state-gas charge** (`spend_state` for the CALL new-account charge and the CREATE account-creation charge). The case: a chain that keeps part of a frame's gas withheld (not spendable by the frame's own execution, but allowed to pay certain charges) wants the opcode's state-gas spill to draw from the withheld part first. When the reservoir cannot cover the state charge and the spill is larger than `remaining`, the base body halts out of gas inside `spend_state`, before `before` runs, so no later hook can change the outcome. A closure `before_state_charge(&mut Gas, &mut InterpreterState, Address, price) -> Result` called just before `spend_state` would cover it. We have it implemented and tested locally (one more closure on both helpers). Happy to add it to this PR, open it separately, or drop it if you would rather not carry it. ## Validation - `cargo test -p evm2 --lib`: 739 passed (default backend and `EVM2_DISPATCH_BACKEND=packed`). - Without the two hook calls, 4 of the 5 new tests fail (the fifth checks that the hooks do not run). - `cargo +nightly fmt --all --check`, `cargo clippy -p evm2 --all-targets --all-features` (nightly) and `cargo clippy -p evm2 --all-targets` (1.97): clean. `cargo doc -p evm2 --no-deps` with `-D warnings`: clean. `cargo build -p evm2 --examples`: ok. - Not measured locally: codegen / instruction counts of the base CALL and CREATE paths (the helpers are `#[inline]` and called from the `#[inline(never)]` base bodies with no-op closures). Workspace-wide `cargo cl` / `cargo docs` need LLVM for the JIT crate and were not run locally. --- .../src/interpreter/instructions/system.rs | 280 +++++++++++++++++- 1 file changed, 275 insertions(+), 5 deletions(-) diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 848a5389c..c4e99e195 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -4,8 +4,8 @@ use crate::{ EvmFeatures, EvmTypesHost, bytecode::Bytecode, interpreter::{ - Gas, Host, InstrStop, InterpreterState, Message, MessageExt, MessageKind, Result, StackMut, - Word, derive_create_destination, + Gas, Host, InstrStop, InterpreterState, Message, MessageExt, MessageKind, MessageResult, + Result, StackMut, Word, derive_create_destination, }, utils::WordExt, version::GasId, @@ -228,15 +228,52 @@ fn prepare_call( #[inline(never)] fn call_inner( + stack: StackMut<'_>, + gas: &mut Gas, + state: &mut InterpreterState<'_, '_, T>, + kind: MessageKind, +) -> Result { + call_with(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) +} + +/// Executes a call opcode of `kind` with hooks around its child message. +/// +/// This is the body of [`call`], [`callcode`], [`delegatecall`] and [`staticcall`], for custom +/// call instructions that need extra behavior. Use it from an +/// `#[instruction(no_stack_preamble, dynamic_gas)]` function with `stack`, `cx.gas` and +/// `cx.state`. +/// +/// `before` runs after every charge the opcode makes, the EIP-8037 new-account state gas +/// included, and after the forwarded gas is taken from `gas`, right before the child message +/// executes. It may charge or credit `gas`, edit the child message (for example its `gas_limit`, +/// `reservoir` or `ext`), or fail the instruction. +/// +/// `after` runs once the child's gas is merged into `gas` and the upfront new-account state gas of +/// a failed child is refunded, before the return data is copied and the success word is pushed. +/// It may charge or credit `gas` or fail the instruction. A fatal child result returns before +/// `after` runs. +/// +/// An `Err` from either hook stops the instruction with that error. Gas the hooks charge is not +/// part of the step cost reported to inspectors. +#[inline] +pub fn call_with( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, kind: MessageKind, + before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, + after: impl FnOnce( + &mut Gas, + &mut InterpreterState<'_, '_, T>, + &Message, + &MessageResult, + ) -> Result, ) -> Result { let mut message = Message::::default(); let mut return_memory_range = 0..0; let new_account_state_gas = prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range)?; + before(gas, state, &mut message)?; let mut result = state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; @@ -251,6 +288,7 @@ fn call_inner( if new_account_state_gas != 0 && !result.stop.is_success() { gas.refill_reservoir(new_account_state_gas); } + after(gas, state, &message, &result)?; let copy_len = min(return_memory_range.len(), result.output.len()); unsafe { let output = result.output.get_unchecked(..copy_len); @@ -287,10 +325,42 @@ pub fn create(cx: _) -> Result { #[inline(never)] fn create_inner( + stack: StackMut<'_>, + gas: &mut Gas, + state: &mut InterpreterState<'_, '_, T>, + is_create2: bool, +) -> Result { + create_with(stack, gas, state, is_create2, |_, _, _| Ok(()), |_, _, _, _| Ok(())) +} + +/// Executes `CREATE` or `CREATE2` with hooks around its child message. +/// +/// This is the body of [`create`], for custom creation instructions that need extra behavior, in +/// the same shape as [`call_with`]. +/// +/// `before` runs after every charge the opcode makes, the EIP-8037 account-creation state gas +/// included, and after the forwarded gas is taken from `gas`, right before the child message +/// executes. It does not run when the opcode refuses the creation before forwarding gas (the +/// EIP-8037 balance, nonce and depth checks). +/// +/// `after` runs once the child's gas is merged into `gas` and the account-creation state gas of a +/// failed creation is refunded, before the return data is set and the created address is pushed. +/// A fatal child result returns before `after` runs. +/// +/// Both hooks may charge or credit `gas` or fail the instruction, as in [`call_with`]. +#[inline] +pub fn create_with( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, is_create2: bool, + before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, + after: impl FnOnce( + &mut Gas, + &mut InterpreterState<'_, '_, T>, + &Message, + &MessageResult, + ) -> Result, ) -> Result { require_non_staticcall(state)?; @@ -394,6 +464,7 @@ fn create_inner( ext: T::MessageExt::default(), _non_exhaustive: (), }; + before(gas, state, &mut message)?; let mut result = state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; if result.stop.is_fatal() { @@ -410,6 +481,7 @@ fn create_inner( if charged_create_state_gas && create_failed { gas.refill_reservoir(state.gas_params().create_state_gas()); } + after(gas, state, &message, &result)?; // EIP-211 exposes CREATE failure data only for REVERT; other failures clear returndata. if result.stop == InstrStop::Revert { state.swap_return_data(&mut result.output); @@ -453,11 +525,14 @@ pub fn selfdestruct(cx: _, [target]: [Word]) -> Result { mod tests { use super::*; use crate::{ - ExecutionError, SpecId, + BaseEvmConfig, EvmConfig, ExecutionConfig, ExecutionError, SpecId, constants::{CALL_DEPTH_LIMIT, MAX_INITCODE_SIZE}, - interpreter::{MessageResultExt, op}, - test_utils::{RunConfig, TestHost, push, push_all, run}, + interpreter::{Interpreter, MessageResultExt, op}, + test_utils::{ + RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push, push_all, run, + }, utils::address_to_word, + version::OpcodeConfig, }; use alloc::vec::Vec; use core::assert_matches; @@ -1005,4 +1080,199 @@ mod tests { assert_matches!(interp.err, InstrStop::SelfDestruct); assert_eq!(host.selfdestructs, [(contract, target, false)]); } + + const HOOKED_CALL: u8 = 0x0c; + const HOOKED_CREATE: u8 = 0x0d; + const KEPT_GAS: u64 = 1_000; + const AFTER_FEE: u64 = 7; + + // Keeps `KEPT_GAS` of the forwarded gas in the caller and charges `AFTER_FEE` after a + // successful child; a failed child fails the instruction. + #[instruction(no_stack_preamble, dynamic_gas)] + fn hooked_call(cx: _) -> Result { + call_with( + stack, + cx.gas, + cx.state, + MessageKind::Call, + |gas, _, message| { + message.gas_limit -= KEPT_GAS; + gas.erase_cost(KEPT_GAS); + Ok(()) + }, + |gas, _, _, result| { + if result.stop.is_success() { gas.spend(AFTER_FEE) } else { Err(InstrStop::Revert) } + }, + ) + } + + #[instruction(no_stack_preamble, dynamic_gas)] + fn hooked_create(cx: _) -> Result { + create_with( + stack, + cx.gas, + cx.state, + false, + |gas, _, message| { + message.gas_limit -= KEPT_GAS; + gas.erase_cost(KEPT_GAS); + Ok(()) + }, + |gas, _, _, _| gas.spend(AFTER_FEE), + ) + } + + struct HookedConfig; + + impl EvmConfig for HookedConfig { + const BASE_SPEC_ID: SpecId = SpecId::AMSTERDAM; + const OPCODE_CONFIG: &'static OpcodeConfig = &hooked_opcode_config(); + } + + const fn hooked_opcode_config() -> OpcodeConfig { + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(HOOKED_CALL, config.static_gas(op::CALL)); + config.set_instruction::>( + HOOKED_CREATE, + config.static_gas(op::CREATE), + ); + config + } + + fn run_hooked(config: RunConfig<'_>) -> TestInterpreter { + let RunConfig { code, host, tx_env, message, gas_limit, return_data, .. } = config; + let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; + let mut inner = Interpreter::::new(&message); + *inner.return_data_mut() = return_data; + let mut default_host = TestHost::default(); + let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; + host.spec_id = SpecId::AMSTERDAM; + host.execution_config = ExecutionConfig::::for_config::(); + let err = inner.run(host).unwrap(); + let (stack, stack_len, gas, memory, output) = inner.into_parts(); + TestInterpreter { stack, stack_len, gas, memory, output, err, execution_error: None } + } + + fn call_code(opcode: u8, value: u64) -> Vec { + let mut code = Vec::new(); + push_all( + &mut code, + [ + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::from(value), + address_to_word(&Address::from([0x22; 20])), + Word::from(5_000), + ], + ); + code.extend([opcode, op::STOP]); + code + } + + #[test] + fn call_with_hooks_adjust_child_and_caller_gas() { + let mut base_host = TestHost::default(); + let base = run_hooked( + RunConfig::new(call_code(op::CALL, 0)).host(&mut base_host).gas_limit(30_000), + ); + let mut host = TestHost::default(); + let hooked = + run_hooked(RunConfig::new(call_code(HOOKED_CALL, 0)).host(&mut host).gas_limit(30_000)); + + assert_matches!(base.err, InstrStop::Stop); + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [Word::from(1)]); + assert_eq!(host.calls.len(), 1); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn call_with_before_runs_after_new_account_state_gas() { + // A value call to an empty account charges EIP-8037 new-account state gas before the + // forward; `before` sees the child message built after that charge. + let mut base_host = TestHost { is_empty: true, ..Default::default() }; + let base = run_hooked( + RunConfig::new(call_code(op::CALL, 1)).host(&mut base_host).gas_limit(200_000), + ); + let mut host = TestHost { is_empty: true, ..Default::default() }; + let hooked = run_hooked( + RunConfig::new(call_code(HOOKED_CALL, 1)).host(&mut host).gas_limit(200_000), + ); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas.state_gas_spent(), base.gas.state_gas_spent()); + assert_ne!(hooked.gas.state_gas_spent(), 0); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn call_with_after_can_fail_the_instruction() { + let mut host = TestHost { + execute_result: MessageResultExt { stop: InstrStop::Revert, ..Default::default() }, + ..Default::default() + }; + let interp = + run_hooked(RunConfig::new(call_code(HOOKED_CALL, 0)).host(&mut host).gas_limit(30_000)); + + assert_matches!(interp.err, InstrStop::Revert); + assert!(interp.stack().is_empty()); + assert_eq!(host.calls.len(), 1); + } + + #[test] + fn create_with_hooks_adjust_child_and_caller_gas() { + let created = Address::from([0x77; 20]); + let execute_result = MessageResultExt { + stop: InstrStop::Return, + created_address: Some(created), + ..MessageResultExt::default() + }; + let mut code = Vec::new(); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut base_code = code.clone(); + base_code.extend([op::CREATE, op::STOP]); + code.extend([HOOKED_CREATE, op::STOP]); + + let mut base_host = TestHost { + execute_result: execute_result.clone(), + is_empty: true, + ..Default::default() + }; + let base = run_hooked(RunConfig::new(base_code).host(&mut base_host).gas_limit(500_000)); + let mut host = TestHost { execute_result, is_empty: true, ..Default::default() }; + let hooked = run_hooked(RunConfig::new(code).host(&mut host).gas_limit(500_000)); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [address_to_word(&created)]); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas.state_gas_spent(), base.gas.state_gas_spent()); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn create_with_skips_hooks_when_creation_is_refused_before_forward() { + let mut code = Vec::new(); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut base_code = code.clone(); + base_code.extend([op::CREATE, op::STOP]); + code.extend([HOOKED_CREATE, op::STOP]); + let message = MessageExt { depth: CALL_DEPTH_LIMIT, ..Default::default() }; + + let base = + run_hooked(RunConfig::new(base_code).message(message.clone()).gas_limit(500_000)); + let mut host = TestHost::default(); + let hooked = + run_hooked(RunConfig::new(code).host(&mut host).message(message).gas_limit(500_000)); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [Word::ZERO]); + assert!(host.calls.is_empty()); + assert_eq!(hooked.gas_remaining(), base.gas_remaining()); + } } From 86fa2648a3f297e405bc1e078efabaae5f80b919 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 14:52:47 +0100 Subject: [PATCH 031/159] refactor(jit): remove inkwell, use llvm-sys directly (#886) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes `inkwell` from the dependency graph. `evm2-jit-llvm` now calls `llvm-sys` directly through a small owned wrapper module (`wrap`) that covers only what the backend uses: contexts, modules, types, values, the instruction builder, targets and target machines, and debug info. Handles are `Copy` pointers without lifetimes, since the backend already owned the context and used `'static` everywhere. The git `[patch]` for inkwell is gone, and `cargo tree -i inkwell` finds nothing. The bench workflow and `bench.py` now also read the LLVM version from the `llvm-sys` requirement, so they still find the right `llvm-config` for both old and new revisions. The generated code does not change. On a clean 12-core box against `origin/main`, `bench.py --codegen-lines --compile-times --runtime --runs 5` reports `opt.s` changed in 0/31 benchmarks, and unopt.ll, opt.ll, opt.s and JIT size equal in every row. The `evm2-jit-llvm`, `evm2-jit-codegen` and `evm2-jit-runtime` suites pass (2836 tests), as do the compiled EEST state and blockchain suites in JIT and AOT mode with `EVM2_COMPILED_EEST_SUBSET=all`. ## Benchmarks Two full runs of `bench.py`, head `71dbfc59` against `main` `abfd1122`. Compile cost is `perf stat` instructions per compile; runtime is instructions and cycles per execution. Cycle counts vary by several percent between runs (`onchain_lm_v2` read +8.9% in run 2 and unchanged in run 1), so judge by instructions. | | run 1 | run 2 | | --- | --- | --- | | compile instructions, total | -0.2% 🟢 | -0.2% 🟢 | | compile instructions, geomean | -0.3% 🟢 | -0.3% 🟢 | | runtime instructions/iter, geomean | -0.2% 🟢 | -0.1% 🟢 | | runtime cycles/iter, geomean | -0.6% 🟢 | +0.7% 🔴 | CodSpeed flags the interpreter benchmark `evm/erc20_transfer` at -13%, with the warning that base and head ran on different runner types. The benchmark never calls into LLVM. Built the same way as CI (`--profile profiling --features jit`) and run on the box, it takes 1.752 and 1.745 ms on `main` and 1.715 and 1.734 ms on this branch (criterion medians). --- .github/workflows/bench.yml | 4 +- Cargo.lock | 24 +- Cargo.toml | 3 - crates/jit/llvm/Cargo.toml | 2 +- crates/jit/llvm/src/cpp.rs | 27 +- crates/jit/llvm/src/dh.rs | 10 +- crates/jit/llvm/src/lib.rs | 620 ++++++----------- crates/jit/llvm/src/orc.rs | 121 ++-- crates/jit/llvm/src/utils.rs | 7 - crates/jit/llvm/src/wrap/di.rs | 161 +++++ crates/jit/llvm/src/wrap/ir.rs | 1042 ++++++++++++++++++++++++++++ crates/jit/llvm/src/wrap/mod.rs | 35 + crates/jit/llvm/src/wrap/string.rs | 62 ++ crates/jit/llvm/src/wrap/target.rs | 238 +++++++ scripts/jit/utils.py | 6 +- 15 files changed, 1824 insertions(+), 538 deletions(-) delete mode 100644 crates/jit/llvm/src/utils.rs create mode 100644 crates/jit/llvm/src/wrap/di.rs create mode 100644 crates/jit/llvm/src/wrap/ir.rs create mode 100644 crates/jit/llvm/src/wrap/mod.rs create mode 100644 crates/jit/llvm/src/wrap/string.rs create mode 100644 crates/jit/llvm/src/wrap/target.rs diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 655056ec2..605737c21 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -78,7 +78,7 @@ jobs: [[ "$COMMIT" =~ ^[0-9a-f]{40}$ ]] || { echo "commit must be a full SHA" >&2; exit 1; } git merge-base --is-ancestor "$COMMIT" HEAD || { echo "$COMMIT is not in this ref's history" >&2; exit 1; } # bench.py builds against the LLVM version the old code requires. - llvm="$(git show "$COMMIT:crates/jit/llvm/Cargo.toml" | sed -nE 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p')" + llvm="$(git show "$COMMIT:crates/jit/llvm/Cargo.toml" | sed -nE -e 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p' -e 's/^llvm-sys = "([0-9]+)[0-9]"$/\1/p')" if ! command -v "llvm-config-$llvm" >/dev/null; then sudo .github/scripts/install_llvm_ubuntu.sh "$llvm" dev fi @@ -176,7 +176,7 @@ jobs: if [[ "$GITHUB_EVENT_NAME" == "pull_request" && "$BASELINE_FOUND" != "true" ]]; then args+=(--diff "$BASE_SHA" --base-output target/jit-bench/baseline) # bench.py builds each side against the LLVM version it requires. - base_llvm="$(git show "$BASE_SHA:crates/jit/llvm/Cargo.toml" | sed -nE 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p')" + base_llvm="$(git show "$BASE_SHA:crates/jit/llvm/Cargo.toml" | sed -nE -e 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p' -e 's/^llvm-sys = "([0-9]+)[0-9]"$/\1/p')" if ! command -v "llvm-config-$base_llvm" >/dev/null; then sudo .github/scripts/install_llvm_ubuntu.sh "$base_llvm" dev fi diff --git a/Cargo.lock b/Cargo.lock index 505533a5a..156c68bf6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2793,7 +2793,7 @@ dependencies = [ "alloy-primitives", "cc", "evm2-jit-backend", - "inkwell", + "llvm-sys", "object", "tracing", ] @@ -3586,28 +3586,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "inkwell" -version = "0.10.0" -source = "git+https://github.com/TheDan64/inkwell?rev=c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff#c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" -dependencies = [ - "bitflags 2.13.2", - "inkwell_internals", - "libc", - "llvm-sys", - "thiserror 2.0.21", -] - -[[package]] -name = "inkwell_internals" -version = "0.15.0" -source = "git+https://github.com/TheDan64/inkwell?rev=c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff#c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - [[package]] name = "intrusive-collections" version = "0.10.3" diff --git a/Cargo.toml b/Cargo.toml index c96f2197e..1a56a47f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -199,6 +199,3 @@ ark-ff-macros = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e ark-poly = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } ark-serialize = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } ark-serialize-derive = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } - -# LLVM 23 support is not released yet. -inkwell = { git = "https://github.com/TheDan64/inkwell", rev = "c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" } diff --git a/crates/jit/llvm/Cargo.toml b/crates/jit/llvm/Cargo.toml index 61742d0d2..2c8181745 100644 --- a/crates/jit/llvm/Cargo.toml +++ b/crates/jit/llvm/Cargo.toml @@ -18,7 +18,7 @@ workspace = true [dependencies] evm2-jit-backend.workspace = true -inkwell = { version = "0.10.0", features = ["llvm23-1"] } +llvm-sys = "231" alloy-primitives = { workspace = true, features = ["std", "map-fxhash"] } object = { version = "0.40", default-features = false, features = ["read_core", "elf"] } tracing.workspace = true diff --git a/crates/jit/llvm/src/cpp.rs b/crates/jit/llvm/src/cpp.rs index f4ce9b5c6..f75fd6fd1 100644 --- a/crates/jit/llvm/src/cpp.rs +++ b/crates/jit/llvm/src/cpp.rs @@ -1,18 +1,14 @@ //! Custom bindings to LLVM. -use inkwell::{ - attributes::Attribute, - context::Context, - llvm_sys::{ - error::LLVMErrorRef, - orc2::{ - LLVMOrcExecutionSessionRef, LLVMOrcExecutorAddress, LLVMOrcJITDylibRef, - lljit::{LLVMOrcLLJITBuilderRef, LLVMOrcLLJITRef}, - }, - prelude::{LLVMAttributeRef, LLVMContextRef, LLVMValueRef}, - target_machine::{LLVMCodeGenOptLevel, LLVMTargetMachineRef}, +use crate::wrap::{Attribute, Context, Function}; +use llvm_sys::{ + error::LLVMErrorRef, + orc2::{ + LLVMOrcExecutionSessionRef, LLVMOrcExecutorAddress, LLVMOrcJITDylibRef, + lljit::{LLVMOrcLLJITBuilderRef, LLVMOrcLLJITRef}, }, - values::AsValueRef, + prelude::{LLVMAttributeRef, LLVMContextRef, LLVMValueRef}, + target_machine::{LLVMCodeGenOptLevel, LLVMTargetMachineRef}, }; use std::{ffi::c_char, sync::atomic::AtomicUsize}; @@ -67,13 +63,10 @@ unsafe extern "C" { pub(crate) fn evm2_jit_llvm_expand_ir_insts_after_lsr(); } -pub(crate) fn set_dso_local(f: inkwell::values::FunctionValue<'_>) { +pub(crate) fn set_dso_local(f: Function) { unsafe { evm2_jit_llvm_set_dso_local(f.as_value_ref(), true) }; } pub(crate) fn create_initializes_attr(cx: &Context, lower: i64, upper: i64) -> Attribute { - unsafe { - let raw = evm2_jit_llvm_create_initializes_attr(cx.raw(), lower, upper); - Attribute::new(raw) - } + Attribute::new(unsafe { evm2_jit_llvm_create_initializes_attr(cx.raw(), lower, upper) }) } diff --git a/crates/jit/llvm/src/dh.rs b/crates/jit/llvm/src/dh.rs index 174eb90ed..6bfe584d8 100644 --- a/crates/jit/llvm/src/dh.rs +++ b/crates/jit/llvm/src/dh.rs @@ -1,7 +1,5 @@ -use inkwell::{ - context::{AsContextRef, Context}, - llvm_sys::{LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*, core::*, prelude::*}, -}; +use crate::wrap::{Context, LLVMString}; +use llvm_sys::{LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*, core::*, prelude::*}; use std::{ffi::c_void, fmt, ptr}; /// LLVM diagnostic handler guard. @@ -20,7 +18,7 @@ impl fmt::Debug for DiagnosticHandlerGuard { impl DiagnosticHandlerGuard { pub(crate) fn new(cx: &Context) -> Self { unsafe { - let c = cx.as_ctx_ref(); + let c = cx.raw(); let prev_dh = LLVMContextGetDiagnosticHandler(c); let prev_dhc = LLVMContextGetDiagnosticContext(c); LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), ptr::null_mut()); @@ -31,7 +29,7 @@ impl DiagnosticHandlerGuard { extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, _context: *mut c_void) { unsafe { // `LLVMGetDiagInfoDescription` returns an LLVM `Message`. - let msg_cstr = crate::llvm_string(LLVMGetDiagInfoDescription(di)); + let msg_cstr = LLVMString::from_raw(LLVMGetDiagInfoDescription(di)); let msg = msg_cstr.to_string_lossy(); match LLVMGetDiagInfoSeverity(di) { LLVMDSError => error!(target: "llvm", "{msg}"), diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 1245890ff..a7ae2fb7c 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -10,28 +10,12 @@ use evm2_jit_backend::{ Backend, BackendConfig, BackendTypes, Builder, CallConv, IntCC, OptimizationLevel, Result, TailCallKind, TypeMethods, U256, eyre, format_bytes, }; -use inkwell::{ - AddressSpace, IntPredicate, - attributes::{Attribute, AttributeLoc}, - basic_block::BasicBlock, - debug_info::{ - AsDIScope, DICompileUnit, DIFlags, DIFlagsConstants, DISubprogram, DWARFEmissionKind, - DWARFSourceLanguage, DebugInfoBuilder, - }, - module::{FlagBehavior, Module}, - passes::PassBuilderOptions, - support::error_handling::install_fatal_error_handler, - targets::{ - CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine, TargetTriple, - }, - types::{ - AnyType, AnyTypeEnum, BasicType, BasicTypeEnum, FunctionType, IntType, PointerType, - StringRadix, VoidType, - }, - values::{ - AsValueRef, BasicMetadataValueEnum, BasicValue, BasicValueEnum, CallSiteValue, - FunctionValue, InstructionValue, PointerValue, UnnamedAddress, - }, +use llvm_sys::{ + LLVMCallConv, LLVMIntPredicate, LLVMLinkage, LLVMModuleFlagBehavior, LLVMTailCallKind, + LLVMUnnamedAddr, + error_handling::LLVMInstallFatalErrorHandler, + support::LLVMParseCommandLineOptions, + target_machine::{LLVMCodeGenFileType, LLVMCodeGenOptLevel, LLVMCodeModel, LLVMRelocMode}, }; use object::{Object, ObjectSymbol}; use std::{ @@ -46,16 +30,16 @@ use std::{ atomic::{AtomicU64, AtomicUsize, Ordering}, }, }; - -pub use inkwell::{self, context::Context}; +use wrap::{ + Attribute, AttributeLoc, DICompileUnit, DebugInfoBuilder, Metadata, PassBuilderOptions, Target, +}; mod cpp; mod dh; pub mod orc; - -mod utils; -pub(crate) use utils::*; +mod wrap; +pub use wrap::{Block, Context, Function, Inst, LLVMString, Module, TargetMachine, Type, Value}; /// Branch weight for non-cold branches. const DEFAULT_WEIGHT: u32 = 20000; @@ -131,8 +115,11 @@ type FxHashMap = alloy_primitives::map::HashMap; pub struct EvmLlvmBackend { cx: &'static Context, _dh: dh::DiagnosticHandlerGuard, - bcx: inkwell::builder::Builder<'static>, - module: Module<'static>, + /// LLVM debug info builder and compile unit, created lazily when `debug_file` is set. + /// Dropped before `module`, which its builder refers to. + di_state: Option, + bcx: wrap::Builder, + module: Module, machine: TargetMachine, /// ORC JIT state. `None` in AOT mode. @@ -145,17 +132,14 @@ pub struct EvmLlvmBackend { /// Owned context for AOT mode. `None` in JIT mode. _aot_cx: Option>, - /// LLVM debug info builder and compile unit, created lazily when `debug_file` is set. - di_state: Option, - - ty_void: VoidType<'static>, - ty_ptr: PointerType<'static>, - ty_i1: IntType<'static>, - ty_i8: IntType<'static>, - ty_i32: IntType<'static>, - ty_i64: IntType<'static>, - ty_i256: IntType<'static>, - ty_isize: IntType<'static>, + ty_void: Type, + ty_ptr: Type, + ty_i1: Type, + ty_i8: Type, + ty_i32: Type, + ty_i64: Type, + ty_i256: Type, + ty_isize: Type, aot: bool, backend_config: BackendConfig, @@ -380,7 +364,7 @@ struct OrcJitState { /// must run while the tracker handles are still live. jd_guard: Arc, /// Functions in the current staging module (not yet committed to JIT). - staged_functions: FxHashMap>, + staged_functions: FxHashMap, /// Absolute symbols collected during translation, flushed to the global /// builtins JITDylib before commit. pending_symbols: Vec<(CString, usize)>, @@ -467,19 +451,6 @@ fn link_jit_object_in_dylib( Ok((addr, tracker)) } -/// Wraps a module in a [`orc::ThreadSafeModule`] for transfer to LLJIT. -/// -/// Uses a raw pointer cast to work around `Module<'ctx>` invariance — the module's -/// context is genuinely owned by `tscx`. -fn create_thread_safe_module( - tscx: &orc::ThreadSafeContext, - module: Module<'static>, -) -> orc::ThreadSafeModule { - let module = std::mem::ManuallyDrop::new(module); - // SAFETY: The module was created in the context owned by `tscx`. - unsafe { orc::ThreadSafeModule::create_in_context(Module::new(module.as_mut_ptr()), tscx) } -} - /// Creates an ORC-owned LLVM context, returning a `&'static` reference to it. /// /// In JIT mode, ORC owns the context via a [`orc::ThreadSafeContext`] so that modules can be @@ -500,8 +471,8 @@ fn create_orc_context() -> (&'static Context, orc::ThreadSafeContext, Box, - compile_unit: DICompileUnit<'static>, + dibuilder: DebugInfoBuilder, + compile_unit: DICompileUnit, finalized: bool, } @@ -531,8 +502,8 @@ impl EvmLlvmBackend { &target_info.cpu, &target_info.features, convert_opt_level(config.opt_level), - if aot { RelocMode::PIC } else { RelocMode::Static }, - CodeModel::Default, + if aot { LLVMRelocMode::LLVMRelocPIC } else { LLVMRelocMode::LLVMRelocStatic }, + LLVMCodeModel::LLVMCodeModelDefault, ) .ok_or_else(|| eyre::eyre!("failed to create target machine"))?; @@ -568,10 +539,9 @@ impl EvmLlvmBackend { let ty_i8 = cx.i8_type(); let ty_i32 = cx.i32_type(); let ty_i64 = cx.i64_type(); - let ty_i256 = - cx.custom_width_int_type(std::num::NonZeroU32::new(256).unwrap()).expect("i256"); - let ty_isize = cx.ptr_sized_int_type(&machine.get_target_data(), None); - let ty_ptr = cx.ptr_type(AddressSpace::default()); + let ty_i256 = cx.int_type(256); + let ty_isize = cx.ptr_sized_int_type(&machine.get_target_data()); + let ty_ptr = cx.ptr_type(); Ok(Self { cx, _dh: dh::DiagnosticHandlerGuard::new(cx), @@ -606,7 +576,7 @@ impl EvmLlvmBackend { } #[inline] - const fn module(&self) -> &Module<'static> { + const fn module(&self) -> &Module { &self.module } @@ -621,16 +591,8 @@ impl EvmLlvmBackend { Ok(self.orc.as_mut().unwrap()) } - fn fn_type( - &self, - ret: Option>, - params: &[BasicTypeEnum<'static>], - ) -> FunctionType<'static> { - let params = params.iter().copied().map(Into::into).collect::>(); - match ret { - Some(ret) => ret.fn_type(¶ms, false), - None => self.ty_void.fn_type(¶ms, false), - } + fn fn_type(&self, ret: Option, params: &[Type]) -> Type { + ret.unwrap_or(self.ty_void).fn_type(params, false) } /// Returns the given name if IR output is being dumped, otherwise an empty string. @@ -660,12 +622,12 @@ impl EvmLlvmBackend { // Add required module flags for debug info. self.module().add_basic_value_flag( "Debug Info Version", - FlagBehavior::Warning, - self.ty_i32.const_int(inkwell::debug_info::debug_metadata_version() as u64, false), + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, + self.ty_i32.const_int(wrap::debug_metadata_version() as u64, false), ); self.module().add_basic_value_flag( "Dwarf Version", - FlagBehavior::Warning, + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, self.ty_i32.const_int(5, false), ); @@ -681,22 +643,13 @@ impl EvmLlvmBackend { flags.push(if self.aot { "--aot" } else { "--jit" }); let flags = flags.join(" "); - let (dibuilder, compile_unit) = self.module().create_debug_info_builder( - true, - DWARFSourceLanguage::C, + let (dibuilder, compile_unit) = DebugInfoBuilder::new( + self.module(), &filename, &directory, "evm2_jit", is_optimized, &flags, - 0, - "", - DWARFEmissionKind::Full, - 0, - false, - false, - "", - "", ); self.di_state = Some(DiState { dibuilder, compile_unit, finalized: false }); @@ -725,7 +678,7 @@ impl EvmLlvmBackend { let tracker = orc.jd().create_resource_tracker(); - let tsm = create_thread_safe_module(tscx, old_module); + let tsm = orc::ThreadSafeModule::create_in_context(old_module, tscx); orc.global.jit.add_module_with_rt(tsm, &tracker).map_err(error_msg)?; let tracker_idx = orc.loaded_trackers.len(); @@ -820,21 +773,21 @@ impl EvmLlvmBackend { } impl BackendTypes for EvmLlvmBackend { - type Type = BasicTypeEnum<'static>; - type Value = BasicValueEnum<'static>; - type StackSlot = PointerValue<'static>; - type BasicBlock = BasicBlock<'static>; - type Function = FunctionValue<'static>; - type Inst = InstructionValue<'static>; + type Type = Type; + type Value = Value; + type StackSlot = Value; + type BasicBlock = Block; + type Function = Function; + type Inst = Inst; } impl TypeMethods for EvmLlvmBackend { fn type_ptr(&self) -> Self::Type { - self.ty_ptr.into() + self.ty_ptr } fn type_ptr_sized_int(&self) -> Self::Type { - self.ty_isize.into() + self.ty_isize } fn type_int(&self, bits: u32) -> Self::Type { @@ -846,20 +799,16 @@ impl TypeMethods for EvmLlvmBackend { 64 => self.ty_i64, 128 => self.cx.i128_type(), 256 => self.ty_i256, - bits => self - .cx - .custom_width_int_type(std::num::NonZeroU32::new(bits).unwrap()) - .expect("custom int type"), + bits => self.cx.int_type(bits), } - .into() } fn type_array(&self, ty: Self::Type, size: u32) -> Self::Type { - ty.array_type(size).into() + ty.array_type(size) } fn type_bit_width(&self, ty: Self::Type) -> u32 { - ty.into_int_type().get_bit_width() + ty.bit_width() } } @@ -890,7 +839,7 @@ impl Backend for EvmLlvmBackend { unsafe { cpp::evm2_jit_llvm_target_machine_set_opt_level( self.machine.as_mut_ptr(), - convert_opt_level(config.opt_level).into(), + convert_opt_level(config.opt_level), ); } } @@ -920,7 +869,9 @@ impl Backend for EvmLlvmBackend { } fn dump_disasm(&mut self, path: &Path) -> Result<()> { - self.machine.write_to_file(self.module(), FileType::Assembly, path).map_err(error_msg) + self.machine + .write_to_file(self.module(), LLVMCodeGenFileType::LLVMAssemblyFile, path) + .map_err(error_msg) } fn build_function( @@ -969,20 +920,9 @@ impl Backend for EvmLlvmBackend { // Attach debug info subprogram if debug is active. self.ensure_di_state(); let debug_scope = if let Some(di) = &self.di_state { - let file = di.compile_unit.get_file(); - let subroutine_type = - di.dibuilder.create_subroutine_type(file, None, &[], DIFlags::PUBLIC); - let subprogram = di.dibuilder.create_function( - di.compile_unit.as_debug_info_scope(), + let subprogram = di.dibuilder.create_subprogram( + di.compile_unit, name, - None, - file, - 0, - subroutine_type, - true, - true, - 0, - DIFlags::PUBLIC, self.backend_config.opt_level != OptimizationLevel::None, ); function.set_subprogram(subprogram); @@ -1036,13 +976,13 @@ impl Backend for EvmLlvmBackend { } }); let opts = PassBuilderOptions::create(); - self.module().run_passes(passes, &self.machine, opts).map_err(error_msg) + self.module().run_passes(passes, &self.machine, &opts).map_err(error_msg) } fn write_object(&mut self, mut w: W) -> Result<()> { let buffer = self .machine - .write_to_memory_buffer(self.module(), FileType::Object) + .write_to_memory_buffer(self.module(), LLVMCodeGenFileType::LLVMObjectFile) .map_err(error_msg)?; w.write_all(buffer.as_slice())?; Ok(()) @@ -1137,33 +1077,17 @@ impl Backend for EvmLlvmBackend { /// Cached target information for the host machine. #[derive(Debug)] struct TargetInfo { - triple: TargetTriple, + triple: CString, target: Target, cpu: String, features: String, } -// SAFETY: No mutability is exposed and `TargetTriple` is an owned string. -unsafe impl std::marker::Send for TargetInfo {} -unsafe impl std::marker::Sync for TargetInfo {} - -impl Clone for TargetInfo { - fn clone(&self) -> Self { - let triple = TargetTriple::create(self.triple.as_str().to_str().unwrap()); - Self { - target: Target::from_triple(&triple).unwrap(), - triple, - cpu: self.cpu.clone(), - features: self.features.clone(), - } - } -} - impl TargetInfo { fn new() -> &'static Self { static HOST_TARGET_INFO: OnceLock = OnceLock::new(); HOST_TARGET_INFO.get_or_init(|| { - let triple = TargetMachine::get_default_triple(); + let triple = TargetMachine::get_default_triple().to_owned(); let target = Target::from_triple(&triple).unwrap(); let cpu = TargetMachine::get_host_cpu_name().to_string_lossy().into_owned(); let features = TargetMachine::get_host_cpu_features().to_string_lossy().into_owned(); @@ -1177,8 +1101,8 @@ impl TargetInfo { #[must_use] pub struct EvmLlvmBuilder<'a> { backend: &'a mut EvmLlvmBackend, - function: FunctionValue<'static>, - debug_scope: Option>, + function: Function, + debug_scope: Option, } impl std::ops::Deref for EvmLlvmBuilder<'_> { @@ -1199,75 +1123,37 @@ impl std::ops::DerefMut for EvmLlvmBuilder<'_> { impl EvmLlvmBuilder<'_> { #[allow(dead_code)] - fn extract_value( - &mut self, - value: BasicValueEnum<'static>, - index: u32, - name: &str, - ) -> BasicValueEnum<'static> { - self.bcx.build_extract_value(value.into_struct_value(), index, self.name(name)).unwrap() + fn extract_value(&mut self, value: Value, index: u32, name: &str) -> Value { + self.bcx.build_extract_value(value, index, self.name(name)) } - fn memcpy_inner( - &mut self, - dst: BasicValueEnum<'static>, - src: BasicValueEnum<'static>, - len: BasicValueEnum<'static>, - inline: bool, - ) { - let dst = dst.into_pointer_value(); - let src = src.into_pointer_value(); - let len = len.into_int_value(); + fn memcpy_inner(&mut self, dst: Value, src: Value, len: Value, inline: bool) { let volatile = self.bool_const(false); - let name = format!( - "llvm.memcpy{}.p0.p0.{}", - if inline { ".inline" } else { "" }, - fmt_ty(len.get_type().into()), - ); let len_ty = len.get_type(); + let name = + format!("llvm.memcpy{}.p0.p0.{}", if inline { ".inline" } else { "" }, fmt_ty(len_ty),); let memcpy = self.get_or_add_function(&name, |this| { - this.ty_void.fn_type( - &[this.ty_ptr.into(), this.ty_ptr.into(), len_ty.into(), this.ty_i1.into()], - false, - ) + this.ty_void.fn_type(&[this.ty_ptr, this.ty_ptr, len_ty, this.ty_i1], false) }); - self.bcx - .build_call(memcpy, &[dst.into(), src.into(), len.into(), volatile.into()], "") - .unwrap(); + self.bcx.build_call(memcpy, &[dst, src, len, volatile], ""); } #[allow(dead_code)] - fn call_overflow_function( - &mut self, - name: &str, - lhs: BasicValueEnum<'static>, - rhs: BasicValueEnum<'static>, - ) -> (BasicValueEnum<'static>, BasicValueEnum<'static>) { + fn call_overflow_function(&mut self, name: &str, lhs: Value, rhs: Value) -> (Value, Value) { let f = self.get_overflow_function(name, lhs.get_type()); let result = self.call(f, &[lhs, rhs]).unwrap(); (self.extract_value(result, 0, "result"), self.extract_value(result, 1, "overflow")) } #[allow(dead_code)] - fn get_overflow_function( - &mut self, - name: &str, - ty: BasicTypeEnum<'static>, - ) -> FunctionValue<'static> { + fn get_overflow_function(&mut self, name: &str, ty: Type) -> Function { let name = format!("llvm.{name}.with.overflow.{}", fmt_ty(ty)); self.get_or_add_function(&name, |this| { - this.fn_type( - Some(this.cx.struct_type(&[ty, this.ty_i1.into()], false).into()), - &[ty, ty], - ) + this.fn_type(Some(this.cx.struct_type(&[ty, this.ty_i1], false)), &[ty, ty]) }) } - fn get_sat_function( - &mut self, - name: &str, - ty: BasicTypeEnum<'static>, - ) -> FunctionValue<'static> { + fn get_sat_function(&mut self, name: &str, ty: Type) -> Function { let name = format!("llvm.{name}.sat.{}", fmt_ty(ty)); self.get_or_add_function(&name, |this| this.fn_type(Some(ty), &[ty, ty])) } @@ -1275,8 +1161,8 @@ impl EvmLlvmBuilder<'_> { fn get_or_add_function( &mut self, name: &str, - mk_ty: impl FnOnce(&mut Self) -> FunctionType<'static>, - ) -> FunctionValue<'static> { + mk_ty: impl FnOnce(&mut Self) -> Type, + ) -> Function { match self.module().get_function(name) { Some(function) => function, None => { @@ -1286,29 +1172,22 @@ impl EvmLlvmBuilder<'_> { } } - fn set_branch_weights( - &self, - inst: InstructionValue<'static>, - weights: impl IntoIterator, - ) { + fn set_branch_weights(&self, inst: Inst, weights: impl IntoIterator) { let weights = weights.into_iter(); - let mut values = Vec::>::with_capacity( - 1 + weights.size_hint().1.unwrap(), - ); - values.push(self.cx.metadata_string("branch_weights").into()); + let mut values = Vec::::with_capacity(1 + weights.size_hint().1.unwrap()); + values.push(self.cx.metadata_string("branch_weights")); for weight in weights { - values.push(self.ty_i32.const_int(weight as u64, false).into()); + values.push(self.ty_i32.const_int(weight as u64, false).as_metadata()); } let metadata = self.cx.metadata_node(&values); let kind_id = self.cx.get_kind_id("prof"); - inst.set_metadata(metadata, kind_id).unwrap(); + inst.set_metadata(metadata, kind_id); } - fn assume_inner(&mut self, cond: BasicValueEnum<'static>) -> CallSiteValue<'static> { - let function = self.get_or_add_function("llvm.assume", |this| { - this.ty_void.fn_type(&[this.ty_i1.into()], false) - }); - self.bcx.build_call(function, &[cond.into()], "").unwrap() + fn assume_inner(&mut self, cond: Value) -> Inst { + let function = self + .get_or_add_function("llvm.assume", |this| this.ty_void.fn_type(&[this.ty_i1], false)); + self.bcx.build_call(function, &[cond], "") } } @@ -1369,7 +1248,7 @@ impl Builder for EvmLlvmBuilder<'_> { let true_ = self.bool_const(true); let callsite = self.assume_inner(true_); let cold = self.cx.create_enum_attribute(Attribute::get_named_enum_kind_id("cold"), 0); - callsite.add_attribute(AttributeLoc::Function, cold); + callsite.add_call_site_attribute(AttributeLoc::Function, cold); } fn current_block(&mut self) -> Option { @@ -1377,7 +1256,7 @@ impl Builder for EvmLlvmBuilder<'_> { } fn block_addr(&mut self, block: Self::BasicBlock) -> Option { - unsafe { block.get_address().map(Into::into) } + unsafe { block.get_address() } } fn add_comment_to_current_inst(&mut self, comment: &str) { @@ -1387,20 +1266,14 @@ impl Builder for EvmLlvmBuilder<'_> { let Some(block) = self.current_block() else { return }; let Some(ins) = block.get_last_instruction() else { return }; let metadata = self.cx.metadata_string(comment); - let metadata = self.cx.metadata_node(&[metadata.into()]); - ins.set_metadata(metadata, self.cx.get_kind_id("annotation")).unwrap(); + let metadata = self.cx.metadata_node(&[metadata]); + ins.set_metadata(metadata, self.cx.get_kind_id("annotation")); } fn set_debug_location(&mut self, line: u32, col: u32) { let Some(scope) = self.debug_scope else { return }; let Some(di) = &self.di_state else { return }; - let loc = di.dibuilder.create_debug_location( - self.cx, - line, - col, - scope.as_debug_info_scope(), - None, - ); + let loc = di.dibuilder.create_debug_location(self.cx, line, col, scope); self.bcx.set_current_debug_location(loc); } @@ -1419,73 +1292,72 @@ impl Builder for EvmLlvmBuilder<'_> { } fn bool_const(&mut self, value: bool) -> Self::Value { - self.ty_i1.const_int(value as u64, false).into() + self.ty_i1.const_int(value as u64, false) } fn iconst(&mut self, ty: Self::Type, value: i64) -> Self::Value { - ty.into_int_type().const_int(value as u64, value.is_negative()).into() + ty.const_int(value as u64, value.is_negative()) } fn uconst(&mut self, ty: Self::Type, value: u64) -> Self::Value { - ty.into_int_type().const_int(value, false).into() + ty.const_int(value, false) } fn iconst_256(&mut self, value: impl TryInto) -> Self::Value { let value = value.try_into().ok().expect("invalid U256 value"); if let Ok(low) = value.try_into() { - return self.ty_i256.const_int(low, false).into(); + return self.ty_i256.const_int(low, false); } self.scratch.clear(); write!(self.scratch, "{value:x}").unwrap(); - self.ty_i256.const_int_from_string(&self.scratch, StringRadix::Hexadecimal).unwrap().into() + self.ty_i256.const_int_from_hex(&self.scratch) } fn str_const(&mut self, value: &str) -> Self::Value { - self.bcx.build_global_string_ptr(value, "").unwrap().as_pointer_value().into() + self.bcx.build_global_string_ptr(value, "") } fn const_array(&mut self, ty: Self::Type, values: &[u64]) -> Self::Value { - let ty = ty.into_int_type(); let values = values.iter().map(|&v| ty.const_int(v, false)).collect::>(); let init = ty.const_array(&values); - let global = self.module().add_global(init.get_type(), None, ""); - global.set_initializer(&init); + let global = self.module().add_global(init.get_type(), ""); + global.set_initializer(init); global.set_constant(true); global.set_linkage(convert_linkage(evm2_jit_backend::Linkage::Private)); - global.set_unnamed_address(UnnamedAddress::Global); - global.as_pointer_value().into() + global.set_unnamed_address(LLVMUnnamedAddr::LLVMGlobalUnnamedAddr); + global.as_pointer_value() } fn nullptr(&mut self) -> Self::Value { - self.ty_ptr.const_null().into() + self.ty_ptr.const_null() } fn new_stack_slot_raw(&mut self, ty: Self::Type, name: &str) -> Self::StackSlot { // let ty = self.ty_i8.array_type(size); - // let ptr = self.bcx.build_alloca(ty, self.name(name)).unwrap(); - // ptr.as_instruction().unwrap().set_alignment(align).unwrap(); + // let ptr = self.bcx.build_alloca(ty, self.name(name)); + // ptr.as_instruction().unwrap().set_alignment(align); // ptr - self.bcx.build_alloca(ty, self.name(name)).unwrap() + self.bcx.build_alloca(ty, self.name(name)) } fn stack_load(&mut self, ty: Self::Type, slot: Self::StackSlot, name: &str) -> Self::Value { - self.load(ty, slot.into(), name) + self.load(ty, slot, name) } fn stack_store(&mut self, value: Self::Value, slot: Self::StackSlot) { - self.store(value, slot.into()); + self.store(value, slot); } fn stack_addr(&mut self, ty: Self::Type, slot: Self::StackSlot) -> Self::Value { let _ = ty; - slot.into() + slot } fn load(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value { - let value = self.bcx.build_load(ty, ptr.into_pointer_value(), self.name(name)).unwrap(); - if ty == self.ty_i256.into() { - self.current_block().unwrap().get_last_instruction().unwrap().set_alignment(8).unwrap(); + let value = self.bcx.build_load(ty, ptr, self.name(name)); + if ty == self.ty_i256 { + value.set_alignment(8); } value } @@ -1497,27 +1369,22 @@ impl Builder for EvmLlvmBuilder<'_> { align: usize, name: &str, ) -> Self::Value { - let value = self.bcx.build_load(ty, ptr.into_pointer_value(), self.name(name)).unwrap(); - self.current_block() - .unwrap() - .get_last_instruction() - .unwrap() - .set_alignment(align as u32) - .unwrap(); + let value = self.bcx.build_load(ty, ptr, self.name(name)); + value.set_alignment(align as u32); value } fn store(&mut self, value: Self::Value, ptr: Self::Value) -> Self::Inst { - let inst = self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); - if value.get_type() == self.ty_i256.into() { - inst.set_alignment(8).unwrap(); + let inst = self.bcx.build_store(ptr, value); + if value.get_type() == self.ty_i256 { + inst.set_alignment(8); } inst } fn store_aligned(&mut self, value: Self::Value, ptr: Self::Value, align: usize) { - let inst = self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); - inst.set_alignment(align as u32).unwrap(); + let inst = self.bcx.build_store(ptr, value); + inst.set_alignment(align as u32); } fn remove_inst(&mut self, inst: Self::Inst) { @@ -1531,10 +1398,9 @@ impl Builder for EvmLlvmBuilder<'_> { fn ret(&mut self, values: &[Self::Value]) { match values { [] => self.bcx.build_return(None), - [value] => self.bcx.build_return(Some(value)), + [value] => self.bcx.build_return(Some(*value)), values => self.bcx.build_aggregate_return(values), - } - .unwrap(); + }; } fn assume(&mut self, cond: Self::Value) { @@ -1542,10 +1408,7 @@ impl Builder for EvmLlvmBuilder<'_> { } fn icmp(&mut self, cond: IntCC, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_compare(convert_intcc(cond), lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_compare(convert_intcc(cond), lhs, rhs, "") } fn icmp_imm(&mut self, cond: IntCC, lhs: Self::Value, rhs: i64) -> Self::Value { @@ -1554,15 +1417,15 @@ impl Builder for EvmLlvmBuilder<'_> { } fn is_null(&mut self, ptr: Self::Value) -> Self::Value { - self.bcx.build_is_null(ptr.into_pointer_value(), "").unwrap().into() + self.bcx.build_is_null(ptr, "") } fn is_not_null(&mut self, ptr: Self::Value) -> Self::Value { - self.bcx.build_is_not_null(ptr.into_pointer_value(), "").unwrap().into() + self.bcx.build_is_not_null(ptr, "") } fn br(&mut self, dest: Self::BasicBlock) { - self.bcx.build_unconditional_branch(dest).unwrap(); + self.bcx.build_unconditional_branch(dest); } fn brif( @@ -1571,7 +1434,7 @@ impl Builder for EvmLlvmBuilder<'_> { then_block: Self::BasicBlock, else_block: Self::BasicBlock, ) { - self.bcx.build_conditional_branch(cond.into_int_value(), then_block, else_block).unwrap(); + self.bcx.build_conditional_branch(cond, then_block, else_block); } fn brif_cold( @@ -1581,10 +1444,7 @@ impl Builder for EvmLlvmBuilder<'_> { else_block: Self::BasicBlock, then_is_cold: bool, ) { - let inst = self - .bcx - .build_conditional_branch(cond.into_int_value(), then_block, else_block) - .unwrap(); + let inst = self.bcx.build_conditional_branch(cond, then_block, else_block); let weights = if then_is_cold { [1, DEFAULT_WEIGHT] } else { [DEFAULT_WEIGHT, 1] }; self.set_branch_weights(inst, weights); } @@ -1596,10 +1456,10 @@ impl Builder for EvmLlvmBuilder<'_> { targets: &[(u64, Self::BasicBlock)], default_is_cold: bool, ) { - let ty = index.get_type().into_int_type(); + let ty = index.get_type(); let targets = targets.iter().map(|(v, b)| (ty.const_int(*v, false), *b)).collect::>(); - let inst = self.bcx.build_switch(index.into_int_value(), default, &targets).unwrap(); + let inst = self.bcx.build_switch(index, default, &targets); if default_is_cold { let weights = iter::once(1).chain(iter::repeat_n(DEFAULT_WEIGHT, targets.len())); self.set_branch_weights(inst, weights); @@ -1607,17 +1467,11 @@ impl Builder for EvmLlvmBuilder<'_> { } fn br_indirect(&mut self, address: Self::Value, destinations: &[Self::BasicBlock]) { - let _ = self.bcx.build_indirect_branch(address, destinations).unwrap(); + self.bcx.build_indirect_branch(address, destinations); } fn phi(&mut self, ty: Self::Type, incoming: &[(Self::Value, Self::BasicBlock)]) -> Self::Value { - let incoming = incoming - .iter() - .map(|(value, block)| (value as &dyn BasicValue<'_>, *block)) - .collect::>(); - let phi = self.bcx.build_phi(ty, "").unwrap(); - phi.add_incoming(&incoming); - phi.as_basic_value() + self.bcx.build_phi(ty, incoming, "") } fn select( @@ -1626,47 +1480,35 @@ impl Builder for EvmLlvmBuilder<'_> { then_value: Self::Value, else_value: Self::Value, ) -> Self::Value { - self.bcx.build_select(cond.into_int_value(), then_value, else_value, "").unwrap() + self.bcx.build_select(cond, then_value, else_value, "") } fn iadd(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_add(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_add(lhs, rhs, "") } fn isub(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_sub(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_sub(lhs, rhs, "") } fn imul(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_mul(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_mul(lhs, rhs, "") } fn udiv(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_unsigned_div(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_unsigned_div(lhs, rhs, "") } fn sdiv(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_signed_div(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_signed_div(lhs, rhs, "") } fn urem(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_unsigned_rem(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_unsigned_rem(lhs, rhs, "") } fn srem(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_signed_rem(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_signed_rem(lhs, rhs, "") } fn iadd_imm(&mut self, lhs: Self::Value, rhs: i64) -> Self::Value { @@ -1726,19 +1568,19 @@ impl Builder for EvmLlvmBuilder<'_> { } fn bitor(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_or(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_or(lhs, rhs, "") } fn bitand(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_and(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_and(lhs, rhs, "") } fn bitxor(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_xor(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_xor(lhs, rhs, "") } fn bitnot(&mut self, value: Self::Value) -> Self::Value { - self.bcx.build_not(value.into_int_value(), "").unwrap().into() + self.bcx.build_not(value, "") } fn clz(&mut self, value: Self::Value) -> Self::Value { @@ -1773,40 +1615,31 @@ impl Builder for EvmLlvmBuilder<'_> { } fn ishl(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_left_shift(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_left_shift(lhs, rhs, "") } fn ushr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_right_shift(lhs.into_int_value(), rhs.into_int_value(), false, "") - .unwrap() - .into() + self.bcx.build_right_shift(lhs, rhs, false, "") } fn sshr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_right_shift(lhs.into_int_value(), rhs.into_int_value(), true, "") - .unwrap() - .into() + self.bcx.build_right_shift(lhs, rhs, true, "") } fn zext(&mut self, ty: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_z_extend(value.into_int_value(), ty.into_int_type(), "").unwrap().into() + self.bcx.build_int_z_extend(value, ty, "") } fn sext(&mut self, ty: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_s_extend(value.into_int_value(), ty.into_int_type(), "").unwrap().into() + self.bcx.build_int_s_extend(value, ty, "") } fn ireduce(&mut self, to: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_truncate(value.into_int_value(), to.into_int_type(), "").unwrap().into() + self.bcx.build_int_truncate(value, to, "") } fn inttoptr(&mut self, value: Self::Value, ty: Self::Type) -> Self::Value { - self.bcx - .build_int_to_ptr(value.into_int_value(), ty.into_pointer_type(), "") - .unwrap() - .into() + self.bcx.build_int_to_ptr(value, ty, "") } fn gep( @@ -1816,17 +1649,7 @@ impl Builder for EvmLlvmBuilder<'_> { indexes: &[Self::Value], name: &str, ) -> Self::Value { - let indexes = indexes.iter().map(|idx| idx.into_int_value()).collect::>(); - unsafe { - self.bcx.build_in_bounds_gep( - elem_ty, - ptr.into_pointer_value(), - &indexes, - self.name(name), - ) - } - .unwrap() - .into() + unsafe { self.bcx.build_in_bounds_gep(elem_ty, ptr, indexes, self.name(name)) } } fn tail_call( @@ -1835,27 +1658,20 @@ impl Builder for EvmLlvmBuilder<'_> { args: &[Self::Value], tail_call: TailCallKind, ) -> Option { - let args = args.iter().copied().map(Into::into).collect::>(); - let callsite = self.bcx.build_call(function, &args, "").unwrap(); + let callsite = self.bcx.build_call(function, args, ""); if let Some(call_conv) = function_call_conv(function) { - unsafe { - inkwell::llvm_sys::core::LLVMSetInstructionCallConv( - callsite.as_value_ref(), - call_conv as _, - ); - } + callsite.set_call_conv(call_conv); } if tail_call != TailCallKind::None { callsite.set_tail_call_kind(convert_tail_call_kind(tail_call)); } - callsite.try_as_basic_value().basic() + callsite.value() } fn is_compile_time_known(&mut self, value: Self::Value) -> Option { let ty = value.get_type(); let name = format!("llvm.is.constant.{}", fmt_ty(ty)); - let f = - self.get_or_add_function(&name, |this| this.fn_type(Some(this.ty_i1.into()), &[ty])); + let f = self.get_or_add_function(&name, |this| this.fn_type(Some(this.ty_i1), &[ty])); Some(self.call(f, &[value]).unwrap()) } @@ -1864,12 +1680,12 @@ impl Builder for EvmLlvmBuilder<'_> { } fn memcpy_inline(&mut self, dst: Self::Value, src: Self::Value, len: i64) { - let len = self.iconst(self.ty_i64.into(), len); + let len = self.iconst(self.ty_i64, len); self.memcpy_inner(dst, src, len, true); } fn unreachable(&mut self) { - self.bcx.build_unreachable().unwrap(); + self.bcx.build_unreachable(); } fn get_or_build_function( @@ -1913,9 +1729,8 @@ impl Builder for EvmLlvmBuilder<'_> { return function; } - let ty = self.cx.void_type().fn_type(&[self.ty_ptr.into()], true); - let function = - self.module().add_function(name, ty, Some(inkwell::module::Linkage::External)); + let ty = self.cx.void_type().fn_type(&[self.ty_ptr], true); + let function = self.module().add_function(name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); cpp::set_dso_local(function); function } @@ -1946,18 +1761,15 @@ impl Builder for EvmLlvmBuilder<'_> { function: Self::Function, call_conv: CallConv, ) -> Self::Function { - let name = function.get_name().to_string_lossy(); + let name = function.get_name(); let stub_name = format!("{name}.stub"); if let Some(stub) = self.module().get_function(&stub_name) { return stub; } let func_ty = function.get_type(); - let stub = self.module().add_function( - &stub_name, - func_ty, - Some(inkwell::module::Linkage::Internal), - ); + let stub = + self.module().add_function(&stub_name, func_ty, Some(LLVMLinkage::LLVMInternalLinkage)); set_function_call_conv(stub, call_conv); cpp::set_dso_local(stub); self.add_function_attribute( @@ -1974,11 +1786,8 @@ impl Builder for EvmLlvmBuilder<'_> { self.bcx.position_at_end(entry); let args = (0..stub.count_params()).map(|i| stub.get_nth_param(i).unwrap()).collect::>(); - if let Some(value) = self.call(function, &args) { - self.bcx.build_return(Some(&value)).unwrap(); - } else { - self.bcx.build_return(None).unwrap(); - } + let value = self.call(function, &args); + self.bcx.build_return(value); if let Some(before) = before { self.bcx.position_at_end(before); @@ -2056,7 +1865,7 @@ fn init_() -> Result<()> { } unsafe { - install_fatal_error_handler(report_fatal_error); + LLVMInstallFatalErrorHandler(Some(report_fatal_error)); } // Collect extra LLVM args from `EVM2_JIT_LLVM_ARGS` env var (space-separated). @@ -2115,34 +1924,16 @@ fn init_() -> Result<()> { c"-capture-tracking-max-uses-to-explore=1000000".as_ptr(), ]; args.extend(extra.iter().map(|s| s.as_ptr())); - unsafe { - inkwell::llvm_sys::support::LLVMParseCommandLineOptions( - args.len() as i32, - args.as_ptr(), - std::ptr::null(), - ) - } + unsafe { LLVMParseCommandLineOptions(args.len() as i32, args.as_ptr(), std::ptr::null()) } // TODO: https://github.com/llvm/llvm-project/issues/235937 unsafe { cpp::evm2_jit_llvm_expand_ir_insts_after_lsr() }; - let config = InitializationConfig { - asm_parser: false, - asm_printer: true, - base: true, - disassembler: true, - info: true, - machine_code: true, - }; - Target::initialize_native(&config).map_err(|e| eyre::eyre!(e))?; + Target::initialize_native().map_err(|e| eyre::eyre!(e))?; Ok(()) } -fn create_module<'ctx>( - cx: &'ctx Context, - machine: &TargetMachine, - aot: bool, -) -> Result> { +fn create_module(cx: &Context, machine: &TargetMachine, aot: bool) -> Result { let module_name = "evm"; let module = cx.create_module(module_name); module.set_source_file_name(module_name); @@ -2151,41 +1942,41 @@ fn create_module<'ctx>( if aot { module.add_basic_value_flag( "PIC Level", - FlagBehavior::Error, // TODO: Min + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorError, // TODO: Min cx.i32_type().const_int(2, false), ); module.add_basic_value_flag( "RtLibUseGOT", - FlagBehavior::Warning, + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, cx.i32_type().const_int(1, false), ); } Ok(module) } -const fn convert_intcc(cond: IntCC) -> IntPredicate { +const fn convert_intcc(cond: IntCC) -> LLVMIntPredicate { match cond { - IntCC::Equal => IntPredicate::EQ, - IntCC::NotEqual => IntPredicate::NE, - IntCC::SignedLessThan => IntPredicate::SLT, - IntCC::SignedGreaterThanOrEqual => IntPredicate::SGE, - IntCC::SignedGreaterThan => IntPredicate::SGT, - IntCC::SignedLessThanOrEqual => IntPredicate::SLE, - IntCC::UnsignedLessThan => IntPredicate::ULT, - IntCC::UnsignedGreaterThanOrEqual => IntPredicate::UGE, - IntCC::UnsignedGreaterThan => IntPredicate::UGT, - IntCC::UnsignedLessThanOrEqual => IntPredicate::ULE, + IntCC::Equal => LLVMIntPredicate::LLVMIntEQ, + IntCC::NotEqual => LLVMIntPredicate::LLVMIntNE, + IntCC::SignedLessThan => LLVMIntPredicate::LLVMIntSLT, + IntCC::SignedGreaterThanOrEqual => LLVMIntPredicate::LLVMIntSGE, + IntCC::SignedGreaterThan => LLVMIntPredicate::LLVMIntSGT, + IntCC::SignedLessThanOrEqual => LLVMIntPredicate::LLVMIntSLE, + IntCC::UnsignedLessThan => LLVMIntPredicate::LLVMIntULT, + IntCC::UnsignedGreaterThanOrEqual => LLVMIntPredicate::LLVMIntUGE, + IntCC::UnsignedGreaterThan => LLVMIntPredicate::LLVMIntUGT, + IntCC::UnsignedLessThanOrEqual => LLVMIntPredicate::LLVMIntULE, } } /// Maps to the codegen level of the target machine, used for AOT and dumps. `Less` skips the /// post-RA scheduler on CPUs that enable it (znver), saving about 5% of compile instructions for /// no runtime gain. The JIT compiler in `lib.cpp` always uses `Less`, whatever the level. -const fn convert_opt_level(level: OptimizationLevel) -> inkwell::OptimizationLevel { +const fn convert_opt_level(level: OptimizationLevel) -> LLVMCodeGenOptLevel { match level { - OptimizationLevel::None => inkwell::OptimizationLevel::None, + OptimizationLevel::None => LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, OptimizationLevel::Less | OptimizationLevel::Default | OptimizationLevel::Aggressive => { - inkwell::OptimizationLevel::Less + LLVMCodeGenOptLevel::LLVMCodeGenLevelLess } } } @@ -2196,7 +1987,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute enum AttrValue<'a> { String(&'a str), Enum(u64), - Type(AnyTypeEnum<'a>), + Type(Type), } let cpu; @@ -2229,9 +2020,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::Align(n) => ("align", AttrValue::Enum(n)), OurAttr::NonNull => ("nonnull", AttrValue::Enum(0)), OurAttr::Dereferenceable(n) => ("dereferenceable", AttrValue::Enum(n)), - OurAttr::SRet(n) => { - ("sret", AttrValue::Type(bcx.type_array(bcx.ty_i8.into(), n as _).as_any_type_enum())) - } + OurAttr::SRet(n) => ("sret", AttrValue::Type(bcx.type_array(bcx.ty_i8, n as _))), OurAttr::ReadNone => ("readnone", AttrValue::Enum(0)), OurAttr::ReadOnly => ("readonly", AttrValue::Enum(0)), OurAttr::WriteOnly => ("writeonly", AttrValue::Enum(0)), @@ -2269,48 +2058,45 @@ const fn convert_attribute_loc(loc: evm2_jit_backend::FunctionAttributeLocation) } } -fn function_call_conv(function: FunctionValue<'_>) -> Option { - let call_conv = - unsafe { inkwell::llvm_sys::core::LLVMGetFunctionCallConv(function.as_value_ref()) }; - (call_conv != inkwell::llvm_sys::LLVMCallConv::LLVMCCallConv as u32).then_some(call_conv) +fn function_call_conv(function: Function) -> Option { + let call_conv = function.get_call_conv(); + (call_conv != LLVMCallConv::LLVMCCallConv as u32).then_some(call_conv) } -fn set_function_call_conv(function: FunctionValue<'_>, call_conv: CallConv) { +fn set_function_call_conv(function: Function, call_conv: CallConv) { let Some(call_conv) = convert_call_conv(call_conv) else { return }; - unsafe { - inkwell::llvm_sys::core::LLVMSetFunctionCallConv(function.as_value_ref(), call_conv as _); - } + function.set_call_conv(call_conv as _); } -const fn convert_call_conv(call_conv: CallConv) -> Option { +const fn convert_call_conv(call_conv: CallConv) -> Option { match call_conv { CallConv::Default => None, - CallConv::Cold => Some(inkwell::llvm_sys::LLVMCallConv::LLVMPreserveMostCallConv), + CallConv::Cold => Some(LLVMCallConv::LLVMPreserveMostCallConv), } } -const fn convert_linkage(linkage: evm2_jit_backend::Linkage) -> inkwell::module::Linkage { +const fn convert_linkage(linkage: evm2_jit_backend::Linkage) -> LLVMLinkage { match linkage { - evm2_jit_backend::Linkage::Public => inkwell::module::Linkage::External, - evm2_jit_backend::Linkage::Import => inkwell::module::Linkage::External, - evm2_jit_backend::Linkage::Private => inkwell::module::Linkage::Private, + evm2_jit_backend::Linkage::Public => LLVMLinkage::LLVMExternalLinkage, + evm2_jit_backend::Linkage::Import => LLVMLinkage::LLVMExternalLinkage, + evm2_jit_backend::Linkage::Private => LLVMLinkage::LLVMPrivateLinkage, } } -const fn convert_tail_call_kind(kind: TailCallKind) -> inkwell::llvm_sys::LLVMTailCallKind { +const fn convert_tail_call_kind(kind: TailCallKind) -> LLVMTailCallKind { match kind { - TailCallKind::None => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindNone, - TailCallKind::Tail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindTail, - TailCallKind::MustTail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindMustTail, - TailCallKind::NoTail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindNoTail, + TailCallKind::None => LLVMTailCallKind::LLVMTailCallKindNone, + TailCallKind::Tail => LLVMTailCallKind::LLVMTailCallKindTail, + TailCallKind::MustTail => LLVMTailCallKind::LLVMTailCallKindMustTail, + TailCallKind::NoTail => LLVMTailCallKind::LLVMTailCallKindNoTail, } } // No `#[track_caller]` because `map_err` doesn't propagate it. -fn error_msg(msg: inkwell::support::LLVMString) -> evm2_jit_backend::Error { +fn error_msg(msg: LLVMString) -> evm2_jit_backend::Error { evm2_jit_backend::Error::msg(msg.to_string_lossy().trim_end().to_string()) } -fn fmt_ty(ty: BasicTypeEnum<'_>) -> impl std::fmt::Display { +fn fmt_ty(ty: Type) -> impl std::fmt::Display { ty.print_to_string().to_str().unwrap().trim_matches('"').to_string() } diff --git a/crates/jit/llvm/src/orc.rs b/crates/jit/llvm/src/orc.rs index 5d65dcfce..bd04337d6 100644 --- a/crates/jit/llvm/src/orc.rs +++ b/crates/jit/llvm/src/orc.rs @@ -1,4 +1,4 @@ -//! Safe wrappers for LLVM ORC and LLJIT, which are not yet implemented in [`inkwell`]. +//! Safe wrappers for LLVM ORC and LLJIT. //! //! Header files implemented: //! - `LLJIT.h` @@ -13,21 +13,15 @@ #![allow(clippy::missing_safety_doc)] #![allow(clippy::too_many_arguments)] -use crate::llvm_string; -use inkwell::{ - context::Context, - llvm_sys::{ - core::{ - LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, - LLVMModuleCreateWithNameInContext, - }, - error::*, - orc2::{lljit::*, *}, - prelude::*, +use crate::wrap::{Context, LLVMString, Module, TargetMachine}; +use llvm_sys::{ + core::{ + LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, + LLVMModuleCreateWithNameInContext, }, - module::Module, - support::LLVMString, - targets::TargetMachine, + error::*, + orc2::{lljit::*, *}, + prelude::*, }; use std::{ ffi::{CStr, CString, c_char, c_void}, @@ -82,7 +76,7 @@ impl ThreadSafeContext { } /// Create a ThreadSafeModule wrapper around the given LLVM module. - pub fn create_module<'ctx>(&'ctx self, module: Module<'ctx>) -> ThreadSafeModule { + pub fn create_module(&self, module: Module) -> ThreadSafeModule { ThreadSafeModule::create_in_context(module, self) } } @@ -121,9 +115,8 @@ impl ThreadSafeModule { } /// Create a ThreadSafeModule wrapper around the given LLVM module. - pub fn create_in_context<'ctx>(module: Module<'ctx>, ctx: &'ctx ThreadSafeContext) -> Self { - let module = mem::ManuallyDrop::new(module); - let ptr = unsafe { LLVMOrcCreateNewThreadSafeModule(module.as_mut_ptr(), ctx.as_inner()) }; + pub fn create_in_context(module: Module, ctx: &ThreadSafeContext) -> Self { + let ptr = unsafe { LLVMOrcCreateNewThreadSafeModule(module.into_raw(), ctx.as_inner()) }; Self { ptr } } @@ -140,18 +133,18 @@ impl ThreadSafeModule { /// Runs the given closure with the module. /// /// This implicitly locks the associated context. - pub fn with_module<'tsm>( - &'tsm self, - mut f: impl FnMut(&Module<'tsm>) -> Result<(), String>, + pub fn with_module( + &self, + mut f: impl FnMut(&Module) -> Result<(), String>, ) -> Result<(), LLVMString> { extern "C" fn shim(ctx: *mut c_void, m: LLVMModuleRef) -> LLVMErrorRef { - let f = ctx.cast::<&mut dyn FnMut(&Module<'_>) -> Result<(), String>>(); + let f = ctx.cast::<&mut dyn FnMut(&Module) -> Result<(), String>>(); let m = mem::ManuallyDrop::new(unsafe { Module::new(m) }); let res = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe { (*f)(&m) })); cvt_cb_res(res) } - let mut f = &mut f as &mut dyn FnMut(&Module<'tsm>) -> Result<(), String>; + let mut f = &mut f as &mut dyn FnMut(&Module) -> Result<(), String>; let ctx = &mut f as *mut _ as *mut c_void; cvt(unsafe { LLVMOrcThreadSafeModuleWithModuleDo(self.as_inner(), shim, ctx) }) } @@ -1207,7 +1200,9 @@ impl JITTargetMachineBuilder { /// Returns the target triple for the given JITTargetMachineBuilder as a string. pub fn get_target_triple(&self) -> LLVMString { - unsafe { llvm_string(LLVMOrcJITTargetMachineBuilderGetTargetTriple(self.as_inner())) } + unsafe { + LLVMString::from_raw(LLVMOrcJITTargetMachineBuilderGetTargetTriple(self.as_inner())) + } } /// Sets the target triple for the given JITTargetMachineBuilder to the given string. @@ -1575,7 +1570,7 @@ impl ObjectTransformLayerRef { /// Returning `Err` aborts materialization. pub fn set_transform(&self, f: ObjTransformFn) { extern "C" fn shim(ctx: *mut c_void, obj_in_out: *mut LLVMMemoryBufferRef) -> LLVMErrorRef { - use inkwell::llvm_sys::core::{ + use llvm_sys::core::{ LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMDisposeMemoryBuffer, LLVMGetBufferSize, LLVMGetBufferStart, }; @@ -1707,7 +1702,11 @@ unsafe impl Send for LLJITBuilder {} /// Converts an `LLVMErrorRef` to a `Result`. pub(crate) fn cvt(ptr: LLVMErrorRef) -> Result<(), LLVMString> { - if ptr.is_null() { Ok(()) } else { Err(unsafe { llvm_string(LLVMGetErrorMessage(ptr)) }) } + if ptr.is_null() { + Ok(()) + } else { + Err(unsafe { LLVMString::from_raw(LLVMGetErrorMessage(ptr)) }) + } } fn cvt_cb_res(res: Result, Box>) -> LLVMErrorRef { @@ -1796,13 +1795,18 @@ impl Drop for ManuallyDropElements { #[cfg(test)] mod tests { use super::*; - use inkwell::{passes::PassBuilderOptions, targets::Target}; + use crate::wrap::{PassBuilderOptions, Target}; + use llvm_sys::{ + LLVMIntPredicate, LLVMLinkage, + core::LLVMGetModuleContext, + target_machine::{LLVMCodeGenOptLevel, LLVMCodeModel, LLVMRelocMode}, + }; #[test] fn e2e_with_tsc() { use mem::ManuallyDrop; - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1819,9 +1823,9 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - inkwell::OptimizationLevel::None, - inkwell::targets::RelocMode::Static, - inkwell::targets::CodeModel::JITDefault, + LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, + LLVMRelocMode::LLVMRelocStatic, + LLVMCodeModel::LLVMCodeModelJITDefault, ) .unwrap(); @@ -1839,9 +1843,8 @@ mod tests { extern "C" fn my_external(x: i64) -> i64 { x + 100 } - let ext_ty = cx.i64_type().fn_type(&[cx.i64_type().into()], false); - let _ext_fn = - m.add_function("my_external", ext_ty, Some(inkwell::module::Linkage::External)); + let ext_ty = cx.i64_type().fn_type(&[cx.i64_type()], false); + let _ext_fn = m.add_function("my_external", ext_ty, Some(LLVMLinkage::LLVMExternalLinkage)); let ext_name = CString::new("my_external").unwrap(); let sym = SymbolMapPair::new( jit.mangle_and_intern(&ext_name), @@ -1856,10 +1859,10 @@ mod tests { .unwrap(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); - bcx.build_return(Some(&cx.i64_type().const_int(142, false))).unwrap(); + bcx.build_return(Some(cx.i64_type().const_int(142, false))); eprintln!("module:\n{}", m.print_to_string().to_string_lossy()); m.verify().unwrap(); @@ -1894,7 +1897,7 @@ mod tests { fn lookup_in_custom_jd() { use mem::ManuallyDrop; - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1911,9 +1914,9 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - inkwell::OptimizationLevel::None, - inkwell::targets::RelocMode::Static, - inkwell::targets::CodeModel::JITDefault, + LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, + LLVMRelocMode::LLVMRelocStatic, + LLVMCodeModel::LLVMCodeModelJITDefault, ) .unwrap(); @@ -1931,16 +1934,13 @@ mod tests { let fn_name = "test_fn"; let bcx = cx.create_builder(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); - bcx.build_return(Some(&cx.i64_type().const_int(value, false))).unwrap(); + bcx.build_return(Some(cx.i64_type().const_int(value, false))); m.verify().unwrap(); - let module = ManuallyDrop::new(m); - let tsm = unsafe { - ThreadSafeModule::create_in_context(Module::new(module.as_mut_ptr()), &tscx) - }; + let tsm = ThreadSafeModule::create_in_context(m, &tscx); jit.add_module_with_dylib(tsm, unsafe { JITDylibRef::from_inner_unchecked(jd.as_inner()) }) @@ -1967,28 +1967,29 @@ mod tests { let tsm = ThreadSafeModule::create("test"); let fn_name = "my_fn"; tsm.with_module(|m| { - let cx = m.get_context(); + let cx = mem::ManuallyDrop::new(unsafe { + Context::new(LLVMGetModuleContext(m.as_mut_ptr())) + }); let bcx = cx.create_builder(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); bcx.build_int_compare( - inkwell::IntPredicate::EQ, - cx.i64_type().const_zero(), - cx.i64_type().const_all_ones(), + LLVMIntPredicate::LLVMIntEQ, + cx.i64_type().const_null(), + cx.i64_type().const_int(u64::MAX, false), "a", - ) - .unwrap(); - bcx.build_return(Some(&cx.i64_type().const_int(69, false))).unwrap(); + ); + bcx.build_return(Some(cx.i64_type().const_int(69, false))); eprintln!("--before--"); eprintln!("{}", m.print_to_string().to_string_lossy()); m.verify().map_err(|e| e.to_string())?; - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1998,13 +1999,13 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - Default::default(), - inkwell::targets::RelocMode::Default, - inkwell::targets::CodeModel::Default, + LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault, + LLVMRelocMode::LLVMRelocDefault, + LLVMCodeModel::LLVMCodeModelDefault, ) .ok_or_else(|| String::from("failed to create target machine"))?; - m.run_passes("default", &machine, PassBuilderOptions::create()).unwrap(); + m.run_passes("default", &machine, &PassBuilderOptions::create()).unwrap(); eprintln!("--after--"); eprintln!("{}", m.print_to_string().to_string_lossy()); diff --git a/crates/jit/llvm/src/utils.rs b/crates/jit/llvm/src/utils.rs deleted file mode 100644 index 59c71c8d2..000000000 --- a/crates/jit/llvm/src/utils.rs +++ /dev/null @@ -1,7 +0,0 @@ -use inkwell::support::LLVMString; -use std::ffi::c_char; - -pub(crate) unsafe fn llvm_string(ptr: *const c_char) -> LLVMString { - // `LLVMString::new` is private - unsafe { std::mem::transmute(ptr) } -} diff --git a/crates/jit/llvm/src/wrap/di.rs b/crates/jit/llvm/src/wrap/di.rs new file mode 100644 index 000000000..b2f1190a1 --- /dev/null +++ b/crates/jit/llvm/src/wrap/di.rs @@ -0,0 +1,161 @@ +//! Debug info. + +use super::{Context, Module}; +use llvm_sys::{ + debuginfo::*, + prelude::{LLVMDIBuilderRef, LLVMMetadataRef}, +}; +use std::ptr; + +/// Returns the debug info metadata version LLVM emits, for the `Debug Info Version` module flag. +pub(crate) fn debug_metadata_version() -> u32 { + unsafe { LLVMDebugMetadataVersion() } +} + +/// An LLVM metadata node. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Metadata(LLVMMetadataRef); + +impl Metadata { + pub(crate) const fn new(raw: LLVMMetadataRef) -> Self { + Self(raw) + } + + pub(crate) const fn raw(self) -> LLVMMetadataRef { + self.0 + } +} + +/// A debug location, attached to the instructions a [`Builder`](super::Builder) creates. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) struct DILocation(LLVMMetadataRef); + +impl DILocation { + pub(crate) fn new(raw: LLVMMetadataRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + pub(crate) const fn raw(self) -> LLVMMetadataRef { + self.0 + } +} + +/// A compile unit and the file it describes. +#[derive(Clone, Copy, Debug)] +pub(crate) struct DICompileUnit { + unit: LLVMMetadataRef, + file: LLVMMetadataRef, +} + +/// An owned debug info builder. +#[derive(Debug)] +pub(crate) struct DebugInfoBuilder { + raw: LLVMDIBuilderRef, +} + +impl DebugInfoBuilder { + /// Creates a builder for `module` and a C compile unit for the file `filename` in `directory`. + pub(crate) fn new( + module: &Module, + filename: &str, + directory: &str, + producer: &str, + is_optimized: bool, + flags: &str, + ) -> (Self, DICompileUnit) { + unsafe { + let builder = Self { raw: LLVMCreateDIBuilder(module.as_mut_ptr()) }; + let file = LLVMDIBuilderCreateFile( + builder.raw, + filename.as_ptr().cast(), + filename.len(), + directory.as_ptr().cast(), + directory.len(), + ); + let unit = LLVMDIBuilderCreateCompileUnit( + builder.raw, + LLVMDWARFSourceLanguage::LLVMDWARFSourceLanguageC, + file, + producer.as_ptr().cast(), + producer.len(), + is_optimized as _, + flags.as_ptr().cast(), + flags.len(), + 0, + c"".as_ptr(), + 0, + LLVMDWARFEmissionKind::LLVMDWARFEmissionKindFull, + 0, + false as _, + false as _, + c"".as_ptr(), + 0, + c"".as_ptr(), + 0, + ); + (builder, DICompileUnit { unit, file }) + } + } + + /// Describes a public, local function without parameters and line information. + pub(crate) fn create_subprogram( + &self, + unit: DICompileUnit, + name: &str, + is_optimized: bool, + ) -> Metadata { + unsafe { + // A null return type means `void`. + let mut types = [ptr::null_mut()]; + let ty = LLVMDIBuilderCreateSubroutineType( + self.raw, + unit.file, + types.as_mut_ptr(), + types.len() as u32, + LLVMDIFlagPublic, + ); + Metadata(LLVMDIBuilderCreateFunction( + self.raw, + unit.unit, + name.as_ptr().cast(), + name.len(), + name.as_ptr().cast(), + name.len(), + unit.file, + 0, + ty, + true as _, + true as _, + 0, + LLVMDIFlagPublic, + is_optimized as _, + )) + } + } + + /// Creates a debug location in `scope`. + pub(crate) fn create_debug_location( + &self, + cx: &Context, + line: u32, + column: u32, + scope: Metadata, + ) -> DILocation { + DILocation(unsafe { + LLVMDIBuilderCreateDebugLocation(cx.raw(), line, column, scope.0, ptr::null_mut()) + }) + } + + /// Resolves forward references. Call before emitting the module. + pub(crate) fn finalize(&self) { + unsafe { LLVMDIBuilderFinalize(self.raw) } + } +} + +impl Drop for DebugInfoBuilder { + fn drop(&mut self) { + self.finalize(); + unsafe { LLVMDisposeDIBuilder(self.raw) } + } +} diff --git a/crates/jit/llvm/src/wrap/ir.rs b/crates/jit/llvm/src/wrap/ir.rs new file mode 100644 index 000000000..8215e21a7 --- /dev/null +++ b/crates/jit/llvm/src/wrap/ir.rs @@ -0,0 +1,1042 @@ +//! LLVM IR: contexts, modules, types, values, and the instruction builder. + +use super::{DILocation, LLVMString, Metadata, TargetData, TargetMachine, to_c_str}; +use llvm_sys::{ + LLVMAttributeFunctionIndex, LLVMIntPredicate, LLVMLinkage, LLVMModuleFlagBehavior, + LLVMTailCallKind, LLVMTypeKind, LLVMUnnamedAddr, + analysis::{LLVMVerifierFailureAction, LLVMVerifyModule}, + core::*, + debuginfo::LLVMSetSubprogram, + error::LLVMGetErrorMessage, + prelude::*, + target::LLVMIntPtrTypeInContext, + transforms::pass_builder::{ + LLVMCreatePassBuilderOptions, LLVMDisposePassBuilderOptions, LLVMPassBuilderOptionsRef, + LLVMRunPasses, + }, +}; +use std::{ffi::CStr, fmt, mem::ManuallyDrop, path::Path, ptr}; + +/// An owned LLVM context. +/// +/// Every other handle in this module belongs to a context and is valid only while the context is +/// alive. +#[derive(Debug)] +pub struct Context { + raw: LLVMContextRef, +} + +impl Context { + /// Creates a new context. + pub(crate) fn create() -> Self { + Self { raw: unsafe { LLVMContextCreate() } } + } + + /// Wraps a raw context, taking ownership of it. + /// + /// # Safety + /// + /// `raw` must be a valid context. Wrap the result in [`ManuallyDrop`] to borrow a context that + /// something else owns. + pub(crate) const unsafe fn new(raw: LLVMContextRef) -> Self { + Self { raw } + } + + /// Returns the raw context. + pub const fn raw(&self) -> LLVMContextRef { + self.raw + } + + /// Creates an instruction builder. + pub(crate) fn create_builder(&self) -> Builder { + Builder { raw: unsafe { LLVMCreateBuilderInContext(self.raw) } } + } + + /// Creates an empty module. + pub(crate) fn create_module(&self, name: &str) -> Module { + let name = to_c_str(name); + Module { raw: unsafe { LLVMModuleCreateWithNameInContext(name.as_ptr(), self.raw) } } + } + + /// The `void` type. + pub(crate) fn void_type(&self) -> Type { + Type(unsafe { LLVMVoidTypeInContext(self.raw) }) + } + + /// The `i1` type. + pub(crate) fn bool_type(&self) -> Type { + self.int_type(1) + } + + /// The `i8` type. + pub(crate) fn i8_type(&self) -> Type { + self.int_type(8) + } + + /// The `i16` type. + pub(crate) fn i16_type(&self) -> Type { + self.int_type(16) + } + + /// The `i32` type. + pub(crate) fn i32_type(&self) -> Type { + self.int_type(32) + } + + /// The `i64` type. + pub(crate) fn i64_type(&self) -> Type { + self.int_type(64) + } + + /// The `i128` type. + pub(crate) fn i128_type(&self) -> Type { + self.int_type(128) + } + + /// The integer type with the given bit width. + pub(crate) fn int_type(&self, bits: u32) -> Type { + Type(unsafe { LLVMIntTypeInContext(self.raw, bits) }) + } + + /// The integer type as wide as a pointer of the given target. + pub(crate) fn ptr_sized_int_type(&self, target_data: &TargetData) -> Type { + Type(unsafe { LLVMIntPtrTypeInContext(self.raw, target_data.as_mut_ptr()) }) + } + + /// The opaque pointer type in the default address space. + pub(crate) fn ptr_type(&self) -> Type { + Type(unsafe { LLVMPointerTypeInContext(self.raw, 0) }) + } + + /// An anonymous struct type. + pub(crate) fn struct_type(&self, fields: &[Type], packed: bool) -> Type { + Type(unsafe { + LLVMStructTypeInContext( + self.raw, + fields.as_ptr().cast_mut().cast(), + fields.len() as u32, + packed as LLVMBool, + ) + }) + } + + /// Appends a basic block to `function`. + pub(crate) fn append_basic_block(&self, function: Function, name: &str) -> Block { + let name = to_c_str(name); + Block(unsafe { LLVMAppendBasicBlockInContext(self.raw, function.0, name.as_ptr()) }) + } + + /// Inserts a basic block right after `block`. + pub(crate) fn insert_basic_block_after(&self, block: Block, name: &str) -> Block { + unsafe { + let next = LLVMGetNextBasicBlock(block.0); + if next.is_null() { + return self.append_basic_block(Function(LLVMGetBasicBlockParent(block.0)), name); + } + let name = to_c_str(name); + Block(LLVMInsertBasicBlockInContext(self.raw, next, name.as_ptr())) + } + } + + /// Creates a metadata string. + pub(crate) fn metadata_string(&self, string: &str) -> Metadata { + let string = to_c_str(string); + Metadata::new(unsafe { + LLVMMDStringInContext2(self.raw, string.as_ptr(), string.to_bytes().len()) + }) + } + + /// Creates a metadata node holding `values`. + pub(crate) fn metadata_node(&self, values: &[Metadata]) -> MetadataNode { + unsafe { + let node = + LLVMMDNodeInContext2(self.raw, values.as_ptr().cast_mut().cast(), values.len()); + MetadataNode(LLVMMetadataAsValue(self.raw, node)) + } + } + + /// Returns the ID of the metadata kind `name`, such as `prof`. + pub(crate) fn get_kind_id(&self, name: &str) -> u32 { + unsafe { LLVMGetMDKindIDInContext(self.raw, name.as_ptr().cast(), name.len() as u32) } + } + + /// Creates an enum attribute. Look the kind up with [`Attribute::get_named_enum_kind_id`]. + pub(crate) fn create_enum_attribute(&self, kind_id: u32, value: u64) -> Attribute { + Attribute(unsafe { LLVMCreateEnumAttribute(self.raw, kind_id, value) }) + } + + /// Creates a string attribute. + pub(crate) fn create_string_attribute(&self, key: &str, value: &str) -> Attribute { + Attribute(unsafe { + LLVMCreateStringAttribute( + self.raw, + key.as_ptr().cast(), + key.len() as u32, + value.as_ptr().cast(), + value.len() as u32, + ) + }) + } + + /// Creates a type attribute, such as `sret`. + pub(crate) fn create_type_attribute(&self, kind_id: u32, ty: Type) -> Attribute { + Attribute(unsafe { LLVMCreateTypeAttribute(self.raw, kind_id, ty.0) }) + } +} + +impl Drop for Context { + fn drop(&mut self) { + unsafe { LLVMContextDispose(self.raw) } + } +} + +/// An owned LLVM module. +#[derive(Debug)] +pub struct Module { + raw: LLVMModuleRef, +} + +impl Module { + /// Wraps a raw module, taking ownership of it. + /// + /// # Safety + /// + /// `raw` must be a valid module that nothing else disposes. Wrap the result in + /// [`ManuallyDrop`] to borrow a module that something else owns. + pub(crate) const unsafe fn new(raw: LLVMModuleRef) -> Self { + Self { raw } + } + + /// Returns the raw module. + pub(crate) const fn as_mut_ptr(&self) -> LLVMModuleRef { + self.raw + } + + /// Releases ownership of the raw module. + pub(crate) fn into_raw(self) -> LLVMModuleRef { + ManuallyDrop::new(self).raw + } + + /// Sets the module identifier. + pub(crate) fn set_name(&self, name: &str) { + unsafe { LLVMSetModuleIdentifier(self.raw, name.as_ptr().cast(), name.len()) } + } + + /// Sets the source file name. + pub(crate) fn set_source_file_name(&self, name: &str) { + unsafe { LLVMSetSourceFileName(self.raw, name.as_ptr().cast(), name.len()) } + } + + /// Sets the data layout string. + pub(crate) fn set_data_layout(&self, data_layout: &CStr) { + unsafe { LLVMSetDataLayout(self.raw, data_layout.as_ptr()) } + } + + /// Sets the target triple. + pub(crate) fn set_triple(&self, triple: &CStr) { + unsafe { LLVMSetTarget(self.raw, triple.as_ptr()) } + } + + /// Adds a module flag holding an integer constant. + pub(crate) fn add_basic_value_flag( + &self, + key: &str, + behavior: LLVMModuleFlagBehavior, + flag: Value, + ) { + unsafe { + LLVMAddModuleFlag( + self.raw, + behavior, + key.as_ptr().cast(), + key.len(), + LLVMValueAsMetadata(flag.0), + ) + } + } + + /// Adds a function declaration. + pub(crate) fn add_function( + &self, + name: &str, + ty: Type, + linkage: Option, + ) -> Function { + let name = to_c_str(name); + let function = Function(unsafe { LLVMAddFunction(self.raw, name.as_ptr(), ty.0) }); + if let Some(linkage) = linkage { + function.set_linkage(linkage); + } + function + } + + /// Returns the function named `name`, if any. + pub(crate) fn get_function(&self, name: &str) -> Option { + let name = to_c_str(name); + Function::new(unsafe { LLVMGetNamedFunction(self.raw, name.as_ptr()) }) + } + + /// Iterates over the functions of the module. + pub(crate) fn get_functions(&self) -> impl Iterator { + let first = Function::new(unsafe { LLVMGetFirstFunction(self.raw) }); + std::iter::successors(first, |f| Function::new(unsafe { LLVMGetNextFunction(f.0) })) + } + + /// Adds a global variable in the default address space. + pub(crate) fn add_global(&self, ty: Type, name: &str) -> Global { + let name = to_c_str(name); + Global(unsafe { LLVMAddGlobal(self.raw, ty.0, name.as_ptr()) }) + } + + /// Checks the module for errors. + pub(crate) fn verify(&self) -> Result<(), LLVMString> { + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMVerifyModule( + self.raw, + LLVMVerifierFailureAction::LLVMReturnStatusAction, + &mut message, + ) + }; + let message = (!message.is_null()).then(|| unsafe { LLVMString::from_raw(message) }); + match message { + Some(message) if failed != 0 => Err(message), + _ => Ok(()), + } + } + + /// Returns the textual IR. + #[cfg(test)] + pub(crate) fn print_to_string(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMPrintModuleToString(self.raw)) } + } + + /// Writes the textual IR to `path`. + pub(crate) fn print_to_file(&self, path: &Path) -> Result<(), LLVMString> { + let path = to_c_str(path.to_str().expect("Did not find a valid Unicode path string")); + let mut message = ptr::null_mut(); + let failed = unsafe { LLVMPrintModuleToFile(self.raw, path.as_ptr(), &mut message) }; + if failed == 0 { + return Ok(()); + } + Err(if message.is_null() { + unsafe { + LLVMString::from_raw(LLVMCreateMessage(c"Error string was uninitialized.".as_ptr())) + } + } else { + unsafe { LLVMString::from_raw(message) } + }) + } + + /// Runs the new-pass-manager pipeline `passes`. + pub(crate) fn run_passes( + &self, + passes: &str, + machine: &TargetMachine, + options: &PassBuilderOptions, + ) -> Result<(), LLVMString> { + let passes = to_c_str(passes); + let error = + unsafe { LLVMRunPasses(self.raw, passes.as_ptr(), machine.as_mut_ptr(), options.raw) }; + if error.is_null() { + return Ok(()); + } + Err(unsafe { LLVMString::from_raw(LLVMGetErrorMessage(error)) }) + } +} + +impl Drop for Module { + fn drop(&mut self) { + unsafe { LLVMDisposeModule(self.raw) } + } +} + +/// Options for [`Module::run_passes`]. +#[derive(Debug)] +pub(crate) struct PassBuilderOptions { + raw: LLVMPassBuilderOptionsRef, +} + +impl PassBuilderOptions { + /// Creates the default options. + pub(crate) fn create() -> Self { + Self { raw: unsafe { LLVMCreatePassBuilderOptions() } } + } +} + +impl Drop for PassBuilderOptions { + fn drop(&mut self) { + unsafe { LLVMDisposePassBuilderOptions(self.raw) } + } +} + +/// An LLVM type. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Type(LLVMTypeRef); + +impl Type { + /// Creates a function type that returns `self`. + pub(crate) fn fn_type(self, params: &[Self], var_args: bool) -> Self { + Self(unsafe { + LLVMFunctionType( + self.0, + params.as_ptr().cast_mut().cast(), + params.len() as u32, + var_args as LLVMBool, + ) + }) + } + + /// Creates an array type of `size` elements of `self`. + pub(crate) fn array_type(self, size: u32) -> Self { + Self(unsafe { LLVMArrayType2(self.0, size as u64) }) + } + + /// The bit width of an integer type. + pub(crate) fn bit_width(self) -> u32 { + unsafe { LLVMGetIntTypeWidth(self.0) } + } + + /// Creates an integer constant of this type. + /// + /// `value` is truncated to the bit width of the type, and sign-extended from it if + /// `sign_extend` is set. + pub(crate) fn const_int(self, mut value: u64, sign_extend: bool) -> Value { + // LLVM 23 no longer truncates implicitly. + let bit_width = self.bit_width(); + if bit_width < u64::BITS { + value &= (1 << bit_width) - 1; + } + Value(unsafe { LLVMConstInt(self.0, value, sign_extend as LLVMBool) }) + } + + /// Creates an integer constant of this type from a hexadecimal string. + pub(crate) fn const_int_from_hex(self, hex: &str) -> Value { + Value(unsafe { + LLVMConstIntOfStringAndSize(self.0, hex.as_ptr().cast(), hex.len() as u32, 16) + }) + } + + /// Creates an array constant with element type `self`. + pub(crate) fn const_array(self, values: &[Value]) -> Value { + Value(unsafe { + LLVMConstArray2(self.0, values.as_ptr().cast_mut().cast(), values.len() as u64) + }) + } + + /// Creates the null constant of this type. + pub(crate) fn const_null(self) -> Value { + Value(unsafe { LLVMConstNull(self.0) }) + } + + /// Returns the textual IR of the type. + pub(crate) fn print_to_string(self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMPrintTypeToString(self.0)) } + } +} + +impl fmt::Debug for Type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.print_to_string().to_string_lossy()) + } +} + +/// An LLVM value: a constant, an argument, or the result of an instruction. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Value(LLVMValueRef); + +impl Value { + /// Returns the type of the value. + pub(crate) fn get_type(self) -> Type { + Type(unsafe { LLVMTypeOf(self.0) }) + } + + /// Sets the name of the value. + pub(crate) fn set_name(self, name: &str) { + unsafe { LLVMSetValueName2(self.0, name.as_ptr().cast(), name.len()) } + } + + /// Sets the alignment of a load. + pub(crate) fn set_alignment(self, align: u32) { + assert!(align.is_power_of_two(), "alignment must be a power of two"); + unsafe { LLVMSetAlignment(self.0, align) } + } + + /// Wraps the value as metadata. + pub(crate) fn as_metadata(self) -> Metadata { + Metadata::new(unsafe { LLVMValueAsMetadata(self.0) }) + } +} + +impl fmt::Debug for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// An LLVM instruction. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Inst(LLVMValueRef); + +impl Inst { + /// Returns the result of the instruction, or `None` if its type is `void`. + pub(crate) fn value(self) -> Option { + let void = unsafe { LLVMGetTypeKind(LLVMTypeOf(self.0)) } == LLVMTypeKind::LLVMVoidTypeKind; + (!void).then_some(Value(self.0)) + } + + /// Sets the alignment of a load or store. + pub(crate) fn set_alignment(self, align: u32) { + assert!(align.is_power_of_two(), "alignment must be a power of two"); + unsafe { LLVMSetAlignment(self.0, align) } + } + + /// Attaches the metadata node `node` of kind `kind_id`. + pub(crate) fn set_metadata(self, node: MetadataNode, kind_id: u32) { + unsafe { LLVMSetMetadata(self.0, kind_id, node.0) } + } + + /// Removes the instruction from its block and deletes it. + pub(crate) fn erase_from_basic_block(self) { + unsafe { LLVMInstructionEraseFromParent(self.0) } + } + + /// Sets the tail call kind of a call. + pub(crate) fn set_tail_call_kind(self, kind: LLVMTailCallKind) { + unsafe { LLVMSetTailCallKind(self.0, kind) } + } + + /// Sets the calling convention of a call. + pub(crate) fn set_call_conv(self, call_conv: u32) { + unsafe { LLVMSetInstructionCallConv(self.0, call_conv) } + } + + /// Adds an attribute to a call. + pub(crate) fn add_call_site_attribute(self, loc: AttributeLoc, attribute: Attribute) { + unsafe { LLVMAddCallSiteAttribute(self.0, loc.index(), attribute.0) } + } +} + +impl fmt::Debug for Inst { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// An LLVM function. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Function(LLVMValueRef); + +impl Function { + fn new(raw: LLVMValueRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + /// Returns the name of the function. + pub(crate) fn get_name(self) -> String { + let mut len = 0; + unsafe { + let name = LLVMGetValueName2(self.0, &mut len); + String::from_utf8_lossy(std::slice::from_raw_parts(name.cast(), len)).into_owned() + } + } + + /// Returns the function type. + pub(crate) fn get_type(self) -> Type { + Type(unsafe { LLVMGlobalGetValueType(self.0) }) + } + + /// Returns the number of parameters. + pub(crate) fn count_params(self) -> u32 { + unsafe { LLVMCountParams(self.0) } + } + + /// Returns the `nth` parameter, if it exists. + pub(crate) fn get_nth_param(self, nth: u32) -> Option { + (nth < self.count_params()).then(|| Value(unsafe { LLVMGetParam(self.0, nth) })) + } + + /// Returns the number of basic blocks. + pub(crate) fn count_basic_blocks(self) -> u32 { + unsafe { LLVMCountBasicBlocks(self.0) } + } + + /// Returns the entry block, if the function has a body. + pub(crate) fn get_first_basic_block(self) -> Option { + Block::new(unsafe { LLVMGetFirstBasicBlock(self.0) }) + } + + /// Sets the linkage. + pub(crate) fn set_linkage(self, linkage: LLVMLinkage) { + unsafe { LLVMSetLinkage(self.0, linkage) } + } + + /// Returns the calling convention. + pub(crate) fn get_call_conv(self) -> u32 { + unsafe { LLVMGetFunctionCallConv(self.0) } + } + + /// Sets the calling convention. + pub(crate) fn set_call_conv(self, call_conv: u32) { + unsafe { LLVMSetFunctionCallConv(self.0, call_conv) } + } + + /// Adds an attribute. + pub(crate) fn add_attribute(self, loc: AttributeLoc, attribute: Attribute) { + unsafe { LLVMAddAttributeAtIndex(self.0, loc.index(), attribute.0) } + } + + /// Attaches the debug info subprogram. + pub(crate) fn set_subprogram(self, subprogram: Metadata) { + unsafe { LLVMSetSubprogram(self.0, subprogram.raw()) } + } + + /// Returns the raw value. + pub(crate) const fn as_value_ref(self) -> LLVMValueRef { + self.0 + } + + /// Deletes the function from its module. + /// + /// # Safety + /// + /// Nothing may use the function afterwards. + pub(crate) unsafe fn delete(self) { + unsafe { LLVMDeleteFunction(self.0) } + } +} + +impl fmt::Debug for Function { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// A global variable. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Global(LLVMValueRef); + +impl Global { + /// Returns the address of the global, a pointer value. + pub(crate) const fn as_pointer_value(self) -> Value { + Value(self.0) + } + + /// Sets the initial value. + pub(crate) fn set_initializer(self, value: Value) { + unsafe { LLVMSetInitializer(self.0, value.0) } + } + + /// Marks the global as constant. + pub(crate) fn set_constant(self, constant: bool) { + unsafe { LLVMSetGlobalConstant(self.0, constant as LLVMBool) } + } + + /// Sets the linkage. + pub(crate) fn set_linkage(self, linkage: LLVMLinkage) { + unsafe { LLVMSetLinkage(self.0, linkage) } + } + + /// Sets whether the address is significant. + pub(crate) fn set_unnamed_address(self, address: LLVMUnnamedAddr) { + unsafe { LLVMSetUnnamedAddress(self.0, address) } + } +} + +/// A basic block. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Block(LLVMBasicBlockRef); + +impl Block { + fn new(raw: LLVMBasicBlockRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + /// Returns the last instruction, if the block is not empty. + pub(crate) fn get_last_instruction(self) -> Option { + let inst = unsafe { LLVMGetLastInstruction(self.0) }; + (!inst.is_null()).then_some(Inst(inst)) + } + + /// Returns the address of the block, or `None` for an entry block or a detached block. + /// + /// # Safety + /// + /// The block must belong to a function that is not deleted. + pub(crate) unsafe fn get_address(self) -> Option { + unsafe { + let parent = LLVMGetBasicBlockParent(self.0); + // Taking the address of the entry block is illegal. + if parent.is_null() || LLVMGetPreviousBasicBlock(self.0).is_null() { + return None; + } + let address = LLVMBlockAddress(parent, self.0); + (!address.is_null()).then_some(Value(address)) + } + } +} + +impl fmt::Debug for Block { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(unsafe { LLVMBasicBlockAsValue(self.0) }, f) + } +} + +/// A metadata node, usable as the operand of an instruction's metadata. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct MetadataNode(LLVMValueRef); + +/// An LLVM attribute. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Attribute(LLVMAttributeRef); + +impl Attribute { + /// Wraps a raw attribute. + pub(crate) const fn new(raw: LLVMAttributeRef) -> Self { + Self(raw) + } + + /// Returns the kind ID of the enum attribute `name`, or 0 if there is none. + pub(crate) fn get_named_enum_kind_id(name: &str) -> u32 { + unsafe { LLVMGetEnumAttributeKindForName(name.as_ptr().cast(), name.len()) } + } +} + +/// The place an attribute applies to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum AttributeLoc { + /// The return value. + Return, + /// The parameter at the given index. + Param(u32), + /// The function itself. + Function, +} + +impl AttributeLoc { + fn index(self) -> u32 { + match self { + Self::Return => 0, + Self::Param(index) => { + assert!(index <= u32::MAX - 2, "param index must be <= u32::MAX - 2"); + index + 1 + } + Self::Function => LLVMAttributeFunctionIndex, + } + } +} + +/// An instruction builder. +#[derive(Debug)] +pub(crate) struct Builder { + raw: LLVMBuilderRef, +} + +macro_rules! binary_ops { + ($($(#[$meta:meta])* $name:ident => $llvm:ident),* $(,)?) => {$( + $(#[$meta])* + pub(crate) fn $name(&self, lhs: Value, rhs: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { $llvm(self.raw, lhs.0, rhs.0, name.as_ptr()) }) + } + )*}; +} + +macro_rules! cast_ops { + ($($(#[$meta:meta])* $name:ident => $llvm:ident),* $(,)?) => {$( + $(#[$meta])* + pub(crate) fn $name(&self, value: Value, ty: Type, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { $llvm(self.raw, value.0, ty.0, name.as_ptr()) }) + } + )*}; +} + +impl Builder { + /// Positions the builder at the end of `block`. + pub(crate) fn position_at_end(&self, block: Block) { + unsafe { LLVMPositionBuilderAtEnd(self.raw, block.0) } + } + + /// Returns the block the builder inserts into. + pub(crate) fn get_insert_block(&self) -> Option { + Block::new(unsafe { LLVMGetInsertBlock(self.raw) }) + } + + /// Sets the debug location attached to new instructions. + pub(crate) fn set_current_debug_location(&self, location: DILocation) { + unsafe { LLVMSetCurrentDebugLocation2(self.raw, location.raw()) } + } + + /// Returns the debug location attached to new instructions, if any. + pub(crate) fn get_current_debug_location(&self) -> Option { + DILocation::new(unsafe { LLVMGetCurrentDebugLocation2(self.raw) }) + } + + /// Stops attaching a debug location to new instructions. + pub(crate) fn unset_current_debug_location(&self) { + unsafe { LLVMSetCurrentDebugLocation2(self.raw, ptr::null_mut()) } + } + + /// Builds a call to `function`. + pub(crate) fn build_call(&self, function: Function, args: &[Value], name: &str) -> Inst { + let fn_ty = function.get_type(); + // LLVM rejects names on calls that return `void`. + let void = unsafe { LLVMGetTypeKind(LLVMGetReturnType(fn_ty.0)) } + == LLVMTypeKind::LLVMVoidTypeKind; + let name = to_c_str(if void { "" } else { name }); + Inst(unsafe { + LLVMBuildCall2( + self.raw, + fn_ty.0, + function.0, + args.as_ptr().cast_mut().cast(), + args.len() as u32, + name.as_ptr(), + ) + }) + } + + /// Builds a stack allocation of `ty`. + pub(crate) fn build_alloca(&self, ty: Type, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildAlloca(self.raw, ty.0, name.as_ptr()) }) + } + + /// Builds a load of `ty` from `ptr`. + pub(crate) fn build_load(&self, ty: Type, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildLoad2(self.raw, ty.0, ptr.0, name.as_ptr()) }) + } + + /// Builds a store of `value` to `ptr`. + pub(crate) fn build_store(&self, ptr: Value, value: Value) -> Inst { + Inst(unsafe { LLVMBuildStore(self.raw, value.0, ptr.0) }) + } + + /// Builds a GEP that stays inside the allocated object. + /// + /// # Safety + /// + /// The address must be in bounds, otherwise the result is poison. + pub(crate) unsafe fn build_in_bounds_gep( + &self, + ty: Type, + ptr: Value, + indexes: &[Value], + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { + LLVMBuildInBoundsGEP2( + self.raw, + ty.0, + ptr.0, + indexes.as_ptr().cast_mut().cast(), + indexes.len() as u32, + name.as_ptr(), + ) + }) + } + + /// Builds a global string constant and returns a pointer to it. + pub(crate) fn build_global_string_ptr(&self, value: &str, name: &str) -> Value { + let value = to_c_str(value); + let name = to_c_str(name); + Value(unsafe { LLVMBuildGlobalString(self.raw, value.as_ptr(), name.as_ptr()) }) + } + + /// Builds an extraction of the field `index` of an aggregate. + pub(crate) fn build_extract_value(&self, aggregate: Value, index: u32, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildExtractValue(self.raw, aggregate.0, index, name.as_ptr()) }) + } + + /// Builds a `phi` of `ty` with the given incoming values. + pub(crate) fn build_phi(&self, ty: Type, incoming: &[(Value, Block)], name: &str) -> Value { + let name = to_c_str(name); + let (mut values, mut blocks): (Vec<_>, Vec<_>) = + incoming.iter().map(|&(value, block)| (value.0, block.0)).unzip(); + unsafe { + let phi = LLVMBuildPhi(self.raw, ty.0, name.as_ptr()); + LLVMAddIncoming(phi, values.as_mut_ptr(), blocks.as_mut_ptr(), incoming.len() as u32); + Value(phi) + } + } + + /// Builds a `select`. + pub(crate) fn build_select( + &self, + cond: Value, + then: Value, + otherwise: Value, + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildSelect(self.raw, cond.0, then.0, otherwise.0, name.as_ptr()) }) + } + + /// Builds an integer comparison. + pub(crate) fn build_int_compare( + &self, + predicate: LLVMIntPredicate, + lhs: Value, + rhs: Value, + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildICmp(self.raw, predicate, lhs.0, rhs.0, name.as_ptr()) }) + } + + /// Builds a comparison of a pointer with null. + pub(crate) fn build_is_null(&self, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildIsNull(self.raw, ptr.0, name.as_ptr()) }) + } + + /// Builds a comparison of a pointer with non-null. + pub(crate) fn build_is_not_null(&self, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildIsNotNull(self.raw, ptr.0, name.as_ptr()) }) + } + + /// Builds a bitwise not. + pub(crate) fn build_not(&self, value: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildNot(self.raw, value.0, name.as_ptr()) }) + } + + /// Builds a right shift, arithmetic if `sign_extend` is set. + pub(crate) fn build_right_shift( + &self, + lhs: Value, + rhs: Value, + sign_extend: bool, + name: &str, + ) -> Value { + if sign_extend { self.build_ashr(lhs, rhs, name) } else { self.build_lshr(lhs, rhs, name) } + } + + binary_ops! { + /// Builds an integer addition. + build_int_add => LLVMBuildAdd, + /// Builds an integer subtraction. + build_int_sub => LLVMBuildSub, + /// Builds an integer multiplication. + build_int_mul => LLVMBuildMul, + /// Builds an unsigned division. + build_int_unsigned_div => LLVMBuildUDiv, + /// Builds a signed division. + build_int_signed_div => LLVMBuildSDiv, + /// Builds an unsigned remainder. + build_int_unsigned_rem => LLVMBuildURem, + /// Builds a signed remainder. + build_int_signed_rem => LLVMBuildSRem, + /// Builds a bitwise or. + build_or => LLVMBuildOr, + /// Builds a bitwise and. + build_and => LLVMBuildAnd, + /// Builds a bitwise xor. + build_xor => LLVMBuildXor, + /// Builds a left shift. + build_left_shift => LLVMBuildShl, + /// Builds a logical right shift. + build_lshr => LLVMBuildLShr, + /// Builds an arithmetic right shift. + build_ashr => LLVMBuildAShr, + } + + cast_ops! { + /// Builds a zero extension to `ty`. + build_int_z_extend => LLVMBuildZExt, + /// Builds a sign extension to `ty`. + build_int_s_extend => LLVMBuildSExt, + /// Builds a truncation to `ty`. + build_int_truncate => LLVMBuildTrunc, + /// Builds an integer to pointer conversion. + build_int_to_ptr => LLVMBuildIntToPtr, + } + + /// Builds a `ret` with no value. + pub(crate) fn build_return(&self, value: Option) -> Inst { + Inst(unsafe { + match value { + Some(value) => LLVMBuildRet(self.raw, value.0), + None => LLVMBuildRetVoid(self.raw), + } + }) + } + + /// Builds a `ret` of an aggregate made of `values`. + pub(crate) fn build_aggregate_return(&self, values: &[Value]) -> Inst { + Inst(unsafe { + LLVMBuildAggregateRet(self.raw, values.as_ptr().cast_mut().cast(), values.len() as u32) + }) + } + + /// Builds an unconditional branch. + pub(crate) fn build_unconditional_branch(&self, dest: Block) -> Inst { + Inst(unsafe { LLVMBuildBr(self.raw, dest.0) }) + } + + /// Builds a conditional branch. + pub(crate) fn build_conditional_branch( + &self, + cond: Value, + then: Block, + otherwise: Block, + ) -> Inst { + Inst(unsafe { LLVMBuildCondBr(self.raw, cond.0, then.0, otherwise.0) }) + } + + /// Builds a `switch` on `value` with the given cases. + pub(crate) fn build_switch( + &self, + value: Value, + default: Block, + cases: &[(Value, Block)], + ) -> Inst { + unsafe { + let switch = LLVMBuildSwitch(self.raw, value.0, default.0, cases.len() as u32); + for &(value, block) in cases { + LLVMAddCase(switch, value.0, block.0); + } + Inst(switch) + } + } + + /// Builds an indirect branch to `address`, which is one of `destinations`. + pub(crate) fn build_indirect_branch(&self, address: Value, destinations: &[Block]) -> Inst { + unsafe { + let branch = LLVMBuildIndirectBr(self.raw, address.0, destinations.len() as u32); + for dest in destinations { + LLVMAddDestination(branch, dest.0); + } + Inst(branch) + } + } + + /// Builds an `unreachable`. + pub(crate) fn build_unreachable(&self) -> Inst { + Inst(unsafe { LLVMBuildUnreachable(self.raw) }) + } +} + +impl Drop for Builder { + fn drop(&mut self) { + unsafe { LLVMDisposeBuilder(self.raw) } + } +} + +fn fmt_value(value: LLVMValueRef, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let string = unsafe { LLVMString::from_raw(LLVMPrintValueToString(value)) }; + f.write_str(&string.to_string_lossy()) +} diff --git a/crates/jit/llvm/src/wrap/mod.rs b/crates/jit/llvm/src/wrap/mod.rs new file mode 100644 index 000000000..efe75ba08 --- /dev/null +++ b/crates/jit/llvm/src/wrap/mod.rs @@ -0,0 +1,35 @@ +//! Small owned wrappers over the `llvm-sys` C API. +//! +//! The wrappers cover only what the backend uses. Handles ([`Type`], [`Value`], [`Block`], ...) are +//! `Copy` pointers into an LLVM context and carry no lifetime: the backend owns the [`Context`] and +//! keeps it alive for as long as any handle exists. + +use std::{ + borrow::Cow, + ffi::{CStr, CString}, +}; + +mod di; +mod ir; +mod string; +mod target; + +pub(crate) use di::*; +pub use ir::*; +pub use string::*; +pub use target::*; + +/// Converts `s` to a C string, truncating it at the first NUL byte. +/// +/// The empty string maps to a static empty C string without allocating. +#[inline] +pub(crate) fn to_c_str(s: &str) -> Cow<'_, CStr> { + if s.is_empty() { + return Cow::Borrowed(c""); + } + match CStr::from_bytes_until_nul(s.as_bytes()) { + Ok(c) => Cow::Borrowed(c), + // SAFETY: There is no NUL byte in `s`, otherwise `from_bytes_until_nul` succeeds. + Err(_) => Cow::Owned(unsafe { CString::from_vec_unchecked(s.as_bytes().to_vec()) }), + } +} diff --git a/crates/jit/llvm/src/wrap/string.rs b/crates/jit/llvm/src/wrap/string.rs new file mode 100644 index 000000000..aec57b8f4 --- /dev/null +++ b/crates/jit/llvm/src/wrap/string.rs @@ -0,0 +1,62 @@ +use llvm_sys::core::LLVMDisposeMessage; +use std::{ + error::Error, + ffi::{CStr, c_char}, + fmt, + ops::Deref, + ptr::NonNull, +}; + +/// An owned, NUL-terminated string allocated by LLVM, such as an error message. +pub struct LLVMString { + ptr: NonNull, +} + +// SAFETY: The string is uniquely owned and never mutated. +unsafe impl Send for LLVMString {} +unsafe impl Sync for LLVMString {} + +impl LLVMString { + /// Takes ownership of a string allocated by LLVM, to be freed with `LLVMDisposeMessage`. + /// + /// # Safety + /// + /// `ptr` must point to a valid NUL-terminated message that nothing else frees. + /// + /// # Panics + /// + /// Panics if `ptr` is null. + pub const unsafe fn from_raw(ptr: *mut c_char) -> Self { + Self { ptr: NonNull::new(ptr).expect("null LLVM string") } + } +} + +impl Deref for LLVMString { + type Target = CStr; + + fn deref(&self) -> &CStr { + // SAFETY: `from_raw` guarantees a valid NUL-terminated string. + unsafe { CStr::from_ptr(self.ptr.as_ptr()) } + } +} + +impl fmt::Debug for LLVMString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} + +impl fmt::Display for LLVMString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.to_string_lossy()) + } +} + +impl Error for LLVMString {} + +impl Drop for LLVMString { + fn drop(&mut self) { + // SAFETY: `from_raw` guarantees the string came from LLVM and is owned by `self`. + unsafe { LLVMDisposeMessage(self.ptr.as_ptr()) } + } +} diff --git a/crates/jit/llvm/src/wrap/target.rs b/crates/jit/llvm/src/wrap/target.rs new file mode 100644 index 000000000..6745fb6f8 --- /dev/null +++ b/crates/jit/llvm/src/wrap/target.rs @@ -0,0 +1,238 @@ +//! Targets, target machines, and code generation. + +use super::{LLVMString, Module, to_c_str}; +use llvm_sys::{ + core::{LLVMDisposeMemoryBuffer, LLVMGetBufferSize, LLVMGetBufferStart}, + prelude::LLVMMemoryBufferRef, + target::*, + target_machine::*, +}; +use std::{ffi::CStr, path::Path, ptr, slice}; + +/// A registered LLVM target. +/// +/// Targets live in a process-wide registry and are never freed. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Target { + raw: LLVMTargetRef, +} + +// SAFETY: The registry entry is immutable and lives for the whole process. +unsafe impl Send for Target {} +unsafe impl Sync for Target {} + +impl Target { + /// Initializes the native target, assembly printer, and disassembler. + pub(crate) fn initialize_native() -> Result<(), String> { + unsafe { + if LLVM_InitializeNativeTarget() == 1 { + return Err("Unknown error in initializing native target".into()); + } + if LLVM_InitializeNativeAsmPrinter() == 1 { + return Err("Unknown error in initializing native asm printer".into()); + } + if LLVM_InitializeNativeDisassembler() == 1 { + return Err("Unknown error in initializing native disassembler".into()); + } + } + Ok(()) + } + + /// Looks up the target for `triple`. + pub(crate) fn from_triple(triple: &CStr) -> Result { + let mut target = ptr::null_mut(); + let mut message = ptr::null_mut(); + let failed = unsafe { LLVMGetTargetFromTriple(triple.as_ptr(), &mut target, &mut message) }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(Self { raw: target }) + } + + /// Returns the name of the target. + pub(crate) fn get_name(&self) -> &CStr { + unsafe { CStr::from_ptr(LLVMGetTargetName(self.raw)) } + } + + /// Returns whether the target supports JIT compilation. + pub(crate) fn has_jit(&self) -> bool { + unsafe { LLVMTargetHasJIT(self.raw) == 1 } + } + + /// Returns whether the target has a target machine. + pub(crate) fn has_target_machine(&self) -> bool { + unsafe { LLVMTargetHasTargetMachine(self.raw) == 1 } + } + + /// Creates a target machine, or `None` if LLVM cannot. + pub(crate) fn create_target_machine( + &self, + triple: &CStr, + cpu: &str, + features: &str, + level: LLVMCodeGenOptLevel, + reloc_mode: LLVMRelocMode, + code_model: LLVMCodeModel, + ) -> Option { + let cpu = to_c_str(cpu); + let features = to_c_str(features); + let machine = unsafe { + LLVMCreateTargetMachine( + self.raw, + triple.as_ptr(), + cpu.as_ptr(), + features.as_ptr(), + level, + reloc_mode, + code_model, + ) + }; + (!machine.is_null()).then_some(TargetMachine { raw: machine }) + } +} + +/// An owned target machine. +#[derive(Debug)] +pub struct TargetMachine { + raw: LLVMTargetMachineRef, +} + +impl TargetMachine { + /// Returns the raw target machine. + pub(crate) const fn as_mut_ptr(&self) -> LLVMTargetMachineRef { + self.raw + } + + /// Returns the default target triple of the host. + pub(crate) fn get_default_triple() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetDefaultTargetTriple()) } + } + + /// Returns the CPU name of the host. + pub(crate) fn get_host_cpu_name() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetHostCPUName()) } + } + + /// Returns the comma-separated CPU features of the host. + pub(crate) fn get_host_cpu_features() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetHostCPUFeatures()) } + } + + /// Returns the target triple of the machine. + pub(crate) fn get_triple(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetTargetMachineTriple(self.raw)) } + } + + /// Returns the CPU name of the machine. + pub(crate) fn get_cpu(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetTargetMachineCPU(self.raw)) } + } + + /// Creates the data layout of the machine. + pub(crate) fn get_target_data(&self) -> TargetData { + TargetData { raw: unsafe { LLVMCreateTargetDataLayout(self.raw) } } + } + + /// Sets whether the assembly output carries comments. + pub(crate) fn set_asm_verbosity(&self, verbose: bool) { + unsafe { LLVMSetTargetMachineAsmVerbosity(self.raw, verbose as i32) } + } + + /// Compiles `module` to assembly or an object file in memory. + pub(crate) fn write_to_memory_buffer( + &self, + module: &Module, + file_type: LLVMCodeGenFileType, + ) -> Result { + let mut buffer = ptr::null_mut(); + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMTargetMachineEmitToMemoryBuffer( + self.raw, + module.as_mut_ptr(), + file_type, + &mut message, + &mut buffer, + ) + }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(MemoryBuffer { raw: buffer }) + } + + /// Compiles `module` to assembly or an object file at `path`. + pub(crate) fn write_to_file( + &self, + module: &Module, + file_type: LLVMCodeGenFileType, + path: &Path, + ) -> Result<(), LLVMString> { + let path = to_c_str(path.to_str().expect("Did not find a valid Unicode path string")); + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMTargetMachineEmitToFile( + self.raw, + module.as_mut_ptr(), + path.as_ptr().cast_mut(), + file_type, + &mut message, + ) + }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(()) + } +} + +impl Drop for TargetMachine { + fn drop(&mut self) { + unsafe { LLVMDisposeTargetMachine(self.raw) } + } +} + +/// An owned target data layout. +#[derive(Debug)] +pub(crate) struct TargetData { + raw: LLVMTargetDataRef, +} + +impl TargetData { + /// Returns the raw target data. + pub(crate) const fn as_mut_ptr(&self) -> LLVMTargetDataRef { + self.raw + } + + /// Returns the data layout string. + pub(crate) fn get_data_layout(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMCopyStringRepOfTargetData(self.raw)) } + } +} + +impl Drop for TargetData { + fn drop(&mut self) { + unsafe { LLVMDisposeTargetData(self.raw) } + } +} + +/// An owned memory buffer, such as the output of [`TargetMachine::write_to_memory_buffer`]. +#[derive(Debug)] +pub(crate) struct MemoryBuffer { + raw: LLVMMemoryBufferRef, +} + +impl MemoryBuffer { + /// Returns the contents of the buffer. + pub(crate) fn as_slice(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(LLVMGetBufferStart(self.raw).cast(), LLVMGetBufferSize(self.raw)) + } + } +} + +impl Drop for MemoryBuffer { + fn drop(&mut self) { + unsafe { LLVMDisposeMemoryBuffer(self.raw) } + } +} diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index 79f502d83..e9c204241 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -61,8 +61,10 @@ def llvm_env(root: str) -> dict[str, str]: evm2-jit-llvm build script otherwise use whichever llvm-config comes first in PATH. """ with open(os.path.join(root, "crates", "jit", "llvm", "Cargo.toml")) as f: - m = re.search(r'"llvm(\d+)-\d+"', f.read()) - llvm_config = m and shutil.which(f"llvm-config-{m[1]}") + # Older code selects LLVM through an inkwell feature, newer code through llvm-sys. + m = re.search(r'"llvm(\d+)-\d+"|^llvm-sys = "(\d+)\d"', f.read(), re.M) + version = m and (m[1] or m[2]) + llvm_config = version and shutil.which(f"llvm-config-{version}") if not llvm_config: return {} bindir = subprocess.run( From 6eac19bef63b8203175e08e5e20a3f1dfc8d3656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Duchesneau?= Date: Fri, 30 Oct 2026 11:07:15 -0400 Subject: [PATCH 032/159] feat(evm): add opt-in inspection of system calls (#888) Currently, system calls run without the active inspector, so tracers cannot see the calls, logs and state changes of EIP-2935, EIP-4788, EIP-7002 and EIP-7251 system calls. With these changes, tracers can opt in via Evm::set_inspect_system_calls (the default stays uninspected). --- crates/evm2/src/evm/mod.rs | 16 +++++++ crates/evm2/src/evm/system.rs | 80 ++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 7c4a0c2aa..45fccacaf 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -223,6 +223,7 @@ pub struct Evm<'a, T: EvmTypesHost> { interpreter_pool: InterpreterPool, #[derive_where(skip)] inspector: Option>, + inspect_system_calls: bool, #[derive_where(skip)] interpreter_runner: Option>>, /// The currently running interpreter frame, if any. @@ -352,6 +353,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { precompiles, interpreter_pool: InterpreterPool::new(), inspector: None, + inspect_system_calls: false, interpreter_runner: None, current_frame: None, frame_checkpoint: None, @@ -996,6 +998,20 @@ impl<'a, T: EvmTypes> Evm<'a, T> { Some(unsafe { Box::from_raw(Box::into_raw(i).cast::()) }) } + /// Returns whether [`Self::system_call`] runs with the active inspector installed. + #[inline] + pub const fn inspects_system_calls(&self) -> bool { + self.inspect_system_calls + } + + /// Sets whether [`Self::system_call`] runs with the active inspector installed. + /// + /// Defaults to `false`: system calls are not inspected. + #[inline] + pub const fn set_inspect_system_calls(&mut self, inspect: bool) { + self.inspect_system_calls = inspect; + } + /// Sets the optional external interpreter runner. #[inline] pub fn set_interpreter_runner>(&mut self, runner: R) { diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index a0d0a64b3..bb8f3de83 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -117,11 +117,17 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// /// The target system contract bytecode must already be present in state. This method does not /// deploy protocol system contracts or synthesize their bytecode. + /// + /// The active inspector observes the call only when [`Evm::set_inspect_system_calls`] is set. pub fn system_call(&mut self, tx: SystemTx) -> HandlerResult> { - // System calls are not inspected. - let inspector = self.inspector.take(); - let result = self.execute_system_call(tx); - self.inspector = inspector; + let result = if self.inspect_system_calls { + self.execute_system_call(tx) + } else { + let inspector = self.inspector.take(); + let result = self.execute_system_call(tx); + self.inspector = inspector; + result + }; match result { Ok(outcome) => self.finish_executed_tx(outcome), Err(error) => { @@ -240,11 +246,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { mod tests { use super::*; use crate::{ - BaseEvmTypes, Precompiles, SpecId, + BaseEvmTypes, Inspector, Precompiles, SpecId, bytecode::Bytecode, env::BlockEnvExt, evm::{AccountInfo, InMemoryDB}, - interpreter::{GasTracker, InstrStop, Message, op}, + interpreter::{GasTracker, InstrStop, Interpreter, Message, MessageResult, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileResult}, registry::{HandlerError, TxRegistry}, }; @@ -555,4 +561,66 @@ mod tests { assert_eq!(result.total_gas_spent, regular_gas + state_gas); assert_eq!(result.execution_gas_spent(), regular_gas); } + + #[derive(Debug, Default)] + struct CountingInspector { + calls: usize, + call_ends: usize, + steps: usize, + } + + impl Inspector for CountingInspector { + fn step(&mut self, _interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + self.steps += 1; + } + + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + _message: &mut Message, + ) -> Option> { + self.calls += 1; + None + } + + fn call_end( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + _message: &Message, + _result: &mut MessageResult, + ) { + self.call_ends += 1; + } + } + + #[rstest::rstest] + #[case(false, 0, 0)] + #[case(true, 1, 3)] + fn system_call_inspection_is_opt_in( + #[case] inspect: bool, + #[case] expected_calls: usize, + #[case] expected_steps: usize, + ) { + let contract = Address::from([0x42; 20]); + let code = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 1, op::PUSH0, op::STOP])); + let mut database = InMemoryDB::default(); + database.insert_account_info(&contract, AccountInfo::default().with_code(code)); + let mut evm = TestEvm::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + database, + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(CountingInspector::default()); + evm.set_inspect_system_calls(inspect); + + let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); + assert!(result.result.status); + + let inspector = evm.clear_inspector_as::().expect("inspector kept"); + assert_eq!(inspector.calls, expected_calls); + assert_eq!(inspector.call_ends, expected_calls); + assert_eq!(inspector.steps, expected_steps); + } } From 6e15c976acb22e83aef1698da207a0c951191900 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:31:12 +0100 Subject: [PATCH 033/159] perf(cli): use B256Map for benchmark JIT function lookup (#894) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `evm2 run` harness and the criterion JIT bench look compiled functions up in a std `HashMap` on every frame, so each frame pays a SipHash (268 instructions under callgrind: 172 in `Sip13Rounds` write, 96 in `RandomState::hash_one`). This switches both maps to `alloy_primitives::map::B256Map`, which hashes with the key's own bytes. It only affects the harnesses: the production `JitInterpreterRunner` never used SipHash. The compiler cache in the criterion bench is keyed by `(B256, SpecId)` and is read once per bench load, so it stays as is. ## Benchmarks Codegen is unchanged (`opt.s` changed in 0/31). Runtime instructions per iteration drop by 1.3% (geomean) and cycles by 1.5% on the small benches, where the per-frame lookup is a visible share; the large benches (`TOTAL`) do not move. A self-call microbenchmark (K nested calls per transaction, `evm2 run --jit-only`, profiling build, base minus head) goes from 2090 to 1838 instructions and from 723 to 678 cycles per nested call; a one-opcode STOP contract goes from 14040 to 13788 instructions per transaction. Head `63e1619378` vs base `origin/main` (`6eac19bef6`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **+0.2% 🔴** | **+0.2% 🔴** | | **GEOMEAN** | **=** | **+0.2% 🔴** | **+1.3% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.23M
37.22M
= | 31.90M
31.95M
+0.1% 🔴 | 17.0ms
17.1ms
+0.7% 🔴 | 206.0µs
225.8µs
+9.6% 🔴 | 497.9µs
531.1µs
+6.7% 🔴 | 1.53ms
1.66ms
+8.4% 🔴 | 3.31ms
3.31ms
+0.2% 🔴 | | factorial | 41.43M
41.43M
= | 34.04M
34.00M
-0.1% 🟢 | 16.9ms
17.5ms
+3.1% 🔴 | 222.2µs
221.9µs
-0.2% 🟢 | 511.7µs
538.6µs
+5.3% 🔴 | 1.55ms
1.71ms
+10.7% 🔴 | 3.71ms
3.77ms
+1.7% 🔴 | | counter | 53.95M
53.95M
= | 35.36M
35.70M
+1.0% 🔴 | 12.0ms
12.7ms
+5.6% 🔴 | 447.7µs
453.5µs
+1.3% 🔴 | 666.4µs
687.2µs
+3.1% 🔴 | 1.91ms
1.96ms
+2.9% 🔴 | 4.80ms
4.83ms
+0.7% 🔴 | | snailtracer | 5.28G
5.28G
= | 2.36G
2.37G
+0.6% 🔴 | 592.8ms
594.7ms
+0.3% 🔴 | 52.3ms
53.0ms
+1.4% 🔴 | 9.23ms
9.21ms
-0.3% 🟢 | 105.1ms
105.7ms
+0.6% 🔴 | 474.8ms
475.0ms
+0.1% 🔴 | | weth | 538.48M
538.66M
= | 260.96M
260.98M
= | 68.6ms
69.2ms
+0.9% 🔴 | 3.27ms
3.31ms
+1.2% 🔴 | 3.04ms
3.01ms
-1.0% 🟢 | 15.6ms
15.8ms
+1.2% 🔴 | 47.1ms
47.1ms
-0.1% 🟢 | | hash_10k | 80.19M
80.19M
= | 47.49M
47.70M
+0.4% 🔴 | 15.2ms
15.7ms
+3.8% 🔴 | 396.4µs
412.0µs
+3.9% 🔴 | 702.0µs
716.7µs
+2.1% 🔴 | 2.53ms
2.66ms
+5.1% 🔴 | 6.72ms
6.72ms
-0.1% 🟢 | | erc20_transfer | 576.91M
576.52M
-0.1% 🟢 | 280.85M
281.44M
+0.2% 🔴 | 73.8ms
74.8ms
+1.4% 🔴 | 3.45ms
3.51ms
+1.5% 🔴 | 3.10ms
3.11ms
+0.6% 🔴 | 17.0ms
17.1ms
+0.3% 🔴 | 50.8ms
50.6ms
-0.4% 🟢 | | push0_proxy | 34.44M
34.49M
+0.2% 🔴 | 28.56M
28.73M
+0.6% 🔴 | 15.6ms
16.1ms
+3.2% 🔴 | 188.4µs
212.9µs
+13.0% 🔴 | 494.7µs
524.4µs
+6.0% 🔴 | 1.16ms
1.30ms
+12.0% 🔴 | 2.76ms
2.87ms
+4.2% 🔴 | | usdc_proxy | 243.76M
243.83M
= | 126.91M
126.98M
+0.1% 🔴 | 35.1ms
35.8ms
+2.2% 🔴 | 2.00ms
2.05ms
+2.6% 🔴 | 1.82ms
1.93ms
+6.4% 🔴 | 8.23ms
8.36ms
+1.5% 🔴 | 21.2ms
21.0ms
-0.6% 🟢 | | fiat_token | 3.98G
3.97G
= | 1.86G
1.85G
-0.3% 🟢 | 466.2ms
465.8ms
-0.1% 🟢 | 21.6ms
21.5ms
-0.7% 🟢 | 16.4ms
16.2ms
-1.2% 🟢 | 102.6ms
102.8ms
+0.2% 🔴 | 364.5ms
365.9ms
+0.4% 🔴 | | uniswap_v2_pair | 2.05G
2.05G
= | 971.35M
972.71M
+0.1% 🔴 | 247.0ms
247.5ms
+0.2% 🔴 | 11.5ms
12.1ms
+5.3% 🔴 | 9.31ms
10.0ms
+7.5% 🔴 | 50.4ms
50.6ms
+0.5% 🔴 | 193.0ms
192.4ms
-0.3% 🟢 | | univ2_router | 4.38G
4.38G
= | 2.07G
2.08G
+0.1% 🔴 | 521.5ms
521.7ms
= | 23.9ms
23.9ms
-0.2% 🟢 | 17.6ms
17.2ms
-2.2% 🟢 | 104.7ms
105.2ms
+0.5% 🔴 | 422.0ms
421.4ms
-0.1% 🟢 | | seaport | 9.73G
9.73G
= | 4.91G
4.93G
+0.4% 🔴 | 1.226s
1.232s
+0.5% 🔴 | 41.1ms
41.4ms
+0.7% 🔴 | 29.7ms
29.3ms
-1.2% 🟢 | 260.7ms
262.3ms
+0.6% 🔴 | 1.029s
1.030s
+0.1% 🔴 | | airdrop | 1.42G
1.42G
= | 676.69M
679.40M
+0.4% 🔴 | 172.7ms
174.0ms
+0.8% 🔴 | 7.25ms
7.43ms
+2.4% 🔴 | 6.24ms
6.85ms
+9.8% 🔴 | 43.1ms
43.5ms
+0.8% 🔴 | 126.1ms
126.6ms
+0.4% 🔴 | | bswap64 | 66.76M
66.81M
+0.1% 🔴 | 40.06M
39.70M
-0.9% 🟢 | 13.2ms
13.8ms
+4.2% 🔴 | 679.9µs
705.7µs
+3.8% 🔴 | 842.5µs
842.6µs
= | 2.14ms
2.16ms
+1.1% 🔴 | 5.04ms
5.01ms
-0.6% 🟢 | | bswap64_opt | 73.46M
73.52M
+0.1% 🔴 | 42.49M
42.68M
+0.4% 🔴 | 13.9ms
14.5ms
+4.0% 🔴 | 623.4µs
619.3µs
-0.7% 🟢 | 776.8µs
805.5µs
+3.7% 🔴 | 2.28ms
2.40ms
+5.4% 🔴 | 5.59ms
5.61ms
+0.3% 🔴 | | eip4788 | 48.35M
48.37M
= | 32.66M
32.72M
+0.2% 🔴 | 11.4ms
12.0ms
+4.9% 🔴 | 290.1µs
309.9µs
+6.9% 🔴 | 580.1µs
613.0µs
+5.7% 🔴 | 1.52ms
1.64ms
+7.8% 🔴 | 4.49ms
4.49ms
= | | eip2935 | 42.16M
42.12M
-0.1% 🟢 | 34.10M
33.95M
-0.4% 🟢 | 17.4ms
17.7ms
+1.4% 🔴 | 245.4µs
276.2µs
+12.5% 🔴 | 522.7µs
575.9µs
+10.2% 🔴 | 1.30ms
1.46ms
+11.9% 🔴 | 3.79ms
3.89ms
+2.5% 🔴 | | burntpix | 6.70G
6.70G
= | 3.12G
3.12G
+0.1% 🔴 | 772.9ms
773.9ms
+0.1% 🔴 | 21.1ms
21.7ms
+2.9% 🔴 | 2.54ms
2.68ms
+5.2% 🔴 | 14.4ms
14.5ms
+0.9% 🔴 | 41.5ms
41.8ms
+0.6% 🔴 | | curve_stableswap | 1.14G
1.14G
= | 544.11M
545.87M
+0.3% 🔴 | 138.7ms
139.9ms
+0.9% 🔴 | 4.93ms
5.03ms
+1.9% 🔴 | 3.85ms
3.84ms
-0.2% 🟢 | 30.4ms
30.6ms
+0.9% 🔴 | 72.6ms
72.5ms
-0.1% 🟢 | | onchain_lm_v2 | 3.66G
3.66G
= | 1.63G
1.63G
-0.3% 🟢 | 413.0ms
411.5ms
-0.4% 🟢 | 16.0ms
16.0ms
-0.2% 🟢 | 11.2ms
11.5ms
+3.1% 🔴 | 72.2ms
72.8ms
+0.8% 🔴 | 285.1ms
285.3ms
+0.1% 🔴 | | usdt | 1.34G
1.34G
+0.1% 🔴 | 626.51M
628.63M
+0.3% 🔴 | 160.9ms
163.0ms
+1.3% 🔴 | 10.1ms
10.1ms
+0.4% 🔴 | 7.99ms
7.99ms
= | 36.7ms
37.1ms
+1.3% 🔴 | 117.0ms
117.9ms
+0.8% 🔴 | | universal_router | 11.56G
11.56G
= | 5.58G
5.60G
+0.2% 🔴 | 1.379s
1.380s
+0.1% 🔴 | 21.0ms
21.4ms
+1.9% 🔴 | 17.2ms
17.6ms
+1.9% 🔴 | 122.4ms
122.5ms
= | 475.2ms
477.4ms
+0.5% 🔴 | | aggregation_router_v6 | 35.12G
35.11G
= | 16.84G
16.88G
+0.2% 🔴 | 4.123s
4.126s
+0.1% 🔴 | 34.8ms
34.0ms
-2.2% 🟢 | 28.5ms
27.7ms
-2.5% 🟢 | 209.0ms
208.6ms
-0.2% 🟢 | 787.5ms
786.6ms
-0.1% 🟢 | | entry_point_v0_7 | 17.88G
17.89G
= | 8.63G
8.67G
+0.5% 🔴 | 2.129s
2.138s
+0.4% 🔴 | 16.3ms
16.5ms
+1.2% 🔴 | 15.0ms
15.0ms
+0.5% 🔴 | 97.1ms
96.0ms
-1.1% 🟢 | 411.6ms
411.1ms
-0.1% 🟢 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.98G
2.99G
+0.5% 🔴 | 746.0ms
750.6ms
+0.6% 🔴 | 33.6ms
33.8ms
+0.5% 🔴 | 27.0ms
25.7ms
-4.9% 🟢 | 167.9ms
168.2ms
+0.2% 🔴 | 601.3ms
602.4ms
+0.2% 🔴 | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 3.19G
3.19G
-0.1% 🟢 | 798.8ms
799.0ms
= | 24.8ms
24.5ms
-1.5% 🟢 | 23.0ms
21.0ms
-8.5% 🟢 | 163.1ms
162.2ms
-0.5% 🟢 | 592.2ms
594.7ms
+0.4% 🔴 | | curve_3pool | 6.59G
6.59G
= | 3.01G
3.01G
+0.1% 🔴 | 753.5ms
754.5ms
+0.1% 🔴 | 33.4ms
33.6ms
+0.5% 🔴 | 21.9ms
21.8ms
-0.7% 🟢 | 129.7ms
129.9ms
+0.2% 🔴 | 658.7ms
655.3ms
-0.5% 🟢 | | curve_tricrypto2 | 9.40G
9.40G
= | 4.23G
4.24G
+0.2% 🔴 | 1.051s
1.054s
+0.3% 🔴 | 30.9ms
30.9ms
-0.1% 🟢 | 25.0ms
25.3ms
+1.0% 🔴 | 145.5ms
145.6ms
+0.1% 🔴 | 639.9ms
637.7ms
-0.3% 🟢 | | balancer_vault | 19.01G
19.03G
+0.1% 🔴 | 9.50G
9.51G
= | 2.340s
2.340s
= | 43.9ms
44.0ms
+0.2% 🔴 | 31.8ms
30.6ms
-3.9% 🟢 | 302.1ms
301.6ms
-0.1% 🟢 | 976.7ms
989.8ms
+1.3% 🔴 | | succinct_staking | 10.84G
10.84G
= | 5.16G
5.16G
+0.2% 🔴 | 1.272s
1.275s
+0.2% 🔴 | 31.5ms
30.7ms
-2.5% 🟢 | 20.0ms
18.3ms
-8.5% 🟢 | 130.0ms
130.2ms
+0.1% 🔴 | 477.9ms
482.4ms
+0.9% 🔴 | | **TOTAL** | **164.82G
164.84G
=** | **78.89G
79.06G
+0.2% 🔴** | **19.614s
19.658s
+0.2% 🔴** | **492.1ms
493.8ms
+0.3% 🔴** | **336.8ms
331.6ms
-1.6% 🟢** | **2.344s
2.348s
+0.2% 🔴** | **8.906s
8.926s
+0.2% 🔴** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|--------------------:|--------------:| | fibonacci-calldata | -1.0% 🟢 | -1.6% 🟢 | | counter | -2.5% 🟢 | -1.0% 🟢 | | usdc_proxy | -2.9% 🟢 | -3.2% 🟢 | | fiat_token | -2.5% 🟢 | -2.9% 🟢 | | uniswap_v2_pair | -0.9% 🟢 | -6.9% 🟢 | | seaport | -1.5% 🟢 | -3.6% 🟢 | | airdrop | -1.8% 🟢 | -1.9% 🟢 | | bswap64 | -3.2% 🟢 | -2.7% 🟢 | | bswap64_opt | -2.6% 🟢 | -1.9% 🟢 | | eip4788 | -1.6% 🟢 | -1.2% 🟢 | | eip2935 | -1.5% 🟢 | -2.2% 🟢 | | usdt | -1.5% 🟢 | -0.7% 🟢 | | universal_router | -2.4% 🟢 | -2.2% 🟢 | | aggregation_router_v6 | -3.5% 🟢 | -2.3% 🟢 | | uniswap_v4_pool_manager | -1.2% 🟢 | -1.8% 🟢 | | curve_tricrypto2 | -2.1% 🟢 | -1.4% 🟢 | | succinct_staking | -2.8% 🟢 | -1.2% 🟢 | | **TOTAL** | **=** | **+0.2% 🔴** | | **GEOMEAN** | **-1.3% 🟢** | **-1.5% 🟢** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 43.62K
43.17K
-1.0% 🟢 | 12.19K
12.00K
-1.6% 🟢 | | factorial | 79.18K
78.84K
-0.4% 🟢 | 23.31K
23.12K
-0.8% 🟢 | | counter | 23.70K
23.11K
-2.5% 🟢 | 9.50K
9.40K
-1.0% 🟢 | | snailtracer | 202.64M
202.62M
= | 73.50M
73.31M
-0.3% 🟢 | | weth | 28.31K
28.09K
-0.8% 🟢 | 11.73K
11.54K
-1.7% 🟢 | | hash_10k | 8.83M
8.83M
= | 3.06M
3.04M
-0.9% 🟢 | | erc20_transfer | 20.73M
20.73M
= | 7.38M
7.24M
-1.9% 🟢 | | push0_proxy | 22.42K
22.50K
+0.3% 🔴 | 9.51K
9.40K
-1.1% 🟢 | | usdc_proxy | 24.72K
23.99K
-2.9% 🟢 | 10.38K
10.05K
-3.2% 🟢 | | fiat_token | 22.46K
21.91K
-2.5% 🟢 | 9.29K
9.02K
-2.9% 🟢 | | uniswap_v2_pair | 22.87K
22.67K
-0.9% 🟢 | 9.83K
9.15K
-6.9% 🟢 | | univ2_router | 20.31K
20.12K
-0.9% 🟢 | 8.75K
8.60K
-1.7% 🟢 | | seaport | 22.95K
22.60K
-1.5% 🟢 | 9.52K
9.18K
-3.6% 🟢 | | airdrop | 22.82K
22.41K
-1.8% 🟢 | 9.48K
9.29K
-1.9% 🟢 | | bswap64 | 19.93K
19.29K
-3.2% 🟢 | 8.30K
8.07K
-2.7% 🟢 | | bswap64_opt | 19.52K
19.00K
-2.6% 🟢 | 8.05K
7.90K
-1.9% 🟢 | | eip4788 | 24.09K
23.70K
-1.6% 🟢 | 10.10K
9.98K
-1.2% 🟢 | | eip2935 | 22.89K
22.55K
-1.5% 🟢 | 9.30K
9.09K
-2.2% 🟢 | | burntpix | 104.08M
104.07M
= | 36.79M
37.01M
+0.6% 🔴 | | curve_stableswap | 263.23K
262.28K
-0.4% 🟢 | 123.11K
123.40K
+0.2% 🔴 | | onchain_lm_v2 | 521.40M
521.40M
= | 252.95M
253.77M
+0.3% 🔴 | | usdt | 40.27K
39.68K
-1.5% 🟢 | 16.90K
16.78K
-0.7% 🟢 | | universal_router | 154.00K
150.34K
-2.4% 🟢 | 63.68K
62.27K
-2.2% 🟢 | | aggregation_router_v6 | 491.74K
474.33K
-3.5% 🟢 | 211.86K
207.02K
-2.3% 🟢 | | entry_point_v0_7 | 1.29M
1.29M
-0.2% 🟢 | 398.64K
398.99K
+0.1% 🔴 | | uniswap_v3_pool | 87.22K
86.76K
-0.5% 🟢 | 36.25K
35.99K
-0.7% 🟢 | | uniswap_v4_pool_manager | 69.81K
68.99K
-1.2% 🟢 | 24.71K
24.28K
-1.8% 🟢 | | curve_3pool | 82.98K
82.83K
-0.2% 🟢 | 32.60K
32.56K
-0.1% 🟢 | | curve_tricrypto2 | 225.83K
220.98K
-2.1% 🟢 | 95.76K
94.41K
-1.4% 🟢 | | balancer_vault | 124.67K
123.75K
-0.7% 🟢 | 48.87K
48.87K
= | | succinct_staking | 65.47K
63.64K
-2.8% 🟢 | 28.60K
28.25K
-1.2% 🟢 | | **TOTAL** | **861.00M
860.93M
=** | **374.92M
375.59M
+0.2% 🔴** |
--- crates/cli/benches/evm/jit.rs | 8 ++++---- crates/cli/src/run.rs | 15 +++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index 3d2e857be..3e682b3c9 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -1,5 +1,5 @@ use crate::fixture::Suites; -use alloy_primitives::{B256, hex}; +use alloy_primitives::{B256, hex, map::B256Map}; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::{ BaseEvmTypes, Evm, InterpreterRunner, Precompiles, SpecId, @@ -60,7 +60,7 @@ pub(crate) struct PreparedBench { db: InMemoryDB, tx: RecoveredTxEnvelope, entry_bytecode: Option, - functions: Arc>, + functions: Arc>, } impl PreparedBench { @@ -81,7 +81,7 @@ impl PreparedBench { return None; } - let mut functions = HashMap::new(); + let mut functions = B256Map::default(); for account in accounts { let func = *compiler.functions.entry((account.code_hash, spec)).or_insert_with(|| { let name = format!("contract_{}_{spec:?}", hex::encode(account.code_hash)); @@ -199,7 +199,7 @@ impl Runner { #[derive(Clone, Debug)] struct FixedJitRunner { - functions: Arc>, + functions: Arc>, } impl InterpreterRunner for FixedJitRunner { diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index af5c7a3e3..e9b79ea1a 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -1,5 +1,5 @@ use alloy_consensus::{TxLegacy, transaction::Recovered}; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, hex, keccak256}; +use alloy_primitives::{Address, B256, Bytes, TxKind, U256, hex, keccak256, map::B256Map}; use clap::ValueEnum; use evm2::{ BaseEvmTypes, Evm, InterpreterRunner, Precompiles, SpecId, Version, @@ -18,7 +18,6 @@ use evm2_jit_runtime::{ }; use std::{ borrow::Cow, - collections::HashMap, hint::black_box, io::{Read, Write}, path::{Path, PathBuf}, @@ -243,7 +242,7 @@ impl RunArgs { } } else if self.load.is_none() { let func = unsafe { compiler.jit_function(f_id)? }; - let mut functions = HashMap::new(); + let mut functions = B256Map::default(); functions.insert(keccak256(&bytecode), func); compiler.clear_ir()?; pending_jit = Some((compiler, functions)); @@ -538,7 +537,7 @@ struct PreparedBench { block: BlockEnv, db: InMemoryDB, tx: RecoveredTxEnvelope, - functions: Arc>, + functions: Arc>, assert_success: bool, } @@ -547,14 +546,14 @@ type LlvmCompiler = EvmCompiler; impl PreparedBench { fn load(bench: &Bench) -> eyre::Result<(Self, LlvmCompiler)> { let mut compiler = EvmCompiler::new_llvm(false)?; - let prepared = Self::load_with_functions(bench, &mut compiler, HashMap::new())?; + let prepared = Self::load_with_functions(bench, &mut compiler, B256Map::default())?; Ok((prepared, compiler)) } fn load_with_functions( bench: &Bench, compiler: &mut LlvmCompiler, - mut functions: HashMap, + mut functions: B256Map, ) -> eyre::Result { let (name, accounts, block, db, tx) = parse_bench(bench)?; for account in &accounts { @@ -587,7 +586,7 @@ impl PreparedBench { ) -> eyre::Result<(Self, libloading::Library)> { let (name, accounts, block, db, tx) = parse_bench(bench)?; let lib = unsafe { libloading::Library::new(lib_path) }?; - let mut functions = HashMap::new(); + let mut functions = B256Map::default(); for account in &accounts { if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { continue; @@ -658,7 +657,7 @@ impl PreparedBench { #[derive(Clone, Debug)] struct FixedJitRunner { - functions: Arc>, + functions: Arc>, } impl InterpreterRunner for FixedJitRunner { From d90029650d0fde1ee28ffe8ad61c2297cebd7fcb Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:35:07 +0100 Subject: [PATCH 034/159] fix(inspectors): report a selfdestructed account's slots as before the wipe in prestate mode (#889) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In non-diff mode the prestate tracer reported the pre-transaction value of every storage slot of an account that selfdestructed in the transaction as `0x0`, on forks where SELFDESTRUCT still wipes storage (before Cancun, or an account created in the same transaction after). #881 fixed diff mode only. reth serves `debug_traceTransaction` through this code, and the user's example shows it: mainnet block 4501969, the Parity multisig library kill, tx `0x47f7cff7a5e671884629c93b368cb18f58a993f4b19c2a53a8662e3f1482f690`. Slot `0x64dc844c661b03668fecb6167617b24b62f1a2e695cbeaa5bd88687febd241e0` of the library held `2` before the transaction and the tracer printed `0x0`. The wipe happens when the transaction's state is finalized: `Account::wipe` (`crates/evm2/src/evm/state/storage.rs:305`) calls `zero_loaded` (`:315-317`), which sets every loaded slot's `current` to zero, and the change stream then reports the now-unchanged slot with `sink.storage_read(address, key, value.current)` (`crates/evm2/src/evm/state/stream.rs:93`). The trace builder's `TxState` stores that zero as the slot's original value, so `slot.original` is zero for every slot of a destroyed account. geth never sees this: its tracer reads a slot with `StateDB.GetState` when the opcode runs, before the transaction's state is finalized (`lookupStorage`, `eth/tracers/native/prestate.go:365-373`, called from `OnOpcode` at `:132`). The builder now reads the slots of a selfdestructed account from the pre-transaction database, as diff mode does since #881 (`crates/inspectors/src/tracing/builder/geth.rs:442`). I reproduced it on the real transaction. geth is `evm t8n --state.fork Byzantium` at go-ethereum `a0cba163` with the library's code, balance, nonce and the seven slots the transaction touched taken from `ethereum.reth.rs` (archive `debug_traceTransaction` and `eth_getStorageAt` at block 4501968, which serve the same zeros as the bug in non-diff mode). Slot `…d241e0`, library `0x863df6bf…`: | mode | geth | `main` | this PR | | :-- | :-- | :-- | :-- | | non-diff, slot `…d241e0` | `0x2` | `0x0` 🔴 | `0x2` 🟢 | | non-diff, the six other slots | `0x0` | `0x0` | `0x0` | | diff, `pre` slot `…d241e0` | `0x2` | `0x2` | `0x2` | The regression test runs a London transaction whose contract reads slot 1 (`0x2a`) and slot 2 (zero) and selfdestructs, which geth reports as `0x2a` and `0x0` and `main` reported as `0x0` and `0x0`. The existing diff test shares its setup. --- crates/inspectors/src/tracing/builder/geth.rs | 9 +++- crates/inspectors/tests/it/geth.rs | 43 ++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 432ac173f..5034a49c6 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -437,7 +437,14 @@ impl<'a> GethTraceBuilder<'a> { // insert the original value of all loaded storage slots if storage_enabled { for (key, slot) in loaded_acc.storage.iter() { - acc_state.storage.insert((*key).into(), slot.original.into()); + // The state stream reports the slots of a selfdestructed account after the + // wipe, as zero. geth reads a slot when the opcode runs, so use the database. + let original = if loaded_acc.selfdestructed { + db.get_storage(&addr, key)? + } else { + slot.original + }; + acc_state.storage.insert((*key).into(), original.into()); } } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 5cb727f08..6fd477f2f 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -2653,13 +2653,11 @@ fn test_geth_prestate_diff_reports_cleared_code() { ); } -/// Geth keeps the pre state of a selfdestructed account whole, with every slot the transaction -/// read, including unchanged and zero ones. -#[test] -fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { +/// Runs a London transaction that reads slots 1 (holds `0x2a`) and 2 (zero) of a contract and +/// selfdestructs it, and returns the contract and the prestate frame. +fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreStateFrame) { let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); let contract = address!("1111111111111111111111111111111111111111"); - let beneficiary = address!("3333333333333333333333333333333333333333"); // SLOAD 1, SLOAD 2, SELFDESTRUCT to the beneficiary. let code = Bytes::from(hex!("6001545060025450733333333333333333333333333333333333333333ff")); @@ -2684,7 +2682,6 @@ fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .build_mainnet(); - let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; let mut insp = TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); let mut db = evm.ctx.db.clone(); @@ -2704,6 +2701,16 @@ fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { .geth_builder() .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) .unwrap(); + (contract, frame) +} + +/// Geth keeps the pre state of a selfdestructed account whole, with every slot the transaction +/// read, including unchanged and zero ones. +#[test] +fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { + let beneficiary = address!("3333333333333333333333333333333333333333"); + let (contract, frame) = + trace_london_selfdestruct(PreStateConfig { diff_mode: Some(true), ..Default::default() }); let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; // Output of geth's `evm t8n --state.fork London` with the prestate tracer in diff mode. @@ -2724,3 +2731,27 @@ fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { }) ); } + +/// geth reads a slot before the transaction runs, so the pre state of a contract that selfdestructs +/// reports the value the slot held, not the zero its storage wipe leaves behind. +#[test] +fn test_geth_prestate_selfdestruct_reports_slots_before_the_wipe() { + let (contract, frame) = trace_london_selfdestruct(PreStateConfig::default()); + let PreStateFrame::Default(prestate) = frame else { panic!("expected a prestate frame") }; + + // Output of geth's `evm t8n --state.fork London` with the prestate tracer. + assert_eq!( + serde_json::to_value(&prestate.0[&contract]).unwrap(), + serde_json::json!({ + "balance": "0x7", + "code": "0x6001545060025450733333333333333333333333333333333333333333ff", + "nonce": 1, + "storage": { + "0x0000000000000000000000000000000000000000000000000000000000000001": + "0x000000000000000000000000000000000000000000000000000000000000002a", + "0x0000000000000000000000000000000000000000000000000000000000000002": + "0x0000000000000000000000000000000000000000000000000000000000000000" + } + }) + ); +} From 337e1eaf8a65d1bb80f4aa303b1713b350d07070 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:47:47 +0100 Subject: [PATCH 035/159] perf(state): seed bytecode hash from account code hash (#891) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The JIT runner keys compiled code by `Bytecode::hash_slow`, which keccaks the code the first time it is called on a `Bytecode` allocation. Code that arrives from a database (`get_code_by_hash`) or in `AccountInfo::code` has an empty hash cell, so the first JIT lookup hashes the whole contract, even though the interpreter never needs the hash and the state already holds it as `account.code_hash`. This adds `Bytecode::seed_hash`, which fills the cached hash if it is empty, and calls it from `AccountHandle::load_code` and `original_code` with the account's `code_hash`. Nothing changes when the cell is already set, apart from a debug assertion that the two hashes agree. Empty code keeps its built-in empty hash. EIP-7702 designators hash their 23 original bytes, which is what `code_hash` holds, and code deployed earlier in the same transaction goes through `set_code_slow`, which already caches the hash. ## Benchmarks Instructions per transaction from `perf stat` (difference of two run lengths), with a scratch change to `evm2 run` that gives each transaction a fresh `Bytecode` from the database. The interpreter is unchanged; the JIT drops by roughly the keccak cost (791k instructions for the 21,943-byte univ2_router code). | Bench | Interpreter before | Interpreter after | JIT before | JIT after | | --- | --- | --- | --- | --- | | univ2_router | 36,581 | 36,644 | 824,585 | 33,066 🟢 | | seaport | 38,121 | 38,279 | 916,450 | 37,178 🟢 | | uniswap_v4_pool_manager | 109,643 | 109,973 | 1,102,287 | 81,117 🟢 | | aggregation_router_v6 | 819,905 | 822,640 | 5,945,053 | 507,480 🟢 | --- crates/evm2/src/bytecode/mod.rs | 38 ++++++++++++++++++++++ crates/evm2/src/evm/state/account.rs | 48 +++++++++++++++++++++------- 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index 182c8564c..4e146eeef 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -335,6 +335,20 @@ impl Bytecode { *self.inner().hash.get_or_init(|| keccak256(self.original_byte_slice())) } + /// Caches `hash` as the hash of the original bytecode unless one is already cached. + /// + /// `hash` must be the keccak256 of [`Self::original_byte_slice`]. Does nothing for empty + /// bytecode or the zero hash. + #[inline] + pub fn seed_hash(&self, hash: B256) { + if self.is_empty() || hash.is_zero() { + return; + } + if self.inner().hash.set(hash).is_err() { + debug_assert_eq!(self.hash_slow(), hash, "seeded hash differs from cached hash"); + } + } + /// Returns a reference to the potentially padded bytecode bytes. #[inline] pub fn bytes(&self) -> &Bytes { @@ -494,6 +508,30 @@ mod tests { assert_eq!(&bytecode.original_byte_slice()[..3], &[0xEF, 0x01, 0x00]); } + #[test] + fn seed_hash_sets_only_an_empty_cache() { + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let seed = B256::repeat_byte(0x5e); + bytecode.seed_hash(seed); + assert_eq!(bytecode.hash_slow(), seed); + + let empty = Bytecode::new(); + empty.seed_hash(seed); + assert_eq!(empty.hash_slow(), KECCAK256_EMPTY); + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + bytecode.seed_hash(B256::ZERO); + assert_eq!(bytecode.hash_slow(), keccak256([0x60, 0x01])); + } + + #[test] + #[cfg(debug_assertions)] + #[should_panic(expected = "seeded hash differs from cached hash")] + fn seed_hash_panics_on_conflicting_hash_in_debug() { + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + bytecode.seed_hash(B256::repeat_byte(0x5e)); + bytecode.seed_hash(B256::repeat_byte(0x5f)); + } + #[test] fn analysis_is_lazy_and_shared() { let raw = Bytes::from_static(&[op::JUMPDEST, op::PUSH1, op::JUMPDEST, op::PUSH32]); diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 968b8b326..84acd2882 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -497,13 +497,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if account.is_empty_code_hash() { return Ok(Bytecode::default()); } - if let Some(code) = account.code.as_ref() - && !code.is_empty() - { - return Ok(code.clone()); - } let code_hash = account.code_hash; - self.inner.database.get_code_by_hash(&code_hash) + let code = match account.code.as_ref() { + Some(code) if !code.is_empty() => code.clone(), + _ => self.inner.database.get_code_by_hash(&code_hash)?, + }; + code.seed_hash(code_hash); + Ok(code) } /// Loads the account's bytecode as it stood at the start of the transaction. @@ -520,13 +520,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if account.is_empty_code_hash() { return Ok(Bytecode::default()); } - if let Some(code) = account.code.as_ref() - && !code.is_empty() - { - return Ok(code.clone()); - } let code_hash = account.code_hash; - self.inner.database.get_code_by_hash(&code_hash) + let code = match account.code.as_ref() { + Some(code) if !code.is_empty() => code.clone(), + _ => self.inner.database.get_code_by_hash(&code_hash)?, + }; + code.seed_hash(code_hash); + Ok(code) } /// Touches the account, recording a revert snapshot the first time it is mutated. @@ -824,6 +824,30 @@ mod tests { assert_eq!(loaded, code); } + #[test] + fn loaded_code_is_seeded_with_account_code_hash() { + use alloy_primitives::Bytes; + + // A hash that is not the keccak of the code proves `hash_slow` returns the seeded value + // instead of hashing. + let seed = B256::repeat_byte(0x5e); + let address = Address::from([0x8d; 20]); + let fresh = || Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + assert_ne!(fresh().hash_slow(), seed); + + let mut database = CacheDB::default(); + database + .insert_account_info(&address, AccountInfo::new(Word::ZERO, 1, seed, Some(fresh()))); + let mut state = State::new(database); + + assert_eq!(state.account(&address).unwrap().load_code().unwrap().hash_slow(), seed); + assert_eq!(state.account(&address).unwrap().original_code().unwrap().hash_slow(), seed); + + let overlay_address = Address::from([0x8e; 20]); + state.account(&overlay_address).unwrap().set_code(seed, fresh()); + assert_eq!(state.account(&overlay_address).unwrap().load_code().unwrap().hash_slow(), seed); + } + #[test] fn journaled_existing_account_field_changes_roll_back_granularly() { use crate::{SpecId, Version, bytecode::Bytecode}; From 1dc537e64fe6bc8ebfbc6538cf0f8739f71442d4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:50:39 +0100 Subject: [PATCH 036/159] perf(jit): hash RuntimeCacheKey as one word (#890) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `RuntimeCacheKey` derived `Hash`, so every resident-map lookup of a JIT-executed frame ran the map's hasher over the 32-byte code hash plus the spec id. `Hash` is now implemented by hand and feeds the hasher one `u64`: the XOR of the four 64-bit words of the code hash and the spec id. Folding all four words keeps the hash well spread even for structured or non-keccak code hashes, and the map's hasher still mixes the word. `Eq` still compares the full key, so a collision costs a bucket collision, not a wrong hit. ## Benchmarks Nested-call microbenchmark (self-CALL loop, callee does CALLDATASIZE/JUMPI/STOP) through the production `JitInterpreterRunner` with `EVM2_RUN_BACKEND=async`, per frame as (100 calls per tx − 0 calls per tx) / 100, on an m4-metal-large box. Base is `6eac19be`, the merge base of this branch. | | base | head | change | |:--|--:|--:|--:| | instructions per frame (median of six) | 2335 | 2221 | 🟢 −114 (−4.9%) | | cycles per frame (min of six) | 872 | 834 | 🟢 −38 (−4.3%) | `opt.s` is unchanged in 0/31 codegen benchmarks. Head `a1e6937e09` with uncommitted changes vs base `6eac19be` (`6eac19bef6`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **-0.1% 🟢** | **=** | | **GEOMEAN** | **=** | **=** | **+1.3% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------:|--------------------------------:|------------------------------:|-------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.22M
37.25M
+0.1% 🔴 | 31.77M
31.89M
+0.4% 🔴 | 16.5ms
17.5ms
+6.0% 🔴 | 203.5µs
218.1µs
+7.2% 🔴 | 509.0µs
531.9µs
+4.5% 🔴 | 1.54ms
1.63ms
+6.0% 🔴 | 3.27ms
3.29ms
+0.6% 🔴 | | factorial | 41.42M
41.43M
= | 34.04M
34.07M
+0.1% 🔴 | 17.8ms
17.8ms
+0.4% 🔴 | 213.7µs
215.6µs
+0.9% 🔴 | 496.7µs
535.2µs
+7.8% 🔴 | 1.57ms
1.69ms
+8.1% 🔴 | 3.73ms
3.76ms
+0.9% 🔴 | | counter | 53.94M
53.92M
= | 35.39M
35.48M
+0.2% 🔴 | 12.0ms
12.7ms
+5.2% 🔴 | 433.8µs
438.7µs
+1.1% 🔴 | 679.7µs
682.3µs
+0.4% 🔴 | 1.90ms
1.94ms
+2.3% 🔴 | 4.82ms
4.85ms
+0.7% 🔴 | | snailtracer | 5.28G
5.28G
= | 2.37G
2.36G
-0.1% 🟢 | 592.1ms
593.3ms
+0.2% 🔴 | 52.5ms
52.5ms
= | 9.24ms
9.44ms
+2.1% 🔴 | 104.0ms
105.2ms
+1.2% 🔴 | 476.9ms
468.5ms
-1.8% 🟢 | | weth | 538.68M
538.51M
= | 260.77M
260.47M
-0.1% 🟢 | 68.4ms
69.0ms
+0.9% 🔴 | 3.28ms
3.30ms
+0.5% 🔴 | 2.95ms
3.01ms
+2.0% 🔴 | 15.9ms
16.0ms
+0.8% 🔴 | 47.1ms
47.1ms
= | | hash_10k | 80.15M
80.22M
+0.1% 🔴 | 47.35M
47.49M
+0.3% 🔴 | 15.1ms
15.7ms
+4.4% 🔴 | 392.4µs
394.1µs
+0.4% 🔴 | 681.7µs
716.0µs
+5.0% 🔴 | 2.52ms
2.68ms
+6.3% 🔴 | 6.77ms
6.72ms
-0.7% 🟢 | | erc20_transfer | 576.60M
576.50M
= | 281.86M
281.04M
-0.3% 🟢 | 74.0ms
74.4ms
+0.5% 🔴 | 3.47ms
3.46ms
-0.4% 🟢 | 3.08ms
3.09ms
+0.5% 🔴 | 17.1ms
17.1ms
= | 50.7ms
50.3ms
-0.9% 🟢 | | push0_proxy | 34.47M
34.47M
= | 28.61M
28.62M
= | 16.0ms
16.8ms
+4.6% 🔴 | 183.9µs
199.4µs
+8.4% 🔴 | 491.7µs
533.0µs
+8.4% 🔴 | 1.20ms
1.29ms
+7.6% 🔴 | 2.75ms
2.79ms
+1.3% 🔴 | | usdc_proxy | 243.77M
243.85M
= | 127.30M
127.04M
-0.2% 🟢 | 35.1ms
35.8ms
+1.8% 🔴 | 1.98ms
2.03ms
+2.2% 🔴 | 1.84ms
1.82ms
-1.1% 🟢 | 8.23ms
8.43ms
+2.4% 🔴 | 21.1ms
21.0ms
-0.4% 🟢 | | fiat_token | 3.97G
3.98G
= | 1.85G
1.85G
-0.1% 🟢 | 466.1ms
467.6ms
+0.3% 🔴 | 21.4ms
21.7ms
+1.3% 🔴 | 16.3ms
16.4ms
+0.6% 🔴 | 103.4ms
103.6ms
+0.1% 🔴 | 364.6ms
363.6ms
-0.3% 🟢 | | uniswap_v2_pair | 2.06G
2.05G
= | 977.79M
973.36M
-0.5% 🟢 | 249.3ms
247.8ms
-0.6% 🟢 | 11.6ms
11.6ms
+0.2% 🔴 | 9.34ms
9.36ms
+0.2% 🔴 | 50.7ms
50.1ms
-1.2% 🟢 | 193.0ms
192.3ms
-0.3% 🟢 | | univ2_router | 4.38G
4.38G
= | 2.08G
2.08G
+0.2% 🔴 | 521.5ms
523.3ms
+0.4% 🔴 | 25.0ms
23.7ms
-5.0% 🟢 | 18.1ms
17.2ms
-5.1% 🟢 | 104.6ms
105.2ms
+0.5% 🔴 | 420.2ms
422.9ms
+0.6% 🔴 | | seaport | 9.73G
9.73G
= | 4.92G
4.96G
+0.7% 🔴 | 1.230s
1.239s
+0.7% 🔴 | 39.8ms
39.9ms
+0.3% 🔴 | 29.1ms
28.6ms
-1.8% 🟢 | 260.3ms
262.7ms
+0.9% 🔴 | 1.044s
1.030s
-1.3% 🟢 | | airdrop | 1.42G
1.42G
= | 678.82M
676.07M
-0.4% 🟢 | 174.1ms
173.3ms
-0.4% 🟢 | 7.34ms
7.32ms
-0.3% 🟢 | 6.20ms
6.21ms
+0.1% 🔴 | 43.2ms
43.7ms
+1.2% 🔴 | 126.0ms
125.9ms
-0.1% 🟢 | | bswap64 | 66.86M
66.82M
-0.1% 🟢 | 39.88M
39.81M
-0.2% 🟢 | 13.4ms
13.8ms
+2.8% 🔴 | 678.6µs
718.5µs
+5.9% 🔴 | 818.1µs
873.3µs
+6.7% 🔴 | 2.11ms
2.26ms
+7.2% 🔴 | 5.04ms
5.01ms
-0.5% 🟢 | | bswap64_opt | 73.48M
73.50M
= | 42.47M
42.49M
+0.1% 🔴 | 13.9ms
14.6ms
+5.0% 🔴 | 624.1µs
617.6µs
-1.0% 🟢 | 769.4µs
791.6µs
+2.9% 🔴 | 2.30ms
2.38ms
+3.2% 🔴 | 5.59ms
5.57ms
-0.2% 🟢 | | eip4788 | 48.34M
48.33M
= | 32.61M
32.65M
+0.1% 🔴 | 11.4ms
12.0ms
+5.2% 🔴 | 281.3µs
280.8µs
-0.2% 🟢 | 602.4µs
576.2µs
-4.3% 🟢 | 1.55ms
1.53ms
-1.3% 🟢 | 4.49ms
4.48ms
-0.2% 🟢 | | eip2935 | 42.11M
42.12M
= | 34.00M
34.02M
+0.1% 🔴 | 17.0ms
17.8ms
+5.0% 🔴 | 233.5µs
270.0µs
+15.6% 🔴 | 543.1µs
578.9µs
+6.6% 🔴 | 1.33ms
1.49ms
+12.3% 🔴 | 3.80ms
3.83ms
+0.8% 🔴 | | burntpix | 6.70G
6.70G
= | 3.11G
3.11G
-0.1% 🟢 | 772.3ms
771.5ms
-0.1% 🟢 | 21.0ms
21.6ms
+3.0% 🔴 | 2.52ms
2.58ms
+2.4% 🔴 | 14.3ms
14.4ms
+1.2% 🔴 | 41.6ms
41.6ms
-0.1% 🟢 | | curve_stableswap | 1.14G
1.14G
= | 544.18M
545.68M
+0.3% 🔴 | 138.9ms
140.2ms
+0.9% 🔴 | 4.93ms
4.94ms
+0.3% 🔴 | 3.84ms
3.89ms
+1.3% 🔴 | 30.5ms
30.5ms
= | 72.5ms
72.0ms
-0.6% 🟢 | | onchain_lm_v2 | 3.66G
3.66G
= | 1.63G
1.63G
+0.1% 🔴 | 411.1ms
411.9ms
+0.2% 🔴 | 16.1ms
16.0ms
-0.6% 🟢 | 11.4ms
11.4ms
+0.1% 🔴 | 72.8ms
72.9ms
+0.2% 🔴 | 285.4ms
285.9ms
+0.2% 🔴 | | usdt | 1.34G
1.34G
= | 624.64M
624.88M
= | 159.9ms
160.4ms
+0.3% 🔴 | 10.0ms
10.2ms
+1.1% 🔴 | 7.91ms
7.79ms
-1.5% 🟢 | 36.8ms
37.1ms
+0.8% 🔴 | 117.6ms
118.1ms
+0.5% 🔴 | | universal_router | 11.57G
11.56G
= | 5.59G
5.57G
-0.3% 🟢 | 1.380s
1.376s
-0.3% 🟢 | 21.3ms
21.1ms
-1.1% 🟢 | 17.7ms
17.9ms
+1.3% 🔴 | 123.4ms
122.8ms
-0.5% 🟢 | 477.1ms
476.3ms
-0.2% 🟢 | | aggregation_router_v6 | 35.11G
35.12G
= | 16.84G
16.81G
-0.2% 🟢 | 4.122s
4.111s
-0.3% 🟢 | 34.0ms
34.3ms
+0.9% 🔴 | 28.7ms
28.6ms
-0.4% 🟢 | 207.8ms
209.4ms
+0.8% 🔴 | 786.3ms
784.5ms
-0.2% 🟢 | | entry_point_v0_7 | 17.88G
17.88G
= | 8.65G
8.64G
-0.1% 🟢 | 2.132s
2.131s
= | 16.1ms
16.1ms
= | 15.0ms
15.0ms
-0.3% 🟢 | 99.9ms
97.5ms
-2.3% 🟢 | 410.3ms
409.5ms
-0.2% 🟢 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.98G
2.97G
-0.3% 🟢 | 748.0ms
746.2ms
-0.2% 🟢 | 33.5ms
33.3ms
-0.9% 🟢 | 25.8ms
25.4ms
-1.9% 🟢 | 175.3ms
167.7ms
-4.3% 🟢 | 605.9ms
604.5ms
-0.2% 🟢 | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 3.20G
3.19G
-0.3% 🟢 | 799.7ms
799.2ms
-0.1% 🟢 | 23.9ms
23.9ms
= | 21.1ms
21.9ms
+3.8% 🔴 | 161.6ms
160.4ms
-0.8% 🟢 | 599.6ms
590.7ms
-1.5% 🟢 | | curve_3pool | 6.59G
6.59G
= | 3.03G
3.00G
-0.9% 🟢 | 758.1ms
754.3ms
-0.5% 🟢 | 33.1ms
33.1ms
-0.2% 🟢 | 22.2ms
21.9ms
-1.7% 🟢 | 129.4ms
130.9ms
+1.1% 🔴 | 661.0ms
655.7ms
-0.8% 🟢 | | curve_tricrypto2 | 9.40G
9.40G
= | 4.23G
4.23G
-0.1% 🟢 | 1.053s
1.053s
-0.1% 🟢 | 30.6ms
31.0ms
+1.2% 🔴 | 24.6ms
25.1ms
+1.7% 🔴 | 145.9ms
145.1ms
-0.5% 🟢 | 638.1ms
637.7ms
-0.1% 🟢 | | balancer_vault | 19.03G
19.02G
-0.1% 🟢 | 9.49G
9.48G
-0.2% 🟢 | 2.339s
2.339s
= | 43.7ms
43.9ms
+0.3% 🔴 | 31.3ms
30.7ms
-2.0% 🟢 | 302.0ms
302.5ms
+0.2% 🔴 | 987.2ms
974.8ms
-1.3% 🟢 | | succinct_staking | 10.84G
10.84G
= | 5.16G
5.17G
+0.1% 🔴 | 1.272s
1.273s
+0.1% 🔴 | 30.8ms
30.6ms
-0.6% 🟢 | 18.6ms
18.4ms
-1.0% 🟢 | 130.5ms
130.4ms
-0.1% 🟢 | 482.3ms
478.6ms
-0.8% 🟢 | | **TOTAL** | **164.84G
164.82G
=** | **78.94G
78.86G
-0.1% 🟢** | **19.630s
19.629s
=** | **488.8ms
488.8ms
=** | **332.6ms
331.3ms
-0.4% 🟢** | **2.354s
2.351s
-0.1% 🟢** | **8.949s
8.892s
-0.6% 🟢** |
--- crates/jit/runtime/src/runtime/storage.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/jit/runtime/src/runtime/storage.rs b/crates/jit/runtime/src/runtime/storage.rs index 8210a611c..9cbb34066 100644 --- a/crates/jit/runtime/src/runtime/storage.rs +++ b/crates/jit/runtime/src/runtime/storage.rs @@ -4,10 +4,15 @@ use crate::{OptimizationLevel, eyre, runtime::RuntimeConfig}; use alloy_primitives::{B256, keccak256}; use dashmap::DashMap; use evm2::{SpecId, version::GasId}; -use std::{fs, io::Write, path::PathBuf}; +use std::{ + array, fs, + hash::{Hash, Hasher}, + io::Write, + path::PathBuf, +}; /// Runtime cache key: the minimal identity for a compiled program at runtime. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RuntimeCacheKey { /// The code hash of the contract bytecode. pub code_hash: B256, @@ -15,6 +20,17 @@ pub struct RuntimeCacheKey { pub spec_id: SpecId, } +impl Hash for RuntimeCacheKey { + /// Feeds the hasher one word: the XOR of the four code hash words and the spec id. + #[inline] + fn hash(&self, state: &mut H) { + let [a, b, c, d] = array::from_fn(|i| { + u64::from_le_bytes(self.code_hash[i * 8..i * 8 + 8].try_into().unwrap()) + }); + state.write_u64(a ^ b ^ c ^ d ^ self.spec_id as u64); + } +} + /// Full artifact identity for persisted artifacts. /// /// Persisted artifacts must match all fields of this key to be loaded. From 6878dad2539564c5bb03b04f1d7d127db4dcbc53 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:07:02 +0100 Subject: [PATCH 037/159] fix(evm): report a wiped slot's pre-transaction value in storage reads (#895) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The change stream reported a slot of a wiped account with the wrong value. `Account::wipe` resets every loaded slot's `current` to zero and keeps its `original` (`crates/evm2/src/evm/state/storage.rs:305-320`). `AccountChanges::visit` then finds `slot.is_changed(wiped)` false for the zeroed slot and emits it as `storage_read(address, key, value.current)` (`crates/evm2/src/evm/state/stream.rs:93`), which is zero while the value before the transaction sits in `value.original`. It now passes `value.original`, which equals `current` for every slot that is not wiped, and the `storage_read` doc and the stale sentence in the `wipe` doc say what the value is. The block access list builder and `CacheDB` ignore the value and the only sink that reads it is the inspectors' `TxState`, so nothing else changes. This is the real cause of the zeros #889 and #881 worked around in the prestate tracer. The tracer took the stream's value as the slot's original value, so every slot of a destroyed account looked zero before the transaction, in non-diff mode and for the slots diff mode lists. The tracer builder no longer re-reads those slots from the database; both regression tests stay as they are and pass without it. Mainnet block 4501969, the Parity library kill (`0x47f7cff7a5e671884629c93b368cb18f58a993f4b19c2a53a8662e3f1482f690`, Byzantium), still matches geth `evm t8n` at `a0cba163` with the state from `ethereum.reth.rs`: | slot `…d241e0` of `0x863df6bf…` | geth | `main` before #889 | this PR | | :-- | :-- | :-- | :-- | | non-diff | `0x2` | `0x0` | `0x2` | | diff, `pre` | `0x2` | `0x2` (via #881's re-read) | `0x2` | --- crates/evm2/src/evm/state/mod.rs | 37 +++++++++++++++++++ crates/evm2/src/evm/state/storage.rs | 9 +++-- crates/evm2/src/evm/state/stream.rs | 5 ++- crates/inspectors/src/tracing/builder/geth.rs | 13 ++----- crates/inspectors/src/tracing/tx_state.rs | 4 +- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index bbb1ced7a..bacac5775 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -1810,6 +1810,43 @@ mod tests { assert_eq!(parent.storage_slot_untracked(&address, &key).unwrap(), Word::from(5)); } + #[test] + fn wiped_slot_is_reported_as_a_read_of_its_original_value() { + #[derive(Default)] + struct Reads(Vec<(Word, Word)>); + + impl StateChangeSink for Reads { + type Error = core::convert::Infallible; + + fn storage_read( + &mut self, + _address: Address, + key: Word, + value: Word, + ) -> Result<(), Self::Error> { + self.0.push((key, value)); + Ok(()) + } + } + + let address = Address::with_last_byte(42); + let (read_key, written_key) = (Word::ONE, Word::from(2)); + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &read_key, &Word::from(5)); + db.insert_account_storage(&address, &written_key, &Word::from(6)); + let mut state = State::new(db); + state.account(&address).unwrap(); + state.storage_slot(&address, read_key).unwrap(); + state.storage_slot(&address, written_key).unwrap().set(Word::from(7)); + state.storage(&address).unwrap().wipe(); + + let mut reads = Reads::default(); + state.take_pending_state().visit(&mut reads).unwrap(); + reads.0.sort(); + assert_eq!(reads.0, [(read_key, Word::from(5)), (written_key, Word::from(6))]); + } + #[test] fn isolated_state_does_not_replay_finalized_eip8246_selfdestruct() { let address = Address::with_last_byte(42); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index ac262387b..cc6beda08 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -297,10 +297,11 @@ impl<'a, 'db> StorageHandle<'a, 'db> { /// /// Only called during transaction finalization (selfdestruct and EIP-161 dead-account /// deletion), after the last revertible scope, so the wipe is not journaled. Loaded slot - /// entries are kept with their values reset to zero: wiped slots resolve to zero on re-load, - /// and resetting `original` alongside `current` turns the transaction's prior writes into - /// unchanged reads, which keeps a destroyed account's storage accesses visible to the EIP-7928 - /// block access list (execution-specs `destroy_storage` converts writes to reads). + /// entries are kept with their `current` value reset to zero and their `original` value kept: + /// wiped slots resolve to zero on re-load, and the reset turns the transaction's prior writes + /// into unchanged reads of the slot's original value, which keeps a destroyed account's storage + /// accesses visible to the EIP-7928 block access list (execution-specs `destroy_storage` + /// converts writes to reads). #[inline] pub fn wipe(&mut self) { self.storage.wiped = true; diff --git a/crates/evm2/src/evm/state/stream.rs b/crates/evm2/src/evm/state/stream.rs index 1593136a8..7856987ce 100644 --- a/crates/evm2/src/evm/state/stream.rs +++ b/crates/evm2/src/evm/state/stream.rs @@ -90,7 +90,7 @@ impl<'a> AccountChanges<'a> { current: value.current, })?; } else { - sink.storage_read(address, key, value.current)?; + sink.storage_read(address, key, value.original)?; } } if self.is_info_changed() { @@ -168,6 +168,9 @@ pub trait StateChangeSink { /// Observes a storage slot the transaction loaded but left unchanged. /// + /// `value` is the slot's value at the start of the transaction. After a storage wipe the + /// slot's value after the transaction is zero. + /// /// Only transaction-level sources report reads; sinks that persist changes can ignore them. #[inline] fn storage_read( diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 5034a49c6..ab6b1f31c 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -437,14 +437,7 @@ impl<'a> GethTraceBuilder<'a> { // insert the original value of all loaded storage slots if storage_enabled { for (key, slot) in loaded_acc.storage.iter() { - // The state stream reports the slots of a selfdestructed account after the - // wipe, as zero. geth reads a slot when the opcode runs, so use the database. - let original = if loaded_acc.selfdestructed { - db.get_storage(&addr, key)? - } else { - slot.original - }; - acc_state.storage.insert((*key).into(), original.into()); + acc_state.storage.insert((*key).into(), slot.original.into()); } } @@ -500,8 +493,8 @@ impl<'a> GethTraceBuilder<'a> { // Geth keeps the pre state of a selfdestructed account as it looked it up, with // every slot the transaction touched, unchanged and zero ones included. if changed_acc.selfdestructed { - for key in changed_acc.storage.keys() { - pre_state.storage.insert((*key).into(), db.get_storage(&addr, key)?.into()); + for (key, slot) in changed_acc.storage.iter() { + pre_state.storage.insert((*key).into(), slot.original.into()); } } else { for (key, slot) in changed_acc.changed_storage() { diff --git a/crates/inspectors/src/tracing/tx_state.rs b/crates/inspectors/src/tracing/tx_state.rs index 2cd97b391..2ac3d019b 100644 --- a/crates/inspectors/src/tracing/tx_state.rs +++ b/crates/inspectors/src/tracing/tx_state.rs @@ -44,7 +44,9 @@ pub(crate) struct TxAccount { pub(crate) created: bool, /// Whether the account was selfdestructed during the transaction. pub(crate) selfdestructed: bool, - /// Storage slots loaded during the transaction, changed and unchanged. + /// Storage slots loaded during the transaction, changed and unchanged. A loaded slot that the + /// transaction left unchanged, or that a storage wipe reset, has its value before the + /// transaction as both `original` and `current`. pub(crate) storage: U256Map>, } From 66003c7d5482cc8eb759a86be4e1a8500e2e887b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:08:04 +0100 Subject: [PATCH 038/159] perf(evm2): decide interpreter runner support once, not per frame (#892) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Frames run through an `InterpreterRunner` paid a fixed cost on every call: `JitInterpreterRunner` re-ran `JitBackend::supports_host` (a 228-byte `GasParams` comparison, a `TypeId` check, and the opcode-table check), and `Evm::run_interpreter` cloned the runner's `Arc` even when no runner could run. Support depends only on the runner and the execution config, which can only change when `!running`. `InterpreterRunner` gains `supports(&ExecutionConfig) -> bool` (default `true`). `Evm` caches the verdict whenever the runner or the execution config changes, and `run_interpreter` clones the runner `Arc` only when the cached verdict is true. `JitInterpreterRunner::supports` is `supports_host`, so the per-frame path keeps only the `enabled()` and gas checks, which can change at runtime. Its safe `run` also keeps a `TypeId` check, because the host field layout the compiled code reads depends on the host type; direct callers of `run` must check `supports` for the gas schedule and opcode table. The public free function `run_interpreter` still checks host support itself, because its callers pass arbitrary hosts. ## Benchmarks Per-call cost of a nested self-CALL loop through the production `JitInterpreterRunner` (`EVM2_RUN_BACKEND=async`, `(nest100 - nest0) / 100`) on an m4-metal-large box. Instructions are the median of 3 runs; cycles are the minimum over six repetitions of 3 runs. | mode | base | head | change | |:-----|-----:|-----:|-------:| | JIT instructions per call | 2241.3 | 2162.3 | -79.0 (-3.5%) 🟢 | | JIT cycles per call | 845.2 | 811.3 | -33.9 (-4.0%) 🟢 | | interpreter instructions per call | 2273.6 | 2275.6 | +2.0 (+0.1%) | | interpreter cycles per call | 814.4 | 817.0 | +2.6, within noise | Codegen is unchanged: `opt.s` changed in 0/31 benchmarks. Head `1492b91a78` with uncommitted changes vs base `origin/main` (`1dc537e64f`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **+0.1% 🔴** | **+1.7% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------:|------------------------------:|------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.24M
37.23M
= | 31.88M
31.43M
-1.4% 🟢 | 16.8ms
18.5ms
+10.2% 🔴 | 204.6µs
208.8µs
+2.1% 🔴 | 485.8µs
501.5µs
+3.2% 🔴 | 1.50ms
1.62ms
+8.1% 🔴 | 3.26ms
3.28ms
+0.6% 🔴 | | factorial | 41.48M
41.42M
-0.1% 🟢 | 33.86M
33.80M
-0.2% 🟢 | 17.1ms
17.6ms
+3.5% 🔴 | 212.8µs
241.0µs
+13.3% 🔴 | 494.6µs
513.4µs
+3.8% 🔴 | 1.56ms
1.67ms
+7.5% 🔴 | 3.72ms
3.71ms
-0.3% 🟢 | | counter | 53.98M
53.99M
= | 35.34M
35.41M
+0.2% 🔴 | 12.0ms
12.8ms
+6.9% 🔴 | 435.8µs
449.6µs
+3.2% 🔴 | 665.1µs
678.5µs
+2.0% 🔴 | 1.92ms
1.94ms
+0.9% 🔴 | 4.83ms
4.78ms
-1.0% 🟢 | | snailtracer | 5.30G
5.28G
-0.3% 🟢 | 2.38G
2.37G
-0.4% 🟢 | 596.4ms
594.6ms
-0.3% 🟢 | 53.1ms
52.2ms
-1.6% 🟢 | 9.08ms
9.27ms
+2.1% 🔴 | 104.5ms
105.0ms
+0.5% 🔴 | 473.5ms
475.6ms
+0.4% 🔴 | | weth | 538.66M
538.53M
= | 260.29M
260.64M
+0.1% 🔴 | 68.2ms
69.0ms
+1.1% 🔴 | 3.33ms
3.35ms
+0.6% 🔴 | 2.98ms
2.97ms
-0.5% 🟢 | 15.8ms
15.8ms
-0.2% 🟢 | 47.0ms
47.1ms
+0.3% 🔴 | | hash_10k | 80.13M
80.20M
+0.1% 🔴 | 47.28M
47.49M
+0.4% 🔴 | 15.0ms
16.1ms
+7.1% 🔴 | 399.2µs
411.7µs
+3.1% 🔴 | 673.2µs
728.5µs
+8.2% 🔴 | 2.54ms
2.68ms
+5.4% 🔴 | 6.72ms
6.76ms
+0.6% 🔴 | | erc20_transfer | 576.75M
576.93M
= | 280.78M
281.15M
+0.1% 🔴 | 73.6ms
74.4ms
+1.1% 🔴 | 3.48ms
3.46ms
-0.4% 🟢 | 3.05ms
3.09ms
+1.3% 🔴 | 17.0ms
17.1ms
+1.1% 🔴 | 50.4ms
50.8ms
+0.6% 🔴 | | push0_proxy | 34.45M
34.46M
= | 28.51M
28.59M
+0.3% 🔴 | 16.8ms
16.6ms
-1.1% 🟢 | 190.0µs
206.8µs
+8.8% 🔴 | 498.4µs
514.2µs
+3.2% 🔴 | 1.21ms
1.29ms
+6.4% 🔴 | 2.71ms
2.78ms
+2.6% 🔴 | | usdc_proxy | 243.69M
243.63M
= | 126.65M
127.46M
+0.6% 🔴 | 35.0ms
35.8ms
+2.4% 🔴 | 2.01ms
2.01ms
-0.1% 🟢 | 1.80ms
1.82ms
+1.4% 🔴 | 8.32ms
8.35ms
+0.3% 🔴 | 21.0ms
21.0ms
+0.1% 🔴 | | fiat_token | 3.98G
3.97G
= | 1.85G
1.86G
+0.4% 🔴 | 465.4ms
467.3ms
+0.4% 🔴 | 21.7ms
21.9ms
+0.9% 🔴 | 16.1ms
16.2ms
+1.0% 🔴 | 102.9ms
103.1ms
+0.1% 🔴 | 363.5ms
364.7ms
+0.3% 🔴 | | uniswap_v2_pair | 2.06G
2.06G
= | 973.53M
974.17M
+0.1% 🔴 | 246.7ms
247.4ms
+0.3% 🔴 | 11.6ms
11.7ms
+0.7% 🔴 | 9.16ms
9.47ms
+3.3% 🔴 | 50.5ms
50.4ms
-0.3% 🟢 | 192.4ms
193.5ms
+0.6% 🔴 | | univ2_router | 4.38G
4.38G
= | 2.07G
2.08G
+0.1% 🔴 | 521.8ms
522.3ms
+0.1% 🔴 | 24.6ms
24.0ms
-2.7% 🟢 | 17.5ms
17.0ms
-3.2% 🟢 | 104.7ms
105.9ms
+1.2% 🔴 | 424.0ms
427.1ms
+0.7% 🔴 | | seaport | 9.73G
9.73G
= | 4.92G
4.93G
+0.2% 🔴 | 1.228s
1.233s
+0.4% 🔴 | 39.8ms
40.0ms
+0.6% 🔴 | 28.6ms
28.5ms
-0.3% 🟢 | 258.2ms
261.4ms
+1.2% 🔴 | 1.039s
1.030s
-0.9% 🟢 | | airdrop | 1.42G
1.42G
= | 675.14M
677.43M
+0.3% 🔴 | 172.7ms
173.5ms
+0.5% 🔴 | 7.27ms
7.30ms
+0.4% 🔴 | 6.13ms
6.15ms
+0.2% 🔴 | 43.1ms
43.5ms
+0.8% 🔴 | 126.6ms
125.8ms
-0.6% 🟢 | | bswap64 | 66.80M
66.88M
+0.1% 🔴 | 39.71M
40.07M
+0.9% 🔴 | 13.1ms
13.8ms
+5.9% 🔴 | 722.2µs
716.2µs
-0.8% 🟢 | 842.3µs
866.7µs
+2.9% 🔴 | 2.13ms
2.28ms
+6.9% 🔴 | 5.10ms
5.07ms
-0.6% 🟢 | | bswap64_opt | 73.57M
73.55M
= | 42.44M
42.50M
+0.1% 🔴 | 13.8ms
14.4ms
+4.6% 🔴 | 597.8µs
631.2µs
+5.6% 🔴 | 756.5µs
789.0µs
+4.3% 🔴 | 2.28ms
2.41ms
+5.8% 🔴 | 5.50ms
5.56ms
+1.1% 🔴 | | eip4788 | 48.36M
48.30M
-0.1% 🟢 | 32.79M
32.71M
-0.2% 🟢 | 11.5ms
12.0ms
+4.0% 🔴 | 301.0µs
282.8µs
-6.0% 🟢 | 577.3µs
587.6µs
+1.8% 🔴 | 1.52ms
1.58ms
+3.4% 🔴 | 4.45ms
4.45ms
= | | eip2935 | 42.15M
42.16M
= | 33.90M
33.90M
= | 17.1ms
17.9ms
+5.0% 🔴 | 239.6µs
258.4µs
+7.8% 🔴 | 530.8µs
539.9µs
+1.7% 🔴 | 1.29ms
1.37ms
+6.0% 🔴 | 3.80ms
3.82ms
+0.5% 🔴 | | burntpix | 6.70G
6.70G
= | 3.11G
3.12G
+0.3% 🔴 | 772.0ms
774.6ms
+0.3% 🔴 | 21.2ms
21.3ms
+0.8% 🔴 | 2.47ms
2.51ms
+1.6% 🔴 | 14.3ms
14.5ms
+1.1% 🔴 | 41.4ms
41.7ms
+0.7% 🔴 | | curve_stableswap | 1.14G
1.14G
-0.1% 🟢 | 542.58M
545.08M
+0.5% 🔴 | 139.3ms
139.7ms
+0.3% 🔴 | 4.96ms
4.94ms
-0.3% 🟢 | 3.89ms
3.93ms
+1.1% 🔴 | 31.7ms
30.7ms
-3.0% 🟢 | 72.8ms
72.6ms
-0.3% 🟢 | | onchain_lm_v2 | 3.66G
3.66G
= | 1.62G
1.63G
+0.2% 🔴 | 409.9ms
412.7ms
+0.7% 🔴 | 16.0ms
15.8ms
-1.5% 🟢 | 11.5ms
11.2ms
-2.9% 🟢 | 72.5ms
72.7ms
+0.3% 🔴 | 285.8ms
285.3ms
-0.2% 🟢 | | usdt | 1.34G
1.34G
-0.1% 🟢 | 626.00M
627.55M
+0.2% 🔴 | 160.0ms
161.5ms
+0.9% 🔴 | 10.1ms
10.0ms
-0.9% 🟢 | 7.73ms
7.81ms
+1.1% 🔴 | 36.9ms
36.9ms
+0.1% 🔴 | 117.3ms
118.0ms
+0.6% 🔴 | | universal_router | 11.56G
11.56G
= | 5.60G
5.59G
-0.2% 🟢 | 1.382s
1.379s
-0.2% 🟢 | 21.2ms
21.0ms
-1.0% 🟢 | 16.9ms
18.0ms
+6.5% 🔴 | 122.0ms
122.7ms
+0.6% 🔴 | 479.9ms
478.3ms
-0.3% 🟢 | | aggregation_router_v6 | 35.12G
35.11G
= | 16.85G
16.84G
-0.1% 🟢 | 4.122s
4.121s
= | 33.7ms
33.7ms
= | 27.6ms
27.8ms
+0.6% 🔴 | 209.4ms
210.9ms
+0.7% 🔴 | 786.6ms
790.5ms
+0.5% 🔴 | | entry_point_v0_7 | 17.88G
17.89G
= | 8.68G
8.67G
-0.1% 🟢 | 2.138s
2.139s
= | 16.9ms
16.1ms
-4.8% 🟢 | 16.1ms
15.1ms
-6.1% 🟢 | 96.9ms
97.1ms
+0.2% 🔴 | 417.9ms
412.2ms
-1.4% 🟢 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.98G
2.98G
+0.1% 🔴 | 749.3ms
747.3ms
-0.3% 🟢 | 33.8ms
33.6ms
-0.6% 🟢 | 25.1ms
25.6ms
+2.0% 🔴 | 167.8ms
169.2ms
+0.9% 🔴 | 601.5ms
601.4ms
= | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 3.20G
3.18G
-0.7% 🟢 | 800.5ms
794.9ms
-0.7% 🟢 | 24.2ms
23.8ms
-1.6% 🟢 | 21.1ms
20.9ms
-0.7% 🟢 | 160.2ms
160.9ms
+0.4% 🔴 | 596.4ms
593.8ms
-0.4% 🟢 | | curve_3pool | 6.59G
6.59G
= | 3.01G
3.01G
+0.2% 🔴 | 757.7ms
756.1ms
-0.2% 🟢 | 33.3ms
33.3ms
-0.2% 🟢 | 21.5ms
21.9ms
+1.7% 🔴 | 129.1ms
130.8ms
+1.4% 🔴 | 655.1ms
655.2ms
= | | curve_tricrypto2 | 9.40G
9.40G
= | 4.24G
4.25G
+0.3% 🔴 | 1.055s
1.059s
+0.3% 🔴 | 30.8ms
30.5ms
-0.9% 🟢 | 24.9ms
25.1ms
+0.6% 🔴 | 144.6ms
146.1ms
+1.0% 🔴 | 640.4ms
644.8ms
+0.7% 🔴 | | balancer_vault | 19.03G
19.03G
= | 9.54G
9.49G
-0.5% 🟢 | 2.350s
2.340s
-0.4% 🟢 | 43.8ms
43.5ms
-0.6% 🟢 | 30.6ms
30.4ms
-0.4% 🟢 | 298.9ms
301.4ms
+0.8% 🔴 | 973.6ms
970.8ms
-0.3% 🟢 | | succinct_staking | 10.84G
10.84G
= | 5.15G
5.16G
+0.2% 🔴 | 1.271s
1.274s
+0.2% 🔴 | 31.3ms
31.0ms
-1.0% 🟢 | 18.7ms
18.7ms
+0.1% 🔴 | 129.7ms
130.9ms
+0.9% 🔴 | 481.5ms
479.5ms
-0.4% 🟢 | | **TOTAL** | **164.85G
164.83G
=** | **79.00G
78.97G
=** | **19.649s
19.655s
=** | **491.5ms
487.9ms
-0.7% 🟢** | **328.1ms
329.2ms
+0.4% 🔴** | **2.335s
2.352s
+0.7% 🔴** | **8.928s
8.920s
-0.1% 🟢** |
--- crates/evm2/src/evm/mod.rs | 128 +++++++++++++++++++++++++- crates/jit/runtime/src/evm2_evm.rs | 50 ++++++++-- crates/jit/runtime/src/runtime/mod.rs | 9 +- 3 files changed, 176 insertions(+), 11 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 45fccacaf..40822870b 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -195,11 +195,22 @@ pub trait InterpreterRunner: core::fmt::Debug + Send + Sync + ' /// /// Record execution errors with [`Interpreter::fail`]; the EVM extracts them after this /// method returns. + /// + /// The EVM calls this only while [`Self::supports`] holds for the active execution config; + /// other callers must check it first. fn run<'frame, 'host>( &self, interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, ) -> Option; + + /// Returns whether this runner can execute frames under `config`. + /// + /// The EVM calls this when the runner or the execution config changes, not per frame, and + /// skips the runner while it returns `false`. + fn supports(&self, _config: &ExecutionConfig) -> bool { + true + } } /// EVM host and transaction dispatcher. @@ -226,6 +237,11 @@ pub struct Evm<'a, T: EvmTypesHost> { inspect_system_calls: bool, #[derive_where(skip)] interpreter_runner: Option>>, + /// Whether `interpreter_runner` is set and supports `execution_config`. + /// + /// Refreshed whenever either changes. + #[derive_where(skip)] + runner_supported: bool, /// The currently running interpreter frame, if any. /// /// This is passed to the inspector call and create hooks as the parent frame. @@ -355,6 +371,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { inspector: None, inspect_system_calls: false, interpreter_runner: None, + runner_supported: false, current_frame: None, frame_checkpoint: None, running: false, @@ -414,6 +431,14 @@ impl<'a, T: EvmTypes> Evm<'a, T> { assert!(!self.running, "execution config cannot be modified during EVM execution"); } + #[inline] + fn refresh_runner_support(&mut self) { + self.runner_supported = self + .interpreter_runner + .as_ref() + .is_some_and(|runner| runner.supports(&self.execution_config)); + } + #[inline] const fn enter_execution(&mut self) -> ExecutionGuard<'_, 'a, T> { let was_running = self.running; @@ -594,6 +619,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.block = block; self.features = execution_config.version().features; self.execution_config = execution_config; + self.refresh_runner_support(); } #[inline] @@ -607,6 +633,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.spec_id = spec_id; self.features = execution_config.version().features; self.execution_config = execution_config; + // Refresh before the old registry and precompiles drop, so an unwinding destructor + // cannot leave a stale verdict. + self.refresh_runner_support(); self.registry = registry; self.precompiles = precompiles; self.evm_send = false; @@ -1017,6 +1046,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_interpreter_runner>(&mut self, runner: R) { self.assert_interpreter_runner_mutable(); self.interpreter_runner = Some(Arc::new(runner)); + self.refresh_runner_support(); } /// Sets the optional shared external interpreter runner. @@ -1024,13 +1054,16 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_shared_interpreter_runner(&mut self, runner: Arc>) { self.assert_interpreter_runner_mutable(); self.interpreter_runner = Some(runner); + self.refresh_runner_support(); } /// Removes the optional external interpreter runner. #[inline] pub fn clear_interpreter_runner(&mut self) -> Option>> { self.assert_interpreter_runner_mutable(); - self.interpreter_runner.take() + let runner = self.interpreter_runner.take(); + self.refresh_runner_support(); + runner } /// Returns the active EVM version. @@ -1729,10 +1762,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { .evm .current_frame .replace(NonNull::from(&mut *interp_ref).cast::>()); - let interpreter_runner = guard.evm.interpreter_runner.clone(); + let runner = + guard.evm.interpreter_runner.as_ref().filter(|_| guard.evm.runner_supported).cloned(); let stop = if let Some(inspector) = inspector { interp_ref.run_inspect(guard.evm, inspector) - } else if let Some(runner) = interpreter_runner + } else if let Some(runner) = runner && let Some(stop) = runner.run(interp_ref, guard.evm) { interp_ref.finish_run(stop) @@ -3310,6 +3344,94 @@ mod tests { assert_eq!(run_with_test_interpreter_runner(None, &[op::STOP]), InstrStop::Stop); } + #[derive(Debug)] + struct SpecBoundRunner { + spec_id: SpecId, + initial_call_depth: u16, + calls: Arc, + } + + impl InterpreterRunner for SpecBoundRunner { + fn run<'frame, 'host>( + &self, + _interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, + _host: &mut Evm<'host, BaseEvmTypes>, + ) -> Option { + self.calls.fetch_add(1, Ordering::Relaxed); + Some(InstrStop::Return) + } + + fn supports(&self, config: &ExecutionConfig) -> bool { + config.base_spec_id() == self.spec_id + && config.version().initial_call_depth == self.initial_call_depth + } + } + + #[test] + fn interpreter_runner_support_follows_runner_and_execution_config() { + let calls = Arc::new(AtomicUsize::new(0)); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + let message = MessageExt { + gas_limit: 30_000, + code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), + ..Default::default() + }; + let run = |evm: &mut Evm<'_, BaseEvmTypes>| { + let stop = evm.run_interpreter(&message).unwrap(); + (stop, calls.load(Ordering::Relaxed)) + }; + let set_spec = |evm: &mut Evm<'_, BaseEvmTypes>, spec_id: SpecId| { + evm.set_execution_config( + ExecutionConfig::for_base_spec::(spec_id), + spec_id, + TxRegistry::new(), + Precompiles::base(spec_id), + ); + }; + + evm.set_shared_interpreter_runner(Arc::new(SpecBoundRunner { + spec_id: SpecId::PRAGUE, + initial_call_depth: 0, + calls: Arc::clone(&calls), + })); + assert_eq!(run(&mut evm), (InstrStop::Stop, 0)); + + set_spec(&mut evm, SpecId::PRAGUE); + assert_eq!(run(&mut evm), (InstrStop::Return, 1)); + + set_spec(&mut evm, SpecId::OSAKA); + assert_eq!(run(&mut evm), (InstrStop::Stop, 1)); + + set_spec(&mut evm, SpecId::PRAGUE); + assert_eq!(run(&mut evm), (InstrStop::Return, 2)); + + let mut version = *Version::base(SpecId::PRAGUE); + version.initial_call_depth = 1; + evm.set_block_and_execution_config_within_spec( + BlockEnvExt::default(), + ExecutionConfig::for_spec_and_version(SpecId::PRAGUE, version), + ); + assert_eq!(run(&mut evm), (InstrStop::Stop, 2)); + + evm.set_block_and_execution_config_within_spec( + BlockEnvExt::default(), + ExecutionConfig::for_base_spec::(SpecId::PRAGUE), + ); + assert_eq!(run(&mut evm), (InstrStop::Return, 3)); + + let runner = evm.clear_interpreter_runner().unwrap(); + assert_eq!(run(&mut evm), (InstrStop::Stop, 3)); + + evm.set_shared_interpreter_runner(runner); + assert_eq!(run(&mut evm), (InstrStop::Return, 4)); + } + #[test] fn dispatches_transaction_with_dynamic_version() { fn handle_test_tx_version( diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 31ad7c6b1..515b7307c 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -2,7 +2,7 @@ use crate::runtime::{JitBackend, LookupDecision, LookupRequest, RuntimeCacheKey}; use evm2::{ - EvmTypesHost, InterpreterRunner, + EvmTypesHost, ExecutionConfig, InterpreterRunner, interpreter::{Host, InstrStop, Interpreter}, }; @@ -33,7 +33,18 @@ impl InterpreterRunner for JitInterpreterRunner { interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, ) -> Option { - run_interpreter(&self.backend, interpreter, host) + // The field layout the compiled code reads depends on the host type, so this check keeps + // direct calls memory safe. Direct callers must check `supports` for the gas schedule and + // opcode table. + if !self.backend.supports_host_type::() { + return None; + } + run_supported(&self.backend, interpreter, host) + } + + #[inline] + fn supports(&self, config: &ExecutionConfig) -> bool { + self.backend.supports_host::(config) } } @@ -46,13 +57,23 @@ pub fn run_interpreter<'frame, 'host, T: EvmTypesHost>( backend: &JitBackend, interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, +) -> Option { + if !backend.enabled() || !backend.supports_host::(host.execution_config()) { + return None; + } + run_supported(backend, interpreter, host) +} + +/// Like [`run_interpreter`], for a host whose execution config the backend already supports. +#[inline] +fn run_supported<'frame, 'host, T: EvmTypesHost>( + backend: &JitBackend, + interpreter: &mut Interpreter<'frame, 'host, T>, + host: &mut T::Host<'host>, ) -> Option { // Disabled runners must not hash code merely to discover that lookup is unavailable. // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if !backend.enabled() - || interpreter.gas().remaining() > i64::MAX as u64 - || !backend.supports_host::(host.execution_config()) - { + if !backend.enabled() || interpreter.gas().remaining() > i64::MAX as u64 { return None; } let code_hash = interpreter.original_bytecode_hash(); @@ -928,6 +949,23 @@ mod custom_host_tests { JitBackend::new(RuntimeConfig { blocking: true, ..Default::default() }).unwrap(); let mut fresh = Interpreter::::new(&message); assert_eq!(run_interpreter(&wrong, &mut fresh, &mut host), None); + let config = host.execution_config(); + let supports = |backend: &JitBackend| { + InterpreterRunner::::supports( + &JitInterpreterRunner::new(backend.clone()), + config, + ) + }; + assert!(supports(&backend)); + assert!(!supports(&wrong)); + assert_eq!( + InterpreterRunner::::run( + &JitInterpreterRunner::new(wrong.clone()), + &mut fresh, + &mut host, + ), + None + ); assert_eq!(fresh.run(&mut host).unwrap(), InstrStop::Return); assert_eq!(fresh.output(), expected_output); } diff --git a/crates/jit/runtime/src/runtime/mod.rs b/crates/jit/runtime/src/runtime/mod.rs index a3b410e12..ab0f5c41c 100644 --- a/crates/jit/runtime/src/runtime/mod.rs +++ b/crates/jit/runtime/src/runtime/mod.rs @@ -223,13 +223,18 @@ impl JitBackend { Ok(this) } + /// Checks that generated code uses the field layout of the interpreter type `T`. + #[inline] + pub(crate) fn supports_host_type(&self) -> bool { + self.inner.host.type_id == std::any::TypeId::of::() + } + /// Checks that generated code matches the interpreter type, gas schedule, and opcode table. pub(crate) fn supports_host( &self, config: &evm2::ExecutionConfig, ) -> bool { - let host = &self.inner.host; - host.type_id == std::any::TypeId::of::() + self.supports_host_type::() && config.version().gas_params == *self .inner From b635a397612a9caecf75572c11ab849d22a54b24 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:18:45 +0100 Subject: [PATCH 039/159] refactor(eest): drop dead env knobs and merge the stable flags (#937) The EEST harness carried configuration that nothing used or that overlapped. This removes the unused `tests/ethereum-tests` root and its path constant (the legacy roots come from the downloaded `legacytests` now), the empty `UNSUPPORTED_FORKS` list, and the separate `EVM2_STATETEST_STABLE` and `EVM2_BLOCKCHAINTEST_STABLE` flags. `EVM2_EEST_STABLE` is now the single stable switch, read by both runners and by `scripts/setup_test_fixtures.py`. Before, setting only one of the three made the script download stable fixtures while the other runner kept looking for the develop `main` root. The undocumented `SINGLE_THREAD` variable is now `EVM2_EEST_SINGLE_THREAD`, documented in `AGENTS.md` and the eest README. Compiled files run on a worker pool inside one test, so nextest's thread count cannot replace it. It is parsed like the other flags, so an empty value or `0` no longer counts as set. `cargo nextest list` for `evm2-eest` prints the same 28467 trials as main, and all of them pass on the interpreter. --- .gitignore | 2 -- AGENTS.md | 9 ++++--- crates/eest/README.md | 16 ++++++------ crates/eest/src/blockchaintest/env.rs | 5 +--- crates/eest/src/compiled.rs | 7 ++++-- crates/eest/src/env.rs | 36 +++------------------------ crates/eest/src/fixtures.rs | 2 +- crates/eest/src/forks.rs | 10 +++----- scripts/setup_test_fixtures.py | 2 +- 9 files changed, 27 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index f7ee5e08a..4fd772a08 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,6 @@ .vscode .idea /tmp -/tests/GeneralStateTests -/tests/BlockchainTests /test-fixtures __pycache__/ diff --git a/AGENTS.md b/AGENTS.md index a1e9c49c3..8fd51794f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,14 +44,15 @@ Cancun/Constantinople state tests. The release defaults to base overridable via `EEST_BASE_URL`); override `EEST_VERSION` and `EEST_TAR` to select a different release (for example a devnet), or clear either to disable it. The release covers frontier..amsterdam, so the EEST `main` develop suite is -opt-in via `EVM2_STATETEST_MAIN=1` (stable with `EVM2_STATETEST_STABLE=1`). Add -`EVM2_STATETEST_EEST_ONLY=1` to skip legacy fixtures. Use `EVM2_STATETEST_ROOT` -or `EVM2_BLOCKCHAINTEST_ROOT` for a single explicit root. +opt-in via `EVM2_STATETEST_MAIN=1` (stable with `EVM2_EEST_STABLE=1`, honored +by both runners). Add `EVM2_STATETEST_EEST_ONLY=1` to skip legacy fixtures. Use +`EVM2_STATETEST_ROOT` or `EVM2_BLOCKCHAINTEST_ROOT` for a single explicit root. Compiled EEST runs use a default subset when the full corpus is too expensive: AOT defaults to the `ci-aot` subset, and JIT defaults to the `ci-smoke` subset. Set `EVM2_COMPILED_EEST_SUBSET=all` to force full coverage, or set it to -`ci-aot` or `ci-smoke` to run those subsets explicitly. +`ci-aot` or `ci-smoke` to run those subsets explicitly. Set +`EVM2_EEST_SINGLE_THREAD=1` to run compiled files on one worker thread. To run additional tests from an arbitrary folder (or single file) without a test-name filter, use `./scripts/eest.sh `. Every JSON file found anywhere diff --git a/crates/eest/README.md b/crates/eest/README.md index fa65d56c2..2d3b7f0e5 100644 --- a/crates/eest/README.md +++ b/crates/eest/README.md @@ -25,7 +25,7 @@ Populate it with: By default this downloads the mainline execution-specs release fixtures (`tests@v21.0.1`, Frontier through Amsterdam) plus the legacy Cancun and Constantinople state tests. Set `EVM2_STATETEST_MAIN=1` to also -download the EEST `main` develop suite (stable with `EVM2_STATETEST_STABLE=1`), +download the EEST `main` develop suite (stable with `EVM2_EEST_STABLE=1`), or `EVM2_STATETEST_EEST_ONLY=1` to skip the legacy fixtures. The legacy fixtures come from the `ethereum/legacytests` commit that the @@ -82,6 +82,9 @@ cargo nextest run -p evm2-eest --features jit --test eest \ --ignore-default-filter blockchain_tests::aot ``` +Compiled files run on a worker pool inside each test, so nextest's thread count +does not limit it. Set `EVM2_EEST_SINGLE_THREAD=1` to use one worker thread. + ## State tests Run all discovered state tests with nextest: @@ -95,8 +98,8 @@ Cancun and Constantinople state tests, and `main/develop/state_tests` when the `main` suite has been downloaded. EEST develop includes the stable fixtures, so stable is not run separately. -Set `EVM2_STATETEST_STABLE=1` to download and run EEST stable fixtures instead -of develop. +Set `EVM2_EEST_STABLE=1` to download and run EEST stable fixtures instead of +develop. Both runners and the setup script read it. ## Blockchain tests @@ -111,9 +114,6 @@ plus `main/develop/blockchain_tests` when the `main` suite has been downloaded. Transition forks are skipped. Blocks with a `blockAccessList` build the block access list and check it against the fixture, including the header hash. -Set `EVM2_BLOCKCHAINTEST_STABLE=1` or `EVM2_EEST_STABLE=1` to use EEST stable -instead of develop for main blockchain fixtures. - ## Filtering The default nextest profile excludes this crate from workspace runs. Pass @@ -151,8 +151,8 @@ cargo nextest list -p evm2-eest --test eest --ignore-default-filter ``` For local experiments, `EVM2_STATETEST_ROOT`, `EVM2_BLOCKCHAINTEST_ROOT`, -`EVM2_STATETEST_STABLE`, `EVM2_BLOCKCHAINTEST_STABLE`, `EVM2_EEST_STABLE`, -`EVM2_TEST_FIXTURES`, and `SUBDIR` are supported as optional filters. +`EVM2_EEST_STABLE`, `EVM2_TEST_FIXTURES`, and `SUBDIR` are supported as optional +filters. No hardforks are skipped by default. Set `EVM2_SKIP_FORKS` to a comma-separated list of hardfork names (for example diff --git a/crates/eest/src/blockchaintest/env.rs b/crates/eest/src/blockchaintest/env.rs index bd070afbe..561c935bd 100644 --- a/crates/eest/src/blockchaintest/env.rs +++ b/crates/eest/src/blockchaintest/env.rs @@ -11,9 +11,6 @@ use std::{env, path::PathBuf}; /// Environment variable for the blockchain test root. pub(crate) const BLOCKCHAIN_TEST_ROOT_ENV: &str = "EVM2_BLOCKCHAINTEST_ROOT"; -/// Environment variable for selecting stable EEST fixtures instead of develop. -pub(crate) const BLOCKCHAINTEST_STABLE_ENV: &str = "EVM2_BLOCKCHAINTEST_STABLE"; - /// Optional environment variable for selecting a subdirectory under the test root. pub(crate) const BLOCKCHAIN_TEST_SUBDIR_ENV: &str = "SUBDIR"; @@ -53,7 +50,7 @@ pub(crate) fn blockchain_test_roots() -> Vec { /// Returns the default repo-relative blockchain-test roots. The `main` root is optional. pub(crate) fn default_blockchain_test_roots() -> Vec { let fixtures = fixtures_root(); - let main_path = if env_flag(BLOCKCHAINTEST_STABLE_ENV) || env_flag(EEST_STABLE_ENV) { + let main_path = if env_flag(EEST_STABLE_ENV) { fixtures.join("main/stable/blockchain_tests") } else { fixtures.join("main/develop/blockchain_tests") diff --git a/crates/eest/src/compiled.rs b/crates/eest/src/compiled.rs index c9c158d0a..a08f0c731 100644 --- a/crates/eest/src/compiled.rs +++ b/crates/eest/src/compiled.rs @@ -1,4 +1,4 @@ -use crate::execution::ExecutionResources; +use crate::{execution::ExecutionResources, fixtures::env_flag}; use evm2::evm::DbStatsCounts; use std::{ panic::{self, AssertUnwindSafe}, @@ -11,6 +11,9 @@ use std::{ time::{Duration, Instant}, }; +/// Environment variable forcing compiled EEST runs onto a single worker thread. +const SINGLE_THREAD_ENV: &str = "EVM2_EEST_SINGLE_THREAD"; + const WORKER_STACK_SIZE: usize = 16 * 1024 * 1024; #[derive(Clone, Copy, Debug, Default)] @@ -43,7 +46,7 @@ where return Ok(FileSummary::default()); } - let num_threads = if std::env::var_os("SINGLE_THREAD").is_some() { + let num_threads = if env_flag(SINGLE_THREAD_ENV) { 1 } else { thread::available_parallelism().map_or(1, |n| n.get().min(n_files)) diff --git a/crates/eest/src/env.rs b/crates/eest/src/env.rs index 05dcc8724..894ba5fd6 100644 --- a/crates/eest/src/env.rs +++ b/crates/eest/src/env.rs @@ -2,27 +2,18 @@ use crate::{ blockchaintest::env::explicit_blockchain_test_root_from_env, fixtures::{ EEST_ONLY_ENV, EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, - fixtures_root, workspace_relative, workspace_root, + fixtures_root, workspace_relative, }, harness::TestRoot, }; -use std::{ - env, - path::{Path, PathBuf}, -}; +use std::{env, path::PathBuf}; /// Environment variable for the state test root. pub(crate) const STATE_TEST_ROOT_ENV: &str = "EVM2_STATETEST_ROOT"; -/// Environment variable for selecting stable EEST fixtures instead of develop. -pub(crate) const STATETEST_STABLE_ENV: &str = "EVM2_STATETEST_STABLE"; - /// Optional environment variable for selecting a subdirectory under the test root. pub(crate) const STATE_TEST_SUBDIR_ENV: &str = "SUBDIR"; -/// Optional repo-relative ethereum/tests checkout path. -pub(crate) const DEFAULT_ETHEREUM_TESTS_PATH: &str = "tests/ethereum-tests"; - /// A named state-test root. pub(crate) type StateTestRoot = TestRoot; @@ -58,8 +49,7 @@ pub(crate) fn state_test_roots() -> Vec { /// exist. pub(crate) fn default_state_test_roots() -> Vec { let fixtures = fixtures_root(); - let ethereum_tests = workspace_root().join(DEFAULT_ETHEREUM_TESTS_PATH); - let eest_path = if env_flag(STATETEST_STABLE_ENV) || env_flag(EEST_STABLE_ENV) { + let eest_path = if env_flag(EEST_STABLE_ENV) { fixtures.join("main/stable/state_tests") } else { fixtures.join("main/develop/state_tests") @@ -91,26 +81,6 @@ pub(crate) fn default_state_test_roots() -> Vec { }); } - if let Some(path) = general_state_tests_path(ðereum_tests) { - roots.push(StateTestRoot { - name: "legacy::ethereum_tests".to_string(), - label: "ethereum/tests GeneralStateTests", - path, - }); - } - apply_subdir_to_roots(&mut roots, STATE_TEST_SUBDIR_ENV); roots } - -fn general_state_tests_path(root: &Path) -> Option { - let sibling = root.parent().map(|parent| parent.join("GeneralStateTests")); - if let Some(path) = sibling - && path.is_dir() - { - return Some(path); - } - - let path = root.join("GeneralStateTests"); - path.is_dir().then_some(path) -} diff --git a/crates/eest/src/fixtures.rs b/crates/eest/src/fixtures.rs index a334b122f..ab39b8447 100644 --- a/crates/eest/src/fixtures.rs +++ b/crates/eest/src/fixtures.rs @@ -7,7 +7,7 @@ use std::{ /// Environment variable for the downloaded fixture root. pub(crate) const TEST_FIXTURES_ENV: &str = "EVM2_TEST_FIXTURES"; -/// Generic EEST stable selector. +/// Environment variable for selecting stable EEST fixtures instead of develop. pub(crate) const EEST_STABLE_ENV: &str = "EVM2_EEST_STABLE"; /// Selector for execution-specs-only runs, where the setup script skips the legacy fixtures. diff --git a/crates/eest/src/forks.rs b/crates/eest/src/forks.rs index 395730cca..f89d07f8e 100644 --- a/crates/eest/src/forks.rs +++ b/crates/eest/src/forks.rs @@ -1,24 +1,20 @@ use evm2::SpecId; use std::{env, sync::OnceLock}; -/// Environment variable listing extra hardforks to skip, comma-separated by name. +/// Environment variable listing hardforks to skip, comma-separated by name. pub(crate) const SKIP_FORKS_ENV: &str = "EVM2_SKIP_FORKS"; -/// Hardforks skipped by default because evm2 does not support them yet. -const UNSUPPORTED_FORKS: &[SpecId] = &[]; - /// Returns whether all test cases targeting `spec` should be skipped. #[inline] pub(crate) fn is_fork_skipped(spec: SpecId) -> bool { skipped_forks().contains(&spec) } -/// Returns the skipped hardforks, combining [`UNSUPPORTED_FORKS`] with any -/// forks named in [`SKIP_FORKS_ENV`]. +/// Returns the hardforks named in [`SKIP_FORKS_ENV`]. fn skipped_forks() -> &'static [SpecId] { static SKIPPED: OnceLock> = OnceLock::new(); SKIPPED.get_or_init(|| { - let mut forks = UNSUPPORTED_FORKS.to_vec(); + let mut forks = Vec::new(); if let Ok(names) = env::var(SKIP_FORKS_ENV) { for name in names.split(',').map(str::trim).filter(|name| !name.is_empty()) { let Some(spec) = parse_fork(name) else { diff --git a/scripts/setup_test_fixtures.py b/scripts/setup_test_fixtures.py index c24001437..f9ff8786e 100755 --- a/scripts/setup_test_fixtures.py +++ b/scripts/setup_test_fixtures.py @@ -96,7 +96,7 @@ def env_flag(name: str) -> bool: def main_fixture() -> Fixture: - if env_flag("EVM2_STATETEST_STABLE") or env_flag("EVM2_BLOCKCHAINTEST_STABLE") or env_flag("EVM2_EEST_STABLE"): + if env_flag("EVM2_EEST_STABLE"): dest = MAIN_STABLE_DIR tar_name = os.environ.get("MAIN_TAR", "fixtures_stable.tar.gz") label = "main stable" From 082bccb0762698f72b468b138077756705d7006b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:19:02 +0100 Subject: [PATCH 040/159] refactor(evm2): return new-account-gas emptiness from load_account (#929) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Host::load_account` did not return what CALL and CREATE need. Its `AccountLoad::is_empty` misses the pre-EIP-161 rule that an account is empty only if it is absent and untouched, and its `exists` field had no reader. CALL and the EIP-8037 CREATE destination check therefore looked the account up a second time through `Host::target_is_empty_for_new_account_gas`. `AccountLoad` now carries `is_empty_for_new_account_gas` in place of `exists`, computed in `Evm::load_account` from the loaded account with one feature-bit test. CALL reads it from the load it already made, the CREATE destination check calls `load_account`, and the extra host method is removed from the interpreter, the JIT builtins and the test host. The destination address was already warmed by `prepare_create_message`, so the earlier load only moves that warm-up a few steps ahead inside the same frame. `Evm::selfdestruct` also asked `account_with_skip` to skip cold loads and then rejected a cold load again. The skip already returns `ColdLoadSkipped` for a cold, non-prewarmed target, so the second check could not fire. The `EIP2929` gate on the skip is gone too: before Berlin the cold and warm costs are zero, so no skip is requested, and `load_account` relies on the same fact. With a custom gas table that sets a cold cost without EIP-2929, SELFDESTRUCT now charges it, as BALANCE, EXTCODE* and CALL already did. Callgrind instruction counts of one transaction through the interpreter, from the `evm` bench in `crates/cli/benches` (`--test` mode, `x86-64-v3` build, counting from the timed root frame of the bench, base `1dc537e6` against this branch). Small benches count about 35,000 instructions of harness start-up that move by a few hundred between builds; a repeated base run reproduces its count exactly. The `erc20_transfer` increase comes from 16 more `realloc` copies inside libc (`_int_realloc`, a heap layout effect); the transaction run itself executes 7,000 instructions fewer once libc `memcpy` is set aside. | Bench | Base | Head | Change | | --- | ---: | ---: | ---: | | counter | 58,811 | 58,584 | -0.39% 🟢 | | weth | 65,732 | 65,999 | +0.41% 🔴 | | erc20_transfer | 28,841,379 | 29,151,258 | +1.07% 🔴 | | usdc_proxy | 59,547 | 59,439 | -0.18% 🟢 | | univ2_router | 58,834 | 58,904 | +0.12% 🔴 | | seaport | 58,652 | 58,750 | +0.17% 🔴 | | uniswap_v3_pool | 194,084 | 193,706 | -0.19% 🟢 | | snailtracer | 499,780,279 | 499,765,270 | -0.00% 🟢 | --- crates/evm2/src/evm/mod.rs | 33 ++++++------------- crates/evm2/src/evm/registry.rs | 10 +----- crates/evm2/src/interpreter/host.rs | 10 +----- .../src/interpreter/instructions/system.rs | 20 +++++------ crates/evm2/src/test_utils.rs | 24 +++++--------- crates/jit/builtins/src/lib.rs | 10 ++---- 6 files changed, 34 insertions(+), 73 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 40822870b..3c219bc3f 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1821,9 +1821,10 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { }; let is_cold = account.warm(); - let (balance, nonce, code_hash, exists, is_empty) = match account.get() { - Some(info) => (info.balance, info.nonce, info.code_hash, true, info.is_empty()), - None => (Word::ZERO, 0, B256::ZERO, false, true), + let is_empty_for_new_account_gas = account.is_empty_for_new_account_gas(self.features); + let (balance, nonce, code_hash, is_empty) = match account.get() { + Some(info) => (info.balance, info.nonce, info.code_hash, info.is_empty()), + None => (Word::ZERO, 0, B256::ZERO, true), }; // load code @@ -1837,21 +1838,13 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { nonce, code_hash, code, - exists, is_empty, + is_empty_for_new_account_gas, is_cold, _non_exhaustive: (), }) } - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result { - Ok(self.state.account(address)?.is_empty_for_new_account_gas(features)) - } - fn block_hash(&mut self, number: &Word) -> Result { self.state.block_hash(number) } @@ -1954,19 +1947,12 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { skip_cold_load: bool, ) -> Result { let features = self.features; - let eip2929 = features.contains(EvmFeatures::EIP2929); let (is_cold, target_is_empty_for_new_account_gas) = - match self.state.account_with_skip(target, eip2929 && skip_cold_load) { - Ok(mut account) => { - let is_cold = account.warm() && eip2929; - (is_cold, account.is_empty_for_new_account_gas(features)) - } + match self.state.account_with_skip(target, skip_cold_load) { + Ok(mut account) => (account.warm(), account.is_empty_for_new_account_gas(features)), Err(LoadError::ColdLoadSkipped) => return Err(InstrStop::OutOfGas.into()), Err(code) => return Err(code.into()), }; - if skip_cold_load && is_cold { - return Err(InstrStop::OutOfGas.into()); - } let (previously_destroyed, balance, should_destroy) = { let account = self.state.account(contract)?; let should_destroy = !features.contains(EvmFeatures::EIP6780) || account.is_created(); @@ -2011,10 +1997,11 @@ pub struct AccountLoad { pub code_hash: B256, /// Account bytecode. pub code: Bytecode, - /// Whether the account exists in state. - pub exists: bool, /// Whether the account is empty. pub is_empty: bool, + /// Whether the account is empty for new-account gas: absent or empty under EIP-161, absent + /// and untouched before it. + pub is_empty_for_new_account_gas: bool, /// Whether the account access was cold. pub is_cold: bool, #[doc(hidden)] // Not public API. Please use an existing constructor. diff --git a/crates/evm2/src/evm/registry.rs b/crates/evm2/src/evm/registry.rs index e03e67b27..205de0ee1 100644 --- a/crates/evm2/src/evm/registry.rs +++ b/crates/evm2/src/evm/registry.rs @@ -422,7 +422,7 @@ impl TxRegistry { mod tests { use super::*; use crate::{ - BaseEvmConfigSelector, EvmFeatures, EvmTypesHost, ExecutionConfig, SpecId, + BaseEvmConfigSelector, EvmTypesHost, ExecutionConfig, SpecId, env::{BlockEnv, BlockEnvExt, TxEnv}, evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, interpreter::{Host, Message, MessageResult, Word}, @@ -502,14 +502,6 @@ mod tests { unimplemented!() } - fn target_is_empty_for_new_account_gas( - &mut self, - _address: &Address, - _features: EvmFeatures, - ) -> Result { - unimplemented!() - } - fn block_hash(&mut self, _number: &Word) -> Result { unimplemented!() } diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index ffcd414ff..62e2ad77c 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -1,7 +1,6 @@ use super::{GasTracker, InstrStop, Message, Result, Word}; use crate::{ - BaseEvmTypes, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, ExecutionError, - HostError, SpecId, + BaseEvmTypes, DatabaseError, EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, env::{BlockEnv, TxEnv}, evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, }; @@ -141,13 +140,6 @@ pub trait Host { skip_cold_load: bool, ) -> Result; - /// Returns whether an account is empty/non-existent for new-account gas checks. - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result; - /// Returns a historical block hash. fn block_hash(&mut self, number: &Word) -> Result; diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index c4e99e195..f58e58df9 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -115,14 +115,9 @@ fn load_acc_and_calc_gas( } let features = state.version().features; let mut new_account_state_gas = 0; - // Check emptiness only when new-account gas can apply: after EIP-161 a zero-value call never - // pays it, so it skips the extra account lookup. if create_empty_account && (!features.contains(EvmFeatures::EIP161) || transfers_value) - && state - .host() - .target_is_empty_for_new_account_gas(&to, features) - .map_err(|error| state.fail(error))? + && account.is_empty_for_new_account_gas { cost += state.gas_params().get(GasId::NewAccountCost); // EIP-8037: value transfer to a new account also charges state gas from @@ -427,11 +422,11 @@ pub fn create_with( // The destination is loaded here and made warm. Balance, nonce, and depth pre-access // failures return above before the destination is accessed. - let features = state.version().features; if state .host() - .target_is_empty_for_new_account_gas(&destination, features) + .load_account(&destination, false, false) .map_err(|error| state.fail(error))? + .is_empty_for_new_account_gas { gas.spend_state(state.gas_params().create_state_gas())?; charged_create_state_gas = true; @@ -999,6 +994,7 @@ mod tests { fn create_too_deep_clears_return_data_and_skips_destination_read_eip8037() { // EIP-8037: a create failing the depth pre-check must not access the destination — // the read would leak the address into the EIP-7928 block access list. + let caller = Address::from([0x11; 20]); let mut host = TestHost { exists: false, ..Default::default() }; let mut code = Vec::new(); push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); @@ -1007,12 +1003,16 @@ mod tests { let interp = run(RunConfig::new(code) .host(&mut host) .spec(SpecId::AMSTERDAM) - .message(MessageExt { depth: CALL_DEPTH_LIMIT, ..Default::default() }) + .message(MessageExt { + depth: CALL_DEPTH_LIMIT, + destination: caller, + ..Default::default() + }) .return_data(Bytes::from_static(b"stale")) .gas_limit(50_000)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::ZERO, Word::ZERO]); - assert!(host.new_account_checks.is_empty()); + assert_eq!(host.account_loads, [caller]); } #[test] diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index 25dd584ef..df4aaf886 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -54,7 +54,7 @@ pub(crate) struct TestHost { pub(crate) calls: Vec>, pub(crate) call_static_flags: Vec, pub(crate) selfdestructs: Vec<(Address, Address, bool)>, - pub(crate) new_account_checks: Vec
, + pub(crate) account_loads: Vec
, } impl Default for TestHost { @@ -87,7 +87,7 @@ impl Default for TestHost { calls: Vec::new(), call_static_flags: Vec::new(), selfdestructs: Vec::new(), - new_account_checks: Vec::new(), + account_loads: Vec::new(), } } } @@ -118,6 +118,8 @@ impl Host for TestHost { if skip_cold_load && self.is_cold { return Err(InstrStop::OutOfGas.into()); } + self.account_loads.push(*address); + let features = self.execution_config.version().features; Ok(AccountLoad { balance: address.into_word().into(), nonce: 0, @@ -127,25 +129,17 @@ impl Host for TestHost { } else { Bytecode::default() }, - exists: self.exists, is_empty: self.is_empty, + is_empty_for_new_account_gas: if features.contains(EvmFeatures::EIP161) { + !self.exists || self.is_empty + } else { + !self.exists && !self.is_touched + }, is_cold: self.is_cold, _non_exhaustive: (), }) } - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result { - self.new_account_checks.push(*address); - if features.contains(EvmFeatures::EIP161) { - return Ok(!self.exists || self.is_empty); - } - Ok(!self.exists && !self.is_touched) - } - fn block_hash(&mut self, number: &Word) -> Result { if self.missing_block_hash { return Err(DatabaseError::new(crate::AnyError::from("missing block hash"), true)); diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 0a14f6fea..cb67c32cf 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -667,8 +667,9 @@ pub unsafe fn __revmc_builtin_create( // failures return above before the destination is accessed. if ecx .host() - .target_is_empty_for_new_account_gas(&destination, version.features) + .load_account(&destination, false, false) .map_err(|error| ecx.interpreter_mut().fail(error))? + .is_empty_for_new_account_gas { ecx.gas.spend_state(version.gas_params.create_state_gas())?; charged_create_state_gas = true; @@ -893,14 +894,9 @@ fn load_acc_and_calc_gas( code_address = delegated_address; } let features = ecx.version().features; - // Check emptiness only when new-account gas can apply: after EIP-161 a zero-value call never - // pays it, so it skips the extra account lookup. if create_empty_account && (!features.contains(EvmFeatures::EIP161) || transfers_value) - && ecx - .host() - .target_is_empty_for_new_account_gas(&to, features) - .map_err(|error| ecx.interpreter_mut().fail(error))? + && account.is_empty_for_new_account_gas { cost += version.gas_params.get(GasId::NewAccountCost); if features.contains(EvmFeatures::EIP8037) && transfers_value { From fe0209f05b2172e51afe2e125a6ec8a65bad7cb4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:19:12 +0100 Subject: [PATCH 041/159] fix(fuzzer): gate program snippets on the opcodes' own forks (#933) The fuzzer gated calls, creates, precompile calls, returndata and the 0xFE and truncated-PUSH snippets on Spurious Dragon, although CALL, CALLCODE, CREATE and INVALID exist in Frontier and DELEGATECALL in Homestead. Before Spurious Dragon these snippets appeared one time in twenty, tagged `FORK_INVALID_OPCODE`, which is the wrong label for valid opcodes. SHL, SHR, SAR, EXTCODEHASH and CREATE2 were gated on Istanbul instead of Constantinople. The spec generator never picked Petersburg or Merge, so the wrong gates stayed hidden. Each snippet now asks `OpcodeConfig::base_for_spec` whether its opcodes are defined in the fork, so the gate follows the interpreter instead of a hand-written table. `FORK_INVALID_OPCODE` is now set only when an opcode is emitted in a fork that does not define it. The spec generator now also picks Petersburg and Merge. I ran the fuzzer across all 15 forks with the default and the four forced dispatch backends (about 68M cases in total). It found no mismatches. --- crates/cli/src/fuzzer/case.rs | 18 ++--- crates/cli/src/fuzzer/program.rs | 119 +++++++++++++++++++------------ 2 files changed, 83 insertions(+), 54 deletions(-) diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 19e0d7a81..48425b87e 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -96,19 +96,21 @@ impl CaseGenerator { pub(crate) fn generate(&mut self, rng: &mut Gen) -> &EvmCase { let Self { case, program, address_pool, call_pool } = self; let EvmCase { spec, block, tx, extra_txs, features, accounts } = case; - *spec = match rng.range(13) { + *spec = match rng.range(15) { 0 => SpecId::FRONTIER, 1 => SpecId::HOMESTEAD, 2 => SpecId::TANGERINE, 3 => SpecId::SPURIOUS_DRAGON, 4 => SpecId::BYZANTIUM, - 5 => SpecId::ISTANBUL, - 6 => SpecId::BERLIN, - 7 => SpecId::LONDON, - 8 => SpecId::SHANGHAI, - 9 => SpecId::CANCUN, - 10 => SpecId::PRAGUE, - 11 => SpecId::OSAKA, + 5 => SpecId::PETERSBURG, + 6 => SpecId::ISTANBUL, + 7 => SpecId::BERLIN, + 8 => SpecId::LONDON, + 9 => SpecId::MERGE, + 10 => SpecId::SHANGHAI, + 11 => SpecId::CANCUN, + 12 => SpecId::PRAGUE, + 13 => SpecId::OSAKA, _ => SpecId::AMSTERDAM, }; let spec = *spec; diff --git a/crates/cli/src/fuzzer/program.rs b/crates/cli/src/fuzzer/program.rs index e7187c91b..823233075 100644 --- a/crates/cli/src/fuzzer/program.rs +++ b/crates/cli/src/fuzzer/program.rs @@ -1,12 +1,13 @@ use crate::fuzzer::{features::FuzzFeatures, precompile, rng::Gen}; use alloy_primitives::{Address, Bytes, U256}; -use evm2::{SpecId, interpreter::op}; +use evm2::{BaseEvmTypes, OpcodeConfig, SpecId, interpreter::op}; #[derive(Default)] pub(crate) struct Program { code: Vec, stack_height: usize, features: FuzzFeatures, + spec: SpecId, } impl Program { @@ -20,6 +21,7 @@ impl Program { self.code.clear(); self.stack_height = 0; self.features = FuzzFeatures::default(); + self.spec = spec; let statements = rng.range_inclusive(1, 48); for _ in 0..statements { match rng.range(100) { @@ -30,31 +32,16 @@ impl Program { 69..=73 => self.calldata(rng), 74..=79 => self.external_account(rng, spec, addresses), 80..=83 => self.log(rng), - 84..=86 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.precompile_call(rng, spec) - } - 84..=86 => self.literal(rng, spec), - 87..=89 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.generic_call(rng, spec, call_addresses) - } - 87..=89 => self.literal(rng, spec), - 90..=91 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.returndata(rng, spec, call_addresses) - } - 90..=91 => self.literal(rng, spec), - 92..=93 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.create(rng, spec) - } - 92..=93 => self.literal(rng, spec), + 84..=86 => self.precompile_call(rng, spec), + 87..=89 => self.generic_call(rng, spec, call_addresses), + 90..=91 => self.returndata(rng, spec, call_addresses), + 92..=93 => self.create(rng, spec), 94..=95 => self.cancun(rng, spec), 96 => self.jump(rng), 97 if rng.one_in(3) => self.stack_shuffle(rng, spec), 97 if rng.one_in(2) => self.selfdestruct(rng, addresses), 97 => self.stack_cleanup(), - 98 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.raw_invalidish(rng) - } - 98 => self.literal(rng, spec), + 98 => self.raw_invalidish(rng), _ => self.literal(rng, spec), } while self.stack_height > 16 { @@ -63,21 +50,22 @@ impl Program { } match rng.range(10) { 0 => self.finish_return(false), - 1 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => self.finish_return(true), + 1 if self.allow_opcodes(rng, spec, &[op::REVERT]) => self.finish_return(true), _ => self.emit(op::STOP, 0, 0), } (Bytes::copy_from_slice(&self.code), self.features) } - fn allow_fork_feature(&mut self, rng: &mut Gen, spec: SpecId, since: SpecId) -> bool { - if spec.enables(since) { - return true; - } - if rng.one_in(20) { - self.mark(FuzzFeatures::FORK_INVALID_OPCODE); + /// Returns whether a snippet using `opcodes` may be emitted for `spec`. + /// + /// Opcodes the fork does not define are emitted once in 20 to cover their invalid-opcode + /// behavior. + fn allow_opcodes(&self, rng: &mut Gen, spec: SpecId, opcodes: &[u8]) -> bool { + let config = OpcodeConfig::::base_for_spec(spec); + if opcodes.iter().all(|&opcode| config.is_defined(opcode)) { return true; } - false + rng.one_in(20) } fn mark(&mut self, feature: FuzzFeatures) { @@ -109,14 +97,14 @@ impl Program { op::XOR, op::BYTE, ]; - if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if self.allow_opcodes(rng, spec, &[op::SHL, op::SHR, op::SAR]) { ops.extend([op::SHL, op::SHR, op::SAR]); } self.emit(rng.pick(&ops), 2, 1); } 17..=18 => { self.push_word(rng.biased_word()); - let opcode = if self.allow_fork_feature(rng, spec, SpecId::OSAKA) { + let opcode = if self.allow_opcodes(rng, spec, &[op::CLZ]) { rng.pick(&[op::ISZERO, op::NOT, op::CLZ]) } else if rng.one_in(2) { op::ISZERO @@ -166,7 +154,7 @@ impl Program { self.push_u64(offset); self.emit(op::CODECOPY, 3, 0); } - 6 if self.allow_fork_feature(rng, spec, SpecId::CANCUN) => { + 6 if self.allow_opcodes(rng, spec, &[op::MCOPY]) => { self.push_u64(rng.pick(&[0, 1, 31, 32, 64])); self.push_u64(rng.pick(&[0, 32, 64, 96])); self.push_u64(offset); @@ -206,13 +194,13 @@ impl Program { op::GASLIMIT, op::GAS, ]; - if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if self.allow_opcodes(rng, spec, &[op::CHAINID]) { ops.push(op::CHAINID); } - if self.allow_fork_feature(rng, spec, SpecId::LONDON) { + if self.allow_opcodes(rng, spec, &[op::BASEFEE]) { ops.push(op::BASEFEE); } - if self.allow_fork_feature(rng, spec, SpecId::AMSTERDAM) { + if self.allow_opcodes(rng, spec, &[op::SLOTNUM]) { ops.push(op::SLOTNUM); } self.emit(rng.pick(&ops), 0, 1); @@ -251,7 +239,7 @@ impl Program { self.push_address(address); self.emit(op::EXTCODESIZE, 1, 1); } - 2 if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) => { + 2 if self.allow_opcodes(rng, spec, &[op::EXTCODEHASH]) => { self.push_address(address); self.emit(op::EXTCODEHASH, 1, 1); } @@ -262,7 +250,7 @@ impl Program { self.push_address(address); self.emit(op::EXTCODECOPY, 4, 0); } - _ if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) => { + _ if self.allow_opcodes(rng, spec, &[op::SELFBALANCE]) => { self.emit(op::SELFBALANCE, 0, 1) } _ => { @@ -307,7 +295,7 @@ impl Program { let return_offset: u64 = rng.pick(&[0, 32, 64, 128]); let return_len: u64 = rng.pick(&[0, 1, 20, 32, 64, 96, 128, 256]); let gas: u64 = rng.pick(&[0, 1, 3_000, 10_000, 50_000, 200_000, 2_000_000]); - if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) && rng.one_in(2) { + if self.allow_opcodes(rng, spec, &[op::STATICCALL]) && rng.one_in(2) { self.mark(FuzzFeatures::PRECOMPILE_STATICCALL); self.push_u64(return_len); self.push_u64(return_offset); @@ -343,7 +331,7 @@ impl Program { let return_len: u64 = rng.pick(&[0, 1, 4, 32, 64]); let gas: u64 = rng.pick(&[0, 1, 2_300, 10_000, 50_000, 200_000]); match rng.range(4) { - 0 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => { + 0 if self.allow_opcodes(rng, spec, &[op::STATICCALL]) => { self.push_u64(return_len); self.push_u64(return_offset); self.push_u64(input_len); @@ -352,7 +340,7 @@ impl Program { self.push_u64(gas); self.emit(op::STATICCALL, 6, 1); } - 1 if self.allow_fork_feature(rng, spec, SpecId::HOMESTEAD) => { + 1 if self.allow_opcodes(rng, spec, &[op::DELEGATECALL]) => { self.push_u64(return_len); self.push_u64(return_offset); self.push_u64(input_len); @@ -385,7 +373,7 @@ impl Program { } fn returndata(&mut self, rng: &mut Gen, spec: SpecId, call_addresses: &[Address]) { - if !self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) { + if !self.allow_opcodes(rng, spec, &[op::RETURNDATASIZE, op::RETURNDATACOPY]) { self.literal(rng, spec); return; } @@ -404,7 +392,7 @@ impl Program { } fn cancun(&mut self, rng: &mut Gen, spec: SpecId) { - if !self.allow_fork_feature(rng, spec, SpecId::CANCUN) { + if !self.allow_opcodes(rng, spec, &[op::TLOAD, op::TSTORE, op::BLOBHASH, op::BLOBBASEFEE]) { self.literal(rng, spec); return; } @@ -431,7 +419,8 @@ impl Program { 0 => Vec::new(), 1 => vec![op::STOP], 2 => vec![op::PUSH1, 0, op::PUSH1, 0, op::RETURN], - 3 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => { + 3 if self.allow_opcodes(rng, spec, &[op::REVERT]) => { + self.mark_opcode(op::REVERT); vec![op::PUSH1, 0, op::PUSH1, 0, op::REVERT] } _ => vec![ @@ -453,7 +442,7 @@ impl Program { self.push_u64(offset); self.emit(op::MSTORE, 2, 0); } - if rng.one_in(2) || !self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if rng.one_in(2) || !self.allow_opcodes(rng, spec, &[op::CREATE2]) { self.push_u64(initcode.len() as u64); self.push_u64(offset); self.push_u64(rng.pick(&[0, 0, 0, 1])); @@ -534,7 +523,7 @@ impl Program { while self.stack_height < 2 { self.push_word(rng.biased_word()); } - if self.allow_fork_feature(rng, spec, SpecId::AMSTERDAM) && rng.one_in(3) { + if self.allow_opcodes(rng, spec, &[op::DUPN, op::SWAPN, op::EXCHANGE]) && rng.one_in(3) { self.relative_stack_shuffle(rng); } else if rng.one_in(2) { let n = rng.range_inclusive(1, self.stack_height.min(16)); @@ -601,7 +590,7 @@ impl Program { } fn literal(&mut self, rng: &mut Gen, spec: SpecId) { - if self.allow_fork_feature(rng, spec, SpecId::SHANGHAI) && rng.one_in(8) { + if self.allow_opcodes(rng, spec, &[op::PUSH0]) && rng.one_in(8) { self.emit(op::PUSH0, 0, 1); } else if rng.one_in(8) { self.push_random_width_word(rng); @@ -721,6 +710,11 @@ impl Program { op::SELFDESTRUCT => self.mark(FuzzFeatures::SELFDESTRUCT), _ => {} } + if opcode != op::INVALID + && !OpcodeConfig::::base_for_spec(self.spec).is_defined(opcode) + { + self.mark(FuzzFeatures::FORK_INVALID_OPCODE); + } } } @@ -763,4 +757,37 @@ mod tests { assert_eq!(program.features, FuzzFeatures::PUSH); } } + + #[test] + fn pre_spurious_dragon_programs_use_frontier_opcodes() { + let addresses = [Address::with_last_byte(1)]; + let mut rng = Gen::new(1); + let mut program = Program::default(); + let wanted = FuzzFeatures::CALL | FuzzFeatures::CREATE | FuzzFeatures::INVALID; + let mut seen = FuzzFeatures::default(); + for _ in 0..256 { + let (_, features) = + program.generate(&mut rng, SpecId::FRONTIER, &addresses, &addresses); + if !features.contains(FuzzFeatures::FORK_INVALID_OPCODE) { + seen |= features; + } + } + assert!(seen.contains(wanted), "{seen:?}"); + } + + #[test] + fn initcode_with_undefined_opcode_is_tagged() { + let revert_initcode = [op::PUSH1, 0, op::PUSH1, 0, op::REVERT]; + let mut rng = Gen::new(1); + let mut found = false; + for _ in 0..1024 { + let mut program = Program { spec: SpecId::FRONTIER, ..Default::default() }; + program.create(&mut rng, SpecId::FRONTIER); + if program.code.windows(revert_initcode.len()).any(|code| code == revert_initcode) { + found = true; + assert!(program.features.contains(FuzzFeatures::FORK_INVALID_OPCODE)); + } + } + assert!(found); + } } From 35fdb1e837b900f0028d1867972a4ec832ef91fc Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:19:23 +0100 Subject: [PATCH 042/159] fix(evm): validate memory limit and refund quotient when installing a version (#922) `ExecutionConfig::with_version` validated `initial_call_depth` but nothing else, so two instructions guarded against bad `Version` values every time they ran. RETURN and REVERT refused memory above 4 GiB because frame output ranges are `Range`, which meant a `memory_limit` above `u32::MAX` applied to those two opcodes only. `final_refund` turned a zero `MaxRefundQuotient` into one. This moves the existing check into `Version::assert_valid` and adds the refund quotient check there: it must be non-zero. `with_version` calls it and clamps `memory_limit` to `u32::MAX`, and it is the only path that installs a dynamic version. `resize_memory` now enforces the limit for every instruction, so the RETURN/REVERT check (in the interpreter and the JIT builtins) and the `final_refund` fallback are deleted. Instruction counts under callgrind for the measured part of each `evm2-cli` interpreter bench, base against this PR. The same binary varies by about 4,000 instructions between runs, so the differences are noise. | Bench | Base | PR | Change | | --- | ---: | ---: | ---: | | `erc20_transfer` | 565,300,167 | 565,291,728 | -8,439 | | `uniswap_v2_pair` | 541,028,224 | 541,013,808 | -14,416 | | `weth` | 475,302,749 | 475,298,071 | -4,678 | | `counter` | 33,572,781 | 33,575,044 | +2,263 | ## Behavior change when a version is installed A `memory_limit` above `u32::MAX` used to apply to every instruction except RETURN and REVERT, which refused memory above 4 GiB. `ExecutionConfig::with_version`, and through it `ExecutionConfig::for_spec_and_version`, now clamps the limit to `u32::MAX`, so memory stops at 4 GiB at resize for every instruction. Nothing panics for a large limit. A zero `MaxRefundQuotient` used to count as one. It is a programming error that would divide by zero, so installing it now panics in the same two calls, before any EVM exists. It is the only remaining panic, together with the existing `initial_call_depth` check. These two calls are the only public paths that reach `Version::assert_valid`. `ExecutionConfig::for_base_spec`, `for_custom_spec` and `for_config` build from the base versions, which are valid, and `Evm::new` and the `set_execution_config` family take an already built `ExecutionConfig`. `with_version` is `const`, so a zero quotient in a const context fails at compile time. --- crates/evm2/src/evm/config.rs | 17 ++++--- crates/evm2/src/interpreter/host.rs | 2 - .../src/interpreter/instructions/control.rs | 3 -- crates/evm2/src/version/mod.rs | 46 +++++++++++++++++-- crates/jit/builtins/src/lib.rs | 6 --- 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/crates/evm2/src/evm/config.rs b/crates/evm2/src/evm/config.rs index 08dcb70a5..fed42dd91 100644 --- a/crates/evm2/src/evm/config.rs +++ b/crates/evm2/src/evm/config.rs @@ -2,7 +2,6 @@ use crate::{ Evm, OpcodeConfig, OpcodeView, SpecId, - constants::CALL_DEPTH_LIMIT, ethereum::TxEnvelope, interpreter::{ Host, @@ -189,17 +188,17 @@ impl ExecutionConfig { /// Replaces the runtime version data while keeping the same dispatch table. /// + /// Clamps [`Version::memory_limit`] to [`u32::MAX`]. + /// /// # Panics /// - /// Panics if [`Version::initial_call_depth`] leaves no room for the full call budget and - /// the attempted-too-deep message, i.e. if it exceeds `u16::MAX - CALL_DEPTH_LIMIT - 1` - /// (64510). + /// Panics if `version` is invalid; see [`Version::assert_valid`]. #[inline] - pub const fn with_version(mut self, version: Version) -> Self { - assert!( - version.initial_call_depth < u16::MAX - CALL_DEPTH_LIMIT, - "initial call depth must leave room for 1024 descendants and the rejected attempt" - ); + pub const fn with_version(mut self, mut version: Version) -> Self { + version.assert_valid(); + if version.memory_limit > u32::MAX as u64 { + version.memory_limit = u32::MAX as u64; + } self.version = version; self } diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index 62e2ad77c..c5b131bd8 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -70,7 +70,6 @@ impl MessageResultExt { if self.gas.refunded() <= 0 { return 0; } - let max_refund_quotient = if max_refund_quotient == 0 { 1 } else { max_refund_quotient }; // EIP-8037: the unused state-gas reservoir is reimbursed to the caller, so it // is not part of the gas actually spent. The EIP-3529 refund cap is a fraction // of the gas the transaction truly consumed, so the reservoir must be excluded @@ -202,6 +201,5 @@ mod tests { assert_eq!(result.final_refund(100, 2), 40); assert_eq!(result.final_refund(100, 5), 16); assert_eq!(result.final_refund(100, 1), 60); - assert_eq!(result.final_refund(100, 0), 60); } } diff --git a/crates/evm2/src/interpreter/instructions/control.rs b/crates/evm2/src/interpreter/instructions/control.rs index e90b6d339..9d48878e9 100644 --- a/crates/evm2/src/interpreter/instructions/control.rs +++ b/crates/evm2/src/interpreter/instructions/control.rs @@ -77,9 +77,6 @@ fn return_inner( let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG); }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG); - } cx.state.resize_memory(cx.gas, offset, len)?; offset as u32..end as u32 } else { diff --git a/crates/evm2/src/version/mod.rs b/crates/evm2/src/version/mod.rs index 3962602da..015c51d14 100644 --- a/crates/evm2/src/version/mod.rs +++ b/crates/evm2/src/version/mod.rs @@ -38,6 +38,10 @@ pub struct Version { /// Transaction gas limit cap. pub tx_gas_limit_cap: u64, /// Hard memory limit in bytes. + /// + /// Frame output ranges are `u32`, so + /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) clamps a larger + /// limit to [`u32::MAX`], and memory stops growing there. pub memory_limit: u64, /// Maximum deployed contract bytecode size. pub max_code_size: usize, @@ -50,8 +54,6 @@ pub struct Version { /// Nested transaction roots can retain their outer call depth while receiving a fresh /// [`CALL_DEPTH_LIMIT`]-descendant budget. This must leave room in `u16` for that budget /// and the attempted-too-deep message: at most `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). - /// Installing a version with a larger value through - /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) panics. pub initial_call_depth: u16, #[doc(hidden)] // Not public API. Please use an existing constructor. @@ -103,10 +105,31 @@ impl Version { } } + /// Panics if a field holds a value the interpreter cannot run with. + /// + /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) calls this when it + /// installs a version, so instructions can rely on these invariants: + /// + /// - [`initial_call_depth`](Self::initial_call_depth) is at most `u16::MAX - CALL_DEPTH_LIMIT - + /// 1` (64510). + /// - [`GasId::MaxRefundQuotient`] is non-zero. + #[inline] + pub const fn assert_valid(&self) { + assert!( + self.initial_call_depth < u16::MAX - CALL_DEPTH_LIMIT, + "initial call depth must leave room for 1024 descendants and the rejected attempt" + ); + assert!( + self.gas_params.get(GasId::MaxRefundQuotient) != 0, + "max refund quotient must be non-zero" + ); + } + /// Returns the deepest permitted inspector-visible call depth. /// /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) validates - /// [`Self::initial_call_depth`], so this cannot overflow for an installed version. + /// [`Self::initial_call_depth`] through [`Self::assert_valid`], so this cannot overflow for an + /// installed version. #[inline] pub const fn max_call_depth(&self) -> u16 { self.initial_call_depth + CALL_DEPTH_LIMIT @@ -407,6 +430,23 @@ mod tests { version.initial_call_depth = u16::MAX - CALL_DEPTH_LIMIT; crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); } + + #[test] + fn execution_config_clamps_memory_limit_to_u32() { + let mut version = Version::new(SpecId::CANCUN); + version.memory_limit = u64::MAX; + let config = + crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); + assert_eq!(config.version().memory_limit, u32::MAX as u64); + } + + #[test] + #[should_panic(expected = "max refund quotient must be non-zero")] + fn execution_config_rejects_zero_max_refund_quotient() { + let mut version = Version::new(SpecId::CANCUN); + version.gas_params.set(GasId::MaxRefundQuotient, 0); + crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); + } } /// EIP-8037 cost per state byte (CPSB) for Glamsterdam. diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index cb67c32cf..240824232 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -933,9 +933,6 @@ pub unsafe fn __revmc_builtin_do_return( let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG.into()); }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG.into()); - } ensure_memory(ecx, offset, len)?; offset as u32..end as u32 } else { @@ -958,9 +955,6 @@ pub unsafe fn __revmc_builtin_do_return_cc( let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG.into()); }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG.into()); - } ensure_memory(ecx, offset, len)?; offset as u32..end as u32 } else { From 2920bf2e071ff0ec45feea7d45ae0bbeb722d933 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:19:33 +0100 Subject: [PATCH 043/159] refactor(evm): verify erased Send types instead of tracking mutation (#930) `Evm::evm_send` recorded that `evm_is_send` had checked the erased database, precompile provider and inspector as `Send`, and thirteen mutable accessors cleared it in case they swapped one for a non-`Send` type. That made `state_mut()` and even `clear_inspector()` break async execution, and the JS tracer, which reads storage through `state_mut()` on every step, could not run in a `Send` future. `evm_is_send` and `evm_is_send_with_inspector` now store the `TypeId`s they verified, and the async entry points compare them with the current erased values before running. A swap to a different type fails the check on its own, and a swap to the same type is still `Send`, so none of the accessors needs to clear anything. The stored ids and the `evm_is_send*` methods exist only with the `async` feature, which is the only place they were read. The overlay database's `db` field is the value `database()` reports, so replacing it through `overlay_db_mut()` is caught too. The borrowed-inspector assertion is unchanged. `TypeId` ignores lifetimes, so a verified `Evm<'static>` could be shrunk and given a shorter-lived value of the same type that is not `Send`. `transact_async_send` and `system_call_async_send` therefore require `'a: 'static`. A handler's host has no such bound, so a handler can no longer start a nested async send, and the test that exercised the borrowed-inspector panic from a handler is removed; the assertion stays as a guard. --- crates/evm2/src/evm/async.rs | 24 ++++++++++ crates/evm2/src/evm/mod.rs | 85 ++++++++++++++++++----------------- crates/evm2/src/evm/system.rs | 2 + 3 files changed, 69 insertions(+), 42 deletions(-) diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index 079b3f54e..123f61f7a 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -791,6 +791,30 @@ mod tests { drop(evm.transact_async_send(&tx)); } + #[test] + fn transaction_async_send_survives_state_mut_access() { + let registry = TxRegistry::new().with_handler( + TEST_TX_TYPE, + crate::ethereum::TxEnvelope::as_legacy, + handler(|_| Ok(()), |req, ()| handle_test_tx(req)), + ); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + registry, + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.evm_is_send::>(); + evm.state_mut(); + evm.overlay_db_mut(); + let tx = test_tx(41); + + let result = poll_ready(assert_send(evm.transact_async_send(&tx))).unwrap(); + + assert_eq!(result.result().tx_gas_used(), 42); + } + #[test] fn transaction_async_accepts_non_send_erased_fields() { let marker = Rc::new(()); diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 3c219bc3f..e5ec45ad6 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -134,9 +134,9 @@ use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec, vec::Vec}; use alloy_consensus::transaction::Recovered; use alloy_eips::eip2718::Typed2718; use alloy_primitives::{Address, B256, Bytes, Log, LogData}; -#[cfg(feature = "async")] -use core::future::Future; use core::ptr::NonNull; +#[cfg(feature = "async")] +use core::{any::TypeId, future::Future}; use derive_where::derive_where; #[cfg(feature = "async")] @@ -255,7 +255,11 @@ pub struct Evm<'a, T: EvmTypesHost> { #[cfg(feature = "async")] #[derive_where(skip)] async_stack: r#async::FiberStack, - evm_send: bool, + /// The erased value types last verified as `Send` by [`Evm::evm_is_send`] or + /// [`Evm::evm_is_send_with_inspector`]. + #[cfg(feature = "async")] + #[derive_where(skip)] + send_types: Option, } impl<'a, T: EvmTypes> Evm<'a, T> { @@ -377,7 +381,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { running: false, #[cfg(feature = "async")] async_stack: r#async::FiberStack::default(), - evm_send: false, + #[cfg(feature = "async")] + send_types: None, } } @@ -638,7 +643,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.refresh_runner_support(); self.registry = registry; self.precompiles = precompiles; - self.evm_send = false; } /// Returns the accepted-state overlay database. @@ -658,7 +662,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// available through [`Self::database_mut`]. #[inline] pub fn overlay_db_mut(&mut self) -> &mut CacheDB> { - self.evm_send = false; self.state.overlay_db_mut() } @@ -690,7 +693,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] pub fn set_database(&mut self, database: impl DynDatabase + 'a) { self.state.set_initial(database); - self.evm_send = false; } #[cfg(feature = "async")] @@ -703,7 +705,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] fn assert_erased_send(&self) { assert!( - self.evm_send, + self.send_types == Some(self.erased_type_ids()), "async EVM execution requires EVM erased fields to be verified as Send with \ Evm::evm_is_send" ); @@ -713,10 +715,24 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ); } + #[cfg(feature = "async")] + #[inline] + fn erased_type_ids(&self) -> ErasedTypeIds { + ErasedTypeIds { + database: self.database().type_id(), + precompiles: self.precompiles().type_id(), + inspector: self.inspector().map(|inspector| inspector.type_id()), + } + } + /// Marks this EVM as thread-sendable after checking the current erased field types. /// /// This requires no active inspector. Use [`Self::evm_is_send_with_inspector`] when an /// inspector is installed. + /// + /// Async execution re-checks that the erased types are still the verified ones, so replacing + /// one with a different type makes it panic until the new types are verified. + #[cfg(feature = "async")] #[inline] pub fn evm_is_send(&mut self) -> &mut Self where @@ -727,11 +743,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.assert_database_type::(); self.assert_precompiles_type::

(); assert!(self.inspector.is_none(), "inspector type mismatch"); - self.evm_send = true; + self.send_types = Some(self.erased_type_ids()); self } /// Marks this EVM as thread-sendable after checking the current erased field types. + #[cfg(feature = "async")] #[inline] pub fn evm_is_send_with_inspector(&mut self) -> &mut Self where @@ -743,10 +760,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.assert_database_type::(); self.assert_precompiles_type::

(); self.assert_inspector_type::(); - self.evm_send = true; + self.send_types = Some(self.erased_type_ids()); self } + #[cfg(feature = "async")] #[inline] fn assert_database_type(&self) where @@ -755,6 +773,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { assert_eq!(self.database().type_id(), typeid::of::(), "database type mismatch"); } + #[cfg(feature = "async")] #[inline] fn assert_precompiles_type + 'static>(&self) where @@ -767,6 +786,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ); } + #[cfg(feature = "async")] #[inline] fn assert_inspector_type + 'static>(&self) where @@ -827,7 +847,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Returns the mutable EVM state. #[inline] pub const fn state_mut(&mut self) -> &mut State<'a> { - self.evm_send = false; &mut self.state } @@ -838,9 +857,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } /// Returns the logs emitted by the current in-flight transaction mutably. - /// - /// Unlike [`Self::state_mut`], this keeps the EVM verified as `Send`, since logs are not - /// erased. #[inline] pub const fn logs_mut(&mut self) -> &mut Vec { self.state.logs_mut() @@ -874,7 +890,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_precompiles(&mut self, precompiles: impl PrecompileProvider + 'a) { self.assert_precompiles_mutable(); self.precompiles = boxed_precompile_provider(precompiles); - self.evm_send = false; } /// Replaces the precompile provider with an already boxed one. @@ -882,7 +897,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_boxed_precompiles(&mut self, precompiles: Box + 'a>) { self.assert_precompiles_mutable(); self.precompiles = precompiles; - self.evm_send = false; } /// Returns the precompile provider as `P` if it has that concrete type. @@ -990,7 +1004,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_inspector + 'a>(&mut self, inspector: I) { self.assert_inspector_mutable(); self.inspector = Some(InspectorSlot::Owned(boxed_inspector(inspector))); - self.evm_send = false; } /// Sets the active boxed execution inspector. @@ -998,14 +1011,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_boxed_inspector(&mut self, inspector: Box + 'a>) { self.assert_inspector_mutable(); self.inspector = Some(InspectorSlot::Owned(inspector)); - self.evm_send = false; } /// Removes the active execution inspector. #[inline] pub fn clear_inspector(&mut self) -> Option + 'a>> { self.assert_inspector_mutable(); - self.evm_send = false; match self.inspector.take()? { InspectorSlot::Owned(inspector) => Some(inspector), InspectorSlot::Borrowed(_) => unreachable!("borrowed inspector outlived its call"), @@ -1164,14 +1175,24 @@ impl<'guard, 'evm, T: EvmTypesHost> Drop for ExecutionGuard<'guard, 'evm, T> { } } +/// The concrete types of the erased database, precompile provider, and inspector. +#[cfg(feature = "async")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct ErasedTypeIds { + database: TypeId, + precompiles: TypeId, + inspector: Option, +} + #[cfg(feature = "async")] struct SendEvmRef<'a, 'evm, T: EvmTypesHost> { evm: &'a mut Evm<'evm, T>, } #[cfg(feature = "async")] -// SAFETY: `SendEvmRef` is only constructed by async entrypoints after `Evm::evm_is_send` has -// verified the concrete erased field types as `Send`. +// SAFETY: `SendEvmRef` is only constructed by async entrypoints for an `Evm<'static, T>` whose +// erased field types `Evm::evm_is_send` verified as `Send`. The `'static` bound keeps a verified +// type from being replaced by a shorter-lived instance of itself, which `TypeId` cannot tell apart. unsafe impl Send for SendEvmRef<'_, '_, T> where T: EvmTypesHost, @@ -1302,6 +1323,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { + 'fut where T::Tx: Sync, + 'a: 'static, { self.assert_erased_send(); let stack = self.async_stack(); @@ -5034,27 +5056,6 @@ mod tests { assert!(evm.logs().is_empty()); } - #[cfg(feature = "async")] - #[test] - #[should_panic(expected = "async EVM execution cannot run with a borrowed inspector")] - fn transact_with_inspector_rejects_nested_async_send() { - let mut evm = scoped_inspector_evm(); - evm.registry.register( - TEST_TX_TYPE, - TxEnvelope::as_legacy, - handler( - |_| Ok(()), - |req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ()| { - drop(req.host.transact_async_send(&test_tx(1))); - Ok(TxResultExt::default()) - }, - ), - ); - evm.evm_is_send::>(); - - let _ = evm.transact_with_inspector(&test_tx(0), &mut CallCounter::default()); - } - #[test] #[should_panic(expected = "inspector cannot be modified during EVM execution")] fn nested_transact_with_inspector_panics() { diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index bb8f3de83..c996d339d 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -226,6 +226,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { &mut self, tx: SystemTx, ) -> impl Future, HandlerError>> + Send + '_ + where + 'a: 'static, { self.assert_erased_send(); let stack = self.async_stack(); From 3d3b2af96862ce2bb0694764ae9fe7e0397856e7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:19:44 +0100 Subject: [PATCH 044/159] fix(jit): treat invalid DUPN/SWAPN/EXCHANGE immediates as diverging (#913) An invalid `DUPN`, `SWAPN` or `EXCHANGE` immediate got a placeholder stack I/O of `(0, 0)` but still counted as falling through. Dead-code marking, CFG building and the gas and stack sections therefore kept charging and stack-checking the instructions after it, and `DUPN ; ADD` on an empty stack returned `StackUnderflow` where the interpreter returns `InvalidImmediateEncoding`. Block analysis and dead-store elimination carried special cases (`inp == 0`, "infeasible immediates") to cope. Parsing now sets an `INVALID_IMMEDIATE` flag on such an instruction, gives it zero stack I/O and gas, and `is_diverging` includes the flag, the same way it treats disabled and unknown opcodes. Those special cases are gone: a valid immediate always has enough stack in its section. The new `dupn_invalid_imm_before_underflow` case only fails once the test oracle compares return codes exactly; the current oracle accepts any halt for a `StackUnderflow`. I confirmed that it returned `StackUnderflow` before this change and returns `InvalidImmediateEncoding` after it, using a local strict comparison. `invalid_dupn_imm_diverges` in `sections.rs` fails without the change under the current oracle. --- crates/jit/codegen/src/bytecode/mod.rs | 11 ++++++++- crates/jit/codegen/src/bytecode/opcode.rs | 7 +++--- .../src/bytecode/passes/block_analysis.rs | 2 +- .../src/bytecode/passes/dead_store_elim.rs | 23 +++++-------------- .../codegen/src/bytecode/passes/sections.rs | 16 ++++++------- crates/jit/codegen/src/tests/mod.rs | 9 ++++++++ 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index efcba46f1..c9927952f 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -271,8 +271,12 @@ impl<'a> Bytecode<'a> { let (base_gas, stack_io) = if info.is_unknown() || info.is_disabled() || info.is_unsupported_host() { (0, (0, 0)) + } else if let Some(stack_io) = compute_stack_io(opcode, immediate) { + (info.base_gas(), stack_io) } else { - (info.base_gas(), compute_stack_io(opcode, immediate)) + // Pays its static gas, then halts before touching the stack. + flags |= InstFlags::INVALID_IMMEDIATE; + (info.base_gas(), (0, 0)) }; let data = match opcode { @@ -1226,6 +1230,7 @@ impl InstData { (self.opcode == op::JUMP && self.flags.contains(InstFlags::INVALID_JUMP)) || self.flags.contains(InstFlags::DISABLED) || self.flags.contains(InstFlags::UNKNOWN) + || self.flags.contains(InstFlags::INVALID_IMMEDIATE) || matches!( self.opcode, op::STOP | op::RETURN | op::REVERT | op::INVALID | op::SELFDESTRUCT @@ -1274,6 +1279,10 @@ bitflags::bitflags! { /// The host implements this opcode in a way compiled code cannot reproduce, so a /// program where it is reachable is not compiled. const UNSUPPORTED_HOST = 1 << 12; + /// The `DUPN`, `SWAPN`, or `EXCHANGE` immediate is invalid. + /// Always returns [`InvalidImmediateEncoding`](evm2::interpreter::InstrStop::InvalidImmediateEncoding) + /// at runtime. + const INVALID_IMMEDIATE = 1 << 13; } } diff --git a/crates/jit/codegen/src/bytecode/opcode.rs b/crates/jit/codegen/src/bytecode/opcode.rs index a3b3599c5..3d7383353 100644 --- a/crates/jit/codegen/src/bytecode/opcode.rs +++ b/crates/jit/codegen/src/bytecode/opcode.rs @@ -208,15 +208,16 @@ pub const fn stack_io(op: u8) -> (u8, u8) { /// Returns the real `(inputs, outputs)` stack I/O for an opcode, decoding the immediate for /// `DUPN`, `SWAPN`, and `EXCHANGE` whose opcode-table entries are placeholders. -pub(crate) fn compute_stack_io(op: u8, immediate: Option<&[u8]>) -> (u8, u8) { +/// +/// Returns `None` if the immediate of one of those opcodes is invalid. +pub(crate) fn compute_stack_io(op: u8, immediate: Option<&[u8]>) -> Option<(u8, u8)> { let imm_u8 = || immediate.map_or(0, |b| b[0]); match op { op::DUPN => decode_single(imm_u8()).map(|n| (n, n + 1)), op::SWAPN => decode_single(imm_u8()).map(|n| (n + 1, n + 1)), op::EXCHANGE => decode_pair(imm_u8()).map(|(_n, m)| (m + 1, m + 1)), - _ => return stack_io(op), + _ => Some(stack_io(op)), } - .unwrap_or((0, 0)) // Invalid immediate. } /// Decodes a DUPN/SWAPN immediate byte into a stack index. diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index 58c6288b9..2a7dc59f9 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -2230,7 +2230,7 @@ impl Bytecode<'_> { } // Unknown, disabled, or an invalid immediate. op::DUP1..=op::DUP16 | op::SWAP1..=op::SWAP16 | op::DUPN | op::SWAPN | op::EXCHANGE - if inp == 0 => + if inst.is_diverging() => { return false; } diff --git a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs index bbb4df0a6..b2c836623 100644 --- a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs +++ b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs @@ -86,6 +86,7 @@ impl Bytecode<'_> { if data.flags.contains(InstFlags::NOOP) || data.flags.contains(InstFlags::DISABLED) || data.flags.contains(InstFlags::UNKNOWN) + || data.flags.contains(InstFlags::INVALID_IMMEDIATE) { continue; } @@ -244,29 +245,21 @@ fn transfer_liveness( transfer_swap(live, h_before, depth); } op::SWAPN => { - if let Some(DecodedImm::Single(depth)) = imm - && (depth as usize) < h_before - { + if let Some(DecodedImm::Single(depth)) = imm { transfer_swap(live, h_before, depth as usize); } - // Infeasible depth or decode failure: conservative no-op. } op::DUP1..=op::DUP16 => { let depth = (opcode - op::DUP1 + 1) as usize; transfer_dup(live, h_before, depth); } op::DUPN => { - if let Some(DecodedImm::Single(depth)) = imm - && depth >= 1 - && (depth as usize) <= h_before - { + if let Some(DecodedImm::Single(depth)) = imm { transfer_dup(live, h_before, depth as usize); } } op::EXCHANGE => { - if let Some(DecodedImm::Pair(n, m)) = imm - && (m as usize) < h_before - { + if let Some(DecodedImm::Pair(n, m)) = imm { let tos = h_before - 1; let pos_a = tos - n as usize; let pos_b = tos - m as usize; @@ -349,19 +342,15 @@ fn all_outputs_dead( (op::SWAP1..=op::SWAP16, _) => { swap_all_dead(live, h_before, (opcode - op::SWAP1 + 1) as usize) } - (op::SWAPN, Some(DecodedImm::Single(d))) if (d as usize) < h_before => { - swap_all_dead(live, h_before, d as usize) - } + (op::SWAPN, Some(DecodedImm::Single(d))) => swap_all_dead(live, h_before, d as usize), // DUP: only the new TOS is a real output. (op::DUP1..=op::DUP16, _) => !live[h_before], (op::DUPN, Some(DecodedImm::Single(_))) => !live[h_before], // EXCHANGE: only the two exchanged non-TOS positions are written. - (op::EXCHANGE, Some(DecodedImm::Pair(n, m))) if (m as usize) < h_before => { + (op::EXCHANGE, Some(DecodedImm::Pair(n, m))) => { let tos = h_before - 1; !live[tos - n as usize] && !live[tos - m as usize] } - // Infeasible immediates — conservatively not dead. - (op::DUPN | op::SWAPN | op::EXCHANGE, _) => false, // Generic: all output positions must be dead. _ if out > 0 => { let write_base = h_before - inp; diff --git a/crates/jit/codegen/src/bytecode/passes/sections.rs b/crates/jit/codegen/src/bytecode/passes/sections.rs index d93e0d276..bcd925672 100644 --- a/crates/jit/codegen/src/bytecode/passes/sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/sections.rs @@ -473,21 +473,21 @@ mod tests { assert_eq!(head.gas_section.gas_cost, 2, "only CALLDATASIZE gas (2) should be charged"); } - /// DUPN with an invalid immediate must have stack_io = (0, 0) so that section analysis - /// matches translate (which emits `InvalidImmediateEncoding` and diverges). - /// Previously, the fallback to OPCODE_INFO's placeholder `(0, 1)` inflated the section's - /// cumulative diff, causing translate to underflow the virtual stack on a subsequent SSTORE. + /// DUPN with an invalid immediate diverges like a disabled opcode, so the instructions after + /// it do not join its section: translate halts with `InvalidImmediateEncoding` before them. #[test] - fn invalid_dupn_imm_does_not_inflate_section() { + fn invalid_dupn_imm_diverges() { // PUSH0, DUPN 0x5b (invalid immediate), PUSH0, SSTORE, STOP - // Without the fix, DUPN had stack_io=(0,1), inflating the section diff by 1. - // Translate would skip the +1 (goto_return!(fail)), then SSTORE's diff=-2 would - // underflow the virtual stack. let bytecode = analyze_code_spec( vec![op::PUSH0, op::DUPN, 0x5b, op::PUSH0, op::SSTORE, op::STOP], SpecId::AMSTERDAM, ); let dupn = bytecode.inst(Inst::from_usize(1)); + assert!(dupn.is_diverging(), "DUPN with invalid immediate must diverge"); assert_eq!(dupn.stack_io(), (0, 0), "DUPN with invalid immediate must have zero stack I/O"); + + // The instructions after it are dead, so they add no stack inputs or gas to a section. + let after = bytecode.inst(Inst::from_usize(3)); + assert!(after.is_dead_code()); } } diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 47603b1e7..3d776e6ff 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -453,6 +453,15 @@ tests! { expected_stack: STACK_WHAT_INTERPRETER_SAYS, expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), + // The invalid immediate halts before the following instructions are charged or + // stack-checked, so an empty stack does not underflow at the section head. + dupn_invalid_imm_before_underflow(@raw { + bytecode: &[op::DUPN, 0x5B, op::ADD], + spec_id: SpecId::AMSTERDAM, + expected_return: InstrStop::InvalidImmediateEncoding, + expected_stack: STACK_WHAT_INTERPRETER_SAYS, + expected_gas: GAS_WHAT_INTERPRETER_SAYS, + }), // Invalid immediate: 82 is in the invalid range [82, 127] for decode_pair. exchange_invalid_imm(@raw { bytecode: &[op::PUSH0, op::PUSH0, op::PUSH0, op::EXCHANGE, 82], From 8bd62b77af99fd7d2148c042c47361587f1f461f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:27:43 +0100 Subject: [PATCH 045/159] refactor(evm2): share gas breakdown between transactions and system calls (#897) System calls worked out their total and state gas on their own, apart from the formula `finalize_gas` uses for normal transactions. The two drifted once already (#841, patched in #842). This moves the breakdown into one function, `gas_spent_breakdown`, which both paths now call. A system call passes `gas_limit + reservoir` as the transaction gas limit and no upfront state gas, so every reported number stays the same. `finalize_gas` still subtracts the EIP-7702 `state_refund` itself, so the helper does not depend on that field. System calls still report the raw refund of a successful call, without the EIP-3529 cap. Their doc said they bypass gas refunds; it now says what the code does. --- crates/evm2/src/ethereum/mod.rs | 38 ++++++++++++++++-------- crates/evm2/src/evm/system.rs | 51 ++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index d9902ee73..8c603f6e8 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -784,24 +784,17 @@ pub fn finalize_gas<'a, T: EvmTypes>( } = settlement; let max_refund_quotient = host.version().gas_params.get(GasId::MaxRefundQuotient); - // Self-contained gas breakdown for the result. `total_gas_spent` is defined so that - // `TxResult::tx_gas_used` reproduces the finalized gas used. State gas is execution state gas - // plus the upfront `initial_state_gas`, less the EIP-7702 per-authorization `state_refund`. - let total_gas_spent = - tx_gas_limit.saturating_sub(result.gas.remaining()).saturating_sub(result.gas.reservoir()); let refunded = result.final_refund(tx_gas_limit, max_refund_quotient); // EIP-7623: when the calldata floor exceeds spent-minus-refund, `TxResult::tx_gas_used` // resolves to the floor. `total_gas_spent` stays pre-refund and pre-floor: block-level // execution gas (EIP-7778/EIP-8037) accumulates `tx_gas_used_before_refund` per // execution-specs, without the floor clamp. // - // The settled tracker's state gas is self-consistent: a failed frame's state charges were - // rolled back before it was merged, while unconditional pre-execution charges (the EIP-7702 - // authorizations, whose delegations survive an execution failure) remain. The upfront - // `initial_state_gas` is likewise added unconditionally. - let state_gas_spent = - (result.gas.state_gas_spent().saturating_add_unsigned(initial_state_gas).max(0) as u64) - .saturating_sub(state_refund); + // The per-authorization `state_refund` is EIP-7702 state gas returned on top of the settled + // tracker's. + let (total_gas_spent, state_gas_spent) = + gas_spent_breakdown(tx_gas_limit, &result.gas, initial_state_gas); + let state_gas_spent = state_gas_spent.saturating_sub(state_refund); Ok(TxResultExt { status: result.stop.is_success(), total_gas_spent, @@ -816,6 +809,27 @@ pub fn finalize_gas<'a, T: EvmTypes>( }) } +/// Returns `(total_gas_spent, state_gas_spent)` for a settled transaction-level gas tracker. +/// +/// `tx_gas_limit` is the execution gas limit plus the state-gas reservoir the transaction started +/// with. `total_gas_spent` is defined so that `TxResult::tx_gas_used` reproduces the finalized gas +/// used. `state_gas_spent` is the tracker's state gas plus the upfront `initial_state_gas`. +/// +/// The settled tracker's state gas is self-consistent: a failed frame's state charges were rolled +/// back before it was merged, while unconditional pre-execution charges (the EIP-7702 +/// authorizations, whose delegations survive an execution failure) remain. The upfront +/// `initial_state_gas` is likewise added unconditionally. +pub(crate) fn gas_spent_breakdown( + tx_gas_limit: u64, + gas: &GasTracker, + initial_state_gas: u64, +) -> (u64, u64) { + let total_gas_spent = + tx_gas_limit.saturating_sub(gas.remaining()).saturating_sub(gas.reservoir()); + let state_gas_spent = gas.state_gas_spent().saturating_add_unsigned(initial_state_gas).max(0); + (total_gas_spent, state_gas_spent as u64) +} + /// Returns the account and storage-key counts in an access list. pub fn access_list_counts(access_list: &AccessList) -> (u64, u64) { (access_list.len() as u64, access_list.storage_keys_count() as u64) diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index c996d339d..e64f5f8e9 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -14,7 +14,7 @@ use crate::registry::HandlerError; use crate::{ EvmTypes, env::TxEnvExt, - ethereum::{execute_initial_frame, prepare_initial_frame}, + ethereum::{execute_initial_frame, gas_spent_breakdown, prepare_initial_frame}, interpreter::GasTracker, registry::HandlerResult, version::{EvmFeatures, GasId}, @@ -111,9 +111,10 @@ impl SystemTx { impl<'a, T: EvmTypes> Evm<'a, T> { /// Executes a system call. /// - /// System calls bypass normal transaction validation, nonce updates, fee charging, gas refunds, - /// and beneficiary rewards. They execute a top-level `CALL` with zero value and - /// [`SystemTx::gas_limit`] gas, then finalize and return an executed transaction handle. + /// System calls bypass normal transaction validation, nonce updates, fee charging, and + /// beneficiary rewards. They execute a top-level `CALL` with zero value and + /// [`SystemTx::gas_limit`] gas, then finalize and return an executed transaction handle. A + /// successful call reports its raw gas refund, without the EIP-3529 cap. /// /// The target system contract bytecode must already be present in state. This method does not /// deploy protocol system contracts or synthesize their bytecode. @@ -161,6 +162,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } else { 0 }; + // The call's total budget is `gas_limit + reservoir` and must fit in a `u64`. + let gas_limit = gas_limit.min(u64::MAX - reservoir); let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(gas_limit, reservoir); let frame = prepare_initial_frame( self, @@ -172,11 +175,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { &mut tx_gas, )?; let result = execute_initial_frame(self, frame, &mut tx_gas, gas_limit, reservoir)?; - // The reservoir is funded on top of `gas_limit`, so the state gas it paid for is added - // back; otherwise `total_gas_spent` would not cover `state_gas_spent`. - let gas_spent = gas_limit - .saturating_sub(result.gas.remaining()) - .saturating_add(reservoir.saturating_sub(result.gas.reservoir())); + // The reservoir is funded on top of `gas_limit`, so the transaction gas limit includes it. + let (total_gas_spent, state_gas_spent) = + gas_spent_breakdown(gas_limit + reservoir, &result.gas, 0); let gas_refunded = if result.stop.is_success() && result.gas.refunded() > 0 { result.gas.refunded() as u64 } else { @@ -184,8 +185,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { }; Ok(TxResultExt { status: result.stop.is_success(), - total_gas_spent: gas_spent, - state_gas_spent: result.gas.state_gas_spent().max(0) as u64, + total_gas_spent, + state_gas_spent, refunded: gas_refunded, floor_gas: 0, stop: result.stop, @@ -564,6 +565,34 @@ mod tests { assert_eq!(result.execution_gas_spent(), regular_gas); } + #[test] + fn system_call_total_covers_state_gas_with_unbounded_gas_limit() { + let contract = Address::from([0x42; 20]); + let code = Bytecode::new_legacy(Bytes::from_static(&[ + op::PUSH1, + 1, + op::PUSH0, + op::SSTORE, + op::STOP, + ])); + let mut database = InMemoryDB::default(); + database.insert_account_info(&contract, AccountInfo::default().with_code(code)); + let mut evm = TestEvm::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + TxRegistry::new(), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + + let tx = SystemTx::new(contract, Bytes::new()).with_gas_limit(u64::MAX); + let result = evm.system_call(tx).unwrap().discard(); + + assert!(result.status); + assert!(result.total_gas_spent >= result.state_gas_spent); + assert_eq!(result.execution_gas_spent(), 3 + 2 + 2_100 + 10_000); + } + #[derive(Debug, Default)] struct CountingInspector { calls: usize, From 55c72ebb2f62dfdea0192f22f49840f0b8eb1aac Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:45:29 +0100 Subject: [PATCH 046/159] fix(evm): charge the modexp gas before the EIP-7823 size check (#874) The Osaka modexp precompile now charges its gas before it checks the EIP-7823 size limit, as go-ethereum does. geth splits the two: `RequiredGas` prices the call (it saturates a length that does not fit in 64 bits to `MaxUint64`) and `RunPrecompiledContract` fails the charge with `ErrOutOfGas`; only after that does `bigModExp.Run` reject an input with a length over 1024 bytes (`core/vm/contracts.go`, master a0cba163). evm2 checked the limit first, so a call with too little gas for an oversized input ended as `PrecompileError` where geth reports `out of gas`. A length that does not fit in a `usize` was also rejected as a size error on every fork, where geth prices it at `MaxUint64` and runs out of gas. The call consumes all its gas either way, so the state and the gas used do not change; only the stop reported to inspectors does. On the parity harness (offline, #859 plus this branch, an empty known-diffs file, tracers `call`, `jsonl`, `structlog`, `flat` and `result`, case `precompile_failing`), the four differences `calls[1].error: geth=out of gas evm2=precompiled failed` (Osaka and Amsterdam, `call`) and `[31].error` (Osaka and Amsterdam, `jsonl`) disappear, and `result` still matches geth. `oversized_lengths_are_charged_before_they_are_rejected` pins the out of gas for a length over 64 bits on each fork and for a 64 KiB length without gas, and `test_osaka_modexp_input_len` now supplies the gas that the size check needs. The 22989 devnet state tests still pass. The size error itself (geth: `one or more of base/exponent/modulus length exceeded 1024 bytes`) still reads `precompiled failed` in evm2's traces until the precompile error texts change lands. Once #859 is on main, this PR deletes or narrows the known-diff entries for the four lines above. --- crates/evm2/src/precompiles/modexp.rs | 85 ++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index 1c91f8b72..753ce57db 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -206,20 +206,11 @@ pub fn run_inner( let exp_len = U256::from_be_bytes(read_word_padded(input, 32).0); let mod_len = U256::from_be_bytes(read_word_padded(input, 64).0); - // Cast base and modulus to usize, it does not make sense to handle larger values - let base_len = usize::try_from(base_len).map_err(|_| PrecompileHalt::ModexpEip7823LimitSize)?; - let mod_len = usize::try_from(mod_len).map_err(|_| PrecompileHalt::ModexpEip7823LimitSize)?; - // cast exp len to the max size, it will fail later in gas calculation if it is too large. - let exp_len = usize::try_from(exp_len).unwrap_or(usize::MAX); - - // for EIP-7823 we need to check size of inputs - if osaka - && (base_len > eip7823::INPUT_SIZE_LIMIT - || mod_len > eip7823::INPUT_SIZE_LIMIT - || exp_len > eip7823::INPUT_SIZE_LIMIT) - { - return Err(PrecompileHalt::ModexpEip7823LimitSize.into()); - } + // Lengths that do not fit in 64 bits cost more gas than any call has. + let [base_len64, exp_len64, mod_len64] = + [base_len, exp_len, mod_len].map(|len| u64::try_from(len).unwrap_or(u64::MAX)); + let [base_len, exp_len, mod_len] = + [base_len64, exp_len64, mod_len64].map(|len| usize::try_from(len).unwrap_or(usize::MAX)); // Used to extract ADJUSTED_EXPONENT_LENGTH. let exp_highp_len = min(exp_len, 32); @@ -237,9 +228,18 @@ pub fn run_inner( }; // Check if we have enough gas. - let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); + let gas_cost = calc_gas(base_len64, exp_len64, mod_len64, &exp_highp); gas.spend(gas_cost.saturating_sub(min_gas))?; + // EIP-7823 limits the size of the inputs, after the gas is charged. + if osaka + && [base_len64, exp_len64, mod_len64] + .into_iter() + .any(|len| len > eip7823::INPUT_SIZE_LIMIT as u64) + { + return Err(PrecompileHalt::ModexpEip7823LimitSize.into()); + } + if base_len == 0 && mod_len == 0 { return Ok(PrecompileOutput::new(Bytes::new())); } @@ -599,6 +599,7 @@ mod tests { base_len: U256, exp_len: U256, mod_len: U256, + gas: u64, expected: Option, } @@ -617,42 +618,69 @@ mod tests { base_len: U256::from(1025), exp_len: U256::from(1024), mod_len: U256::from(1024), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1025), mod_len: U256::from(1024), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1024), mod_len: U256::from(1025), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(0), exp_len: U256::from(0), mod_len: U256::from(1025), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1024), mod_len: U256::from(1024), + gas: 100_000_000, expected: Some(PrecompileHalt::OutOfGas), }, + TestInput { + base_len: U256::from(1) << 64, + exp_len: U256::from(1), + mod_len: U256::from(1), + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, + TestInput { + base_len: U256::from(1), + exp_len: U256::from(1) << 64, + mod_len: U256::from(1), + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, + TestInput { + base_len: U256::from(1), + exp_len: U256::from(1), + mod_len: U256::from(1) << 64, + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, TestInput { base_len: U256::from(0), exp_len: U256::from(0), mod_len: U256::from(0), + gas: 100_000_000, expected: None, }, ]; for test in test_inputs { let input = test.input(); - let res = run_osaka(&input, &mut GasTracker::new(100_000_000)).err(); + let res = run_osaka(&input, &mut GasTracker::new(test.gas)).err(); let halt = match &res { Some(PrecompileError::Halt(halt)) => Some(halt), None => None, @@ -664,6 +692,31 @@ mod tests { } } + #[test] + fn oversized_lengths_are_charged_before_they_are_rejected() { + let input = |lengths: [U256; 3]| { + lengths.iter().flat_map(|len| len.to_be_bytes::<32>()).collect::>() + }; + let halt = |input: &[u8], run: fn(&[u8], &mut GasTracker) -> PrecompileResult| match run( + input, + &mut GasTracker::new(100_000), + ) { + Err(PrecompileError::Halt(halt)) => halt, + result => panic!("expected a halt, got {result:?}"), + }; + // A length that does not fit in 64 bits costs more than any call can pay, on every fork. + for position in 0..3 { + let mut lengths = [U256::from(1); 3]; + lengths[position] = U256::from(1) << 64; + for run in [run_byzantium, run_berlin, run_osaka] { + assert_eq!(halt(&input(lengths), run), PrecompileHalt::OutOfGas, "{position}"); + } + } + // An Osaka length over the limit is out of gas when the gas does not cover its price. + let lengths = [U256::from(65_536), U256::from(1), U256::from(1)]; + assert_eq!(halt(&input(lengths), run_osaka), PrecompileHalt::OutOfGas); + } + #[test] fn test_modexp_edge_cases() { // Test case 1: Zero base with non-zero exponent and modulus From 13d1ef033c7883530d29c365c4364778f8e46592 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:29:46 +0100 Subject: [PATCH 047/159] refactor(inspectors): drop CallTraceStep::gas_used and final_refunded wrapper (#896) The inspectors carried two pieces of gas arithmetic that evm2 already provides or that the trace does not need. `CallTraceStep::gas_used` applied the transaction refund cap to the running frame's own spent gas and refund counter on every step. The quotient was hard-coded (5 from London, 2 before) and ignored the configured max refund quotient, and a transaction-level cap has no meaning for a single frame. Its only reader was the trace writer, which subtracted two steps of the same frame to print an internal function's gas. The field and `utils::gas_used` are removed, and the writer now prints the start step's `gas_remaining` minus the end step's. The printed value no longer subtracts a capped refund, so it now equals the gas the function actually consumed; the writer snapshots do not change. `tracing::final_refunded` was a wrapper around `TxResultExt::final_refunded`. It is removed and the method is called directly in `debug.rs`, `mux.rs` and `builder/geth.rs`. The geth parity harness from #859 reports the same result with and without this change (0 new differences, the same 59 stale known entries that already exist on main merged with the harness), so no known diff is fixed or added here. --- crates/inspectors/src/tracing/builder/geth.rs | 5 ++--- .../src/tracing/builder/geth/steps_tests.rs | 1 - crates/inspectors/src/tracing/debug.rs | 7 +++---- crates/inspectors/src/tracing/mod.rs | 11 ----------- crates/inspectors/src/tracing/mux.rs | 4 ++-- crates/inspectors/src/tracing/types.rs | 2 -- crates/inspectors/src/tracing/utils.rs | 9 +-------- crates/inspectors/src/tracing/writer.rs | 3 ++- 8 files changed, 10 insertions(+), 32 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index ab6b1f31c..0c5747601 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -1,6 +1,5 @@ //! Geth trace builder use crate::tracing::{ - final_refunded, tx_state::TxState, types::{CallKind, CallTraceNode, CallTraceStepStackItem}, utils::load_account_code, @@ -230,7 +229,7 @@ impl<'a> GethTraceBuilder<'a> { ) -> DefaultFrame { self.geth_traces_inner( gas.tx_gas_used(), - Some((gas.execution_gas_spent(), gas.state_gas_spent(), final_refunded(gas))), + Some((gas.execution_gas_spent(), gas.state_gas_spent(), gas.final_refunded())), return_value, opts, None, @@ -301,7 +300,7 @@ impl<'a> GethTraceBuilder<'a> { self.geth_call_traces_inner( opts, gas.tx_gas_used(), - Some((gas.execution_gas_spent(), gas.state_gas_spent(), final_refunded(gas))), + Some((gas.execution_gas_spent(), gas.state_gas_spent(), gas.final_refunded())), ) } diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index b2bdc2457..ad1e9aec8 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -18,7 +18,6 @@ fn test_step(pc: usize, op: u8) -> CallTraceStep { returndata: Bytes::from(vec![pc as u8]), gas_remaining: 1000 - pc as u64, gas_refund_counter: 0, - gas_used: pc as u64, gas_cost: 1, state_gas_cost: None, state_gas_reservoir: None, diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index 5b7217e37..e246a42d1 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -1,7 +1,6 @@ use crate::tracing::{ FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MuxInspector, - TracingInspector, TracingInspectorConfig, TransactionContext, final_refunded, - utils::transaction_info, + TracingInspector, TracingInspectorConfig, TransactionContext, utils::transaction_info, }; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; @@ -280,7 +279,7 @@ impl DebugInspector { gas_used: res.result.tx_gas_used(), execution_gas_used: res.result.execution_gas_spent(), state_gas_used: res.result.state_gas_spent(), - gas_refund: final_refunded(&res.result), + gas_refund: res.result.final_refunded(), } .into(), Self::Mux(inspector, _) => { @@ -330,7 +329,7 @@ impl DebugInspector { Some(( gas.execution_gas_spent(), gas.state_gas_spent(), - final_refunded(gas), + gas.final_refunded(), )), res.result.output.clone(), *config, diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index fcfb8f69e..3866a0401 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -8,7 +8,6 @@ use crate::{ CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, TraceMemberOrder, }, - utils::gas_used, }, }; use alloc::{boxed::Box, vec::Vec}; @@ -76,12 +75,6 @@ pub use mux::{Error as MuxError, MuxInspector}; mod debug; pub use debug::{DebugInspector, DebugInspectorError}; -/// Returns the effective gas refund after the calldata floor adjustment. -#[inline] -pub(crate) const fn final_refunded(gas: &TxResultExt) -> u64 { - gas.final_refunded() -} - /// An inspector that collects call traces. /// /// This [Inspector] can be hooked into evm2's EVM which then calls the inspector functions, such @@ -519,9 +512,6 @@ impl TracingInspector { Bytes::new() }; - let gas_used = - gas_used(interp.spec(), interp.gas().spent(), interp.gas().refunded() as u64); - let mut immediate_bytes = None; if self.config.record_immediate_bytes { let size = usize::from(immediate_size(op.get())); @@ -551,7 +541,6 @@ impl TracingInspector { returndata, gas_remaining: interp.gas().remaining(), gas_refund_counter: interp.gas().refunded() as u64, - gas_used, immediate_bytes, // These fields will be populated in `step_end`. diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index c34a4eaf7..50971cb93 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -1,6 +1,6 @@ use crate::tracing::{ DebugInspector, DebugInspectorError, FourByteInspector, TracingInspector, - TracingInspectorConfig, TransactionContext, final_refunded, utils::transaction_info, + TracingInspectorConfig, TransactionContext, utils::transaction_info, }; use alloc::{boxed::Box, vec::Vec}; use alloy_consensus::{Transaction, transaction::Recovered}; @@ -236,7 +236,7 @@ impl MuxInspector { gas_used: result.result.tx_gas_used(), execution_gas_used: result.result.execution_gas_spent(), state_gas_used: result.result.state_gas_spent(), - gas_refund: final_refunded(&result.result), + gas_refund: result.result.final_refunded(), } .into(), }; diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 04d06f1cb..afabe082e 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -661,8 +661,6 @@ pub struct CallTraceStep { pub gas_remaining: u64, /// Gas refund counter before step execution pub gas_refund_counter: u64, - /// Total gas used before step execution - pub gas_used: u64, // Fields filled in `step_end` /// Gas cost of step execution pub gas_cost: u64, diff --git a/crates/inspectors/src/tracing/utils.rs b/crates/inspectors/src/tracing/utils.rs index 4d8c2e55c..859c3aa5f 100644 --- a/crates/inspectors/src/tracing/utils.rs +++ b/crates/inspectors/src/tracing/utils.rs @@ -9,7 +9,7 @@ use alloy_primitives::{Bytes, hex}; use alloy_rpc_types_eth::TransactionInfo; use alloy_sol_types::{ContractError, GenericRevertReason}; use evm2::{ - AccountInfo, EvmTypesHost, SpecId, + AccountInfo, EvmTypesHost, env::BlockEnv, evm::{DbResult, DynDatabase}, interpreter::InstrStop, @@ -129,13 +129,6 @@ pub(crate) fn convert_memory(data: &[u8]) -> Vec { memory } -/// Get the gas used, accounting for refunds -#[inline] -pub(crate) fn gas_used(spec: SpecId, spent: u64, refunded: u64) -> u64 { - let refund_quotient = if spec.enables(SpecId::LONDON) { 5 } else { 2 }; - spent - (refunded).min(spent / refund_quotient) -} - /// Loads the code for the given account from the account itself or the database /// /// Returns None if the code hash is the KECCAK_EMPTY hash diff --git a/crates/inspectors/src/tracing/writer.rs b/crates/inspectors/src/tracing/writer.rs index 26c249f66..9ba9b718e 100644 --- a/crates/inspectors/src/tracing/writer.rs +++ b/crates/inspectors/src/tracing/writer.rs @@ -376,7 +376,8 @@ impl TraceWriter { match &**decoded { DecodedTraceStep::InternalCall(call, end_idx) => { - let gas_used = node.trace.steps[*end_idx].gas_used.saturating_sub(step.gas_used); + let gas_used = + step.gas_remaining.saturating_sub(node.trace.steps[*end_idx].gas_remaining); self.write_branch()?; self.indentation_level += 1; From f5c7a57b4f023a22aa8b96b32617e5d455ad26c7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:29:55 +0100 Subject: [PATCH 048/159] refactor(inspectors): take stateDiff originals from the pending state (#899) `populate_state_diff` re-read every account's pre-transaction info from the database, even though the change stream it already walks reports each account as it was at the start of the transaction (`TxAccount::original`, `None` when the account did not exist). It now takes the existence check, balance, nonce and code hash from that stream. The database is used only to load bytecode by hash, since `AccountInfo::code` may be unset, so the doc comments no longer require it to point at the start of the transaction. The `stateDiff` output is unchanged: the inspector tests and the EEST suite pass as before. The stored geth parity harness (#859) reports the same result with and without this change: 0 new differences and the same 59 stale known entries, so it fixes no listed known diff and adds no deliberate one. --- .../inspectors/src/tracing/builder/parity.rs | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/parity.rs b/crates/inspectors/src/tracing/builder/parity.rs index cca1d3980..e4b7d015c 100644 --- a/crates/inspectors/src/tracing/builder/parity.rs +++ b/crates/inspectors/src/tracing/builder/parity.rs @@ -13,7 +13,7 @@ use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::parity::*; use core::iter::Peekable; use evm2::{ - EvmTypesHost, SpecId, TxResultExt, TxResultWithState, + AccountInfo, EvmTypesHost, SpecId, TxResultExt, TxResultWithState, evm::{DbResult, DynDatabase, PendingState}, interpreter::InstrStop, }; @@ -126,7 +126,7 @@ impl ParityTraceBuilder { /// Warning: If `trace_types` contains [TraceType::StateDiff] the returned [StateDiff] will not /// be filled. Use [ParityTraceBuilder::into_trace_results_with_state] or /// [populate_state_diff] to populate the balance and nonce changes for the [StateDiff] - /// using the database. + /// using the pending state and the database for bytecode. pub fn into_trace_results( self, res: &TxResultExt, @@ -142,7 +142,8 @@ impl ParityTraceBuilder { /// Consumes the inspector and returns the trace results according to the configured trace /// types. /// - /// This also takes the database to populate the balance and nonce changes for the [StateDiff]. + /// This also takes the pending state and a database (used for bytecode) to populate the + /// [StateDiff]. /// /// Note: this is considered a convenience method that takes the state changes after inspecting /// a transaction with the [TracingInspector](crate::tracing::TracingInspector). @@ -162,8 +163,7 @@ impl ParityTraceBuilder { /// state, without cloning the state into a result container. /// /// Populates state diffs when requested by `trace_types`. VM bytecode comes from the recorded - /// frames. The database must represent the state before the transaction's changes are - /// committed. + /// frames. The database is only used to load bytecode by hash. pub fn into_trace_results_with_state_parts( self, result: &TxResultExt, @@ -477,32 +477,32 @@ where /// Populates [StateDiff] given the [PendingState] of a transaction and a database. /// /// Loops over all state accounts in the accounts diff that contains all accounts that are included -/// in the [PendingState] and compares the balance and nonce against what's in the `db`, which -/// should point to the beginning of the transaction. +/// in the [PendingState] and compares the balance and nonce against the account as it was at the +/// start of the transaction, which the pending state records. The `db` is only used to load +/// bytecode by hash. pub fn populate_state_diff( state_diff: &mut StateDiff, db: &mut dyn DynDatabase, pending: &PendingState, ) -> DbResult<()> { let state = TxState::from_pending(pending); + let absent = AccountInfo::default(); for (addr, changed_acc) in state.accounts.iter() { - let db_acc = db.get_account(addr)?; - // An account created and destroyed in this transaction has no net change unless it // already existed, for example with a prefunded balance. - if changed_acc.current.is_none() && db_acc.is_none() { + if changed_acc.current.is_none() && changed_acc.original.is_none() { continue; } - let existed = db_acc.is_some(); - let db_acc = db_acc.unwrap_or_default(); + let existed = changed_acc.original.is_some(); + let original = changed_acc.original.as_ref().unwrap_or(&absent); let entry = state_diff.entry(*addr).or_default(); // Use the finalized account state: selfdestruct can preserve a balance-only account. if changed_acc.current.is_none() { - entry.balance = Delta::Removed(db_acc.balance); - entry.nonce = Delta::Removed(U64::from(db_acc.nonce)); - entry.code = Delta::Removed(load_account_code(db, &db_acc)?.unwrap_or_default()); + entry.balance = Delta::Removed(original.balance); + entry.nonce = Delta::Removed(U64::from(original.nonce)); + entry.code = Delta::Removed(load_account_code(db, original)?.unwrap_or_default()); // Deletion wipes all storage, which the removed account implies. PendingState // holds only accessed slots, so listing them would produce a partial diff. continue; @@ -526,8 +526,8 @@ pub fn populate_state_diff( } } else { // EIP-7702 can change code without creating the account, even if execution reverts. - if db_acc.code_hash != info.code_hash { - let original_account_code = load_account_code(db, &db_acc)?.unwrap_or_default(); + if original.code_hash != info.code_hash { + let original_account_code = load_account_code(db, original)?.unwrap_or_default(); let present_account_code = load_account_code(db, info)?.unwrap_or_default(); entry.code = Delta::changed(original_account_code, present_account_code); } @@ -541,24 +541,24 @@ pub fn populate_state_diff( } // check if the account was changed at all - if entry.storage.is_empty() && &db_acc == info && !changed_acc.selfdestructed { + if entry.storage.is_empty() && original == info && !changed_acc.selfdestructed { // clear the entry if the account was not changed state_diff.remove(addr); continue; } - entry.balance = if db_acc.balance == info.balance { + entry.balance = if original.balance == info.balance { Delta::Unchanged } else { - Delta::Changed(ChangedType { from: db_acc.balance, to: info.balance }) + Delta::Changed(ChangedType { from: original.balance, to: info.balance }) }; // this is relevant for the caller and contracts - entry.nonce = if db_acc.nonce == info.nonce { + entry.nonce = if original.nonce == info.nonce { Delta::Unchanged } else { Delta::Changed(ChangedType { - from: U64::from(db_acc.nonce), + from: U64::from(original.nonce), to: U64::from(info.nonce), }) }; From 68a8b7670e28ef4f2458df5142ff5fc21db20c15 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:04 +0100 Subject: [PATCH 049/159] fix: set CREATE code_address to the destination in the message builders (#901) Both CREATE message builders, the interpreter instruction and the JIT builtin, set `code_address` to the caller as a placeholder, and `Evm::execute_create_message` overwrote it with the destination just before running the frame. That overwrite dates from when the destination was unknown at the opcode; the builders now derive it themselves, and the top-level create builder already sets `code_address` to the destination. The builders now set `code_address: destination` and the host overwrite is deleted. The one visible change is that the inspector `create` hook now sees the real code address instead of the caller. No create hook in the workspace rewrites `destination`, so none needs to set `code_address` as well. The existing create inspector test asserts that the hook sees `code_address == destination`. --- crates/evm2/src/evm/inspector.rs | 1 + crates/evm2/src/evm/mod.rs | 1 - crates/evm2/src/interpreter/instructions/system.rs | 2 +- crates/jit/builtins/src/lib.rs | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 4edc3d372..373819166 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -213,6 +213,7 @@ mod tests { _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, message: &mut Message, ) -> Option> { + assert_eq!(message.code_address, message.destination); self.create_depths.push(message.depth); self.create_destinations.push(message.destination); None diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index e5ec45ad6..5300f6129 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1488,7 +1488,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { HostError::Execution(error) => Err(error), }; } - message.code_address = message.destination; // Empty initcode deploys an empty contract without executing the bytecode's padding STOP. let (stop, gas, output) = if message.code.is_empty() { ( diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index f58e58df9..eb506c3a4 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -452,7 +452,7 @@ pub fn create_with( code: Bytecode::new_legacy(input.clone()), input, value, - code_address: caller, + code_address: destination, // CREATE is rejected in a static context (see `require_non_staticcall`). caller_is_static: false, salt, diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 240824232..450e30907 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -693,7 +693,7 @@ pub unsafe fn __revmc_builtin_create( code: Bytecode::new_legacy(code.clone()), input: code, value, - code_address: caller, + code_address: destination, caller_is_static: false, salt, ext: T::MessageExt::default(), From 348adc4c9b1c73ccdd46326ddd384835708979f6 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:13 +0100 Subject: [PATCH 050/159] refactor(jit): remove dead translator code (#902) The JIT translator carried leftovers from an earlier design: an unused `pf` shim ("HACK: need these fields public for `offset_of!`", while offsets already come from `mem::offset_of!` on the real types), `call_ir_builtin` with its aliasing `unsafe`, the printf helpers and their commented-out call sites, `SwitchTargets`, two commented-out methods, and `default_attrs` helpers behind `#[allow(dead_code)]`. This PR deletes them. It also drops the always-true `little_endian()` check (`compile` already rejects big-endian hosts), replaces the one-variant `EvmCompilerInput` enum with `&[u8]`, moves a function-local `use std::fmt::Write` to the top of the file, and merges a duplicated comment on constant folding. Codegen is meant to be unchanged. CI's JIT benchmark confirms codegen is unchanged: `opt.s` is identical in 31 of 31 benchmarks, JIT size and optimized IR are identical, compile instructions are 164.83G on both main and this branch, and runtime instructions are 848.05M against 847.94M (noise). The cycle deltas are runner noise. --- crates/cli/src/run.rs | 2 +- crates/jit/codegen/src/compiler/mod.rs | 66 +-------- .../jit/codegen/src/compiler/translate/mod.rs | 137 +----------------- crates/jit/codegen/src/lib.rs | 2 +- 4 files changed, 14 insertions(+), 193 deletions(-) diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index e9b79ea1a..4719356d2 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -199,7 +199,7 @@ impl RunArgs { compiler.set_frame_pointers(false); } - let parsed = compiler.parse(bytecode.as_slice().into(), compile_spec_id)?; + let parsed = compiler.parse(bytecode.as_slice(), compile_spec_id)?; if self.display || self.parse_only { println!("{name}()\n{parsed:#}"); } diff --git a/crates/jit/codegen/src/compiler/mod.rs b/crates/jit/codegen/src/compiler/mod.rs index 20da64194..6e32c6d8f 100644 --- a/crates/jit/codegen/src/compiler/mod.rs +++ b/crates/jit/codegen/src/compiler/mod.rs @@ -4,7 +4,6 @@ use crate::{ Backend, Builder, Bytecode, EvmCompilerFn, EvmContext, EvmStack, FxHashMap, Result, bytecode::AnalysisConfig, }; -use alloy_primitives::Bytes; use evm2::{OpcodeView, SpecId, version::GasParams}; use evm2_jit_backend::{ Attribute, BackendConfig, FunctionAttributeLocation, Linkage, OptimizationLevel, eyre::ensure, @@ -567,14 +566,9 @@ impl EvmCompiler { /// /// NOTE: `name` must be unique for each function, as it is used as the name of the final /// symbol. - pub fn translate<'a>( - &mut self, - name: &str, - input: impl Into>, - spec_id: SpecId, - ) -> Result { + pub fn translate(&mut self, name: &str, input: &[u8], spec_id: SpecId) -> Result { ensure!(!self.finalized, "cannot compile more functions after finalizing the module"); - let bytecode = self.parse(input.into(), spec_id)?; + let bytecode = self.parse(input, spec_id)?; self.translate_inner(name, &bytecode) } @@ -586,13 +580,13 @@ impl EvmCompiler { /// /// The returned function pointer is owned by the module, and must not be called after the /// module is cleared or the function is freed. - pub unsafe fn jit<'a>( + pub unsafe fn jit( &mut self, name: &str, - bytecode: impl Into>, + bytecode: &[u8], spec_id: SpecId, ) -> Result { - let id = self.translate(name, bytecode.into(), spec_id)?; + let id = self.translate(name, bytecode, spec_id)?; unsafe { self.jit_function(id) } } @@ -685,14 +679,8 @@ impl EvmCompiler { /// Parses the given EVM bytecode. Not public API. #[doc(hidden)] // Not public API. - pub fn parse<'a>( - &mut self, - input: EvmCompilerInput<'a>, - spec_id: SpecId, - ) -> Result> { + pub fn parse<'a>(&mut self, bytecode: &'a [u8], spec_id: SpecId) -> Result> { let _t = self.remarks.time(|r| &r.parse); - let EvmCompilerInput::Code(bytecode) = input; - let mut bytecode = Bytecode::new(bytecode, spec_id, self.gas_params, self.opcode_view.as_ref()); bytecode.compiler_gas_limit = self.compiler_gas_limit; @@ -1104,32 +1092,6 @@ total: {total:>11.3?} } } -/// [`EvmCompiler`] input. -#[allow(missing_debug_implementations)] -pub enum EvmCompilerInput<'a> { - /// EVM bytecode. - Code(&'a [u8]), -} - -impl<'a> From<&'a [u8]> for EvmCompilerInput<'a> { - fn from(code: &'a [u8]) -> Self { - EvmCompilerInput::Code(code) - } -} - -impl<'a> From<&'a Vec> for EvmCompilerInput<'a> { - fn from(code: &'a Vec) -> Self { - EvmCompilerInput::Code(code) - } -} - -impl<'a> From<&'a Bytes> for EvmCompilerInput<'a> { - fn from(code: &'a Bytes) -> Self { - EvmCompilerInput::Code(code) - } -} - -#[allow(dead_code)] mod default_attrs { use evm2_jit_backend::Attribute; @@ -1152,14 +1114,6 @@ mod default_attrs { for_param().chain([Attribute::NoCapture]) } - pub(crate) fn for_sized_ptr((size, align): (usize, usize)) -> impl Iterator { - for_ptr().chain([Attribute::Dereferenceable(size as u64), Attribute::Align(align as u64)]) - } - - pub(crate) fn for_ptr_t() -> impl Iterator { - for_sized_ptr(size_align::()) - } - pub(crate) fn for_ref() -> impl Iterator { // `NoFree` keeps `dereferenceable` valid past control flow, which LICM needs to promote // stack slots in loops. @@ -1169,14 +1123,6 @@ mod default_attrs { pub(crate) fn for_sized_ref((size, align): (usize, usize)) -> impl Iterator { for_ref().chain([Attribute::Dereferenceable(size as u64), Attribute::Align(align as u64)]) } - - pub(crate) fn for_ref_t() -> impl Iterator { - for_sized_ref(size_align::()) - } - - pub(crate) const fn size_align() -> (usize, usize) { - (std::mem::size_of::(), std::mem::align_of::()) - } } /// Resolves a `# bytecode.txt:LL:CC` or `# bytecode.txt:LL` comment in an assembly line diff --git a/crates/jit/codegen/src/compiler/translate/mod.rs b/crates/jit/codegen/src/compiler/translate/mod.rs index 1e8539ac0..ea1f1e877 100644 --- a/crates/jit/codegen/src/compiler/translate/mod.rs +++ b/crates/jit/codegen/src/compiler/translate/mod.rs @@ -1,6 +1,5 @@ //! EVM to IR translation. -use super::default_attrs; use crate::{ Backend, Builder, Bytecode, EvmContext, Inst, InstData, InstFlags, IntCC, Result, StackSection, decode_pair, decode_single, @@ -11,10 +10,10 @@ use evm2::{ env::{BlockEnv, TxEnv}, interpreter::{InstrStop, Interpreter, Message, op}, }; -use evm2_jit_backend::{Attribute, BackendTypes, FunctionAttributeLocation, Pointer, TypeMethods}; +use evm2_jit_backend::{BackendTypes, Pointer, TypeMethods}; use evm2_jit_builtins::{Builtin, Builtins, CallKind, CreateKind}; use oxc_index::IndexVec; -use std::{mem, ops::Range}; +use std::{fmt::Write, mem, ops::Range}; mod peephole; @@ -163,10 +162,6 @@ impl HostLayout { /// A list of incoming values for a block. Represents a `phi` node. type Incoming = Vec<(::Value, ::BasicBlock)>; -/// A list of `switch` targets. -#[allow(dead_code)] -type SwitchTargets = Vec<(u64, ::BasicBlock)>; - pub(super) struct FunctionCx<'a, B: Backend> { // Configuration. config: FcxConfig, @@ -529,8 +524,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.set_debug_location(self.inst_lines[inst], 1); } - // self.call_printf(format_printf!("{}\n", self.op_block_name("")), &[]); - let branch_to_next_opcode = |this: &mut Self| { debug_assert!( !this.bytecode.is_instr_diverging(inst), @@ -648,10 +641,8 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // skip the real logic and just write the result. // The inputs are not loaded; we simply adjust the stack offset to consume them and // push the folded constant. This turns e.g. `PUSH 3, PUSH 4, ADD` into a single - // store of `7`. - // Pure ops with a known-constant output: skip the opcode logic and just - // store the folded constant. Works for single-output arithmetic (ADD, MUL, - // ...) and DUP (which adds one new TOS without consuming its input). + // store of `7`. Works for single-output arithmetic (ADD, MUL, ...) and DUP (which adds + // one new TOS without consuming its input). if out >= 1 && let Some(const_out) = self.bytecode.foldable_output(inst) { @@ -1016,13 +1007,13 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let offset = self.pop(); let addr = self.build_ensure_memory(offset, 32); let value = self.bcx.load_aligned(self.word_type, addr, 1, "mload.value"); - let value = if self.little_endian() { self.bcx.bswap(value) } else { value }; + let value = self.bcx.bswap(value); self.push(value); } op::MSTORE => { let [offset, value] = self.popn(); let addr = self.build_ensure_memory(offset, 32); - let value = if self.little_endian() { self.bcx.bswap(value) } else { value }; + let value = self.bcx.bswap(value); self.bcx.store_aligned(value, addr, 1); } op::MSTORE8 => { @@ -1325,7 +1316,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { if d.is_dead_code() { continue; } - use std::fmt::Write; let _ = write!( section_dump, "\n ic{i} pc={} {:?} io={:?} flags={:?} gas={:?} stack={:?}{}{}", @@ -1974,15 +1964,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.load(ptr_type, mem_base_field, "ecx.mem_base") } - /* - /// Builds a check, failing if the condition is false. - /// - /// `if success_cond { ... } else { return ret }` - fn build_failure_inv(&mut self, success_cond: B::Value, ret: InstrStop) { - self.build_failure_imm_inner(false, success_cond, ret); - } - */ - /// Emits under/overflow bounds checks for a stack section. fn check_stack_bounds(&mut self, stack_section: StackSection) { let inp = stack_section.inputs; @@ -2208,15 +2189,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { block } - #[allow(dead_code)] - fn call_printf(&mut self, template: &std::ffi::CStr, values: &[B::Value]) { - let mut args = Vec::with_capacity(values.len() + 1); - args.push(self.bcx.cstr_const(template)); - args.extend_from_slice(values); - let printf = self.bcx.get_printf_function(); - let _ = self.bcx.call(printf, &args); - } - /// Build a call to a fallible builtin. /// /// The builtin longjmps on error, so no return value check is needed. @@ -2229,10 +2201,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { #[must_use] fn call_builtin(&mut self, builtin: Builtin, args: &[B::Value]) -> Option { let function = self.builtin_function(builtin); - // self.call_printf( - // format_printf!("{} - calling {}\n", self.op_block_name(""), builtin.name()), - // &[], - // ); let invalidate_mem_base = !self.current_inst.is_some_and(|inst| self.can_skip_ensure_memory(inst)); let value = self.bcx.call(function, args); @@ -2290,18 +2258,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.current_block().expect("no blocks") } - const fn little_endian(&self) -> bool { - true - } - - /* - /// Creates a named block. - fn create_block(&mut self, name: &str) -> B::BasicBlock { - let name = self.op_block_name(name); - self.bcx.create_block(&name) - } - */ - /// Creates a named block after the given block. fn create_block_after(&mut self, after: B::BasicBlock, name: &str) -> B::BasicBlock { let name = self.op_block_name(name); @@ -2329,78 +2285,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } } -/// IR builtins. -impl FunctionCx<'_, B> { - #[allow(dead_code)] - #[must_use] - fn call_ir_builtin( - &mut self, - name: &str, - args: &[B::Value], - arg_types: &[B::Type], - ret: Option, - build: impl FnOnce(&mut Self), - ) -> Option { - let prefix = "__revmc_ir_builtin_"; - let name = &format!("{prefix}{name}")[..]; - - // self.call_printf(format_printf!("{} - calling {name}\n", self.op_block_name("")), &[]); - - debug_assert_eq!(args.len(), arg_types.len()); - let linkage = evm2_jit_backend::Linkage::Private; - // SAFETY: `this` aliases `self` to work around the borrow checker: we need `self.bcx` - // borrowed by `get_or_build_function` while also swapping `self.bcx` inside the closure. - // The closure's `bcx` is a fresh builder (not `self.bcx`), so the swap is safe. - let debug_location = self - .config - .debug - .then(|| self.current_inst.map(|inst| self.inst_lines[inst])) - .flatten(); - self.bcx.clear_debug_location(); - - let this = unsafe { &mut *(self as *mut Self) }; - let f = self.bcx.get_or_build_function(name, arg_types, ret, linkage, |bcx| { - let prev_return_block = this.return_block.take(); - let prev_failure_block = this.failure_block.take(); - // SAFETY: `this.bcx` and `bcx` are non-overlapping (bcx is a fresh builder). - unsafe { std::ptr::swap(&mut this.bcx, bcx) }; - - for attr in default_attrs::for_fn().chain(std::iter::once(Attribute::NoUnwind)) { - this.bcx.add_function_attribute(None, attr, FunctionAttributeLocation::Function) - } - for i in 0..this.bcx.num_fn_params() as u32 { - for attr in default_attrs::for_param() { - this.bcx.add_function_attribute(None, attr, FunctionAttributeLocation::Param(i)) - } - } - build(this); - - // SAFETY: same as above. - unsafe { std::ptr::swap(&mut this.bcx, bcx) }; - this.failure_block = prev_failure_block; - this.return_block = prev_return_block; - }); - if let Some(line) = debug_location { - self.bcx.set_debug_location(line, 1); - } - self.bcx.call(f, args) - } -} - -// HACK: Need these structs' fields to be public for `offset_of!`. -// `pf == private_fields`. -#[allow(dead_code)] -mod pf { - use super::*; - - #[repr(C)] // See core::ptr::metadata::PtrComponents - pub(super) struct Slice { - pub(super) ptr: *const u8, - pub(super) len: usize, - } - const _: [(); mem::size_of::<&'static [u8]>()] = [(); mem::size_of::()]; -} - fn get_field(bcx: &mut B, ptr: B::Value, offset: usize, name: &str) -> B::Value { // A field at offset 0 is `ptr` itself; a zero GEP would only clutter the IR. if offset == 0 { @@ -2409,12 +2293,3 @@ fn get_field(bcx: &mut B, ptr: B::Value, offset: usize, name: &str) let offset = bcx.iconst(bcx.type_ptr_sized_int(), offset as i64); bcx.gep(bcx.type_int(8), ptr, &[offset], name) } - -#[allow(unused)] -macro_rules! format_printf { - ($($t:tt)*) => { - &std::ffi::CString::new(format!($($t)*)).unwrap() - }; -} -#[allow(unused)] -use format_printf; diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index f42914052..df1abf7d7 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -13,7 +13,7 @@ mod bytecode; pub use bytecode::*; mod compiler; -pub use compiler::{CompileTimings, EvmCompiler, EvmCompilerInput, HostLayout}; +pub use compiler::{CompileTimings, EvmCompiler, HostLayout}; mod linker; pub use linker::{Linker, shared_library_path}; From 5acbba505967cb1b9249934e5615aeb28e4c2933 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:22 +0100 Subject: [PATCH 051/159] refactor(jit): derive opcode availability from the core opcode config (#903) The JIT kept its own per-fork opcode table (`SPEC_GATED_OPCODES`, 24 hand-written opcode and spec pairs) next to the one in evm2 core, along with a dynamic-gas opcode list and flag that only tests read, and an empty `UNSUPPORTED_OPCODES` list that still had a code path. `make_map` now marks an opcode disabled when the core opcode config for the spec leaves it undefined (`INVALID` keeps its own stop reason), and takes its static gas from the same config. The lists, the `DYNAMIC` flag, and the forced zero gas for `SSTORE`, `CREATE` and `CREATE2` (core already gives them zero static gas) are gone; `FULLY_DYNAMIC` stays only as the rule that a host gas change on those opcodes needs the host's own instruction. A new test checks the generated map against core for every spec. The compiler's gas limit uses `DEFAULT_COMPILER_GAS_LIMIT` instead of repeating `100_000`. ## Benchmarks The CI codegen benchmark against `main` reports no codegen changes: `opt.s` changed in 0/31 benchmarks, and optimized IR lines, assembly lines, JIT size and compile and runtime instructions are all equal (the cycle figures vary with the runner). --- crates/jit/codegen/src/bytecode/info.rs | 157 +++++------------------- crates/jit/codegen/src/bytecode/mod.rs | 2 +- 2 files changed, 35 insertions(+), 124 deletions(-) diff --git a/crates/jit/codegen/src/bytecode/info.rs b/crates/jit/codegen/src/bytecode/info.rs index eaefd42b2..698f32eaf 100644 --- a/crates/jit/codegen/src/bytecode/info.rs +++ b/crates/jit/codegen/src/bytecode/info.rs @@ -11,8 +11,6 @@ pub struct OpcodeInfo(u32); impl OpcodeInfo { /// The unknown flag. pub const UNKNOWN: u32 = 1 << 31; - /// The dynamic flag. - pub const DYNAMIC: u32 = 1 << 30; /// The disabled flag. pub const DISABLED: u32 = 1 << 29; /// The host-instruction flag. @@ -34,12 +32,6 @@ impl OpcodeInfo { self.0 == Self::UNKNOWN } - /// Returns `true` if the gas cost is dynamic. - #[inline] - pub const fn is_dynamic(self) -> bool { - self.0 & Self::DYNAMIC != 0 - } - /// Returns `true` if the opcode is known, but disabled in the current EVM /// version. #[inline] @@ -86,12 +78,6 @@ impl OpcodeInfo { self.0 = Self::UNKNOWN; } - /// Sets the dynamic flag. - #[inline] - pub const fn set_dynamic(&mut self) { - self.0 |= Self::DYNAMIC; - } - /// Sets the disabled flag. #[inline] pub const fn set_disabled(&mut self) { @@ -117,69 +103,10 @@ pub const fn op_info_map(spec_id: SpecId) -> &'static [OpcodeInfo; 256] { &MAPS[spec_id as usize] } -/// Opcodes with a dynamic gas component that also have a base (static) cost deducted upfront. -/// Only the dynamic part is paid in builtins. -const DYNAMIC_WITH_BASE_GAS: &[u8] = &[ - op::EXP, - op::KECCAK256, - op::BALANCE, - op::CALLDATACOPY, - op::CODECOPY, - op::EXTCODESIZE, - op::EXTCODECOPY, - op::RETURNDATACOPY, - op::EXTCODEHASH, - op::MLOAD, - op::MSTORE, - op::MSTORE8, - op::MCOPY, - op::LOG0, - op::LOG1, - op::LOG2, - op::LOG3, - op::LOG4, - op::SLOAD, - op::CALL, - op::CALLCODE, - op::DELEGATECALL, - op::STATICCALL, - op::SELFDESTRUCT, -]; - -/// Opcodes whose gas cost is entirely dynamic — computed fully in builtins at runtime. +/// Opcodes whose builtin pays the whole gas cost, including the static part, at runtime. Core +/// gives them no static gas, so a host cannot change it without running its own instruction. const FULLY_DYNAMIC: &[u8] = &[op::SSTORE, op::CREATE, op::CREATE2]; -/// Opcodes that are gated behind a specific `SpecId`, paired with the spec they were introduced in. -const SPEC_GATED_OPCODES: &[(u8, SpecId)] = &[ - (op::SHL, SpecId::PETERSBURG), - (op::SHR, SpecId::PETERSBURG), - (op::SAR, SpecId::PETERSBURG), - (op::CLZ, SpecId::OSAKA), - (op::RETURNDATASIZE, SpecId::BYZANTIUM), - (op::RETURNDATACOPY, SpecId::BYZANTIUM), - (op::EXTCODEHASH, SpecId::PETERSBURG), - (op::CHAINID, SpecId::ISTANBUL), - (op::SELFBALANCE, SpecId::ISTANBUL), - (op::BASEFEE, SpecId::LONDON), - (op::BLOBHASH, SpecId::CANCUN), - (op::BLOBBASEFEE, SpecId::CANCUN), - (op::TLOAD, SpecId::CANCUN), - (op::TSTORE, SpecId::CANCUN), - (op::MCOPY, SpecId::CANCUN), - (op::PUSH0, SpecId::SHANGHAI), - (op::DELEGATECALL, SpecId::HOMESTEAD), - (op::CREATE2, SpecId::PETERSBURG), - (op::STATICCALL, SpecId::BYZANTIUM), - (op::REVERT, SpecId::BYZANTIUM), - (op::DUPN, SpecId::AMSTERDAM), - (op::SWAPN, SpecId::AMSTERDAM), - (op::EXCHANGE, SpecId::AMSTERDAM), - (op::SLOTNUM, SpecId::AMSTERDAM), -]; - -/// Opcodes defined by the opcode table but intentionally disabled for compiled execution. -const UNSUPPORTED_OPCODES: &[u8] = &[]; - const fn contains(haystack: &[u8], needle: u8) -> bool { let mut i = 0; while i < haystack.len() { @@ -196,7 +123,6 @@ const fn make_map() -> [OpcodeInfo; 256] { BASE_SPEC_ID, { u32::MAX }, > as EvmConfig>::OPCODE_CONFIG; - let spec_id = SpecId::try_from_u32(BASE_SPEC_ID).expect("invalid spec id"); let mut map = [OpcodeInfo(OpcodeInfo::UNKNOWN); 256]; let mut i = 0u16; @@ -209,49 +135,20 @@ const fn make_map() -> [OpcodeInfo; 256] { continue; } - // Mark unsupported opcodes as disabled rather than - // unknown, so they return `NotActivated` instead of `OpcodeNotFound` at runtime. - if contains(UNSUPPORTED_OPCODES, op) { - map[op as usize].set_disabled(); - i += 1; - continue; - } - - let is_fully_dynamic = contains(FULLY_DYNAMIC, op); - let is_dynamic_with_base = contains(DYNAMIC_WITH_BASE_GAS, op); - - // Fully dynamic opcodes have their entire gas cost handled in builtins. - let gas = if is_fully_dynamic { - 0u16 + // Mark opcodes the version leaves out as disabled rather than unknown, so they return + // `NotActivated` instead of `OpcodeNotFound` at runtime. `INVALID` is never in a config + // and keeps its own stop reason. + let mut info = OpcodeInfo::new(0); + if !config.is_defined(op) && op != op::INVALID { + info.set_disabled(); } else { - let static_gas = config.static_gas(op) as u64; - assert!( - static_gas <= OpcodeInfo::MASK as u64, - "static gas exceeds OpcodeInfo capacity" - ); - static_gas as u16 - }; - - let mut info = OpcodeInfo::new(gas); - - if is_fully_dynamic || is_dynamic_with_base { - info.set_dynamic(); + info.set_gas(config.static_gas(op)); } map[op as usize] = info; i += 1; } - // Apply spec-gating: mark opcodes as disabled if the current spec is before their introduction. - let mut j = 0; - while j < SPEC_GATED_OPCODES.len() { - let (op, required_spec) = SPEC_GATED_OPCODES[j]; - if !spec_id.enables(required_spec) { - map[op as usize].set_disabled(); - } - j += 1; - } - map } @@ -302,10 +199,9 @@ pub(crate) fn opcode_infos(view: &OpcodeView, spec_id: SpecId) -> [OpcodeInfo; 2 continue; } let mut info = base[i]; - info.set_gas(if contains(FULLY_DYNAMIC, op) && !host { 0 } else { view.static_gas(op) }); + info.set_gas(view.static_gas(op)); if host { info.set_host(); - info.set_dynamic(); } map[i] = info; } @@ -315,6 +211,27 @@ pub(crate) fn opcode_infos(view: &OpcodeView, spec_id: SpecId) -> [OpcodeInfo; 2 #[cfg(test)] mod tests { use super::*; + use evm2::OpcodeConfig; + + #[test] + fn matches_core_opcode_config() { + for spec in 0..SpecId::COUNT as u32 { + let spec_id = SpecId::try_from_u32(spec).unwrap(); + let config = OpcodeConfig::::base_for_spec(spec_id); + let map = op_info_map(spec_id); + for op in 0..=u8::MAX { + let info = map[op as usize]; + if OpCode::new(op).is_none() { + assert!(info.is_unknown(), "{spec_id:?} {op:#04x}"); + } else if op == op::INVALID || config.is_defined(op) { + assert!(!info.is_unknown() && !info.is_disabled(), "{spec_id:?} {op:#04x}"); + assert_eq!(info.base_gas(), config.static_gas(op), "{spec_id:?} {op:#04x}"); + } else { + assert!(info.is_disabled(), "{spec_id:?} {op:#04x}"); + } + } + } + } #[test] fn test_clz_flags() { @@ -362,9 +279,8 @@ mod tests { assert_eq!(cancun[op::MUL as usize].base_gas(), 5); assert_eq!(cancun[op::ADDMOD as usize].base_gas(), 8); - // EXP: base 10, dynamic. + // EXP: static gas only; the dynamic part is paid in builtins. assert_eq!(cancun[op::EXP as usize].base_gas(), 10); - assert!(cancun[op::EXP as usize].is_dynamic()); // PUSH/DUP/SWAP gas. assert_eq!(cancun[op::PUSH1 as usize].base_gas(), 3); @@ -378,13 +294,10 @@ mod tests { assert_eq!(cancun[op::LOG2 as usize].base_gas(), 375); assert_eq!(cancun[op::LOG3 as usize].base_gas(), 375); assert_eq!(cancun[op::LOG4 as usize].base_gas(), 375); - assert!(cancun[op::LOG0 as usize].is_dynamic()); - // Memory ops: dynamic with base cost 3. + // Memory ops: static gas only. assert_eq!(cancun[op::MLOAD as usize].base_gas(), 3); - assert!(cancun[op::MLOAD as usize].is_dynamic()); assert_eq!(cancun[op::KECCAK256 as usize].base_gas(), 30); - assert!(cancun[op::KECCAK256 as usize].is_dynamic()); // Transient storage (Cancun). assert_eq!(cancun[op::TLOAD as usize].base_gas(), 100); @@ -412,18 +325,16 @@ mod tests { assert!(pre_shanghai[op::PUSH0 as usize].is_disabled()); assert!(!cancun[op::PUSH0 as usize].is_disabled()); - // Dynamic-with-base-gas opcodes: base gas varies by spec. + // Static gas of opcodes with a dynamic component varies by spec. let frontier = op_info_map(SpecId::FRONTIER); assert_eq!(frontier[op::SLOAD as usize].base_gas(), 50); assert_eq!(frontier[op::SELFDESTRUCT as usize].base_gas(), 0); - assert!(frontier[op::SELFDESTRUCT as usize].is_dynamic()); assert_eq!(frontier[op::CALL as usize].base_gas(), 40); assert_eq!(frontier[op::BALANCE as usize].base_gas(), 20); assert_eq!(frontier[op::EXTCODESIZE as usize].base_gas(), 20); let tangerine = op_info_map(SpecId::TANGERINE); assert_eq!(tangerine[op::SLOAD as usize].base_gas(), 200); assert_eq!(tangerine[op::SELFDESTRUCT as usize].base_gas(), 5000); - assert!(tangerine[op::SELFDESTRUCT as usize].is_dynamic()); assert_eq!(tangerine[op::CALL as usize].base_gas(), 700); assert_eq!(tangerine[op::BALANCE as usize].base_gas(), 400); assert_eq!(tangerine[op::EXTCODESIZE as usize].base_gas(), 700); diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index c9927952f..55d2c16d5 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -365,7 +365,7 @@ impl<'a> Bytecode<'a> { redirects: FxHashMap::default(), cfg: Cfg::default(), config: AnalysisConfig::default(), - compiler_gas_limit: 100_000, + compiler_gas_limit: DEFAULT_COMPILER_GAS_LIMIT, compiler_gas_used: 0, } } From dc01b42e58e36b84dc5c33157e3130457e175afb Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:32 +0100 Subject: [PATCH 052/159] fix(jit): return LLVM backend errors instead of dropping them (#908) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LLVM backend dropped three kinds of error. The diagnostic handler logged error diagnostics and let compilation continue, so errors raised through `LLVMContext::emitError` (for example running out of registers) could produce broken code that was then linked. Only the first caller of `init()` saw its result, so a failed initialization made the next `EvmLlvmBackend::new` panic unwrapping the target. And a failing process symbol generator was ignored until it showed up as an unrelated missing symbol. The diagnostic handler guard now records the first error diagnostic, and optimization, object and assembly emission and JIT lookups return it as an error. `init()` and the host target info cache their result and return it to every caller, and creating a JITDylib fails if the process symbol generator cannot be created. A unit test checks that an LLVM error fails object emission and does not leak into the next backend. ## Benchmarks Generated code is unchanged: `opt.s` changed in 0/31 benchmarks, and compile instructions are equal. Head `d7af56e161` vs base `origin/main` (`66003c7d54`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |

Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|---------:|------------:| | push0_proxy | - | - | +9.7% 🔴 | | eip2935 | - | - | -6.2% 🟢 | | **TOTAL** | **-** | **-** | **-0.6% 🟢** | | **GEOMEAN** | **-** | **-** | **-0.2% 🟢** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------:|----------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | -
-
- | -
-
- | 13.0ms
12.6ms
-2.7% 🟢 | 164.1µs
165.3µs
+0.7% 🔴 | 379.4µs
390.5µs
+2.9% 🔴 | 1.33ms
1.38ms
+3.7% 🔴 | 2.83ms
3.01ms
+6.2% 🔴 | | factorial | -
-
- | -
-
- | 11.8ms
12.0ms
+1.3% 🔴 | 147.4µs
150.3µs
+1.9% 🔴 | 343.2µs
359.1µs
+4.6% 🔴 | 1.14ms
1.21ms
+5.9% 🔴 | 3.06ms
3.21ms
+4.8% 🔴 | | counter | -
-
- | -
-
- | 9.52ms
9.46ms
-0.6% 🟢 | 298.2µs
297.7µs
-0.2% 🟢 | 454.6µs
455.5µs
+0.2% 🔴 | 1.39ms
1.58ms
+14.3% 🔴 | 3.68ms
3.64ms
-0.9% 🟢 | | snailtracer | -
-
- | -
-
- | 390.0ms
380.7ms
-2.4% 🟢 | 33.6ms
32.4ms
-3.6% 🟢 | 6.70ms
6.76ms
+1.0% 🔴 | 68.0ms
67.3ms
-0.9% 🟢 | 317.8ms
315.4ms
-0.8% 🟢 | | weth | -
-
- | -
-
- | 48.9ms
49.3ms
+0.7% 🔴 | 2.19ms
2.27ms
+3.7% 🔴 | 1.95ms
1.97ms
+1.0% 🔴 | 11.7ms
11.6ms
-1.1% 🟢 | 32.8ms
33.2ms
+1.4% 🔴 | | hash_10k | -
-
- | -
-
- | 11.9ms
11.7ms
-1.5% 🟢 | 265.4µs
304.8µs
+14.8% 🔴 | 457.9µs
500.2µs
+9.2% 🔴 | 1.81ms
2.21ms
+21.8% 🔴 | 5.18ms
5.51ms
+6.4% 🔴 | | erc20_transfer | -
-
- | -
-
- | 51.2ms
50.1ms
-2.2% 🟢 | 2.28ms
2.27ms
-0.7% 🟢 | 1.98ms
1.96ms
-0.9% 🟢 | 12.5ms
11.9ms
-4.6% 🟢 | 33.7ms
33.9ms
+0.7% 🔴 | | push0_proxy | -
-
- | -
-
- | 10.5ms
11.5ms
+9.7% 🔴 | 134.1µs
135.2µs
+0.9% 🔴 | 342.9µs
335.2µs
-2.2% 🟢 | 834.8µs
818.0µs
-2.0% 🟢 | 2.16ms
2.17ms
+0.3% 🔴 | | usdc_proxy | -
-
- | -
-
- | 25.5ms
25.2ms
-1.0% 🟢 | 1.31ms
1.32ms
+0.6% 🔴 | 1.16ms
1.24ms
+7.5% 🔴 | 5.85ms
6.39ms
+9.3% 🔴 | 14.6ms
14.7ms
+0.1% 🔴 | | fiat_token | -
-
- | -
-
- | 306.7ms
309.2ms
+0.8% 🔴 | 14.2ms
14.2ms
-0.3% 🟢 | 10.2ms
10.3ms
+0.6% 🔴 | 68.6ms
69.7ms
+1.5% 🔴 | 243.7ms
244.0ms
+0.1% 🔴 | | uniswap_v2_pair | -
-
- | -
-
- | 165.5ms
167.3ms
+1.1% 🔴 | 7.49ms
7.50ms
+0.2% 🔴 | 5.92ms
5.86ms
-1.0% 🟢 | 34.0ms
34.5ms
+1.2% 🔴 | 129.0ms
130.6ms
+1.2% 🔴 | | univ2_router | -
-
- | -
-
- | 346.6ms
347.6ms
+0.3% 🔴 | 15.4ms
15.8ms
+2.2% 🔴 | 10.4ms
11.0ms
+5.5% 🔴 | 69.1ms
69.9ms
+1.1% 🔴 | 284.9ms
285.9ms
+0.4% 🔴 | | seaport | -
-
- | -
-
- | 817.0ms
808.6ms
-1.0% 🟢 | 26.1ms
26.4ms
+1.1% 🔴 | 17.4ms
17.8ms
+2.4% 🔴 | 175.6ms
176.3ms
+0.4% 🔴 | 695.8ms
695.5ms
-0.1% 🟢 | | airdrop | -
-
- | -
-
- | 116.7ms
116.1ms
-0.5% 🟢 | 4.74ms
4.79ms
+1.1% 🔴 | 4.19ms
4.06ms
-3.1% 🟢 | 29.2ms
29.4ms
+0.5% 🔴 | 83.0ms
83.7ms
+0.8% 🔴 | | bswap64 | -
-
- | -
-
- | 10.6ms
10.8ms
+1.7% 🔴 | 459.8µs
477.2µs
+3.8% 🔴 | 542.5µs
574.1µs
+5.8% 🔴 | 1.57ms
1.66ms
+5.5% 🔴 | 3.88ms
3.88ms
-0.1% 🟢 | | bswap64_opt | -
-
- | -
-
- | 10.2ms
10.3ms
+1.2% 🔴 | 414.1µs
409.7µs
-1.1% 🟢 | 489.1µs
510.4µs
+4.4% 🔴 | 1.66ms
1.69ms
+1.9% 🔴 | 4.30ms
4.23ms
-1.6% 🟢 | | eip4788 | -
-
- | -
-
- | 8.48ms
8.45ms
-0.4% 🟢 | 185.7µs
193.6µs
+4.3% 🔴 | 372.7µs
391.5µs
+5.0% 🔴 | 1.00ms
1.10ms
+9.6% 🔴 | 3.40ms
3.42ms
+0.4% 🔴 | | eip2935 | -
-
- | -
-
- | 11.8ms
11.1ms
-6.2% 🟢 | 187.5µs
165.6µs
-11.7% 🟢 | 567.3µs
352.7µs
-37.8% 🟢 | 945.3µs
968.0µs
+2.4% 🔴 | 2.97ms
2.94ms
-1.1% 🟢 | | burntpix | -
-
- | -
-
- | 506.6ms
506.9ms
+0.1% 🔴 | 14.2ms
13.7ms
-3.6% 🟢 | 1.56ms
1.58ms
+1.1% 🔴 | 9.98ms
10.1ms
+0.9% 🔴 | 28.0ms
28.2ms
+0.7% 🔴 | | curve_stableswap | -
-
- | -
-
- | 95.0ms
94.7ms
-0.3% 🟢 | 3.22ms
3.24ms
+0.6% 🔴 | 2.46ms
2.48ms
+1.2% 🔴 | 21.1ms
21.2ms
+0.5% 🔴 | 48.0ms
48.1ms
+0.2% 🔴 | | onchain_lm_v2 | -
-
- | -
-
- | 274.4ms
274.6ms
+0.1% 🔴 | 10.4ms
10.5ms
+0.8% 🔴 | 7.07ms
7.02ms
-0.7% 🟢 | 46.9ms
47.5ms
+1.1% 🔴 | 189.3ms
192.2ms
+1.5% 🔴 | | usdt | -
-
- | -
-
- | 108.2ms
108.9ms
+0.6% 🔴 | 6.50ms
6.59ms
+1.3% 🔴 | 4.99ms
5.13ms
+2.8% 🔴 | 25.2ms
25.7ms
+1.7% 🔴 | 78.5ms
79.6ms
+1.4% 🔴 | | universal_router | -
-
- | -
-
- | 918.8ms
911.9ms
-0.8% 🟢 | 13.7ms
13.9ms
+1.6% 🔴 | 10.9ms
10.8ms
-0.4% 🟢 | 80.9ms
81.7ms
+1.0% 🔴 | 318.7ms
320.9ms
+0.7% 🔴 | | aggregation_router_v6 | -
-
- | -
-
- | 2.729s
2.715s
-0.5% 🟢 | 22.3ms
22.5ms
+0.9% 🔴 | 16.9ms
16.8ms
-0.6% 🟢 | 138.1ms
139.0ms
+0.7% 🔴 | 528.3ms
526.4ms
-0.4% 🟢 | | entry_point_v0_7 | -
-
- | -
-
- | 1.408s
1.400s
-0.5% 🟢 | 10.6ms
10.8ms
+1.1% 🔴 | 9.10ms
9.66ms
+6.2% 🔴 | 63.3ms
65.7ms
+3.9% 🔴 | 273.9ms
273.1ms
-0.3% 🟢 | | uniswap_v3_pool | -
-
- | -
-
- | 496.1ms
493.1ms
-0.6% 🟢 | 21.8ms
23.1ms
+5.8% 🔴 | 16.1ms
16.1ms
+0.1% 🔴 | 110.7ms
111.4ms
+0.6% 🔴 | 405.4ms
410.3ms
+1.2% 🔴 | | uniswap_v4_pool_manager | -
-
- | -
-
- | 528.3ms
526.9ms
-0.3% 🟢 | 15.6ms
15.7ms
+0.6% 🔴 | 13.1ms
12.9ms
-1.7% 🟢 | 108.3ms
107.7ms
-0.5% 🟢 | 400.5ms
397.7ms
-0.7% 🟢 | | curve_3pool | -
-
- | -
-
- | 492.6ms
492.3ms
-0.1% 🟢 | 21.1ms
21.8ms
+3.1% 🔴 | 13.4ms
13.4ms
+0.5% 🔴 | 87.0ms
87.7ms
+0.9% 🔴 | 439.3ms
440.5ms
+0.3% 🔴 | | curve_tricrypto2 | -
-
- | -
-
- | 693.6ms
693.2ms
-0.1% 🟢 | 20.6ms
20.0ms
-3.3% 🟢 | 14.9ms
15.0ms
+0.5% 🔴 | 96.2ms
98.9ms
+2.8% 🔴 | 424.5ms
428.4ms
+0.9% 🔴 | | balancer_vault | -
-
- | -
-
- | 1.561s
1.534s
-1.8% 🟢 | 28.6ms
28.9ms
+1.0% 🔴 | 19.2ms
18.7ms
-2.9% 🟢 | 203.9ms
207.4ms
+1.7% 🔴 | 664.8ms
661.2ms
-0.5% 🟢 | | succinct_staking | -
-
- | -
-
- | 840.3ms
839.8ms
-0.1% 🟢 | 20.0ms
20.5ms
+2.3% 🔴 | 11.3ms
11.2ms
-0.6% 🟢 | 87.4ms
88.5ms
+1.3% 🔴 | 320.4ms
318.9ms
-0.5% 🟢 | | **TOTAL** | **-
-
-** | **-
-
-** | **13.017s
12.943s
-0.6% 🟢** | **318.4ms
320.3ms
+0.6% 🔴** | **204.9ms
205.7ms
+0.4% 🔴** | **1.565s
1.582s
+1.1% 🔴** | **5.986s
5.994s
+0.1% 🔴** |
--- crates/jit/llvm/src/dh.rs | 34 ++++++-- crates/jit/llvm/src/lib.rs | 154 ++++++++++++++++++++++++++++--------- 2 files changed, 146 insertions(+), 42 deletions(-) diff --git a/crates/jit/llvm/src/dh.rs b/crates/jit/llvm/src/dh.rs index 6bfe584d8..d62a0a54a 100644 --- a/crates/jit/llvm/src/dh.rs +++ b/crates/jit/llvm/src/dh.rs @@ -1,12 +1,20 @@ use crate::wrap::{Context, LLVMString}; +use evm2_jit_backend::{Result, eyre}; use llvm_sys::{LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*, core::*, prelude::*}; -use std::{ffi::c_void, fmt, ptr}; +use std::{ffi::c_void, fmt, sync::Mutex}; /// LLVM diagnostic handler guard. +/// +/// LLVM keeps compiling after an error diagnostic such as "ran out of registers" and then emits +/// broken code, so the handler records the first error and [`check`](Self::check) returns it to +/// the caller. pub(crate) struct DiagnosticHandlerGuard { cx: LLVMContextRef, prev_dh: LLVMDiagnosticHandler, prev_dhc: *mut c_void, + /// First error diagnostic since the last [`check`](Self::check). Boxed so the + /// handler's context pointer stays valid when the guard moves. + error: Box>>, } impl fmt::Debug for DiagnosticHandlerGuard { @@ -21,18 +29,34 @@ impl DiagnosticHandlerGuard { let c = cx.raw(); let prev_dh = LLVMContextGetDiagnosticHandler(c); let prev_dhc = LLVMContextGetDiagnosticContext(c); - LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), ptr::null_mut()); - Self { cx: c, prev_dh, prev_dhc } + let error = Box::new(Mutex::new(None)); + let error_ptr = &*error as *const Mutex> as *mut c_void; + LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), error_ptr); + Self { cx: c, prev_dh, prev_dhc, error } } } - extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, _context: *mut c_void) { + /// Returns `result`, adding the first error LLVM reported since the last call to it. Always + /// clears the recorded error so it does not leak into the next operation. + pub(crate) fn check(&self, result: Result) -> Result { + let Some(msg) = self.error.lock().unwrap().take() else { return result }; + match result { + Ok(_) => Err(eyre::eyre!("LLVM error: {msg}")), + Err(e) => Err(e.wrap_err(format!("LLVM error: {msg}"))), + } + } + + extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, context: *mut c_void) { unsafe { // `LLVMGetDiagInfoDescription` returns an LLVM `Message`. let msg_cstr = LLVMString::from_raw(LLVMGetDiagInfoDescription(di)); let msg = msg_cstr.to_string_lossy(); match LLVMGetDiagInfoSeverity(di) { - LLVMDSError => error!(target: "llvm", "{msg}"), + LLVMDSError => { + error!(target: "llvm", "{msg}"); + let error = &*(context as *const Mutex>); + error.lock().unwrap().get_or_insert_with(|| msg.into_owned()); + } LLVMDSWarning => warn!(target: "llvm", "{msg}"), LLVMDSRemark => trace!(target: "llvm", "{msg}"), LLVMDSNote => debug!(target: "llvm", "{msg}"), diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index a7ae2fb7c..10ec18a02 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -26,7 +26,7 @@ use std::{ mem::ManuallyDrop, path::Path, sync::{ - Arc, Once, OnceLock, + Arc, OnceLock, atomic::{AtomicU64, AtomicUsize, Ordering}, }, }; @@ -114,7 +114,7 @@ type FxHashMap = alloy_primitives::map::HashMap; #[must_use] pub struct EvmLlvmBackend { cx: &'static Context, - _dh: dh::DiagnosticHandlerGuard, + dh: dh::DiagnosticHandlerGuard, /// LLVM debug info builder and compile unit, created lazily when `debug_file` is set. /// Dropped before `module`, which its builder refers to. di_state: Option, @@ -272,20 +272,20 @@ impl GlobalOrcJit { } /// Creates a fresh JITDylib for a compiler. - fn create_jit_dylib(&self) -> orc::JITDylibRef { + fn create_jit_dylib(&self) -> Result { + // Create the process symbol generator first, so a failure leaves no dylib behind. + // It lets the JITDylib resolve libc and other process-level symbols (e.g. printf, + // memcpy) that JIT-compiled code may reference. + let prefix = self.jit.get_global_prefix(); + let generator = orc::DefinitionGenerator::for_current_process(prefix).map_err(error_msg)?; let id = self.next_dylib_id.fetch_add(1, Ordering::Relaxed); let name = CString::new(format!("evm2_jit.compiler.{id}")).unwrap(); let es = self.jit.get_execution_session(); let jd = es.create_bare_jit_dylib(&name); // Link against the builtins JD so compiled code can resolve builtin symbols. jd.add_to_link_order(self.builtins_jd); - // Attach a process symbol generator so the JITDylib can resolve libc and other - // process-level symbols (e.g. printf, memcpy) that JIT-compiled code may reference. - let prefix = self.jit.get_global_prefix(); - if let Ok(generator) = orc::DefinitionGenerator::for_current_process(prefix) { - jd.add_generator(generator); - } - jd + jd.add_generator(generator); + Ok(jd) } /// Removes a JITDylib from the ExecutionSession, freeing all its resources. @@ -393,7 +393,7 @@ impl fmt::Debug for OrcJitState { impl OrcJitState { fn new(debug_support: bool, profiling_support: bool, simple_perf: bool) -> Result { let global = GlobalOrcJit::get(debug_support, profiling_support, simple_perf)?; - let jd = global.create_jit_dylib(); + let jd = global.create_jit_dylib()?; let jd_guard = Arc::new(JitDylibGuard { global, jd }); Ok(Self { global, @@ -494,7 +494,7 @@ impl EvmLlvmBackend { let config = BackendConfig::default(); init()?; - let target_info = TargetInfo::new(); + let target_info = TargetInfo::new()?; let target = &target_info.target; let machine = target .create_target_machine( @@ -544,7 +544,7 @@ impl EvmLlvmBackend { let ty_ptr = cx.ptr_type(); Ok(Self { cx, - _dh: dh::DiagnosticHandlerGuard::new(cx), + dh: dh::DiagnosticHandlerGuard::new(cx), bcx, module, machine, @@ -705,6 +705,14 @@ impl EvmLlvmBackend { Ok(()) } + /// Removes the committed module that contains `id`, with the code of all its functions. + fn remove_committed_module(&mut self, id: u32) -> Result<()> { + let Some(orc) = &mut self.orc else { return Ok(()) }; + let Some(tracker_idx) = orc.committed_functions.remove(&id) else { return Ok(()) }; + orc.committed_functions.retain(|_, idx| *idx != tracker_idx); + orc.loaded_trackers[tracker_idx].remove().map_err(error_msg) + } + /// Returns pending absolute symbols collected while translating the current JIT module. pub fn pending_symbol_names(&self) -> Vec { self.orc @@ -737,7 +745,7 @@ impl EvmLlvmBackend { symbols: &[(CString, usize)], ) -> Result<(usize, orc::ResourceTracker, Arc)> { let orc = self.ensure_orc()?; - let jd = orc.global.create_jit_dylib(); + let jd = orc.global.create_jit_dylib()?; let jd_guard = Arc::new(JitDylibGuard { global: orc.global, jd }); let (addr, tracker) = link_jit_object_in_dylib(orc, jd, symbol_name, object, symbols)?; Ok((addr, tracker, jd_guard)) @@ -869,9 +877,11 @@ impl Backend for EvmLlvmBackend { } fn dump_disasm(&mut self, path: &Path) -> Result<()> { - self.machine + let result = self + .machine .write_to_file(self.module(), LLVMCodeGenFileType::LLVMAssemblyFile, path) - .map_err(error_msg) + .map_err(error_msg); + self.dh.check(result) } fn build_function( @@ -976,14 +986,16 @@ impl Backend for EvmLlvmBackend { } }); let opts = PassBuilderOptions::create(); - self.module().run_passes(passes, &self.machine, &opts).map_err(error_msg) + let result = self.module().run_passes(passes, &self.machine, &opts).map_err(error_msg); + self.dh.check(result) } fn write_object(&mut self, mut w: W) -> Result<()> { let buffer = self .machine .write_to_memory_buffer(self.module(), LLVMCodeGenFileType::LLVMObjectFile) - .map_err(error_msg)?; + .map_err(error_msg); + let buffer = self.dh.check(buffer)?; w.write_all(buffer.as_slice())?; Ok(()) } @@ -999,10 +1011,23 @@ impl Backend for EvmLlvmBackend { // in lookup_in when the symbol is first requested. let mut captured = None; let _guard = ScopedObjCapture::install(&mut captured); - let addr = orc.global.jit.lookup_in(orc.jd(), &name).map_err(error_msg)?; + let addr = orc.global.jit.lookup_in(orc.jd(), &name).map_err(error_msg); drop(_guard); + let compiled = addr.is_ok(); + let addr = match self.dh.check(addr) { + Ok(addr) => addr, + Err(e) => { + // ORC compiled the whole module and keeps it, so later lookups would return the + // broken code without the error. Remove the module's code but keep the function + // names, so a later lookup fails with an error instead of a missing name. + if compiled && let Err(remove_err) = self.remove_committed_module(id) { + warn!("failed to remove module after an LLVM error: {remove_err}"); + } + return Err(e); + } + }; if captured.is_some() { - orc.last_compiled_object = captured; + self.orc.as_mut().unwrap().last_compiled_object = captured; } Ok(addr) } @@ -1084,15 +1109,20 @@ struct TargetInfo { } impl TargetInfo { - fn new() -> &'static Self { - static HOST_TARGET_INFO: OnceLock = OnceLock::new(); - HOST_TARGET_INFO.get_or_init(|| { - let triple = TargetMachine::get_default_triple().to_owned(); - let target = Target::from_triple(&triple).unwrap(); - let cpu = TargetMachine::get_host_cpu_name().to_string_lossy().into_owned(); - let features = TargetMachine::get_host_cpu_features().to_string_lossy().into_owned(); - Self { target, triple, cpu, features } - }) + fn new() -> Result<&'static Self> { + static HOST_TARGET_INFO: OnceLock> = OnceLock::new(); + HOST_TARGET_INFO + .get_or_init(|| { + let triple = TargetMachine::get_default_triple().to_owned(); + let target = Target::from_triple(&triple) + .map_err(|e| e.to_string_lossy().trim_end().to_string())?; + let cpu = TargetMachine::get_host_cpu_name().to_string_lossy().into_owned(); + let features = + TargetMachine::get_host_cpu_features().to_string_lossy().into_owned(); + Ok(Self { target, triple, cpu, features }) + }) + .as_ref() + .map_err(|e| eyre::eyre!("{e}")) } } @@ -1847,17 +1877,14 @@ fn build_pass_pipeline(with_licm: bool) -> String { } fn init() -> Result<()> { - let mut init_result = Ok(()); - static INIT: Once = Once::new(); - INIT.call_once(|| init_result = init_()); - init_result + static INIT: OnceLock> = OnceLock::new(); + match INIT.get_or_init(|| init_().map_err(|e| format!("{e:#}"))) { + Ok(()) => Ok(()), + Err(e) => Err(eyre::eyre!("{e}")), + } } fn init_() -> Result<()> { - // TODO: This also reports "PLEASE submit a bug report to..." when the segfault is - // outside of LLVM. - // enable_llvm_pretty_stack_trace(); - extern "C" fn report_fatal_error(msg: *const std::ffi::c_char) { let msg_cstr = unsafe { std::ffi::CStr::from_ptr(msg) }; let msg = msg_cstr.to_string_lossy(); @@ -2100,3 +2127,56 @@ fn error_msg(msg: LLVMString) -> evm2_jit_backend::Error { fn fmt_ty(ty: Type) -> impl std::fmt::Display { ty.print_to_string().to_str().unwrap().trim_matches('"').to_string() } + +#[cfg(test)] +mod tests { + use super::*; + use llvm_sys::{LLVMInlineAsmDialect, core::*}; + + /// An error diagnostic LLVM raises while emitting code must fail `write_object`, and must not + /// leak into the next operation. + #[test] + fn diagnostic_error_fails_write_object() { + const ARGS: usize = 64; + + let mut backend = EvmLlvmBackend::new(true).unwrap(); + unsafe { + let cx = backend.cx.raw(); + let void = LLVMVoidTypeInContext(cx); + let i32 = LLVMInt32TypeInContext(cx); + let function_ty = LLVMFunctionType(void, std::ptr::null_mut(), 0, 0); + let function = LLVMAddFunction(backend.module.as_mut_ptr(), c"f".as_ptr(), function_ty); + let entry = LLVMAppendBasicBlockInContext(cx, function, c"entry".as_ptr()); + let builder = LLVMCreateBuilderInContext(cx); + LLVMPositionBuilderAtEnd(builder, entry); + + // More register inputs than the target has registers. + let mut param_tys = [i32; ARGS]; + let asm_ty = LLVMFunctionType(void, param_tys.as_mut_ptr(), ARGS as u32, 0); + let constraints = vec!["r"; ARGS].join(","); + let asm = LLVMGetInlineAsm( + asm_ty, + c"".as_ptr().cast_mut(), + 0, + constraints.as_ptr().cast::().cast_mut(), + constraints.len(), + 1, + 0, + LLVMInlineAsmDialect::LLVMInlineAsmDialectATT, + 0, + ); + let mut args = [LLVMConstInt(i32, 1, 0); ARGS]; + LLVMBuildCall2(builder, asm_ty, asm, args.as_mut_ptr(), ARGS as u32, c"".as_ptr()); + LLVMBuildRetVoid(builder); + LLVMDisposeBuilder(builder); + } + + let err = backend.write_object(Vec::new()).unwrap_err(); + assert_eq!( + err.to_string(), + "LLVM error: inline assembly requires more registers than available" + ); + + backend.dh.check(Ok(())).unwrap(); + } +} From 3f68316ea30575bf60497a0191b805c2693f5f19 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:43 +0100 Subject: [PATCH 053/159] test(jit): compare interpreter stop reasons exactly and reject dead halting expectations (#911) The JIT codegen test runner tolerated mismatches that should fail. `dupn_underflow` expected `StackOverflow` while the interpreter reports `StackUnderflow`, and `instruction_results_match_for_oracle` hid the difference. The test now expects `StackUnderflow` and the interpreter stop reason is compared with plain equality. The unused `modify_ecx` hook is gone. It changed only the JIT context and made the runner skip most checks. Halting cases did carry stack and gas expectations that nothing read; the runner now asserts that a halting case sets no stack, memory or gas expectation (defaults or the interpreter sentinel only), and those values are removed from the 23 tests that had them. The runner still accepts any stack error or any out-of-gas kind in place of another when checking the JIT's stop reason. Exact equality there fails 9 tests where the JIT returns `MemoryOOG` or `MemoryLimitOOG` where the interpreter returns `InvalidOperandOOG`, `MemoryOOG` or `OutOfGas`. That is a codegen question and is left for a later change. --- crates/jit/codegen/src/tests/mod.rs | 92 +--------------- crates/jit/codegen/src/tests/runner.rs | 147 +++++++++---------------- 2 files changed, 50 insertions(+), 189 deletions(-) diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 3d776e6ff..b9563e2f5 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -115,34 +115,26 @@ tests! { invalid(@raw { bytecode: &[op::INVALID], expected_return: InstrStop::InvalidFEOpcode, - expected_gas: 0, }), unknown(@raw { bytecode: &[0x21], expected_return: InstrStop::OpcodeNotFound, - expected_gas: 0, }), underflow1(@raw { bytecode: &[op::ADD], expected_return: InstrStop::StackUnderflow, - expected_gas: 3, }), underflow2(@raw { bytecode: &[op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 5, }), underflow3(@raw { bytecode: &[op::PUSH0, op::POP, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_gas: 7, }), underflow4(@raw { bytecode: &[op::PUSH0, op::ADD, op::POP], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 5, }), overflow_not0(@raw { bytecode: &[op::PUSH0; 1023], @@ -159,14 +151,10 @@ tests! { overflow0(@raw { bytecode: &[op::PUSH0; 1025], expected_return: InstrStop::StackOverflow, - expected_stack: &[0_U256; 1024], - expected_gas: 2 * 1025, }), overflow1(@raw { bytecode: &[op::PUSH0; 1026], expected_return: InstrStop::StackOverflow, - expected_stack: &[0_U256; 1024], - expected_gas: 2 * 1025, }), } @@ -175,7 +163,6 @@ tests! { bytecode: &[op::PUSH0], spec_id: SpecId::MERGE, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), push0_shanghai(@raw { bytecode: &[op::PUSH0], @@ -193,44 +180,32 @@ tests! { bytecode: &[op::MSIZE, op::CLZ], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), clz_arrow_glacier(@raw { bytecode: &[op::MSIZE, op::CLZ], spec_id: SpecId::LONDON, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dupn_cancun(@raw { bytecode: &[op::PUSH0, op::DUPN, 0x00], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swapn_cancun(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x00], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), exchange_cancun(@raw { bytecode: &[op::PUSH0, op::EXCHANGE, 0x01], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), slotnum_cancun(@raw { bytecode: &[op::SLOTNUM], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // EXP gas: pre-SPURIOUS_DRAGON exp_byte_gas=10, post=50. @@ -410,32 +385,24 @@ tests! { dupn_underflow(@raw { bytecode: &[op::PUSH0, op::DUPN, 0x80], spec_id: SpecId::AMSTERDAM, - expected_return: InstrStop::StackOverflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, + expected_return: InstrStop::StackUnderflow, }), // Insufficient stack depth: SWAPN 0x80 decodes to depth 17, but only 1 item on stack. swapn_underflow(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x80], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Insufficient stack depth: EXCHANGE 0x8E decodes to (1,2), but only 2 items on stack. exchange_underflow(@raw { bytecode: &[op::PUSH0, op::PUSH0, op::EXCHANGE, 0x8E], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swap16_dup10_selfdestruct_underflow(@raw { bytecode: &[op::SWAP16, op::DUP10, op::SELFDESTRUCT], spec_id: SpecId::OSAKA, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Invalid immediate: 0x5B (91) is in the invalid range [91, 127] for decode_single. @@ -443,15 +410,11 @@ tests! { bytecode: &[op::PUSH0, op::DUPN, 0x5B], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swapn_invalid_imm(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x5B], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The invalid immediate halts before the following instructions are charged or // stack-checked, so an empty stack does not underflow at the section head. @@ -467,8 +430,6 @@ tests! { bytecode: &[op::PUSH0, op::PUSH0, op::PUSH0, op::EXCHANGE, 82], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Truncated trailing DUPN at EOF: immediate byte missing, zero-padded to 0x00. @@ -561,8 +522,6 @@ tests! { ], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidJump, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Same masked target, but reached through an unresolved dynamic jump // (CALLDATASIZE is unknown at compile time): the dynamic dispatch table must @@ -576,8 +535,6 @@ tests! { ], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidJump, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A valid target inside a PUSH immediate: DUPN desynchronizes decoding, so the // 0x5b sits in the data of a decoded PUSH1 while the analysis (which does not @@ -655,30 +612,22 @@ tests! { unmodified_stack_after_push_jump(@raw { bytecode: &[op::PUSH1, 3, op::JUMP, op::JUMPDEST, op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 3 + 8 + 1 + 2 + 3, }), bad_jump(@raw { bytecode: &[op::JUMP], expected_return: InstrStop::StackUnderflow, - expected_gas: 8, }), bad_jumpi1(@raw { bytecode: &[op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_gas: 10, }), bad_jumpi2(@raw { bytecode: &[op::PUSH0, op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_stack: &[0_U256], - expected_gas: 2 + 10, }), bad_jumpi3(@raw { bytecode: &[op::JUMPDEST, op::PUSH0, op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_stack: &[0_U256], - expected_gas: 1 + 2 + 10, }), basic_jumpi1(@raw { @@ -699,13 +648,10 @@ tests! { basic_jumpi2_lazy_invalid_target(@raw { bytecode: &[op::PUSH1, 1, op::PUSH0, op::JUMPI, op::PUSH1, 69], expected_return: InstrStop::InvalidJump, - expected_gas: 3 + 2 + 10, }), unmodified_stack_after_push_jumpi(@raw { bytecode: &[op::PUSH1, 1, op::PUSH1, 5, op::JUMPI, op::JUMPDEST, op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 3 + 3 + 10 + 1 + 2 + 3, }), basic_loop(@raw { @@ -910,7 +856,6 @@ tests! { bytecode: &asm(&range_jump_table(1)), inspect_stack: Some(false), expected_return: InstrStop::InvalidJump, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The `JUMPI` condition is a different valid jump target in each caller, so its // constant set proves it nonzero. @@ -1306,7 +1251,6 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::InvalidJump, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The return address sits below the tracked abstract stack and is brought up with // `SWAPN`, so the return must stay dynamic. @@ -1626,38 +1570,26 @@ tests! { dynamic_jump_to_65(@raw { bytecode: &bytecode_dynamic_jump(65_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_97(@raw { bytecode: &bytecode_dynamic_jump(97_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_code_end(@raw { bytecode: &bytecode_dynamic_jump(133_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_table_end(@raw { bytecode: &bytecode_dynamic_jump(160_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_u64_max(@raw { bytecode: &bytecode_dynamic_jump(U256::from(u64::MAX)), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_u64_overflow(@raw { bytecode: &bytecode_dynamic_jump((1_U256 << 64) + 63_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -2016,7 +1948,6 @@ tests! { // No return data exists, so copying 32 bytes from offset 0 fails with OutOfOffset bytecode: &[op::PUSH1, 32, op::PUSH0, op::PUSH0, op::RETURNDATACOPY], expected_return: InstrStop::OutOfOffset, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -2165,38 +2096,26 @@ tests! { mload_overflow1(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX)], - expected_gas: 3 + 3, }), mload_overflow2(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 1, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 1)], - expected_gas: 3 + 3, }), mload_overflow3(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 31, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 31)], - expected_gas: 3 + 3, }), mload_overflow4(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 32, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 32)], - expected_gas: 3 + 3, }), mload_overflow5(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 33, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 33)], - expected_gas: 3 + 3, }), mload_overflow6(@raw { bytecode: &[op::ADDRESS, op::MLOAD], expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[DEF_ADDR.into_word().into()], - expected_gas: 5, }), mload_const_offset_too_large(@raw { bytecode: &{ @@ -2207,8 +2126,6 @@ tests! { code }, expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[0x1_0000_0000_0000_0000_U256], - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), mstore1(@raw { bytecode: &[op::PUSH0, op::PUSH0, op::MSTORE], @@ -2345,9 +2262,6 @@ tests! { MLOAD "), expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[!64_U256], - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // `TLOAD` overwrites the stack slot holding `p = 64` with `0x1000`. dyn_offset_overwritten_by_builtin(@raw { @@ -2768,14 +2682,12 @@ tests! { is_static: true, gas_limit: 4000, expected_return: InstrStop::OutOfGas, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Static-context SELFDESTRUCT: gas >= 5000 → charges 5000, then StateChangeDuringStaticCall. selfdestruct_static_enough_gas(@raw { bytecode: &[op::PUSH1, 0x01, op::SELFDESTRUCT], is_static: true, expected_return: InstrStop::StateChangeDuringStaticCall, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -3601,7 +3513,6 @@ tests! { bytecode: &[op::CALLDATASIZE, op::TSTORE], spec_id: SpecId::SHANGHAI, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // PUSH0(0->1) ; TLOAD(1->1, disabled before Cancun). @@ -3609,7 +3520,6 @@ tests! { bytecode: &[op::PUSH0, op::TLOAD], spec_id: SpecId::SHANGHAI, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Mismatched costs in < BERLIN. diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index 65f354eda..9d503edc7 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -90,7 +90,6 @@ pub struct TestCase<'a> { /// Override `inspect_stack` on the compiler. `None` uses the default (`true`). pub inspect_stack: Option, pub modify_message: Option)>, - pub modify_ecx: Option)>, pub expected_return: InstrStop, pub expected_stack: &'a [U256], @@ -110,7 +109,6 @@ impl Default for TestCase<'_> { gas_limit: DEF_GAS_LIMIT, inspect_stack: None, modify_message: None, - modify_ecx: None, expected_return: InstrStop::Stop, expected_stack: &[], expected_memory: &[], @@ -129,7 +127,6 @@ impl fmt::Debug for TestCase<'_> { .field("spec_id", &self.spec_id) .field("inspect_stack", &self.inspect_stack) .field("modify_message", &self.modify_message.is_some()) - .field("modify_ecx", &self.modify_ecx.is_some()) .field("expected_return", &self.expected_return) .field("expected_stack", &self.expected_stack) .field("expected_memory", &MemDisplay(self.expected_memory)) @@ -150,7 +147,6 @@ impl<'a> TestCase<'a> { gas_limit: DEF_GAS_LIMIT, inspect_stack: None, modify_message: None, - modify_ecx: None, expected_return: RETURN_WHAT_INTERPRETER_SAYS, expected_stack: STACK_WHAT_INTERPRETER_SAYS, expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, @@ -490,7 +486,6 @@ fn run_compiled_test_case_with_context( gas_limit, inspect_stack: _, modify_message, - modify_ecx, expected_return, expected_stack, expected_memory, @@ -506,10 +501,6 @@ fn run_compiled_test_case_with_context( if gas_limit != DEF_GAS_LIMIT { ecx.gas = Gas::new(gas_limit); } - if let Some(modify_ecx) = modify_ecx { - modify_ecx(ecx); - ecx.refresh_memory_cache(); - } // Interpreter - run evm2 as the oracle let mut message = def_message(gas_limit); @@ -523,86 +514,37 @@ fn run_compiled_test_case_with_context( }; let mut interpreter = Interpreter::::new(&message); let mut int_host = prepare_host(spec_id); - let int_stop = interpreter.run(&mut int_host).unwrap(); - let int_result = int_stop; + let int_result = interpreter.run(&mut int_host).unwrap(); let interpreter_output = interpreter.output(); - let mut expected_return = expected_return; - if expected_return == RETURN_WHAT_INTERPRETER_SAYS { - expected_return = int_result; - } else if modify_ecx.is_none() { - // Only check interpreter return if modify_ecx is not set. - // When modify_ecx is used, it only modifies the JIT context, not the interpreter's - // input, so the interpreter may return a different result. - assert!( - instruction_results_match_for_oracle(int_result, expected_return), - "interpreter return value mismatch: {int_result:?} != {expected_return:?}" - ); - } - - // When modify_ecx is set, the interpreter runs with different inputs than the JIT, - // so we cannot use interpreter results as expected values or compare against them. - let skip_interpreter_checks = modify_ecx.is_some() || expected_return.is_halt(); - let interpreter_stack_ref = (!skip_interpreter_checks).then(|| interpreter.stack()); - let interpreter_stack = interpreter_stack_ref.as_ref().map(|stack| stack.as_slice()); - - let mut expected_stack = expected_stack; - if expected_stack == STACK_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_stack = &[]; // Will skip comparison below - } else { - expected_stack = interpreter_stack.unwrap(); - } - } else if !skip_interpreter_checks { - assert_eq!(interpreter_stack.unwrap(), expected_stack, "interpreter stack mismatch"); - } + let expected_return = if expected_return == RETURN_WHAT_INTERPRETER_SAYS { + int_result + } else { + assert_eq!(int_result, expected_return, "interpreter return value mismatch"); + expected_return + }; - let interpreter_memory = interpreter.memory().as_slice(); - let mut expected_memory = expected_memory; - if expected_memory == MEMORY_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_memory = &[]; // Will skip comparison below - } else { - expected_memory = interpreter_memory; - } - } else if !skip_interpreter_checks { - assert_eq!( - MemDisplay(interpreter_memory), - MemDisplay(expected_memory), - "interpreter memory mismatch" + // On EVM halt all available gas is consumed, so the resulting stack, memory, and gas are + // unspecified and nothing compares them. A halting case must not set expectations for them. + let halts = expected_return.is_halt(); + if halts { + assert!( + matches!(expected_stack, &[] | STACK_WHAT_INTERPRETER_SAYS) + && matches!(expected_memory, &[] | MEMORY_WHAT_INTERPRETER_SAYS) + && matches!(expected_gas, 0 | GAS_WHAT_INTERPRETER_SAYS), + "halting test case sets an unchecked stack, memory, or gas expectation" ); } - let mut expected_gas = expected_gas; - if expected_gas == GAS_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_gas = 0; // Will skip comparison below - } else { - expected_gas = interpreter.gas().spent(); - } - } else if !skip_interpreter_checks { - assert_eq!(interpreter.gas().spent(), expected_gas, "interpreter gas mismatch"); - } - let expected_output = if let Some(expected_output) = expected_output { - if !skip_interpreter_checks { - assert_eq!(interpreter_output, expected_output, "interpreter output mismatch"); - } + assert_eq!(interpreter_output, expected_output, "interpreter output mismatch"); Some(expected_output) - } else if skip_interpreter_checks { + } else if halts { None } else { Some(interpreter_output) }; - // Track whether we should skip JIT stack/gas/memory comparisons - let skip_jit_stack = - skip_interpreter_checks && test_case.expected_stack == STACK_WHAT_INTERPRETER_SAYS; - let skip_jit_memory = - skip_interpreter_checks && test_case.expected_memory == MEMORY_WHAT_INTERPRETER_SAYS; - let skip_jit_gas = - skip_interpreter_checks && test_case.expected_gas == GAS_WHAT_INTERPRETER_SAYS; - let actual_return = unsafe { f.call(ecx, stack, stack_len) }; if matches!( @@ -624,27 +566,41 @@ fn run_compiled_test_case_with_context( assert_eq!(actual_return, expected_return, "return value mismatch"); } - let actual_stack = - unsafe { stack.as_slice(*stack_len).iter().map(|x| x.to_u256()).collect::>() }; - - // On EVM halt all available gas is consumed, so resulting stack, memory, and gas do not - // matter. We do less work than the interpreter by bailing out earlier due to sections. - if !actual_return.is_halt() { - if !skip_jit_stack { - assert_eq!(actual_stack, *expected_stack, "stack mismatch"); - } - - if !skip_jit_memory { + if !halts { + let interpreter_stack = interpreter.stack(); + let expected_stack = if expected_stack == STACK_WHAT_INTERPRETER_SAYS { + interpreter_stack.as_slice() + } else { + assert_eq!(interpreter_stack.as_slice(), expected_stack, "interpreter stack mismatch"); + expected_stack + }; + let interpreter_memory = interpreter.memory().as_slice(); + let expected_memory = if expected_memory == MEMORY_WHAT_INTERPRETER_SAYS { + interpreter_memory + } else { assert_eq!( - MemDisplay(ecx.memory().as_slice()), + MemDisplay(interpreter_memory), MemDisplay(expected_memory), - "memory mismatch" + "interpreter memory mismatch" ); - } + expected_memory + }; + let expected_gas = if expected_gas == GAS_WHAT_INTERPRETER_SAYS { + interpreter.gas().spent() + } else { + assert_eq!(interpreter.gas().spent(), expected_gas, "interpreter gas mismatch"); + expected_gas + }; - if !skip_jit_gas { - assert_eq!(ecx.gas.spent(), expected_gas, "gas mismatch"); - } + let actual_stack = + unsafe { stack.as_slice(*stack_len).iter().map(|x| x.to_u256()).collect::>() }; + assert_eq!(actual_stack, *expected_stack, "stack mismatch"); + assert_eq!( + MemDisplay(ecx.memory().as_slice()), + MemDisplay(expected_memory), + "memory mismatch" + ); + assert_eq!(ecx.gas.spent(), expected_gas, "gas mismatch"); } if let Some(expected_output) = expected_output { @@ -656,11 +612,6 @@ fn run_compiled_test_case_with_context( } } -fn instruction_results_match_for_oracle(actual: InstrStop, expected: InstrStop) -> bool { - actual == expected - || matches!((actual, expected), (InstrStop::StackUnderflow, InstrStop::StackOverflow)) -} - #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] struct MemDisplay<'a>(&'a [u8]); impl fmt::Debug for MemDisplay<'_> { From 642166905af2c2e070e11b7e8ede31612cf4e2fd Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:30:52 +0100 Subject: [PATCH 054/159] fix(macros): reject unsupported instruction signatures (#912) The `#[instruction]` macro skipped arguments and return types it did not understand, so a signature like `x: [Word]` consumed no stack inputs and gave the opcode the wrong stack effect with no error. Unsupported argument shapes (`self`, a non-identifier `cx`, a non-slice pattern for `[Word]`) and return types now produce spanned compile errors. The `evm_types` alias of `EvmTypes`, which nothing used, is removed, and the docs no longer promise `-> Result`, which the macro always rejected. --- crates/macros/src/lib.rs | 93 +++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 6f63e7455..a4629164d 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -25,8 +25,8 @@ use syn::{ /// reference per non-`_` pattern. Inputs that overlap output slots are shared reborrows of the /// output slot; other inputs are shared references. Use reference patterns like `[&x]: [Word]` to /// copy. -/// - `-> out`: Automatic stack output. The output name is a mutable `Word` slot. Multiple outputs -/// can be written as `-> Result` for fallible instructions. +/// - `-> out`: Automatic stack output. The output name is a mutable `Word` slot. At most one output +/// is supported. /// - `-> Result`: Fallible instruction with no automatic outputs. The body must evaluate to /// `evm2::interpreter::Result`, so `?` can be used to return an `InstrStop`. /// - `-> Result`: Fallible instruction with automatic outputs. The body gets mutable output @@ -109,7 +109,7 @@ enum InstructionAttr { impl Parse for InstructionAttr { fn parse(input: ParseStream<'_>) -> syn::Result { let ident = input.parse::()?; - if ident == "EvmTypes" || ident == "evm_types" { + if ident == "EvmTypes" { let span = ident.span(); if input.peek(Token![=]) { input.parse::()?; @@ -237,23 +237,50 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } else { quote! { where #evm_types: #evm_types_bound #( + #evm_types_bounds)* } }; - let (_, outputs) = parse_return(sig.output); + let outputs = match parse_return(sig.output) { + Ok(outputs) => outputs, + Err(err) => return err.to_compile_error(), + }; let mut has_cx = false; let mut cx_arg = None; let mut inputs = Vec::new(); + let mut has_stack_inputs = false; for arg in sig.inputs { - let FnArg::Typed(arg) = arg else { continue }; + let FnArg::Typed(arg) = arg else { + return syn::Error::new_spanned(arg, "`self` is not supported in #[instruction]") + .to_compile_error(); + }; if is_infer(&arg.ty) { - let Pat::Ident(PatIdent { ident, .. }) = *arg.pat else { continue }; + let Pat::Ident(PatIdent { ident, .. }) = *arg.pat else { + return syn::Error::new_spanned(arg.pat, "expected an identifier for `cx: _`") + .to_compile_error(); + }; + if has_cx { + return syn::Error::new_spanned(ident, "duplicate `cx: _` argument") + .to_compile_error(); + } has_cx = true; cx_arg = Some(ident); } else if is_word_slice(&arg.ty) { - let Pat::Slice(PatSlice { elems, .. }) = *arg.pat else { continue }; + let Pat::Slice(PatSlice { elems, .. }) = *arg.pat else { + return syn::Error::new_spanned( + arg.pat, + "expected a slice pattern like `[a, b]` for `[Word]` stack inputs", + ) + .to_compile_error(); + }; if let Some(rest) = elems.iter().find(|pat| matches!(pat, Pat::Rest(_))) { return syn::Error::new_spanned(rest, "`..` is not supported in stack inputs") .to_compile_error(); } + if std::mem::replace(&mut has_stack_inputs, true) { + return syn::Error::new_spanned( + elems, + "duplicate `[..]: [Word]` stack inputs argument", + ) + .to_compile_error(); + } inputs.extend(elems); } else { return syn::Error::new_spanned( @@ -264,6 +291,14 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } } + if instruction_attrs.no_stack_preamble && (!inputs.is_empty() || !outputs.is_empty()) { + return syn::Error::new( + ident.span(), + "stack inputs and outputs are not supported with `no_stack_preamble`", + ) + .to_compile_error(); + } + let body = body(input.block.stmts, outputs.is_empty()); let stack_setup = if instruction_attrs.no_stack_preamble { None @@ -353,26 +388,34 @@ fn is_word_slice(ty: &Type) -> bool { path.get_ident().is_some_and(|ident| ident == "Word") } -fn parse_return(output: ReturnType) -> (bool, Vec) { +fn parse_return(output: ReturnType) -> syn::Result> { let ReturnType::Type(_, ty) = output else { - return (false, Vec::new()); + return Ok(Vec::new()); }; - let Type::Path(TypePath { path, .. }) = *ty else { - return (false, Vec::new()); + let unsupported = |ty: &dyn quote::ToTokens| { + syn::Error::new_spanned( + ty, + "unsupported #[instruction] return type; use `-> out`, `-> Result`, or `-> Result`", + ) + }; + let Type::Path(TypePath { path, .. }) = &*ty else { + return Err(unsupported(&ty)); }; let Some(segment) = path.segments.last() else { - return (false, Vec::new()); + return Err(unsupported(&ty)); }; - if segment.ident == "Result" { - let outputs = match &segment.arguments { - PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => { - args.iter().filter_map(generic_ident).collect() + if segment.ident != "Result" { + return Ok(vec![path.get_ident().cloned().ok_or_else(|| unsupported(&ty))?]); + } + match &segment.arguments { + PathArguments::None => Ok(Vec::new()), + PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => { + if let Some(extra) = args.iter().nth(1) { + return Err(syn::Error::new_spanned(extra, "multiple outputs not supported")); } - _ => Vec::new(), - }; - (true, outputs) - } else { - (false, vec![segment.ident.clone()]) + args.iter().map(|arg| generic_ident(arg).ok_or_else(|| unsupported(arg))).collect() + } + PathArguments::Parenthesized(_) => Err(unsupported(&ty)), } } @@ -414,14 +457,6 @@ fn stack_setup(inputs: &[Pat], outputs: &[Ident]) -> TokenStream2 { } fn stack_bindings(inputs: &[Pat], outputs: &[Ident]) -> TokenStream2 { - if outputs.len() > 1 { - return syn::Error::new( - outputs.iter().map(|i| i.span()).reduce(|a, b| a.join(b).unwrap_or(a)).unwrap(), - "multiple outputs not supported", - ) - .into_compile_error(); - } - let count = inputs.len().max(outputs.len()); let mut bindings = Vec::with_capacity(count); for i in 0..count { From 9c81ef4eeb8c1136cf8055b85d57f1696634ac5c Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:01 +0100 Subject: [PATCH 055/159] refactor: keep one authorization list in LazyTxEip7702 (#914) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `LazyTxEip7702` held the authorization list twice: a public `Vec` and a private `Vec` that `Transaction::authorization_list` returns. The constructors filled both, but nothing kept them equal, and the public field could be edited without the private copy. Recovered entries also dropped their signature, so the signed copy could not be rebuilt from the public list. The transaction now keeps one private signed list and an optional parallel cache of recovered authorities, filled by the constructors. `LazyAuthorization` becomes a borrowed view of one entry, either signed (recovered on demand) or signed with a cached authority, so a pre-recovered authority still skips secp256k1 recovery. `LazyTxEip7702::authorizations` yields these views, and `apply_auth_list` takes its items by value so the handler can pass them. `from_cached_recovered_authorizations` now takes one authority per authorization of the transaction and panics on a length mismatch. `from_authorizations`, `as_signed` and `as_recovered` are removed. The handler change is limited to how it reads the list. ## API changes | Item | Before | After | | :--- | :----- | :---- | | `LazyTxEip7702::authorization_list` | `pub Vec` | private `Vec`; read it with `Transaction::authorization_list()` or `LazyTxEip7702::authorizations()` | | `LazyTxEip7702::from_cached_recovered_authorizations` | `(tx: TxEip7702, authorization_list: Vec) -> Self` | `(tx: TxEip7702, authorities: Vec>) -> Self`; `tx` carries the signed list, `None` marks an invalid signature, and a length mismatch panics | | `LazyTxEip7702::from_authorizations` | `(tx: TxEip7702, authorization_list: Vec) -> Self` | removed | | `LazyTxEip7702::authorizations` | none | `(&self) -> impl ExactSizeIterator>` | | `LazyAuthorization` | `enum { Signed(SignedAuthorization), Recovered(RecoveredAuthorization) }` | `enum LazyAuthorization<'a> { Signed(&'a SignedAuthorization), Recovered { authorization: &'a SignedAuthorization, authority: Option
} }`; `as_signed`, `as_recovered` and the `From` impls are removed | | `Eip7702Authorization` impl | `for LazyAuthorization` | `for LazyAuthorization<'_>` | | `apply_auth_list` | `<'a, 'b, T, A: Eip7702Authorization + 'b>(host, chain_id, authorizations: impl IntoIterator, accounting)` | `<'a, T, A: Eip7702Authorization>(host, chain_id, authorizations: impl IntoIterator, accounting)` | `from_signed_authorizations` and `from_recovered_authorizations` keep their signatures. A caller that held the recovered list builds `authorities` with `recovered.iter().map(|a| a.authority()).collect()` and passes the signed list in `tx.authorization_list`; a caller of `apply_auth_list` passes owned items (`[a, b]` or `list.into_iter()`) instead of `&[a, b]`. One entry can no longer be recovered while its neighbour stays signed: the cache covers the whole list or none of it. ## Benchmark Retired user instructions per transaction (`perf stat -e instructions:u`, differential between 200 and 1200 iterations, two runs agree within 0.2%, `profiling` build, AMD EPYC 4585PX), on the merge base `1dc537e6` and on this PR. The driver builds a Prague EIP-7702 transaction with N authorizations to distinct existing authorities, then runs it through `Evm::transact`. "Construct" builds the `LazyTxEip7702` from cached authorities the way an embedder would, or from signed authorizations; "transact" includes that construction, a fresh `Evm` and the execution. Time is not reported because the machine was under heavy load. | Authorizations | Construct, cached | Transact, cached | Construct, signed | Transact, signed | | :-: | :-: | :-: | :-: | :-: | | 1 | 856 → 627 🟢 −27% | 16,789 → 16,600 🟢 −1.1% | 617 → 398 🟢 −36% | 495,465 → 495,282 🟢 −0.04% | | 4 | 969 → 689 🟢 −29% | 35,041 → 34,902 🟢 −0.4% | 686 → 432 🟢 −37% | 1,940,880 → 1,940,732 🟢 −0.01% | | 16 | 2,121 → 1,082 🟢 −49% | 112,561 → 111,387 🟢 −1.0% | 1,294 → 773 🟢 −40% | 7,774,364 → 7,773,351 🟢 −0.01% | Signed transactions are dominated by on-demand secp256k1 recovery, about 480,000 instructions per authorization, which this change does not touch; cached transactions skip it. --- crates/evm2/src/ethereum/eip7702.rs | 17 +- crates/evm2/src/ethereum/lazy_eip7702.rs | 181 +++++++++--------- crates/evm2/src/tests.rs | 44 +++-- crates/evm2/tests/eip7702_recovery_crypto.rs | 8 +- .../tests/it/parity/state_diff_7702.rs | 20 +- .../inspectors/tests/it/parity/vm_bytecode.rs | 17 +- 6 files changed, 154 insertions(+), 133 deletions(-) diff --git a/crates/evm2/src/ethereum/eip7702.rs b/crates/evm2/src/ethereum/eip7702.rs index 3f5019848..53be3ff30 100644 --- a/crates/evm2/src/ethereum/eip7702.rs +++ b/crates/evm2/src/ethereum/eip7702.rs @@ -40,7 +40,8 @@ pub fn prepare_with_hooks>( let caller = req.tx.signer(); let tx = req.tx.inner(); let envelope = req.envelope; - if tx.authorization_list.is_empty() { + let auth_count = tx.authorizations().len(); + if auth_count == 0 { return Err(HandlerError::EmptyAuthorizationList); } let max_fee_per_gas = U256::from(tx.max_fee_per_gas); @@ -65,7 +66,7 @@ pub fn prepare_with_hooks>( access_list_storage_keys, tx.value, ); - intrinsic += eip7702_authorization_gas(req.host.version(), tx.authorization_list.len()); + intrinsic += eip7702_authorization_gas(req.host.version(), auth_count); // EIP-2780 (ethereum/EIPs#11844): the per-auth state-dependent charges are applied at the // runtime gas phase, so no state gas is charged at the intrinsic phase (pre-Amsterdam there is // none either). A transaction that passes the intrinsic check but cannot afford the runtime @@ -136,11 +137,11 @@ pub fn execute_prepared>( let (auth_oog, state_refund, execution_refund) = if req.host.feature(EvmFeatures::EIP2780) { let mut auth_charges = RuntimeAuthCharges::new(req.host.version(), &mut tx_gas, caller, tx.to, tx.value); - let oog = apply_auth_list(req.host, chain_id, &tx.authorization_list, &mut auth_charges)?; + let oog = apply_auth_list(req.host, chain_id, tx.authorizations(), &mut auth_charges)?; (oog, 0, 0) } else { let mut auth_refunds = AuthRefunds::new(req.host.version()); - apply_auth_list(req.host, chain_id, &tx.authorization_list, &mut auth_refunds)?; + apply_auth_list(req.host, chain_id, tx.authorizations(), &mut auth_refunds)?; let AuthRefunds { state_refund, execution_refund, .. } = auth_refunds; tx_gas.set_reservoir(tx_gas.reservoir() + state_refund); (false, state_refund, execution_refund) @@ -459,14 +460,14 @@ impl AuthAccounting for AuthRefunds { /// their delegation is applied. An accounting out-of-gas aborts the list without loading the /// remaining authorities — keeping them out of the EIP-7928 block access list — and returns /// `true`; the caller is responsible for rolling back the partially applied delegations. -pub fn apply_auth_list<'a, 'b, T: EvmTypes, A: Eip7702Authorization + 'b>( +pub fn apply_auth_list<'a, T: EvmTypes, A: Eip7702Authorization>( host: &mut Evm<'a, T>, chain_id: u64, - authorizations: impl IntoIterator, + authorizations: impl IntoIterator, accounting: &mut impl AuthAccounting, ) -> HandlerResult { for authorization in authorizations { - let Some((authority, auth)) = validate_one_auth(host, chain_id, authorization)? else { + let Some((authority, auth)) = validate_one_auth(host, chain_id, &authorization)? else { accounting.rejected(); continue; }; @@ -496,7 +497,7 @@ pub trait Eip7702Authorization { fn authority(&self) -> Option
; } -impl Eip7702Authorization for LazyAuthorization { +impl Eip7702Authorization for LazyAuthorization<'_> { fn chain_id(&self) -> U256 { *Self::chain_id(self) } diff --git a/crates/evm2/src/ethereum/lazy_eip7702.rs b/crates/evm2/src/ethereum/lazy_eip7702.rs index aa366873f..4e0bbb8f8 100644 --- a/crates/evm2/src/ethereum/lazy_eip7702.rs +++ b/crates/evm2/src/ethereum/lazy_eip7702.rs @@ -4,43 +4,49 @@ use alloy_consensus::{Transaction, TxEip7702, TxType}; use alloy_eips::{ Typed2718, eip2930::AccessList, - eip7702::{ - Authorization, RecoveredAuthority, RecoveredAuthorization, SignedAuthorization, - constants::SECP256K1N_HALF, - }, + eip7702::{Authorization, SignedAuthorization, constants::SECP256K1N_HALF}, }; use alloy_primitives::{Address, B256, Bytes, ChainId, TxKind, U256}; -/// EIP-7702 authorization that may already have its authority recovered. -#[derive(Clone, Debug, Eq, PartialEq, Hash)] -pub enum LazyAuthorization { +/// Borrowed EIP-7702 authorization of a [`LazyTxEip7702`], with its authority possibly recovered. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub enum LazyAuthorization<'a> { /// Signed authorization whose authority is recovered on demand. - Signed(SignedAuthorization), - /// Authorization with a cached recovery result. - Recovered(RecoveredAuthorization), + Signed(&'a SignedAuthorization), + /// Signed authorization with a cached recovery result. + Recovered { + /// The signed authorization. + authorization: &'a SignedAuthorization, + /// The cached authority, or `None` if the signature is invalid. + authority: Option
, + }, } -impl LazyAuthorization { - /// Returns the inner unsigned authorization. - pub fn inner(&self) -> &Authorization { - match self { - Self::Signed(authorization) => authorization.inner(), - Self::Recovered(authorization) => authorization, +impl<'a> LazyAuthorization<'a> { + /// Returns the signed authorization. + const fn signed(&self) -> &'a SignedAuthorization { + match *self { + Self::Signed(authorization) | Self::Recovered { authorization, .. } => authorization, } } + /// Returns the inner unsigned authorization. + pub const fn inner(&self) -> &'a Authorization { + self.signed().inner() + } + /// Returns the authorization chain ID. - pub fn chain_id(&self) -> &U256 { + pub const fn chain_id(&self) -> &'a U256 { self.inner().chain_id() } /// Returns the delegated address. - pub fn address(&self) -> &Address { + pub const fn address(&self) -> &'a Address { self.inner().address() } /// Returns the authorization nonce. - pub fn nonce(&self) -> u64 { + pub const fn nonce(&self) -> u64 { self.inner().nonce() } @@ -49,45 +55,17 @@ impl LazyAuthorization { /// Signed authorizations perform recovery on demand. Recovered authorizations return their /// cached result without doing secp256k1 recovery. pub fn authority(&self) -> Option
{ - match self { + match *self { Self::Signed(authorization) => recover_authority(authorization), - Self::Recovered(authorization) => authorization.authority(), - } - } - - /// Returns the signed authorization if this authorization has not been recovered yet. - pub const fn as_signed(&self) -> Option<&SignedAuthorization> { - match self { - Self::Signed(authorization) => Some(authorization), - Self::Recovered(_) => None, + Self::Recovered { authority, .. } => authority, } } - - /// Returns the recovered authorization if this authorization has a cached recovery result. - pub const fn as_recovered(&self) -> Option<&RecoveredAuthorization> { - match self { - Self::Recovered(authorization) => Some(authorization), - Self::Signed(_) => None, - } - } -} - -impl From for LazyAuthorization { - fn from(value: SignedAuthorization) -> Self { - Self::Signed(value) - } -} - -impl From for LazyAuthorization { - fn from(value: RecoveredAuthorization) -> Self { - Self::Recovered(value) - } } /// EIP-7702 transaction used by the executable Ethereum transaction model. /// -/// This preserves the consensus [`TxEip7702`] fields but stores authorization list entries as -/// signed-or-recovered values so callers can supply cached EIP-7702 authority recovery results. +/// This preserves the consensus [`TxEip7702`] fields and optionally caches the recovered authority +/// of each authorization, so callers can supply EIP-7702 authority recovery results. #[derive(Clone, Debug, Default, Eq, PartialEq, Hash)] pub struct LazyTxEip7702 { /// EIP-155 replay protection chain ID. @@ -106,42 +84,51 @@ pub struct LazyTxEip7702 { pub value: U256, /// EIP-2930 access list. pub access_list: AccessList, - /// EIP-7702 authorizations, either signed or with cached recovery results. - pub authorization_list: Vec, - signed_authorization_list: Vec, /// Transaction input calldata. pub input: Bytes, + authorization_list: Vec, + /// Recovered authority of each entry of `authorization_list`, or `None` if not recovered yet. + /// An authority is `None` if its signature is invalid. + authorities: Option>>, } impl LazyTxEip7702 { /// Converts a consensus transaction while keeping signed authorizations unresolved. pub fn from_signed_authorizations(tx: TxEip7702) -> Self { - let authorization_list = tx.authorization_list.iter().cloned().map(Into::into).collect(); - Self::from_authorizations(tx, authorization_list) + Self::new(tx, None) } /// Converts a consensus transaction and eagerly recovers all authorization authorities. /// - /// Invalid authorization signatures are cached as invalid recovered authorizations so execution - /// skips them in the same way as a failed on-demand recovery. + /// Invalid authorization signatures are cached as invalid authorities so execution skips them + /// in the same way as a failed on-demand recovery. pub fn from_recovered_authorizations(tx: TxEip7702) -> Self { - let authorization_list = tx - .authorization_list - .iter() - .map(|authorization| { - let authority = recover_authority(authorization) - .map_or(RecoveredAuthority::Invalid, RecoveredAuthority::Valid); - RecoveredAuthorization::new_unchecked(authorization.inner().clone(), authority) - .into() - }) - .collect(); - Self::from_authorizations(tx, authorization_list) - } - - /// Converts a consensus transaction using an externally supplied authorization list. + let authorities = tx.authorization_list.iter().map(recover_authority).collect(); + Self::new(tx, Some(authorities)) + } + + /// Converts a consensus transaction using externally supplied recovered authorities, one per + /// authorization of `tx`; `None` marks an invalid signature. + /// + /// The caller is responsible for ensuring that each authority matches the signature of its + /// authorization. /// - /// The caller is responsible for ensuring that `authorization_list` corresponds to `tx`. - pub fn from_authorizations(tx: TxEip7702, authorization_list: Vec) -> Self { + /// # Panics + /// + /// Panics if `authorities` and the authorization list of `tx` differ in length. + pub fn from_cached_recovered_authorizations( + tx: TxEip7702, + authorities: Vec>, + ) -> Self { + assert_eq!( + tx.authorization_list.len(), + authorities.len(), + "one recovered authority per authorization" + ); + Self::new(tx, Some(authorities)) + } + + fn new(tx: TxEip7702, authorities: Option>>) -> Self { let TxEip7702 { chain_id, nonce, @@ -151,7 +138,7 @@ impl LazyTxEip7702 { to, value, access_list, - authorization_list: signed_authorization_list, + authorization_list, input, } = tx; Self { @@ -163,20 +150,23 @@ impl LazyTxEip7702 { to, value, access_list, - authorization_list, - signed_authorization_list, input, + authorization_list, + authorities, } } - /// Converts a consensus transaction using externally supplied recovered authorizations. - /// - /// The caller is responsible for ensuring that `authorization_list` corresponds to `tx`. - pub fn from_cached_recovered_authorizations( - tx: TxEip7702, - authorization_list: Vec, - ) -> Self { - Self::from_authorizations(tx, authorization_list.into_iter().map(Into::into).collect()) + /// Returns the authorizations, each with its cached authority if recovered. + pub fn authorizations(&self) -> impl ExactSizeIterator> { + let authorities = self.authorities.as_deref(); + self.authorization_list.iter().enumerate().map( + move |(i, authorization)| match authorities { + Some(authorities) => { + LazyAuthorization::Recovered { authorization, authority: authorities[i] } + } + None => LazyAuthorization::Signed(authorization), + }, + ) } /// Calculates a heuristic for the in-memory size of the transaction. @@ -185,8 +175,8 @@ impl LazyTxEip7702 { size_of::() + self.access_list.size() + self.input.len() - + self.authorization_list.capacity() * size_of::() - + self.signed_authorization_list.capacity() * size_of::() + + self.authorization_list.capacity() * size_of::() + + self.authorities.as_ref().map_or(0, |a| a.capacity() * size_of::>()) } } @@ -272,7 +262,7 @@ impl Transaction for LazyTxEip7702 { } fn authorization_list(&self) -> Option<&[SignedAuthorization]> { - Some(&self.signed_authorization_list) + Some(&self.authorization_list) } } @@ -315,7 +305,7 @@ mod tests { let signed = |y, r, s| SignedAuthorization::new_unchecked(inner.clone(), y, r, s); let valid = signed(y, r, s); - assert_eq!(LazyAuthorization::from(valid.clone()).authority(), Some(expected)); + assert_eq!(LazyAuthorization::Signed(&valid).authority(), Some(expected)); // Without the low-s check, the high-s signature would recover `expected` too. let authorization_list = vec![ valid, @@ -328,14 +318,17 @@ mod tests { let tx = TxEip7702 { authorization_list, ..Default::default() }; let signed = LazyTxEip7702::from_signed_authorizations(tx.clone()); let recovered = LazyTxEip7702::from_recovered_authorizations(tx.clone()); - for (i, authorization) in tx.authorization_list.iter().enumerate() { + let entries = signed.authorizations().zip(recovered.authorizations()); + for (i, (authorization, (signed, recovered))) in + tx.authorization_list.iter().zip(entries).enumerate() + { let want = authorization.recover_authority().ok(); assert_eq!(want.is_some(), i == 0, "case {i}"); - assert_eq!(signed.authorization_list[i].authority(), want, "signed case {i}"); - assert_eq!(recovered.authorization_list[i].authority(), want, "recovered case {i}"); + assert_eq!(signed, LazyAuthorization::Signed(authorization), "signed entry {i}"); + assert_eq!(signed.authority(), want, "signed case {i}"); assert_eq!( - recovered.authorization_list[i], - LazyAuthorization::from(authorization.clone().into_recovered()), + recovered, + LazyAuthorization::Recovered { authorization, authority: want }, "recovered entry {i}" ); } diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 985166b39..4934e0fbd 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -3,7 +3,7 @@ use crate::{ bytecode::Bytecode, env::BlockEnvExt, ethereum::{ - LazyAuthorization, LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, + LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, eip7702::{AuthRefunds, Eip7702Authorization, RuntimeAuthCharges, apply_auth_list}, ethereum_tx_registry, prepare_initial_frame, }, @@ -15,7 +15,7 @@ use crate::{ }; use alloc::{vec, vec::Vec}; use alloy_consensus::{TxEip7702, TxLegacy, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, Log, TxKind, U256, hex}; type TestEvm = Evm<'static, BaseEvmTypes>; @@ -274,11 +274,18 @@ fn eip2780_authorization_charges_no_state_gas_without_eip8037() { let gas_limit = 12_000 + 3_000 + 7_816 + 9_000; let tx = Recovered::new_unchecked( TxEnvelope::Eip7702(LazyTxEip7702::from_cached_recovered_authorizations( - TxEip7702 { chain_id, to: recipient, gas_limit, ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::ZERO, address: delegate, nonce: 0 }, - RecoveredAuthority::Valid(authority), - )], + TxEip7702 { + chain_id, + to: recipient, + gas_limit, + authorization_list: vec![unsigned(Authorization { + chain_id: U256::ZERO, + address: delegate, + nonce: 0, + })], + ..Default::default() + }, + vec![Some(authority)], )), caller, ); @@ -291,6 +298,11 @@ fn eip2780_authorization_charges_no_state_gas_without_eip8037() { assert_eq!(authority.code_hash, Bytecode::new_eip7702(delegate).hash_slow()); } +/// Wraps an authorization with a placeholder signature, for use with a cached authority. +fn unsigned(authorization: Authorization) -> SignedAuthorization { + SignedAuthorization::new_unchecked(authorization, 0, U256::ZERO, U256::ZERO) +} + fn absent_authority_evm(spec: SpecId, touched: Address) -> TestEvm { let mut evm = TestEvm::new( spec, @@ -303,12 +315,12 @@ fn absent_authority_evm(spec: SpecId, touched: Address) -> TestEvm { evm } -fn auth_for(authority: Address) -> LazyAuthorization { - RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::ZERO, address: Address::with_last_byte(0xdd), nonce: 0 }, - RecoveredAuthority::Valid(authority), - ) - .into() +fn auth_for(authority: Address) -> CustomAuthorization { + CustomAuthorization { + nonce: 0, + address: Address::with_last_byte(0xdd), + authority: Some(authority), + } } #[test] @@ -318,7 +330,7 @@ fn apply_auth_list_refunds_touched_absent_authority() { let mut evm = absent_authority_evm(SpecId::PRAGUE, touched); let mut refunds = AuthRefunds::new(evm.version()); - apply_auth_list(&mut evm, 1, &[auth_for(touched), auth_for(untouched)], &mut refunds).unwrap(); + apply_auth_list(&mut evm, 1, [auth_for(touched), auth_for(untouched)], &mut refunds).unwrap(); // An authority is refunded unless it is absent and untouched. let per_auth = evm.version().gas_params.get(GasId::TxEip7702AuthRefund); @@ -338,7 +350,7 @@ fn eip2780_touched_absent_authority_pays_no_new_account_state_gas() { Address::with_last_byte(0xc0), U256::ZERO, ); - assert!(!apply_auth_list(&mut evm, 1, &[auth_for(authority)], &mut charges).unwrap()); + assert!(!apply_auth_list(&mut evm, 1, [auth_for(authority)], &mut charges).unwrap()); (gas.state_gas_spent(), evm.version().gas_params.new_account_state_gas()) }; @@ -634,7 +646,7 @@ fn apply_auth_list_accepts_custom_filtered_authorizations() { let oog = apply_auth_list( &mut evm, 1, - list.iter().filter(|auth| auth.authority != Some(skipped)), + list.into_iter().filter(|auth| auth.authority != Some(skipped)), &mut refunds, ) .unwrap(); diff --git a/crates/evm2/tests/eip7702_recovery_crypto.rs b/crates/evm2/tests/eip7702_recovery_crypto.rs index 066820f7e..9e19d5ec6 100644 --- a/crates/evm2/tests/eip7702_recovery_crypto.rs +++ b/crates/evm2/tests/eip7702_recovery_crypto.rs @@ -38,13 +38,13 @@ fn recovery_ignores_crypto_provider() { let authorization = SignedAuthorization::new_unchecked(inner, recid.to_byte(), r, s); let tx = TxEip7702 { authorization_list: vec![authorization.clone()], ..Default::default() }; - assert_eq!(LazyAuthorization::from(authorization.clone()).authority(), Some(signer)); + assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(signer)); let recovered = LazyTxEip7702::from_recovered_authorizations(tx.clone()); - assert_eq!(recovered.authorization_list[0].authority(), Some(signer)); + assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(signer)); assert!(install_crypto(BogusCrypto), "recovery initialized the crypto provider"); - assert_eq!(LazyAuthorization::from(authorization).authority(), Some(signer)); + assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(signer)); let recovered = LazyTxEip7702::from_recovered_authorizations(tx); - assert_eq!(recovered.authorization_list[0].authority(), Some(signer)); + assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(signer)); } diff --git a/crates/inspectors/tests/it/parity/state_diff_7702.rs b/crates/inspectors/tests/it/parity/state_diff_7702.rs index 7be439cfd..2638670c2 100644 --- a/crates/inspectors/tests/it/parity/state_diff_7702.rs +++ b/crates/inspectors/tests/it/parity/state_diff_7702.rs @@ -1,5 +1,5 @@ use alloy_consensus::{TxEip7702, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, U64, U256, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, TraceType}; use evm2::{ @@ -46,11 +46,19 @@ fn authorization_code_changes() { evm.set_inspector(&mut inspector); let tx = Recovered::new_unchecked( TxEnvelope::Eip7702(LazyTxEip7702::from_cached_recovered_authorizations( - TxEip7702 { chain_id: 1, to: target, gas_limit: 100_000, ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::from(1), address: delegate, nonce: 1 }, - RecoveredAuthority::Valid(authority), - )], + TxEip7702 { + chain_id: 1, + to: target, + gas_limit: 100_000, + authorization_list: vec![SignedAuthorization::new_unchecked( + Authorization { chain_id: U256::from(1), address: delegate, nonce: 1 }, + 0, + U256::ZERO, + U256::ZERO, + )], + ..Default::default() + }, + vec![Some(authority)], )), Address::ZERO, ); diff --git a/crates/inspectors/tests/it/parity/vm_bytecode.rs b/crates/inspectors/tests/it/parity/vm_bytecode.rs index af3bedf95..ac3191d21 100644 --- a/crates/inspectors/tests/it/parity/vm_bytecode.rs +++ b/crates/inspectors/tests/it/parity/vm_bytecode.rs @@ -1,6 +1,6 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; use alloy_consensus::{TxEip7702, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, Bytes, U256, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{TraceResults, TraceType}; use evm2::{ @@ -111,12 +111,19 @@ fn authorization_resolves_executed_bytecode() { chain_id: 1, to: authority, gas_limit: 100_000, + authorization_list: vec![SignedAuthorization::new_unchecked( + Authorization { + chain_id: U256::from(1), + address: delegate, + nonce: 0, + }, + 0, + U256::ZERO, + U256::ZERO, + )], ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::from(1), address: delegate, nonce: 0 }, - RecoveredAuthority::Valid(authority), - )], + vec![Some(authority)], )), Address::ZERO, ), From dd617a7c77e0c57346f09a5e293ee2eb01b84a3b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:11 +0100 Subject: [PATCH 056/159] fix(inspectors): pair struct log children through node ordering (#916) The struct logger walked a node's steps and children by pairing each call-like step with the next entry of `children` by position. A call-like step with no attached child shifted every later pairing. This happens when steps are recorded together with `exclude_precompile_calls`: a zero-value call to a precompile is recorded with `PushTraceKind::PushOnly`, so it is not attached to its parent, and its CALL step took the next contract call's child. The contract's struct logs then appeared after the precompile's CALL step instead of after its own. geth's struct logger appends a log from `OnOpcode` ([`eth/tracers/logger/logger.go`](https://github.com/ethereum/go-ethereum/blob/a0cba163/eth/tracers/logger/logger.go#L283)) as the interpreter runs each opcode, so logs come in execution order and a child's steps directly follow the call step that entered it. A precompile runs no opcodes and adds none. `steps_with_children` now finds the child through the node's `ordering`, where `TraceMemberOrder::Call` is pushed right after the step that made the call, as the parity vmTrace builder already does. The new test records steps with `exclude_precompile_calls`, calls the identity precompile with no value and then a contract that runs a few opcodes, and expects the contract's steps right after the second CALL. It fails on main. The fixture of the existing `opcode_trace_resumes_parents_after_nested_and_empty_calls` test builds nodes by hand, so it now fills `ordering` the way the inspector does. #857 attaches every call to its parent, after which this case no longer occurs, but the ordering-based walk stays correct. The geth parity harness from #859 reports the same output with and without this change, so no known diff in it goes away. --- .../src/tracing/builder/geth/steps_tests.rs | 14 ++++- crates/inspectors/src/tracing/types.rs | 23 +++++--- crates/inspectors/tests/it/geth.rs | 52 +++++++++++++++++++ 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index ad1e9aec8..af548c580 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -1,6 +1,6 @@ use super::*; use crate::tracing::types::{ - CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, + CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, TraceMemberOrder, }; use alloc::string::String; use evm2::{ @@ -206,6 +206,18 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { } }) .collect(); + // Each call opcode enters the next child, as the inspector records them. + for node in &mut nodes { + let mut children = 0..node.children.len(); + for (step_idx, step) in node.trace.steps.iter().enumerate() { + node.ordering.push(TraceMemberOrder::Step(step_idx)); + if step.is_call_like_op() + && let Some(child) = children.next() + { + node.ordering.push(TraceMemberOrder::Call(child)); + } + } + } nodes[0].trace.steps.last_mut().unwrap().status = Some(InstrStop::OutOfGas); // The SLOAD reads the value its delegatecall child wrote. nodes[2].trace.steps[2].storage_slot = Some(Box::new((U256::ZERO, U256::from(30)))); diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index afabe082e..b46789b7e 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -279,14 +279,23 @@ impl CallTraceNode { if self.trace.kind.is_delegate() { self.trace.caller } else { self.trace.address } } - /// Iterates over steps in execution order, matching call opcodes to recorded children. + /// Iterates over steps in execution order, pairing each step with the child it called. + /// + /// A child is recorded right after the step that called it, so the node's `ordering` locates + /// it. A call opcode without a following child either failed before entering a frame, e.g. due + /// to out of gas (), or called an excluded + /// precompile that was not attached to this node. pub(crate) fn steps_with_children(&self) -> impl Iterator> { - let mut children = self.children.iter(); - self.trace.steps.iter().map(move |step| { - // A call opcode can fail before a child is recorded, e.g. due to out of gas: - // . - let call_child_id = - if step.is_call_like_op() { children.next().copied() } else { None }; + let mut ordering = self.ordering.iter().peekable(); + self.trace.steps.iter().enumerate().map(move |(step_idx, step)| { + ordering + .find(|member| matches!(member, TraceMemberOrder::Step(idx) if *idx == step_idx)); + let call_child_id = match ordering.next_if(|member| { + step.is_call_like_op() && matches!(member, TraceMemberOrder::Call(_)) + }) { + Some(TraceMemberOrder::Call(child)) => self.children.get(*child).copied(), + _ => None, + }; CallTraceStepStackItem { trace_node: self, step, call_child_id } }) } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 6fd477f2f..5185aca57 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -1739,6 +1739,58 @@ fn test_geth_opcode_limit_end_to_end() { } } +/// geth emits a child's steps right after the call step that entered it. A zero-value call to an +/// excluded precompile attaches no child, so it must not take the next call's child. +#[test] +fn test_geth_struct_logs_follow_calls_with_excluded_precompiles() { + let account = address!("1000000000000000000000000000000000000001"); + let child = address!("0000000000000000000000000000000000001234"); + let call = |target: [u8; 2]| { + [ + vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0], + vec![op::PUSH2, target[0], target[1], op::PUSH2, 0xff, 0xff, op::CALL, op::POP], + ] + .concat() + }; + // Call the identity precompile, then the child, which runs a few opcodes. + let code = [call([0, 4]), call([0x12, 0x34]), vec![op::STOP]].concat(); + let child_code = vec![op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP]; + + let context = + Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + for (addr, code) in [(account, code), (child, child_code)] { + db.insert_account_info( + &addr, + AccountInfo { + code: Some(Bytecode::new_raw(code.into())), + ..Default::default() + }, + ); + } + }); + let config = TracingInspectorConfig::default_geth().set_exclude_precompile_calls(true); + let mut inspector = TracingInspector::new(config); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TxKind::Call(account), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = inspector.geth_builder().geth_traces(0, Bytes::new(), Default::default()); + let ops: Vec<_> = frame.struct_logs.iter().map(|log| (&*log.op, log.depth)).collect(); + let mut expected = vec![("PUSH1", 1); 5]; + expected.extend([("PUSH2", 1), ("PUSH2", 1), ("CALL", 1), ("POP", 1)]); + expected.extend(vec![("PUSH1", 1); 5]); + expected.extend([("PUSH2", 1), ("PUSH2", 1), ("CALL", 1)]); + expected.extend([("PUSH1", 2), ("PUSH1", 2), ("ADD", 2), ("STOP", 2)]); + expected.extend([("POP", 1), ("STOP", 1)]); + assert_eq!(ops, expected); +} + /// Returns the bytes of the JSON geth writes for a struct log, which leaves out a zero refund and /// empty return data, memory and storage. fn geth_json_len(log: &serde_json::Value) -> usize { From 34545150c36e4cda37bc4dd70c5b7fa4a72b38cf Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:25 +0100 Subject: [PATCH 057/159] fix(jit): report host instructions that break compiled code assumptions (#919) When a host-overridden instruction run by compiled code changes the opcode's fixed stack effect, shrinks memory, or moves the program counter, compiled code cannot continue. The `host_instruction` builtin returned a fatal stop without recording an error, so the run failed with "interpreter returned a fatal stop without an error". The builtin now records an execution error through the interpreter's `fail` before it returns, naming the violation, the opcode and the pc: the stack length left against the length the opcode's fixed inputs and outputs imply, or the memory size before and after. Memory shrinking is only known at run time, so that check stays in the builtin. An override that moves the program counter was already detected, in `Interpreter::run_external_instruction`, but it returned the same bare fatal stop. It now records an error naming the opcode and pc as well. The test that pinned the bare stop now asserts all three messages through `Host::execute_message`. --- crates/evm2/src/interpreter/runtime.rs | 31 ++++++++-- crates/jit/builtins/src/lib.rs | 29 ++++++++- crates/jit/runtime/src/evm2_evm.rs | 81 +++++++++++++++++++++++--- 3 files changed, 126 insertions(+), 15 deletions(-) diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 88bc91664..c62c57188 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -6,11 +6,11 @@ use crate::{ EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, Version, bytecode::Bytecode, evm::inspector::Inspector, - interpreter::dispatch, + interpreter::{dispatch, opcode::OpCode}, trustme, version::{EvmFeatures, GasParams}, }; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, format, vec::Vec}; use alloy_primitives::{Address, B256, Bytes}; use core::{fmt, hint::cold_path, ops::Range, ptr::NonNull}; use derive_where::derive_where; @@ -474,7 +474,8 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { // SAFETY: `prepare_run` installed the host, which outlives the run. let config = unsafe { host.as_ref() }.execution_config(); // SAFETY: `pc` is in bounds. - let mut pc = Pc::new(unsafe { bytecode.as_ptr().add(pc) }); + let offset = pc; + let mut pc = Pc::new(unsafe { bytecode.as_ptr().add(offset) }); let instr = config.opcode_config().instruction(pc.op()).instr; self.stack_len = stack_len; let this: *mut Self = self; @@ -490,11 +491,33 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { instr(&mut pc, stack.reborrow(), state)?; // Compiled code continues at the next instruction. if pc.as_ptr() != start { - return Err(InstrStop::FatalExternalError); + return Err(self.fail_host_instruction( + offset, + format_args!("moved the program counter, which compiled code cannot follow"), + )); } Ok(self.stack_len) } + /// Records a fatal error for the host instruction at `pc` that compiled code cannot continue + /// after. + /// + /// # Panics + /// + /// Panics if `pc` is out of bounds. + #[doc(hidden)] + pub fn fail_host_instruction(&mut self, pc: usize, detail: fmt::Arguments<'_>) -> InstrStop { + let opcode = OpCode::new_or_unknown(self.original_bytecode_slice()[pc]); + self.fail(ExecutionError::Fatal( + format!( + "host instruction {} (0x{:02x}) at pc {pc} {detail}", + opcode.as_str(), + opcode.get() + ) + .into(), + )) + } + #[inline(never)] fn run_inner( &mut self, diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 450e30907..4fcc2a367 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -519,9 +519,32 @@ pub unsafe fn __revmc_builtin_host_instruction( ecx.return_data_len = ecx.interpreter().return_data().len(); ecx.refresh_memory_cache(); let new_len = result?; - // Compiled code relies on memory never shrinking, and on the opcode's stack effect. - if new_len != len - inputs + outputs || ecx.memory().len() < memory_len { - return Err(InstrStop::FatalExternalError.into()); + // Compiled code relies on the opcode's stack effect, and on memory never shrinking. + let expected_len = len - inputs + outputs; + if new_len != expected_len { + return Err(ecx + .interpreter_mut() + .fail_host_instruction( + pc, + format_args!( + "left {new_len} stack items, but compiled code expects {expected_len} from \ + the opcode's fixed inputs and outputs" + ), + ) + .into()); + } + let new_memory_len = ecx.memory().len(); + if new_memory_len < memory_len { + return Err(ecx + .interpreter_mut() + .fail_host_instruction( + pc, + format_args!( + "shrank memory from {memory_len} to {new_memory_len} bytes, but compiled \ + code expects memory never to shrink" + ), + ) + .into()); } Ok(()) } diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 515b7307c..72115b302 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -860,12 +860,13 @@ mod custom_host_tests { use crate::runtime::{HostConfig, RuntimeConfig}; use alloy_primitives::{Address, Bytes, U256}; use evm2::{ - BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, OpcodeConfig, SpecId, + BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, ExecutionError, + OpcodeConfig, SpecId, bytecode::Bytecode, config::BaseEvmConfigSelector, env::BlockEnvExt, evm::{EmptyDB, precompile::NoPrecompiles}, - interpreter::{MessageExt, Word, instructions::sstore_with, op}, + interpreter::{MessageExt, MessageResult, Word, instructions::sstore_with, op}, registry::TxRegistry, }; use evm2_macros::instruction; @@ -1015,6 +1016,18 @@ mod custom_host_tests { Ok(()) } + #[instruction(EvmTypes = TableTypes, no_stack_preamble)] + fn pushing_blockhash(_cx: _) -> Result { + // Keeps its operand, which the opcode's fixed inputs and outputs consume. + stack.push(Word::ZERO) + } + + #[instruction(EvmTypes = TableTypes, no_stack_preamble)] + fn memory_clearing_selfbalance(cx: _) -> Result { + cx.state.memory().clear(); + stack.push(Word::ZERO) + } + #[instruction(EvmTypes = TableTypes)] fn added(cx: _) -> out { *out = Word::from(0xdead_u64); @@ -1039,6 +1052,10 @@ mod custom_host_tests { config.set_instruction::(op::BALANCE, balance_gas); let extcodesize_gas = config.static_gas(op::EXTCODESIZE); config.set_instruction::(op::EXTCODESIZE, extcodesize_gas); + let blockhash_gas = config.static_gas(op::BLOCKHASH); + config.set_instruction::(op::BLOCKHASH, blockhash_gas); + let selfbalance_gas = config.static_gas(op::SELFBALANCE); + config.set_instruction::(op::SELFBALANCE, selfbalance_gas); config.set_instruction::(ADDED_OPCODE, 2); config.set_instruction::(op::DUPN, 2); config @@ -1108,6 +1125,34 @@ mod custom_host_tests { (compiled_stop, compiled, stop, interpreted) } + /// Runs `code` as a message through a host that uses the compiled runner. + fn run_compiled_message( + host_config: HostConfig, + code: &'static [u8], + ) -> Result, ExecutionError> { + let backend = JitBackend::new(RuntimeConfig { + blocking: true, + single_error: false, + host: host_config, + ..Default::default() + }) + .unwrap(); + let mut host = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + EmptyDB::default(), + NoPrecompiles::default(), + ); + host.set_interpreter_runner(JitInterpreterRunner::new(backend)); + let mut message = MessageExt { + gas_limit: 100_000, + code: Bytecode::new_legacy(Bytes::from_static(code)), + ..Default::default() + }; + Host::execute_message(&mut host, &mut message) + } + fn table_host() -> HostConfig { let config = TableSelector::execution_config(SpecId::OSAKA); HostConfig::for_config::(&config) @@ -1215,12 +1260,32 @@ mod custom_host_tests { } #[test] - fn host_instruction_that_moves_the_program_counter_halts() { - // The interpreter skips the `POP` after the override, which compiled code cannot do. - let code = &[op::PUSH0, op::TLOAD, op::POP, op::STOP][..]; - let (compiled_stop, _, stop, _) = run_both(table_host(), code); - assert_eq!(stop, InstrStop::Stop); - assert_eq!(compiled_stop, Some(InstrStop::FatalExternalError)); + fn host_instruction_that_breaks_compiled_code_assumptions_fails() { + for (code, error) in [ + ( + // The interpreter skips the `POP` after the override, which compiled code cannot + // do. + &[op::PUSH0, op::TLOAD, op::POP, op::STOP][..], + "host instruction TLOAD (0x5c) at pc 1 moved the program counter, which compiled \ + code cannot follow", + ), + ( + &[op::PUSH0, op::BLOCKHASH, op::STOP][..], + "host instruction BLOCKHASH (0x40) at pc 1 left 2 stack items, but compiled code \ + expects 1 from the opcode's fixed inputs and outputs", + ), + ( + &[op::PUSH0, op::PUSH0, op::MSTORE, op::SELFBALANCE, op::STOP][..], + "host instruction SELFBALANCE (0x47) at pc 3 shrank memory from 32 to 0 bytes, \ + but compiled code expects memory never to shrink", + ), + ] { + let result = run_compiled_message(table_host(), code); + let Err(ExecutionError::Fatal(fatal)) = result else { + panic!("expected a fatal error, got {:?} for {code:?}", result.map(|r| r.stop)); + }; + assert_eq!(fatal.to_string(), error); + } } #[test] From d69e1d23c67e98d5a1de1294e0c744278b6cf444 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:35 +0100 Subject: [PATCH 058/159] fix(jit): count interpreter fallbacks and fail EEST JIT runs on them (#920) The JIT left frames to the interpreter without saying so, and the EEST JIT runs could not tell. This PR counts those fallbacks in a new `interpreter_fallbacks` runtime stat, makes `check_compilations` in the EEST harness fail when it or `compilations_failed` is nonzero, and corrects the docs. In blocking mode, every lookup that does not return a compiled function now counts: a compilation that fails or times out under `sync_compile_timeout`, a full pending-job cap, a saturated worker pool, a program evicted before use, and empty bytecode. A failed compilation shows up in both counters, since `compilations_failed` counts compilations and `interpreter_fallbacks` counts frames, including later frames of the same failed code. Blocking mode is for tests and debugging, so it also ignores `jit_max_bytecode_len` and compiles every non-empty frame; EIP-3860 initcode no longer runs on the interpreter there. The `blocking` doc no longer claims it never falls back. A rejected host is counted too. When `supports_host` rejects a config because the host type, gas schedule, or opcode table differs, an enabled backend increments the counter and logs the reason at warn level, instead of the `Evm` quietly never calling the runner. A rejected host counts once per support check, not once per frame, because the `Evm` then stops calling the runner. Frames whose gas exceeds `i64::MAX` count as well. Keying compiled code by gas schedule, so that one backend compiles for several schedules, is out of scope; a host with a different schedule is rejected, not compiled for. The opcode-table verdict cache is gone. It keyed two single-slot atomics by a tagged pointer, which breaks with 57-bit address spaces or pointer tagging and thrashes when two configs alternate. Support is checked once per runner or config change, so the verdict is computed directly, and `run_interpreter` documents that it checks on every call. Against the full devnet corpus (`EVM2_COMPILED_EEST_SUBSET=all`), `statetests::jit` and `blockchain_tests::jit` pass with zero fallbacks, as do the AOT suites. With the pending-job cap forced to zero, the state suite fails with "0 compilation(s) failed and 113 frame(s) fell back to the interpreter". --- crates/eest/src/execution.rs | 14 ++-- crates/jit/runtime/src/evm2_evm.rs | 62 +++++++++++++++- crates/jit/runtime/src/runtime/config.rs | 13 +++- crates/jit/runtime/src/runtime/mod.rs | 93 +++++++++++++++--------- crates/jit/runtime/src/runtime/stats.rs | 17 +++++ crates/jit/runtime/src/runtime/tests.rs | 55 ++++++++++++++ 6 files changed, 208 insertions(+), 46 deletions(-) diff --git a/crates/eest/src/execution.rs b/crates/eest/src/execution.rs index d418eebde..4941bb772 100644 --- a/crates/eest/src/execution.rs +++ b/crates/eest/src/execution.rs @@ -87,17 +87,19 @@ impl ExecutionResources { } } - /// Fails if a compilation failed, since the runtime then silently runs the interpreter and - /// the test would pass without exercising compiled code. + /// Fails if a compilation failed or a frame fell back to the interpreter, since the test + /// would then pass without exercising compiled code. #[cfg(feature = "jit")] pub(crate) fn check_compilations(&self) -> Result<(), ExecutionResourceError> { - let failed = self.backend.as_ref().map_or(0, |backend| backend.stats().compilations_failed); - if failed == 0 { + let Some(backend) = &self.backend else { return Ok(()) }; + let stats = backend.stats(); + let (failed, fallbacks) = (stats.compilations_failed, stats.interpreter_fallbacks); + if failed == 0 && fallbacks == 0 { return Ok(()); } Err(ExecutionResourceError(format!( - "{failed} compilation(s) failed in this run and fell back to the interpreter; \ - rerun with RUST_LOG=warn for the cause" + "{failed} compilation(s) failed and {fallbacks} frame(s) fell back to the interpreter \ + in this run; rerun with RUST_LOG=warn for the cause" ))) } diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 72115b302..bb8ea2935 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -52,13 +52,21 @@ impl InterpreterRunner for JitInterpreterRunner { /// /// Returns `None` when no compiled program is available, leaving the caller to /// run the same frame through the evm2 interpreter. +/// +/// Checks on every call that the backend supports the host's gas schedule and opcode table, and +/// counts a rejected host as a fallback each time without logging it. +/// [`InterpreterRunner::supports`] does that check once per runner or config change instead. #[inline] pub fn run_interpreter<'frame, 'host, T: EvmTypesHost>( backend: &JitBackend, interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, ) -> Option { - if !backend.enabled() || !backend.supports_host::(host.execution_config()) { + if !backend.enabled() { + return None; + } + if backend.host_mismatch::(host.execution_config()).is_some() { + backend.count_interpreter_fallback(); return None; } run_supported(backend, interpreter, host) @@ -73,7 +81,11 @@ fn run_supported<'frame, 'host, T: EvmTypesHost>( ) -> Option { // Disabled runners must not hash code merely to discover that lookup is unavailable. // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if !backend.enabled() || interpreter.gas().remaining() > i64::MAX as u64 { + if !backend.enabled() { + return None; + } + if interpreter.gas().remaining() > i64::MAX as u64 { + backend.count_interpreter_fallback(); return None; } let code_hash = interpreter.original_bytecode_hash(); @@ -308,7 +320,49 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); - assert_eq!(run_interpreter(&blocking_backend(), &mut interpreter, &mut host), None); + let backend = blocking_backend(); + assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), None); + assert_eq!(backend.stats().interpreter_fallbacks, 1); + } + + #[test] + #[cfg(feature = "llvm")] + fn rejected_gas_schedule_counts_fallback() { + let spec = SpecId::CANCUN; + let mut version = *Version::base(spec); + version.gas_params.set(GasId::SstoreStatic, 800); + let host = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnv::::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + let supports = |backend: &JitBackend| { + InterpreterRunner::::supports( + &JitInterpreterRunner::new(backend.clone()), + host.execution_config(), + ) + }; + + let base = blocking_backend(); + assert!(!supports(&base)); + assert_eq!(base.stats().interpreter_fallbacks, 1); + + let matching = JitBackend::new(RuntimeConfig { + enabled: true, + blocking: true, + gas_params: Some(version.gas_params), + ..RuntimeConfig::default() + }) + .unwrap(); + assert!(supports(&matching)); + assert_eq!(matching.stats().interpreter_fallbacks, 0); + + let disabled = JitBackend::disabled(); + assert!(!supports(&disabled)); + assert_eq!(disabled.stats().interpreter_fallbacks, 0); } #[test] @@ -969,6 +1023,8 @@ mod custom_host_tests { ); assert_eq!(fresh.run(&mut host).unwrap(), InstrStop::Return); assert_eq!(fresh.output(), expected_output); + assert_eq!(backend.stats().interpreter_fallbacks, 0); + assert_eq!(wrong.stats().interpreter_fallbacks, 2); } const SSTORE_SURCHARGE: u64 = 1_000; diff --git a/crates/jit/runtime/src/runtime/config.rs b/crates/jit/runtime/src/runtime/config.rs index 7826c5d97..033b59596 100644 --- a/crates/jit/runtime/src/runtime/config.rs +++ b/crates/jit/runtime/src/runtime/config.rs @@ -119,14 +119,19 @@ pub struct RuntimeConfig { /// Defaults to `None`. pub jit_helper_path: Option, - /// Blocking mode: every lookup synchronously JIT-compiles on miss and never - /// falls back to the interpreter. + /// Blocking mode: every lookup synchronously JIT-compiles on miss. /// /// When `true`, [`lookup()`](super::JitBackend::lookup) behaves like /// [`lookup_blocking()`](super::JitBackend::lookup_blocking): if the /// compiled function is not already resident, the calling thread blocks - /// until JIT compilation completes. This implies `enabled = true` and - /// `jit_hot_threshold = 0`. + /// until JIT compilation completes. This implies `enabled = true`, + /// `jit_hot_threshold = 0`, and `jit_max_bytecode_len = 0` (no size limit), so every + /// non-empty frame is compiled. + /// + /// A frame still runs on the interpreter when compilation fails, times out + /// (`sync_compile_timeout`), is dropped because the pending-job cap or the worker pool is + /// full, or the program is evicted before use. Each such frame is counted in + /// [`interpreter_fallbacks`](super::RuntimeStatsSnapshot::interpreter_fallbacks). /// /// Intended for debugging and testing only — not for production use. /// diff --git a/crates/jit/runtime/src/runtime/mod.rs b/crates/jit/runtime/src/runtime/mod.rs index ab0f5c41c..7e40c4365 100644 --- a/crates/jit/runtime/src/runtime/mod.rs +++ b/crates/jit/runtime/src/runtime/mod.rs @@ -105,13 +105,9 @@ pub(crate) struct BackendInner { enabled: AtomicBool, /// Whether the backend thread has been spawned and is draining the command channel. started: AtomicBool, - /// Blocking mode: every lookup synchronously compiles and never falls back. + /// Blocking mode: every lookup synchronously compiles and counts each fallback. blocking: bool, host: config::HostConfig, - /// Key of the last opcode config and spec that matched the host's opcode table. - verified_opcodes: std::sync::atomic::AtomicU64, - /// Key of the last opcode config and spec that did not match the host's opcode table. - rejected_opcodes: std::sync::atomic::AtomicU64, gas_params: Option, /// Tuning knobs (Copy). Cached for hot-path eligibility checks. tuning: crate::runtime::config::RuntimeTuning, @@ -177,6 +173,7 @@ impl JitBackend { if config.blocking { config.enabled = true; config.tuning.jit_hot_threshold = 0; + config.tuning.jit_max_bytecode_len = 0; } #[cfg(not(unix))] if config.jit_mode == JitMode::OutOfProcess { @@ -209,8 +206,6 @@ impl JitBackend { started: AtomicBool::new(false), blocking: config.blocking, host: config.host.clone(), - verified_opcodes: std::sync::atomic::AtomicU64::new(0), - rejected_opcodes: std::sync::atomic::AtomicU64::new(0), gas_params: config.gas_params, tx, thread: std::sync::Mutex::new(None), @@ -223,6 +218,11 @@ impl JitBackend { Ok(this) } + /// Counts a frame that ran on the interpreter because the runtime could not run it. + pub(crate) fn count_interpreter_fallback(&self) { + self.inner.shared.stats.interpreter_fallbacks.fetch_add(1, Ordering::Relaxed); + } + /// Checks that generated code uses the field layout of the interpreter type `T`. #[inline] pub(crate) fn supports_host_type(&self) -> bool { @@ -230,44 +230,61 @@ impl JitBackend { } /// Checks that generated code matches the interpreter type, gas schedule, and opcode table. + /// + /// An enabled backend counts a rejection as an interpreter fallback and logs the reason, + /// because the caller then runs every frame on the interpreter. Compiled code is not keyed + /// by gas schedule, so a host whose schedule differs from the backend's is never compiled + /// for. pub(crate) fn supports_host( &self, config: &evm2::ExecutionConfig, ) -> bool { - self.supports_host_type::() - && config.version().gas_params - == *self - .inner - .gas_params - .as_ref() - .unwrap_or(&evm2::Version::base(config.base_spec_id()).gas_params) - && self.opcodes_match(config) + let Some(reason) = self.host_mismatch(config) else { return true }; + if self.enabled() { + self.count_interpreter_fallback(); + warn!( + spec_id = ?config.base_spec_id(), + reason, + "host unsupported, running the interpreter", + ); + } + false + } + + /// Returns why generated code does not match `config`, or `None` if it does. + pub(crate) fn host_mismatch( + &self, + config: &evm2::ExecutionConfig, + ) -> Option<&'static str> { + if !self.supports_host_type::() { + return Some("host type differs from the one the backend was built for"); + } + let gas_params = self + .inner + .gas_params + .as_ref() + .unwrap_or(&evm2::Version::base(config.base_spec_id()).gas_params); + if config.version().gas_params != *gas_params { + return Some("gas schedule differs from the one the backend compiles with"); + } + if !self.opcodes_match(config) { + return Some("opcode table differs from the one the backend compiles with"); + } + None } /// Checks the active opcode config against the host's opcode table. fn opcodes_match(&self, config: &evm2::ExecutionConfig) -> bool { - let active = config.opcode_config(); - // The verdict depends on the base spec too. Addresses leave the top byte free. - let address = - active as *const evm2::OpcodeConfig as u64 | (config.base_spec_id() as u64) << 56; - if self.inner.verified_opcodes.load(Ordering::Relaxed) == address { - return true; - } - if self.inner.rejected_opcodes.load(Ordering::Relaxed) == address { - return false; - } - let matches = match &self.inner.host.opcodes { + match &self.inner.host.opcodes { None => config.has_base_opcode_table(), Some(opcodes) => { // SAFETY: The caller checked that the host type is `T`, so `opcodes.config` is a // `&'static OpcodeConfig`. - active.same_table(unsafe { &*(opcodes.config as *const evm2::OpcodeConfig) }) + config + .opcode_config() + .same_table(unsafe { &*(opcodes.config as *const evm2::OpcodeConfig) }) } - }; - let cache = - if matches { &self.inner.verified_opcodes } else { &self.inner.rejected_opcodes }; - cache.store(address, Ordering::Relaxed); - matches + } } /// Looks up a compiled function for the given request. @@ -334,8 +351,18 @@ impl JitBackend { /// If the function is already compiled, returns it immediately. Otherwise, enqueues /// a synchronous JIT compilation request and blocks until the result is available. /// Returns [`LookupDecision::Unavailable`] if the bytecode is ineligible for - /// compilation (see [`RuntimeTuning::should_compile`]) or compilation fails. + /// compilation (see [`RuntimeTuning::should_compile`]) or compilation does not produce a + /// resident program. Each such miss is counted in + /// [`interpreter_fallbacks`](RuntimeStatsSnapshot::interpreter_fallbacks). pub fn lookup_blocking(&self, req: LookupRequest) -> LookupDecision { + let decision = self.lookup_blocking_inner(req); + if let LookupDecision::Unavailable(_) = decision { + self.count_interpreter_fallback(); + } + decision + } + + fn lookup_blocking_inner(&self, req: LookupRequest) -> LookupDecision { if !self.inner.tuning.should_compile(&req.code) { return LookupDecision::Unavailable(LookupMissReason::Ineligible); } diff --git a/crates/jit/runtime/src/runtime/stats.rs b/crates/jit/runtime/src/runtime/stats.rs index ebfaa0d8b..f40e8c6ef 100644 --- a/crates/jit/runtime/src/runtime/stats.rs +++ b/crates/jit/runtime/src/runtime/stats.rs @@ -11,6 +11,8 @@ pub(crate) struct RuntimeStats { pub(crate) lookup_misses: AtomicU64, /// Total lookup events dropped due to event-queue overflow. pub(crate) events_dropped: AtomicU64, + /// Total frames that fell back to the interpreter although the runtime is enabled. + pub(crate) interpreter_fallbacks: AtomicU64, /// Total control commands dropped because the command channel was full. pub(crate) commands_dropped: AtomicU64, /// Total number of entries evicted (idle + budget). @@ -62,6 +64,20 @@ pub struct RuntimeStatsSnapshot { pub lookup_misses: u64, /// Total lookup events dropped due to event-queue overflow. pub events_dropped: u64, + /// Total frames that fell back to the interpreter although the runtime is enabled. + /// + /// Counted in [`blocking`](super::RuntimeConfig::blocking) mode, where every lookup that does + /// not return a compiled function is a fallback: a failed or timed-out compilation, a full + /// pending-job cap, a saturated worker pool, a program evicted before use, or empty bytecode. + /// A failed compilation is also counted once in `compilations_failed`; this counter counts + /// every frame, including later frames of the same failed code. + /// + /// Also counts frames whose gas exceeds `i64::MAX`, and hosts that the runtime rejects + /// because their type, gas schedule, or opcode table differs from the compiled code's. A + /// rejected host is counted once per support check of the runner, not once per frame, because + /// the `Evm` then stops asking the runtime to run frames. `run_interpreter` counts it on every + /// call. + pub interpreter_fallbacks: u64, /// Total control commands dropped because the command channel was full. /// /// Pause/resume commands are best-effort and dropped instead of blocking the caller when @@ -142,6 +158,7 @@ impl RuntimeStats { lookup_hits: self.lookup_hits.load(Ordering::Relaxed), lookup_misses: self.lookup_misses.load(Ordering::Relaxed), events_dropped: self.events_dropped.load(Ordering::Relaxed), + interpreter_fallbacks: self.interpreter_fallbacks.load(Ordering::Relaxed), commands_dropped: self.commands_dropped.load(Ordering::Relaxed), resident_entries: gauges.resident_entries, events_queued: gauges.events_queued, diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index d9e1ec47d..3f234f6a3 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -306,6 +306,60 @@ fn zero_workers_fails_sync_compile_without_blocking() { let stats = tb.stats(); assert_eq!(stats.resident_entries, 0); assert_eq!(stats.compilations_dispatched, 0); + assert_eq!(stats.interpreter_fallbacks, 1); +} + +#[test] +fn blocking_pending_job_cap_counts_fallback() { + let tb = TestBackend::new(RuntimeConfig { + blocking: true, + tuning: RuntimeTuning { jit_max_pending_jobs: 0, ..Default::default() }, + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::JitFailed))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); +} + +#[test] +#[cfg(feature = "llvm")] +fn blocking_sync_compile_timeout_counts_fallback() { + // The callback runs before the waiting caller is notified, so the caller times out first. + let tb = TestBackend::new(RuntimeConfig { + blocking: true, + tuning: RuntimeTuning { + jit_worker_count: 1, + sync_compile_timeout: std::time::Duration::from_millis(20), + ..Default::default() + }, + on_compilation: Some(Arc::new(|_| { + std::thread::sleep(std::time::Duration::from_millis(500)) + })), + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::JitFailed))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); +} + +#[test] +#[cfg(feature = "llvm")] +fn blocking_mode_compiles_bytecode_over_max_len() { + let tb = TestBackend::new(RuntimeConfig { + blocking: true, + tuning: RuntimeTuning { + jit_worker_count: 1, + jit_max_bytecode_len: 4, + ..Default::default() + }, + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Compiled(_))); + assert_eq!(tb.stats().interpreter_fallbacks, 0); } #[test] @@ -818,6 +872,7 @@ fn blocking_mode() { // Empty bytecodes return Ineligible (nothing to compile). let decision = tb.lookup(TestBackend::req_cancun(&[])); assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::Ineligible))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); } #[test] From 14ca45dbc390bb6895f21060f6a6320f97c73473 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:43 +0100 Subject: [PATCH 059/159] docs(jit): document the single_error default as a measured trade-off (#925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `single_error` makes every in-function failure path return `OutOfGas`, so under the JIT an invalid jump or stack underflow reports `OutOfGas` through the transaction result, unlike the interpreter. I tried flipping the default to `false` to remove that difference. It costs real compile time, code size and runtime instructions, so the default stays `true`. This PR replaces the option's contradictory doc ("Useful for benchmarking... Defaults to `true`") with the measured trade-off and records the rejected default in `EXPERIMENTS.md`. No behaviour changes. Tests that compare stop reasons keep their `single_error: false` override. ## Benchmarks The tables below measure the default flipped to `false` (head) against `origin/main` (base); this PR itself leaves codegen unchanged. Flipping the default changes `opt.s` in 23/31 benchmarks: JIT size +4.0% (geomean +2.8%), `opt.s` +6.9%, compile instructions +10.7% (geomean +6.1%), and runtime instructions +3.8% (geomean +0.5%), with `onchain_lm_v2` +4.3%, `burntpix` +4.2% and `snailtracer` +2.8%. Head `66003c7d54` with uncommitted changes vs base `origin/main` (`66003c7d54`). ### Codegen statistics `opt.s` changed in 23/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|------------:|------------:|------------:|------------:|-------------:|--------------:|----------:|-------------:|------------:| | fibonacci-calldata | +1.2% 🔴 | = | = | = | = | = | = | = | = | | factorial | +1.2% 🔴 | = | = | = | = | = | = | = | = | | snailtracer | = | +5.3% 🔴 | +5.4% 🔴 | +5.1% 🔴 | = | = | = | = | -2.6% 🟢 | | weth | = | +0.2% 🔴 | -0.2% 🟢 | -1.1% 🟢 | = | = | = | +3.1% 🔴 | = | | erc20_transfer | = | +1.9% 🔴 | +6.5% 🔴 | +3.7% 🔴 | = | = | = | = | = | | push0_proxy | +1.3% 🔴 | = | = | = | = | = | - | - | - | | usdc_proxy | +0.1% 🔴 | +2.4% 🔴 | +6.0% 🔴 | +4.3% 🔴 | = | = | = | +20.0% 🔴 | +40.0% 🔴 | | fiat_token | = | +1.3% 🔴 | +4.4% 🔴 | +2.3% 🔴 | = | = | = | -1.5% 🟢 | -1.5% 🟢 | | uniswap_v2_pair | = | +2.1% 🔴 | +5.4% 🔴 | +2.6% 🔴 | = | = | = | +7.3% 🔴 | -16.8% 🟢 | | univ2_router | = | +1.2% 🔴 | +5.2% 🔴 | +3.5% 🔴 | = | = | = | +10.3% 🔴 | +44.3% 🔴 | | seaport | = | +2.6% 🔴 | +7.9% 🔴 | +4.6% 🔴 | = | = | = | -16.3% 🟢 | -1.0% 🟢 | | airdrop | = | +2.5% 🔴 | +5.0% 🔴 | +5.2% 🔴 | = | = | = | +13.3% 🔴 | -10.0% 🟢 | | eip2935 | +0.8% 🔴 | +3.7% 🔴 | +2.6% 🔴 | +3.6% 🔴 | = | = | = | - | - | | burntpix | = | +2.1% 🔴 | +4.1% 🔴 | +2.4% 🔴 | = | = | = | = | +40.0% 🔴 | | curve_stableswap | = | +5.9% 🔴 | +10.1% 🔴 | +7.7% 🔴 | = | = | = | -44.4% 🟢 | -20.0% 🟢 | | onchain_lm_v2 | = | +2.7% 🔴 | +8.4% 🔴 | +6.1% 🔴 | = | = | = | -1.8% 🟢 | +21.9% 🔴 | | usdt | = | +0.8% 🔴 | +3.2% 🔴 | +2.0% 🔴 | = | = | = | = | +2.1% 🔴 | | universal_router | = | +2.6% 🔴 | +8.4% 🔴 | +4.3% 🔴 | = | = | = | -5.6% 🟢 | -5.9% 🟢 | | aggregation_router_v6 | = | +3.2% 🔴 | +11.3% 🔴 | +6.9% 🔴 | = | = | = | +29.7% 🔴 | +92.0% 🔴 | | entry_point_v0_7 | = | +2.0% 🔴 | +6.8% 🔴 | +4.1% 🔴 | = | = | = | -23.4% 🟢 | +3.6% 🔴 | | uniswap_v3_pool | = | +2.3% 🔴 | +4.4% 🔴 | +2.2% 🔴 | +0.1% 🔴 | -0.2% 🟢 | = | +16.8% 🔴 | -32.9% 🟢 | | uniswap_v4_pool_manager | = | +2.2% 🔴 | +5.3% 🔴 | +1.2% 🔴 | = | = | = | +55.1% 🔴 | -55.2% 🟢 | | curve_3pool | = | +0.7% 🔴 | +3.4% 🔴 | +1.9% 🔴 | = | = | = | +0.3% 🔴 | +1.5% 🔴 | | curve_tricrypto2 | = | +0.5% 🔴 | +3.3% 🔴 | +2.4% 🔴 | = | = | = | -16.0% 🟢 | +28.9% 🔴 | | balancer_vault | = | +4.1% 🔴 | +13.5% 🔴 | +6.3% 🔴 | = | = | = | +291.6% 🔴 | +87.2% 🔴 | | succinct_staking | = | +3.1% 🔴 | +7.3% 🔴 | +5.3% 🔴 | = | = | = | +57.1% 🔴 | -0.6% 🟢 | | **TOTAL** | **=** | **+2.5% 🔴** | **+6.9% 🔴** | **+4.0% 🔴** | **=** | **=** | **=** | **+11.5% 🔴** | **+9.0% 🔴** | | **GEOMEAN** | **+0.2% 🔴** | **+1.8% 🔴** | **+4.4% 🔴** | **+2.8% 🔴** | **=** | **=** | **=** | **+7.5% 🔴** | **+3.9% 🔴** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------------:|--------------------------------:|----------------------------------:|------------------------:|------------------------:|----------------------:|-----------------------------:|------------------------------:| | fibonacci-calldata | 335
339
+1.2% 🔴 | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
335
+1.2% 🔴 | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
996
+0.4% 🔴 | 341
340
-0.3% 🟢 | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60160
= | 26888
28300
+5.3% 🔴 | 33501
35295
+5.4% 🔴 | 95.2 KiB
100.1 KiB
+5.1% 🔴 | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
225
-2.6% 🟢 | | weth | 11578
11582
= | 2825
2830
+0.2% 🔴 | 4518
4507
-0.2% 🟢 | 14.0 KiB
13.8 KiB
-1.1% 🟢 | 178
178
= | 237
237
= | 25
25
= | 32
33
+3.1% 🔴 | 92
92
= | | hash_10k | 991
995
+0.4% 🔴 | 354
353
-0.3% 🟢 | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12077
= | 3811
3885
+1.9% 🔴 | 4978
5302
+6.5% 🔴 | 13.9 KiB
14.5 KiB
+3.7% 🔴 | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
317
+1.3% 🔴 | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6058
+0.1% 🔴 | 1583
1621
+2.4% 🔴 | 2274
2410
+6.0% 🔴 | 5.5 KiB
5.7 KiB
+4.3% 🔴 | 54
54
= | 131
131
= | 21
21
= | 5
6
+20.0% 🔴 | 5
7
+40.0% 🔴 | | fiat_token | 75544
75548
= | 21440
21721
+1.3% 🔴 | 29239
30522
+4.4% 🔴 | 92.8 KiB
95.0 KiB
+2.3% 🔴 | 1320
1320
= | 1902
1902
= | 277
277
= | 66
65
-1.5% 🟢 | 65
64
-1.5% 🟢 | | uniswap_v2_pair | 41668
41672
= | 12039
12288
+2.1% 🔴 | 16662
17558
+5.4% 🔴 | 52.5 KiB
53.8 KiB
+2.6% 🔴 | 680
680
= | 1079
1079
= | 147
147
= | 55
59
+7.3% 🔴 | 434
361
-16.8% 🟢 | | univ2_router | 80476
80480
= | 24414
24718
+1.2% 🔴 | 32740
34440
+5.2% 🔴 | 112.0 KiB
116.0 KiB
+3.5% 🔴 | 1675
1675
= | 2262
2262
= | 320
320
= | 107
118
+10.3% 🔴 | 436
629
+44.3% 🔴 | | seaport | 133044
133048
= | 51832
53161
+2.6% 🔴 | 68657
74055
+7.9% 🔴 | 233.6 KiB
244.5 KiB
+4.6% 🔴 | 3785
3785
= | 4386
4386
= | 782
782
= | 245
205
-16.3% 🟢 | 3057
3026
-1.0% 🟢 | | airdrop | 26607
26611
= | 8462
8675
+2.5% 🔴 | 11032
11589
+5.0% 🔴 | 31.6 KiB
33.2 KiB
+5.2% 🔴 | 432
432
= | 666
666
= | 91
91
= | 30
34
+13.3% 🔴 | 211
190
-10.0% 🟢 | | bswap64 | 1757
1761
+0.2% 🔴 | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1500
+0.3% 🔴 | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
635
+0.6% 🔴 | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
492
+0.8% 🔴 | 190
197
+3.7% 🔴 | 420
431
+2.6% 🔴 | 554 B
574 B
+3.6% 🔴 | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9183
= | 3048
3112
+2.1% 🔴 | 3880
4041
+4.1% 🔴 | 11.9 KiB
12.1 KiB
+2.4% 🔴 | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
28
+40.0% 🔴 | | curve_stableswap | 16363
16367
= | 5666
6003
+5.9% 🔴 | 7170
7893
+10.1% 🔴 | 20.5 KiB
22.0 KiB
+7.7% 🔴 | 324
324
= | 506
506
= | 44
44
= | 18
10
-44.4% 🟢 | 20
16
-20.0% 🟢 | | onchain_lm_v2 | 53236
53240
= | 17441
17911
+2.7% 🔴 | 25264
27374
+8.4% 🔴 | 74.7 KiB
79.3 KiB
+6.1% 🔴 | 1087
1087
= | 1294
1294
= | 155
155
= | 57
56
-1.8% 🟢 | 1138
1387
+21.9% 🔴 | | usdt | 34574
34578
= | 8007
8069
+0.8% 🔴 | 10687
11029
+3.2% 🔴 | 31.4 KiB
32.0 KiB
+2.0% 🔴 | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
48
+2.1% 🔴 | | universal_router | 77644
77648
= | 29450
30218
+2.6% 🔴 | 36915
40009
+8.4% 🔴 | 121.0 KiB
126.1 KiB
+4.3% 🔴 | 2195
2195
= | 2850
2850
= | 232
232
= | 197
186
-5.6% 🟢 | 1221
1149
-5.9% 🟢 | | aggregation_router_v6 | 125084
125088
= | 45234
46701
+3.2% 🔴 | 52441
58376
+11.3% 🔴 | 171.4 KiB
183.2 KiB
+6.9% 🔴 | 3087
3087
= | 4672
4672
= | 313
313
= | 74
96
+29.7% 🔴 | 846
1624
+92.0% 🔴 | | entry_point_v0_7 | 66940
66944
= | 25010
25520
+2.0% 🔴 | 31306
33440
+6.8% 🔴 | 103.9 KiB
108.1 KiB
+4.1% 🔴 | 1771
1771
= | 2081
2081
= | 501
501
= | 47
36
-23.4% 🟢 | 854
885
+3.6% 🔴 | | uniswap_v3_pool | 117314
117318
= | 33907
34701
+2.3% 🔴 | 44303
46256
+4.4% 🔴 | 146.1 KiB
149.2 KiB
+2.2% 🔴 | 2275
2277
+0.1% 🔴 | 2598
2594
-0.2% 🟢 | 432
432
= | 131
153
+16.8% 🔴 | 1345
902
-32.9% 🟢 | | uniswap_v4_pool_manager | 93527
93531
= | 32124
32817
+2.2% 🔴 | 42527
44782
+5.3% 🔴 | 141.6 KiB
143.3 KiB
+1.2% 🔴 | 2241
2241
= | 2625
2625
= | 358
358
= | 185
287
+55.1% 🔴 | 1146
513
-55.2% 🟢 | | curve_3pool | 105047
105051
= | 30590
30790
+0.7% 🔴 | 47304
48909
+3.4% 🔴 | 170.2 KiB
173.5 KiB
+1.9% 🔴 | 1784
1784
= | 2299
2299
= | 460
460
= | 374
375
+0.3% 🔴 | 3164
3211
+1.5% 🔴 | | curve_tricrypto2 | 114225
114229
= | 31974
32119
+0.5% 🔴 | 49484
51126
+3.3% 🔴 | 175.7 KiB
179.9 KiB
+2.4% 🔴 | 2030
2030
= | 2635
2635
= | 342
342
= | 645
542
-16.0% 🟢 | 1683
2169
+28.9% 🔴 | | balancer_vault | 145873
145877
= | 51478
53607
+4.1% 🔴 | 64838
73607
+13.5% 🔴 | 219.1 KiB
233.0 KiB
+6.3% 🔴 | 3384
3385
= | 4377
4378
= | 508
508
= | 83
325
+291.6% 🔴 | 1493
2795
+87.2% 🔴 | | succinct_staking | 85875
85879
= | 26294
27118
+3.1% 🔴 | 38378
41185
+7.3% 🔴 | 117.3 KiB
123.4 KiB
+5.3% 🔴 | 1508
1508
= | 2340
2340
= | 214
214
= | 98
154
+57.1% 🔴 | 2534
2518
-0.6% 🟢 | | **TOTAL** | **1499415
1499539
=** | **495713
508086
+2.5% 🔴** | **662536
708154
+6.9% 🔴** | **2.1 MiB
2.2 MiB
+4.0% 🔴** | **32188
32191
=** | **42286
42283
=** | **5830
5830
=** | **2535
2826
+11.5% 🔴** | **20063
21860
+9.0% 🔴** |
### Compile times | benchmark | instructions | cycles | time | |:------------------------|---------------:|------------:|------------:| | counter | +1.3% 🔴 | -2.8% 🟢 | -5.6% 🟢 | | snailtracer | +7.9% 🔴 | +8.6% 🔴 | +12.8% 🔴 | | erc20_transfer | +4.7% 🔴 | -22.2% 🟢 | -25.7% 🟢 | | usdc_proxy | +7.2% 🔴 | -1.8% 🟢 | +8.4% 🔴 | | fiat_token | +5.7% 🔴 | -24.8% 🟢 | -27.7% 🟢 | | uniswap_v2_pair | +6.6% 🔴 | -0.3% 🟢 | +6.4% 🔴 | | univ2_router | +5.5% 🔴 | +26.8% 🔴 | +29.3% 🔴 | | seaport | +18.0% 🔴 | +6.4% 🔴 | +5.6% 🔴 | | airdrop | +5.8% 🔴 | +17.0% 🔴 | +13.9% 🔴 | | burntpix | +7.3% 🔴 | +12.4% 🔴 | +10.4% 🔴 | | curve_stableswap | +8.3% 🔴 | +4.4% 🔴 | +4.8% 🔴 | | onchain_lm_v2 | +11.5% 🔴 | -13.9% 🟢 | -10.1% 🟢 | | usdt | +5.3% 🔴 | -1.6% 🟢 | -0.3% 🟢 | | universal_router | +8.8% 🔴 | +18.8% 🔴 | +20.3% 🔴 | | aggregation_router_v6 | +9.6% 🔴 | +3.6% 🔴 | -4.8% 🟢 | | entry_point_v0_7 | +13.8% 🔴 | +10.2% 🔴 | +12.7% 🔴 | | uniswap_v3_pool | +11.0% 🔴 | +7.2% 🔴 | +7.4% 🔴 | | uniswap_v4_pool_manager | +6.6% 🔴 | -7.4% 🟢 | -2.9% 🟢 | | curve_3pool | +11.1% 🔴 | +27.6% 🔴 | +27.8% 🔴 | | curve_tricrypto2 | +5.9% 🔴 | -12.1% 🟢 | -10.6% 🟢 | | balancer_vault | +17.1% 🔴 | +13.4% 🔴 | +17.6% 🔴 | | succinct_staking | +10.5% 🔴 | +69.8% 🔴 | +72.7% 🔴 | | **TOTAL** | **+10.7% 🔴** | **+8.9% 🔴** | **+8.2% 🔴** | | **GEOMEAN** | **+6.1% 🔴** | **+3.6% 🔴** | **+4.8% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|-----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:| | fibonacci-calldata | 37.47M
37.57M
+0.3% 🔴 | 63.42M
73.64M
+16.1% 🔴 | 23.0ms
24.2ms
+5.4% 🔴 | 1.22ms
553.7µs
-54.7% 🟢 | 6.28ms
860.7µs
-86.3% 🟢 | 19.6ms
3.50ms
-82.1% 🟢 | 22.1ms
30.0ms
+35.6% 🔴 | | factorial | 41.65M
41.70M
+0.1% 🔴 | 74.35M
63.17M
-15.0% 🟢 | 25.9ms
22.3ms
-13.8% 🟢 | 481.1µs
317.5µs
-34.0% 🟢 | 887.3µs
717.0µs
-19.2% 🟢 | 11.4ms
6.24ms
-45.1% 🟢 | 18.3ms
29.1ms
+59.3% 🔴 | | counter | 54.18M
54.89M
+1.3% 🔴 | 75.05M
72.94M
-2.8% 🟢 | 21.7ms
20.4ms
-5.6% 🟢 | 875.2µs
4.28ms
+389.2% 🔴 | 1.14ms
4.23ms
+270.8% 🔴 | 3.91ms
5.85ms
+49.4% 🔴 | 26.0ms
11.8ms
-54.6% 🟢 | | snailtracer | 5.29G
5.70G
+7.9% 🔴 | 5.61G
6.09G
+8.6% 🔴 | 1.214s
1.370s
+12.8% 🔴 | 113.5ms
299.6ms
+163.9% 🔴 | 51.7ms
84.0ms
+62.5% 🔴 | 253.4ms
845.6ms
+233.7% 🔴 | 1.020s
1.475s
+44.6% 🔴 | | weth | 538.89M
543.51M
+0.9% 🔴 | 356.90M
377.02M
+5.6% 🔴 | 80.1ms
91.0ms
+13.6% 🔴 | 5.43ms
7.77ms
+43.2% 🔴 | 4.39ms
15.7ms
+258.9% 🔴 | 52.9ms
43.5ms
-17.8% 🟢 | 108.5ms
154.1ms
+42.0% 🔴 | | hash_10k | 80.44M
80.45M
= | 84.72M
82.12M
-3.1% 🟢 | 23.8ms
22.8ms
-4.2% 🟢 | 6.26ms
624.5µs
-90.0% 🟢 | 4.66ms
931.4µs
-80.0% 🟢 | 35.4ms
6.67ms
-81.2% 🟢 | 61.1ms
19.4ms
-68.2% 🟢 | | erc20_transfer | 576.99M
604.30M
+4.7% 🔴 | 415.76M
323.32M
-22.2% 🟢 | 101.6ms
75.5ms
-25.7% 🟢 | 19.5ms
13.8ms
-29.3% 🟢 | 11.9ms
3.88ms
-67.3% 🟢 | 183.1ms
51.5ms
-71.8% 🟢 | 559.8ms
193.2ms
-65.5% 🟢 | | push0_proxy | 34.73M
34.76M
+0.1% 🔴 | 42.05M
46.82M
+11.3% 🔴 | 15.9ms
19.5ms
+22.1% 🔴 | 327.8µs
331.5µs
+1.1% 🔴 | 766.7µs
929.3µs
+21.2% 🔴 | 4.92ms
2.11ms
-57.1% 🟢 | 7.48ms
4.86ms
-35.0% 🟢 | | usdc_proxy | 243.94M
261.52M
+7.2% 🔴 | 159.65M
156.74M
-1.8% 🟢 | 35.9ms
38.9ms
+8.4% 🔴 | 14.3ms
6.84ms
-52.1% 🟢 | 11.8ms
4.78ms
-59.6% 🟢 | 74.3ms
22.5ms
-69.7% 🟢 | 149.3ms
40.2ms
-73.1% 🟢 | | fiat_token | 3.97G
4.20G
+5.7% 🔴 | 2.87G
2.16G
-24.8% 🟢 | 644.5ms
465.9ms
-27.7% 🟢 | 97.8ms
76.8ms
-21.4% 🟢 | 106.2ms
73.3ms
-30.9% 🟢 | 364.2ms
657.1ms
+80.4% 🔴 | 1.435s
1.497s
+4.3% 🔴 | | uniswap_v2_pair | 2.06G
2.19G
+6.6% 🔴 | 1.56G
1.55G
-0.3% 🟢 | 350.3ms
372.6ms
+6.4% 🔴 | 45.4ms
22.9ms
-49.5% 🟢 | 56.0ms
22.7ms
-59.5% 🟢 | 694.4ms
325.3ms
-53.2% 🟢 | 573.1ms
597.5ms
+4.3% 🔴 | | univ2_router | 4.39G
4.63G
+5.5% 🔴 | 3.49G
4.43G
+26.8% 🔴 | 788.2ms
1.019s
+29.3% 🔴 | 36.1ms
47.9ms
+32.6% 🔴 | 29.8ms
31.2ms
+4.7% 🔴 | 254.5ms
196.3ms
-22.9% 🟢 | 1.222s
828.2ms
-32.2% 🟢 | | seaport | 9.73G
11.48G
+18.0% 🔴 | 11.21G
11.93G
+6.4% 🔴 | 2.471s
2.609s
+5.6% 🔴 | 121.3ms
590.8ms
+387.0% 🔴 | 102.7ms
74.3ms
-27.7% 🟢 | 1.132s
844.3ms
-25.4% 🟢 | 4.499s
4.580s
+1.8% 🔴 | | airdrop | 1.42G
1.50G
+5.8% 🔴 | 1.01G
1.18G
+17.0% 🔴 | 233.8ms
266.3ms
+13.9% 🔴 | 62.7ms
53.9ms
-14.1% 🟢 | 78.2ms
36.3ms
-53.6% 🟢 | 587.0ms
177.0ms
-69.9% 🟢 | 1.043s
330.3ms
-68.3% 🟢 | | bswap64 | 67.07M
66.92M
-0.2% 🟢 | 70.90M
76.97M
+8.6% 🔴 | 20.0ms
21.7ms
+8.4% 🔴 | 1.01ms
5.34ms
+427.0% 🔴 | 19.2ms
1.31ms
-93.2% 🟢 | 21.7ms
3.81ms
-82.5% 🟢 | 74.2ms
28.8ms
-61.2% 🟢 | | bswap64_opt | 73.76M
73.68M
-0.1% 🟢 | 71.64M
79.13M
+10.5% 🔴 | 22.5ms
22.8ms
+1.2% 🔴 | 878.1µs
1.61ms
+83.0% 🔴 | 2.16ms
2.19ms
+1.4% 🔴 | 12.0ms
4.96ms
-58.7% 🟢 | 23.0ms
12.8ms
-44.1% 🟢 | | eip4788 | 48.63M
49.02M
+0.8% 🔴 | 60.55M
56.88M
-6.1% 🟢 | 17.5ms
17.0ms
-2.8% 🟢 | 470.0µs
544.7µs
+15.9% 🔴 | 885.2µs
861.8µs
-2.6% 🟢 | 2.60ms
3.09ms
+18.7% 🔴 | 9.81ms
24.6ms
+150.5% 🔴 | | eip2935 | 42.42M
42.70M
+0.7% 🔴 | 59.00M
50.92M
-13.7% 🟢 | 21.0ms
21.2ms
+0.9% 🔴 | 334.7µs
1.51ms
+352.0% 🔴 | 737.0µs
4.88ms
+562.2% 🔴 | 10.7ms
7.65ms
-28.2% 🟢 | 8.20ms
13.3ms
+62.2% 🔴 | | burntpix | 6.70G
7.19G
+7.3% 🔴 | 4.84G
5.44G
+12.4% 🔴 | 1.070s
1.181s
+10.4% 🔴 | 39.8ms
97.5ms
+145.0% 🔴 | 6.18ms
6.53ms
+5.6% 🔴 | 36.5ms
112.4ms
+207.9% 🔴 | 59.6ms
161.8ms
+171.5% 🔴 | | curve_stableswap | 1.14G
1.23G
+8.3% 🔴 | 849.70M
887.35M
+4.4% 🔴 | 195.9ms
205.3ms
+4.8% 🔴 | 16.8ms
17.6ms
+4.4% 🔴 | 17.2ms
11.8ms
-31.8% 🟢 | 164.8ms
176.6ms
+7.1% 🔴 | 469.9ms
217.4ms
-53.7% 🟢 | | onchain_lm_v2 | 3.65G
4.07G
+11.5% 🔴 | 3.63G
3.13G
-13.9% 🟢 | 810.9ms
728.7ms
-10.1% 🟢 | 99.2ms
50.3ms
-49.3% 🟢 | 71.0ms
63.6ms
-10.4% 🟢 | 817.7ms
184.6ms
-77.4% 🟢 | 740.8ms
982.6ms
+32.6% 🔴 | | usdt | 1.34G
1.41G
+5.3% 🔴 | 955.22M
939.58M
-1.6% 🟢 | 217.4ms
216.7ms
-0.3% 🟢 | 76.7ms
31.3ms
-59.1% 🟢 | 17.9ms
19.6ms
+9.5% 🔴 | 109.3ms
139.5ms
+27.7% 🔴 | 195.2ms
376.8ms
+93.1% 🔴 | | universal_router | 11.56G
12.58G
+8.8% 🔴 | 9.62G
11.43G
+18.8% 🔴 | 2.126s
2.558s
+20.3% 🔴 | 266.7ms
87.8ms
-67.1% 🟢 | 41.8ms
37.7ms
-9.7% 🟢 | 216.3ms
388.8ms
+79.8% 🔴 | 2.277s
2.460s
+8.0% 🔴 | | aggregation_router_v6 | 35.12G
38.51G
+9.6% 🔴 | 33.74G
34.95G
+3.6% 🔴 | 8.031s
7.642s
-4.8% 🟢 | 399.9ms
195.6ms
-51.1% 🟢 | 99.4ms
168.2ms
+69.3% 🔴 | 475.7ms
551.9ms
+16.0% 🔴 | 1.748s
2.051s
+17.3% 🔴 | | entry_point_v0_7 | 17.89G
20.36G
+13.8% 🔴 | 16.33G
17.99G
+10.2% 🔴 | 3.548s
4.000s
+12.7% 🔴 | 40.9ms
93.2ms
+127.8% 🔴 | 64.6ms
55.1ms
-14.7% 🟢 | 209.4ms
356.9ms
+70.4% 🔴 | 1.064s
1.509s
+41.8% 🔴 | | uniswap_v3_pool | 6.20G
6.88G
+11.0% 🔴 | 5.00G
5.36G
+7.2% 🔴 | 1.133s
1.216s
+7.4% 🔴 | 170.3ms
348.1ms
+104.4% 🔴 | 68.5ms
163.2ms
+138.3% 🔴 | 962.6ms
998.8ms
+3.8% 🔴 | 2.433s
5.429s
+123.1% 🔴 | | uniswap_v4_pool_manager | 6.70G
7.14G
+6.6% 🔴 | 6.59G
6.10G
-7.4% 🟢 | 1.466s
1.423s
-2.9% 🟢 | 265.5ms
128.5ms
-51.6% 🟢 | 140.9ms
155.3ms
+10.2% 🔴 | 1.851s
802.0ms
-56.7% 🟢 | 3.673s
1.666s
-54.6% 🟢 | | curve_3pool | 6.59G
7.32G
+11.1% 🔴 | 5.06G
6.45G
+27.6% 🔴 | 1.145s
1.464s
+27.8% 🔴 | 67.9ms
441.1ms
+549.8% 🔴 | 62.8ms
89.8ms
+43.0% 🔴 | 1.032s
358.0ms
-65.3% 🟢 | 4.169s
2.135s
-48.8% 🟢 | | curve_tricrypto2 | 9.40G
9.96G
+5.9% 🔴 | 9.58G
8.42G
-12.1% 🟢 | 2.135s
1.909s
-10.6% 🟢 | 152.9ms
118.6ms
-22.4% 🟢 | 49.4ms
145.9ms
+195.4% 🔴 | 628.9ms
2.176s
+246.0% 🔴 | 1.789s
2.231s
+24.7% 🔴 | | balancer_vault | 19.04G
22.29G
+17.1% 🔴 | 22.29G
25.28G
+13.4% 🔴 | 4.710s
5.539s
+17.6% 🔴 | 447.3ms
94.6ms
-78.8% 🟢 | 126.2ms
85.6ms
-32.1% 🟢 | 679.5ms
987.9ms
+45.4% 🔴 | 4.780s
4.726s
-1.1% 🟢 | | succinct_staking | 10.84G
11.99G
+10.5% 🔴 | 5.82G
9.88G
+69.8% 🔴 | 1.248s
2.156s
+72.7% 🔴 | 126.3ms
102.4ms
-18.9% 🟢 | 78.2ms
29.8ms
-61.9% 🟢 | 333.8ms
226.1ms
-32.3% 🟢 | 2.103s
1.953s
-7.1% 🟢 | | **TOTAL** | **164.87G
182.54G
+10.7% 🔴** | **151.59G
165.07G
+8.9% 🔴** | **33.948s
36.738s
+8.2% 🔴** | **2.698s
2.942s
+9.0% 🔴** | **1.333s
1.395s
+4.6% 🔴** | **11.236s
10.667s
-5.1% 🟢** | **36.361s
35.769s
-1.6% 🟢** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:-----------------|--------------------:|--------------:| | snailtracer | +2.8% 🔴 | +3.7% 🔴 | | weth | -1.7% 🟢 | +9.1% 🔴 | | hash_10k | = | +14.1% 🔴 | | push0_proxy | +0.2% 🔴 | +9.4% 🔴 | | usdc_proxy | +2.1% 🔴 | +2.3% 🔴 | | seaport | -0.6% 🟢 | +13.9% 🔴 | | bswap64_opt | -1.9% 🟢 | -9.8% 🟢 | | eip4788 | -1.3% 🟢 | -9.8% 🟢 | | eip2935 | +1.7% 🔴 | +4.3% 🔴 | | burntpix | +4.2% 🔴 | -6.8% 🟢 | | curve_stableswap | +1.7% 🔴 | +1.0% 🔴 | | onchain_lm_v2 | +4.3% 🔴 | +0.5% 🔴 | | entry_point_v0_7 | +0.1% 🔴 | -5.1% 🟢 | | curve_tricrypto2 | +0.6% 🔴 | +7.6% 🔴 | | balancer_vault | +1.8% 🔴 | +1.6% 🔴 | | **TOTAL** | **+3.8% 🔴** | **+0.3% 🔴** | | **GEOMEAN** | **+0.5% 🔴** | **+0.9% 🔴** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|----------------------------------:|----------------------------------:| | fibonacci-calldata | 42.90K
42.89K
= | 13.48K
13.37K
-0.8% 🟢 | | factorial | 78.54K
78.63K
+0.1% 🔴 | 23.21K
23.61K
+1.7% 🔴 | | counter | 22.30K
22.39K
+0.4% 🔴 | 10.43K
10.16K
-2.6% 🟢 | | snailtracer | 200.35M
206.00M
+2.8% 🔴 | 79.22M
82.13M
+3.7% 🔴 | | weth | 27.21K
26.76K
-1.7% 🟢 | 11.23K
12.26K
+9.1% 🔴 | | hash_10k | 7.15M
7.15M
= | 3.00M
3.43M
+14.1% 🔴 | | erc20_transfer | 19.73M
19.83M
+0.5% 🔴 | 8.02M
8.06M
+0.5% 🔴 | | push0_proxy | 21.73K
21.77K
+0.2% 🔴 | 10.09K
11.04K
+9.4% 🔴 | | usdc_proxy | 23.42K
23.92K
+2.1% 🔴 | 11.51K
11.78K
+2.3% 🔴 | | fiat_token | 21.60K
21.63K
+0.1% 🔴 | 10.41K
10.54K
+1.2% 🔴 | | uniswap_v2_pair | 21.81K
21.94K
+0.6% 🔴 | 10.68K
10.83K
+1.4% 🔴 | | univ2_router | 19.48K
19.54K
+0.3% 🔴 | 9.42K
9.76K
+3.6% 🔴 | | seaport | 22.26K
22.12K
-0.6% 🟢 | 9.62K
10.95K
+13.9% 🔴 | | airdrop | 21.97K
21.78K
-0.9% 🟢 | 9.33K
9.05K
-3.0% 🟢 | | bswap64 | 18.73K
18.73K
= | 7.89K
7.72K
-2.1% 🟢 | | bswap64_opt | 18.91K
18.55K
-1.9% 🟢 | 8.24K
7.43K
-9.8% 🟢 | | eip4788 | 23.29K
22.99K
-1.3% 🟢 | 11.87K
10.71K
-9.8% 🟢 | | eip2935 | 21.72K
22.09K
+1.7% 🔴 | 10.79K
11.26K
+4.3% 🔴 | | burntpix | 104.64M
109.07M
+4.2% 🔴 | 51.87M
48.35M
-6.8% 🟢 | | curve_stableswap | 242.76K
246.89K
+1.7% 🔴 | 119.92K
121.14K
+1.0% 🔴 | | onchain_lm_v2 | 521.34M
543.89M
+4.3% 🔴 | 265.62M
266.99M
+0.5% 🔴 | | usdt | 38.79K
38.46K
-0.8% 🟢 | 13.22K
13.78K
+4.3% 🔴 | | universal_router | 141.59K
142.09K
+0.4% 🔴 | 59.29K
58.76K
-0.9% 🟢 | | aggregation_router_v6 | 450.54K
451.91K
+0.3% 🔴 | 185.27K
182.50K
-1.5% 🟢 | | entry_point_v0_7 | 1.25M
1.25M
+0.1% 🔴 | 343.83K
326.30K
-5.1% 🟢 | | uniswap_v3_pool | 86.29K
86.53K
+0.3% 🔴 | 28.36K
28.36K
= | | uniswap_v4_pool_manager | 60.21K
60.21K
= | 20.56K
21.14K
+2.8% 🔴 | | curve_3pool | 79.03K
79.15K
+0.2% 🔴 | 29.97K
28.84K
-3.8% 🟢 | | curve_tricrypto2 | 206.46K
207.61K
+0.6% 🔴 | 90.01K
96.81K
+7.6% 🔴 | | balancer_vault | 120.02K
122.23K
+1.8% 🔴 | 43.09K
43.79K
+1.6% 🔴 | | succinct_staking | 62.10K
62.23K
+0.2% 🔴 | 24.50K
23.59K
-3.7% 🟢 | | **TOTAL** | **856.35M
889.11M
+3.8% 🔴** | **408.86M
410.07M
+0.3% 🔴** |
--- crates/jit/EXPERIMENTS.md | 5 +++++ crates/jit/codegen/src/compiler/mod.rs | 5 ++++- crates/jit/runtime/src/runtime/config.rs | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/jit/EXPERIMENTS.md b/crates/jit/EXPERIMENTS.md index 28182a41f..eada5e150 100644 --- a/crates/jit/EXPERIMENTS.md +++ b/crates/jit/EXPERIMENTS.md @@ -27,6 +27,11 @@ ones here with the numbers that rejected them. - Dropping `relieve_vstack_pressure`, so the virtual stack only stores at section boundaries and builtin calls: i256 stores -23%, but JIT size +13%, spills ×6 and runtime instructions +9.6% (onchain_lm_v2 +13.5%). +- Making `single_error = false` the default, so the JIT reports the + interpreter's halt reason instead of `OutOfGas`: `opt.s` changed in 23/31 + benchmarks, JIT size +4.0% total, compile instructions +10.7% total (geomean + +6.1%) and runtime instructions +3.8% total (geomean +0.5%; `onchain_lm_v2` + +4.3%, `burntpix` +4.2%, `snailtracer` +2.8%). ## Optimization passes diff --git a/crates/jit/codegen/src/compiler/mod.rs b/crates/jit/codegen/src/compiler/mod.rs index 6e32c6d8f..3d10dca0f 100644 --- a/crates/jit/codegen/src/compiler/mod.rs +++ b/crates/jit/codegen/src/compiler/mod.rs @@ -513,7 +513,10 @@ impl EvmCompiler { /// this lets LLVM DCE the per-failure-site materialization and the failure-block phi. /// Successful exits (`STOP`/`RETURN`/`REVERT`) keep their original codes. /// - /// Useful for benchmarking the cost of failure-result materialization. + /// This is a deliberate trade-off: the compiled function reports `OutOfGas` where the + /// interpreter reports the real halt reason (for example, an invalid jump). Preserving the + /// reasons (`false`) makes the JIT 4.0% larger, compile 10.7% more instructions, and + /// execute 3.8% more instructions in total across the benchmarks (`onchain_lm_v2` +4.3%). /// /// Defaults to `true`. pub const fn set_single_error(&mut self, yes: bool) { diff --git a/crates/jit/runtime/src/runtime/config.rs b/crates/jit/runtime/src/runtime/config.rs index 033b59596..1156c7004 100644 --- a/crates/jit/runtime/src/runtime/config.rs +++ b/crates/jit/runtime/src/runtime/config.rs @@ -62,7 +62,10 @@ pub struct RuntimeConfig { /// this lets LLVM DCE the per-failure-site materialization and the failure-block phi. /// Successful exits (`STOP`/`RETURN`/`REVERT`) keep their original codes. /// - /// Useful for benchmarking the cost of failure-result materialization. + /// This is a deliberate trade-off: the compiled function reports `OutOfGas` where the + /// interpreter reports the real halt reason (for example, an invalid jump). Preserving the + /// reasons (`false`) makes the JIT 4.0% larger, compile 10.7% more instructions, and + /// execute 3.8% more instructions in total across the benchmarks (`onchain_lm_v2` +4.3%). /// /// Defaults to `true`. pub single_error: bool, From d80a7d1305ee7aef2f36e2a62e8535700cde88f9 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:31:52 +0100 Subject: [PATCH 060/159] refactor(jit): build LLVM memory, uwtable and captures attributes through LLVM's API (#927) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `memory`, `uwtable` and `captures` function and parameter attributes were built from hand-copied encodings (`ModRef(3) << ArgMem(0)`, `UWTableKind::Async` as `2`, `CaptureInfo::none()` as `0`). These break silently if LLVM changes the layout, as it did when it reworked the memory location list. Three C++ shims now call `Attribute::getWithMemoryEffects(MemoryEffects::argMemOnly())`, `getWithUWTableKind(UWTableKind::Async)` and `getWithCaptureInfo(CaptureInfo::none())`, and the Rust conversion calls them in place of the magic numbers. The attributes come out unchanged: `unopt.ll` and `opt.ll` are identical for all 31 benchmarks, and `opt.s` changed in 0/31. ## Benchmarks Head `66003c7d54` with uncommitted changes vs base `origin/main` (`66003c7d54`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|---------:|------------:| | eip2935 | - | - | +5.9% 🔴 | | **TOTAL** | **-** | **-** | **+0.7% 🔴** | | **GEOMEAN** | **-** | **-** | **+0.4% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------:|----------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | -
-
- | -
-
- | 11.7ms
12.0ms
+2.2% 🔴 | 164.1µs
157.3µs
-4.2% 🟢 | 395.1µs
380.7µs
-3.7% 🟢 | 1.44ms
1.37ms
-4.9% 🟢 | 2.96ms
3.02ms
+2.2% 🔴 | | factorial | -
-
- | -
-
- | 11.9ms
11.7ms
-1.8% 🟢 | 141.5µs
146.9µs
+3.8% 🔴 | 340.8µs
349.9µs
+2.6% 🔴 | 1.14ms
1.22ms
+6.6% 🔴 | 3.08ms
3.15ms
+2.4% 🔴 | | counter | -
-
- | -
-
- | 9.35ms
9.27ms
-0.8% 🟢 | 298.1µs
298.0µs
= | 461.2µs
443.9µs
-3.8% 🟢 | 1.43ms
1.34ms
-6.4% 🟢 | 4.20ms
3.62ms
-13.8% 🟢 | | snailtracer | -
-
- | -
-
- | 382.6ms
392.0ms
+2.5% 🔴 | 32.6ms
32.4ms
-0.7% 🟢 | 6.93ms
6.78ms
-2.2% 🟢 | 68.9ms
67.5ms
-2.0% 🟢 | 309.1ms
320.2ms
+3.6% 🔴 | | weth | -
-
- | -
-
- | 49.3ms
48.8ms
-0.9% 🟢 | 2.21ms
2.22ms
+0.5% 🔴 | 1.97ms
1.95ms
-1.2% 🟢 | 11.1ms
11.1ms
+0.8% 🔴 | 32.7ms
33.0ms
+1.0% 🔴 | | hash_10k | -
-
- | -
-
- | 11.5ms
11.6ms
+1.3% 🔴 | 259.2µs
265.9µs
+2.6% 🔴 | 477.4µs
456.9µs
-4.3% 🟢 | 1.94ms
1.91ms
-1.8% 🟢 | 5.19ms
5.18ms
-0.1% 🟢 | | erc20_transfer | -
-
- | -
-
- | 49.3ms
50.1ms
+1.5% 🔴 | 2.24ms
2.25ms
+0.6% 🔴 | 1.96ms
1.93ms
-1.2% 🟢 | 11.7ms
12.1ms
+2.8% 🔴 | 33.6ms
34.0ms
+1.2% 🔴 | | push0_proxy | -
-
- | -
-
- | 10.5ms
10.1ms
-4.0% 🟢 | 132.8µs
130.8µs
-1.5% 🟢 | 338.2µs
326.6µs
-3.4% 🟢 | 873.4µs
844.2µs
-3.3% 🟢 | 2.25ms
2.15ms
-4.3% 🟢 | | usdc_proxy | -
-
- | -
-
- | 24.9ms
25.4ms
+2.1% 🔴 | 1.32ms
1.32ms
+0.2% 🔴 | 1.18ms
1.15ms
-2.7% 🟢 | 5.89ms
5.89ms
+0.1% 🔴 | 14.7ms
14.7ms
+0.2% 🔴 | | fiat_token | -
-
- | -
-
- | 307.1ms
307.2ms
= | 13.9ms
13.7ms
-1.1% 🟢 | 10.4ms
9.96ms
-4.4% 🟢 | 68.7ms
69.1ms
+0.5% 🔴 | 243.0ms
243.0ms
= | | uniswap_v2_pair | -
-
- | -
-
- | 166.0ms
165.5ms
-0.3% 🟢 | 7.68ms
7.50ms
-2.3% 🟢 | 5.79ms
5.81ms
+0.4% 🔴 | 34.1ms
34.4ms
+0.9% 🔴 | 130.0ms
130.3ms
+0.3% 🔴 | | univ2_router | -
-
- | -
-
- | 346.4ms
345.6ms
-0.3% 🟢 | 15.5ms
15.4ms
-0.3% 🟢 | 10.9ms
10.8ms
-1.4% 🟢 | 69.4ms
69.9ms
+0.8% 🔴 | 284.5ms
288.5ms
+1.4% 🔴 | | seaport | -
-
- | -
-
- | 809.3ms
812.7ms
+0.4% 🔴 | 26.4ms
26.4ms
= | 18.1ms
17.5ms
-3.0% 🟢 | 175.6ms
176.2ms
+0.4% 🔴 | 695.9ms
698.7ms
+0.4% 🔴 | | airdrop | -
-
- | -
-
- | 115.9ms
115.6ms
-0.3% 🟢 | 4.74ms
4.79ms
+1.0% 🔴 | 4.05ms
4.01ms
-1.0% 🟢 | 29.3ms
29.5ms
+0.7% 🔴 | 83.6ms
83.8ms
+0.3% 🔴 | | bswap64 | -
-
- | -
-
- | 10.6ms
11.0ms
+4.2% 🔴 | 490.5µs
463.2µs
-5.6% 🟢 | 586.1µs
563.0µs
-3.9% 🟢 | 1.65ms
1.64ms
-0.5% 🟢 | 3.87ms
3.83ms
-1.1% 🟢 | | bswap64_opt | -
-
- | -
-
- | 10.6ms
10.2ms
-3.9% 🟢 | 412.6µs
400.8µs
-2.9% 🟢 | 519.7µs
482.6µs
-7.1% 🟢 | 1.66ms
1.59ms
-3.9% 🟢 | 4.18ms
4.15ms
-0.6% 🟢 | | eip4788 | -
-
- | -
-
- | 8.64ms
8.64ms
= | 194.6µs
188.1µs
-3.4% 🟢 | 418.4µs
372.1µs
-11.1% 🟢 | 1.08ms
1.04ms
-3.7% 🟢 | 3.39ms
3.52ms
+3.9% 🔴 | | eip2935 | -
-
- | -
-
- | 10.8ms
11.5ms
+5.9% 🔴 | 162.7µs
156.8µs
-3.6% 🟢 | 356.0µs
353.3µs
-0.8% 🟢 | 942.8µs
925.4µs
-1.8% 🟢 | 2.91ms
3.01ms
+3.2% 🔴 | | burntpix | -
-
- | -
-
- | 504.8ms
506.7ms
+0.4% 🔴 | 13.5ms
13.9ms
+3.3% 🔴 | 1.60ms
1.58ms
-1.7% 🟢 | 10.0ms
10.1ms
+1.0% 🔴 | 28.3ms
28.4ms
+0.3% 🔴 | | curve_stableswap | -
-
- | -
-
- | 95.4ms
94.2ms
-1.3% 🟢 | 3.20ms
3.57ms
+11.7% 🔴 | 2.51ms
2.53ms
+0.7% 🔴 | 20.9ms
21.1ms
+1.2% 🔴 | 47.5ms
47.9ms
+0.8% 🔴 | | onchain_lm_v2 | -
-
- | -
-
- | 273.7ms
273.7ms
= | 10.5ms
10.4ms
-1.1% 🟢 | 7.08ms
6.98ms
-1.4% 🟢 | 46.7ms
47.2ms
+1.0% 🔴 | 189.3ms
190.7ms
+0.8% 🔴 | | usdt | -
-
- | -
-
- | 109.2ms
108.2ms
-0.9% 🟢 | 6.54ms
7.19ms
+10.0% 🔴 | 5.29ms
4.88ms
-7.9% 🟢 | 25.3ms
25.5ms
+0.9% 🔴 | 78.2ms
78.9ms
+0.9% 🔴 | | universal_router | -
-
- | -
-
- | 911.9ms
915.4ms
+0.4% 🔴 | 13.6ms
13.7ms
+0.6% 🔴 | 10.8ms
10.5ms
-2.1% 🟢 | 81.8ms
81.8ms
= | 318.8ms
322.6ms
+1.2% 🔴 | | aggregation_router_v6 | -
-
- | -
-
- | 2.709s
2.719s
+0.4% 🔴 | 23.0ms
21.8ms
-5.1% 🟢 | 18.3ms
16.8ms
-8.3% 🟢 | 139.2ms
139.5ms
+0.2% 🔴 | 530.5ms
529.7ms
-0.2% 🟢 | | entry_point_v0_7 | -
-
- | -
-
- | 1.386s
1.403s
+1.2% 🔴 | 10.6ms
10.6ms
-0.4% 🟢 | 10.0ms
9.33ms
-7.1% 🟢 | 63.6ms
63.9ms
+0.4% 🔴 | 269.4ms
273.4ms
+1.5% 🔴 | | uniswap_v3_pool | -
-
- | -
-
- | 491.0ms
492.6ms
+0.3% 🔴 | 21.7ms
21.6ms
-0.5% 🟢 | 15.8ms
16.0ms
+1.0% 🔴 | 111.5ms
112.5ms
+0.9% 🔴 | 405.0ms
408.1ms
+0.8% 🔴 | | uniswap_v4_pool_manager | -
-
- | -
-
- | 523.0ms
525.3ms
+0.4% 🔴 | 15.7ms
15.5ms
-1.3% 🟢 | 13.3ms
12.9ms
-3.4% 🟢 | 107.9ms
107.8ms
-0.1% 🟢 | 394.6ms
404.3ms
+2.5% 🔴 | | curve_3pool | -
-
- | -
-
- | 490.0ms
494.0ms
+0.8% 🔴 | 21.4ms
24.3ms
+13.6% 🔴 | 13.9ms
13.4ms
-3.5% 🟢 | 87.0ms
86.6ms
-0.5% 🟢 | 438.4ms
438.8ms
+0.1% 🔴 | | curve_tricrypto2 | -
-
- | -
-
- | 691.9ms
694.6ms
+0.4% 🔴 | 20.3ms
20.1ms
-1.0% 🟢 | 15.9ms
14.9ms
-6.8% 🟢 | 95.6ms
96.8ms
+1.2% 🔴 | 425.7ms
427.7ms
+0.5% 🔴 | | balancer_vault | -
-
- | -
-
- | 1.539s
1.565s
+1.7% 🔴 | 29.1ms
29.1ms
-0.1% 🟢 | 18.9ms
18.7ms
-1.5% 🟢 | 205.4ms
205.9ms
+0.2% 🔴 | 661.9ms
668.4ms
+1.0% 🔴 | | succinct_staking | -
-
- | -
-
- | 836.9ms
841.7ms
+0.6% 🔴 | 19.9ms
19.9ms
= | 11.4ms
11.2ms
-1.7% 🟢 | 87.7ms
88.9ms
+1.3% 🔴 | 318.9ms
323.4ms
+1.4% 🔴 | | **TOTAL** | **-
-
-** | **-
-
-** | **12.908s
12.992s
+0.7% 🔴** | **317.8ms
319.8ms
+0.6% 🔴** | **210.1ms
203.2ms
-3.3% 🟢** | **1.570s
1.575s
+0.4% 🔴** | **5.965s
6.020s
+0.9% 🔴** |
--- crates/jit/llvm/cpp/lib.cpp | 12 ++++++++++++ crates/jit/llvm/src/cpp.rs | 18 ++++++++++++++++++ crates/jit/llvm/src/lib.rs | 7 +++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/jit/llvm/cpp/lib.cpp b/crates/jit/llvm/cpp/lib.cpp index f749ea007..7d6d46030 100644 --- a/crates/jit/llvm/cpp/lib.cpp +++ b/crates/jit/llvm/cpp/lib.cpp @@ -35,6 +35,18 @@ extern "C" LLVMAttributeRef evm2_jit_llvm_create_initializes_attr(LLVMContextRef return wrap(Attribute::get(Ctx, Attribute::Initializes, ArrayRef(CR))); } +extern "C" LLVMAttributeRef evm2_jit_llvm_create_arg_mem_only_attr(LLVMContextRef C) { + return wrap(Attribute::getWithMemoryEffects(*unwrap(C), MemoryEffects::argMemOnly())); +} + +extern "C" LLVMAttributeRef evm2_jit_llvm_create_async_uwtable_attr(LLVMContextRef C) { + return wrap(Attribute::getWithUWTableKind(*unwrap(C), UWTableKind::Async)); +} + +extern "C" LLVMAttributeRef evm2_jit_llvm_create_no_captures_attr(LLVMContextRef C) { + return wrap(Attribute::getWithCaptureInfo(*unwrap(C), CaptureInfo::none())); +} + /// Remove a JITDylib from the ExecutionSession. extern "C" LLVMErrorRef evm2_jit_llvm_execution_session_remove_jit_dylib(LLVMOrcExecutionSessionRef ES, diff --git a/crates/jit/llvm/src/cpp.rs b/crates/jit/llvm/src/cpp.rs index f75fd6fd1..cfc14208e 100644 --- a/crates/jit/llvm/src/cpp.rs +++ b/crates/jit/llvm/src/cpp.rs @@ -20,6 +20,12 @@ unsafe extern "C" { upper: i64, ) -> LLVMAttributeRef; + fn evm2_jit_llvm_create_arg_mem_only_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + + fn evm2_jit_llvm_create_async_uwtable_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + + fn evm2_jit_llvm_create_no_captures_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + pub(crate) fn evm2_jit_llvm_jit_dylib_add_to_link_order( jd: LLVMOrcJITDylibRef, other: LLVMOrcJITDylibRef, @@ -70,3 +76,15 @@ pub(crate) fn set_dso_local(f: Function) { pub(crate) fn create_initializes_attr(cx: &Context, lower: i64, upper: i64) -> Attribute { Attribute::new(unsafe { evm2_jit_llvm_create_initializes_attr(cx.raw(), lower, upper) }) } + +pub(crate) fn create_arg_mem_only_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_arg_mem_only_attr(cx.raw()) }) +} + +pub(crate) fn create_async_uwtable_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_async_uwtable_attr(cx.raw()) }) +} + +pub(crate) fn create_no_captures_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_no_captures_attr(cx.raw()) }) +} diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 10ec18a02..45e3808da 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -2025,7 +2025,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::NoSync => ("nosync", AttrValue::Enum(0)), OurAttr::NoUnwind => ("nounwind", AttrValue::Enum(0)), OurAttr::NonLazyBind => ("nonlazybind", AttrValue::Enum(0)), - OurAttr::UWTable => ("uwtable", AttrValue::Enum(2)), + OurAttr::UWTable => return cpp::create_async_uwtable_attr(bcx.cx), OurAttr::AllFramePointers => ("frame-pointer", AttrValue::String("all")), OurAttr::NativeTargetCpu => ( "target-cpu", @@ -2042,7 +2042,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::Speculatable => ("speculatable", AttrValue::Enum(0)), OurAttr::NoAlias => ("noalias", AttrValue::Enum(0)), - OurAttr::NoCapture => ("captures", AttrValue::Enum(0)), // captures(none) - no capture + OurAttr::NoCapture => return cpp::create_no_captures_attr(bcx.cx), OurAttr::NoUndef => ("noundef", AttrValue::Enum(0)), OurAttr::Align(n) => ("align", AttrValue::Enum(n)), OurAttr::NonNull => ("nonnull", AttrValue::Enum(0)), @@ -2052,8 +2052,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::ReadOnly => ("readonly", AttrValue::Enum(0)), OurAttr::WriteOnly => ("writeonly", AttrValue::Enum(0)), OurAttr::Writable => ("writable", AttrValue::Enum(0)), - // memory(argmem: readwrite) = ModRef(3) << ArgMem(0) = 3. - OurAttr::ArgMemOnly => ("memory", AttrValue::Enum(3)), + OurAttr::ArgMemOnly => return cpp::create_arg_mem_only_attr(bcx.cx), // `u64::MAX` marks all reachable memory as dead; `0` would mark zero bytes. OurAttr::DeadOnReturn => ("dead_on_return", AttrValue::Enum(u64::MAX)), OurAttr::DeadOnUnwind => ("dead_on_unwind", AttrValue::Enum(0)), From 203f3e0b66a05f0a64c8abf3c71142687542ea1e Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:32:02 +0100 Subject: [PATCH 061/159] fix(bench): record real post state and valid gas limits in data fixtures (#928) Thirty-one of the 41 fixtures in `data/` had an all-zero post state root and logs hash, and most set a transaction gas limit of `0x7fffffffffffffff`, which Osaka rejects above its cap. The EEST runner compares the real root against `post.hash`, so none of them could pass as state tests, and three bench loaders (`benches/evm/fixture.rs`, `benches/evm/revm.rs`, `evm2 run`) clamped the gas limit to the Osaka cap to get around it. The bench sanity checks were weak for the same reason: the interpreter check discarded its result, and the JIT check compared only the status. This regenerates the 31 fixtures with the state root and logs hash that `evm2 replay` computes, and sets the gas limit of the Osaka ones to the cap (`0x1000000`). Fixtures for earlier forks keep their limits, which are valid for those forks. Every fixture ends in `Return` or `Stop`. All 41 pass `evm2 replay` on the interpreter, JIT and AOT. The three clamps are gone. The interpreter sanity check now asserts success, and the JIT check asserts success and compares the whole interpreter and JIT `TxResult` (status, gas, output, logs). `evm2 run --gas-limit` is now optional: given, it overrides the fixture's limit; otherwise fixtures use their own and bytecode defaults to the Osaka cap, where it previously defaulted to 1,000,000,000 and was silently clamped. Custom bytecode on a pre-Osaka fork now runs with the lower default unless `--gas-limit` is passed. No CodSpeed-visible change is expected. The Osaka fixtures that were clamped to the cap now carry that limit, so each benchmark executes the same transaction. The new assertions run in the setup pass, outside the timed loops. --- crates/cli/benches/evm/fixture.rs | 10 +-- crates/cli/benches/evm/jit.rs | 11 +-- crates/cli/benches/evm/revm.rs | 9 +-- crates/cli/benches/evm/support.rs | 9 ++- crates/cli/src/run.rs | 25 +++--- data/airdrop.json | 6 +- data/bswap64.json | 6 +- data/bswap64_opt.json | 6 +- data/burntpix.json | 4 +- data/counter.json | 6 +- data/curve-stableswap-2pool.json | 4 +- data/eip2935.json | 6 +- data/eip4788.json | 6 +- data/erc20_transfer.json | 6 +- data/factorial.json | 6 +- data/fiat_token.json | 6 +- data/fibonacci-calldata.json | 6 +- data/hash_10k.json | 6 +- data/onchain-lm-v2.json | 4 +- data/precompile-blake2.json | 40 +++++----- data/precompile-bls12-381.json | 92 +++++++++++------------ data/precompile-bn254.json | 24 +++--- data/precompile-ecrecover.json | 8 +- data/precompile-identity.json | 20 ++--- data/precompile-kzg-point-evaluation.json | 8 +- data/precompile-modexp.json | 24 +++--- data/precompile-p256verify.json | 8 +- data/precompile-ripemd160.json | 24 +++--- data/precompile-sha256.json | 24 +++--- data/push0_proxy.json | 6 +- data/seaport.json | 6 +- data/snailtracer.json | 4 +- data/uniswap_v2_pair.json | 6 +- data/univ2_router.json | 6 +- data/usdc_proxy.json | 6 +- data/weth.json | 6 +- 36 files changed, 227 insertions(+), 227 deletions(-) diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index afd5b88ce..b7dca9988 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -3,7 +3,7 @@ use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::Bytes; use alloy_primitives::{B256, TxKind, U256}; use evm2::{ - SpecId, Version, + SpecId, bytecode::Bytecode, env::{BlockEnv, BlockEnvExt}, ethereum::{RecoveredTxEnvelope, TxEnvelope}, @@ -123,12 +123,12 @@ impl Case<'_> { db } - pub(crate) fn tx(&self, spec: SpecId) -> RecoveredTxEnvelope { + pub(crate) fn tx(&self) -> RecoveredTxEnvelope { let raw = &self.unit.transaction; let indexes = self.post.indexes; - let gas_limit = - raw.gas_limit.get(indexes.gas).copied().expect("fixture gas index must exist"); - let gas_limit = u64_value(gas_limit).min(Version::base(spec).tx_gas_limit_cap); + let gas_limit = u64_value( + raw.gas_limit.get(indexes.gas).copied().expect("fixture gas index must exist"), + ); let tx = TxLegacy { chain_id: None, nonce: u64_value(raw.nonce), diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index 3e682b3c9..40c10fa28 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -101,7 +101,7 @@ impl PreparedBench { spec, block: case.block(), db: case.state(), - tx: case.tx(spec), + tx: case.tx(), entry_bytecode: case.entry_bytecode(), functions: Arc::new(functions), }) @@ -114,11 +114,12 @@ impl PreparedBench { let jit = self.run_jit().unwrap_or_else(|err| { panic!("{} JIT benchmark transaction must execute: {err:?}", self.name) }); - assert_eq!( - interpreter.status, jit.status, - "{} interpreter and JIT status differ", - self.name + assert!( + interpreter.status, + "{} benchmark transaction must succeed: {:?}", + self.name, interpreter.stop ); + assert_eq!(interpreter, jit, "{} interpreter and JIT results differ", self.name); } pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { diff --git a/crates/cli/benches/evm/revm.rs b/crates/cli/benches/evm/revm.rs index 7df14dd79..3929e158e 100644 --- a/crates/cli/benches/evm/revm.rs +++ b/crates/cli/benches/evm/revm.rs @@ -1,12 +1,12 @@ use crate::fixture::Suites; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; -use evm2::{SpecId, Version}; +use evm2::SpecId; use evm2_cli::evm_bench::BenchCase; use revm::{ ExecuteEvm, MainBuilder, MainContext, context::{BlockEnv, CfgEnv, Context, TxEnv}, database::{CacheDB, EmptyDB, InMemoryDB}, - primitives::{U256, hardfork::SpecId as RevmSpecId}, + primitives::hardfork::SpecId as RevmSpecId, statetest_types::{Test, TestSuite, TestUnit}, }; use std::{borrow::Cow, sync::Arc}; @@ -108,11 +108,6 @@ fn revm_case(input: &str, name: &str, spec: SpecId) -> (TestUnit, Test) { .map(|(_, tests)| tests) .expect("fixture suite must contain revm post test"); let test = tests.remove(0); - let gas_limit = &mut unit.transaction.gas_limit[test.indexes.gas]; - let cap = U256::from(Version::base(spec).tx_gas_limit_cap); - if *gas_limit > cap { - *gas_limit = cap; - } (unit, test) } diff --git a/crates/cli/benches/evm/support.rs b/crates/cli/benches/evm/support.rs index 1ae48a22d..a18e6024b 100644 --- a/crates/cli/benches/evm/support.rs +++ b/crates/cli/benches/evm/support.rs @@ -30,15 +30,20 @@ impl PreparedBench { spec, block: case.block(), db: case.state(), - tx: case.tx(spec), + tx: case.tx(), } } pub(crate) fn sanity_check(&self) { let mut runner = Runner::new(self); - let _ = runner.run().unwrap_or_else(|err| { + let result = runner.run().unwrap_or_else(|err| { panic!("{} benchmark transaction must execute: {err:?}", self.name) }); + assert!( + result.status, + "{} benchmark transaction must succeed: {:?}", + self.name, result.stop + ); } pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 4719356d2..bc332de84 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -100,8 +100,9 @@ pub(crate) struct RunArgs { /// Disable frame pointers in the compiled function. #[arg(long)] no_frame_pointers: bool, - #[arg(long, default_value = "1000000000")] - gas_limit: u64, + /// Transaction gas limit. Defaults to the fixture's, or the Osaka cap for bytecode. + #[arg(long)] + gas_limit: Option, } impl RunArgs { @@ -464,7 +465,7 @@ struct Bench { source: BenchSource, spec_id: SpecId, calldata: Option>, - gas_limit: u64, + gas_limit: Option, assert_success: bool, } @@ -484,7 +485,7 @@ impl Bench { source: BenchSource::Fixture(bench.fixture_path), spec_id: spec, calldata: None, - gas_limit: u64::MAX, + gas_limit: None, assert_success: true, }) } @@ -514,7 +515,7 @@ fn fixture_from_bytecode( bytecode: Vec, spec_id: SpecId, calldata: Option>, - gas_limit: u64, + gas_limit: Option, ) -> Bench { Bench { name: name.into(), @@ -725,7 +726,8 @@ fn parse_bytecode_bench(bench: &Bench, bytecode: &[u8]) -> eyre::Result gas_limit, + None => u64_value(gas_limit)?, + }; let value = *raw .value .get(indexes.value) @@ -826,12 +831,6 @@ fn build_fixture_tx( Ok(Recovered::new_unchecked(TxEnvelope::Legacy(tx), caller)) } -impl Bench { - fn gas_limit(&self) -> u64 { - self.gas_limit.min(Version::base(self.spec_id).tx_gas_limit_cap) - } -} - fn u128_value(value: U256) -> eyre::Result { value.try_into().map_err(|_| eyre::eyre!("fixture value overflows u128")) } diff --git a/data/airdrop.json b/data/airdrop.json index c3017cf5f..0d52a802f 100644 --- a/data/airdrop.json +++ b/data/airdrop.json @@ -26,7 +26,7 @@ "0x5c975abb" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdf3a11531397647396a4774087e1b0811e6e10bdbf39088917241a12bc6da4b7", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/bswap64.json b/data/bswap64.json index 1ce02b4af..0361b57e0 100644 --- a/data/bswap64.json +++ b/data/bswap64.json @@ -26,7 +26,7 @@ "0xff2f79f10000000000000000000000000000000000000000000000000000000000000102" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x7f29c37dbfd1d49b0596a5443125add9efe700f74f2a87ba048c9137d742a22b", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/bswap64_opt.json b/data/bswap64_opt.json index 049111b27..6812dc9c5 100644 --- a/data/bswap64_opt.json +++ b/data/bswap64_opt.json @@ -26,7 +26,7 @@ "0xff2f79f10000000000000000000000000000000000000000000000000000000000000102" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdc36c0d0f84fc01e8dd1fdcae87f6137b0e048c027cf37f0113baf62184fbbbe", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/burntpix.json b/data/burntpix.json index 7a1ce3e4a..bec4bf6fc 100644 --- a/data/burntpix.json +++ b/data/burntpix.json @@ -78,8 +78,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x8424dec58ec41225ad9d8f98e05962103b581ee5d8e026560cd58bceb4c79037", + "logs": "0xec16cb4f899a9811f7a842392c0663fb7dc752d2c6a4ebf24040291be4c05700" } ] } diff --git a/data/counter.json b/data/counter.json index 97fea5e8a..b3259052a 100644 --- a/data/counter.json +++ b/data/counter.json @@ -26,7 +26,7 @@ "0xd09de08a" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x0a40f0c6a8da740f9cc742f23c18f71b5ed54c8824157ede1cb3778da52fe0b1", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/curve-stableswap-2pool.json b/data/curve-stableswap-2pool.json index 4d4a6977a..339d84831 100644 --- a/data/curve-stableswap-2pool.json +++ b/data/curve-stableswap-2pool.json @@ -86,8 +86,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xe1b7ab78dab505a9c9526c52fa8d3b07ddce732c624ce835d467dc2d1ed5069b", + "logs": "0xe433310b26e9377e85fd586c3b503c09fed9d91e34e1a85b58b3e18d6ed73f93" } ] } diff --git a/data/eip2935.json b/data/eip2935.json index 431118558..6f9e838f8 100644 --- a/data/eip2935.json +++ b/data/eip2935.json @@ -28,7 +28,7 @@ "0x0000000000000000000000000000000000000000000000000000000000000001" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x13ed5357d9842645c6f78e84eb61be5033b5810857f43597e9c0617c77916707", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/eip4788.json b/data/eip4788.json index a06cfa5de..2349ae2c5 100644 --- a/data/eip4788.json +++ b/data/eip4788.json @@ -29,7 +29,7 @@ "0x0000000000000000000000000000000000000000000000000000000069c32393" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -49,8 +49,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xac30e699719355145636f52b3ad493fb8a5ef8f966818ee34c421898e9f82648", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/erc20_transfer.json b/data/erc20_transfer.json index 1916e1562..9ee485317 100644 --- a/data/erc20_transfer.json +++ b/data/erc20_transfer.json @@ -26,7 +26,7 @@ "0x30627b7c" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdbf50f40e7f142ff5f8a947c46a98bbabfc37351145dfdb73dee3ebac3879e73", + "logs": "0x173caeedb3a7168fde25244b48d9e11e231df14c2a41179954d3993d66e56332" } ] } diff --git a/data/factorial.json b/data/factorial.json index 83a024981..014bb7e99 100644 --- a/data/factorial.json +++ b/data/factorial.json @@ -26,7 +26,7 @@ "0x00000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xee622c8e979017debf38b42d2cd7cbd9de7bd84d44341ec51ec118e7852d6c01", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/fiat_token.json b/data/fiat_token.json index 3f67e61a0..2974d4956 100644 --- a/data/fiat_token.json +++ b/data/fiat_token.json @@ -26,7 +26,7 @@ "0x313ce567" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x4b179ee014516854f22d175fb53d72309f502dd0f4c16f4042af45e6f9adc0f5", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/fibonacci-calldata.json b/data/fibonacci-calldata.json index 2926f174b..889dbde35 100644 --- a/data/fibonacci-calldata.json +++ b/data/fibonacci-calldata.json @@ -26,7 +26,7 @@ "0x00000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x6331b8166430337d838f03a97ab8cb51a78d8d866d9c9bf079959ad41bff5e90", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/hash_10k.json b/data/hash_10k.json index 74075e19d..aee2fcaa1 100644 --- a/data/hash_10k.json +++ b/data/hash_10k.json @@ -26,7 +26,7 @@ "0x30627b7c" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x4896f5b04bea1cffbce29379d8cdc0958d46efadb0a6045ab1762217cb5f3114", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/onchain-lm-v2.json b/data/onchain-lm-v2.json index cb22d9e3f..894e80145 100644 --- a/data/onchain-lm-v2.json +++ b/data/onchain-lm-v2.json @@ -277,8 +277,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x306f35f5f4ca399c283a24a310185458d8bebb371282aa2f10c7a4004dd39fef", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/precompile-blake2.json b/data/precompile-blake2.json index 8c942fc58..3327ad085 100644 --- a/data/precompile-blake2.json +++ b/data/precompile-blake2.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -293,13 +293,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -340,13 +340,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -387,13 +387,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -434,13 +434,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-bls12-381.json b/data/precompile-bls12-381.json index c040bab0f..891f0daa8 100644 --- a/data/precompile-bls12-381.json +++ b/data/precompile-bls12-381.json @@ -11,13 +11,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -293,13 +293,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -340,13 +340,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -387,13 +387,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -434,13 +434,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -481,13 +481,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -528,13 +528,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -575,13 +575,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -622,13 +622,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -669,13 +669,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -716,13 +716,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -763,13 +763,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -810,13 +810,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -857,13 +857,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -904,13 +904,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -951,13 +951,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -998,13 +998,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -1045,13 +1045,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-bn254.json b/data/precompile-bn254.json index e8047aa0a..48256aafa 100644 --- a/data/precompile-bn254.json +++ b/data/precompile-bn254.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-ecrecover.json b/data/precompile-ecrecover.json index 258e4cdb7..3bcf39399 100644 --- a/data/precompile-ecrecover.json +++ b/data/precompile-ecrecover.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-identity.json b/data/precompile-identity.json index 62d2dc93d..f1fdee793 100644 --- a/data/precompile-identity.json +++ b/data/precompile-identity.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-kzg-point-evaluation.json b/data/precompile-kzg-point-evaluation.json index 300234108..83de10741 100644 --- a/data/precompile-kzg-point-evaluation.json +++ b/data/precompile-kzg-point-evaluation.json @@ -11,13 +11,13 @@ "Cancun": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Cancun": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-modexp.json b/data/precompile-modexp.json index 55b2d1a30..93cb1ffdf 100644 --- a/data/precompile-modexp.json +++ b/data/precompile-modexp.json @@ -11,13 +11,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-p256verify.json b/data/precompile-p256verify.json index a51534d25..0f44ee41b 100644 --- a/data/precompile-p256verify.json +++ b/data/precompile-p256verify.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-ripemd160.json b/data/precompile-ripemd160.json index c58c2f9d9..f076ad929 100644 --- a/data/precompile-ripemd160.json +++ b/data/precompile-ripemd160.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-sha256.json b/data/precompile-sha256.json index 57cd67c2e..d556f3c19 100644 --- a/data/precompile-sha256.json +++ b/data/precompile-sha256.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/push0_proxy.json b/data/push0_proxy.json index 70be8faca..6cf8896fd 100644 --- a/data/push0_proxy.json +++ b/data/push0_proxy.json @@ -26,7 +26,7 @@ "0x" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x44b14b1e0aca7f0259055b7fc43f8db20ce53f748096bd63781c00cee651c06e", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/seaport.json b/data/seaport.json index e2a3278fa..7c05fa2e3 100644 --- a/data/seaport.json +++ b/data/seaport.json @@ -28,7 +28,7 @@ "0x46423aa70123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x74332c5431cbce90a512d9826384c400f1a0b93665e82418fb2dff53e94869d0", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/snailtracer.json b/data/snailtracer.json index 8c78ff5f2..da96debb3 100644 --- a/data/snailtracer.json +++ b/data/snailtracer.json @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x0e1e0b07fefadcf18149b0a7307d4c70d63bbdd88e3403749eb299e89dc7937f", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/uniswap_v2_pair.json b/data/uniswap_v2_pair.json index 4a5e8d5a1..7ff402332 100644 --- a/data/uniswap_v2_pair.json +++ b/data/uniswap_v2_pair.json @@ -26,7 +26,7 @@ "0x0902f1ac" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xd9bdd4b0e4ba2763150008c869ae47512baf54e7d7950d6db2cefed3938649ca", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/univ2_router.json b/data/univ2_router.json index 4bde67f50..da558bbbb 100644 --- a/data/univ2_router.json +++ b/data/univ2_router.json @@ -26,7 +26,7 @@ "0x054d50d400000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000174876e8000000000000000000000000000000000000000000000000000000000ba43b7400" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xa8ce14d3936f11af4d2a568098debc1a043800dba6ae8fc9274adde376d85cbd", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/usdc_proxy.json b/data/usdc_proxy.json index f2338318d..8324b1fed 100644 --- a/data/usdc_proxy.json +++ b/data/usdc_proxy.json @@ -26,7 +26,7 @@ "0x" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xe543886e90abb4ab88976bc4036019f1cb5d35b0d2ec34a698974dadce8fa76b", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/weth.json b/data/weth.json index e8d5f710f..84b1cf117 100644 --- a/data/weth.json +++ b/data/weth.json @@ -28,7 +28,7 @@ "0xa9059cbb000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x5857a425635f19db7cedcf0f043d246fa048f189b80c07667989f2bd95e91fb7", + "logs": "0xb2ea7219aa8e8235c1c4871a071613dbfb0e943af175a1d1a28b253740a1785b" } ] } From 32c73d34a7765c56867af0767ac9c711b4fb67b4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:32:12 +0100 Subject: [PATCH 062/159] fix(jit): default debug support and simple perf map to off (#932) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `BackendConfig` defaulted `debug_support` and `simple_perf` to true. The ORC JIT applies these flags once per process, so the first compiler to run decided for the whole process, and any path that forgot to override them enabled debug object copies and the append-only `/tmp/perf-.map`, which the docs call unsuitable for long-running programs. The runtime and both codegen test harnesses all overrode the defaults, and nothing in the CLI or dump code opts in. Both flags now default to false, the overrides are gone, and `backend_config_defaults_match_runtime_settings` asserts the new defaults. Callers that want either flag still set it through `set_debug_support` / `set_simple_perf`. ## Benchmarks Codegen is unchanged: `opt.s` changed in 0/31 benchmarks. Head `66003c7d54` with uncommitted changes vs base `origin/main` (`66003c7d54`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **+7.7% 🔴** | **+6.6% 🔴** | | **GEOMEAN** | **=** | **+0.1% 🔴** | **+0.9% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|-----------------------------------:|--------------------------------:|-----------------------------------:| | fibonacci-calldata | 37.49M
37.44M
-0.1% 🟢 | 46.33M
46.29M
-0.1% 🟢 | 18.8ms
18.0ms
-4.5% 🟢 | 571.8µs
304.8µs
-46.7% 🟢 | 1.76ms
615.5µs
-65.0% 🟢 | 6.51ms
2.77ms
-57.4% 🟢 | 5.89ms
5.46ms
-7.2% 🟢 | | factorial | 41.63M
41.64M
= | 47.45M
51.04M
+7.6% 🔴 | 18.1ms
19.2ms
+6.2% 🔴 | 346.8µs
408.4µs
+17.8% 🔴 | 720.1µs
649.5µs
-9.8% 🟢 | 2.48ms
2.39ms
-3.7% 🟢 | 6.22ms
5.94ms
-4.5% 🟢 | | counter | 54.21M
54.13M
-0.1% 🟢 | 65.31M
54.70M
-16.3% 🟢 | 18.5ms
16.8ms
-9.5% 🟢 | 684.5µs
575.7µs
-15.9% 🟢 | 929.4µs
756.0µs
-18.7% 🟢 | 2.92ms
2.72ms
-6.8% 🟢 | 7.40ms
7.65ms
+3.4% 🔴 | | snailtracer | 5.29G
5.29G
-0.1% 🟢 | 3.70G
4.00G
+8.2% 🔴 | 856.4ms
934.5ms
+9.1% 🔴 | 160.6ms
61.7ms
-61.6% 🟢 | 9.21ms
12.7ms
+37.6% 🔴 | 95.4ms
117.1ms
+22.8% 🔴 | 522.8ms
484.9ms
-7.2% 🟢 | | weth | 538.83M
538.70M
= | 403.28M
387.03M
-4.0% 🟢 | 100.5ms
94.9ms
-5.6% 🟢 | 3.02ms
4.80ms
+59.2% 🔴 | 2.51ms
3.57ms
+42.6% 🔴 | 15.4ms
21.3ms
+38.0% 🔴 | 45.5ms
62.5ms
+37.2% 🔴 | | hash_10k | 80.41M
80.42M
= | 81.23M
75.27M
-7.3% 🟢 | 22.4ms
21.7ms
-3.0% 🟢 | 397.6µs
626.8µs
+57.7% 🔴 | 667.0µs
838.9µs
+25.8% 🔴 | 2.70ms
3.83ms
+41.9% 🔴 | 6.96ms
11.4ms
+63.9% 🔴 | | erc20_transfer | 577.14M
576.99M
= | 429.82M
381.94M
-11.1% 🟢 | 106.1ms
95.4ms
-10.1% 🟢 | 3.35ms
4.93ms
+47.1% 🔴 | 3.20ms
3.64ms
+13.7% 🔴 | 17.4ms
17.4ms
= | 53.5ms
61.8ms
+15.6% 🔴 | | push0_proxy | 34.70M
34.69M
= | 41.01M
40.44M
-1.4% 🟢 | 16.2ms
16.7ms
+2.8% 🔴 | 287.2µs
369.6µs
+28.7% 🔴 | 599.5µs
638.6µs
+6.5% 🔴 | 1.15ms
2.28ms
+98.8% 🔴 | 3.21ms
5.99ms
+86.8% 🔴 | | usdc_proxy | 244.06M
244.03M
= | 179.88M
174.20M
-3.2% 🟢 | 46.0ms
47.4ms
+3.0% 🔴 | 2.83ms
2.67ms
-5.6% 🟢 | 2.80ms
2.30ms
-17.8% 🟢 | 8.84ms
9.76ms
+10.4% 🔴 | 19.1ms
21.4ms
+11.7% 🔴 | | fiat_token | 3.97G
3.98G
= | 2.06G
2.14G
+3.9% 🔴 | 481.5ms
501.9ms
+4.2% 🔴 | 17.8ms
29.6ms
+66.3% 🔴 | 13.8ms
14.7ms
+7.1% 🔴 | 91.2ms
152.0ms
+66.8% 🔴 | 327.5ms
418.1ms
+27.7% 🔴 | | uniswap_v2_pair | 2.06G
2.06G
= | 1.63G
1.42G
-13.0% 🟢 | 368.7ms
340.8ms
-7.6% 🟢 | 9.50ms
16.8ms
+77.1% 🔴 | 6.65ms
11.4ms
+72.0% 🔴 | 44.1ms
87.6ms
+98.6% 🔴 | 183.4ms
252.7ms
+37.8% 🔴 | | univ2_router | 4.39G
4.39G
= | 3.11G
4.25G
+36.9% 🔴 | 720.9ms
959.4ms
+33.1% 🔴 | 18.5ms
22.5ms
+21.7% 🔴 | 12.3ms
15.6ms
+26.9% 🔴 | 97.8ms
136.4ms
+39.4% 🔴 | 416.4ms
539.8ms
+29.6% 🔴 | | seaport | 9.73G
9.73G
= | 8.18G
12.79G
+56.3% 🔴 | 1.714s
2.866s
+67.3% 🔴 | 36.0ms
47.2ms
+30.9% 🔴 | 23.0ms
32.6ms
+41.5% 🔴 | 340.0ms
326.0ms
-4.1% 🟢 | 1.173s
1.195s
+1.9% 🔴 | | airdrop | 1.42G
1.42G
= | 1.19G
1.03G
-13.5% 🟢 | 284.9ms
242.9ms
-14.7% 🟢 | 7.03ms
10.8ms
+53.8% 🔴 | 7.62ms
7.14ms
-6.3% 🟢 | 44.2ms
48.5ms
+9.9% 🔴 | 157.5ms
129.4ms
-17.8% 🟢 | | bswap64 | 67.08M
66.97M
-0.2% 🟢 | 62.92M
71.41M
+13.5% 🔴 | 19.2ms
24.5ms
+27.5% 🔴 | 1.01ms
790.6µs
-21.7% 🟢 | 1.17ms
695.1µs
-40.3% 🟢 | 2.90ms
2.30ms
-20.7% 🟢 | 6.65ms
6.11ms
-8.1% 🟢 | | bswap64_opt | 73.78M
73.70M
-0.1% 🟢 | 70.73M
79.76M
+12.8% 🔴 | 21.7ms
23.1ms
+6.6% 🔴 | 933.6µs
890.2µs
-4.7% 🟢 | 891.0µs
888.0µs
-0.3% 🟢 | 2.78ms
3.03ms
+9.1% 🔴 | 5.08ms
7.17ms
+41.1% 🔴 | | eip4788 | 48.59M
48.52M
-0.2% 🟢 | 59.40M
57.75M
-2.8% 🟢 | 18.6ms
17.6ms
-5.2% 🟢 | 349.3µs
630.8µs
+80.6% 🔴 | 541.7µs
655.7µs
+21.0% 🔴 | 1.53ms
2.16ms
+41.0% 🔴 | 4.73ms
6.16ms
+30.3% 🔴 | | eip2935 | 42.35M
42.33M
-0.1% 🟢 | 58.30M
54.27M
-6.9% 🟢 | 21.3ms
21.5ms
+0.8% 🔴 | 374.0µs
351.3µs
-6.1% 🟢 | 654.1µs
488.4µs
-25.3% 🟢 | 1.57ms
1.41ms
-10.6% 🟢 | 5.47ms
4.40ms
-19.5% 🟢 | | burntpix | 6.70G
6.70G
= | 5.35G
5.46G
+2.0% 🔴 | 1.213s
1.245s
+2.6% 🔴 | 18.2ms
26.4ms
+44.5% 🔴 | 2.90ms
2.65ms
-8.7% 🟢 | 18.3ms
14.0ms
-23.8% 🟢 | 55.5ms
44.3ms
-20.2% 🟢 | | curve_stableswap | 1.14G
1.14G
= | 892.51M
844.82M
-5.3% 🟢 | 213.9ms
202.4ms
-5.4% 🟢 | 4.81ms
5.33ms
+10.9% 🔴 | 3.70ms
3.10ms
-16.1% 🟢 | 34.9ms
26.6ms
-23.8% 🟢 | 67.4ms
66.7ms
-1.1% 🟢 | | onchain_lm_v2 | 3.65G
3.65G
= | 3.02G
2.96G
-2.1% 🟢 | 718.8ms
686.8ms
-4.4% 🟢 | 17.3ms
22.8ms
+31.7% 🔴 | 8.79ms
13.7ms
+56.1% 🔴 | 67.7ms
60.9ms
-10.1% 🟢 | 263.7ms
285.0ms
+8.1% 🔴 | | usdt | 1.34G
1.34G
-0.1% 🟢 | 980.81M
938.08M
-4.4% 🟢 | 231.6ms
232.2ms
+0.3% 🔴 | 12.0ms
16.0ms
+34.0% 🔴 | 10.2ms
9.18ms
-9.8% 🟢 | 44.4ms
48.5ms
+9.1% 🔴 | 155.6ms
147.5ms
-5.2% 🟢 | | universal_router | 11.56G
11.56G
= | 9.44G
9.40G
-0.5% 🟢 | 2.612s
2.209s
-15.4% 🟢 | 31.2ms
37.5ms
+20.3% 🔴 | 24.1ms
29.6ms
+22.8% 🔴 | 191.8ms
251.1ms
+30.9% 🔴 | 600.0ms
752.9ms
+25.5% 🔴 | | aggregation_router_v6 | 35.12G
35.12G
= | 26.63G
30.96G
+16.3% 🔴 | 6.179s
7.082s
+14.6% 🔴 | 48.8ms
61.4ms
+26.0% 🔴 | 35.5ms
41.5ms
+16.7% 🔴 | 223.6ms
210.7ms
-5.8% 🟢 | 855.3ms
1.114s
+30.2% 🔴 | | entry_point_v0_7 | 17.89G
17.89G
= | 16.63G
15.85G
-4.7% 🟢 | 3.580s
3.645s
+1.8% 🔴 | 14.8ms
26.5ms
+78.9% 🔴 | 11.8ms
23.0ms
+94.3% 🔴 | 89.8ms
215.3ms
+139.8% 🔴 | 406.8ms
497.0ms
+22.2% 🔴 | | uniswap_v3_pool | 6.20G
6.20G
= | 5.84G
5.40G
-7.5% 🟢 | 1.343s
1.244s
-7.4% 🟢 | 40.5ms
51.0ms
+25.7% 🔴 | 23.0ms
36.0ms
+56.0% 🔴 | 243.6ms
305.2ms
+25.3% 🔴 | 625.5ms
838.8ms
+34.1% 🔴 | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 5.11G
4.51G
-11.8% 🟢 | 1.149s
1.060s
-7.7% 🟢 | 34.9ms
35.5ms
+1.7% 🔴 | 27.4ms
29.4ms
+7.1% 🔴 | 283.5ms
210.0ms
-25.9% 🟢 | 834.2ms
1.070s
+28.3% 🔴 | | curve_3pool | 6.59G
6.59G
= | 5.03G
3.99G
-20.7% 🟢 | 1.185s
912.9ms
-23.0% 🟢 | 56.0ms
49.5ms
-11.7% 🟢 | 25.8ms
29.9ms
+15.7% 🔴 | 225.0ms
396.2ms
+76.1% 🔴 | 1.056s
1.223s
+15.8% 🔴 | | curve_tricrypto2 | 9.40G
9.40G
= | 6.72G
5.80G
-13.6% 🟢 | 1.581s
1.342s
-15.1% 🟢 | 44.0ms
45.0ms
+2.2% 🔴 | 33.3ms
35.3ms
+5.9% 🔴 | 506.4ms
212.2ms
-58.1% 🟢 | 771.0ms
627.3ms
-18.6% 🟢 | | balancer_vault | 19.04G
19.05G
= | 13.08G
16.50G
+26.2% 🔴 | 2.979s
3.617s
+21.4% 🔴 | 62.6ms
35.1ms
-43.8% 🟢 | 51.4ms
29.3ms
-43.0% 🟢 | 503.6ms
397.4ms
-21.1% 🟢 | 1.458s
1.514s
+3.8% 🔴 | | succinct_staking | 10.84G
10.85G
= | 8.10G
8.40G
+3.8% 🔴 | 1.876s
1.936s
+3.2% 🔴 | 46.2ms
117.8ms
+154.7% 🔴 | 22.6ms
46.9ms
+107.9% 🔴 | 165.7ms
277.0ms
+67.2% 🔴 | 825.6ms
658.3ms
-20.3% 🟢 | | **TOTAL** | **164.88G
164.88G
=** | **128.23G
138.12G
+7.7% 🔴** | **29.715s
31.677s
+6.6% 🔴** | **695.0ms
735.7ms
+5.9% 🔴** | **369.6ms
439.3ms
+18.9% 🔴** | **3.377s
3.564s
+5.5% 🔴** | **10.925s
12.065s
+10.4% 🔴** |
--- crates/jit/backend/src/traits.rs | 8 ++++---- crates/jit/codegen/src/compiler/mod.rs | 4 ++-- crates/jit/codegen/src/tests/mod.rs | 2 -- crates/jit/codegen/tests.rs | 2 -- crates/jit/runtime/src/runtime/backend.rs | 13 +------------ crates/jit/runtime/src/runtime/worker.rs | 3 --- 6 files changed, 7 insertions(+), 25 deletions(-) diff --git a/crates/jit/backend/src/traits.rs b/crates/jit/backend/src/traits.rs index ffb155323..6af8d41f1 100644 --- a/crates/jit/backend/src/traits.rs +++ b/crates/jit/backend/src/traits.rs @@ -48,9 +48,9 @@ impl Default for BackendConfig { opt_level: OptimizationLevel::Default, is_dumping: false, debug_assertions: cfg!(debug_assertions), - debug_support: true, + debug_support: false, profiling_support: false, - simple_perf: true, + simple_perf: false, debug_file: None, } } @@ -527,9 +527,9 @@ mod tests { assert_eq!(config.opt_level, OptimizationLevel::Default); assert!(!config.is_dumping); assert_eq!(config.debug_assertions, cfg!(debug_assertions)); - assert!(config.debug_support); + assert!(!config.debug_support); assert!(!config.profiling_support); - assert!(config.simple_perf); + assert!(!config.simple_perf); assert_eq!(config.debug_file, None); } diff --git a/crates/jit/codegen/src/compiler/mod.rs b/crates/jit/codegen/src/compiler/mod.rs index 3d10dca0f..ec4f48503 100644 --- a/crates/jit/codegen/src/compiler/mod.rs +++ b/crates/jit/codegen/src/compiler/mod.rs @@ -319,7 +319,7 @@ impl EvmCompiler { /// This setting is applied once per process on first JIT compilation. /// Subsequent compilers inherit the value set by the first. /// - /// Defaults to `true`. + /// Defaults to `false`. pub fn debug_support(&self) -> bool { self.backend.config().debug_support } @@ -366,7 +366,7 @@ impl EvmCompiler { /// This setting is applied once per process on first JIT compilation. /// Subsequent compilers inherit the value set by the first. /// - /// Defaults to `true`. + /// Defaults to `false`. pub fn simple_perf(&self) -> bool { self.backend.config().simple_perf } diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index b9563e2f5..9a7501b31 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -65,8 +65,6 @@ pub fn with_jit_compiler( init_tracing(); let mut compiler = EvmCompiler::new_llvm(false).unwrap(); compiler.set_opt_level(opt_level); - compiler.set_profiling_support(false); - compiler.set_simple_perf(false); compiler.set_single_error(false); f(&mut compiler) } diff --git a/crates/jit/codegen/tests.rs b/crates/jit/codegen/tests.rs index 6c6e59039..1ce219d35 100644 --- a/crates/jit/codegen/tests.rs +++ b/crates/jit/codegen/tests.rs @@ -111,8 +111,6 @@ fn compile_test(path: &Path, build_dir: &Path, test_name: &str) { let mut compiler = EvmCompiler::new_llvm(false).unwrap(); compiler.set_opt_level(OptimizationLevel::Default); - compiler.set_profiling_support(false); - compiler.set_simple_perf(false); compiler.set_module_name(test_name); compiler.set_dump_to(Some(build_dir.to_path_buf())); unsafe { diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index dea7df93f..fcc76a28c 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -35,8 +35,6 @@ use std::{ #[cfg(feature = "llvm")] use crate::llvm::jit_memory_usage; #[cfg(feature = "llvm")] -use evm2_jit_backend::{Backend, BackendConfig}; -#[cfg(feature = "llvm")] use evm2_jit_context::RawEvmCompilerFn; /// The resident map type: code_hash+spec_id → compiled program. @@ -91,16 +89,7 @@ impl JitObjectLinker { ) -> eyre::Result<(EvmCompilerFn, Arc)> { let backend = match &mut self.backend { Some(backend) => backend, - None => { - let mut backend = crate::EvmLlvmBackend::new(false)?; - // Long-running nodes cannot afford debug object copies or the append-only perf map. - backend.apply_config(BackendConfig { - debug_support: false, - simple_perf: false, - ..backend.config().clone() - }); - self.backend.insert(backend) - } + None => self.backend.insert(crate::EvmLlvmBackend::new(false)?), }; let symbol_name = CString::new(symbol_name)?; diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index 2fd2dbc8d..eb4e05b49 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -638,9 +638,6 @@ pub(super) fn create_compiler( } else { config.tuning.jit_opt_level }); - // Long-running nodes cannot afford the debug object copies or the append-only perf map. - compiler.set_debug_support(false); - compiler.set_simple_perf(false); compiler.set_debug_assertions(config.debug_assertions); compiler.set_single_error(config.single_error); compiler.set_dedup(!config.no_dedup); From d4e24e46867a81a56129433c75de8026a7c1274e Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:34:28 +0100 Subject: [PATCH 063/159] fix(cli): map only the entry contract in run --aot --load (#917) `evm2 run --aot --load` compiles only the entry contract into the object, but the loader mapped the one exported symbol to every account that has code. In multi-contract fixtures the callees therefore ran the entry contract's machine code. The loader now maps only the entry contract's code hash to the symbol; other contracts run on the interpreter, which is the simplest correct choice since the library never contained them. The single-iteration sanity check compared status only. It now also compares output and gas spent (gas is skipped under `--no-gas`, where it differs by design), so a wrong mapping fails the check. With the old mapping and the stronger check, `run --aot --load` fails on universal_router, burntpix, aggregation_router_v6, entry_point_v0_7, curve_tricrypto2, balancer_vault and succinct_staking (the loaded code reverts where the interpreter succeeds). With the fix all of them, plus usdc_proxy and univ2_router, pass. --- crates/cli/src/run.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index bc332de84..5941a5707 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -170,9 +170,9 @@ impl RunArgs { let default_aot_dir = || std::env::temp_dir().join("evm2-cli").join(&bench_name); let mut aot_dir = None; let mut pending_jit = None; + let entry_bytecode = bench_entry.entry_bytecode()?; { - let bytecode = bench_entry.entry_bytecode()?; let compile_spec_id = bench_entry.spec_id; let mut compiler = EvmCompiler::new_llvm(self.aot)?; @@ -200,7 +200,7 @@ impl RunArgs { compiler.set_frame_pointers(false); } - let parsed = compiler.parse(bytecode.as_slice(), compile_spec_id)?; + let parsed = compiler.parse(entry_bytecode.as_slice(), compile_spec_id)?; if self.display || self.parse_only { println!("{name}()\n{parsed:#}"); } @@ -244,7 +244,7 @@ impl RunArgs { } else if self.load.is_none() { let func = unsafe { compiler.jit_function(f_id)? }; let mut functions = B256Map::default(); - functions.insert(keccak256(&bytecode), func); + functions.insert(keccak256(&entry_bytecode), func); compiler.clear_ir()?; pending_jit = Some((compiler, functions)); } @@ -260,7 +260,8 @@ impl RunArgs { let (prepared, _compiler, _lib); if let Some(ref load_path) = load_path { let lib; - (prepared, lib) = PreparedBench::load_from_library(&bench_entry, load_path, name)?; + (prepared, lib) = + PreparedBench::load_from_library(&bench_entry, load_path, name, &entry_bytecode)?; _compiler = None; _lib = Some(lib); } else if let Some((mut compiler, functions)) = pending_jit { @@ -278,7 +279,7 @@ impl RunArgs { match self.n_iters { 0 => {} - 1 if !self.interpret || self.jit_only => prepared.sanity_check()?, + 1 if !self.interpret || self.jit_only => prepared.sanity_check(!self.no_gas)?, _ => { if self.interpret || !self.jit_only { bench(self.n_iters, &format!("{name}/interpreter"), || { @@ -584,20 +585,16 @@ impl PreparedBench { bench: &Bench, lib_path: &Path, symbol_name: &str, + entry_bytecode: &[u8], ) -> eyre::Result<(Self, libloading::Library)> { - let (name, accounts, block, db, tx) = parse_bench(bench)?; + let (name, _, block, db, tx) = parse_bench(bench)?; let lib = unsafe { libloading::Library::new(lib_path) }?; - let mut functions = B256Map::default(); - for account in &accounts { - if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { - continue; - } - let func = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = lib.get(symbol_name.as_bytes())?; - *sym - }; - functions.insert(account.code_hash, func); - } + // The library holds only the entry contract; other contracts run on the interpreter. + let func = unsafe { + let sym: libloading::Symbol<'_, EvmCompilerFn> = lib.get(symbol_name.as_bytes())?; + *sym + }; + let functions = B256Map::from_iter([(keccak256(entry_bytecode), func)]); Ok(( Self { @@ -624,7 +621,7 @@ impl PreparedBench { evm.transact(&self.tx).map(evm2::ExecutedTx::commit).map_err(Into::into) } - fn sanity_check(&self) -> eyre::Result<()> { + fn sanity_check(&self, compare_gas: bool) -> eyre::Result<()> { let interpreter = self.run_interpreter()?; let jit = self.run_jit()?; if self.assert_success { @@ -637,7 +634,10 @@ impl PreparedBench { ); } eyre::ensure!( - interpreter.status == jit.status, + interpreter.status == jit.status + && (!compare_gas + || (interpreter.output == jit.output + && interpreter.total_gas_spent == jit.total_gas_spent)), "interpreter and JIT results differ:\n interpreter: {:?}\n JIT: {:?}", interpreter, jit From d6c7e21c43e8d4f6f83c0139da093604376c49ee Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:41:40 +0100 Subject: [PATCH 064/159] fix(inspectors): give fault-only JS tracers geth's empty log (#884) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A JS tracer without a `step` function got the wrong log in `fault`. Geth fills the log it hands to `fault` in its step hook, and that hook returns at once for a tracer without `step`, so such a tracer sees an empty log with only the error set: pc 0, opcode `STOP`, no gas, cost, depth or refund, and an empty stack and memory. evm2 gave it the post-execution stack and memory of the failing instruction, with its real pc, opcode, gas, cost and depth. The inspector no longer records anything for these tracers and calls `fault` with the empty log, which also removes the post-execution snapshot path. I ran each tracer through geth master (`a0cba163`, evm 1.17.11-unstable) `evm t8n --trace.tracer` (Cancun) and through `origin/main` and this branch on the same alloc and transaction. The tracer logs pc, opcode name and number, `isPush`, gas, cost, depth, refund, stack length, memory length, a four-byte memory slice and the error from `fault`; all five layouts below now match geth. | Case | geth master | `origin/main` | this PR | | --- | --- | --- | --- | | `JUMP` to an invalid destination | pc 0, `STOP`, gas 0, cost 0, depth 0 | 🔴 pc 2, `JUMP`, gas 978997, cost 8, depth 1 | 🟢 same as geth | | `RETURNDATACOPY` out of bounds | pc 0, `STOP`, memory length 0 | 🔴 pc 11, `RETURNDATACOPY`, memory length 32 | 🟢 same as geth | | child frame fails with an invalid jump | pc 0, `STOP`, depth 0 | 🔴 pc 2, `JUMP`, depth 2 | 🟢 same as geth | | two failing calls in a row | two empty logs | 🔴 two logs with pc 2, `JUMP` | 🟢 same as geth | | `TSTORE` in a static call, `fault` only | op `STOP` | 🔴 op `TSTORE` | 🟢 same as geth | One difference stays: a tracer that wraps `log.stack.peek(0)` in `try`/`catch` catches the out-of-bounds error, while geth's interrupt cannot be caught. --- crates/inspectors/src/tracing/js/bindings.rs | 18 +++++++++++++++++ crates/inspectors/src/tracing/js/mod.rs | 21 ++++++++------------ crates/inspectors/tests/it/geth_js/errors.rs | 12 +++++++---- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 3ecc123b1..3679de8c8 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -272,6 +272,24 @@ pub(crate) struct StepInfo<'a> { pub(crate) call_id: u64, } +impl Default for StepInfo<'_> { + /// The log of a tracer whose step hook never ran: everything is zero. + fn default() -> Self { + static NO_INPUT: Bytes = Bytes::new(); + Self { + cost: 0, + refund: 0, + depth: 0, + error: None, + caller: Address::ZERO, + contract: Address::ZERO, + value: U256::ZERO, + input: &NO_INPUT, + call_id: 0, + } + } +} + /// Revokes the JS log object's access to the interpreter's stack and memory when dropped. #[derive(Debug)] #[must_use] diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 03ac6c61f..13eab4d39 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -581,6 +581,13 @@ impl JsInspector { input: &call.contract.input, call_id: call.id, }; + // Geth fills the log in its step hook, which a tracer without `step` never runs, so + // `fault` sees an empty log with only the error set. + let info = if self.step_fn.is_none() { + StepInfo { error: info.error, ..Default::default() } + } else { + info + }; let (stack, memory, host) = interp.stack_memory_host(); let res = self.reusable_db.with_state_scope(host.state_mut(), || { self.reusable_step_log.with_scope( @@ -679,19 +686,7 @@ impl Inspector for JsInspector { let result = interp.result().err(); let is_fault = result.is_some_and(|stop| should_fault(stop, pending.instruction.op)); let error = result.and_then(|stop| utils::fmt_error_msg(stop, TraceStyle::Geth)); - if self.step_fn.is_none() { - if !is_fault { - return; - } - // Fault-only tracers expose post-execution stack and memory. - pending.snapshot.record(PreStep { - pc: pending.instruction.pc, - op: pending.instruction.op, - gas_remaining: pending.instruction.gas, - stack: interp.stack().as_slice(), - memory: interp.memory().as_slice(), - }); - } else { + if self.step_fn.is_some() { self.invoke_step_hook(interp, false, if is_fault { None } else { error.clone() }); if self.check_and_halt(interp) || self.hook_errored() { return; diff --git a/crates/inspectors/tests/it/geth_js/errors.rs b/crates/inspectors/tests/it/geth_js/errors.rs index ed949cad9..bab91e8b4 100644 --- a/crates/inspectors/tests/it/geth_js/errors.rs +++ b/crates/inspectors/tests/it/geth_js/errors.rs @@ -94,10 +94,11 @@ fn faults_follow_steps_and_preserve_instruction_metadata() { assert!(!result.result.is_success()); assert_eq!(actual.unwrap(), expected); } - let fault_only = "{e:[],fault:function(l){this.e.push([l.getPC(),l.op.toString(),l.getCost()])},result:function(){return this.e}}"; + // Geth fills the log in its step hook, so a tracer without `step` sees an empty one. + let fault_only = "{e:[],fault:function(l){this.e.push([l.getPC(),l.op.toString(),l.getCost(),l.getDepth(),l.stack.length(),l.memory.length(),l.getError()])},result:function(){return this.e}}"; assert_eq!( - execute(fault_only, &hex!("60ff5600"), 1_000_000).0.unwrap(), - json!([[2, "JUMP", 8]]) + execute(fault_only, &hex!("600160005260ff5600"), 1_000_000).0.unwrap(), + json!([[0, "STOP", 0, 0, 0, 0, "invalid jump destination"]]) ); } @@ -142,8 +143,11 @@ fn static_write_errors_follow_the_opcode_validation_stage() { assert!(result.result.is_success()); assert_eq!( output.unwrap(), - if has_step || callback == "fault" { + if has_step { json!([[callback, opcode, "write protection"]]) + } else if callback == "fault" { + // A tracer without `step` sees geth's empty log. + json!([[callback, "STOP", "write protection"]]) } else { json!([]) }, From d9acced3f137596cbb52d9fddaf870905800dbae Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:13:14 +0100 Subject: [PATCH 065/159] chore(jit): remove set_current_block_cold (#961) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `set_current_block_cold` emitted `llvm.assume(i1 true) #cold`. LLVM treats an assume of a constant true as dead and early-cse deletes it, so the hint never reached block placement (every `assume(true) #cold` is in `unopt.ll` and none is left in `opt.ll`). This PR removes the method from the `Builder` trait and the LLVM backend, along with its two callers. The edges into the cold blocks already carry the hint. Every `build_check` failure edge goes through `brif_cold`, as do the jump-table range checks. I also tried weighting the `MRESIZE` check (`brif_cold`, then-block cold). That moved the resize call out of line, changed `opt.s` in 28/31 benchmarks and was worse: JIT size +1.8%, spills +3.9%, compile instructions +0.8%, runtime instructions +0.8% and cycles +1.4%. The check stays a plain branch, and `EXPERIMENTS.md` records the experiment. The deletion is not a strict no-op. Before early-cse removes it, the assume makes the `failure` block non-empty, so the first `simplifycfg` keeps it apart from the `return` block. Without it the two merge (`br i1 %c, label %failure` becomes `label %return` in `opt.ll`). That changes `opt.s` in a few benchmarks (`usdt` and `curve_stableswap` about +1% size, `weth` -0.8%) and nets out flat in total size and runtime instructions. ## Benchmarks The numbers come from the working tree on top of `d6c7e21c`, before the follow-up commit that deletes helpers no caller uses. JIT size and `opt.s` are flat in total (`opt.s` +0.03%); spills +0.4% in total. Compile instructions are +0.2% in total (`erc20_transfer` and `usdc_proxy` +2.7%); runtime instructions are flat in total. Head `d6c7e21c43` with uncommitted changes vs base `origin/main` (`d6c7e21c43`). ### Codegen statistics `opt.s` changed in 14/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:-------------------|------------:|---------:|------------:|-----------:|-------------:|--------------:|----------:|------------:|------------:| | fibonacci-calldata | -1.8% 🟢 | = | = | = | = | = | = | = | = | | factorial | -1.8% 🟢 | = | = | = | = | = | = | = | = | | weth | -0.2% 🟢 | -0.1% 🟢 | +0.1% 🔴 | -0.8% 🟢 | = | = | = | +6.2% 🔴 | +1.1% 🔴 | | erc20_transfer | -0.4% 🟢 | -0.1% 🟢 | +0.1% 🔴 | +0.2% 🔴 | = | = | = | +33.3% 🔴 | +16.7% 🔴 | | push0_proxy | -1.9% 🟢 | = | = | = | = | = | - | - | - | | usdc_proxy | -0.1% 🟢 | -0.1% 🟢 | +0.1% 🔴 | = | = | = | = | +40.0% 🔴 | +20.0% 🔴 | | airdrop | -0.3% 🟢 | = | = | +0.1% 🔴 | = | = | = | +6.7% 🔴 | +0.5% 🔴 | | eip2935 | -1.2% 🟢 | = | = | = | = | = | = | - | - | | curve_stableswap | -0.2% 🟢 | = | +0.8% 🔴 | +0.8% 🔴 | = | = | = | = | +10.0% 🔴 | | usdt | -0.1% 🟢 | = | +1.0% 🔴 | +1.0% 🔴 | = | = | = | +5.1% 🔴 | +8.5% 🔴 | | **TOTAL** | **-0.4% 🟢** | **=** | **=** | **=** | **=** | **=** | **=** | **+0.4% 🔴** | **=** | | **GEOMEAN** | **-0.6% 🟢** | **=** | **+0.1% 🔴** | **=** | **=** | **=** | **=** | **+3.1% 🔴** | **+2.0% 🔴** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------------:|--------------------------:|--------------------------:|--------------------------------:|------------------------:|------------------------:|----------------------:|----------------------------:|------------------------:| | fibonacci-calldata | 335
329
-1.8% 🟢 | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
325
-1.8% 🟢 | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
986
-0.6% 🟢 | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
59703
-0.8% 🟢 | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11558
-0.2% 🟢 | 2825
2823
-0.1% 🟢 | 4518
4524
+0.1% 🔴 | 14.0 KiB
13.9 KiB
-0.8% 🟢 | 178
178
= | 237
237
= | 25
25
= | 32
34
+6.2% 🔴 | 92
93
+1.1% 🔴 | | hash_10k | 991
982
-0.9% 🟢 | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12026
-0.4% 🟢 | 3811
3809
-0.1% 🟢 | 4978
4981
+0.1% 🔴 | 13.9 KiB
14.0 KiB
+0.2% 🔴 | 160
160
= | 322
322
= | 53
53
= | 6
8
+33.3% 🔴 | 6
7
+16.7% 🔴 | | push0_proxy | 313
307
-1.9% 🟢 | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6047
-0.1% 🟢 | 1583
1581
-0.1% 🟢 | 2274
2277
+0.1% 🔴 | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
7
+40.0% 🔴 | 5
6
+20.0% 🔴 | | fiat_token | 75544
75310
-0.3% 🟢 | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41568
-0.2% 🟢 | 12039
12037
= | 16662
16688
+0.2% 🔴 | 52.5 KiB
52.4 KiB
-0.1% 🟢 | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
433
-0.2% 🟢 | | univ2_router | 80476
80165
-0.4% 🟢 | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
132264
-0.6% 🟢 | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26516
-0.3% 🟢 | 8462
8460
= | 11032
11035
= | 31.6 KiB
31.6 KiB
+0.1% 🔴 | 432
432
= | 666
666
= | 91
91
= | 30
32
+6.7% 🔴 | 211
212
+0.5% 🔴 | | bswap64 | 1757
1751
-0.3% 🟢 | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1490
-0.4% 🟢 | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
625
-1.0% 🟢 | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
482
-1.2% 🟢 | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9152
-0.3% 🟢 | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16328
-0.2% 🟢 | 5666
5664
= | 7170
7227
+0.8% 🔴 | 20.5 KiB
20.6 KiB
+0.8% 🔴 | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
22
+10.0% 🔴 | | onchain_lm_v2 | 53236
53109
-0.2% 🟢 | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34549
-0.1% 🟢 | 8007
8005
= | 10687
10792
+1.0% 🔴 | 31.4 KiB
31.7 KiB
+1.0% 🔴 | 473
473
= | 657
657
= | 52
52
= | 39
41
+5.1% 🔴 | 47
51
+8.5% 🔴 | | universal_router | 77644
77412
-0.3% 🟢 | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
124769
-0.3% 🟢 | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66480
-0.7% 🟢 | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
116922
-0.3% 🟢 | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93186
-0.4% 🟢 | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
104646
-0.4% 🟢 | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
113933
-0.3% 🟢 | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145361
-0.4% 🟢 | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85681
-0.2% 🟢 | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1493962
-0.4% 🟢** | **495713
495699
=** | **662536
662739
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2545
+0.4% 🔴** | **20063
20072
=** |
### Compile times | benchmark | instructions | cycles | time | |:-----------------|---------------:|------------:|------------:| | weth | +1.6% 🔴 | +1.2% 🔴 | +0.7% 🔴 | | erc20_transfer | +2.7% 🔴 | +1.5% 🔴 | +0.4% 🔴 | | usdc_proxy | +2.7% 🔴 | +1.2% 🔴 | +0.4% 🔴 | | uniswap_v2_pair | +2.5% 🔴 | +3.0% 🔴 | +2.9% 🔴 | | airdrop | +2.2% 🔴 | +2.1% 🔴 | +1.4% 🔴 | | curve_stableswap | +2.2% 🔴 | +2.2% 🔴 | +2.2% 🔴 | | usdt | +1.6% 🔴 | +1.7% 🔴 | +1.5% 🔴 | | **TOTAL** | **+0.2% 🔴** | **+0.2% 🔴** | **+0.2% 🔴** | | **GEOMEAN** | **+0.5% 🔴** | **+0.4% 🔴** | **+0.1% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.43M
37.43M
= | 36.96M
36.59M
-1.0% 🟢 | 11.1ms
10.7ms
-4.3% 🟢 | 150.4µs
155.3µs
+3.2% 🔴 | 361.9µs
366.4µs
+1.3% 🔴 | 1.37ms
1.37ms
-0.4% 🟢 | 2.85ms
3.07ms
+7.8% 🔴 | | factorial | 41.65M
41.58M
-0.2% 🟢 | 39.22M
38.46M
-1.9% 🟢 | 11.6ms
11.2ms
-2.9% 🟢 | 135.2µs
142.8µs
+5.7% 🔴 | 319.6µs
333.9µs
+4.5% 🔴 | 1.17ms
1.19ms
+2.1% 🔴 | 3.08ms
3.09ms
+0.3% 🔴 | | counter | 54.14M
54.11M
= | 39.27M
39.26M
= | 9.27ms
9.21ms
-0.6% 🟢 | 328.6µs
270.8µs
-17.6% 🟢 | 427.3µs
432.7µs
+1.3% 🔴 | 1.32ms
1.32ms
= | 3.53ms
3.54ms
+0.3% 🔴 | | snailtracer | 5.30G
5.28G
-0.4% 🟢 | 2.00G
1.98G
-0.6% 🟢 | 377.5ms
374.3ms
-0.8% 🟢 | 30.1ms
31.1ms
+3.3% 🔴 | 6.75ms
6.69ms
-0.8% 🟢 | 66.4ms
66.2ms
-0.4% 🟢 | 303.1ms
303.0ms
-0.1% 🟢 | | weth | 538.66M
547.52M
+1.6% 🔴 | 242.54M
245.33M
+1.2% 🔴 | 47.8ms
48.1ms
+0.7% 🔴 | 1.83ms
1.81ms
-1.1% 🟢 | 1.85ms
1.91ms
+3.2% 🔴 | 10.9ms
11.1ms
+1.7% 🔴 | 32.2ms
32.9ms
+2.0% 🔴 | | hash_10k | 80.40M
80.20M
-0.2% 🟢 | 51.47M
51.68M
+0.4% 🔴 | 11.7ms
11.7ms
+0.6% 🔴 | 252.3µs
252.5µs
+0.1% 🔴 | 446.7µs
440.3µs
-1.4% 🟢 | 1.95ms
1.88ms
-3.4% 🟢 | 5.07ms
5.10ms
+0.7% 🔴 | | erc20_transfer | 576.95M
592.72M
+2.7% 🔴 | 253.07M
256.76M
+1.5% 🔴 | 50.0ms
50.1ms
+0.4% 🔴 | 1.97ms
1.94ms
-1.5% 🟢 | 1.87ms
1.91ms
+2.0% 🔴 | 11.6ms
11.9ms
+2.8% 🔴 | 33.1ms
34.1ms
+3.1% 🔴 | | push0_proxy | 34.65M
34.65M
= | 32.01M
32.33M
+1.0% 🔴 | 9.96ms
9.95ms
-0.1% 🟢 | 127.2µs
127.6µs
+0.3% 🔴 | 321.0µs
307.2µs
-4.3% 🟢 | 866.2µs
889.8µs
+2.7% 🔴 | 2.18ms
2.17ms
-0.4% 🟢 | | usdc_proxy | 243.90M
250.53M
+2.7% 🔴 | 122.59M
124.06M
+1.2% 🔴 | 25.2ms
25.3ms
+0.4% 🔴 | 1.14ms
1.17ms
+1.9% 🔴 | 1.11ms
1.15ms
+3.6% 🔴 | 5.76ms
5.92ms
+2.7% 🔴 | 14.4ms
14.7ms
+2.3% 🔴 | | fiat_token | 3.98G
3.97G
-0.1% 🟢 | 1.59G
1.59G
-0.1% 🟢 | 302.1ms
302.2ms
= | 11.6ms
11.5ms
-1.3% 🟢 | 9.87ms
9.92ms
+0.5% 🔴 | 67.9ms
67.8ms
-0.2% 🟢 | 238.8ms
239.3ms
+0.2% 🔴 | | uniswap_v2_pair | 2.05G
2.11G
+2.5% 🔴 | 850.31M
875.60M
+3.0% 🔴 | 162.7ms
167.4ms
+2.9% 🔴 | 6.37ms
6.30ms
-1.1% 🟢 | 5.68ms
5.94ms
+4.6% 🔴 | 33.6ms
34.8ms
+3.6% 🔴 | 127.6ms
130.9ms
+2.6% 🔴 | | univ2_router | 4.38G
4.38G
= | 1.79G
1.79G
+0.3% 🔴 | 339.7ms
339.1ms
-0.2% 🟢 | 13.3ms
13.5ms
+1.8% 🔴 | 10.5ms
10.5ms
= | 69.0ms
68.4ms
-0.8% 🟢 | 281.9ms
282.0ms
= | | seaport | 9.73G
9.72G
= | 4.23G
4.23G
-0.1% 🟢 | 796.9ms
796.7ms
= | 23.0ms
23.4ms
+1.8% 🔴 | 17.5ms
17.3ms
-1.0% 🟢 | 174.0ms
173.2ms
-0.4% 🟢 | 688.3ms
682.6ms
-0.8% 🟢 | | airdrop | 1.42G
1.45G
+2.2% 🔴 | 588.17M
600.27M
+2.1% 🔴 | 113.4ms
115.0ms
+1.4% 🔴 | 4.05ms
4.03ms
-0.7% 🟢 | 3.89ms
3.87ms
-0.4% 🟢 | 29.7ms
29.7ms
+0.2% 🔴 | 82.1ms
84.1ms
+2.5% 🔴 | | bswap64 | 67.02M
67.00M
= | 43.18M
43.67M
+1.1% 🔴 | 10.2ms
10.4ms
+1.9% 🔴 | 418.9µs
420.9µs
+0.5% 🔴 | 539.0µs
559.5µs
+3.8% 🔴 | 1.63ms
1.70ms
+4.7% 🔴 | 3.73ms
3.90ms
+4.6% 🔴 | | bswap64_opt | 73.67M
73.70M
= | 45.03M
45.12M
+0.2% 🔴 | 10.3ms
10.4ms
+1.0% 🔴 | 354.3µs
373.5µs
+5.4% 🔴 | 469.4µs
491.2µs
+4.6% 🔴 | 1.56ms
1.65ms
+5.9% 🔴 | 4.04ms
4.14ms
+2.3% 🔴 | | eip4788 | 48.58M
48.48M
-0.2% 🟢 | 36.14M
36.12M
= | 8.54ms
8.51ms
-0.4% 🟢 | 179.9µs
175.2µs
-2.6% 🟢 | 353.2µs
385.2µs
+9.0% 🔴 | 986.6µs
1.08ms
+9.1% 🔴 | 3.28ms
3.33ms
+1.5% 🔴 | | eip2935 | 42.38M
42.28M
-0.2% 🟢 | 37.39M
37.10M
-0.8% 🟢 | 11.0ms
10.8ms
-2.4% 🟢 | 145.4µs
148.3µs
+2.0% 🔴 | 333.3µs
339.0µs
+1.7% 🔴 | 887.0µs
928.2µs
+4.6% 🔴 | 2.83ms
2.90ms
+2.4% 🔴 | | burntpix | 6.70G
6.69G
-0.1% 🟢 | 2.65G
2.65G
+0.2% 🔴 | 499.1ms
498.0ms
-0.2% 🟢 | 10.4ms
10.3ms
-1.0% 🟢 | 1.57ms
1.53ms
-2.5% 🟢 | 9.99ms
9.96ms
-0.4% 🟢 | 27.8ms
28.0ms
+0.9% 🔴 | | curve_stableswap | 1.14G
1.16G
+2.2% 🔴 | 483.69M
494.13M
+2.2% 🔴 | 93.3ms
95.3ms
+2.2% 🔴 | 2.83ms
2.83ms
+0.1% 🔴 | 2.45ms
2.47ms
+0.9% 🔴 | 20.8ms
21.4ms
+2.9% 🔴 | 46.7ms
48.9ms
+4.7% 🔴 | | onchain_lm_v2 | 3.67G
3.67G
= | 1.41G
1.42G
+0.4% 🔴 | 268.1ms
268.8ms
+0.2% 🔴 | 8.97ms
9.38ms
+4.6% 🔴 | 7.05ms
7.28ms
+3.2% 🔴 | 46.4ms
46.5ms
+0.3% 🔴 | 186.1ms
187.4ms
+0.7% 🔴 | | usdt | 1.34G
1.36G
+1.6% 🔴 | 552.07M
561.25M
+1.7% 🔴 | 106.0ms
107.7ms
+1.5% 🔴 | 5.42ms
5.31ms
-2.1% 🟢 | 4.87ms
4.84ms
-0.6% 🟢 | 24.9ms
25.9ms
+4.2% 🔴 | 77.2ms
78.6ms
+1.8% 🔴 | | universal_router | 11.56G
11.58G
+0.1% 🔴 | 4.78G
4.79G
+0.4% 🔴 | 896.1ms
898.7ms
+0.3% 🔴 | 11.6ms
11.9ms
+2.7% 🔴 | 10.3ms
11.3ms
+10.2% 🔴 | 80.1ms
80.7ms
+0.7% 🔴 | 312.6ms
314.8ms
+0.7% 🔴 | | aggregation_router_v6 | 35.12G
35.17G
+0.2% 🔴 | 14.35G
14.36G
+0.1% 🔴 | 2.677s
2.682s
+0.2% 🔴 | 19.2ms
18.9ms
-1.6% 🟢 | 16.8ms
17.5ms
+4.4% 🔴 | 136.0ms
137.2ms
+0.9% 🔴 | 513.5ms
520.1ms
+1.3% 🔴 | | entry_point_v0_7 | 17.88G
17.95G
+0.4% 🔴 | 7.36G
7.37G
+0.1% 🔴 | 1.377s
1.377s
= | 8.88ms
8.98ms
+1.1% 🔴 | 8.95ms
9.24ms
+3.2% 🔴 | 62.3ms
64.2ms
+3.1% 🔴 | 265.8ms
267.6ms
+0.7% 🔴 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.56G
2.56G
= | 483.3ms
483.8ms
+0.1% 🔴 | 18.8ms
18.7ms
-0.7% 🟢 | 16.0ms
15.5ms
-3.0% 🟢 | 110.5ms
109.6ms
-0.8% 🟢 | 399.1ms
397.7ms
-0.3% 🟢 | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 2.73G
2.73G
+0.1% 🔴 | 513.9ms
515.6ms
+0.3% 🔴 | 13.3ms
13.3ms
-0.4% 🟢 | 12.7ms
13.1ms
+3.2% 🔴 | 106.3ms
105.6ms
-0.6% 🟢 | 390.5ms
391.6ms
+0.3% 🔴 | | curve_3pool | 6.59G
6.59G
= | 2.56G
2.56G
= | 484.7ms
483.6ms
-0.2% 🟢 | 18.7ms
18.4ms
-1.9% 🟢 | 13.6ms
13.4ms
-1.1% 🟢 | 85.6ms
85.7ms
= | 430.9ms
433.0ms
+0.5% 🔴 | | curve_tricrypto2 | 9.40G
9.40G
= | 3.62G
3.62G
-0.1% 🟢 | 682.3ms
681.7ms
-0.1% 🟢 | 16.7ms
16.8ms
+0.4% 🔴 | 14.6ms
15.8ms
+7.8% 🔴 | 95.3ms
94.9ms
-0.5% 🟢 | 417.1ms
421.3ms
+1.0% 🔴 | | balancer_vault | 19.03G
19.01G
-0.1% 🟢 | 8.16G
8.18G
+0.2% 🔴 | 1.522s
1.528s
+0.4% 🔴 | 24.4ms
24.5ms
+0.6% 🔴 | 18.1ms
18.3ms
+1.0% 🔴 | 203.4ms
204.0ms
+0.3% 🔴 | 654.7ms
659.9ms
+0.8% 🔴 | | succinct_staking | 10.84G
10.92G
+0.7% 🔴 | 4.41G
4.43G
+0.4% 🔴 | 826.6ms
829.4ms
+0.3% 🔴 | 16.7ms
16.6ms
-0.5% 🟢 | 11.6ms
12.0ms
+3.3% 🔴 | 86.7ms
87.5ms
+1.0% 🔴 | 314.4ms
316.9ms
+0.8% 🔴 | | **TOTAL** | **164.86G
165.18G
+0.2% 🔴** | **67.65G
67.78G
+0.2% 🔴** | **12.737s
12.760s
+0.2% 🔴** | **271.6ms
272.8ms
+0.4% 🔴** | **201.1ms
205.1ms
+2.0% 🔴** | **1.549s
1.554s
+0.4% 🔴** | **5.868s
5.901s
+0.5% 🔴** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:-------------|--------------------:|--------------:| | weth | +1.0% 🔴 | -3.9% 🟢 | | push0_proxy | -1.1% 🟢 | -2.2% 🟢 | | univ2_router | -0.9% 🟢 | -5.8% 🟢 | | bswap64 | +2.2% 🔴 | -4.0% 🟢 | | bswap64_opt | -1.7% 🟢 | -7.4% 🟢 | | eip4788 | -1.4% 🟢 | -3.2% 🟢 | | eip2935 | -1.0% 🟢 | -2.2% 🟢 | | **TOTAL** | **=** | **+0.3% 🔴** | | **GEOMEAN** | **=** | **-1.8% 🟢** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 43.38K
43.53K
+0.4% 🔴 | 10.92K
10.64K
-2.6% 🟢 | | factorial | 79.64K
79.00K
-0.8% 🟢 | 18.41K
17.92K
-2.6% 🟢 | | counter | 22.87K
22.69K
-0.8% 🟢 | 7.78K
7.50K
-3.5% 🟢 | | snailtracer | 193.34M
193.35M
= | 58.38M
58.11M
-0.5% 🟢 | | weth | 27.09K
27.36K
+1.0% 🔴 | 9.55K
9.17K
-3.9% 🟢 | | hash_10k | 8.80M
8.80M
= | 2.77M
2.78M
+0.3% 🔴 | | erc20_transfer | 18.87M
18.87M
= | 5.35M
5.35M
+0.1% 🔴 | | push0_proxy | 22.53K
22.29K
-1.1% 🟢 | 8.13K
7.96K
-2.2% 🟢 | | usdc_proxy | 23.50K
23.69K
+0.8% 🔴 | 8.40K
8.13K
-3.2% 🟢 | | fiat_token | 21.82K
21.96K
+0.7% 🔴 | 7.91K
7.70K
-2.6% 🟢 | | uniswap_v2_pair | 22.16K
22.30K
+0.7% 🔴 | 8.00K
7.84K
-2.0% 🟢 | | univ2_router | 20.29K
20.11K
-0.9% 🟢 | 7.55K
7.11K
-5.8% 🟢 | | seaport | 22.47K
22.62K
+0.7% 🔴 | 8.00K
7.84K
-2.0% 🟢 | | airdrop | 22.14K
22.32K
+0.8% 🔴 | 8.06K
7.84K
-2.7% 🟢 | | bswap64 | 19.14K
19.57K
+2.2% 🔴 | 7.07K
6.78K
-4.0% 🟢 | | bswap64_opt | 19.68K
19.34K
-1.7% 🟢 | 7.25K
6.71K
-7.4% 🟢 | | eip4788 | 23.49K
23.16K
-1.4% 🟢 | 8.24K
7.98K
-3.2% 🟢 | | eip2935 | 22.36K
22.14K
-1.0% 🟢 | 7.83K
7.66K
-2.2% 🟢 | | burntpix | 102.34M
102.34M
= | 27.54M
27.73M
+0.7% 🔴 | | curve_stableswap | 258.97K
259.61K
+0.2% 🔴 | 102.90K
102.15K
-0.7% 🟢 | | onchain_lm_v2 | 521.39M
521.39M
= | 214.43M
215.59M
+0.5% 🔴 | | usdt | 38.00K
38.10K
+0.3% 🔴 | 12.70K
12.96K
+2.1% 🔴 | | universal_router | 147.51K
147.77K
+0.2% 🔴 | 52.65K
52.24K
-0.8% 🟢 | | aggregation_router_v6 | 457.33K
457.20K
= | 166.74K
163.93K
-1.7% 🟢 | | entry_point_v0_7 | 1.28M
1.28M
= | 328.82K
327.32K
-0.5% 🟢 | | uniswap_v3_pool | 81.02K
81.23K
+0.3% 🔴 | 27.44K
27.06K
-1.4% 🟢 | | uniswap_v4_pool_manager | 68.12K
68.03K
-0.1% 🟢 | 21.46K
21.23K
-1.1% 🟢 | | curve_3pool | 81.13K
81.35K
+0.3% 🔴 | 27.46K
27.27K
-0.7% 🟢 | | curve_tricrypto2 | 218.35K
218.21K
-0.1% 🟢 | 84.23K
82.48K
-2.1% 🟢 | | balancer_vault | 122.13K
122.17K
= | 41.31K
41.49K
+0.4% 🔴 | | succinct_staking | 62.17K
62.05K
-0.2% 🟢 | 23.42K
23.43K
= | | **TOTAL** | **847.97M
847.96M
=** | **309.48M
310.57M
+0.3% 🔴** |
--- crates/jit/EXPERIMENTS.md | 4 ++++ crates/jit/backend/src/traits.rs | 1 - .../jit/codegen/src/compiler/translate/mod.rs | 2 -- crates/jit/llvm/src/lib.rs | 17 +++-------------- crates/jit/llvm/src/wrap/ir.rs | 5 ----- 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/crates/jit/EXPERIMENTS.md b/crates/jit/EXPERIMENTS.md index eada5e150..611807df0 100644 --- a/crates/jit/EXPERIMENTS.md +++ b/crates/jit/EXPERIMENTS.md @@ -32,6 +32,10 @@ ones here with the numbers that rejected them. benchmarks, JIT size +4.0% total, compile instructions +10.7% total (geomean +6.1%) and runtime instructions +3.8% total (geomean +0.5%; `onchain_lm_v2` +4.3%, `burntpix` +4.2%, `snailtracer` +2.8%). +- Branch weights on the `MRESIZE` check (`then` block cold): moving the resize + call out of line made `opt.s` change in 28/31 benchmarks, JIT size + +1.8% total (geomean +1.3%), spills +3.9%, compile instructions +0.8% and + runtime instructions +0.8% (cycles +1.4%). ## Optimization passes diff --git a/crates/jit/backend/src/traits.rs b/crates/jit/backend/src/traits.rs index 6af8d41f1..e5d760f21 100644 --- a/crates/jit/backend/src/traits.rs +++ b/crates/jit/backend/src/traits.rs @@ -294,7 +294,6 @@ pub trait Builder: BackendTypes + TypeMethods { fn switch_to_block(&mut self, block: Self::BasicBlock); fn seal_block(&mut self, block: Self::BasicBlock); fn seal_all_blocks(&mut self); - fn set_current_block_cold(&mut self); fn current_block(&mut self) -> Option; fn block_addr(&mut self, block: Self::BasicBlock) -> Option; diff --git a/crates/jit/codegen/src/compiler/translate/mod.rs b/crates/jit/codegen/src/compiler/translate/mod.rs index ea1f1e877..cfaf5e903 100644 --- a/crates/jit/codegen/src/compiler/translate/mod.rs +++ b/crates/jit/codegen/src/compiler/translate/mod.rs @@ -457,7 +457,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { failure_value } }; - fx.bcx.set_current_block_cold(); // Do not sync the stack here: this is a shared synthetic merge block for // failures that may occur before their instruction body has materialized // the current virtual stack. Syncing here can use values that do not @@ -1918,7 +1917,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.cached_mem_base = None; self.if_then(exceeds, |this| { - this.bcx.set_current_block_cold(); this.call_fallible_builtin(Builtin::Mresize, &[this.ecx, min_size]); }); self.build_memory_addr(offset) diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 45e3808da..090344683 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -1213,12 +1213,6 @@ impl EvmLlvmBuilder<'_> { let kind_id = self.cx.get_kind_id("prof"); inst.set_metadata(metadata, kind_id); } - - fn assume_inner(&mut self, cond: Value) -> Inst { - let function = self - .get_or_add_function("llvm.assume", |this| this.ty_void.fn_type(&[this.ty_i1], false)); - self.bcx.build_call(function, &[cond], "") - } } impl BackendTypes for EvmLlvmBuilder<'_> { @@ -1274,13 +1268,6 @@ impl Builder for EvmLlvmBuilder<'_> { // Nothing to do. } - fn set_current_block_cold(&mut self) { - let true_ = self.bool_const(true); - let callsite = self.assume_inner(true_); - let cold = self.cx.create_enum_attribute(Attribute::get_named_enum_kind_id("cold"), 0); - callsite.add_call_site_attribute(AttributeLoc::Function, cold); - } - fn current_block(&mut self) -> Option { self.bcx.get_insert_block() } @@ -1434,7 +1421,9 @@ impl Builder for EvmLlvmBuilder<'_> { } fn assume(&mut self, cond: Self::Value) { - self.assume_inner(cond); + let function = self + .get_or_add_function("llvm.assume", |this| this.ty_void.fn_type(&[this.ty_i1], false)); + self.bcx.build_call(function, &[cond], ""); } fn icmp(&mut self, cond: IntCC, lhs: Self::Value, rhs: Self::Value) -> Self::Value { diff --git a/crates/jit/llvm/src/wrap/ir.rs b/crates/jit/llvm/src/wrap/ir.rs index 8215e21a7..f78e1fcd0 100644 --- a/crates/jit/llvm/src/wrap/ir.rs +++ b/crates/jit/llvm/src/wrap/ir.rs @@ -513,11 +513,6 @@ impl Inst { pub(crate) fn set_call_conv(self, call_conv: u32) { unsafe { LLVMSetInstructionCallConv(self.0, call_conv) } } - - /// Adds an attribute to a call. - pub(crate) fn add_call_site_attribute(self, loc: AttributeLoc, attribute: Attribute) { - unsafe { LLVMAddCallSiteAttribute(self.0, loc.index(), attribute.0) } - } } impl fmt::Debug for Inst { From e50f56c47da8999d491f0b7be0887a3255ed9bbf Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:13:25 +0100 Subject: [PATCH 066/159] perf(jit): gate LICM on the largest function's block count (#960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `optimize_module` ran LICM only when the module's blocks, summed over all functions, were at most 4000. The comment called this a limit on a function's size, and it justified the limit with two data points. It also said EVM has no loops to optimize, then explained why LICM helps EVM loops. The sum is the largest function's block count plus the two stub blocks, so the code did measure the largest function. It did so by accident, and the gate was unmeasured. The gate is now `LICM_MAX_BLOCKS`, compared with the block count of the largest function, and both comments are fixed. I swept LICM on and off (the pipeline with and without the LICM group, via `EVM2_JIT_PASSES`) over the 31 benchmarks and 1371 distinct contract codes from the mainnet replay fixture, and recorded the sweep in `EXPERIMENTS.md`. The data show that LICM pays only on loops in small functions: fibonacci and factorial run 8% fewer instructions, hash_10k 1%, and no benchmark above 897 blocks changes by more than 1.7% in either direction (median +0.02%). Its cost grows with function size: +5% compile instructions at 501–1000 blocks, +8–10% at 1001–8000, +20% above 8000, plus about 1% more code. The cutoff is 1000 blocks, which cuts the gate from 4000. The benchmarks have no function between 262 and 896 blocks, so that range is untested and the exact cutoff within it is a judgment call. ## Benchmarks Only contracts whose largest function has 1001–4000 blocks change: 26 of the 91 benchmarks (five named and 21 sampled contracts). They compile with 2.3–12.7% fewer instructions. Of the 23 with a JIT size change, 19 shrink (up to -4.0%) and four grow by 0.1–0.9%. Total compile instructions change by -0.8% (geomean -2.2%), JIT size by -0.1% (geomean -0.4%), and runtime instructions by +0.1% (geomean -0.1%). The benchmarks with unchanged assembly show cycle moves of a few percent, which is run-to-run noise. The run covers the 31 benchmarks and a sample of 60 mainnet contracts; GitHub's size limit forced dropping the collapsed full-detail tables from the output below. Head `d6c7e21c43` with uncommitted changes vs base `origin/main` (`d6c7e21c43`). ### Codegen statistics `opt.s` changed in 26/91 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:-----------------|-----------:|---------:|------------:|------------:|-------------:|--------------:|----------:|------------:|-------------:| | weth | = | +0.6% 🔴 | -2.8% 🟢 | -3.4% 🟢 | +5.6% 🔴 | = | = | -59.4% 🟢 | -90.2% 🟢 | | airdrop | = | -0.4% 🟢 | -2.1% 🟢 | -2.7% 🟢 | = | +0.2% 🔴 | = | +20.0% 🔴 | -80.6% 🟢 | | burntpix | = | -0.3% 🟢 | -0.6% 🟢 | -1.7% 🟢 | +1.2% 🔴 | -0.7% 🟢 | = | -44.4% 🟢 | -75.0% 🟢 | | curve_stableswap | = | +0.5% 🔴 | -2.5% 🟢 | -2.7% 🟢 | = | = | = | -44.4% 🟢 | -35.0% 🟢 | | 0000Fb77_7314 | = | +0.9% 🔴 | -1.1% 🟢 | -1.6% 🟢 | +0.4% 🔴 | +0.1% 🔴 | = | -54.5% 🟢 | -26.5% 🟢 | | 0439e60F_6000 | = | +0.7% 🔴 | -0.9% 🟢 | -1.7% 🟢 | +0.2% 🔴 | -0.2% 🟢 | = | +92.3% 🔴 | +137.5% 🔴 | | 04ACaF8D_1945 | = | -1.1% 🟢 | -0.7% 🟢 | -0.5% 🟢 | = | -0.4% 🟢 | = | = | = | | 0F5D2fB2_3045 | = | +0.6% 🔴 | -3.8% 🟢 | -4.0% 🟢 | +2.4% 🔴 | = | = | -31.6% 🟢 | -89.7% 🟢 | | 0fC1f19A_5148 | = | +0.5% 🔴 | -0.7% 🟢 | -2.1% 🟢 | = | = | = | +8.0% 🔴 | -34.2% 🟢 | | 39778bc7_2075 | = | +1.4% 🔴 | -1.4% 🟢 | -2.3% 🟢 | +13.7% 🔴 | +0.4% 🔴 | = | -65.6% 🟢 | -72.6% 🟢 | | 3f73F03a_1700 | = | -0.3% 🟢 | +0.1% 🔴 | -4.0% 🟢 | = | -0.9% 🟢 | = | = | -5.9% 🟢 | | 69825081_4517 | = | -0.2% 🟢 | -1.8% 🟢 | -3.5% 🟢 | +1.1% 🔴 | = | = | -66.7% 🟢 | -99.4% 🟢 | | 6c5bA916_8491 | = | +0.1% 🔴 | -3.4% 🟢 | -3.2% 🟢 | +3.7% 🔴 | -0.2% 🟢 | = | -53.1% 🟢 | -94.8% 🟢 | | 7fFE42C4_3531 | = | -0.1% 🟢 | -0.3% 🟢 | -0.4% 🟢 | +0.7% 🔴 | = | = | -22.2% 🟢 | -27.8% 🟢 | | 80b1931D_3007 | = | = | -0.3% 🟢 | -0.3% 🟢 | = | = | = | -75.0% 🟢 | -80.0% 🟢 | | 870aC11D_2717 | = | -0.3% 🟢 | -3.2% 🟢 | -2.3% 🟢 | +1.9% 🔴 | -0.3% 🟢 | = | -42.1% 🟢 | -83.1% 🟢 | | 932cD885_3066 | = | -0.1% 🟢 | -0.1% 🟢 | +0.6% 🔴 | = | -0.2% 🟢 | = | +14.3% 🔴 | +100.0% 🔴 | | A188EEC8_2972 | = | = | -0.2% 🟢 | -0.7% 🟢 | = | = | = | +62.5% 🔴 | +20.0% 🔴 | | cf6958D6_2083 | = | -1.2% 🟢 | -1.3% 🟢 | -0.8% 🟢 | = | -0.4% 🟢 | = | = | - | | d72a6BA4_2227 | = | +0.1% 🔴 | = | +0.9% 🔴 | = | -0.4% 🟢 | = | +400.0% 🔴 | +355.6% 🔴 | | **TOTAL** | **=** | **=** | **-0.1% 🟢** | **-0.1% 🟢** | **+0.1% 🔴** | **=** | **=** | **-1.7% 🟢** | **-2.4% 🟢** | | **GEOMEAN** | **=** | **=** | **-0.3% 🟢** | **-0.4% 🟢** | **+0.3% 🔴** | **=** | **=** | **-6.2% 🟢** | **-20.8% 🟢** | ### Compile times | benchmark | instructions | cycles | time | |:-----------------|---------------:|------------:|------------:| | weth | -7.9% 🟢 | -7.5% 🟢 | -7.7% 🟢 | | erc20_transfer | -5.0% 🟢 | -6.2% 🟢 | -7.1% 🟢 | | airdrop | -8.9% 🟢 | -9.2% 🟢 | -9.2% 🟢 | | burntpix | -6.1% 🟢 | -5.0% 🟢 | -5.0% 🟢 | | curve_stableswap | -7.9% 🟢 | -7.0% 🟢 | -7.0% 🟢 | | 00000000_5937 | -5.9% 🟢 | -5.3% 🟢 | -5.3% 🟢 | | 0000Fb77_7314 | -10.9% 🟢 | -11.7% 🟢 | -11.5% 🟢 | | 0439e60F_6000 | -7.3% 🟢 | -7.2% 🟢 | -7.8% 🟢 | | 04ACaF8D_1945 | -9.6% 🟢 | -8.7% 🟢 | -7.7% 🟢 | | 0D562A65_2929 | -2.7% 🟢 | -2.4% 🟢 | -2.7% 🟢 | | 0F5D2fB2_3045 | -9.5% 🟢 | -8.5% 🟢 | -8.8% 🟢 | | 0fC1f19A_5148 | -7.5% 🟢 | -7.3% 🟢 | -7.3% 🟢 | | 39778bc7_2075 | -5.9% 🟢 | -5.7% 🟢 | -5.8% 🟢 | | 3f73F03a_1700 | -6.5% 🟢 | -6.0% 🟢 | -6.4% 🟢 | | 4Ff1D40f_1868 | -4.6% 🟢 | -5.0% 🟢 | -5.6% 🟢 | | 69825081_4517 | -9.0% 🟢 | -8.8% 🟢 | -9.0% 🟢 | | 6c5bA916_8491 | -10.5% 🟢 | -10.0% 🟢 | -9.9% 🟢 | | 7fFE42C4_3531 | -6.1% 🟢 | -6.2% 🟢 | -6.4% 🟢 | | 80b1931D_3007 | -4.2% 🟢 | -4.7% 🟢 | -5.0% 🟢 | | 81F248Ff_1825 | -3.7% 🟢 | -3.5% 🟢 | -3.9% 🟢 | | 870aC11D_2717 | -12.1% 🟢 | -12.4% 🟢 | -12.1% 🟢 | | 932cD885_3066 | -12.7% 🟢 | -10.2% 🟢 | -10.1% 🟢 | | 9759A6Ac_2916 | -2.3% 🟢 | -2.4% 🟢 | -2.8% 🟢 | | A188EEC8_2972 | -3.6% 🟢 | -3.7% 🟢 | -3.7% 🟢 | | cf6958D6_2083 | -10.0% 🟢 | -10.0% 🟢 | -9.8% 🟢 | | d72a6BA4_2227 | -11.2% 🟢 | -9.1% 🟢 | -9.1% 🟢 | | **TOTAL** | **-0.8% 🟢** | **+0.2% 🔴** | **+0.1% 🔴** | | **GEOMEAN** | **-2.2% 🟢** | **-1.8% 🟢** | **-2.1% 🟢** | ### Runtime | benchmark | instructions/iter | cycles/iter | |:-----------------|--------------------:|--------------:| | weth | -1.2% 🟢 | +2.5% 🔴 | | bswap64 | +1.6% 🔴 | +3.9% 🔴 | | bswap64_opt | +1.4% 🔴 | +6.0% 🔴 | | eip2935 | -1.6% 🟢 | +3.3% 🔴 | | curve_stableswap | -1.3% 🟢 | -1.1% 🟢 | | 00000000_9152 | -2.2% 🟢 | +4.9% 🔴 | | 0D562A65_2929 | -1.7% 🟢 | +4.9% 🔴 | | 0fC1f19A_5148 | +1.2% 🔴 | +4.3% 🔴 | | 39778bc7_2075 | +1.4% 🔴 | +2.1% 🔴 | | 3C0f8950_1165 | -1.6% 🟢 | +1.3% 🔴 | | 4Fabb145_1506 | -1.1% 🟢 | +0.3% 🔴 | | 6c5bA916_8491 | = | +5.0% 🔴 | | 6ca84080_23641 | -1.0% 🟢 | +3.2% 🔴 | | 7fFE42C4_3531 | -1.1% 🟢 | +3.6% 🔴 | | 80296FF8_9452 | +1.0% 🔴 | +1.2% 🔴 | | 81F248Ff_1825 | -1.0% 🟢 | +3.7% 🔴 | | 9759A6Ac_2916 | +2.4% 🔴 | +5.6% 🔴 | | 98AB855d_1131 | +1.6% 🔴 | +5.3% 🔴 | | d47f1B1e_13522 | -1.0% 🟢 | +4.0% 🔴 | | f6337b38_171 | -1.7% 🟢 | +1.4% 🔴 | | **TOTAL** | **+0.1% 🔴** | **-0.1% 🟢** | | **GEOMEAN** | **-0.1% 🟢** | **+2.7% 🔴** | --- crates/jit/EXPERIMENTS.md | 41 ++++++++++++++++++++++++++++++++++++++ crates/jit/llvm/src/lib.rs | 20 +++++++++++-------- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/crates/jit/EXPERIMENTS.md b/crates/jit/EXPERIMENTS.md index 611807df0..d49879dda 100644 --- a/crates/jit/EXPERIMENTS.md +++ b/crates/jit/EXPERIMENTS.md @@ -56,6 +56,47 @@ ones here with the numbers that rejected them. - LICM at every function size: opt instructions +75% on the mainnet corpus, JIT size +0.4%, reloads +12%. No LICM at all: JIT size -0.1%, but hash_10k runtime +1.2%. +- LICM at every size, and the old gate of 4000 blocks summed over the module (the + largest function's block count plus two, on every benchmark). The gate is now + `LICM_MAX_BLOCKS` in `optimize_module`: 1000 blocks in the largest function. + Sweep: the pipeline with and without the LICM group (`EVM2_JIT_PASSES`) on the 31 + benchmarks and on the 1371 distinct contract codes of 32 bytes or more in the + pre-state of `data/mainnet-25347446-25347455.bin.zst`, with compile instructions + (`perf stat`, median of 3) and JIT size per contract, binned by the largest + function's block count in `unopt.ll`. The last column is the mean per contract: + + | Largest function (blocks) | Contracts | Compile instructions | JIT size | Extra instructions | + | --- | ---: | ---: | ---: | ---: | + | ≤250 | 111 | +0.8% | +3.2% | 0M | + | 251–500 | 33 | +3.5% | -0.0% | 4M | + | 501–1000 | 91 | +5.2% | +0.5% | 12M | + | 1001–2000 | 164 | +8.0% | +0.9% | 37M | + | 2001–4000 | 217 | +8.8% | +1.0% | 87M | + | 4001–8000 | 298 | +10.3% | +1.1% | 216M | + | >8000 | 457 | +20.2% | +0.7% | 1170M | + + The extra cost per block rises from about 15k instructions at 500–1000 blocks to + 30–36k at 2000–8000 and 80k above 8000, so it is mildly super-linear and steepest + in the largest functions. It is not a cliff: snailtracer (8007 blocks) pays +37% + compile instructions, seaport (17620) +32%, balancer_vault (20019) +82%. + + Runtime instructions per execution with LICM, on the benchmarks: fibonacci -8.3% + and factorial -7.8% (43 blocks), hash_10k -1.0% (133), bswap64 +0.9% and + bswap64_opt +1.2% (about 250), every other function of at most 261 blocks within + ±1%. From 897 blocks up (22 benchmarks) the median is +0.02% and the range -1.7% + (balancer_vault) to +1.2% (curve_stableswap); burntpix (1248 blocks) and + snailtracer (8007), the two compute loops in that range, change by +0.2% and + +0.04%. JIT size is 0.5–1.1% larger with LICM above 500 blocks. + + So LICM pays only on loops in small functions. Above 1000 blocks it costs 8% or + more compile instructions and about 1% more code for no measured runtime gain. + Up to 1000 blocks it costs 3.5–5.2% (at most 12M instructions per contract) and + stays: the benchmarks have no function between 262 and 896 blocks, so the + runtime effect there is unmeasured, and a cutoff of 500 would save at most that + 5.2% on 91 contracts. Across the corpus the new gate saves almost no total + compile cost against the old one (+0.0% over no LICM at all, against +0.7%, + since the largest functions dominate the total) but removes the 8–9% cost from + the 381 contracts with 1001–4000 blocks. - `bdce`, `aggressive-instcombine`, `mldst-motion`, `dfa-jump-threading`, and `simplifycfg` with `switch-range-to-icmp`, `switch-to-arithmetic`, `forward-switch-cond` or `no-keep-loops` change at most one benchmark. diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 090344683..2cceb56a9 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -44,6 +44,12 @@ pub use wrap::{Block, Context, Function, Inst, LLVMString, Module, TargetMachine /// Branch weight for non-cold branches. const DEFAULT_WEIGHT: u32 = 20000; +/// Largest function, in basic blocks, that [`EvmLlvmBackend::optimize_module`] runs LICM on. +/// +/// LICM only pays off on hot loops in small functions. The cutoff comes from the sweep in +/// `EXPERIMENTS.md`. +const LICM_MAX_BLOCKS: u32 = 1000; + /// Current JIT memory usage counters. /// /// Counters reflect live memory: bytes are added on compilation and @@ -957,13 +963,11 @@ impl Backend for EvmLlvmBackend { // // The standard `default` is also slow (~730ms on snailtracer) because the loop // analysis infrastructure (LoopInfo, DominatorTree, MemorySSA, LCSSA) is expensive to - // compute on functions with thousands of basic blocks, even though the loop passes - // themselves do nothing useful — EVM has no natural loops to optimize. + // compute on functions with thousands of basic blocks. // // LICM (Loop Invariant Code Motion) helps tight EVM loops by hoisting gas counter and - // stack slot loads/stores into registers. However, the loop analysis infrastructure is - // quadratic on large functions — e.g. +430ms on snailtracer (7770 BBs) vs +0ms on - // fibonacci (45 BBs). We skip it for functions with >4000 basic blocks. + // stack slot loads/stores into registers. It only pays off on small functions, and its + // compile cost grows faster than the function: see `LICM_MAX_BLOCKS`. // // Can be overridden with `EVM2_JIT_PASSES` env var for experimentation. // From `opt --help`, `-passes`. @@ -978,9 +982,9 @@ impl Backend for EvmLlvmBackend { OptimizationLevel::Less | OptimizationLevel::Default | OptimizationLevel::Aggressive => { - let total_bbs: u32 = - self.module.get_functions().map(|f| f.count_basic_blocks()).sum(); - let with_licm = total_bbs <= 4000; + let max_blocks = + self.module.get_functions().map(|f| f.count_basic_blocks()).max().unwrap_or(0); + let with_licm = max_blocks <= LICM_MAX_BLOCKS; let passes = if with_licm { &PASSES_WITH_LICM } else { &PASSES }; passes.get_or_init(|| build_pass_pipeline(with_licm)) } From b30f36a7110353b3ee45b424871a04420c3f90f8 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:13:34 +0100 Subject: [PATCH 067/159] fix(cli): make fuzzer cases reproducible from --seed (#904) Fuzzer access lists took storage keys from `HashMap` iteration, whose order differs per instance, so one seed could generate different cases. The case storage is now an `IndexMap`, which iterates in insertion order. Generation inserts keys in a fixed order for a given seed, so key selection and the saved JSON are reproducible and the sorting serializer is gone. The regeneration test now compares whole cases instead of zeroing the access-list keys to hide the difference, and a second test checks that a seed serializes to identical JSON that round-trips. --- crates/cli/Cargo.toml | 2 +- crates/cli/src/fuzzer/case.rs | 51 +++++++++++-------------------- crates/cli/src/fuzzer/minimize.rs | 2 +- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 04d90e79b..023a57657 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -26,7 +26,7 @@ alloy-consensus = { workspace = true, features = ["k256", "secp256k1", "std"] } alloy-chains.workspace = true alloy-eips = { workspace = true, features = ["serde", "std"] } alloy-hardforks.workspace = true -alloy-primitives = { workspace = true, features = ["keccak-cache-global", "secp256k1", "serde", "std"] } +alloy-primitives = { workspace = true, features = ["keccak-cache-global", "map-indexmap", "secp256k1", "serde", "std"] } alloy-provider = { workspace = true, features = ["debug-api", "reqwest", "reqwest-rustls-tls"] } alloy-rpc-client = { workspace = true, features = ["reqwest"] } alloy-rpc-types-eth = { workspace = true, features = ["serde", "std"] } diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 48425b87e..48e5fd103 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -12,7 +12,7 @@ use alloy_eips::{ eip2930::{AccessList, AccessListItem}, eip7702::{Authorization, SignedAuthorization}, }; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, map::HashMap}; +use alloy_primitives::{Address, B256, Bytes, TxKind, U256, map::IndexMap}; use evm2::{ SpecId, env::{BlockEnv, BlockEnvExt}, @@ -24,7 +24,7 @@ use revm::{ primitives::TxKind as RevmTxKind, }; use secp256k1::{Message, SecretKey, ecdsa::RecoverableSignature}; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::{fmt, sync::OnceLock}; pub(crate) const CALLER: Address = Address::new([0x10; 20]); @@ -449,7 +449,7 @@ fn add_eip7702_accounts<'a>( balance: U256::from(1_000), nonce: 0, code: tiny_callee_code(rng, SpecId::PRAGUE), - storage: HashMap::default(), + storage: IndexMap::default(), }, ); @@ -492,7 +492,7 @@ fn upsert_account(accounts: &mut Vec, account: CaseAccount) { } fn eip7702_authority_account(address: Address, nonce: u64, code: Bytes) -> CaseAccount { - CaseAccount { address, balance: U256::ZERO, nonce, code, storage: HashMap::default() } + CaseAccount { address, balance: U256::ZERO, nonce, code, storage: IndexMap::default() } } fn eip7702_designation(address: Address) -> Bytes { @@ -739,17 +739,7 @@ pub(crate) struct CaseAccount { pub(crate) balance: U256, pub(crate) nonce: u64, pub(crate) code: Bytes, - #[serde(serialize_with = "serialize_storage")] - pub(crate) storage: HashMap, -} - -fn serialize_storage( - storage: &HashMap, - serializer: S, -) -> Result { - let mut entries = storage.iter().collect::>(); - entries.sort_unstable_by_key(|&(key, _)| key); - serializer.collect_map(entries) + pub(crate) storage: IndexMap, } mod spec_serde { @@ -824,30 +814,23 @@ mod tests { for seed in 0..512 { let mut rng = Gen::new(seed); let mut fresh_rng = Gen::new(seed); - let mut reused = generator.generate(&mut rng).clone(); - let mut fresh = CaseGenerator::default().generate(&mut fresh_rng).clone(); + let reused = generator.generate(&mut rng).clone(); + let fresh = CaseGenerator::default().generate(&mut fresh_rng).clone(); assert_eq!(rng.bytes(32), fresh_rng.bytes(32), "RNG state at seed {seed}"); - // HashMap iteration can select different storage keys, but not different counts. - for case in [&mut reused, &mut fresh] { - for tx in core::iter::once(&mut case.tx).chain(&mut case.extra_txs) { - for item in &mut tx.access_list.0 { - item.storage_keys.fill(B256::ZERO); - } - } - } assert_eq!(reused, fresh, "seed {seed}"); } } #[test] - fn storage_serialization_is_canonical() { - let mut case = CaseGenerator::default().generate(&mut Gen::new(1)).clone(); - case.accounts[0].storage = (0..32).map(|key| (U256::from(key), U256::ONE)).collect(); - let json = serde_json::to_vec(&case).unwrap(); - case.accounts[0].storage = (0..32).rev().map(|key| (U256::from(key), U256::ONE)).collect(); - assert_eq!(serde_json::to_vec(&case).unwrap(), json); - let decoded = serde_json::from_slice::(&json).unwrap(); - assert_eq!(decoded, case); - assert_eq!(serde_json::to_vec(&decoded).unwrap(), json); + fn serialized_case_is_reproducible_and_round_trips() { + for seed in 0..64 { + let case = CaseGenerator::default().generate(&mut Gen::new(seed)).clone(); + let json = serde_json::to_vec(&case).unwrap(); + let fresh = CaseGenerator::default().generate(&mut Gen::new(seed)).clone(); + assert_eq!(serde_json::to_vec(&fresh).unwrap(), json, "seed {seed}"); + let decoded = serde_json::from_slice::(&json).unwrap(); + assert_eq!(decoded, case, "seed {seed}"); + assert_eq!(serde_json::to_vec(&decoded).unwrap(), json, "seed {seed}"); + } } } diff --git a/crates/cli/src/fuzzer/minimize.rs b/crates/cli/src/fuzzer/minimize.rs index c7582acf1..cdfe8ff67 100644 --- a/crates/cli/src/fuzzer/minimize.rs +++ b/crates/cli/src/fuzzer/minimize.rs @@ -33,7 +33,7 @@ fn minimize_storage(backends: &mut [&mut dyn EvmBackend; 2], case: &mut EvmCase) for account_index in 0..case.accounts.len() { while let Some(key) = case.accounts[account_index].storage.keys().min().copied() { let mut candidate = case.clone(); - candidate.accounts[account_index].storage.remove(&key); + candidate.accounts[account_index].storage.shift_remove(&key); if differs(backends, &candidate) { *case = candidate; } else { From ffa05b618652cf6e0ac023b950849df739f04dac Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:13:43 +0100 Subject: [PATCH 068/159] refactor(state): derive touched and selfdestructed from account flags (#947) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Finalization erased or duplicated account facts that `AccountFlags` already holds, and two compensating pieces of state grew around that. `finalize_transaction` cleared `TOUCHED` and set a second flag, `FINALIZED_TOUCHED`, so that `AccountChanges::touched` could still report the touch. Rollback then had to exempt the new flag. Finalization now keeps `TOUCHED`, `touched` reads it, and `FINALIZED_TOUCHED` and its exemption are gone. Nothing in the tree reattaches a finalized `PendingState` with `set_pending_state` and executes on it again without committing first; the doc of `set_pending_state` now says that touch and selfdestruct flags stay set, so a caller must commit before the next transaction. The `selfdestructs` set in `StateInner`, `StateSnapshot` and `PendingState` duplicated `DESTROYED`. Rollback reconciled the two on every `AccountChange`, with a hash lookup per entry, and after EIP-8246 they drifted apart on purpose. The set is removed. `finalize_transaction` now collects `DESTROYED` accounts in the single pass over the overlay that it already made for touched accounts, and handles them first. `DESTROYED` stays set on EIP-8246 survivors, and `selfdestructed` and `is_destructed` read the flag. `is_existing_dead` also matched an account with no present info but an original. Only `delete_for_finalization` produces that state, and the selfdestruct loop had just run it and wiped the storage, so the EIP-161 sweep deleted and wiped every pre-EIP-8246 selfdestructed account a second time. It now matches only an account whose present info is empty. The test-only `prepare_isolated_state` and `merge_isolated_state` helpers, and their tests, are removed. They outlived the public isolated-state API, and clearing `DESTROYED` when an EIP-8246 survivor is reset served only their pending check. SELFDESTRUCT and every revert lose a hash lookup, and finalization no longer takes and restores the set or writes flags on every touched account. I counted instructions with callgrind (AMD EPYC 4585PX, `bench` profile built for x86-64-v3, base and head in worktrees of equal path length), collecting only `Runner::run`, the transaction and commit the benchmark times, for one execution of each of the 99 single-transaction `evm` benchmark cases, twice per side. Two runs of the same build differ by up to about 0.01% on the large cases because of hash seeds, so a change inside that spread is noise. | Case | Base (Ir) | Head (Ir) | Change | | :--- | ---: | ---: | ---: | | counter | 13,382 | 13,362 | 🟢 -0.15% | | erc20_transfer | 25,219,717 | 25,219,690 | ⚪ noise | | uniswap_v2_pair | 13,441 | 13,414 | 🟢 -0.20% | | seaport | 15,896 | 15,869 | 🟢 -0.17% | | snailtracer | 441,481,988 | 441,472,482 | ⚪ noise | | precompile/sha256/size_64 | 12,584 | 12,228 | 🟢 -2.8% | | all 99 cases, summed | 5,495,960,152 | 5,495,794,714 | 🟢 -0.003% | | whole bench process, including the mainnet replay | 7,947,764,744 | 7,947,538,320 | 🟢 -0.003% | Of the 99 cases, 34 improve beyond the run-to-run spread, 7 get worse by it (the largest is 3.6K Ir on a 369M Ir case), and 58 are within it. The saving is about 30 instructions per transaction, so it shows only on the cheapest cases. The mainnet replay alone does not separate from noise. --- crates/evm2/src/evm/bal/list.rs | 19 +- crates/evm2/src/evm/inspector.rs | 62 ------ crates/evm2/src/evm/mod.rs | 11 +- crates/evm2/src/evm/state/account.rs | 41 ++-- crates/evm2/src/evm/state/journal.rs | 3 +- crates/evm2/src/evm/state/mod.rs | 275 ++++----------------------- crates/evm2/src/evm/state/pending.rs | 37 ++-- crates/evm2/src/evm/state/storage.rs | 18 -- 8 files changed, 70 insertions(+), 396 deletions(-) diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index 3ffe51e88..51531cc6f 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -307,7 +307,7 @@ mod tests { bytecode::Bytecode, evm::{ bal::{AccountInfoBal, BalChanges, BalCodeChange, StorageBal}, - state::{Account, AccountInfo, StorageOverlay, StorageSlot, Tracked}, + state::{Account, AccountFlags, AccountInfo, StorageOverlay, StorageSlot, Tracked}, }, }; use alloc::{vec, vec::Vec}; @@ -316,10 +316,7 @@ mod tests { CodeChange as AlloyCodeChange, NonceChange as AlloyNonceChange, SlotChanges as AlloySlotChanges, StorageChange as AlloyStorageChange, }; - use alloy_primitives::{ - B256, Bytes, U256, - map::{AddressSet, U256Map}, - }; + use alloy_primitives::{B256, Bytes, U256, map::U256Map}; fn code(byte: u8) -> (B256, Bytecode) { let bytecode = Bytecode::new_raw(vec![byte].into()); @@ -459,10 +456,7 @@ mod tests { overlay.slots.insert(U256::from(5), slot(U256::ZERO, U256::from(42))); overlay.slots.insert(U256::from(6), slot(U256::from(7), U256::from(7))); let account = Account { storage: overlay, ..account }; - let pending = PendingState { - accounts: AddressMap::from_iter([(address, account)]), - selfdestructs: Default::default(), - }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; let mut bal = Bal::new(); bal.commit(idx(1), pending); @@ -499,11 +493,8 @@ mod tests { }; let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; overlay.slots.insert(U256::from(5), slot(U256::from(42), U256::from(42))); - let account = Account { storage: overlay, ..account }; - let pending = PendingState { - accounts: AddressMap::from_iter([(address, account)]), - selfdestructs: AddressSet::from_iter([address]), - }; + let account = Account { storage: overlay, flags: AccountFlags::DESTROYED, ..account }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; let mut bal = Bal::new(); bal.commit(idx(2), pending); diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 373819166..6ef6dc767 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -1780,68 +1780,6 @@ mod tests { ); } - #[test] - fn isolated_transaction_resets_transient_state_and_merges_storage() { - let target = Address::with_last_byte(0xbb); - let caller = Address::with_last_byte(0x99); - for reverts in [false, true] { - let mut parent = State::new(EmptyDB::default()); - // Return the initial persistent and transient values, then write persistent storage. - let code = vec![ - op::PUSH0, - op::SLOAD, - op::PUSH0, - op::MSTORE, - op::PUSH0, - op::TLOAD, - op::PUSH1, - 32, - op::MSTORE, - op::PUSH1, - 8, - op::PUSH0, - op::SSTORE, - op::PUSH1, - 64, - op::PUSH0, - if reverts { op::REVERT } else { op::RETURN }, - ]; - parent.account(&target).unwrap().set_code_slow(legacy_bytecode(code)); - parent.account(&caller).unwrap().set_balance(Word::from(1_000_000_000)); - parent.storage_slot(&target, Word::ZERO).unwrap().set(Word::from(7)); - parent.storage_slot(&target, Word::ZERO).unwrap().warm(); - parent.tstore(&target, &Word::ZERO, &Word::from(9)); - let checkpoint = parent.checkpoint(); - let mut child = Evm::::new( - SpecId::CANCUN, - BlockEnvExt::default(), - ethereum_tx_registry(SpecId::CANCUN), - EmptyDB::default(), - Precompiles::base(SpecId::CANCUN), - ); - child.state_mut().set_pending_state(parent.prepare_isolated_state()); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - to: TxKind::Call(target), - gas_limit: 100_000, - ..Default::default() - }), - caller, - ); - let output = child.transact(&tx).unwrap().detach(); - assert_eq!(output.result.status, !reverts); - assert_eq!(Word::from_be_slice(&output.result.output[..32]), Word::from(7)); - assert_eq!(Word::from_be_slice(&output.result.output[32..]), Word::ZERO); - parent.merge_isolated_state(output.pending_state); - assert_eq!(parent.checkpoint(), checkpoint); - let slot = parent.storage_slot(&target, Word::ZERO).unwrap(); - assert_eq!(slot.original(), Word::ZERO); - assert_eq!(slot.current(), Word::from(if reverts { 7 } else { 8 })); - assert!(slot.is_warm()); - assert_eq!(parent.tload(&target, &Word::ZERO), Word::from(9)); - } - } - #[test] fn inspector_gas_changes_survive_dispatch_errors() { struct GasEdit; diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 5300f6129..ea9b467b3 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -34,9 +34,9 @@ //! 1. **Accepted overlay**: transaction-boundary state accepted by prior commits. It shadows the //! wrapped database and is visible to later transactions executed by the same [`Evm`]. //! 2. **Transaction scratch**: writes, warm-access state, transient storage, journal entries, -//! touched accounts, selfdestruct markers, and logs for the currently executing transaction. It -//! is cleared after `commit`, `commit_to`, `commit_with`, `discard`, `discard_with`, or `detach` -//! while retaining capacity where possible. +//! touched and selfdestructed account flags, and logs for the currently executing transaction. +//! It is cleared after `commit`, `commit_to`, `commit_with`, `discard`, `discard_with`, or +//! `detach` while retaining capacity where possible. //! 3. **Block accumulator**: optional block-level state output. It coalesces committed transaction //! writes and keeps block-boundary originals. //! @@ -4323,12 +4323,11 @@ mod tests { assert!(info.is_empty_code_hash()); let pending = state.take_pending_state(); - assert!(pending.accounts.contains_key(&contract), "account change recorded"); - assert!(pending.selfdestructs.contains(&contract)); + assert!(pending.accounts[&contract].flags.contains(AccountFlags::DESTROYED)); } #[test] - fn commit_transaction_clears_detached_selfdestructs() { + fn commit_transaction_clears_detached_destroyed_flag() { let contract = Address::from([0x44; 20]); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 84acd2882..276ddcfa2 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -172,11 +172,11 @@ bitflags! { pub struct AccountFlags: u8 { /// The account is warm in the current transaction. const WARM = 1 << 0; - /// The account is touched for transaction-finalization account-lifetime rules. + /// The account is touched for transaction-finalization account-lifetime rules. Kept + /// through finalization. const TOUCHED = 1 << 1; - /// Touch metadata retained after finalization consumes [`Self::TOUCHED`]. - const FINALIZED_TOUCHED = 1 << 2; - /// The account has been self-destructed in the current transaction. + /// The account has been self-destructed in the current transaction. Kept through + /// finalization, also on an EIP-8246 survivor. const DESTROYED = 1 << 3; /// The present overlay account was created in the current transaction. const CREATED = 1 << 4; @@ -210,8 +210,7 @@ pub(crate) struct Account { pub(crate) original: Option, /// Present account overlay after mutations. `None` means the account is absent/deleted. pub(crate) present: Option, - /// Lifecycle flags: warm, touched, finalized-touched, destroyed, created, keeps-storage, and - /// code changed. + /// Lifecycle flags: warm, touched, destroyed, created, keeps-storage, and code changed. pub(crate) flags: AccountFlags, /// Persistent transaction storage owned by this account. pub(crate) storage: StorageOverlay, @@ -224,14 +223,6 @@ impl Account { Self { original, present, ..Self::default() } } - /// Applies isolated execution changes, retaining parent originals and account warmth. - #[cfg(test)] - pub(crate) fn merge_isolated(&mut self, child: Self) { - self.present = child.present; - self.flags |= child.flags - AccountFlags::WARM - AccountFlags::FINALIZED_TOUCHED; - self.storage.merge_isolated(child.storage); - } - /// Marks the account as created during the transaction, which also flags its code as changed. /// /// The first creation mark decides whether the account keeps its storage baseline. @@ -273,7 +264,6 @@ impl Account { #[inline] pub(crate) fn is_existing_dead(&self) -> bool { self.present.as_ref().is_some_and(AccountInfo::is_empty) - || (self.present.is_none() && self.original.is_some()) } /// Returns the account's new bytecode keyed by code hash when its code changed during the @@ -292,14 +282,14 @@ impl Account { } /// Returns this account's transaction changes. - pub(super) fn changes(&self, address: Address, selfdestructed: bool) -> AccountChanges<'_> { + pub(super) fn changes(&self, address: Address) -> AccountChanges<'_> { AccountChanges { address, original: self.original.as_ref(), current: self.present.as_ref(), created: self.is_created(), - selfdestructed, - touched: self.flags.contains(AccountFlags::FINALIZED_TOUCHED), + selfdestructed: self.flags.contains(AccountFlags::DESTROYED), + touched: self.flags.contains(AccountFlags::TOUCHED), code: self.changed_code(), storage: &self.storage, } @@ -447,8 +437,8 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Returns whether the account has been marked self-destructed in the current transaction. #[inline] - pub fn is_destructed(&self) -> bool { - self.inner.selfdestructs.contains(&self.address) + pub const fn is_destructed(&self) -> bool { + self.tracked.flags.contains(AccountFlags::DESTROYED) } /// Returns whether the account is touched for transaction-finalization account-lifetime rules. @@ -465,9 +455,8 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Returns whether the account is dead by the EIP-161 definition while existing in the overlay. /// - /// An account is existing-dead when it has zero nonce, zero balance, and empty code, or when it - /// was deleted in this transaction but existed at the transaction boundary. Spurious Dragon - /// deletes such touched accounts during transaction finalization. + /// An account is existing-dead when it has zero nonce, zero balance, and empty code. Spurious + /// Dragon deletes such touched accounts during transaction finalization. #[inline] pub fn is_existing_dead(&self) -> bool { self.tracked.is_existing_dead() @@ -544,14 +533,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Marks the account self-destructed in the current transaction, recording a revert snapshot /// the first time and touching the account. /// - /// Touching makes the account participate in EIP-158/161 cleanup, and the self-destruct set - /// membership is undone by [`State::rollback`](super::State::rollback). + /// Touching makes the account participate in EIP-158/161 cleanup, and + /// [`State::rollback`](super::State::rollback) restores the destroyed flag. #[inline] pub fn mark_destructed(&mut self) { if !self.tracked.flags.contains(AccountFlags::DESTROYED) { self.record_change(); self.tracked.flags.insert(AccountFlags::DESTROYED); - self.inner.selfdestructs.insert(self.address); } self.touch(); } @@ -727,7 +715,6 @@ impl<'a, 'db> AccountHandle<'a, 'db> { #[inline] pub(crate) fn reset_selfdestructed_for_finalization(&mut self) { let balance = self.balance(); - self.tracked.flags.remove(AccountFlags::DESTROYED); self.tracked.present = Some(AccountInfo::default().with_balance(balance)); } diff --git a/crates/evm2/src/evm/state/journal.rs b/crates/evm2/src/evm/state/journal.rs index 4c4742889..410849bc2 100644 --- a/crates/evm2/src/evm/state/journal.rs +++ b/crates/evm2/src/evm/state/journal.rs @@ -156,8 +156,7 @@ mod tests { assert_eq!(state.account(&precompile3).unwrap().is_touched(), touched); assert!(!state.account(&other).unwrap().is_touched()); - // Finalization consumes the touch but keeps it in the reported changes, also when run - // twice. + // Finalization keeps the touch in the reported changes, also when run twice. state.finalize_transaction(Version::base(spec)).unwrap(); state.finalize_transaction(Version::base(spec)).unwrap(); let pending = state.take_pending_state(); diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index bacac5775..f7198d7af 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -39,7 +39,7 @@ use crate::{ use alloc::{boxed::Box, sync::Arc, vec::Vec}; use alloy_primitives::{ Address, B256, KECCAK256_EMPTY, Log, - map::{AddressMap, AddressSet, hash_map}, + map::{AddressMap, hash_map}, }; use core::{ mem, @@ -74,7 +74,6 @@ pub struct StateSnapshot { prewarm_set: PrewarmSet, journal: Vec, logs: Vec, - selfdestructs: AddressSet, } impl StateSnapshot { @@ -94,7 +93,6 @@ impl StateSnapshot { prewarm_set: self.prewarm_set, journal: self.journal, logs: self.logs, - selfdestructs: self.selfdestructs, }, } } @@ -119,7 +117,6 @@ impl State<'_> { prewarm_set: self.prewarm_set.clone(), journal: self.journal.clone(), logs: self.logs.clone(), - selfdestructs: self.selfdestructs.clone(), } } @@ -151,7 +148,6 @@ impl State<'_> { database, journal: Vec::new(), logs: Vec::new(), - selfdestructs: AddressSet::default(), prewarm_set: PrewarmSet::new(), }, } @@ -189,8 +185,6 @@ pub struct StateInner<'a> { journal: Vec, /// Logs emitted by the current transaction. logs: Vec, - /// Accounts self-destructed in the current transaction. - selfdestructs: AddressSet, /// Pre-warmed set: precompiles, coinbase, and the EIP-2930 access list. prewarm_set: PrewarmSet, } @@ -228,7 +222,6 @@ impl<'a> State<'a> { database: CacheDB::new(initial), journal: Vec::new(), logs: Vec::new(), - selfdestructs: AddressSet::default(), prewarm_set: PrewarmSet::new(), }, } @@ -513,7 +506,7 @@ impl<'a> State<'a> { accounts, storage_pool, transient_storage, - inner: StateInner { prewarm_set, journal, selfdestructs, logs, database: _ }, + inner: StateInner { prewarm_set, journal, logs, database: _ }, .. } = self; for account in accounts.values_mut() { @@ -523,7 +516,6 @@ impl<'a> State<'a> { transient_storage.clear(); prewarm_set.clear(); journal.clear(); - selfdestructs.clear(); logs.clear(); } @@ -843,27 +835,15 @@ impl<'a> State<'a> { }; match entry { JournalEntry::AccountChange { address, previous, previous_flags } => { - // Reconcile the self-destruct set with the restored destroyed flag. - let was_destroyed = self - .accounts - .get(&address) - .is_some_and(|entry| entry.flags.contains(AccountFlags::DESTROYED)); - let previous_destroyed = previous_flags.contains(AccountFlags::DESTROYED); - if was_destroyed && !previous_destroyed { - self.selfdestructs.remove(&address); - } else if !was_destroyed && previous_destroyed { - self.selfdestructs.insert(address); - } if let Some(entry) = self.accounts.get_mut(&address) { let discarded_storage = entry.discards_storage(); entry.present = previous; // EIP-161 preserves the historical Yellow Paper K.1 precompile-3 touch. let keep_touch = features.contains(EvmFeatures::EIP161) && address == Address::with_last_byte(3); - // Neither the finalized touch nor the storage baseline choice is journaled, - // so rollback never restores them. - let mut kept = - AccountFlags::FINALIZED_TOUCHED | AccountFlags::KEEPS_STORAGE; + // The storage baseline choice is not journaled, so rollback never restores + // it. + let mut kept = AccountFlags::KEEPS_STORAGE; if keep_touch { kept |= AccountFlags::TOUCHED; } @@ -936,10 +916,26 @@ impl<'a> State<'a> { /// turns that substate into account deletions, storage wipes, balance-only /// selfdestruct resets (EIP-8246), or pre-EIP-161 empty-account materialization. pub(crate) fn finalize_transaction(&mut self, version: &Version) -> DbResult<()> { - let selfdestructs = mem::take(&mut self.selfdestructs); + // Finalization neither touches nor destroys accounts, so one pass over the overlay selects + // both sets. Selfdestructed accounts are always touched, so they are not selected twice. + // Only the selected accounts need another lookup, and they are rare. EIP-161 deletes + // touched dead accounts at transaction finalization. Before EIP-161, touching a + // non-existent account materializes it as empty. + let eip161 = version.feature(EvmFeatures::EIP161); + let mut destroyed = Vec::new(); + let mut finalize = Vec::new(); + for (&address, entry) in &self.accounts { + if entry.flags.contains(AccountFlags::DESTROYED) { + destroyed.push(address); + } else if entry.flags.contains(AccountFlags::TOUCHED) + && if eip161 { entry.is_existing_dead() } else { entry.present.is_none() } + { + finalize.push(address); + } + } let eip8246 = version.feature(EvmFeatures::EIP8246); - for address in &selfdestructs { + for address in &destroyed { // EIP-8246: a self-destructed account that still holds balance is preserved as a // balance-only account instead of being burned. One with no balance is removed. The // handle is scoped so its `AccountChange` flushes on drop before the storage wipe. @@ -961,28 +957,6 @@ impl<'a> State<'a> { if zero_loaded_only { storage.zero_loaded() } else { storage.wipe() } } - // Finalization never touches accounts, so clearing touches in the same pass that selects - // the touched accounts to finalize sees the same set. Only those accounts need another - // lookup, and they are rare. - let eip161 = version.feature(EvmFeatures::EIP161); - let mut finalize = Vec::new(); - for (&address, entry) in &mut self.accounts { - if !entry.flags.contains(AccountFlags::TOUCHED) { - continue; - } - entry.flags.remove(AccountFlags::TOUCHED); - entry.flags.insert(AccountFlags::FINALIZED_TOUCHED); - // EIP-161 deletes touched dead accounts at transaction finalization. Before EIP-161, - // touching a non-existent account materializes it as empty. - let needs_finalization = if eip161 { - entry.is_existing_dead() - } else { - entry.present.is_none() && !selfdestructs.contains(&address) - }; - if needs_finalization { - finalize.push(address); - } - } for address in &finalize { if eip161 { self.account(address)?.delete_for_finalization(); @@ -991,18 +965,13 @@ impl<'a> State<'a> { self.materialize_empty_account_for_finalization(address)?; } } - - // Restore the selfdestruct set without clearing it: `take_pending_state` moves it into the - // detached [`PendingState`] to flag selfdestructed accounts, and `clear_transaction_state` - // clears it at the end of the transaction lifecycle. - self.selfdestructs = selfdestructs; Ok(()) } /// Returns a borrowed view of the transaction layer's changes, without detaching it. #[inline] - pub(crate) fn transaction_changes(&self) -> TxStateChanges<'_> { - TxStateChanges::new(&self.accounts, &self.selfdestructs) + pub(crate) const fn transaction_changes(&self) -> TxStateChanges<'_> { + TxStateChanges::new(&self.accounts) } /// Detaches the transaction overlay into an owned [`PendingState`]. @@ -1010,81 +979,30 @@ impl<'a> State<'a> { /// The remaining transaction scratch (journal, logs, warm sets, transient storage) is left for /// [`Self::clear_transaction_state`]. pub(crate) fn take_pending_state(&mut self) -> PendingState { - PendingState { - accounts: mem::take(&mut self.accounts), - selfdestructs: mem::take(&mut self.inner.selfdestructs), - } - } - - /// Copies loaded state for an isolated transaction, without transaction scratch. - /// - /// Account and slot originals become their current values, runtime slot warmth is cleared, and - /// account warmth is retained only for pre-warmed addresses. Other account metadata is - /// preserved. - #[cfg(test)] - pub(crate) fn prepare_isolated_state(&self) -> PendingState { - let mut accounts = self.accounts.clone(); - for (address, account) in &mut accounts { - account.original = account.present.clone(); - account.flags.set(AccountFlags::WARM, self.inner.prewarm_set.is_warm(address)); - for slot in account.storage.slots.values_mut() { - slot.value = Tracked::new(slot.value.current); - slot.is_warm = false; - } - } - PendingState { accounts, selfdestructs: self.inner.selfdestructs.clone() } - } - - /// Merges an isolated transaction's returned state without adding journal entries. - /// - /// Existing account and slot originals are retained, as is existing account warmth. Slot - /// warmth is combined, current values are replaced, and account lifecycle flags are combined. - /// Newly loaded accounts and slots retain their child metadata. Journals, logs, pre-warmed - /// sets, transient storage and backing databases are unchanged. - #[cfg(test)] - pub(crate) fn merge_isolated_state(&mut self, child: PendingState) { - for (address, account) in child.accounts { - match self.accounts.entry(address) { - hash_map::Entry::Vacant(entry) => { - entry.insert(account); - } - hash_map::Entry::Occupied(mut entry) => { - let parent = entry.get_mut(); - parent.merge_isolated(account); - } - } - } - for address in child.selfdestructs { - let still_pending = self - .accounts - .get(&address) - .is_some_and(|account| account.flags.contains(AccountFlags::DESTROYED)); - if still_pending { - self.inner.selfdestructs.insert(address); - } - } + PendingState { accounts: mem::take(&mut self.accounts) } } /// Reattaches a detached [`PendingState`] as the current transaction overlay, replacing it. /// /// This is the inverse of the detach performed by - /// [`ExecutedTx::detach`](crate::ExecutedTx::detach): the pending accounts, storage, and - /// selfdestruct set become the transaction layer again, as if the transaction had just been - /// finalized. Other transaction scratch (journal, logs, warm sets, transient storage) is not + /// [`ExecutedTx::detach`](crate::ExecutedTx::detach): the pending accounts and storage become + /// the transaction layer again, as if the transaction had just been finalized. Touch and + /// selfdestruct flags are kept, so finalizing again would reset EIP-8246 survivors a second + /// time: call [`Self::commit_transaction`] before executing another + /// transaction. Other transaction scratch (journal, logs, warm sets, transient storage) is not /// affected. pub fn set_pending_state(&mut self, pending: PendingState) { for account in self.accounts.values_mut() { self.storage_pool.clear_overlay(&mut account.storage); } self.accounts = pending.accounts; - self.inner.selfdestructs = pending.selfdestructs; } /// Accepts the current transaction's state transition into the accepted overlay. /// /// This advances the in-memory accepted overlay by the transaction's write-set and clears the - /// transaction account/storage layers and selfdestruct markers. It does not take logs or write - /// to the wrapped backing database. + /// transaction account/storage layers. It does not take logs or write to the wrapped backing + /// database. pub fn commit_transaction(&mut self) { // The transaction overlay is folded into the accepted-overlay database directly, without // detaching it. @@ -1093,7 +1011,6 @@ impl<'a> State<'a> { self.storage_pool.clear_overlay(&mut account.storage); } self.accounts.clear(); - self.inner.selfdestructs.clear(); } } @@ -1709,107 +1626,6 @@ mod tests { } } - #[test] - fn isolated_state_resets_originals_and_preserves_parent_metadata() { - let address = Address::with_last_byte(42); - let prewarmed = Address::with_last_byte(43); - let created = Address::with_last_byte(44); - let destroyed = Address::with_last_byte(45); - let wiped = Address::with_last_byte(46); - let child_only = Address::with_last_byte(47); - let mut parent = State::new(EmptyDB::default()); - parent.account(&address).unwrap().warm(); - parent.prewarm(&prewarmed); - assert!(parent.account(&prewarmed).unwrap().is_warm()); - { - let mut created_account = parent.account(&created).unwrap(); - created_account.set_balance(Word::ONE); - created_account.mark_created(); - } - parent.account(&destroyed).unwrap().mark_destructed(); - parent.storage(&wiped).unwrap().wipe(); - let mut slot = parent.storage_slot(&address, Word::ZERO).unwrap(); - slot.set(Word::from(7)); - slot.warm(); - parent.tstore(&address, &Word::ZERO, &Word::from(9)); - let checkpoint = parent.checkpoint(); - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - assert!(!child.account(&address).unwrap().is_warm()); - assert!(child.account(&prewarmed).unwrap().is_warm()); - assert!(child.account(&created).unwrap().is_created()); - assert!(child.account(&destroyed).unwrap().is_destructed()); - assert!(child.storage(&wiped).unwrap().is_wiped()); - assert_eq!(child.tload(&address, &Word::ZERO), Word::ZERO); - let mut slot = child.storage_slot(&address, Word::ZERO).unwrap(); - assert_eq!((slot.original(), slot.current()), (Word::from(7), Word::from(7))); - assert!(!slot.is_warm()); - slot.set(Word::from(8)); - child.storage_slot(&address, Word::ONE).unwrap().set(Word::from(10)); - child.account(&child_only).unwrap().set_balance(Word::ONE); - child.storage_slot(&child_only, Word::ZERO).unwrap().set(Word::ONE); - parent.merge_isolated_state(child.take_pending_state()); - assert_eq!(parent.checkpoint(), checkpoint); - let slot = parent.storage_slot(&address, Word::ZERO).unwrap(); - assert_eq!((slot.original(), slot.current()), (Word::ZERO, Word::from(8))); - assert!(slot.is_warm()); - assert_eq!(parent.get_storage(&address, &Word::ONE), Some(Word::from(10))); - assert_eq!(parent.tload(&address, &Word::ZERO), Word::from(9)); - assert!(parent.accounts[&created].is_created()); - assert!(parent.inner.selfdestructs.contains(&destroyed)); - assert!(parent.accounts[&wiped].storage.wiped); - assert_eq!(parent.accounts[&child_only].present.as_ref().unwrap().balance, Word::ONE); - assert_eq!(parent.get_storage(&child_only, &Word::ZERO), Some(Word::ONE)); - } - - #[test] - fn isolated_state_rebases_account_original_and_preserves_parent_warmth() { - let address = Address::with_last_byte(42); - let cold_address = Address::with_last_byte(43); - let mut parent = State::new(EmptyDB::default()); - parent.account(&address).unwrap().set_balance(Word::from(7)); - parent.account(&cold_address).unwrap().set_balance(Word::ONE); - assert!(!parent.account(&address).unwrap().is_warm()); - assert!(!parent.account(&cold_address).unwrap().is_warm()); - parent.prewarm(&address); - - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - assert_eq!(child.accounts[&address].original, child.accounts[&address].present); - assert!(child.account(&address).unwrap().is_warm()); - assert!(!child.account(&cold_address).unwrap().is_warm()); - child.account(&cold_address).unwrap().warm(); - child.account(&address).unwrap().set_balance(Word::from(8)); - parent.merge_isolated_state(child.take_pending_state()); - - assert!(parent.accounts[&address].original.is_none()); - assert_eq!(parent.account(&address).unwrap().balance(), Word::from(8)); - assert!(parent.account(&address).unwrap().is_warm()); - assert!(!parent.account(&cold_address).unwrap().is_warm()); - } - - #[test] - fn isolated_state_reinserts_restored_slot_after_wipe() { - let address = Address::with_last_byte(42); - let key = Word::ONE; - let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); - db.insert_account_storage(&address, &key, &Word::from(5)); - let mut parent = State::new(db); - parent.account(&address).unwrap(); - parent.storage_slot(&address, key).unwrap(); - parent.storage(&address).unwrap().wipe(); - - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - child.storage_slot(&address, key).unwrap().set(Word::from(5)); - parent.merge_isolated_state(child.take_pending_state()); - assert_eq!(parent.get_storage(&address, &key), Some(Word::from(5))); - parent.commit_transaction(); - - assert_eq!(parent.storage_slot_untracked(&address, &key).unwrap(), Word::from(5)); - } - #[test] fn wiped_slot_is_reported_as_a_read_of_its_original_value() { #[derive(Default)] @@ -1847,31 +1663,6 @@ mod tests { assert_eq!(reads.0, [(read_key, Word::from(5)), (written_key, Word::from(6))]); } - #[test] - fn isolated_state_does_not_replay_finalized_eip8246_selfdestruct() { - let address = Address::with_last_byte(42); - let key = Word::ONE; - let mut child = State::new(EmptyDB::default()); - { - let mut account = child.account(&address).unwrap(); - account.set_balance(Word::from(5)); - account.mark_destructed(); - } - child.finalize_transaction_(Version::base(crate::SpecId::AMSTERDAM)); - assert!(!child.accounts[&address].flags.contains(AccountFlags::DESTROYED)); - assert!(child.inner.selfdestructs.contains(&address)); - - let mut parent = State::new(EmptyDB::default()); - parent.merge_isolated_state(child.take_pending_state()); - assert!(!parent.inner.selfdestructs.contains(&address)); - parent.account(&address).unwrap().set_nonce(1); - parent.storage_slot(&address, key).unwrap().set(Word::from(9)); - parent.finalize_transaction_(Version::base(crate::SpecId::AMSTERDAM)); - - assert_eq!(parent.account(&address).unwrap().nonce(), 1); - assert_eq!(parent.storage_slot_untracked(&address, &key).unwrap(), Word::from(9)); - } - #[test] fn replaced_prewarm_set_keeps_runtime_warmth_and_journal() { let old_base = Address::with_last_byte(41); diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 186242ae1..178bd5fed 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -5,10 +5,7 @@ use super::{ StorageSlot, Tracked, }; use crate::interpreter::Word; -use alloy_primitives::{ - Address, - map::{AddressMap, AddressSet}, -}; +use alloy_primitives::{Address, map::AddressMap}; /// A transaction's finalized-but-uncommitted state, moved out of the EVM. /// @@ -32,8 +29,6 @@ pub struct PendingState { /// Accounts loaded by the transaction: transaction-boundary original info, present info, and /// account-lifetime flags. pub(crate) accounts: AddressMap, - /// Accounts selfdestructed by the transaction. - pub(crate) selfdestructs: AddressSet, } impl PendingState { @@ -101,7 +96,7 @@ impl PendingState { /// Returns a borrowed view of the transaction's changes. #[inline] pub const fn changes(&self) -> TxStateChanges<'_> { - TxStateChanges::new(&self.accounts, &self.selfdestructs) + TxStateChanges::new(&self.accounts) } /// Returns each loaded account's changes, in an unspecified account order. @@ -141,44 +136,39 @@ impl StateChangeSource for PendingState { /// transaction wrote before deciding to commit or discard it, without detaching the state. #[derive(Clone, Copy, Debug, Default)] pub struct TxStateChanges<'a> { - inner: Option<(&'a AddressMap, &'a AddressSet)>, + accounts: Option<&'a AddressMap>, } impl<'a> TxStateChanges<'a> { #[inline] - pub(crate) const fn new( - accounts: &'a AddressMap, - selfdestructs: &'a AddressSet, - ) -> Self { - Self { inner: Some((accounts, selfdestructs)) } + pub(crate) const fn new(accounts: &'a AddressMap) -> Self { + Self { accounts: Some(accounts) } } /// Returns a view with no changes. #[inline] pub const fn empty() -> Self { - Self { inner: None } + Self { accounts: None } } /// Returns whether the transaction loaded no accounts and no storage. #[inline] pub fn is_empty(&self) -> bool { - self.inner.is_none_or(|(accounts, _)| accounts.is_empty()) + self.accounts.is_none_or(AddressMap::is_empty) } /// Returns the current account information when the transaction loaded the account and it /// exists after the transaction. #[inline] pub fn account_info(&self, address: &Address) -> Option<&'a AccountInfo> { - self.inner?.0.get(address).and_then(|account| account.present.as_ref()) + self.accounts?.get(address).and_then(|account| account.present.as_ref()) } /// Returns each loaded account's changes, in an unspecified account order. #[inline] pub fn account_changes(self) -> impl Iterator> { - self.inner.into_iter().flat_map(|(accounts, selfdestructs)| { - accounts.iter().map(|(&address, account)| { - account.changes(address, selfdestructs.contains(&address)) - }) + self.accounts.into_iter().flat_map(|accounts| { + accounts.iter().map(|(&address, account)| account.changes(address)) }) } @@ -186,10 +176,7 @@ impl<'a> TxStateChanges<'a> { /// it. #[inline] pub fn account_changes_for(&self, address: &Address) -> Option> { - let (accounts, selfdestructs) = self.inner?; - accounts - .get(address) - .map(|account| account.changes(*address, selfdestructs.contains(address))) + self.accounts?.get(address).map(|account| account.changes(*address)) } } @@ -241,7 +228,7 @@ mod tests { state.insert_account(unchanged, Some(info.clone()), Some(info.clone())); let destroyed = Address::with_last_byte(0xdd); state.insert_account(destroyed, Some(info.clone()), Some(info)); - state.selfdestructs.insert(destroyed); + state.accounts.get_mut(&destroyed).unwrap().flags.insert(AccountFlags::DESTROYED); let changes = state.account_changes_for(&changed).unwrap(); assert_eq!(changes.address, changed); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index cc6beda08..86877ad02 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -23,24 +23,6 @@ pub struct StorageOverlay { } impl StorageOverlay { - /// Applies an isolated execution's slots, retaining parent originals and combining warmth. - #[cfg(test)] - pub(crate) fn merge_isolated(&mut self, child: Self) { - self.wiped |= child.wiped; - for (key, slot) in child.slots { - match self.slots.entry(key) { - hash_map::Entry::Vacant(entry) => { - entry.insert(slot); - } - hash_map::Entry::Occupied(mut entry) => { - let parent = entry.get_mut(); - parent.value.current = slot.value.current; - parent.is_warm |= slot.is_warm; - } - } - } - } - /// Returns the changed storage slots. /// /// A slot is changed when its current value differs from its transaction-boundary original, From c3744e648d532ec384dfc31d9f988742fced0a5f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:14:36 +0100 Subject: [PATCH 069/159] fix(jit-llvm): take llvm-config from llvm-sys and stop hiding warnings (#923) The `evm2-jit-llvm` build script searched `PATH` for `llvm-config` and ignored `LLVM_SYS_231_PREFIX`, so `lib.cpp` could compile against different LLVM headers than the library `llvm-sys` links, or panic with "no llvm-config found". It now reads the `llvm-config` that `llvm-sys` exports as `DEP_LLVM_23_CONFIG_PATH` and fails with a clear message when that is missing. It also passed `-w`, which hid every warning, including ones in `lib.cpp`. LLVM's include dirs from `--cxxflags` now go in as `-isystem`, so only LLVM's headers are quiet. This exposed one unused variable in `lib.cpp`: the leaked pass-config callback registration, now marked `[[maybe_unused]]`. No codegen change. --- crates/jit/llvm/build.rs | 62 ++++++++++++++++--------------------- crates/jit/llvm/cpp/lib.cpp | 2 +- scripts/jit/utils.py | 4 +-- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/crates/jit/llvm/build.rs b/crates/jit/llvm/build.rs index 4f551b65f..ed1fb8bdd 100644 --- a/crates/jit/llvm/build.rs +++ b/crates/jit/llvm/build.rs @@ -1,46 +1,38 @@ #![allow(missing_docs)] +use std::process::Command; + fn main() { println!("cargo:rerun-if-changed=cpp/lib.cpp"); - let cxxflags = ["llvm-config", "llvm-config-23"] - .into_iter() - .find_map(detect_cxxflags) - .expect("no llvm-config found"); - - cc::Build::new() - .cpp(true) - .flags(cxxflags.split_whitespace()) - .flag("-w") - .file("cpp/lib.cpp") - .compile("evm2_jit_llvm_cpp"); -} + // `llvm-sys` (`links = "llvm-23"`) exports the `llvm-config` it links against, so `lib.cpp` + // compiles against the same headers. + let llvm_config = std::env::var("DEP_LLVM_23_CONFIG_PATH") + .expect("DEP_LLVM_23_CONFIG_PATH is not set; `llvm-sys` did not report its llvm-config"); -/// Returns the `--cxxflags` reported by `llvm_config`, or `None` if it cannot be used. -/// -/// [`Command::output`] only reports whether the process could be spawned, so a `llvm-config` that -/// exists but exits with a non-zero status still yields `Ok`. Checking the status keeps such a -/// broken binary from shadowing a working one later in the candidate list. -fn detect_cxxflags(llvm_config: &str) -> Option { - let output = std::process::Command::new(llvm_config) + let output = Command::new(&llvm_config) .arg("--cxxflags") .output() - .inspect_err(|error| eprintln!("failed to run {llvm_config}: {error}")) - .ok()?; + .unwrap_or_else(|error| panic!("failed to run {llvm_config}: {error}")); + assert!( + output.status.success(), + "{llvm_config} --cxxflags failed with {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim(), + ); + let cxxflags = String::from_utf8(output.stdout) + .unwrap_or_else(|error| panic!("{llvm_config} --cxxflags is not valid UTF-8: {error}")); - let status = output.status; - if !status.success() { - let stderr = String::from_utf8_lossy(&output.stderr); - let stderr = stderr.trim(); - if stderr.is_empty() { - eprintln!("{llvm_config} --cxxflags failed with {status}"); - } else { - eprintln!("{llvm_config} --cxxflags failed with {status}: {stderr}"); - } - return None; + let mut build = cc::Build::new(); + build.cpp(true); + for flag in cxxflags.split_whitespace() { + // Mark LLVM's include dirs as system dirs to silence warnings from its headers only. A + // distro LLVM reports `-I/usr/include`, which as `-isystem` would precede the C++ standard + // library's own directories and break `#include_next`. + match flag.strip_prefix("-I") { + Some(dir) if dir != "/usr/include" => build.flag("-isystem").flag(dir), + _ => build.flag(flag), + }; } - - String::from_utf8(output.stdout) - .inspect_err(|error| eprintln!("{llvm_config} --cxxflags is not valid UTF-8: {error}")) - .ok() + build.file("cpp/lib.cpp").compile("evm2_jit_llvm_cpp"); } diff --git a/crates/jit/llvm/cpp/lib.cpp b/crates/jit/llvm/cpp/lib.cpp index 7d6d46030..e72bac64f 100644 --- a/crates/jit/llvm/cpp/lib.cpp +++ b/crates/jit/llvm/cpp/lib.cpp @@ -390,7 +390,7 @@ extern "C" void evm2_jit_llvm_set_dso_local(LLVMValueRef Val, bool Local) { /// The registration is leaked so that no destructor edits LLVM's global /// callback list at process exit while a compile thread may still read it. extern "C" void evm2_jit_llvm_expand_ir_insts_after_lsr() { - static auto *Callback = new RegisterTargetPassConfigCallback( + [[maybe_unused]] static auto *Callback = new RegisterTargetPassConfigCallback( [](TargetMachine &, legacy::PassManagerBase &, TargetPassConfig *PC) { // Only the IDs matter: the pipeline builds the inserted pass with its default // constructor, which uses `CodeGenOptLevel::None`. diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index e9c204241..92570c8ac 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -57,8 +57,8 @@ def cargo_build(root: str, target_dir: str | None = None) -> str: def llvm_env(root: str) -> dict[str, str]: """Put the LLVM version that ``root`` requires first in PATH, if it is installed. - Base and head can require different LLVM versions, and both llvm-sys and the - evm2-jit-llvm build script otherwise use whichever llvm-config comes first in PATH. + Base and head can require different LLVM versions, and llvm-sys otherwise uses + whichever llvm-config comes first in PATH. """ with open(os.path.join(root, "crates", "jit", "llvm", "Cargo.toml")) as f: # Older code selects LLVM through an inkwell feature, newer code through llvm-sys. From 67a17f45141181582a9807988e9a555ebf7c7a56 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:14:46 +0100 Subject: [PATCH 070/159] fix(eest): read the blob schedule from the fixture config (#953) The blockchain runner chose blob parameters from hard-coded mainnet BPO timestamps, carried a one-off `bpo2_blobs` flag for BPO2 to Amsterdam, and returned `None` for the four BPO transition forks, so those files were skipped and reported as passes. Fixtures already carry `config.blobSchedule`; `BlockchainTestCase` now deserializes `config` and the runner picks each block's `BlobParams` from the schedule and the fork active at that block. The BPO transition forks run with the Osaka spec and only change blob parameters (63 BPO fixtures now execute). A missing schedule entry from Cancun on is an error, not a default, so a legacy fixture with a Cancun or later network and no `blobSchedule` now fails. `evm2 capture` writes `config.chainid` and a `blobSchedule` for the fork its blocks run under, and uses new `BPO1` / `BPO2` networks for Osaka blocks after a BPO activation, so a capture is self-describing. A capture that spans two blob forks is rejected like one that spans two specs. The replay benchmark calls the runner's now-public `blob_params` instead of its own copy of the timestamps, and the committed mainnet replay fixture is rewritten with the schedule and the `BPO2` network (its blocks are after the BPO2 activation, which the old code inferred from the timestamp). A case on a fork the runner cannot execute (only the DAO fork is left; no default fixture uses it, but the legacy `bcHomesteadToDao` files do) now fails instead of counting as skipped. `EVM2_SKIP_FORKS` and the name filter still skip, since the user asked for those. --- crates/cli/src/capture/export.rs | 99 ++++++- crates/cli/src/replay_bench.rs | 38 +-- crates/eest/src/binary.rs | 8 +- crates/eest/src/blockchaintest/error.rs | 10 + crates/eest/src/blockchaintest/execute.rs | 304 +++++++++++++++++----- crates/eest/src/blockchaintest/mod.rs | 9 +- crates/eest/src/blockchaintest/types.rs | 83 ++++++ data/mainnet-25347446-25347455.bin.zst | Bin 4255570 -> 4255623 bytes 8 files changed, 444 insertions(+), 107 deletions(-) diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index a7b5dc2b1..58a5619dc 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -2,26 +2,30 @@ use super::{ error::CaptureError, model::{CapturedBlock, CapturedCase, CapturedInput, State as CapturedState}, }; +use crate::ethereum::mainnet_spec_for_header; use alloy_consensus::{ EthereumTxEnvelope, Header, TxEip4844, transaction::{Transaction as _, to_eip155_value}, }; -use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; +use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization, eip7840::BlobParams}; +use alloy_hardforks::EthereumHardfork; use alloy_primitives::{Address, B256, Bytes, FixedBytes, U256}; use evm2::SpecId; use evm2_eest::{ AccessListItem, TestAuthorization, blockchaintest::{ - Account, Block, BlockHash as EestBlockHash, BlockHeader, BlockchainTest, - BlockchainTestCase, ForkSpec, SealEngine, State as EestState, Transaction, Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHash as EestBlockHash, BlockHeader, + BlockchainTest, BlockchainTestCase, ChainConfig, ForkSpec, SealEngine, State as EestState, + Transaction, Withdrawal, }, }; use serde_json::json; use std::collections::BTreeMap; pub(super) fn suite(capture: &CapturedCase) -> Result { - let network = network(capture)?; let inputs = captured_blocks(capture)?; + let blob_fork = blob_fork(inputs)?; + let network = network(capture, blob_fork)?; let decoded_blocks = inputs.iter().map(export_block).collect::, CaptureError>>()?; let first = decoded_blocks.first().expect("blocks is not empty"); @@ -45,6 +49,10 @@ pub(super) fn suite(capture: &CapturedCase) -> Result Result<&[CapturedBlock], CaptureEr Ok(blocks) } -fn network(capture: &CapturedCase) -> Result { +/// Returns the mainnet fork that sets the blob parameters of every block, or `None` before Cancun. +fn blob_fork(blocks: &[CapturedBlock]) -> Result, CaptureError> { + let mut forks = blocks.iter().map(|input| mainnet_blob_fork(&input.block.header)); + let first = forks.next().expect("blocks is not empty")?; + for fork in forks { + if fork? != first { + return Err(CaptureError::MultipleSpecs); + } + } + Ok(first) +} + +fn mainnet_blob_fork(header: &Header) -> Result, CaptureError> { + let spec = mainnet_spec_for_header(header); + if !spec.enables(SpecId::CANCUN) { + return Ok(None); + } + let hardfork = EthereumHardfork::from_chain_and_timestamp( + alloy_chains::Chain::mainnet(), + header.timestamp, + ); + match hardfork { + Some(EthereumHardfork::Cancun) => Ok(Some(BlobFork::Cancun)), + Some(EthereumHardfork::Prague) => Ok(Some(BlobFork::Prague)), + Some(EthereumHardfork::Osaka) => Ok(Some(BlobFork::Osaka)), + Some(EthereumHardfork::Bpo1) => Ok(Some(BlobFork::Bpo1)), + Some(EthereumHardfork::Bpo2) => Ok(Some(BlobFork::Bpo2)), + Some(EthereumHardfork::Amsterdam) => Ok(Some(BlobFork::Amsterdam)), + _ => Err(CaptureError::UnsupportedSpec(spec as u32)), + } +} + +/// Returns the mainnet blob parameters of every fork up to `last`, or none before Cancun. +fn mainnet_blob_schedule(last: Option) -> BTreeMap { + [ + (BlobFork::Cancun, BlobParams::cancun()), + (BlobFork::Prague, BlobParams::prague()), + (BlobFork::Osaka, BlobParams::osaka()), + (BlobFork::Bpo1, BlobParams::bpo1()), + (BlobFork::Bpo2, BlobParams::bpo2()), + (BlobFork::Amsterdam, BlobParams::bpo2()), + ] + .into_iter() + .filter(|(fork, _)| Some(*fork) <= last) + .map(|(fork, params)| (fork, params.into())) + .collect() +} + +fn network(capture: &CapturedCase, blob_fork: Option) -> Result { let first = capture.versions.versions.first().ok_or(CaptureError::EmptyCapturedVersions)?; if capture.versions.versions.iter().any(|version| version.spec_id != first.spec_id) { return Err(CaptureError::MultipleSpecs); @@ -94,7 +150,11 @@ fn network(capture: &CapturedCase) -> Result { SpecId::SHANGHAI => Ok(ForkSpec::Shanghai), SpecId::CANCUN => Ok(ForkSpec::Cancun), SpecId::PRAGUE => Ok(ForkSpec::Prague), - SpecId::OSAKA => Ok(ForkSpec::Osaka), + SpecId::OSAKA => Ok(match blob_fork { + Some(BlobFork::Bpo1) => ForkSpec::BPO1, + Some(BlobFork::Bpo2) => ForkSpec::BPO2, + _ => ForkSpec::Osaka, + }), SpecId::AMSTERDAM => Ok(ForkSpec::Amsterdam), _ => Err(CaptureError::UnsupportedSpec(first.spec_id)), } @@ -278,3 +338,30 @@ fn export_authorization_list(authorizations: &[SignedAuthorization]) -> Vec>(), + [BlobFork::Cancun, BlobFork::Prague, BlobFork::Osaka, BlobFork::Bpo1] + ); + assert_eq!(schedule[&BlobFork::Bpo1].params().update_fraction, 8_346_193); + } +} diff --git a/crates/cli/src/replay_bench.rs b/crates/cli/src/replay_bench.rs index a02612452..97bb76dd0 100644 --- a/crates/cli/src/replay_bench.rs +++ b/crates/cli/src/replay_bench.rs @@ -4,7 +4,7 @@ //! transaction outcomes; timed runs use a no-op hook and include database setup, //! transaction decoding, execution and block commits. -use alloy_eips::{eip7702::SignedAuthorization, eip7840::BlobParams}; +use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, Keccak256, Log, TxKind, U256}; use alloy_rpc_types_eth::{AccessList as RpcAccessList, AccessListItem as RpcAccessListItem}; use evm2::{ @@ -19,7 +19,7 @@ use evm2_eest::{ blockchaintest::{ Block, BlockFinished, BlockHeader, BlockStarted, BlockchainTest, BlockchainTestCase, CaseStarted, ForkSpec, Hook, Transaction, TransactionFinished, TransactionStarted, - Withdrawal, + Withdrawal, blob_params, }, execute_blockchain_tests_suite, }; @@ -326,7 +326,7 @@ impl ReplayFixture { for (block_index, block) in case.blocks.iter().enumerate() { let header = block_header(block).expect("replay fixture block must carry a header"); - let block_env = revm_block_env(header, parent_excess_blob_gas, spec); + let block_env = revm_block_env(&self.name, case, header, parent_excess_blob_gas); let block_number = header.number.saturating_to::(); let transactions = block_transactions(block); @@ -553,24 +553,6 @@ fn block_withdrawals(block: &Block) -> &[Withdrawal] { block.rlp_decoded.as_ref().map(|decoded| decoded.withdrawals.as_slice()).unwrap_or_default() } -fn blob_params_for_timestamp(timestamp: U256, spec: SpecId) -> BlobParams { - const MAINNET_BPO1_TIMESTAMP: u64 = 1_765_290_071; - const MAINNET_BPO2_TIMESTAMP: u64 = 1_767_747_671; - - if spec.enables(SpecId::AMSTERDAM) || timestamp.saturating_to::() >= MAINNET_BPO2_TIMESTAMP - { - BlobParams::bpo2() - } else if timestamp.saturating_to::() >= MAINNET_BPO1_TIMESTAMP { - BlobParams::bpo1() - } else if spec.enables(SpecId::OSAKA) { - BlobParams::osaka() - } else if spec.enables(SpecId::PRAGUE) { - BlobParams::prague() - } else { - BlobParams::cancun() - } -} - fn fork_to_spec_id(fork: ForkSpec) -> SpecId { match fork { ForkSpec::Frontier => SpecId::FRONTIER, @@ -589,7 +571,7 @@ fn fork_to_spec_id(fork: ForkSpec) -> SpecId { ForkSpec::Shanghai => SpecId::SHANGHAI, ForkSpec::Cancun => SpecId::CANCUN, ForkSpec::Prague => SpecId::PRAGUE, - ForkSpec::Osaka => SpecId::OSAKA, + ForkSpec::Osaka | ForkSpec::BPO1 | ForkSpec::BPO2 => SpecId::OSAKA, ForkSpec::Amsterdam => SpecId::AMSTERDAM, other => panic!("replay fixture fork {other:?} is not supported"), } @@ -671,15 +653,21 @@ fn revm_seed_block_hashes(database: &mut RevmInMemoryDB, case: &BlockchainTestCa .insert(case.genesis_block_header.number, case.genesis_block_header.hash); } -fn revm_block_env(header: &BlockHeader, parent_excess_blob_gas: u64, spec: SpecId) -> RevmBlockEnv { +fn revm_block_env( + name: &str, + case: &BlockchainTestCase, + header: &BlockHeader, + parent_excess_blob_gas: u64, +) -> RevmBlockEnv { let excess_blob_gas = header .excess_blob_gas .map(|gas| gas.saturating_to::()) .unwrap_or(parent_excess_blob_gas); // The blob gas price is computed with the same `BlobParams` selection the // evm2 side uses, so both engines see an identical blob base fee. - let blob_gasprice = - blob_params_for_timestamp(header.timestamp, spec).calc_blob_fee(excess_blob_gas); + let blob_gasprice = blob_params(Path::new(name), name, case, header) + .expect("replay fixture must carry the blob schedule") + .map_or(1, |params| params.calc_blob_fee(excess_blob_gas)); RevmBlockEnv { number: header.number, beneficiary: header.coinbase, diff --git a/crates/eest/src/binary.rs b/crates/eest/src/binary.rs index 070ba40c2..134b634c3 100644 --- a/crates/eest/src/binary.rs +++ b/crates/eest/src/binary.rs @@ -1,7 +1,7 @@ use crate::{ blockchaintest::{ - Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, Transaction, Withdrawal, + Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, ChainConfig, + DecodedBlock, ForkSpec, SealEngine, State, Transaction, Withdrawal, }, tx::{AccessListItem, TestAuthorization}, }; @@ -38,6 +38,7 @@ struct BlockchainTestCaseRef<'a> { lastblockhash: &'a B256, network: &'a ForkSpec, seal_engine: &'a SealEngine, + config: &'a Option, } impl<'a> BlockchainTestCaseRef<'a> { @@ -52,6 +53,7 @@ impl<'a> BlockchainTestCaseRef<'a> { lastblockhash: &case.lastblockhash, network: &case.network, seal_engine: &case.seal_engine, + config: &case.config, } } } @@ -238,6 +240,7 @@ struct BlockchainTestCaseWire { lastblockhash: B256, network: ForkSpec, seal_engine: SealEngine, + config: Option, } impl From for BlockchainTestCase { @@ -252,6 +255,7 @@ impl From for BlockchainTestCase { lastblockhash: value.lastblockhash, network: value.network, seal_engine: value.seal_engine, + config: value.config, } } } diff --git a/crates/eest/src/blockchaintest/error.rs b/crates/eest/src/blockchaintest/error.rs index c1ccee263..e878aa53c 100644 --- a/crates/eest/src/blockchaintest/error.rs +++ b/crates/eest/src/blockchaintest/error.rs @@ -1,3 +1,4 @@ +use super::types::{BlobFork, ForkSpec}; use crate::{execution::ExecutionResourceError, tx::TxBuildError}; use evm2::registry::HandlerError; use std::{io, path::PathBuf}; @@ -46,6 +47,15 @@ pub(crate) enum TestErrorKind { /// Numeric value overflowed the target type. #[error("value overflows {0}")] Overflow(&'static str), + /// The runner does not support the test's fork. + #[error("unsupported fork: {0:?}")] + UnsupportedFork(ForkSpec), + /// The test's chain configuration has no blob parameters for the active fork. + #[error("blob schedule has no entry for {0:?}")] + MissingBlobSchedule(BlobFork), + /// The test's blob schedule has a zero base fee update fraction for the active fork. + #[error("blob schedule entry for {0:?} has a zero base fee update fraction")] + ZeroBlobUpdateFraction(BlobFork), /// Sender is required in blockchain tests. #[error("transaction sender is required")] MissingSender, diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 2b45dd4b4..52fd58c36 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -5,8 +5,8 @@ use super::{ TransactionFinished, TransactionStarted, }, types::{ - Account, Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, Transaction, - Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHeader, BlockchainTest, + BlockchainTestCase, ForkSpec, Transaction, Withdrawal, }, }; #[cfg(feature = "jit")] @@ -41,7 +41,7 @@ use evm2::{ }; #[cfg(feature = "jit")] use std::path::PathBuf; -use std::{mem, path::Path}; +use std::{collections::BTreeMap, mem, path::Path}; pub use crate::execution::ExecutionMode; @@ -210,7 +210,7 @@ fn execute_suite_with_resources( let mut summary = ExecuteSummary::default(); for (name, test_case) in &suite.0 { if !test_filter.matches(name) - || fork_schedule(test_case.network).is_none_or(|schedule| schedule.is_skipped()) + || fork_schedule(test_case.network).is_some_and(|schedule| schedule.is_skipped()) { summary.skipped += 1; continue; @@ -260,17 +260,14 @@ fn execute_case( hook: &mut dyn Hook, resources: &ExecutionResources, ) -> Result, TestError> { - let schedule = fork_schedule(test_case.network).expect("unsupported forks are skipped"); - let spec = schedule.initial; + let schedule = case_schedule(path, name, test_case)?; + let spec = schedule.initial.spec; let mut parent_block_hash = Some(test_case.genesis_block_header.hash); let mut parent_excess_blob_gas = test_case.genesis_block_header.excess_blob_gas.unwrap_or_default().saturating_to::(); - let mut block_env = block_env_from_header( - &test_case.genesis_block_header, - parent_excess_blob_gas, - &schedule, - spec, - ); + let mut block_env = + block_env_from_header(&test_case.genesis_block_header, parent_excess_blob_gas, &schedule) + .map_err(|err| TestError::case(path, name, err))?; let total_blocks = test_case.blocks.len(); let mut db_stats_counts = DbStatsCounts::default(); // `evm2 capture` keeps only the accounts the blocks touch and zeroes the genesis state @@ -427,7 +424,8 @@ fn execute_block( if let Some(header) = block_header(block) { block_hash = Some(header.hash); beacon_root = header.parent_beacon_block_root; - next_block_env = block_env_from_header(header, *parent_excess_blob_gas, schedule, spec); + next_block_env = block_env_from_header(header, *parent_excess_blob_gas, schedule) + .map_err(|err| TestError::case(path, name, err))?; this_excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()); expected_state_root = Some(header.state_root); } @@ -1048,11 +1046,13 @@ fn block_env_from_header( header: &BlockHeader, excess_blob_gas: u64, schedule: &ForkSchedule, - spec: SpecId, -) -> BlockEnv { +) -> Result { let excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()).unwrap_or(excess_blob_gas); - BlockEnvExt { + let blob_basefee = schedule + .blob_params(header.number, header.timestamp)? + .map_or(U256::ONE, |params| U256::from(params.calc_blob_fee(excess_blob_gas))); + Ok(BlockEnvExt { number: header.number, beneficiary: header.coinbase, timestamp: header.timestamp, @@ -1064,30 +1064,11 @@ fn block_env_from_header( } else { U256::ZERO }, - blob_basefee: U256::from( - schedule.blob_params(header.timestamp, spec).calc_blob_fee(excess_blob_gas), - ), + blob_basefee, slot_num: header.slot_number.unwrap_or_default(), ext: (), _non_exhaustive: (), - } -} - -fn blob_params_for_timestamp(timestamp: U256, spec: SpecId) -> BlobParams { - const MAINNET_BPO1_TIMESTAMP: u64 = 1_765_290_071; - const MAINNET_BPO2_TIMESTAMP: u64 = 1_767_747_671; - - if spec.enables(SpecId::AMSTERDAM) || timestamp.to::() >= MAINNET_BPO2_TIMESTAMP { - BlobParams::bpo2() - } else if timestamp.to::() >= MAINNET_BPO1_TIMESTAMP { - BlobParams::bpo1() - } else if spec.enables(SpecId::OSAKA) { - BlobParams::osaka() - } else if spec.enables(SpecId::PRAGUE) { - BlobParams::prague() - } else { - BlobParams::cancun() - } + }) } fn build_tx(raw: &Transaction) -> Result { @@ -1373,11 +1354,19 @@ fn is_blockchain_fork_skipped(spec: SpecId) -> bool { /// The specs a fork runs, in order. struct ForkSchedule { - initial: SpecId, - /// The spec that a transition fork switches to, and when. - transition: Option<(Activation, SpecId)>, - /// Whether the blob schedule is BPO2 throughout, as before the BPO2 to Amsterdam transition. - bpo2_blobs: bool, + initial: ForkStage, + /// The stage that a transition fork switches to, and when. + transition: Option<(Activation, ForkStage)>, + /// The test's blob parameters by fork. + blob_schedule: BTreeMap, +} + +/// What is active between two transitions: a spec, and the fork that sets the blob parameters. +#[derive(Clone, Copy)] +struct ForkStage { + spec: SpecId, + /// `None` before Cancun. + blob_fork: Option, } /// When a transition fork activates. @@ -1387,47 +1376,113 @@ enum Activation { Timestamp(u64), } +impl ForkStage { + /// A stage whose blob fork is the latest one the spec enables. + const fn new(spec: SpecId) -> Self { + let blob_fork = if spec.enables(SpecId::AMSTERDAM) { + Some(BlobFork::Amsterdam) + } else if spec.enables(SpecId::OSAKA) { + Some(BlobFork::Osaka) + } else if spec.enables(SpecId::PRAGUE) { + Some(BlobFork::Prague) + } else if spec.enables(SpecId::CANCUN) { + Some(BlobFork::Cancun) + } else { + None + }; + Self { spec, blob_fork } + } + + /// A stage of a blob parameters only fork, which keeps the Osaka spec. + const fn bpo(blob_fork: BlobFork) -> Self { + Self { spec: SpecId::OSAKA, blob_fork: Some(blob_fork) } + } +} + impl ForkSchedule { const fn fixed(spec: SpecId) -> Self { - Self { initial: spec, transition: None, bpo2_blobs: false } + Self::new(ForkStage::new(spec), None) } const fn transition(initial: SpecId, activation: Activation, spec: SpecId) -> Self { - Self { initial, transition: Some((activation, spec)), bpo2_blobs: false } + Self::new(ForkStage::new(initial), Some((activation, ForkStage::new(spec)))) } - const fn with_bpo2_blobs(mut self) -> Self { - self.bpo2_blobs = true; - self + const fn new(initial: ForkStage, transition: Option<(Activation, ForkStage)>) -> Self { + Self { initial, transition, blob_schedule: BTreeMap::new() } } - /// Returns the blob parameters of the block with this timestamp and spec. - fn blob_params(&self, timestamp: U256, spec: SpecId) -> BlobParams { - if self.bpo2_blobs { - BlobParams::bpo2() - } else { - blob_params_for_timestamp(timestamp, spec) + /// Returns the stage of the block with this number and timestamp. + fn stage_at(&self, number: U256, timestamp: U256) -> ForkStage { + match self.transition { + Some((Activation::Block(at), stage)) if number >= U256::from(at) => stage, + Some((Activation::Timestamp(at), stage)) if timestamp >= U256::from(at) => stage, + _ => self.initial, } } /// Returns the spec of the block with this number and timestamp. fn spec_at(&self, number: U256, timestamp: U256) -> SpecId { - match self.transition { - Some((Activation::Block(at), spec)) if number >= U256::from(at) => spec, - Some((Activation::Timestamp(at), spec)) if timestamp >= U256::from(at) => spec, - _ => self.initial, + self.stage_at(number, timestamp).spec + } + + /// Returns the blob parameters of the block with this number and timestamp, or `None` before + /// Cancun. + fn blob_params( + &self, + number: U256, + timestamp: U256, + ) -> Result, TestErrorKind> { + let Some(blob_fork) = self.stage_at(number, timestamp).blob_fork else { return Ok(None) }; + let entry = self + .blob_schedule + .get(&blob_fork) + .ok_or(TestErrorKind::MissingBlobSchedule(blob_fork))?; + if entry.base_fee_update_fraction.is_zero() { + return Err(TestErrorKind::ZeroBlobUpdateFraction(blob_fork)); } + Ok(Some(entry.params())) } /// Returns whether any spec of the fork is skipped. fn is_skipped(&self) -> bool { - is_blockchain_fork_skipped(self.initial) - || self.transition.is_some_and(|(_, spec)| is_blockchain_fork_skipped(spec)) + is_blockchain_fork_skipped(self.initial.spec) + || self.transition.is_some_and(|(_, stage)| is_blockchain_fork_skipped(stage.spec)) } } -/// Returns the specs of a fork, or `None` for the transitions that the runner does not support: -/// the DAO fork, and the BPO blob schedule forks, which share a spec. +/// Returns the fork schedule of a test case, with the blob parameters from its `config`. +fn case_schedule( + path: &Path, + name: &str, + test_case: &BlockchainTestCase, +) -> Result { + let mut schedule = fork_schedule(test_case.network).ok_or_else(|| { + TestError::case(path, name, TestErrorKind::UnsupportedFork(test_case.network)) + })?; + if let Some(config) = &test_case.config { + schedule.blob_schedule.clone_from(&config.blob_schedule); + } + Ok(schedule) +} + +/// Returns the blob parameters of the block with this header in a test case, or `None` before +/// Cancun. +/// +/// The parameters come from the case's `config.blobSchedule`, and the fork that is active at the +/// header's number and timestamp. +pub fn blob_params( + path: &Path, + name: &str, + test_case: &BlockchainTestCase, + header: &BlockHeader, +) -> Result, TestError> { + case_schedule(path, name, test_case)? + .blob_params(header.number, header.timestamp) + .map_err(|err| TestError::case(path, name, err)) +} + +/// Returns the specs of a fork, or `None` for the DAO fork, which the runner does not support. const fn fork_schedule(fork: ForkSpec) -> Option { use Activation::{Block, Timestamp}; const AT_5: Activation = Block(5); @@ -1453,6 +1508,8 @@ const fn fork_schedule(fork: ForkSpec) -> Option { ForkSpec::Cancun => ForkSchedule::fixed(SpecId::CANCUN), ForkSpec::Prague => ForkSchedule::fixed(SpecId::PRAGUE), ForkSpec::Osaka => ForkSchedule::fixed(SpecId::OSAKA), + ForkSpec::BPO1 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo1), None), + ForkSpec::BPO2 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo2), None), ForkSpec::Amsterdam => ForkSchedule::fixed(SpecId::AMSTERDAM), ForkSpec::FrontierToHomesteadAt5 => { ForkSchedule::transition(SpecId::FRONTIER, AT_5, SpecId::HOMESTEAD) @@ -1481,21 +1538,34 @@ const fn fork_schedule(fork: ForkSpec) -> Option { ForkSpec::PragueToOsakaAtTime15k => { ForkSchedule::transition(SpecId::PRAGUE, AT_15K, SpecId::OSAKA) } - ForkSpec::BPO2ToAmsterdamAtTime15k => { - ForkSchedule::transition(SpecId::OSAKA, AT_15K, SpecId::AMSTERDAM).with_bpo2_blobs() - } - ForkSpec::HomesteadToDaoAt5 - | ForkSpec::OsakaToBPO1AtTime15k - | ForkSpec::BPO1ToBPO2AtTime15k - | ForkSpec::BPO2ToBPO3AtTime15k - | ForkSpec::BPO3ToBPO4AtTime15k => return None, + ForkSpec::OsakaToBPO1AtTime15k => ForkSchedule::new( + ForkStage::new(SpecId::OSAKA), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo1))), + ), + ForkSpec::BPO1ToBPO2AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo1), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo2))), + ), + ForkSpec::BPO2ToBPO3AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo2), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo3))), + ), + ForkSpec::BPO3ToBPO4AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo3), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo4))), + ), + ForkSpec::BPO2ToAmsterdamAtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo2), + Some((AT_15K, ForkStage::new(SpecId::AMSTERDAM))), + ), + ForkSpec::HomesteadToDaoAt5 => return None, }) } #[cfg(test)] mod tests { use super::{ - super::types::{SealEngine, State}, + super::types::{ChainConfig, SealEngine, State}, *, }; #[cfg(feature = "jit")] @@ -1505,6 +1575,93 @@ mod tests { #[cfg(feature = "jit")] const BYTECODE_STORE42: &[u8] = &[op::PUSH1, 0x42, op::PUSH0, op::SSTORE, op::STOP]; + fn cancun_config() -> ChainConfig { + ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([( + BlobFork::Cancun, + BlobScheduleEntry::from(BlobParams::cancun()), + )]), + } + } + + fn test_case(network: ForkSpec, config: Option) -> BlockchainTestCase { + BlockchainTestCase { + genesis_block_header: BlockHeader::default(), + genesis_rlp: None, + blocks: Vec::new(), + post_state: None, + pre: State(BTreeMap::new()), + block_hashes: Vec::new(), + lastblockhash: B256::ZERO, + network, + seal_engine: SealEngine::NoProof, + config, + } + } + + #[test] + fn blob_params_switch_at_the_transition_fork() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([ + (BlobFork::Bpo1, BlobScheduleEntry::from(BlobParams::bpo1())), + (BlobFork::Bpo2, BlobScheduleEntry::from(BlobParams::bpo2())), + ]), + }; + let case = test_case(ForkSpec::BPO1ToBPO2AtTime15k, Some(config)); + let blob_params = |timestamp| { + let header = BlockHeader { timestamp: U256::from(timestamp), ..Default::default() }; + blob_params(Path::new("test.json"), "test", &case, &header).unwrap().unwrap() + }; + assert_eq!(blob_params(14_999).update_fraction, BlobParams::bpo1().update_fraction); + assert_eq!(blob_params(15_000).update_fraction, BlobParams::bpo2().update_fraction); + } + + #[test] + fn blob_params_require_a_schedule_entry_from_cancun() { + let case = test_case(ForkSpec::Cancun, None); + let err = blob_params(Path::new("test.json"), "test", &case, &BlockHeader::default()) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::MissingBlobSchedule(BlobFork::Cancun))); + } + + #[test] + fn blob_params_reject_a_zero_update_fraction() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([( + BlobFork::Cancun, + BlobScheduleEntry { + target: U256::from(3), + max: U256::from(6), + base_fee_update_fraction: U256::ZERO, + }, + )]), + }; + let case = test_case(ForkSpec::Cancun, Some(config)); + let err = blob_params(Path::new("test.json"), "test", &case, &BlockHeader::default()) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::ZeroBlobUpdateFraction(BlobFork::Cancun))); + } + + #[test] + fn unsupported_fork_fails_instead_of_skipping() { + let suite = BlockchainTest(BTreeMap::from([( + "dao".to_string(), + test_case(ForkSpec::HomesteadToDaoAt5, None), + )])); + let err = execute_suite( + Path::new("dao.json"), + &suite, + ExecuteConfig::default(), + &NameFilter::default(), + &mut NoopHook, + ) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::UnsupportedFork(ForkSpec::HomesteadToDaoAt5))); + } + #[test] fn post_state_rejects_omitted_live_accounts() { let address = Address::with_last_byte(1); @@ -1603,6 +1760,7 @@ mod tests { lastblockhash: B256::ZERO, network: ForkSpec::Paris, seal_engine: SealEngine::NoProof, + config: None, }, )])); let run = |suite: &BlockchainTest, config| { @@ -1710,6 +1868,7 @@ mod tests { lastblockhash: block_hash, network: fork, seal_engine: SealEngine::NoProof, + config: None, genesis_rlp: None, }, )])); @@ -1764,6 +1923,7 @@ mod tests { lastblockhash: B256::with_last_byte(2), network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1822,6 +1982,7 @@ mod tests { lastblockhash: block_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1880,6 +2041,7 @@ mod tests { lastblockhash: genesis_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1933,6 +2095,7 @@ mod tests { lastblockhash: genesis_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -2052,6 +2215,7 @@ mod tests { lastblockhash: block_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), }, )])); let input = serde_json::to_string(&suite).unwrap(); diff --git a/crates/eest/src/blockchaintest/mod.rs b/crates/eest/src/blockchaintest/mod.rs index f788bb621..98017744b 100644 --- a/crates/eest/src/blockchaintest/mod.rs +++ b/crates/eest/src/blockchaintest/mod.rs @@ -9,8 +9,8 @@ mod types; pub use error::TestError; pub use execute::{ - ExecuteConfig, ExecuteSummary, ExecutionMode, execute_case_on, execute_str, execute_suite, - pre_state, + ExecuteConfig, ExecuteSummary, ExecutionMode, blob_params, execute_case_on, execute_str, + execute_suite, pre_state, }; pub use hook::{ BlockFailed, BlockFinished, BlockStarted, CaseStarted, Hook, NoopHook, TransactionFailed, @@ -19,6 +19,7 @@ pub use hook::{ pub use runner::run; pub(crate) use runner::{IGNORED_TESTS, suites}; pub use types::{ - Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, Transaction, Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHash, BlockHeader, BlockchainTest, + BlockchainTestCase, ChainConfig, DecodedBlock, ForkSpec, SealEngine, State, Transaction, + Withdrawal, }; diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 440ffdec0..9c3172244 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -2,6 +2,7 @@ use crate::tx::{AccessListItem, TestAuthorization}; use alloy_eip7928::BlockAccessList; +use alloy_eips::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, eip7840::BlobParams}; use alloy_primitives::{Address, B256, Bytes, FixedBytes, TxKind, U256}; use serde::{Deserialize, Deserializer, Serialize, de}; use std::collections::BTreeMap; @@ -39,6 +40,9 @@ pub struct BlockchainTestCase { /// Seal engine type. #[serde(default)] pub seal_engine: SealEngine, + /// Chain configuration the test ran under. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub config: Option, } /// Historical block hash entry. @@ -311,6 +315,12 @@ pub enum ForkSpec { PragueToOsakaAtTime15k, /// Osaka. Osaka, + /// BPO1 (Osaka with the first blob parameters only fork). + #[serde(rename = "BPO1")] + BPO1, + /// BPO2 (Osaka with the second blob parameters only fork). + #[serde(rename = "BPO2")] + BPO2, /// Osaka to BPO1 transition. OsakaToBPO1AtTime15k, /// BPO1 to BPO2 transition. @@ -335,6 +345,54 @@ pub enum SealEngine { Ethash, } +/// Chain configuration of a blockchain test case. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ChainConfig { + /// Chain ID. + pub chainid: U256, + /// Blob parameters of every fork up to the last fork the test runs. + #[serde(default)] + pub blob_schedule: BTreeMap, +} + +/// A fork that sets the blob parameters. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] +pub enum BlobFork { + /// Cancun. + Cancun, + /// Prague. + Prague, + /// Osaka. + Osaka, + /// BPO1, a fork that only changes the blob parameters. + #[serde(rename = "BPO1")] + Bpo1, + /// BPO2, a fork that only changes the blob parameters. + #[serde(rename = "BPO2")] + Bpo2, + /// BPO3, a fork that only changes the blob parameters. + #[serde(rename = "BPO3")] + Bpo3, + /// BPO4, a fork that only changes the blob parameters. + #[serde(rename = "BPO4")] + Bpo4, + /// Amsterdam. + Amsterdam, +} + +/// Blob parameters of one fork. +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct BlobScheduleEntry { + /// Target blobs per block. + pub target: U256, + /// Maximum blobs per block. + pub max: U256, + /// Blob base fee update fraction. + pub base_fee_update_fraction: U256, +} + /// Converts an optional address field that may be encoded as an empty string. fn deserialize_maybe_empty<'de, D>(deserializer: D) -> Result, D::Error> where @@ -357,6 +415,31 @@ fn is_empty_bytes(bytes: &Bytes) -> bool { bytes.as_ref().is_empty() } +impl BlobScheduleEntry { + /// Returns the blob parameters of this entry. + pub fn params(&self) -> BlobParams { + let max_blob_count = self.max.saturating_to(); + BlobParams { + target_blob_count: self.target.saturating_to(), + max_blob_count, + update_fraction: self.base_fee_update_fraction.saturating_to(), + min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE, + max_blobs_per_tx: max_blob_count, + blob_base_cost: 0, + } + } +} + +impl From for BlobScheduleEntry { + fn from(params: BlobParams) -> Self { + Self { + target: U256::from(params.target_blob_count), + max: U256::from(params.max_blob_count), + base_fee_update_fraction: U256::from(params.update_fraction), + } + } +} + impl Transaction { /// Returns this transaction's EIP-2718 type byte. pub(crate) fn tx_type(&self) -> u8 { diff --git a/data/mainnet-25347446-25347455.bin.zst b/data/mainnet-25347446-25347455.bin.zst index f8f64abac4cfe16213d91f0aa7cc17888e3f4614..8577dd341f1aba12537316a46ae3784c3eb6ee48 100644 GIT binary patch delta 2794 zcmV%--7~xgaL#Cgad>Ggaw2Kga?EOgb9QSgbRcWgbjoagb#!egb{=i zgcF1mgcXDqgcpPugc*bygd2n$gdKz)gdc<;gdv0?gd>C`ge8O~geQb3gein7ge!zB zge`-EILilRA_2-W5{#EW z%Rm4nwg`8@o$#EuT+2YW2ns{G-#O=4oL70Rw-eMrt_Xhtb8On8KPQ}(wWUEs zPZ4~}vGl74_st6mX7$!W7%mKTATS+T1l7k$4?to?wig-lRBMY&^h+*HmoA1<{u-S&~W8s{h!~RGSm1iKhBfIU#R2Ed>b~2p2;$GfIeP(kryK@z?W%|QDA|8-t=x)iqL;{VK#~77j-xdf3)tsY;%|< zIw~jrV-P5o_?S^gAkg@QXJ8L?Y69J2?qiPow}b`{h%BX$)C#a^F z3@cyPS?eJM@94n=6wsI`(0%P;Hd!j!D zn}Hb}iG^I;P_+O;sg@UPL^Aah%e7NWlPZb6qUqj&&aA z#a#953E=8bnStPc2?dnNC#NlLvV-QVq(guB&trb7nl9 zbus6@O{D7}^NYA)P3^s!9ghxm!I5~F?Sy5fjXBURSjyBvK$U(Uow4{KENng;UQ&Mt zsI00@oodC3@w9CewX{SJ6p&&YN-6HPmj^^esAJIh?y% zB^c}u?8C$#paM?%kBD_Af|2x2kMg~Ru_Ln#qABc^yx2F@`E)^Q^?$s(nHGLVxFydn zM1-8N#42pDeDs8jVrYxFYRd0{z|4Ptk7vCrdE;GDnU=_~gaw@WEQRh^q)jMX!md=R zo8ixp)30X$o*2VI3XJu_X*OvR1cUuSWFa54oILos9^P0=GPZxm^*e$`+O|Y>?qb$Q z%mY#~RW_lFEaCAY9@o`7m+>Z#pW}&A+?B5J*9j|C9yLky6sGzLuQ;o-W;cHXUb3q( zFF^bot?`^Lu?Cj9j>Lv}PUV+RtyD*Jf^$}Z{>mF+M5UdczlJngQ&^wJWF?Mu2ISMV zt54AFN|YH!LxgeJ^ZE^G#=_6=K;M$Q%hc;}4~$(Wyxs8qs!~q}u>^^oUDSF@NT7ZH z7+B!1PacVYDjxAGQgRQ`uJC`j0z%0{fz`jl5wGp;Px-$PFySQgieQKGVnIz#IlK$} zIOgT`FW>{60TR19%(QLD`S=x} z?`tVlKqFd1NKS};A~0bJFb;Oa?~3|sQn~_?^yZ6fm0ffQNyvB0o_eqz{)T^E?Ue`W2T?p?%I#2* zt^s}FdKP@I700@@(kd-@!spAMVJt+E*!&gw#z~&@ffj-<1xGqqEQ+7!17_}iCsq#P zl>P!*rB15h%TT3elBc3+W8}VRS8?8u<-xySUSX;~ zKj_A87*X_KWDodd+i?#4X$AenBMUNfbVWsUzgSfh`Q?jDsSx$e5oZe^#G^b%SvzMG zr7gO9`-M@EM!0sgarPg-bK&+wm=*}e2Ner>jdek-Dky*EAUgGhGF(pxq&}~%;KSn{ zPch_rAd5I9Op+e8Y7ZkzVkwGthYwNZNX>nIPALC(iyM`gzPFVw5sM)g0EY&OKGHTv z6%i*;y55CX2uup@cNfdhLt;0m@z8ZOhjO+eXw*q z_9Obt{?ikvoa755R{2#j*hk0Gu#F zO|Tsn4N_Zx7ZVH%Wok|m3gqTC4-O=^dgVZ)2!9cQE2tq5f)D{A0wN?tP>6sKeho2C zlu?qD6tWyb5D0=t5P}r~1tCZngt{apDP-6TVR4eJ&I-V(Ny#Fd0T=}d$adrPf7(je z3QB0)BA5#u%BYegQl&~&N{gzNmR40Qt*Tn8RH{^~w5p;q^DR(j0gl_-@esVZ7hRkWm}s7Ps1$x^A3rBWqDMM|oQs+1O0 zDJ?2dDs-xlHqW8EZUwG_Eipu5N!smxu1Bq*31F=wj&gA5Bl&AZS`~rcg&h!lpZWVf wpg11_;bYz254&%{PRxjgwe^DNv@$Ub5@Dj@fn-8)aaZLQGR9tO-;rBw5Ny#Y$N&HU delta 2740 zcmV;l3QP5e--80u-h%>wgaL#Cgad>Ggaw2Kga?EOgb9QSgbRcWgbjoagb#!egb{=i zgcF1mgcXDqgcpPugc*bygd2n$gdKz)gdc<;gdv0?gd>C`ge8O~geQb3gein7ge!zB zge`-EILimUAOUJIx{H@T z%RmhFw}v-bZ8M& zA16Hki51yiWXMykEi%=6WoBzCCg^PZ4=^Ob%7-XIK;qw^(iz#XikO;zjC4Z7m6P>< zes{`DS#H!ZKi-U1yb~9k>-7SjVk&P-i;?jE?zO z_Gd(2@VyR8 z-7qr%)7*o)Q7KY&VY8CfUgnJ%pbZ*ed#2g%68&Ogp@)2Rx8Xo3x`coE%`<~Q;}@QR zJ=CcQbc?xJnoW#(kZY@oLiV$C<*Y1!t43&2@L&OJ;lwMvE;Hi?4>JwVUVGiokvwYd4X-)F_pX0^$Ui{@;OAzduZ?2m zye6-)u&TUSj)AGgX~{xKBr|6?Lv;Nnw=koQH`o_cVGC}?%al$jE<-hE^|?n1vbRJn zR%^w0{~v4tU0m!+032$&FWJm>v4lI;d7Kw>)w3snt3zc5g8wBHP$r+8wz$a-nzND) z;gjD1EBDae|IvSN7yyfy1`?#%Pcl}{*GWq49n8({%AxUlK$UE7xn%Sh<&-B9d-;At ze*8y1YZV;a>kx@Zl<7W;X0hC;;%m*B@pRV3oclJBu7k`k;)XS~_iA=LI@AS6;$gNE zmYFu@K)YZmQwIT6`h9f9;)k%X`EYni9iXzRHg&2ME5?7*wo%m5PB9$($-7fPlDYN% zATTYCR-I!FPHsDI#!=T$yH(S-=!E5P?rN1_usg616MujTIO#tk)}07O(mOrM_ZG&E z%rc0kuvhY8-&E(*1*z5l@$P0?_!;4rJi8DPa>f#?u*LGx6EcdSE#j&vzXt*{|2>}d zuH=n(No9XpBEu3EaOSfVx?_PSiQmJl+KSNHxo&k7b3=1hR)(fZEq)8AA_6L!L ze9&_8;OBaHV=2kl{vFrv2p(zM64kkjSsyVENXb;$gfg;($BTGeSMOZLn>>DwCrWWw zy2f88tWMOkBtj?O<5O~S1#=HRWZ?u2LbGpPDSn4_w8|FEcUp}=`9nlHS zSq1tlZ-fz*c6$CA(r8U#eIApQIMx}EPuH$KLANVWW*7|-#%0gzH>4R0Kf?okOY$yL zug5(wcAfBc!}qI7JsrdnBzAUD>n$OH_WfgEfxkX^Bm$~<#IH!nJw&_0;|d5R4+U2L z3P*puw!1&&|3biolgulE9mQ*gV>G8(IZAhggP#dy(KH?jt^> z_!2^$kq3S~g5y|gNbImI=EE=d&0b|-ZhAOK2I8N81lW?-(O*MvQfK&scCwhg2E~6V z?-f4exp_~aYV|X0$-56BCDcoZ%NFI_3v;QiR82u^xd>h%^gw8>DJkNslj*kLKj=y> z4})P)M>6zrDTf*g9y|Cc9-nLHAsf04Pi3jBM1P0%x$8_3L<{9pnFS1p3 z(IF%u-z|IU!Fu=`dbL*`s2@b}h$(-!Lq)m<^o8qL@V!()xEwBQM!FMo!y5Jh71 zSL7QfdCmt~2)+~?>0q%aex47Qx%-`1IfzsG3uu)(sfI6CHS#tRA(kIJV=zz(YF4ae zYZF|5Ksdj|#a{u6XMj-xycKH`;>;Vi@kQl&*cZq?E+mQZ{efg12EzU)3`>8Wil&W` zGsk(Xvwz01>B+H4TNPf#c|(>5|9*Less8++8@pjd(Swma;FoR3IrOI$^b?OP$js3d z718}-RZZlVFEXV<)Hg?*Er1Y@@*HLDoKcjv=B+deo{tj4X+zDB2x9M3o~o z_xU-Y{NF8ZRATzxR=PwihFky~8Yucm+Z(Z>l<|-pP1^;_ zF_2yXj_&g7Qa0mtW0Hz=v*VB8p!`9y8`dnq4scU=(^$;2Q=9 zIRN&Qhxyd4WX4DCB*H!b9|9E11}Feq9Zm-fdzFtiYRgqE=6UiY-m58W_NJ^-*ibP7Z zRD~o(R8mDHB~+?K5+y3CB9ba9sUnpUB~>9w5mi+YNhy^okwl4#s*BOzYB*U@FnK&`+-9G uvZT*K&OX*hUUy Date: Sat, 31 Oct 2026 17:15:54 +0100 Subject: [PATCH 071/159] refactor(state): derive changed code from the code hash (#948) The state stream reported an account's new bytecode when a `CODE_CHANGED` flag was set or the code hash differed from the original's, and the code was non-empty, non-zero-hash and not the empty hash. The flag only caused code to be re-emitted when its hash had not changed, and every sink stores code by hash, so it added nothing. The zero-hash check was a sentinel and the empty-code checks repeated each other. `Account::changed_code` now reports the present code when its hash differs from the original's and the code is not empty. `AccountFlags::CODE_CHANGED`, its setting in `mark_created` and `set_code`, and the flag synthesis in `PendingState::insert_account` are removed. A created account whose code hash already exists under another address still reports its code, since the original has no code hash. The `CacheDB` and `BlockStateAccumulator` sinks insert code by hash, and the BAL and inspector consumers do not read `AccountChanges::code`. The docs on `Account` claimed `CREATED` is meaningful only while `present` is `Some`, which contradicted `Account::is_created`: the flag survives selfdestruct finalization. The docs now say so. The zero-hash check was only safe if a zero code hash never enters state. `CacheDB` maps a zero hash to the empty code hash in its account load, for every entry including ones written straight into the cache, and in its code lookup by hash. It also maps it before keying inserted contracts, so `insert_account_info` and commits store the real hash. `State` always reads through `CacheDB`, so it never sees a zero hash. The placeholder zero-hash entry in the contract cache is removed. The `Cache` fields stay public because `reth` reads and writes them, as do the test harness and the CLI, so the guard sits where reads converge instead of on every write. --- crates/evm2/src/evm/db/cache.rs | 42 +++++++++++++++++++-- crates/evm2/src/evm/state/account.rs | 56 ++++++++++++++++++---------- crates/evm2/src/evm/state/pending.rs | 5 +-- 3 files changed, 76 insertions(+), 27 deletions(-) diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 38d297130..3c2bfc5eb 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -88,7 +88,6 @@ impl Default for Cache { fn default() -> Self { let mut contracts = B256Map::default(); contracts.insert(KECCAK256_EMPTY, Bytecode::default()); - contracts.insert(B256::ZERO, Bytecode::default()); Self { accounts: AddressMap::default(), contracts, @@ -199,6 +198,7 @@ impl CacheDB { #[inline] fn insert_contract_inner(contracts: &mut B256Map, info: &mut AccountInfo) { + normalize_code_hash(info); if let Some(code) = &info.code && !code.is_empty() { @@ -207,9 +207,6 @@ impl CacheDB { } contracts.entry(info.code_hash).or_insert_with(|| code.clone()); } - if info.code_hash.is_zero() { - info.code_hash = KECCAK256_EMPTY; - } } /// Inserts account info. @@ -361,6 +358,11 @@ impl DynDatabase for CacheDB { } }; + // The cache fields are public, so entries written directly can still carry a zero hash. + if let Some(info) = &mut account { + normalize_code_hash(info); + } + // Apply the resolved BAL account's info at the current block access index. if let Some(bal_account) = bal_account { self.bal_context.populate_bal_account(bal_account, &mut account); @@ -370,6 +372,7 @@ impl DynDatabase for CacheDB { #[inline] fn get_code_by_hash(&mut self, code_hash: &B256) -> DbResult { + let code_hash = if code_hash.is_zero() { &KECCAK256_EMPTY } else { code_hash }; match self.cache.contracts.entry(*code_hash) { Entry::Occupied(entry) => Ok(entry.get().clone()), Entry::Vacant(entry) => Ok(entry.insert(self.db.get_code_by_hash(code_hash)?).clone()), @@ -427,6 +430,14 @@ impl DynDatabase for CacheDB { } } +/// Maps a zero code hash, which some databases use for no code, to the empty code hash. +#[inline] +fn normalize_code_hash(info: &mut AccountInfo) { + if info.code_hash.is_zero() { + info.code_hash = KECCAK256_EMPTY; + } +} + mod typed { use super::*; use crate::evm::Database; @@ -578,6 +589,29 @@ mod tests { } } + #[test] + fn cache_db_normalizes_zero_code_hash() { + let address = Address::with_last_byte(1); + let zero_hash = AccountInfo { code_hash: B256::ZERO, ..AccountInfo::empty() }; + + let db = CountingDB { account: Some(zero_hash.clone()), ..CountingDB::default() }; + let mut cache = CacheDB::new(crate::evm::Db::new(db)); + let loaded = cache.get_account(&address).unwrap().unwrap(); + assert_eq!(loaded.code_hash, KECCAK256_EMPTY); + + let mut cache = CacheDB::new(crate::evm::EmptyDB::default()); + cache.insert_account_info(&address, zero_hash.clone()); + assert_eq!(cache.account_info(&address).unwrap().code_hash, KECCAK256_EMPTY); + + let mut incoming = Cache::default(); + incoming.accounts.insert(address, Some(zero_hash)); + let mut cache = CacheDB::new(crate::evm::EmptyDB::default()); + cache.cache.merge(incoming); + let loaded = cache.get_account(&address).unwrap().unwrap(); + assert_eq!(loaded.code_hash, KECCAK256_EMPTY); + assert_eq!(cache.get_code_by_hash(&B256::ZERO).unwrap(), Bytecode::default()); + } + #[test] fn cache_db_caches_wrapped_db_reads() { let address = Address::with_last_byte(1); diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 276ddcfa2..d482b6fe5 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -184,8 +184,6 @@ bitflags! { /// [`AccountHandle::mark_created_keeping_storage`]. Meaningful only while /// [`Self::CREATED`] is set. const KEEPS_STORAGE = 1 << 5; - /// The present overlay account's code has been modified. - const CODE_CHANGED = 1 << 6; } } @@ -197,9 +195,9 @@ bitflags! { /// [`State`](super::State) emit the transaction's account transition without re-reading the backing /// database. `present` is `None` for an account that was loaded as non-existent or deleted. /// -/// [`AccountFlags::CREATED`] and [`AccountFlags::CODE_CHANGED`] track creation and -/// code-modification state of the present overlay account, driving transaction-finalization and -/// change-emission rules. They are meaningful only while `present` is `Some`. +/// [`AccountFlags::CREATED`] tracks creation in the current transaction and drives +/// transaction-finalization rules. It survives selfdestruct finalization, so it can stay set while +/// `present` is `None`. /// /// Detached from the EVM as part of a [`PendingState`](super::PendingState), the entry describes /// the account's transaction transition: `original` against `present`, with @@ -210,7 +208,7 @@ pub(crate) struct Account { pub(crate) original: Option, /// Present account overlay after mutations. `None` means the account is absent/deleted. pub(crate) present: Option, - /// Lifecycle flags: warm, touched, destroyed, created, keeps-storage, and code changed. + /// Lifecycle flags: warm, touched, destroyed, created, and keeps-storage. pub(crate) flags: AccountFlags, /// Persistent transaction storage owned by this account. pub(crate) storage: StorageOverlay, @@ -223,7 +221,7 @@ impl Account { Self { original, present, ..Self::default() } } - /// Marks the account as created during the transaction, which also flags its code as changed. + /// Marks the account as created during the transaction. /// /// The first creation mark decides whether the account keeps its storage baseline. #[inline] @@ -232,7 +230,7 @@ impl Account { if !flags.contains(AccountFlags::CREATED) { flags = flags.difference(AccountFlags::KEEPS_STORAGE); } - self.flags = flags.union(AccountFlags::CREATED.union(AccountFlags::CODE_CHANGED)); + self.flags = flags.union(AccountFlags::CREATED); } /// Returns whether the account was created during the transaction. @@ -273,11 +271,8 @@ impl Account { let account = self.present.as_ref()?; let code = account.code.as_ref()?; let code_hash = account.code_hash; - ((self.flags.contains(AccountFlags::CODE_CHANGED) - || self.original.as_ref().map(|original| original.code_hash) != Some(code_hash)) - && !code.is_empty() - && !code_hash.is_zero() - && !account.is_empty_code_hash()) + (self.original.as_ref().map(|original| original.code_hash) != Some(code_hash) + && !code.is_empty()) .then_some((code_hash, code)) } @@ -304,7 +299,7 @@ impl Account { /// cannot drift apart, mirroring revm's `AccountHandle`. /// /// The first mutation made through the handle captures a snapshot of the overlay entry (present -/// value plus the warm/touched/destroyed/created/code-changed flags); on drop the handle flushes +/// value plus the warm/touched/destroyed/created flags); on drop the handle flushes /// that snapshot as a single [`JournalEntry::AccountChange`], which /// [`State::rollback`](super::State::rollback) replays to restore the whole entry at once. A handle /// used only for reads records nothing, so it emits no journal entry. @@ -611,7 +606,6 @@ impl<'a, 'db> AccountHandle<'a, 'db> { let account = self.present_mut(); account.code_hash = code_hash; account.code = Some(code); - self.tracked.flags.insert(AccountFlags::CODE_CHANGED); } /// Sets the account code, computing its hash, touching the account and recording a revert @@ -645,9 +639,9 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.present.get_or_insert_with(empty_account) } - /// Marks the present overlay account as created in the current transaction, also flagging its - /// code as changed. The creation/code-change status is reverted together with the rest of the - /// overlay by the [`JournalEntry::AccountChange`] snapshot recorded for this handle. + /// Marks the present overlay account as created in the current transaction. The creation + /// status is reverted together with the rest of the overlay by the + /// [`JournalEntry::AccountChange`] snapshot recorded for this handle. /// /// Created accounts follow the EIP-6780 rules for `SELFDESTRUCT` and are emitted as created by /// [`PendingState`](super::PendingState). @@ -811,6 +805,31 @@ mod tests { assert_eq!(loaded, code); } + #[test] + fn changed_code_requires_new_non_empty_code_hash() { + use alloy_primitives::Bytes; + + let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let with_code = AccountInfo::default().with_code(code.clone()); + let other = AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from_static(&[0]))); + + let created = Account::new(None, Some(with_code.clone())); + assert_eq!(created.changed_code(), Some((with_code.code_hash, &code))); + + let replaced = Account::new(Some(other), Some(with_code.clone())); + assert_eq!(replaced.changed_code(), Some((with_code.code_hash, &code))); + + assert_eq!( + Account::new(Some(with_code.clone()), Some(with_code.clone())).changed_code(), + None + ); + assert_eq!( + Account::new(Some(with_code), Some(AccountInfo::default())).changed_code(), + None + ); + assert_eq!(Account::new(None, Some(AccountInfo::default())).changed_code(), None); + } + #[test] fn loaded_code_is_seeded_with_account_code_hash() { use alloy_primitives::Bytes; @@ -962,7 +981,6 @@ mod tests { assert!(!account.is_warm()); assert!(!account.is_created()); assert!(!account.is_destructed()); - assert!(!account.tracked.flags.contains(AccountFlags::CODE_CHANGED)); } } diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 178bd5fed..201678c1e 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -51,14 +51,11 @@ impl PendingState { original: Option, current: Option, ) { - let code_changed = original.as_ref().map(|account| account.code_hash) - != current.as_ref().map(|account| account.code_hash); - let flags = if code_changed { AccountFlags::CODE_CHANGED } else { AccountFlags::empty() }; let account = self.accounts.entry(address).or_default(); *account = Account { original, present: current, - flags, + flags: AccountFlags::empty(), storage: core::mem::take(&mut account.storage), }; } From 10621c366ec557bb2ff9e75e6befcf0bd4a3f63e Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:04:23 +0100 Subject: [PATCH 072/159] refactor(inspectors): use the engine's call values, create addresses and halt gas (#900) The tracing inspector overwrote two values that evm2 already reports correctly. This removes those overrides and records what the engine provides. A DELEGATECALL frame took its parent trace's value instead of `message.value`, but evm2 already sets a delegate message's value to the caller frame's value, and a top-level message carries the transaction value. The geth call frame forced a halted frame to report its gas limit as gas used and no output, but a halt settles all remaining gas and returns empty output, so `gas.spent()` already equals the frame's limit. A create trace still takes its address from the result's `created_address` when present. The result is the frame's outcome as the parent sees it, and another inspector may have replaced it, so it wins over the address recorded when the create started. With `active_trace` unused after this, it goes too. Before deleting the overrides I asserted the equalities (CREATE, CREATE2, failed creates, and every halt kind) across the inspectors tests and all 360 case/fork pairs of the geth parity harness on every fork including Amsterdam; none fired. The harness reports the same differences before and after, with no new ones. --- crates/inspectors/src/tracing/mod.rs | 23 ++++------------------- crates/inspectors/src/tracing/types.rs | 5 ----- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 3866a0401..4b55c06d8 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -331,15 +331,6 @@ impl TracingInspector { false } - /// Returns the currently active call trace. - /// - /// This will be the last call trace pushed to the stack: the call we entered most recently. - #[track_caller] - #[inline] - fn active_trace(&self) -> Option<&CallTraceNode> { - self.trace_stack.last().map(|entry| &self.traces.arena[entry.trace_idx]) - } - /// Returns the last trace [CallTrace] index from the stack. /// /// This will be the currently active call trace. @@ -442,8 +433,9 @@ impl TracingInspector { self.log_count = log_count; } + // The result is the frame's outcome as the parent sees it; another inspector may have + // overridden it, so it wins over the address recorded when the create started. if let Some(address) = result.created_address { - // A new contract was created via CREATE trace.address = address; } } @@ -802,18 +794,11 @@ impl Inspector for TracingInspector { _ => (message.caller, message.destination), }; - let value = if matches!(message.kind, MessageKind::DelegateCall) { - // for delegate calls we need to use the value of the top trace - if let Some(parent) = self.active_trace() { parent.trace.value } else { message.value } - } else { - message.value - }; - // if calls to precompiles should be excluded, check whether this is a call to a precompile let maybe_precompile = self .config .exclude_precompile_calls - .then(|| self.is_precompile_call(interp.host(), &to, &value)); + .then(|| self.is_precompile_call(interp.host(), &to, &message.value)); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) @@ -826,7 +811,7 @@ impl Inspector for TracingInspector { usize::from(message.depth), to, input, - value, + message.value, message.kind.into(), from, message.gas_limit, diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index b46789b7e..dcbd3df2d 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -454,11 +454,6 @@ impl CallTraceNode { call_frame.to = None; } - if !self.status().is_some_and(InstrStop::is_frame_revert) { - call_frame.gas_used = U256::from(self.trace.gas_limit); - call_frame.output = None; - } - call_frame.revert_reason = utils::maybe_revert_reason(self.trace.output.as_ref()); // Note: regular calltracer uses geth errors, only flatCallTracer uses parity errors: From 9510b414dc92cc08a32ce038511993a01e929303 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:04:26 +0100 Subject: [PATCH 073/159] refactor(bal): fold commits through the state change stream (#906) Two folds turned a transaction's account changes into block access list entries and disagreed on wiped storage. `Bal::update_account` and `StorageBal::update_pending`, used by `Bal::commit` and the `CacheDB` commit, ignored the storage wipe, so a wiped slot with a nonzero original that ended at zero became a write to zero. The `StateChangeSink` impl for `BalContext`, fed by `AccountChanges::visit`, treated the same slot as a read, which is what the specification's `destroy_storage` does. Both paths now go through one sink, `BalWriter`, driven by the change stream. `Bal::commit` and `BalContext::commit` visit the transaction's changes with it, and the `BalContext` sink delegates to it, so the commit path now records the wiped slot as a read. `CacheDB::commit` and `State::commit_transaction` pass the selfdestruct set along so the commit path can build the same `TxStateChanges` view. I also removed `Bal::populate_storage_slot`, an unused copy of `BalContext::bal_storage`'s lookup. A new test commits a wiped account whose slot goes from nonzero to zero and checks that it is a read; it fails on main. The stream's wiped-slot read value is out of scope and is changed by #895. --- crates/evm2/src/evm/bal/account.rs | 16 +-- crates/evm2/src/evm/bal/bal_context.rs | 77 ++++--------- crates/evm2/src/evm/bal/list.rs | 149 ++++++++++++++++++------- crates/evm2/src/evm/db/cache.rs | 10 +- crates/evm2/src/evm/state/mod.rs | 4 +- 5 files changed, 139 insertions(+), 117 deletions(-) diff --git a/crates/evm2/src/evm/bal/account.rs b/crates/evm2/src/evm/bal/account.rs index 60aea3c3f..44dac58c7 100644 --- a/crates/evm2/src/evm/bal/account.rs +++ b/crates/evm2/src/evm/bal/account.rs @@ -6,7 +6,7 @@ use super::{ }; use crate::{ bytecode::{Bytecode, BytecodeDecodeError}, - evm::state::{AccountInfo, StorageSlot}, + evm::state::AccountInfo, }; use alloc::vec::Vec; use alloy_eip7928::{ @@ -320,20 +320,6 @@ impl StorageBal { } } - /// Update storage from an account's pending [`StorageSlot`] overlay: a changed slot records a - /// write at `bal_index`, a loaded-but-unchanged slot records a read. - #[inline] - pub fn update_pending(&mut self, bal_index: BlockAccessIndex, slots: &U256Map) { - self.storage.reserve(slots.len()); - for (key, slot) in slots { - self.storage.entry(*key).or_default().update( - bal_index, - &slot.value.original, - slot.value.current, - ); - } - } - /// Update reads with new storage keys. /// /// It will expend inner map with new reads. diff --git a/crates/evm2/src/evm/bal/bal_context.rs b/crates/evm2/src/evm/bal/bal_context.rs index 31f72bdeb..f58a78888 100644 --- a/crates/evm2/src/evm/bal/bal_context.rs +++ b/crates/evm2/src/evm/bal/bal_context.rs @@ -1,15 +1,16 @@ //! EIP-7928 Block Access List state carried alongside the accepted-overlay database. -use super::{AccountBal, Bal, BalError, BlockAccessIndex}; +use super::{AccountBal, Bal, BalError, BlockAccessIndex, list::BalWriter}; use crate::{ evm::state::{ - Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StorageChange, + AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, + StorageChange, TxStateChanges, }, interpreter::Word, }; use alloc::sync::Arc; use alloy_eip7928::{BalAccountInfo, BlockAccessList}; -use alloy_primitives::{Address, map::AddressMap}; +use alloy_primitives::Address; use core::convert::Infallible; /// Result of an EIP-7928 BAL lookup during a read. @@ -169,23 +170,26 @@ impl BalContext { /// are recorded as BAL reads; changed ones as writes. #[inline] pub fn commit_pending(&mut self, pending: &PendingState) { - self.commit(&pending.accounts); + self.commit(pending.changes()); } - /// Folds a committed transaction's pending accounts and storage overlays into the BAL builder - /// at the current [`Self::bal_index`]. + /// Folds a committed transaction's changes into the BAL builder at the current + /// [`Self::bal_index`]. /// /// Same as [`Self::commit_pending`], operating on the transaction layers directly so the /// overlay need not be detached. No-op when BAL construction is disabled. #[inline] - pub(crate) fn commit(&mut self, accounts: &AddressMap) { - let index = self.bal_index; - let Some(bal) = self.bal_builder.as_mut() else { + pub(crate) fn commit(&mut self, changes: TxStateChanges<'_>) { + let Some(mut writer) = self.writer() else { return; }; - for (&address, account) in accounts { - bal.update_account(index, address, account); - } + let Ok(()) = changes.visit(&mut writer); + } + + #[inline] + fn writer(&mut self) -> Option> { + let index = self.bal_index; + self.bal_builder.as_mut().map(|bal| BalWriter::new(bal, index)) } /// Takes the built BAL, resetting the block access index. Returns `None` when BAL construction @@ -321,58 +325,28 @@ pub enum BalAccountLookup { NotCovered, } -/// Folds streamed state changes into the BAL builder at the current [`BalContext::bal_index`]. -/// -/// This is the sink shape of the [`Self::commit_pending`] fold: changed accounts and storage -/// slots are -/// recorded as writes, loaded-but-unchanged ones -- the read callbacks -- as reads. The bytecode -/// and storage-wipe callbacks need no BAL action: code changes surface through -/// [`StateChangeSink::account`], and a wiped account's storage surfaces through the storage -/// callbacks. Every callback is a no-op when BAL construction is disabled. +/// Folds streamed state changes into the BAL builder at the current [`BalContext::bal_index`], as +/// [`Bal::commit`] does. Every callback is a no-op when BAL construction is disabled. impl StateChangeSink for BalContext { type Error = Infallible; #[inline] fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { - let index = self.bal_index; - if let Some(bal) = self.bal_builder.as_mut() { - let original = change.original.cloned().unwrap_or_default(); - let current = change.current.cloned().unwrap_or_default(); - bal.accounts - .entry(change.address) - .or_default() - .account_info - .update(index, &original, ¤t); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.account(change)) } #[inline] fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { - let index = self.bal_index; - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts - .entry(change.address) - .or_default() - .storage - .storage - .entry(change.key) - .or_default() - .update(index, &change.original, change.current); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.storage(change)) } #[inline] fn account_read( &mut self, address: Address, - _info: Option<&AccountInfo>, + info: Option<&AccountInfo>, ) -> Result<(), Self::Error> { - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts.entry(address).or_default(); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.account_read(address, info)) } #[inline] @@ -380,12 +354,9 @@ impl StateChangeSink for BalContext { &mut self, address: Address, key: Word, - _value: Word, + value: Word, ) -> Result<(), Self::Error> { - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts.entry(address).or_default().storage.storage.entry(key).or_default(); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.storage_read(address, key, value)) } } diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index 51531cc6f..97747b6a1 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -1,16 +1,21 @@ //! The top-level Block Access List structure. -use super::{AccountBal, BalError, BlockAccessIndex}; +use super::{AccountBal, BlockAccessIndex}; use crate::{ bytecode::BytecodeDecodeError, - evm::state::{Account, AccountInfo, PendingState}, + evm::state::{ + AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, + StorageChange, + }, + interpreter::Word, }; use alloc::vec::Vec; use alloy_eip7928::{ AccountChanges as AlloyAccountChanges, BlockAccessList as AlloyBal, CodeChange as AlloyCodeChange, SlotChanges as AlloySlotChanges, }; -use alloy_primitives::{Address, U256, map::AddressMap}; +use alloy_primitives::{Address, map::AddressMap}; +use core::convert::Infallible; /// BAL structure. #[derive(Debug, Default, Clone, PartialEq, Eq)] @@ -80,56 +85,84 @@ impl Bal { /// This is the fold [`Evm`](crate::Evm) applies on transaction commit when its BAL builder is /// enabled: loaded-but-unchanged accounts and storage slots are recorded as reads, changed /// ones as writes at `bal_index`. Apply pending states in transaction order, since writes - /// must be appended with ascending indices. - pub fn commit(&mut self, bal_index: BlockAccessIndex, pending: PendingState) { - for (&address, account) in &pending.accounts { - self.update_account(bal_index, address, account); - } - } - - /// Extend BAL with one pending account overlay entry: its transaction-boundary original info - /// against its present info, and its loaded storage slots. An absent side is the non-existent - /// (default) account, so an account removed by the transaction records zeroed writes. + /// must be appended with ascending indices. Commit each transaction once, at its own index: a + /// wiped slot is recorded as a read, which does not retract a write an earlier commit made at + /// the same index. /// /// A selfdestructed account needs no special-casing: transaction finalization already resolved /// its present info to the EIP-8246 balance-only remnant or to a removed account, and its /// destroyed storage writes surface as reads (execution-specs `destroy_storage`). + pub fn commit(&mut self, bal_index: BlockAccessIndex, pending: PendingState) { + let Ok(()) = pending.visit(&mut BalWriter::new(self, bal_index)); + } +} + +/// Folds streamed state changes into a [`Bal`] at one block access index. +/// +/// Changed accounts and storage slots are recorded as writes, loaded-but-unchanged ones -- the +/// read callbacks -- as reads. An absent account side is the non-existent (default) account, so an +/// account removed by the transaction records zeroed writes. The bytecode and storage-wipe +/// callbacks need no BAL action: code changes surface through [`StateChangeSink::account`], and a +/// wiped account's storage surfaces through the storage callbacks. +#[derive(Debug)] +pub(super) struct BalWriter<'a> { + bal: &'a mut Bal, + index: BlockAccessIndex, +} + +impl<'a> BalWriter<'a> { #[inline] - pub(crate) fn update_account( - &mut self, - bal_index: BlockAccessIndex, - address: Address, - account: &Account, - ) { - let bal_account = self.accounts.entry(address).or_default(); + pub(super) const fn new(bal: &'a mut Bal, index: BlockAccessIndex) -> Self { + Self { bal, index } + } +} + +impl StateChangeSink for BalWriter<'_> { + type Error = Infallible; + + #[inline] + fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { let absent = AccountInfo::default(); - bal_account.account_info.update( - bal_index, - account.original.as_ref().unwrap_or(&absent), - account.present.as_ref().unwrap_or(&absent), + self.bal.accounts.entry(change.address).or_default().account_info.update( + self.index, + change.original.unwrap_or(&absent), + change.current.unwrap_or(&absent), ); - bal_account.storage.update_pending(bal_index, &account.storage.slots); + Ok(()) } - /// Populate storage slot from BAL by account address. - /// - /// If the account is not found in the BAL, or the slot is not listed under it, an error is - /// returned. #[inline] - pub fn populate_storage_slot( - &self, - account_address: Address, - bal_index: BlockAccessIndex, - key: U256, - value: &mut U256, - ) -> Result<(), BalError> { - let Some(bal_account) = self.accounts.get(&account_address) else { - return Err(BalError::AccountNotFound { address: account_address }); - }; + fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { + self.bal + .accounts + .entry(change.address) + .or_default() + .storage + .storage + .entry(change.key) + .or_default() + .update(self.index, &change.original, change.current); + Ok(()) + } - if let Some(bal_value) = bal_account.storage.get(&account_address, key, bal_index)? { - *value = bal_value; - }; + #[inline] + fn account_read( + &mut self, + address: Address, + _info: Option<&AccountInfo>, + ) -> Result<(), Self::Error> { + self.bal.accounts.entry(address).or_default(); + Ok(()) + } + + #[inline] + fn storage_read( + &mut self, + address: Address, + key: Word, + _value: Word, + ) -> Result<(), Self::Error> { + self.bal.accounts.entry(address).or_default().storage.storage.entry(key).or_default(); Ok(()) } } @@ -509,6 +542,38 @@ mod tests { assert!(account.storage.storage.get(&U256::from(5)).unwrap().is_empty()); } + #[test] + fn commit_records_wiped_slot_zeroing_as_read() { + let address = Address::with_last_byte(1); + + // A wiped account whose slot started nonzero and ended zero: the wipe is not a write. + // Only the slot that is nonzero after the wipe is recorded as one. + let info = AccountInfo::default().with_balance(U256::from(100)); + let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; + overlay.slots.insert(U256::from(5), slot(U256::from(42), U256::ZERO)); + overlay.slots.insert(U256::from(6), slot(U256::from(7), U256::from(8))); + let account = Account { + original: Some(info.clone()), + present: Some(info), + storage: overlay, + ..Default::default() + }; + let pending = PendingState { + accounts: AddressMap::from_iter([(address, account)]), + selfdestructs: Default::default(), + }; + + let mut bal = Bal::new(); + bal.commit(idx(1), pending); + + let storage = &bal.accounts.get(&address).unwrap().storage.storage; + assert!(storage.get(&U256::from(5)).unwrap().is_empty()); + assert_eq!( + storage.get(&U256::from(6)).unwrap().changes, + vec![AlloyStorageChange::new(idx(1), U256::from(8))] + ); + } + #[test] fn try_from_alloy_decodes_block_access_list() { let address = Address::with_last_byte(1); diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 3c2bfc5eb..6c06f25bf 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -8,14 +8,14 @@ use crate::{ bal::BalContext, state::{ Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSink, - StateChangeSource, StorageChange, + StateChangeSource, StorageChange, TxStateChanges, }, }, interpreter::Word, }; use alloy_primitives::{ Address, B256, KECCAK256_EMPTY, - map::{AddressMap, B256Map, U256Map, hash_map::Entry}, + map::{AddressMap, AddressSet, B256Map, U256Map, hash_map::Entry}, }; use core::convert::Infallible; @@ -148,17 +148,17 @@ impl CacheDB { /// storage slots, code, and account info are then applied to the cache; the wrapped backing /// database is not written. pub fn commit_pending(&mut self, pending: &PendingState) { - self.commit(&pending.accounts); + self.commit(&pending.accounts, &pending.selfdestructs); } /// Accepts a committed transaction's pending accounts and storage overlays into this cache. /// /// Same as [`Self::commit_pending`], operating on the transaction layers directly so the /// overlay need not be detached. - pub(crate) fn commit(&mut self, accounts: &AddressMap) { + pub(crate) fn commit(&mut self, accounts: &AddressMap, selfdestructs: &AddressSet) { // When BAL construction is enabled, fold the transaction's pending post-state into the // builder before applying the changes to the cache. - self.bal_context.commit(accounts); + self.bal_context.commit(TxStateChanges::new(accounts, selfdestructs)); for (&address, entry) in accounts { let overlay = &entry.storage; diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index f7198d7af..761c69275 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -139,7 +139,7 @@ impl State<'_> { bal_context: self.database.bal_context.clone(), _non_exhaustive: (), }; - database.commit(&self.accounts); + database.commit(&self.accounts, &self.inner.selfdestructs); State { accounts: AddressMap::default(), storage_pool: storage_pool::StoragePool::default(), @@ -1006,7 +1006,7 @@ impl<'a> State<'a> { pub fn commit_transaction(&mut self) { // The transaction overlay is folded into the accepted-overlay database directly, without // detaching it. - self.inner.database.commit(&self.accounts); + self.inner.database.commit(&self.accounts, &self.inner.selfdestructs); for account in self.accounts.values_mut() { self.storage_pool.clear_overlay(&mut account.storage); } From f2537a84f732e497f9d96bb1c0ea38313de32b82 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:05:06 +0100 Subject: [PATCH 074/159] test(eest): recover transaction senders from signatures (#918) The EEST runners trusted the fixture's `sender` and never checked signatures. Three state fixtures (`bad_v_r_s`, `unrecoverable_signature`, `invalid_chain_id`) sat in `IGNORED_TESTS` because of it, and the blockchain `bad_v_r_s` case passed only because the block's gas used did not match. State tests now decode the EIP-2718 transaction in each post entry's `txbytes`, recover its signer and run the decoded transaction. Blockchain tests rebuild the signature from `r`, `s` and `v`, take a legacy transaction's chain ID from `v`, recover the signer, and fail the transaction if it differs from the fixture's `sender`. High-`s` signatures are rejected from Homestead on and accepted before it. A decode or recovery failure is a transaction exception. State fixtures with no `txbytes` (some legacy ones) still use the fixture sender, since they carry nothing to recover from. The three fixtures are un-ignored and pass on the interpreter, JIT and AOT, as does the full interpreter state and blockchain corpus. The blanket `#![allow(dead_code)]` is gone from both `types.rs` files; nothing it hid is unused. Sender recovery is harness validation, not execution, so the blockchain `ExecuteConfig::validate_post_state` flag becomes `validate` and also covers it: when false, the runner trusts the fixture's `sender`. The mainnet replay bench and `replay_bench` set it false, as they already did for the post-state comparison, and the bench's sanity check keeps the default so signatures are still checked once. Without this, recovering 10 blocks of mainnet senders inside the timed loop made the replay bench 64% slower. ## Replay bench `mainnet_25347446_25347455/replay`, criterion wall time on a 12-core box, three alternating rounds (ms): | Build | Round 1 | Round 2 | Round 3 | | --- | --- | --- | --- | | main (66003c7) | 84.49 | 84.40 | 85.30 | | this PR, `validate: false` (bench setting) | 84.37 | 86.08 | 86.25 | | this PR, `validate: true` | 138.33 | 140.18 | 138.40 | With the bench setting the replay is back within about 1% of main; recovering every sender in the timed loop costs about 54 ms. --- crates/cli/benches/evm/mainnet.rs | 3 +- crates/cli/src/replay_bench.rs | 2 +- crates/eest/Cargo.toml | 2 +- crates/eest/src/blockchaintest/error.rs | 17 ++- crates/eest/src/blockchaintest/execute.rs | 111 ++++++++++----- crates/eest/src/blockchaintest/types.rs | 2 - crates/eest/src/error.rs | 7 + crates/eest/src/execute.rs | 47 ++++--- crates/eest/src/runner.rs | 6 - crates/eest/src/tx.rs | 161 ++++++++++++++++++++-- crates/eest/src/types.rs | 5 +- 11 files changed, 284 insertions(+), 79 deletions(-) diff --git a/crates/cli/benches/evm/mainnet.rs b/crates/cli/benches/evm/mainnet.rs index f97d0c2f4..82c64cc89 100644 --- a/crates/cli/benches/evm/mainnet.rs +++ b/crates/cli/benches/evm/mainnet.rs @@ -57,8 +57,7 @@ impl PreparedBench { pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { let (case_name, case) = self.suite.0.first_key_value().expect("fixture must contain a case"); - let config = - BlockchainTestExecuteConfig { validate_post_state: false, ..Default::default() }; + let config = BlockchainTestExecuteConfig { validate: false, ..Default::default() }; group.bench_function(format!("{}/replay", self.name), |b| { b.iter_batched_ref( || self.pre_state.clone(), diff --git a/crates/cli/src/replay_bench.rs b/crates/cli/src/replay_bench.rs index 97bb76dd0..b5ab75799 100644 --- a/crates/cli/src/replay_bench.rs +++ b/crates/cli/src/replay_bench.rs @@ -275,7 +275,7 @@ impl ReplayFixture { Path::new(&self.name), &self.suite, BlockchainTestExecuteConfig { - validate_post_state: false, + validate: false, compare_receipt_root: true, ..Default::default() }, diff --git a/crates/eest/Cargo.toml b/crates/eest/Cargo.toml index 8b071d054..8ef340917 100644 --- a/crates/eest/Cargo.toml +++ b/crates/eest/Cargo.toml @@ -17,7 +17,7 @@ workspace = true evm2.workspace = true evm2-jit-runtime = { workspace = true, features = ["llvm"], optional = true } -alloy-consensus = { workspace = true, features = ["std"] } +alloy-consensus = { workspace = true, features = ["k256", "std"] } alloy-eip7928 = { workspace = true, features = ["std", "serde", "rlp"] } alloy-eips = { workspace = true, features = ["std"] } alloy-primitives = { workspace = true, features = ["std", "serde", "rlp"] } diff --git a/crates/eest/src/blockchaintest/error.rs b/crates/eest/src/blockchaintest/error.rs index e878aa53c..4294c9dc4 100644 --- a/crates/eest/src/blockchaintest/error.rs +++ b/crates/eest/src/blockchaintest/error.rs @@ -56,9 +56,20 @@ pub(crate) enum TestErrorKind { /// The test's blob schedule has a zero base fee update fraction for the active fork. #[error("blob schedule entry for {0:?} has a zero base fee update fraction")] ZeroBlobUpdateFraction(BlobFork), - /// Sender is required in blockchain tests. + /// Sender is required when signatures are not validated. #[error("transaction sender is required")] MissingSender, + /// Signed transaction has an invalid signature. + #[error("{0}")] + InvalidSignedTransaction(String), + /// Sender recovered from the signature differs from the fixture's sender. + #[error("sender mismatch: recovered {recovered}, fixture {expected}")] + SenderMismatch { + /// Sender recovered from the signature. + recovered: alloy_primitives::Address, + /// Sender declared by the fixture. + expected: alloy_primitives::Address, + }, /// An ommer is not old enough, or is too old, to receive a block reward. #[error("invalid ommer age: block {block_number}, ommer {ommer_number}")] InvalidOmmerAge { @@ -141,6 +152,10 @@ impl From for TestErrorKind { TxBuildError::Overflow(name) => Self::Overflow(name), TxBuildError::BuildTransaction(error) => Self::BuildTransaction(error), TxBuildError::SerdeDeserialize(error) => Self::SerdeDeserialize(error), + error @ (TxBuildError::Decode(_) + | TxBuildError::InvalidV(_) + | TxBuildError::ProtectedLegacy + | TxBuildError::Recover(_)) => Self::InvalidSignedTransaction(error.to_string()), } } } diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 52fd58c36..fd000f715 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -20,7 +20,10 @@ use crate::{ forks::is_fork_skipped, state::{insert_account_with_storage, parse_bytecode}, trace::{self, Eip3155Tracer}, - tx::{TxFields, build_recovered_tx, rpc_access_list, signed_authorizations}, + tx::{ + TxFields, TxSigner, build_recovered_tx, rpc_access_list, signature_from_vrs, + signed_authorizations, + }, }; use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; @@ -51,9 +54,11 @@ const ONE_ETHER: u128 = 1_000_000_000_000_000_000; /// Execution options for a single suite. #[derive(Clone, Copy, Debug)] pub struct ExecuteConfig { - /// Whether to validate the full final-state allocation when fixtures contain `postState`. - /// Unexpected live accounts and non-zero storage are rejected. - pub validate_post_state: bool, + /// Whether to run the harness checks that are not part of execution: comparing the final + /// state with the fixture's `postState` (unexpected live accounts and non-zero storage are + /// rejected) and recovering each sender from its signature. When false, the runner trusts + /// the fixture's `sender`. + pub validate: bool, /// Whether to recompute each committed block's state root and compare it to /// the block header's `stateRoot`. pub compare_state_root: bool, @@ -80,7 +85,7 @@ pub struct ExecuteConfig { impl Default for ExecuteConfig { fn default() -> Self { Self { - validate_post_state: true, + validate: true, compare_state_root: false, compare_receipt_root: false, trace: false, @@ -307,6 +312,7 @@ fn execute_case( hook, resources, config.trace, + config.validate, compare_state_root, config.compare_receipt_root, if config.db_stats { Some(&mut db_stats_counts) } else { None }, @@ -344,7 +350,7 @@ fn execute_case( )); } - if config.validate_post_state + if config.validate && let Some(expected) = &test_case.post_state { validate_post_state(database, expected).map_err(|err| TestError::case(path, name, err))?; @@ -395,6 +401,7 @@ fn execute_block( hook: &mut dyn Hook, resources: &ExecutionResources, trace: bool, + validate: bool, compare_state_root: bool, compare_receipt_root: bool, db_stats_counts: Option<&mut DbStatsCounts>, @@ -512,7 +519,7 @@ fn execute_block( transaction_index, total_transactions, }); - let tx = match build_tx(raw_tx) { + let tx = match build_tx(raw_tx, spec, validate) { Ok(tx) => tx, Err(_err) if should_fail => { return Ok(BlockResolution::Discard); @@ -1071,28 +1078,50 @@ fn block_env_from_header( }) } -fn build_tx(raw: &Transaction) -> Result { - let caller = raw.sender.ok_or(TestErrorKind::MissingSender)?; +fn build_tx( + raw: &Transaction, + spec: SpecId, + validate: bool, +) -> Result { let gas_limit = raw.gas_limit.try_into().map_err(|_| TestErrorKind::Overflow("gasLimit"))?; let nonce = raw.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; + let tx_type = raw.tx_type(); + let (signer, chain_id) = if validate { + let (signature, legacy_chain_id) = + signature_from_vrs(raw.r, raw.s, raw.v, tx_type == 0, spec)?; + // A legacy transaction signs its chain ID through `v`, so `v` decides it. + let chain_id = if tx_type == 0 { legacy_chain_id.map(U256::from) } else { raw.chain_id }; + (TxSigner::Signature { signature, spec }, chain_id) + } else { + (TxSigner::Fixture(raw.sender.ok_or(TestErrorKind::MissingSender)?), raw.chain_id) + }; - Ok(build_recovered_tx(TxFields { - tx_type: Some(raw.tx_type()), - caller, - kind: raw.kind(), - data: raw.data.clone(), - gas_limit, - nonce, - value: raw.value, - chain_id: raw.chain_id, - gas_price: raw.gas_price, - max_fee_per_gas: raw.max_fee_per_gas, - max_priority_fee_per_gas: raw.max_priority_fee_per_gas, - access_list: access_list(raw)?, - authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, - blob_versioned_hashes: raw.blob_versioned_hashes.clone(), - max_fee_per_blob_gas: raw.max_fee_per_blob_gas, - })?) + let tx = build_recovered_tx( + TxFields { + tx_type: Some(tx_type), + kind: raw.kind(), + data: raw.data.clone(), + gas_limit, + nonce, + value: raw.value, + chain_id, + gas_price: raw.gas_price, + max_fee_per_gas: raw.max_fee_per_gas, + max_priority_fee_per_gas: raw.max_priority_fee_per_gas, + access_list: access_list(raw)?, + authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, + blob_versioned_hashes: raw.blob_versioned_hashes.clone(), + max_fee_per_blob_gas: raw.max_fee_per_blob_gas, + }, + signer, + )?; + if validate + && let Some(expected) = raw.sender + && expected != tx.signer() + { + return Err(TestErrorKind::SenderMismatch { recovered: tx.signer(), expected }); + } + Ok(tx) } fn access_list(raw: &Transaction) -> Result, TestErrorKind> { @@ -1569,6 +1598,12 @@ mod tests { *, }; #[cfg(feature = "jit")] + use crate::tx::recover_address; + #[cfg(feature = "jit")] + use alloy_consensus::{SignableTransaction, TxLegacy, crypto::secp256k1}; + #[cfg(feature = "jit")] + use alloy_primitives::{TxKind, to_eip155_v}; + #[cfg(feature = "jit")] use evm2::interpreter::op; use std::collections::BTreeMap; @@ -1776,9 +1811,7 @@ mod tests { assert!(matches!(err.kind, TestErrorKind::UnexpectedFailure(message) if message == format!("unexpected account in post-state: {address}"))); - let summary = - run(&suite, ExecuteConfig { validate_post_state: false, ..Default::default() }) - .unwrap(); + let summary = run(&suite, ExecuteConfig { validate: false, ..Default::default() }).unwrap(); assert_eq!(summary.executed, 1); let case = suite.0.get_mut("omitted-account").unwrap(); @@ -2118,8 +2151,18 @@ mod tests { #[cfg(feature = "jit")] fn execute_simple_storage_block(mode: ExecutionMode) -> ExecuteSummary { - let caller = Address::from([0x11; 20]); + let secret = B256::with_last_byte(1); + let caller = recover_address(secret.as_slice()).unwrap(); let target = Address::from([0x22; 20]); + let unsigned = TxLegacy { + chain_id: Some(1), + nonce: 0, + gas_price: 0, + gas_limit: 100_000, + to: TxKind::Call(target), + ..Default::default() + }; + let signature = secp256k1::sign_message(secret, unsigned.signature_hash()).unwrap(); let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let mut pre = BTreeMap::new(); @@ -2193,9 +2236,9 @@ mod tests { gas_limit: U256::from(100_000), gas_price: Some(U256::ZERO), nonce: U256::ZERO, - r: U256::ZERO, - s: U256::ZERO, - v: U256::ZERO, + r: signature.r(), + s: signature.s(), + v: U256::from(to_eip155_v(signature.v() as u8, 1)), value: U256::ZERO, to: Some(target), chain_id: Some(U256::ONE), @@ -2223,7 +2266,7 @@ mod tests { execute_str( Path::new("simple-storage.json"), &input, - ExecuteConfig { validate_post_state: true, mode, ..ExecuteConfig::default() }, + ExecuteConfig { validate: true, mode, ..ExecuteConfig::default() }, &NameFilter::default(), &mut hook, ) diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 9c3172244..b65464d08 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - use crate::tx::{AccessListItem, TestAuthorization}; use alloy_eip7928::BlockAccessList; use alloy_eips::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, eip7840::BlobParams}; diff --git a/crates/eest/src/error.rs b/crates/eest/src/error.rs index b12759829..14feba739 100644 --- a/crates/eest/src/error.rs +++ b/crates/eest/src/error.rs @@ -84,6 +84,9 @@ pub(crate) enum TestErrorKind { /// Transaction request could not be converted to a consensus transaction. #[error("could not build consensus transaction: {0}")] BuildTransaction(String), + /// Signed transaction could not be decoded or its sender recovered. + #[error("{0}")] + InvalidSignedTransaction(String), /// Execution resource initialization or compilation failed. #[error(transparent)] ExecutionResource(#[from] ExecutionResourceError), @@ -95,6 +98,10 @@ impl From for TestErrorKind { TxBuildError::Overflow(name) => Self::Overflow(name), TxBuildError::BuildTransaction(error) => Self::BuildTransaction(error), TxBuildError::SerdeDeserialize(error) => Self::SerdeDeserialize(error), + error @ (TxBuildError::Decode(_) + | TxBuildError::InvalidV(_) + | TxBuildError::ProtectedLegacy + | TxBuildError::Recover(_)) => Self::InvalidSignedTransaction(error.to_string()), } } } diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index ff3f136e3..b3e0b7925 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -11,7 +11,10 @@ use crate::{ insert_account_with_storage, parse_bytecode, storage_for_root, system_contract_has_code, }, trace::{self, Eip3155Tracer}, - tx::{TxFields, build_recovered_tx, recover_address, rpc_access_list, signed_authorizations}, + tx::{ + TxFields, TxSigner, build_recovered_tx, decode_recovered_tx, recover_address, + rpc_access_list, signed_authorizations, + }, types::{AccountInfo, Env, Test, TestSuite, TestUnit, TransactionParts, TxPartIndices}, }; use alloy_eips::{eip4844, eip7691}; @@ -204,7 +207,11 @@ fn execute_unit( } let block = parse_block(&unit.env, spec); for post in posts { - let tx = match build_tx(&unit.transaction, &post.indexes, unit.env.current_chain_id) { + let tx = match &post.txbytes { + Some(bytes) => decode_recovered_tx(bytes, spec).map_err(Into::into), + None => build_tx(&unit.transaction, &post.indexes, unit.env.current_chain_id), + }; + let tx = match tx { Ok(tx) => tx, Err(_) if post.expect_exception.is_some() => { continue; @@ -602,23 +609,25 @@ fn build_tx( let value = *raw.value.get(indexes.value).ok_or(TestErrorKind::BadIndex("value"))?; let nonce = raw.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; - Ok(build_recovered_tx(TxFields { - tx_type: raw.tx_type, - caller, - kind: TxKind::from(raw.to), - data, - gas_limit, - nonce, - value, - chain_id: raw.chain_id.or(chain_id), - gas_price: raw.gas_price, - max_fee_per_gas: raw.max_fee_per_gas, - max_priority_fee_per_gas: raw.max_priority_fee_per_gas, - access_list: access_list(raw, indexes.data)?, - authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, - blob_versioned_hashes: raw.blob_versioned_hashes.clone(), - max_fee_per_blob_gas: raw.max_fee_per_blob_gas, - })?) + Ok(build_recovered_tx( + TxFields { + tx_type: raw.tx_type, + kind: TxKind::from(raw.to), + data, + gas_limit, + nonce, + value, + chain_id: raw.chain_id.or(chain_id), + gas_price: raw.gas_price, + max_fee_per_gas: raw.max_fee_per_gas, + max_priority_fee_per_gas: raw.max_priority_fee_per_gas, + access_list: access_list(raw, indexes.data)?, + authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, + blob_versioned_hashes: raw.blob_versioned_hashes.clone(), + max_fee_per_blob_gas: raw.max_fee_per_blob_gas, + }, + TxSigner::Fixture(caller), + )?) } fn access_list( diff --git a/crates/eest/src/runner.rs b/crates/eest/src/runner.rs index 860bce687..70e2f57bc 100644 --- a/crates/eest/src/runner.rs +++ b/crates/eest/src/runner.rs @@ -136,12 +136,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "create2collisionStorageParis.json", "dynamicAccountOverwriteEmpty.json", "dynamicAccountOverwriteEmpty_Paris.json", - - // The state-test runner takes the sender from the fixture and does not recover or - // validate legacy-tx signatures, so v/r/s and chain-id rejection tests cannot pass. - "frontier/validation/transaction/bad_v_r_s.json", - "frontier/validation/transaction/unrecoverable_signature.json", - "london/eip1559_fee_market_change/tx_type/invalid_chain_id.json", ]; fn should_ignore(name: &str) -> bool { diff --git a/crates/eest/src/tx.rs b/crates/eest/src/tx.rs index 9bda2c0e6..059d59ff5 100644 --- a/crates/eest/src/tx.rs +++ b/crates/eest/src/tx.rs @@ -1,11 +1,21 @@ -use alloy_consensus::{TypedTransaction, transaction::Recovered}; -use alloy_eips::eip7702::SignedAuthorization; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; +use alloy_consensus::{ + EthereumTxEnvelope, SignableTransaction, TxEip4844, TypedTransaction, + crypto::{RecoveryError, secp256k1}, + transaction::Recovered, +}; +use alloy_eips::{ + eip2718::{Decodable2718, Eip2718Error}, + eip7702::SignedAuthorization, +}; +use alloy_primitives::{Address, B256, Bytes, Signature, TxKind, U256}; use alloy_rpc_types_eth::{ AccessList as RpcAccessList, AccessListItem as RpcAccessListItem, TransactionInput, TransactionRequest, }; -use evm2::ethereum::{RecoveredTxEnvelope, TxEnvelope}; +use evm2::{ + SpecId, + ethereum::{RecoveredTxEnvelope, TxEnvelope}, +}; use k256::ecdsa::SigningKey; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -67,14 +77,32 @@ pub(crate) enum TxBuildError { /// JSON decoding failed. #[error(transparent)] SerdeDeserialize(#[from] serde_json::Error), + /// Signed transaction bytes could not be decoded. + #[error("could not decode signed transaction: {0}")] + Decode(#[from] Eip2718Error), + /// Signature `v` value is not valid for the transaction type. + #[error("invalid signature v value: {0}")] + InvalidV(U256), + /// An EIP-155 legacy transaction appears before Spurious Dragon. + #[error("EIP-155 transaction before Spurious Dragon")] + ProtectedLegacy, + /// Sender could not be recovered from the signature. + #[error("could not recover sender: {0}")] + Recover(#[from] RecoveryError), +} + +/// Where a transaction's sender comes from. +pub(crate) enum TxSigner { + /// Sender taken from the fixture, for fixtures that carry no signature. + Fixture(Address), + /// Sender recovered from the signature, as the fork at `spec` validates it. + Signature { signature: Signature, spec: SpecId }, } /// Common transaction fields already selected from a fixture. pub(crate) struct TxFields { /// Explicit transaction type. pub(crate) tx_type: Option, - /// Recovered or explicit sender. - pub(crate) caller: Address, /// Transaction recipient, or create. pub(crate) kind: TxKind, /// Transaction calldata. @@ -104,9 +132,11 @@ pub(crate) struct TxFields { } /// Builds an evm2 recovered transaction envelope from common fixture fields. -pub(crate) fn build_recovered_tx(fields: TxFields) -> Result { +pub(crate) fn build_recovered_tx( + fields: TxFields, + signer: TxSigner, +) -> Result { let mut request = TransactionRequest::default() - .from(fields.caller) .gas_limit(fields.gas_limit) .nonce(fields.nonce) .value(fields.value) @@ -162,7 +192,69 @@ pub(crate) fn build_recovered_tx(fields: TxFields) -> Result caller, + TxSigner::Signature { signature, spec } => { + recover_sender(&signature, tx.signature_hash(), spec)? + } + }; + Ok(recovered_envelope(tx, caller)) +} + +/// Decodes an EIP-2718 signed transaction and recovers its sender. +pub(crate) fn decode_recovered_tx( + bytes: &[u8], + spec: SpecId, +) -> Result { + let tx = EthereumTxEnvelope::::decode_2718_exact(bytes)?; + if let EthereumTxEnvelope::Legacy(signed) = &tx + && signed.tx().chain_id.is_some() + && !spec.enables(SpecId::SPURIOUS_DRAGON) + { + return Err(TxBuildError::ProtectedLegacy); + } + let caller = recover_sender(tx.signature(), tx.signature_hash(), spec)?; + Ok(Recovered::new_unchecked(tx.into(), caller)) +} + +/// Splits a fixture `v` value into the signature parity and, for legacy transactions, the EIP-155 +/// chain ID it encodes. A legacy `v` is 27 or 28, or from Spurious Dragon on `35 + 2 * chain_id` +/// plus the parity. +pub(crate) fn signature_from_vrs( + r: U256, + s: U256, + v: U256, + legacy: bool, + spec: SpecId, +) -> Result<(Signature, Option), TxBuildError> { + let invalid = || TxBuildError::InvalidV(v); + let (y_parity, chain_id) = if !legacy && v <= U256::ONE { + (v == U256::ONE, None) + } else if legacy && (v == U256::from(27) || v == U256::from(28)) { + (v == U256::from(28), None) + } else if legacy && v >= U256::from(35) { + if !spec.enables(SpecId::SPURIOUS_DRAGON) { + return Err(TxBuildError::ProtectedLegacy); + } + let encoded = v - U256::from(35); + (encoded.bit(0), Some(u64::try_from(encoded >> 1).map_err(|_| invalid())?)) + } else { + return Err(invalid()); + }; + Ok((Signature::new(r, s, y_parity), chain_id)) +} + +fn recover_sender( + signature: &Signature, + hash: B256, + spec: SpecId, +) -> Result { + // EIP-2 rejects high-`s` signatures from Homestead on. + if spec.enables(SpecId::HOMESTEAD) { + secp256k1::recover_signer(signature, hash) + } else { + secp256k1::recover_signer_unchecked(signature, hash) + } } /// Converts EEST access list items into RPC access list items. @@ -209,7 +301,9 @@ fn recovered_envelope(tx: TypedTransaction, caller: Address) -> RecoveredTxEnvel #[cfg(test)] mod tests { - use super::TestAuthorization; + use super::*; + use alloy_consensus::{TxLegacy, crypto::secp256k1}; + use alloy_eips::eip2718::Encodable2718; use serde_json::json; #[test] @@ -226,4 +320,51 @@ mod tests { assert_eq!(serde_json::to_value(authorization).unwrap(), value); } + + #[test] + fn signature_from_vrs_accepts_the_v_each_fork_allows() { + let (r, s) = (U256::ONE, U256::ONE); + let v = |v: u64| U256::from(v); + let parse = |v: U256, legacy, spec| signature_from_vrs(r, s, v, legacy, spec); + + assert_eq!(parse(v(27), true, SpecId::FRONTIER).unwrap().1, None); + assert!(!parse(v(27), true, SpecId::FRONTIER).unwrap().0.v()); + assert!(parse(v(28), true, SpecId::FRONTIER).unwrap().0.v()); + assert!(matches!( + parse(v(37), true, SpecId::TANGERINE), + Err(TxBuildError::ProtectedLegacy) + )); + let (signature, chain_id) = parse(v(38), true, SpecId::SPURIOUS_DRAGON).unwrap(); + assert!(signature.v()); + assert_eq!(chain_id, Some(1)); + assert!(matches!(parse(v(29), true, SpecId::CANCUN), Err(TxBuildError::InvalidV(_)))); + assert!(matches!(parse(v(27), false, SpecId::CANCUN), Err(TxBuildError::InvalidV(_)))); + assert!(parse(v(1), false, SpecId::CANCUN).unwrap().0.v()); + } + + #[test] + fn signature_from_vrs_keeps_v_at_full_width() { + let encoded = |chain_id: u128| U256::from(chain_id) * U256::from(2) + U256::from(35); + let parse = |v| signature_from_vrs(U256::ONE, U256::ONE, v, true, SpecId::CANCUN); + + // The encoded `v` of the largest chain ID needs 65 bits. + let chain_id = u128::from(u64::MAX); + assert_eq!(parse(encoded(chain_id)).unwrap().1, Some(u64::MAX)); + assert!(matches!(parse(encoded(chain_id + 1)), Err(TxBuildError::InvalidV(_)))); + } + + #[test] + fn decode_recovered_tx_rejects_eip155_before_spurious_dragon() { + let secret = B256::with_last_byte(1); + let tx = TxLegacy { chain_id: Some(1), gas_limit: 21_000, ..Default::default() }; + let signature = secp256k1::sign_message(secret, tx.signature_hash()).unwrap(); + let bytes = tx.into_signed(signature).encoded_2718(); + + assert!(matches!( + decode_recovered_tx(&bytes, SpecId::TANGERINE), + Err(TxBuildError::ProtectedLegacy) + )); + let recovered = decode_recovered_tx(&bytes, SpecId::SPURIOUS_DRAGON).unwrap(); + assert_eq!(recovered.signer(), recover_address(secret.as_slice()).unwrap()); + } } diff --git a/crates/eest/src/types.rs b/crates/eest/src/types.rs index ceefda94d..48df9b1e3 100644 --- a/crates/eest/src/types.rs +++ b/crates/eest/src/types.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - use crate::tx::{AccessListItem, TestAuthorization}; use alloy_primitives::{Address, B256, Bytes, U256}; use evm2::SpecId; @@ -231,7 +229,8 @@ pub struct Test { /// Optional full expected state. #[serde(default)] pub state: BTreeMap, - /// Optional encoded transaction bytes. + /// Signed EIP-2718 transaction bytes. When present they replace the indexed `transaction` + /// fields and the sender is recovered from their signature. pub txbytes: Option, } From 1d7c7c276d73828abb5e4d73b5334ddfe9d525be Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:05:16 +0100 Subject: [PATCH 075/159] fix(eest): validate block limits in the blockchain harness (#951) The blockchain harness skipped block validity checks that execution-specs performs, and ignored the fixtures that test them. `execute_block` now checks the cumulative block gas allowance on every fork (it was Amsterdam only), the EIP-7934 block RLP size limit from Osaka, and the EIP-4844 header fields: excess blob gas against the parent via `BlobParams`, blob gas used against the transactions, field presence by fork, and the block blob gas limit. A failing check is an invalid block and is discarded when the fixture expects an exception. Checked system calls (EIP-7002, EIP-7251, EIP-8282) now follow the checked system transaction rule: a target without code, or a failed call, makes the block invalid. The dead branch that re-read the account after a failed call and returned `Ok` for a codeless target is gone, since core already treats a call to a codeless address as a successful no-op. The ignore entries for the cumulative gas, block RLP size and blob header/count fixtures are removed. Consensus-only entries (gas limit minimum, invalid header, withdrawals root, slot and BAL field presence, full-blob RLP decoding) stay. The Prague request fixtures stay ignored until the requests-hash work. --- crates/eest/src/blockchaintest/execute.rs | 361 ++++++++++++++++++---- crates/eest/src/blockchaintest/runner.rs | 43 +-- 2 files changed, 308 insertions(+), 96 deletions(-) diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index fd000f715..398fd0f23 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -27,8 +27,12 @@ use crate::{ }; use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; -use alloy_eips::{eip2718::Typed2718, eip7840::BlobParams}; -use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, U256}; +use alloy_eips::{ + eip2718::Typed2718, + eip4844::DATA_GAS_PER_BLOB, + eip7840::{BLOB_BASE_COST, BlobParams}, +}; +use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, U256, aliases::U512}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use anstyle::{AnsiColor, Color, Style}; use evm2::{ @@ -51,6 +55,10 @@ pub use crate::execution::ExecutionMode; const ONE_GWEI: u64 = 1_000_000_000; const ONE_ETHER: u128 = 1_000_000_000_000_000_000; +/// EIP-7934 maximum RLP-encoded block size: `MAX_BLOCK_SIZE` (10 MiB) minus `SAFETY_MARGIN` +/// (2 MiB). +const MAX_RLP_BLOCK_SIZE: usize = 10 * 1024 * 1024 - 2 * 1024 * 1024; + /// Execution options for a single suite. #[derive(Clone, Copy, Debug)] pub struct ExecuteConfig { @@ -268,17 +276,15 @@ fn execute_case( let schedule = case_schedule(path, name, test_case)?; let spec = schedule.initial.spec; let mut parent_block_hash = Some(test_case.genesis_block_header.hash); - let mut parent_excess_blob_gas = - test_case.genesis_block_header.excess_blob_gas.unwrap_or_default().saturating_to::(); - let mut block_env = - block_env_from_header(&test_case.genesis_block_header, parent_excess_blob_gas, &schedule) - .map_err(|err| TestError::case(path, name, err))?; + let mut parent_header = &test_case.genesis_block_header; + let mut block_env = block_env_from_header(parent_header, &schedule) + .map_err(|err| TestError::case(path, name, err))?; let total_blocks = test_case.blocks.len(); let mut db_stats_counts = DbStatsCounts::default(); // `evm2 capture` keeps only the accounts the blocks touch and zeroes the genesis state // root, so its blocks' state roots can't be reproduced. Receipt roots still check them. - let compare_state_root = - config.compare_state_root && !test_case.genesis_block_header.state_root.is_zero(); + let captured = test_case.genesis_block_header.state_root.is_zero(); + let compare_state_root = config.compare_state_root && !captured; hook.case_started(CaseStarted { name, total_blocks, network: test_case.network }); @@ -308,7 +314,8 @@ fn execute_case( database, &mut block_env, &mut parent_block_hash, - &mut parent_excess_blob_gas, + &mut parent_header, + captured, hook, resources, config.trace, @@ -384,20 +391,21 @@ enum BlockResolution { } #[expect(clippy::too_many_arguments)] -fn execute_block( +fn execute_block<'a>( path: &Path, name: &str, block_index: usize, total_blocks: usize, block_number: Option, total_transactions: usize, - block: &Block, + block: &'a Block, schedule: &ForkSchedule, spec: SpecId, database: &mut InMemoryDB, block_env: &mut BlockEnv, parent_block_hash: &mut Option, - parent_excess_blob_gas: &mut u64, + parent_header: &mut &'a BlockHeader, + captured: bool, hook: &mut dyn Hook, resources: &ExecutionResources, trace: bool, @@ -424,18 +432,44 @@ fn execute_block( let mut block_hash = None; let mut beacon_root = None; - let mut this_excess_blob_gas = None; let mut expected_state_root = None; + let mut blob_params = None; let mut next_block_env = *block_env; if let Some(header) = block_header(block) { block_hash = Some(header.hash); beacon_root = header.parent_beacon_block_root; - next_block_env = block_env_from_header(header, *parent_excess_blob_gas, schedule) + next_block_env = block_env_from_header(header, schedule) .map_err(|err| TestError::case(path, name, err))?; - this_excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()); + blob_params = schedule + .blob_params(header.number, header.timestamp) + .map_err(|err| TestError::case(path, name, err))? + // The reserve price of EIP-7918 comes from the spec, not from the fixture's schedule. + .map(|params| { + if spec.enables(SpecId::OSAKA) { + params.with_blob_base_cost(BLOB_BASE_COST) + } else { + params + } + }); expected_state_root = Some(header.state_root); } + // The blob checks multiply the blob counts by the blob gas, which must not overflow. + if let Some(params) = &blob_params + && (params.max_blob_count == 0 + || params.max_blob_count < params.target_blob_count + || params.max_blob_count.checked_mul(DATA_GAS_PER_BLOB).is_none()) + { + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure(format!( + "invalid blob schedule: max {} and target {} must satisfy target <= max, max > 0, and \ + fit in blob gas", + params.max_blob_count, params.target_blob_count + )), + )); + } let initial_database = mem::take(database); let receipt_base = (compare_receipt_root && !spec.enables(SpecId::BYZANTIUM)) @@ -489,6 +523,15 @@ fn execute_block( )); } + // A captured case's genesis header is the first block's own header, not its parent's. + let parent = (!(captured && block_index == 0)).then_some(*parent_header); + if let Some(header) = block_header(block) + && let Err(reason) = + validate_block_header(block, header, parent, blob_params.as_ref(), spec) + { + return reject_block(should_fail, path, name, reason); + } + if let Err(err) = pre_block_system_calls( &mut evm, &mut block_state, @@ -510,6 +553,7 @@ fn execute_block( let mut cumulative_tx_gas_used = 0u64; let mut block_execution_gas_used = 0u64; let mut block_state_gas_used = 0u64; + let mut block_blob_gas_used = 0u64; let mut receipts = Vec::with_capacity(transactions.len()); for (transaction_index, raw_tx) in transactions.iter().enumerate() { hook.transaction_started(TransactionStarted { @@ -537,32 +581,56 @@ fn execute_block( } }; - // EIP-8037: per-dimension block-level inclusion check (execution-specs - // `process_transaction`). A transaction is only includable when its gas limit -- + // Block-level inclusion check (execution-specs `check_transaction`): the transaction's + // gas limit must fit the block gas remaining, and its blobs the block blob gas + // remaining. Under EIP-8037 (Amsterdam+) the gas limit is checked per dimension: // capped by the EIP-7825 limit on the execution dimension, uncapped on the state - // dimension -- still fits the block gas remaining on both dimensions. - if spec.enables(SpecId::AMSTERDAM) - && let Some(header) = block_header(block) - { + // dimension. + if let Some(header) = block_header(block) { let block_gas_limit = header.gas_limit.saturating_to::(); let tx_gas_limit = tx.gas_limit(); - let execution_gas_available = - block_gas_limit.saturating_sub(block_execution_gas_used); - let state_gas_available = block_gas_limit.saturating_sub(block_state_gas_used); - if tx_gas_limit.min(evm.version().tx_gas_limit_cap) > execution_gas_available + let (execution_gas_used, state_gas_used, execution_gas_limit) = + if spec.enables(SpecId::AMSTERDAM) { + ( + block_execution_gas_used, + block_state_gas_used, + tx_gas_limit.min(evm.version().tx_gas_limit_cap), + ) + } else { + (cumulative_tx_gas_used, cumulative_tx_gas_used, tx_gas_limit) + }; + let execution_gas_available = block_gas_limit.saturating_sub(execution_gas_used); + let state_gas_available = block_gas_limit.saturating_sub(state_gas_used); + if execution_gas_limit > execution_gas_available || tx_gas_limit > state_gas_available { - if should_fail { - return Ok(BlockResolution::Discard); - } - return Err(TestError::case( + return reject_block( + should_fail, path, name, - TestErrorKind::UnexpectedFailure(format!( + format!( "transaction gas limit {tx_gas_limit} exceeds available block gas \ (execution {execution_gas_available}, state {state_gas_available})" - )), - )); + ), + ); + } + + if let Some(blob_params) = &blob_params { + let blob_gas = blob_gas_used(raw_tx); + let blob_gas_available = + blob_params.max_blob_gas_per_block().saturating_sub(block_blob_gas_used); + if blob_gas > blob_gas_available { + return reject_block( + should_fail, + path, + name, + format!( + "transaction blob gas {blob_gas} exceeds available block blob \ + gas {blob_gas_available}" + ), + ); + } + block_blob_gas_used += blob_gas; } } @@ -654,6 +722,21 @@ fn execute_block( } } + if spec.enables(SpecId::CANCUN) + && let Some(header) = block_header(block) + && header.blob_gas_used != Some(U256::from(block_blob_gas_used)) + { + return reject_block( + should_fail, + path, + name, + format!( + "block blob gas used mismatch: header {:?}, computed {block_blob_gas_used}", + header.blob_gas_used + ), + ); + } + if compare_receipt_root && let Some(header) = block_header(block) { let got = alloy_consensus::proofs::calculate_receipt_root(&receipts); if got != header.receipt_trie { @@ -680,6 +763,7 @@ fn execute_block( next_block_env, block_ommers(block), block_withdrawals(block), + captured, ) { // A failed post-block system call (e.g. SYSTEM_CONTRACT_CALL_FAILED) invalidates the // block, mirroring execution-specs raising `InvalidBlock`. @@ -739,8 +823,8 @@ fn execute_block( *database = restored_database; *block_env = next_block_env; *parent_block_hash = block_hash; - if let Some(excess_blob_gas) = this_excess_blob_gas { - *parent_excess_blob_gas = excess_blob_gas; + if let Some(header) = block_header(block) { + *parent_header = header; } if trace || compare_state_root { let got = state_root_from_database(database); @@ -844,6 +928,127 @@ fn block_ommers(block: &Block) -> &[BlockHeader] { block.rlp_decoded.as_ref().map(|decoded| decoded.uncle_headers.as_slice()).unwrap_or_default() } +/// Block-level validity checks that execution-specs performs before executing the transactions: +/// the EIP-7934 block RLP size limit and the EIP-4844 header blob fields. +fn validate_block_header( + block: &Block, + header: &BlockHeader, + parent: Option<&BlockHeader>, + blob_params: Option<&BlobParams>, + spec: SpecId, +) -> Result<(), String> { + if spec.enables(SpecId::OSAKA) && block.rlp.len() > MAX_RLP_BLOCK_SIZE { + return Err(format!( + "block RLP size {} exceeds the limit {MAX_RLP_BLOCK_SIZE}", + block.rlp.len() + )); + } + + // The blob parameters are `None` before Cancun. + let Some(blob_params) = blob_params else { + if header.excess_blob_gas.is_some() || header.blob_gas_used.is_some() { + return Err("pre-Cancun block header carries blob gas fields".to_string()); + } + return Ok(()); + }; + + if header.blob_gas_used.is_none() { + return Err("block header is missing blobGasUsed".to_string()); + } + let Some(parent) = parent else { return Ok(()) }; + let parent_excess = parent.excess_blob_gas.unwrap_or_default(); + let parent_used = parent.blob_gas_used.unwrap_or_default(); + // Reject parent values whose sum the excess blob gas formula cannot add without overflow. + if parent_excess.saturating_add(parent_used) > U256::from(u64::MAX) { + return Err(format!( + "parent blob gas fields out of range: excess {parent_excess}, used {parent_used}" + )); + } + let parent_base_fee = parent.base_fee_per_gas.unwrap_or_default(); + let expected_excess_blob_gas = + next_excess_blob_gas(blob_params, parent_excess.to(), parent_used.to(), parent_base_fee); + if header.excess_blob_gas != Some(U256::from(expected_excess_blob_gas)) { + return Err(format!( + "excess blob gas mismatch: header {:?}, expected {expected_excess_blob_gas}", + header.excess_blob_gas + )); + } + Ok(()) +} + +/// Returns the excess blob gas of the block after a parent with these values (execution-specs +/// `calculate_excess_blob_gas`, with the EIP-7918 reserve price from Osaka). Unlike +/// [`BlobParams::next_block_excess_blob_gas_osaka`], the reserve price comparison takes a base +/// fee of any width. +fn next_excess_blob_gas( + params: &BlobParams, + parent_excess: u64, + parent_used: u64, + parent_base_fee: U256, +) -> u64 { + let excess = parent_excess + parent_used; + let target = params.target_blob_gas_per_block(); + if excess < target { + return 0; + } + if reserve_price_exceeds_blob_price(params, parent_excess, parent_base_fee) { + // The scaled blob gas is at most `parent_used`, so the sum stays below `u64::MAX`. + let scaled_used = u128::from(parent_used) + * u128::from(params.max_blob_count - params.target_blob_count) + / u128::from(params.max_blob_count); + parent_excess + scaled_used as u64 + } else { + excess - target + } +} + +/// Returns whether `blob_base_cost * base_fee > DATA_GAS_PER_BLOB * blob_price` for the blob price +/// at this excess blob gas, in unbounded integer arithmetic like execution-specs. The blob price +/// is the EIP-4844 `fake_exponential` sum, which this stops accumulating once it reaches the +/// reserve price, so every product stays far below the 512-bit limit. +fn reserve_price_exceeds_blob_price( + params: &BlobParams, + excess_blob_gas: u64, + base_fee: U256, +) -> bool { + let reserve_price = U512::from(params.blob_base_cost) * U512::from(base_fee); + let denominator = U512::from(params.update_fraction); + let numerator = U512::from(excess_blob_gas); + let gas_per_blob = U512::from(DATA_GAS_PER_BLOB); + let mut term = U512::from(params.min_blob_fee) * denominator; + let mut sum = U512::ZERO; + let mut index = U512::ONE; + while !term.is_zero() { + sum += term; + if gas_per_blob * (sum / denominator) >= reserve_price { + return false; + } + term = term * numerator / (denominator * index); + index += U512::ONE; + } + reserve_price > gas_per_blob * (sum / denominator) +} + +/// Resolves an invalid block: it is discarded when the fixture expects an exception, and a test +/// failure otherwise. +fn reject_block( + should_fail: bool, + path: &Path, + name: &str, + reason: String, +) -> Result { + if should_fail { + Ok(BlockResolution::Discard) + } else { + Err(TestError::case(path, name, TestErrorKind::UnexpectedFailure(reason))) + } +} + +/// Returns the blob gas a transaction consumes (EIP-4844). +const fn blob_gas_used(tx: &Transaction) -> u64 { + tx.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB +} + fn pre_block_system_calls( evm: &mut Evm<'_, BaseEvmTypes>, block_state: &mut BlockStateAccumulator, @@ -864,7 +1069,7 @@ fn pre_block_system_calls( BEACON_ROOTS_ADDRESS, Bytes::copy_from_slice(root.as_slice()), "eip4788", - false, + SystemCallCheck::None, )?; } if spec.enables(SpecId::PRAGUE) @@ -876,7 +1081,7 @@ fn pre_block_system_calls( HISTORY_STORAGE_ADDRESS, Bytes::copy_from_slice(hash.as_slice()), "eip2935", - false, + SystemCallCheck::None, )?; } Ok(()) @@ -889,7 +1094,10 @@ fn post_block_transition( block: BlockEnv, ommers: &[BlockHeader], withdrawals: &[Withdrawal], + captured: bool, ) -> Result<(), TestErrorKind> { + // A captured case keeps only the accounts its blocks touch, so the system contracts are absent. + let check = if captured { SystemCallCheck::Outcome } else { SystemCallCheck::OutcomeAndCode }; let reward = block_reward(spec, ommers.len()); if reward != 0 { increment_balance(evm, block_state, block.beneficiary, U256::from(reward))?; @@ -931,7 +1139,7 @@ fn post_block_transition( WITHDRAWAL_REQUEST_ADDRESS, Bytes::new(), "eip7002", - true, + check, )?; run_system_call( evm, @@ -939,7 +1147,7 @@ fn post_block_transition( evm2::evm::CONSOLIDATION_REQUEST_ADDRESS, Bytes::new(), "eip7251", - true, + check, )?; } @@ -951,7 +1159,7 @@ fn post_block_transition( evm2::evm::BUILDER_DEPOSIT_REQUEST_ADDRESS, Bytes::new(), "eip8282_deposit", - true, + check, )?; run_system_call( evm, @@ -959,36 +1167,53 @@ fn post_block_transition( evm2::evm::BUILDER_EXIT_REQUEST_ADDRESS, Bytes::new(), "eip8282_exit", - true, + check, )?; } Ok(()) } +/// What a system call must satisfy for the block to be valid. +#[derive(Clone, Copy, Eq, PartialEq)] +enum SystemCallCheck { + /// The call may fail (EIP-4788, EIP-2935). + None, + /// The call must succeed. + Outcome, + /// The target must have code, and the call must succeed (execution-specs + /// `process_checked_system_transaction`). + OutcomeAndCode, +} + fn run_system_call( evm: &mut Evm<'_, BaseEvmTypes>, block_state: &mut BlockStateAccumulator, address: Address, data: Bytes, label: &'static str, - checked: bool, + check: SystemCallCheck, ) -> Result<(), TestErrorKind> { + // A checked call to a target without code is an invalid block (execution-specs + // `SYSTEM_CONTRACT_EMPTY`); the EVM itself treats it as a successful no-op. + if check == SystemCallCheck::OutcomeAndCode { + let info = evm + .read_account_info(&address) + .map_err(|code| TestErrorKind::UnexpectedFailure(code.to_string()))?; + if info.is_none_or(|info| info.is_empty_code_hash()) { + return Err(TestErrorKind::UnexpectedFailure(format!( + "{label} system contract has no code" + ))); + } + } let executed = evm.system_call(SystemTx::new(address, data))?; if !executed.result().status { // A failed unchecked call keeps the state accesses it recorded, which a BAL lists as reads. - if !checked { + if check == SystemCallCheck::None { let _ = executed.commit_to(block_state); return Ok(()); } let _ = executed.discard(); - let has_code = match evm.read_account_info(&address) { - Ok(info) => info.is_some_and(|info| !info.is_empty_code_hash()), - Err(code) => return Err(TestErrorKind::UnexpectedFailure(code.to_string())), - }; - if has_code { - return Err(TestErrorKind::SystemCall(label)); - } - return Ok(()); + return Err(TestErrorKind::SystemCall(label)); } let _ = executed.commit_to(block_state); Ok(()) @@ -1051,11 +1276,9 @@ fn parse_state( fn block_env_from_header( header: &BlockHeader, - excess_blob_gas: u64, schedule: &ForkSchedule, ) -> Result { - let excess_blob_gas = - header.excess_blob_gas.map(|gas| gas.saturating_to::()).unwrap_or(excess_blob_gas); + let excess_blob_gas = header.excess_blob_gas.unwrap_or_default().saturating_to::(); let blob_basefee = schedule .blob_params(header.number, header.timestamp)? .map_or(U256::ONE, |params| U256::from(params.calc_blob_fee(excess_blob_gas))); @@ -1946,6 +2169,8 @@ mod tests { number: U256::ONE, gas_limit: U256::from(30_000_000), receipt_trie: B256::ZERO, + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..Default::default() }), ..Default::default() @@ -1977,6 +2202,30 @@ mod tests { assert!(matches!(error.kind, super::TestErrorKind::ReceiptRootMismatch { .. })); } + #[test] + fn next_excess_blob_gas_matches_blob_params_and_keeps_full_width_base_fees() { + let params = BlobParams::osaka(); + for excess in [0, 786_432, 5_000_000, 249_036_800, 400_000_000] { + for used in [0, 131_072, 786_432] { + for base_fee in [0, 1, 1_000_000_000, u64::MAX] { + assert_eq!( + next_excess_blob_gas(¶ms, excess, used, U256::from(base_fee)), + params.next_block_excess_blob_gas_osaka(excess, used, base_fee), + ); + } + } + } + + // The reserve price, 2^256, exceeds the blob price, so the reserve branch applies. + assert_eq!(next_excess_blob_gas(¶ms, 786_432, 0, U256::ONE << 243), 786_432); + // The blob price exceeds `u128`, and 2^145 times it still exceeds this reserve price, so + // the target subtraction applies. + assert_eq!( + next_excess_blob_gas(¶ms, 1_000_000_000, 786_432, U256::ONE << 140), + 1_000_000_000, + ); + } + #[test] fn rejects_block_with_wrong_parent_hash() { use super::{ @@ -2118,6 +2367,8 @@ mod tests { hash: block_hash, number: U256::ONE, gas_limit: U256::from(30_000_000), + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..Default::default() }), ..Default::default() @@ -2227,6 +2478,8 @@ mod tests { gas_used: U256::from(43_105), base_fee_per_gas: Some(U256::ZERO), timestamp: U256::ONE, + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..BlockHeader::default() }), transactions: Some(vec![Transaction { diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index 3660fdb7d..805e5b051 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -132,45 +132,13 @@ fn should_ignore(name: &str) -> bool { #[rustfmt::skip] pub(crate) const IGNORED_TESTS: &[&str] = &[ - // Block header/blob-gas validation is consensus-level block validation, not EVM execution. - "cancun/eip4844_blobs/test_blob_type_tx_pre_fork.json", - "cancun/eip4844_blobs/test_invalid_blob_gas_used_in_header.json", - "cancun/eip4844_blobs/test_invalid_block_blob_count.json", - // The same tests were renamed into subdirectories on BAL releases. - "cancun/eip4844_blobs/blob_txs/invalid_block_blob_count.json", - "cancun/eip4844_blobs/excess_blob_gas/invalid_blob_gas_used_in_header.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_above_target_change.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_change.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_target_blobs_increase_from_zero.json", - "cancun/eip4844_blobs/test_invalid_negative_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_non_multiple_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_post_fork_block_without_blob_fields.json", - "cancun/eip4844_blobs/test_invalid_pre_fork_block_with_blob_fields.json", - "cancun/eip4844_blobs/blob_txs/blob_type_tx_pre_fork.json", - "cancun/eip4844_blobs/excess_blob_gas_fork_transition/invalid_post_fork_block_without_blob_fields.json", - "cancun/eip4844_blobs/excess_blob_gas_fork_transition/invalid_pre_fork_block_with_blob_fields.json", - "cancun/eip4844_blobs/test_invalid_static_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_static_excess_blob_gas_from_zero_on_blobs_above_target.json", - "cancun/eip4844_blobs/test_invalid_zero_excess_blob_gas_in_header.json", - // The same excess-blob-gas tests were renamed into this subdirectory on BAL releases. - "cancun/eip4844_blobs/excess_blob_gas/", - - // These validate block RLP encoding or full blob sidecar rejection, which this runner does not decode/validate. + // These validate block RLP structure or full blob sidecar rejection, which this runner does not decode/validate. "cancun/eip4844_blobs/test_invalid_blob_tx_contract_creation.json", "cancun/eip4844_blobs/test_reject_valid_full_blob_in_block_rlp.json", // The same tests were renamed into subdirectories on BAL releases. "cancun/eip4844_blobs/blob_txs/invalid_blob_tx_contract_creation.json", "cancun/eip4844_blobs/blob_txs_full/reject_valid_full_blob_in_block_rlp.json", - // These are block-level blob count / fork transition tests, not transaction execution tests. - "cancun/eip4844_blobs/test_invalid_tx_blob_count.json", - "osaka/eip7594_peerdas/test_invalid_max_blobs_per_tx.json", - "osaka/eip7594_peerdas/test_max_blobs_per_tx_fork_transition.json", - // The same tests were renamed into subdirectories on BAL releases. - "cancun/eip4844_blobs/blob_txs/invalid_tx_blob_count.json", - "osaka/eip7594_peerdas/max_blob_per_tx/invalid_max_blobs_per_tx.json", - "osaka/eip7594_peerdas/max_blob_per_tx/max_blobs_per_tx_fork_transition.json", - // Create-collision fixtures need storage-aware collision handling. "eip7610_create_collision", "InitCollision.json", @@ -184,15 +152,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "dynamicAccountOverwriteEmpty.json", "dynamicAccountOverwriteEmpty_Paris.json", - // The harness does not track cumulative block gas allowance or validate Osaka block RLP size limits. - "osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_larger_than_block_gas_limit.json", - "osaka/eip7934_block_rlp_limit/test_block_at_rlp_size_limit_boundary.json", - "osaka/eip7934_block_rlp_limit/test_fork_transition_block_rlp_limit.json", - // The same tests were renamed into subdirectories on BAL releases. - "osaka/eip7825_transaction_gas_limit_cap/tx_gas_limit/tx_gas_larger_than_block_gas_limit.json", - "osaka/eip7934_block_rlp_limit/max_block_rlp_size/block_at_rlp_size_limit_boundary.json", - "osaka/eip7934_block_rlp_limit/max_block_rlp_size/fork_transition_block_rlp_limit.json", - // These validate block header fields/roots and belong to consensus-level block validation. "frontier/validation/header/gas_limit_below_minimum.json", // The same test was renamed with a `block_` prefix on glamsterdam-devnet releases. From 1a332a53c692f3a6e5ccf182999326216c05e9a3 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:05:25 +0100 Subject: [PATCH 076/159] refactor(cli): count perf events only over the measured iterations (#924) `evm2 run N` enabled the perf counters before its warmup loop, so `scripts/jit/bench.py` kept a hand-written copy of that loop (`total_iters`, plus its inverse) to divide the counts by the right number of iterations. The CLI now enables the counters after warmup, so they cover exactly the `N` measured iterations, and prints `perf counted N iterations` when a `perf stat --control` wrapper is attached. `perf_counts` parses that line and `bench.py` divides by it. A `--diff` base binary that predates the report prints nothing, and only then does `bench.py` fall back to the old `N + max(N / 10, 10)` formula. On a box, `bench.py --runtime --diff origin/main` on `seaport` and `usdc_proxy` gives 22.53K vs 22.47K and 23.85K vs 23.69K instructions per iteration (base vs head, -0.3% and -0.6%); the head no longer counts its cold warmup iterations. --- crates/cli/src/run.rs | 26 +++++++++++++++++++++++--- scripts/jit/bench.py | 23 ++++++++++++----------- scripts/jit/utils.py | 27 ++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 5941a5707..21550c084 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -370,17 +370,20 @@ impl DotFormat { fn bench(n_iters: u64, name: &str, mut f: impl FnMut() -> eyre::Result) { let warmup = (n_iters / 10).max(10); - perf_control("enable"); for _ in 0..warmup { black_box(f().expect("warmup execution failed")); } + let counting = perf_control("enable"); let t = std::time::Instant::now(); for _ in 0..n_iters { black_box(f().expect("benchmark execution failed")); } let d = t.elapsed(); perf_control("disable"); + if counting { + perf_report(n_iters); + } eprintln!("{name}: {:>9?} ({d:>12?} / {n_iters})", d / n_iters as u32); } @@ -389,8 +392,10 @@ fn bench(n_iters: u64, name: &str, mut f: impl FnMut() -> eyre::Result) { /// /// The variable holds the file descriptors of the control pipe to write and of the pipe that /// acknowledges commands, separated by a comma. -fn perf_control(command: &str) { - let Ok(fds) = std::env::var("EVM2_PERF_CONTROL") else { return }; +/// +/// Returns whether a wrapper received the command. +fn perf_control(command: &str) -> bool { + let Ok(fds) = std::env::var("EVM2_PERF_CONTROL") else { return false }; let (ctl, ack) = fds.split_once(',').expect("invalid EVM2_PERF_CONTROL"); let mut ctl = std::fs::File::options() .write(true) @@ -401,6 +406,21 @@ fn perf_control(command: &str) { ctl.write_all(format!("{command}\n").as_bytes()).expect("failed to write to perf"); // `perf` answers `ack\n` and a NUL. ack.read_exact(&mut [0; 5]).expect("failed to read the perf acknowledgement"); + true +} + +/// Tells the `perf stat` wrapper's owner how many iterations its counters covered, by writing +/// the count to the pipe whose file descriptor `EVM2_PERF_REPORT` holds, if any. +/// +/// The pipe carries nothing else, so the report cannot be forged by text the CLI prints, such as a +/// benchmark name. +fn perf_report(n_iters: u64) { + let Ok(fd) = std::env::var("EVM2_PERF_REPORT") else { return }; + let mut report = std::fs::File::options() + .write(true) + .open(format!("/proc/self/fd/{fd}")) + .expect("failed to open the perf report pipe"); + writeln!(report, "{n_iters}").expect("failed to write the perf report"); } #[derive(Clone, Copy, Debug, ValueEnum)] diff --git a/scripts/jit/bench.py b/scripts/jit/bench.py index 9fea6c9bb..264ecadef 100755 --- a/scripts/jit/bench.py +++ b/scripts/jit/bench.py @@ -1038,12 +1038,13 @@ def measure(sides: list[Side], benches: list[str], runs: int, perf: bool): RUNTIME_PROBE = 1e8 -def total_iters(n: int) -> int: - """`evm2 run N` runs `max(N / 10, 10)` warmup iterations before the timed ones. +def counted_iters(n: int, counted: int | None) -> int: + """Return the iterations a run of `N` counted: what the CLI reports, else what it did before. - Keep in sync with `bench` in `crates/cli/src/run.rs`. + A binary that predates the report enabled the counters before its `max(N / 10, 10)` + warmup iterations. """ - return n + max(n // 10, 10) + return n + max(n // 10, 10) if counted is None else counted def runtime_args(bench: str, n: int) -> list[str]: @@ -1053,13 +1054,13 @@ def runtime_args(bench: str, n: int) -> list[str]: def sample_iters(side: Side, bench: str) -> int: """Return the `N` whose run executes about `RUNTIME_TARGET / RUNTIME_SAMPLES` instructions.""" n = 10 - while ( - instructions := perf_counts(side.binary, runtime_args(bench, n))[0] - ) < RUNTIME_PROBE and n < 10**7: + while True: + instructions, _, counted = perf_counts(side.binary, runtime_args(bench, n)) + if instructions >= RUNTIME_PROBE or n >= 10**7: + break n *= 10 - iters = int(RUNTIME_TARGET / RUNTIME_SAMPLES / (instructions / total_iters(n))) - # Invert `total_iters`. - return max(10, iters - 10 if iters < 110 else int(iters / 1.1)) + per_iter = instructions / counted_iters(n, counted) + return max(10, int(RUNTIME_TARGET / RUNTIME_SAMPLES / per_iter)) def measure_runtime_bench(sides: list[Side], bench: str, skipped: set[str]): @@ -1097,8 +1098,8 @@ def measure_runtime_bench(sides: list[Side], bench: str, skipped: set[str]): side.failures[bench] = last_line(e.stderr) live.remove(side) for side in live: - total = total_iters(iters[side.label]) counts = samples[side.label] + total = counted_iters(iters[side.label], counts[0][2]) side.runtime[bench] = statistics.median(c[0] for c in counts) / total side.runtime_cycles[bench] = min(c[1] for c in counts) / total diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index 92570c8ac..4460c6902 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -203,8 +203,12 @@ class NoBenchCounts(Exception): """The binary ran without counting a benchmark loop: it predates ``EVM2_PERF_CONTROL``.""" -def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: - """Run ``evm2 run`` under ``perf stat``; return ``(instructions, cycles)`` of its bench loop. +def perf_counts(binary: str, args: list[str]) -> tuple[int, int, int | None]: + """Run ``evm2 run`` under ``perf stat``; return ``(instructions, cycles, iterations)``. + + The counts cover its bench loop, and ``iterations`` is the number of iterations the CLI + reports having counted on a pipe of its own (``EVM2_PERF_REPORT``), or ``None`` when the + binary predates that report. The pipe carries nothing a benchmark name can write. The counters start disabled and the CLI enables them around the loop through ``EVM2_PERF_CONTROL``, so the compile cost, which is large and noisy, is not counted. @@ -214,7 +218,8 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: """ ctl_read, ctl_write = os.pipe() ack_read, ack_write = os.pipe() - fds = [ctl_read, ctl_write, ack_read, ack_write] + report_read, report_write = os.pipe() + fds = [ctl_read, ctl_write, ack_read, ack_write, report_read, report_write] try: with tempfile.NamedTemporaryFile("r") as out: subprocess.run( @@ -238,9 +243,21 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: stderr=subprocess.PIPE, text=True, check=True, - env=fixed_stack_env(binary, args, {"EVM2_PERF_CONTROL": f"{ctl_write},{ack_read}"}), + env=fixed_stack_env( + binary, + args, + { + "EVM2_PERF_CONTROL": f"{ctl_write},{ack_read}", + "EVM2_PERF_REPORT": str(report_write), + }, + ), pass_fds=fds, ) + # The run is over, so the report is in the pipe once the write end closes. + os.close(report_write) + fds.remove(report_write) + with os.fdopen(report_read, closefd=False) as report: + counted = report.read().split() stats = {} for line in out: parts = line.strip().split(",") @@ -254,4 +271,4 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: os.close(fd) if not stats.get("instructions:u"): raise NoBenchCounts(binary) - return int(stats["instructions:u"]), int(stats["cycles:u"]) + return int(stats["instructions:u"]), int(stats["cycles:u"]), sum(map(int, counted)) or None From 26bbec8ee030e82860bf3914822f5d5d26b9ec26 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:05:35 +0100 Subject: [PATCH 077/159] fix(jit): derive EXP static gas from one predicate shared with translation (#940) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Whether EXP's dynamic gas is charged with its section was decided in three places kept in step by hand: `SectionsAnalysis` charged it when the output folds or the exponent is at most 2, `foldable_output` special-cased EXP, and `peephole_exp` inlined exponents 0, 1 and 2. Changing the set the peephole inlines would have charged gas twice or not at all, and nothing tied the three together. `Bytecode::exp_static_exponent` is now the single decision: the dynamic gas is static exactly when the exponent is a known constant. `SectionsAnalysis` charges it with the section, `foldable_output` refuses to fold EXP otherwise, and `peephole_exp` lowers every constant exponent without charging gas, calling a new gas-free `Pow` builtin for exponents above 2 instead of `Exp`. A constant exponent therefore never pays at run time, and the lowering of 0, 1 and 2 no longer affects gas accounting. A new compiler test checks the up-front charge around a constant exponent above 2 against `GAS`; the existing `_dyn_const` EXP matrix compares exact gas with the interpreter for constant exponents 0 to 4 and wider ones. A section's static gas is stored in 32 bits and saturated at `u32::MAX`, so a section costing more charged too little. That is only reachable now that a constant EXP exponent is charged with its section, and only under an absurd custom schedule such as `ExpByte = u32::MAX` with a 32-byte exponent. Bytecode with such a section now fails to compile, with a logged reason, so it runs on the interpreter and counts as a failed compilation and an interpreter fallback. Entry guards and section charges are unchanged. A section just under the bound compiles and charges its exact cost; the compiler test and a runtime test that compares against the interpreter cover both sides. ## Benchmarks Generated assembly is unchanged in all 31 benchmarks, so no benchmark compiles a constant exponent above 2 with a dynamic base or a section above `u32::MAX`. Compile instructions were not available on the benchmark machine. Head `8e17b573c2` vs base `origin/main` (`d6c7e21c43`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:-------------------|---------------:|---------:|------------:| | fibonacci-calldata | - | - | +7.3% 🔴 | | push0_proxy | - | - | +8.2% 🔴 | | bswap64 | - | - | +6.3% 🔴 | | **TOTAL** | **-** | **-** | **+1.2% 🔴** | | **GEOMEAN** | **-** | **-** | **+1.5% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------:|----------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | -
-
- | -
-
- | 11.7ms
12.6ms
+7.3% 🔴 | 149.3µs
143.5µs
-3.9% 🟢 | 359.7µs
354.1µs
-1.6% 🟢 | 1.29ms
1.35ms
+5.0% 🔴 | 2.77ms
2.88ms
+3.8% 🔴 | | factorial | -
-
- | -
-
- | 11.7ms
11.4ms
-2.4% 🟢 | 132.9µs
147.7µs
+11.1% 🔴 | 319.2µs
316.3µs
-0.9% 🟢 | 1.13ms
1.16ms
+3.0% 🔴 | 3.00ms
3.02ms
+0.4% 🔴 | | counter | -
-
- | -
-
- | 9.03ms
9.34ms
+3.3% 🔴 | 259.2µs
271.7µs
+4.8% 🔴 | 419.0µs
428.1µs
+2.2% 🔴 | 1.31ms
1.32ms
+1.3% 🔴 | 3.49ms
3.52ms
+0.7% 🔴 | | snailtracer | -
-
- | -
-
- | 375.4ms
388.5ms
+3.5% 🔴 | 29.6ms
30.2ms
+2.1% 🔴 | 6.53ms
6.48ms
-0.8% 🟢 | 65.2ms
65.5ms
+0.6% 🔴 | 296.8ms
309.7ms
+4.4% 🔴 | | weth | -
-
- | -
-
- | 48.3ms
48.1ms
-0.5% 🟢 | 1.79ms
1.84ms
+2.8% 🔴 | 1.83ms
1.86ms
+1.2% 🔴 | 10.8ms
10.8ms
= | 31.9ms
32.1ms
+0.5% 🔴 | | hash_10k | -
-
- | -
-
- | 11.1ms
11.4ms
+3.1% 🔴 | 244.7µs
239.7µs
-2.1% 🟢 | 462.1µs
430.3µs
-6.9% 🟢 | 1.94ms
1.86ms
-4.2% 🟢 | 5.55ms
5.06ms
-8.9% 🟢 | | erc20_transfer | -
-
- | -
-
- | 49.4ms
49.2ms
-0.4% 🟢 | 1.92ms
1.95ms
+1.4% 🔴 | 1.86ms
1.84ms
-1.0% 🟢 | 11.4ms
11.6ms
+1.2% 🔴 | 32.6ms
33.0ms
+1.0% 🔴 | | push0_proxy | -
-
- | -
-
- | 10.2ms
11.0ms
+8.2% 🔴 | 121.6µs
130.4µs
+7.2% 🔴 | 315.8µs
312.7µs
-1.0% 🟢 | 796.9µs
848.1µs
+6.4% 🔴 | 2.08ms
2.14ms
+2.7% 🔴 | | usdc_proxy | -
-
- | -
-
- | 24.1ms
25.0ms
+3.7% 🔴 | 1.13ms
1.14ms
+1.0% 🔴 | 1.11ms
1.11ms
-0.5% 🟢 | 5.65ms
5.68ms
+0.5% 🔴 | 14.2ms
14.2ms
+0.3% 🔴 | | fiat_token | -
-
- | -
-
- | 301.5ms
302.9ms
+0.5% 🔴 | 11.5ms
11.3ms
-1.9% 🟢 | 9.85ms
9.68ms
-1.7% 🟢 | 67.4ms
66.6ms
-1.2% 🟢 | 237.1ms
236.2ms
-0.4% 🟢 | | uniswap_v2_pair | -
-
- | -
-
- | 163.6ms
164.0ms
+0.2% 🔴 | 6.20ms
6.29ms
+1.5% 🔴 | 5.75ms
5.48ms
-4.7% 🟢 | 33.1ms
33.0ms
-0.5% 🟢 | 125.4ms
126.5ms
+0.9% 🔴 | | univ2_router | -
-
- | -
-
- | 337.9ms
341.0ms
+0.9% 🔴 | 13.2ms
12.9ms
-1.6% 🟢 | 10.1ms
9.99ms
-1.0% 🟢 | 67.6ms
68.2ms
+0.9% 🔴 | 275.2ms
278.8ms
+1.3% 🔴 | | seaport | -
-
- | -
-
- | 793.4ms
807.0ms
+1.7% 🔴 | 23.1ms
22.4ms
-3.0% 🟢 | 17.3ms
16.9ms
-2.8% 🟢 | 172.9ms
171.1ms
-1.0% 🟢 | 679.3ms
683.3ms
+0.6% 🔴 | | airdrop | -
-
- | -
-
- | 113.3ms
114.6ms
+1.1% 🔴 | 3.98ms
3.93ms
-1.3% 🟢 | 3.85ms
3.67ms
-4.8% 🟢 | 28.7ms
28.4ms
-1.1% 🟢 | 81.4ms
81.1ms
-0.4% 🟢 | | bswap64 | -
-
- | -
-
- | 10.3ms
11.0ms
+6.3% 🔴 | 402.9µs
405.0µs
+0.5% 🔴 | 547.8µs
517.6µs
-5.5% 🟢 | 1.62ms
1.53ms
-5.8% 🟢 | 3.82ms
3.66ms
-4.2% 🟢 | | bswap64_opt | -
-
- | -
-
- | 10.1ms
10.1ms
+0.2% 🔴 | 345.7µs
333.6µs
-3.5% 🟢 | 478.6µs
457.1µs
-4.5% 🟢 | 1.60ms
1.54ms
-3.9% 🟢 | 4.06ms
3.99ms
-1.7% 🟢 | | eip4788 | -
-
- | -
-
- | 8.44ms
8.51ms
+0.8% 🔴 | 176.5µs
179.9µs
+1.9% 🔴 | 364.5µs
358.7µs
-1.6% 🟢 | 1.02ms
1.02ms
= | 3.36ms
3.36ms
-0.2% 🟢 | | eip2935 | -
-
- | -
-
- | 11.5ms
11.5ms
-0.3% 🟢 | 145.6µs
148.8µs
+2.2% 🔴 | 341.9µs
333.0µs
-2.6% 🟢 | 911.6µs
916.4µs
+0.5% 🔴 | 2.88ms
2.86ms
-0.4% 🟢 | | burntpix | -
-
- | -
-
- | 496.6ms
498.7ms
+0.4% 🔴 | 10.6ms
10.4ms
-2.2% 🟢 | 1.55ms
1.51ms
-2.4% 🟢 | 9.93ms
10.2ms
+2.6% 🔴 | 27.6ms
27.7ms
+0.2% 🔴 | | curve_stableswap | -
-
- | -
-
- | 93.0ms
93.8ms
+0.9% 🔴 | 2.78ms
2.97ms
+7.1% 🔴 | 2.38ms
2.39ms
+0.3% 🔴 | 20.8ms
20.5ms
-1.2% 🟢 | 47.2ms
46.7ms
-1.0% 🟢 | | onchain_lm_v2 | -
-
- | -
-
- | 268.9ms
270.9ms
+0.7% 🔴 | 8.78ms
8.82ms
+0.5% 🔴 | 7.12ms
6.91ms
-3.1% 🟢 | 46.0ms
45.6ms
-0.9% 🟢 | 184.1ms
186.3ms
+1.2% 🔴 | | usdt | -
-
- | -
-
- | 107.3ms
106.3ms
-0.9% 🟢 | 5.33ms
5.27ms
-1.1% 🟢 | 4.82ms
4.68ms
-2.8% 🟢 | 24.9ms
24.7ms
-0.9% 🟢 | 76.8ms
76.8ms
= | | universal_router | -
-
- | -
-
- | 888.1ms
897.4ms
+1.0% 🔴 | 11.5ms
11.7ms
+2.1% 🔴 | 10.5ms
10.2ms
-2.7% 🟢 | 80.3ms
79.1ms
-1.4% 🟢 | 311.7ms
311.5ms
-0.1% 🟢 | | aggregation_router_v6 | -
-
- | -
-
- | 2.661s
2.694s
+1.2% 🔴 | 18.4ms
18.4ms
+0.1% 🔴 | 16.4ms
16.2ms
-0.9% 🟢 | 135.9ms
134.8ms
-0.8% 🟢 | 509.7ms
517.4ms
+1.5% 🔴 | | entry_point_v0_7 | -
-
- | -
-
- | 1.372s
1.387s
+1.1% 🔴 | 8.78ms
9.16ms
+4.3% 🔴 | 8.88ms
8.84ms
-0.5% 🟢 | 62.2ms
61.4ms
-1.2% 🟢 | 262.1ms
266.2ms
+1.6% 🔴 | | uniswap_v3_pool | -
-
- | -
-
- | 482.5ms
484.1ms
+0.3% 🔴 | 18.8ms
18.5ms
-1.4% 🟢 | 16.0ms
15.7ms
-2.1% 🟢 | 109.5ms
108.9ms
-0.6% 🟢 | 398.5ms
404.5ms
+1.5% 🔴 | | uniswap_v4_pool_manager | -
-
- | -
-
- | 512.9ms
520.6ms
+1.5% 🔴 | 13.2ms
13.0ms
-1.9% 🟢 | 12.9ms
12.5ms
-3.2% 🟢 | 105.8ms
105.6ms
-0.2% 🟢 | 389.8ms
394.6ms
+1.2% 🔴 | | curve_3pool | -
-
- | -
-
- | 482.8ms
487.6ms
+1.0% 🔴 | 18.6ms
17.9ms
-3.7% 🟢 | 13.1ms
13.2ms
+0.7% 🔴 | 85.0ms
84.3ms
-0.8% 🟢 | 430.0ms
432.8ms
+0.7% 🔴 | | curve_tricrypto2 | -
-
- | -
-
- | 676.6ms
679.8ms
+0.5% 🔴 | 16.3ms
16.8ms
+3.1% 🔴 | 15.4ms
14.8ms
-3.7% 🟢 | 94.3ms
92.9ms
-1.4% 🟢 | 419.1ms
416.3ms
-0.7% 🟢 | | balancer_vault | -
-
- | -
-
- | 1.514s
1.540s
+1.7% 🔴 | 24.1ms
23.6ms
-2.2% 🟢 | 18.1ms
17.7ms
-2.3% 🟢 | 202.1ms
198.8ms
-1.6% 🟢 | 644.5ms
656.7ms
+1.9% 🔴 | | succinct_staking | -
-
- | -
-
- | 820.6ms
826.1ms
+0.7% 🔴 | 16.6ms
16.3ms
-1.6% 🟢 | 10.8ms
11.7ms
+7.4% 🔴 | 86.6ms
87.3ms
+0.8% 🔴 | 312.6ms
314.8ms
+0.7% 🔴 | | **TOTAL** | **-
-
-** | **-
-
-** | **12.677s
12.823s
+1.2% 🔴** | **268.1ms
266.8ms
-0.5% 🟢** | **199.7ms
196.7ms
-1.5% 🟢** | **1.538s
1.526s
-0.7% 🟢** | **5.819s
5.878s
+1.0% 🔴** |
--- crates/cli/benches/evm/jit.rs | 5 +- crates/cli/src/run.rs | 5 +- crates/jit/builtins/src/ir.rs | 2 + crates/jit/builtins/src/lib.rs | 15 ++- crates/jit/codegen/src/bytecode/mod.rs | 35 ++++++- .../codegen/src/bytecode/passes/sections.rs | 15 +-- .../src/compiler/translate/peephole.rs | 17 ++-- .../codegen/src/tests/large_section_gas.rs | 92 +++++++++++++++++++ crates/jit/codegen/src/tests/mod.rs | 9 ++ crates/jit/context/src/lib.rs | 14 ++- crates/jit/runtime/src/evm2_evm.rs | 45 ++++++++- 11 files changed, 223 insertions(+), 31 deletions(-) create mode 100644 crates/jit/codegen/src/tests/large_section_gas.rs diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index 40c10fa28..c737dab00 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -9,7 +9,7 @@ use evm2::{ interpreter::{InstrStop, Interpreter}, }; use evm2_cli::evm_bench::BenchCase; -use evm2_jit_context::EvmCompilerFn; +use evm2_jit_context::{EvmCompilerFn, can_run_compiled}; use evm2_jit_llvm::EvmLlvmBackend; use evm2_jit_runtime::{EvmCompiler, OptimizationLevel}; use std::{borrow::Cow, collections::HashMap, sync::Arc}; @@ -209,8 +209,7 @@ impl InterpreterRunner for FixedJitRunner { interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, host: &mut Evm<'host, BaseEvmTypes>, ) -> Option { - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if interpreter.gas().remaining() > i64::MAX as u64 { + if !can_run_compiled(interpreter) { return None; } let func = *self.functions.get(&interpreter.original_bytecode_hash())?; diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 21550c084..ecbab9261 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -11,7 +11,7 @@ use evm2::{ }; use evm2_cli::evm_bench; use evm2_eest::{StateTestPost, StateTestSuite, StateTestUnit}; -use evm2_jit_context::EvmCompilerFn; +use evm2_jit_context::{EvmCompilerFn, can_run_compiled}; use evm2_jit_llvm::EvmLlvmBackend; use evm2_jit_runtime::{ EvmCompiler, Linker, OptimizationLevel, eyre, parse_asm, shared_library_path, @@ -687,8 +687,7 @@ impl InterpreterRunner for FixedJitRunner { interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, host: &mut Evm<'host, BaseEvmTypes>, ) -> Option { - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if interpreter.gas().remaining() > i64::MAX as u64 { + if !can_run_compiled(interpreter) { return None; } let func = *self.functions.get(&interpreter.original_bytecode_hash())?; diff --git a/crates/jit/builtins/src/ir.rs b/crates/jit/builtins/src/ir.rs index ac9fcbe04..a7650d23f 100644 --- a/crates/jit/builtins/src/ir.rs +++ b/crates/jit/builtins/src/ir.rs @@ -225,6 +225,7 @@ macro_rules! builtins { const PANIC: u8 = _0_0; const ASSERTSPECID: u8 = _0_0; + const POW: u8 = EXP; const KECCAK256CC: u8 = _0_1; const CALLDATALOADC: u8 = _0_1; @@ -314,6 +315,7 @@ builtins! { AddMod = __revmc_builtin_addmod(@[sp] ptr) None, MulMod = __revmc_builtin_mulmod(@[sp] ptr) None, Exp = __revmc_builtin_exp(@[ecx] ptr, @[sp] ptr) None, + Pow = __revmc_builtin_pow(@[sp] ptr) None, Keccak256 = __revmc_builtin_keccak256(@[ecx] ptr, @[sp] ptr) None, Keccak256CC = __revmc_builtin_keccak256_cc(@[ecx] ptr, @[sp] ptr, usize, usize) None, Balance = __revmc_builtin_balance(@[ecx] ptr, @[sp] ptr) None, diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 4fcc2a367..cf1ba6d3c 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -142,13 +142,19 @@ pub unsafe fn __revmc_builtin_exp( ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2], ) -> BuiltinResult { - let rev![base, exponent_ptr] = sp; - let exponent = exponent_ptr.to_u256(); - ecx.gas.spend(ecx.gas_params().exp_cost(exponent))?; - *exponent_ptr = base.to_u256().pow(exponent).into(); + let rev![_, exponent] = &*sp; + ecx.gas.spend(ecx.gas_params().exp_cost(exponent.to_u256()))?; + unsafe { __revmc_builtin_pow::(sp) }; Ok(()) } +/// `EXP` without gas, for compiled code that already charged the dynamic gas of a constant +/// exponent in its section. +#[inline] +pub unsafe fn __revmc_builtin_pow(rev![base, exponent]: &mut [EvmWord; 2]) { + *exponent = base.to_u256().pow(exponent.to_u256()).into(); +} + #[inline] pub unsafe fn __revmc_builtin_keccak256( ecx: &mut EvmContext<'_, '_, '_, T>, @@ -1078,6 +1084,7 @@ mod abi { __revmc_builtin_mulmod(sp: &mut [EvmWord; 3]); __revmc_builtin_assert_spec_id(ecx: &EvmContext<'_, '_, '_, T>, expected: u32); __revmc_builtin_exp(ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2]) -> BuiltinResult; + __revmc_builtin_pow(sp: &mut [EvmWord; 2]); __revmc_builtin_keccak256(ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2]) -> BuiltinResult; __revmc_builtin_keccak256_cc(ecx: &mut EvmContext<'_, '_, '_, T>, out: &mut EvmWord, offset: u64, len: u64) -> BuiltinResult; __revmc_builtin_balance(ecx: &mut EvmContext<'_, '_, '_, T>, address: &mut EvmWord) -> BuiltinResult; diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index 55d2c16d5..bd3cc75e5 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -463,7 +463,7 @@ impl<'a> Bytecode<'a> { self.dead_store_elim(); } - self.construct_sections(); + self.construct_sections()?; self.construct_memory_sections(); debug!( @@ -595,7 +595,7 @@ impl<'a> Bytecode<'a> { /// Constructs the sections in the bytecode. #[instrument(name = "sections", level = "debug", skip_all)] #[inline(never)] - fn construct_sections(&mut self) { + fn construct_sections(&mut self) -> Result<()> { let mut analysis = SectionsAnalysis::default(); for inst in self.insts.indices() { if !self.inst(inst).is_dead_code() { @@ -603,6 +603,20 @@ impl<'a> Bytecode<'a> { } } analysis.finish(self); + + if let Some((inst, _)) = self + .insts + .iter_enumerated() + .find(|(_, data)| data.gas_section.gas_cost == GasSection::SATURATED) + { + bail!( + "the gas section at instruction {} costs at least {} gas, which compiled code \ + cannot charge ahead of its instructions", + inst.index(), + GasSection::SATURATED, + ); + } + Ok(()) } /// Constructs the memory sections in the bytecode. @@ -724,15 +738,26 @@ impl<'a> Bytecode<'a> { /// Returns the known output of the given instruction if translation can store it instead of /// executing the instruction. /// - /// EXP's dynamic gas is only charged statically when its exponent is known, and a constant - /// output joined across call contexts does not imply a constant exponent. + /// A constant output joined across call contexts does not imply that EXP's exponent is + /// constant, so EXP's dynamic gas must also be static. pub(crate) fn foldable_output(&self, inst: Inst) -> Option { - if self.insts[inst].opcode == op::EXP && self.const_operand(inst, 1).is_none() { + if self.insts[inst].opcode == op::EXP && self.exp_static_exponent(inst).is_none() { return None; } self.const_output(inst) } + /// Returns the exponent of the EXP instruction if its dynamic gas is charged statically with + /// the section. + /// + /// This holds exactly when the exponent is a known constant. Translation then never charges + /// the dynamic gas itself, whether it folds, inlines or calls the gas-free builtin; otherwise + /// it charges the gas at run time. + pub(crate) fn exp_static_exponent(&self, inst: Inst) -> Option { + debug_assert_eq!(self.insts[inst].opcode, op::EXP); + self.const_operand(inst, 1) + } + /// Returns the first constant EVM memory access `(offset, len)` for the given instruction. #[allow(dead_code)] pub(crate) fn const_memory_access(&self, inst: Inst) -> Option<(Option, Option)> { diff --git a/crates/jit/codegen/src/bytecode/passes/sections.rs b/crates/jit/codegen/src/bytecode/passes/sections.rs index bcd925672..e7c19d497 100644 --- a/crates/jit/codegen/src/bytecode/passes/sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/sections.rs @@ -1,5 +1,4 @@ use crate::bytecode::{Bytecode, Inst, InstFlags}; -use alloy_primitives::U256; use core::{fmt, ops::Range}; use evm2::interpreter::op; use smallvec::SmallVec; @@ -24,6 +23,13 @@ impl fmt::Debug for GasSection { } impl GasSection { + /// The cost recorded for a section that costs at least this much. + /// + /// A section's cost is stored in 32 bits, so a larger one saturates here. Section gas is + /// charged ahead of the instructions, so a saturated cost would charge too little, and + /// [`Bytecode::analyze`] rejects the bytecode instead. + pub(crate) const SATURATED: u32 = u32::MAX; + /// Returns `true` if the section is empty. #[inline] pub(crate) fn is_empty(self) -> bool { @@ -235,12 +241,9 @@ impl SectionsAnalysis { self.gas.process_gas(inst); } - // When EXP's builtin will be skipped — either because both operands are known - // (foldable_output) or because the exponent is known and handled by a peephole (≤ 2) — - // fold the dynamic gas into the section statically. + // The exponent is known, so translation does not charge EXP's dynamic gas. if data.opcode == op::EXP - && let Some(exponent) = bytecode.const_operand(inst, 1) - && (bytecode.foldable_output(inst).is_some() || exponent <= U256::from(2)) + && let Some(exponent) = bytecode.exp_static_exponent(inst) { self.gas.process_extra(bytecode.gas_params.exp_cost(exponent)); } diff --git a/crates/jit/codegen/src/compiler/translate/peephole.rs b/crates/jit/codegen/src/compiler/translate/peephole.rs index 9aadd3656..ea28cee54 100644 --- a/crates/jit/codegen/src/compiler/translate/peephole.rs +++ b/crates/jit/codegen/src/compiler/translate/peephole.rs @@ -208,12 +208,13 @@ impl<'a, B: Backend> FunctionCx<'a, B> { /// EXP base, exp => base ** exp. /// - /// Dynamic gas is folded into the section gas cost by `SectionsAnalysis` when the - /// exponent is a compile-time constant, so the section gas check already covers it. - /// For constant-base cases with a dynamic exponent, charge the dynamic gas and compute the - /// result inline. + /// A constant exponent (`Bytecode::exp_static_exponent`) has its dynamic gas folded into the + /// section gas cost by `SectionsAnalysis`, so it is lowered without charging gas. For + /// constant-base cases with a dynamic exponent, charge the dynamic gas and compute the result + /// inline. fn peephole_exp(&mut self) -> bool { - let [base, exponent] = self.const_operands(); + let [base, _] = self.const_operands(); + let exponent = self.bytecode.exp_static_exponent(self.current_inst.unwrap()); match exponent { // x ** 0 => 1. Some(U256::ZERO) => self.fold_const(1), @@ -228,7 +229,11 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let r = self.bcx.imul(a, a); self.push(r); } - Some(_) => return false, + // x ** e => x ** e, with the gas already paid. + Some(_) => { + let sp = self.sp_after_inputs(); + let _ = self.call_builtin(Builtin::Pow, &[sp]); + } None => {} } if exponent.is_some() { diff --git a/crates/jit/codegen/src/tests/large_section_gas.rs b/crates/jit/codegen/src/tests/large_section_gas.rs new file mode 100644 index 000000000..0f4b6c12b --- /dev/null +++ b/crates/jit/codegen/src/tests/large_section_gas.rs @@ -0,0 +1,92 @@ +//! Sections whose static gas is close to or past what compiled code can charge ahead. + +use super::{DEF_SPEC, with_evm_context}; +use crate::{Backend, EvmCompiler, parse_asm}; +use alloy_primitives::U256; +use evm2::{ + Version, + interpreter::{Gas, InstrStop}, + version::GasId, +}; + +/// A 32-byte exponent. +const MAX: &str = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; + +/// The largest `ExpByte` for which a section of `PUSH32` (3), `CALLVALUE` (2) and `EXP` (10 plus +/// 32 bytes of exponent) still costs less than `u32::MAX`. +const FITTING_EXP_BYTE: u32 = (1 << 27) - 1; +/// The gas of that section. +const FITTING_GAS: u64 = 3 + 2 + 10 + FITTING_EXP_BYTE as u64 * 32; + +/// Compiles `src` with the given `ExpByte`, returning the compile error if any. +fn compile( + compiler: &mut EvmCompiler, + code: &[u8], + exp_byte: u32, +) -> Result { + unsafe { compiler.clear() }.unwrap(); + let mut gas_params = Version::base(DEF_SPEC).gas_params; + gas_params.set(GasId::ExpByte, exp_byte); + compiler.set_gas_params(gas_params); + compiler.set_inspect_stack(true); + unsafe { compiler.jit("large_section_gas", code, DEF_SPEC) }.map_err(|e| e.to_string()) +} + +/// Runs `src` with a gas limit of `limit`, returning the stop reason, the final stack and the gas +/// spent. +fn run( + compiler: &mut EvmCompiler, + src: &str, + limit: u64, +) -> (InstrStop, Vec, u64) { + let code = parse_asm(src).unwrap(); + let f = compile(compiler, &code, FITTING_EXP_BYTE).unwrap(); + with_evm_context(&code, DEF_SPEC, |ecx, stack, stack_len| { + ecx.gas = Gas::new(limit); + let r = unsafe { f.call(ecx, stack, stack_len) }; + let stack = + unsafe { stack.as_slice(*stack_len) }.iter().map(|word| word.to_u256()).collect(); + (r, stack, ecx.gas.spent()) + }) +} + +// A section costing more than `u32::MAX` is not compiled, instead of being charged too little. +matrix_tests!( + large_section_gas_rejected = |compiler| { + let code = parse_asm(&format!("PUSH32 {MAX} CALLVALUE EXP STOP")).unwrap(); + let err = compile(compiler, &code, u32::MAX).err().unwrap(); + assert_eq!( + err, + "the gas section at instruction 0 costs at least 4294967295 gas, which compiled code \ + cannot charge ahead of its instructions", + ); + } +); + +// A section just under the bound compiles and charges its exact cost, with a constant exponent +// above 2. +matrix_tests!( + large_section_gas_const_exponent = |compiler| { + let src = format!("PUSH32 {MAX} CALLVALUE EXP STOP"); + let (r, _, spent) = run(compiler, &src, 2 * FITTING_GAS); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS)); + // The exact cost is enough, and one less is not. + let (r, _, spent) = run(compiler, &src, FITTING_GAS); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS)); + let (r, _, _) = run(compiler, &src, FITTING_GAS - 1); + assert_eq!(r, InstrStop::OutOfGas); + } +); + +// The section head charges the whole cost, so the `GAS` after it owes the rest back to the +// remaining gas it reads. +matrix_tests!( + large_section_gas_owed = |compiler| { + let limit = 2 * FITTING_GAS; + let (r, stack, spent) = + run(compiler, &format!("GAS PUSH32 {MAX} CALLVALUE EXP GAS STOP"), limit); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS + 4)); + assert_eq!(stack[0], U256::from(limit - 2)); + assert_eq!(stack[2], U256::from(limit - FITTING_GAS - 4)); + } +); diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 9a7501b31..37c431a1d 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -54,6 +54,8 @@ mod fibonacci; mod entry_stack; +mod large_section_gas; + mod runner; pub use runner::*; @@ -1843,6 +1845,13 @@ tests! { expected_stack: &[DEF_GAS_LIMIT_U256 - 2_U256, DEF_GAS_LIMIT_U256 - 122_U256], expected_gas: 2 + 3 + 3 + 110 + 2 + 2, }), + // A constant exponent above 2 lowers to a gas-free builtin: its dynamic gas (10 + 50 * 2) + // is charged up front with the section, once. + gas_around_const_exponent_pow(@raw { + bytecode: &[op::GAS, op::PUSH2, 1, 0, op::ADDRESS, op::EXP, op::POP, op::GAS], + expected_stack: &[DEF_GAS_LIMIT_U256 - 2_U256, DEF_GAS_LIMIT_U256 - 121_U256], + expected_gas: 2 + 3 + 2 + 110 + 2 + 2, + }), // `SLOAD` ends the gas section, so only its base gas is owed by the first `GAS`. gas_around_sload(@raw { bytecode: &[op::GAS, op::PUSH1, 69, op::SLOAD, op::GAS], diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index 4c27cc36b..c279665de 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -26,6 +26,16 @@ mod arch; use arch::evm2_jit_entry; pub use arch::evm2_jit_exit; +/// Returns `true` if compiled code can run the interpreter's frame. +/// +/// Compiled code keeps the remaining gas in a signed counter, so a frame with more than +/// `i64::MAX` gas must run on the interpreter. Check this before calling +/// [`EvmCompilerFn::call_with_interpreter`] or [`EvmCompilerFn::call`]. +#[inline] +pub const fn can_run_compiled(interpreter: &Interpreter<'_, '_, T>) -> bool { + interpreter.gas().remaining() <= i64::MAX as u64 +} + /// The EVM bytecode compiler runtime context. /// /// This is a simple wrapper around the interpreter's resources, allowing the compiled function to @@ -342,7 +352,7 @@ impl EvmCompilerFn { /// # Safety /// /// The caller must ensure that the function is safe to call for this interpreter state, and - /// that the remaining gas is at most `i64::MAX`. + /// that [`can_run_compiled`] holds. #[inline] pub unsafe fn call_with_interpreter<'ctx, 'frame, 'host, T: EvmTypesHost>( self, @@ -370,7 +380,7 @@ impl EvmCompilerFn { /// # Safety /// /// The caller must ensure that the arguments are valid, that the function is safe to call, and - /// that the remaining gas is at most `i64::MAX`. + /// that [`can_run_compiled`] holds. #[inline] pub unsafe fn call( self, diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index bb8ea2935..e62b0fa95 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -5,6 +5,7 @@ use evm2::{ EvmTypesHost, ExecutionConfig, InterpreterRunner, interpreter::{Host, InstrStop, Interpreter}, }; +use evm2_jit_context::can_run_compiled; /// External interpreter runner backed by [`JitBackend`]. #[derive(Clone, Debug)] @@ -80,11 +81,10 @@ fn run_supported<'frame, 'host, T: EvmTypesHost>( host: &mut T::Host<'host>, ) -> Option { // Disabled runners must not hash code merely to discover that lookup is unavailable. - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. if !backend.enabled() { return None; } - if interpreter.gas().remaining() > i64::MAX as u64 { + if !can_run_compiled(interpreter) { backend.count_interpreter_fallback(); return None; } @@ -776,6 +776,47 @@ mod tests { } } + #[test] + #[cfg(feature = "llvm")] + fn section_gas_too_large_to_compile_falls_back_to_interpreter() { + let spec = SpecId::CANCUN; + let mut version = *Version::base(spec); + version.gas_params.set(GasId::ExpByte, u32::MAX); + let backend = JitBackend::new(RuntimeConfig { + enabled: true, + blocking: true, + gas_params: Some(version.gas_params), + ..RuntimeConfig::default() + }) + .unwrap(); + let mut host = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnv::::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + // `0 ** (2^256 - 1)`: the exponent's 32 bytes cost more than `u32::MAX` gas in one section. + let mut code = vec![op::PUSH32]; + code.extend([0xff; 32]); + code.extend([op::PUSH0, op::EXP, op::STOP]); + let expected_gas = 3 + 2 + 10 + 32 * u32::MAX as u64; + let message = MessageExt { + gas_limit: 2 * expected_gas, + code: Bytecode::new_legacy(Bytes::from(code)), + ..MessageExt::default() + }; + let mut interpreter = Interpreter::::new(&message); + + assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), None); + let stats = backend.stats(); + assert_eq!((stats.compilations_failed, stats.interpreter_fallbacks), (1, 1)); + + assert_eq!(interpreter.run(&mut host).unwrap(), InstrStop::Stop); + assert_eq!(interpreter.gas().spent(), expected_gas); + } + #[test] #[cfg(feature = "llvm")] fn compiled_sstore_stipend_uses_active_features() { From 31ffe60c4c6fa6c0d977a57cc665309535b04714 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:05:44 +0100 Subject: [PATCH 078/159] refactor(inspectors): record SLOAD and SSTORE storage once at step (#907) The tracing inspector recorded the storage an SLOAD or SSTORE touches in two parallel ways, both patched after the opcode ran: `storage_change` matched the last journal entry at step end, and `storage_slot` held a zero placeholder at step that step end filled in. The journal match saw nothing for a warm SLOAD or an unchanged SSTORE, so the erc7562 builder dropped the first read of any slot an access list or an earlier frame had already warmed. geth records that read, because its tracer reads the slot with `GetState` in `OnOpcode`, before the opcode runs. This reads the slot once at `step`, before the opcode runs, and keeps it in the single `StorageChange` record. For an SLOAD the record holds the key from the stack and the slot's current value; for an SSTORE it holds the key, the value written and the previous value, read with `storage_slot_untracked`, which does not load or warm the slot. The `storage_slot` field, the `last_journal_len` bookkeeping, the journal matching and the SLOAD fill-in at step end are gone. The struct-log builder, the erc7562 builder and the trace writer read the one field, and all three skip steps that halted, since a step that failed before running did not access the slot. A step that halts on the trace byte budget never runs, so it records no access and its parity `vmTrace` entry reports no store. A database error on the step read leaves the record empty; an opcode that reads the slot fails on it too. The struct-log storage output and the writer's storage-change output are unchanged. The erc7562 `accessedSlots.reads` now includes first reads of warm slots, covered by a new test that reads a slot in a frame and again in a delegatecall child. The geth parity harness reports no new differences against the stored geth output. --- crates/inspectors/src/tracing/builder/geth.rs | 25 +++-- .../src/tracing/builder/geth/steps_tests.rs | 19 +++- crates/inspectors/src/tracing/limits.rs | 7 +- crates/inspectors/src/tracing/mod.rs | 91 +++++-------------- crates/inspectors/src/tracing/types.rs | 21 ++--- crates/inspectors/src/tracing/writer.rs | 19 ++-- crates/inspectors/tests/it/geth.rs | 52 +++++++++++ crates/inspectors/tests/it/inputs.rs | 36 +++++++- 8 files changed, 159 insertions(+), 111 deletions(-) diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 0c5747601..bb5636a5a 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -1,7 +1,7 @@ //! Geth trace builder use crate::tracing::{ tx_state::TxState, - types::{CallKind, CallTraceNode, CallTraceStepStackItem}, + types::{CallKind, CallTraceNode, CallTraceStepStackItem, StorageChange}, utils::load_account_code, }; use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, format, vec, vec::Vec}; @@ -183,7 +183,8 @@ impl<'a> GethTraceBuilder<'a> { // SLOADs and SSTOREs, unless the opcode fails before it runs. if opts.is_storage_enabled() && !step.is_error() - && let Some((key, value)) = step.storage_slot.as_deref().copied() + && let Some(StorageChange { key, value, .. }) = + step.storage_change.as_deref().copied() { let contract_storage = storage.entry(trace_node.execution_address()).or_default(); contract_storage.insert(key.into(), value.into()); @@ -597,19 +598,15 @@ impl<'a> GethTraceBuilder<'a> { // Accessed storage slots match op { op::SLOAD => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) + // geth records the first read of each slot, unless the opcode fails before + // it runs. + if !step.is_error() + && let Some(change) = &step.storage_change + && let slot = B256::from(change.key) + && !accessed_slots.reads.contains_key(&slot) + && !accessed_slots.writes.contains_key(&slot) { - let slot: B256 = (*slot).into(); - let already_read = accessed_slots.reads.contains_key(&slot); - let already_written = accessed_slots.writes.contains_key(&slot); - if !already_read - && !already_written - && let Some(change) = &step.storage_change - { - let value: B256 = change.value.into(); - accessed_slots.reads.entry(slot).or_default().push(value); - } + accessed_slots.reads.entry(slot).or_default().push(change.value.into()); } } op::SSTORE => { diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index af548c580..635df19e9 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -26,11 +26,10 @@ fn test_step(pc: usize, op: u8) -> CallTraceStep { alloc::boxed::Box::new(StorageChange { key: U256::ZERO, value: U256::from(pc), - had_value: None, + had_value: Some(U256::ZERO), reason: StorageChangeReason::SSTORE, }) }), - storage_slot: (op == op::SSTORE).then(|| Box::new((U256::ZERO, U256::from(pc)))), status: (op == op::REVERT).then_some(InstrStop::Revert), immediate_bytes: None, decoded: None, @@ -220,7 +219,12 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { } nodes[0].trace.steps.last_mut().unwrap().status = Some(InstrStop::OutOfGas); // The SLOAD reads the value its delegatecall child wrote. - nodes[2].trace.steps[2].storage_slot = Some(Box::new((U256::ZERO, U256::from(30)))); + nodes[2].trace.steps[2].storage_change = Some(Box::new(StorageChange { + key: U256::ZERO, + value: U256::from(30), + had_value: None, + reason: StorageChangeReason::SLOAD, + })); let builder = GethTraceBuilder::new(nodes); let expected = [ (0, 1), @@ -318,10 +322,15 @@ fn storage_snapshots_accumulate_and_skip_failed_steps() { let mut failed = test_step(4, op::SSTORE); failed.status = Some(InstrStop::OutOfGas); let mut sload = test_step(1, op::SLOAD); - sload.storage_slot = Some(Box::new((U256::ZERO, U256::from(1)))); + sload.storage_change = Some(Box::new(StorageChange { + key: U256::ZERO, + value: U256::from(1), + had_value: None, + reason: StorageChangeReason::SLOAD, + })); // A step that is not an SLOAD or SSTORE has no slot. let mut add = test_step(2, op::ADD); - add.storage_slot = None; + add.storage_change = None; let builder = GethTraceBuilder::new(vec![CallTraceNode { trace: CallTrace { steps: vec![test_step(0, op::PUSH0), sload, add, failed], diff --git a/crates/inspectors/src/tracing/limits.rs b/crates/inspectors/src/tracing/limits.rs index ce1219c64..aab797285 100644 --- a/crates/inspectors/src/tracing/limits.rs +++ b/crates/inspectors/src/tracing/limits.rs @@ -69,8 +69,13 @@ impl TraceBudget { self.exceeded } + /// Returns whether the budget was exceeded and the limits halt execution. + pub(crate) fn halts(&self) -> bool { + self.exceeded && self.limits.behavior == TraceLimitBehavior::Halt + } + pub(crate) fn halt(&self, interp: &mut Interpreter<'_, '_, T>, in_step: bool) { - if self.exceeded && self.limits.behavior == TraceLimitBehavior::Halt { + if self.halts() { let stop = interp.fail(ExecutionError::Fatal("trace recorded byte limit exceeded".into())); if in_step { diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 4b55c06d8..defee9e49 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -17,7 +17,6 @@ use alloy_rpc_types_trace::parity::StorageDelta; use core::{mem, ops::Range}; use evm2::{ Evm, EvmFeatures, EvmTypes, Inspector, SpecId, TxResultExt, - evm::JournalEntry, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -102,8 +101,6 @@ pub struct TracingInspector { step_stack: Vec<(usize, usize)>, /// Number of opcode steps captured across all calls since the last reset. recorded_steps: u64, - /// Tracks the journal len in the step, used in step_end to check if the journal has changed - last_journal_len: usize, /// The spec id of the EVM. /// /// This is filled during execution. @@ -152,7 +149,6 @@ impl TracingInspector { log_count, step_stack, recorded_steps, - last_journal_len, spec_id, features, // kept @@ -179,7 +175,6 @@ impl TracingInspector { *recorded_steps = 0; spec_id.take(); *features = EvmFeatures::empty(); - *last_journal_len = 0; budget.recorded = 0; budget.exceeded = false; } @@ -516,10 +511,9 @@ impl TracingInspector { } } - self.last_journal_len = interp.host().state().journal().len(); - - let storage_slot = if self.config.record_state_diff { - storage_slot(interp.stack().as_slice(), op.get()) + // A step that halts on the budget never runs, so it records no access. + let storage_change = if self.config.record_state_diff && !self.budget.halts() { + storage_change(interp, op.get()) } else { None }; @@ -544,8 +538,7 @@ impl TracingInspector { .feature(EvmFeatures::EIP8037) .then_some(interp.gas().reservoir()), state_gas_spent: interp.gas().state_gas_spent(), - storage_change: None, - storage_slot, + storage_change, status: None, // This is never populated in `TracingInspector`. @@ -557,7 +550,9 @@ impl TracingInspector { if self.config.record_step_deltas { let write_range = memory_write_range(op.get(), interp.stack().as_slice()); - let store = storage_write(op.get(), interp.stack().as_slice()); + let store = (!self.budget.halts()) + .then(|| storage_write(op.get(), interp.stack().as_slice())) + .flatten(); if write_range.is_some() || store.is_some() || node.trace.steps[step_idx].is_call_like_op() @@ -590,7 +585,6 @@ impl TracingInspector { debug_assert!( step.push_stack.is_none() && step.gas_cost == 0 - && step.storage_change.is_none() && step.status.is_none() && step.decoded.is_none(), "step in step_end is already filled: {trace_idx} -> {step:#?}", @@ -610,49 +604,6 @@ impl TracingInspector { ); } - let address = interp.message().destination; - let host = interp.host(); - let journal = host.state().journal(); - - // If journal has not changed, there is no state change to be recorded. - if self.config.record_state_diff && journal.len() != self.last_journal_len { - let op = step.op.get(); - - step.storage_change = if matches!(op, op::SLOAD | op::SSTORE) { - let reason = match op { - op::SLOAD => StorageChangeReason::SLOAD, - op::SSTORE => StorageChangeReason::SSTORE, - _ => unreachable!(), - }; - - match journal.last() { - Some(JournalEntry::StorageChange { address, key, previous }) => { - // SAFETY: (Address,key) exists if part if StorageChange - let value = host.state().get_storage(address, key).unwrap_or_default(); - let change = - StorageChange { key: *key, value, had_value: Some(*previous), reason }; - Some(Box::new(change)) - } - Some(JournalEntry::StorageWarmed { key, address }) => { - // SAFETY: (Address,key) exists if part if StorageChange - let value = host.state().get_storage(address, key).unwrap_or_default(); - let change = StorageChange { key: *key, value, had_value: None, reason }; - Some(Box::new(change)) - } - _ => None, - } - } else { - None - }; - } - - // The slot an SLOAD read is loaded now, and the SLOAD did not change it. - if let Some(slot) = &mut step.storage_slot - && step.op.get() == op::SLOAD - { - slot.1 = host.state().get_storage(&address, &slot.0).unwrap_or_default(); - } - // An out-of-gas step consumes the whole remainder, except the reentrancy sentry, which // halts before any charge. step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { @@ -934,17 +885,23 @@ impl From for TransactionContext { } } -/// Returns the key and value of the storage slot an SSTORE is about to write, or the key of the -/// slot an SLOAD is about to read, with a zero value that `step_end` replaces. -fn storage_slot(stack: &[U256], op: u8) -> Option> { - match op { - op::SLOAD => Some(Box::new((*stack.last()?, U256::ZERO))), - op::SSTORE => { - let [.., value, key] = stack else { return None }; - Some(Box::new((*key, *value))) - } - _ => None, - } +/// Returns the storage access an SLOAD or SSTORE is about to make, read before the opcode runs. +fn storage_change( + interp: &mut Interpreter<'_, '_, T>, + op: u8, +) -> Option> { + let (key, value, reason) = match (op, interp.stack().as_slice()) { + (op::SLOAD, [.., key]) => (*key, None, StorageChangeReason::SLOAD), + (op::SSTORE, [.., value, key]) => (*key, Some(*value), StorageChangeReason::SSTORE), + _ => return None, + }; + let address = interp.message().destination; + // A database error leaves the record empty: an opcode that reads the slot fails on it too. + let current = interp.host().state_mut().storage_slot_untracked(&address, &key).ok()?; + Some(Box::new(match value { + Some(value) => StorageChange { key, value, had_value: Some(current), reason }, + None => StorageChange { key, value: current, had_value: None, reason }, + })) } /// Returns the memory range whose contents after execution the opcode reports, derived from its diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index dcbd3df2d..956ed5d05 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -665,6 +665,8 @@ pub struct CallTraceStep { pub gas_remaining: u64, /// Gas refund counter before step execution pub gas_refund_counter: u64, + /// The storage access of an SLOAD/SSTORE. Only set if state diffs are recorded. + pub storage_change: Option>, // Fields filled in `step_end` /// Gas cost of step execution pub gas_cost: u64, @@ -675,13 +677,6 @@ pub struct CallTraceStep { /// State-gas spent before this step, used to calculate `state_gas_cost`. #[cfg_attr(feature = "serde", serde(skip))] pub state_gas_spent: i64, - /// Change of the contract state after step execution (effect of the SLOAD/SSTORE instructions) - pub storage_change: Option>, - /// The key and value a geth struct log snapshots for an SLOAD or SSTORE: the value the slot - /// holds before an SLOAD, or the value an SSTORE writes. - /// - /// Only set if state diffs are recorded. - pub storage_slot: Option>, /// Final status of the step /// /// This is set after the step was executed. @@ -824,20 +819,18 @@ pub enum StorageChangeReason { SSTORE, } -/// Represents a storage change during execution. -/// -/// This maps to evm storage journal entries. +/// Represents the storage access of an SLOAD or SSTORE step, read before the opcode runs. /// -/// It is used to track both storage change and warm load of a storage slot. For warm load in regard -/// to EIP-2929 AccessList had_value will be None. +/// For an SLOAD, `value` is the value the slot holds and `had_value` is `None`. For an SSTORE, +/// `value` is the value written and `had_value` is the value the slot held before. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct StorageChange { /// key of the storage slot pub key: U256, - /// Current value of the storage slot + /// The value read by an SLOAD, or written by an SSTORE pub value: U256, - /// The previous value of the storage slot, if any + /// The value an SSTORE overwrites, `None` for an SLOAD pub had_value: Option, /// How this storage was accessed pub reason: StorageChangeReason, diff --git a/crates/inspectors/src/tracing/writer.rs b/crates/inspectors/src/tracing/writer.rs index 9ba9b718e..7b1f8b130 100644 --- a/crates/inspectors/src/tracing/writer.rs +++ b/crates/inspectors/src/tracing/writer.rs @@ -506,15 +506,16 @@ impl TraceWriter { // of their last write. let mut change_id = 0; for step in &node.trace.steps { - if let Some(change) = &step.storage_change { - // Only handle storage changes, signalled by `had_value` being `Some`. - if change.had_value.is_some() { - let (_first, last, last_change_id) = - changes_map.entry(&change.key).or_insert((change, change, change_id)); - *last = change; - *last_change_id = change_id; - change_id += 1; - } + // Only handle storage changes, signalled by `had_value` differing from `value`. + if !step.is_error() + && let Some(change) = &step.storage_change + && change.had_value.is_some_and(|had| had != change.value) + { + let (_first, last, last_change_id) = + changes_map.entry(&change.key).or_insert((change, change, change_id)); + *last = change; + *last_change_id = change_id; + change_id += 1; } } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 5185aca57..10c51e500 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -404,6 +404,58 @@ fn test_geth_erc7562_tracer() { } } +/// geth reads each slot in `OnOpcode`, so a frame records its first read of a slot that an earlier +/// frame already warmed. +#[test] +fn test_geth_erc7562_records_warm_first_reads() { + let outer = address!("1000000000000000000000000000000000000001"); + let inner = address!("1000000000000000000000000000000000000003"); + let caller = address!("1000000000000000000000000000000000000002"); + + // SLOAD 1, then DELEGATECALL the inner code, which SLOADs the same, now warm, slot. + let outer_code = + hex!("6001545060006000600060007310000000000000000000000000000000000000035af45000").to_vec(); + let inner_code = hex!("6001545000").to_vec(); + let context = + Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + for (address, code) in [(outer, outer_code), (inner, inner_code)] { + db.insert_account_info( + &address, + AccountInfo { + code: Some(Bytecode::new_raw(code.into())), + ..Default::default() + }, + ); + } + db.insert_account_storage(&outer, &U256::from(1), &U256::from(0x2a)); + }); + + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + caller, + gas_limit: 1_000_000, + kind: TransactTo::Call(outer), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let slot = B256::from(U256::from(1)); + let value = B256::from(U256::from(0x2a)); + assert_eq!(frame.accessed_slots.reads[&slot], vec![value]); + assert_eq!(frame.calls.len(), 1); + assert_eq!(frame.calls[0].accessed_slots.reads[&slot], vec![value]); +} + fn trace_contract( spec: SpecId, opts: GethDebugTracingOptions, diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index 9f791bb09..74732752b 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -4,7 +4,7 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, EmptyDB, SpecId, TransactTo, use alloy_primitives::{Address, Bytes, U256, hex}; use evm2::{ BaseEvmTypes, Evm, ExecutionConfig, Inspector, Precompiles, Version, - constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, + constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, interpreter::opcode::op, }; use evm2_inspectors::{ access_list::AccessListInspector, @@ -222,6 +222,40 @@ fn byte_budget_can_halt_from_a_step() { assert!(inspector.limit_exceeded()); } +#[test] +fn byte_budget_halt_before_sload_records_no_storage_access() { + // The memory snapshot of the SLOAD step exceeds the budget, which halts before the SLOAD runs. + let (inspector, completed) = inspect_with_limits( + &hex!("600060015f525400"), + Bytes::new(), + TracingInspectorConfig::none().steps().memory_snapshots().set_state_diffs(true), + TraceLimits::default() + .set_max_recorded_bytes(Some(31)) + .set_behavior(TraceLimitBehavior::Halt), + ); + assert!(!completed); + let step = inspector.traces().nodes()[0].trace.steps.last().unwrap(); + assert_eq!(step.op.get(), op::SLOAD); + assert!(step.storage_change.is_none()); +} + +#[test] +fn byte_budget_halt_before_sstore_reports_no_store_delta() { + // The memory snapshot of the SSTORE step exceeds the budget, which halts before it runs. + let (inspector, completed) = inspect_with_limits( + &hex!("6001600060015f525500"), + Bytes::new(), + TracingInspectorConfig::none().steps().memory_snapshots().set_step_deltas(true), + TraceLimits::default() + .set_max_recorded_bytes(Some(32)) + .set_behavior(TraceLimitBehavior::Halt), + ); + assert!(!completed); + let trace = &inspector.traces().nodes()[0].trace; + assert_eq!(trace.steps.last().unwrap().op.get(), op::SSTORE); + assert!(trace.step_deltas.iter().all(|delta| delta.store.is_none())); +} + #[test] fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { let caller = Address::repeat_byte(0xaa); From 56b57cfc22ba00302e3633673c735695400ff0f7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:09:03 +0100 Subject: [PATCH 079/159] fix(state): drop the removed selfdestructs set from CacheDB::commit (#965) #906 and #947 merged without each other: #906 added a `CacheDB::commit` that passes the `selfdestructs` set, which #947 removed in favour of the `DESTROYED` account flag. Main does not build; this drops the argument, since `TxStateChanges` already reads the flag. --- crates/evm2/src/evm/bal/list.rs | 5 +---- crates/evm2/src/evm/db/cache.rs | 6 +++--- crates/evm2/src/evm/state/mod.rs | 4 ++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index 97747b6a1..7e2093deb 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -558,10 +558,7 @@ mod tests { storage: overlay, ..Default::default() }; - let pending = PendingState { - accounts: AddressMap::from_iter([(address, account)]), - selfdestructs: Default::default(), - }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; let mut bal = Bal::new(); bal.commit(idx(1), pending); diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 6c06f25bf..444cf84ea 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -148,17 +148,17 @@ impl CacheDB { /// storage slots, code, and account info are then applied to the cache; the wrapped backing /// database is not written. pub fn commit_pending(&mut self, pending: &PendingState) { - self.commit(&pending.accounts, &pending.selfdestructs); + self.commit(&pending.accounts); } /// Accepts a committed transaction's pending accounts and storage overlays into this cache. /// /// Same as [`Self::commit_pending`], operating on the transaction layers directly so the /// overlay need not be detached. - pub(crate) fn commit(&mut self, accounts: &AddressMap, selfdestructs: &AddressSet) { + pub(crate) fn commit(&mut self, accounts: &AddressMap) { // When BAL construction is enabled, fold the transaction's pending post-state into the // builder before applying the changes to the cache. - self.bal_context.commit(TxStateChanges::new(accounts, selfdestructs)); + self.bal_context.commit(TxStateChanges::new(accounts)); for (&address, entry) in accounts { let overlay = &entry.storage; diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index 761c69275..f7198d7af 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -139,7 +139,7 @@ impl State<'_> { bal_context: self.database.bal_context.clone(), _non_exhaustive: (), }; - database.commit(&self.accounts, &self.inner.selfdestructs); + database.commit(&self.accounts); State { accounts: AddressMap::default(), storage_pool: storage_pool::StoragePool::default(), @@ -1006,7 +1006,7 @@ impl<'a> State<'a> { pub fn commit_transaction(&mut self) { // The transaction overlay is folded into the accepted-overlay database directly, without // detaching it. - self.inner.database.commit(&self.accounts, &self.inner.selfdestructs); + self.inner.database.commit(&self.accounts); for account in self.accounts.values_mut() { self.storage_pool.clear_overlay(&mut account.storage); } From 2057b79d41805552fce1e6804885d74b0bf5fcae Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:10:20 +0100 Subject: [PATCH 080/159] fix(db): drop the unused AddressSet import (#966) Follow-up to #965: `CacheDB::commit` no longer takes the selfdestructs set, so `AddressSet` is unused in db/cache.rs and warns. --- crates/evm2/src/evm/db/cache.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 444cf84ea..54f7d3c97 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -15,7 +15,7 @@ use crate::{ }; use alloy_primitives::{ Address, B256, KECCAK256_EMPTY, - map::{AddressMap, AddressSet, B256Map, U256Map, hash_map::Entry}, + map::{AddressMap, B256Map, U256Map, hash_map::Entry}, }; use core::convert::Infallible; From 47a0cc7c5eedbc11aed973f542b8acd12d294b67 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:11:32 +0100 Subject: [PATCH 081/159] refactor(state): separate discarded storage from account creation (#934) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A contract can be created at an address that already holds storage. On Ethereum that only happens for accounts that predate Spurious Dragon: nonce zero, empty code, nonzero storage. The creation discards the old storage, so a slot first loaded while the account is created reads zero without a database read. This PR concerns one case of that: a constructor reads such a slot, and the creation then rolls back. A rollback now leaves the loaded zero in the overlay, as it leaves every other loaded value, since a rollback only restores what the journal changed and never evicts a cached slot. A retried creation at the same address therefore reads zero again, which is what a created account must read. #845 and #855 instead reread those slots from the database on rollback. That made two identical creations in one transaction read different values for the same slot, zero in the first and the backing value in the second, because the retry sees the reread value as already loaded. It also took a per-slot `created_zero` marker, a retry path for failed rereads that discarded their error in `storage_slot_untracked`, and an extra pass over the touched accounts in every rollback. All of that is removed. The reread existed because `AccountFlags::CREATED` meant two things, created in this transaction and unloaded slots read as zero. #823 then needed a negative `KEEPS_STORAGE` flag stacked on `CREATED`, a rule that the first mark decides, and an exemption in rollback. A positive, journaled `DISCARDS_STORAGE` flag, set only by `State::create_account` and by the empty-account materialization at finalization, now carries the second meaning. `mark_created` sets only `CREATED | CODE_CHANGED`, so an embedder that resumes a transaction on a fresh overlay calls it and keeps the database's storage; `mark_created_keeping_storage` is removed. `storage_slot_untracked` and the slot load path now share one function for the rule that a wiped or discarded account reads zero after the BAL coverage check. The tests that pinned the reread now assert the kept zero, and the two that exercised the failed-reload retry are gone with that path. Instruction counts for the interpreter benches of `cargo bench -p evm2-cli --bench evm`, measured with callgrind (one run of each case, collecting only the `Runner` iteration, `profiling` build, `-Ctarget-cpu=x86-64-v2`) against the parent commit 1dc537e6. Six cases could not be measured because their runs were killed for memory. | benchmark | base Ir | head Ir | change | | :-- | --: | --: | --: | | snailtracer | 521,861,720 | 521,192,049 | 🟢 -0.13% | | hash_10k | 16,503,749 | 16,495,588 | 🟢 -0.05% | | erc20_transfer | 33,865,482 | 33,775,328 | 🟢 -0.27% | | burntpix | 419,995,185 | 419,979,963 | ⚪ -0.00% | | onchain_lm_v2 | 3,320,560,116 | 3,320,559,756 | ⚪ -0.00% | | entry_point_v0_7 | 1,562,330 | 1,561,965 | 🟢 -0.02% | | 19 smaller cases, summed | 3,624,280 | 3,624,322 | ⚪ +0.00% | | all 25 measured | 4,317,972,862 | 4,317,188,971 | 🟢 -0.018% | The smaller cases move by up to about 1% of roughly 62,000 instructions, which is 300 to 850 instructions. Two base runs of the same binary differ by under 15 instructions on four cases, so these shifts are real, but they are not in the changed code. For `eip2935`, the largest shift, the per-function diff shows about +700 in libc `malloc` and start-up code and no net change in the state functions: the new `read_unloaded_slot` call costs 193 instructions and `get_storage` loses 186. CodSpeed reports all 173 benchmarks untouched against main.
Per-case instruction counts for the smaller cases | benchmark | base Ir | head Ir | change | | :-- | --: | --: | --: | | fibonacci-calldata | 405,660 | 406,223 | +0.14% | | factorial | 461,870 | 461,824 | -0.01% | | counter | 63,244 | 62,962 | -0.45% | | weth | 71,666 | 71,936 | +0.38% | | push0_proxy | 61,600 | 61,378 | -0.36% | | usdc_proxy | 64,859 | 64,529 | -0.51% | | fiat_token | 62,120 | 62,263 | +0.23% | | uniswap_v2_pair | 63,815 | 64,048 | +0.37% | | univ2_router | 63,109 | 62,789 | -0.51% | | seaport | 62,776 | 63,347 | +0.91% | | airdrop | 62,172 | 62,972 | +1.29% | | bswap64 | 62,347 | 62,117 | -0.37% | | bswap64_opt | 61,330 | 61,699 | +0.60% | | eip4788 | 62,329 | 62,934 | +0.97% | | eip2935 | 61,089 | 61,939 | +1.39% | | curve_stableswap | 683,519 | 683,439 | -0.01% | | usdt | 88,717 | 88,594 | -0.14% | | universal_router | 306,223 | 305,381 | -0.27% | | aggregation_router_v6 | 855,835 | 853,948 | -0.22% |
--- crates/evm2/src/evm/state/account.rs | 61 ++++----- crates/evm2/src/evm/state/block.rs | 1 - crates/evm2/src/evm/state/mod.rs | 184 ++++++++++----------------- crates/evm2/src/evm/state/pending.rs | 1 - crates/evm2/src/evm/state/storage.rs | 70 ++-------- 5 files changed, 106 insertions(+), 211 deletions(-) diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index d482b6fe5..9216abbd1 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -180,10 +180,9 @@ bitflags! { const DESTROYED = 1 << 3; /// The present overlay account was created in the current transaction. const CREATED = 1 << 4; - /// A created account keeps its storage baseline, see - /// [`AccountHandle::mark_created_keeping_storage`]. Meaningful only while - /// [`Self::CREATED`] is set. - const KEEPS_STORAGE = 1 << 5; + /// The present overlay account's creation discarded its old storage, so unloaded slots + /// read as zero. + const DISCARDS_STORAGE = 1 << 5; } } @@ -197,7 +196,8 @@ bitflags! { /// /// [`AccountFlags::CREATED`] tracks creation in the current transaction and drives /// transaction-finalization rules. It survives selfdestruct finalization, so it can stay set while -/// `present` is `None`. +/// `present` is `None`. [`AccountFlags::DISCARDS_STORAGE`] marks a creation that discarded the +/// account's old storage. /// /// Detached from the EVM as part of a [`PendingState`](super::PendingState), the entry describes /// the account's transaction transition: `original` against `present`, with @@ -208,7 +208,7 @@ pub(crate) struct Account { pub(crate) original: Option, /// Present account overlay after mutations. `None` means the account is absent/deleted. pub(crate) present: Option, - /// Lifecycle flags: warm, touched, destroyed, created, and keeps-storage. + /// Lifecycle flags: warm, touched, destroyed, created, and discards-storage. pub(crate) flags: AccountFlags, /// Persistent transaction storage owned by this account. pub(crate) storage: StorageOverlay, @@ -222,15 +222,16 @@ impl Account { } /// Marks the account as created during the transaction. - /// - /// The first creation mark decides whether the account keeps its storage baseline. #[inline] pub(crate) const fn mark_created(&mut self) { - let mut flags = self.flags; - if !flags.contains(AccountFlags::CREATED) { - flags = flags.difference(AccountFlags::KEEPS_STORAGE); - } - self.flags = flags.union(AccountFlags::CREATED); + self.flags = self.flags.union(AccountFlags::CREATED); + } + + /// Marks the account as created and discards its old storage. + #[inline] + pub(crate) const fn mark_created_discarding_storage(&mut self) { + self.mark_created(); + self.flags = self.flags.union(AccountFlags::DISCARDS_STORAGE); } /// Returns whether the account was created during the transaction. @@ -246,8 +247,7 @@ impl Account { /// slots read as zero and committing it drops cached slots. #[inline] pub(crate) const fn discards_storage(&self) -> bool { - self.flags.contains(AccountFlags::CREATED) - && !self.flags.contains(AccountFlags::KEEPS_STORAGE) + self.flags.contains(AccountFlags::DISCARDS_STORAGE) } /// Returns whether the account's info changed during the transaction. @@ -644,7 +644,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// [`JournalEntry::AccountChange`] snapshot recorded for this handle. /// /// Created accounts follow the EIP-6780 rules for `SELFDESTRUCT` and are emitted as created by - /// [`PendingState`](super::PendingState). + /// [`PendingState`](super::PendingState). The mark does not discard the account's old storage: + /// unloaded slots still read from the database and committing the account keeps its cached + /// slots. This suits an embedder that continues a transaction on a fresh overlay whose database + /// already holds the account's creation and storage writes, such as after suspending the + /// transaction. Slots then load with the database values as their transaction originals, so + /// `SSTORE` gas and refunds can differ from an uninterrupted run, where a created account's + /// originals are zero. /// /// The account must be present, for example through [`Self::get_or_insert`]. #[inline] @@ -653,25 +659,12 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.mark_created(); } - /// Marks the present overlay account as created in the current transaction, like - /// [`Self::mark_created`], but keeps its storage baseline: unloaded slots read from the - /// database and committing the account does not drop its cached slots. - /// - /// For embedders that continue a transaction on a fresh overlay whose database already holds - /// the account's creation and storage writes, such as after suspending the transaction. The - /// account stays eligible for EIP-6780 `SELFDESTRUCT`. An account already created in this - /// overlay keeps reading its old storage as zero. - /// - /// Slots load with the database values as their transaction originals, so `SSTORE` gas and - /// refunds can differ from an uninterrupted run, where a created account's originals are zero. - /// This matches resuming any other account on a fresh overlay. + /// Marks the account as created like [`Self::mark_created`] and discards its old storage, so + /// unloaded slots read as zero. #[inline] - pub fn mark_created_keeping_storage(&mut self) { - let first = !self.tracked.is_created(); - self.mark_created(); - if first { - self.tracked.flags.insert(AccountFlags::KEEPS_STORAGE); - } + pub(super) fn mark_created_discarding_storage(&mut self) { + self.record_change(); + self.tracked.mark_created_discarding_storage(); } /// Records a revert snapshot and returns the live account, materializing an empty one when it diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 1187c464b..75ce25ab3 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -323,7 +323,6 @@ mod tests { StorageSlot { value: Tracked::from_parts(original, current), is_warm: false, - created_zero: false, _non_exhaustive: (), }, )]) diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index f7198d7af..51bbf4d63 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -205,6 +205,25 @@ impl StateInner<'_> { self.journal.set_len(len + 1); } } + + /// Reads a slot absent from the overlay. + /// + /// A slot of a wiped account, or of an account whose creation discarded its old storage, reads + /// zero without a database read, though a strict BAL must still cover it. + #[inline] + fn read_unloaded_slot( + &mut self, + address: &Address, + key: &Word, + reads_zero: bool, + ) -> DbResult { + if reads_zero { + self.database.check_bal_storage(address, key)?; + Ok(Word::ZERO) + } else { + self.database.get_storage(address, key) + } + } } impl<'a> State<'a> { @@ -657,23 +676,18 @@ impl<'a> State<'a> { /// This is the storage-side mirror of [`Self::account_info_untracked`]: a non-loading peek that /// returns the overlay slot value when one has been loaded or written this transaction, /// otherwise it reads the backing database directly without caching the result in the overlay. - /// A slot of a wiped or created account that has not been rewritten reads as zero, though an - /// attached strict BAL must still cover it. Use [`Self::storage`] when the slot should be - /// loaded and preserved. + /// A slot of a wiped account, or of one whose creation discarded its old storage, that has not + /// been loaded or written reads as zero, though an attached strict BAL must still cover it. Use + /// [`Self::storage`] when the slot should be loaded and preserved. #[inline(never)] pub fn storage_slot_untracked(&mut self, address: &Address, key: &Word) -> DbResult { - if let Some(account) = self.accounts.get(address) { - if let Some(slot) = account.storage.slots.get(key) { - // A slot a failed rollback reload left at its cached zero reads the database. - if !slot.created_zero || account.discards_storage() { - return Ok(slot.value.current); - } - } else if account.storage.wiped || account.discards_storage() { - self.database.check_bal_storage(address, key)?; - return Ok(Word::ZERO); - } + let account = self.accounts.get(address); + if let Some(slot) = account.and_then(|account| account.storage.slots.get(key)) { + return Ok(slot.value.current); } - self.database.get_storage(address, key) + let reads_zero = + account.is_some_and(|account| account.storage.wiped || account.discards_storage()); + self.inner.read_unloaded_slot(address, key, reads_zero) } /// Transfers value between accounts. @@ -754,7 +768,7 @@ impl<'a> State<'a> { #[cfg(feature = "account-ext")] extension, }; - target.mark_created(); + target.mark_created_discarding_storage(); target.touch(); Ok(Ok(())) } @@ -826,9 +840,6 @@ impl<'a> State<'a> { // Restoring an older snapshot can leave active cursors past the current lengths. Preserve // shorter prefixes and revert only entries beyond each cursor. self.logs.truncate(checkpoint.logs_len); - // Accounts whose creation was undone. Their slot reloads wait until every slot write of - // the rolled-back scope is reverted, which can happen after the account entry is. - let mut uncreated = Vec::new(); while self.journal.len() > checkpoint.journal_len { let Some(entry) = self.journal.pop() else { unreachable!("journal length is checked above") @@ -836,21 +847,13 @@ impl<'a> State<'a> { match entry { JournalEntry::AccountChange { address, previous, previous_flags } => { if let Some(entry) = self.accounts.get_mut(&address) { - let discarded_storage = entry.discards_storage(); entry.present = previous; // EIP-161 preserves the historical Yellow Paper K.1 precompile-3 touch. let keep_touch = features.contains(EvmFeatures::EIP161) && address == Address::with_last_byte(3); - // The storage baseline choice is not journaled, so rollback never restores - // it. - let mut kept = AccountFlags::KEEPS_STORAGE; - if keep_touch { - kept |= AccountFlags::TOUCHED; - } + let kept = + if keep_touch { AccountFlags::TOUCHED } else { AccountFlags::empty() }; entry.flags = (previous_flags - kept) | (entry.flags & kept); - if discarded_storage && !entry.discards_storage() { - uncreated.push(address); - } } } JournalEntry::StorageChange { address, key, previous } => { @@ -881,13 +884,6 @@ impl<'a> State<'a> { } } } - for address in uncreated { - if let Some(entry) = self.accounts.get_mut(&address) - && !entry.discards_storage() - { - entry.storage.reload_zeroed(&address, &mut self.inner.database); - } - } } fn materialize_empty_account_for_finalization(&mut self, address: &Address) -> DbResult<()> { @@ -898,7 +894,7 @@ impl<'a> State<'a> { // Finalization runs after the last revertible scope, so this is not journaled: the // entry would never be replayed before `clear_transaction_state` clears it. entry.present = Some(AccountInfo::default()); - entry.mark_created(); + entry.mark_created_discarding_storage(); } Ok(()) } @@ -949,9 +945,9 @@ impl<'a> State<'a> { } survives }; - // Like revm, a created survivor keeps its unloaded old storage, which later - // transactions read as zero. One that keeps its storage baseline has no such reads, - // so its storage is wiped. + // A created survivor that discarded its old storage keeps it unloaded, and later + // transactions read it as zero. One that did not discard has no such reads, so its + // storage is wiped. let zero_loaded_only = survives && self.accounts[address].discards_storage(); let mut storage = self.storage(address)?; if zero_loaded_only { storage.zero_loaded() } else { storage.wipe() } @@ -1017,7 +1013,6 @@ impl<'a> State<'a> { #[cfg(test)] mod tests { use super::*; - use crate::evm::bal::{AccountBal, Bal, BlockAccessIndex}; use alloc::vec; #[test] @@ -1096,7 +1091,7 @@ mod tests { let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); - // A slot loaded before the creation keeps its value, as in revm. + // A slot loaded before the creation keeps its value. assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); assert_eq!(state.storage_slot(&address, unloaded).unwrap().original(), Word::ZERO); @@ -1105,20 +1100,19 @@ mod tests { state.storage_slot(&address, unloaded).unwrap().write(Word::from(9)); state.rollback(checkpoint, features); - // Rolling back the creation reloads the slot it read as zero from the backing database. + // Rolling back the creation keeps the zero it loaded. assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); - assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); assert_eq!(state.storage_slot(&address, fresh).unwrap().current(), Word::ZERO); - // Like `loaded`, the slot was already loaded before this creation, so it keeps its value. - assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); state.rollback(checkpoint, features); assert!(!state.take_pending_state().is_changed()); } #[test] - fn created_account_keeping_storage_reads_and_commits_its_baseline() { + fn marked_created_account_reads_and_commits_its_baseline() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); let (written, unwritten) = (Word::from(1), Word::from(2)); @@ -1130,7 +1124,7 @@ mod tests { let mut state = State::new(db); let mut account = state.account(&address).unwrap(); - account.mark_created_keeping_storage(); + account.mark_created(); // Still created for EIP-6780 `SELFDESTRUCT`. assert!(account.is_created()); drop(account); @@ -1144,19 +1138,15 @@ mod tests { assert_eq!(state.storage_slot(&address, written).unwrap().current(), Word::from(9)); assert_eq!(state.storage_slot(&address, unwritten).unwrap().current(), Word::from(6)); - // Rolling back the mark forgets the kept baseline, and an account already created in the - // overlay keeps reading its old storage as zero. + // Marking an account created in the overlay does not undo its discarded storage. let other = Address::with_last_byte(3); state .overlay_db_mut() .insert_account_info(&other, AccountInfo::default().with_balance(Word::from(1))); state.overlay_db_mut().insert_account_storage(&other, &unwritten, &Word::from(6)); - let checkpoint = state.checkpoint(); - state.account(&other).unwrap().mark_created_keeping_storage(); - state.rollback(checkpoint, features); state.create_account(&caller, &other, &Word::ZERO, features).unwrap().unwrap(); + state.account(&other).unwrap().mark_created(); assert_eq!(state.storage_slot_untracked(&other, &unwritten).unwrap(), Word::ZERO); - state.account(&other).unwrap().mark_created_keeping_storage(); assert_eq!(state.storage_slot(&other, unwritten).unwrap().current(), Word::ZERO); } @@ -1206,7 +1196,7 @@ mod tests { } #[test] - fn eip8246_survivor_keeping_storage_wipes_it() { + fn eip8246_marked_created_survivor_wipes_storage() { let address = Address::with_last_byte(2); let (written, earlier) = (Word::from(1), Word::from(2)); let version = Version::base(crate::SpecId::AMSTERDAM); @@ -1216,7 +1206,7 @@ mod tests { db.insert_account_storage(&address, &earlier, &Word::from(6)); let mut state = State::new(db); - state.account(&address).unwrap().mark_created_keeping_storage(); + state.account(&address).unwrap().mark_created(); state.storage_slot(&address, written).unwrap().write(Word::from(9)); state.account(&address).unwrap().mark_destructed(); state.finalize_transaction_(version); @@ -1229,7 +1219,7 @@ mod tests { } #[test] - fn rollback_of_later_creation_mark_keeps_storage_baseline() { + fn rollback_of_storage_discard_restores_storage_baseline() { let address = Address::with_last_byte(2); let key = Word::from(1); let features = Version::base(crate::SpecId::CANCUN).features; @@ -1238,17 +1228,19 @@ mod tests { db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); - state.account(&address).unwrap().mark_created_keeping_storage(); - let checkpoint = state.checkpoint(); state.account(&address).unwrap().mark_created(); + let checkpoint = state.checkpoint(); + state.account(&address).unwrap().mark_created_discarding_storage(); + assert!(state.accounts[&address].discards_storage()); state.rollback(checkpoint, features); + assert!(state.accounts[&address].is_created()); assert!(!state.accounts[&address].discards_storage()); assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); } #[test] - fn rollback_of_creation_reloads_slots_read_as_zero() { + fn rollback_of_creation_keeps_slots_read_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); let (read, written, empty) = (Word::from(1), Word::from(2), Word::from(3)); @@ -1264,22 +1256,19 @@ mod tests { assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::ZERO); state.storage_slot(&address, written).unwrap().write(Word::from(9)); assert_eq!(state.storage_slot(&address, empty).unwrap().current(), Word::ZERO); - assert!(state.accounts[&address].storage.slots[&empty].created_zero); state.rollback(checkpoint, version.features); assert!(!state.accounts[&address].discards_storage()); - assert!(state.accounts[&address].storage.slots.values().all(|slot| !slot.created_zero)); - for (key, value) in [(read, 6), (written, 7), (empty, 0)] { - let value = Word::from(value); - assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), value); + for key in [read, written, empty] { + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::ZERO); let slot = state.storage_slot(&address, key).unwrap(); - assert_eq!((slot.original(), slot.current()), (value, value)); + assert_eq!((slot.original(), slot.current()), (Word::ZERO, Word::ZERO)); } assert!(!state.take_pending_state().is_changed()); } #[test] - fn rollback_reloads_zeroed_slots_only_when_the_creation_is_undone() { + fn rollback_keeps_slot_read_as_zero_under_the_creation() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); let key = Word::from(1); @@ -1296,11 +1285,11 @@ mod tests { state.rollback(inner, features); assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); state.rollback(outer, features); - assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); } #[test] - fn rollback_of_creation_keeping_storage_leaves_slots_alone() { + fn rollback_of_creation_mark_leaves_slots_alone() { let address = Address::with_last_byte(2); let (read, zero) = (Word::from(1), Word::from(2)); let features = Version::base(crate::SpecId::CANCUN).features; @@ -1310,7 +1299,7 @@ mod tests { let mut state = State::new(db); let checkpoint = state.checkpoint(); - state.account(&address).unwrap().mark_created_keeping_storage(); + state.account(&address).unwrap().mark_created(); assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::from(6)); assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); state.rollback(checkpoint, features); @@ -1320,47 +1309,7 @@ mod tests { } #[test] - fn rollback_of_creation_keeps_slot_whose_reload_fails() { - let caller = Address::with_last_byte(1); - let address = Address::with_last_byte(2); - let key = Word::from(1); - let features = Version::base(crate::SpecId::CANCUN).features; - let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); - db.insert_account_storage(&address, &key, &Word::from(6)); - let mut state = State::new(db); - let bal = Arc::new(Bal::from_iter([(address, AccountBal::default())])); - - // The creation's own read checks the BAL, so detach it there. The BAL covers the account - // but omits the slot, so rereading the slot during the rollback fails. - let checkpoint = state.checkpoint(); - state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); - assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); - state.set_bal(bal); - state.set_bal_index(BlockAccessIndex::new(1)); - state.rollback(checkpoint, features); - - // The slot stays loaded, so its read reaches the BAL, and later loads report the failure - // instead of serving the zero. - assert!(state.accounts[&address].storage.slots[&key].created_zero); - // An unaffordable cold access is skipped before the pending reload reads the database. - assert!(matches!( - state.storage(&address).unwrap().into_slot_with_skip(key, true), - Err(LoadError::ColdLoadSkipped) - )); - assert!(state.storage_slot(&address, key).is_err()); - assert!(state.storage_slot_untracked(&address, &key).is_err()); - assert!(state.accounts[&address].storage.slots[&key].created_zero); - - state.clear_bal(); - assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::from(6)); - let slot = state.storage_slot(&address, key).unwrap(); - assert_eq!((slot.original(), slot.current()), (Word::from(6), Word::from(6))); - assert!(!state.accounts[&address].storage.slots[&key].created_zero); - } - - #[test] - fn finalization_wipe_clears_the_marker_of_a_slot_whose_reload_failed() { + fn finalization_wipe_after_rolled_back_creation_reads_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); let key = Word::from(1); @@ -1373,10 +1322,7 @@ mod tests { let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); - state.set_bal(Arc::new(Bal::from_iter([(address, AccountBal::default())]))); - state.set_bal_index(BlockAccessIndex::new(1)); state.rollback(checkpoint, version.features); - state.clear_bal(); state.account(&address).unwrap().mark_destructed(); state.finalize_transaction_(version); @@ -1386,7 +1332,7 @@ mod tests { } #[test] - fn rollback_of_creation_reloads_only_slots_loaded_under_it() { + fn rollback_of_creation_keeps_slots_loaded_under_it() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); let (before, under) = (Word::from(1), Word::from(2)); @@ -1395,7 +1341,7 @@ mod tests { db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); let mut state = State::new(db); assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); - // Only a reread can observe these values. + // Only a reread can observe these values, and rollback does not reread. for key in [before, under] { state.overlay_db_mut().insert_account_storage(&address, &key, &Word::from(5)); } @@ -1406,11 +1352,11 @@ mod tests { state.rollback(checkpoint, features); assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); - assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::from(5)); + assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::ZERO); } #[test] - fn rollback_reloads_slots_written_through_the_creating_handle() { + fn rollback_keeps_zero_of_slot_written_through_the_creating_handle() { let address = Address::with_last_byte(2); let key = Word::from(1); let features = Version::base(crate::SpecId::CANCUN).features; @@ -1421,13 +1367,13 @@ mod tests { let checkpoint = state.checkpoint(); let mut account = state.account(&address).unwrap(); - account.mark_created(); + account.mark_created_discarding_storage(); // The slot write is journaled before the handle flushes its creation entry on drop. account.storage().into_slot(key).unwrap().write(Word::from(9)); drop(account); state.rollback(checkpoint, features); - assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); } #[test] diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 201678c1e..a4da6e6ed 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -76,7 +76,6 @@ impl PendingState { StorageSlot { value: Tracked::from_parts(original, current), is_warm: false, - created_zero: false, _non_exhaustive: (), }, ); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index 86877ad02..5ae04c274 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -1,6 +1,6 @@ //! Transaction-scoped persistent storage overlay. -use super::{DbResult, DynDatabase, JournalEntry, StateInner, Tracked}; +use super::{DbResult, JournalEntry, StateInner, Tracked}; use crate::{LoadError, interpreter::Word}; use alloy_primitives::{ Address, @@ -40,20 +40,6 @@ impl StorageOverlay { pub fn is_changed(&self) -> bool { self.wiped || self.changed_slots().next().is_some() } - - /// Rereads the slots first loaded under the account's creation after that creation was rolled - /// back. - /// - /// Such a slot cached zero instead of its backing value, and the rollback restores its zero - /// current value. A slot that fails to reload keeps its zero and its marker, so its read stays - /// in the overlay for the BAL and its next load rereads it and reports the error. - pub(super) fn reload_zeroed(&mut self, address: &Address, database: &mut dyn DynDatabase) { - for (key, slot) in &mut self.slots { - if slot.created_zero { - let _ = slot.reload_created_zero(address, key, database); - } - } - } } /// Persistent storage slot cached by [`super::State`]. @@ -67,9 +53,6 @@ pub struct StorageSlot { pub value: Tracked, /// Whether the slot was warmed during execution this transaction, for EIP-2929 gas accounting. pub is_warm: bool, - /// Whether the slot was first loaded while its account was created in the transaction, so it - /// cached zero instead of its backing value. Rolling that creation back reloads the slot. - pub(crate) created_zero: bool, #[doc(hidden)] // Not public API. Please use an existing constructor. pub _non_exhaustive: (), } @@ -81,24 +64,11 @@ impl StorageSlot { if wiped { !self.value.current.is_zero() } else { self.value.is_changed() } } - /// Rereads a zero cached under a creation that was rolled back. - #[cold] - pub(super) fn reload_created_zero( - &mut self, - address: &Address, - key: &Word, - database: &mut dyn DynDatabase, - ) -> DbResult<()> { - self.value = Tracked::new(database.get_storage(address, key)?); - self.created_zero = false; - Ok(()) - } - /// Creates a freshly loaded slot whose original and current values are `value`, with the given - /// EIP-2929 warmth. `created_zero` marks a zero cached without a database read. + /// EIP-2929 warmth. #[inline] - fn loaded(value: Word, is_warm: bool, created_zero: bool) -> Self { - Self { value: Tracked::new(value), is_warm, created_zero, _non_exhaustive: () } + fn loaded(value: Word, is_warm: bool) -> Self { + Self { value: Tracked::new(value), is_warm, _non_exhaustive: () } } } @@ -119,8 +89,9 @@ pub struct StorageHandle<'a, 'db> { address: &'a Address, /// Transaction overlay entry: the per-account storage slots plus the wipe flag. storage: &'a mut StorageOverlay, - /// Whether the account was created in this transaction, so its unloaded slots read as zero. - created: bool, + /// Whether the account's creation discarded its old storage, so its unloaded slots read as + /// zero. + discards_storage: bool, /// Shared inner state: backing database, revert journal, and base warm set. #[derive_where(skip)] inner: &'a mut StateInner<'db>, @@ -133,10 +104,10 @@ impl<'a, 'db> StorageHandle<'a, 'db> { pub(crate) const fn new( address: &'a Address, storage: &'a mut StorageOverlay, - created: bool, + discards_storage: bool, inner: &'a mut StateInner<'db>, ) -> Self { - Self { address, storage, created, inner } + Self { address, storage, discards_storage, inner } } /// Returns the account address. @@ -198,8 +169,7 @@ impl<'a, 'db> StorageHandle<'a, 'db> { /// slot's [`StorageSlot::is_warm`] is seeded from the base warm set (EIP-2930 access list), /// mirroring how [`account_raw`](super::State) seeds an account's warmth. The load itself /// records no revert entry: the cached value's original equals its current, so it is left in - /// place by [`State::rollback`](super::State::rollback) as a harmless cache, except that a - /// creation's rollback reloads the slots it read as zero. Used by + /// place by [`State::rollback`](super::State::rollback) as a harmless cache. Used by /// [`State::storage_slot`](super::State::storage_slot) to reach a single slot directly. #[inline] pub fn into_slot_with_skip( @@ -229,7 +199,7 @@ impl<'a, 'db> StorageHandle<'a, 'db> { key: Word, skip_cold_load: bool, ) -> Result<(StorageSlotHandle<'a, 'db>, bool), LoadError> { - let Self { address, storage, created, inner } = self; + let Self { address, storage, discards_storage, inner } = self; let (slot, is_cold) = match storage.slots.entry(key) { hash_map::Entry::Occupied(entry) => { // An already-loaded slot has no cold database read to skip, so the skip only @@ -245,10 +215,6 @@ impl<'a, 'db> StorageHandle<'a, 'db> { if skip_cold_load && is_cold { return Err(LoadError::ColdLoadSkipped); } - // A rollback that failed to reload the slot leaves it for this load to retry. - if slot.created_zero && !created { - slot.reload_created_zero(address, &key, &mut inner.database)?; - } (slot, is_cold) } hash_map::Entry::Vacant(entry) => { @@ -256,16 +222,9 @@ impl<'a, 'db> StorageHandle<'a, 'db> { if skip_cold_load && !is_warm { return Err(LoadError::ColdLoadSkipped); } - // Like revm, an unloaded slot of a created account reads zero without a database - // read, since the creation discards the address's old storage. - let value = if storage.wiped || created { - // The zero needs no database read, but a strict BAL must still cover the slot. - inner.database.check_bal_storage(address, &key)?; - Word::ZERO - } else { - inner.database.get_storage(address, &key)? - }; - (entry.insert(StorageSlot::loaded(value, is_warm, created)), !is_warm) + let value = + inner.read_unloaded_slot(address, &key, storage.wiped || discards_storage)?; + (entry.insert(StorageSlot::loaded(value, is_warm)), !is_warm) } }; if WARM && is_cold { @@ -298,7 +257,6 @@ impl<'a, 'db> StorageHandle<'a, 'db> { pub(crate) fn zero_loaded(&mut self) { self.storage.slots.iter_mut().for_each(|(_, slot)| { slot.value.set_current(Word::ZERO); - slot.created_zero = false; }); } } From d1b2b46f554b54730444afe939b4249a779cb826 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:42:34 +0100 Subject: [PATCH 082/159] fix(evm2): give the calldata floor and execution gas cap checks their own errors (#974) Execution-specs rejects a transaction before execution with `InsufficientTransactionGasError` when the gas limit is below the intrinsic gas, below the EIP-7623 calldata floor, or, under EIP-8037, when the intrinsic execution gas or the floor exceeds `TX_MAX_GAS_LIMIT` (`validate_transaction` in `transactions.py`). A gas limit above the total cap, `tx.gas > TX_MAX_TOTAL_GAS_LIMIT`, is a different rule and a different error, `TransactionGasLimitExceededError`. `HandlerError` did not keep those rules apart. `validate_floor_gas` returned the same `IntrinsicGasTooLow` as the intrinsic gas check, and `validate_execution_gas_limit_cap` returned `TxGasLimitGreaterThanCap`, the error for a gas limit above the cap, for intrinsic gas above the execution cap. A caller could not tell which rule rejected a transaction. This adds `GasLimitBelowFloor` for the floor check and `IntrinsicGasGreaterThanCap` for the execution cap check, and updates the tests that expected the old errors. The differential fuzzer normalizer counts both as intrinsic gas too low, the category the reference implementation's matching errors already fall in. --- crates/cli/src/fuzzer/normalize.rs | 13 +++++++++++-- crates/evm2/src/ethereum/mod.rs | 25 +++++++++++++++++++------ crates/evm2/src/evm/registry.rs | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/fuzzer/normalize.rs b/crates/cli/src/fuzzer/normalize.rs index c86c7e93b..2ab99391d 100644 --- a/crates/cli/src/fuzzer/normalize.rs +++ b/crates/cli/src/fuzzer/normalize.rs @@ -151,7 +151,9 @@ impl fmt::Display for FuzzError { impl From for FuzzError { fn from(error: HandlerError) -> Self { match error { - HandlerError::IntrinsicGasTooLow { .. } => Self::IntrinsicGasTooLow, + HandlerError::IntrinsicGasTooLow { .. } + | HandlerError::GasLimitBelowFloor { .. } + | HandlerError::IntrinsicGasGreaterThanCap { .. } => Self::IntrinsicGasTooLow, HandlerError::InsufficientFunds { .. } => Self::InsufficientFunds, HandlerError::InvalidNonce { .. } => Self::InvalidNonce, HandlerError::NonceOverflow => Self::NonceOverflow, @@ -349,7 +351,7 @@ mod tests { }, ), ( - HandlerError::IntrinsicGasTooLow { required: 66_200, got: 60_000 }, + HandlerError::GasLimitBelowFloor { required: 66_200, got: 60_000 }, InvalidTransaction::GasFloorMoreThanGasLimit { gas_floor: 66_200, gas_limit: 60_000, @@ -368,6 +370,13 @@ mod tests { ), (HandlerError::NonceOverflow, InvalidTransaction::NonceOverflowInTransaction), (HandlerError::UnsupportedTransactionType(4), InvalidTransaction::Eip7702NotSupported), + ( + HandlerError::IntrinsicGasGreaterThanCap { required: 17_000_000, cap: 16_777_216 }, + InvalidTransaction::GasFloorMoreThanGasLimit { + gas_floor: 17_000_000, + gas_limit: 16_777_216, + }, + ), (HandlerError::EmptyBlobs, InvalidTransaction::EmptyBlobs), ( HandlerError::TooManyBlobs { have: 10, max: 6 }, diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 8c603f6e8..4032ef05e 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -353,8 +353,8 @@ pub const fn validate_execution_gas_limit_cap( if tx_gas_limit > cap { let required_execution_gas = if intrinsic > floor_gas { intrinsic } else { floor_gas }; if required_execution_gas > cap { - return Err(HandlerError::TxGasLimitGreaterThanCap { - gas_limit: required_execution_gas, + return Err(HandlerError::IntrinsicGasGreaterThanCap { + required: required_execution_gas, cap, }); } @@ -419,7 +419,7 @@ pub const fn validate_intrinsic_gas( /// Validates that the gas limit covers the calldata floor gas. pub const fn validate_floor_gas(gas_limit: u64, floor_gas: u64) -> HandlerResult<()> { if gas_limit < floor_gas { - return Err(HandlerError::IntrinsicGasTooLow { required: floor_gas, got: gas_limit }); + return Err(HandlerError::GasLimitBelowFloor { required: floor_gas, got: gas_limit }); } Ok(()) } @@ -993,7 +993,7 @@ mod tests { if gas_limit < 31_050 { assert_eq!( result, - Err(HandlerError::IntrinsicGasTooLow { required: 31_050, got: gas_limit }) + Err(HandlerError::GasLimitBelowFloor { required: 31_050, got: gas_limit }) ); } else { let result = result.unwrap(); @@ -1092,6 +1092,19 @@ mod tests { ); } + #[test] + fn gas_limit_below_floor_is_not_an_intrinsic_gas_error() { + assert_eq!( + validate_intrinsic_gas(20_999, 21_000, 0), + Err(HandlerError::IntrinsicGasTooLow { required: 21_000, got: 20_999 }) + ); + assert_eq!( + validate_floor_gas(21_009, 21_010), + Err(HandlerError::GasLimitBelowFloor { required: 21_010, got: 21_009 }) + ); + assert_eq!(validate_floor_gas(21_010, 21_010), Ok(())); + } + #[test] fn floor_gas_charges_prague_calldata_tokens() { let input = Bytes::from_static(&[0, 1, 2]); @@ -1411,8 +1424,8 @@ mod tests { amsterdam.tx_gas_limit_cap + 1, floor_gas, ), - Err(HandlerError::TxGasLimitGreaterThanCap { - gas_limit: amsterdam.tx_gas_limit_cap + 1, + Err(HandlerError::IntrinsicGasGreaterThanCap { + required: amsterdam.tx_gas_limit_cap + 1, cap: amsterdam.tx_gas_limit_cap }) ); diff --git a/crates/evm2/src/evm/registry.rs b/crates/evm2/src/evm/registry.rs index 205de0ee1..6fae6f6a1 100644 --- a/crates/evm2/src/evm/registry.rs +++ b/crates/evm2/src/evm/registry.rs @@ -65,6 +65,14 @@ pub enum HandlerError { /// Transaction gas limit. got: u64, }, + /// Transaction gas limit is lower than the calldata floor gas (EIP-7623). + #[error("gas limit below floor gas: required {required}, got {got}")] + GasLimitBelowFloor { + /// Required floor gas. + required: u64, + /// Transaction gas limit. + got: u64, + }, /// Sender cannot pay value plus maximum gas cost. #[error("lack of funds ({balance}) for max fee ({fee})")] InsufficientFunds { @@ -95,6 +103,19 @@ pub enum HandlerError { /// Active transaction gas limit cap. cap: u64, }, + /// Intrinsic execution gas, or the calldata floor gas if larger, exceeds the active execution + /// gas cap (EIP-8037). + /// + /// Unlike [`TxGasLimitGreaterThanCap`](Self::TxGasLimitGreaterThanCap), the transaction's own + /// gas limit is not what exceeds a cap: it may exceed the execution cap, but the gas it must + /// spend before execution cannot. + #[error("intrinsic gas exceeds cap: required {required}, cap {cap}")] + IntrinsicGasGreaterThanCap { + /// Required execution gas: the larger of the intrinsic and floor gas. + required: u64, + /// Active execution gas cap. + cap: u64, + }, /// Create transaction initcode exceeds the active size limit. #[error("create initcode size limit exceeded: limit {limit}, got {got}")] CreateInitCodeSizeLimit { From 7304de7b818af3bbaca7dae69038e2beaf3f679a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:42:38 +0100 Subject: [PATCH 083/159] fix(eest): derive the EIP-7918 reserve price in BlobScheduleEntry::params (#967) `BlobScheduleEntry::params` returned a zero `blob_base_cost`, and `execute_block` patched the EIP-7918 reserve price back in for Osaka and later. Any other consumer of the schedule, such as `blob_params` in the replay bench, got parameters that ignored the reserve price. `params` now takes the spec and sets `blob_base_cost` itself, and `ForkSchedule::blob_params` passes the spec of the block's stage. The per-block patch in `execute_block` is gone. --- crates/cli/src/capture/export.rs | 2 +- crates/eest/src/blockchaintest/execute.rs | 42 ++++++++++++++--------- crates/eest/src/blockchaintest/types.rs | 14 +++++--- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index 58a5619dc..ad3f2217d 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -362,6 +362,6 @@ mod tests { schedule.keys().copied().collect::>(), [BlobFork::Cancun, BlobFork::Prague, BlobFork::Osaka, BlobFork::Bpo1] ); - assert_eq!(schedule[&BlobFork::Bpo1].params().update_fraction, 8_346_193); + assert_eq!(schedule[&BlobFork::Bpo1].params(SpecId::OSAKA).update_fraction, 8_346_193); } } diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 398fd0f23..ce167a933 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -27,11 +27,7 @@ use crate::{ }; use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; -use alloy_eips::{ - eip2718::Typed2718, - eip4844::DATA_GAS_PER_BLOB, - eip7840::{BLOB_BASE_COST, BlobParams}, -}; +use alloy_eips::{eip2718::Typed2718, eip4844::DATA_GAS_PER_BLOB, eip7840::BlobParams}; use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, U256, aliases::U512}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use anstyle::{AnsiColor, Color, Style}; @@ -443,15 +439,7 @@ fn execute_block<'a>( .map_err(|err| TestError::case(path, name, err))?; blob_params = schedule .blob_params(header.number, header.timestamp) - .map_err(|err| TestError::case(path, name, err))? - // The reserve price of EIP-7918 comes from the spec, not from the fixture's schedule. - .map(|params| { - if spec.enables(SpecId::OSAKA) { - params.with_blob_base_cost(BLOB_BASE_COST) - } else { - params - } - }); + .map_err(|err| TestError::case(path, name, err))?; expected_state_root = Some(header.state_root); } // The blob checks multiply the blob counts by the blob gas, which must not overflow. @@ -1685,7 +1673,8 @@ impl ForkSchedule { number: U256, timestamp: U256, ) -> Result, TestErrorKind> { - let Some(blob_fork) = self.stage_at(number, timestamp).blob_fork else { return Ok(None) }; + let stage = self.stage_at(number, timestamp); + let Some(blob_fork) = stage.blob_fork else { return Ok(None) }; let entry = self .blob_schedule .get(&blob_fork) @@ -1693,7 +1682,7 @@ impl ForkSchedule { if entry.base_fee_update_fraction.is_zero() { return Err(TestErrorKind::ZeroBlobUpdateFraction(blob_fork)); } - Ok(Some(entry.params())) + Ok(Some(entry.params(stage.spec))) } /// Returns whether any spec of the fork is skipped. @@ -1820,6 +1809,9 @@ mod tests { super::types::{ChainConfig, SealEngine, State}, *, }; + use alloy_eips::eip7840::BLOB_BASE_COST; + use std::collections::BTreeMap; + #[cfg(feature = "jit")] use crate::tx::recover_address; #[cfg(feature = "jit")] @@ -1828,7 +1820,6 @@ mod tests { use alloy_primitives::{TxKind, to_eip155_v}; #[cfg(feature = "jit")] use evm2::interpreter::op; - use std::collections::BTreeMap; #[cfg(feature = "jit")] const BYTECODE_STORE42: &[u8] = &[op::PUSH1, 0x42, op::PUSH0, op::SSTORE, op::STOP]; @@ -1876,6 +1867,23 @@ mod tests { assert_eq!(blob_params(15_000).update_fraction, BlobParams::bpo2().update_fraction); } + #[test] + fn blob_params_carry_the_reserve_price_from_osaka() { + let config = |fork, entry| ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([(fork, BlobScheduleEntry::from(entry))]), + }; + let header = BlockHeader::default(); + let osaka_config = config(BlobFork::Osaka, BlobParams::osaka()); + let osaka = test_case(ForkSpec::Osaka, Some(osaka_config)); + let params = blob_params(Path::new("test.json"), "test", &osaka, &header); + assert_eq!(params.unwrap().unwrap().blob_base_cost, BLOB_BASE_COST); + let prague_config = config(BlobFork::Prague, BlobParams::prague()); + let prague = test_case(ForkSpec::Prague, Some(prague_config)); + let params = blob_params(Path::new("test.json"), "test", &prague, &header); + assert_eq!(params.unwrap().unwrap().blob_base_cost, 0); + } + #[test] fn blob_params_require_a_schedule_entry_from_cancun() { let case = test_case(ForkSpec::Cancun, None); diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index b65464d08..2fc3d61e8 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -1,7 +1,11 @@ use crate::tx::{AccessListItem, TestAuthorization}; use alloy_eip7928::BlockAccessList; -use alloy_eips::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, eip7840::BlobParams}; +use alloy_eips::{ + eip4844::BLOB_TX_MIN_BLOB_GASPRICE, + eip7840::{BLOB_BASE_COST, BlobParams}, +}; use alloy_primitives::{Address, B256, Bytes, FixedBytes, TxKind, U256}; +use evm2::SpecId; use serde::{Deserialize, Deserializer, Serialize, de}; use std::collections::BTreeMap; @@ -414,8 +418,10 @@ fn is_empty_bytes(bytes: &Bytes) -> bool { } impl BlobScheduleEntry { - /// Returns the blob parameters of this entry. - pub fn params(&self) -> BlobParams { + /// Returns the blob parameters of this entry under the spec that is active. + /// + /// The reserve price of EIP-7918 comes from the spec, not from the fixture's schedule. + pub fn params(&self, spec: SpecId) -> BlobParams { let max_blob_count = self.max.saturating_to(); BlobParams { target_blob_count: self.target.saturating_to(), @@ -423,7 +429,7 @@ impl BlobScheduleEntry { update_fraction: self.base_fee_update_fraction.saturating_to(), min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE, max_blobs_per_tx: max_blob_count, - blob_base_cost: 0, + blob_base_cost: if spec.enables(SpecId::OSAKA) { BLOB_BASE_COST } else { 0 }, } } } From dd3bd78ac8c262b927ab05f266aa8a0d9e3ccc42 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:42:47 +0100 Subject: [PATCH 084/159] fix(cli): list each benchmark's fixture path in run --list (#968) `scripts/jit/bench.py` linked each benchmark to its fixture by running a regex over `crates/cli/src/evm_bench.rs`. The regex depended on the catalog's call syntax, and it also matched `Bench::blockchain_replay`, which `evm2 run` cannot run. `evm2 run --list` now prints one `\t` line per benchmark. `get_benches` returns that mapping, and `source_links` builds the links from the binary it measures, so the regex is gone and the list and the links come from one catalog. The only caller of `run --list` is `get_benches`. Against the old regex, the links for all 31 built-in benchmarks are identical; the one difference is the `mainnet_25347446_25347455` replay entry, which the regex produced and no benchmark uses. A base binary built before this change lists names only, so base-side `results.json` has no `source_links` until this merges. --- crates/cli/src/run.rs | 6 ++++-- scripts/jit/bench.py | 17 ++++++----------- scripts/jit/utils.py | 10 ++++++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index ecbab9261..7a9999994 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -34,7 +34,7 @@ pub(crate) struct RunArgs { #[arg(default_value = "1")] n_iters: u64, - /// List available benchmark names and exit. + /// List available benchmarks and exit, as `\t` lines. #[arg(long)] list: bool, @@ -109,7 +109,9 @@ impl RunArgs { pub(crate) fn run(self) -> eyre::Result<()> { if self.list { for bench in get_benches() { - println!("{}", bench.name); + if let BenchSource::Fixture(fixture_path) = bench.source { + println!("{}\t{fixture_path}", bench.name); + } } return Ok(()); } diff --git a/scripts/jit/bench.py b/scripts/jit/bench.py index 264ecadef..cd62a2555 100755 --- a/scripts/jit/bench.py +++ b/scripts/jit/bench.py @@ -1227,20 +1227,15 @@ def base_binary(root: str, sha: str, base_dump: str) -> str: } ARTIFACT_FILES = ["bytecode.txt", "bytecode.dot", "opt.ll", "opt.s"] TARGET_CPU_RE = re.compile(r'"target-cpu"="([^"]+)"') -BENCH_FIXTURE_RE = re.compile(r'Bench::\w+\(\s*"([^"]+)",[^"]*"([^"]+)"') -def source_links(root: str, rev: str) -> dict[str, list[dict[str, str]]]: - """Link each built-in benchmark to its fixture in the benchmark catalog.""" - try: - with open(os.path.join(root, "crates/cli/src/evm_bench.rs")) as f: - catalog = f.read() - except FileNotFoundError: - return {} +def source_links(binary: str, rev: str) -> dict[str, list[dict[str, str]]]: + """Link each built-in benchmark to the fixture that ``binary`` lists for it.""" repo = os.environ.get("GITHUB_REPOSITORY", "alloy-rs/evm2") return { name: [{"label": path, "url": f"https://github.com/{repo}/blob/{rev}/{path}"}] - for name, path in BENCH_FIXTURE_RE.findall(catalog) + for name, path in get_benches(binary).items() + if path } @@ -1259,7 +1254,7 @@ def write_results(out_dir: str, benches: list[str], side: Side, perf: bool): """Write ``results.json`` and normalized artifacts for the perf site and ``compare.py``.""" codegen = CodegenLines()._collect(benches, side.dump_dir) times = CompileTime()._collect(benches, side) - links = source_links(side.root, side.rev) + links = source_links(side.binary, side.rev) artifacts = os.path.join(out_dir, "artifacts") if os.path.isdir(artifacts): shutil.rmtree(artifacts) @@ -1457,7 +1452,7 @@ def main(): base_root = os.path.join(base_dump, ".worktree") base = Side("base", base_sha, base_binary(root, base_sha, base_dump), base_dump, base_root) head = Side("head", head_sha, cargo_build(root), args.dump_dir, root) - builtin = args.benches or get_benches(head.binary) + builtin = args.benches or list(get_benches(head.binary)) extra_dirs = ( args.extra_dir if args.extra_dir is not None else ([] if args.benches else ["tmp/mainnet"]) ) diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index 4460c6902..25b8a1639 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -73,14 +73,20 @@ def llvm_env(root: str) -> dict[str, str]: return {"PATH": bindir + os.pathsep + os.environ.get("PATH", "")} -def get_benches(binary: str) -> list[str]: +def get_benches(binary: str) -> dict[str, str]: + """Map each built-in benchmark to its fixture path, as listed by ``run --list``.""" r = subprocess.run( [binary, "run", "--list"], stdout=subprocess.PIPE, text=True, check=True, ) - return [line.strip() for line in r.stdout.splitlines() if line.strip()] + benches = {} + for line in r.stdout.splitlines(): + name, _, path = line.strip().partition("\t") + if name: + benches[name] = path + return benches def run_cli( From b78ed02faf97d963e8ae247568d0473eb295d049 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:42:51 +0100 Subject: [PATCH 085/159] refactor(scripts): share repo_root, cargo_env and pct helpers (#972) `scripts/jit/utils.py` defined its own `repo_root` and `cargo_env`, `setup_test_fixtures.py` recomputed the repository root, and `compare.py` kept `pct` and `geomean_pct` next to the same arithmetic inside `bench.py`'s `fmt_pct` and `fmt_geomean`. The root helpers now live in one module, `scripts/common.py`, which `dump_asm.py`, `fuzzer_coverage.py`, `setup_test_fixtures.py` and the jit scripts import. The jit `cargo_env` extends the shared base environment with `CARGO_TARGET_DIR` and `RUST_LOG`. The module is `common.py` rather than `utils.py` because a script in `scripts/jit` imports `utils` by name, and two modules with that name cannot both be loaded. `pct` and `geomean_pct` move to `scripts/jit/utils.py`, and `fmt_pct` and `fmt_geomean` format their result. Behaviour is unchanged: the five scripts start from `/`, `/tmp`, `scripts` and `scripts/jit`, `fmt_pct`, `fmt_geomean`, `pct` and `geomean_pct` agree with the old code on 2000 random inputs, and `bench.py --diff` against the previous commit on `counter` and `factorial` followed by `compare.py` gives the same report as before. --- .github/workflows/bench-comment.yml | 2 ++ .github/workflows/bench.yml | 8 +++++--- scripts/{utils.py => common.py} | 2 +- scripts/dump_asm.py | 2 +- scripts/fuzzer_coverage.py | 2 +- scripts/jit/bench.py | 14 ++++++-------- scripts/jit/compare.py | 15 +-------------- scripts/jit/utils.py | 26 +++++++++++++++++++++----- scripts/setup_test_fixtures.py | 5 +++-- 9 files changed, 41 insertions(+), 35 deletions(-) rename scripts/{utils.py => common.py} (78%) diff --git a/.github/workflows/bench-comment.yml b/.github/workflows/bench-comment.yml index 571ecd5c8..89658f9ac 100644 --- a/.github/workflows/bench-comment.yml +++ b/.github/workflows/bench-comment.yml @@ -45,6 +45,8 @@ jobs: ".github/workflows/bench.yml", "scripts/jit/bench.py", "scripts/jit/compare.py", + "scripts/jit/utils.py", + "scripts/common.py", ]; let behind = false; try { diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 605737c21..40d230864 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -68,7 +68,7 @@ jobs: fi # The CLI reads fixtures from its source tree, so check out the old code in place and - # keep only this ref's benchmark scripts. + # keep only this ref's benchmark scripts and the helpers they share. - name: Check out backfill commit if: inputs.commit != '' env: @@ -83,10 +83,12 @@ jobs: sudo .github/scripts/install_llvm_ubuntu.sh "$llvm" dev fi scripts="$RUNNER_TEMP/jit-scripts" - cp -R scripts/jit "$scripts" + mkdir "$scripts" + cp -R scripts/common.py scripts/jit "$scripts" git checkout --quiet --detach "$COMMIT" rm -rf scripts/jit - cp -R "$scripts" scripts/jit + cp -R "$scripts/jit" scripts/jit + cp "$scripts/common.py" scripts/common.py - name: Resolve and download exact baseline id: baseline diff --git a/scripts/utils.py b/scripts/common.py similarity index 78% rename from scripts/utils.py rename to scripts/common.py index c6c20fd1a..a04ac31a4 100644 --- a/scripts/utils.py +++ b/scripts/common.py @@ -5,7 +5,7 @@ def repo_root() -> str: """Return the repository root.""" - return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) def cargo_env() -> dict[str, str]: diff --git a/scripts/dump_asm.py b/scripts/dump_asm.py index 88680602c..a39731813 100755 --- a/scripts/dump_asm.py +++ b/scripts/dump_asm.py @@ -27,7 +27,7 @@ from rich.highlighter import NullHighlighter from rich.logging import RichHandler -from utils import cargo_env, repo_root +from common import cargo_env, repo_root ROOT = Path(repo_root()) OPCODE_RS = ROOT / "crates" / "evm2" / "src" / "interpreter" / "opcode.rs" diff --git a/scripts/fuzzer_coverage.py b/scripts/fuzzer_coverage.py index 09e0573c1..9c340efb3 100755 --- a/scripts/fuzzer_coverage.py +++ b/scripts/fuzzer_coverage.py @@ -19,7 +19,7 @@ import sys from pathlib import Path -from utils import cargo_env, repo_root +from common import cargo_env, repo_root ROOT = Path(repo_root()) DEFAULT_OUTPUT_DIR = ROOT / "target" / "llvm-cov" / "fuzzer-html" diff --git a/scripts/jit/bench.py b/scripts/jit/bench.py index cd62a2555..0d56b1713 100755 --- a/scripts/jit/bench.py +++ b/scripts/jit/bench.py @@ -51,7 +51,9 @@ cargo_build, cargo_env, eprint, + geomean_pct, get_benches, + pct, perf_available, perf_counts, repo_root, @@ -138,17 +140,13 @@ def fmt_rel(pct: float) -> str: def fmt_pct(base: float | int, current: float | int) -> str: - if base == 0: - return "-" - return fmt_rel((current - base) / base * 100) + change = pct(base, current) + return "-" if change is None else fmt_rel(change) def fmt_geomean(pairs: list[tuple[float, float]]) -> str: - """Geometric mean of the current/base ratios, so every benchmark weighs the same.""" - ratios = [c / b for b, c in pairs if b > 0 and c > 0] - if not ratios: - return "-" - return fmt_rel((statistics.geometric_mean(ratios) - 1) * 100) + change = geomean_pct(pairs) + return "-" if change is None else fmt_rel(change) def fmt_detail(current: str | int, base: str | int, diff: str) -> str: diff --git a/scripts/jit/compare.py b/scripts/jit/compare.py index a4f61d082..b41dee52a 100644 --- a/scripts/jit/compare.py +++ b/scripts/jit/compare.py @@ -20,7 +20,6 @@ import json import os import re -import statistics import subprocess import sys import uuid @@ -46,6 +45,7 @@ runtime_changed, time_changed, ) +from utils import geomean_pct, pct PERF_SITE_URL = "https://www.getfoundry.sh/perf/evm2/" # Metrics whose changes are deterministic and decide whether a benchmark changed. @@ -130,19 +130,6 @@ def artifact_hashes(document: dict, test_id: str) -> dict[str, str]: } -def pct(before, after) -> float | None: - if before is None or after is None or before == 0: - return None - return (after - before) / before * 100 - - -def geomean_pct(pairs: list[tuple]) -> float | None: - ratios = [a / b for b, a in pairs if b and a and b > 0 and a > 0] - if not ratios: - return None - return (statistics.geometric_mean(ratios) - 1) * 100 - - def fmt_change(value: float | None) -> str: if value is None: return "n/a" diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index 25b8a1639..94b8dfc25 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -4,11 +4,16 @@ import os import re import shutil +import statistics import subprocess import sys import tempfile import time +# `utils` names this module when a jit script runs, so the shared helpers live in `common`. +sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from common import cargo_env as base_cargo_env, repo_root # noqa: E402 + ANSI_RE = re.compile(r"\x1b\[[0-9;]*m") @@ -20,22 +25,33 @@ def strip_ansi(s: str) -> str: return ANSI_RE.sub("", s) -def repo_root() -> str: - return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - - def _shared_target_dir() -> str: """Return a shared CARGO_TARGET_DIR so worktrees reuse the same build cache.""" return os.path.join(repo_root(), "target") def cargo_env(rust_log: str | None = None) -> dict[str, str]: - env = {**os.environ, "NO_COLOR": "1", "CARGO_TARGET_DIR": _shared_target_dir()} + env = {**base_cargo_env(), "CARGO_TARGET_DIR": _shared_target_dir()} if rust_log: env["RUST_LOG"] = rust_log return env +def pct(before: float | None, after: float | None) -> float | None: + """Percent change from ``before`` to ``after``, or ``None`` when there is no baseline.""" + if before is None or after is None or before == 0: + return None + return (after - before) / before * 100 + + +def geomean_pct(pairs: list[tuple[float, float]]) -> float | None: + """Geometric mean of the after/before ratios as a percent change, equal weight per pair.""" + ratios = [a / b for b, a in pairs if b and a and b > 0 and a > 0] + if not ratios: + return None + return (statistics.geometric_mean(ratios) - 1) * 100 + + def cargo_build(root: str, target_dir: str | None = None) -> str: """Build the CLI binary and return its path. diff --git a/scripts/setup_test_fixtures.py b/scripts/setup_test_fixtures.py index f9ff8786e..da89a0633 100755 --- a/scripts/setup_test_fixtures.py +++ b/scripts/setup_test_fixtures.py @@ -33,6 +33,8 @@ ) from rich.text import Text +from common import repo_root + MAIN_VERSION = os.environ.get("MAIN_VERSION", "v5.3.0") # ethereum/legacytests commit pinned by the `LegacyTests` submodule of ethereum/tests v17.2. The # ethereum/tests archives do not include submodules, so the tests are fetched from the submodule's @@ -50,8 +52,7 @@ DEFAULT_EEST_TAR = "fixtures.tar.gz" LEGACY_URL = f"https://github.com/ethereum/legacytests/archive/{LEGACY_VERSION}.tar.gz" -SCRIPT_DIR = Path(__file__).resolve().parent -REPO_ROOT = SCRIPT_DIR.parent +REPO_ROOT = Path(repo_root()) FIXTURES_DIR = Path(os.environ.get("EVM2_TEST_FIXTURES", REPO_ROOT / "test-fixtures")) MAIN_STABLE_DIR = FIXTURES_DIR / "main" / "stable" From 8a0ea86ebd880c2e4c900abf76c508ebc38a273f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:52:43 +0100 Subject: [PATCH 086/159] refactor(cli): derive spec name and fork tables from one definition each (#970) One SpecId name table (SpecId::name and FromStr), one ForkSpec/SpecId conversion pair in eest, and one reference spec shared by the fuzzer, replay_bench and the bench. Unknown names no longer fall back silently, and run --spec-id latest means SpecId::NEXT. --- crates/cli/benches/evm/revm.rs | 27 +------ crates/cli/src/capture/error.rs | 2 + crates/cli/src/capture/export.rs | 25 +------ crates/cli/src/capture/mod.rs | 2 +- crates/cli/src/ethereum.rs | 67 +++++++++-------- crates/cli/src/fuzzer/backend.rs | 29 +------- crates/cli/src/fuzzer/case.rs | 46 +----------- crates/cli/src/lib.rs | 1 + crates/cli/src/reference.rs | 21 ++++++ crates/cli/src/replay_bench.rs | 58 ++------------- crates/cli/src/run.rs | 73 ++++--------------- crates/eest/src/blockchaintest/execute.rs | 25 +------ crates/eest/src/blockchaintest/types.rs | 89 +++++++++++++++++++++++ crates/eest/src/forks.rs | 28 +------ crates/evm2/src/lib.rs | 2 +- crates/evm2/src/spec_id.rs | 54 ++++++++++++++ 16 files changed, 239 insertions(+), 310 deletions(-) create mode 100644 crates/cli/src/reference.rs diff --git a/crates/cli/benches/evm/revm.rs b/crates/cli/benches/evm/revm.rs index 3929e158e..da65088a8 100644 --- a/crates/cli/benches/evm/revm.rs +++ b/crates/cli/benches/evm/revm.rs @@ -1,7 +1,7 @@ use crate::fixture::Suites; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::SpecId; -use evm2_cli::evm_bench::BenchCase; +use evm2_cli::{evm_bench::BenchCase, reference}; use revm::{ ExecuteEvm, MainBuilder, MainContext, context::{BlockEnv, CfgEnv, Context, TxEnv}, @@ -29,7 +29,7 @@ impl PreparedBench { let suite = suites.get(bench.fixture_path); let (unit, test) = revm_case(suite.input(), &bench.name, spec); let tx = test.tx_env(&unit).expect("revm benchmark transaction must build"); - let (cfg, block) = envs(&unit, revm_spec_id(spec)); + let (cfg, block) = envs(&unit, reference::spec_id(spec)); let db = Arc::new(database(&unit)); Self { name: bench.name.clone(), cfg, block, db, tx } } @@ -104,7 +104,7 @@ fn revm_case(input: &str, name: &str, spec: SpecId) -> (TestUnit, Test) { let tests = unit .post .iter_mut() - .find(|(spec_name, _)| spec_name.to_spec_id() == revm_spec_id(spec)) + .find(|(spec_name, _)| spec_name.to_spec_id() == reference::spec_id(spec)) .map(|(_, tests)| tests) .expect("fixture suite must contain revm post test"); let test = tests.remove(0); @@ -145,24 +145,3 @@ fn database(unit: &TestUnit) -> InMemoryDB { } db } - -fn revm_spec_id(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - _ => panic!("unsupported benchmark spec: {spec:?}"), - } -} diff --git a/crates/cli/src/capture/error.rs b/crates/cli/src/capture/error.rs index 1e5b622a7..5daede9aa 100644 --- a/crates/cli/src/capture/error.rs +++ b/crates/cli/src/capture/error.rs @@ -47,6 +47,8 @@ pub(crate) enum CaptureError { MultipleSpecs, #[error("capture uses unsupported spec id {0}")] UnsupportedSpec(u32), + #[error("block timestamp {timestamp} has no supported mainnet hardfork")] + UnsupportedHardfork { timestamp: u64 }, #[error("capture has conflicting bytecode for code hash {code_hash}")] CodeHashCollision { code_hash: B256 }, #[error( diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index ad3f2217d..7538605dc 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -94,7 +94,7 @@ fn blob_fork(blocks: &[CapturedBlock]) -> Result, CaptureError> } fn mainnet_blob_fork(header: &Header) -> Result, CaptureError> { - let spec = mainnet_spec_for_header(header); + let spec = mainnet_spec_for_header(header)?; if !spec.enables(SpecId::CANCUN) { return Ok(None); } @@ -136,28 +136,7 @@ fn network(capture: &CapturedCase, blob_fork: Option) -> Result Ok(ForkSpec::Frontier), - SpecId::HOMESTEAD => Ok(ForkSpec::Homestead), - SpecId::TANGERINE => Ok(ForkSpec::EIP150), - SpecId::SPURIOUS_DRAGON => Ok(ForkSpec::EIP158), - SpecId::BYZANTIUM => Ok(ForkSpec::Byzantium), - SpecId::PETERSBURG => Ok(ForkSpec::ConstantinopleFix), - SpecId::ISTANBUL => Ok(ForkSpec::Istanbul), - SpecId::BERLIN => Ok(ForkSpec::Berlin), - SpecId::LONDON => Ok(ForkSpec::London), - SpecId::MERGE => Ok(ForkSpec::Paris), - SpecId::SHANGHAI => Ok(ForkSpec::Shanghai), - SpecId::CANCUN => Ok(ForkSpec::Cancun), - SpecId::PRAGUE => Ok(ForkSpec::Prague), - SpecId::OSAKA => Ok(match blob_fork { - Some(BlobFork::Bpo1) => ForkSpec::BPO1, - Some(BlobFork::Bpo2) => ForkSpec::BPO2, - _ => ForkSpec::Osaka, - }), - SpecId::AMSTERDAM => Ok(ForkSpec::Amsterdam), - _ => Err(CaptureError::UnsupportedSpec(first.spec_id)), - } + ForkSpec::from_spec_id(spec, blob_fork).ok_or(CaptureError::UnsupportedSpec(first.spec_id)) } fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { diff --git a/crates/cli/src/capture/mod.rs b/crates/cli/src/capture/mod.rs index 6eb880faa..720ad77f6 100644 --- a/crates/cli/src/capture/mod.rs +++ b/crates/cli/src/capture/mod.rs @@ -122,7 +122,7 @@ async fn capture( } overlay.apply_withdrawals(&consensus_block); - let spec = ethereum::mainnet_spec_for_header(&consensus_block.header); + let spec = ethereum::mainnet_spec_for_header(&consensus_block.header)?; let _version_index = builder.version_index(spec)?; let block_transaction_count = transactions.len(); transaction_count += block_transaction_count; diff --git a/crates/cli/src/ethereum.rs b/crates/cli/src/ethereum.rs index ccf2584dc..db7156bdc 100644 --- a/crates/cli/src/ethereum.rs +++ b/crates/cli/src/ethereum.rs @@ -1,43 +1,42 @@ +use crate::capture::CaptureError; use alloy_consensus::Header; use alloy_hardforks::EthereumHardfork; use evm2::SpecId; -pub(crate) fn mainnet_spec_for_header(header: &Header) -> SpecId { - spec_for_hardfork(EthereumHardfork::from_chain_and_timestamp( +pub(crate) fn mainnet_spec_for_header(header: &Header) -> Result { + let hardfork = EthereumHardfork::from_chain_and_timestamp( alloy_chains::Chain::mainnet(), header.timestamp, - )) + ); + hardfork + .and_then(spec_for_hardfork) + .ok_or(CaptureError::UnsupportedHardfork { timestamp: header.timestamp }) } -const fn spec_for_hardfork(hardfork: Option) -> SpecId { - match hardfork { - Some(EthereumHardfork::Frontier) => SpecId::FRONTIER, - Some(EthereumHardfork::Homestead | EthereumHardfork::Dao) => SpecId::HOMESTEAD, - Some(EthereumHardfork::Tangerine) => SpecId::TANGERINE, - Some(EthereumHardfork::SpuriousDragon) => SpecId::SPURIOUS_DRAGON, - Some(EthereumHardfork::Byzantium | EthereumHardfork::Constantinople) => SpecId::BYZANTIUM, - Some(EthereumHardfork::Petersburg) => SpecId::PETERSBURG, - Some(EthereumHardfork::Istanbul | EthereumHardfork::MuirGlacier) => SpecId::ISTANBUL, - Some(EthereumHardfork::Berlin) => SpecId::BERLIN, - Some( - EthereumHardfork::London - | EthereumHardfork::ArrowGlacier - | EthereumHardfork::GrayGlacier, - ) => SpecId::LONDON, - Some(EthereumHardfork::Paris) => SpecId::MERGE, - Some(EthereumHardfork::Shanghai) => SpecId::SHANGHAI, - Some(EthereumHardfork::Cancun) => SpecId::CANCUN, - Some(EthereumHardfork::Prague) => SpecId::PRAGUE, - Some( - EthereumHardfork::Osaka - | EthereumHardfork::Bpo1 - | EthereumHardfork::Bpo2 - | EthereumHardfork::Bpo3 - | EthereumHardfork::Bpo4 - | EthereumHardfork::Bpo5, - ) => SpecId::OSAKA, - Some(EthereumHardfork::Amsterdam) => SpecId::AMSTERDAM, - Some(_) => SpecId::AMSTERDAM, - None => SpecId::FRONTIER, - } +const fn spec_for_hardfork(hardfork: EthereumHardfork) -> Option { + Some(match hardfork { + EthereumHardfork::Frontier => SpecId::FRONTIER, + EthereumHardfork::Homestead | EthereumHardfork::Dao => SpecId::HOMESTEAD, + EthereumHardfork::Tangerine => SpecId::TANGERINE, + EthereumHardfork::SpuriousDragon => SpecId::SPURIOUS_DRAGON, + EthereumHardfork::Byzantium | EthereumHardfork::Constantinople => SpecId::BYZANTIUM, + EthereumHardfork::Petersburg => SpecId::PETERSBURG, + EthereumHardfork::Istanbul | EthereumHardfork::MuirGlacier => SpecId::ISTANBUL, + EthereumHardfork::Berlin => SpecId::BERLIN, + EthereumHardfork::London + | EthereumHardfork::ArrowGlacier + | EthereumHardfork::GrayGlacier => SpecId::LONDON, + EthereumHardfork::Paris => SpecId::MERGE, + EthereumHardfork::Shanghai => SpecId::SHANGHAI, + EthereumHardfork::Cancun => SpecId::CANCUN, + EthereumHardfork::Prague => SpecId::PRAGUE, + EthereumHardfork::Osaka + | EthereumHardfork::Bpo1 + | EthereumHardfork::Bpo2 + | EthereumHardfork::Bpo3 + | EthereumHardfork::Bpo4 + | EthereumHardfork::Bpo5 => SpecId::OSAKA, + EthereumHardfork::Amsterdam => SpecId::AMSTERDAM, + _ => return None, + }) } diff --git a/crates/cli/src/fuzzer/backend.rs b/crates/cli/src/fuzzer/backend.rs index 38c73ca88..89099a91f 100644 --- a/crates/cli/src/fuzzer/backend.rs +++ b/crates/cli/src/fuzzer/backend.rs @@ -7,18 +7,18 @@ use crate::fuzzer::{ }; use alloy_primitives::{Address, map::HashMap}; use evm2::{ - BaseEvmConfigSelector, BaseEvmTypes, Evm, EvmConfigSelector, Precompiles, SpecId, + BaseEvmConfigSelector, BaseEvmTypes, Evm, EvmConfigSelector, Precompiles, bytecode::Bytecode, ethereum::ethereum_tx_registry, evm::{AccountInfo as Evm2AccountInfo, InMemoryDB}, interpreter::InstrStop, }; +use evm2_cli::reference; use revm::{ ExecuteCommitEvm, ExecuteEvm, MainBuilder, MainContext, context::{CfgEnv, Context}, context_interface::either::Either, database::{EmptyDB as RevmEmptyDB, InMemoryDB as RevmInMemoryDB, State as RevmState}, - primitives::hardfork::SpecId as RevmSpecId, }; pub(crate) trait EvmBackend { @@ -109,7 +109,7 @@ impl EvmBackend for RevmBackend { fn run(&mut self, case: &EvmCase) -> Outcome { let mut cfg = CfgEnv::new(); - cfg.set_spec_and_mainnet_gas_params(revm_spec(case.spec)); + cfg.set_spec_and_mainnet_gas_params(reference::spec_id(case.spec)); cfg = cfg.disable_tx_chain_id_check(); let context = Context::mainnet() .with_cfg(cfg) @@ -118,7 +118,7 @@ impl EvmBackend for RevmBackend { let evm = if let Some(evm) = &mut self.evm { evm.ctx = context; evm.instruction = revm::handler::instructions::EthInstructions::new_mainnet_with_spec( - revm_spec(case.spec), + reference::spec_id(case.spec), ); evm } else { @@ -202,27 +202,6 @@ fn revm_db(case: &EvmCase) -> RevmInMemoryDB { db } -const fn revm_spec(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - _ => RevmSpecId::CANCUN, - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 48e5fd103..3646a2d18 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -750,7 +750,7 @@ mod spec_serde { where S: Serializer, { - serializer.serialize_str(name(*spec)) + serializer.serialize_str(spec.name()) } pub(super) fn deserialize<'de, D>(deserializer: D) -> Result @@ -758,49 +758,7 @@ mod spec_serde { D: Deserializer<'de>, { let name = String::deserialize(deserializer)?; - from_name(&name).ok_or_else(|| de::Error::custom(format!("unknown spec id {name:?}"))) - } - - const fn name(spec: SpecId) -> &'static str { - match spec { - SpecId::FRONTIER => "FRONTIER", - SpecId::HOMESTEAD => "HOMESTEAD", - SpecId::TANGERINE => "TANGERINE", - SpecId::SPURIOUS_DRAGON => "SPURIOUS_DRAGON", - SpecId::BYZANTIUM => "BYZANTIUM", - SpecId::PETERSBURG => "PETERSBURG", - SpecId::ISTANBUL => "ISTANBUL", - SpecId::BERLIN => "BERLIN", - SpecId::LONDON => "LONDON", - SpecId::MERGE => "MERGE", - SpecId::SHANGHAI => "SHANGHAI", - SpecId::CANCUN => "CANCUN", - SpecId::PRAGUE => "PRAGUE", - SpecId::OSAKA => "OSAKA", - SpecId::AMSTERDAM => "AMSTERDAM", - _ => "CANCUN", - } - } - - fn from_name(name: &str) -> Option { - match name { - "FRONTIER" => Some(SpecId::FRONTIER), - "HOMESTEAD" => Some(SpecId::HOMESTEAD), - "TANGERINE" => Some(SpecId::TANGERINE), - "SPURIOUS_DRAGON" => Some(SpecId::SPURIOUS_DRAGON), - "BYZANTIUM" => Some(SpecId::BYZANTIUM), - "PETERSBURG" => Some(SpecId::PETERSBURG), - "ISTANBUL" => Some(SpecId::ISTANBUL), - "BERLIN" => Some(SpecId::BERLIN), - "LONDON" => Some(SpecId::LONDON), - "MERGE" => Some(SpecId::MERGE), - "SHANGHAI" => Some(SpecId::SHANGHAI), - "CANCUN" => Some(SpecId::CANCUN), - "PRAGUE" => Some(SpecId::PRAGUE), - "OSAKA" => Some(SpecId::OSAKA), - "AMSTERDAM" => Some(SpecId::AMSTERDAM), - _ => None, - } + name.parse().map_err(|_| de::Error::custom(format!("unknown spec id {name:?}"))) } } diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 5660bd8ee..ad69a90b1 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -2,4 +2,5 @@ #[allow(missing_docs)] pub mod evm_bench; +pub mod reference; pub mod replay_bench; diff --git a/crates/cli/src/reference.rs b/crates/cli/src/reference.rs new file mode 100644 index 000000000..5630fc2b7 --- /dev/null +++ b/crates/cli/src/reference.rs @@ -0,0 +1,21 @@ +//! Conversions to the reference implementation the fuzzer and replay benchmarks compare against. + +use evm2::SpecId; +use revm::primitives::hardfork::SpecId as ReferenceSpecId; + +/// Returns the reference implementation's spec ID for `spec`. +/// +/// # Panics +/// +/// Panics when the reference implementation has no counterpart of `spec`. +pub fn spec_id(spec: SpecId) -> ReferenceSpecId { + macro_rules! convert { + ([$spec:ident] $($variant:ident $lower:ident,)*) => { + match $spec { + $(SpecId::$variant => ReferenceSpecId::$variant,)* + other => panic!("unsupported spec: {other:?}"), + } + }; + } + evm2::for_each_spec!([spec] convert) +} diff --git a/crates/cli/src/replay_bench.rs b/crates/cli/src/replay_bench.rs index b5ab75799..9cefd6f1e 100644 --- a/crates/cli/src/replay_bench.rs +++ b/crates/cli/src/replay_bench.rs @@ -4,6 +4,7 @@ //! transaction outcomes; timed runs use a no-op hook and include database setup, //! transaction decoding, execution and block commits. +use crate::reference; use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, Keccak256, Log, TxKind, U256}; use alloy_rpc_types_eth::{AccessList as RpcAccessList, AccessListItem as RpcAccessListItem}; @@ -18,8 +19,8 @@ use evm2_eest::{ BlockchainTestExecuteConfig, NameFilter, blockchaintest::{ Block, BlockFinished, BlockHeader, BlockStarted, BlockchainTest, BlockchainTestCase, - CaseStarted, ForkSpec, Hook, Transaction, TransactionFinished, TransactionStarted, - Withdrawal, blob_params, + CaseStarted, Hook, Transaction, TransactionFinished, TransactionStarted, Withdrawal, + blob_params, }, execute_blockchain_tests_suite, }; @@ -29,7 +30,6 @@ use revm::{ context_interface::{Cfg, block::BlobExcessGasAndPrice, either::Either}, database::{CacheDB, EmptyDB, InMemoryDB as RevmInMemoryDB}, handler::EvmTr, - primitives::hardfork::SpecId as RevmSpecId, state::{AccountInfo as RevmAccountInfo, Bytecode as RevmBytecode}, }; use std::path::Path; @@ -239,7 +239,10 @@ impl ReplayFixture { /// final block's hash. Pre-merge forks and block access lists are unsupported. pub fn from_case(name: String, case: BlockchainTestCase) -> Self { assert_canonical(&name, &case); - let spec = fork_to_spec_id(case.network); + let spec = case + .network + .to_spec_id() + .unwrap_or_else(|| panic!("replay fixture fork {:?} is not supported", case.network)); assert!(spec.enables(SpecId::MERGE), "replay corpus must be post-merge"); Self { name: name.clone(), spec, suite: BlockchainTest([(name, case)].into()) } } @@ -313,7 +316,7 @@ impl ReplayFixture { pub fn execute_revm_on(&self, database: &mut RevmInMemoryDB, hook: &mut dyn Hook) { let case = self.case(); let spec = self.spec; - let revm_spec = revm_spec_id(spec); + let revm_spec = reference::spec_id(spec); let mut cfg = CfgEnv::new(); cfg.set_spec_and_mainnet_gas_params(revm_spec); @@ -553,51 +556,6 @@ fn block_withdrawals(block: &Block) -> &[Withdrawal] { block.rlp_decoded.as_ref().map(|decoded| decoded.withdrawals.as_slice()).unwrap_or_default() } -fn fork_to_spec_id(fork: ForkSpec) -> SpecId { - match fork { - ForkSpec::Frontier => SpecId::FRONTIER, - ForkSpec::Homestead => SpecId::HOMESTEAD, - ForkSpec::EIP150 => SpecId::TANGERINE, - ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON, - ForkSpec::Byzantium => SpecId::BYZANTIUM, - ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => SpecId::PETERSBURG, - ForkSpec::Istanbul => SpecId::ISTANBUL, - ForkSpec::Berlin => SpecId::BERLIN, - ForkSpec::London => SpecId::LONDON, - ForkSpec::Paris - | ForkSpec::MergeEOF - | ForkSpec::MergeMeterInitCode - | ForkSpec::MergePush0 => SpecId::MERGE, - ForkSpec::Shanghai => SpecId::SHANGHAI, - ForkSpec::Cancun => SpecId::CANCUN, - ForkSpec::Prague => SpecId::PRAGUE, - ForkSpec::Osaka | ForkSpec::BPO1 | ForkSpec::BPO2 => SpecId::OSAKA, - ForkSpec::Amsterdam => SpecId::AMSTERDAM, - other => panic!("replay fixture fork {other:?} is not supported"), - } -} - -fn revm_spec_id(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - other => panic!("unsupported replay spec: {other:?}"), - } -} - fn authorization_list(raw: &Transaction) -> Option> { let authorizations = raw.authorization_list.as_deref()?; Some( diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 7a9999994..c77b82902 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -83,8 +83,9 @@ pub(crate) struct RunArgs { out_dir: Option, #[arg(short = 'O', long, default_value = "2")] opt_level: OptimizationLevel, - #[arg(long, value_enum, default_value = "osaka")] - spec_id: SpecIdValueEnum, + /// Spec to run: a fork name such as `osaka`, or `latest` for the newest one. + #[arg(long, value_parser = parse_spec_id, default_value = SpecId::DEFAULT.name())] + spec_id: SpecId, #[arg(long)] debug_assertions: bool, #[arg(long)] @@ -119,7 +120,7 @@ impl RunArgs { let Some(bench_name) = self.bench_name.clone() else { eyre::bail!("missing ; use `--list` to see available benchmarks"); }; - let spec_id = self.spec_id.into(); + let spec_id = self.spec_id; let calldata = self .calldata .as_deref() @@ -425,61 +426,11 @@ fn perf_report(n_iters: u64) { writeln!(report, "{n_iters}").expect("failed to write the perf report"); } -#[derive(Clone, Copy, Debug, ValueEnum)] -#[clap(rename_all = "lowercase")] -#[allow(non_camel_case_types, clippy::upper_case_acronyms)] -enum SpecIdValueEnum { - FRONTIER, - FRONTIER_THAWING, - HOMESTEAD, - DAO_FORK, - TANGERINE, - SPURIOUS_DRAGON, - BYZANTIUM, - CONSTANTINOPLE, - PETERSBURG, - ISTANBUL, - MUIR_GLACIER, - BERLIN, - LONDON, - ARROW_GLACIER, - GRAY_GLACIER, - MERGE, - SHANGHAI, - CANCUN, - PRAGUE, - OSAKA, - AMSTERDAM, - LATEST, -} - -impl From for SpecId { - fn from(value: SpecIdValueEnum) -> Self { - match value { - SpecIdValueEnum::FRONTIER => Self::FRONTIER, - SpecIdValueEnum::FRONTIER_THAWING => Self::FRONTIER, - SpecIdValueEnum::HOMESTEAD => Self::HOMESTEAD, - SpecIdValueEnum::DAO_FORK => Self::HOMESTEAD, - SpecIdValueEnum::TANGERINE => Self::TANGERINE, - SpecIdValueEnum::SPURIOUS_DRAGON => Self::SPURIOUS_DRAGON, - SpecIdValueEnum::BYZANTIUM => Self::BYZANTIUM, - SpecIdValueEnum::CONSTANTINOPLE => Self::PETERSBURG, - SpecIdValueEnum::PETERSBURG => Self::PETERSBURG, - SpecIdValueEnum::ISTANBUL => Self::ISTANBUL, - SpecIdValueEnum::MUIR_GLACIER => Self::ISTANBUL, - SpecIdValueEnum::BERLIN => Self::BERLIN, - SpecIdValueEnum::LONDON => Self::LONDON, - SpecIdValueEnum::ARROW_GLACIER => Self::LONDON, - SpecIdValueEnum::GRAY_GLACIER => Self::LONDON, - SpecIdValueEnum::MERGE => Self::MERGE, - SpecIdValueEnum::SHANGHAI => Self::SHANGHAI, - SpecIdValueEnum::CANCUN => Self::CANCUN, - SpecIdValueEnum::PRAGUE => Self::PRAGUE, - SpecIdValueEnum::OSAKA => Self::OSAKA, - SpecIdValueEnum::AMSTERDAM => Self::AMSTERDAM, - SpecIdValueEnum::LATEST => Self::OSAKA, - } +fn parse_spec_id(name: &str) -> Result { + if name.eq_ignore_ascii_case("latest") { + return Ok(SpecId::NEXT); } + name.parse().map_err(|_| format!("unknown spec id {name:?}")) } #[derive(Clone, Debug)] @@ -908,7 +859,7 @@ fn read_code_string(contents: &[u8], ext: Option<&str>) -> eyre::Result> #[cfg(test)] mod tests { - use super::{SpecId, SpecIdValueEnum, read_code_string}; + use super::{SpecId, parse_spec_id, read_code_string}; use crate::args::{Args, Command}; use clap::Parser; @@ -918,8 +869,10 @@ mod tests { } #[test] - fn latest_spec_matches_default_run_spec() { - assert_eq!(SpecId::from(SpecIdValueEnum::LATEST), SpecId::OSAKA); + fn spec_id_parser_accepts_names_and_latest() { + assert_eq!(parse_spec_id("osaka"), Ok(SpecId::OSAKA)); + assert_eq!(parse_spec_id("LATEST"), Ok(SpecId::NEXT)); + assert!(parse_spec_id("constantinople").is_err()); } #[test] diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index ce167a933..a4571e509 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -1730,28 +1730,8 @@ const fn fork_schedule(fork: ForkSpec) -> Option { const AT_15K: Activation = Timestamp(15_000); Some(match fork { - ForkSpec::Frontier => ForkSchedule::fixed(SpecId::FRONTIER), - ForkSpec::Homestead => ForkSchedule::fixed(SpecId::HOMESTEAD), - ForkSpec::EIP150 => ForkSchedule::fixed(SpecId::TANGERINE), - ForkSpec::EIP158 => ForkSchedule::fixed(SpecId::SPURIOUS_DRAGON), - ForkSpec::Byzantium => ForkSchedule::fixed(SpecId::BYZANTIUM), - ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => { - ForkSchedule::fixed(SpecId::PETERSBURG) - } - ForkSpec::Istanbul => ForkSchedule::fixed(SpecId::ISTANBUL), - ForkSpec::Berlin => ForkSchedule::fixed(SpecId::BERLIN), - ForkSpec::London => ForkSchedule::fixed(SpecId::LONDON), - ForkSpec::Paris - | ForkSpec::MergeEOF - | ForkSpec::MergeMeterInitCode - | ForkSpec::MergePush0 => ForkSchedule::fixed(SpecId::MERGE), - ForkSpec::Shanghai => ForkSchedule::fixed(SpecId::SHANGHAI), - ForkSpec::Cancun => ForkSchedule::fixed(SpecId::CANCUN), - ForkSpec::Prague => ForkSchedule::fixed(SpecId::PRAGUE), - ForkSpec::Osaka => ForkSchedule::fixed(SpecId::OSAKA), ForkSpec::BPO1 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo1), None), ForkSpec::BPO2 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo2), None), - ForkSpec::Amsterdam => ForkSchedule::fixed(SpecId::AMSTERDAM), ForkSpec::FrontierToHomesteadAt5 => { ForkSchedule::transition(SpecId::FRONTIER, AT_5, SpecId::HOMESTEAD) } @@ -1799,7 +1779,10 @@ const fn fork_schedule(fork: ForkSpec) -> Option { ForkStage::bpo(BlobFork::Bpo2), Some((AT_15K, ForkStage::new(SpecId::AMSTERDAM))), ), - ForkSpec::HomesteadToDaoAt5 => return None, + fixed => match fixed.to_spec_id() { + Some(spec) => ForkSchedule::fixed(spec), + None => return None, + }, }) } diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 2fc3d61e8..60ef98be7 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -337,6 +337,76 @@ pub enum ForkSpec { Amsterdam, } +impl ForkSpec { + /// Returns the spec ID a fork runs under, or `None` for a transition fork, which runs under + /// several specs, and for the DAO fork. + pub const fn to_spec_id(self) -> Option { + Some(match self { + Self::Frontier => SpecId::FRONTIER, + Self::Homestead => SpecId::HOMESTEAD, + Self::EIP150 => SpecId::TANGERINE, + Self::EIP158 => SpecId::SPURIOUS_DRAGON, + Self::Byzantium => SpecId::BYZANTIUM, + Self::Constantinople | Self::ConstantinopleFix => SpecId::PETERSBURG, + Self::Istanbul => SpecId::ISTANBUL, + Self::Berlin => SpecId::BERLIN, + Self::London => SpecId::LONDON, + Self::Paris | Self::MergeEOF | Self::MergeMeterInitCode | Self::MergePush0 => { + SpecId::MERGE + } + Self::Shanghai => SpecId::SHANGHAI, + Self::Cancun => SpecId::CANCUN, + Self::Prague => SpecId::PRAGUE, + Self::Osaka | Self::BPO1 | Self::BPO2 => SpecId::OSAKA, + Self::Amsterdam => SpecId::AMSTERDAM, + Self::FrontierToHomesteadAt5 + | Self::HomesteadToDaoAt5 + | Self::HomesteadToEIP150At5 + | Self::EIP158ToByzantiumAt5 + | Self::ByzantiumToConstantinopleAt5 + | Self::ByzantiumToConstantinopleFixAt5 + | Self::BerlinToLondonAt5 + | Self::ParisToShanghaiAtTime15k + | Self::ShanghaiToCancunAtTime15k + | Self::CancunToPragueAtTime15k + | Self::PragueToOsakaAtTime15k + | Self::OsakaToBPO1AtTime15k + | Self::BPO1ToBPO2AtTime15k + | Self::BPO2ToBPO3AtTime15k + | Self::BPO3ToBPO4AtTime15k + | Self::BPO2ToAmsterdamAtTime15k => return None, + }) + } + + /// Returns the fork that runs under `spec`, or `None` if no fork does. + /// + /// Osaka maps to the BPO fork that sets `blob_fork`, which only changes the blob parameters. + pub const fn from_spec_id(spec: SpecId, blob_fork: Option) -> Option { + Some(match spec { + SpecId::FRONTIER => Self::Frontier, + SpecId::HOMESTEAD => Self::Homestead, + SpecId::TANGERINE => Self::EIP150, + SpecId::SPURIOUS_DRAGON => Self::EIP158, + SpecId::BYZANTIUM => Self::Byzantium, + SpecId::PETERSBURG => Self::ConstantinopleFix, + SpecId::ISTANBUL => Self::Istanbul, + SpecId::BERLIN => Self::Berlin, + SpecId::LONDON => Self::London, + SpecId::MERGE => Self::Paris, + SpecId::SHANGHAI => Self::Shanghai, + SpecId::CANCUN => Self::Cancun, + SpecId::PRAGUE => Self::Prague, + SpecId::OSAKA => match blob_fork { + Some(BlobFork::Bpo1) => Self::BPO1, + Some(BlobFork::Bpo2) => Self::BPO2, + _ => Self::Osaka, + }, + SpecId::AMSTERDAM => Self::Amsterdam, + _ => return None, + }) + } +} + /// Possible seal engines. #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] pub enum SealEngine { @@ -455,3 +525,22 @@ impl Transaction { self.to.map_or(TxKind::Create, TxKind::Call) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_spec_id_maps_to_a_fork_that_runs_under_it() { + for value in 0..SpecId::COUNT as u32 { + let spec = SpecId::try_from_u32(value).unwrap(); + let fork = ForkSpec::from_spec_id(spec, None).unwrap(); + assert_eq!(fork.to_spec_id(), Some(spec)); + } + assert_eq!( + ForkSpec::from_spec_id(SpecId::OSAKA, Some(BlobFork::Bpo2)), + Some(ForkSpec::BPO2) + ); + assert_eq!(ForkSpec::BPO2ToAmsterdamAtTime15k.to_spec_id(), None); + } +} diff --git a/crates/eest/src/forks.rs b/crates/eest/src/forks.rs index f89d07f8e..d6c0fb342 100644 --- a/crates/eest/src/forks.rs +++ b/crates/eest/src/forks.rs @@ -17,7 +17,7 @@ fn skipped_forks() -> &'static [SpecId] { let mut forks = Vec::new(); if let Ok(names) = env::var(SKIP_FORKS_ENV) { for name in names.split(',').map(str::trim).filter(|name| !name.is_empty()) { - let Some(spec) = parse_fork(name) else { + let Ok(spec) = name.parse() else { panic!("{SKIP_FORKS_ENV}: unknown hardfork name `{name}`"); }; forks.push(spec); @@ -26,29 +26,3 @@ fn skipped_forks() -> &'static [SpecId] { forks }) } - -/// Parses a case-insensitive hardfork name into a spec ID. -fn parse_fork(name: &str) -> Option { - macro_rules! parse { - ([$name:ident] $($spec:ident $lower:ident,)*) => { - match $name.to_ascii_lowercase().as_str() { - $(stringify!($lower) => Some(SpecId::$spec),)* - _ => None, - } - }; - } - evm2::for_each_spec!([name] parse) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn parses_fork_names() { - assert_eq!(parse_fork("amsterdam"), Some(SpecId::AMSTERDAM)); - assert_eq!(parse_fork("Osaka"), Some(SpecId::OSAKA)); - assert_eq!(parse_fork("SPURIOUS_DRAGON"), Some(SpecId::SPURIOUS_DRAGON)); - assert_eq!(parse_fork("atlantis"), None); - } -} diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index f32e35036..6b46af6ec 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -47,7 +47,7 @@ pub mod version; pub use version::{EvmFeatures, OpcodeChange, OpcodeConfig, OpcodeView, Version}; mod spec_id; -pub use spec_id::SpecId; +pub use spec_id::{ParseSpecIdError, SpecId}; mod once_lock; mod storage_key; diff --git a/crates/evm2/src/spec_id.rs b/crates/evm2/src/spec_id.rs index 02fa4731a..f4e6f7846 100644 --- a/crates/evm2/src/spec_id.rs +++ b/crates/evm2/src/spec_id.rs @@ -1,3 +1,5 @@ +use core::{fmt, str::FromStr}; + /// Specification IDs and their activation points. /// /// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs). @@ -119,6 +121,47 @@ impl SpecId { } } +macro_rules! spec_names { + ([] $($spec:ident $lower:ident,)*) => { + impl SpecId { + /// Returns the upper-case variant name, such as `"SPURIOUS_DRAGON"`. + pub const fn name(self) -> &'static str { + match self { + $(Self::$spec => stringify!($spec),)* + } + } + } + + impl FromStr for SpecId { + type Err = ParseSpecIdError; + + /// Parses a variant name, ignoring ASCII case. + fn from_str(s: &str) -> Result { + $( + if s.eq_ignore_ascii_case(stringify!($spec)) { + return Ok(Self::$spec); + } + )* + Err(ParseSpecIdError) + } + } + }; +} + +crate::for_each_spec!([] spec_names); + +/// Error returned when parsing a [`SpecId`] from an unknown name. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ParseSpecIdError; + +impl fmt::Display for ParseSpecIdError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("unknown spec id") + } +} + +impl core::error::Error for ParseSpecIdError {} + impl From for u32 { #[inline] fn from(spec_id: SpecId) -> Self { @@ -193,4 +236,15 @@ mod tests { fn defaults() { assert_eq!(SpecId::DEFAULT, SpecId::default()); } + + #[test] + fn names_round_trip() { + for value in 0..SpecId::COUNT as u32 { + let spec = SpecId::try_from_u32(value).unwrap(); + assert_eq!(spec.name().parse(), Ok(spec)); + assert_eq!(spec.name().to_ascii_lowercase().parse(), Ok(spec)); + } + assert_eq!("SPURIOUS_DRAGON".parse(), Ok(SpecId::SPURIOUS_DRAGON)); + assert_eq!("CONSTANTINOPLE".parse::(), Err(ParseSpecIdError)); + } } From f688d777acc8b2db1eb646cbef3531c8f2073b07 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:55:14 +0100 Subject: [PATCH 087/159] refactor(inspectors): add a dedicated erc7562 inspector (#936) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The erc7562Tracer was built from `TracingInspector` steps: every instruction of the transaction recorded the full stack, memory and storage change, and the result builder rebuilt geth's counters from those snapshots. That is slow, and it does not match geth. Code sizes came from the state before the transaction, the used-opcode counts ignored geth's ignore list and its `GAS` and `EXTCODESIZE` rules, and keccak preimages were listed per frame and dropped for failed steps, where geth lists one sorted set on the root. `Erc7562Inspector` follows geth's tracer instead. A call-only `TracingInspector` records the call tree, with its inputs, outputs and logs. Each instruction updates the counters of the frame that runs it in `step`, reading the stack, memory, code and storage as the instruction is about to run, like geth's `OnOpcode`. A step that fails is counted like any other. Code sizes and slot values are read without warming accounts or slots. A frame that fails at a `KECCAK256` still leaves its preimage, which can be 1 MiB of zero padding for a few hundred gas, so the inspector bounds the bytes of the preimages it retains (64 MiB by default, which geth does not do). `frame()` fails when it reaches that limit, or when a read of the state failed, instead of reporting a wrong preimage set, code size or slot value. Against go-ethereum `a0cba163`, the harness (`evm2 t8n` with the `erc7562Tracer`, with and without `withLog`) shows: | cases | differences, main | differences, this PR | | :-- | --: | --: | | 92 harness cases, all forks | 4545 | 199 🟢 | | new `erc7562_ops` case (opcodes, slots, code sizes, a frame that fails after `EXTCODESIZE`, one that fails at `KECCAK256`) | 232 | 0 🟢 | What main got wrong, by field: used-opcode counts (3574), code sizes (570), `outOfGas` (140), keccak preimages (46), accessed slots (22) and `extCodeAccessInfo` (6). All of these are gone. The 199 that remain are not from this tracer. Error text accounts for 120 and the `outOfGas` of a frame whose halt evm2 reports differently from geth for 18; both come from the frame status that the call tracer shares. Five are the index of a log. The last 56 are the `SELFDESTRUCT` child frame that geth emits: `CallFrameType` in alloy has no such variant, so the frame cannot be printed yet. An explicit `ignoredOpcodes: []` also reads as the default list, because alloy's `Erc7562Config` cannot tell an empty list from none. The harness case and tracer entries are not in this PR, because the harness is not on main. Two tests cover the counting rules through the public `DebugInspector`, and one covers the code size of an account that only the accepted state holds. The counting test fails on main. Cost, `DebugInspector` with the erc7562 tracer on the `data/` fixtures (minimum of two criterion runs on one box; the no-inspector time is the same on both builds): | fixture | main | this PR | change | | :-- | --: | --: | --: | | usdt | 36.7 µs | 7.3 µs | -80.0% 🟢 | | weth | 16.3 µs | 4.2 µs | -74.1% 🟢 | | universal_router | 494.8 µs | 87.4 µs | -82.3% 🟢 | | aggregation_router_v6 | 1.4 ms | 286.8 µs | -80.0% 🟢 | | entry_point_v0_7 | 1.1 ms | 207.1 µs | -80.8% 🟢 | | uniswap_v3_pool | 368.5 µs | 35.1 µs | -90.5% 🟢 | | curve_3pool | 421.4 µs | 49.6 µs | -88.2% 🟢 | | balancer_vault | 618.7 µs | 85.8 µs | -86.1% 🟢 | | snailtracer | out of memory (47.7 GB, killed) | 141.7 ms, 34.5 MB peak | 🟢 | The interpreter, the frame hooks and the no-inspector path are untouched. Related: #915 records the code size at the opcode in the step recording that this PR removes. --- crates/inspectors/src/tracing/builder/geth.rs | 200 +------- .../src/tracing/builder/geth/steps_tests.rs | 86 +--- crates/inspectors/src/tracing/config.rs | 23 +- crates/inspectors/src/tracing/debug.rs | 37 +- crates/inspectors/src/tracing/erc7562.rs | 437 ++++++++++++++++++ crates/inspectors/src/tracing/keccak.rs | 2 +- crates/inspectors/src/tracing/mod.rs | 3 + crates/inspectors/tests/it/geth.rs | 432 +++++++++++++++-- 8 files changed, 871 insertions(+), 349 deletions(-) create mode 100644 crates/inspectors/src/tracing/erc7562.rs diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index bb5636a5a..6ec676256 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -4,20 +4,18 @@ use crate::tracing::{ types::{CallKind, CallTraceNode, CallTraceStepStackItem, StorageChange}, utils::load_account_code, }; -use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, format, vec, vec::Vec}; +use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, vec, vec::Vec}; use alloy_primitives::{ Address, B256, Bytes, U256, - map::{AddressSet, Entry, HashMap}, + map::{AddressSet, HashMap}, }; use alloy_rpc_types_trace::geth::{ AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, GethDefaultTracingOptions, - PreStateConfig, PreStateFrame, PreStateMode, StructLog, - erc7562::{AccessedSlots, CallFrameType, ContractSize, Erc7562Config, Erc7562Frame}, + PreStateConfig, PreStateFrame, PreStateMode, StructLog, erc7562::CallFrameType, }; use evm2::{ EvmFeatures, EvmTypesHost, TxResultExt, TxResultWithState, evm::{DbResult, DynDatabase}, - interpreter::{InstrStop, op}, }; /// The smallest size in bytes of the JSON of a struct log, @@ -82,11 +80,11 @@ fn struct_log_json_len(log: &StructLog) -> usize { } /// A frame awaiting tree assembly, with visibility inherited by children in the forward pass. -struct PendingCallFrame { +struct PendingCallFrame { /// Whether this call and its ancestors permit descendant logs. Independent of whether /// the frame itself contains logs. logs_visible: bool, - frame: T, + frame: CallFrame, } #[cfg(test)] @@ -551,176 +549,6 @@ impl<'a> GethTraceBuilder<'a> { pre.retain(|addr, _pre_state| !empty_before.contains(addr)); } - /// Traces ERC-7562 calls using the call tracer. - pub fn geth_erc7562_traces( - &self, - opts: Erc7562Config, - gas_used: u64, - db: &mut dyn DynDatabase, - ) -> DbResult { - if self.nodes.is_empty() { - return Ok(Default::default()); - } - - let include_logs = opts.with_log.unwrap_or_default(); - // only the root frame's own fields are needed, its children are assembled below - let mut top_call = { - let root_logs_visible = include_logs && !self.nodes[0].trace.is_error(); - let mut root = self.nodes[0].geth_empty_call_frame(root_logs_visible); - root.gas_used = U256::from(gas_used); - Some(root) - }; - - let mut frames: Vec> = Vec::with_capacity(self.nodes.len()); - - for (idx, node) in self.nodes.iter().enumerate() { - let trace = &node.trace; - - let mut accessed_slots = AccessedSlots::default(); - let mut used_opcodes = HashMap::default(); - let mut contract_size = HashMap::default(); - let mut ext_code_access_info = Vec::new(); - let mut keccak = Vec::new(); - let mut out_of_gas = trace.status.is_some_and(InstrStop::is_out_of_gas); - - for step in &trace.steps { - let op = step.op.get(); - out_of_gas |= step.status.is_some_and(InstrStop::is_out_of_gas); - - // Skip if opcode is ignored - if opts.ignored_opcodes.contains(&op) { - continue; - } - - // Count used opcodes - *used_opcodes.entry(op).or_insert(0) += 1; - - // Accessed storage slots - match op { - op::SLOAD => { - // geth records the first read of each slot, unless the opcode fails before - // it runs. - if !step.is_error() - && let Some(change) = &step.storage_change - && let slot = B256::from(change.key) - && !accessed_slots.reads.contains_key(&slot) - && !accessed_slots.writes.contains_key(&slot) - { - accessed_slots.reads.entry(slot).or_default().push(change.value.into()); - } - } - op::SSTORE => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.writes.entry(slot).or_insert(0) += 1; - } - } - op::TLOAD => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.transient_reads.entry(slot).or_insert(0) += 1; - } - } - op::TSTORE => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.transient_writes.entry(slot).or_insert(0) += 1; - } - } - _ => {} - } - - if matches!(op, op::EXTCODESIZE | op::EXTCODECOPY | op::EXTCODEHASH) - && let Some(stack) = &step.stack - && let Some(item) = stack.get(stack.len().saturating_sub(1)) - { - let address = Address::from_word((*item).into()); - ext_code_access_info.push(format!("{address:?}")); - if let Entry::Vacant(e) = contract_size.entry(address) - && let Some(account) = db.get_account(&address)? - && let Some(code) = load_account_code(db, &account)? - { - e.insert(ContractSize { contract_size: code.len() as u64, opcode: op }); - } - } - - // KECCAK preimages from returndata - if op == op::KECCAK256 - && step.status.is_none() - && let (Some(stack), Some(memory)) = (&step.stack, &step.memory) - && stack.len() >= 2 - { - let offset = stack[stack.len() - 1]; - let len = stack[stack.len() - 2]; - if let (Ok(offset), Ok(len)) = (usize::try_from(offset), usize::try_from(len)) { - let mut data = vec![0; len]; - if offset < memory.0.len() { - let end = (offset + len).min(memory.0.len()); - let copy_len = end - offset; - data[..copy_len].copy_from_slice(&memory.0[offset..end]); - } - keccak.push(Bytes::from(data)); - } - } - } - - let include_logs = include_logs - && !trace.is_error() - && node.parent.is_none_or(|parent| frames[parent].logs_visible); - let call_frame = if idx == 0 { - top_call.take().unwrap() - } else { - self.nodes[idx].geth_empty_call_frame(include_logs) - }; - - let call_frame_type = Self::convert_call_kind(node.kind()); - - frames.push(PendingCallFrame { - logs_visible: include_logs, - frame: Erc7562Frame { - call_frame_type, - from: call_frame.from, - gas: call_frame.gas.to(), - gas_used: call_frame.gas_used.to(), - to: call_frame.to, - input: call_frame.input, - output: call_frame.output, - error: call_frame.error, - revert_reason: call_frame.revert_reason, - logs: call_frame.logs, - value: call_frame.value, - accessed_slots, - ext_code_access_info, - used_opcodes, - contract_size, - out_of_gas, - keccak, - calls: vec![], - }, - }); - } - - // Assemble tree, see `geth_call_traces_inner` for the ordering logic - loop { - let PendingCallFrame { mut frame, .. } = frames.pop().expect("call frames not empty"); - let idx = frames.len(); - frame.calls.reverse(); - let node = &self.nodes[idx]; - if let Some(parent) = node.parent { - frames[parent].frame.calls.push(frame); - } else { - debug_assert!(frames.is_empty(), "only one root node has no parent"); - return Ok(frame); - } - } - } - /// Converts a CallKind to a CallFrameType. pub const fn convert_call_kind(kind: CallKind) -> CallFrameType { match kind { @@ -747,7 +575,7 @@ mod tests { AccountInfo, DatabaseError, bytecode::Bytecode, evm::{CacheDB, DbResult, DynDatabase, EmptyDB}, - interpreter::{InstrStop, Word}, + interpreter::{InstrStop, Word, op}, }; struct FailingDb { @@ -834,20 +662,11 @@ mod tests { CallConfig { with_log: Some(with_log), ..Default::default() }, 0, ); - let erc = builder - .geth_erc7562_traces( - Erc7562Config { with_log: Some(with_log), ..Default::default() }, - 0, - &mut EmptyDB::default(), - ) - .unwrap(); - let mut pending = vec![(&call, &erc)]; + let mut pending = vec![&call]; let mut visited = Vec::new(); - while let Some((call, erc)) = pending.pop() { + while let Some(call) = pending.pop() { let idx = call.to.unwrap().as_slice()[19] as usize; visited.push(idx); - assert_eq!(call.to, erc.to); - assert_eq!(call.logs, erc.logs); let visible = with_log && !root_failed && (idx == 0 || idx >= 5); assert_eq!(call.logs.len(), if visible { 2 } else { 0 }, "node {idx}"); if visible { @@ -857,8 +676,7 @@ mod tests { assert_eq!(log.data, Some(Bytes::from(vec![idx as u8, log_idx as u8]))); } } - assert_eq!(call.calls.len(), erc.calls.len()); - pending.extend(call.calls.iter().zip(&erc.calls).rev()); + pending.extend(call.calls.iter().rev()); } assert_eq!(visited, [0, 1, 2, 3, 4, 5, 6]); } diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index 635df19e9..615fed436 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -3,10 +3,7 @@ use crate::tracing::types::{ CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, TraceMemberOrder, }; use alloc::string::String; -use evm2::{ - evm::EmptyDB, - interpreter::{InstrStop, opcode::OpCode}, -}; +use evm2::interpreter::{InstrStop, op, opcode::OpCode}; fn test_step(pc: usize, op: u8) -> CallTraceStep { CallTraceStep { @@ -71,87 +68,6 @@ fn struct_log_omits_zero_refund_and_empty_memory() { assert_eq!(log.memory.map(|memory| memory.len()), Some(1)); } -#[test] -fn erc7562_keccak_preimages_require_successful_steps() { - for status in [ - None, - Some(InstrStop::OutOfGas), - Some(InstrStop::MemoryOOG), - Some(InstrStop::MemoryLimitOOG), - Some(InstrStop::PrecompileOOG), - Some(InstrStop::InvalidOperandOOG), - Some(InstrStop::ReentrancySentryOOG), - Some(InstrStop::StackUnderflow), - Some(InstrStop::FatalExternalError), - ] { - for (offset, len, expected) in - [(0, 0, vec![]), (1, 2, vec![2, 3]), (1, 4, vec![2, 3, 0, 0]), (4, 2, vec![0, 0])] - { - let mut step = test_step(0, op::KECCAK256); - step.stack = Some(vec![U256::from(len), U256::from(offset)].into_boxed_slice()); - step.memory = Some(RecordedMemory::new(&[1, 2, 3])); - step.status = status; - let builder = GethTraceBuilder::new(vec![CallTraceNode { - trace: CallTrace { - steps: vec![step, test_step(1, op::REVERT)], - status: Some(InstrStop::Revert), - ..Default::default() - }, - ..Default::default() - }]); - let frame = builder - .geth_erc7562_traces(Erc7562Config::default(), 0, &mut EmptyDB::default()) - .unwrap(); - let expected = if status.is_none() { vec![Bytes::from(expected)] } else { vec![] }; - assert_eq!(frame.keccak, expected, "status {status:?}, offset {offset}, len {len}"); - } - } -} - -#[test] -fn erc7562_classifies_all_out_of_gas_statuses() { - for (status, expected) in [ - (None, false), - (Some(InstrStop::Stop), false), - (Some(InstrStop::Return), false), - (Some(InstrStop::Revert), false), - (Some(InstrStop::OutOfFunds), false), - (Some(InstrStop::OpcodeNotFound), false), - (Some(InstrStop::OutOfGas), true), - (Some(InstrStop::MemoryOOG), true), - (Some(InstrStop::MemoryLimitOOG), true), - (Some(InstrStop::PrecompileOOG), true), - (Some(InstrStop::InvalidOperandOOG), true), - (Some(InstrStop::ReentrancySentryOOG), true), - ] { - assert_eq!(status.is_some_and(InstrStop::is_out_of_gas), expected); - for frame_status in [false, true] { - let mut step = test_step(0, op::KECCAK256); - step.status = status; - let builder = GethTraceBuilder::new(vec![CallTraceNode { - trace: CallTrace { - status: if frame_status { status } else { None }, - steps: if frame_status { vec![] } else { vec![step] }, - ..Default::default() - }, - ..Default::default() - }]); - for ignored in [false, true] { - let mut config = Erc7562Config::default(); - if ignored { - config.ignored_opcodes.push(op::KECCAK256); - } - let frame = - builder.geth_erc7562_traces(config, 0, &mut EmptyDB::default()).unwrap(); - assert_eq!( - frame.out_of_gas, expected, - "status {status:?}, frame_status {frame_status}, ignored {ignored}" - ); - } - } - } -} - #[test] fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { // Cover all call-like opcodes, a precompile with no steps, an empty creation, diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 1db883705..58d5d6042 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -1,10 +1,7 @@ use crate::tracing::builder::geth::MIN_STRUCT_LOG_JSON_LEN; use alloy_primitives::{U256, map::HashSet}; use alloy_rpc_types_trace::{ - geth::{ - CallConfig, FlatCallConfig, GethDefaultTracingOptions, PreStateConfig, - erc7562::Erc7562Config, - }, + geth::{CallConfig, FlatCallConfig, GethDefaultTracingOptions, PreStateConfig}, parity::TraceType, }; use core::num::NonZeroU64; @@ -252,23 +249,6 @@ impl TracingInspectorConfig { .set_record_logs(config.with_log.unwrap_or_default()) } - /// Returns a config for geth's - /// [Erc7562Frame](alloy_rpc_types_trace::geth::erc7562::Erc7562Frame). - #[inline] - pub fn from_geth_erc7562_config(config: &Erc7562Config) -> Self { - Self::none() - .set_record_inputs(true) - // call tracer is similar parity tracer with optional support for logs - .set_record_logs(config.with_log.unwrap_or_default()) - // need memory snapshots for keccak preimages - .set_memory_snapshots(true) - // need stack snapshots for keccak preimages - .set_stack_snapshots(StackSnapshotType::Full) - // need state diffs to capture SLOAD values in accessedSlots.reads - .set_state_diffs(true) - .steps() - } - /// Returns a config for geth's /// [FlatCallTracer](alloy_rpc_types_trace::geth::call::FlatCallFrame). /// @@ -543,7 +523,6 @@ mod tests { TracingInspectorConfig::all(), TracingInspectorConfig::default_parity(), TracingInspectorConfig::from_geth_call_config(&CallConfig::default()), - TracingInspectorConfig::from_geth_erc7562_config(&Erc7562Config::default()), TracingInspectorConfig::from_flat_call_config(&FlatCallConfig::default()), ] { assert!(config.record_inputs); diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index e246a42d1..72fef5107 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -1,6 +1,7 @@ use crate::tracing::{ - FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MuxInspector, - TracingInspector, TracingInspectorConfig, TransactionContext, utils::transaction_info, + Erc7562Inspector, FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, + MuxInspector, TracingInspector, TracingInspectorConfig, TransactionContext, + utils::transaction_info, }; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; @@ -46,7 +47,7 @@ pub enum DebugInspector { /// FlatCallTracer FlatCallTracer(TracingInspector), /// Erc7562Tracer - Erc7562Tracer(TracingInspector, Erc7562Config), + Erc7562Tracer(Erc7562Inspector), /// Keccak256PreimageTracer Keccak256Preimage(Keccak256PreimageInspector), /// Default tracer @@ -69,9 +70,7 @@ impl DebugInspector { Self::StateGasTracer(inspector) => Self::StateGasTracer(inspector.clone()), Self::Mux(inspector, config) => Self::Mux(inspector.try_clone()?, config.clone()), Self::FlatCallTracer(inspector) => Self::FlatCallTracer(inspector.clone()), - Self::Erc7562Tracer(inspector, config) => { - Self::Erc7562Tracer(inspector.clone(), config.clone()) - } + Self::Erc7562Tracer(inspector) => Self::Erc7562Tracer(inspector.clone()), Self::Keccak256Preimage(inspector) => Self::Keccak256Preimage(inspector.clone()), Self::Default(inspector, config) => Self::Default(inspector.clone(), *config), #[cfg(feature = "js-tracer")] @@ -148,12 +147,7 @@ impl DebugInspector { .map_err(|_| DebugInspectorError::InvalidTracerConfig)? }; - Self::Erc7562Tracer( - TracingInspector::new( - TracingInspectorConfig::from_geth_erc7562_config(&config), - ), - config, - ) + Self::Erc7562Tracer(Erc7562Inspector::new(config)) } _ => { // Note: this match is non-exhaustive in case we need to add support for @@ -200,8 +194,8 @@ impl DebugInspector { Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) | Self::FlatCallTracer(inspector) - | Self::Erc7562Tracer(inspector, _) | Self::Default(inspector, _) => inspector.fuse(), + Self::Erc7562Tracer(inspector) => inspector.fuse(), Self::Keccak256Preimage(inspector) => inspector.clear(), Self::Noop(_) | Self::StateGasTracer(_) => {} Self::Mux(inspector, _) => inspector.fuse()?, @@ -230,8 +224,8 @@ impl DebugInspector { Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) | Self::FlatCallTracer(inspector) - | Self::Erc7562Tracer(inspector, _) | Self::Default(inspector, _) => inspector.set_next_log_index(index), + Self::Erc7562Tracer(inspector) => inspector.set_next_log_index(index), Self::Mux(inspector, _) => inspector.set_next_log_index(index), _ => {} } @@ -296,15 +290,7 @@ impl DebugInspector { .into_localized_transaction_traces(tx_info) .into() } - Self::Erc7562Tracer(inspector, config) => { - inspector.set_transaction_gas_limit(tx.gas_limit()); - inspector.set_transaction_caller(tx.signer()); - inspector - .geth_builder() - .geth_erc7562_traces(config.clone(), res.result.tx_gas_used(), db) - .map_err(DebugInspectorError::Database)? - .into() - } + Self::Erc7562Tracer(inspector) => inspector.frame(tx, &res.result)?.into(), Self::Keccak256Preimage(inspector) => GethTrace::JS(inspector.json_result()?), Self::Default(inspector, config) => { inspector.set_transaction_gas_limit(tx.gas_limit()); @@ -355,7 +341,7 @@ macro_rules! delegate { Self::CallTracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::PreStateTracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::FlatCallTracer($insp) => Inspector::::$method($insp, $($arg),*), - Self::Erc7562Tracer($insp, _) => Inspector::::$method($insp, $($arg),*), + Self::Erc7562Tracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Default($insp, _) => Inspector::::$method($insp, $($arg),*), Self::Keccak256Preimage($insp) => Inspector::::$method($insp, $($arg),*), Self::Noop($insp) => Inspector::::$method($insp, $($arg),*), @@ -447,6 +433,9 @@ pub enum DebugInspectorError { /// The keccak256 preimage tracer reached its limit of recorded bytes #[error(transparent)] Keccak256Preimage(#[from] crate::tracing::Keccak256PreimageLimitExceeded), + /// The erc7562 tracer reached its limit of recorded preimage bytes or failed to read the state + #[error(transparent)] + Erc7562(#[from] crate::tracing::Erc7562Error), /// Error from JS inspector #[cfg(feature = "js-tracer")] #[error(transparent)] diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs new file mode 100644 index 000000000..752b9a7b7 --- /dev/null +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -0,0 +1,437 @@ +//! ERC-7562 tracing inspector +//! +//! The erc7562Tracer reports a call tree in which every frame also lists what its code did: the +//! opcodes it ran, the storage slots it accessed and the code of other accounts it inspected. The +//! root frame lists the preimages of every `KECCAK256` of the transaction. +//! +//! This inspector follows geth's erc7562Tracer. The call tree, with its inputs, outputs and logs, +//! comes from a [`TracingInspector`] that records calls only. Each instruction updates the +//! counters of the frame that runs it as the instruction is about to execute, reading the stack, +//! the memory and the state like geth does. A step that fails counts like any other. +//! +//! A frame that fails at a `KECCAK256` still leaves its preimage, and a preimage can reach 1 MiB of +//! zero padding for a few hundred gas, so the [`TraceLimits`] of the inspector bound the bytes of +//! the preimages it retains, which geth does not. [`DEFAULT_MAX_RECORDED_BYTES`] is the default. +//! +//! See also + +use crate::tracing::{ + DEFAULT_MAX_RECORDED_BYTES, GethTraceBuilder, TraceLimits, TracingInspector, + TracingInspectorConfig, keccak::memory_copy_padded, limits::TraceBudget, + utils::load_account_code, +}; +use alloc::{borrow::Cow, collections::BTreeSet, format, vec::Vec}; +use alloy_consensus::{Transaction, transaction::Recovered}; +use alloy_primitives::{ + Address, B256, Bytes, Log, U256, + map::{Entry, HashMap}, +}; +use alloy_rpc_types_trace::geth::{ + CallConfig, + erc7562::{AccessedSlots, ContractSize, Erc7562Config, Erc7562Frame}, +}; +use evm2::{ + DatabaseError, EvmTypes, Inspector, TxResultExt, + evm::DbResult, + interpreter::{InstrStop, Interpreter, Message, MessageResult, opcode::op}, +}; +use thiserror::Error; + +/// The result of an [`Erc7562Inspector`] would be wrong. +#[derive(Clone, Debug, Error)] +pub enum Error { + /// The inspector reached its byte limit, so the result would miss preimages. + #[error("erc7562Tracer reached its limit of recorded preimage bytes")] + LimitExceeded, + /// The inspector could not read the state that an instruction addressed, so a code size or a + /// slot value would be wrong. + #[error("database error {0:?}")] + Database(DatabaseError), +} + +/// Inspector that collects what geth's `erc7562Tracer` reports for a transaction. +#[derive(Clone, Debug)] +pub struct Erc7562Inspector { + /// Whether the result lists logs. + with_log: bool, + /// The opcodes that the used opcode counts leave out, one bit per opcode. + ignored: U256, + /// Records the call tree. + calls: TracingInspector, + /// What each call did, indexed like the nodes of `calls`. + frames: Vec, + /// The preimages of the `KECCAK256` operations of the transaction, sorted. + keccak: BTreeSet, + /// The bytes of the preimages. + budget: TraceBudget, + /// The first error of a read of the state, which the result reports. + error: Option, + /// The instruction that ran last, in any frame. + last: Option, +} + +/// What a call did. +#[derive(Clone, Debug, Default)] +struct FrameCounters { + /// The storage slots the call accessed. + accessed_slots: AccessedSlots, + /// The accounts whose code an `EXTCODE*` instruction inspected, in order. + ext_code_access_info: Vec
, + /// How often the call ran each opcode that is not ignored. + used_opcodes: HashMap, + /// The code size of the accounts that `EXTCODE*` and call instructions addressed. + contract_size: HashMap, +} + +/// The instruction that ran last. +#[derive(Clone, Copy, Debug)] +struct LastStep { + /// The opcode. + op: u8, + /// The top of the stack before the instruction, if the stack had an item. + top: Option, +} + +impl Erc7562Inspector { + /// Returns an inspector that collects the result of the given tracer config. + pub fn new(config: Erc7562Config) -> Self { + let with_log = config.with_log.unwrap_or_default(); + let ignored_opcodes = if config.ignored_opcodes.is_empty() { + (op::PUSH0..=op::SWAP16).chain(DEFAULT_IGNORED_OPCODES).collect() + } else { + config.ignored_opcodes + }; + let mut ignored = U256::ZERO; + for op in ignored_opcodes { + ignored.set_bit(usize::from(op), true); + } + let calls = + TracingInspector::new(TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(with_log), + ..Default::default() + })); + let limits = + TraceLimits::default().set_max_recorded_bytes(Some(DEFAULT_MAX_RECORDED_BYTES)); + Self { + with_log, + ignored, + calls, + frames: Vec::new(), + keccak: BTreeSet::new(), + budget: TraceBudget { limits, ..Default::default() }, + error: None, + last: None, + } + } + + /// Bounds the bytes of the preimages that the inspector retains by the given limits, which + /// default to [`DEFAULT_MAX_RECORDED_BYTES`]. + pub const fn with_limits(mut self, limits: TraceLimits) -> Self { + self.budget.limits = limits; + self + } + + /// Returns whether a preimage was omitted after reaching the byte limit. + pub const fn limit_exceeded(&self) -> bool { + self.budget.exceeded() + } + + /// Clears what the inspector collected, to trace the next transaction. + pub fn fuse(&mut self) { + self.calls.fuse(); + self.frames.clear(); + self.keccak.clear(); + self.budget.recorded = 0; + self.budget.exceeded = false; + self.error = None; + self.last = None; + } + + /// Sets the index the next recorded log receives, see + /// [`TracingInspector::set_next_log_index`]. + pub const fn set_next_log_index(&mut self, index: usize) { + self.calls.set_next_log_index(index); + } + + /// Returns the result of the tracer for a transaction that ran with this inspector. + /// + /// Fails if the byte limit was reached, because the result would miss preimages, or if a read + /// of the state failed, because a code size or a slot value would be wrong. + pub fn frame( + &mut self, + tx: &Recovered, + result: &TxResultExt, + ) -> Result { + if self.budget.exceeded() { + return Err(Error::LimitExceeded); + } + if let Some(error) = &self.error { + return Err(Error::Database(error.clone())); + } + self.calls.recover_runtime_oog_root(tx, result); + self.calls.set_transaction_gas_limit(tx.gas_limit()); + self.calls.set_transaction_caller(tx.signer()); + let nodes = self.calls.traces.nodes(); + + // Parents precede their children, so a frame knows whether its parent shows logs. + let mut frames = Vec::<(bool, Erc7562Frame)>::with_capacity(nodes.len()); + for (idx, node) in nodes.iter().enumerate() { + let counters = self.frames.get(idx).cloned().unwrap_or_default(); + let logs_visible = self.with_log + && !node.trace.is_error() + && node.parent.is_none_or(|parent| frames[parent].0); + let call = node.geth_empty_call_frame(logs_visible); + let frame = Erc7562Frame { + call_frame_type: GethTraceBuilder::convert_call_kind(node.kind()), + from: call.from, + gas: call.gas.to(), + gas_used: if idx == 0 { result.tx_gas_used() } else { call.gas_used.to() }, + to: call.to, + input: call.input, + output: call.output, + error: call.error, + revert_reason: call.revert_reason, + logs: call.logs, + value: call.value, + accessed_slots: counters.accessed_slots, + ext_code_access_info: counters + .ext_code_access_info + .iter() + .map(|address| format!("{address:?}")) + .collect(), + used_opcodes: counters.used_opcodes, + contract_size: counters.contract_size, + // geth reports it for frames that exit with an out of gas error, not for the root. + out_of_gas: idx != 0 && node.trace.status.is_some_and(InstrStop::is_out_of_gas), + keccak: if idx == 0 { self.keccak.iter().cloned().collect() } else { Vec::new() }, + calls: Vec::new(), + }; + frames.push((logs_visible, frame)); + } + + // Children are attached from the back, which reverses their order. + loop { + let (_, mut frame) = frames.pop().expect("the root frame exists"); + frame.calls.reverse(); + match nodes[frames.len()].parent { + Some(parent) => frames[parent].1.calls.push(frame), + None => return Ok(frame), + } + } + } + + /// Adds the counters of the call a hook started. + fn push_frame(&mut self) { + let idx = self.calls.last_trace_idx(); + if self.frames.len() <= idx { + self.frames.resize_with(idx + 1, FrameCounters::default); + } + } + + /// Returns the data that a `KECCAK256` with these operands hashes, if geth records it. + fn preimage<'a, T: EvmTypes>( + interp: &'a Interpreter<'_, '_, T>, + offset: Option, + length: Option, + ) -> Option> { + // The operands are truncated to 64 bits, and geth fails on a negative one. + let (offset, length) = (offset?.as_limbs()[0], length?.as_limbs()[0]); + i64::try_from(offset).ok()?; + i64::try_from(length).ok()?; + memory_copy_padded(interp.memory().as_slice(), offset, length) + } + + /// Returns the size of the code of the account, without loading the account. + fn code_size( + interp: &mut Interpreter<'_, '_, T>, + address: &Address, + ) -> DbResult { + let state = interp.host().state_mut(); + let Some(info) = state.account_info_untracked(address)? else { return Ok(0) }; + let code = load_account_code(state.overlay_db_mut(), &info)?; + Ok(code.map_or(0, |code| code.len() as u64)) + } +} + +/// The opcodes besides `PUSH0` to `SWAP16` that the used opcode counts leave out by default. +const DEFAULT_IGNORED_OPCODES: [u8; 16] = [ + op::POP, + op::ADD, + op::SUB, + op::MUL, + op::DIV, + op::EQ, + op::LT, + op::GT, + op::SLT, + op::SGT, + op::SHL, + op::SHR, + op::AND, + op::OR, + op::NOT, + op::ISZERO, +]; + +const fn is_ext(op: u8) -> bool { + matches!(op, op::EXTCODEHASH | op::EXTCODESIZE | op::EXTCODECOPY) +} + +const fn is_call(op: u8) -> bool { + matches!(op, op::CALL | op::CALLCODE | op::DELEGATECALL | op::STATICCALL) +} + +/// Keeps the first error of a read of the state, and returns the zero that the read reports. +fn record(first: &mut Option, error: DatabaseError) -> V { + first.get_or_insert(error); + V::default() +} + +fn word_to_address(word: U256) -> Address { + Address::from_word(word.into()) +} + +impl Inspector for Erc7562Inspector { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.calls.initialize_interp(interp); + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + let op = interp.opcode(); + // The stack can be too short if the instruction is about to fail. + let (top, second) = match *interp.stack().as_slice() { + [.., second, top] => (Some(top), Some(second)), + [top] => (Some(top), None), + [] => (None, None), + }; + // A return does not leave its frame's last instruction to the frame that resumes. + let last = if matches!(op, op::RETURN | op::REVERT) { None } else { self.last }; + let frame = &mut self.frames[self.calls.last_trace_idx()]; + + // Only the last `EXTCODE*` instruction per address would matter, but geth lists them all, + // except an `EXTCODESIZE` that `ISZERO` consumes. + if let Some(last) = last + && is_ext(last.op) + && let Some(top) = last.top + && !(last.op == op::EXTCODESIZE && op == op::ISZERO) + { + frame.ext_code_access_info.push(word_to_address(top)); + } + if last.is_some_and(|last| last.op == op::GAS) && !is_call(op) { + *frame.used_opcodes.entry(op::GAS).or_default() += 1; + } + if op != op::GAS && !self.ignored.bit(usize::from(op)) { + *frame.used_opcodes.entry(op).or_default() += 1; + } + + let slots = &mut frame.accessed_slots; + match op { + op if is_ext(op) || is_call(op) => { + // The code size of the account that the instruction addresses. + if let Some(word) = if is_ext(op) { top } else { second } { + let address = word_to_address(word); + if let Entry::Vacant(entry) = frame.contract_size.entry(address) { + let contract_size = Self::code_size(interp, &address) + .unwrap_or_else(|error| record(&mut self.error, error)); + entry.insert(ContractSize { contract_size, opcode: op }); + } + } + } + op::SLOAD => { + // The value of a slot that the frame did not write or read yet. + if let Some(slot) = top.map(B256::from) + && !slots.reads.contains_key(&slot) + && !slots.writes.contains_key(&slot) + { + let address = interp.message().destination; + let value = interp + .host() + .state_mut() + .storage_slot_untracked(&address, &slot.into()) + .unwrap_or_else(|error| record(&mut self.error, error)); + slots.reads.entry(slot).or_default().push(value.into()); + } + } + op::SSTORE => { + if let Some(slot) = top { + *slots.writes.entry(slot.into()).or_default() += 1; + } + } + op::TLOAD => { + if let Some(slot) = top { + *slots.transient_reads.entry(slot.into()).or_default() += 1; + } + } + op::TSTORE => { + if let Some(slot) = top { + *slots.transient_writes.entry(slot.into()).or_default() += 1; + } + } + op::KECCAK256 => { + if !self.budget.exceeded() + && let Some(preimage) = Self::preimage(interp, top, second) + && !self.keccak.contains(&*preimage) + && self.budget.reserve(preimage.len()) + { + self.keccak.insert(Bytes::from(preimage.into_owned())); + } + } + _ => {} + } + // Only `EXTCODE*` and `GAS` look at the instruction that ran before them. + self.last = (is_ext(op) || op == op::GAS).then_some(LastStep { op, top }); + self.budget.halt(interp, true); + } + + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + Inspector::::log(&mut self.calls, log, host); + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let result = self.calls.call(interp, message); + self.push_frame(); + result + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.calls.call_end(interp, message, result); + } + + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let result = self.calls.create(interp, message); + self.push_frame(); + result + } + + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.calls.create_end(interp, message, result); + } + + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + Inspector::::selfdestruct(&mut self.calls, contract, target, value, host); + } +} diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs index fe6117d79..6f548fe6b 100644 --- a/crates/inspectors/src/tracing/keccak.rs +++ b/crates/inspectors/src/tracing/keccak.rs @@ -109,7 +109,7 @@ impl Inspector for Keccak256PreimageInspector { /// Returns the `size` bytes of `memory` at `offset`, zero padded past its end. /// /// Returns `None` if the range reaches more than [`MEMORY_PAD_LIMIT`] past the end of memory. -fn memory_copy_padded(memory: &[u8], offset: u64, size: u64) -> Option> { +pub(crate) fn memory_copy_padded(memory: &[u8], offset: u64, size: u64) -> Option> { let end = offset.saturating_add(size); if end.saturating_sub(memory.len() as u64) > MEMORY_PAD_LIMIT { return None; diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index defee9e49..13aae2cbd 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -35,6 +35,9 @@ pub use builder::{ mod config; pub use config::{OpcodeFilter, StackSnapshotType, TracingInspectorConfig}; +mod erc7562; +pub use erc7562::{Erc7562Inspector, Error as Erc7562Error}; + mod fourbyte; pub use fourbyte::FourByteInspector; diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 10c51e500..8cd8064ee 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -16,12 +16,15 @@ use alloy_rpc_types_trace::geth::{ PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, }; use evm2::{ - BaseEvmTypes, Evm, EvmFeatures, ExecutionConfig, Precompiles, Version, + AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, ExecutionConfig, Precompiles, Version, ethereum::ethereum_tx_registry, + evm::{DbResult, DynDatabase}, + interpreter::Word, }; use evm2_inspectors::tracing::{ - DebugInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, MEMORY_PAD_LIMIT, - MuxInspector, TraceLimits, TracingInspector, TracingInspectorConfig, + DebugInspector, Erc7562Error, Erc7562Inspector, KECCAK256_PREIMAGE_TRACER, + Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimits, TracingInspector, + TracingInspectorConfig, }; use std::collections::BTreeMap; @@ -404,6 +407,373 @@ fn test_geth_erc7562_tracer() { } } +#[test] +fn test_geth_erc7562_counts_like_geth() { + let (root, target, failing, empty) = ( + address!("1000000000000000000000000000000000000001"), + address!("1000000000000000000000000000000000000002"), + address!("1000000000000000000000000000000000000003"), + address!("1000000000000000000000000000000000000004"), + ); + let push_address = |code: &mut Vec, address: Address| { + code.push(op::PUSH20); + code.extend_from_slice(address.as_slice()); + }; + // A call with no value, no data and no return data. + let call = |code: &mut Vec, address: Address, gas: &[u8]| { + code.extend_from_slice(&hex!("60006000600060006000")); + push_address(code, address); + code.extend_from_slice(gas); + code.extend_from_slice(&[op::CALL, op::POP]); + }; + let mut code = vec![op::GAS, op::POP]; + // `ISZERO` after `EXTCODESIZE` is a check, not an access. + push_address(&mut code, target); + code.extend_from_slice(&[op::EXTCODESIZE, op::ISZERO, op::POP]); + push_address(&mut code, target); + code.extend_from_slice(&[op::EXTCODESIZE, op::POP]); + // The second call runs out of gas at its `KECCAK256`, and `GAS` before a call does not count. + call(&mut code, empty, &[op::GAS]); + call(&mut code, failing, &hex!("6028")); + // The same preimage twice, from a range that memory does not reach yet. + for _ in 0..2 { + code.extend_from_slice(&hex!("60206000")); + code.extend_from_slice(&[op::KECCAK256, op::POP]); + } + code.push(op::STOP); + let target_code = Bytes::from_static(&[op::STOP]); + let failing_code = Bytes::from_static(&hex!("6001600020")); + + let mut db = CacheDB::::default(); + for (address, code) in [ + (root, Bytes::from(code)), + (target, target_code), + (failing, failing_code), + (empty, Bytes::from_static(&[op::STOP, op::STOP])), + ] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + } + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(root), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + // `GAS` counts unless a call follows, and the ignored opcodes do not count. + assert_eq!( + frame.used_opcodes, + HashMap::from_iter([ + (op::GAS, 1), + (op::EXTCODESIZE, 2), + (op::CALL, 2), + (op::KECCAK256, 2), + (op::STOP, 1), + ]) + ); + assert_eq!(frame.ext_code_access_info, [format!("{target:?}")]); + let size = |address: Address| frame.contract_size[&address].clone(); + assert_eq!( + [size(target), size(empty), size(failing)].map(|size| (size.contract_size, size.opcode)), + [(1, op::EXTCODESIZE), (2, op::CALL), (5, op::CALL)] + ); + assert!(!frame.out_of_gas); + assert_eq!(frame.calls.len(), 2); + assert_eq!(frame.calls[0].used_opcodes, HashMap::from_iter([(op::STOP, 1)])); + // A frame that fails at its `KECCAK256` still counts it and leaves its preimage. + assert!(frame.calls[1].out_of_gas); + assert_eq!(frame.calls[1].used_opcodes, HashMap::from_iter([(op::KECCAK256, 1)])); + // The preimages of all frames are on the root, sorted and without repeats. + assert_eq!(frame.keccak, [Bytes::from_static(&[0]), Bytes::from(vec![0; 32])]); + assert!(frame.calls.iter().all(|call| call.keccak.is_empty())); +} + +#[test] +fn test_geth_erc7562_accessed_slots_and_ignored_opcodes() { + let contract = address!("1000000000000000000000000000000000000001"); + let code = Bytes::from(vec![ + // SLOAD of slot 1 reads 0x2a, and the SSTORE after it is counted. + op::PUSH1, + 1, + op::SLOAD, + op::POP, + op::PUSH1, + 7, + op::PUSH1, + 1, + op::SSTORE, + // The slot was read and written, so a second SLOAD adds no read. Slot 2 is read once. + op::PUSH1, + 1, + op::SLOAD, + op::POP, + op::PUSH1, + 2, + op::SLOAD, + op::POP, + // Slot 3 is written before it is read, so it has no read at all. + op::PUSH1, + 7, + op::PUSH1, + 3, + op::SSTORE, + op::PUSH1, + 3, + op::SLOAD, + op::POP, + op::PUSH1, + 4, + op::TLOAD, + op::POP, + op::PUSH1, + 5, + op::PUSH1, + 4, + op::TSTORE, + op::STOP, + ]); + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + db.insert_account_storage(&contract, &U256::from(1), &U256::from(0x2a)); + db.insert_account_storage(&contract, &U256::from(2), &U256::from(0x2b)); + + // Only `SLOAD` and `STOP` are ignored, which is not the default list. + let ignored_opcodes = vec![op::SLOAD, op::STOP]; + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config { + ignored_opcodes, + ..Default::default() + }); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let slot = |slot: u8| B256::with_last_byte(slot); + let slots = &frame.accessed_slots; + assert_eq!( + slots.reads, + HashMap::from_iter([ + (slot(1), vec![B256::with_last_byte(0x2a)]), + (slot(2), vec![B256::with_last_byte(0x2b)]), + ]) + ); + assert_eq!(slots.writes, HashMap::from_iter([(slot(1), 1), (slot(3), 1)])); + assert_eq!(slots.transient_reads, HashMap::from_iter([(slot(4), 1)])); + assert_eq!(slots.transient_writes, HashMap::from_iter([(slot(4), 1)])); + // The opcodes of the config are not counted, and every other opcode is, including `PUSH1`. + assert_eq!( + frame.used_opcodes, + HashMap::from_iter([ + (op::PUSH1, 11), + (op::POP, 5), + (op::SSTORE, 2), + (op::TLOAD, 1), + (op::TSTORE, 1), + ]) + ); +} + +#[test] +fn test_geth_erc7562_bounds_retained_preimages() { + let contract = address!("1000000000000000000000000000000000000001"); + let spec = SpecId::CANCUN; + // KECCAK256 of 60 and of 61 bytes of zero padding. + let code = Bytes::from(vec![ + op::PUSH1, + 60, + op::PUSH0, + op::KECCAK256, + op::POP, + op::PUSH1, + 61, + op::PUSH0, + op::KECCAK256, + op::STOP, + ]); + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + let block = *Context::mainnet().block(); + for (limit, preimages) in [(121, Some(2)), (120, None)] { + let mut inspector = Erc7562Inspector::new(Erc7562Config::default()) + .with_limits(TraceLimits::default().set_max_recorded_bytes(Some(limit))); + let mut evm = Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ); + evm.set_inspector(&mut inspector); + let tx = TxEnv { + gas_limit: 1_000_000, + gas_price: 0, + kind: TransactTo::Call(contract), + ..Default::default() + }; + let envelope = tx.envelope(); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + assert_eq!(inspector.limit_exceeded(), preimages.is_none()); + assert_eq!( + inspector.frame(&envelope, &res.result).ok().map(|frame| frame.keccak.len()), + preimages + ); + } +} + +/// A database whose storage reads fail. +struct NoStorageDb(CacheDB); + +impl DynDatabase for NoStorageDb { + fn get_account(&mut self, address: &Address) -> DbResult> { + DynDatabase::get_account(&mut self.0, address) + } + + fn get_code_by_hash(&mut self, code_hash: &B256) -> DbResult { + DynDatabase::get_code_by_hash(&mut self.0, code_hash) + } + + fn get_storage(&mut self, _address: &Address, _key: &Word) -> DbResult { + Err(DatabaseError::new(AnyError::from("no storage"), true)) + } + + fn get_block_hash(&mut self, number: &Word) -> DbResult { + DynDatabase::get_block_hash(&mut self.0, number) + } +} + +#[test] +fn test_geth_erc7562_reports_a_failed_state_read() { + let contract = address!("1000000000000000000000000000000000000001"); + let spec = SpecId::CANCUN; + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo { + code: Some(Bytecode::new_raw(Bytes::from_static(&[op::PUSH1, 1, op::SLOAD, op::STOP]))), + ..Default::default() + }, + ); + let block = *Context::mainnet().block(); + // The transaction runs out of gas at the `SLOAD`, before it reads the slot, and does not fail. + let tx = TxEnv { + gas_limit: 21_000 + 3 + 2_099, + gas_price: 0, + kind: TransactTo::Call(contract), + ..Default::default() + }; + let envelope = tx.envelope(); + for fails in [false, true] { + let mut inspector = Erc7562Inspector::new(Erc7562Config::default()); + let mut evm = if fails { + Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + NoStorageDb(db.clone()), + Precompiles::base(spec), + ) + } else { + Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ) + }; + evm.set_inspector(&mut inspector); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + assert!(!res.result.is_success()); + let frame = inspector.frame(&envelope, &res.result); + assert_eq!(matches!(frame, Err(Erc7562Error::Database(_))), fails, "{frame:?}"); + } +} + +#[test] +fn test_geth_erc7562_contract_size_reads_accepted_state() { + let (root, target) = ( + address!("1000000000000000000000000000000000000001"), + address!("1000000000000000000000000000000000000002"), + ); + let spec = SpecId::CANCUN; + let mut code = vec![op::PUSH20]; + code.extend_from_slice(target.as_slice()); + code.extend_from_slice(&[op::EXTCODESIZE, op::STOP]); + let mut db = CacheDB::::default(); + db.insert_account_info( + &root, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + let block = *Context::mainnet().block(); + let mut inspector = + DebugInspector::new(GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default())) + .unwrap(); + let mut evm = Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ); + // The code is in the accepted state only, like that of an earlier transaction of the block. + evm.overlay_db_mut().insert_account_info( + &target, + AccountInfo { + code: Some(Bytecode::new_raw(Bytes::from_static(&[op::STOP; 3]))), + ..Default::default() + }, + ); + evm.set_inspector(&mut inspector); + let tx = TxEnv { + gas_limit: 1_000_000, + gas_price: 0, + kind: TransactTo::Call(root), + ..Default::default() + }; + let envelope = tx.envelope(); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + let trace = inspector.get_result(None, &envelope, &block, &res, &mut db).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + let size = &frame.contract_size[&target]; + assert_eq!((size.contract_size, size.opcode), (3, op::EXTCODESIZE)); +} + /// geth reads each slot in `OnOpcode`, so a frame records its first read of a slot that an earlier /// frame already warmed. #[test] @@ -1968,22 +2338,14 @@ fn test_geth_opcode_limit() { #[test] fn test_geth_opcode_limit_ignores_named_tracers() { - for tracer in - [GethDebugBuiltInTracerType::CallTracer, GethDebugBuiltInTracerType::Erc7562Tracer] - { - let opts = GethDebugTracingOptions { - tracer: Some(GethDebugTracerType::BuiltInTracer(tracer)), - config: GethDefaultTracingOptions { limit: Some(2), ..Default::default() }, - ..Default::default() - }; - let inspector = DebugInspector::new(opts).unwrap(); - let config = match &inspector { - DebugInspector::CallTracer(inspector, _) - | DebugInspector::Erc7562Tracer(inspector, _) => inspector.config(), - _ => panic!("unexpected tracer"), - }; - assert_eq!(config.step_limit, None); - } + let opts = GethDebugTracingOptions { + tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)), + config: GethDefaultTracingOptions { limit: Some(2), ..Default::default() }, + ..Default::default() + }; + let inspector = DebugInspector::new(opts).unwrap(); + let DebugInspector::CallTracer(inspector, _) = &inspector else { panic!("unexpected tracer") }; + assert_eq!(inspector.config().step_limit, None); } #[test] @@ -2301,7 +2663,7 @@ fn test_geth_failed_root_logs() { let mut inspector = TracingInspector::new( TracingInspectorConfig::from_geth_call_config(&CallConfig::default().with_log()), ); - let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); let res = evm .inspect_tx(TxEnv { kind: if create { TxKind::Create } else { TxKind::Call(contract) }, @@ -2329,14 +2691,32 @@ fn test_geth_failed_root_logs() { assert_eq!(serde_json::to_value(frame).unwrap().get("logs"), None); } } + } - let frame = builder - .geth_erc7562_traces( - Erc7562Config { with_log: Some(with_log), ..Default::default() }, - res.result.tx_gas_used(), - &mut EmptyDB::default(), - ) + for with_log in [false, true] { + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config { + with_log: Some(with_log), + ..Default::default() + }); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + kind: if create { TxKind::Create } else { TxKind::Call(contract) }, + data: if create { code.clone() } else { Bytes::new() }, + gas_limit: 100_000, + ..Default::default() + }) .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = + inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { + panic!("Expected Erc7562Tracer") + }; + let expected_logs = usize::from(with_log && error.is_none()); assert_eq!(frame.error.as_deref(), error); assert_eq!(frame.logs.len(), expected_logs); if expected_logs == 0 { From 85d09d9a1d1234ac22e8414c1212b0568457ab0b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 12:04:43 +0100 Subject: [PATCH 088/159] fix(eest): build and check EIP-7685 requests in the blockchain runner (#971) The blockchain runner never built or checked EIP-7685 requests, so it ignored the four Prague fixture directories for deposits, withdrawal requests, consolidations and general-purpose requests. Until the shared block executor lands, the harness is the executor, so the fix goes there. The runner now collects deposit requests from deposit-contract logs, keeps the outputs of the EIP-7002, EIP-7251 and (from Amsterdam) EIP-8282 system calls, which `run_system_call` used to discard, and computes the requests hash. A block is rejected if the hash differs from the header's or if a deposit log is malformed, as in execution-specs. The four ignore entries are gone, and all 68 fixtures in them pass in the interpreter, JIT and AOT suites; the full `cargo st` run also passes. --- crates/eest/Cargo.toml | 2 +- crates/eest/src/blockchaintest/execute.rs | 111 ++++++++++++++++++++-- crates/eest/src/blockchaintest/runner.rs | 6 -- 3 files changed, 103 insertions(+), 16 deletions(-) diff --git a/crates/eest/Cargo.toml b/crates/eest/Cargo.toml index 8ef340917..5698f073a 100644 --- a/crates/eest/Cargo.toml +++ b/crates/eest/Cargo.toml @@ -19,7 +19,7 @@ evm2-jit-runtime = { workspace = true, features = ["llvm"], optional = true } alloy-consensus = { workspace = true, features = ["k256", "std"] } alloy-eip7928 = { workspace = true, features = ["std", "serde", "rlp"] } -alloy-eips = { workspace = true, features = ["std"] } +alloy-eips = { workspace = true, features = ["sha2", "std"] } alloy-primitives = { workspace = true, features = ["std", "serde", "rlp"] } alloy-rlp = { workspace = true, features = ["std"] } alloy-rpc-types-eth = { workspace = true, features = ["std"] } diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index a4571e509..153ae8cdc 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -27,8 +27,16 @@ use crate::{ }; use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; -use alloy_eips::{eip2718::Typed2718, eip4844::DATA_GAS_PER_BLOB, eip7840::BlobParams}; -use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, U256, aliases::U512}; +use alloy_eips::{ + eip2718::Typed2718, + eip4844::DATA_GAS_PER_BLOB, + eip6110::{DEPOSIT_REQUEST_TYPE, MAINNET_DEPOSIT_CONTRACT_ADDRESS}, + eip7002::WITHDRAWAL_REQUEST_TYPE, + eip7251::CONSOLIDATION_REQUEST_TYPE, + eip7685::Requests, + eip7840::BlobParams, +}; +use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, Log, U256, aliases::U512, b256}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use anstyle::{AnsiColor, Color, Style}; use evm2::{ @@ -55,6 +63,25 @@ const ONE_ETHER: u128 = 1_000_000_000_000_000_000; /// (2 MiB). const MAX_RLP_BLOCK_SIZE: usize = 10 * 1024 * 1024 - 2 * 1024 * 1024; +/// First topic of the deposit contract's `DepositEvent`. +const DEPOSIT_EVENT_SIGNATURE_HASH: B256 = + b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"); + +/// ABI layout of a `DepositEvent` payload: the offset of each dynamic field's length word and +/// the field's size, in the order the consensus layer consumes them (public key, withdrawal +/// credentials, amount, signature, index). +const DEPOSIT_EVENT_FIELDS: [(usize, usize); 5] = + [(160, 48), (256, 32), (320, 8), (384, 96), (512, 8)]; + +/// Length of an ABI-encoded `DepositEvent` payload. +const DEPOSIT_EVENT_LENGTH: usize = 576; + +/// EIP-8282 builder deposit request type. +const BUILDER_DEPOSIT_REQUEST_TYPE: u8 = 0x03; + +/// EIP-8282 builder exit request type. +const BUILDER_EXIT_REQUEST_TYPE: u8 = 0x04; + /// Execution options for a single suite. #[derive(Clone, Copy, Debug)] pub struct ExecuteConfig { @@ -543,6 +570,8 @@ fn execute_block<'a>( let mut block_state_gas_used = 0u64; let mut block_blob_gas_used = 0u64; let mut receipts = Vec::with_capacity(transactions.len()); + let mut requests = Requests::default(); + let mut deposit_requests = Vec::new(); for (transaction_index, raw_tx) in transactions.iter().enumerate() { hook.transaction_started(TransactionStarted { block_index, @@ -646,6 +675,12 @@ fn execute_block<'a>( success: result.status, logs: &result.logs, }); + if spec.enables(SpecId::PRAGUE) + && let Err(reason) = + collect_deposit_requests(&result.logs, &mut deposit_requests) + { + return reject_block(should_fail, path, name, reason); + } if compare_receipt_root { let status = if spec.enables(SpecId::BYZANTIUM) { Eip658Value::Eip658(result.status) @@ -744,6 +779,7 @@ fn execute_block<'a>( evm.state_mut().bump_bal_index(); } + requests.push_request_with_type(DEPOSIT_REQUEST_TYPE, deposit_requests); if let Err(err) = post_block_transition( &mut evm, &mut block_state, @@ -752,6 +788,7 @@ fn execute_block<'a>( block_ommers(block), block_withdrawals(block), captured, + &mut requests, ) { // A failed post-block system call (e.g. SYSTEM_CONTRACT_CALL_FAILED) invalidates the // block, mirroring execution-specs raising `InvalidBlock`. @@ -761,6 +798,23 @@ fn execute_block<'a>( return Err(TestError::case(path, name, err)); } + if spec.enables(SpecId::PRAGUE) + && let Some(header) = block_header(block) + { + let computed = requests.requests_hash(); + if header.requests_hash != Some(computed) { + return reject_block( + should_fail, + path, + name, + format!( + "requests hash mismatch: header {:?}, computed {computed}", + header.requests_hash + ), + ); + } + } + if let Some(expected_bal) = block_access_list(block) { let built = evm.state_mut().take_bal_builder().unwrap_or_default(); if let Err(kind) = @@ -1075,6 +1129,7 @@ fn pre_block_system_calls( Ok(()) } +#[expect(clippy::too_many_arguments)] fn post_block_transition( evm: &mut Evm<'_, BaseEvmTypes>, block_state: &mut BlockStateAccumulator, @@ -1083,6 +1138,7 @@ fn post_block_transition( ommers: &[BlockHeader], withdrawals: &[Withdrawal], captured: bool, + requests: &mut Requests, ) -> Result<(), TestErrorKind> { // A captured case keeps only the accounts its blocks touch, so the system contracts are absent. let check = if captured { SystemCallCheck::Outcome } else { SystemCallCheck::OutcomeAndCode }; @@ -1121,7 +1177,7 @@ fn post_block_transition( } if spec.enables(SpecId::PRAGUE) { - run_system_call( + let output = run_system_call( evm, block_state, WITHDRAWAL_REQUEST_ADDRESS, @@ -1129,7 +1185,8 @@ fn post_block_transition( "eip7002", check, )?; - run_system_call( + requests.push_request_with_type(WITHDRAWAL_REQUEST_TYPE, output); + let output = run_system_call( evm, block_state, evm2::evm::CONSOLIDATION_REQUEST_ADDRESS, @@ -1137,11 +1194,12 @@ fn post_block_transition( "eip7251", check, )?; + requests.push_request_with_type(CONSOLIDATION_REQUEST_TYPE, output); } // EIP-8282: builder deposit (request type 0x03) and builder exit (0x04) requests, Amsterdam+. if spec.enables(SpecId::AMSTERDAM) { - run_system_call( + let output = run_system_call( evm, block_state, evm2::evm::BUILDER_DEPOSIT_REQUEST_ADDRESS, @@ -1149,7 +1207,8 @@ fn post_block_transition( "eip8282_deposit", check, )?; - run_system_call( + requests.push_request_with_type(BUILDER_DEPOSIT_REQUEST_TYPE, output); + let output = run_system_call( evm, block_state, evm2::evm::BUILDER_EXIT_REQUEST_ADDRESS, @@ -1157,6 +1216,7 @@ fn post_block_transition( "eip8282_exit", check, )?; + requests.push_request_with_type(BUILDER_EXIT_REQUEST_TYPE, output); } Ok(()) } @@ -1180,7 +1240,7 @@ fn run_system_call( data: Bytes, label: &'static str, check: SystemCallCheck, -) -> Result<(), TestErrorKind> { +) -> Result { // A checked call to a target without code is an invalid block (execution-specs // `SYSTEM_CONTRACT_EMPTY`); the EVM itself treats it as a successful no-op. if check == SystemCallCheck::OutcomeAndCode { @@ -1198,12 +1258,45 @@ fn run_system_call( // A failed unchecked call keeps the state accesses it recorded, which a BAL lists as reads. if check == SystemCallCheck::None { let _ = executed.commit_to(block_state); - return Ok(()); + return Ok(Bytes::new()); } let _ = executed.discard(); return Err(TestErrorKind::SystemCall(label)); } - let _ = executed.commit_to(block_state); + Ok(executed.commit_to(block_state).output) +} + +/// Appends the unframed fields of every deposit event in `logs` to `deposit_requests` +/// (execution-specs `parse_deposit_requests`). +fn collect_deposit_requests(logs: &[Log], deposit_requests: &mut Vec) -> Result<(), String> { + for log in logs { + if log.address == MAINNET_DEPOSIT_CONTRACT_ADDRESS + && log.topics().first() == Some(&DEPOSIT_EVENT_SIGNATURE_HASH) + { + extract_deposit_data(&log.data.data, deposit_requests)?; + } + } + Ok(()) +} + +/// Strips the ABI framing from a `DepositEvent` payload, appending the fields to `out` +/// (execution-specs `extract_deposit_data`). +fn extract_deposit_data(data: &[u8], out: &mut Vec) -> Result<(), String> { + if data.len() != DEPOSIT_EVENT_LENGTH { + return Err("invalid deposit event data length".to_string()); + } + let word = |offset: usize| U256::from_be_slice(&data[offset..offset + 32]); + for (index, (offset, _)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(index * 32) != U256::from(offset) { + return Err(format!("invalid offset for deposit field {index}")); + } + } + for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(offset) != U256::from(size) { + return Err(format!("invalid size for deposit field {index}")); + } + out.extend_from_slice(&data[offset + 32..offset + 32 + size]); + } Ok(()) } diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index 805e5b051..a698476a6 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -170,12 +170,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "eip7928_block_level_access_lists/fork_transition/invalid_post_fork_block_without_bal_hash_field.json", "eip7928_block_level_access_lists/fork_transition/invalid_pre_fork_block_with_bal_hash_field.json", - // Prague request/deposit fixtures validate EL request extraction and system-contract block processing. - "prague/eip6110_deposits", - "prague/eip7002_el_triggerable_withdrawals", - "prague/eip7251_consolidations", - "prague/eip7685_general_purpose_el_requests", - // This fixture has a block gas limit below the transaction intrinsic gas and belongs to block validation. "static/state_tests/stEIP1559/lowGasLimit.json", ]; From e10563c0f9f07224e2cf72c8e880f253e293922b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 12:13:35 +0100 Subject: [PATCH 089/159] ci: pin nightly to 2026-10-31 until boa_engine builds again (#979) Main CI is red: the latest nightly fails to compile `boa_engine` 0.22.0 (used by the `js-tracer` feature of `evm2-inspectors`) with E0391, "cycle detected when computing when `error::Repr` needs async drop". This breaks the clippy, docs and `test +nightly ubuntu --all-features` jobs. It is a nightly regression tracked in rust-lang/rust#165263. This pins the nightly jobs that compile the workspace (the matrix `nightly` entries, clippy and docs) to `nightly-2026-10-31` through one `NIGHTLY_TOOLCHAIN` env var, with a comment to remove it once a nightly builds `boa_engine` again. `cargo check -p evm2-inspectors --all-features` passes on `nightly-2026-10-31` and fails on `nightly-2026-11-01` with the same error. The `fmt` and `deny` jobs do not compile dependencies and stay on the latest nightly. --- .github/workflows/ci.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 96ad1563a..af99458b6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,10 @@ on: env: CARGO_TERM_COLOR: always RUSTC_WRAPPER: "sccache" + # Nightly after 2026-10-31 fails to compile boa_engine (E0391 "needs async drop" cycle): + # https://github.com/rust-lang/rust/issues/165263 + # Remove this pin once a nightly builds boa_engine again. + NIGHTLY_TOOLCHAIN: nightly-2026-10-31 concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -67,11 +71,11 @@ jobs: - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 if: ${{ !matrix.target }} with: - toolchain: ${{ matrix.rust }} + toolchain: ${{ matrix.rust == 'nightly' && env.NIGHTLY_TOOLCHAIN || matrix.rust }} - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 if: ${{ matrix.target }} with: - toolchain: ${{ matrix.rust }} + toolchain: ${{ matrix.rust == 'nightly' && env.NIGHTLY_TOOLCHAIN || matrix.rust }} targets: ${{ matrix.target }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 if: runner.os == 'Linux' @@ -399,7 +403,7 @@ jobs: run: .github/scripts/install_llvm.sh ubuntu - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} components: clippy - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 @@ -427,7 +431,7 @@ jobs: run: .github/scripts/install_llvm.sh ubuntu - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 From 3da55b470c540d180eebba8d14d5831f790ac069 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 13:33:28 +0100 Subject: [PATCH 090/159] ci(bench): stop polling the base benchmark run (#980) The baseline step in `bench.yml` ran `gh run watch --interval 10` on the base commit's benchmark run whenever it had not finished. With many PRs waiting on one main run, the polling exhausted the repository's GitHub App installation rate limit ("API rate limit exceeded for installation"), which failed `jit codegen benchmark` and `sccache-action` on most open PRs. The step no longer waits. A base run that is still in progress is skipped, and the job takes the existing "No comparable baseline artifact" path and benchmarks the base itself. When the base run has already completed, which is the common case, nothing changes. A failed `list_runs` call now emits a warning instead of being treated as no runs. --- .github/workflows/bench.yml | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 40d230864..11a05a33b 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -119,23 +119,11 @@ jobs: | select(.display_title | startswith("Benchmark backfill ") | not) | {createdAt: .created_at, databaseId: .id, status}]' } - runs_json="$(list_runs 2>/dev/null || echo '[]')" - latest_run_id="$( - jq -r --argjson current "$CURRENT_RUN_ID" \ - '[.[] | select(.databaseId != $current)] | max_by(.createdAt).databaseId // empty' \ - <<< "$runs_json" - )" - if [[ -n "$latest_run_id" ]]; then - latest_status="$( - jq -r --argjson run_id "$latest_run_id" \ - '.[] | select(.databaseId == $run_id) | .status' \ - <<< "$runs_json" - )" - if [[ "$latest_status" != "completed" ]]; then - echo "Waiting for exact-base benchmark run $latest_run_id" - gh run watch "$latest_run_id" --interval 10 || true - runs_json="$(list_runs)" - fi + # A base run that is still in progress is not waited for: polling it from every + # PR run exhausts the API rate limit, so such a PR benchmarks the base itself. + if ! runs_json="$(list_runs)"; then + echo "::warning::Could not list benchmark runs for $base_sha" + runs_json='[]' fi run_ids="$( jq -r --argjson current "$CURRENT_RUN_ID" \ From c47757e13860cb2e9f688101020b3feaa5a86760 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 1 Nov 2026 14:23:38 +0100 Subject: [PATCH 091/159] ci(fuzzer): pin nightly to 2026-10-31 (#981) The differential fuzzer in `fuzzer.yml` still used floating `nightly`, which fails to compile `boa_engine` 0.22.0 with E0391 ("needs async drop" cycle) on nightlies after 2026-10-31 ([rust-lang/rust#165263](https://github.com/rust-lang/rust/issues/165263)). #979 pinned the nightly jobs in `ci.yml` only, so the fuzzer's four smoke jobs fail on any PR whose run picks a newer nightly, for example #859. This pins the fuzzer to `nightly-2026-10-31` with the same `NIGHTLY_TOOLCHAIN` variable, comment and issue link as `ci.yml`. Workflows cannot share an env block, so the value is copied. No other workflow compiles dependencies on nightly: `bench.yml` uses stable, and the `fmt` and `deny` jobs in `ci.yml` stay on the latest nightly because they do not build dependencies. --- .github/workflows/fuzzer.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/fuzzer.yml b/.github/workflows/fuzzer.yml index e5e52f51d..a21cf3fb7 100644 --- a/.github/workflows/fuzzer.yml +++ b/.github/workflows/fuzzer.yml @@ -19,6 +19,10 @@ concurrency: env: CARGO_TERM_COLOR: always RUSTC_WRAPPER: "sccache" + # Nightly after 2026-10-31 fails to compile boa_engine (E0391 "needs async drop" cycle): + # https://github.com/rust-lang/rust/issues/165263 + # Remove this pin once a nightly builds boa_engine again. + NIGHTLY_TOOLCHAIN: nightly-2026-10-31 jobs: smoke: @@ -40,7 +44,7 @@ jobs: persist-credentials: false - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 From aedd425999e030084f2fe5fb7bc35ed4f2fd2e55 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:53:58 +0100 Subject: [PATCH 092/159] chore(deps): bump the ci-weekly group with 5 updates (#982) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumps the ci-weekly group with 5 updates: | Package | From | To | | --- | --- | --- | | [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) | `10.3.0` | `10.3.2` | | [Swatinem/rust-cache](https://github.com/swatinem/rust-cache) | `2.9.2` | `2.9.3` | | [taiki-e/install-action](https://github.com/taiki-e/install-action) | `2.87.29` | `2.87.31` | | [CodSpeedHQ/action](https://github.com/codspeedhq/action) | `5.5.0` | `5.6.0` | | [actions/setup-python](https://github.com/actions/setup-python) | `7.0.0` | `7.1.0` | Updates `astral-sh/setup-uv` from 10.3.0 to 10.3.2
Release notes

Sourced from astral-sh/setup-uv's releases.

v10.3.2 🌈 update known checksums for 0.13.5

Changes

🧰 Maintenance

📚 Documentation

v10.3.1 🌈 preserve cache error class names in save-cache bundle

Changes

This release includes a fix so that multiple concurrent workflows trying to upload the same cache no longer warn

##[warning]Failed to save: Unable to reserve cache with key ..., another job may be creating this cache.

🐛 Bug fixes

🧰 Maintenance

📚 Documentation

⬆️ Dependency updates

Commits
  • 07a047e chore: update known checksums for 0.13.5 (#1103)
  • e1b8536 docs: update version references to v10.3.1 (#1102)
  • 18d369b chore(deps): roll up Dependabot updates (#1101)
  • 4ad2329 chore: update known checksums for 0.13.4 (#1099)
  • 4bc6759 chore: update known checksums for 0.13.2/0.13.3 (#1098)
  • c97f06e fix: preserve cache error class names in save-cache bundle (#1093)
  • 8ca4be0 chore: update known checksums for 0.13.1 (#1094)
  • b627d85 docs: cache-dependency-glob matches relative to the working directory (#1092)
  • f8c8897 Verify npm dependency signatures before builds (#1027)
  • f64d32b docs: update version references to v10.3.0 (#1090)
  • See full diff in compare view

Updates `Swatinem/rust-cache` from 2.9.2 to 2.9.3
Release notes

Sourced from Swatinem/rust-cache's releases.

v2.9.3

What's Changed

New Contributors

Full Changelog: https://github.com/Swatinem/rust-cache/compare/v2.9.2...v2.9.3

Changelog

Sourced from Swatinem/rust-cache's changelog.

Changelog

2.9.3

  • Fix timestamp based pruning
  • Add missing awaits
  • Do not emit warning on missing rustup

2.9.2

  • Fix credentials.toml cleanup
  • Improvements to cleanup, preserving more valid targets
  • Improvements to cargo install handling
  • Correctly sort/dedupe Rust versions

2.9.1

  • Fix regression in hash calculation

2.9.0

  • Update to node24
  • Support running from within a nix shell
  • Consider all installed toolchains for cache key
  • Use case-insensitive comparison to determine exact cache hit

2.8.2

  • Don't overwrite env for cargo-metadata call

2.8.1

  • Set empty CARGO_ENCODED_RUSTFLAGS when retrieving metadata
  • Various dependency updates

2.8.0

  • Add support for warpbuild cache provider
  • Add new cache-workspace-crates feature

2.7.8

  • Include CPU arch in the cache key

2.7.7

  • Also cache cargo install metadata

2.7.6

... (truncated)

Commits
  • d1527c8 2.9.3
  • f0fca6f update dependencies and rebuild
  • b49b17f add missing awaits
  • 6a7bc3e Bump the dev-minor group across 1 directory with 2 updates (#383)
  • 4ece996 Bump the actions group across 1 directory with 6 updates (#392)
  • 8103aff Bump the prd-minor group across 1 directory with 2 updates (#389)
  • 0e361f3 fix: log rustup toolchain list failure as info instead of warning (#393) (#394)
  • f0d9c38 Merge pull request #380 from Swatinem/dependabot/github_actions/actions-8ff58...
  • ccd9742 Bump the actions group with 2 updates
  • 258712b fix: stop cleanup timestamp pruning after the first entry (#377)
  • Additional commits viewable in compare view

Updates `taiki-e/install-action` from 2.87.29 to 2.87.31
Release notes

Sourced from taiki-e/install-action's releases.

2.87.31

  • Downgrade cargo-sort@latest to 2.1.3.

  • Update vacuum@latest to 0.35.0.

  • Update xh@latest to 0.26.3.

  • Update uv@latest to 0.13.5.

  • Update ubi@latest to 0.13.0.

  • Update trivy@latest to 0.76.1.

  • Update tombi@latest to 1.9.0.

  • Update release-plz@latest to 0.3.174.

  • Update prek@latest to 0.5.7.

  • Update mise@latest to 2026.10.17.

  • Update jaq@latest to 3.1.2.

  • Update espup@latest to 0.17.1.

  • Update cargo-zigbuild@latest to 0.24.0.

  • Update cargo-nextest@latest to 0.9.149.

  • Update biome@latest to 2.6.2.

2.87.30

  • Update zizmor@latest to 1.31.1.

  • Update wasmtime@latest to 50.0.0.

  • Update vacuum@latest to 0.33.2.

  • Update uv@latest to 0.13.4.

  • Update trunk@latest to 0.22.0.

  • Update tombi@latest to 1.8.9.

  • Update syft@latest to 1.55.0.

  • Update release-plz@latest to 0.3.173.

  • Update prek@latest to 0.5.6.

... (truncated)

Changelog

Sourced from taiki-e/install-action's changelog.

Changelog

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

[Unreleased]

[2.89.0] - 2026-11-01

  • Support yq. (#2036, thanks @​FirelightFlagboy)

  • Support mold. (#2044, thanks @​FirelightFlagboy)

  • Fix an error when old cargo-binstall had already been installed at non-standard location. (#2048)

  • Update wasm-tools@latest to 1.263.0.

  • Update wasm-pack@latest to 0.15.1.

  • Update wasm-bindgen@latest to 0.2.130.

  • Update vacuum@latest to 0.36.1.

  • Update prek@latest to 0.5.9.

  • Update mise@latest to 2026.10.25.

  • Update kingfisher@latest to 2.16.0.

  • Update cargo-binstall@latest to 1.26.2.

[2.88.1] - 2026-10-30

  • Update wasmtime@latest to 50.0.1.

  • Update vacuum@latest to 0.36.0.

  • Update tombi@latest to 1.9.8.

  • Update mise@latest to 2026.10.23.

  • Update kingfisher@latest to 2.15.0.

  • Update espup@latest to 0.19.0.

... (truncated)

Commits

Updates `CodSpeedHQ/action` from 5.5.0 to 5.6.0
Release notes

Sourced from CodSpeedHQ/action's releases.

v5.6.0

Bumps the CodSpeed runner to v5.6.0.

Highlights

More consistent CPU Simulation results

Changes to your CI environment, like a different path or an extra environment variable, no longer change the performance. Each benchmark now starts with the same memory layout, reducing the chance of false regressions.

🐛 Bug Fixes

⚙️ Internals

Full Runner Changelog: https://github.com/CodSpeedHQ/codspeed/blob/main/CHANGELOG.md

What's Changed

Full Changelog: https://github.com/CodSpeedHQ/action/compare/v5.5.0...v5.6.0

Commits

Updates `actions/setup-python` from 7.0.0 to 7.1.0
Release notes

Sourced from actions/setup-python's releases.

v7.1.0

What's Changed

Enhancements

Dependency Upgrade

New Contributors

Full Changelog: https://github.com/actions/setup-python/compare/v7.0.0...v7.1.0

Commits
  • 6d4fd22 Cache upgrade to 6.3.0 (#1370)
  • ec52d0a Bump @​typescript-eslint/eslint-plugin from 8.62.0 to 8.71.1 (#1369)
  • ff73b24 Bump prettier from 3.8.4 to 3.9.9 and package.json from 7.0.0 to 7.1.0 (#1363)
  • 0e40890 Bump brace-expansion from 5.0.9 to 5.0.12 (#1365)
  • 3892123 Bump ts-jest from 29.4.11 to 29.4.14 (#1361)
  • 6609657 Bump undici from 6.28.0 to 6.29.0 (#1364)
  • e1be392 Bump @​types/semver from 7.7.1 to 7.8.0 (#1359)
  • 06da627 Bump @​vercel/ncc from 0.44.0 to 0.45.0 (#1357)
  • af9bcc7 Bump eslint-plugin-jest from 29.15.2 to 29.16.6 (#1342)
  • 7aeabeb Bump @​typescript-eslint/parser from 8.62.0 to 8.70.0 (#1343)
  • Additional commits viewable in compare view

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore ` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore ` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore ` will remove the ignore condition of the specified dependency and ignore conditions
Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/bench-comment.yml | 2 +- .github/workflows/bench.yml | 10 +++++----- .github/workflows/ci.yml | 20 ++++++++++---------- .github/workflows/fuzzer.yml | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/bench-comment.yml b/.github/workflows/bench-comment.yml index 89658f9ac..b120f7fa4 100644 --- a/.github/workflows/bench-comment.yml +++ b/.github/workflows/bench-comment.yml @@ -68,7 +68,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 with: version: "0.13.4" diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 11a05a33b..38f82514a 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -44,11 +44,11 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 with: version: "0.13.4" @@ -244,18 +244,18 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false - name: Install cargo-codspeed - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + uses: taiki-e/install-action@1e29709c0ec63babbe8f266397a465f29a825f91 # v2.87.31 with: tool: cargo-codspeed - name: Build the benchmark target run: cargo codspeed build --profile profiling -p evm2-cli --features jit --bench evm - name: Run the benchmarks - uses: CodSpeedHQ/action@821a40cd6c80e3cfe387caf55668287addd808da # v5.5.0 + uses: CodSpeedHQ/action@78102a468f96d8e0ccb92d0ec49425753fc42062 # v5.6.0 with: run: cargo codspeed run -p evm2-cli --bench evm mode: simulation diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af99458b6..bfe480496 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: "3.14" - name: Generate matrices @@ -80,7 +80,7 @@ jobs: - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 if: runner.os == 'Linux' - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -105,7 +105,7 @@ jobs: checksum: true fallback: none if: ${{ matrix.command == 'wasm-test' }} - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 with: version: "0.13.4" if: ${{ matrix.kind == 'eest' }} @@ -238,7 +238,7 @@ jobs: - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 if: runner.os == 'Linux' - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -248,7 +248,7 @@ jobs: tool: nextest@0.9.140 checksum: true fallback: none - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 with: version: "0.13.4" - name: Download EEST fixtures @@ -282,7 +282,7 @@ jobs: toolchain: stable targets: ${{ matrix.target }} - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -319,7 +319,7 @@ jobs: checksum: true fallback: none - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -366,7 +366,7 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -407,7 +407,7 @@ jobs: components: clippy - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -434,7 +434,7 @@ jobs: toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false diff --git a/.github/workflows/fuzzer.yml b/.github/workflows/fuzzer.yml index a21cf3fb7..26ea4cbbc 100644 --- a/.github/workflows/fuzzer.yml +++ b/.github/workflows/fuzzer.yml @@ -47,7 +47,7 @@ jobs: toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false From ed7fe8e7e22507584f51e2eebc2d2ceb1d8c0f77 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:12:41 +0100 Subject: [PATCH 093/159] test(eest): trust core state guarantees in the harness (#950) The EEST harness re-patched state guarantees that core already provides. `increment_balance` reset a zero code hash to the empty hash, but every account read already normalises it. The state-test runner skipped the beacon-roots and history-storage system calls when the contract had no code, although core makes such a call a successful no-op and the blockchain runner never checked, so the two runners disagreed. Both checks and the `system_contract_has_code` helper are gone. A state-test system call to an existing empty account at a system address now touches it, so it is removed at finalize. That state cannot occur: system calls exist only from Cancun, and EIP-7523 forbids empty accounts after the merge (execution-specs has no touched-account cleanup from Paris on). A committed blockchain-test block without a header also no longer defaults its hash to zero. That overwrote the parent's real hash and silently skipped EIP-2935 and the parent-hash check for the next block; it now fails the case. --- crates/eest/src/blockchaintest/execute.rs | 23 +++++++++++++---------- crates/eest/src/execute.rs | 6 +----- crates/eest/src/state.rs | 11 ----------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 153ae8cdc..911046d94 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -36,7 +36,7 @@ use alloy_eips::{ eip7685::Requests, eip7840::BlobParams, }; -use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, Log, U256, aliases::U512, b256}; +use alloy_primitives::{Address, B256, Bytes, Log, U256, aliases::U512, b256}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use anstyle::{AnsiColor, Color, Style}; use evm2::{ @@ -409,7 +409,7 @@ fn seed_block_hashes(database: &mut InMemoryDB, test_case: &BlockchainTestCase) #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum BlockResolution { - Commit, + Commit(B256), Discard, } @@ -832,7 +832,14 @@ fn execute_block<'a>( return Err(TestError::case(path, name, TestErrorKind::UnexpectedSuccess(expected))); } - Ok(BlockResolution::Commit) + let Some(block_hash) = block_hash else { + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure("committed block has no header".into()), + )); + }; + Ok(BlockResolution::Commit(block_hash)) })(); // The EVM was constructed with this concrete database above; recover it before returning so @@ -858,13 +865,12 @@ fn execute_block<'a>( } match result { - Ok(BlockResolution::Commit) => { + Ok(BlockResolution::Commit(block_hash)) => { restored_database.commit_source(&block_state); - restored_database - .insert_block_hash(&next_block_env.number, &block_hash.unwrap_or_default()); + restored_database.insert_block_hash(&next_block_env.number, &block_hash); *database = restored_database; *block_env = next_block_env; - *parent_block_hash = block_hash; + *parent_block_hash = Some(block_hash); if let Some(header) = block_header(block) { *parent_header = header; } @@ -1467,9 +1473,6 @@ fn increment_balance( }; let mut current = original.clone().unwrap_or_default(); current.balance = current.balance.saturating_add(amount); - if current.code_hash.is_zero() { - current.code_hash = KECCAK256_EMPTY; - } // EELS `modify_state` destroys an account left empty by the modification, so a zero-amount // withdrawal neither creates its recipient nor keeps a pre-existing empty one alive. The // BAL commit below still runs to record the touched address. diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index b3e0b7925..63c776eab 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -7,9 +7,7 @@ use crate::{ filter::NameFilter, fixture_io, forks::is_fork_skipped, - state::{ - insert_account_with_storage, parse_bytecode, storage_for_root, system_contract_has_code, - }, + state::{insert_account_with_storage, parse_bytecode, storage_for_root}, trace::{self, Eip3155Tracer}, tx::{ TxFields, TxSigner, build_recovered_tx, decode_recovered_tx, recover_address, @@ -475,7 +473,6 @@ fn pre_block_system_calls( if spec.enables(SpecId::CANCUN) && let Some(beacon_root) = env.current_beacon_root - && system_contract_has_code(post, BEACON_ROOTS_ADDRESS) { commit_system_call( evm, @@ -486,7 +483,6 @@ fn pre_block_system_calls( } if spec.enables(SpecId::PRAGUE) && let Some(previous_hash) = env.previous_hash - && system_contract_has_code(post, HISTORY_STORAGE_ADDRESS) { commit_system_call( evm, diff --git a/crates/eest/src/state.rs b/crates/eest/src/state.rs index c9a78320a..de0143147 100644 --- a/crates/eest/src/state.rs +++ b/crates/eest/src/state.rs @@ -22,17 +22,6 @@ pub(crate) fn insert_account_with_storage( } } -/// Returns whether the given system contract exists with non-empty code. -pub(crate) fn system_contract_has_code(database: &InMemoryDB, address: Address) -> bool { - database - .cache - .accounts - .get(&address) - .and_then(Option::as_ref) - .and_then(|info| database.cache.contracts.get(&info.code_hash)) - .is_some_and(|code| !code.is_empty()) -} - /// Returns persistent storage values for trie-root calculation. pub(crate) fn storage_for_root( state: &InMemoryDB, From fc22ede5c334ef964e0a05001d0fc286aa12a887 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:13:44 +0100 Subject: [PATCH 094/159] fix(state): read code without loading the account (#942) `State` had no way to read an account's code without loading the account. The JS tracer's `db.getCode` went through `state.account(address)?.load_code()`, which adds the account to the transaction overlay. A tracer that only looked at code therefore put the account into `PendingState`, the change stream and, when a builder runs, the block access list. This adds `State::code_untracked`, next to `account_info_untracked` and `storage_slot_untracked`, with the same contract. It returns the overlay account's code when the account is loaded and otherwise the code of the accepted overlay or database account, resolved by hash. It loads, caches and tracks nothing in the transaction overlay, and returns empty code for an absent account. The JS reader's `read_code` now uses it, and a test shows that `getCode` of an address the transaction never touched leaves that address out of the pending state and the change stream. The erc7562 inspector's `code_size` read, which hand-rolled this with `account_info_untracked` and `load_account_code`, now uses `code_untracked` too. --- crates/evm2/src/evm/state/mod.rs | 23 +++++++ crates/inspectors/src/tracing/erc7562.rs | 6 +- crates/inspectors/src/tracing/js/bindings.rs | 2 +- crates/inspectors/tests/it/geth_js.rs | 63 ++++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index 51bbf4d63..aee788125 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -690,6 +690,29 @@ impl<'a> State<'a> { self.inner.read_unloaded_slot(address, key, reads_zero) } + /// Returns an account's bytecode from the overlay or the backing database. + /// + /// This is the code-side mirror of [`Self::account_info_untracked`]: a non-loading peek that + /// resolves the account without caching it in the overlay, then returns the code held by the + /// account info or reads it from the database by code hash. It returns empty bytecode for an + /// absent account or the empty code hash. Use [`Self::account`] and + /// [`AccountHandle::load_code`] when the account should be loaded and preserved. + #[inline(never)] + pub fn code_untracked(&mut self, address: &Address) -> DbResult { + let Some(info) = self.account_info_untracked(address)? else { + return Ok(Bytecode::default()); + }; + if info.is_empty_code_hash() { + return Ok(Bytecode::default()); + } + let code = match info.code { + Some(code) if !code.is_empty() => code, + _ => self.database.get_code_by_hash(&info.code_hash)?, + }; + code.seed_hash(info.code_hash); + Ok(code) + } + /// Transfers value between accounts. pub fn transfer(&mut self, from: &Address, to: &Address, value: &Word) -> DbResult { if value.is_zero() { diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index 752b9a7b7..f3e7462f6 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -18,7 +18,6 @@ use crate::tracing::{ DEFAULT_MAX_RECORDED_BYTES, GethTraceBuilder, TraceLimits, TracingInspector, TracingInspectorConfig, keccak::memory_copy_padded, limits::TraceBudget, - utils::load_account_code, }; use alloc::{borrow::Cow, collections::BTreeSet, format, vec::Vec}; use alloy_consensus::{Transaction, transaction::Recovered}; @@ -246,10 +245,7 @@ impl Erc7562Inspector { interp: &mut Interpreter<'_, '_, T>, address: &Address, ) -> DbResult { - let state = interp.host().state_mut(); - let Some(info) = state.account_info_untracked(address)? else { return Ok(0) }; - let code = load_account_code(state.overlay_db_mut(), &info)?; - Ok(code.map_or(0, |code| code.len() as u64)) + Ok(interp.host().state_mut().code_untracked(address)?.len() as u64) } } diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 3679de8c8..fa9e3b79d 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -552,7 +552,7 @@ impl EvmDbReader for StateDbReader<'_, '_> { } fn read_code(&mut self, address: &Address) -> DbResult { - self.state.account(address)?.load_code() + self.state.code_untracked(address) } fn read_state(&mut self, address: &Address, slot: &Word) -> DbResult { diff --git a/crates/inspectors/tests/it/geth_js.rs b/crates/inspectors/tests/it/geth_js.rs index 7596bff11..daf5048b7 100644 --- a/crates/inspectors/tests/it/geth_js.rs +++ b/crates/inspectors/tests/it/geth_js.rs @@ -512,6 +512,69 @@ fn test_geth_debug_inspector_jstracer() { assert_eq!(result["error"], json!(false)); } +#[test] +fn test_geth_jstracer_get_code_does_not_load_the_account() { + let target = address!("1000000000000000000000000000000000000001"); + let observed = address!("1000000000000000000000000000000000000002"); + let caller = address!("1000000000000000000000000000000000000003"); + + let context = + Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), + ); + db.insert_account_info( + &target, + AccountInfo { + code: Some(Bytecode::new_legacy(hex!("00").into())), + ..Default::default() + }, + ); + db.insert_account_info( + &observed, + AccountInfo { + code: Some(Bytecode::new_legacy(hex!("6001600100").into())), + ..Default::default() + }, + ); + }); + let context = context.modify_tx_chained(|tx| { + tx.gas_limit = 1_000_000; + tx.gas_price = 1; + tx.kind = TransactTo::Call(target); + }); + + let code = r#" +{ + codeLength: -1, + step: function(log, db) { + this.codeLength = db.getCode(toAddress("1000000000000000000000000000000000000002")).length; + }, + fault: function() {}, + result: function() { return this.codeLength; } +}"#; + let mut inspector = DebugInspector::new(GethDebugTracingOptions::js_tracer(code)).unwrap(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + + let tx = evm.ctx().tx().clone(); + let res = evm.inspect_tx(TxEnv { caller, ..tx }).unwrap(); + assert!(res.result.is_success()); + + let (context, inspector) = evm.ctx_inspector(); + let tx_env = context.tx().envelope(); + let block_env = *context.block(); + let trace = + inspector.get_result(None, &tx_env, &block_env, &res.tx_result, context.db_mut()).unwrap(); + let GethTrace::JS(result) = trace else { panic!("expected JS trace") }; + assert_eq!(result, json!(5)); + + // The transaction loads its caller and target. The code read does not add `observed`. + let pending = &res.tx_result.pending_state; + assert!(pending.account_changes_for(&observed).is_none()); + assert!(pending.account_changes_for(&target).is_some()); +} + #[test] fn mux_runs_a_js_tracer_next_to_a_built_in_tracer() { let code = "{ steps: 0, step: function() { this.steps++; }, fault: function() {}, \ From d96ad09c61dce7b5820bf85e79463dda99f6ee88 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:14:45 +0100 Subject: [PATCH 095/159] refactor(inspectors): read account changes from the pending state (#946) `TxState` rebuilt a transaction's changes into its own hash maps by visiting the `PendingState` change stream, although `PendingState::account_changes` and `account_changes_for` already give random access to each account's `AccountChanges`. The rebuild also lost information: the stream reports a wiped account's read-only slots with their pre-wipe value and drops the wipe marker, and `TxAccount::changed_storage` filtered with `Tracked::is_changed`, which ignores the wipe, while the overlay keeps the post-wipe current value and `StorageOverlay::changed_slots` applies the wipe rule. This deletes `tx_state.rs`. The parity state diff and the geth prestate builders iterate `account_changes()` and take changed slots from `storage.changed_slots()` and original slot values from the overlay. The JS tracer's reader looks accounts up with `account_changes_for` and returns `AccountChanges::code` when it is set; `read_state` still reads the database. The geth builders keep their `db.get_account` re-reads. Tests that built a `TxState` by hand now build a `PendingState` with `insert_account`. The builder tests set no `created` flag any more, since the geth builders never read it. Outputs are unchanged in the inspectors tests, EEST and the geth parity harness. The one possible difference is a slot that is wiped and rewritten to its old value in one transaction, which now counts as changed, as it does in evm2's own change stream. --- crates/inspectors/src/tracing/builder/geth.rs | 90 +++++---------- .../inspectors/src/tracing/builder/parity.rs | 13 +-- crates/inspectors/src/tracing/js/bindings.rs | 24 ++-- crates/inspectors/src/tracing/js/mod.rs | 4 +- crates/inspectors/src/tracing/mod.rs | 2 - crates/inspectors/src/tracing/tx_state.rs | 109 ------------------ 6 files changed, 51 insertions(+), 191 deletions(-) delete mode 100644 crates/inspectors/src/tracing/tx_state.rs diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 6ec676256..0b8c90438 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -1,6 +1,5 @@ //! Geth trace builder use crate::tracing::{ - tx_state::TxState, types::{CallKind, CallTraceNode, CallTraceStepStackItem, StorageChange}, utils::load_account_code, }; @@ -15,7 +14,7 @@ use alloy_rpc_types_trace::geth::{ }; use evm2::{ EvmFeatures, EvmTypesHost, TxResultExt, TxResultWithState, - evm::{DbResult, DynDatabase}, + evm::{DbResult, DynDatabase, PendingState}, }; /// The smallest size in bytes of the JSON of a struct log, @@ -401,19 +400,18 @@ impl<'a> GethTraceBuilder<'a> { prestate_config: &PreStateConfig, db: &mut dyn DynDatabase, ) -> DbResult { - let state = TxState::from_pending(pending_state); let code_enabled = prestate_config.code_enabled(); let storage_enabled = prestate_config.storage_enabled(); if prestate_config.is_diff_mode() { - self.geth_prestate_diff_traces(&state, db, code_enabled, storage_enabled) + self.geth_prestate_diff_traces(pending_state, db, code_enabled, storage_enabled) } else { - self.geth_prestate_pre_traces(&state, db, code_enabled, storage_enabled) + self.geth_prestate_pre_traces(pending_state, db, code_enabled, storage_enabled) } } fn geth_prestate_pre_traces( &self, - state: &TxState, + state: &PendingState, db: &mut dyn DynDatabase, code_enabled: bool, storage_enabled: bool, @@ -421,8 +419,8 @@ impl<'a> GethTraceBuilder<'a> { let mut prestate = PreStateMode::default(); // we only want changed accounts for things like balance changes etc - for (addr, loaded_acc) in state.accounts.iter() { - let addr = *addr; + for loaded_acc in state.account_changes() { + let addr = loaded_acc.address; let db_acc = db.get_account(&addr)?.unwrap_or_default(); // Geth omits accounts that were empty before execution, even if fees fund them. // Check the original code hash before applying the disableCode output option. @@ -434,8 +432,8 @@ impl<'a> GethTraceBuilder<'a> { // insert the original value of all loaded storage slots if storage_enabled { - for (key, slot) in loaded_acc.storage.iter() { - acc_state.storage.insert((*key).into(), slot.original.into()); + for (key, slot) in &loaded_acc.storage.slots { + acc_state.storage.insert((*key).into(), slot.value.original.into()); } } @@ -447,21 +445,21 @@ impl<'a> GethTraceBuilder<'a> { fn geth_prestate_diff_traces( &self, - state: &TxState, + state: &PendingState, db: &mut dyn DynDatabase, code_enabled: bool, storage_enabled: bool, ) -> DbResult { let mut state_diff = DiffMode::default(); let mut empty_before = AddressSet::default(); - for (addr, changed_acc) in state.accounts.iter() { - let addr = *addr; + for changed_acc in state.account_changes() { + let addr = changed_acc.address; let db_acc = db.get_account(&addr)?.unwrap_or_default(); let pre_code = if code_enabled { load_account_code(db, &db_acc)? } else { None }; // deleted accounts are treated as drained: the post info is empty - let info = changed_acc.current.clone().unwrap_or_default(); + let info = changed_acc.current.cloned().unwrap_or_default(); let mut post_state = AccountState::from_account_info( info.nonce, @@ -491,11 +489,11 @@ impl<'a> GethTraceBuilder<'a> { // Geth keeps the pre state of a selfdestructed account as it looked it up, with // every slot the transaction touched, unchanged and zero ones included. if changed_acc.selfdestructed { - for (key, slot) in changed_acc.storage.iter() { - pre_state.storage.insert((*key).into(), slot.original.into()); + for (key, slot) in &changed_acc.storage.slots { + pre_state.storage.insert((*key).into(), slot.value.original.into()); } } else { - for (key, slot) in changed_acc.changed_storage() { + for (key, slot) in changed_acc.storage.changed_slots() { if !slot.original.is_zero() { pre_state.storage.insert((*key).into(), slot.original.into()); } @@ -566,10 +564,7 @@ impl<'a> GethTraceBuilder<'a> { #[cfg(test)] mod tests { use super::*; - use crate::tracing::{ - tx_state::TxAccount, - types::{CallLog, CallTrace}, - }; + use crate::tracing::types::{CallLog, CallTrace}; use alloy_primitives::{B256, U256, address}; use evm2::{ AccountInfo, DatabaseError, @@ -684,28 +679,17 @@ mod tests { } #[test] - fn prestate_diff_keeps_prefunded_created_accounts() { - let mut state = TxState::default(); + fn prestate_diff_keeps_prefunded_accounts() { + let mut state = PendingState::default(); let prefunded_addr = address!("1000000000000000000000000000000000000001"); let empty_addr = address!("2000000000000000000000000000000000000002"); - state.accounts.insert( + state.insert_account( prefunded_addr, - TxAccount { - original: Some(AccountInfo::default().with_balance(U256::from(10))), - current: Some(AccountInfo::default().with_balance(U256::from(1)).with_nonce(1)), - created: true, - ..TxAccount::default() - }, - ); - state.accounts.insert( - empty_addr, - TxAccount { - current: Some(AccountInfo::default().with_nonce(1)), - created: true, - ..TxAccount::default() - }, + Some(AccountInfo::default().with_balance(U256::from(10))), + Some(AccountInfo::default().with_balance(U256::from(1)).with_nonce(1)), ); + state.insert_account(empty_addr, None, Some(AccountInfo::default().with_nonce(1))); let mut db = CacheDB::new(EmptyDB::default()); db.insert_account_info( @@ -733,16 +717,13 @@ mod tests { #[test] fn prestate_diff_loads_post_code_from_state_changes() { - let mut state = TxState::default(); + let mut state = PendingState::default(); let address = address!("1000000000000000000000000000000000000001"); let code = Bytecode::new_legacy(vec![op::PUSH1, 0x01, op::STOP].into()); // the changed account from the state output always holds the code let account = AccountInfo::default().with_nonce(1).with_code(code.clone()); - state.accounts.insert( - address, - TxAccount { current: Some(account), created: true, ..TxAccount::default() }, - ); + state.insert_account(address, None, Some(account)); let mut db = CacheDB::new(EmptyDB::default()); let builder = GethTraceBuilder::new(Vec::new()); @@ -762,16 +743,9 @@ mod tests { #[test] fn prestate_propagates_db_account_errors() { - let mut state = TxState::default(); + let mut state = PendingState::default(); let address = address!("1000000000000000000000000000000000000001"); - state.accounts.insert( - address, - TxAccount { - current: Some(AccountInfo::default().with_nonce(1)), - created: true, - ..TxAccount::default() - }, - ); + state.insert_account(address, None, Some(AccountInfo::default().with_nonce(1))); let error = DatabaseError::new(evm2::AnyError::from("test database error"), true); let mut db = FailingDb::new(error.clone()); @@ -800,19 +774,17 @@ mod tests { db.insert_account_info(&nonce_account, AccountInfo::default().with_nonce(1)); db.insert_account_info(&contract, AccountInfo::default().with_code(code.clone())); - let mut state = TxState::default(); + let mut state = PendingState::default(); for address in [absent_beneficiary, empty_beneficiary] { - state.accounts.insert( + state.insert_account( address, - TxAccount { - current: Some(AccountInfo::default().with_balance(U256::from(1))), - ..Default::default() - }, + None, + Some(AccountInfo::default().with_balance(U256::from(1))), ); } // An account emptied by execution must retain its original prestate. for address in [funded_beneficiary, nonce_account, contract] { - state.accounts.insert(address, TxAccount::default()); + state.insert_account(address, None, None); } let builder = GethTraceBuilder::new(Vec::new()); diff --git a/crates/inspectors/src/tracing/builder/parity.rs b/crates/inspectors/src/tracing/builder/parity.rs index e4b7d015c..41bd6f2c8 100644 --- a/crates/inspectors/src/tracing/builder/parity.rs +++ b/crates/inspectors/src/tracing/builder/parity.rs @@ -1,6 +1,5 @@ use crate::tracing::{ TracingInspectorConfig, - tx_state::TxState, types::{CallTraceNode, CallTraceStep, StepDelta, TraceMemberOrder}, utils::load_account_code, }; @@ -485,9 +484,9 @@ pub fn populate_state_diff( db: &mut dyn DynDatabase, pending: &PendingState, ) -> DbResult<()> { - let state = TxState::from_pending(pending); let absent = AccountInfo::default(); - for (addr, changed_acc) in state.accounts.iter() { + for changed_acc in pending.account_changes() { + let addr = &changed_acc.address; // An account created and destroyed in this transaction has no net change unless it // already existed, for example with a prefunded balance. if changed_acc.current.is_none() && changed_acc.original.is_none() { @@ -495,7 +494,7 @@ pub fn populate_state_diff( } let existed = changed_acc.original.is_some(); - let original = changed_acc.original.as_ref().unwrap_or(&absent); + let original = changed_acc.original.unwrap_or(&absent); let entry = state_diff.entry(*addr).or_default(); // Use the finalized account state: selfdestruct can preserve a balance-only account. @@ -508,7 +507,7 @@ pub fn populate_state_diff( continue; } - let info = changed_acc.current.as_ref().expect("deleted accounts handled above"); + let info = changed_acc.current.expect("deleted accounts handled above"); // An absent prestate account is added if it has nonempty final state or // the EVM marked it created (which can preserve an empty account). @@ -521,7 +520,7 @@ pub fn populate_state_diff( entry.code = Delta::Added(account_code); // Only changed slots are added; unchanged zero-valued slots are omitted. - for (key, slot) in changed_acc.changed_storage() { + for (key, slot) in changed_acc.storage.changed_slots() { entry.storage.insert((*key).into(), Delta::Added(slot.current.into())); } } else { @@ -533,7 +532,7 @@ pub fn populate_state_diff( } // update _changed_ storage values - for (key, slot) in changed_acc.changed_storage() { + for (key, slot) in changed_acc.storage.changed_slots() { entry.storage.insert( (*key).into(), Delta::changed(slot.original.into(), slot.current.into()), diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index fa9e3b79d..16587fb08 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -7,7 +7,6 @@ use crate::tracing::{ call_kind_js_string, to_bigint, to_uint8_array, to_uint8_array_value, uint8_array_from_block, }, - tx_state::TxState, types::CallKind, }; use alloc::{ @@ -34,7 +33,7 @@ use core::{ }; use evm2::{ bytecode::Bytecode, - evm::{AccountInfo, DbResult, DynDatabase, State}, + evm::{AccountInfo, DbResult, DynDatabase, PendingState, State}, interpreter::{ Word, opcode::{ @@ -481,10 +480,10 @@ impl ReusableEvmDb { self.with_scope(&mut StateDbReader { state }, f) } - /// Borrows a transaction's materialized changes and backing database during a callback. + /// Borrows a transaction's pending state and backing database during a callback. pub(crate) fn with_changes_scope( &self, - changes: &TxState, + changes: &PendingState, db: &mut dyn DynDatabase, f: impl FnOnce() -> R, ) -> R { @@ -561,7 +560,7 @@ impl EvmDbReader for StateDbReader<'_, '_> { } struct ChangesDbReader<'a> { - changes: &'a TxState, + changes: &'a PendingState, db: &'a mut dyn DynDatabase, } @@ -582,13 +581,16 @@ impl ChangesDbReader<'_> { impl EvmDbReader for ChangesDbReader<'_> { fn read_basic(&mut self, address: &Address) -> DbResult> { - if let Some(account) = self.changes.accounts.get(address) { - return Ok(account.current.clone()); + if let Some(account) = self.changes.account_changes_for(address) { + return Ok(account.current.cloned()); } self.db.get_account(address) } fn read_code(&mut self, address: &Address) -> DbResult { + if let Some((_, code)) = self.changes.account_changes_for(address).and_then(|a| a.code) { + return Ok(code.clone()); + } let Some(info) = self.read_basic(address)? else { return Ok(Bytecode::default()); }; @@ -1236,7 +1238,7 @@ mod tests { let f = result.as_callable().unwrap(); let mut db = CacheDB::new(EmptyDB::default()); - let state = TxState::default(); + let state = PendingState::default(); let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); { let addr = JsValue::from(js_string!(Address::default().to_string())); @@ -1289,7 +1291,7 @@ mod tests { let setup_fn = obj.get(js_string!("setup"), &mut context).unwrap().as_object().unwrap(); let mut db = CacheDB::new(EmptyDB::default()); - let state = TxState::default(); + let state = PendingState::default(); { let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); let addr = JsValue::from(js_string!(Address::default().to_string())); @@ -1357,7 +1359,7 @@ mod tests { .with_nonce(9) .with_code(code.clone()); let mut db = Db::new(BackingDb { address, account, slot, value }); - let changes = TxState::default(); + let changes = PendingState::default(); let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); reusable_db.with_changes_scope(&changes, &mut db, || { let js_db = reusable_db.value().as_object().unwrap(); @@ -1536,7 +1538,7 @@ mod tests { let stack = vec![U256::from(42)]; let memory = RefCell::new(vec![7u8; 32]); let contract = Contract::default(); - let state = TxState::default(); + let state = PendingState::default(); let mut database = CacheDB::new(EmptyDB::default()); let result = catch_unwind(AssertUnwindSafe(|| { db.with_changes_scope(&state, &mut database, || { diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 13eab4d39..4b6e6a7a7 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -11,7 +11,6 @@ use crate::tracing::{ }, builtins::{PrecompileList, register_builtins, to_serde_value}, }, - tx_state::TxState, types::CallKind, utils, }; @@ -332,7 +331,6 @@ impl JsInspector { } self.ensure_not_interrupted()?; let TxResultWithState { result, pending_state, .. } = res; - let state = TxState::from_pending(pending_state); let mut output_bytes = None; let mut error = None; @@ -377,7 +375,7 @@ impl JsInspector { error, }; let ctx = ctx.into_js_object(&mut self.ctx)?; - let result = self.reusable_db.with_changes_scope(&state, db, || { + let result = self.reusable_db.with_changes_scope(pending_state, db, || { self.result_fn.call(&self.this, &[ctx.into(), self.reusable_db.value()], &mut self.ctx) }); self.ensure_not_interrupted()?; diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 13aae2cbd..e54ce8d74 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -57,8 +57,6 @@ pub use opcount::OpcodeCountInspector; pub mod types; use types::{CallLog, CallTrace, CallTraceStep}; -mod tx_state; - mod utils; #[cfg(feature = "std")] diff --git a/crates/inspectors/src/tracing/tx_state.rs b/crates/inspectors/src/tracing/tx_state.rs deleted file mode 100644 index 2ac3d019b..000000000 --- a/crates/inspectors/src/tracing/tx_state.rs +++ /dev/null @@ -1,109 +0,0 @@ -//! Materialized per-transaction state collected from a pending-state stream. - -use alloy_primitives::{ - Address, - map::{AddressMap, U256Map}, -}; -use core::convert::Infallible; -use evm2::{ - evm::{ - AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, - StorageChange, Tracked, - }, - interpreter::Word, -}; - -/// A transaction's loaded accounts and storage, materialized from the [`PendingState`] change -/// stream for random access by trace builders. -/// -/// Contains every account and storage slot the transaction loaded: changed entries arrive through -/// the [`StateChangeSink`] change callbacks, loaded-but-unchanged ones through its read callbacks. -#[derive(Debug, Default)] -pub(crate) struct TxState { - /// Accounts loaded by the transaction, keyed by address. - pub(crate) accounts: AddressMap, -} - -impl TxState { - /// Collects a transaction's detached pending state into a materialized view. - pub(crate) fn from_pending(pending: &PendingState) -> Self { - let mut state = Self::default(); - let Ok(()) = pending.visit(&mut state); - state - } -} - -/// One account loaded by the transaction, with its loaded storage slots. -#[derive(Debug, Default)] -pub(crate) struct TxAccount { - /// Account info at the start of the transaction. `None` means the account did not exist. - pub(crate) original: Option, - /// Account info after the transaction. `None` is an explicit deletion. - pub(crate) current: Option, - /// Whether the account was created during the transaction. - pub(crate) created: bool, - /// Whether the account was selfdestructed during the transaction. - pub(crate) selfdestructed: bool, - /// Storage slots loaded during the transaction, changed and unchanged. A loaded slot that the - /// transaction left unchanged, or that a storage wipe reset, has its value before the - /// transaction as both `original` and `current`. - pub(crate) storage: U256Map>, -} - -impl TxAccount { - /// Returns the storage slots the transaction changed. - pub(crate) fn changed_storage(&self) -> impl Iterator)> { - self.storage.iter().filter(|(_, slot)| slot.is_changed()) - } -} - -fn clone_account_info(target: &mut Option, source: Option<&AccountInfo>) { - match (target, source) { - (Some(target), Some(source)) => target.clone_from(source), - (target, Some(source)) => *target = Some(source.clone()), - (target, None) => *target = None, - } -} - -impl StateChangeSink for TxState { - type Error = Infallible; - - fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { - let entry = self.accounts.entry(change.address).or_default(); - clone_account_info(&mut entry.original, change.original); - clone_account_info(&mut entry.current, change.current); - entry.created = change.created; - entry.selfdestructed = change.selfdestructed; - Ok(()) - } - - fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { - self.accounts - .entry(change.address) - .or_default() - .storage - .insert(change.key, Tracked::from_parts(change.original, change.current)); - Ok(()) - } - - fn account_read( - &mut self, - address: Address, - info: Option<&AccountInfo>, - ) -> Result<(), Self::Error> { - let entry = self.accounts.entry(address).or_default(); - clone_account_info(&mut entry.original, info); - entry.current.clone_from(&entry.original); - Ok(()) - } - - fn storage_read( - &mut self, - address: Address, - key: Word, - value: Word, - ) -> Result<(), Self::Error> { - self.accounts.entry(address).or_default().storage.insert(key, Tracked::new(value)); - Ok(()) - } -} From 2b577567dd2bf98a431ea726daa3d770a4ac0b5d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:15:20 +0100 Subject: [PATCH 096/159] refactor(bytecode): add Bytecode::from_stored for code read from state (#944) Code read from state that starts with the EIP-7702 magic but is not a valid delegation designator is plain legacy code, deployed before EIP-3541. `Bytecode::new_raw_checked` rejects it, so the serde decoder and the EEST state loader each repeated the same `unwrap_or_else(|_| Bytecode::new_legacy(..))` fallback. This adds `Bytecode::from_stored`, which parses a valid designator as EIP-7702 and everything else as legacy, and uses it in both places. The fixture loader of the CLI bench runner built every pre-state account with `new_legacy`, so a delegated account was never treated as a delegation. It now uses `from_stored` as well. `new_raw_checked` stays for the block access list decoder, which must reject malformed designators. --- crates/cli/benches/evm/fixture.rs | 4 +-- crates/cli/src/run.rs | 2 +- crates/eest/src/state.rs | 2 +- crates/evm2/src/bytecode/mod.rs | 50 ++++++++++++++++++++++++-- crates/evm2/src/bytecode/serde_impl.rs | 12 ++----- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index b7dca9988..099dd10cb 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -108,7 +108,7 @@ impl Case<'_> { pub(crate) fn state(&self) -> InMemoryDB { let mut db = InMemoryDB::default(); for (address, account) in &self.unit.pre { - let bytecode = Bytecode::new_legacy(account.code.clone()); + let bytecode = Bytecode::from_stored(account.code.clone()); // Keep first-use jump analysis outside the timed execution. let _ = bytecode.legacy_jump_table(); let mut info = AccountInfo::default().with_code(bytecode); @@ -168,7 +168,7 @@ impl Case<'_> { if account.code.is_empty() { return None; } - let bytecode = Bytecode::new_legacy(account.code.clone()); + let bytecode = Bytecode::from_stored(account.code.clone()); Some(CompiledAccount { code_hash: bytecode.hash_slow(), bytecode: account.code.clone(), diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index c77b82902..938f213b7 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -673,7 +673,7 @@ fn parse_fixture_bench(bench: &Bench, fixture_path: &str) -> eyre::Result Bytecode { - Bytecode::new_raw_checked(code.clone()).unwrap_or_else(|_| Bytecode::new_legacy(code)) + Bytecode::from_stored(code) } /// Inserts an account and its storage into an in-memory database. diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index 4e146eeef..9e8079244 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -217,11 +217,31 @@ impl Bytecode { } } + /// Creates a new [`Bytecode`] from code read from state. + /// + /// Valid EIP-7702 delegation designators become EIP-7702 bytecode and anything else is legacy + /// code. Unlike [`Self::new_raw_checked`], code starting with the EIP-7702 magic that is not a + /// valid designator is accepted as legacy, since it may have been deployed before EIP-3541. + #[inline] + pub fn from_stored(bytes: Bytes) -> Self { + if Self::check_eip7702(&bytes).is_ok() { + Self::from_valid_eip7702(bytes) + } else { + Self::new_legacy(bytes) + } + } + /// Creates a new EIP-7702 [`Bytecode`] from raw bytes. /// /// Returns an error if the bytes are not valid EIP-7702 bytecode. #[inline] pub fn new_eip7702_raw(bytes: Bytes) -> Result { + Self::check_eip7702(&bytes)?; + Ok(Self::from_valid_eip7702(bytes)) + } + + /// Checks that `bytes` is a valid EIP-7702 delegation designator. + fn check_eip7702(bytes: &[u8]) -> Result<(), Eip7702DecodeError> { if bytes.len() != EIP7702_BYTECODE_LEN { return Err(Eip7702DecodeError::InvalidLength); } @@ -231,13 +251,18 @@ impl Bytecode { if bytes[2] != EIP7702_VERSION { return Err(Eip7702DecodeError::UnsupportedVersion); } - Ok(Self(Some(Arc::new(BytecodeInner { + Ok(()) + } + + /// Wraps `bytes`, which [`Self::check_eip7702`] accepted, as EIP-7702 bytecode. + fn from_valid_eip7702(bytes: Bytes) -> Self { + Self(Some(Arc::new(BytecodeInner { kind: BytecodeKind::Eip7702, original_len: bytes.len(), bytecode: bytes, jump_table: OnceLock::new(), hash: OnceLock::new(), - })))) + }))) } /// Create new checked bytecode from pre-analyzed components. @@ -508,6 +533,27 @@ mod tests { assert_eq!(&bytecode.original_byte_slice()[..3], &[0xEF, 0x01, 0x00]); } + #[test] + fn from_stored_accepts_non_designator_magic_as_legacy() { + let address = Address::new([0x01; 20]); + let designator = Bytecode::new_eip7702(address).original_bytes(); + let from_designator = Bytecode::from_stored(designator.clone()); + assert_eq!(from_designator.kind(), BytecodeKind::Eip7702); + assert_eq!(from_designator.eip7702_address(), Some(address)); + + for bytes in [ + Bytes::from_static(&[0xEF, 0x01, 0x00, 0x60, 0x01]), + designator.slice(..EIP7702_BYTECODE_LEN - 1), + Bytes::from([designator.as_ref(), &[op::STOP]].concat()), + Bytes::from_static(&[0xEF, 0x01, 0x01]), + ] { + assert!(Bytecode::new_raw_checked(bytes.clone()).is_err()); + let bytecode = Bytecode::from_stored(bytes.clone()); + assert_eq!(bytecode.kind(), BytecodeKind::Legacy); + assert_eq!(bytecode.original_bytes(), bytes); + } + } + #[test] fn seed_hash_sets_only_an_empty_cache() { let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); diff --git a/crates/evm2/src/bytecode/serde_impl.rs b/crates/evm2/src/bytecode/serde_impl.rs index e9489421f..2cda097f4 100644 --- a/crates/evm2/src/bytecode/serde_impl.rs +++ b/crates/evm2/src/bytecode/serde_impl.rs @@ -44,7 +44,7 @@ impl<'de> Deserialize<'de> for Bytecode { fn deserialize>(deserializer: D) -> Result { if deserializer.is_human_readable() { match BytecodeSerde::deserialize(deserializer)? { - BytecodeSerde::New(bytes) => Ok(from_original_bytes(bytes)), + BytecodeSerde::New(bytes) => Ok(Self::from_stored(bytes)), BytecodeSerde::Old(bytecode_serde_old) => from_old(bytecode_serde_old), } } else { @@ -63,7 +63,7 @@ impl<'de> Visitor<'de> for BytecodeVisitor { } fn visit_bytes(self, value: &[u8]) -> Result { - Ok(from_original_bytes(Bytes::copy_from_slice(value))) + Ok(Bytecode::from_stored(Bytes::copy_from_slice(value))) } // Self-describing binary formats such as MessagePack may hold revm's enum encoding. @@ -86,14 +86,6 @@ fn from_old(old: BytecodeSerdeOld) -> Result { } } -/// Decodes bytecode from its serialized original bytes. -/// -/// Legacy code may start with the EIP-7702 magic without being a valid delegation (e.g. code -/// deployed before EIP-3541), so it falls back to legacy instead of failing to round-trip. -fn from_original_bytes(bytes: Bytes) -> Bytecode { - Bytecode::new_raw_checked(bytes.clone()).unwrap_or_else(|_| Bytecode::new_legacy(bytes)) -} - #[cfg(test)] mod tests { use super::*; From 4239984cf400febf5923d8f642f850ffba19efa1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 15:18:33 +0100 Subject: [PATCH 097/159] refactor(eest): share one fixture kind detector with the cli (#983) The cli and the additional-tests runner each kept a hand-written list of case fields that tell a state fixture from a blockchain fixture, and a second copy of the detection logic. This replaces both with one public `evm2_eest::FixtureKind` that the cli and the runner share. `FixtureKind::from_json` streams the first case's keys, and `FixtureKind::from_path` covers the binary format, which only holds blockchain tests and has no header to sniff. The two copies disagreed on one point: the cli's streaming reader fell through to later cases when the first case had no known field, while its own `Value` reader and the runner looked only at the first object case. The shared detector uses the stricter rule and inspects only the first case. The field lists were identical. A first top-level value that is not an object is now a decode error instead of being skipped; no fixture in the downloaded corpus has one. --- crates/cli/src/fixture.rs | 157 ++------------------------------ crates/cli/src/list.rs | 12 +-- crates/cli/src/replay/mod.rs | 13 ++- crates/eest/src/additional.rs | 25 +---- crates/eest/src/fixture_io.rs | 2 +- crates/eest/src/fixture_kind.rs | 146 +++++++++++++++++++++++++++++ crates/eest/src/lib.rs | 7 +- 7 files changed, 174 insertions(+), 188 deletions(-) create mode 100644 crates/eest/src/fixture_kind.rs diff --git a/crates/cli/src/fixture.rs b/crates/cli/src/fixture.rs index c541cc659..ec16c30c6 100644 --- a/crates/cli/src/fixture.rs +++ b/crates/cli/src/fixture.rs @@ -1,24 +1,7 @@ use crate::error::{Error, Result}; -use serde::de::{DeserializeSeed, Deserializer, IgnoredAny, MapAccess, Visitor}; -use serde_json::Value; -use std::{fmt, path::Path}; - -pub(crate) struct FixtureInput { - pub(crate) json: Value, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum FixtureKind { - StateTest, - BlockchainTest, -} - -pub(crate) fn read(path: &Path) -> Result { - let text = read_text(path)?; - let json = serde_json::from_str(&text) - .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source })?; - Ok(FixtureInput { json }) -} +use evm2_eest::FixtureKind; +use serde::de::IgnoredAny; +use std::{collections::BTreeMap, path::Path}; pub(crate) fn read_blockchain(path: &Path) -> Result { evm2_eest::read_blockchain_fixture(path) @@ -30,135 +13,15 @@ pub(crate) fn read_text(path: &Path) -> Result { .map_err(|source| Error::ReadInput { path: path.to_path_buf(), source }) } -pub(crate) fn is_binary_path(path: &Path) -> bool { - evm2_eest::is_binary_fixture_path(path) -} - +/// Detects the kind of a JSON fixture from its text. pub(crate) fn detect_str(path: &Path, input: &str) -> Result> { - struct FixtureKindVisitor; - - impl<'de> Visitor<'de> for FixtureKindVisitor { - type Value = Option; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an EEST fixture object") - } - - fn visit_map(self, mut map: A) -> std::result::Result - where - A: MapAccess<'de>, - { - let mut kind = None; - while map.next_key::()?.is_some() { - if kind.is_none() { - kind = map.next_value_seed(FixtureCaseVisitor)?; - } else { - map.next_value::()?; - } - } - Ok(kind) - } - } - - struct FixtureCaseVisitor; - - impl<'de> DeserializeSeed<'de> for FixtureCaseVisitor { - type Value = Option; - - fn deserialize(self, deserializer: D) -> std::result::Result - where - D: serde::Deserializer<'de>, - { - deserializer.deserialize_any(self) - } - } - - impl<'de> Visitor<'de> for FixtureCaseVisitor { - type Value = Option; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an EEST fixture case object") - } - - fn visit_map(self, mut map: A) -> std::result::Result - where - A: MapAccess<'de>, - { - let mut is_blockchain = false; - let mut is_state = false; - while let Some(field) = map.next_key::<&str>()? { - is_blockchain |= - matches!(field, "blocks" | "genesisBlockHeader" | "lastblockhash" | "network"); - is_state |= matches!(field, "env" | "post" | "transaction"); - map.next_value::()?; - } - Ok(if is_blockchain { - Some(FixtureKind::BlockchainTest) - } else if is_state { - Some(FixtureKind::StateTest) - } else { - None - }) - } - } - - serde_json::Deserializer::from_str(input) - .deserialize_any(FixtureKindVisitor) + FixtureKind::from_json(input) .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source }) } -pub(crate) fn detect(value: &Value) -> Option { - let cases = value.as_object()?; - let first = cases.values().find_map(Value::as_object)?; - - if has_any(first, &["blocks", "genesisBlockHeader", "lastblockhash", "network"]) { - return Some(FixtureKind::BlockchainTest); - } - if has_any(first, &["env", "post", "transaction"]) { - return Some(FixtureKind::StateTest); - } - None -} - -pub(crate) fn test_names(value: &Value) -> Option> { - let mut names = value.as_object()?.keys().map(String::as_str).collect::>(); - names.sort_unstable(); - Some(names) -} - -fn has_any(object: &serde_json::Map, fields: &[&str]) -> bool { - fields.iter().any(|field| object.contains_key(*field)) -} - -#[cfg(test)] -mod tests { - use super::{FixtureKind, detect, detect_str, test_names}; - use serde_json::json; - use std::path::Path; - - #[test] - fn detects_blockchain_fixture() { - let value = json!({"case": {"blocks": [], "pre": {}, "network": "Prague"}}); - assert_eq!(detect(&value), Some(FixtureKind::BlockchainTest)); - assert_eq!( - detect_str(Path::new("fixture.json"), &value.to_string()).unwrap(), - Some(FixtureKind::BlockchainTest) - ); - } - - #[test] - fn detects_state_fixture() { - let value = json!({"case": {"env": {}, "pre": {}, "post": {}, "transaction": {}}}); - assert_eq!(detect(&value), Some(FixtureKind::StateTest)); - assert_eq!( - detect_str(Path::new("fixture.json"), &value.to_string()).unwrap(), - Some(FixtureKind::StateTest) - ); - } - - #[test] - fn lists_top_level_case_names() { - let value = json!({"b": {"env": {}}, "a": {"env": {}}}); - assert_eq!(test_names(&value).unwrap(), ["a", "b"]); - } +/// Returns the sorted top-level test names of a JSON fixture. +pub(crate) fn test_names(path: &Path, input: &str) -> Result> { + let cases = serde_json::from_str::>(input) + .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source })?; + Ok(cases.into_keys().collect()) } diff --git a/crates/cli/src/list.rs b/crates/cli/src/list.rs index 46e1784b8..11b42e4e9 100644 --- a/crates/cli/src/list.rs +++ b/crates/cli/src/list.rs @@ -3,9 +3,10 @@ use crate::{ error::{Error, Result}, fixture, }; +use evm2_eest::FixtureKind; pub(crate) fn run(command: List) -> Result<()> { - if fixture::is_binary_path(&command.path) { + if FixtureKind::from_path(&command.path).is_some() { let suite = fixture::read_blockchain(&command.path)?; let mut names = suite.0.keys().map(String::as_str).collect::>(); names.sort_unstable(); @@ -15,14 +16,11 @@ pub(crate) fn run(command: List) -> Result<()> { return Ok(()); } - let input = fixture::read(&command.path)?; - if fixture::detect(&input.json).is_none() { + let input = fixture::read_text(&command.path)?; + if fixture::detect_str(&command.path, &input)?.is_none() { return Err(Error::UnknownFixtureKind { path: command.path }); } - let Some(names) = fixture::test_names(&input.json) else { - return Err(Error::UnknownFixtureKind { path: command.path }); - }; - for name in names { + for name in fixture::test_names(&command.path, &input)? { println!("{name}"); } Ok(()) diff --git a/crates/cli/src/replay/mod.rs b/crates/cli/src/replay/mod.rs index 83d76620b..fad320f08 100644 --- a/crates/cli/src/replay/mod.rs +++ b/crates/cli/src/replay/mod.rs @@ -1,16 +1,15 @@ use crate::{ args::Replay, error::{Error, Result}, - fixture::{self, FixtureKind}, - style, + fixture, style, }; use alloy_primitives::U256; use evm2_eest::{ BlockchainTestBlockFailed, BlockchainTestBlockFinished, BlockchainTestBlockStarted, BlockchainTestCaseStarted, BlockchainTestExecuteConfig, BlockchainTestExecutionMode, BlockchainTestHook, BlockchainTestTransactionFailed, BlockchainTestTransactionFinished, - BlockchainTestTransactionStarted, NameFilter, StateTestExecuteConfig, StateTestExecutionMode, - execute_blockchain_tests_str, execute_blockchain_tests_suite, + BlockchainTestTransactionStarted, FixtureKind, NameFilter, StateTestExecuteConfig, + StateTestExecutionMode, execute_blockchain_tests_str, execute_blockchain_tests_suite, execute_state_tests_str_with_filter, }; use std::{ @@ -145,7 +144,7 @@ fn run_dir(dir: &Path, options: &ReplayOptions) -> Result<()> { /// Replays one fixture file, auto-detecting its kind (binary blockchain, or a /// text state/blockchain test). fn replay_file(path: &Path, options: &ReplayOptions) -> Result { - if fixture::is_binary_path(path) { + if FixtureKind::from_path(path).is_some() { let suite = fixture::read_blockchain(path)?; let mut hook = ReplayProgressHook::default(); let summary = execute_blockchain_tests_suite( @@ -174,7 +173,7 @@ fn replay_file(path: &Path, options: &ReplayOptions) -> Result { let input = fixture::read_text(path)?; match fixture::detect_str(path, &input)? { - Some(FixtureKind::StateTest) => { + Some(FixtureKind::State) => { let summary = execute_state_tests_str_with_filter( path, &input, @@ -190,7 +189,7 @@ fn replay_file(path: &Path, options: &ReplayOptions) -> Result { .map_err(|source| Error::StateTest { source })?; Ok(FileReplay { kind: "state", executed: summary.executed, skipped: summary.skipped }) } - Some(FixtureKind::BlockchainTest) => { + Some(FixtureKind::Blockchain) => { let mut hook = ReplayProgressHook::default(); let summary = execute_blockchain_tests_str( path, diff --git a/crates/eest/src/additional.rs b/crates/eest/src/additional.rs index 30f2d069a..17f87710c 100644 --- a/crates/eest/src/additional.rs +++ b/crates/eest/src/additional.rs @@ -9,6 +9,7 @@ use crate::{ execute::{self, ExecuteConfig as StateConfig}, filter::NameFilter, fixture_io, + fixture_kind::FixtureKind, harness::{TestRoot, TestSuite, descend_all}, }; use libtest_mimic::Failed; @@ -39,7 +40,7 @@ fn should_ignore(name: &str) -> bool { fn run_file(path: PathBuf) -> Result<(), Failed> { let input = fixture_io::read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?; - match detect_kind(&input) { + match FixtureKind::from_json(&input).map_err(|err| format!("{}: {err}", path.display()))? { Some(FixtureKind::Blockchain) => execute_str( &path, &input, @@ -57,25 +58,3 @@ fn run_file(path: PathBuf) -> Result<(), Failed> { None => Err(format!("could not detect fixture kind in {}", path.display()).into()), } } - -enum FixtureKind { - State, - Blockchain, -} - -/// Detects a fixture's kind from the fields of its first case. -fn detect_kind(input: &str) -> Option { - let value: serde_json::Value = serde_json::from_str(input).ok()?; - let first = value.as_object()?.values().find_map(serde_json::Value::as_object)?; - if has_any(first, &["blocks", "genesisBlockHeader", "lastblockhash", "network"]) { - Some(FixtureKind::Blockchain) - } else if has_any(first, &["env", "post", "transaction"]) { - Some(FixtureKind::State) - } else { - None - } -} - -fn has_any(object: &serde_json::Map, fields: &[&str]) -> bool { - fields.iter().any(|field| object.contains_key(*field)) -} diff --git a/crates/eest/src/fixture_io.rs b/crates/eest/src/fixture_io.rs index 710221009..2ae2f4d92 100644 --- a/crates/eest/src/fixture_io.rs +++ b/crates/eest/src/fixture_io.rs @@ -38,7 +38,7 @@ pub enum FixtureWriteError { } /// Returns true when the path uses the binary fixture extension. -pub fn is_binary_path(path: &Path) -> bool { +pub(crate) fn is_binary_path(path: &Path) -> bool { has_extension(path, "bin") || is_zstd_path(path) && stem_has_extension(path, "bin") } diff --git a/crates/eest/src/fixture_kind.rs b/crates/eest/src/fixture_kind.rs new file mode 100644 index 000000000..988d37517 --- /dev/null +++ b/crates/eest/src/fixture_kind.rs @@ -0,0 +1,146 @@ +//! Detection of the kind of an EEST fixture file. + +use crate::fixture_io; +use serde::de::{DeserializeSeed, Deserializer, IgnoredAny, MapAccess, Visitor}; +use std::{borrow::Cow, fmt, path::Path}; + +/// Case fields only blockchain tests have. +const BLOCKCHAIN_FIELDS: &[&str] = &["blocks", "genesisBlockHeader", "lastblockhash", "network"]; + +/// Case fields only state tests have. +const STATE_FIELDS: &[&str] = &["env", "post", "transaction"]; + +/// The kind of an EEST fixture. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FixtureKind { + /// A state test. + State, + /// A blockchain test, in JSON or postcard binary form. + Blockchain, +} + +impl FixtureKind { + /// Returns the kind of a binary fixture from its path, or `None` for any other path. + /// + /// The binary format has no header to sniff, and only holds blockchain tests. + pub fn from_path(path: &Path) -> Option { + fixture_io::is_binary_path(path).then_some(Self::Blockchain) + } + + /// Detects the kind of a JSON fixture from the fields of its first case. + /// + /// A case with any of `blocks`, `genesisBlockHeader`, `lastblockhash` or `network` is a + /// blockchain test; otherwise a case with any of `env`, `post` or `transaction` is a state + /// test. Only the first case is inspected, and the rest of the input is skipped without + /// being materialized. Returns `Ok(None)` when the first case has none of these fields, or + /// the input has no case. + pub fn from_json(input: &str) -> Result, serde_json::Error> { + serde_json::Deserializer::from_str(input).deserialize_map(FixtureVisitor) + } +} + +struct FixtureVisitor; + +impl<'de> Visitor<'de> for FixtureVisitor { + type Value = Option; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an EEST fixture object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut kind = None; + let mut seen_case = false; + while map.next_key::()?.is_some() { + if seen_case { + map.next_value::()?; + } else { + seen_case = true; + kind = map.next_value_seed(CaseSeed)?; + } + } + Ok(kind) + } +} + +struct CaseSeed; + +impl<'de> DeserializeSeed<'de> for CaseSeed { + type Value = Option; + + fn deserialize>(self, deserializer: D) -> Result { + deserializer.deserialize_map(self) + } +} + +impl<'de> Visitor<'de> for CaseSeed { + type Value = Option; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an EEST fixture case object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut blockchain = false; + let mut state = false; + while let Some(field) = map.next_key::>()? { + blockchain |= BLOCKCHAIN_FIELDS.contains(&&*field); + state |= STATE_FIELDS.contains(&&*field); + map.next_value::()?; + } + Ok(if blockchain { + Some(FixtureKind::Blockchain) + } else if state { + Some(FixtureKind::State) + } else { + None + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn detects_state_json() { + let input = r#"{"case": {"env": {}, "pre": {}, "post": {}, "transaction": {}}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::State)); + } + + #[test] + fn detects_blockchain_json() { + let input = r#"{"case": {"blocks": [], "pre": {}, "network": "Prague"}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::Blockchain)); + } + + #[test] + fn detects_binary_path() { + for path in ["fixture.bin", "fixture.bin.zst"] { + assert_eq!(FixtureKind::from_path(Path::new(path)), Some(FixtureKind::Blockchain)); + } + for path in ["fixture.json", "fixture.json.zst"] { + assert_eq!(FixtureKind::from_path(Path::new(path)), None); + } + } + + #[test] + fn rejects_unknown_input() { + assert_eq!(FixtureKind::from_json("{}").unwrap(), None); + assert_eq!(FixtureKind::from_json(r#"{"case": {"pre": {}}}"#).unwrap(), None); + assert!(FixtureKind::from_json("[]").is_err()); + assert!(FixtureKind::from_json("not json").is_err()); + } + + #[test] + fn decodes_escaped_field_names() { + let input = r#"{"case": {"\u0062locks": []}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::Blockchain)); + } + + #[test] + fn inspects_only_the_first_case() { + let input = r#"{"a": {"pre": {}}, "b": {"env": {}}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), None); + } +} diff --git a/crates/eest/src/lib.rs b/crates/eest/src/lib.rs index be4915d2c..688755e64 100644 --- a/crates/eest/src/lib.rs +++ b/crates/eest/src/lib.rs @@ -15,6 +15,7 @@ mod execute; mod execution; mod filter; mod fixture_io; +mod fixture_kind; mod fixtures; mod forks; mod harness; @@ -47,10 +48,10 @@ pub use execute::{ }; pub use filter::NameFilter; pub use fixture_io::{ - FixtureReadError, FixtureWriteError, is_binary_path as is_binary_fixture_path, - read_blockchain as read_blockchain_fixture, read_to_string as read_fixture_text, - write_blockchain as write_blockchain_fixture, + FixtureReadError, FixtureWriteError, read_blockchain as read_blockchain_fixture, + read_to_string as read_fixture_text, write_blockchain as write_blockchain_fixture, }; +pub use fixture_kind::FixtureKind; pub use runner::run as run_statetests; pub use tx::{AccessListItem, TestAuthorization}; pub use types::{ From 0b04d2abf3052357fdede608c8e30170fcf256d6 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 2 Nov 2026 15:40:55 +0100 Subject: [PATCH 098/159] refactor(eest): use ForkSpec for state test fork names (#978) State tests parsed fork names into `SpecName`, a second copy of `ForkSpec` with its own serde name table and its own spec mapping. They now deserialize `ForkSpec`, so both runners share one enum and one name table, and `SpecName` and the `StateTestSpecName` re-export are gone. The runners still differ where they must. A state test runs one transaction under one stage of the fork schedule the blockchain runner builds, so a transition fork runs under the stage after the transition (`ForkSpec::to_state_spec_id` returns its spec). The blob base fee comes from the blob parameters of that stage, read from the fixture's `config.blobSchedule` as the blockchain runner reads it, so a BPO fork runs with its own update fraction instead of the Osaka one. Fixtures without a config, such as the legacy ones, use the mainnet parameters, now one table in `BlobFork::mainnet_params` that the capture exporter shares. Constantinople stays unsupported, now in one place: `ForkSpec::to_spec_id` returns `None` for it. evm2 has no `CONSTANTINOPLE` spec, and Constantinople differs from Petersburg by EIP-1283 net gas metering, so running it under Petersburg would charge the wrong gas. The Merge feature forks and the DAO fork are unsupported there too. A state test with an unsupported fork fails, as a blockchain test does, except for Constantinople, which legacy fixtures run beside Petersburg. No fixture the runners load uses any of these forks as a network, so no result changes. --- crates/cli/benches/evm/fixture.rs | 2 +- crates/cli/src/capture/export.rs | 18 +-- crates/cli/src/run.rs | 2 +- crates/eest/src/blockchaintest/execute.rs | 18 ++- crates/eest/src/blockchaintest/mod.rs | 1 + crates/eest/src/blockchaintest/types.rs | 47 ++++++- crates/eest/src/error.rs | 15 ++- crates/eest/src/execute.rs | 83 +++++++++---- crates/eest/src/lib.rs | 4 +- crates/eest/src/types.rs | 144 +++++++++------------- 10 files changed, 195 insertions(+), 139 deletions(-) diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index 099dd10cb..d6d07dada 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -62,7 +62,7 @@ impl Suite { let post = unit .post .iter() - .filter(|(spec_name, _)| spec_name.to_spec_id() == Some(spec)) + .filter(|(spec_name, _)| spec_name.to_state_spec_id() == Some(spec)) .flat_map(|(_, posts)| posts) .next() .unwrap_or_else(|| panic!("fixture suite does not contain {name} post for {spec:?}")); diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index 7538605dc..bc815a525 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -7,7 +7,7 @@ use alloy_consensus::{ EthereumTxEnvelope, Header, TxEip4844, transaction::{Transaction as _, to_eip155_value}, }; -use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization, eip7840::BlobParams}; +use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; use alloy_hardforks::EthereumHardfork; use alloy_primitives::{Address, B256, Bytes, FixedBytes, U256}; use evm2::SpecId; @@ -116,16 +116,16 @@ fn mainnet_blob_fork(header: &Header) -> Result, CaptureError> /// Returns the mainnet blob parameters of every fork up to `last`, or none before Cancun. fn mainnet_blob_schedule(last: Option) -> BTreeMap { [ - (BlobFork::Cancun, BlobParams::cancun()), - (BlobFork::Prague, BlobParams::prague()), - (BlobFork::Osaka, BlobParams::osaka()), - (BlobFork::Bpo1, BlobParams::bpo1()), - (BlobFork::Bpo2, BlobParams::bpo2()), - (BlobFork::Amsterdam, BlobParams::bpo2()), + BlobFork::Cancun, + BlobFork::Prague, + BlobFork::Osaka, + BlobFork::Bpo1, + BlobFork::Bpo2, + BlobFork::Amsterdam, ] .into_iter() - .filter(|(fork, _)| Some(*fork) <= last) - .map(|(fork, params)| (fork, params.into())) + .filter(|fork| Some(*fork) <= last) + .filter_map(|fork| Some((fork, fork.mainnet_params()?.into()))) .collect() } diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 938f213b7..6461e5625 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -739,7 +739,7 @@ fn select_case<'a>( let post = unit .post .iter() - .filter(|(spec_name, _)| spec_name.to_spec_id() == Some(spec_id)) + .filter(|(spec_name, _)| spec_name.to_state_spec_id() == Some(spec_id)) .flat_map(|(_, posts)| posts) .next() .ok_or_else(|| { diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 911046d94..dcd7ae33a 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -1699,10 +1699,10 @@ struct ForkSchedule { /// What is active between two transitions: a spec, and the fork that sets the blob parameters. #[derive(Clone, Copy)] -struct ForkStage { - spec: SpecId, +pub(crate) struct ForkStage { + pub(crate) spec: SpecId, /// `None` before Cancun. - blob_fork: Option, + pub(crate) blob_fork: Option, } /// When a transition fork activates. @@ -1819,7 +1819,17 @@ pub fn blob_params( .map_err(|err| TestError::case(path, name, err)) } -/// Returns the specs of a fork, or `None` for the DAO fork, which the runner does not support. +/// Returns the stage a state test runs a fork under, or `None` for an unsupported fork. +/// +/// A state test runs one transaction under one stage, so a transition fork runs under the stage +/// after the transition. +pub(crate) fn state_stage(fork: ForkSpec) -> Option { + let schedule = fork_schedule(fork)?; + Some(schedule.transition.map_or(schedule.initial, |(_, stage)| stage)) +} + +/// Returns the specs of a fork, or `None` for the forks the runner does not support: the DAO fork, +/// Constantinople and the Merge feature forks. const fn fork_schedule(fork: ForkSpec) -> Option { use Activation::{Block, Timestamp}; const AT_5: Activation = Block(5); diff --git a/crates/eest/src/blockchaintest/mod.rs b/crates/eest/src/blockchaintest/mod.rs index 98017744b..9e6768ca8 100644 --- a/crates/eest/src/blockchaintest/mod.rs +++ b/crates/eest/src/blockchaintest/mod.rs @@ -8,6 +8,7 @@ mod runner; mod types; pub use error::TestError; +pub(crate) use execute::state_stage; pub use execute::{ ExecuteConfig, ExecuteSummary, ExecutionMode, blob_params, execute_case_on, execute_str, execute_suite, pre_state, diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 60ef98be7..e1b05b84b 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -339,7 +339,12 @@ pub enum ForkSpec { impl ForkSpec { /// Returns the spec ID a fork runs under, or `None` for a transition fork, which runs under - /// several specs, and for the DAO fork. + /// several specs, and for the forks no runner supports. + /// + /// evm2 has no `CONSTANTINOPLE` spec. Constantinople differs from Petersburg by EIP-1283 net + /// gas metering for `SSTORE`, so running it under Petersburg would charge the wrong gas. The + /// Merge feature forks enable single EIPs on top of Merge, which no spec ID expresses, and the + /// DAO fork applies an irregular state change. pub const fn to_spec_id(self) -> Option { Some(match self { Self::Frontier => SpecId::FRONTIER, @@ -347,19 +352,21 @@ impl ForkSpec { Self::EIP150 => SpecId::TANGERINE, Self::EIP158 => SpecId::SPURIOUS_DRAGON, Self::Byzantium => SpecId::BYZANTIUM, - Self::Constantinople | Self::ConstantinopleFix => SpecId::PETERSBURG, + Self::ConstantinopleFix => SpecId::PETERSBURG, Self::Istanbul => SpecId::ISTANBUL, Self::Berlin => SpecId::BERLIN, Self::London => SpecId::LONDON, - Self::Paris | Self::MergeEOF | Self::MergeMeterInitCode | Self::MergePush0 => { - SpecId::MERGE - } + Self::Paris => SpecId::MERGE, Self::Shanghai => SpecId::SHANGHAI, Self::Cancun => SpecId::CANCUN, Self::Prague => SpecId::PRAGUE, Self::Osaka | Self::BPO1 | Self::BPO2 => SpecId::OSAKA, Self::Amsterdam => SpecId::AMSTERDAM, - Self::FrontierToHomesteadAt5 + Self::Constantinople + | Self::MergeEOF + | Self::MergeMeterInitCode + | Self::MergePush0 + | Self::FrontierToHomesteadAt5 | Self::HomesteadToDaoAt5 | Self::HomesteadToEIP150At5 | Self::EIP158ToByzantiumAt5 @@ -378,6 +385,14 @@ impl ForkSpec { }) } + /// Returns the spec ID a state test runs a fork under, or `None` for an unsupported fork. + /// + /// A state test runs one transaction under one spec, so a transition fork maps to the spec + /// after the transition. The blockchain runner instead runs both specs of a transition. + pub fn to_state_spec_id(self) -> Option { + super::execute::state_stage(self).map(|stage| stage.spec) + } + /// Returns the fork that runs under `spec`, or `None` if no fork does. /// /// Osaka maps to the BPO fork that sets `blob_fork`, which only changes the blob parameters. @@ -453,6 +468,21 @@ pub enum BlobFork { Amsterdam, } +impl BlobFork { + /// Returns the mainnet blob parameters of this fork, or `None` for BPO3 and BPO4, which + /// mainnet has not scheduled. + pub const fn mainnet_params(self) -> Option { + Some(match self { + Self::Cancun => BlobParams::cancun(), + Self::Prague => BlobParams::prague(), + Self::Osaka => BlobParams::osaka(), + Self::Bpo1 => BlobParams::bpo1(), + Self::Bpo2 | Self::Amsterdam => BlobParams::bpo2(), + Self::Bpo3 | Self::Bpo4 => return None, + }) + } +} + /// Blob parameters of one fork. #[derive(Clone, Copy, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] @@ -542,5 +572,10 @@ mod tests { Some(ForkSpec::BPO2) ); assert_eq!(ForkSpec::BPO2ToAmsterdamAtTime15k.to_spec_id(), None); + assert_eq!(ForkSpec::Constantinople.to_spec_id(), None); + assert_eq!(ForkSpec::Constantinople.to_state_spec_id(), None); + assert_eq!(ForkSpec::MergePush0.to_state_spec_id(), None); + assert_eq!(ForkSpec::BPO1.to_state_spec_id(), Some(SpecId::OSAKA)); + assert_eq!(ForkSpec::BerlinToLondonAt5.to_state_spec_id(), Some(SpecId::LONDON)); } } diff --git a/crates/eest/src/error.rs b/crates/eest/src/error.rs index 14feba739..37457c998 100644 --- a/crates/eest/src/error.rs +++ b/crates/eest/src/error.rs @@ -1,4 +1,8 @@ -use crate::{execution::ExecutionResourceError, tx::TxBuildError}; +use crate::{ + blockchaintest::{BlobFork, ForkSpec}, + execution::ExecutionResourceError, + tx::TxBuildError, +}; use alloy_primitives::{B256, Bytes}; use std::{io, path::PathBuf}; use thiserror::Error; @@ -78,6 +82,15 @@ pub(crate) enum TestErrorKind { /// Numeric value overflowed the target type. #[error("value overflows {0}")] Overflow(&'static str), + /// The runner does not support the test's fork. + #[error("unsupported fork: {0:?}")] + UnsupportedFork(ForkSpec), + /// The test's chain configuration has no blob parameters for the fork. + #[error("blob schedule has no entry for {0:?}")] + MissingBlobSchedule(BlobFork), + /// The test's blob schedule has a zero base fee update fraction for the fork. + #[error("blob schedule entry for {0:?} has a zero base fee update fraction")] + ZeroBlobUpdateFraction(BlobFork), /// Transaction part index was invalid. #[error("bad transaction index: {0}")] BadIndex(&'static str), diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index 63c776eab..b046ffc58 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -1,6 +1,7 @@ #[cfg(feature = "jit")] use crate::compiled::{self, FileSummary}; use crate::{ + blockchaintest::{BlobFork, BlobScheduleEntry, ChainConfig, ForkSpec, state_stage}, dump, error::{TestError, TestErrorKind}, execution::ExecutionResources, @@ -15,7 +16,7 @@ use crate::{ }, types::{AccountInfo, Env, Test, TestSuite, TestUnit, TransactionParts, TxPartIndices}, }; -use alloy_eips::{eip4844, eip7691}; +use alloy_eips::eip7840::BlobParams; use alloy_primitives::{Address, B256, Bytes, Log, TxKind, U256, keccak256}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use alloy_trie::{ @@ -196,14 +197,22 @@ fn execute_unit( db_stats_counts: &mut DbStatsCounts, ) -> Result<(), TestError> { let state = parse_state(&unit.pre).map_err(|err| TestError::case(path, name, err))?; - for (spec_name, posts) in &unit.post { - let Some(spec) = spec_name.to_spec_id() else { - continue; + for (&fork, posts) in &unit.post { + let Some(stage) = state_stage(fork) else { + // Legacy fixtures run each test under Constantinople and Petersburg, and evm2 only + // supports Petersburg. + if fork == ForkSpec::Constantinople { + continue; + } + return Err(TestError::case(path, name, TestErrorKind::UnsupportedFork(fork))); }; + let spec = stage.spec; if is_fork_skipped(spec) { continue; } - let block = parse_block(&unit.env, spec); + let blob_params = blob_params(stage.blob_fork, spec, unit.config.as_ref()) + .map_err(|err| TestError::case(path, name, err))?; + let block = parse_block(&unit.env, blob_params.as_ref()); for post in posts { let tx = match &post.txbytes { Some(bytes) => decode_recovered_tx(bytes, spec).map_err(Into::into), @@ -544,7 +553,7 @@ fn parse_state(pre: &BTreeMap) -> Result BlockEnv { +fn parse_block(env: &Env, blob_params: Option<&BlobParams>) -> BlockEnv { BlockEnvExt { number: env.current_number, beneficiary: env.current_coinbase, @@ -557,32 +566,36 @@ fn parse_block(env: &Env, spec: SpecId) -> BlockEnv { .map_or(U256::ZERO, |random| U256::from_be_slice(random.as_slice())), blob_basefee: env .current_excess_blob_gas - .map_or(U256::ONE, |excess| U256::from(blob_basefee(excess, spec))), + .zip(blob_params) + .map_or(U256::ONE, |(excess, params)| { + U256::from(params.calc_blob_fee(excess.saturating_to())) + }), slot_num: env.slot_number.unwrap_or_default(), ext: (), _non_exhaustive: (), } } -fn blob_basefee(excess_blob_gas: U256, spec: SpecId) -> u128 { - let excess_blob_gas = excess_blob_gas.saturating_to::(); - // EIP-4844 defines blob base fee with fake exponential; EIP-7691 changes the - // update fraction from Prague, and the Amsterdam (BPO2) blob schedule changes it again. - if spec.enables(SpecId::AMSTERDAM) { - eip4844::fake_exponential( - eip4844::BLOB_TX_MIN_BLOB_GASPRICE, - excess_blob_gas as u128, - evm2::constants::BLOB_BASE_FEE_UPDATE_FRACTION_AMSTERDAM as u128, - ) - } else if spec.enables(SpecId::PRAGUE) { - eip7691::calc_blob_gasprice(excess_blob_gas) - } else { - eip4844::fake_exponential( - eip4844::BLOB_TX_MIN_BLOB_GASPRICE, - excess_blob_gas as u128, - eip4844::BLOB_GASPRICE_UPDATE_FRACTION, +/// Returns the blob parameters of the fork that sets them, or `None` before Cancun. +/// +/// They come from the test's `config.blobSchedule`. Fixtures without a configuration use the +/// mainnet parameters. +fn blob_params( + blob_fork: Option, + spec: SpecId, + config: Option<&ChainConfig>, +) -> Result, TestErrorKind> { + let Some(blob_fork) = blob_fork else { return Ok(None) }; + let entry = config + .map_or_else( + || blob_fork.mainnet_params().map(BlobScheduleEntry::from), + |config| config.blob_schedule.get(&blob_fork).copied(), ) + .ok_or(TestErrorKind::MissingBlobSchedule(blob_fork))?; + if entry.base_fee_update_fraction.is_zero() { + return Err(TestErrorKind::ZeroBlobUpdateFraction(blob_fork)); } + Ok(Some(entry.params(spec))) } fn build_tx( @@ -679,6 +692,24 @@ mod tests { ]) } + #[test] + fn bpo_state_tests_use_the_blob_parameters_of_their_fork() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([ + (BlobFork::Osaka, BlobParams::osaka().into()), + (BlobFork::Bpo1, BlobParams::bpo1().into()), + ]), + }; + let stage = state_stage(ForkSpec::BPO1).unwrap(); + let params = blob_params(stage.blob_fork, stage.spec, Some(&config)).unwrap().unwrap(); + assert_eq!(params.update_fraction, BlobParams::bpo1().update_fraction); + assert!(matches!( + blob_params(Some(BlobFork::Bpo2), stage.spec, Some(&config)), + Err(TestErrorKind::MissingBlobSchedule(BlobFork::Bpo2)) + )); + } + #[test] fn logs_hash_matches_empty_logs() { assert_eq!(logs_hash(&[]), keccak256([alloy_rlp::EMPTY_LIST_CODE])); @@ -833,7 +864,7 @@ mod tests { let resources = ExecutionResources::new(mode).unwrap(); execute_spec( SpecId::CANCUN, - parse_block(&env, SpecId::CANCUN), + parse_block(&env, Some(&BlobParams::cancun())), parse_state(&pre).unwrap(), &tx, &env, @@ -901,7 +932,7 @@ mod tests { let resources = ExecutionResources::new(mode).unwrap(); execute_spec( SpecId::AMSTERDAM, - parse_block(&env, SpecId::AMSTERDAM), + parse_block(&env, Some(&BlobParams::bpo2())), parse_state(&pre).unwrap(), &tx, &env, diff --git a/crates/eest/src/lib.rs b/crates/eest/src/lib.rs index 688755e64..876df39d7 100644 --- a/crates/eest/src/lib.rs +++ b/crates/eest/src/lib.rs @@ -55,8 +55,8 @@ pub use fixture_kind::FixtureKind; pub use runner::run as run_statetests; pub use tx::{AccessListItem, TestAuthorization}; pub use types::{ - AccountInfo as StateTestAccountInfo, Env as StateTestEnv, SpecName as StateTestSpecName, - Test as StateTestPost, TestSuite as StateTestSuite, TestUnit as StateTestUnit, + AccountInfo as StateTestAccountInfo, Env as StateTestEnv, Test as StateTestPost, + TestSuite as StateTestSuite, TestUnit as StateTestUnit, TransactionParts as StateTestTransactionParts, TxPartIndices as StateTestTxPartIndices, }; diff --git a/crates/eest/src/types.rs b/crates/eest/src/types.rs index 48df9b1e3..8e437406d 100644 --- a/crates/eest/src/types.rs +++ b/crates/eest/src/types.rs @@ -1,8 +1,10 @@ -use crate::tx::{AccessListItem, TestAuthorization}; +use crate::{ + blockchaintest::{ChainConfig, ForkSpec}, + tx::{AccessListItem, TestAuthorization}, +}; use alloy_primitives::{Address, B256, Bytes, U256}; -use evm2::SpecId; use serde::{Deserialize, Deserializer, de}; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, fmt}; /// Top-level state test suite. #[derive(Debug, Deserialize)] @@ -19,12 +21,16 @@ pub struct TestUnit { /// Pre-state accounts. pub pre: BTreeMap, /// Expected post-state roots by fork. - pub post: BTreeMap>, + #[serde(deserialize_with = "deserialize_post")] + pub post: BTreeMap>, /// Transaction parts indexed by each post entry. pub transaction: TransactionParts, /// Expected output. #[serde(default)] pub out: Option, + /// Chain configuration the test ran under. + #[serde(default)] + pub config: Option, } /// State test block environment. @@ -126,91 +132,6 @@ pub struct TransactionParts { pub max_fee_per_blob_gas: Option, } -/// State test fork name. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Hash)] -pub enum SpecName { - /// Frontier. - Frontier, - /// Frontier to Homestead transition. - FrontierToHomesteadAt5, - /// Homestead. - Homestead, - /// Homestead to DAO transition. - HomesteadToDaoAt5, - /// Homestead to EIP-150 transition. - HomesteadToEIP150At5, - /// EIP-150 aka Tangerine Whistle. - #[serde(alias = "TangerineWhistle")] - EIP150, - /// EIP-158 aka Spurious Dragon. - #[serde(alias = "SpuriousDragon")] - EIP158, - /// EIP-158 to Byzantium transition. - EIP158ToByzantiumAt5, - /// Byzantium. - Byzantium, - /// Skipped Constantinople transition. - ByzantiumToConstantinopleAt5, - /// Byzantium to Petersburg transition. - ByzantiumToConstantinopleFixAt5, - /// Skipped Constantinople. - Constantinople, - /// Petersburg. - ConstantinopleFix, - /// Istanbul. - Istanbul, - /// Berlin. - Berlin, - /// Berlin to London transition. - BerlinToLondonAt5, - /// London. - London, - /// Paris. - Paris, - /// Merge. - Merge, - /// Shanghai. - Shanghai, - /// Cancun. - Cancun, - /// Prague. - Prague, - /// Osaka. - Osaka, - /// Amsterdam. - Amsterdam, -} - -impl SpecName { - /// Converts the state test fork name to the local spec ID. - #[inline] - pub const fn to_spec_id(self) -> Option { - match self { - Self::Frontier => Some(SpecId::FRONTIER), - Self::FrontierToHomesteadAt5 | Self::Homestead => Some(SpecId::HOMESTEAD), - Self::HomesteadToDaoAt5 | Self::HomesteadToEIP150At5 | Self::EIP150 => { - Some(SpecId::TANGERINE) - } - Self::EIP158 => Some(SpecId::SPURIOUS_DRAGON), - Self::EIP158ToByzantiumAt5 | Self::Byzantium => Some(SpecId::BYZANTIUM), - Self::ByzantiumToConstantinopleAt5 - | Self::ByzantiumToConstantinopleFixAt5 - | Self::ConstantinopleFix => Some(SpecId::PETERSBURG), - Self::Istanbul => Some(SpecId::ISTANBUL), - Self::Berlin => Some(SpecId::BERLIN), - Self::BerlinToLondonAt5 | Self::London => Some(SpecId::LONDON), - Self::Paris | Self::Merge => Some(SpecId::MERGE), - Self::Shanghai => Some(SpecId::SHANGHAI), - Self::Cancun => Some(SpecId::CANCUN), - Self::Prague => Some(SpecId::PRAGUE), - Self::Osaka => Some(SpecId::OSAKA), - Self::Amsterdam => Some(SpecId::AMSTERDAM), - // Skip Constantinople due to the pre-Petersburg reentrancy bug. - Self::Constantinople => None, - } - } -} - /// State test post entry. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] @@ -288,10 +209,55 @@ where .collect() } +/// Deserializes the post entries, rejecting two keys that name the same fork, such as `Paris` and +/// its alias `Merge`. +fn deserialize_post<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: Deserializer<'de>, +{ + struct PostVisitor; + + impl<'de> de::Visitor<'de> for PostVisitor { + type Value = BTreeMap>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map of fork names to post entries") + } + + fn visit_map(self, mut map: A) -> Result + where + A: de::MapAccess<'de>, + { + let mut post = BTreeMap::new(); + while let Some((fork, tests)) = map.next_entry::>()? { + if post.insert(fork, tests).is_some() { + return Err(de::Error::custom(format!("duplicate post entry for {fork:?}"))); + } + } + Ok(post) + } + } + + deserializer.deserialize_map(PostVisitor) +} + #[cfg(test)] mod tests { use super::*; + #[derive(Debug, Deserialize)] + struct Post(#[serde(deserialize_with = "deserialize_post")] BTreeMap>); + + #[test] + fn post_rejects_two_keys_for_one_fork() { + assert!(serde_json::from_str::(r#"{"Paris": [], "Merge": []}"#).is_err()); + assert!(serde_json::from_str::(r#"{"Merge": [], "Paris": []}"#).is_err()); + assert_eq!( + serde_json::from_str::(r#"{"Paris": [], "Cancun": []}"#).unwrap().0.len(), + 2 + ); + } + #[test] fn transaction_value_allows_bigint_marker() { let input = r#"{ From bba1378716f8721fb5b490b8598de42c7b3f865b Mon Sep 17 00:00:00 2001 From: aristide-bayard Date: Mon, 2 Nov 2026 16:57:23 +0100 Subject: [PATCH 099/159] feat(inspector): implement Inspector for tuples and Option (#984) Adds Inspector for (L, R), which forwards every hook to both inspectors, L first; when both call or create hooks return a result, L's wins. Adds Inspector for Option, where None behaves like NoopInspector, so callers can combine and toggle inspectors without a hand-written forwarding type. --- crates/evm2/src/evm/inspector.rs | 234 +++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 6ef6dc767..f30582b69 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -123,6 +123,179 @@ pub struct NoopInspector(()); impl Inspector for NoopInspector {} +/// Runs both inspectors, `L` first. +/// +/// Every hook is forwarded to both. When both `call` or `create` hooks return a result, the result +/// from `L` wins. +impl, R: Inspector> Inspector for (L, R) { + #[inline] + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.initialize_interp(interp); + self.1.initialize_interp(interp); + } + + #[inline] + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.step(interp); + self.1.step(interp); + } + + #[inline] + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.step_end(interp); + self.1.step_end(interp); + } + + #[inline] + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + self.0.log(log, host); + self.1.log(log, host); + } + + #[inline] + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let first = self.0.call(interp, message); + let second = self.1.call(interp, message); + first.or(second) + } + + #[inline] + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.0.call_end(interp, message, result); + self.1.call_end(interp, message, result); + } + + #[inline] + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let first = self.0.create(interp, message); + let second = self.1.create(interp, message); + first.or(second) + } + + #[inline] + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.0.create_end(interp, message, result); + self.1.create_end(interp, message, result); + } + + #[inline] + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + self.0.selfdestruct(contract, target, value, host); + self.1.selfdestruct(contract, target, value, host); + } +} + +/// Forwards every hook to the inner inspector when present; `None` behaves like +/// [`NoopInspector`]. +impl> Inspector for Option { + #[inline] + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.initialize_interp(interp); + } + } + + #[inline] + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.step(interp); + } + } + + #[inline] + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.step_end(interp); + } + } + + #[inline] + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + if let Some(inspector) = self { + inspector.log(log, host); + } + } + + #[inline] + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + self.as_mut()?.call(interp, message) + } + + #[inline] + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + if let Some(inspector) = self { + inspector.call_end(interp, message, result); + } + } + + #[inline] + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + self.as_mut()?.create(interp, message) + } + + #[inline] + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + if let Some(inspector) = self { + inspector.create_end(interp, message, result); + } + } + + #[inline] + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + if let Some(inspector) = self { + inspector.selfdestruct(contract, target, value, host); + } + } +} + impl<'a, T: EvmTypesHost> core::ops::Deref for dyn Inspector + 'a { type Target = dyn NonStaticAny + 'a; @@ -1805,4 +1978,65 @@ mod tests { assert_eq!(interp.gas().remaining(), 1000); } } + + #[test] + fn tuple_inspector_forwards_hooks_and_prefers_first_override() { + let target = Address::from([0x22; 20]); + let mut code = call_code(target); + code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); + return_top_word(&mut code); + let override_inspector = |output: &'static [u8]| OverrideCallInspector { + result: MessageResultExt { + stop: InstrStop::Return, + output: Bytes::from_static(output), + ..Default::default() + }, + min_depth: 1, + call_depth: None, + call_end_stop: None, + }; + + let (result, inspector, _) = run_evm_with_inspector( + code.clone(), + &MessageExt::default(), + 50_000, + (HookInspector::default(), override_inspector(&[0xaa, 0xbb, 0xcc])), + ); + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(Word::from_be_slice(&result.output), Word::from(3)); + assert_eq!(inspector.0.call_depths, [0, 1]); + assert_eq!(inspector.0.call_end_stops, [InstrStop::Return, InstrStop::Return]); + assert_eq!(inspector.1.call_depth, Some(1)); + assert_eq!(inspector.1.call_end_stop, Some(InstrStop::Return)); + + let (result, ..) = run_evm_with_inspector( + code, + &MessageExt::default(), + 50_000, + (override_inspector(&[0xaa]), override_inspector(&[0xaa, 0xbb, 0xcc])), + ); + assert_eq!(Word::from_be_slice(&result.output), Word::from(1)); + } + + #[test] + fn option_inspector_forwards_hooks_only_when_present() { + let target = Address::from([0x22; 20]); + let mut code = call_code(target); + code.extend([op::CALL, op::STOP]); + + let (_, inspector, _) = run_evm_with_inspector( + code.clone(), + &MessageExt::default(), + 50_000, + Some(HookInspector::default()), + ); + let inspector = inspector.unwrap(); + assert_eq!(inspector.call_depths, [0, 1]); + assert_eq!(inspector.call_end_stops, [InstrStop::Stop, InstrStop::Stop]); + + let (result, inspector, _) = + run_evm_with_inspector(code, &MessageExt::default(), 50_000, None::); + assert_matches!(result.stop, InstrStop::Stop); + assert!(inspector.is_none()); + } } From 6cf0538de59ec25649a142eae1fb4784a0b5edce Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:26:31 +0100 Subject: [PATCH 100/159] perf(evm2): pick tco dispatch only on 64-bit targets (#996) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `auto` interpreter dispatch backend now picks `tco` only on 64-bit targets. On 32-bit targets `extern "rust-preserve-none"` gets no register convention: on i686 and armv7 the tail-call handlers take their six arguments on the stack and push callee-saved registers, while x86_64, aarch64, riscv64, s390x and loongarch64 pass them in registers. With this change 32-bit targets fall through to `unpacked`, as they already do on stable. x86_64 and aarch64 are unchanged: `tco` on nightly, `packed` on stable. Measured on i686-unknown-linux-gnu binaries running natively in 32-bit mode on an AMD EPYC 4244P (Zen 4), with `cargo bench -p evm2-cli --bench evm` (median of 3 interleaved runs; `asm-keccak` and `bn254-mcl` disabled, as they do not build for that target). Wall time per iteration, `unpacked` against `tco`: | case | tco | unpacked | change | |---|---|---|---| | fibonacci-calldata | 64 µs | 47 µs | 🟢 -26.0% | | snailtracer | 87.7 ms | 65.3 ms | 🟢 -25.6% | | burntpix | 69.5 ms | 55.0 ms | 🟢 -20.8% | | erc20_transfer | 4.52 ms | 3.39 ms | 🟢 -25.1% | | curve_stableswap | 110 µs | 85 µs | 🟢 -22.5% | | uniswap_v3_pool | 26 µs | 23 µs | 🟢 -11.3% | | mainnet 25347446-25347455 replay | 275 ms | 267 ms | 🟢 -2.8% | | geomean, 19 cases ≥ 5 µs | | | 🟢 -13.3% | --- crates/evm2/build.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/evm2/build.rs b/crates/evm2/build.rs index 6477bc1a6..0105646ed 100644 --- a/crates/evm2/build.rs +++ b/crates/evm2/build.rs @@ -59,10 +59,9 @@ impl DispatchBackend { Self::Auto => { if is_wasm { Self::SingleReturn - } else if !no_tco && rustc_is_nightly() { - Self::Tco } else if target_pointer_width == Some(64) { - Self::Packed + // `rust-preserve-none` passes arguments on the stack on 32-bit targets. + if !no_tco && rustc_is_nightly() { Self::Tco } else { Self::Packed } } else { Self::Unpacked } From 767a125acff34c173dc8199c62bf6d52799ef2fa Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:45:32 +0100 Subject: [PATCH 101/159] perf(utils): store stack words as two 16-byte stores on x86_64 without AVX (#997) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On x86_64 without AVX (the default `x86_64-unknown-linux-gnu` target), `write_word` and `write_u64` fall back to `dst.write(..)`, which LLVM lowers to an 8-byte store, a 16-byte store at offset 8 and another 8-byte store. The next handler (DUP, SWAP, MSTORE and so on) reads the word back with 16-byte loads at offsets 0 and 16, each of which spans two pending stores, so store-to-load forwarding fails. This is the stall the AVX arms already fix with one 32-byte store (#667). This adds the SSE2 counterpart: an arm after the AVX arm in both functions that stores the word as two 16-byte `_mm_storeu_si128` stores. It uses `core::arch::x86_64` intrinsics rather than inline assembly, since at 16 bytes LLVM keeps the stores whole. The LT and PUSH1 handlers now end with two `movdqu` stores at offsets 0 and 16 and no stack spill. The AVX arms are unchanged. Each binary is built from the same base commit with and without the change, runs `cargo bench -p evm2-cli --bench evm` on the default x86_64 target in its own process, in rotated order per round, and the table shows the median over rounds. Toolchains are stable 1.99.1 and nightly 1.101.0-nightly (2026-11-02). The geomean covers the cases that take at least 5 µs. `tco` is the `auto` backend on nightly and `packed` the `auto` backend on stable; `unpacked` is not `auto` on any target this touches. Geomean change by backend: | Backend | Zen 4 (EPYC 4244P), 3 rounds | Zen 5 (EPYC 4245P), 2 rounds | Zen 3 (EPYC 7763, GitHub runner), 3 rounds | | :-- | :-- | :-- | :-- | | `tco` | 🟢 −10.8% | 🟢 −14.2% | 🟢 −9.8% | | `packed` | 🟢 −1.4% | 🟢 −5.2% | −0.9% | | `unpacked` | 🟢 −11.5% | 🟢 −9.5% | 🔴 +5.1% | `tco` per case, before → after: | Case | Zen 4 | Zen 5 | Zen 3 | | :-- | :-- | :-- | :-- | | snailtracer | 32.30 → 28.89 ms 🟢 −10.6% | 30.02 → 26.22 ms 🟢 −12.7% | 55.70 → 50.73 ms 🟢 −8.9% | | burntpix | 24.88 → 25.17 ms +1.2% | 19.63 → 16.86 ms 🟢 −14.1% | 40.89 → 40.00 ms −2.2% | | erc20_transfer | 1.58 → 1.46 ms 🟢 −7.6% | 1.31 → 1.19 ms 🟢 −9.6% | 2.63 → 2.45 ms 🟢 −6.8% | | curve_stableswap | 37.5 → 36.4 µs −2.8% | 34.0 → 29.9 µs 🟢 −11.9% | 66.5 → 63.5 µs −4.5% | | fibonacci-calldata | 31.4 → 17.5 µs 🟢 −44.3% | 26.1 → 12.9 µs 🟢 −50.7% | 49.1 → 27.9 µs 🟢 −43.1% | | mainnet replay (10 blocks) | 86.31 → 85.63 ms −0.8% | 85.17 → 84.58 ms −0.7% | 161.14 → 158.46 ms −1.7% | On Zen 4, `ls_bad_status2.stli_other` per 1000 instructions on snailtracer falls from 49.7 to 31.4 for `tco` and from 23.4 to 17.0 for `packed`, and instructions per cycle for `tco` rise from 2.81 to 3.15. On Zen 3 the `unpacked` backend is 5% slower, consistent across rounds (snailtracer 77 → 85 ms). A control run with 64-byte function alignment is in progress to tell a code-layout effect from a real one. --- crates/evm2/src/utils.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/evm2/src/utils.rs b/crates/evm2/src/utils.rs index 6af0afab9..024c3770b 100644 --- a/crates/evm2/src/utils.rs +++ b/crates/evm2/src/utils.rs @@ -27,7 +27,7 @@ pub const fn b256_to_word(value: B256) -> Word { /// /// This trait is sealed and only implemented for [`Word`]. pub trait WordExt: sealed::Sealed { - /// Writes `value` with a single 32-byte store. + /// Writes `value` with one 32-byte store, or two 16-byte stores without AVX. /// /// With `*self = value`, LLVM writes a word held in general-purpose registers as four 8-byte /// stores. The next instruction often reads the word back with one 32-byte load, for example @@ -39,17 +39,20 @@ pub trait WordExt: sealed::Sealed { /// copied from memory already move with one 32-byte load and store, so plain assignment is /// better for them; this method would route them through general-purpose registers. /// - /// Without AVX this is the same as `*self = value`. + /// Without AVX on x86_64 this is two 16-byte stores, which a pair of 16-byte loads can still + /// forward from. On other targets it is the same as `*self = value`. fn write(&mut self, value: Word); - /// Writes `value`, zero-extended to a word, with a single 32-byte store. + /// Writes `value`, zero-extended to a word, with one 32-byte store, or two 16-byte stores + /// without AVX. /// /// With `*self = Word::from(value)`, LLVM stores the value and the three zero limbs separately, /// so a later 32-byte load of the word waits for those stores to reach the cache, as described /// in [`write`](Self::write). Use this for results that fit in a `u64`, such as comparison /// flags, sizes and offsets. /// - /// Without AVX this is the same as `*self = Word::from(value)`. + /// Without AVX on x86_64 this is two 16-byte stores. On other targets it is the same as + /// `*self = Word::from(value)`. fn write_u64(&mut self, value: u64); /// Converts the word to an address, keeping its low 20 bytes. @@ -132,6 +135,14 @@ pub(crate) unsafe fn write_word(dst: *mut Word, value: Word) { options(nostack, preserves_flags), ); }, + // LLVM splits the word store into 8- and 16-byte stores, which the 16-byte loads that read + // it back cannot forward from, so store two 16-byte halves. + all(target_arch = "x86_64", not(miri)) => unsafe { + use core::arch::x86_64::{__m128i, _mm_set_epi64x, _mm_storeu_si128}; + let [l0, l1, l2, l3] = value.into_limbs().map(|l| l as i64); + _mm_storeu_si128(dst.cast(), _mm_set_epi64x(l1, l0)); + _mm_storeu_si128(dst.cast::<__m128i>().add(1), _mm_set_epi64x(l3, l2)); + }, _ => unsafe { dst.write(value) }, } } @@ -158,6 +169,15 @@ pub(crate) unsafe fn write_u64(dst: *mut Word, value: u64) { options(nostack, preserves_flags), ); }, + // LLVM splits the word store into 8- and 16-byte stores, which the 16-byte loads that read + // it back cannot forward from, so store two 16-byte halves. + all(target_arch = "x86_64", not(miri)) => unsafe { + use core::arch::x86_64::{ + __m128i, _mm_cvtsi64_si128, _mm_setzero_si128, _mm_storeu_si128, + }; + _mm_storeu_si128(dst.cast(), _mm_cvtsi64_si128(value as i64)); + _mm_storeu_si128(dst.cast::<__m128i>().add(1), _mm_setzero_si128()); + }, _ => unsafe { dst.write(Word::from(value)) }, } } From bba8a58519cdbd79b333219365180d66b5cf30e7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:02:48 +0100 Subject: [PATCH 102/159] perf(inspector): let inspectors choose which opcodes get step hooks (#873) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An inspector can now say which opcodes get `step` and `step_end`. `Inspector::inspector_config` returns an `InspectorConfig`, which today holds the step set (`with_steps`, `steps()`, an `OpcodeSet`). The default observes everything, so existing inspectors behave as before. Call, create, log and self-destruct hooks fire whatever the set is, and they always see the pc, opcode and stack of the instruction that runs the message. This supersedes #837 and covers every backend, tail-call included. A frame with an empty set runs on the plain table and loop, one that steps every opcode on the existing inspect table, and a sparse set on a table with inspect handlers at its opcodes and plain handlers elsewhere. The sparse table is built into the interpreter that runs the frame and reused while its set and config stay the same. The tail-call backend calls the step hooks from its handlers. The packed, unpacked and single-return backends call them from the dispatch loop, outside the stack borrow, because a hook may use the stack through the interpreter. Frames that step some opcodes or none run their loop behind a call, so it does not compile into `run_inner`, where it came out slower than the loop for no inspector. The config is read when a frame starts and again after each child message that a message instruction runs, so a child message (a Foundry cheatcode, say) can change what its caller's inspector sees. `call_with` and `create_with` check the config after the child returns and, if the set changed, return an internal stop, `InstrStop::Reload`. Only the run loop in `runtime.rs` acts on it: it continues after the instruction on the table for the new set, and `step_end` hooks see the instruction as done. A stop that a hook sets ends the frame first. `Instruction::DYNAMIC_GAS` becomes `Instruction::FLAGS`, an `InstructionFlags` set that the macro fills, with a new `HOOKED` flag set by `#[instruction(hooked)]`. A hooked instruction may run inspector hooks or host code that read the interpreter, so before it runs the dispatch stores the latest pc, stack length and gas into the interpreter, and `hooked` implies `dynamic_gas`. `CALL`, `CALLCODE`, `DELEGATECALL`, `STATICCALL`, `CREATE` and `CREATE2` are hooked, because the `call`, `call_end`, `create` and `create_end` hooks get the interpreter from inside them, and a custom instruction that runs a child message must be hooked too. `step` and `step_end` get the current state from the dispatch, `initialize_interp` runs at frame start, and `log` and `selfdestruct` get only the host, so `LOG` and `SELFDESTRUCT` are not hooked. The dispatch marks the frame while a hooked instruction runs, and the config reload panics if the mark is missing, so a custom message instruction that is not hooked fails at its first message instead of resuming from a stale pc. `call_with` and `create_with` take no pc. `MuxInspector`, `DebugInspector`, the `(L, R)` tuple and `Option` forward `inspector_config` and return the union of their members, and `None` steps nothing. The 4byte, transfer, internal operations and no-op inspectors only use message, log and self-destruct hooks, so they step nothing. `TracingInspector` steps nothing without `record_steps`, the filter's opcodes with `record_opcodes_filter`, and every opcode when a halting byte budget is set, since the budget halts from the step hooks. `OpcodeFilter` is replaced by `OpcodeSet`, and `record_opcodes_filter` is an `Option`. The keccak256 preimage tracer steps `KECCAK256`. The call tracer, the prestate tracer and the parity traces other than `vmTrace` run without step hooks, and a test checks that they trace the same with the steps they ask for as with every step. Without an inspector, 163 of the 177 packed and tail-call handlers for each spec compile to the same instructions as on main once struct offsets are normalised. The other 14 are the message handlers. Callgrind on one run of each case, instructions in `Interpreter::run_inner` and below it: | no inspector | tco main → branch | packed main → branch | |:--|:--|:--| | erc20_transfer | 4,812,378,168 → 4,813,544,707 | 6,947,802,911 → 6,780,981,912 🟢 | | snailtracer | 4,902,055,038 → 4,902,434,014 | 7,051,841,215 → 6,882,813,154 🟢 | With an inspector, the hooks-only inspector returns no steps on this branch and the default on main, where it steps every opcode. The sparse inspector steps `SLOAD`, `SSTORE` and the `CALL` and `CREATE` families, and the full inspector steps everything. | erc20_transfer | tco | packed | |:--|:--|:--| | main, hooks only or full steps | 11,327,424,788 | 15,256,793,861 | | branch, hooks only | 4,813,129,785 🟢 | 6,780,780,293 🟢 | | branch, sparse | 4,814,398,982 🟢 | 7,540,884,620 🟢 | | branch, full steps | 11,167,816,672 🟢 | 15,392,191,285 🔴 | | snailtracer | tco | packed | |:--|:--|:--| | main, hooks only or full steps | 11,463,658,413 | 15,420,521,739 | | branch, hooks only | 4,903,012,440 🟢 | 6,883,387,324 🟢 | | branch, sparse | 4,903,938,647 🟢 | 7,650,229,504 🟢 | | branch, full steps | 11,303,921,028 🟢 | 15,557,209,438 🔴 | --- crates/evm2/src/evm/inspector.rs | 761 +++++++++++++++++- crates/evm2/src/interpreter/dispatch/mod.rs | 17 +- .../src/interpreter/dispatch/table/mod.rs | 129 ++- crates/evm2/src/interpreter/dispatch/tco.rs | 17 +- crates/evm2/src/interpreter/host.rs | 3 +- .../src/interpreter/instructions/system.rs | 72 +- crates/evm2/src/interpreter/mod.rs | 5 + crates/evm2/src/interpreter/private.rs | 18 +- crates/evm2/src/interpreter/runtime.rs | 177 +++- crates/evm2/src/lib.rs | 2 +- crates/evm2/src/version/opcode_config.rs | 29 +- crates/inspectors/src/otterscan.rs | 6 +- crates/inspectors/src/tracing/config.rs | 53 +- crates/inspectors/src/tracing/debug.rs | 9 +- crates/inspectors/src/tracing/fourbyte.rs | 6 +- crates/inspectors/src/tracing/keccak.rs | 6 +- crates/inspectors/src/tracing/limits.rs | 6 + crates/inspectors/src/tracing/mod.rs | 18 +- crates/inspectors/src/tracing/mux.rs | 46 +- crates/inspectors/src/transfer.rs | 6 +- crates/inspectors/tests/it/compat.rs | 9 +- crates/inspectors/tests/it/geth.rs | 233 +++++- crates/macros/src/lib.rs | 30 +- 23 files changed, 1482 insertions(+), 176 deletions(-) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index f30582b69..a170cb452 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -9,6 +9,107 @@ use alloc::boxed::Box; use alloy_primitives::{Address, Log, U256}; use auto_impl::auto_impl; +/// Set of opcodes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct OpcodeSet(U256); + +impl OpcodeSet { + /// Set containing no opcode. + pub const EMPTY: Self = Self(U256::ZERO); + + /// Set containing every opcode value. + pub const ALL: Self = Self(U256::MAX); + + /// Returns whether the set contains `opcode`. + #[inline] + pub const fn contains(&self, opcode: u8) -> bool { + self.0.bit(opcode as usize) + } + + /// Adds `opcode` to the set. + #[inline] + pub const fn insert(&mut self, opcode: u8) { + self.0.set_bit(opcode as usize, true); + } + + /// Removes `opcode` from the set. + #[inline] + pub const fn remove(&mut self, opcode: u8) { + self.0.set_bit(opcode as usize, false); + } + + /// Returns the opcodes in `self` or `other`. + #[inline] + pub fn union(self, other: Self) -> Self { + Self(self.0 | other.0) + } +} + +impl FromIterator for OpcodeSet { + fn from_iter>(opcodes: I) -> Self { + let mut set = Self::EMPTY; + for opcode in opcodes { + set.insert(opcode); + } + set + } +} + +/// What an [`Inspector`] observes of execution. +/// +/// Returned by [`Inspector::inspector_config`]. The EVM reads it when a frame starts and after each +/// child message returns into the frame, and runs the frame so that it only pays for what the +/// config observes. The default observes everything. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct InspectorConfig { + steps: OpcodeSet, +} + +impl InspectorConfig { + /// Creates a config that observes everything. + #[inline] + pub const fn new() -> Self { + Self { steps: OpcodeSet::ALL } + } + + /// Creates a config that calls no step hooks. + #[inline] + pub const fn without_steps() -> Self { + Self { steps: OpcodeSet::EMPTY } + } + + /// Sets the opcodes for which [`Inspector::step`] and [`Inspector::step_end`] are called. + /// + /// The EVM may call the step hooks for more opcodes than set, for example when several + /// inspectors share a frame, so a hook must ignore the opcodes it does not handle. Call, + /// create, log and self-destruct hooks run regardless. + #[inline] + pub const fn with_steps(mut self, steps: OpcodeSet) -> Self { + self.steps = steps; + self + } + + /// Returns the opcodes for which [`Inspector::step`] and [`Inspector::step_end`] are called. + #[inline] + pub const fn steps(&self) -> OpcodeSet { + self.steps + } + + /// Returns a config that observes everything either `self` or `other` observes. + #[inline] + pub fn union(self, other: Self) -> Self { + self.with_steps(self.steps.union(other.steps)) + } +} + +impl Default for InspectorConfig { + /// Returns a config that observes everything. + #[inline] + fn default() -> Self { + Self::new() + } +} + /// EVM execution inspector. #[auto_impl(&mut, Box)] pub trait Inspector: NonStaticAny { @@ -18,6 +119,16 @@ pub trait Inspector: NonStaticAny { let _ = interp; } + /// Returns what this inspector observes, as an [`InspectorConfig`]. + /// + /// Read when a frame starts and when a child message of the frame returns, so a child message + /// may change the answer. A change that a hook makes itself, such as in [`Self::step`], takes + /// effect when the next child message of the frame returns. The default observes everything. + #[inline] + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::new() + } + /// Called before each instruction executes. #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { @@ -121,13 +232,23 @@ pub(crate) fn boxed_inspector<'a, T: EvmTypesHost>( #[derive(Clone, Debug, Default)] pub struct NoopInspector(()); -impl Inspector for NoopInspector {} +impl Inspector for NoopInspector { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } +} /// Runs both inspectors, `L` first. /// -/// Every hook is forwarded to both. When both `call` or `create` hooks return a result, the result -/// from `L` wins. +/// Every hook is forwarded to both, and the steps are those of either inspector. When both `call` +/// or `create` hooks return a result, the result from `L` wins. impl, R: Inspector> Inspector for (L, R) { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + self.0.inspector_config().union(self.1.inspector_config()) + } + #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { self.0.initialize_interp(interp); @@ -210,8 +331,13 @@ impl, R: Inspector> Inspector for (L, R) } /// Forwards every hook to the inner inspector when present; `None` behaves like -/// [`NoopInspector`]. +/// [`NoopInspector`] and steps nothing. impl> Inspector for Option { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + self.as_ref().map_or_else(InspectorConfig::without_steps, I::inspector_config) + } + #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { if let Some(inspector) = self { @@ -316,16 +442,16 @@ impl<'a, T: EvmTypesHost> core::ops::DerefMut for dyn Inspector + 'a { mod tests { use super::*; use crate::{ - BaseEvmTypes, DatabaseError, Evm, EvmTypesHost, ExecutionConfig, Precompiles, SpecId, - Version, + BaseEvmConfig, BaseEvmTypes, DatabaseError, Evm, EvmConfig, EvmTypesHost, ExecutionConfig, + OpcodeConfig, Precompiles, SpecId, Version, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, evm::{AccountInfo, EmptyDB, InMemoryDB, SYSTEM_ADDRESS, State, StateCheckpoint}, interpreter::{ - GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageResult, - MessageResultExt, Word, op, + GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageKind, + MessageResult, MessageResultExt, Word, instructions as instr, op, }, registry::TxRegistry, test_utils::{TestHost, TestTypes, legacy_bytecode, push, push_all}, @@ -335,6 +461,7 @@ mod tests { use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, Bytes, Log, TxKind, U256}; use core::assert_matches; + use evm2_macros::instruction; #[derive(Default)] struct SelfdestructInspector { @@ -657,6 +784,565 @@ mod tests { assert_eq!(inspector.step_ends, 1); } + /// Records the stack and gas around the steps its config asks for. + #[derive(Default)] + struct ConfigInspector { + before: OpcodeSet, + after: Option, + switched: bool, + /// Opcodes between their `step` and `step_end`, innermost frame last. + running: Vec, + steps: Vec<(u8, u64, Vec)>, + step_ends: Vec<(u8, u64, Vec)>, + calls: usize, + creates: usize, + /// The pc, opcode and stack that the call and create hooks of child messages see. + views: Vec<(usize, u8, Vec)>, + logs: Vec, + /// Stops the parent frame when a child message ends. + stop_after_child: bool, + /// Stops the frame in `step_end` of this opcode. + stop_in_step_end: Option, + /// Stops every frame before it runs. + stop_on_init: bool, + /// Answers the calls of child messages in the `call` hook, so no child frame runs. + answer_calls: Option>, + } + + impl ConfigInspector { + fn new(steps: OpcodeSet) -> Self { + Self { before: steps, ..Default::default() } + } + + /// Uses `after` instead of `before` once a child message starts. + fn switching_to(mut self, after: OpcodeSet) -> Self { + self.after = Some(after); + self + } + + /// Answers the calls of child messages with `result` in the `call` hook. + fn answering_calls(mut self, result: MessageResult) -> Self { + self.answer_calls = Some(result); + self + } + } + + impl Inspector for ConfigInspector { + fn inspector_config(&self) -> InspectorConfig { + let steps = match self.after { + Some(after) if self.switched => after, + _ => self.before, + }; + InspectorConfig::new().with_steps(steps) + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let opcode = interp.opcode(); + self.running.push(opcode); + self.steps.push((opcode, interp.gas().remaining(), interp.stack().to_vec())); + } + + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let opcode = self.running.pop().unwrap(); + self.step_ends.push((opcode, interp.gas().remaining(), interp.stack().to_vec())); + if self.stop_in_step_end == Some(opcode) { + interp.set_stop(InstrStop::Revert); + } + } + + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if self.stop_on_init { + interp.set_stop(InstrStop::Revert); + } + } + + fn log(&mut self, log: &Log, _host: &mut Evm<'_, BaseEvmTypes>) { + self.logs.push(log.clone()); + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.calls += 1; + if message.depth > 0 { + self.views.push((interp.pc(), interp.opcode(), interp.stack().to_vec())); + } + // Like a cheatcode that changes what its caller's inspector wants to see. + self.switched |= message.depth > 0; + if message.depth > 0 + && let Some(answer) = &self.answer_calls + { + let mut result = answer.clone(); + result.gas.set_remaining(message.gas_limit); + return Some(result); + } + None + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + _result: &mut MessageResult, + ) { + if self.stop_after_child && message.depth > 0 { + interp.set_stop(InstrStop::Revert); + } + } + + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.creates += 1; + if message.depth > 0 { + self.views.push((interp.pc(), interp.opcode(), interp.stack().to_vec())); + } + None + } + } + + fn opcode_set(opcodes: impl IntoIterator) -> OpcodeSet { + opcodes.into_iter().collect() + } + + /// Runs `code` through the first call of a frame without an inspector. + fn run_evm_uninspected( + db: InMemoryDB, + code: Vec, + gas_limit: u64, + ) -> (MessageResult, Vec) { + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + db, + Precompiles::base(SpecId::OSAKA), + ); + let mut message = + MessageExt { gas_limit, code: legacy_bytecode(code), ..Default::default() }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + (result, evm.logs().to_vec()) + } + + /// Code that stores, loads, logs, calls a child that loads a slot, and creates. + fn config_test_code() -> (InMemoryDB, Vec) { + let child = Address::from([0x66; 20]); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &child, + AccountInfo::default().with_code(legacy_bytecode(vec![ + op::PUSH0, + op::SLOAD, + op::POP, + op::STOP, + ])), + ); + let mut code = vec![op::PUSH1, 5, op::PUSH0, op::SSTORE, op::PUSH0, op::SLOAD, op::POP]; + code.extend([op::PUSH1, 0xaa, op::PUSH0, op::MSTORE8, op::PUSH1, 1, op::PUSH0, op::LOG0]); + code.extend(call_code(child)); + code.extend([op::CALL, op::POP]); + code.extend(create_code()); + code.extend([op::CREATE, op::POP, op::PUSH0, op::SLOAD, op::POP, op::STOP]); + (db, code) + } + + #[test] + fn sparse_steps_match_full_inspector_at_those_opcodes() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let (result, full, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(OpcodeSet::ALL), + ); + assert_eq!(result, expected); + assert_eq!(full.logs, expected_logs); + + for opcodes in [ + vec![op::SLOAD, op::SSTORE, op::CALL, op::CREATE], + vec![op::SLOAD], + vec![op::CALL, op::CREATE, op::CREATE2], + vec![op::POP, op::STOP, op::LOG0], + vec![op::INVALID], + ] { + let steps = opcode_set(opcodes); + let (result, sparse, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(steps), + ); + let filter = |records: &[(u8, u64, Vec)]| { + records + .iter() + .filter(|record| steps.contains(record.0)) + .cloned() + .collect::>() + }; + assert_eq!(result, expected, "{steps:?}"); + assert_eq!(sparse.logs, expected_logs); + assert_eq!(sparse.steps, filter(&full.steps)); + assert_eq!(sparse.step_ends, filter(&full.step_ends)); + assert_eq!((sparse.calls, sparse.creates), (full.calls, full.creates)); + } + } + + #[test] + fn message_hook_can_stop_the_parent_without_steps() { + let (db, code) = config_test_code(); + let run = |steps| { + let inspector = + ConfigInspector { stop_after_child: true, ..ConfigInspector::new(steps) }; + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ) + }; + let (full, _, _) = run(OpcodeSet::ALL); + assert_eq!(full.stop, InstrStop::Revert); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::CALL])] { + let (result, inspector, _) = run(steps); + assert_eq!(result, full); + // The frame stopped at the CALL, before the later CREATE. + assert_eq!(inspector.creates, 0); + } + // The stop wins over a reload for the changed steps. + let inspector = ConfigInspector { + stop_after_child: true, + ..ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL) + }; + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ); + assert_eq!(result, full); + assert_eq!(inspector.creates, 0); + } + + #[test] + fn step_end_stop_wins_over_a_reload() { + let (db, code) = config_test_code(); + let run = |inspector| { + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ) + }; + let (full, _, _) = run(ConfigInspector { + stop_in_step_end: Some(op::CALL), + ..ConfigInspector::new(OpcodeSet::ALL) + }); + assert_eq!(full.stop, InstrStop::Revert); + // The child message of the stepped CALL changes the steps while its `step_end` stops. + let (result, inspector, _) = run(ConfigInspector { + stop_in_step_end: Some(op::CALL), + ..ConfigInspector::new(opcode_set([op::CALL])).switching_to(opcode_set([op::SSTORE])) + }); + assert_eq!(result, full); + // The frame stopped at the CALL, before the later CREATE. + assert_eq!(inspector.creates, 0); + } + + #[test] + fn message_hooks_see_the_message_instruction_whatever_the_steps() { + let (db, code) = config_test_code(); + let views = |steps| { + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(steps), + ) + .1 + .views + }; + let full = views(OpcodeSet::ALL); + assert_eq!(full.iter().map(|view| view.1).collect::>(), [op::CALL, op::CREATE]); + assert_eq!(full[0].2.len(), 7); + assert_eq!(full[1].2.len(), 3); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::CALL])] { + assert_eq!(views(steps), full, "{steps:?}"); + } + } + + #[test] + fn stop_set_before_the_run_ends_the_frame_without_steps() { + let code = vec![op::PUSH1, 1, op::PUSH0, op::SSTORE, op::STOP]; + let run = |steps| { + let inspector = ConfigInspector { stop_on_init: true, ..ConfigInspector::new(steps) }; + run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector).0 + }; + let full = run(OpcodeSet::ALL); + assert_eq!((full.stop, full.gas.remaining()), (InstrStop::Revert, 100_000)); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::SSTORE])] { + assert_eq!(run(steps), full); + } + } + + /// The base config with `CALL` also installed at [`CUSTOM_CALL`]. + struct CustomCallConfig; + + const CUSTOM_CALL: u8 = 0xb0; + + impl EvmConfig for CustomCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(CUSTOM_CALL, 100); + config + }; + } + + /// The base config with `CALL` installed at `PUSH30`, an opcode with immediate bytes. + struct PushSlotCallConfig; + + impl EvmConfig for PushSlotCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(op::PUSH30, 100); + config + }; + } + + /// A call that is not `hooked`, so a config reload would resume from a stale pc. + #[instruction(no_stack_preamble, dynamic_gas, EvmTypes = BaseEvmTypes)] + fn unflagged_call(cx: _) -> Result { + instr::call_with( + stack, + cx.gas, + cx.state, + MessageKind::Call, + |_, _, _| Ok(()), + |_, _, _, _| Ok(()), + ) + } + + /// The base config with the call without the flag installed at [`CUSTOM_CALL`]. + struct UnflaggedCallConfig; + + impl EvmConfig for UnflaggedCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + + /// Runs a call to a child through `message_op`, which `C` installs as a call, followed by + /// `immediates`. + fn run_custom_call>( + inspector: ConfigInspector, + message_op: u8, + immediates: &[u8], + ) -> (MessageResult, ConfigInspector) { + let mut code = call_code(Address::from([0x66; 20])); + code.push(message_op); + code.extend(immediates); + code.extend([op::GAS, op::POP, op::STOP]); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_config::(), + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(inspector); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + (result, *evm.clear_inspector_as::().unwrap()) + } + + #[test] + fn custom_call_opcode_rereads_the_steps() { + let (expected, full) = run_custom_call::( + ConfigInspector::new(OpcodeSet::ALL), + CUSTOM_CALL, + &[], + ); + + // The child message of the custom opcode switches the steps to `GAS`, and no steps before. + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + CUSTOM_CALL, + &[], + ); + assert_eq!(result, expected); + let gas = full.steps.iter().position(|record| record.0 == op::GAS).unwrap(); + assert_eq!(inspector.steps, [full.steps[gas].clone()]); + } + + #[test] + #[should_panic(expected = "an instruction that runs a message must be `hooked`")] + fn message_instruction_that_is_not_hooked_is_rejected() { + run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY), + CUSTOM_CALL, + &[], + ); + } + + #[test] + fn custom_call_opcode_with_immediates_resumes_after_them() { + // The immediates are invalid opcodes that fail the frame if the reload resumes inside them. + let immediates = [op::INVALID; 30]; + let (expected, _) = run_custom_call::( + ConfigInspector::new(OpcodeSet::ALL), + op::PUSH30, + &immediates, + ); + assert_eq!(expected.stop, InstrStop::Stop); + + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + op::PUSH30, + &immediates, + ); + assert_eq!(result, expected); + assert_eq!(inspector.steps.len(), 1); + } + + #[test] + fn empty_steps_skip_step_hooks_but_keep_message_and_log_hooks() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let (result, inspector, _) = run_evm_with_inspector_db( + db, + code, + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(OpcodeSet::EMPTY), + ); + + assert_eq!(result, expected); + assert!(inspector.steps.is_empty()); + assert!(inspector.step_ends.is_empty()); + // The top-level message and the child call. + assert_eq!(inspector.calls, 2); + assert_eq!(inspector.creates, 1); + assert_eq!(inspector.logs, expected_logs); + assert_eq!(inspector.logs.len(), 1); + } + + #[test] + fn answered_call_can_change_the_parent_steps() { + let mut code = call_code(Address::from([0x66; 20])); + code.extend([op::CALL, op::GAS, op::POP, op::STOP]); + let answer = MessageResultExt { + stop: InstrStop::Return, + output: Bytes::from_static(&[0xaa]), + ..Default::default() + }; + let run = |inspector: ConfigInspector| { + run_evm_with_inspector( + code.clone(), + &MessageExt::default(), + 100_000, + inspector.answering_calls(answer.clone()), + ) + }; + let (expected, full, _) = run(ConfigInspector::new(OpcodeSet::ALL)); + // The hook answers the call, so every step belongs to the caller. + assert_eq!(full.calls, 2); + let call = full.steps.iter().position(|record| record.0 == op::CALL).unwrap(); + let end = full.steps.len(); + + for (before, after, range, kept) in [ + (OpcodeSet::EMPTY, OpcodeSet::ALL, call + 1..end, OpcodeSet::ALL), + ( + OpcodeSet::EMPTY, + opcode_set([op::GAS, op::STOP]), + call + 1..end, + opcode_set([op::GAS, op::STOP]), + ), + (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), + ( + opcode_set([op::CALL]), + opcode_set([op::POP]), + call..end, + opcode_set([op::CALL, op::POP]), + ), + ] { + let (result, inspector, _) = run(ConfigInspector::new(before).switching_to(after)); + assert_eq!(result, expected, "{before:?} {after:?}"); + let keep = |records: &[(u8, u64, Vec)]| { + let records = records[range.clone()].iter(); + records.filter(|record| kept.contains(record.0)).cloned().collect::>() + }; + assert_eq!(inspector.steps, keep(&full.steps), "{before:?} {after:?}"); + assert_eq!(inspector.step_ends, keep(&full.step_ends), "{before:?} {after:?}"); + } + } + + #[test] + fn child_message_can_change_the_parent_steps() { + let mut code = call_code(Address::from([0x66; 20])); + code.extend([op::CALL, op::GAS, op::POP, op::STOP]); + let run = |inspector| { + run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector) + }; + let (expected, _) = run_evm_uninspected(InMemoryDB::default(), code.clone(), 100_000); + let (result, full, _) = run(ConfigInspector::new(OpcodeSet::ALL)); + assert_eq!(result, expected); + let call = full.steps.iter().position(|record| record.0 == op::CALL).unwrap(); + + let all = 0..full.steps.len(); + for (before, after, range, kept) in [ + // Starts stepping after the CALL. + (OpcodeSet::EMPTY, OpcodeSet::ALL, call + 1..all.end, OpcodeSet::ALL), + // Stops stepping after the CALL. + (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), + ( + OpcodeSet::EMPTY, + opcode_set([op::GAS, op::STOP]), + call + 1..all.end, + opcode_set([op::GAS, op::STOP]), + ), + ( + opcode_set([op::CALL]), + opcode_set([op::POP]), + call..all.end, + opcode_set([op::CALL, op::POP]), + ), + ] { + let (result, inspector, _) = run(ConfigInspector::new(before).switching_to(after)); + assert_eq!(result, expected, "{before:?} {after:?}"); + let keep = |records: &[(u8, u64, Vec)]| { + let records = records[range.clone()].iter(); + records.filter(|record| kept.contains(record.0)).cloned().collect::>() + }; + assert_eq!(inspector.steps, keep(&full.steps)); + assert_eq!(inspector.step_ends, keep(&full.step_ends)); + } + // The first step after the CALL sees its success flag on the stack. + let (_, inspector, _) = + run(ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL)); + assert_eq!(inspector.steps[0], (op::GAS, full.steps[call + 1].1, vec![Word::ONE])); + } + #[test] fn inspector_gas_edits_survive_dispatch() { #[derive(Default)] @@ -2039,4 +2725,63 @@ mod tests { assert_matches!(result.stop, InstrStop::Stop); assert!(inspector.is_none()); } + + #[test] + fn tuple_and_option_inspectors_step_the_union_of_their_sets() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let config = |inspector: &dyn Inspector| inspector.inspector_config().steps(); + let stepped = |inspector: &ConfigInspector| { + let mut opcodes = inspector.steps.iter().map(|step| step.0).collect::>(); + opcodes.sort_unstable(); + opcodes.dedup(); + opcodes + }; + let sload = || ConfigInspector::new(opcode_set([op::SLOAD])); + let call = || ConfigInspector::new(opcode_set([op::CALL])); + + assert_eq!(config(&(sload(), call())), opcode_set([op::SLOAD, op::CALL])); + assert_eq!( + config(&(sload(), ConfigInspector::new(OpcodeSet::EMPTY))), + opcode_set([op::SLOAD]) + ); + assert_eq!(config(&(sload(), NoopInspector::default())), opcode_set([op::SLOAD])); + assert_eq!(config(&(sload(), HookInspector::default())), OpcodeSet::ALL); + assert_eq!(config(&Some(sload())), opcode_set([op::SLOAD])); + assert_eq!(config(&None::), OpcodeSet::EMPTY); + + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + (sload(), call()), + ); + assert_eq!(result, expected); + assert_eq!(inspector.0.logs, expected_logs); + // Each member also sees the steps of the other, and both see every message. + assert_eq!(stepped(&inspector.0), [op::SLOAD, op::CALL]); + assert_eq!(inspector.0.steps, inspector.1.steps); + assert_eq!((inspector.0.calls, inspector.1.calls), (2, 2)); + + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + Some(sload()), + ); + assert_eq!(result, expected); + assert_eq!(stepped(&inspector.unwrap()), [op::SLOAD]); + + let (result, inspector, _) = run_evm_with_inspector_db( + db, + code, + &MessageExt::default(), + 1_000_000, + None::, + ); + assert_eq!(result, expected); + assert!(inspector.is_none()); + } } diff --git a/crates/evm2/src/interpreter/dispatch/mod.rs b/crates/evm2/src/interpreter/dispatch/mod.rs index d011f3990..921e3db94 100644 --- a/crates/evm2/src/interpreter/dispatch/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/mod.rs @@ -42,7 +42,7 @@ const fn inc_pc(pc: &mut Pc, op: u8) { } #[inline(always)] -const fn instruction_len(op: u8) -> usize { +pub(crate) const fn instruction_len(op: u8) -> usize { match op { op::JUMP | op::JUMPI => 0, // Set inside. op::PUSH1..=op::PUSH32 => (op - op::PUSH1 + 2) as usize, @@ -217,8 +217,15 @@ const fn instruction_changed( } trait InspectMode { + /// Whether the dispatch calls the inspector's step hooks: from the handlers on the tail-call + /// backend, and from the loop on the others, where a hook must not run while a handler holds + /// the stack borrow. const INSPECT: bool; + /// Whether the inspector steps every opcode, so the dispatch loop need not look at the set. + #[cfg(not(tco))] + const ALL_STEPS: bool = false; + fn step(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize); fn step_end(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize); @@ -236,10 +243,14 @@ impl InspectMode for NoInspector { fn step_end(_state: &mut InterpreterState<'_, '_, T>, _pc: Pc, _stack_len: usize) {} } -struct DynInspector; +/// Mode of the inspect table's handlers. `ALL` is the loop mode of an inspector that steps every +/// opcode. +struct DynInspector; -impl InspectMode for DynInspector { +impl InspectMode for DynInspector { const INSPECT: bool = true; + #[cfg(not(tco))] + const ALL_STEPS: bool = ALL; #[inline(always)] fn step(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize) { diff --git a/crates/evm2/src/interpreter/dispatch/table/mod.rs b/crates/evm2/src/interpreter/dispatch/table/mod.rs index 033929309..31970ae02 100644 --- a/crates/evm2/src/interpreter/dispatch/table/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/table/mod.rs @@ -1,7 +1,10 @@ use super::{DynInspector, InspectMode, NoInspector, inc_pc, run_state}; use crate::{ - EvmConfig, EvmTypesHost, - interpreter::{InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, StackMut}, + EvmConfig, EvmTypesHost, OpcodeSet, + interpreter::{ + InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, StackMut, + private::InstructionFlags, + }, }; use core::hint::cold_path; @@ -109,12 +112,20 @@ fn dispatch_inner, M: InspectMode, G: Dispat ) -> (Pc, G) { let instruction = C::OPCODE_CONFIG.instruction(op); let instr = instruction.instr; - let dynamic_gas = instruction.dynamic_gas; + let dynamic_gas = instruction.flags.contains(InstructionFlags::DYNAMIC_GAS); + let hooked = instruction.flags.contains(InstructionFlags::HOOKED); let r; match gas.pre_step::(state, op) { Ok(()) => { gas.sync_before_exec(state, dynamic_gas); + if hooked { + state.set_pc_stack_len(pc.as_ptr(), *stack.len); + state.set_hooked_running(true); + } r = instr(&mut pc, stack.reborrow(), state); + if hooked { + state.set_hooked_running(false); + } if r.is_ok() { inc_pc(&mut pc, op); } @@ -141,8 +152,12 @@ pub(in crate::interpreter) fn run( instructions: &RawInstrTable, ) -> InstrStop { let (state, pc, stack) = run_state(interpreter); - if state.is_inspecting() { - return run_inner::(state, pc, stack, instructions); + // An inspector that steps nothing runs the plain loop. + if state.is_inspecting() && state.steps() != OpcodeSet::EMPTY { + if state.steps() == OpcodeSet::ALL { + return run_inner::>(state, pc, stack, instructions); + } + return run_inner::>(state, pc, stack, instructions); } run_inner::(state, pc, stack, instructions) } @@ -156,37 +171,93 @@ fn run_inner>( ) -> InstrStop { let mut loop_state = imp::loop_state(state.gas_mut()); let mut cursor = imp::Cursor::new(pc, stack.len()); + let steps = if M::INSPECT { state.steps() } else { OpcodeSet::EMPTY }; loop { - if M::INSPECT { - imp::sync_loop_state(state, loop_state); - M::step(state, cursor.pc(), cursor.stack_len()); - loop_state = imp::loop_state(state.gas_mut()); - if state.result().is_err() { - return finish_run(state, cursor, loop_state); - } - } - let op = cursor.pc().op(); - let instr = instructions[op as usize]; - cursor = { - // SAFETY: This is the only stack borrow outside inspector callbacks. - let stack_view = unsafe { stack.borrow() }; - imp::dispatch_loop_call(instr, cursor, stack_view.stack, state, &mut loop_state) - }; - - if M::INSPECT { - imp::sync_loop_state(state, loop_state); - M::step_end(state, cursor.pc(), cursor.stack_len()); - loop_state = imp::loop_state(state.gas_mut()); - if state.result().is_err() { + // A sparse set's table holds inspect handlers at the stepped opcodes and plain handlers + // elsewhere, so each path calls the handlers it matches. + if M::INSPECT && (M::ALL_STEPS || steps.contains(op)) { + let stepped = if M::ALL_STEPS { + run_stepped::(state, cursor, stack, instructions[op as usize], loop_state) + } else { + run_stepped_out_of_line::( + state, + cursor, + stack, + instructions[op as usize], + loop_state, + ) + }; + match stepped { + Ok(next) => (cursor, loop_state) = next, + Err(stop) => return stop, + } + } else { + cursor = { + // SAFETY: This is the only stack borrow outside inspector callbacks. + let stack_view = unsafe { stack.borrow() }; + imp::dispatch_loop_call( + instructions[op as usize], + cursor, + stack_view.stack, + state, + &mut loop_state, + ) + }; + if cursor.pc().as_ptr().is_null() { return finish_run(state, cursor, loop_state); } - } else if cursor.pc().as_ptr().is_null() { - return finish_run(state, cursor, loop_state); } } } +/// Runs one instruction between its step hooks, or returns the stop that ended the frame. +/// +/// The hooks run here, outside the stack borrow, because a hook may access the stack through the +/// interpreter and no handler may hold the borrow meanwhile. +#[inline(always)] +fn run_stepped>( + state: &mut InterpreterState<'_, '_, T>, + mut cursor: imp::Cursor, + mut stack: RawStack<'_>, + instr: imp::RawInstrFn, + mut loop_state: imp::LoopState, +) -> core::result::Result<(imp::Cursor, imp::LoopState), InstrStop> { + imp::sync_loop_state(state, loop_state); + M::step(state, cursor.pc(), cursor.stack_len()); + loop_state = imp::loop_state(state.gas_mut()); + if state.result().is_err() { + return Err(finish_run(state, cursor, loop_state)); + } + + cursor = { + // SAFETY: This is the only stack borrow outside inspector callbacks. + let stack_view = unsafe { stack.borrow() }; + imp::dispatch_loop_call(instr, cursor, stack_view.stack, state, &mut loop_state) + }; + + imp::sync_loop_state(state, loop_state); + M::step_end(state, cursor.pc(), cursor.stack_len()); + loop_state = imp::loop_state(state.gas_mut()); + if state.result().is_err() { + return Err(finish_run(state, cursor, loop_state)); + } + Ok((cursor, loop_state)) +} + +/// [`run_stepped`] behind a call, so that the registers the loop holds for the unstepped +/// instructions of a sparse set are not shared with the hook calls. +#[inline(never)] +fn run_stepped_out_of_line>( + state: &mut InterpreterState<'_, '_, T>, + cursor: imp::Cursor, + stack: RawStack<'_>, + instr: imp::RawInstrFn, + loop_state: imp::LoopState, +) -> core::result::Result<(imp::Cursor, imp::LoopState), InstrStop> { + run_stepped::(state, cursor, stack, instr, loop_state) +} + #[inline(always)] fn finish_run( state: &mut InterpreterState<'_, '_, T>, @@ -196,7 +267,7 @@ fn finish_run( cold_path(); let (pc, stack_len) = (cursor.pc(), cursor.stack_len()); // The NULL `pc` is only a loop-exit sentinel; never publish it. - let pc = if pc.as_ptr().is_null() { state.bytecode().as_ptr() } else { pc.as_ptr() }; + let pc = if pc.as_ptr().is_null() { state.0.pc } else { pc.as_ptr() }; state.set_pc_stack_len(pc, stack_len); imp::finish_loop(state.gas_mut(), loop_state); state.result().unwrap_err() diff --git a/crates/evm2/src/interpreter/dispatch/tco.rs b/crates/evm2/src/interpreter/dispatch/tco.rs index 47251a224..984934d83 100644 --- a/crates/evm2/src/interpreter/dispatch/tco.rs +++ b/crates/evm2/src/interpreter/dispatch/tco.rs @@ -2,8 +2,9 @@ use super::{InspectMode, run_state}; use crate::{ EvmConfig, EvmTypesHost, interpreter::{ - InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, gas::RemainingGas, - private::InstructionImplFn, + InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, + gas::RemainingGas, + private::{InstructionFlags, InstructionImplFn}, }, }; use core::hint::cold_path; @@ -54,8 +55,11 @@ extern_table! { ) { let instruction = C::OPCODE_CONFIG.instruction(OP); let instr: InstructionImplFn = instruction.instr; - let dynamic_gas = instruction.dynamic_gas; + let dynamic_gas = instruction.flags.contains(InstructionFlags::DYNAMIC_GAS); + let hooked = instruction.flags.contains(InstructionFlags::HOOKED); if M::INSPECT { + // Handlers that skip the hooks keep the gas in a register. + state.gas_mut().set_remaining(remaining_gas.get()); M::step(state, pc, stack.len()); remaining_gas.set(state.gas_mut().remaining()); if state.result().is_err() { @@ -76,6 +80,10 @@ extern_table! { if M::INSPECT || dynamic_gas { state.gas_mut().set_remaining(remaining_gas.get()); } + if hooked { + state.set_pc_stack_len(pc.as_ptr(), stack.len()); + state.set_hooked_running(true); + } let (r, stack_len) = { // SAFETY: This is the only stack borrow outside inspector callbacks. let mut stack_view = unsafe { stack.borrow() }; @@ -83,6 +91,9 @@ extern_table! { (r, stack_view.len) }; stack.set_len(stack_len); + if hooked { + state.set_hooked_running(false); + } if dynamic_gas { remaining_gas.set(state.gas_mut().remaining()); } diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index c5b131bd8..648407ee2 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -116,7 +116,8 @@ pub trait Host { /// Returns the active execution configuration. /// /// The interpreter keeps a reference to the returned version for the whole run, so the host - /// must not replace or mutate it while the interpreter runs. + /// must not replace or mutate it while the interpreter runs. An inspected run reads the config + /// again when its inspector changes the opcodes it steps. fn execution_config(&self) -> &ExecutionConfig; /// Returns the block environment. diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index eb506c3a4..c57af126e 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -221,6 +221,31 @@ fn prepare_call( Ok(new_account_state_gas) } +#[instruction(no_stack_preamble, hooked)] +pub fn call(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::Call) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn callcode(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::CallCode) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn delegatecall(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::DelegateCall) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn staticcall(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::StaticCall) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn create(cx: _) -> Result { + create_inner(stack, cx.gas, cx.state, IS_CREATE2) +} + #[inline(never)] fn call_inner( stack: StackMut<'_>, @@ -235,8 +260,9 @@ fn call_inner( /// /// This is the body of [`call`], [`callcode`], [`delegatecall`] and [`staticcall`], for custom /// call instructions that need extra behavior. Use it from an -/// `#[instruction(no_stack_preamble, dynamic_gas)]` function with `stack`, `cx.gas` and -/// `cx.state`. +/// `#[instruction(no_stack_preamble, hooked)]` function with `stack`, `cx.gas` and +/// `cx.state`. The instruction must be `hooked`, so that the hooks and the host see its pc, opcode, +/// stack and gas. /// /// `before` runs after every charge the opcode makes, the EIP-8037 new-account state gas /// included, and after the forwarded gas is taken from `gas`, right before the child message @@ -250,6 +276,8 @@ fn call_inner( /// /// An `Err` from either hook stops the instruction with that error. Gas the hooks charge is not /// part of the step cost reported to inspectors. +/// +/// The result can be the stop for a config reload. Return it as the last step of the instruction. #[inline] pub fn call_with( mut stack: StackMut<'_>, @@ -275,6 +303,7 @@ pub fn call_with( if result.stop.is_fatal() { return Err(result.stop); } + state.note_inspector_config_change(); gas.merge_child_gas(result.gas, result.stop); // EIP-8037: a value-bearing CALL that creates the target charges NEW_ACCOUNT state // gas upfront on this frame. If the call does not succeed (depth/balance failure, @@ -290,32 +319,9 @@ pub fn call_with( state.memory().set_unchecked(return_memory_range.start, output); } state.swap_return_data(&mut result.output); - stack.push_u64(result.stop.is_success() as u64) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn call(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::Call) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn callcode(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::CallCode) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn delegatecall(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::DelegateCall) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn staticcall(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::StaticCall) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn create(cx: _) -> Result { - create_inner(stack, cx.gas, cx.state, IS_CREATE2) + stack.push_u64(result.stop.is_success() as u64)?; + // A hook's stop or a config reload. + state.result() } #[inline(never)] @@ -343,6 +349,7 @@ fn create_inner( /// A fatal child result returns before `after` runs. /// /// Both hooks may charge or credit `gas` or fail the instruction, as in [`call_with`]. +/// Its result can be the stop for a config reload, as in [`call_with`]. #[inline] pub fn create_with( mut stack: StackMut<'_>, @@ -465,6 +472,7 @@ pub fn create_with( if result.stop.is_fatal() { return Err(result.stop); } + state.note_inspector_config_change(); gas.merge_child_gas(result.gas, result.stop); // EIP-8037: when the opcode charged the conditional `create_state_gas` @@ -483,7 +491,9 @@ pub fn create_with( } else { state.clear_return_data(); } - stack.push(result.created_address_for_parent()) + stack.push(result.created_address_for_parent())?; + // A hook's stop or a config reload. + state.result() } #[instruction(dynamic_gas)] @@ -1088,7 +1098,7 @@ mod tests { // Keeps `KEPT_GAS` of the forwarded gas in the caller and charges `AFTER_FEE` after a // successful child; a failed child fails the instruction. - #[instruction(no_stack_preamble, dynamic_gas)] + #[instruction(no_stack_preamble, hooked)] fn hooked_call(cx: _) -> Result { call_with( stack, @@ -1106,7 +1116,7 @@ mod tests { ) } - #[instruction(no_stack_preamble, dynamic_gas)] + #[instruction(no_stack_preamble, hooked)] fn hooked_create(cx: _) -> Result { create_with( stack, diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index 1312bcf73..5882aafba 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -113,6 +113,11 @@ pub enum InstrStop { FatalExternalError, /// Invalid encoding of an instruction's immediate operand. InvalidImmediateEncoding, + /// The inspector's config changed while a child message ran. The frame leaves the dispatch, + /// continues after the instruction on the table for the new config, and never reports this + /// stop. + #[doc(hidden)] + Reload, } impl InstrStop { diff --git a/crates/evm2/src/interpreter/private.rs b/crates/evm2/src/interpreter/private.rs index 5bbbea2b6..d660decd2 100644 --- a/crates/evm2/src/interpreter/private.rs +++ b/crates/evm2/src/interpreter/private.rs @@ -1,14 +1,28 @@ use super::{Gas, InterpreterState, Pc, Result, StackMut, Word}; use crate::EvmTypesHost; +use bitflags::bitflags; /// Function signature of an `#[instruction]`. pub(crate) type InstructionImplFn = fn(pc: &mut Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) -> Result; +bitflags! { + /// Properties of an [`Instruction`] that the dispatch acts on. + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] + pub struct InstructionFlags: u64 { + /// The instruction needs mutable gas state. + const DYNAMIC_GAS = 1 << 0; + /// The instruction may run inspector hooks or host code that read the interpreter, so the + /// dispatch stores the latest pc, stack length and gas into it first. Implies + /// [`Self::DYNAMIC_GAS`]. + const HOOKED = 1 << 1; + } +} + /// EVM instruction implementation. pub trait Instruction { - /// Whether this instruction needs mutable gas state. - const DYNAMIC_GAS: bool = true; + /// The properties of this instruction. + const FLAGS: InstructionFlags = InstructionFlags::DYNAMIC_GAS; /// Executes this instruction. fn execute(pc: &mut Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index c62c57188..f440032de 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -5,8 +5,11 @@ use super::{ use crate::{ EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, Version, bytecode::Bytecode, - evm::inspector::Inspector, - interpreter::{dispatch, opcode::OpCode}, + evm::inspector::{Inspector, OpcodeSet}, + interpreter::{ + dispatch::{self, InstrTable}, + opcode::OpCode, + }, trustme, version::{EvmFeatures, GasParams}, }; @@ -15,6 +18,15 @@ use alloy_primitives::{Address, B256, Bytes}; use core::{fmt, hint::cold_path, ops::Range, ptr::NonNull}; use derive_where::derive_where; +/// Instruction table for a sparse set of stepped opcodes. +struct SparseTable { + /// The stepped opcodes. + steps: OpcodeSet, + /// The config's inspect table the entries at `steps` came from. + source: *const InstrTable, + instructions: InstrTable, +} + /// EVM interpreter. #[derive_where(Debug)] pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { @@ -34,6 +46,14 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { message: Option<&'frame Message>, host: Option>>, inspector: Option + 'host>>, + /// Opcodes the inspector steps on the table this frame runs on. + steps: OpcodeSet, + /// Whether the running instruction is `hooked`. + hooked_running: bool, + /// The last sparse inspected run's step set, the config table it was built from, and the + /// table. + #[derive_where(skip)] + inspect_table: Option>>, pub(in crate::interpreter) stack_len: usize, #[derive_where(skip)] pub(in crate::interpreter) stack: Box, @@ -94,6 +114,9 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { return_data: Bytes::new(), host: None, inspector: None, + steps: OpcodeSet::EMPTY, + hooked_running: false, + inspect_table: None, spec: SpecId::DEFAULT, features: EvmFeatures::empty(), // SAFETY: `MaybeUninit` does not need initialization. @@ -532,15 +555,97 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { } else { // Load the table only after setup: passing it in keeps it in a callee-saved register // across setup, which makes the dispatch loop spill the stack pointer. - // SAFETY: `prepare_run` initialized the host, and the config is only read here. - let config = unsafe { self.execution_config_detached() }; - let instructions = - if inspector.is_some() { config.inspect_instructions } else { config.instructions }; - dispatch::run(self, instructions) + match inspector { + Some(_) => self.run_inspected(), + None => { + // SAFETY: `prepare_run` initialized the host, and the config is only read + // here. + let config = unsafe { self.execution_config_detached() }; + dispatch::run(self, config.instructions) + } + } }; self.finish_run(stop) } + /// Runs the frame on the table for its inspector's config, and again on the table for the new + /// config each time a child message changes it. + /// + /// Only `self` is live across the dispatch call, which recurses once per nested frame. + #[inline(always)] + fn run_inspected(&mut self) -> InstrStop { + loop { + // A hook before the run, such as `initialize_interp`, may have stopped the frame. + if let Err(stop) = self.result { + cold_path(); + return stop; + } + let instructions = self.select_inspect_table(); + // SAFETY: The table is a static one, or the one `self` holds until the next selection. + let instructions = unsafe { &*instructions }; + let stop = if self.steps == OpcodeSet::ALL { + dispatch::run(self, instructions) + } else { + self.run_inspected_table(instructions) + }; + if stop != InstrStop::Reload { + return stop; + } + cold_path(); + self.result = Ok(()); + // SAFETY: The stop left the pc at the message instruction, which a next one follows. + self.pc = unsafe { self.pc.add(dispatch::instruction_len(*self.pc)) }; + } + } + + /// Runs a frame that steps some opcodes, or none. Behind a call, so that its loops are not + /// copies inlined into `run_inner`, where they compile worse than the loop for no inspector. + #[inline(never)] + fn run_inspected_table(&mut self, instructions: &InstrTable) -> InstrStop { + dispatch::run(self, instructions) + } + + /// Records the opcodes the inspector steps now and returns the table for them. Not inlined, so + /// that its temporaries stay out of the frame of `run_inspected`. + #[inline(never)] + fn select_inspect_table(&mut self) -> *const InstrTable { + // SAFETY: `prepare_run` initialized the host. The config is read again on each reload and + // only its static tables are used, so nothing outlives a replaced config. + let config = unsafe { self.execution_config_detached() }; + // SAFETY: `run_inner` installed the inspector, which is valid for this run. + let inspector = unsafe { self.inspector.unwrap_unchecked().as_ref() }; + self.steps = inspector.inspector_config().steps(); + if self.steps == OpcodeSet::EMPTY { + return config.instructions; + } + if self.steps == OpcodeSet::ALL { + return config.inspect_instructions; + } + // A sparse set runs the plain handlers, with the inspect table's at the opcodes in the + // set. The table lives in this interpreter and is rebuilt when the set or the config + // differs from the previous run's, so the pooled interpreters of a deep call stack mostly + // reuse theirs. + let source = core::ptr::from_ref(config.inspect_instructions); + let table = self.inspect_table.get_or_insert_with(|| { + Box::new(SparseTable { + steps: OpcodeSet::EMPTY, + source: core::ptr::null(), + instructions: *config.instructions, + }) + }); + if table.steps != self.steps || table.source != source { + table.steps = self.steps; + table.source = source; + table.instructions = *config.instructions; + for op in 0..=u8::MAX { + if self.steps.contains(op) { + table.instructions[op as usize] = config.inspect_instructions[op as usize]; + } + } + } + &raw const table.instructions + } + /// Takes an owned error recorded by an instruction or inspector hook. pub(crate) const fn take_error(&mut self) -> Option { self.error.take() @@ -602,6 +707,42 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { self.0.inspector.is_some() } + /// Returns the opcodes the inspector steps in this frame. + #[inline] + #[cfg(not(tco))] + pub(crate) const fn steps(&self) -> OpcodeSet { + self.0.steps + } + + /// Stops the instruction with [`InstrStop::Reload`] if the inspector's config no longer + /// matches the table this frame runs on. A message instruction calls it after its child + /// message and returns the result. + #[inline(always)] + pub(crate) fn note_inspector_config_change(&mut self) { + if self.0.inspector.is_some() { + self.reload_if_config_changed(); + } + } + + #[inline(never)] + fn reload_if_config_changed(&mut self) { + assert!(self.0.hooked_running, "an instruction that runs a message must be `hooked`"); + if self.0.result.is_ok() + && let Some(inspector) = self.0.inspector + // SAFETY: The inspector pointer is valid while the frame executes. + && unsafe { inspector.as_ref() }.inspector_config().steps() != self.0.steps + { + self.0.result = Err(InstrStop::Reload); + } + } + + /// Records whether the running instruction is `hooked`, so that the dispatch stored the latest + /// state into the interpreter before it ran. + #[inline] + pub(crate) const fn set_hooked_running(&mut self, running: bool) { + self.0.hooked_running = running; + } + #[inline] pub(crate) const fn set_pc_stack_len(&mut self, pc: *const u8, stack_len: usize) { self.0.pc = pc; @@ -739,15 +880,31 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { pub(crate) fn inspect_step_end(&mut self, pc: Pc, stack_len: usize) { self.0.pc = pc.as_ptr(); self.0.stack_len = stack_len; - if self.0.result.is_err_and(InstrStop::is_out_of_gas) { + let reload = self.0.result == Err(InstrStop::Reload); + if reload { + cold_path(); + // The hook sees the instruction as done, with the pc after it. + self.0.result = Ok(()); + // SAFETY: A next instruction follows the instruction at `pc`. + self.0.pc = unsafe { pc.as_ptr().add(dispatch::instruction_len(pc.op())) }; + } else if self.0.result.is_err() { cold_path(); - // Failed charges may leave a wrapped counter until frame settlement. - self.0.gas.set_remaining(0); + if self.0.result.is_err_and(InstrStop::is_out_of_gas) { + // Failed charges may leave a wrapped counter until frame settlement. + self.0.gas.set_remaining(0); + } } unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step_end(&mut self.0); } + // A stop the hook set wins over the reload. + if reload { + self.0.pc = pc.as_ptr(); + if self.0.result.is_ok() { + self.0.result = Err(InstrStop::Reload); + } + } } /// Marks that the current step finished charging gas, so [`Interpreter::step_cost`] stops diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index 6b46af6ec..c4502de64 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -31,7 +31,7 @@ pub use evm::{ }, env, handler, inspector, precompile, registry, }; -pub use inspector::{Inspector, NoopInspector}; +pub use inspector::{Inspector, InspectorConfig, NoopInspector, OpcodeSet}; pub mod precompiles; pub use precompiles::{ diff --git a/crates/evm2/src/version/opcode_config.rs b/crates/evm2/src/version/opcode_config.rs index 4b10b7697..26eebaff7 100644 --- a/crates/evm2/src/version/opcode_config.rs +++ b/crates/evm2/src/version/opcode_config.rs @@ -2,7 +2,7 @@ use crate::{ BaseEvmConfig, EvmConfig, EvmTypesHost, SpecId, interpreter::{ instructions as instr, op, - private::{Instruction, InstructionImplFn}, + private::{Instruction, InstructionFlags, InstructionImplFn}, }, }; use core::fmt; @@ -26,8 +26,7 @@ pub struct OpcodeConfig { pub(crate) struct InstructionInfo { pub(crate) instr: InstructionImplFn, - #[allow(dead_code)] - pub(crate) dynamic_gas: bool, + pub(crate) flags: InstructionFlags, } impl fmt::Debug for OpcodeConfig { @@ -162,7 +161,7 @@ impl OpcodeConfig { assert!(opcode != op::INVALID, "INVALID opcode cannot be overridden"); self.set_static_gas(opcode, gas); self.instruction_sets[opcode as usize] += 1; - if self.instruction_impls.set(opcode, I::execute, I::DYNAMIC_GAS) { + if self.instruction_impls.set(opcode, I::execute, I::FLAGS) { self.bump_revision(opcode); } } @@ -242,24 +241,25 @@ impl StaticGasTable { struct InstructionImplTable { instrs: [Option>; 256], - dynamic_gas: [bool; 256], + flags: [InstructionFlags; 256], } impl InstructionImplTable { /// Creates an empty instruction implementation table. #[inline] const fn empty() -> Self { - Self { instrs: [None; 256], dynamic_gas: [false; 256] } + Self { instrs: [None; 256], flags: [InstructionFlags::empty(); 256] } } /// Returns the instruction implementation for `opcode`. #[inline(always)] const fn get(&self, opcode: u8) -> InstructionInfo { match self.instrs[opcode as usize] { - Some(instr) => { - InstructionInfo { instr, dynamic_gas: self.dynamic_gas[opcode as usize] } - } - None => InstructionInfo { instr: instr::invalid::::execute, dynamic_gas: false }, + Some(instr) => InstructionInfo { instr, flags: self.flags[opcode as usize] }, + None => InstructionInfo { + instr: instr::invalid::::execute, + flags: InstructionFlags::empty(), + }, } } @@ -271,10 +271,15 @@ impl InstructionImplTable { /// Sets the instruction implementation for `opcode`. #[inline(always)] - const fn set(&mut self, opcode: u8, instr: InstructionImplFn, dynamic_gas: bool) -> bool { + const fn set( + &mut self, + opcode: u8, + instr: InstructionImplFn, + flags: InstructionFlags, + ) -> bool { let index = opcode as usize; self.instrs[index] = Some(instr); - self.dynamic_gas[index] = dynamic_gas; + self.flags[index] = flags; true } } diff --git a/crates/inspectors/src/otterscan.rs b/crates/inspectors/src/otterscan.rs index 5ef56340a..cab8fd632 100644 --- a/crates/inspectors/src/otterscan.rs +++ b/crates/inspectors/src/otterscan.rs @@ -4,7 +4,7 @@ use alloc::vec::Vec; use alloy_primitives::{Address, U256}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; use evm2::{ - EvmTypesHost, Inspector, + EvmTypesHost, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; @@ -52,6 +52,10 @@ impl InternalOperationsInspector { } impl Inspector for InternalOperationsInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + fn call( &mut self, interp: &mut Interpreter<'_, '_, T>, diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 58d5d6042..32feb86b7 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -1,54 +1,11 @@ use crate::tracing::builder::geth::MIN_STRUCT_LOG_JSON_LEN; -use alloy_primitives::{U256, map::HashSet}; +use alloy_primitives::map::HashSet; use alloy_rpc_types_trace::{ geth::{CallConfig, FlatCallConfig, GethDefaultTracingOptions, PreStateConfig}, parity::TraceType, }; use core::num::NonZeroU64; -use evm2::interpreter::opcode::OpCode; - -/// 256 bits each marking whether an opcode should be included into steps trace or not. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[must_use] -pub struct OpcodeFilter(U256); - -impl Default for OpcodeFilter { - fn default() -> Self { - Self::new() - } -} - -impl OpcodeFilter { - /// Returns a new [OpcodeFilter] that does not trace any opcodes. - #[inline] - pub const fn new() -> Self { - Self(U256::ZERO) - } - - /// Returns whether steps with given [OpCode] should be traced. - #[inline] - pub const fn is_enabled(&self, op: OpCode) -> bool { - self.0.bit(op.get() as usize) - } - - /// Enables tracing of given [OpCode]. - #[inline] - pub const fn enable(&mut self, op: OpCode) -> &mut Self { - self.0.set_bit(op.get() as usize, true); - self - } - - /// Enables tracing of given [OpCode]. - #[inline] - pub const fn enabled(mut self, op: OpCode) -> Self { - let index = op.get() as usize; - let mut limbs = self.0.into_limbs(); - let (limb, bit) = (index / 64, index % 64); - limbs[limb] |= 1 << bit; - self.0 = U256::from_limbs(limbs); - self - } -} +use evm2::{OpcodeSet, interpreter::opcode::OpCode}; /// Gives guidance to the [TracingInspector](crate::tracing::TracingInspector). /// @@ -78,7 +35,7 @@ pub struct TracingInspectorConfig { pub record_returndata_snapshots: bool, /// Optional filter for opcodes to record. If provided, only steps with opcode in this set will /// be recorded. - pub record_opcodes_filter: Option, + pub record_opcodes_filter: Option, /// Whether to ignore precompile calls. pub exclude_precompile_calls: bool, /// Whether to record logs @@ -432,11 +389,11 @@ impl TracingInspectorConfig { self.set_immediate_bytes(true) } - /// If [OpcodeFilter] is configured, returns whether the given opcode should be recorded. + /// If an opcode filter is configured, returns whether the given opcode should be recorded. /// Otherwise, always returns true. #[inline] pub fn should_record_opcode(&self, op: OpCode) -> bool { - self.record_opcodes_filter.as_ref().is_none_or(|filter| filter.is_enabled(op)) + self.record_opcodes_filter.as_ref().is_none_or(|filter| filter.contains(op.get())) } } diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index 72fef5107..16cecf67a 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -12,7 +12,8 @@ use alloy_rpc_types_trace::geth::{ StateGasTrace, erc7562::Erc7562Config, mux::MuxConfig, }; use evm2::{ - DatabaseError, EvmTypes, EvmTypesHost, Inspector, NoopInspector, TxResultWithState, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, InspectorConfig, NoopInspector, + TxResultWithState, env::BlockEnv, evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, @@ -348,7 +349,7 @@ macro_rules! delegate { Self::StateGasTracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Mux($insp, _) => Inspector::::$method($insp, $($arg),*), #[cfg(feature = "js-tracer")] - Self::Js($insp) => Inspector::::$method(&mut **$insp, $($arg),*), + Self::Js($insp) => Inspector::::$method($insp, $($arg),*), } }; } @@ -358,6 +359,10 @@ impl Inspector for DebugInspector { delegate!(self => inspector.initialize_interp(interp)) } + fn inspector_config(&self) -> InspectorConfig { + delegate!(self => inspector.inspector_config()) + } + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { delegate!(self => inspector.step(interp)) } diff --git a/crates/inspectors/src/tracing/fourbyte.rs b/crates/inspectors/src/tracing/fourbyte.rs index 092d9dd56..9acbb66af 100644 --- a/crates/inspectors/src/tracing/fourbyte.rs +++ b/crates/inspectors/src/tracing/fourbyte.rs @@ -26,7 +26,7 @@ use alloc::format; use alloy_primitives::{Selector, hex, map::HashMap}; use alloy_rpc_types_trace::geth::FourByteFrame; use evm2::{ - EvmTypes, Inspector, + EvmTypes, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageResult}, }; @@ -45,6 +45,10 @@ impl FourByteInspector { } impl Inspector for FourByteInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + fn call( &mut self, interp: &mut Interpreter<'_, '_, T>, diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs index 6f548fe6b..6a0bf6e2b 100644 --- a/crates/inspectors/src/tracing/keccak.rs +++ b/crates/inspectors/src/tracing/keccak.rs @@ -13,7 +13,7 @@ use crate::tracing::{TraceLimits, limits::TraceBudget}; use alloc::{borrow::Cow, vec}; use alloy_primitives::{B256, Bytes, hex, keccak256, map::HashMap}; use evm2::{ - EvmTypes, Inspector, + EvmTypes, Inspector, InspectorConfig, OpcodeSet, interpreter::{Interpreter, opcode::op}, }; use serde_json::Value; @@ -87,6 +87,10 @@ impl Keccak256PreimageInspector { } impl Inspector for Keccak256PreimageInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::new().with_steps([op::KECCAK256].into_iter().collect::()) + } + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { if interp.opcode() != op::KECCAK256 || self.budget.exceeded() { return; diff --git a/crates/inspectors/src/tracing/limits.rs b/crates/inspectors/src/tracing/limits.rs index aab797285..5f9a971ab 100644 --- a/crates/inspectors/src/tracing/limits.rs +++ b/crates/inspectors/src/tracing/limits.rs @@ -69,6 +69,12 @@ impl TraceBudget { self.exceeded } + /// Returns whether exceeding the budget can halt execution. + pub(crate) const fn can_halt(&self) -> bool { + self.limits.max_recorded_bytes.is_some() + && matches!(self.limits.behavior, TraceLimitBehavior::Halt) + } + /// Returns whether the budget was exceeded and the limits halt execution. pub(crate) fn halts(&self) -> bool { self.exceeded && self.limits.behavior == TraceLimitBehavior::Halt diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index e54ce8d74..9b4a02c3e 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -16,7 +16,7 @@ use alloy_primitives::{Address, B256, Bytes, Log, TxKind, U256}; use alloy_rpc_types_trace::parity::StorageDelta; use core::{mem, ops::Range}; use evm2::{ - Evm, EvmFeatures, EvmTypes, Inspector, SpecId, TxResultExt, + Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, TxResultExt, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -33,7 +33,7 @@ pub use builder::{ }; mod config; -pub use config::{OpcodeFilter, StackSnapshotType, TracingInspectorConfig}; +pub use config::{StackSnapshotType, TracingInspectorConfig}; mod erc7562; pub use erc7562::{Erc7562Inspector, Error as Erc7562Error}; @@ -698,6 +698,20 @@ impl Inspector for TracingInspector { } } + #[inline] + fn inspector_config(&self) -> InspectorConfig { + // The step hooks record steps, and halt the execution once a halting byte budget is + // exceeded, which any hook can cause. + let steps = if self.budget.can_halt() { + OpcodeSet::ALL + } else if self.config.record_steps { + self.config.record_opcodes_filter.unwrap_or(OpcodeSet::ALL) + } else { + OpcodeSet::EMPTY + }; + InspectorConfig::new().with_steps(steps) + } + #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { if self.config.record_steps { diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index 50971cb93..af8ee8302 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -11,7 +11,7 @@ use alloy_rpc_types_trace::geth::{ mux::{MuxConfig, MuxFrame}, }; use evm2::{ - DatabaseError, EvmTypes, EvmTypesHost, Inspector, TxResultWithState, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, InspectorConfig, TxResultWithState, env::BlockEnv, evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, @@ -262,27 +262,53 @@ impl MuxInspector { } } -/// Calls a hook on every inspector of the mux. -macro_rules! fan_out { - ($mux:ident.$hook:ident($($arg:expr),*)) => {{ - for (_, child) in &mut $mux.children { - Inspector::::$hook(child, $($arg),*); +/// Runs `$body` with `$member` bound to each inspector of the mux, borrowed as the pattern says. +macro_rules! each_member { + (&mut $mux:ident, | $member:ident | $body:expr) => {{ + for (_, $member) in &mut $mux.children { + $body; } - if let Some(inspector) = &mut $mux.four_byte { - Inspector::::$hook(inspector, $($arg),*); + if let Some($member) = &mut $mux.four_byte { + $body; } - if let Some(inspector) = &mut $mux.tracing { - Inspector::::$hook(inspector, $($arg),*); + if let Some($member) = &mut $mux.tracing { + $body; + } + }}; + (& $mux:ident, | $member:ident | $body:expr) => {{ + for (_, $member) in &$mux.children { + $body; + } + if let Some($member) = &$mux.four_byte { + $body; + } + if let Some($member) = &$mux.tracing { + $body; } }}; } +/// Calls a hook on every inspector of the mux. +macro_rules! fan_out { + ($mux:ident.$hook:ident($($arg:expr),*)) => { + each_member!(&mut $mux, |member| Inspector::::$hook(member, $($arg),*)) + }; +} + impl Inspector for MuxInspector { #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { fan_out!(self.initialize_interp(interp)); } + #[inline] + fn inspector_config(&self) -> InspectorConfig { + let mut config = InspectorConfig::without_steps(); + each_member!(&self, |member| config = + config.union(Inspector::::inspector_config(member))); + config + } + #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { fan_out!(self.step(interp)); diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index e72f2b2ab..400b33a5b 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -4,7 +4,7 @@ use alloc::{vec, vec::Vec}; use alloy_primitives::{Address, B256, Log, U256, address, b256}; use alloy_sol_types::SolValue; use evm2::{ - Evm, EvmFeatures, EvmTypes, Inspector, + Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; @@ -166,6 +166,10 @@ impl TransferInspector { } impl Inspector for TransferInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + fn log(&mut self, log: &Log, _host: &mut T::Host<'_>) { if self.collect_logs && let Some(frame) = self.frames.last_mut() diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index e6e890dea..08d281c42 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -5,8 +5,8 @@ use alloy_eip2930::AccessList; use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; use evm2::{ - BaseEvmTypes, Evm, EvmTypesHost, Inspector, NoopInspector, Precompiles, TxResult, - TxResultWithState, env as evm_env, + BaseEvmTypes, Evm, EvmTypesHost, Inspector, InspectorConfig, NoopInspector, Precompiles, + TxResult, TxResultWithState, env as evm_env, ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, evm::PendingState, interpreter::{Interpreter, Message, MessageResult}, @@ -576,6 +576,11 @@ impl> Inspector for RawInspector { self.inner().initialize_interp(interp); } + fn inspector_config(&self) -> InspectorConfig { + // SAFETY: As in `inner`. + unsafe { &*self.inspector }.inspector_config() + } + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { self.inner().step(interp); } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 8cd8064ee..9aa328629 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -9,24 +9,27 @@ use crate::utils::{ }; use alloy_consensus::crypto::secp256k1::sign_message; use alloy_eips::eip7702::Authorization; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, keccak256, map::HashMap}; +use alloy_primitives::{ + Address, B256, Bytes, Log, TxKind, U256, address, hex, keccak256, map::HashMap, +}; use alloy_rpc_types_trace::geth::{ CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, }; use evm2::{ - AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, ExecutionConfig, Precompiles, Version, + AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, EvmTypesHost, ExecutionConfig, + Inspector, InspectorConfig, OpcodeSet, Precompiles, Version, ethereum::ethereum_tx_registry, evm::{DbResult, DynDatabase}, - interpreter::Word, + interpreter::{Interpreter, Message, MessageResult, Word}, }; use evm2_inspectors::tracing::{ DebugInspector, Erc7562Error, Erc7562Inspector, KECCAK256_PREIMAGE_TRACER, - Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimits, TracingInspector, - TracingInspectorConfig, + Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimitBehavior, TraceLimits, + TracingInspector, TracingInspectorConfig, }; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; #[test] fn test_calltracer_invalid_opcode_errors() { @@ -2797,6 +2800,28 @@ fn keccak256_preimage_tracer_runs_standalone_and_in_a_mux() { assert_eq!(preimages.as_object().unwrap().len(), 1); } +#[test] +fn mux_and_debug_inspector_forward_the_steps_of_their_members() { + let steps = |inspector: &dyn Inspector| inspector.inspector_config().steps(); + let four_byte = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FourByteTracer); + let keccak = GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()); + let mux = |tracers: &[&GethDebugTracerType]| { + let config = MuxConfig(tracers.iter().map(|tracer| ((*tracer).clone(), None)).collect()); + MuxInspector::try_from_config(config).unwrap() + }; + + // The 4byte tracer only needs message hooks, and the keccak tracer steps `KECCAK256`. + let keccak_steps: OpcodeSet = [op::KECCAK256].into_iter().collect(); + assert_eq!(steps(&mux(&[&four_byte])), OpcodeSet::EMPTY); + assert_eq!(steps(&mux(&[&four_byte, &keccak])), keccak_steps); + let options: GethDebugTracingOptions = + serde_json::from_value(serde_json::json!({"tracer": "4byteTracer"})).unwrap(); + assert_eq!(steps(&DebugInspector::new(options).unwrap()), OpcodeSet::EMPTY); + let options: GethDebugTracingOptions = + serde_json::from_value(serde_json::json!({"tracer": "keccak256PreimageTracer"})).unwrap(); + assert_eq!(steps(&DebugInspector::new(options).unwrap()), keccak_steps); +} + #[test] fn keccak256_preimage_tracer_bounds_the_padding_like_geth() { // KECCAK256 of `MEMORY_PAD_LIMIT` and of one byte more at offset 0 of empty memory. @@ -3239,3 +3264,199 @@ fn test_geth_prestate_selfdestruct_reports_slots_before_the_wipe() { }) ); } + +/// Forwards every hook to `inner` and records the opcodes it steps. Steps every opcode if `all`. +struct SteppedSpy { + inner: I, + all: bool, + stepped: BTreeSet, +} + +impl> Inspector for SteppedSpy { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + self.inner.initialize_interp(interp); + } + + fn inspector_config(&self) -> InspectorConfig { + if self.all { InspectorConfig::new() } else { self.inner.inspector_config() } + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + self.stepped.insert(interp.opcode()); + self.inner.step(interp); + } + + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + self.inner.step_end(interp); + } + + fn log(&mut self, log: &Log, host: &mut ::Host<'_>) { + self.inner.log(log, host); + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.inner.call(interp, message) + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + result: &mut MessageResult, + ) { + self.inner.call_end(interp, message, result); + } + + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.inner.create(interp, message) + } + + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + result: &mut MessageResult, + ) { + self.inner.create_end(interp, message, result); + } + + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut ::Host<'_>, + ) { + self.inner.selfdestruct(contract, target, value, host); + } +} + +/// Traces a contract that stores, loads, writes memory, logs and calls a second contract that +/// stores and loads. +fn run_spy(config: TracingInspectorConfig, all: bool) -> SteppedSpy { + let (contract, mut db) = contract_with_code(&hex!( + // SSTORE(0, 5), SLOAD(0), MSTORE8(0, 0xaa), LOG0(0, 1), CALL(0x2000, 0x43), STOP. + "6005" "5f55" "5f54" "50" "60aa" "5f53" "6001" "5f" "a0" + "5f5f5f5f5f" "6043" "612000" "f1" "50" "00" + )); + db.insert_account_info( + &Address::with_last_byte(0x43), + AccountInfo::default().with_code(Bytecode::new_raw( + // SSTORE(1, 7), SLOAD(0), STOP. + hex!("6007" "6001" "55" "5f54" "50" "00").to_vec().into(), + )), + ); + let mut spy = + SteppedSpy { inner: TracingInspector::new(config), all, stepped: BTreeSet::new() }; + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut spy) + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + spy +} + +#[test] +fn tracing_inspector_steps_only_what_its_config_records() { + let steps = |config: TracingInspectorConfig| { + Inspector::::inspector_config(&TracingInspector::new(config)).steps() + }; + let filter: OpcodeSet = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect(); + let filtered = TracingInspectorConfig { + record_opcodes_filter: Some(filter), + ..TracingInspectorConfig::default_geth() + }; + + // The call tracer, the prestate tracer and the parity traces other than `vmTrace` record no + // steps, and the struct logger, `vmTrace` and an unfiltered config step every opcode. + for config in [ + TracingInspectorConfig::none(), + TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(true), + ..Default::default() + }), + TracingInspectorConfig::from_geth_prestate_config(&PreStateConfig::default()), + TracingInspectorConfig::from_flat_call_config(&FlatCallConfig::default()), + TracingInspectorConfig::default_parity(), + TracingInspectorConfig::parity_statediff(), + TracingInspectorConfig { record_opcodes_filter: Some(filter), ..Default::default() }, + ] { + assert_eq!(steps(config), OpcodeSet::EMPTY, "{config:?}"); + } + for config in [ + TracingInspectorConfig::all(), + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace(), + ] { + assert_eq!(steps(config), OpcodeSet::ALL, "{config:?}"); + } + assert_eq!(steps(filtered), filter); + + // Exceeding a halting budget in any hook halts the next step, so every opcode steps. + let halting = TraceLimits::default() + .set_max_recorded_bytes(Some(1024)) + .set_behavior(TraceLimitBehavior::Halt); + for config in [TracingInspectorConfig::none(), filtered] { + let inspector = TracingInspector::new(config).with_limits(halting); + assert_eq!(Inspector::::inspector_config(&inspector).steps(), OpcodeSet::ALL); + } +} + +#[test] +fn tracing_inspector_traces_the_same_with_the_steps_it_asks_for_and_with_every_step() { + let filter: OpcodeSet = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect(); + for (config, stepped) in [ + (TracingInspectorConfig::none(), Some(OpcodeSet::EMPTY)), + ( + TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(true), + ..Default::default() + }), + Some(OpcodeSet::EMPTY), + ), + (TracingInspectorConfig::default_parity(), Some(OpcodeSet::EMPTY)), + (TracingInspectorConfig::parity_statediff(), Some(OpcodeSet::EMPTY)), + (TracingInspectorConfig::default_geth(), None), + (TracingInspectorConfig::parity_vm_trace(), None), + ( + TracingInspectorConfig { + record_opcodes_filter: Some(filter), + record_memory_snapshots: true, + ..TracingInspectorConfig::default_geth() + }, + Some(filter), + ), + ] { + let asked = run_spy(config, false); + let every = run_spy(config, true); + assert_eq!( + format!("{:?}", asked.inner.traces()), + format!("{:?}", every.inner.traces()), + "{config:?}" + ); + let expected: OpcodeSet = match stepped { + Some(set) => { + assert!(every.stepped.len() > set_len(set), "{config:?}"); + set + } + None => every.stepped.iter().copied().collect(), + }; + assert_eq!(asked.stepped.iter().copied().collect::(), expected, "{config:?}"); + } +} + +fn set_len(set: OpcodeSet) -> usize { + (0..=u8::MAX).filter(|op| set.contains(*op)).count() +} diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index a4629164d..bba8e9c74 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -39,6 +39,14 @@ use syn::{ /// checks. /// - `#[instruction(dynamic_gas)]`: Exposes `cx.gas` and marks the instruction as needing access to /// mutable gas state. +/// - `#[instruction(hooked)]`: Marks an instruction that may run inspector hooks or host code that +/// read the interpreter. Before the instruction runs, the dispatch stores the latest pc, stack +/// length and gas into the interpreter, so `hooked` implies `dynamic_gas`. Set it on an +/// instruction that runs a child message. The `call`, `call_end`, `create` and `create_end` hooks +/// get the interpreter from inside such an instruction. `step` and `step_end` get the current +/// state from the dispatch, and `initialize_interp` runs at frame start, so they need no flag. An +/// instruction that only reaches `log` or `selfdestruct` needs none either, because those hooks +/// get the host, which has no current frame. Flag it if a hook starts receiving the interpreter. /// - `#[instruction(EvmTypes = CustomTypes)]`: Implements the instruction for a concrete EVM type /// family instead of generating a generic implementation. /// - `#[instruction(EvmTypes: CustomTypesTrait)]`: Adds a trait bound to the generated generic EVM @@ -140,6 +148,7 @@ impl Parse for InstructionAttr { struct InstructionAttrs { no_stack_preamble: bool, dynamic_gas: bool, + hooked: bool, evm_types: Option, evm_types_span: Option, evm_types_bounds: Vec>, @@ -157,6 +166,10 @@ impl InstructionAttrs { InstructionAttr::Flag(arg) if arg == "dynamic_gas" => { attrs.dynamic_gas = true; } + InstructionAttr::Flag(arg) if arg == "hooked" => { + attrs.hooked = true; + attrs.dynamic_gas = true; + } InstructionAttr::Flag(arg) => { return Err(syn::Error::new_spanned( arg, @@ -329,7 +342,20 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } } }); - let dynamic_gas = instruction_attrs.dynamic_gas; + let flags = { + let mut flags = quote! { evm2::interpreter::private::InstructionFlags::empty() }; + if instruction_attrs.dynamic_gas { + flags = quote! { + #flags.union(evm2::interpreter::private::InstructionFlags::DYNAMIC_GAS) + }; + } + if instruction_attrs.hooked { + flags = quote! { + #flags.union(evm2::interpreter::private::InstructionFlags::HOOKED) + }; + } + flags + }; quote! { #(#fn_attrs)* #[allow(non_camel_case_types)] @@ -340,7 +366,7 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok impl #struct_generics evm2::interpreter::private::Instruction<#evm_types> for #ident #type_generics #where_clause { - const DYNAMIC_GAS: bool = #dynamic_gas; + const FLAGS: evm2::interpreter::private::InstructionFlags = #flags; #[inline] fn execute( From b0aba885e923b0fcdde202e56d284630dc1aeef3 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:09:16 +0100 Subject: [PATCH 103/159] feat(block): add block executor and run EEST blocks on it (#872) Adds `evm2::block` behind a new `block` feature: block executor traits, `BundleState`/`BlockState` and the Ethereum block executor, moved from reth (`reth-evm`, `reth-evm-ethereum`, `reth-execution-types`, `reth-trie-common`) so nodes can execute Ethereum blocks on evm2 without depending on reth. reth becomes a thin adapter over it (paradigmxyz/reth#28238). The EEST blockchain-test runner now executes blocks through `EthBlockExecutor` instead of its own block loop; devnet blockchain and state fixtures give the same results as before. The runner keeps only the checks that compare a block with its header, such as the RLP size, the blob header fields, gas used, receipts and requests hash. The executor owns the checks that need the state: a transaction over the block gas or blob gas allowance (`EthBlockRules::max_blob_gas_per_block`) invalidates the block, and so does a request system contract without code or a failed request call. The DAO drain runs before the block's transactions and keeps the drained accounts, as the fork predates EIP-161. Captured cases hold only the accounts their transactions touch, so the runner adds the EIP-7002 and EIP-7251 contracts to a captured Prague pre-state. Deposit requests are parsed strictly per EIP-6110 (exact length, offsets and sizes; a non-canonical log invalidates the block), replacing the `sol!` decoder, so `alloy-sol-types` is no longer needed. --- Cargo.lock | 2 + crates/eest/Cargo.toml | 2 +- crates/eest/src/blockchaintest/error.rs | 3 - crates/eest/src/blockchaintest/execute.rs | 690 +++----- crates/eest/src/tx.rs | 33 +- crates/evm2/Cargo.toml | 7 + .../src/block/bundle_state/account_status.rs | 388 +++++ .../src/block/bundle_state/bundle_account.rs | 386 +++++ .../src/block/bundle_state/bundle_state.rs | 1397 +++++++++++++++++ crates/evm2/src/block/bundle_state/changes.rs | 75 + crates/evm2/src/block/bundle_state/mod.rs | 45 + .../src/block/bundle_state/plain_account.rs | 82 + crates/evm2/src/block/bundle_state/reverts.rs | 375 +++++ .../block/bundle_state/transition_account.rs | 112 ++ .../block/bundle_state/transition_state.rs | 34 + crates/evm2/src/block/error.rs | 272 ++++ crates/evm2/src/block/ethereum/dao_fork.rs | 128 ++ crates/evm2/src/block/ethereum/execution.rs | 735 +++++++++ crates/evm2/src/block/ethereum/mod.rs | 622 ++++++++ crates/evm2/src/block/ethereum/tests.rs | 287 ++++ crates/evm2/src/block/mod.rs | 311 ++++ crates/evm2/src/block/state.rs | 705 +++++++++ crates/evm2/src/lib.rs | 2 + 23 files changed, 6196 insertions(+), 497 deletions(-) create mode 100644 crates/evm2/src/block/bundle_state/account_status.rs create mode 100644 crates/evm2/src/block/bundle_state/bundle_account.rs create mode 100644 crates/evm2/src/block/bundle_state/bundle_state.rs create mode 100644 crates/evm2/src/block/bundle_state/changes.rs create mode 100644 crates/evm2/src/block/bundle_state/mod.rs create mode 100644 crates/evm2/src/block/bundle_state/plain_account.rs create mode 100644 crates/evm2/src/block/bundle_state/reverts.rs create mode 100644 crates/evm2/src/block/bundle_state/transition_account.rs create mode 100644 crates/evm2/src/block/bundle_state/transition_state.rs create mode 100644 crates/evm2/src/block/error.rs create mode 100644 crates/evm2/src/block/ethereum/dao_fork.rs create mode 100644 crates/evm2/src/block/ethereum/execution.rs create mode 100644 crates/evm2/src/block/ethereum/mod.rs create mode 100644 crates/evm2/src/block/ethereum/tests.rs create mode 100644 crates/evm2/src/block/mod.rs create mode 100644 crates/evm2/src/block/state.rs diff --git a/Cargo.lock b/Cargo.lock index 156c68bf6..8051e8eda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,6 +201,7 @@ dependencies = [ "alloy-primitives", "auto_impl", "dyn-clone", + "serde", ] [[package]] @@ -2580,6 +2581,7 @@ dependencies = [ "alloy-consensus", "alloy-eip7928", "alloy-eips", + "alloy-hardforks", "alloy-primitives", "ark-bls12-381", "ark-bn254", diff --git a/crates/eest/Cargo.toml b/crates/eest/Cargo.toml index 5698f073a..2dfe4f656 100644 --- a/crates/eest/Cargo.toml +++ b/crates/eest/Cargo.toml @@ -14,7 +14,7 @@ repository.workspace = true workspace = true [dependencies] -evm2.workspace = true +evm2 = { workspace = true, features = ["block"] } evm2-jit-runtime = { workspace = true, features = ["llvm"], optional = true } alloy-consensus = { workspace = true, features = ["k256", "std"] } diff --git a/crates/eest/src/blockchaintest/error.rs b/crates/eest/src/blockchaintest/error.rs index 4294c9dc4..84627775e 100644 --- a/crates/eest/src/blockchaintest/error.rs +++ b/crates/eest/src/blockchaintest/error.rs @@ -90,9 +90,6 @@ pub(crate) enum TestErrorKind { /// EVM execution unexpectedly succeeded. #[error("execution succeeded, but expected exception: {0}")] UnexpectedSuccess(String), - /// A system call failed. - #[error("system call failed: {0}")] - SystemCall(&'static str), /// Block gas used in the header does not match the executed transactions. #[error("block gas used mismatch: header {expected}, computed {actual}")] BlockGasUsedMismatch { diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index dcd7ae33a..15269676f 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -21,67 +21,49 @@ use crate::{ state::{insert_account_with_storage, parse_bytecode}, trace::{self, Eip3155Tracer}, tx::{ - TxFields, TxSigner, build_recovered_tx, rpc_access_list, signature_from_vrs, + TxFields, TxSigner, build_signed_recovered_tx, rpc_access_list, signature_from_vrs, signed_authorizations, }, }; -use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; +use alloy_consensus::{ + Eip658Value, EthereumTxEnvelope, Header, Receipt, ReceiptEnvelope, ReceiptWithBloom, TxEip4844, + TxType, +}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; use alloy_eips::{ - eip2718::Typed2718, - eip4844::DATA_GAS_PER_BLOB, - eip6110::{DEPOSIT_REQUEST_TYPE, MAINNET_DEPOSIT_CONTRACT_ADDRESS}, - eip7002::WITHDRAWAL_REQUEST_TYPE, - eip7251::CONSOLIDATION_REQUEST_TYPE, - eip7685::Requests, + eip4844::DATA_GAS_PER_BLOB, eip4895::Withdrawal as AlloyWithdrawal, + eip7002::WITHDRAWAL_REQUEST_PREDEPLOY_CODE, eip7251::CONSOLIDATION_REQUEST_PREDEPLOY_CODE, eip7840::BlobParams, }; -use alloy_primitives::{Address, B256, Bytes, Log, U256, aliases::U512, b256}; +use alloy_primitives::{Address, B256, Bloom, Bytes, Signature, U256, aliases::U512}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use anstyle::{AnsiColor, Color, Style}; use evm2::{ - BaseEvmTypes, Evm, Precompiles, SpecId, TxResult, + BaseEvmTypes, Evm, EvmTypes, Precompiles, SpecId, TxResult, + block::{ + BlockExecutor as _, BundleSource, ReceiptBuilder, ReceiptBuilderCtx, + bundle_state::BundleRetention, + ethereum::{EthBlockExecutionCtx, EthBlockExecutor, EthBlockRules}, + }, env::{BlockEnv, BlockEnvExt}, ethereum::{RecoveredTxEnvelope, ethereum_tx_registry}, evm::{ - AccountChangeRef, AccountInfo as EvmAccountInfo, BEACON_ROOTS_ADDRESS, Bal, - BlockStateAccumulator, DbStats, DbStatsCounts, HISTORY_STORAGE_ADDRESS, InMemoryDB, - StateChangeSink, StateChangeSource, SystemTx, Tee, WITHDRAWAL_REQUEST_ADDRESS, + AccountInfo as EvmAccountInfo, Bal, CONSOLIDATION_REQUEST_ADDRESS, DbStats, DbStatsCounts, + InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, }, - registry::HandlerError, }; #[cfg(feature = "jit")] use std::path::PathBuf; -use std::{collections::BTreeMap, mem, path::Path}; +use std::{borrow::Cow, collections::BTreeMap, mem, path::Path}; pub use crate::execution::ExecutionMode; -const ONE_GWEI: u64 = 1_000_000_000; const ONE_ETHER: u128 = 1_000_000_000_000_000_000; /// EIP-7934 maximum RLP-encoded block size: `MAX_BLOCK_SIZE` (10 MiB) minus `SAFETY_MARGIN` /// (2 MiB). const MAX_RLP_BLOCK_SIZE: usize = 10 * 1024 * 1024 - 2 * 1024 * 1024; -/// First topic of the deposit contract's `DepositEvent`. -const DEPOSIT_EVENT_SIGNATURE_HASH: B256 = - b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"); - -/// ABI layout of a `DepositEvent` payload: the offset of each dynamic field's length word and -/// the field's size, in the order the consensus layer consumes them (public key, withdrawal -/// credentials, amount, signature, index). -const DEPOSIT_EVENT_FIELDS: [(usize, usize); 5] = - [(160, 48), (256, 32), (320, 8), (384, 96), (512, 8)]; - -/// Length of an ABI-encoded `DepositEvent` payload. -const DEPOSIT_EVENT_LENGTH: usize = 576; - -/// EIP-8282 builder deposit request type. -const BUILDER_DEPOSIT_REQUEST_TYPE: u8 = 0x03; - -/// EIP-8282 builder exit request type. -const BUILDER_EXIT_REQUEST_TYPE: u8 = 0x04; - /// Execution options for a single suite. #[derive(Clone, Copy, Debug)] pub struct ExecuteConfig { @@ -213,12 +195,33 @@ pub fn pre_state( name: &str, test_case: &BlockchainTestCase, ) -> Result { - let mut database = - parse_state(&test_case.pre.0).map_err(|err| TestError::case(path, name, err))?; + let mut pre = Cow::Borrowed(&test_case.pre.0); + if is_captured(test_case) + && fork_schedule(test_case.network).is_some_and(|fork| fork.runs_prague()) + { + // A capture holds only the accounts its transactions touch, but the blocks' system calls + // read the request contracts, which must have code. + for (address, code) in [ + (WITHDRAWAL_REQUEST_ADDRESS, WITHDRAWAL_REQUEST_PREDEPLOY_CODE.clone()), + (CONSOLIDATION_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_PREDEPLOY_CODE.clone()), + ] { + pre.to_mut().entry(address).or_insert_with(|| Account { + code, + nonce: U256::ONE, + ..Account::default() + }); + } + } + let mut database = parse_state(&pre).map_err(|err| TestError::case(path, name, err))?; seed_block_hashes(&mut database, test_case); Ok(database) } +/// Returns whether the case comes from `evm2 capture`, which zeroes the genesis state root. +fn is_captured(test_case: &BlockchainTestCase) -> bool { + test_case.genesis_block_header.state_root.is_zero() +} + /// Executes a blockchain test case on `database`, which must hold the case's [`pre_state`]. /// /// Returns the final state root when `config` needs it. @@ -306,7 +309,7 @@ fn execute_case( let mut db_stats_counts = DbStatsCounts::default(); // `evm2 capture` keeps only the accounts the blocks touch and zeroes the genesis state // root, so its blocks' state roots can't be reproduced. Receipt roots still check them. - let captured = test_case.genesis_block_header.state_root.is_zero(); + let captured = is_captured(test_case); let compare_state_root = config.compare_state_root && !captured; hook.case_started(CaseStarted { name, total_blocks, network: test_case.network }); @@ -486,6 +489,23 @@ fn execute_block<'a>( )); } + let ommers = block_ommers(block) + .iter() + .map(|ommer| Header { + beneficiary: ommer.coinbase, + number: ommer.number.saturating_to(), + ..Header::default() + }) + .collect::>(); + let withdrawals = block_withdrawals(block) + .iter() + .map(|withdrawal| AlloyWithdrawal { + index: withdrawal.index.saturating_to(), + validator_index: withdrawal.validator_index.saturating_to(), + address: withdrawal.address, + amount: withdrawal.amount.saturating_to(), + }) + .collect::>(); let initial_database = mem::take(database); let receipt_base = (compare_receipt_root && !spec.enables(SpecId::BYZANTIUM)) .then(|| initial_database.clone()); @@ -510,17 +530,33 @@ fn execute_block<'a>( if trace { evm.set_inspector(Eip3155Tracer::to_stdout()); } - let mut block_state = BlockStateAccumulator::new(); + + let ctx = EthBlockExecutionCtx { + tx_count_hint: Some(block_transactions(block).len()), + parent_hash: parent_block_hash.unwrap_or_default(), + parent_beacon_block_root: beacon_root, + ommers: Cow::Owned(ommers), + withdrawals: Some(Cow::Owned(withdrawals)), + }; + let reward = base_block_reward(spec); + let rules = EthBlockRules { + base_block_reward: (reward != 0).then_some(reward), + max_blob_gas_per_block: blob_params.as_ref().map(BlobParams::max_blob_gas_per_block), + ..EthBlockRules::default() + }; + let mut executor = EthBlockExecutor::new( + evm, + ctx, + rules, + HarnessReceiptBuilder { compute_bloom: compare_receipt_root }, + ); // When the fixture declares an EIP-7928 block access list, build one alongside execution and - // validate it against the fixture. The block access index follows the EIP-7928 layout: index 0 - // for pre-block system calls, `i + 1` for transaction `i`, and the final index for post-block - // rewards/withdrawals. Only Amsterdam+ fixtures carry a block access list, so pre-Amsterdam - // blocks pay nothing. + // validate it against the fixture. Only Amsterdam+ fixtures carry a block access list, so + // pre-Amsterdam blocks pay nothing. let build_bal = block_access_list(block).is_some(); if build_bal { - evm.state_mut().enable_bal_builder(); - evm.state_mut().reset_bal_index(); + executor.enable_block_access_list_builder(); } let result = (|| -> Result { @@ -547,31 +583,12 @@ fn execute_block<'a>( return reject_block(should_fail, path, name, reason); } - if let Err(err) = pre_block_system_calls( - &mut evm, - &mut block_state, - spec, - next_block_env, - *parent_block_hash, - beacon_root, - ) { - // A failed pre-block system call invalidates the block only when the fixture expects - // an exception; otherwise it is a test failure. - if should_fail { - return Ok(BlockResolution::Discard); - } - return Err(TestError::case(path, name, err)); + if let Err(err) = executor.apply_pre_execution_changes() { + return reject_block(should_fail, path, name, err.to_string()); } let transactions = block_transactions(block); - // EIP-8037: track execution and state gas separately for the block-header gas check. - let mut cumulative_tx_gas_used = 0u64; - let mut block_execution_gas_used = 0u64; - let mut block_state_gas_used = 0u64; - let mut block_blob_gas_used = 0u64; - let mut receipts = Vec::with_capacity(transactions.len()); - let mut requests = Requests::default(); - let mut deposit_requests = Vec::new(); + let mut post_state_roots = Vec::new(); for (transaction_index, raw_tx) in transactions.iter().enumerate() { hook.transaction_started(TransactionStarted { block_index, @@ -580,7 +597,7 @@ fn execute_block<'a>( transaction_index, total_transactions, }); - let tx = match build_tx(raw_tx, spec, validate) { + let (tx_env, tx) = match build_tx(raw_tx, spec, validate) { Ok(tx) => tx, Err(_err) if should_fail => { return Ok(BlockResolution::Discard); @@ -598,113 +615,28 @@ fn execute_block<'a>( } }; - // Block-level inclusion check (execution-specs `check_transaction`): the transaction's - // gas limit must fit the block gas remaining, and its blobs the block blob gas - // remaining. Under EIP-8037 (Amsterdam+) the gas limit is checked per dimension: - // capped by the EIP-7825 limit on the execution dimension, uncapped on the state - // dimension. - if let Some(header) = block_header(block) { - let block_gas_limit = header.gas_limit.saturating_to::(); - let tx_gas_limit = tx.gas_limit(); - let (execution_gas_used, state_gas_used, execution_gas_limit) = - if spec.enables(SpecId::AMSTERDAM) { - ( - block_execution_gas_used, - block_state_gas_used, - tx_gas_limit.min(evm.version().tx_gas_limit_cap), - ) - } else { - (cumulative_tx_gas_used, cumulative_tx_gas_used, tx_gas_limit) - }; - let execution_gas_available = block_gas_limit.saturating_sub(execution_gas_used); - let state_gas_available = block_gas_limit.saturating_sub(state_gas_used); - if execution_gas_limit > execution_gas_available - || tx_gas_limit > state_gas_available - { - return reject_block( - should_fail, - path, - name, - format!( - "transaction gas limit {tx_gas_limit} exceeds available block gas \ - (execution {execution_gas_available}, state {state_gas_available})" - ), - ); - } - - if let Some(blob_params) = &blob_params { - let blob_gas = blob_gas_used(raw_tx); - let blob_gas_available = - blob_params.max_blob_gas_per_block().saturating_sub(block_blob_gas_used); - if blob_gas > blob_gas_available { - return reject_block( - should_fail, - path, - name, - format!( - "transaction blob gas {blob_gas} exceeds available block blob \ - gas {blob_gas_available}" - ), - ); - } - block_blob_gas_used += blob_gas; - } - } - - // Transaction `i` is recorded at block access index `i + 1`. - if build_bal { - evm.state_mut().bump_bal_index(); - } - match execute_tx(&mut evm, &mut block_state, &tx) { - Ok(result) => { - cumulative_tx_gas_used = - cumulative_tx_gas_used.saturating_add(result.tx_gas_used()); - block_execution_gas_used = - block_execution_gas_used.saturating_add(result.execution_gas_spent()); - block_state_gas_used = - block_state_gas_used.saturating_add(result.state_gas_spent()); + match executor.execute_transaction((tx_env, &tx)) { + Ok(gas) => { + let receipt = executor.receipts.last().expect("committed transaction receipt"); hook.transaction_finished(TransactionFinished { block_index, total_blocks, block_number, transaction_index, total_transactions, - gas_used: result.tx_gas_used(), - execution_gas_used: result.execution_gas_spent(), - state_gas_used: result.state_gas_spent(), - success: result.status, - logs: &result.logs, + gas_used: gas.tx_gas_used(), + execution_gas_used: gas.regular_gas_used(), + state_gas_used: gas.state_gas_used(), + success: receipt.status(), + logs: receipt.logs(), }); - if spec.enables(SpecId::PRAGUE) - && let Err(reason) = - collect_deposit_requests(&result.logs, &mut deposit_requests) - { - return reject_block(should_fail, path, name, reason); - } - if compare_receipt_root { - let status = if spec.enables(SpecId::BYZANTIUM) { - Eip658Value::Eip658(result.status) - } else { - let mut post_state = receipt_base - .as_ref() - .expect("pre-Byzantium receipt base should be available") - .clone(); - post_state.commit_source(&block_state); - Eip658Value::PostState(state_root_from_database(&post_state)) - }; - let receipt = Receipt { - status, - cumulative_gas_used: cumulative_tx_gas_used, - logs: result.logs, - } - .with_bloom(); - let tx_type = TxType::try_from(tx.inner().ty()) - .expect("executor only builds supported Ethereum transaction types"); - receipts.push(ReceiptEnvelope::from_typed(tx_type, receipt)); + if let Some(base) = &receipt_base { + let mut post_state = base.clone(); + post_state.cache.merge(executor.evm.overlay_db_mut().cache.clone()); + post_state_roots.push(state_root_from_database(&post_state)); } } - Err(err) if should_fail => { - let _ = err; + Err(_err) if should_fail => { return Ok(BlockResolution::Discard); } Err(err) => { @@ -716,23 +648,40 @@ fn execute_block<'a>( total_transactions, error: &err, }); - return Err(TestError::case(path, name, err.into())); + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure(err.to_string()), + )); } } } + for ommer in block_ommers(block) { + let age = next_block_env.number.checked_sub(ommer.number); + if age.is_none_or(|age| age.is_zero() || age > U256::from(6)) { + let err = TestErrorKind::InvalidOmmerAge { + block_number: next_block_env.number, + ommer_number: ommer.number, + }; + if should_fail { + return Ok(BlockResolution::Discard); + } + return Err(TestError::case(path, name, err)); + } + } + + let post_execution = match executor.apply_post_execution_changes(0) { + Ok(output) => output, + Err(err) => return reject_block(should_fail, path, name, err.to_string()), + }; + // Validate the block header's gas used against the executed transactions. Under EIP-8037 // (Amsterdam+) execution and state gas are tracked separately and the header records their // max; earlier forks record the cumulative per-transaction gas used (refunds included). - // Block-validation failures on a block that expects an exception resolve to a discard, - // mirroring execution-specs raising `InvalidBlock`. if let Some(expected) = block_gas_used(block) { let expected = expected.saturating_to::(); - let actual = if spec.enables(SpecId::AMSTERDAM) { - block_execution_gas_used.max(block_state_gas_used) - } else { - cumulative_tx_gas_used - }; + let actual = post_execution.gas_used; if actual != expected { if should_fail { return Ok(BlockResolution::Discard); @@ -747,21 +696,38 @@ fn execute_block<'a>( if spec.enables(SpecId::CANCUN) && let Some(header) = block_header(block) - && header.blob_gas_used != Some(U256::from(block_blob_gas_used)) + && header.blob_gas_used != Some(U256::from(post_execution.blob_gas_used)) { return reject_block( should_fail, path, name, format!( - "block blob gas used mismatch: header {:?}, computed {block_blob_gas_used}", - header.blob_gas_used + "block blob gas used mismatch: header {:?}, computed {}", + header.blob_gas_used, post_execution.blob_gas_used ), ); } if compare_receipt_root && let Some(header) = block_header(block) { - let got = alloy_consensus::proofs::calculate_receipt_root(&receipts); + let got = if receipt_base.is_some() { + let receipts = executor + .receipts + .iter() + .zip(&post_state_roots) + .map(|(receipt, root)| { + let receipt_root = Receipt { + status: Eip658Value::PostState(*root), + cumulative_gas_used: receipt.cumulative_gas_used(), + logs: receipt.logs().to_vec(), + }; + ReceiptEnvelope::from_typed(receipt.tx_type(), receipt_root.with_bloom()) + }) + .collect::>(); + alloy_consensus::proofs::calculate_receipt_root(&receipts) + } else { + alloy_consensus::proofs::calculate_receipt_root(&executor.receipts) + }; if got != header.receipt_trie { if should_fail { return Ok(BlockResolution::Discard); @@ -774,34 +740,10 @@ fn execute_block<'a>( } } - // Post-block rewards and withdrawals are recorded at the final (post-execution) index. - if build_bal { - evm.state_mut().bump_bal_index(); - } - - requests.push_request_with_type(DEPOSIT_REQUEST_TYPE, deposit_requests); - if let Err(err) = post_block_transition( - &mut evm, - &mut block_state, - spec, - next_block_env, - block_ommers(block), - block_withdrawals(block), - captured, - &mut requests, - ) { - // A failed post-block system call (e.g. SYSTEM_CONTRACT_CALL_FAILED) invalidates the - // block, mirroring execution-specs raising `InvalidBlock`. - if should_fail { - return Ok(BlockResolution::Discard); - } - return Err(TestError::case(path, name, err)); - } - if spec.enables(SpecId::PRAGUE) && let Some(header) = block_header(block) { - let computed = requests.requests_hash(); + let computed = post_execution.requests.requests_hash(); if header.requests_hash != Some(computed) { return reject_block( should_fail, @@ -816,7 +758,7 @@ fn execute_block<'a>( } if let Some(expected_bal) = block_access_list(block) { - let built = evm.state_mut().take_bal_builder().unwrap_or_default(); + let built = executor.take_block_access_list().unwrap_or_default(); if let Err(kind) = check_block_access_list(block_index, built, expected_bal, block_header(block)) { @@ -844,6 +786,8 @@ fn execute_block<'a>( // The EVM was constructed with this concrete database above; recover it before returning so // invalid blocks leave the caller's state unchanged. + let mut evm = executor.evm; + let block_state = executor.block_state; let (mut restored_database, block_db_stats_counts) = if db_stats { let stats = evm .database_as_mut::>() @@ -866,7 +810,9 @@ fn execute_block<'a>( match result { Ok(BlockResolution::Commit(block_hash)) => { - restored_database.commit_source(&block_state); + // Only the plain state is applied, so the block's reverts are not built. + let block_state = block_state.into_bundle_with_retention(BundleRetention::PlainState); + restored_database.commit_source(&BundleSource(&block_state)); restored_database.insert_block_hash(&next_block_env.number, &block_hash); *database = restored_database; *block_env = next_block_env; @@ -905,6 +851,36 @@ fn execute_block<'a>( } } +/// Builds the same receipts as [`EthReceiptBuilder`](evm2::block::ethereum::EthReceiptBuilder), +/// but computes the logs bloom only when the receipt root is compared, as nothing else reads it. +#[derive(Clone, Copy, Debug)] +struct HarnessReceiptBuilder { + compute_bloom: bool, +} + +impl ReceiptBuilder for HarnessReceiptBuilder { + type Transaction = EthereumTxEnvelope; + type Receipt = ReceiptEnvelope; + + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx>, + ) -> ReceiptEnvelope { + let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; + let receipt = Receipt { + status: Eip658Value::Eip658(result.status), + cumulative_gas_used, + logs: result.logs, + }; + let receipt = if self.compute_bloom { + receipt.with_bloom() + } else { + ReceiptWithBloom::new(receipt, Bloom::ZERO) + }; + ReceiptEnvelope::from_typed(tx_type, receipt) + } +} + fn print_db_stats(counts: DbStatsCounts) { let style = db_stats_style(); eprintln!("{style}db stats{style:#}: get_account={}", counts.get_account); @@ -1092,255 +1068,6 @@ fn reject_block( } } -/// Returns the blob gas a transaction consumes (EIP-4844). -const fn blob_gas_used(tx: &Transaction) -> u64 { - tx.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB -} - -fn pre_block_system_calls( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - spec: SpecId, - block: BlockEnv, - parent_block_hash: Option, - parent_beacon_block_root: Option, -) -> Result<(), TestErrorKind> { - if block.number.is_zero() { - return Ok(()); - } - if spec.enables(SpecId::CANCUN) - && let Some(root) = parent_beacon_block_root - { - run_system_call( - evm, - block_state, - BEACON_ROOTS_ADDRESS, - Bytes::copy_from_slice(root.as_slice()), - "eip4788", - SystemCallCheck::None, - )?; - } - if spec.enables(SpecId::PRAGUE) - && let Some(hash) = parent_block_hash - { - run_system_call( - evm, - block_state, - HISTORY_STORAGE_ADDRESS, - Bytes::copy_from_slice(hash.as_slice()), - "eip2935", - SystemCallCheck::None, - )?; - } - Ok(()) -} - -#[expect(clippy::too_many_arguments)] -fn post_block_transition( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - spec: SpecId, - block: BlockEnv, - ommers: &[BlockHeader], - withdrawals: &[Withdrawal], - captured: bool, - requests: &mut Requests, -) -> Result<(), TestErrorKind> { - // A captured case keeps only the accounts its blocks touch, so the system contracts are absent. - let check = if captured { SystemCallCheck::Outcome } else { SystemCallCheck::OutcomeAndCode }; - let reward = block_reward(spec, ommers.len()); - if reward != 0 { - increment_balance(evm, block_state, block.beneficiary, U256::from(reward))?; - - let base_reward = U256::from(base_block_reward(spec)); - for ommer in ommers { - let Some(age) = block.number.checked_sub(ommer.number) else { - return Err(TestErrorKind::InvalidOmmerAge { - block_number: block.number, - ommer_number: ommer.number, - }); - }; - if age.is_zero() || age > U256::from(6) { - return Err(TestErrorKind::InvalidOmmerAge { - block_number: block.number, - ommer_number: ommer.number, - }); - } - let reward = (U256::from(8) - age) * base_reward / U256::from(8); - increment_balance(evm, block_state, ommer.coinbase, reward)?; - } - } - - if spec.enables(SpecId::SHANGHAI) { - for withdrawal in withdrawals { - increment_balance( - evm, - block_state, - withdrawal.address, - withdrawal.amount.saturating_mul(U256::from(ONE_GWEI)), - )?; - } - } - - if spec.enables(SpecId::PRAGUE) { - let output = run_system_call( - evm, - block_state, - WITHDRAWAL_REQUEST_ADDRESS, - Bytes::new(), - "eip7002", - check, - )?; - requests.push_request_with_type(WITHDRAWAL_REQUEST_TYPE, output); - let output = run_system_call( - evm, - block_state, - evm2::evm::CONSOLIDATION_REQUEST_ADDRESS, - Bytes::new(), - "eip7251", - check, - )?; - requests.push_request_with_type(CONSOLIDATION_REQUEST_TYPE, output); - } - - // EIP-8282: builder deposit (request type 0x03) and builder exit (0x04) requests, Amsterdam+. - if spec.enables(SpecId::AMSTERDAM) { - let output = run_system_call( - evm, - block_state, - evm2::evm::BUILDER_DEPOSIT_REQUEST_ADDRESS, - Bytes::new(), - "eip8282_deposit", - check, - )?; - requests.push_request_with_type(BUILDER_DEPOSIT_REQUEST_TYPE, output); - let output = run_system_call( - evm, - block_state, - evm2::evm::BUILDER_EXIT_REQUEST_ADDRESS, - Bytes::new(), - "eip8282_exit", - check, - )?; - requests.push_request_with_type(BUILDER_EXIT_REQUEST_TYPE, output); - } - Ok(()) -} - -/// What a system call must satisfy for the block to be valid. -#[derive(Clone, Copy, Eq, PartialEq)] -enum SystemCallCheck { - /// The call may fail (EIP-4788, EIP-2935). - None, - /// The call must succeed. - Outcome, - /// The target must have code, and the call must succeed (execution-specs - /// `process_checked_system_transaction`). - OutcomeAndCode, -} - -fn run_system_call( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - address: Address, - data: Bytes, - label: &'static str, - check: SystemCallCheck, -) -> Result { - // A checked call to a target without code is an invalid block (execution-specs - // `SYSTEM_CONTRACT_EMPTY`); the EVM itself treats it as a successful no-op. - if check == SystemCallCheck::OutcomeAndCode { - let info = evm - .read_account_info(&address) - .map_err(|code| TestErrorKind::UnexpectedFailure(code.to_string()))?; - if info.is_none_or(|info| info.is_empty_code_hash()) { - return Err(TestErrorKind::UnexpectedFailure(format!( - "{label} system contract has no code" - ))); - } - } - let executed = evm.system_call(SystemTx::new(address, data))?; - if !executed.result().status { - // A failed unchecked call keeps the state accesses it recorded, which a BAL lists as reads. - if check == SystemCallCheck::None { - let _ = executed.commit_to(block_state); - return Ok(Bytes::new()); - } - let _ = executed.discard(); - return Err(TestErrorKind::SystemCall(label)); - } - Ok(executed.commit_to(block_state).output) -} - -/// Appends the unframed fields of every deposit event in `logs` to `deposit_requests` -/// (execution-specs `parse_deposit_requests`). -fn collect_deposit_requests(logs: &[Log], deposit_requests: &mut Vec) -> Result<(), String> { - for log in logs { - if log.address == MAINNET_DEPOSIT_CONTRACT_ADDRESS - && log.topics().first() == Some(&DEPOSIT_EVENT_SIGNATURE_HASH) - { - extract_deposit_data(&log.data.data, deposit_requests)?; - } - } - Ok(()) -} - -/// Strips the ABI framing from a `DepositEvent` payload, appending the fields to `out` -/// (execution-specs `extract_deposit_data`). -fn extract_deposit_data(data: &[u8], out: &mut Vec) -> Result<(), String> { - if data.len() != DEPOSIT_EVENT_LENGTH { - return Err("invalid deposit event data length".to_string()); - } - let word = |offset: usize| U256::from_be_slice(&data[offset..offset + 32]); - for (index, (offset, _)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { - if word(index * 32) != U256::from(offset) { - return Err(format!("invalid offset for deposit field {index}")); - } - } - for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { - if word(offset) != U256::from(size) { - return Err(format!("invalid size for deposit field {index}")); - } - out.extend_from_slice(&data[offset + 32..offset + 32 + size]); - } - Ok(()) -} - -fn execute_tx( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - tx: &RecoveredTxEnvelope, -) -> Result { - Ok(evm.transact(tx)?.commit_to(block_state)) -} - -fn commit_state_changes( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - changes: &S, -) { - let mut sink = Tee::new(evm.overlay_db_mut(), block_state); - let Ok(()) = changes.visit(&mut sink); -} - -struct AccountStateChange { - address: Address, - original: Option, - current: Option, -} - -impl StateChangeSource for AccountStateChange { - fn visit(&self, sink: &mut S) -> Result<(), S::Error> { - sink.account(AccountChangeRef { - address: self.address, - original: self.original.as_ref(), - current: self.current.as_ref(), - created: false, - selfdestructed: false, - }) - } -} - fn parse_state( accounts: &std::collections::BTreeMap, ) -> Result { @@ -1392,21 +1119,27 @@ fn build_tx( raw: &Transaction, spec: SpecId, validate: bool, -) -> Result { +) -> Result<(RecoveredTxEnvelope, EthereumTxEnvelope), TestErrorKind> { let gas_limit = raw.gas_limit.try_into().map_err(|_| TestErrorKind::Overflow("gasLimit"))?; let nonce = raw.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; let tx_type = raw.tx_type(); - let (signer, chain_id) = if validate { + let (signer, chain_id, signature) = if validate { let (signature, legacy_chain_id) = signature_from_vrs(raw.r, raw.s, raw.v, tx_type == 0, spec)?; // A legacy transaction signs its chain ID through `v`, so `v` decides it. let chain_id = if tx_type == 0 { legacy_chain_id.map(U256::from) } else { raw.chain_id }; - (TxSigner::Signature { signature, spec }, chain_id) + (TxSigner::Signature { signature, spec }, chain_id, signature) } else { - (TxSigner::Fixture(raw.sender.ok_or(TestErrorKind::MissingSender)?), raw.chain_id) + // The signature only names the transaction (its hash) when the sender is not recovered. + let signature = Signature::new(raw.r, raw.s, raw.v.bit(0)); + ( + TxSigner::Fixture(raw.sender.ok_or(TestErrorKind::MissingSender)?), + raw.chain_id, + signature, + ) }; - let tx = build_recovered_tx( + let (tx, signed) = build_signed_recovered_tx( TxFields { tx_type: Some(tx_type), kind: raw.kind(), @@ -1424,6 +1157,7 @@ fn build_tx( max_fee_per_blob_gas: raw.max_fee_per_blob_gas, }, signer, + signature, )?; if validate && let Some(expected) = raw.sender @@ -1431,7 +1165,7 @@ fn build_tx( { return Err(TestErrorKind::SenderMismatch { recovered: tx.signer(), expected }); } - Ok(tx) + Ok((tx, signed)) } fn access_list(raw: &Transaction) -> Result, TestErrorKind> { @@ -1444,11 +1178,6 @@ fn access_list(raw: &Transaction) -> Result, TestErrorKind Ok(Some(rpc_access_list(access_list.iter()))) } -const fn block_reward(spec: SpecId, ommers: usize) -> u128 { - let reward = base_block_reward(spec); - reward + (reward >> 5) * ommers as u128 -} - const fn base_block_reward(spec: SpecId) -> u128 { if spec.enables(SpecId::MERGE) { 0 @@ -1461,28 +1190,6 @@ const fn base_block_reward(spec: SpecId) -> u128 { } } -fn increment_balance( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - address: Address, - amount: U256, -) -> Result<(), TestErrorKind> { - let original = match evm.read_account_info(&address) { - Ok(info) => info, - Err(code) => return Err(TestErrorKind::UnexpectedFailure(code.to_string())), - }; - let mut current = original.clone().unwrap_or_default(); - current.balance = current.balance.saturating_add(amount); - // EELS `modify_state` destroys an account left empty by the modification, so a zero-amount - // withdrawal neither creates its recipient nor keeps a pre-existing empty one alive. The - // BAL commit below still runs to record the touched address. - let current = (!current.is_empty()).then_some(current); - - let change = AccountStateChange { address, original, current }; - commit_state_changes(evm, block_state, &change); - Ok(()) -} - fn validate_post_state( database: &InMemoryDB, expected: &std::collections::BTreeMap, @@ -1781,6 +1488,12 @@ impl ForkSchedule { Ok(Some(entry.params(stage.spec))) } + /// Returns whether any block of the fork runs Prague or later. + fn runs_prague(&self) -> bool { + self.initial.spec.enables(SpecId::PRAGUE) + || self.transition.is_some_and(|(_, stage)| stage.spec.enables(SpecId::PRAGUE)) + } + /// Returns whether any spec of the fork is skipped. fn is_skipped(&self) -> bool { is_blockchain_fork_skipped(self.initial.spec) @@ -2261,6 +1974,7 @@ mod tests { }, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: B256::with_last_byte(2), number: U256::ONE, @@ -2460,6 +2174,7 @@ mod tests { }, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: block_hash, number: U256::ONE, @@ -2568,6 +2283,7 @@ mod tests { genesis_rlp: None, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: block_hash, number: U256::ONE, diff --git a/crates/eest/src/tx.rs b/crates/eest/src/tx.rs index 059d59ff5..b93aaa6df 100644 --- a/crates/eest/src/tx.rs +++ b/crates/eest/src/tx.rs @@ -1,5 +1,6 @@ use alloy_consensus::{ - EthereumTxEnvelope, SignableTransaction, TxEip4844, TypedTransaction, + EthereumTxEnvelope, SignableTransaction, TxEip4844, TxEnvelope as AlloyTxEnvelope, + TypedTransaction, crypto::{RecoveryError, secp256k1}, transaction::Recovered, }; @@ -136,6 +137,25 @@ pub(crate) fn build_recovered_tx( fields: TxFields, signer: TxSigner, ) -> Result { + let tx = consensus_tx(fields)?; + let caller = tx_caller(&tx, signer)?; + Ok(recovered_envelope(tx, caller)) +} + +/// Builds a fixture transaction like [`build_recovered_tx`], also returning it signed with +/// `signature` as a consensus envelope. +pub(crate) fn build_signed_recovered_tx( + fields: TxFields, + signer: TxSigner, + signature: Signature, +) -> Result<(RecoveredTxEnvelope, EthereumTxEnvelope), TxBuildError> { + let tx = consensus_tx(fields)?; + let caller = tx_caller(&tx, signer)?; + let signed = AlloyTxEnvelope::from(tx.clone().into_signed(signature)).into(); + Ok((recovered_envelope(tx, caller), signed)) +} + +fn consensus_tx(fields: TxFields) -> Result { let mut request = TransactionRequest::default() .gas_limit(fields.gas_limit) .nonce(fields.nonce) @@ -190,15 +210,16 @@ pub(crate) fn build_recovered_tx( request.blob_versioned_hashes = Some(fields.blob_versioned_hashes); } - let tx = - request.build_consensus_tx().map_err(|err| TxBuildError::BuildTransaction(err.error))?; - let caller = match signer { + request.build_consensus_tx().map_err(|err| TxBuildError::BuildTransaction(err.error)) +} + +fn tx_caller(tx: &TypedTransaction, signer: TxSigner) -> Result { + Ok(match signer { TxSigner::Fixture(caller) => caller, TxSigner::Signature { signature, spec } => { recover_sender(&signature, tx.signature_hash(), spec)? } - }; - Ok(recovered_envelope(tx, caller)) + }) } /// Decodes an EIP-2718 signed transaction and recovers its sender. diff --git a/crates/evm2/Cargo.toml b/crates/evm2/Cargo.toml index 640184f03..e2905df48 100644 --- a/crates/evm2/Cargo.toml +++ b/crates/evm2/Cargo.toml @@ -38,6 +38,9 @@ serde = { workspace = true, features = ["derive", "alloc"], optional = true } # "wincode" wincode = { workspace = true, optional = true } +# "block" +alloy-hardforks = { workspace = true, optional = true } + # "async" corosensei = { workspace = true, optional = true } @@ -148,6 +151,9 @@ std = [ async = ["std", "dep:corosensei"] +# Block execution: `evm2::block`. +block = ["dep:alloy-hardforks"] + wincode = ["dep:wincode"] serde = [ @@ -155,6 +161,7 @@ serde = [ "alloy-consensus/serde", "alloy-eip7928/serde", "alloy-eips/serde", + "alloy-hardforks?/serde", "alloy-primitives/serde", "ark-serialize/serde", "bitflags/serde", diff --git a/crates/evm2/src/block/bundle_state/account_status.rs b/crates/evm2/src/block/bundle_state/account_status.rs new file mode 100644 index 000000000..4a3dbdcac --- /dev/null +++ b/crates/evm2/src/block/bundle_state/account_status.rs @@ -0,0 +1,388 @@ +/// AccountStatus represents the various states an account can be in after being loaded from the +/// database. +/// +/// After account get loaded from database it can be in a lot of different states +/// while we execute multiple transaction and even blocks over account that is in memory. +/// This structure models all possible states that account can be in. +/// +/// # Variants +/// +/// - `LoadedNotExisting`: the account has been loaded but does not exist. +/// - `Loaded`: the account has been loaded and exists. +/// - `LoadedEmptyEIP161`: the account is loaded and empty, as per EIP-161. +/// - `InMemoryChange`: there are changes in the account that exist only in memory. +/// - `Changed`: the account has been modified. +/// - `Destroyed`: the account has been destroyed. +/// - `DestroyedChanged`: the account has been destroyed and then modified. +/// - `DestroyedAgain`: the account has been destroyed again. +#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum AccountStatus { + /// The account has been loaded but does not exist. + #[default] + LoadedNotExisting, + /// The account has been loaded and exists. + Loaded, + /// The account is loaded and empty, as per EIP-161. + LoadedEmptyEIP161, + /// There are changes in the account that exist only in memory. + InMemoryChange, + /// The account has been modified. + Changed, + /// The account has been destroyed. + Destroyed, + /// The account has been destroyed and then modified. + DestroyedChanged, + /// The account has been destroyed again. + DestroyedAgain, +} + +impl AccountStatus { + /// Account is not modified and just loaded from database. + pub const fn is_not_modified(&self) -> bool { + matches!(self, Self::LoadedNotExisting | Self::Loaded | Self::LoadedEmptyEIP161) + } + + /// Account was destroyed by calling SELFDESTRUCT. + /// This means that full account and storage are inside memory. + pub const fn was_destroyed(&self) -> bool { + matches!(self, Self::Destroyed | Self::DestroyedChanged | Self::DestroyedAgain) + } + + /// This means storage is known, it can be newly created or storage got destroyed. + pub const fn is_storage_known(&self) -> bool { + matches!( + self, + Self::LoadedNotExisting + | Self::InMemoryChange + | Self::Destroyed + | Self::DestroyedChanged + | Self::DestroyedAgain + ) + } + + /// Account is modified but not destroyed. + /// This means that some storage values can be found in both + /// memory and database. + pub const fn is_modified_and_not_destroyed(&self) -> bool { + matches!(self, Self::Changed | Self::InMemoryChange) + } + + /// Returns the next account status on creation. + pub const fn on_created(&self) -> Self { + match self { + // If account was destroyed previously just copy new info to it. + Self::DestroyedAgain + | Self::Destroyed + | Self::DestroyedChanged => Self::DestroyedChanged, + // If account is loaded from db. + Self::LoadedNotExisting + // Loaded empty eip161 to creates is not possible as CREATE2 was added after EIP-161 + | Self::LoadedEmptyEIP161 + | Self::Loaded + | Self::Changed + | Self::InMemoryChange => { + // If account is loaded and not empty this means that account has some balance. + // This means that account cannot be created. + // We are assuming that EVM did necessary checks before allowing account to be created. + Self::InMemoryChange + } + } + } + + /// Returns the next account status on touched empty account post state clear EIP (EIP-161). + pub const fn on_touched_empty_post_eip161(&self) -> Self { + match self { + // Account can be touched but not existing. The status should remain the same. + Self::LoadedNotExisting => Self::LoadedNotExisting, + // Account can be created empty and only then touched. + Self::InMemoryChange | Self::Destroyed | Self::LoadedEmptyEIP161 => Self::Destroyed, + // Transition to destroy the account. + Self::DestroyedAgain | Self::DestroyedChanged => Self::DestroyedAgain, + // Account can become empty. + Self::Changed | Self::Loaded => Self::Destroyed, + } + } + + /// Returns the next account status on change. + pub const fn on_changed(&self, had_no_nonce_and_code: bool) -> Self { + match self { + // If the account was loaded as not existing, promote it to changed. + // This account was likely created by a balance transfer. + Self::LoadedNotExisting => Self::InMemoryChange, + // Change on empty account, should transfer storage if there is any. + // There is possibility that there are storage entries inside db. + // That storage is used in merkle tree calculation before state clear EIP. + Self::LoadedEmptyEIP161 => Self::InMemoryChange, + // The account was loaded as existing. + Self::Loaded => { + if had_no_nonce_and_code { + // Account is fully in memory + Self::InMemoryChange + } else { + // Can be contract and some of storage slots can be present inside db. + Self::Changed + } + } + + // On change, the "changed" type account statuses are preserved. + // Any checks for empty accounts are done outside of this fn. + Self::Changed => Self::Changed, + Self::InMemoryChange => Self::InMemoryChange, + Self::DestroyedChanged => Self::DestroyedChanged, + + // If account is destroyed and then changed this means this is + // balance transfer. + Self::Destroyed | Self::DestroyedAgain => Self::DestroyedChanged, + } + } + + /// Returns the next account status on selfdestruct. + pub const fn on_selfdestructed(&self) -> Self { + match self { + // Non existing account can't be destroyed. + Self::LoadedNotExisting => Self::LoadedNotExisting, + // If account is created and selfdestructed in the same block, mark it as destroyed + // again. Note: There is no big difference between Destroyed and + // DestroyedAgain in this case, but was added for clarity. + Self::DestroyedChanged | Self::DestroyedAgain | Self::Destroyed => Self::DestroyedAgain, + + // Transition to destroyed status. + _ => Self::Destroyed, + } + } + + /// Transition to other state while preserving invariance of this state. + /// + /// It this account was Destroyed and other account is not: + /// - We should mark extended account as destroyed too. + /// - And as other account had some changes, extended account should be marked as + /// DestroyedChanged. + /// + /// If both account are not destroyed and if this account is in memory: + /// - This means that extended account is in memory too. + /// + /// Otherwise, if both are destroyed or other is destroyed: + /// - Sets other status to extended account. + pub fn transition(&mut self, other: Self) { + *self = match (self.was_destroyed(), other.was_destroyed()) { + (true, false) => Self::DestroyedChanged, + (false, false) if *self == Self::InMemoryChange => Self::InMemoryChange, + _ => other, + }; + } +} + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn test_account_status() { + // Account not modified + assert!(AccountStatus::Loaded.is_not_modified()); + assert!(AccountStatus::LoadedEmptyEIP161.is_not_modified()); + assert!(AccountStatus::LoadedNotExisting.is_not_modified()); + assert!(!AccountStatus::Changed.is_not_modified()); + assert!(!AccountStatus::InMemoryChange.is_not_modified()); + assert!(!AccountStatus::Destroyed.is_not_modified()); + assert!(!AccountStatus::DestroyedChanged.is_not_modified()); + assert!(!AccountStatus::DestroyedAgain.is_not_modified()); + + // We know full storage + assert!(!AccountStatus::LoadedEmptyEIP161.is_storage_known()); + assert!(AccountStatus::LoadedNotExisting.is_storage_known()); + assert!(AccountStatus::InMemoryChange.is_storage_known()); + assert!(AccountStatus::Destroyed.is_storage_known()); + assert!(AccountStatus::DestroyedChanged.is_storage_known()); + assert!(AccountStatus::DestroyedAgain.is_storage_known()); + assert!(!AccountStatus::Loaded.is_storage_known()); + assert!(!AccountStatus::Changed.is_storage_known()); + + // Account was destroyed + assert!(!AccountStatus::LoadedEmptyEIP161.was_destroyed()); + assert!(!AccountStatus::LoadedNotExisting.was_destroyed()); + assert!(!AccountStatus::InMemoryChange.was_destroyed()); + assert!(AccountStatus::Destroyed.was_destroyed()); + assert!(AccountStatus::DestroyedChanged.was_destroyed()); + assert!(AccountStatus::DestroyedAgain.was_destroyed()); + assert!(!AccountStatus::Loaded.was_destroyed()); + assert!(!AccountStatus::Changed.was_destroyed()); + + // Account modified but not destroyed + assert!(AccountStatus::Changed.is_modified_and_not_destroyed()); + assert!(AccountStatus::InMemoryChange.is_modified_and_not_destroyed()); + assert!(!AccountStatus::Loaded.is_modified_and_not_destroyed()); + assert!(!AccountStatus::LoadedEmptyEIP161.is_modified_and_not_destroyed()); + assert!(!AccountStatus::LoadedNotExisting.is_modified_and_not_destroyed()); + assert!(!AccountStatus::Destroyed.is_modified_and_not_destroyed()); + assert!(!AccountStatus::DestroyedChanged.is_modified_and_not_destroyed()); + assert!(!AccountStatus::DestroyedAgain.is_modified_and_not_destroyed()); + } + + #[test] + fn test_on_created() { + assert_eq!(AccountStatus::Destroyed.on_created(), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::DestroyedAgain.on_created(), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::DestroyedChanged.on_created(), AccountStatus::DestroyedChanged); + + assert_eq!(AccountStatus::LoadedNotExisting.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Loaded.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::LoadedEmptyEIP161.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Changed.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::InMemoryChange.on_created(), AccountStatus::InMemoryChange); + } + + #[test] + fn test_on_touched_empty_post_eip161() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_touched_empty_post_eip161(), + AccountStatus::LoadedNotExisting + ); + assert_eq!( + AccountStatus::InMemoryChange.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::Destroyed.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::DestroyedAgain.on_touched_empty_post_eip161(), + AccountStatus::DestroyedAgain + ); + assert_eq!( + AccountStatus::DestroyedChanged.on_touched_empty_post_eip161(), + AccountStatus::DestroyedAgain + ); + assert_eq!(AccountStatus::Loaded.on_touched_empty_post_eip161(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::Changed.on_touched_empty_post_eip161(), AccountStatus::Destroyed); + } + + #[test] + fn test_on_changed() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_changed(true), + AccountStatus::InMemoryChange + ); + assert_eq!( + AccountStatus::LoadedNotExisting.on_changed(false), + AccountStatus::InMemoryChange + ); + + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_changed(true), + AccountStatus::InMemoryChange + ); + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_changed(false), + AccountStatus::InMemoryChange + ); + + assert_eq!(AccountStatus::Loaded.on_changed(true), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Loaded.on_changed(false), AccountStatus::Changed); + + assert_eq!(AccountStatus::Changed.on_changed(true), AccountStatus::Changed); + assert_eq!(AccountStatus::Changed.on_changed(false), AccountStatus::Changed); + + assert_eq!(AccountStatus::InMemoryChange.on_changed(true), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::InMemoryChange.on_changed(false), AccountStatus::InMemoryChange); + + assert_eq!( + AccountStatus::DestroyedChanged.on_changed(true), + AccountStatus::DestroyedChanged + ); + assert_eq!( + AccountStatus::DestroyedChanged.on_changed(false), + AccountStatus::DestroyedChanged + ); + + assert_eq!(AccountStatus::Destroyed.on_changed(true), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::Destroyed.on_changed(false), AccountStatus::DestroyedChanged); + + assert_eq!(AccountStatus::DestroyedAgain.on_changed(true), AccountStatus::DestroyedChanged); + assert_eq!( + AccountStatus::DestroyedAgain.on_changed(false), + AccountStatus::DestroyedChanged + ); + } + + #[test] + fn test_on_selfdestructed() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_selfdestructed(), + AccountStatus::LoadedNotExisting + ); + + assert_eq!( + AccountStatus::DestroyedChanged.on_selfdestructed(), + AccountStatus::DestroyedAgain + ); + assert_eq!( + AccountStatus::DestroyedAgain.on_selfdestructed(), + AccountStatus::DestroyedAgain + ); + assert_eq!(AccountStatus::Destroyed.on_selfdestructed(), AccountStatus::DestroyedAgain); + + assert_eq!(AccountStatus::Loaded.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::LoadedEmptyEIP161.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::InMemoryChange.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::Changed.on_selfdestructed(), AccountStatus::Destroyed); + } + + #[test] + fn test_transition() { + let mut status = AccountStatus::Destroyed; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedChanged; + status.transition(AccountStatus::InMemoryChange); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedAgain; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::InMemoryChange; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::InMemoryChange; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::Loaded; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::Changed); + + let mut status = AccountStatus::LoadedNotExisting; + status.transition(AccountStatus::InMemoryChange); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::LoadedEmptyEIP161; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::Loaded); + + let mut status = AccountStatus::Destroyed; + status.transition(AccountStatus::DestroyedChanged); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedAgain; + status.transition(AccountStatus::Destroyed); + assert_eq!(status, AccountStatus::Destroyed); + + let mut status = AccountStatus::Loaded; + status.transition(AccountStatus::Destroyed); + assert_eq!(status, AccountStatus::Destroyed); + + let mut status = AccountStatus::Changed; + status.transition(AccountStatus::DestroyedAgain); + assert_eq!(status, AccountStatus::DestroyedAgain); + } +} diff --git a/crates/evm2/src/block/bundle_state/bundle_account.rs b/crates/evm2/src/block/bundle_state/bundle_account.rs new file mode 100644 index 000000000..ceb12a555 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/bundle_account.rs @@ -0,0 +1,386 @@ +use super::{ + AccountRevert, AccountStatus, RevertToSlot, StorageSlot, StorageWithOriginalValues, + TransitionAccount, reverts::AccountInfoRevert, +}; +use crate::evm::AccountInfo; +use alloy_primitives::{ + U256, + map::{HashMap, U256Map}, +}; + +/// Account information focused on creating of database changesets +/// and Reverts. +/// +/// Status is needed as to know from what state we are applying the TransitionAccount. +/// +/// Original account info is needed to know if there was a change. +/// +/// Same thing for storage with original value. +/// +/// On selfdestruct storage original value is ignored. +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BundleAccount { + /// Current account information. + pub info: Option, + /// Original account information before modifications. + pub original_info: Option, + /// Contains both original and present state. + /// When extracting changeset we compare if original value is different from present value. + /// If it is different we add it to changeset. + /// + /// If Account was destroyed we ignore original value and compare present state with + /// U256::ZERO. + pub storage: StorageWithOriginalValues, + /// Account status. + pub status: AccountStatus, +} + +impl BundleAccount { + /// Create new BundleAccount. + pub const fn new( + original_info: Option, + present_info: Option, + storage: StorageWithOriginalValues, + status: AccountStatus, + ) -> Self { + Self { info: present_info, original_info, storage, status } + } + + /// The approximate size of changes needed to store this account. + /// + /// `1 + storage_len` + pub fn size_hint(&self) -> usize { + 1 + self.storage.len() + } + + /// Return storage slot if it exists. + /// + /// In case we know that account is newly created or destroyed, return + /// `Some(U256::ZERO)` + pub fn storage_slot(&self, slot: U256) -> Option { + let slot = self.storage.get(&slot).map(|s| s.present_value); + if slot.is_some() { + slot + } else if self.status.is_storage_known() { + Some(U256::ZERO) + } else { + None + } + } + + /// Fetch account info if it exists. + pub fn account_info(&self) -> Option { + self.info.clone() + } + + /// Was this account destroyed. + pub const fn was_destroyed(&self) -> bool { + self.status.was_destroyed() + } + + /// Return true of account info was changed. + pub fn is_info_changed(&self) -> bool { + self.info != self.original_info + } + + /// Return true if contract was changed + pub fn is_contract_changed(&self) -> bool { + self.info.as_ref().map(|a| a.code_hash) != self.original_info.as_ref().map(|a| a.code_hash) + } + + /// Revert account to previous state and return true if account can be removed. + pub fn revert(&mut self, revert: AccountRevert) -> bool { + self.status = revert.previous_status; + + match revert.account { + AccountInfoRevert::DoNothing => (), + AccountInfoRevert::DeleteIt => { + self.info = None; + if self.original_info.is_none() { + self.storage = HashMap::default(); + return true; + } else { + // Set all storage to zero but preserve original values. + self.storage.iter_mut().for_each(|(_, v)| { + v.present_value = U256::ZERO; + }); + return false; + } + } + AccountInfoRevert::RevertTo(info) => self.info = Some(info), + }; + // Revert storage + for (key, slot) in revert.storage { + match slot { + RevertToSlot::Some(value) => { + // Don't overwrite original values if present + // if storage is not present set original value as current value. + self.storage + .entry(key) + .or_insert_with(|| StorageSlot::new(value)) + .present_value = value; + } + RevertToSlot::Destroyed => { + // If it was destroyed this means that storage was created and we need to remove + // it. + self.storage.remove(&key); + } + } + } + false + } + + /// Update to new state and generate AccountRevert that if applied to new state will + /// revert it to previous state. + /// + /// If no revert is present, update is noop. + pub fn update_and_create_revert( + &mut self, + transition: TransitionAccount, + ) -> Option { + let updated_info = transition.info; + let updated_storage = transition.storage; + let updated_status = transition.status; + + // The helper that extends this storage but preserves original value. + let extend_storage = + |this_storage: &mut StorageWithOriginalValues, + storage_update: StorageWithOriginalValues| { + this_storage.reserve(storage_update.len()); + for (key, value) in storage_update { + this_storage.entry(key).or_insert(value).present_value = value.present_value; + } + }; + + let previous_storage_from_update = + |updated_storage: &StorageWithOriginalValues| -> U256Map { + updated_storage + .iter() + .filter(|s| s.1.is_changed()) + .map(|(key, value)| { + (*key, RevertToSlot::Some(value.previous_or_original_value)) + }) + .collect() + }; + + // Needed for some reverts. + let info_revert = if self.info != updated_info { + AccountInfoRevert::RevertTo(self.info.clone().unwrap_or_default()) + } else { + AccountInfoRevert::DoNothing + }; + + let account_revert = match updated_status { + AccountStatus::Changed => { + let previous_storage = previous_storage_from_update(&updated_storage); + match self.status { + AccountStatus::Changed | AccountStatus::Loaded => { + // Extend the storage. original values is not used inside bundle. + extend_storage(&mut self.storage, updated_storage); + } + AccountStatus::LoadedEmptyEIP161 => { + // Do nothing. + // Only change that can happen from LoadedEmpty to Changed is if balance + // is send to account. So we are only checking account change here. + } + _ => unreachable!("Invalid state transfer to Changed from {self:?}"), + }; + let previous_status = self.status; + self.status = AccountStatus::Changed; + self.info = updated_info; + Some(AccountRevert { + account: info_revert, + storage: previous_storage, + previous_status, + wipe_storage: false, + }) + } + AccountStatus::InMemoryChange => { + let previous_storage = previous_storage_from_update(&updated_storage); + let in_memory_info_revert = match self.status { + AccountStatus::Loaded | AccountStatus::InMemoryChange => { + // From loaded (Or LoadedEmpty) to InMemoryChange can happen if there is + // balance change or new created account but Loaded + // didn't have contract. + extend_storage(&mut self.storage, updated_storage); + info_revert + } + AccountStatus::LoadedEmptyEIP161 => { + self.storage = updated_storage; + info_revert + } + AccountStatus::LoadedNotExisting => { + self.storage = updated_storage; + AccountInfoRevert::DeleteIt + } + _ => unreachable!("Invalid change to InMemoryChange from {self:?}"), + }; + let previous_status = self.status; + self.status = AccountStatus::InMemoryChange; + self.info = updated_info; + Some(AccountRevert { + account: in_memory_info_revert, + storage: previous_storage, + previous_status, + wipe_storage: false, + }) + } + AccountStatus::Loaded + | AccountStatus::LoadedNotExisting + | AccountStatus::LoadedEmptyEIP161 => { + // No changeset, maybe just update data + // Do nothing for now. + None + } + AccountStatus::Destroyed => { + // Clear this storage and move it to the Revert. + let this_storage = core::mem::take(&mut self.storage); + let ret = match self.status { + AccountStatus::InMemoryChange + | AccountStatus::Changed + | AccountStatus::Loaded + | AccountStatus::LoadedEmptyEIP161 => Some(AccountRevert::new_selfdestructed( + self.status, + info_revert, + this_storage, + )), + AccountStatus::LoadedNotExisting => { + // Do nothing as we have LoadedNotExisting -> Destroyed (It is noop) + None + } + _ => unreachable!( + "Invalid transition to Destroyed account from: {self:?} to {updated_info:?} {updated_status:?}" + ), + }; + + if ret.is_some() { + self.status = AccountStatus::Destroyed; + self.info = None; + } + + // Set present to destroyed. + ret + } + AccountStatus::DestroyedChanged => { + // Previous block created account or changed. + // (It was destroyed on previous block or one before). + + // Check common pre destroy paths. + // If common path is there it will drain the storage. + if let Some(revert_state) = AccountRevert::new_selfdestructed_from_bundle( + info_revert.clone(), + self, + &updated_storage, + ) { + // Set to destroyed and revert state. + self.status = AccountStatus::DestroyedChanged; + self.info = updated_info; + self.storage = updated_storage; + + Some(revert_state) + } else { + let ret = match self.status { + AccountStatus::Destroyed | AccountStatus::LoadedNotExisting => { + // From destroyed state new account is made + Some(AccountRevert { + account: AccountInfoRevert::DeleteIt, + storage: previous_storage_from_update(&updated_storage), + previous_status: self.status, + wipe_storage: false, + }) + } + AccountStatus::DestroyedChanged => { + // Account was destroyed in this transition. So we should clear present + // storage and insert it inside revert. + + let previous_storage = if transition.storage_was_destroyed { + let mut storage = core::mem::take(&mut self.storage) + .into_iter() + .map(|t| (t.0, RevertToSlot::Some(t.1.present_value))) + .collect::>(); + for key in updated_storage.keys() { + // As it is not existing inside Destroyed storage this means + // that previous values must be zero + storage.entry(*key).or_insert(RevertToSlot::Destroyed); + } + storage + } else { + previous_storage_from_update(&updated_storage) + }; + + Some(AccountRevert { + account: info_revert, + storage: previous_storage, + previous_status: AccountStatus::DestroyedChanged, + wipe_storage: false, + }) + } + AccountStatus::DestroyedAgain => { + Some(AccountRevert::new_selfdestructed_again( + // Destroyed again will set empty account. + AccountStatus::DestroyedAgain, + AccountInfoRevert::DeleteIt, + HashMap::default(), + updated_storage.clone(), + )) + } + _ => unreachable!("Invalid state transfer to DestroyedNew from {self:?}"), + }; + self.status = AccountStatus::DestroyedChanged; + self.info = updated_info; + // Extends current storage. + extend_storage(&mut self.storage, updated_storage); + + ret + } + } + AccountStatus::DestroyedAgain => { + // Previous block created account + // (It was destroyed on previous block or one before). + + // Check common pre destroy paths. + // This will drain the storage if it is common transition. + let ret = if let Some(revert_state) = AccountRevert::new_selfdestructed_from_bundle( + info_revert, + self, + &HashMap::default(), + ) { + Some(revert_state) + } else { + match self.status { + AccountStatus::Destroyed + | AccountStatus::DestroyedAgain + | AccountStatus::LoadedNotExisting => { + // From destroyed to destroyed again is noop + // + // From DestroyedAgain to DestroyedAgain is noop + // + // From LoadedNotExisting to DestroyedAgain is noop + // as account is destroyed again + None + } + AccountStatus::DestroyedChanged => { + // From destroyed changed to destroyed again. + Some(AccountRevert::new_selfdestructed_again( + // Destroyed again will set empty account. + AccountStatus::DestroyedChanged, + AccountInfoRevert::RevertTo(self.info.clone().unwrap_or_default()), + core::mem::take(&mut self.storage), + HashMap::default(), + )) + } + _ => unreachable!("Invalid state to DestroyedAgain from {self:?}"), + } + }; + // Set to destroyed and revert state. + self.status = AccountStatus::DestroyedAgain; + self.info = None; + self.storage.clear(); + ret + } + }; + + account_revert.filter(|acc| !acc.is_empty()) + } +} diff --git a/crates/evm2/src/block/bundle_state/bundle_state.rs b/crates/evm2/src/block/bundle_state/bundle_state.rs new file mode 100644 index 000000000..0607575cc --- /dev/null +++ b/crates/evm2/src/block/bundle_state/bundle_state.rs @@ -0,0 +1,1397 @@ +use super::{ + AccountRevert, AccountStatus, BundleAccount, PlainStateReverts, RevertToSlot, StorageSlot, + TransitionState, + changes::{PlainStorageChangeset, StateChangeset}, + reverts::{AccountInfoRevert, Reverts}, +}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::{ + collections::{BTreeMap, BTreeSet}, + vec::Vec, +}; +use alloy_primitives::{ + Address, B256, KECCAK256_EMPTY, U256, + map::{AddressMap, AddressSet, B256Map, HashMap, U256Map, hash_map::Entry}, +}; +use core::{mem, ops::RangeInclusive}; + +/// This builder is used to help to facilitate the initialization of `BundleState` struct +#[derive(Debug)] +pub struct BundleBuilder { + states: AddressSet, + state_original: AddressMap, + state_present: AddressMap, + state_storage: AddressMap>, + + reverts: BTreeSet<(u64, Address)>, + revert_range: RangeInclusive, + revert_account: HashMap<(u64, Address), Option>>, + revert_storage: HashMap<(u64, Address), Vec<(U256, U256)>>, + + contracts: B256Map, +} + +/// Option for [`BundleState`] when converting it to the plain state. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum OriginalValuesKnown { + /// Checks changed with original values that [`BundleState`] has. + /// + /// If we don't expect parent blocks to be committed or unwinded from database, this option + /// should be used. + Yes, + /// Doesn't check original values, see the implementation of [`BundleState::to_plain_state`] + /// for more info. + /// + /// If the Bundle can be split or extended, we would not be sure about original values, in that + /// case this option should be used. + No, +} +impl OriginalValuesKnown { + /// Original value is not known for sure. + pub const fn is_not_known(&self) -> bool { + matches!(self, Self::No) + } +} + +impl Default for BundleBuilder { + fn default() -> Self { + BundleBuilder { + states: AddressSet::default(), + state_original: AddressMap::default(), + state_present: AddressMap::default(), + state_storage: AddressMap::default(), + reverts: BTreeSet::new(), + revert_range: 0..=0, + revert_account: HashMap::default(), + revert_storage: HashMap::default(), + contracts: B256Map::default(), + } + } +} + +impl BundleBuilder { + /// Creates builder instance. + /// + /// `revert_range` indicates the size of BundleState `reverts` field. + pub fn new(revert_range: RangeInclusive) -> Self { + BundleBuilder { revert_range, ..Default::default() } + } + + /// Applies a transformation to the builder. + pub fn apply(self, f: F) -> Self + where + F: FnOnce(Self) -> Self, + { + f(self) + } + + /// Applies a mutable transformation to the builder. + pub fn apply_mut(&mut self, f: F) -> &mut Self + where + F: FnOnce(&mut Self), + { + f(self); + self + } + + /// Collects address info of BundleState state. + pub fn state_address(mut self, address: Address) -> Self { + self.set_state_address(address); + self + } + + /// Collects account info of BundleState state. + pub fn state_original_account_info(mut self, address: Address, original: AccountInfo) -> Self { + self.set_state_original_account_info(address, original); + self + } + + /// Collects account info of BundleState state. + pub fn state_present_account_info(mut self, address: Address, present: AccountInfo) -> Self { + self.set_state_present_account_info(address, present); + self + } + + /// Collects storage info of BundleState state. + pub fn state_storage(mut self, address: Address, storage: U256Map<(U256, U256)>) -> Self { + self.set_state_storage(address, storage); + self + } + + /// Collects address info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_address(mut self, block_number: u64, address: Address) -> Self { + self.set_revert_address(block_number, address); + self + } + + /// Collects account info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_account_info( + mut self, + block_number: u64, + address: Address, + account: Option>, + ) -> Self { + self.set_revert_account_info(block_number, address, account); + self + } + + /// Collects storage info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_storage( + mut self, + block_number: u64, + address: Address, + storage: Vec<(U256, U256)>, + ) -> Self { + self.set_revert_storage(block_number, address, storage); + self + } + + /// Collects contracts info. + pub fn contract(mut self, address: B256, bytecode: Bytecode) -> Self { + self.set_contract(address, bytecode); + self + } + + /// Sets address info of BundleState state. + pub fn set_state_address(&mut self, address: Address) -> &mut Self { + self.states.insert(address); + self + } + + /// Sets original account info of BundleState state. + pub fn set_state_original_account_info( + &mut self, + address: Address, + original: AccountInfo, + ) -> &mut Self { + self.states.insert(address); + self.state_original.insert(address, original); + self + } + + /// Sets present account info of BundleState state. + pub fn set_state_present_account_info( + &mut self, + address: Address, + present: AccountInfo, + ) -> &mut Self { + self.states.insert(address); + self.state_present.insert(address, present); + self + } + + /// Sets storage info of BundleState state. + pub fn set_state_storage( + &mut self, + address: Address, + storage: U256Map<(U256, U256)>, + ) -> &mut Self { + self.states.insert(address); + self.state_storage.insert(address, storage); + self + } + + /// Sets address info of BundleState reverts. + pub fn set_revert_address(&mut self, block_number: u64, address: Address) -> &mut Self { + self.reverts.insert((block_number, address)); + self + } + + /// Sets account info of BundleState reverts. + pub fn set_revert_account_info( + &mut self, + block_number: u64, + address: Address, + account: Option>, + ) -> &mut Self { + self.reverts.insert((block_number, address)); + self.revert_account.insert((block_number, address), account); + self + } + + /// Sets storage info of BundleState reverts. + pub fn set_revert_storage( + &mut self, + block_number: u64, + address: Address, + storage: Vec<(U256, U256)>, + ) -> &mut Self { + self.reverts.insert((block_number, address)); + self.revert_storage.insert((block_number, address), storage); + self + } + + /// Sets contracts info. + pub fn set_contract(&mut self, address: B256, bytecode: Bytecode) -> &mut Self { + self.contracts.insert(address, bytecode); + self + } + + /// Creates `BundleState` instance based on collected information. + pub fn build(mut self) -> BundleState { + let mut state_size = 0; + let state = self + .states + .into_iter() + .map(|address| { + let storage = self + .state_storage + .remove(&address) + .map(|s| { + s.into_iter() + .map(|(k, (o_val, p_val))| (k, StorageSlot::new_changed(o_val, p_val))) + .collect() + }) + .unwrap_or_default(); + let bundle_account = BundleAccount::new( + self.state_original.remove(&address), + self.state_present.remove(&address), + storage, + AccountStatus::Changed, + ); + state_size += bundle_account.size_hint(); + (address, bundle_account) + }) + .collect(); + + let mut reverts_size = 0; + let mut reverts_map = BTreeMap::new(); + for block_number in self.revert_range { + reverts_map.insert(block_number, Vec::new()); + } + self.reverts.into_iter().for_each(|(block_number, address)| { + let account = + match self.revert_account.remove(&(block_number, address)).unwrap_or_default() { + Some(Some(account)) => AccountInfoRevert::RevertTo(account), + Some(None) => AccountInfoRevert::DeleteIt, + None => AccountInfoRevert::DoNothing, + }; + let storage = self + .revert_storage + .remove(&(block_number, address)) + .map(|s| s.into_iter().map(|(k, v)| (k, RevertToSlot::Some(v))).collect()) + .unwrap_or_default(); + let account_revert = AccountRevert { + account, + storage, + previous_status: AccountStatus::Changed, + wipe_storage: false, + }; + + if let Some(vec) = reverts_map.get_mut(&block_number) { + reverts_size += account_revert.size_hint(); + vec.push((address, account_revert)); + } + }); + + BundleState { + state, + contracts: self.contracts, + reverts: Reverts::new(reverts_map.into_values().collect()), + state_size, + reverts_size, + } + } + + /// Getter for `states` field + pub const fn get_states(&self) -> &AddressSet { + &self.states + } + + /// Mutable getter for `states` field + pub const fn get_states_mut(&mut self) -> &mut AddressSet { + &mut self.states + } + + /// Mutable getter for `state_original` field + pub const fn get_state_original_mut(&mut self) -> &mut AddressMap { + &mut self.state_original + } + + /// Mutable getter for `state_present` field + pub const fn get_state_present_mut(&mut self) -> &mut AddressMap { + &mut self.state_present + } + + /// Mutable getter for `state_storage` field + pub const fn get_state_storage_mut(&mut self) -> &mut AddressMap> { + &mut self.state_storage + } + + /// Mutable getter for `reverts` field + pub const fn get_reverts_mut(&mut self) -> &mut BTreeSet<(u64, Address)> { + &mut self.reverts + } + + /// Mutable getter for `revert_range` field + pub const fn get_revert_range_mut(&mut self) -> &mut RangeInclusive { + &mut self.revert_range + } + + /// Mutable getter for `revert_account` field + pub const fn get_revert_account_mut( + &mut self, + ) -> &mut HashMap<(u64, Address), Option>> { + &mut self.revert_account + } + + /// Mutable getter for `revert_storage` field + pub const fn get_revert_storage_mut( + &mut self, + ) -> &mut HashMap<(u64, Address), Vec<(U256, U256)>> { + &mut self.revert_storage + } + + /// Mutable getter for `contracts` field + pub const fn get_contracts_mut(&mut self) -> &mut B256Map { + &mut self.contracts + } +} + +/// Bundle retention policy for applying substate to the bundle. +#[derive(Debug)] +pub enum BundleRetention { + /// Only plain state is updated. + PlainState, + /// Both, plain state and reverts, are retained + Reverts, +} + +impl BundleRetention { + /// Returns `true` if reverts should be retained. + pub const fn includes_reverts(&self) -> bool { + matches!(self, Self::Reverts) + } +} + +/// Bundle state contain only values that got changed +/// +/// For every account it contains both original and present state. +/// +/// This is needed to decide if there were any changes to the account. +/// +/// Changes are applied and reverts are created when TransitionState is applied to BundleState. +/// +/// And can be used to revert BundleState to the state before transition. +#[derive(Default, Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BundleState { + /// Account state + pub state: AddressMap, + /// All created contracts in this block. + pub contracts: B256Map, + /// Changes to revert + /// + /// **Note**: Inside vector is *not* sorted by address. + /// + /// But it is unique by address. + pub reverts: Reverts, + /// The size of the plain state in the bundle state + pub state_size: usize, + /// The size of reverts in the bundle state + pub reverts_size: usize, +} + +impl BundleState { + /// Returns builder instance for further manipulation. + pub fn builder(revert_range: RangeInclusive) -> BundleBuilder { + BundleBuilder::new(revert_range) + } + + /// Creates it with new and old values of both Storage and AccountInfo. + pub fn new( + state: impl IntoIterator< + Item = (Address, Option, Option, HashMap), + >, + reverts: impl IntoIterator< + Item = impl IntoIterator< + Item = ( + Address, + Option>, + impl IntoIterator, + ), + >, + >, + contracts: impl IntoIterator, + ) -> Self { + // Create state from iterator. + let mut state_size = 0; + let state = state + .into_iter() + .map(|(address, original, present, storage)| { + let account = BundleAccount::new( + original, + present, + storage + .into_iter() + .map(|(k, (o_val, p_val))| (k, StorageSlot::new_changed(o_val, p_val))) + .collect(), + AccountStatus::Changed, + ); + state_size += account.size_hint(); + (address, account) + }) + .collect(); + + // Create reverts from iterator. + let mut reverts_size = 0; + let reverts = reverts + .into_iter() + .map(|block_reverts| { + block_reverts + .into_iter() + .map(|(address, account, storage)| { + let account = match account { + Some(Some(account)) => AccountInfoRevert::RevertTo(account), + Some(None) => AccountInfoRevert::DeleteIt, + None => AccountInfoRevert::DoNothing, + }; + let revert = AccountRevert { + account, + storage: storage + .into_iter() + .map(|(k, v)| (k, RevertToSlot::Some(v))) + .collect(), + previous_status: AccountStatus::Changed, + wipe_storage: false, + }; + reverts_size += revert.size_hint(); + (address, revert) + }) + .collect::>() + }) + .collect::>(); + + Self { + state, + contracts: contracts.into_iter().collect(), + reverts: Reverts::new(reverts), + state_size, + reverts_size, + } + } + + /// Returns the approximate size of changes in the bundle state. + /// + /// The estimation is not precise, because the information about the number of + /// destroyed entries that need to be removed is not accessible to the bundle state. + pub fn size_hint(&self) -> usize { + self.state_size + self.reverts_size + self.contracts.len() + } + + /// Returns reference to the state. + pub const fn state(&self) -> &AddressMap { + &self.state + } + + /// Checks whether bundle state is empty. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Return number of changed accounts. + pub fn len(&self) -> usize { + self.state.len() + } + + /// Gets account from state. + pub fn account(&self, address: &Address) -> Option<&BundleAccount> { + self.state.get(address) + } + + /// Gets bytecode from state. + pub fn bytecode(&self, hash: &B256) -> Option { + self.contracts.get(hash).cloned() + } + + /// Gets storage value from state. + /// + /// Returns `None` if the account is not present in the bundle state or the slot is not known. + pub fn storage(&self, address: &Address, storage_key: U256) -> Option { + self.account(address).and_then(|account| account.storage_slot(storage_key)) + } + + /// Consumes [`TransitionState`] by applying the changes and creating the + /// reverts. + /// + /// If [`BundleRetention::includes_reverts`] is `true`, then the reverts will + /// be retained. + pub fn apply_transitions_and_create_reverts( + &mut self, + transitions: TransitionState, + retention: BundleRetention, + ) { + let include_reverts = retention.includes_reverts(); + // Pessimistically pre-allocate assuming _all_ accounts changed. + let reverts_capacity = if include_reverts { transitions.transitions.len() } else { 0 }; + let mut reverts = Vec::with_capacity(reverts_capacity); + + self.state.reserve(transitions.transitions.len()); + for (address, transition) in transitions.transitions.into_iter() { + // Add new contract if it was created/changed. + if let Some((hash, new_bytecode)) = transition.has_new_contract() { + self.contracts.insert(hash, new_bytecode.clone()); + } + // Update state and create revert. + let revert = match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let entry = entry.get_mut(); + self.state_size -= entry.size_hint(); + // Update and create revert if it is present + let revert = entry.update_and_create_revert(transition); + // Update the state size + self.state_size += entry.size_hint(); + revert + } + Entry::Vacant(entry) => { + // Make revert from transition account + let present_bundle = transition.present_bundle_account(); + let revert = transition.create_revert(); + if revert.is_some() { + self.state_size += present_bundle.size_hint(); + entry.insert(present_bundle); + } + revert + } + }; + + // Append revert if present. + if let Some(revert) = revert.filter(|_| include_reverts) { + self.reverts_size += revert.size_hint(); + reverts.push((address, revert)); + } + } + + self.reverts.push(reverts); + } + + /// Generate a [`StateChangeset`] from the bundle state without consuming + /// it. + pub fn to_plain_state(&self, is_value_known: OriginalValuesKnown) -> StateChangeset { + // Pessimistically pre-allocate assuming _all_ accounts changed. + let state_len = self.state.len(); + let mut accounts = Vec::with_capacity(state_len); + let mut storage = Vec::with_capacity(state_len); + + for (address, account) in &self.state { + // Append account info if it is changed. + let was_destroyed = account.was_destroyed(); + if is_value_known.is_not_known() || account.is_info_changed() { + let info = + account.info.as_ref().map(|info| AccountInfo { code: None, ..info.clone() }); + accounts.push((*address, info)); + } + + // Append storage changes + + // Note: Assumption is that revert is going to remove whole plain storage from + // database so we can check if plain state was wiped or not. + let mut account_storage_changed = Vec::with_capacity(account.storage.len()); + + for (&key, &slot) in account.storage.iter() { + // If storage was destroyed that means that storage was wiped. + // In that case we need to check if present storage value is different then ZERO. + let destroyed_and_not_zero = was_destroyed && !slot.present_value.is_zero(); + + // If account is not destroyed check if original values was changed, + // so we can update it. + let not_destroyed_and_changed = !was_destroyed && slot.is_changed(); + + if is_value_known.is_not_known() + || destroyed_and_not_zero + || not_destroyed_and_changed + { + account_storage_changed.push((key, slot.present_value)); + } + } + + if !account_storage_changed.is_empty() || was_destroyed { + // Append storage changes to account. + storage.push(PlainStorageChangeset { + address: *address, + wipe_storage: was_destroyed, + storage: account_storage_changed, + }); + } + } + + let contracts = self + .contracts + .iter() + // Remove empty bytecodes + .filter(|(b, _)| **b != KECCAK256_EMPTY) + .map(|(b, code)| (*b, code.clone())) + .collect::>(); + StateChangeset { accounts, storage, contracts } + } + + /// Generates a [`StateChangeset`] and [`PlainStateReverts`] from the bundle + /// state. + pub fn to_plain_state_and_reverts( + &self, + is_value_known: OriginalValuesKnown, + ) -> (StateChangeset, PlainStateReverts) { + (self.to_plain_state(is_value_known), self.reverts.to_plain_state_reverts()) + } + + /// Consumes the bundle state and split it into a [`StateChangeset`] and a + /// [`PlainStateReverts`]. + #[deprecated = "Use `to_plain_state_and_reverts` instead"] + pub fn into_plain_state_and_reverts( + self, + is_value_known: OriginalValuesKnown, + ) -> (StateChangeset, PlainStateReverts) { + self.to_plain_state_and_reverts(is_value_known) + } + + /// Extends the bundle with other state. + /// + /// Updates the `other` state only if `other` is not flagged as destroyed. + pub fn extend_state(&mut self, other_state: AddressMap) { + self.state.reserve(other_state.len()); + for (address, other_account) in other_state { + match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let this = entry.get_mut(); + self.state_size -= this.size_hint(); + + // If other was destroyed. replace `this` storage with + // the `other one. + if other_account.was_destroyed() { + this.storage = other_account.storage; + } else { + // Otherwise extend this storage with other + this.storage.reserve(other_account.storage.len()); + for (key, storage_slot) in other_account.storage { + // Update present value or insert storage slot. + this.storage.entry(key).or_insert(storage_slot).present_value = + storage_slot.present_value; + } + } + this.info = other_account.info; + this.status.transition(other_account.status); + + // Update the state size + self.state_size += this.size_hint(); + } + Entry::Vacant(entry) => { + // Just insert if empty + self.state_size += other_account.size_hint(); + entry.insert(other_account); + } + } + } + } + /// Extends the state with state that is build on top of it. + /// + /// If storage was wiped in `other` state, copy `this` plain state + /// and put it inside `other` revert (if there is no duplicates of course). + /// + /// If `this` and `other` accounts were both destroyed invalidate second + /// wipe flag (from `other`). + /// + /// As wiping from database should be done only once + /// and we already transferred all potentially missing storages to the `other` revert. + pub fn extend(&mut self, mut other: Self) { + // Iterate over reverts and if its storage is wiped try to add previous bundle + // state as there is potential missing slots. + let mut seen = AddressSet::default(); + for (address, revert) in other.reverts.iter_mut().flatten() { + // `other` was built on a fresh bundle, so its first revert for an account records the + // account as freshly loaded. Revert to the status this bundle accumulated instead. + // Later reverts record statuses local to `other`, so they combine with it. + if let Some(this_account) = self.state.get(address) { + if seen.insert(*address) { + revert.previous_status = this_account.status; + } else { + let mut status = this_account.status; + status.transition(revert.previous_status); + revert.previous_status = status; + } + } + + if revert.wipe_storage { + // If there is wipe storage in `other` revert + // we need to move storage from present state. + if let Some(this_account) = self.state.get_mut(address) { + // As this account was destroyed inside `other` bundle. + // We are fine to wipe/drain this storage and put it inside revert. + // A `Destroyed` slot in `other` means the value before the wipe, which is + // this bundle's present value, not the database value. + for (key, value) in this_account.storage.drain() { + let previous = RevertToSlot::Some(value.present_value); + revert + .storage + .entry(key) + .and_modify(|slot| { + if *slot == RevertToSlot::Destroyed { + *slot = previous; + } + }) + .or_insert(previous); + } + + // Nullify `other` wipe as primary database wipe is done in `this`. + if this_account.was_destroyed() { + revert.wipe_storage = false; + } + } + } + + // Increment reverts size for each of the updated reverts. + self.reverts_size += revert.size_hint(); + } + // Extension of state + self.extend_state(other.state); + // Contract can be just extended, when counter is introduced we will take into account that. + self.contracts.extend(other.contracts); + // Reverts can be just extended + self.reverts.extend(other.reverts); + } + + /// Takes first N raw reverts from the [`BundleState`]. + pub fn take_n_reverts(&mut self, reverts_to_take: usize) -> Reverts { + // Split is done as [0, num) and [num, len]. + if reverts_to_take > self.reverts.len() { + return self.take_all_reverts(); + } + let this = self.reverts.split_off(reverts_to_take); + self.reverts_size = + this.iter().flatten().fold(0, |acc, (_, revert)| acc + revert.size_hint()); + mem::replace(&mut self.reverts, Reverts::new(this)) + } + + /// Returns and clears all reverts from [`BundleState`]. + pub fn take_all_reverts(&mut self) -> Reverts { + self.reverts_size = 0; + mem::take(&mut self.reverts) + } + + /// Reverts the state changes of the latest transition. + /// + /// **Note**: This is the same as `BundleState::revert(1)` + /// + /// Returns true if the state was reverted. + pub fn revert_latest(&mut self) -> bool { + // Revert the latest recorded state + if let Some(reverts) = self.reverts.pop() { + for (address, revert_account) in reverts.into_iter() { + self.reverts_size -= revert_account.size_hint(); + match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let account = entry.get_mut(); + self.state_size -= account.size_hint(); + if account.revert(revert_account) { + entry.remove(); + } else { + self.state_size += account.size_hint(); + } + } + Entry::Vacant(entry) => { + // Create empty account that we will revert on. + // Only place where this account is not existing is if revert is DeleteIt. + let mut account = BundleAccount::new( + None, + None, + HashMap::default(), + AccountStatus::LoadedNotExisting, + ); + if !account.revert(revert_account) { + self.state_size += account.size_hint(); + entry.insert(account); + } + } + } + } + return true; + } + + false + } + + /// Reverts the state changes by N transitions back. + /// + /// See also [`Self::revert_latest`] + pub fn revert(&mut self, mut num_transitions: usize) { + if num_transitions == 0 { + return; + } + + while self.revert_latest() { + num_transitions -= 1; + if num_transitions == 0 { + // Break the loop. + break; + } + } + } + + /// Prepends present the state with the given [`BundleState`]. + /// + /// It adds changes from the given state but does not override any existing changes. + /// + /// The given state's reverts are discarded; this bundle keeps its own. + pub fn prepend_state(&mut self, mut other: BundleState) { + let other_reverts = other.reverts.len(); + // Take this bundle + let this_bundle = mem::take(self); + // Extend other bundle with this + other.extend(this_bundle); + // Discard other's reverts, which come first + other.take_n_reverts(other_reverts); + // Swap bundles + mem::swap(self, &mut other) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::block::bundle_state::{StorageWithOriginalValues, TransitionAccount}; + use alloc::vec; + + #[test] + fn transition_states() { + // Dummy data + let address = Address::new([0x01; 20]); + let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, ..Default::default() }; + + let mut bundle_state = BundleState::default(); + + // Have transition from loaded to all other states + + let transition = TransitionAccount { + info: Some(acc1), + status: AccountStatus::InMemoryChange, + previous_info: None, + previous_status: AccountStatus::LoadedNotExisting, + storage: StorageWithOriginalValues::default(), + storage_was_destroyed: false, + }; + + // Apply first transition + bundle_state.apply_transitions_and_create_reverts( + TransitionState::single(address, transition.clone()), + BundleRetention::Reverts, + ); + } + + const fn account1() -> Address { + Address::new([0x60; 20]) + } + + const fn account2() -> Address { + Address::new([0x61; 20]) + } + + fn slot1() -> U256 { + U256::from(5) + } + + fn slot2() -> U256 { + U256::from(7) + } + + /// Tests bundle one. + fn test_bundle1() -> BundleState { + // Block changes + BundleState::new( + vec![ + ( + account1(), + None, + Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }), + HashMap::from_iter([ + (slot1(), (U256::from(0), U256::from(10))), + (slot2(), (U256::from(0), U256::from(15))), + ]), + ), + ( + account2(), + None, + Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }), + HashMap::default(), + ), + ], + vec![vec![ + (account1(), Some(None), vec![(slot1(), U256::from(0)), (slot2(), U256::from(0))]), + (account2(), Some(None), vec![]), + ]], + vec![], + ) + } + + /// Tests bundle two. + fn test_bundle2() -> BundleState { + // Block changes + BundleState::new( + vec![( + account1(), + None, + Some(AccountInfo { nonce: 3, balance: U256::from(20), ..Default::default() }), + HashMap::from_iter([(slot1(), (U256::from(0), U256::from(15)))]), + )], + vec![vec![( + account1(), + Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() })), + vec![(slot1(), U256::from(10))], + )]], + vec![], + ) + } + + /// Tests bundle three. + fn test_bundle3() -> BundleState { + BundleState::builder(0..=0) + .state_present_account_info( + account1(), + AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }, + ) + .state_storage( + account1(), + HashMap::from_iter([(slot1(), (U256::from(0), U256::from(10)))]), + ) + .state_address(account2()) + .state_present_account_info( + account2(), + AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }, + ) + .revert_address(0, account1()) + .revert_account_info(0, account1(), Some(None)) + .revert_storage(0, account1(), vec![(slot1(), U256::from(0))]) + .revert_account_info(0, account2(), Some(None)) + .build() + } + + /// Tests bundle four. + fn test_bundle4() -> BundleState { + BundleState::builder(0..=0) + .state_present_account_info( + account1(), + AccountInfo { nonce: 3, balance: U256::from(20), ..Default::default() }, + ) + .state_storage( + account1(), + HashMap::from_iter([(slot1(), (U256::from(0), U256::from(15)))]), + ) + .revert_address(0, account1()) + .revert_account_info( + 0, + account1(), + Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() })), + ) + .revert_storage(0, account1(), vec![(slot1(), U256::from(10))]) + .build() + } + + /// Per-block bundles are built on a fresh bundle, so merging them must still revert an account + /// to the status accumulated over the earlier blocks. + #[test] + fn extend_reverts_to_accumulated_status() { + let address = Address::new([0x70; 20]); + let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; + let destroy = TransitionAccount { + info: None, + status: AccountStatus::Destroyed, + previous_info: Some(info.clone()), + previous_status: AccountStatus::Loaded, + storage: HashMap::from_iter([( + slot1(), + StorageSlot::new_changed(U256::from(3), U256::ZERO), + )]), + storage_was_destroyed: true, + }; + let recreate = TransitionAccount { + info: Some(info), + status: AccountStatus::InMemoryChange, + previous_info: None, + previous_status: AccountStatus::LoadedNotExisting, + storage: HashMap::from_iter([( + slot2(), + StorageSlot::new_changed(U256::ZERO, U256::from(5)), + )]), + storage_was_destroyed: false, + }; + let block_bundle = |transition| { + let mut bundle = BundleState::default(); + bundle.apply_transitions_and_create_reverts( + TransitionState::single(address, transition), + BundleRetention::Reverts, + ); + bundle + }; + + let first = block_bundle(destroy); + let mut merged = first.clone(); + merged.extend(block_bundle(recreate.clone())); + + // A single bundle built over both blocks tracks the status across them. + let mut cumulative = first.clone(); + cumulative.apply_transitions_and_create_reverts( + TransitionState::single( + address, + TransitionAccount { + status: AccountStatus::DestroyedChanged, + previous_status: AccountStatus::Destroyed, + ..recreate + }, + ), + BundleRetention::Reverts, + ); + assert_eq!(merged.state[&address].status, AccountStatus::DestroyedChanged); + assert_eq!(merged.reverts, cumulative.reverts); + + merged.revert(1); + cumulative.revert(1); + let account = &merged.state[&address]; + assert_eq!(account.status, AccountStatus::Destroyed); + assert!(account.was_destroyed()); + assert_eq!(merged, cumulative); + } + + /// Every block of an extending bundle reverts to the status accumulated over the earlier + /// blocks, not only the first one. + #[test] + fn extend_reverts_later_blocks_to_accumulated_status() { + let address = Address::new([0x71; 20]); + let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; + let changed = AccountInfo { balance: U256::from(11), ..info.clone() }; + let transition = |previous: &AccountInfo, next: &AccountInfo, from, to| TransitionAccount { + info: Some(next.clone()), + status: to, + previous_info: Some(previous.clone()), + previous_status: from, + storage: HashMap::default(), + storage_was_destroyed: false, + }; + let block_bundle = |transitions: &[TransitionAccount]| { + let mut bundle = BundleState::default(); + for transition in transitions { + bundle.apply_transitions_and_create_reverts( + TransitionState::single(address, transition.clone()), + BundleRetention::Reverts, + ); + } + bundle + }; + let destroy = TransitionAccount { + info: None, + previous_info: Some(info.clone()), + ..transition(&info, &info, AccountStatus::Loaded, AccountStatus::Destroyed) + }; + let recreate = TransitionAccount { + previous_info: None, + ..transition( + &info, + &info, + AccountStatus::LoadedNotExisting, + AccountStatus::InMemoryChange, + ) + }; + let change = transition( + &info, + &changed, + AccountStatus::InMemoryChange, + AccountStatus::InMemoryChange, + ); + + let mut merged = block_bundle(core::slice::from_ref(&destroy)); + merged.extend(block_bundle(&[recreate.clone(), change.clone()])); + + // A single bundle built over every block tracks the status across them. + let mut cumulative = block_bundle(&[destroy]); + for (previous, transition) in + [(AccountStatus::Destroyed, recreate), (AccountStatus::DestroyedChanged, change)] + { + cumulative.apply_transitions_and_create_reverts( + TransitionState::single( + address, + TransitionAccount { + status: AccountStatus::DestroyedChanged, + previous_status: previous, + ..transition + }, + ), + BundleRetention::Reverts, + ); + } + assert_eq!(merged.reverts, cumulative.reverts); + + merged.revert(1); + cumulative.revert(1); + assert_eq!(merged.state[&address].status, AccountStatus::DestroyedChanged); + assert_eq!(merged, cumulative); + } + + fn sanity_path(bundle1: BundleState, bundle2: BundleState) { + let mut extended = bundle1.clone(); + extended.extend(bundle2.clone()); + + let mut reverted = extended.clone(); + // Revert zero does nothing. + reverted.revert(0); + assert_eq!(reverted, extended); + + // Revert by one gives us bundle one. + reverted.revert(1); + assert_eq!(reverted, bundle1); + + // Reverted by additional one gives us empty bundle. + reverted.revert(1); + assert_eq!(reverted, BundleState::default()); + + let mut reverted = extended.clone(); + + // Reverted by bigger number gives us empty bundle + reverted.revert(10); + assert_eq!(reverted, BundleState::default()); + } + + #[test] + fn extend_on_destroyed_values() { + let base_bundle1 = test_bundle1(); + let base_bundle2 = test_bundle2(); + + // test1 + // bundle1 has Destroyed + // bundle2 has Changed + // end should be DestroyedChanged. + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::Destroyed; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::DestroyedChanged); + + // test2 + // bundle1 has Changed + // bundle2 has Destroyed + // end should be Destroyed + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Destroyed; + b2.reverts[0][0].1.wipe_storage = true; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::Destroyed); + + // test2 extension + // revert of b2 should contains plain state of b1. + let mut revert1 = base_bundle2.reverts[0][0].clone(); + revert1.1.wipe_storage = true; + revert1.1.storage.insert(slot2(), RevertToSlot::Some(U256::from(15))); + + assert_eq!(b1.reverts.as_ref(), vec![base_bundle1.reverts[0].clone(), vec![revert1]],); + + // test3 + // bundle1 has InMemoryChange + // bundle2 has Change + // end should be InMemoryChange. + + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::InMemoryChange; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::InMemoryChange); + } + + #[test] + fn extend_replaces_destroyed_revert_with_previous_value() { + // bundle1 writes slot1 = 10 and slot2 = 15; bundle2 selfdestructs and re-creates + // account1, so its revert wipes storage and marks slot1 as destroyed. + let mut b1 = test_bundle1(); + let mut b2 = test_bundle2(); + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::DestroyedChanged; + let revert = &mut b2.reverts[0][0].1; + revert.wipe_storage = true; + revert.storage.insert(slot1(), RevertToSlot::Destroyed); + + b1.extend(b2); + let revert = &b1.reverts[1][0].1; + assert!(revert.wipe_storage); + assert_eq!(revert.storage.get(&slot1()), Some(&RevertToSlot::Some(U256::from(10)))); + assert_eq!(revert.storage.get(&slot2()), Some(&RevertToSlot::Some(U256::from(15)))); + } + + #[test] + fn test_sanity_path() { + sanity_path(test_bundle1(), test_bundle2()); + sanity_path(test_bundle3(), test_bundle4()); + } + + #[test] + fn test_multi_reverts_with_delete() { + let mut state = BundleBuilder::new(0..=3) + .revert_address(0, account1()) + .revert_account_info(2, account1(), Some(Some(AccountInfo::default()))) + .revert_account_info(3, account1(), Some(None)) + .build(); + + state.revert_latest(); + // State for account one was deleted + assert_eq!(state.state.get(&account1()), None); + + state.revert_latest(); + // State is set to + assert_eq!( + state.state.get(&account1()), + Some(&BundleAccount::new( + None, + Some(AccountInfo::default()), + HashMap::default(), + AccountStatus::Changed + )) + ); + } + + #[test] + fn test_revert_capacity() { + let state = BundleState::builder(0..=3) + .revert_address(0, account1()) + .revert_address(2, account2()) + .revert_account_info(0, account1(), Some(None)) + .revert_account_info(2, account2(), None) + .revert_storage(0, account1(), vec![(slot1(), U256::from(10))]) + .build(); + + assert_eq!(state.reverts.len(), 4); + assert_eq!(state.reverts[1], vec![]); + assert_eq!(state.reverts[3], vec![]); + assert_eq!(state.reverts[0].len(), 1); + assert_eq!(state.reverts[2].len(), 1); + + let (addr1, revert1) = &state.reverts[0][0]; + assert_eq!(addr1, &account1()); + assert_eq!(revert1.account, AccountInfoRevert::DeleteIt); + + let (addr2, revert2) = &state.reverts[2][0]; + assert_eq!(addr2, &account2()); + assert_eq!(revert2.account, AccountInfoRevert::DoNothing); + } + + #[test] + fn take_reverts() { + let bundle1 = test_bundle1(); + let bundle2 = test_bundle2(); + + let mut extended = bundle1.clone(); + extended.extend(bundle2.clone()); + + // Check that we have two reverts + assert_eq!(extended.reverts.len(), 2); + + // Take all by big N + let mut extended2 = extended.clone(); + assert_eq!(extended2.take_n_reverts(100), extended.reverts); + + // Take all reverts + let mut extended2 = extended.clone(); + assert_eq!(extended2.take_all_reverts(), extended.reverts); + + // Take zero revert + let taken_reverts = extended.take_n_reverts(0); + assert_eq!(taken_reverts, Reverts::default()); + assert_eq!(extended.reverts.len(), 2); + + // Take one revert + let taken_reverts = extended.take_n_reverts(1); + assert_eq!(taken_reverts, bundle1.reverts); + + // Take last revert + let taken_reverts = extended.take_n_reverts(1); + assert_eq!(taken_reverts, bundle2.reverts); + } + + #[test] + fn prepend_state() { + let address1 = account1(); + let address2 = account2(); + + let account1 = AccountInfo { nonce: 1, ..Default::default() }; + let account1_changed = AccountInfo { nonce: 1, ..Default::default() }; + let account2 = AccountInfo { nonce: 1, ..Default::default() }; + + let present_state = BundleState::builder(2..=2) + .state_present_account_info(address1, account1_changed.clone()) + .revert_account_info(2, address1, Some(None)) + .build(); + assert_eq!(present_state.reverts.len(), 1); + let present_reverts = present_state.reverts.clone(); + let previous_state = BundleState::builder(1..=1) + .state_present_account_info(address1, account1) + .state_present_account_info(address2, account2.clone()) + .revert_account_info(1, address2, Some(None)) + .build(); + assert_eq!(previous_state.reverts.len(), 1); + + let mut test = present_state; + + test.prepend_state(previous_state); + + assert_eq!(test.state.len(), 2); + // The receiver keeps its own reverts. + assert_eq!(test.reverts, present_reverts); + // Account1 is not overwritten. + assert_eq!(test.state.get(&address1).unwrap().info, Some(account1_changed)); + // Account2 got inserted + assert_eq!(test.state.get(&address2).unwrap().info, Some(account2)); + } + + #[test] + fn test_getters() { + let mut builder = BundleBuilder::new(0..=3); + + // Test get_states and get_states_mut + assert!(builder.get_states().is_empty()); + builder.get_states_mut().insert(account1()); + assert!(builder.get_states().contains(&account1())); + + // Test get_state_original_mut + assert!(builder.get_state_original_mut().is_empty()); + builder.get_state_original_mut().insert(account1(), AccountInfo::default()); + assert!(builder.get_state_original_mut().contains_key(&account1())); + + // Test get_state_present_mut + assert!(builder.get_state_present_mut().is_empty()); + builder.get_state_present_mut().insert(account1(), AccountInfo::default()); + assert!(builder.get_state_present_mut().contains_key(&account1())); + + // Test get_state_storage_mut + assert!(builder.get_state_storage_mut().is_empty()); + builder.get_state_storage_mut().insert(account1(), HashMap::default()); + assert!(builder.get_state_storage_mut().contains_key(&account1())); + + // Test get_reverts_mut + assert!(builder.get_reverts_mut().is_empty()); + builder.get_reverts_mut().insert((0, account1())); + assert!(builder.get_reverts_mut().contains(&(0, account1()))); + + // Test get_revert_range_mut + assert_eq!(builder.get_revert_range_mut().clone(), 0..=3); + + // Test get_revert_account_mut + assert!(builder.get_revert_account_mut().is_empty()); + builder.get_revert_account_mut().insert((0, account1()), Some(None)); + assert!(builder.get_revert_account_mut().contains_key(&(0, account1()))); + + // Test get_revert_storage_mut + assert!(builder.get_revert_storage_mut().is_empty()); + builder.get_revert_storage_mut().insert((0, account1()), vec![(slot1(), U256::from(0))]); + assert!(builder.get_revert_storage_mut().contains_key(&(0, account1()))); + + // Test get_contracts_mut + assert!(builder.get_contracts_mut().is_empty()); + builder.get_contracts_mut().insert(B256::default(), Bytecode::default()); + assert!(builder.get_contracts_mut().contains_key(&B256::default())); + } +} diff --git a/crates/evm2/src/block/bundle_state/changes.rs b/crates/evm2/src/block/bundle_state/changes.rs new file mode 100644 index 000000000..7c131520c --- /dev/null +++ b/crates/evm2/src/block/bundle_state/changes.rs @@ -0,0 +1,75 @@ +use super::RevertToSlot; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::vec::Vec; +use alloy_primitives::{Address, B256, U256}; + +/// `accounts`/`storages`/`contracts` for inclusion into database. +/// +/// Structure is made so it is easier to apply directly to database +/// that mostly have separate tables to store `accounts`/`storages`/`contracts` data. +/// +/// **Note**: That data is **not** sorted. Some database benefit of faster inclusion +/// and smaller footprint if data is inserted in sorted order. +#[derive(Clone, Debug, Default)] +pub struct StateChangeset { + /// Vector of **not** sorted accounts information. + pub accounts: Vec<(Address, Option)>, + /// Vector of **not** sorted storage. + pub storage: Vec, + /// Vector of contracts by bytecode hash. **not** sorted. + pub contracts: Vec<(B256, Bytecode)>, +} + +/// Plain storage changeset. +/// +/// Used to apply storage changes of plain state to the database. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct PlainStorageChangeset { + /// Address of account + pub address: Address, + /// Wipe storage + pub wipe_storage: bool, + /// Storage key value pairs + pub storage: Vec<(U256, U256)>, +} + +/// Plain Storage Revert. +/// +/// [`PlainStorageRevert`] contains old values of changed storage. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct PlainStorageRevert { + /// Address of account + pub address: Address, + /// Whether storage is wiped in this revert + /// + /// **Note**: Wiped flag is set on first known selfdestruct and would require clearing the + /// state of this storage from database (And moving it to revert). + pub wiped: bool, + /// Contains the storage key and old values of that storage + /// + /// **Note**: Reverts are **not** sorted. + pub storage_revert: Vec<(U256, RevertToSlot)>, +} + +/// Plain state reverts are used to easily store reverts into database. +/// +/// Note that accounts are assumed **not** sorted. +#[derive(Clone, Debug, Default)] +pub struct PlainStateReverts { + /// Vector of account with removed contracts bytecode. + /// + /// **Note**: If AccountInfo is None means that account needs to be removed. + pub accounts: Vec)>>, + /// Vector of storage with its address. + pub storage: Vec>, +} + +impl PlainStateReverts { + /// Constructs new [`PlainStateReverts`] with pre-allocated capacity. + pub fn with_capacity(capacity: usize) -> Self { + Self { accounts: Vec::with_capacity(capacity), storage: Vec::with_capacity(capacity) } + } +} + +/// Storage reverts +pub type StorageRevert = Vec)>>; diff --git a/crates/evm2/src/block/bundle_state/mod.rs b/crates/evm2/src/block/bundle_state/mod.rs new file mode 100644 index 000000000..3475865e5 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/mod.rs @@ -0,0 +1,45 @@ +//! Block execution changes with the reverts needed to persist and unwind them. +//! +//! Ported from the `revm-database` crate of revm (), +//! Copyright (c) 2021-2026 draganrakita, licensed under the MIT License (see `LICENSES/MIT.txt`). +//! The code is kept close to upstream, so upstream style is allowed below. + +#![allow( + missing_copy_implementations, + clippy::collapsible_if, + clippy::doc_markdown, + clippy::explicit_into_iter_loop, + clippy::explicit_iter_loop, + clippy::if_not_else, + clippy::match_same_arms, + clippy::module_inception, + clippy::redundant_clone, + clippy::redundant_else, + clippy::use_self +)] + +mod account_status; +pub use account_status::AccountStatus; + +mod bundle_account; +pub use bundle_account::BundleAccount; + +mod bundle_state; +pub use bundle_state::{BundleBuilder, BundleRetention, BundleState, OriginalValuesKnown}; + +mod changes; +pub use changes::{ + PlainStateReverts, PlainStorageChangeset, PlainStorageRevert, StateChangeset, StorageRevert, +}; + +mod plain_account; +pub use plain_account::{PlainAccount, PlainStorage, StorageSlot, StorageWithOriginalValues}; + +mod reverts; +pub use reverts::{AccountInfoRevert, AccountRevert, RevertToSlot, Reverts}; + +mod transition_account; +pub use transition_account::TransitionAccount; + +mod transition_state; +pub use transition_state::TransitionState; diff --git a/crates/evm2/src/block/bundle_state/plain_account.rs b/crates/evm2/src/block/bundle_state/plain_account.rs new file mode 100644 index 000000000..84473d22a --- /dev/null +++ b/crates/evm2/src/block/bundle_state/plain_account.rs @@ -0,0 +1,82 @@ +use crate::evm::AccountInfo; +use alloy_primitives::{ + U256, + map::{HashMap, U256Map}, +}; + +/// Plain account of StateDatabase. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PlainAccount { + /// Account information. + pub info: AccountInfo, + /// Account storage. + pub storage: PlainStorage, +} + +impl PlainAccount { + /// Creates a new empty account with the given storage. + pub fn new_empty_with_storage(storage: PlainStorage) -> Self { + Self { info: AccountInfo::default(), storage } + } + + /// Consumes the account and returns its components. + pub fn into_components(self) -> (AccountInfo, PlainStorage) { + (self.info, self.storage) + } +} + +/// This type keeps track of the current value of a storage slot. +#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct StorageSlot { + /// The value of the storage slot before it was changed. + /// + /// When the slot is first loaded, this is the original value. + /// + /// If the slot was not changed, this is equal to the present value. + pub previous_or_original_value: U256, + /// When loaded with sload present value is set to original value + pub present_value: U256, +} + +impl StorageSlot { + /// Creates a new _unchanged_ `StorageSlot` for the given value. + pub const fn new(original: U256) -> Self { + Self { previous_or_original_value: original, present_value: original } + } + + /// Creates a new _changed_ `StorageSlot`. + pub const fn new_changed(previous_or_original_value: U256, present_value: U256) -> Self { + Self { previous_or_original_value, present_value } + } + + /// Returns true if the present value differs from the original value + pub fn is_changed(&self) -> bool { + self.previous_or_original_value != self.present_value + } + + /// Returns the original value of the storage slot. + pub const fn original_value(&self) -> U256 { + self.previous_or_original_value + } + + /// Returns the current value of the storage slot. + pub const fn present_value(&self) -> U256 { + self.present_value + } +} + +/// This storage represent values that are before block changed. +/// +/// Note: Storage that we get EVM contains original values before block changed. +pub type StorageWithOriginalValues = U256Map; + +/// Simple plain storage that does not have previous value. +/// This is used for loading from database, cache and for bundle state. +pub type PlainStorage = U256Map; + +impl From for PlainAccount { + fn from(info: AccountInfo) -> Self { + Self { info, storage: HashMap::default() } + } +} diff --git a/crates/evm2/src/block/bundle_state/reverts.rs b/crates/evm2/src/block/bundle_state/reverts.rs new file mode 100644 index 000000000..418a5c8d9 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/reverts.rs @@ -0,0 +1,375 @@ +use super::{ + AccountStatus, BundleAccount, PlainStateReverts, StorageWithOriginalValues, + changes::PlainStorageRevert, +}; +use crate::evm::AccountInfo; +use alloc::vec::Vec; +use alloy_primitives::{Address, U256, map::U256Map}; +use core::{ + cmp::Ordering, + ops::{Deref, DerefMut}, +}; + +/// Contains reverts of multiple account in multiple transitions (Transitions as a block). +#[derive(Clone, Debug, Default, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Reverts(Vec>); + +impl Deref for Reverts { + type Target = Vec>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Reverts { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Reverts { + /// Creates new reverts. + pub const fn new(reverts: Vec>) -> Self { + Self(reverts) + } + + /// Sorts account inside transition by their address. + pub fn sort(&mut self) { + for revert in &mut self.0 { + revert.sort_by_key(|(address, _)| *address); + } + } + + /// Extends reverts with other reverts. + pub fn extend(&mut self, other: Reverts) { + self.0.extend(other.0); + } + + /// Generates a [`PlainStateReverts`]. + /// + /// Note that account are sorted by address. + pub fn to_plain_state_reverts(&self) -> PlainStateReverts { + let mut state_reverts = PlainStateReverts::with_capacity(self.0.len()); + for reverts in &self.0 { + // Pessimistically pre-allocate assuming _all_ accounts changed. + let mut accounts = Vec::with_capacity(reverts.len()); + let mut storage = Vec::with_capacity(reverts.len()); + for (address, revert_account) in reverts { + match &revert_account.account { + AccountInfoRevert::RevertTo(acc) => { + // Cloning is cheap, because account info has 3 small + // fields and a Bytes + accounts.push((*address, Some(acc.clone()))) + } + AccountInfoRevert::DeleteIt => accounts.push((*address, None)), + AccountInfoRevert::DoNothing => (), + } + if revert_account.wipe_storage || !revert_account.storage.is_empty() { + storage.push(PlainStorageRevert { + address: *address, + wiped: revert_account.wipe_storage, + storage_revert: revert_account + .storage + .iter() + .map(|(k, v)| (*k, *v)) + .collect::>(), + }); + } + } + state_reverts.accounts.push(accounts); + state_reverts.storage.push(storage); + } + state_reverts + } + + /// Compare two Reverts instances, ignoring the order of elements + pub fn content_eq(&self, other: &Self) -> bool { + if self.0.len() != other.0.len() { + return false; + } + + for (self_transition, other_transition) in self.0.iter().zip(other.0.iter()) { + if self_transition.len() != other_transition.len() { + return false; + } + + let mut self_transition = self_transition.clone(); + let mut other_transition = other_transition.clone(); + // Sort both transitions + self_transition.sort_by(|(addr1, revert1), (addr2, revert2)| { + addr1.cmp(addr2).then_with(|| revert1.cmp(revert2)) + }); + other_transition.sort_by(|(addr1, revert1), (addr2, revert2)| { + addr1.cmp(addr2).then_with(|| revert1.cmp(revert2)) + }); + + // Compare sorted transitions + if self_transition != other_transition { + return false; + } + } + + true + } + + /// Consume reverts and create [`PlainStateReverts`]. + /// + /// Note that account are sorted by address. + #[deprecated = "Use `to_plain_state_reverts` instead"] + pub fn into_plain_state_reverts(self) -> PlainStateReverts { + self.to_plain_state_reverts() + } +} + +impl PartialEq for Reverts { + fn eq(&self, other: &Self) -> bool { + self.content_eq(other) + } +} + +/// Assumption is that Revert can return full state from any future state to any past state. +/// +/// # Note +/// It is created when new account state is applied to old account state. +/// +/// And it is used to revert new account state to the old account state. +/// +/// [`AccountRevert`] is structured in this way as we need to save it inside database. +/// +/// And we need to be able to read it from database. +#[derive(Clone, Default, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct AccountRevert { + /// Account information revert. + pub account: AccountInfoRevert, + /// Storage slots to revert. + pub storage: U256Map, + /// Previous account status before the change. + pub previous_status: AccountStatus, + /// Whether to wipe the storage. + pub wipe_storage: bool, +} + +impl AccountRevert { + /// The approximate size of changes needed to store this account revert. + /// + /// `1 + storage_reverts_len` + pub fn size_hint(&self) -> usize { + 1 + self.storage.len() + } + + /// Very similar to new_selfdestructed but it will add additional zeros + /// ([`RevertToSlot::Destroyed`]) for the storage that are set if account is again created. + pub fn new_selfdestructed_again( + status: AccountStatus, + account: AccountInfoRevert, + mut previous_storage: StorageWithOriginalValues, + updated_storage: StorageWithOriginalValues, + ) -> Self { + // Take present storage values as the storages that we are going to revert to. + // As those values got destroyed. + let mut previous_storage: U256Map = previous_storage + .drain() + .map(|(key, value)| (key, RevertToSlot::Some(value.present_value))) + .collect(); + for (key, _) in updated_storage { + previous_storage.entry(key).or_insert(RevertToSlot::Destroyed); + } + AccountRevert { + account, + storage: previous_storage, + previous_status: status, + wipe_storage: false, + } + } + + /// Creates revert for states that were before selfdestruct. + pub fn new_selfdestructed_from_bundle( + account_info_revert: AccountInfoRevert, + bundle_account: &mut BundleAccount, + updated_storage: &StorageWithOriginalValues, + ) -> Option { + match bundle_account.status { + AccountStatus::InMemoryChange + | AccountStatus::Changed + | AccountStatus::LoadedEmptyEIP161 + | AccountStatus::Loaded => { + let mut ret = AccountRevert::new_selfdestructed_again( + bundle_account.status, + account_info_revert, + core::mem::take(&mut bundle_account.storage), + updated_storage.clone(), + ); + ret.wipe_storage = true; + Some(ret) + } + _ => None, + } + } + + /// Create new selfdestruct revert. + pub fn new_selfdestructed( + status: AccountStatus, + account: AccountInfoRevert, + mut storage: StorageWithOriginalValues, + ) -> Self { + // Zero all present storage values and save present values to AccountRevert. + let previous_storage = storage + .iter_mut() + .map(|(key, value)| { + // Take previous value and set ZERO as storage got destroyed. + (*key, RevertToSlot::Some(value.present_value)) + }) + .collect(); + + Self { account, storage: previous_storage, previous_status: status, wipe_storage: true } + } + + /// Returns `true` if there is nothing to revert, + /// by checking that: + /// * both account info and storage have been left untouched + /// * we don't need to wipe storage + pub fn is_empty(&self) -> bool { + self.account == AccountInfoRevert::DoNothing + && self.storage.is_empty() + && !self.wipe_storage + } +} + +/// Implements partial ordering for AccountRevert +impl PartialOrd for AccountRevert { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +/// Implements total ordering for AccountRevert +impl Ord for AccountRevert { + fn cmp(&self, other: &Self) -> Ordering { + // First compare accounts + if let Some(ord) = self.account.partial_cmp(&other.account) { + if ord != Ordering::Equal { + return ord; + } + } + + // Convert HashMaps to sorted vectors for comparison + let mut self_storage: Vec<_> = self.storage.iter().collect(); + let mut other_storage: Vec<_> = other.storage.iter().collect(); + + // Sort by key and then by value + self_storage.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then_with(|| v1.cmp(v2))); + other_storage.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then_with(|| v1.cmp(v2))); + + // Compare each element + for (self_entry, other_entry) in self_storage.iter().zip(other_storage.iter()) { + let key_ord = self_entry.0.cmp(other_entry.0); + if key_ord != Ordering::Equal { + return key_ord; + } + let value_ord = self_entry.1.cmp(other_entry.1); + if value_ord != Ordering::Equal { + return value_ord; + } + } + + // If one vector is longer than the other, or if all elements are equal + self_storage + .len() + .cmp(&other_storage.len()) + .then_with(|| self.previous_status.cmp(&other.previous_status)) + .then_with(|| self.wipe_storage.cmp(&other.wipe_storage)) + } +} + +/// Depending on previous state of account info this +/// will tell us what to do on revert. +#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum AccountInfoRevert { + #[default] + /// Nothing changed + DoNothing, + /// Account was created and on revert we need to remove it with all storage. + DeleteIt, + /// Account was changed and on revert we need to put old state. + RevertTo(AccountInfo), +} + +impl PartialOrd for AccountInfoRevert { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +/// Orders by variant (`DoNothing`, `DeleteIt`, `RevertTo`), then by the [`AccountInfo`] fields that +/// its `Eq` compares. +impl Ord for AccountInfoRevert { + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + (Self::RevertTo(a), Self::RevertTo(b)) => { + let order = a + .balance + .cmp(&b.balance) + .then_with(|| a.nonce.cmp(&b.nonce)) + .then_with(|| a.code_hash.cmp(&b.code_hash)); + #[cfg(feature = "account-ext")] + let order = order.then_with(|| a.extension.as_ref().cmp(b.extension.as_ref())); + order + } + (Self::DoNothing, Self::DoNothing) | (Self::DeleteIt, Self::DeleteIt) => { + Ordering::Equal + } + (Self::DoNothing, _) | (Self::DeleteIt, Self::RevertTo(_)) => Ordering::Less, + (_, Self::DoNothing) | (Self::RevertTo(_), Self::DeleteIt) => Ordering::Greater, + } + } +} + +/// So storage can have multiple types: +/// * Zero, on revert remove plain state. +/// * Value, on revert set this value +/// * Destroyed, should be removed on revert but on Revert set it as zero. +/// +/// **Note**: It is completely different state if Storage is Zero or Some or if Storage was +/// Destroyed. +/// +/// Because if it is destroyed, previous values can be found in database or it can be zero. +#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum RevertToSlot { + /// Revert to this value. + Some(U256), + /// Storage was destroyed. + Destroyed, +} + +impl RevertToSlot { + /// Returns the previous value to set on revert. + pub const fn to_previous_value(self) -> U256 { + match self { + RevertToSlot::Some(value) => value, + RevertToSlot::Destroyed => U256::ZERO, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use alloc::vec; + + #[test] + fn content_eq_ignores_order_of_same_address() { + let address = Address::with_last_byte(1); + let do_nothing = AccountRevert::default(); + let delete_it = + AccountRevert { account: AccountInfoRevert::DeleteIt, ..Default::default() }; + let a = + Reverts::new(vec![vec![(address, do_nothing.clone()), (address, delete_it.clone())]]); + let b = Reverts::new(vec![vec![(address, delete_it), (address, do_nothing)]]); + assert_eq!(a, b); + } +} diff --git a/crates/evm2/src/block/bundle_state/transition_account.rs b/crates/evm2/src/block/bundle_state/transition_account.rs new file mode 100644 index 000000000..5580943fe --- /dev/null +++ b/crates/evm2/src/block/bundle_state/transition_account.rs @@ -0,0 +1,112 @@ +use super::{AccountRevert, AccountStatus, BundleAccount, StorageWithOriginalValues}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloy_primitives::{B256, U256}; + +/// Account Created when EVM state is merged to cache state. +/// And it is sent to Block state. +/// +/// It is used when block state gets merged to bundle state to +/// create needed Reverts. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct TransitionAccount { + /// Account information, if account exists. + pub info: Option, + /// Current account status. + pub status: AccountStatus, + /// Previous account info is needed for account that got initially loaded. + /// Initially loaded account are not present inside bundle and are needed + /// to generate Reverts. + pub previous_info: Option, + /// Mostly needed when previous status Loaded/LoadedEmpty. + pub previous_status: AccountStatus, + /// Storage contains both old and new account + pub storage: S, + /// If there is transition that clears the storage we should mark it here and + /// delete all storages in BundleState. This flag is needed if we have transition + /// between Destroyed states from DestroyedChanged-> DestroyedAgain-> DestroyedChanged + /// in the end transition that we would have would be `DestroyedChanged->DestroyedChanged` + /// and with only that info we couldn't decide what to do. + pub storage_was_destroyed: bool, +} + +impl TransitionAccount { + /// Create new LoadedEmpty account. + pub fn new_empty_eip161(storage: StorageWithOriginalValues) -> Self { + Self { + info: Some(AccountInfo::default()), + status: AccountStatus::InMemoryChange, + previous_info: None, + previous_status: AccountStatus::LoadedNotExisting, + storage, + storage_was_destroyed: false, + } + } + + /// Return new contract bytecode if it is changed or newly created. + pub fn has_new_contract(&self) -> Option<(B256, &Bytecode)> { + let present_new_codehash = self.info.as_ref().map(|info| &info.code_hash); + let previous_codehash = self.previous_info.as_ref().map(|info| &info.code_hash); + if present_new_codehash != previous_codehash { + return self + .info + .as_ref() + .and_then(|info| info.code.as_ref().map(|c| (info.code_hash, c))); + } + None + } + + /// Return the balance of account before transition. + pub fn previous_balance(&self) -> U256 { + self.previous_info.as_ref().map(|info| info.balance).unwrap_or_default() + } + + /// Return the balance of account after transition. + pub fn current_balance(&self) -> U256 { + self.info.as_ref().map(|info| info.balance).unwrap_or_default() + } + + /// Consume Self and create account revert from it. + pub fn create_revert(self) -> Option { + let mut previous_account = self.original_bundle_account(); + previous_account.update_and_create_revert(self) + } + + /// Present bundle account + pub fn present_bundle_account(&self) -> BundleAccount { + BundleAccount { + info: self.info.clone(), + original_info: self.previous_info.clone(), + storage: self.storage.clone(), + status: self.status, + } + } + + /// Original bundle account + fn original_bundle_account(&self) -> BundleAccount { + BundleAccount { + info: self.previous_info.clone(), + original_info: self.previous_info.clone(), + storage: StorageWithOriginalValues::default(), + status: self.previous_status, + } + } +} + +impl TransitionAccount { + /// Map the storage of the transition account. + pub fn map_storage(self, f: F) -> TransitionAccount + where + F: FnOnce(S) -> N, + { + let Self { info, status, previous_info, previous_status, storage, storage_was_destroyed } = + self; + TransitionAccount { + info, + status, + previous_info, + previous_status, + storage: f(storage), + storage_was_destroyed, + } + } +} diff --git a/crates/evm2/src/block/bundle_state/transition_state.rs b/crates/evm2/src/block/bundle_state/transition_state.rs new file mode 100644 index 000000000..8c6c45f07 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/transition_state.rs @@ -0,0 +1,34 @@ +use super::TransitionAccount; +use alloy_primitives::{ + Address, + map::{AddressMap, HashMap}, +}; + +/// State of accounts in transition between transaction executions. +#[derive(Clone, Default, Debug, PartialEq, Eq)] +pub struct TransitionState { + /// Block state account with account state + pub transitions: AddressMap, +} + +impl TransitionState { + /// Create new transition state containing one [`TransitionAccount`]. + pub fn single(address: Address, transition: TransitionAccount) -> Self { + let mut transitions = HashMap::default(); + transitions.insert(address, transition); + TransitionState { transitions } + } + + /// Take the contents of this [`TransitionState`] and replace it with an + /// empty one. + /// + /// See [core::mem::take]. + pub fn take(&mut self) -> TransitionState { + core::mem::take(self) + } + + /// Clear the transition state. + pub fn clear(&mut self) { + self.transitions.clear(); + } +} diff --git a/crates/evm2/src/block/error.rs b/crates/evm2/src/block/error.rs new file mode 100644 index 000000000..8c27477fa --- /dev/null +++ b/crates/evm2/src/block/error.rs @@ -0,0 +1,272 @@ +//! Block execution errors. + +use crate::{AnyError, DatabaseError, registry::HandlerError}; +use alloc::{ + boxed::Box, + string::{String, ToString}, +}; +use alloy_primitives::{Address, B256}; +use core::{any::Any, error::Error}; + +/// Abstraction over transaction validation errors. +pub trait InvalidTxError: Error + Send + Sync + Any + 'static { + /// Returns whether the transaction nonce is lower than expected. + fn is_nonce_too_low(&self) -> bool { + false + } + + /// Returns whether the transaction gas limit is higher than allowed. + fn is_gas_limit_too_high(&self) -> bool { + false + } + + /// Returns whether the transaction gas limit is lower than required. + fn is_gas_limit_too_low(&self) -> bool { + false + } + + /// Returns the error as [`Any`] for consumers that need engine-specific downcasts. + fn as_any(&self) -> &(dyn Any + 'static); +} + +/// Block validation error. +#[derive(Debug, thiserror::Error)] +pub enum BlockValidationError { + /// EVM error with transaction hash and message. + #[error("EVM reported invalid transaction ({hash}): {error}")] + InvalidTx { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: Box, + }, + /// EVM error that is not a transaction validation error. + #[error("EVM execution failed: {error}")] + EVM { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: AnyError, + }, + /// Error when incrementing balance in post execution. + #[error("incrementing balance in post execution failed")] + IncrementBalanceFailed, + /// Error when transaction gas limit exceeds available block gas. + #[error( + "transaction gas limit {transaction_gas_limit} is more than blocks available gas {block_available_gas}" + )] + TransactionGasLimitMoreThanAvailableBlockGas { + /// The transaction's gas limit. + transaction_gas_limit: u64, + /// The available block gas. + block_available_gas: u64, + }, + /// Error when transaction blob gas exceeds available block blob gas. + #[error( + "transaction blob gas {transaction_blob_gas} is more than blocks available blob gas {block_available_blob_gas}" + )] + TransactionBlobGasMoreThanAvailableBlockBlobGas { + /// The transaction's blob gas. + transaction_blob_gas: u64, + /// The available block blob gas. + block_available_blob_gas: u64, + }, + /// Error for EIP-4788 when parent beacon block root is missing. + #[error("EIP-4788 parent beacon block root missing for active Cancun block")] + MissingParentBeaconBlockRoot, + /// Error for Cancun genesis block when parent beacon block root is not zero. + #[error( + "the parent beacon block root is not zero for Cancun genesis block: {parent_beacon_block_root}" + )] + CancunGenesisParentBeaconBlockRootNotZero { + /// The beacon block root. + parent_beacon_block_root: B256, + }, + /// EVM error during EIP-4788 beacon root contract call. + #[error("failed to apply beacon root contract call at {parent_beacon_block_root}: {message}")] + BeaconRootContractCall { + /// The beacon block root. + parent_beacon_block_root: Box, + /// The error message. + message: String, + }, + /// EVM error during EIP-2935 blockhash contract call. + #[error("failed to apply blockhash contract call: {message}")] + BlockHashContractCall { + /// The error message. + message: String, + }, + /// EVM error during withdrawal requests contract call. + #[error("failed to apply withdrawal requests contract call: {message}")] + WithdrawalRequestsContractCall { + /// The error message. + message: String, + }, + /// EVM error during consolidation requests contract call. + #[error("failed to apply consolidation requests contract call: {message}")] + ConsolidationRequestsContractCall { + /// The error message. + message: String, + }, + /// A request system contract has no code, so the block cannot produce its requests. + #[error("system contract {address} has no code")] + SystemContractEmpty { + /// The address of the system contract. + address: Address, + }, + /// Error when decoding deposit requests from receipts. + #[error("failed to decode deposit requests from receipts: {_0}")] + DepositRequestDecode(String), + /// Error when block's total gas used exceeds the block gas limit. + #[error("block gas used exceeds block gas limit")] + BlockGasExceeded, + /// Arbitrary block validation error. + #[error(transparent)] + Other(AnyError), +} + +impl BlockValidationError { + /// Create a new [`BlockValidationError::Other`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Other(AnyError::new(error)) + } + + /// Create a new [`BlockValidationError::Other`] variant from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Other(AnyError::from(msg.to_string())) + } +} + +/// Block executor errors. +#[derive(Debug, thiserror::Error)] +pub enum BlockExecutionError { + /// Validation error. + #[error(transparent)] + Validation(#[from] BlockValidationError), + /// Internal, i.e. non consensus or validation related block executor error. + #[error(transparent)] + Internal(#[from] InternalBlockExecutionError), +} + +impl BlockExecutionError { + /// Create a new [`BlockExecutionError::Internal`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Internal(InternalBlockExecutionError::other(error)) + } + + /// Create a new [`BlockExecutionError::Internal`] variant from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Internal(InternalBlockExecutionError::msg(msg)) + } + + /// Returns the inner [`BlockValidationError`] if this is a validation error. + pub const fn as_validation(&self) -> Option<&BlockValidationError> { + match self { + Self::Validation(err) => Some(err), + _ => None, + } + } + + /// Returns the inner [`InternalBlockExecutionError`] if this is an internal error. + pub const fn as_internal(&self) -> Option<&InternalBlockExecutionError> { + match self { + Self::Internal(err) => Some(err), + _ => None, + } + } +} + +/// Internal, i.e. not validation or consensus related, block executor errors. +#[derive(Debug, thiserror::Error)] +pub enum InternalBlockExecutionError { + /// EVM error occurred when executing a transaction. + #[error("internal EVM error occurred when executing transaction {hash}: {error}")] + EVM { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: AnyError, + }, + /// Arbitrary block executor error. + #[error(transparent)] + Other(AnyError), +} + +impl InternalBlockExecutionError { + /// Create a new [`InternalBlockExecutionError::Other`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Other(AnyError::new(error)) + } + + /// Create a new [`InternalBlockExecutionError::Other`] from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Other(AnyError::from(msg.to_string())) + } + + /// Returns the arbitrary error if this is [`InternalBlockExecutionError::Other`]. + pub const fn as_other(&self) -> Option<&AnyError> { + match self { + Self::Other(err) => Some(err), + _ => None, + } + } + + /// Returns a reference to the [`InternalBlockExecutionError::Other`] value if it has type `T`. + pub fn downcast_other(&self) -> Option<&T> { + self.as_other()?.downcast_ref() + } + + /// Returns true if this is an [`InternalBlockExecutionError::Other`] of type `T`. + pub fn is_other(&self) -> bool { + self.downcast_other::().is_some() + } + + /// Returns the EVM error and transaction hash if this is [`InternalBlockExecutionError::EVM`]. + pub const fn as_evm(&self) -> Option<(&B256, &AnyError)> { + match self { + Self::EVM { hash, error } => Some((hash, error)), + _ => None, + } + } + + /// Returns a reference to the inner EVM error if it is of type `T`. + pub fn downcast_evm(&self) -> Option<&T> { + let (_, err) = self.as_evm()?; + err.downcast_ref() + } + + /// Returns true if this is an [`InternalBlockExecutionError::EVM`] error of type `T`. + pub fn is_evm(&self) -> bool { + self.downcast_evm::().is_some() + } +} + +/// Converts a handler error into a block error without transaction hash context. +/// Validation errors retain the original handler error for downstream classification. +pub fn map_handler_error(error: HandlerError) -> BlockExecutionError { + match error { + HandlerError::Database(error) => map_database_error(error), + HandlerError::Fatal(_) | HandlerError::WrongTransactionType { .. } => { + BlockExecutionError::other(error) + } + error => BlockValidationError::Other(AnyError::new(error)).into(), + } +} + +/// Converts an owned database error using its internal-failure classification. +pub fn map_database_error(error: DatabaseError) -> BlockExecutionError { + if error.is_fatal() { + BlockExecutionError::other(error) + } else { + BlockValidationError::Other(AnyError::new(error)).into() + } +} diff --git a/crates/evm2/src/block/ethereum/dao_fork.rs b/crates/evm2/src/block/ethereum/dao_fork.rs new file mode 100644 index 000000000..03a8a7b4e --- /dev/null +++ b/crates/evm2/src/block/ethereum/dao_fork.rs @@ -0,0 +1,128 @@ +//! DAO Fork related constants from [EIP-779](https://eips.ethereum.org/EIPS/eip-779). +//! It happened on Ethereum block `1_920_000`. + +use alloy_primitives::{Address, address}; + +/// Dao hardfork beneficiary that received ether from accounts from DAO and DAO creator children. +pub static DAO_HARDFORK_BENEFICIARY: Address = + address!("0xbf4ed7b27f1d666546e30d74d50d173d20bca754"); + +/// Accounts drained into the beneficiary at the DAO hardfork. +pub static DAO_HARDFORK_ACCOUNTS: [Address; 116] = [ + address!("0xd4fe7bc31cedb7bfb8a345f31e668033056b2728"), + address!("0xb3fb0e5aba0e20e5c49d252dfd30e102b171a425"), + address!("0x2c19c7f9ae8b751e37aeb2d93a699722395ae18f"), + address!("0xecd135fa4f61a655311e86238c92adcd779555d2"), + address!("0x1975bd06d486162d5dc297798dfc41edd5d160a7"), + address!("0xa3acf3a1e16b1d7c315e23510fdd7847b48234f6"), + address!("0x319f70bab6845585f412ec7724b744fec6095c85"), + address!("0x06706dd3f2c9abf0a21ddcc6941d9b86f0596936"), + address!("0x5c8536898fbb74fc7445814902fd08422eac56d0"), + address!("0x6966ab0d485353095148a2155858910e0965b6f9"), + address!("0x779543a0491a837ca36ce8c635d6154e3c4911a6"), + address!("0x2a5ed960395e2a49b1c758cef4aa15213cfd874c"), + address!("0x5c6e67ccd5849c0d29219c4f95f1a7a93b3f5dc5"), + address!("0x9c50426be05db97f5d64fc54bf89eff947f0a321"), + address!("0x200450f06520bdd6c527622a273333384d870efb"), + address!("0xbe8539bfe837b67d1282b2b1d61c3f723966f049"), + address!("0x6b0c4d41ba9ab8d8cfb5d379c69a612f2ced8ecb"), + address!("0xf1385fb24aad0cd7432824085e42aff90886fef5"), + address!("0xd1ac8b1ef1b69ff51d1d401a476e7e612414f091"), + address!("0x8163e7fb499e90f8544ea62bbf80d21cd26d9efd"), + address!("0x51e0ddd9998364a2eb38588679f0d2c42653e4a6"), + address!("0x627a0a960c079c21c34f7612d5d230e01b4ad4c7"), + address!("0xf0b1aa0eb660754448a7937c022e30aa692fe0c5"), + address!("0x24c4d950dfd4dd1902bbed3508144a54542bba94"), + address!("0x9f27daea7aca0aa0446220b98d028715e3bc803d"), + address!("0xa5dc5acd6a7968a4554d89d65e59b7fd3bff0f90"), + address!("0xd9aef3a1e38a39c16b31d1ace71bca8ef58d315b"), + address!("0x63ed5a272de2f6d968408b4acb9024f4cc208ebf"), + address!("0x6f6704e5a10332af6672e50b3d9754dc460dfa4d"), + address!("0x77ca7b50b6cd7e2f3fa008e24ab793fd56cb15f6"), + address!("0x492ea3bb0f3315521c31f273e565b868fc090f17"), + address!("0x0ff30d6de14a8224aa97b78aea5388d1c51c1f00"), + address!("0x9ea779f907f0b315b364b0cfc39a0fde5b02a416"), + address!("0xceaeb481747ca6c540a000c1f3641f8cef161fa7"), + address!("0xcc34673c6c40e791051898567a1222daf90be287"), + address!("0x579a80d909f346fbfb1189493f521d7f48d52238"), + address!("0xe308bd1ac5fda103967359b2712dd89deffb7973"), + address!("0x4cb31628079fb14e4bc3cd5e30c2f7489b00960c"), + address!("0xac1ecab32727358dba8962a0f3b261731aad9723"), + address!("0x4fd6ace747f06ece9c49699c7cabc62d02211f75"), + address!("0x440c59b325d2997a134c2c7c60a8c61611212bad"), + address!("0x4486a3d68fac6967006d7a517b889fd3f98c102b"), + address!("0x9c15b54878ba618f494b38f0ae7443db6af648ba"), + address!("0x27b137a85656544b1ccb5a0f2e561a5703c6a68f"), + address!("0x21c7fdb9ed8d291d79ffd82eb2c4356ec0d81241"), + address!("0x23b75c2f6791eef49c69684db4c6c1f93bf49a50"), + address!("0x1ca6abd14d30affe533b24d7a21bff4c2d5e1f3b"), + address!("0xb9637156d330c0d605a791f1c31ba5890582fe1c"), + address!("0x6131c42fa982e56929107413a9d526fd99405560"), + address!("0x1591fc0f688c81fbeb17f5426a162a7024d430c2"), + address!("0x542a9515200d14b68e934e9830d91645a980dd7a"), + address!("0xc4bbd073882dd2add2424cf47d35213405b01324"), + address!("0x782495b7b3355efb2833d56ecb34dc22ad7dfcc4"), + address!("0x58b95c9a9d5d26825e70a82b6adb139d3fd829eb"), + address!("0x3ba4d81db016dc2890c81f3acec2454bff5aada5"), + address!("0xb52042c8ca3f8aa246fa79c3feaa3d959347c0ab"), + address!("0xe4ae1efdfc53b73893af49113d8694a057b9c0d1"), + address!("0x3c02a7bc0391e86d91b7d144e61c2c01a25a79c5"), + address!("0x0737a6b837f97f46ebade41b9bc3e1c509c85c53"), + address!("0x97f43a37f595ab5dd318fb46e7a155eae057317a"), + address!("0x52c5317c848ba20c7504cb2c8052abd1fde29d03"), + address!("0x4863226780fe7c0356454236d3b1c8792785748d"), + address!("0x5d2b2e6fcbe3b11d26b525e085ff818dae332479"), + address!("0x5f9f3392e9f62f63b8eac0beb55541fc8627f42c"), + address!("0x057b56736d32b86616a10f619859c6cd6f59092a"), + address!("0x9aa008f65de0b923a2a4f02012ad034a5e2e2192"), + address!("0x304a554a310c7e546dfe434669c62820b7d83490"), + address!("0x914d1b8b43e92723e64fd0a06f5bdb8dd9b10c79"), + address!("0x4deb0033bb26bc534b197e61d19e0733e5679784"), + address!("0x07f5c1e1bc2c93e0402f23341973a0e043f7bf8a"), + address!("0x35a051a0010aba705c9008d7a7eff6fb88f6ea7b"), + address!("0x4fa802324e929786dbda3b8820dc7834e9134a2a"), + address!("0x9da397b9e80755301a3b32173283a91c0ef6c87e"), + address!("0x8d9edb3054ce5c5774a420ac37ebae0ac02343c6"), + address!("0x0101f3be8ebb4bbd39a2e3b9a3639d4259832fd9"), + address!("0x5dc28b15dffed94048d73806ce4b7a4612a1d48f"), + address!("0xbcf899e6c7d9d5a215ab1e3444c86806fa854c76"), + address!("0x12e626b0eebfe86a56d633b9864e389b45dcb260"), + address!("0xa2f1ccba9395d7fcb155bba8bc92db9bafaeade7"), + address!("0xec8e57756626fdc07c63ad2eafbd28d08e7b0ca5"), + address!("0xd164b088bd9108b60d0ca3751da4bceb207b0782"), + address!("0x6231b6d0d5e77fe001c2a460bd9584fee60d409b"), + address!("0x1cba23d343a983e9b5cfd19496b9a9701ada385f"), + address!("0xa82f360a8d3455c5c41366975bde739c37bfeb8a"), + address!("0x9fcd2deaff372a39cc679d5c5e4de7bafb0b1339"), + address!("0x005f5cee7a43331d5a3d3eec71305925a62f34b6"), + address!("0x0e0da70933f4c7849fc0d203f5d1d43b9ae4532d"), + address!("0xd131637d5275fd1a68a3200f4ad25c71a2a9522e"), + address!("0xbc07118b9ac290e4622f5e77a0853539789effbe"), + address!("0x47e7aa56d6bdf3f36be34619660de61275420af8"), + address!("0xacd87e28b0c9d1254e868b81cba4cc20d9a32225"), + address!("0xadf80daec7ba8dcf15392f1ac611fff65d94f880"), + address!("0x5524c55fb03cf21f549444ccbecb664d0acad706"), + address!("0x40b803a9abce16f50f36a77ba41180eb90023925"), + address!("0xfe24cdd8648121a43a7c86d289be4dd2951ed49f"), + address!("0x17802f43a0137c506ba92291391a8a8f207f487d"), + address!("0x253488078a4edf4d6f42f113d1e62836a942cf1a"), + address!("0x86af3e9626fce1957c82e88cbf04ddf3a2ed7915"), + address!("0xb136707642a4ea12fb4bae820f03d2562ebff487"), + address!("0xdbe9b615a3ae8709af8b93336ce9b477e4ac0940"), + address!("0xf14c14075d6c4ed84b86798af0956deef67365b5"), + address!("0xca544e5c4687d109611d0f8f928b53a25af72448"), + address!("0xaeeb8ff27288bdabc0fa5ebb731b6f409507516c"), + address!("0xcbb9d3703e651b0d496cdefb8b92c25aeb2171f7"), + address!("0x6d87578288b6cb5549d5076a207456a1f6a63dc0"), + address!("0xb2c6f0dfbb716ac562e2d85d6cb2f8d5ee87603e"), + address!("0xaccc230e8a6e5be9160b8cdf2864dd2a001c28b6"), + address!("0x2b3455ec7fedf16e646268bf88846bd7a2319bb2"), + address!("0x4613f3bca5c44ea06337a9e439fbc6d42e501d0a"), + address!("0xd343b217de44030afaa275f54d31a9317c7f441e"), + address!("0x84ef4b2357079cd7a7c69fd7a37cd0609a679106"), + address!("0xda2fef9e4a3230988ff17df2165440f37e8b1708"), + address!("0xf4c64518ea10f995918a454158c6b61407ea345c"), + address!("0x7602b46df5390e432ef1c307d4f2c9ff6d65cc97"), + address!("0xbb9bc244d798123fde783fcc1c72d3bb8c189413"), + address!("0x807640a13483f8ac783c557fcdf27be11ea4ac7a"), +]; diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs new file mode 100644 index 000000000..814993dd8 --- /dev/null +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -0,0 +1,735 @@ +//! EVM-backed Ethereum execution helpers. + +use super::dao_fork; +use crate::{ + AnyError, Evm, EvmTypes, SpecId, TxResult, TxResultWithState, TxStateChanges, + block::{ + BlockExecutionError, BlockState, BlockValidationError, CommitChanges, + InternalBlockExecutionError, InvalidTxError, StateUpdate, map_database_error, + map_handler_error, + }, + evm::{ + AccountChangeRef, AccountInfo, BEACON_ROOTS_ADDRESS, BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS, HISTORY_STORAGE_ADDRESS, + StateChangeSink, SystemTx, WITHDRAWAL_REQUEST_ADDRESS, + }, + interpreter::InstrStop, + registry::HandlerError, +}; +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, +}; +use alloy_consensus::{ + BlockHeader, Header, TxReceipt, constants::ETH_TO_WEI, transaction::Recovered, +}; +use alloy_eips::{ + eip2718::Typed2718, + eip4895::Withdrawal, + eip6110::{DEPOSIT_REQUEST_TYPE, MAINNET_DEPOSIT_CONTRACT_ADDRESS}, + eip7002::WITHDRAWAL_REQUEST_TYPE, + eip7251::CONSOLIDATION_REQUEST_TYPE, + eip7685::Requests, +}; +use alloy_hardforks::EthereumHardforks; +use alloy_primitives::{Address, B256, Bytes, Log, U256, b256, map::AddressMap}; +use core::any::Any; + +/// Size of one encoded EIP-6110 deposit request. +pub const DEPOSIT_BYTES_SIZE: usize = 48 + 32 + 8 + 96 + 8; +/// EIP-8282 builder deposit request type. +pub const BUILDER_DEPOSIT_REQUEST_TYPE: u8 = 0x03; +/// EIP-8282 builder exit request type. +pub const BUILDER_EXIT_REQUEST_TYPE: u8 = 0x04; + +/// First topic of the deposit contract's `DepositEvent`. +const DEPOSIT_EVENT_SIGNATURE_HASH: B256 = + b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"); + +/// ABI layout of a `DepositEvent` payload: the offset of each dynamic field's length word and +/// the field's size, in the order the consensus layer consumes them (public key, withdrawal +/// credentials, amount, signature, index). +const DEPOSIT_EVENT_FIELDS: [(usize, usize); 5] = + [(160, 48), (256, 32), (320, 8), (384, 96), (512, 8)]; + +/// Length of an ABI-encoded `DepositEvent` payload. +const DEPOSIT_EVENT_LENGTH: usize = 576; + +/// Ethereum transaction validation error returned by evm2 handlers. +#[derive(Debug, Clone, PartialEq, Eq)] +struct EthInvalidTxError(HandlerError); + +impl core::fmt::Display for EthInvalidTxError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + +impl core::error::Error for EthInvalidTxError {} + +impl InvalidTxError for EthInvalidTxError { + fn is_nonce_too_low(&self) -> bool { + matches!(self.0, HandlerError::InvalidNonce { expected, got } if got < expected) + } + + fn is_gas_limit_too_high(&self) -> bool { + matches!( + self.0, + HandlerError::GasLimitMoreThanBlock { .. } + | HandlerError::TxGasLimitGreaterThanCap { .. } + ) + } + + fn is_gas_limit_too_low(&self) -> bool { + matches!( + self.0, + HandlerError::IntrinsicGasTooLow { .. } | HandlerError::GasLimitBelowFloor { .. } + ) + } + + /// Exposes the wrapped [`HandlerError`] so RPC layers can recover the structured error. + fn as_any(&self) -> &(dyn Any + 'static) { + &self.0 + } +} + +impl TryFrom for EthInvalidTxError { + type Error = HandlerError; + + fn try_from(error: HandlerError) -> Result { + match error { + HandlerError::Database(_) + | HandlerError::Fatal(_) + | HandlerError::WrongTransactionType { .. } => Err(error), + HandlerError::External(_) + | HandlerError::UnsupportedTransactionType(_) + | HandlerError::InvalidNonce { .. } + | HandlerError::InvalidChainId { .. } + | HandlerError::MissingChainId + | HandlerError::IntrinsicGasTooLow { .. } + | HandlerError::GasLimitBelowFloor { .. } + | HandlerError::IntrinsicGasGreaterThanCap { .. } + | HandlerError::InsufficientFunds { .. } + | HandlerError::RejectCallerWithCode + | HandlerError::NonceOverflow + | HandlerError::GasLimitMoreThanBlock { .. } + | HandlerError::TxGasLimitGreaterThanCap { .. } + | HandlerError::CreateInitCodeSizeLimit { .. } + | HandlerError::FeeCapLessThanBaseFee { .. } + | HandlerError::EmptyAuthorizationList + | HandlerError::BlobFeeCapLessThanBlobBaseFee { .. } + | HandlerError::EmptyBlobs + | HandlerError::TooManyBlobs { .. } + | HandlerError::BlobVersionNotSupported + | HandlerError::PriorityFeeGreaterThanMaxFee => Ok(Self(error)), + } + } +} + +/// Additional block-level execution context. +#[derive(Debug, Clone, Copy, Default)] +pub struct BlockExecutionContext<'a> { + /// Pre-block system calls to run before transaction execution. + pub system_calls: Option, + /// Pre-merge ommer headers included in the block. + pub ommers: Option<&'a [Header]>, + /// Post-block withdrawals to apply after transaction execution. + pub withdrawals: Option<&'a [Withdrawal]>, + /// Deposit contract address used to derive EIP-6110 deposit requests from receipts. + pub deposit_contract_address: Option
, +} + +/// Inputs required by Ethereum pre-block system calls. +#[derive(Debug, Clone, Copy)] +pub struct BlockSystemCalls { + /// Parent block hash for EIP-2935 history storage. + pub parent_hash: B256, + /// Parent beacon block root for EIP-4788 beacon roots. + pub parent_beacon_block_root: Option, +} + +/// Converts a transaction's handler error into a block error with its hash. +pub fn map_transaction_error(error: HandlerError, hash: B256) -> BlockExecutionError { + let error = match EthInvalidTxError::try_from(error) { + Ok(error) => { + return BlockValidationError::InvalidTx { hash, error: Box::new(error) }.into(); + } + Err(error) => error, + }; + if let HandlerError::Database(database_error) = &error + && !database_error.is_fatal() + { + BlockValidationError::EVM { hash, error: AnyError::new(error) }.into() + } else { + InternalBlockExecutionError::EVM { hash, error: AnyError::new(error) }.into() + } +} + +fn send_state_update(state: StateUpdate, on_state_update: &mut impl FnMut(StateUpdate)) { + if !state.is_empty() { + on_state_update(state); + } +} + +/// Executes a transaction and commits it when `commit` accepts its result. +pub fn execute_transaction_with_condition( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + transaction: &Recovered, + commit: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, +) -> Result>, HandlerError> +where + T::Tx: Typed2718, +{ + let mut update = StateUpdate::default(); + let result = evm.transact(transaction).map(|executed| { + if commit(executed.result(), executed.changes()).should_commit() { + for changes in executed.account_changes() { + block_state.commit_account_changes(changes, stream_state.then_some(&mut update)); + } + Some(executed.commit()) + } else { + let _ = executed.discard(); + None + } + }); + if stream_state { + send_state_update(update, on_state_update); + } + result +} + +/// Executes a transaction and detaches its state changes. +pub fn execute_transaction_without_commit( + evm: &mut Evm<'_, T>, + transaction: &Recovered, +) -> Result, HandlerError> +where + T::Tx: Typed2718, +{ + evm.transact(transaction).map(|executed| executed.detach()) +} + +/// Commits a detached transaction into the EVM and the block state. +pub fn commit_detached_transaction( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + output: TxResultWithState, +) -> TxResult { + let TxResultWithState { result, pending_state, .. } = output; + accumulate_pending_state(block_state, stream_state, on_state_update, &pending_state); + // Reattach the finalized transaction so evm2 retains its account capacity and recycles + // storage maps for the next transaction instead of dropping the detached allocations. + evm.state_mut().set_pending_state(pending_state); + evm.state_mut().commit_transaction(); + result +} + +/// Commits detached state into the EVM overlay and the block state. +pub fn commit_pending_state( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + pending_state: &evm2::evm::PendingState, +) { + accumulate_pending_state(block_state, stream_state, on_state_update, pending_state); + evm.overlay_db_mut().commit_pending(pending_state); +} + +fn accumulate_pending_state( + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + pending_state: &evm2::evm::PendingState, +) { + let mut update = StateUpdate::default(); + for changes in pending_state.account_changes() { + block_state.commit_account_changes(changes, stream_state.then_some(&mut update)); + } + if stream_state { + send_state_update(update, on_state_update); + } +} + +/// Runs the EIP-2935 and EIP-4788 pre-block system calls. +pub fn pre_execution_system_call_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + block_number: u64, + context: BlockExecutionContext<'_>, +) -> Result<(), BlockExecutionError> { + let Some(system_calls) = context.system_calls else { + return Ok(()); + }; + + if spec_id.enables(SpecId::CANCUN) { + let parent_beacon_block_root = system_calls + .parent_beacon_block_root + .ok_or(BlockValidationError::MissingParentBeaconBlockRoot)?; + + if block_number == 0 { + if parent_beacon_block_root != B256::ZERO { + return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero { + parent_beacon_block_root, + } + .into()); + } + } else { + let _ = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + BEACON_ROOTS_ADDRESS, + parent_beacon_block_root.0.into(), + )?; + } + } + + if spec_id.enables(SpecId::PRAGUE) && block_number != 0 { + let _ = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + HISTORY_STORAGE_ADDRESS, + system_calls.parent_hash.0.into(), + )?; + } + + Ok(()) +} + +/// Collects the block's EIP-6110 deposit requests from its receipts. +pub fn block_requests_from_receipts( + spec_id: SpecId, + context: BlockExecutionContext<'_>, + receipts: &[R], +) -> Result +where + R: TxReceipt, +{ + let mut requests = Requests::default(); + if context.system_calls.is_none() || !spec_id.enables(SpecId::PRAGUE) { + return Ok(requests); + } + + let deposit_requests = parse_deposit_requests_from_receipts( + context.deposit_contract_address.unwrap_or(MAINNET_DEPOSIT_CONTRACT_ADDRESS), + receipts, + )?; + requests.push_request_with_type(DEPOSIT_REQUEST_TYPE, deposit_requests); + + Ok(requests) +} + +// TODO: Replace the local deposit event and encoding with alloy-eips helpers once +// https://github.com/alloy-rs/alloy/pull/4244 is released. +fn parse_deposit_requests_from_receipts( + deposit_contract_address: Address, + receipts: &[R], +) -> Result, BlockExecutionError> +where + R: TxReceipt, +{ + let mut out = Vec::new(); + for receipt in receipts { + for log in receipt.logs() { + if log.address != deposit_contract_address + || log.topics().first() != Some(&DEPOSIT_EVENT_SIGNATURE_HASH) + { + continue; + } + extract_deposit_data(&log.data.data, &mut out) + .map_err(BlockValidationError::DepositRequestDecode)?; + } + } + + Ok(out) +} + +/// Strips the ABI framing from a `DepositEvent` payload, appending its fields to `out`. A payload +/// that is not the canonical encoding makes the block invalid (EIP-6110). +fn extract_deposit_data(data: &[u8], out: &mut Vec) -> Result<(), String> { + if data.len() != DEPOSIT_EVENT_LENGTH { + return Err("invalid deposit event data length".to_string()); + } + let word = |offset: usize| U256::from_be_slice(&data[offset..offset + 32]); + for (index, (offset, _)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(index * 32) != U256::from(offset) { + return Err(alloc::format!("invalid offset for deposit field {index}")); + } + } + out.reserve(DEPOSIT_BYTES_SIZE); + for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(offset) != U256::from(size) { + return Err(alloc::format!("invalid size for deposit field {index}")); + } + out.extend_from_slice(&data[offset + 32..offset + 32 + size]); + } + Ok(()) +} + +/// Runs the EIP-7002, EIP-7251 and EIP-8282 request system calls. +pub fn post_execution_system_call_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + context: BlockExecutionContext<'_>, + requests: &mut Requests, +) -> Result<(), BlockExecutionError> { + if context.system_calls.is_none() || !spec_id.enables(SpecId::PRAGUE) { + return Ok(()); + } + + let withdrawal_requests = execute_request_system_call( + evm, + block_state, + stream_state, + on_state_update, + WITHDRAWAL_REQUEST_ADDRESS, + )?; + requests.push_request_with_type( + WITHDRAWAL_REQUEST_TYPE, + withdrawal_requests.output.iter().copied(), + ); + + let consolidation_requests = execute_request_system_call( + evm, + block_state, + stream_state, + on_state_update, + CONSOLIDATION_REQUEST_ADDRESS, + )?; + requests.push_request_with_type( + CONSOLIDATION_REQUEST_TYPE, + consolidation_requests.output.iter().copied(), + ); + + if spec_id.enables(SpecId::AMSTERDAM) { + let builder_deposit_requests = execute_request_system_call( + evm, + block_state, + stream_state, + on_state_update, + BUILDER_DEPOSIT_REQUEST_ADDRESS, + )?; + requests.push_request_with_type( + BUILDER_DEPOSIT_REQUEST_TYPE, + builder_deposit_requests.output.iter().copied(), + ); + + let builder_exit_requests = execute_request_system_call( + evm, + block_state, + stream_state, + on_state_update, + BUILDER_EXIT_REQUEST_ADDRESS, + )?; + requests.push_request_with_type( + BUILDER_EXIT_REQUEST_TYPE, + builder_exit_requests.output.iter().copied(), + ); + } + + Ok(()) +} + +/// Runs a request system call. A target without code makes the block invalid: the EVM itself +/// treats a call to it as a successful no-op. +fn execute_request_system_call( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + address: Address, +) -> Result, BlockExecutionError> { + let info = evm.read_account_info(&address).map_err(map_database_error)?; + if info.is_none_or(|info| info.is_empty_code_hash()) { + return Err(BlockValidationError::SystemContractEmpty { address }.into()); + } + execute_system_call(evm, block_state, stream_state, on_state_update, address, Bytes::new()) +} + +fn execute_system_call( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + address: Address, + data: Bytes, +) -> Result, BlockExecutionError> { + let executed = evm.system_call(SystemTx::new(address, data)).map_err(map_handler_error)?; + // History and beacon-root calls accept reverts and halts, unlike request-producing calls. + if !executed.result().status + && !matches!(address, BEACON_ROOTS_ADDRESS | HISTORY_STORAGE_ADDRESS) + { + let reason = executed.result().stop; + let _ = executed.discard(); + return Err(match address { + WITHDRAWAL_REQUEST_ADDRESS => BlockValidationError::WithdrawalRequestsContractCall { + message: alloc::format!("{reason:?}"), + }, + CONSOLIDATION_REQUEST_ADDRESS => { + BlockValidationError::ConsolidationRequestsContractCall { + message: alloc::format!("{reason:?}"), + } + } + _ => BlockValidationError::Other(AnyError::new(SystemCallFailed { address, reason })), + } + .into()); + } + let outcome = executed.detach(); + Ok(commit_detached_transaction(evm, block_state, stream_state, on_state_update, outcome)) +} + +fn commit_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + changes: &[(Address, Option, Option)], +) { + let mut pending_state = evm2::evm::PendingState::default(); + for (address, original, current) in changes { + let change = AccountChangeRef { + address: *address, + original: original.as_ref(), + current: current.as_ref(), + created: false, + selfdestructed: false, + }; + let Ok(()) = evm.overlay_db_mut().account(change); + pending_state.insert_account(*address, original.clone(), current.clone()); + } + accumulate_pending_state(block_state, stream_state, on_state_update, &pending_state); +} + +/// Drains the DAO accounts into the refund beneficiary. Runs before the block's transactions. +/// +/// The fork predates EIP-161, so drained accounts stay, empty or absent ones included. +pub fn dao_fork_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), +) -> Result<(), BlockExecutionError> { + let mut drained_balance = U256::ZERO; + let mut changes = Vec::new(); + for address in dao_fork::DAO_HARDFORK_ACCOUNTS { + let original = evm.read_account_info(&address).map_err(map_database_error)?; + let mut current = original.clone().unwrap_or_else(AccountInfo::empty); + drained_balance = drained_balance.saturating_add(current.balance); + current.balance = U256::ZERO; + if original.as_ref() != Some(¤t) { + changes.push((address, original, Some(current))); + } + } + commit_state_changes(evm, block_state, stream_state, on_state_update, &changes); + + // The beneficiary may also be drained, so read its balance after the drain. + let mut balance_increments = AddressMap::default(); + balance_increments.insert(dao_fork::DAO_HARDFORK_BENEFICIARY, drained_balance); + apply_balance_increments( + evm, + block_state, + stream_state, + on_state_update, + false, + balance_increments, + ) +} + +/// Applies block and ommer rewards and withdrawals. +#[expect(clippy::too_many_arguments)] +pub fn post_block_balance_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + base_block_reward: Option, + block_number: u64, + block_beneficiary: Address, + ommers: Option<&[Header]>, + withdrawals: Option<&[Withdrawal]>, +) -> Result<(), BlockExecutionError> { + let mut balance_increments = AddressMap::::default(); + + if let Some(base_block_reward) = base_block_reward { + let ommers = ommers.unwrap_or_default(); + for ommer in ommers { + *balance_increments.entry(ommer.beneficiary()).or_default() += + U256::from(ommer_reward(base_block_reward, block_number, ommer.number())); + } + *balance_increments.entry(block_beneficiary).or_default() += + U256::from(block_reward(base_block_reward, ommers.len())); + } + + for withdrawal in withdrawals.into_iter().flatten() { + *balance_increments.entry(withdrawal.address).or_default() += withdrawal.amount_wei(); + } + + apply_balance_increments( + evm, + block_state, + stream_state, + on_state_update, + spec_id.enables(SpecId::SPURIOUS_DRAGON), + balance_increments, + ) +} + +/// Adds each increment to its account's balance. With `eip161`, a zero increment removes an empty +/// account. +fn apply_balance_increments( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + eip161: bool, + balance_increments: AddressMap, +) -> Result<(), BlockExecutionError> { + let mut changes = Vec::with_capacity(balance_increments.len()); + for (address, increment) in balance_increments { + let original = evm.read_account_info(&address).map_err(map_database_error)?; + let current = + if eip161 && increment.is_zero() && original.as_ref().is_none_or(AccountInfo::is_empty) + { + // The increment only touches the account, which EIP-161 removes when empty. + None + } else { + let mut current = original.clone().unwrap_or_else(AccountInfo::empty); + current.balance = current.balance.saturating_add(increment); + Some(current) + }; + if original == current { + // Zero-value withdrawals still count as account accesses in the BAL. + let Ok(()) = evm.overlay_db_mut().account_read(address, original.as_ref()); + continue; + } + changes.push((address, original, current)); + } + commit_state_changes(evm, block_state, stream_state, on_state_update, &changes); + Ok(()) +} + +/// Returns the base block reward at `block_number`, or `None` after the merge. +pub fn base_block_reward(chain_spec: &C, block_number: u64) -> Option +where + C: EthereumHardforks + ?Sized, +{ + if chain_spec.is_paris_active_at_block(block_number) { + None + } else if chain_spec.is_constantinople_active_at_block(block_number) { + Some(ETH_TO_WEI * 2) + } else if chain_spec.is_byzantium_active_at_block(block_number) { + Some(ETH_TO_WEI * 3) + } else { + Some(ETH_TO_WEI * 5) + } +} + +/// Returns the block reward with the ommer inclusion bonus. +pub const fn block_reward(base_block_reward: u128, ommers: usize) -> u128 { + base_block_reward + (base_block_reward >> 5) * ommers as u128 +} + +/// Returns the reward of an ommer included at `block_number`. +pub fn ommer_reward(base_block_reward: u128, block_number: u64, ommer_block_number: u64) -> u128 { + let distance = 8u64.saturating_add(ommer_block_number).saturating_sub(block_number); + (u128::from(distance) * base_block_reward) >> 3 +} + +/// A system contract reverted or halted without producing a successful result. +#[derive(Debug)] +struct SystemCallFailed { + address: Address, + reason: InstrStop, +} + +impl core::fmt::Display for SystemCallFailed { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "EVM system call to {} failed: {:?}", self.address, self.reason) + } +} + +impl core::error::Error for SystemCallFailed {} + +#[cfg(test)] +mod tests { + use super::*; + use alloc::vec; + use alloy_primitives::LogData; + + fn deposit_log(data: Vec) -> Log { + Log { + address: MAINNET_DEPOSIT_CONTRACT_ADDRESS, + data: LogData::new_unchecked(vec![DEPOSIT_EVENT_SIGNATURE_HASH], data.into()), + } + } + + /// Encodes a canonical `DepositEvent` payload whose fields are filled with `fill`. + fn deposit_data(fill: u8) -> Vec { + let mut data = vec![0; DEPOSIT_EVENT_LENGTH]; + for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + data[index * 32..index * 32 + 32] + .copy_from_slice(&U256::from(offset).to_be_bytes::<32>()); + data[offset..offset + 32].copy_from_slice(&U256::from(size).to_be_bytes::<32>()); + data[offset + 32..offset + 32 + size].fill(fill); + } + data + } + + fn requests(log: Log) -> Result, BlockExecutionError> { + let receipt = alloy_consensus::Receipt { + status: true.into(), + cumulative_gas_used: 0, + logs: vec![log], + }; + parse_deposit_requests_from_receipts(MAINNET_DEPOSIT_CONTRACT_ADDRESS, &[receipt]) + } + + #[test] + fn parses_canonical_deposit() { + assert_eq!(requests(deposit_log(deposit_data(7))).unwrap(), vec![7; DEPOSIT_BYTES_SIZE]); + } + + #[test] + fn rejects_non_canonical_deposits() { + let mut short = deposit_data(7); + short.pop(); + let mut bad_offset = deposit_data(7); + bad_offset[31] = 0; + let mut bad_size = deposit_data(7); + bad_size[160 + 31] = 47; + for data in [short, bad_offset, bad_size] { + assert!(matches!( + requests(deposit_log(data)), + Err(BlockExecutionError::Validation(BlockValidationError::DepositRequestDecode(_))) + )); + } + } + + #[test] + fn classifies_gas_errors() { + let floor = EthInvalidTxError(HandlerError::GasLimitBelowFloor { required: 2, got: 1 }); + assert!(floor.is_gas_limit_too_low()); + assert!(!floor.is_gas_limit_too_high()); + let cap = + EthInvalidTxError(HandlerError::IntrinsicGasGreaterThanCap { required: 2, cap: 1 }); + assert!(!cap.is_gas_limit_too_low()); + assert!(!cap.is_gas_limit_too_high()); + } +} diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs new file mode 100644 index 000000000..e6fabff6b --- /dev/null +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -0,0 +1,622 @@ +//! Ethereum block execution. + +use crate::{ + BaseEvmTypes, Evm, EvmFeatures, EvmTypes, SpecId, TxResult, TxResultWithState, TxStateChanges, + block::{ + BlockExecutionError, BlockExecutionOutput, BlockExecutionResult, BlockExecutor, BlockState, + BlockTransactionResult, BlockValidationError, BundleState, CommitChanges, ExecutableTx, + GasOutput, ReceiptBuilder, ReceiptBuilderCtx, StateUpdate, + }, + evm::{Bal, PendingState}, + interpreter::Host, +}; +use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec}; +use alloy_consensus::{ + Eip658Value, EthereumTxEnvelope, Header, Receipt, ReceiptEnvelope, Transaction, TxEip4844, + TxType, + transaction::{TransactionEnvelope, TxHashRef}, +}; +use alloy_eip7928::BlockAccessIndex; +use alloy_eips::{eip2718::Typed2718, eip4895::Withdrawal, eip7685::Requests}; +use alloy_hardforks::{EthereumHardfork, EthereumHardforks}; +use alloy_primitives::{Address, B256}; +use core::borrow::Borrow; + +pub mod dao_fork; + +pub mod execution; +use execution::{ + BlockExecutionContext, BlockSystemCalls, block_requests_from_receipts, + commit_detached_transaction, commit_pending_state, dao_fork_state_changes, + execute_transaction_without_commit, map_transaction_error, post_block_balance_state_changes, + post_execution_system_call_state_changes, pre_execution_system_call_state_changes, +}; + +#[cfg(test)] +mod tests; + +/// Block inputs of [`EthBlockExecutor`] that are not in the EVM's block environment. +#[derive(Clone, Debug, Default)] +pub struct EthBlockExecutionCtx<'a> { + /// Optional transaction count hint. + pub tx_count_hint: Option, + /// Parent block hash. + pub parent_hash: B256, + /// Parent beacon block root. + pub parent_beacon_block_root: Option, + /// Ommer headers. + pub ommers: Cow<'a, [Header]>, + /// Withdrawals. + pub withdrawals: Option>, +} + +/// Chain rules of [`EthBlockExecutor`] that depend on the block number. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct EthBlockRules { + /// Block reward before the merge. + pub base_block_reward: Option, + /// Whether the block is the DAO fork block. + pub dao_fork_transition: bool, + /// EIP-6110 deposit contract. `None` means Ethereum mainnet's, which is wrong for other chains + /// with deposits, such as Sepolia or Hoodi. + pub deposit_contract_address: Option
, + /// Blob gas the block's transactions may use in total (EIP-4844). `None` leaves it unchecked. + pub max_blob_gas_per_block: Option, +} + +impl EthBlockRules { + /// Reads the rules of block `block_number` from a chain's hardforks. + pub fn new( + chain: &C, + block_number: u64, + deposit_contract_address: Option
, + ) -> Self { + Self { + base_block_reward: execution::base_block_reward(chain, block_number), + dao_fork_transition: chain + .ethereum_fork_activation(EthereumHardfork::Dao) + .transitions_at_block(block_number), + deposit_contract_address, + max_blob_gas_per_block: None, + } + } +} + +/// Builds [`ReceiptEnvelope`]s for Ethereum transactions without blob sidecars. +/// +/// Receipts carry an EIP-658 status, so their root matches headers only from Byzantium on; earlier +/// receipts carry the intermediate state root instead. +#[derive(Clone, Copy, Debug, Default)] +#[non_exhaustive] +pub struct EthReceiptBuilder; + +impl ReceiptBuilder for EthReceiptBuilder { + type Transaction = EthereumTxEnvelope; + type Receipt = ReceiptEnvelope; + + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx>, + ) -> ReceiptEnvelope { + let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; + let receipt = Receipt { + status: Eip658Value::Eip658(result.status), + cumulative_gas_used, + logs: result.logs, + }; + ReceiptEnvelope::from_typed(tx_type, receipt.with_bloom()) + } +} + +/// Detached Ethereum transaction result with the metadata needed for canonical receipt commit. +#[derive(Debug)] +pub struct EthTransactionResultWithState { + result: TxResultWithState, + tx_type: Ty, + blob_gas_used: u64, +} + +impl EthTransactionResultWithState { + /// Creates a detached transaction result with its receipt metadata. + pub const fn new(result: TxResultWithState, tx_type: Ty, blob_gas_used: u64) -> Self { + Self { result, tx_type, blob_gas_used } + } + + /// Returns the detached EVM transaction result. + pub const fn result(&self) -> &TxResultWithState { + &self.result + } + + /// Splits the result into its parts. + pub fn into_parts(self) -> (TxResultWithState, Ty, u64) { + (self.result, self.tx_type, self.blob_gas_used) + } +} + +impl BlockTransactionResult for EthTransactionResultWithState { + fn result(&self) -> &TxResultWithState { + &self.result + } +} + +type StateUpdateHook = Option>; + +/// Post-execution output of one block, before its receipts and state are taken. +#[derive(Debug)] +pub struct EthPostExecutionOutput { + /// EIP-7685 requests of the block. + pub requests: Requests, + /// Gas used by the block. + pub gas_used: u64, + /// Blob gas used by the block. + pub blob_gas_used: u64, +} + +/// Configured Ethereum block executor backed by evm2. +#[expect(missing_debug_implementations)] +pub struct EthBlockExecutor<'a, T = BaseEvmTypes, R = EthReceiptBuilder> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + /// EVM containing the execution overlay and optional block access list builder. + pub evm: Evm<'a, T>, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + receipt_builder: R, + spec_id: SpecId, + /// Accumulated state changes, including state committed through the executor's hooks. + pub block_state: BlockState, + state_update_hook: StateUpdateHook, + /// Transaction receipts in execution order. + pub receipts: Vec, + /// Total receipt gas used. + pub cumulative_gas_used: u64, + /// Regular gas used for Amsterdam block-capacity accounting. + pub block_regular_gas_used: u64, + /// State gas used for Amsterdam block-capacity accounting. + pub block_state_gas_used: u64, + separate_block_gas: bool, + /// Total blob gas used. + pub blob_gas_used: u64, + bal_index_offset: u64, +} + +impl<'a, T, R> EthBlockExecutor<'a, T, R> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + /// Creates an executor for the block in `evm`'s block environment. + pub fn new( + evm: Evm<'a, T>, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + receipt_builder: R, + ) -> Self { + // The executor stores evm2's base spec ID, while custom type families may expose a + // richer host spec ID that converts into it. + #[allow(clippy::useless_conversion)] + let spec_id = evm.spec_id().into(); + let separate_block_gas = evm.version().feature(EvmFeatures::EIP8037); + let receipts = Vec::with_capacity(ctx.tx_count_hint.unwrap_or_default()); + Self { + evm, + ctx, + rules, + receipt_builder, + spec_id, + block_state: BlockState::new(), + state_update_hook: None, + receipts, + cumulative_gas_used: 0, + block_regular_gas_used: 0, + block_state_gas_used: 0, + separate_block_gas, + blob_gas_used: 0, + bal_index_offset: 0, + } + } + + const fn block_context<'ctx>( + rules: &EthBlockRules, + parent_hash: B256, + parent_beacon_block_root: Option, + ommers: &'ctx [Header], + withdrawals: Option<&'ctx [Withdrawal]>, + ) -> BlockExecutionContext<'ctx> { + BlockExecutionContext { + system_calls: Some(BlockSystemCalls { parent_hash, parent_beacon_block_root }), + ommers: Some(ommers), + withdrawals, + deposit_contract_address: rules.deposit_contract_address, + } + } + + const fn block_access_list_builder_enabled(&self) -> bool { + self.evm.state().bal_builder().is_some() + } + + /// Returns the block execution context. + pub const fn context(&self) -> &EthBlockExecutionCtx<'a> { + &self.ctx + } + + /// Returns the chain rules of the block. + pub const fn rules(&self) -> &EthBlockRules { + &self.rules + } + + /// Returns the accumulated block state. + pub const fn block_state(&self) -> &BlockState { + &self.block_state + } + + /// Commits detached state without recording a transaction receipt or gas usage. + pub fn commit_pending_state(&mut self, state: &PendingState) { + let stream_state = self.state_update_hook.is_some(); + commit_pending_state( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + state, + ); + } + + /// Sets the Ethereum fork used for block system calls and deposit request processing. + pub const fn set_spec_id(&mut self, spec_id: SpecId) { + self.spec_id = spec_id; + } + + /// Starts a new block on the same EVM and block state, as when executing several blocks as + /// one: reads the spec and gas rules from the EVM's current configuration, installs `ctx` + /// and `rules`, offsets the block access indices by `bal_index_offset`, and resets the gas + /// counters. Receipts and block state carry over. + /// + /// Finish each block with [`Self::apply_post_execution_changes`] from its first receipt and the + /// last one with [`BlockExecutor::into_state`]: [`BlockExecutor::finish`] applies + /// post-execution changes again, over every receipt. + pub fn start_block( + &mut self, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + bal_index_offset: u64, + ) { + #[allow(clippy::useless_conversion)] + let spec_id = self.evm.spec_id().into(); + self.spec_id = spec_id; + self.separate_block_gas = self.evm.version().feature(EvmFeatures::EIP8037); + self.ctx = ctx; + self.rules = rules; + self.bal_index_offset = bal_index_offset; + self.reset_gas_used(); + } + + /// Resets the block's gas counters. + pub const fn reset_gas_used(&mut self) { + self.cumulative_gas_used = 0; + self.block_regular_gas_used = 0; + self.block_state_gas_used = 0; + self.blob_gas_used = 0; + } + + /// Applies the block's post-execution changes without consuming the executor: requests from + /// the receipts from `receipts_start` on, rewards and withdrawals, then the request system + /// calls (execution-specs order). + pub fn apply_post_execution_changes( + &mut self, + receipts_start: usize, + ) -> Result { + self.set_transaction_block_access_index(); + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + None, + ); + let mut requests = + block_requests_from_receipts(self.spec_id, context, &self.receipts[receipts_start..])?; + let stream_state = self.state_update_hook.is_some(); + let withdrawals = + self.ctx.withdrawals.as_deref().filter(|_| self.spec_id.enables(SpecId::SHANGHAI)); + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + withdrawals, + ); + let block_number = self.evm.block_env().number.to::(); + let block_beneficiary = self.evm.block_env().beneficiary; + post_block_balance_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + self.rules.base_block_reward, + block_number, + block_beneficiary, + context.ommers, + context.withdrawals, + )?; + + post_execution_system_call_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + context, + &mut requests, + )?; + + Ok(EthPostExecutionOutput { + requests, + gas_used: final_block_gas_used( + self.separate_block_gas, + self.cumulative_gas_used, + self.block_regular_gas_used, + self.block_state_gas_used, + ), + blob_gas_used: self.blob_gas_used, + }) + } + + /// Validates that a transaction's blobs fit the block's remaining blob gas. + fn validate_transaction_blob_gas( + &self, + transaction_blob_gas: u64, + ) -> Result<(), BlockExecutionError> { + let Some(max_blob_gas) = self.rules.max_blob_gas_per_block else { return Ok(()) }; + let block_available_blob_gas = max_blob_gas.saturating_sub(self.blob_gas_used); + if transaction_blob_gas > block_available_blob_gas { + return Err(BlockValidationError::TransactionBlobGasMoreThanAvailableBlockBlobGas { + transaction_blob_gas, + block_available_blob_gas, + } + .into()); + } + Ok(()) + } + + #[inline] + const fn set_transaction_block_access_index(&mut self) { + if self.block_access_list_builder_enabled() { + let index = self.receipts.len() as u64 + 1 + self.bal_index_offset; + self.evm.state_mut().set_bal_index(BlockAccessIndex::new(index)); + } + } + + fn record_receipt( + &mut self, + tx_type: ::TxType, + blob_gas_used: u64, + outcome: TxResult, + ) -> GasOutput { + let tx_gas_used = outcome.tx_gas_used(); + let regular_gas_used = outcome.execution_gas_spent(); + let state_gas_used = outcome.state_gas_spent(); + self.block_regular_gas_used = self.block_regular_gas_used.saturating_add(regular_gas_used); + self.block_state_gas_used = self.block_state_gas_used.saturating_add(state_gas_used); + self.cumulative_gas_used += tx_gas_used; + self.blob_gas_used += blob_gas_used; + self.receipts.push(self.receipt_builder.build_receipt::(ReceiptBuilderCtx { + tx_type, + result: outcome, + cumulative_gas_used: self.cumulative_gas_used, + })); + GasOutput::new_with_regular(tx_gas_used, regular_gas_used, state_gas_used) + } +} + +impl<'a, T, R> BlockExecutor for EthBlockExecutor<'a, T, R> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + type Transaction = R::Transaction; + type Receipt = R::Receipt; + type EvmTypes = T; + type Evm = Evm<'a, T>; + type TransactionResultWithState = + EthTransactionResultWithState::TxType>; + + fn evm(&self) -> &Self::Evm { + &self.evm + } + + fn evm_mut(&mut self) -> &mut Self::Evm { + &mut self.evm + } + + fn set_state_hook(&mut self, hook: impl FnMut(StateUpdate) + Send + 'static) -> bool { + self.state_update_hook = Some(Box::new(hook)); + true + } + + fn set_block_access_list(&mut self, block_access_list: Arc) { + self.evm.state_mut().set_bal(block_access_list); + } + + fn set_block_access_index(&mut self, index: BlockAccessIndex) { + self.evm.state_mut().set_bal_index(index); + } + + fn enable_block_access_list_builder(&mut self) { + self.evm.state_mut().enable_bal_builder(); + self.evm.state_mut().reset_bal_index(); + } + + fn take_block_access_list(&mut self) -> Option { + self.evm.state_mut().take_bal_builder() + } + + fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError> { + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + None, + ); + let block_number = self.evm.block_env().number.to::(); + let stream_state = self.state_update_hook.is_some(); + if self.rules.dao_fork_transition { + core::hint::cold_path(); + dao_fork_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + )?; + } + pre_execution_system_call_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + block_number, + context, + ) + } + + fn into_state(self) -> BundleState { + self.block_state.into_bundle() + } + + fn validate_transaction_gas_limit( + &mut self, + transaction_gas_limit: u64, + ) -> Result<(), BlockExecutionError> { + let block_gas_limit = self.evm.block_env().gas_limit.to::(); + let regular_limit = transaction_gas_limit.min(self.evm.version().tx_gas_limit_cap); + let unavailable = if self.separate_block_gas { + let regular_available = block_gas_limit.saturating_sub(self.block_regular_gas_used); + let state_available = block_gas_limit.saturating_sub(self.block_state_gas_used); + if regular_limit > regular_available { + Some((transaction_gas_limit, regular_available)) + } else if transaction_gas_limit > state_available { + Some((transaction_gas_limit, state_available)) + } else { + None + } + } else { + let available = block_gas_limit.saturating_sub(self.cumulative_gas_used); + (regular_limit > available).then_some((transaction_gas_limit, available)) + }; + if let Some((transaction_gas_limit, block_available_gas)) = unavailable { + return Err(BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { + transaction_gas_limit, + block_available_gas, + } + .into()); + } + Ok(()) + } + + fn execute_transaction_with_commit_condition( + &mut self, + tx: impl ExecutableTx, + commit: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, + ) -> Result, BlockExecutionError> { + let (transaction, tx) = tx.into_parts(); + let tx = tx.borrow(); + self.set_transaction_block_access_index(); + self.validate_transaction_gas_limit(tx.gas_limit())?; + let blob_gas_used = tx.blob_gas_used().unwrap_or_default(); + self.validate_transaction_blob_gas(blob_gas_used)?; + let tx_type = tx.tx_type(); + let Some(outcome) = execution::execute_transaction_with_condition( + &mut self.evm, + &mut self.block_state, + self.state_update_hook.is_some(), + &mut |state| emit_state(&mut self.state_update_hook, state), + &transaction, + commit, + ) + .map_err(|error| map_transaction_error(error, *tx.tx_hash()))? + else { + return Ok(None); + }; + Ok(Some(self.record_receipt(tx_type, blob_gas_used, outcome))) + } + + fn execute_transaction_without_commit( + &mut self, + tx: impl ExecutableTx, + ) -> Result { + let (transaction, tx) = tx.into_parts(); + let tx = tx.borrow(); + self.set_transaction_block_access_index(); + self.validate_transaction_gas_limit(tx.gas_limit())?; + let blob_gas_used = tx.blob_gas_used().unwrap_or_default(); + self.validate_transaction_blob_gas(blob_gas_used)?; + let tx_type = tx.tx_type(); + let result = execute_transaction_without_commit(&mut self.evm, &transaction) + .map_err(|err| map_transaction_error(err, *tx.tx_hash()))?; + Ok(EthTransactionResultWithState::new(result, tx_type, blob_gas_used)) + } + + fn commit_transaction( + &mut self, + output: Self::TransactionResultWithState, + ) -> Result { + let EthTransactionResultWithState { result, tx_type, blob_gas_used } = output; + self.set_transaction_block_access_index(); + let stream_state = self.state_update_hook.is_some(); + let outcome = commit_detached_transaction( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + result, + ); + Ok(self.record_receipt(tx_type, blob_gas_used, outcome)) + } + + fn receipts(&self) -> &[Self::Receipt] { + &self.receipts + } + + fn finish_with_block_access_list( + mut self, + ) -> Result<(BlockExecutionOutput, Option), BlockExecutionError> { + let EthPostExecutionOutput { requests, gas_used, blob_gas_used } = + self.apply_post_execution_changes(0)?; + let block_access_list = self.evm.state_mut().take_bal_builder(); + let output = BlockExecutionOutput::new( + BlockExecutionResult { receipts: self.receipts, requests, gas_used, blob_gas_used }, + self.block_state.into_bundle(), + ); + Ok((output, block_access_list)) + } +} + +/// Returns the block header's gas used: the larger of regular and state gas under EIP-8037, +/// receipt gas otherwise. +pub const fn final_block_gas_used( + separate_block_gas: bool, + cumulative_gas_used: u64, + block_regular_gas_used: u64, + block_state_gas_used: u64, +) -> u64 { + if separate_block_gas { + if block_regular_gas_used > block_state_gas_used { + block_regular_gas_used + } else { + block_state_gas_used + } + } else { + cumulative_gas_used + } +} + +fn emit_state(hook: &mut StateUpdateHook, state: StateUpdate) { + if let Some(hook) = hook.as_mut() { + hook(state); + } +} diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs new file mode 100644 index 000000000..a9221dc3b --- /dev/null +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -0,0 +1,287 @@ +use super::*; +use crate::{ + BaseEvmTypes, Precompiles, SpecId, + block::BlockExecutionError, + bytecode::Bytecode, + env::BlockEnvExt, + ethereum::ethereum_tx_registry, + evm::{ + AccountInfo, BEACON_ROOTS_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS, HISTORY_STORAGE_ADDRESS, + InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, + }, +}; +use alloc::vec; +use alloy_consensus::{Signed, TxLegacy, constants::ETH_TO_WEI, transaction::Recovered}; +use alloy_eips::{ + eip2935::HISTORY_STORAGE_CODE, eip4788::BEACON_ROOTS_CODE, + eip7002::WITHDRAWAL_REQUEST_PREDEPLOY_CODE, eip7251::CONSOLIDATION_REQUEST_PREDEPLOY_CODE, +}; +use alloy_primitives::{Signature, TxKind, U256}; + +const SENDER: Address = Address::repeat_byte(0x01); +const RECIPIENT: Address = Address::repeat_byte(0x02); +const BENEFICIARY: Address = Address::repeat_byte(0x03); +const PARENT_HASH: B256 = B256::repeat_byte(0x11); +const PARENT_BEACON_BLOCK_ROOT: B256 = B256::repeat_byte(0x22); + +fn database() -> InMemoryDB { + let mut database = InMemoryDB::default(); + let sender = AccountInfo { balance: U256::from(ETH_TO_WEI), ..AccountInfo::default() }; + database.insert_account_info(&SENDER, sender); + for (address, code) in [ + (HISTORY_STORAGE_ADDRESS, &HISTORY_STORAGE_CODE), + (BEACON_ROOTS_ADDRESS, &BEACON_ROOTS_CODE), + (WITHDRAWAL_REQUEST_ADDRESS, &WITHDRAWAL_REQUEST_PREDEPLOY_CODE), + (CONSOLIDATION_REQUEST_ADDRESS, &CONSOLIDATION_REQUEST_PREDEPLOY_CODE), + ] { + let code = Bytecode::new_legacy(code.clone()); + database.insert_account_info(&address, AccountInfo::default().with_code(code)); + } + database +} + +fn evm(spec: SpecId, gas_limit: u64) -> Evm<'static, BaseEvmTypes> { + evm_with(spec, gas_limit, database()) +} + +fn evm_with(spec: SpecId, gas_limit: u64, database: InMemoryDB) -> Evm<'static, BaseEvmTypes> { + let block = BlockEnvExt { + number: U256::from(1), + timestamp: U256::from(12), + beneficiary: BENEFICIARY, + gas_limit: U256::from(gas_limit), + ..BlockEnvExt::default() + }; + Evm::new(spec, block, ethereum_tx_registry(spec), database, Precompiles::base(spec)) +} + +fn ctx<'a>() -> EthBlockExecutionCtx<'a> { + EthBlockExecutionCtx { + parent_hash: PARENT_HASH, + parent_beacon_block_root: Some(PARENT_BEACON_BLOCK_ROOT), + ..EthBlockExecutionCtx::default() + } +} + +fn executor<'a>( + spec: SpecId, + gas_limit: u64, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, +) -> EthBlockExecutor<'a> { + let evm = evm(spec, gas_limit); + EthBlockExecutor::new(evm, ctx, rules, EthReceiptBuilder) +} + +fn transfer(gas_limit: u64) -> Recovered> { + let tx = TxLegacy { + nonce: 0, + gas_price: 1, + gas_limit, + to: TxKind::Call(RECIPIENT), + value: U256::from(1), + ..TxLegacy::default() + }; + let signature = Signature::new(U256::from(1), U256::from(1), false); + Recovered::new_unchecked( + EthereumTxEnvelope::Legacy(Signed::new_unhashed(tx, signature)), + SENDER, + ) +} + +fn blob_transfer() -> Recovered> { + let tx = TxEip4844 { + gas_limit: 21_000, + to: RECIPIENT, + blob_versioned_hashes: vec![B256::ZERO], + ..TxEip4844::default() + }; + let signature = Signature::new(U256::from(1), U256::from(1), false); + Recovered::new_unchecked( + EthereumTxEnvelope::Eip4844(Signed::new_unhashed(tx, signature)), + SENDER, + ) +} + +fn balance(state: &BundleState, address: Address) -> Option { + state.account(&address).and_then(|account| account.info.as_ref()).map(|info| info.balance) +} + +fn storage(state: &BundleState, address: Address, slot: U256) -> Option { + state.storage(&address, slot) +} + +#[test] +fn executes_prague_block() { + let mut executor = executor(SpecId::PRAGUE, 30_000_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + let gas = executor.execute_transaction(&transfer(21_000)).unwrap(); + assert_eq!(gas.tx_gas_used(), 21_000); + + let output = executor.finish().unwrap(); + let [receipt] = output.result.receipts.as_slice() else { panic!("expected one receipt") }; + assert!(receipt.status()); + assert_eq!(receipt.cumulative_gas_used(), 21_000); + assert_eq!(output.result.gas_used, 21_000); + assert!(output.result.requests.is_empty()); + + let state = &output.state; + assert_eq!(balance(state, RECIPIENT), Some(U256::from(1))); + // EIP-2935 stores the parent hash at slot `(number - 1) % 8191`. + let parent_hash = U256::from_be_bytes(PARENT_HASH.0); + assert_eq!(storage(state, HISTORY_STORAGE_ADDRESS, U256::ZERO), Some(parent_hash)); + // EIP-4788 stores the timestamp at `timestamp % 8191` and the root 8191 slots later. + let root = U256::from_be_bytes(PARENT_BEACON_BLOCK_ROOT.0); + assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12)), Some(U256::from(12))); + assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12 + 8191)), Some(root)); +} + +#[test] +fn rejects_cancun_block_without_beacon_root() { + let ctx = EthBlockExecutionCtx { parent_beacon_block_root: None, ..ctx() }; + let mut executor = executor(SpecId::CANCUN, 30_000_000, ctx, EthBlockRules::default()); + let error = executor.apply_pre_execution_changes().unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation(BlockValidationError::MissingParentBeaconBlockRoot) + )); +} + +#[test] +fn rejects_transaction_over_block_gas() { + let mut executor = executor(SpecId::PRAGUE, 30_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + executor.execute_transaction(&transfer(21_000)).unwrap(); + let error = executor.execute_transaction(&transfer(21_000)).unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation( + BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { + transaction_gas_limit: 21_000, + block_available_gas: 9_000, + } + ) + )); +} + +#[test] +fn discards_uncommitted_transaction() { + let mut executor = executor(SpecId::PRAGUE, 30_000_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + let gas = executor + .execute_transaction_with_commit_condition(&transfer(21_000), |_, _| CommitChanges::No) + .unwrap(); + assert_eq!(gas, None); + + let output = executor.finish().unwrap(); + assert!(output.result.receipts.is_empty()); + assert_eq!(balance(&output.state, RECIPIENT), None); +} + +#[test] +fn pays_block_and_ommer_rewards() { + let ommer_beneficiary = Address::repeat_byte(0x04); + let ommers = [Header { beneficiary: ommer_beneficiary, number: 0, ..Header::default() }]; + let ctx = EthBlockExecutionCtx { ommers: Cow::Borrowed(&ommers), ..ctx() }; + let reward = 2 * ETH_TO_WEI; + let rules = EthBlockRules { base_block_reward: Some(reward), ..EthBlockRules::default() }; + let mut executor = executor(SpecId::PETERSBURG, 30_000_000, ctx, rules); + executor.apply_pre_execution_changes().unwrap(); + + let output = executor.finish().unwrap(); + assert_eq!(balance(&output.state, BENEFICIARY), Some(U256::from(reward + reward / 32))); + // An ommer one block back earns 7/8 of the block reward. + assert_eq!(balance(&output.state, ommer_beneficiary), Some(U256::from(reward * 7 / 8))); +} + +#[test] +fn applies_withdrawals_from_shanghai() { + let withdrawals = [Withdrawal { address: RECIPIENT, amount: 5, ..Withdrawal::default() }]; + let ctx = EthBlockExecutionCtx { withdrawals: Some(Cow::Borrowed(&withdrawals)), ..ctx() }; + let mut shanghai = + executor(SpecId::SHANGHAI, 30_000_000, ctx.clone(), EthBlockRules::default()); + shanghai.apply_pre_execution_changes().unwrap(); + let output = shanghai.finish().unwrap(); + assert_eq!(balance(&output.state, RECIPIENT), Some(U256::from(5_000_000_000u64))); + + let mut merge = executor(SpecId::MERGE, 30_000_000, ctx, EthBlockRules::default()); + merge.apply_pre_execution_changes().unwrap(); + let output = merge.finish().unwrap(); + assert_eq!(balance(&output.state, RECIPIENT), None); +} + +#[test] +fn reads_rules_from_hardforks() { + let mainnet = alloy_hardforks::EthereumChainHardforks::mainnet(); + let dao = EthBlockRules::new(&mainnet, 1_920_000, None); + assert_eq!(dao.base_block_reward, Some(5 * ETH_TO_WEI)); + assert!(dao.dao_fork_transition); + let merged = EthBlockRules::new(&mainnet, 15_537_394, None); + assert_eq!(merged, EthBlockRules::default()); +} + +#[test] +fn rejects_request_contract_without_code() { + let mut database = database(); + database.insert_account_info(&WITHDRAWAL_REQUEST_ADDRESS, AccountInfo::default()); + let evm = evm_with(SpecId::PRAGUE, 30_000_000, database); + let mut executor = + EthBlockExecutor::new(evm, ctx(), EthBlockRules::default(), EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + let error = executor.finish().unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation(BlockValidationError::SystemContractEmpty { address }) + if address == WITHDRAWAL_REQUEST_ADDRESS + )); +} + +#[test] +fn rejects_transaction_over_block_blob_gas() { + let rules = EthBlockRules { max_blob_gas_per_block: Some(0), ..EthBlockRules::default() }; + let mut executor = executor(SpecId::CANCUN, 30_000_000, ctx(), rules); + executor.apply_pre_execution_changes().unwrap(); + let error = executor.execute_transaction(&blob_transfer()).unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation( + BlockValidationError::TransactionBlobGasMoreThanAvailableBlockBlobGas { + transaction_blob_gas: 131_072, + block_available_blob_gas: 0, + } + ) + )); +} + +#[test] +fn drains_dao_accounts_before_transactions() { + let drained = dao_fork::DAO_HARDFORK_ACCOUNTS[0]; + let mut database = database(); + let balance_before = AccountInfo { balance: U256::from(7), nonce: 1, ..AccountInfo::default() }; + database.insert_account_info(&drained, balance_before); + let evm = evm_with(SpecId::HOMESTEAD, 30_000_000, database); + let rules = EthBlockRules { dao_fork_transition: true, ..EthBlockRules::default() }; + let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + + let state = executor.into_state(); + assert_eq!(balance(&state, drained), Some(U256::ZERO)); + assert_eq!(balance(&state, dao_fork::DAO_HARDFORK_BENEFICIARY), Some(U256::from(7))); + // Drained accounts stay, empty ones and absent ones included. + assert_eq!(balance(&state, dao_fork::DAO_HARDFORK_ACCOUNTS[1]), Some(U256::ZERO)); +} + +#[test] +fn zero_reward_removes_empty_account_from_spurious_dragon() { + let mut database = database(); + database.insert_account_info(&BENEFICIARY, AccountInfo::default()); + let rules = EthBlockRules { base_block_reward: Some(0), ..EthBlockRules::default() }; + let run = |spec| { + let evm = evm_with(spec, 30_000_000, database.clone()); + let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + executor.finish().unwrap().state + }; + assert!(run(SpecId::SPURIOUS_DRAGON).account(&BENEFICIARY).unwrap().info.is_none()); + assert!(run(SpecId::TANGERINE).account(&BENEFICIARY).is_none()); +} diff --git a/crates/evm2/src/block/mod.rs b/crates/evm2/src/block/mod.rs new file mode 100644 index 000000000..edf6f27cb --- /dev/null +++ b/crates/evm2/src/block/mod.rs @@ -0,0 +1,311 @@ +//! Block execution. +//! +//! [`BlockExecutor`] executes the transactions of one block on an [`Evm`](crate::Evm), applies +//! the block's pre- and post-execution changes, and collects receipts and the block's +//! [`BundleState`]. [`ethereum::EthBlockExecutor`] implements it with Ethereum's block rules. + +use crate::{EvmTypes, EvmTypesHost, TxResult, TxResultWithState, TxStateChanges, evm::Bal}; +use alloc::{sync::Arc, vec::Vec}; +use alloy_consensus::transaction::{Recovered, TransactionEnvelope}; +use alloy_eip7928::BlockAccessIndex; +use alloy_eips::eip7685::Requests; +use alloy_primitives::Log; +use core::borrow::Borrow; + +pub mod bundle_state; +pub use bundle_state::BundleState; + +mod error; +pub use error::{ + BlockExecutionError, BlockValidationError, InternalBlockExecutionError, InvalidTxError, + map_database_error, map_handler_error, +}; + +pub mod ethereum; + +mod state; +pub use state::{AccountUpdate, BlockState, BundleSource, StateUpdate, TransactionChanges}; + +/// The transaction environment executed by a [`BlockExecutor`]. +pub type EvmTxOf = <::EvmTypes as EvmTypesHost>::Tx; + +/// Gas used by a successfully executed transaction. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct GasOutput { + /// Gas used for receipt accounting. + tx_gas_used: u64, + /// Regular gas used for Amsterdam block-capacity accounting. + regular_gas_used: u64, + /// Gas used for state-capacity accounting. + state_gas_used: u64, +} + +impl GasOutput { + /// Creates a new gas output. + pub const fn new(tx_gas_used: u64, state_gas_used: u64) -> Self { + Self { tx_gas_used, regular_gas_used: tx_gas_used, state_gas_used } + } + + /// Creates a gas output with independent receipt, regular, and state gas accounting. + pub const fn new_with_regular( + tx_gas_used: u64, + regular_gas_used: u64, + state_gas_used: u64, + ) -> Self { + Self { tx_gas_used, regular_gas_used, state_gas_used } + } + + /// Returns transaction gas used for receipt accounting. + pub const fn tx_gas_used(&self) -> u64 { + self.tx_gas_used + } + + /// Returns regular gas used for Amsterdam block-capacity accounting. + pub const fn regular_gas_used(&self) -> u64 { + self.regular_gas_used + } + + /// Returns state gas used for state-capacity accounting. + pub const fn state_gas_used(&self) -> u64 { + self.state_gas_used + } +} + +impl From for GasOutput { + fn from(gas_used: u64) -> Self { + Self::new(gas_used, gas_used) + } +} + +/// Whether a block executor commits an executed transaction. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CommitChanges { + /// Transaction should be committed into block executor's state. + Yes, + /// Transaction should not be committed. + No, +} + +impl CommitChanges { + /// Returns `true` if transaction should be committed into block executor's state. + pub const fn should_commit(self) -> bool { + matches!(self, Self::Yes) + } +} + +/// Context for building a transaction receipt from an execution result. +#[derive(Clone, Debug)] +pub struct ReceiptBuilderCtx { + /// Transaction type used by the receipt. + pub tx_type: TxType, + /// Raw transaction execution result. + pub result: TransactionResult, + /// Cumulative gas used by this transaction and all prior transactions in the block. + pub cumulative_gas_used: u64, +} + +/// Builds chain-specific receipts from raw transaction execution results. +#[auto_impl::auto_impl(&, Arc)] +pub trait ReceiptBuilder { + /// Consensus transaction type accepted by the executor. + type Transaction: TransactionEnvelope; + /// Receipt produced by this builder. + type Receipt: alloy_consensus::TxReceipt; + + /// Builds a receipt for the transaction execution result. + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx<::TxType, TxResult>, + ) -> Self::Receipt; +} + +/// A transaction a [`BlockExecutor`] can execute: the EVM transaction environment with its +/// recovered signer, and the consensus transaction it was built from. +pub trait ExecutableTx { + /// Consensus transaction accessor. + type Tx: Borrow; + + /// Splits the transaction into its EVM environment and consensus transaction. + fn into_parts(self) -> (Recovered>, Self::Tx); +} + +impl ExecutableTx for (Recovered>, Tx) +where + E: BlockExecutor, + Tx: Borrow, +{ + type Tx = Tx; + + fn into_parts(self) -> (Recovered>, Tx) { + self + } +} + +impl<'a, E> ExecutableTx for &'a Recovered +where + E: BlockExecutor, + EvmTxOf: From, +{ + type Tx = &'a E::Transaction; + + fn into_parts(self) -> (Recovered>, &'a E::Transaction) { + let tx = Recovered::new_unchecked(self.inner().clone().into(), self.signer()); + (tx, self.inner()) + } +} + +/// A detached block transaction output containing its raw execution result. +pub trait BlockTransactionResult { + /// Returns the raw transaction execution result. + fn result(&self) -> &TxResultWithState; +} + +/// The result of executing a block. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct BlockExecutionResult { + /// All the receipts of the transactions in the block. + pub receipts: Vec, + /// All the EIP-7685 requests in the block. + pub requests: Requests, + /// The total gas used by the block. + pub gas_used: u64, + /// Blob gas used by the block. + pub blob_gas_used: u64, +} + +impl Default for BlockExecutionResult { + fn default() -> Self { + Self { receipts: Vec::new(), requests: Requests::default(), gas_used: 0, blob_gas_used: 0 } + } +} + +/// [`BlockExecutionResult`] combined with state. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct BlockExecutionOutput { + /// All the receipts of the transactions in the block. + pub result: BlockExecutionResult, + /// The changed state of the block after execution. + pub state: BundleState, +} + +impl BlockExecutionOutput { + /// Combines execution results with their persistent state. + pub const fn new(result: BlockExecutionResult, state: BundleState) -> Self { + Self { result, state } + } +} + +/// A configured block executor. +pub trait BlockExecutor: Sized { + /// Consensus transaction type executed by this executor. + type Transaction; + /// Receipt type produced by this executor. + type Receipt; + /// Runtime EVM type family. + type EvmTypes: EvmTypes; + /// EVM instance used by this executor. + type Evm; + /// Owned transaction execution result and detached state changes. + type TransactionResultWithState: BlockTransactionResult; + + /// Returns the underlying EVM. + fn evm(&self) -> &Self::Evm; + + /// Returns the underlying EVM mutably. + fn evm_mut(&mut self) -> &mut Self::Evm; + + /// Sets a hook for state updates emitted during block execution, including pre-execution + /// block changes and transaction changes. + /// + /// Returns `true` if the hook was installed. + fn set_state_hook(&mut self, _hook: impl FnMut(StateUpdate) + Send + 'static) -> bool { + false + } + + /// Attaches an EIP-7928 block access list for indexed state reads. + fn set_block_access_list(&mut self, block_access_list: Arc); + + /// Sets the EIP-7928 block access index used by state reads and BAL construction. + fn set_block_access_index(&mut self, index: BlockAccessIndex); + + /// Enables construction of an EIP-7928 block access list from committed state changes. + fn enable_block_access_list_builder(&mut self); + + /// Takes the block access list built from committed state changes. + fn take_block_access_list(&mut self) -> Option; + + /// Applies pre-execution block changes. + fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError>; + + /// Consumes the executor and returns the state accumulated so far without applying + /// post-execution block changes. + fn into_state(self) -> BundleState; + + /// Executes a transaction, invokes `f` with the borrowed execution result and state changes, + /// and commits changes when `f` returns [`CommitChanges::Yes`]. + fn execute_transaction_with_commit_condition( + &mut self, + tx: impl ExecutableTx, + f: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, + ) -> Result, BlockExecutionError> { + let output = self.execute_transaction_without_commit(tx)?; + let result = output.result(); + if f(&result.result, result.pending_state.changes()).should_commit() { + self.commit_transaction(output).map(Some) + } else { + Ok(None) + } + } + + /// Validates transaction gas admission against the executor's current block budget. + fn validate_transaction_gas_limit(&mut self, gas_limit: u64) + -> Result<(), BlockExecutionError>; + + /// Executes a transaction and detaches its state changes without committing them. + fn execute_transaction_without_commit( + &mut self, + tx: impl ExecutableTx, + ) -> Result; + + /// Commits detached transaction state and records its receipt and gas accounting. + fn commit_transaction( + &mut self, + output: Self::TransactionResultWithState, + ) -> Result; + + /// Executes a transaction, invokes `f` with the borrowed execution result, and commits + /// changes. + fn execute_transaction_with_result_closure( + &mut self, + tx: impl ExecutableTx, + f: impl FnOnce(&TxResult), + ) -> Result { + self.execute_transaction_with_commit_condition(tx, |result, _| { + f(result); + CommitChanges::Yes + }) + .map(Option::unwrap_or_default) + } + + /// Executes a transaction and commits changes. + fn execute_transaction( + &mut self, + tx: impl ExecutableTx, + ) -> Result { + self.execute_transaction_with_result_closure(tx, |_| {}) + } + + /// Returns receipts accumulated so far. + fn receipts(&self) -> &[Self::Receipt]; + + /// Finishes block execution and returns the output with the block access list built during + /// execution, if its builder was enabled. + fn finish_with_block_access_list( + self, + ) -> Result<(BlockExecutionOutput, Option), BlockExecutionError>; + + /// Finishes block execution and returns the output. + fn finish(self) -> Result, BlockExecutionError> { + self.finish_with_block_access_list().map(|(output, _)| output) + } +} diff --git a/crates/evm2/src/block/state.rs b/crates/evm2/src/block/state.rs new file mode 100644 index 000000000..820e19c8d --- /dev/null +++ b/crates/evm2/src/block/state.rs @@ -0,0 +1,705 @@ +//! Block state accumulated from committed transactions. + +use super::bundle_state::{ + AccountStatus, BundleRetention, BundleState, StorageSlot, TransitionAccount, TransitionState, +}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::vec::Vec; +use alloy_primitives::{ + Address, B256, U256, + map::{AddressMap, B256Map, U256Map, hash_map::Entry}, +}; + +/// One block's account transitions, finalized into a [`BundleState`] with their reverts. +#[derive(Debug, Default)] +pub struct BlockState { + transitions: AddressMap, + contracts: B256Map, +} + +impl BlockState { + /// Creates an empty block accumulator. + pub fn new() -> Self { + Self::default() + } + + /// Applies a collected transaction to the block's transitions. + pub fn commit(&mut self, changes: &TransactionChanges) { + self.contracts.extend(changes.contracts.iter().map(|(hash, code)| (*hash, code.clone()))); + for (address, account) in &changes.accounts { + self.commit_account( + *address, + account.original.clone(), + (!account.selfdestructed).then(|| account.info.clone()), + account.created, + account.wiped, + account.storage.iter().filter_map(|(key, slot)| { + // A wipe reinserts every nonzero value that follows it. + (slot.is_changed() || account.wiped && !slot.present_value().is_zero()) + .then_some((*key, *slot)) + }), + ); + } + } + + /// Commits one account's finalized evm2 transaction changes into the block without + /// materializing EVM state. Accounts that were only loaded are skipped. + /// + /// When `update` is set, the committed account is also recorded into it for execution state + /// hooks. + pub fn commit_account_changes( + &mut self, + changes: crate::evm::AccountChanges<'_>, + update: Option<&mut StateUpdate>, + ) { + if !changes.is_changed() { + return; + } + if let Some((hash, code)) = changes.code { + self.contracts.entry(hash).or_insert_with(|| code.clone()); + } + let address = changes.address; + let created = changes.created; + let wiped = changes.storage.wiped; + let original = changes.original.map(AccountInfo::clone_no_code); + let current = changes.current.map(AccountInfo::clone_no_code); + let slots = changes + .storage + .changed_slots() + .map(|(&key, value)| (key, StorageSlot::new_changed(value.original, value.current))); + let Some(update) = update else { + self.commit_account(address, original, current, created, wiped, slots); + return; + }; + let storage: Vec<_> = slots.collect(); + self.commit_account( + address, + original.clone(), + current.clone(), + created, + wiped, + storage.iter().copied(), + ); + update.accounts.push(AccountUpdate { address, original, current, created, wiped, storage }); + } + + fn commit_account( + &mut self, + address: Address, + original: Option, + current: Option, + created: bool, + wiped: bool, + storage: impl Iterator, + ) { + let entry = self.transitions.entry(address); + let previous_status = match &entry { + Entry::Occupied(entry) => entry.get().status, + Entry::Vacant(_) => loaded_status(original.as_ref()), + }; + let status = if current.is_none() { + previous_status.on_selfdestructed() + } else if wiped { + AccountStatus::DestroyedChanged + } else if created { + previous_status.on_created() + } else { + previous_status.on_changed( + original.as_ref().is_none_or(|i| i.nonce == 0 && i.is_empty_code_hash()), + ) + }; + match entry { + Entry::Vacant(entry) => { + entry.insert(TransitionAccount { + info: current, + status, + previous_info: original, + previous_status, + storage: storage.collect(), + storage_was_destroyed: wiped, + }); + } + Entry::Occupied(mut entry) => { + let account = entry.get_mut(); + account.info = current; + account.status = status; + // A deletion or wipe starts a new storage lifetime. + if wiped + || matches!(status, AccountStatus::Destroyed | AccountStatus::DestroyedAgain) + { + account.storage.clear(); + account.storage.extend(storage); + account.storage_was_destroyed = true; + } else { + // After a wipe, every nonzero value is reinserted, so a slot that returns to + // its original value stays. + let wiped_lifetime = account.storage_was_destroyed || status.was_destroyed(); + for (key, slot) in storage { + match account.storage.entry(key) { + Entry::Vacant(entry) => { + entry.insert(slot); + } + Entry::Occupied(mut entry) => { + if !wiped_lifetime + && entry.get().original_value() == slot.present_value() + { + entry.remove(); + } else { + entry.get_mut().present_value = slot.present_value(); + } + } + } + } + } + } + } + } + + /// Finalizes one block's transitions and reverts. + pub fn into_bundle(self) -> BundleState { + self.into_bundle_with_retention(BundleRetention::Reverts) + } + + /// Finalizes one block's transitions, creating reverts only if `retention` asks for them. + pub fn into_bundle_with_retention(self, retention: BundleRetention) -> BundleState { + let mut bundle = BundleState::default(); + bundle.apply_transitions_and_create_reverts( + TransitionState { transitions: self.transitions }, + retention, + ); + bundle.contracts.extend(self.contracts); + bundle + } +} + +/// Transaction changes collected from an evm2 change stream for [`BlockState::commit`]. +#[derive(Debug, Default)] +pub struct TransactionChanges { + accounts: AddressMap, + contracts: B256Map, +} + +/// One account's changes within a [`TransactionChanges`]. +#[derive(Debug)] +struct TransactionAccount { + /// Account at the start of the transaction, without code. `None` means it did not exist. + original: Option, + /// Account after the transaction, without code. + info: AccountInfo, + created: bool, + selfdestructed: bool, + wiped: bool, + storage: U256Map, +} + +impl Default for TransactionAccount { + fn default() -> Self { + Self { + original: Some(AccountInfo::empty()), + info: AccountInfo::empty(), + created: false, + selfdestructed: false, + wiped: false, + storage: U256Map::default(), + } + } +} + +impl TransactionAccount { + fn update(&mut self, change: crate::evm::AccountChangeRef<'_>) { + self.original = change.original.map(AccountInfo::clone_no_code); + self.info = + change.current.map(AccountInfo::clone_no_code).unwrap_or_else(AccountInfo::empty); + self.created |= change.created; + self.selfdestructed |= change.current.is_none(); + } +} + +impl crate::evm::StateChangeSink for TransactionChanges { + type Error = core::convert::Infallible; + fn bytecode(&mut self, hash: B256, code: &Bytecode) -> Result<(), Self::Error> { + self.contracts.entry(hash).or_insert_with(|| code.clone()); + Ok(()) + } + fn storage_wipe(&mut self, address: Address) -> Result<(), Self::Error> { + let account = self.accounts.entry(address).or_default(); + account.wiped = true; + account.storage.clear(); + Ok(()) + } + fn storage(&mut self, change: crate::evm::StorageChange) -> Result<(), Self::Error> { + self.accounts + .entry(change.address) + .or_default() + .storage + .insert(change.key, StorageSlot::new_changed(change.original, change.current)); + Ok(()) + } + fn account(&mut self, change: crate::evm::AccountChangeRef<'_>) -> Result<(), Self::Error> { + self.accounts.entry(change.address).or_default().update(change); + Ok(()) + } + fn account_read( + &mut self, + address: Address, + info: Option<&AccountInfo>, + ) -> Result<(), Self::Error> { + if let Some(account) = self.accounts.get_mut(&address) { + account.update(crate::evm::AccountChangeRef { + address, + original: info, + current: info, + created: false, + selfdestructed: false, + }); + } + Ok(()) + } +} + +/// Per-transaction state changes streamed to execution state hooks. +/// +/// Holds only the accounts a transaction committed, with account info but without bytecode, so +/// building it does not copy the transaction's state maps. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct StateUpdate { + /// Committed accounts in stream order. + pub accounts: Vec, +} + +impl StateUpdate { + /// Returns whether the update commits no accounts. + pub const fn is_empty(&self) -> bool { + self.accounts.is_empty() + } + + /// Returns the update for `address`, if the transaction committed it. + pub fn account(&self, address: &Address) -> Option<&AccountUpdate> { + self.accounts.iter().find(|account| account.address == *address) + } +} + +/// One committed account in a [`StateUpdate`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AccountUpdate { + /// Account address. + pub address: Address, + /// Account at the start of the transaction, without code. `None` means it did not exist. + pub original: Option, + /// Account after the transaction, without code. `None` is a deletion. + pub current: Option, + /// Whether the account was created during the transaction. + pub created: bool, + /// Whether the account's prior storage was wiped. + pub wiped: bool, + /// Storage slots whose value changed. + pub storage: Vec<(U256, StorageSlot)>, +} + +/// Visits a persisted bundle as native changes when seeding an execution cache. +#[derive(Debug)] +pub struct BundleSource<'a>(pub &'a BundleState); + +impl crate::evm::StateChangeSource for BundleSource<'_> { + fn visit(&self, sink: &mut S) -> Result<(), S::Error> { + for (hash, code) in &self.0.contracts { + sink.bytecode(*hash, code)?; + } + for (address, account) in &self.0.state { + let current = account.info.as_ref(); + if account.status.was_destroyed() { + sink.storage_wipe(*address)?; + } + sink.account(crate::evm::AccountChangeRef { + address: *address, + original: account.original_info.as_ref(), + current, + created: account.status.is_storage_known() && current.is_some(), + selfdestructed: current.is_none(), + })?; + for (key, value) in &account.storage { + sink.storage(crate::evm::StorageChange { + address: *address, + key: *key, + original: value.original_value(), + current: value.present_value(), + })?; + } + } + Ok(()) + } +} + +fn loaded_status(info: Option<&AccountInfo>) -> AccountStatus { + match info { + None => AccountStatus::LoadedNotExisting, + Some(info) if info.is_empty() => AccountStatus::LoadedEmptyEIP161, + Some(_) => AccountStatus::Loaded, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + block::bundle_state::OriginalValuesKnown, + evm::{ + AccountChangeRef, AccountChanges, StateChangeSink, StorageChange, StorageOverlay, + StorageSlot as NativeSlot, Tracked, + }, + }; + + /// Builds a storage overlay from `(key, original, current)` slots. + fn overlay(wiped: bool, slots: &[(U256, U256, U256)]) -> StorageOverlay { + let mut storage = StorageOverlay { wiped, ..Default::default() }; + for &(key, original, current) in slots { + let slot = + NativeSlot { value: Tracked::from_parts(original, current), ..Default::default() }; + storage.slots.insert(key, slot); + } + storage + } + + #[cfg(feature = "account-ext")] + #[test] + fn account_extensions_survive_native_updates_and_reverts() { + let address = Address::with_last_byte(1); + let original = AccountInfo { + balance: U256::from(10), + extension: crate::evm::AccountExtension::copy_from_slice(b"original extension"), + ..Default::default() + }; + let mut updated = original.clone(); + updated.balance = U256::from(20); + updated.extension = crate::evm::AccountExtension::copy_from_slice(b"updated extension"); + let mut state = BlockState::new(); + state.commit_account_changes( + AccountChanges { + address, + original: Some(&original), + current: Some(&updated), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &StorageOverlay::default(), + }, + None, + ); + let mut bundle = state.into_bundle(); + let account = bundle.state.get(&address).unwrap(); + assert_eq!( + account.original_info.as_ref().unwrap().extension.as_ref(), + b"original extension" + ); + assert_eq!(account.info.as_ref().unwrap().extension.as_ref(), b"updated extension"); + assert!(bundle.revert_latest()); + assert_eq!( + bundle.state.get(&address).unwrap().info.as_ref().unwrap().extension.as_ref(), + b"original extension" + ); + } + + #[test] + fn native_sink_preserves_storage_lifetimes_and_reverts() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let info = AccountInfo::default().with_nonce(1).with_code(code.clone()); + let mut converted = BlockState::new(); + let mut native = BlockState::new(); + // Storage-only writes, restoration, deletion, recreation, a surviving storage wipe, + // and another deletion exercise both occupied and vacant transition entries. + for step in 0..7 { + let slots: &[_] = if step < 2 || step == 3 || step == 4 { + let (original, current) = if step == 1 { (8, 7) } else { (7, 8) }; + &[(U256::from(3), U256::from(original), U256::from(current))] + } else { + &[] + }; + let storage = overlay(step >= 2, slots); + let changes = if step < 2 { + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + } + } else { + AccountChanges { + address, + original: (step != 3 && step != 6).then_some(&info), + current: (step == 3 || step == 4).then_some(&info), + created: step == 3, + selfdestructed: step != 3, + touched: false, + code: (step == 3).then(|| (code.hash_slow(), &code)), + storage: &storage, + } + }; + let empty = StorageOverlay::default(); + // Reads alone must never introduce an account transition. + let read = AccountChanges { + address: Address::with_last_byte(2), + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &empty, + }; + let mut transaction = TransactionChanges::default(); + let Ok(()) = changes.visit(&mut transaction); + let Ok(()) = read.visit(&mut transaction); + converted.commit(&transaction); + native.commit_account_changes(changes, None); + native.commit_account_changes(read, None); + assert_eq!(native.transitions, converted.transitions, "step {step}"); + assert_eq!(native.contracts, converted.contracts); + } + let mut native = native.into_bundle(); + let mut converted = converted.into_bundle(); + assert_eq!(native, converted); + assert!(native.revert_latest()); + assert!(converted.revert_latest()); + assert_eq!(native, converted); + } + + #[test] + fn stream_commit_keeps_unchanged_slot_after_wipe() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut changes = TransactionChanges::default(); + changes.storage_wipe(address).unwrap(); + changes + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::from(5), + current: U256::from(5), + }) + .unwrap(); + changes.account_read(address, Some(&info)).unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let bundle = block.into_bundle(); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(5))); + } + + #[test] + fn created_account_keeps_its_storage_wipe() { + let address = Address::with_last_byte(1); + let info = AccountInfo { balance: U256::from(1), ..Default::default() }; + let storage = overlay(true, &[]); + let mut block = BlockState::new(); + block.commit_account_changes( + AccountChanges { + address, + original: None, + current: Some(&info), + created: true, + selfdestructed: true, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + let bundle = block.into_bundle(); + assert!(bundle.account(&address).unwrap().was_destroyed()); + let changeset = bundle.to_plain_state(OriginalValuesKnown::Yes); + assert!(changeset.storage.iter().any(|storage| storage.wipe_storage)); + } + + #[test] + fn slot_returning_to_original_after_wipe_is_reinserted() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut block = BlockState::new(); + for (wiped, slot) in [ + (true, (U256::from(3), U256::from(5), U256::from(6))), + (false, (U256::from(3), U256::from(6), U256::from(5))), + ] { + let storage = overlay(wiped, &[slot]); + block.commit_account_changes( + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + } + let bundle = block.into_bundle(); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(5))); + } + + #[test] + fn live_account_wipe_drops_earlier_slots() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut block = BlockState::new(); + for (wiped, slots) in [ + (false, &[(U256::from(3), U256::ZERO, U256::from(1))][..]), + (true, &[(U256::from(4), U256::ZERO, U256::from(2))][..]), + ] { + let storage = overlay(wiped, slots); + block.commit_account_changes( + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + } + let bundle = block.into_bundle(); + let account = bundle.account(&address).unwrap(); + assert_eq!(account.storage_slot(U256::from(3)), Some(U256::ZERO)); + assert_eq!(account.storage_slot(U256::from(4)), Some(U256::from(2))); + } + + #[test] + fn cached_bytecode_does_not_change_bundle_output() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let info = AccountInfo { + balance: U256::from(5), + code_hash: code.hash_slow(), + ..Default::default() + }; + let bundle = |info: &AccountInfo| { + let mut changes = TransactionChanges::default(); + changes + .account(AccountChangeRef { + address, + original: None, + current: Some(info), + created: false, + selfdestructed: false, + }) + .unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + block.into_bundle() + }; + let uncached = bundle(&info); + let cached = bundle(&AccountInfo { code: Some(code), ..info }); + assert_eq!(uncached, cached); + assert!(cached.contracts.is_empty()); + } + + #[test] + fn deployed_bytecode_is_retained_separately_from_account_metadata() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let hash = code.hash_slow(); + let info = AccountInfo { + nonce: 1, + code_hash: hash, + code: Some(code.clone()), + ..Default::default() + }; + let mut changes = TransactionChanges::default(); + changes.bytecode(hash, &code).unwrap(); + changes + .account(AccountChangeRef { + address, + original: None, + current: Some(&info), + created: true, + selfdestructed: false, + }) + .unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let bundle = block.into_bundle(); + assert_eq!(bundle.contracts[&hash].original_bytes(), code.original_bytes()); + assert_eq!(bundle.account(&address).unwrap().info.as_ref().unwrap().code_hash, hash); + } + + #[test] + fn storage_only_change_retains_account_and_reverts() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut changes = TransactionChanges::default(); + changes + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::from(7), + current: U256::from(8), + }) + .unwrap(); + changes.account_read(address, Some(&info)).unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let mut bundle = block.into_bundle(); + assert_eq!(bundle.account(&address).unwrap().info.as_ref().unwrap().nonce, 1); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(8))); + assert_eq!(bundle.reverts.len(), 1); + assert!(bundle.revert_latest()); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(7))); + } + + #[test] + fn destroy_then_recreate_retains_parent_storage_wipe() { + let address = Address::with_last_byte(1); + let original = AccountInfo { nonce: 1, balance: U256::from(7), ..Default::default() }; + let recreated = AccountInfo { nonce: 1, balance: U256::from(9), ..Default::default() }; + let mut block = BlockState::new(); + let mut destroyed = TransactionChanges::default(); + destroyed + .account(AccountChangeRef { + address, + original: Some(&original), + current: None, + created: false, + selfdestructed: true, + }) + .unwrap(); + block.commit(&destroyed); + let mut created = TransactionChanges::default(); + created.storage_wipe(address).unwrap(); + created + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::ZERO, + current: U256::from(8), + }) + .unwrap(); + created + .account(AccountChangeRef { + address, + original: None, + current: Some(&recreated), + created: true, + selfdestructed: false, + }) + .unwrap(); + block.commit(&created); + let bundle = block.into_bundle(); + let account = bundle.account(&address).unwrap(); + assert!(account.was_destroyed()); + assert_eq!(account.original_info.as_ref().unwrap().balance, U256::from(7)); + assert_eq!(account.info.as_ref().unwrap().balance, U256::from(9)); + assert_eq!(account.storage_slot(U256::from(4)), Some(U256::ZERO)); + assert_eq!(account.storage_slot(U256::from(3)), Some(U256::from(8))); + assert!(bundle.reverts[0][0].1.wipe_storage); + } +} diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index c4502de64..04841b159 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -11,6 +11,8 @@ extern crate alloc; #[cfg(feature = "nightly")] use ruint as _; +#[cfg(feature = "block")] +pub mod block; pub mod bytecode; pub mod constants; mod error; From 8f42a4ff67af17fa2472aaadd278504986f77d80 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:58:40 +0100 Subject: [PATCH 104/159] fix(inspectors): keep the serde error when a tracer config is rejected (#1002) `DebugInspector::new` discarded the `serde_json::Error` from the built-in tracer config parsers (call, prestate, mux, flat call, ERC-7562), so an RPC caller who sent a bad config got only "invalid tracer config". `InvalidTracerConfig` now carries the error and prints the reason, as `MuxError::InvalidConfig` already does for mux children. Adds a test that `{"onlyTopCall": "yes"}` yields `InvalidTracerConfig` with the serde message. --- crates/inspectors/src/tracing/debug.rs | 24 +++++++----------------- crates/inspectors/tests/it/geth.rs | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index 16cecf67a..ed9367fb7 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -96,9 +96,7 @@ impl DebugInspector { Self::FourByte(FourByteInspector::default()) } GethDebugBuiltInTracerType::CallTracer => { - let config = tracer_config - .into_call_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_call_config()?; Self::CallTracer( TracingInspector::new(TracingInspectorConfig::from_geth_call_config( @@ -108,9 +106,7 @@ impl DebugInspector { ) } GethDebugBuiltInTracerType::PreStateTracer => { - let config = tracer_config - .into_pre_state_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_pre_state_config()?; Self::PreStateTracer( TracingInspector::new( @@ -124,16 +120,12 @@ impl DebugInspector { Self::StateGasTracer(NoopInspector::default()) } GethDebugBuiltInTracerType::MuxTracer => { - let config = tracer_config - .into_mux_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_mux_config()?; Self::Mux(MuxInspector::try_from_config(config.clone())?, config) } GethDebugBuiltInTracerType::FlatCallTracer => { - let flat_call_config = tracer_config - .into_flat_call_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let flat_call_config = tracer_config.into_flat_call_config()?; Self::FlatCallTracer(TracingInspector::new( TracingInspectorConfig::from_flat_call_config(&flat_call_config), @@ -143,9 +135,7 @@ impl DebugInspector { let config = if tracer_config.is_null() { Erc7562Config::default() } else { - tracer_config - .from_value() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)? + tracer_config.from_value()? }; Self::Erc7562Tracer(Erc7562Inspector::new(config)) @@ -424,8 +414,8 @@ impl Inspector for DebugInspector { #[derive(Debug, Error)] pub enum DebugInspectorError { /// Invalid tracer configuration - #[error("invalid tracer config")] - InvalidTracerConfig, + #[error("invalid tracer config: {0}")] + InvalidTracerConfig(#[from] serde_json::Error), /// Unsupported tracer #[error("unsupported tracer")] UnsupportedTracer, diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 9aa328629..59abc4742 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -271,6 +271,20 @@ fn test_debug_nonempty_tracer() { } } +#[test] +fn test_debug_invalid_tracer_config() { + let opts = serde_json::from_value( + serde_json::json!({"tracer": "callTracer", "tracerConfig": {"onlyTopCall": "yes"}}), + ) + .unwrap(); + let err = DebugInspector::new(opts).unwrap_err(); + assert!(matches!(err, evm2_inspectors::tracing::DebugInspectorError::InvalidTracerConfig(_))); + assert_eq!( + err.to_string(), + r#"invalid tracer config: invalid type: string "yes", expected a boolean"# + ); +} + #[test] fn test_geth_calltracer_logs() { /* From 2f24ea152a2703f19a0492a199777a4d0252be68 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:59:46 +0100 Subject: [PATCH 105/159] refactor(evm): drop redundant HostError map_err calls (#1003) Removes four `.map_err(HostError::from)` calls in `crates/evm2/src/evm/mod.rs`. Each callee returns `DbResult<_>` and each enclosing function returns `Result<_, HostError>`, so `?` already performs the conversion through `impl From for HostError`. No behavior change. --- crates/evm2/src/evm/mod.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index ea9b467b3..32632ef9d 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1510,7 +1510,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { fn prepare_create_message(&mut self, message: &mut Message) -> Result<(), HostError> { let nested = message.depth > self.version().initial_call_depth; let info = if message.value > 0 || nested { - self.state.account_info_untracked(&message.caller).map_err(HostError::from)? + self.state.account_info_untracked(&message.caller)? } else { None }; @@ -1539,9 +1539,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn create_message_account(&mut self, message: &Message) -> Result<(), HostError> { - self.state - .create_account(&message.caller, &message.destination, &message.value, self.features) - .map_err(HostError::from)??; + self.state.create_account( + &message.caller, + &message.destination, + &message.value, + self.features, + )??; self.log_eip7708_transfer(&message.caller, &message.destination, &message.value); Ok(()) @@ -1849,11 +1852,7 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { }; // load code - let code = if load_code { - account.load_code().map_err(HostError::from)? - } else { - Bytecode::default() - }; + let code = if load_code { account.load_code()? } else { Bytecode::default() }; Ok(AccountLoad { balance, nonce, @@ -1981,8 +1980,7 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { }; if contract != target { - let transferred = - self.state.transfer(contract, target, &balance).map_err(HostError::from)?; + let transferred = self.state.transfer(contract, target, &balance)?; if transferred { self.log_eip7708_transfer(contract, target, &balance); } From ea93dbd4462ad68fda461fc7636cc9c6c4a3464a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:04:31 +0100 Subject: [PATCH 106/159] refactor: apply slapslop duplication hits in inspectors, eest and cli (#1015) Replaces hand-rolled code in the inspectors, eest and cli crates with helpers that already exist, as flagged by slapslop. In the inspectors it uses `WordExt::to_address`, `AccountInfo::new`, `TxEnvelope::from(TxEip7702)`, the `TracingInspectorConfig` builder shorthands, `execute_transaction_without_commit`, one shared `type_error`, and one `transaction` test helper in place of copies. In eest it keeps one copy of `print_db_stats`, one explicit-root lookup, one ignore-list match and one `has_extension`. In cli it uses `b256_to_word`, `u32::from(SpecId)`, one bench `workspace_path`, and a named struct instead of the five-element tuple in `run.rs`. The change adds no public item and changes no public signature, and it leaves constants as they are. The inspectors tests gain a dev-dependency on `evm2/block` for `execute_transaction_without_commit`. --- crates/cli/benches/evm/fixture.rs | 2 +- crates/cli/benches/evm/mainnet.rs | 11 +---- crates/cli/benches/evm/mainnet_revm.rs | 11 +---- crates/cli/src/capture/export.rs | 6 +-- crates/cli/src/fuzzer/case.rs | 27 ++++++------- crates/cli/src/fuzzer/rng.rs | 2 +- crates/cli/src/run.rs | 27 ++++++++----- crates/eest/src/blockchaintest/env.rs | 12 ++---- crates/eest/src/blockchaintest/error.rs | 2 +- crates/eest/src/blockchaintest/execute.rs | 24 +---------- crates/eest/src/blockchaintest/runner.rs | 4 +- crates/eest/src/discover.rs | 4 +- crates/eest/src/env.rs | 11 ++--- crates/eest/src/error.rs | 2 +- crates/eest/src/execute.rs | 2 +- crates/eest/src/fixture_io.rs | 6 +-- crates/eest/src/fixtures.rs | 7 ++++ crates/eest/src/harness.rs | 7 +++- crates/eest/src/runner.rs | 4 +- crates/inspectors/Cargo.toml | 1 + crates/inspectors/src/access_list.rs | 5 ++- .../src/tracing/builder/geth/steps_tests.rs | 40 ++++++++++++------- crates/inspectors/src/tracing/config.rs | 2 +- crates/inspectors/src/tracing/erc7562.rs | 9 ++--- crates/inspectors/src/tracing/js/bindings.rs | 2 +- crates/inspectors/src/tracing/js/builtins.rs | 6 +-- .../src/tracing/js/interrupt_tests.rs | 5 ++- crates/inspectors/src/tracing/js/mod.rs | 20 ++-------- .../src/tracing/js/snapshot_tests.rs | 14 ++----- crates/inspectors/src/tracing/mod.rs | 2 +- crates/inspectors/src/tracing/types.rs | 2 +- crates/inspectors/tests/it/accesslist.rs | 14 +------ crates/inspectors/tests/it/compat.rs | 29 ++++++-------- crates/inspectors/tests/it/geth.rs | 31 ++++++-------- crates/inspectors/tests/it/inputs.rs | 4 +- crates/inspectors/tests/it/parity.rs | 5 ++- crates/inspectors/tests/it/repro/mod.rs | 7 ++-- crates/inspectors/tests/it/stack.rs | 2 +- crates/inspectors/tests/it/transfer.rs | 14 +------ crates/inspectors/tests/it/utils.rs | 1 + 40 files changed, 153 insertions(+), 233 deletions(-) diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index d6d07dada..de9d1956e 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -35,7 +35,7 @@ impl Suites { } } -fn workspace_path(path: &str) -> PathBuf { +pub(crate) fn workspace_path(path: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) } diff --git a/crates/cli/benches/evm/mainnet.rs b/crates/cli/benches/evm/mainnet.rs index 82c64cc89..826328a70 100644 --- a/crates/cli/benches/evm/mainnet.rs +++ b/crates/cli/benches/evm/mainnet.rs @@ -1,3 +1,4 @@ +use crate::fixture::workspace_path; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::evm::InMemoryDB; use evm2_cli::evm_bench::BenchCase; @@ -6,11 +7,7 @@ use evm2_eest::{ blockchaintest::BlockchainTest, execute_blockchain_test_case_on, execute_blockchain_tests_suite, }; -use std::{ - borrow::Cow, - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{borrow::Cow, path::PathBuf, sync::Arc}; #[derive(Clone)] pub(crate) struct PreparedBench { @@ -79,7 +76,3 @@ impl PreparedBench { }); } } - -fn workspace_path(path: &str) -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) -} diff --git a/crates/cli/benches/evm/mainnet_revm.rs b/crates/cli/benches/evm/mainnet_revm.rs index 1bc1bfb56..6699418a9 100644 --- a/crates/cli/benches/evm/mainnet_revm.rs +++ b/crates/cli/benches/evm/mainnet_revm.rs @@ -1,3 +1,4 @@ +use crate::fixture::workspace_path; use criterion::{BatchSize, BenchmarkGroup, measurement::WallTime}; use evm2_cli::{ evm_bench::BenchCase, @@ -5,11 +6,7 @@ use evm2_cli::{ }; use evm2_eest::BlockchainTestNoopHook; use revm::database::InMemoryDB; -use std::{ - borrow::Cow, - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{borrow::Cow, sync::Arc}; /// revm counterpart of `mainnet::PreparedBench`. #[derive(Clone)] @@ -63,7 +60,3 @@ impl PreparedBench { }); } } - -fn workspace_path(path: &str) -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) -} diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index bc815a525..0487af8ac 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -10,7 +10,7 @@ use alloy_consensus::{ use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; use alloy_hardforks::EthereumHardfork; use alloy_primitives::{Address, B256, Bytes, FixedBytes, U256}; -use evm2::SpecId; +use evm2::{SpecId, utils::b256_to_word}; use evm2_eest::{ AccessListItem, TestAuthorization, blockchaintest::{ @@ -109,7 +109,7 @@ fn mainnet_blob_fork(header: &Header) -> Result, CaptureError> Some(EthereumHardfork::Bpo1) => Ok(Some(BlobFork::Bpo1)), Some(EthereumHardfork::Bpo2) => Ok(Some(BlobFork::Bpo2)), Some(EthereumHardfork::Amsterdam) => Ok(Some(BlobFork::Amsterdam)), - _ => Err(CaptureError::UnsupportedSpec(spec as u32)), + _ => Err(CaptureError::UnsupportedSpec(u32::from(spec))), } } @@ -154,7 +154,7 @@ fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { let storage = account .storage .iter() - .map(|entry| (U256::from_be_bytes(entry.slot.0), entry.value)) + .map(|entry| (b256_to_word(entry.slot), entry.value)) .collect(); ( account.address, diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 3646a2d18..511a96d88 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -650,21 +650,18 @@ impl CaseTx { self.caller, ), FuzzTxKind::Eip7702 => Recovered::new_unchecked( - TxEnvelope::Eip7702( - TxEip7702 { - chain_id: 1, - nonce: self.nonce, - gas_limit: self.gas_limit, - max_fee_per_gas: self.gas_price, - max_priority_fee_per_gas: 0, - to: self.target, - value: self.value, - access_list: self.access_list.clone(), - authorization_list: self.eip7702_authorization_list(), - input: self.input.clone(), - } - .into(), - ), + TxEnvelope::from(TxEip7702 { + chain_id: 1, + nonce: self.nonce, + gas_limit: self.gas_limit, + max_fee_per_gas: self.gas_price, + max_priority_fee_per_gas: 0, + to: self.target, + value: self.value, + access_list: self.access_list.clone(), + authorization_list: self.eip7702_authorization_list(), + input: self.input.clone(), + }), self.caller, ), } diff --git a/crates/cli/src/fuzzer/rng.rs b/crates/cli/src/fuzzer/rng.rs index 1a19be2a9..2381c4c12 100644 --- a/crates/cli/src/fuzzer/rng.rs +++ b/crates/cli/src/fuzzer/rng.rs @@ -30,7 +30,7 @@ impl Gen { } pub(crate) fn one_in(&mut self, n: usize) -> bool { - self.rng.random_range(..n) == 0 + self.range(n) == 0 } pub(crate) fn pick(&mut self, values: &[T]) -> T { diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 6461e5625..1abb9ef4f 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -530,7 +530,7 @@ impl PreparedBench { compiler: &mut LlvmCompiler, mut functions: B256Map, ) -> eyre::Result { - let (name, accounts, block, db, tx) = parse_bench(bench)?; + let PreparedBenchParts { name, accounts, block, db, tx } = parse_bench(bench)?; for account in &accounts { if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { continue; @@ -560,7 +560,7 @@ impl PreparedBench { symbol_name: &str, entry_bytecode: &[u8], ) -> eyre::Result<(Self, libloading::Library)> { - let (name, _, block, db, tx) = parse_bench(bench)?; + let PreparedBenchParts { name, block, db, tx, .. } = parse_bench(bench)?; let lib = unsafe { libloading::Library::new(lib_path) }?; // The library holds only the entry contract; other contracts run on the interpreter. let func = unsafe { @@ -655,8 +655,13 @@ struct ParsedAccount { code_hash: B256, } -type PreparedBenchParts = - (Cow<'static, str>, Vec, BlockEnv, InMemoryDB, RecoveredTxEnvelope); +struct PreparedBenchParts { + name: Cow<'static, str>, + accounts: Vec, + block: BlockEnv, + db: InMemoryDB, + tx: RecoveredTxEnvelope, +} fn parse_bench(bench: &Bench) -> eyre::Result { match &bench.source { @@ -686,7 +691,7 @@ fn parse_fixture_bench(bench: &Bench, fixture_path: &str) -> eyre::Result eyre::Result { @@ -709,13 +714,13 @@ fn parse_bytecode_bench(bench: &Bench, bytecode: &[u8]) -> eyre::Result eyre::Result { diff --git a/crates/eest/src/blockchaintest/env.rs b/crates/eest/src/blockchaintest/env.rs index 561c935bd..b66994e38 100644 --- a/crates/eest/src/blockchaintest/env.rs +++ b/crates/eest/src/blockchaintest/env.rs @@ -1,12 +1,11 @@ use crate::{ env::explicit_state_test_root_from_env, fixtures::{ - EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, fixtures_root, - workspace_relative, + EEST_STABLE_ENV, apply_subdir_to_roots, env_flag, explicit_root_from_env, fixtures_root, }, harness::TestRoot, }; -use std::{env, path::PathBuf}; +use std::path::PathBuf; /// Environment variable for the blockchain test root. pub(crate) const BLOCKCHAIN_TEST_ROOT_ENV: &str = "EVM2_BLOCKCHAINTEST_ROOT"; @@ -19,12 +18,7 @@ pub(crate) type BlockchainTestRoot = TestRoot; /// Returns the explicit blockchain-test root configured through environment variables. pub(crate) fn explicit_blockchain_test_root_from_env() -> Option { - env::var_os(BLOCKCHAIN_TEST_ROOT_ENV).map(PathBuf::from).map(workspace_relative).map( - |mut path| { - apply_subdir(&mut path, BLOCKCHAIN_TEST_SUBDIR_ENV); - path - }, - ) + explicit_root_from_env(BLOCKCHAIN_TEST_ROOT_ENV, BLOCKCHAIN_TEST_SUBDIR_ENV) } /// Returns the blockchain-test roots to run by default. diff --git a/crates/eest/src/blockchaintest/error.rs b/crates/eest/src/blockchaintest/error.rs index 84627775e..c1a2ed354 100644 --- a/crates/eest/src/blockchaintest/error.rs +++ b/crates/eest/src/blockchaintest/error.rs @@ -19,7 +19,7 @@ pub struct TestError { impl TestError { /// Creates an error for an unknown test name. pub(crate) fn unknown(path: impl Into, kind: TestErrorKind) -> Self { - Self { path: path.into().display().to_string(), name: "Unknown".to_string(), kind } + Self::case(path, "Unknown", kind) } /// Creates an error for a named test case. diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 15269676f..3754059e0 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -13,7 +13,7 @@ use super::{ use crate::compiled::{self, FileSummary}; use crate::{ dump, - execute::state_root_from_database, + execute::{print_db_stats, state_root_from_database}, execution::ExecutionResources, filter::NameFilter, fixture_io, @@ -37,7 +37,6 @@ use alloy_eips::{ }; use alloy_primitives::{Address, B256, Bloom, Bytes, Signature, U256, aliases::U512}; use alloy_rpc_types_eth::AccessList as RpcAccessList; -use anstyle::{AnsiColor, Color, Style}; use evm2::{ BaseEvmTypes, Evm, EvmTypes, Precompiles, SpecId, TxResult, block::{ @@ -881,27 +880,6 @@ impl ReceiptBuilder for HarnessReceiptBuilder { } } -fn print_db_stats(counts: DbStatsCounts) { - let style = db_stats_style(); - eprintln!("{style}db stats{style:#}: get_account={}", counts.get_account); - eprintln!("{style}db stats{style:#}: get_code_by_hash={}", counts.get_code_by_hash); - eprintln!("{style}db stats{style:#}: get_storage={}", counts.get_storage); - eprintln!( - "{style}db stats{style:#}: get_storage_same_address_repeats={}", - counts.get_storage_same_address_repeats - ); - eprintln!( - "{style}db stats{style:#}: get_storage_same_address_longest_streak={}", - counts.get_storage_same_address_longest_streak - ); - eprintln!("{style}db stats{style:#}: get_block_hash={}", counts.get_block_hash); -} - -#[inline] -const fn db_stats_style() -> Style { - Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightCyan))).bold() -} - fn block_number(block: &Block) -> Option { block_header(block).map(|header| header.number) } diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index a698476a6..c742e8e15 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -2,7 +2,7 @@ use super::{ env::blockchain_test_roots, execute::{ExecuteConfig, ExecutionMode, execute_test_suite}, }; -use crate::harness::{TestSuite, run_json_harnesses}; +use crate::harness::{TestSuite, contains_any, run_json_harnesses}; #[cfg(feature = "jit")] use crate::{ blockchaintest::execute::execute_test_suites, execution::CompiledMode, harness::compiled_roots, @@ -127,7 +127,7 @@ fn should_descend(path: &Path) -> bool { } fn should_ignore(name: &str) -> bool { - IGNORED_TESTS.iter().any(|ignored| name.contains(ignored)) + contains_any(name, IGNORED_TESTS) } #[rustfmt::skip] diff --git a/crates/eest/src/discover.rs b/crates/eest/src/discover.rs index 5503a0ae0..8a0d90c8e 100644 --- a/crates/eest/src/discover.rs +++ b/crates/eest/src/discover.rs @@ -1,3 +1,4 @@ +use crate::fixture_io::has_extension; use std::path::{Path, PathBuf}; use walkdir::{DirEntry, WalkDir}; @@ -47,6 +48,5 @@ fn should_walk_entry(entry: &DirEntry, should_descend: fn(&Path) -> bool) -> boo } fn is_json_test(path: &Path) -> bool { - path.file_name().is_none_or(|name| name != "index.json") - && path.extension().is_some_and(|ext| ext == "json") + path.file_name().is_none_or(|name| name != "index.json") && has_extension(path, "json") } diff --git a/crates/eest/src/env.rs b/crates/eest/src/env.rs index 894ba5fd6..0fbe1adbb 100644 --- a/crates/eest/src/env.rs +++ b/crates/eest/src/env.rs @@ -1,12 +1,12 @@ use crate::{ blockchaintest::env::explicit_blockchain_test_root_from_env, fixtures::{ - EEST_ONLY_ENV, EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, - fixtures_root, workspace_relative, + EEST_ONLY_ENV, EEST_STABLE_ENV, apply_subdir_to_roots, env_flag, explicit_root_from_env, + fixtures_root, }, harness::TestRoot, }; -use std::{env, path::PathBuf}; +use std::path::PathBuf; /// Environment variable for the state test root. pub(crate) const STATE_TEST_ROOT_ENV: &str = "EVM2_STATETEST_ROOT"; @@ -19,10 +19,7 @@ pub(crate) type StateTestRoot = TestRoot; /// Returns the explicit state-test root configured through environment variables. pub(crate) fn explicit_state_test_root_from_env() -> Option { - env::var_os(STATE_TEST_ROOT_ENV).map(PathBuf::from).map(workspace_relative).map(|mut x| { - apply_subdir(&mut x, STATE_TEST_SUBDIR_ENV); - x - }) + explicit_root_from_env(STATE_TEST_ROOT_ENV, STATE_TEST_SUBDIR_ENV) } /// Returns the state-test roots to run by default. diff --git a/crates/eest/src/error.rs b/crates/eest/src/error.rs index 37457c998..583c61a37 100644 --- a/crates/eest/src/error.rs +++ b/crates/eest/src/error.rs @@ -22,7 +22,7 @@ pub struct TestError { impl TestError { /// Creates an error for an unknown test name. pub(crate) fn unknown(path: impl Into, kind: TestErrorKind) -> Self { - Self { path: path.into().display().to_string(), name: "Unknown".to_string(), kind } + Self::case(path, "Unknown", kind) } /// Creates an error for a named test case. diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index b046ffc58..dc0d3b845 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -426,7 +426,7 @@ fn execute_spec( Ok(outcome) } -fn print_db_stats(counts: DbStatsCounts) { +pub(crate) fn print_db_stats(counts: DbStatsCounts) { let style = db_stats_style(); eprintln!("{style}db stats{style:#}: get_account={}", counts.get_account); eprintln!("{style}db stats{style:#}: get_code_by_hash={}", counts.get_code_by_hash); diff --git a/crates/eest/src/fixture_io.rs b/crates/eest/src/fixture_io.rs index 2ae2f4d92..c78e8ee01 100644 --- a/crates/eest/src/fixture_io.rs +++ b/crates/eest/src/fixture_io.rs @@ -109,12 +109,10 @@ fn is_zstd_path(path: &Path) -> bool { has_extension(path, "zst") } -fn has_extension(path: &Path, extension: &str) -> bool { +pub(crate) fn has_extension(path: &Path, extension: &str) -> bool { path.extension().is_some_and(|candidate| candidate == extension) } fn stem_has_extension(path: &Path, extension: &str) -> bool { - path.file_stem().is_some_and(|stem| { - Path::new(stem).extension().is_some_and(|candidate| candidate == extension) - }) + path.file_stem().is_some_and(|stem| has_extension(Path::new(stem), extension)) } diff --git a/crates/eest/src/fixtures.rs b/crates/eest/src/fixtures.rs index ab39b8447..8c2d60b5e 100644 --- a/crates/eest/src/fixtures.rs +++ b/crates/eest/src/fixtures.rs @@ -74,3 +74,10 @@ pub(crate) fn apply_subdir_to_roots(roots: &mut Vec, name: &str) { pub(crate) fn workspace_relative(path: PathBuf) -> PathBuf { if path.is_absolute() { path } else { workspace_root().join(path) } } + +/// Returns the explicit test root in the `root_env` variable, with the `subdir_env` filter applied. +pub(crate) fn explicit_root_from_env(root_env: &str, subdir_env: &str) -> Option { + let mut root = workspace_relative(env::var_os(root_env)?.into()); + apply_subdir(&mut root, subdir_env); + Some(root) +} diff --git a/crates/eest/src/harness.rs b/crates/eest/src/harness.rs index cff225fe2..902c43318 100644 --- a/crates/eest/src/harness.rs +++ b/crates/eest/src/harness.rs @@ -169,7 +169,7 @@ fn compiled_eest_subset_allows(subset: Option, name: &str) - let subset = subset.unwrap_or_else(|| default_compiled_eest_subset(name)); match subset { CompiledEestSubset::All => true, - CompiledEestSubset::CiAot => CI_AOT_STATE_DIRS.iter().any(|path| name.contains(path)), + CompiledEestSubset::CiAot => contains_any(name, CI_AOT_STATE_DIRS), CompiledEestSubset::CiSmoke => CI_SMOKE_STATE_FILES.iter().any(|path| name.ends_with(path)), } } @@ -184,6 +184,11 @@ fn default_compiled_eest_subset(name: &str) -> CompiledEestSubset { } } +/// Returns whether `name` contains any of `patterns`. +pub(crate) fn contains_any(name: &str, patterns: &[&str]) -> bool { + patterns.iter().any(|pattern| name.contains(pattern)) +} + const CI_AOT_STATE_DIRS: &[&str] = &[ "frontier/create/", "frontier/opcodes/", diff --git a/crates/eest/src/runner.rs b/crates/eest/src/runner.rs index 70e2f57bc..6f14806f4 100644 --- a/crates/eest/src/runner.rs +++ b/crates/eest/src/runner.rs @@ -1,7 +1,7 @@ use crate::{ env::state_test_roots, execute::{ExecuteConfig, ExecutionMode, execute_test_suite}, - harness::{TestSuite, descend_all, run_json_harnesses}, + harness::{TestSuite, contains_any, descend_all, run_json_harnesses}, }; #[cfg(feature = "jit")] use crate::{execute::execute_test_suites, execution::CompiledMode, harness::compiled_roots}; @@ -139,5 +139,5 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ ]; fn should_ignore(name: &str) -> bool { - IGNORED_TESTS.iter().any(|pattern| name.contains(pattern)) + contains_any(name, IGNORED_TESTS) } diff --git a/crates/inspectors/Cargo.toml b/crates/inspectors/Cargo.toml index d51592fd2..ce68cba50 100644 --- a/crates/inspectors/Cargo.toml +++ b/crates/inspectors/Cargo.toml @@ -39,6 +39,7 @@ num-bigint = { workspace = true, optional = true } [dev-dependencies] alloy-consensus = { workspace = true, features = ["k256"] } alloy-hardforks.workspace = true +evm2 = { workspace = true, features = ["block"] } snapbox = { workspace = true, features = ["term-svg"] } [features] diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index cf571eef4..07b37f22f 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -8,6 +8,7 @@ use alloy_primitives::{ use evm2::{ Evm, EvmTypes, Inspector, interpreter::{Interpreter, Message, MessageResult, op}, + utils::WordExt, }; /// An [Inspector] that collects touched accounts and storage slots. @@ -139,7 +140,7 @@ impl Inspector for AccessListInspector { | op::BALANCE | op::SELFDESTRUCT => { if let Some([slot]) = interp.stack().peekn() { - let addr = Address::from_word(B256::from(slot.to_be_bytes())); + let addr = slot.to_address(); if !self.excluded.contains(&addr) { self.touched_slots.entry(addr).or_default(); } @@ -149,7 +150,7 @@ impl Inspector for AccessListInspector { // `go-ethereum` reads the target once five items are on the stack, even if the call // then underflows. if let Some([_, slot, _, _, _]) = interp.stack().peekn() { - let addr = Address::from_word(B256::from(slot.to_be_bytes())); + let addr = slot.to_address(); if !self.excluded.contains(&addr) { self.touched_slots.entry(addr).or_default(); } diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index 615fed436..abe0493df 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -72,13 +72,18 @@ fn struct_log_omits_zero_refund_and_empty_memory() { fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { // Cover all call-like opcodes, a precompile with no steps, an empty creation, // a reverting grandchild, and a final call opcode that failed before recording a child. - type Fixture = (Option, usize, &'static [usize], &'static [u8]); - let fixtures: &[Fixture] = &[ - ( - None, - 0, - &[1, 2, 4, 5, 6], - &[ + struct Fixture { + parent: Option, + depth: usize, + children: &'static [usize], + ops: &'static [u8], + } + let fixtures = [ + Fixture { + parent: None, + depth: 0, + children: &[1, 2, 4, 5, 6], + ops: &[ op::SSTORE, op::CALL, op::CALLCODE, @@ -87,18 +92,23 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { op::CREATE2, op::CALL, ], - ), - (Some(0), 1, &[], &[]), - (Some(0), 1, &[3], &[op::SSTORE, op::DELEGATECALL, op::SLOAD, op::STOP]), - (Some(2), 2, &[], &[op::SSTORE, op::REVERT]), - (Some(0), 1, &[], &[op::RETURN]), - (Some(0), 1, &[], &[]), - (Some(0), 1, &[], &[op::STOP]), + }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[] }, + Fixture { + parent: Some(0), + depth: 1, + children: &[3], + ops: &[op::SSTORE, op::DELEGATECALL, op::SLOAD, op::STOP], + }, + Fixture { parent: Some(2), depth: 2, children: &[], ops: &[op::SSTORE, op::REVERT] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[op::RETURN] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[op::STOP] }, ]; let mut nodes: Vec<_> = fixtures .iter() .enumerate() - .map(|(idx, &(parent, depth, children, ops))| { + .map(|(idx, &Fixture { parent, depth, children, ops })| { CallTraceNode { idx, parent, diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 32feb86b7..a5a3054e5 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -122,7 +122,7 @@ impl TracingInspectorConfig { pub const fn parity_vm_trace() -> Self { Self::default_parity() .set_record_inputs(false) - .set_steps(true) + .steps() .set_bytecode(true) .set_stack_snapshots(StackSnapshotType::Pushes) .set_step_deltas(true) diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index f3e7462f6..e20b14375 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -33,6 +33,7 @@ use evm2::{ DatabaseError, EvmTypes, Inspector, TxResultExt, evm::DbResult, interpreter::{InstrStop, Interpreter, Message, MessageResult, opcode::op}, + utils::WordExt, }; use thiserror::Error; @@ -283,10 +284,6 @@ fn record(first: &mut Option, error: DatabaseError) - V::default() } -fn word_to_address(word: U256) -> Address { - Address::from_word(word.into()) -} - impl Inspector for Erc7562Inspector { fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { self.calls.initialize_interp(interp); @@ -311,7 +308,7 @@ impl Inspector for Erc7562Inspector { && let Some(top) = last.top && !(last.op == op::EXTCODESIZE && op == op::ISZERO) { - frame.ext_code_access_info.push(word_to_address(top)); + frame.ext_code_access_info.push(top.to_address()); } if last.is_some_and(|last| last.op == op::GAS) && !is_call(op) { *frame.used_opcodes.entry(op::GAS).or_default() += 1; @@ -325,7 +322,7 @@ impl Inspector for Erc7562Inspector { op if is_ext(op) || is_call(op) => { // The code size of the account that the instruction addresses. if let Some(word) = if is_ext(op) { top } else { second } { - let address = word_to_address(word); + let address = word.to_address(); if let Entry::Vacant(entry) = frame.contract_size.entry(address) { let contract_size = Self::code_size(interp, &address) .unwrap_or_else(|error| record(&mut self.error, error)); diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 16587fb08..6252682ed 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -111,7 +111,7 @@ fn state_fn( .build() } -fn type_error(message: String) -> JsError { +pub(crate) fn type_error(message: String) -> JsError { JsError::from_native(JsNativeError::typ().with_message(message)) } diff --git a/crates/inspectors/src/tracing/js/builtins.rs b/crates/inspectors/src/tracing/js/builtins.rs index ab9e64cdb..733fca014 100644 --- a/crates/inspectors/src/tracing/js/builtins.rs +++ b/crates/inspectors/src/tracing/js/builtins.rs @@ -1,6 +1,6 @@ //! Builtin functions -use crate::tracing::types::CallKind; +use crate::tracing::{js::bindings::type_error, types::CallKind}; use alloc::{borrow::Cow, format, rc::Rc, vec::Vec}; use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::HashSet}; use boa_engine::{ @@ -188,9 +188,7 @@ pub(crate) fn bytes_from_value(val: JsValue, context: &mut Context) -> JsResult< return hex_decode_js_string(&js_string); } - Err(JsError::from_native( - JsNativeError::typ().with_message(format!("invalid buffer type: {}", val.type_of())), - )) + Err(type_error(format!("invalid buffer type: {}", val.type_of()))) } /// Copies the viewed bytes of a `Uint8Array` directly out of its buffer. diff --git a/crates/inspectors/src/tracing/js/interrupt_tests.rs b/crates/inspectors/src/tracing/js/interrupt_tests.rs index 4bdcc0978..1a3b23cbe 100644 --- a/crates/inspectors/src/tracing/js/interrupt_tests.rs +++ b/crates/inspectors/src/tracing/js/interrupt_tests.rs @@ -7,6 +7,7 @@ use boa_engine::NativeFunction; use boa_gc::{Finalize, Trace}; use evm2::{ BaseEvmTypes, Precompiles, SpecId, + block::ethereum::execution::execute_transaction_without_commit, bytecode::Bytecode, env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, @@ -260,7 +261,7 @@ fn run( Precompiles::base(SpecId::CANCUN), ); evm.set_inspector(inspector); - let result = evm.transact(&transaction(kind)).map(|result| result.detach()); + let result = execute_transaction_without_commit(&mut evm, &transaction(kind)); (*evm.clear_inspector_as::().unwrap(), result) } @@ -271,7 +272,7 @@ fn assert_interrupted(result: Result) { )); } -fn transaction(kind: TxKind) -> Recovered { +pub(super) fn transaction(kind: TxKind) -> Recovered { Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { gas_limit: 1_000_000, to: kind, ..Default::default() }), Address::ZERO, diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 4b6e6a7a7..3749074f5 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -1122,7 +1122,7 @@ fn with_message_suffix(err: JsError, suffix: &str, ctx: &mut Context) -> JsError #[cfg(test)] mod tests { - use super::*; + use super::{interrupt_tests::transaction, *}; use alloc::{string::ToString, vec, vec::Vec}; use alloy_consensus::{TxEip1559, TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, B256, Bytes, TxKind, U256, bytes, hex}; @@ -1837,14 +1837,7 @@ mod tests { db, Precompiles::base(SpecId::CANCUN), ); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = transaction(TxKind::Call(addr)); for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); @@ -1914,14 +1907,7 @@ mod tests { db, Precompiles::base(SpecId::CANCUN), ); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = transaction(TxKind::Call(addr)); for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); diff --git a/crates/inspectors/src/tracing/js/snapshot_tests.rs b/crates/inspectors/src/tracing/js/snapshot_tests.rs index 5ee86fca8..59e1bc446 100644 --- a/crates/inspectors/src/tracing/js/snapshot_tests.rs +++ b/crates/inspectors/src/tracing/js/snapshot_tests.rs @@ -1,13 +1,12 @@ //! Compare reconstructed JS step data with snapshots captured before interpreter execution. -use super::*; -use alloy_consensus::TxLegacy; +use super::{interrupt_tests::transaction, *}; use alloy_primitives::hex; use evm2::{ BaseEvmTypes, ExecutionConfig, Precompiles, SpecId, Version, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, - ethereum::{TxEnvelope, ethereum_tx_registry}, + ethereum::ethereum_tx_registry, evm::{AccountInfo, CacheDB, EmptyDB}, interpreter::Host, }; @@ -112,14 +111,7 @@ fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec().unwrap(); let block = *evm.block_env(); diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 9b4a02c3e..bcf3d681b 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -423,7 +423,7 @@ impl TracingInspector { trace.gas_refund_counter = result.gas.refunded().max(0) as u64; trace.status = Some(result.stop); - trace.success = result.stop.is_success(); + trace.success = result.is_success(); trace.output = result.output.clone(); if !trace.success { self.log_count = log_count; diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 956ed5d05..cb670fd09 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -139,7 +139,7 @@ impl CallTrace { /// Returns true if the status code is a revert. #[inline] pub fn is_revert(&self) -> bool { - self.status.is_some_and(|status| status == InstrStop::Revert) + self.status.is_some_and(InstrStop::is_revert) } /// Returns `true` if this trace was a selfdestruct. diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index ad921f04b..1fb361c45 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -1,6 +1,6 @@ //! Accesslist tests -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv, a}; use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; @@ -139,18 +139,6 @@ fn test_access_list_authorization_exclusions() { assert_eq!(excluded.len(), 2); } -const fn a(n: u64) -> Address { - let mut bytes = [0; 20]; - let (_, low) = bytes.split_at_mut(12); - let n = n.to_be_bytes(); - let mut i = 0; - while i < 8 { - low[i] = n[i]; - i += 1; - } - Address::new(bytes) -} - /// A call run through `go-ethereum`'s `eth_createAccessList`, and the list it returned. struct AccessListCase { name: &'static str, diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index 08d281c42..91d10fb91 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -172,7 +172,7 @@ impl Context { } pub fn modify_tx_chained(mut self, f: impl FnOnce(&mut TxEnv)) -> Self { - f(&mut self.tx); + self.modify_tx(f); self } @@ -347,21 +347,18 @@ impl TxEnv { if !self.authorization_list.is_empty() { let TxKind::Call(to) = self.kind else { panic!("EIP-7702 transactions must be calls") }; return Recovered::new_unchecked( - TxEnvelope::Eip7702( - TxEip7702 { - chain_id: self.chain_id.unwrap_or(1), - nonce: self.nonce, - gas_limit: self.gas_limit, - max_fee_per_gas: self.gas_price, - max_priority_fee_per_gas: self.gas_priority_fee.unwrap_or(self.gas_price), - to, - value: self.value, - access_list: Default::default(), - authorization_list: self.authorization_list.clone(), - input: self.data.clone(), - } - .into(), - ), + TxEnvelope::from(TxEip7702 { + chain_id: self.chain_id.unwrap_or(1), + nonce: self.nonce, + gas_limit: self.gas_limit, + max_fee_per_gas: self.gas_price, + max_priority_fee_per_gas: self.gas_priority_fee.unwrap_or(self.gas_price), + to, + value: self.value, + access_list: Default::default(), + authorization_list: self.authorization_list.clone(), + input: self.data.clone(), + }), self.caller, ); } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 59abc4742..7f72fe73d 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -327,8 +327,7 @@ fn test_geth_calltracer_logs() { let addr = deploy_contract(&mut evm, code.into(), deployer, SpecId::LONDON).created_address().unwrap(); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); // Three logs emitted by preceding transactions in the block. insp.set_next_log_index(3); @@ -1453,8 +1452,7 @@ fn test_geth_calltracer_nested_revert() { ); // Test revert with topcall - let mut insp3 = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp3 = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm3 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); let addr3 = deploy_contract(&mut evm3, code.into(), deployer, SpecId::LONDON) @@ -1889,8 +1887,7 @@ fn test_geth_calltracer_logs_eip7708() { ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -1975,8 +1972,7 @@ fn test_geth_calltracer_logs_address_regular() { ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -2058,8 +2054,7 @@ fn test_geth_calltracer_logs_delegatecall() { ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -2455,8 +2450,7 @@ fn test_geth_calltracer_log_index_eip7708_revert() { ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); let res = evm @@ -3191,13 +3185,12 @@ fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreSt ); db.insert_account_info( &contract, - AccountInfo { - balance: U256::from(7), - nonce: 1, - code_hash: alloy_primitives::keccak256(&code), - code: Some(Bytecode::new_raw(code)), - ..Default::default() - }, + AccountInfo::new( + U256::from(7), + 1, + alloy_primitives::keccak256(&code), + Some(Bytecode::new_raw(code)), + ), ); db.insert_account_storage(&contract, &U256::from(1), &U256::from(0x2a)); let evm = Context::mainnet() diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index 74732752b..a97d02355 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -193,7 +193,7 @@ fn byte_budget_counts_memory_snapshots() { fn byte_budget_covers_immediates_and_memory_deltas() { let code = hex!("60015f5200"); for (config, limit) in [ - (TracingInspectorConfig::none().steps().set_immediate_bytes(true), 0), + (TracingInspectorConfig::none().steps().record_immediate_bytes(), 0), (TracingInspectorConfig::none().steps().set_step_deltas(true), 31), ] { let (inspector, completed) = inspect_with_limits( @@ -213,7 +213,7 @@ fn byte_budget_can_halt_from_a_step() { let (inspector, completed) = inspect_with_limits( &hex!("600100"), Bytes::new(), - TracingInspectorConfig::none().steps().set_immediate_bytes(true), + TracingInspectorConfig::none().steps().record_immediate_bytes(), TraceLimits::default() .set_max_recorded_bytes(Some(0)) .set_behavior(TraceLimitBehavior::Halt), diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index d7c317b1e..d33086c4e 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -13,6 +13,7 @@ use alloy_rpc_types_trace::{ TransactionTrace, VmTrace, }, }; +use evm2::utils::address_to_word; use evm2_inspectors::tracing::{ TracingInspector, TracingInspectorConfig, parity::populate_state_diff, }; @@ -865,8 +866,8 @@ fn vmtrace_stop_only_child_preserves_parent_and_creation_result() { assert_eq!(trace.ops.len(), 6); let create = &trace.ops[3]; assert_eq!( - create.ex.as_ref().unwrap().push.as_slice(), - &[U256::from_be_slice(Address::with_last_byte(0x42).create(0).as_slice())] + create.ex.as_ref().unwrap().push, + &[address_to_word(&Address::with_last_byte(0x42).create(0))] ); assert!(create.sub.is_some()); } diff --git a/crates/inspectors/tests/it/repro/mod.rs b/crates/inspectors/tests/it/repro/mod.rs index c4814df32..b2e094201 100644 --- a/crates/inspectors/tests/it/repro/mod.rs +++ b/crates/inspectors/tests/it/repro/mod.rs @@ -99,13 +99,12 @@ pub fn build_db_from_prestate(prestate: &BTreeMap) -> Cac db.insert_account_info( addr, - AccountInfo { + AccountInfo::new( balance, nonce, - code_hash: code.as_ref().map(|c| c.hash_slow()).unwrap_or_default(), + code.as_ref().map(|c| c.hash_slow()).unwrap_or_default(), code, - ..Default::default() - }, + ), ); // Insert storage diff --git a/crates/inspectors/tests/it/stack.rs b/crates/inspectors/tests/it/stack.rs index 5f49274f3..b96b40e71 100644 --- a/crates/inspectors/tests/it/stack.rs +++ b/crates/inspectors/tests/it/stack.rs @@ -8,7 +8,7 @@ use evm2_inspectors::tracing::{StackSnapshotType, TracingInspector, TracingInspe /// snapshot of each step. fn stack_snapshots(snapshot_type: StackSnapshotType) -> Vec>> { let mut insp = TracingInspector::new( - TracingInspectorConfig::none().set_steps(true).set_stack_snapshots(snapshot_type), + TracingInspectorConfig::none().steps().set_stack_snapshots(snapshot_type), ); let mut evm = Context::mainnet() diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index b074f494a..65610ff96 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -2,7 +2,7 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, - SpecId, TestDbExt, TransactTo, TxEnv, + SpecId, TestDbExt, TransactTo, TxEnv, a, }; use alloy_primitives::{Address, B256, Log, U256, address, b256, hex}; use evm2_inspectors::{ @@ -289,18 +289,6 @@ fn discards_internal_create_transfer_on_nonce_overflow() { assert_eq!(evm.inspector().logs(), &[transfer(deployer, factory, 10)]); } -const fn a(n: u64) -> Address { - let mut bytes = [0; 20]; - let (_, low) = bytes.split_at_mut(12); - let n = n.to_be_bytes(); - let mut i = 0; - while i < 8 { - low[i] = n[i]; - i += 1; - } - Address::new(bytes) -} - fn transfer(from: Address, to: Address, value: u64) -> Log { Log::new_unchecked( TRANSFER_LOG_EMITTER, diff --git a/crates/inspectors/tests/it/utils.rs b/crates/inspectors/tests/it/utils.rs index 89f685d4b..2b44cc645 100644 --- a/crates/inspectors/tests/it/utils.rs +++ b/crates/inspectors/tests/it/utils.rs @@ -3,6 +3,7 @@ use colorchoice::ColorChoice; use evm2_inspectors::tracing::{TraceWriter, TraceWriterConfig, TracingInspector}; pub use crate::compat::*; +pub(crate) use evm2::utils::u64_to_address as a; pub fn write_traces(tracer: &TracingInspector) -> String { write_traces_with(tracer, TraceWriterConfig::new().color_choice(ColorChoice::Never)) From 3f6405a791578063d124012dd0dddaf01b582f00 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:09:10 +0100 Subject: [PATCH 107/159] refactor(jit): remove duplicated code in the JIT crates (#1004) Removes duplicated code across the JIT crates by swapping hand-inlined copies for the helpers that already exist: `is_success`, `is_revert`, `new_account_state_gas`, `Version::new`, `address_to_word`, `is_jumpdest`, `is_dead_code`, `LLJIT::new`, `ThreadSafeContext::create_module`, `GlobalOrcJit::gc`, `SymbolFlags::set_generic`, `ArtifactKey::llvm` and `TestBackend::with_tuning`. It also merges a few near-copies into shared private or `pub(crate)` helpers: `Bytecode::effective_inst` and `target_block`, `load_ecx_ptr`, `VStack::set_slot`, the SWAP/EXCHANGE liveness pair in dead-store elimination, `HelperPauseState::set_paused`, `word_cost`, and three `TestBackend` constructors. The public API does not change. Generated code does not change: `bench.py --diff origin/main` reports `opt.s` changed in 0/31 benchmarks, with equal totals for every codegen column and compile instructions. --- crates/jit/builtins/src/lib.rs | 15 ++- crates/jit/codegen/src/bytecode/asm.rs | 2 +- crates/jit/codegen/src/bytecode/fmt.rs | 8 +- crates/jit/codegen/src/bytecode/interner.rs | 2 +- crates/jit/codegen/src/bytecode/mod.rs | 16 ++- crates/jit/codegen/src/bytecode/opcode.rs | 2 +- .../src/bytecode/passes/block_analysis.rs | 32 ++---- .../src/bytecode/passes/dead_store_elim.rs | 44 ++++---- .../jit/codegen/src/bytecode/passes/dedup.rs | 21 +--- .../codegen/src/bytecode/passes/sections.rs | 2 +- .../jit/codegen/src/compiler/translate/mod.rs | 52 ++++----- .../codegen/src/compiler/translate/vstack.rs | 18 ++-- crates/jit/codegen/src/tests/mod.rs | 20 ++-- crates/jit/codegen/src/tests/runner.rs | 33 +++--- crates/jit/llvm/src/lib.rs | 13 +-- crates/jit/llvm/src/orc.rs | 19 ++-- crates/jit/llvm/src/wrap/ir.rs | 5 +- crates/jit/runtime/src/evm2_evm.rs | 14 +-- .../jit/runtime/src/runtime/out_of_process.rs | 8 +- crates/jit/runtime/src/runtime/storage.rs | 12 +-- crates/jit/runtime/src/runtime/tests.rs | 100 ++++++++---------- 21 files changed, 198 insertions(+), 240 deletions(-) diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index cf1ba6d3c..f0bd89cbe 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -738,17 +738,14 @@ pub unsafe fn __revmc_builtin_create( ecx.gas.merge_child_gas(result.gas, result.stop); // EIP-8037: refund the conditional create state gas when the create fails // to deploy (no new account leaf is created). - let create_failed = result.created_address.is_none() || !result.stop.is_success(); + let create_failed = result.created_address.is_none() || !result.is_success(); if charged_create_state_gas && create_failed { ecx.gas.refill_reservoir(version.gas_params.create_state_gas()); } let address = EvmWord::from(result.created_address_for_parent()); - let return_data = if result.stop == InstrStop::Revert { - core::mem::take(&mut result.output) - } else { - Bytes::new() - }; + let return_data = + if result.stop.is_revert() { core::mem::take(&mut result.output) } else { Bytes::new() }; unsafe { sp.write(address); } @@ -853,7 +850,7 @@ pub unsafe fn __revmc_builtin_call( return Err(result.stop.into()); } ecx.gas.merge_child_gas(result.gas, result.stop); - if new_account_state_gas != 0 && !result.stop.is_success() { + if new_account_state_gas != 0 && !result.is_success() { ecx.gas.refill_reservoir(new_account_state_gas); } @@ -861,7 +858,7 @@ pub unsafe fn __revmc_builtin_call( if copy_len != 0 { ecx.memory_mut().set(out_offset, &result.output[..copy_len]); } - let success = EvmWord::from(Word::from(u8::from(result.stop.is_success()))); + let success = EvmWord::from(Word::from(u8::from(result.is_success()))); unsafe { sp.write(success); } @@ -1020,7 +1017,7 @@ pub unsafe fn __revmc_builtin_selfdestruct( // State gas for new account creation (EIP-8037). if ecx.enables(EvmFeatures::EIP8037) && should_charge_topup { - ecx.gas.spend_state(ecx.gas_params().get(GasId::NewAccountState))?; + ecx.gas.spend_state(ecx.gas_params().new_account_state_gas())?; } if !res.previously_destroyed { diff --git a/crates/jit/codegen/src/bytecode/asm.rs b/crates/jit/codegen/src/bytecode/asm.rs index caf9c2f5a..ddd23c55b 100644 --- a/crates/jit/codegen/src/bytecode/asm.rs +++ b/crates/jit/codegen/src/bytecode/asm.rs @@ -553,7 +553,7 @@ fn parse_items<'a>(tokens: &[Token<'a>]) -> Result>> { push_kind: PushKind::Fixed(1), })); } else { - let imm_len = opc.info().immediate_size(); + let imm_len = opc.immediate_size(); if imm_len > 0 { let imm = expect_imm(tokens, &mut i, opc)?; items.push(Item::Inst(Inst { diff --git a/crates/jit/codegen/src/bytecode/fmt.rs b/crates/jit/codegen/src/bytecode/fmt.rs index b1d84ed6e..85e8ed1b5 100644 --- a/crates/jit/codegen/src/bytecode/fmt.rs +++ b/crates/jit/codegen/src/bytecode/fmt.rs @@ -2,7 +2,6 @@ use super::{ Bytecode, Inst, InstData, InstFlags, asm::{TokenKind, Tokenizer}, bitvec_as_bytes, - passes::block_analysis::Block, }; use alloy_primitives::hex; use evm2::interpreter::{op, opcode::OpCode}; @@ -10,11 +9,6 @@ use oxc_index::{IndexVec, index_vec}; use std::{borrow::Cow, fmt, fmt::Write, io::IsTerminal}; impl Bytecode<'_> { - /// Resolves a jump target instruction to its CFG block index. - fn target_block(&self, target: Inst) -> Option { - self.cfg.inst_to_block.get(target).copied().flatten() - } - /// Collects formatted lines and builds the inst-to-line map stored in `self.inst_lines`. pub(super) fn collect_lines(&self) -> Vec<(String, String)> { let mut lines: Vec<(String, String)> = Vec::new(); @@ -143,7 +137,7 @@ impl Bytecode<'_> { if flags.contains(InstFlags::NOOP) { comment.push_str(" noop"); } - if flags.contains(InstFlags::DEAD_CODE) { + if data.is_dead_code() { comment.push_str(" dead"); } if flags.contains(InstFlags::DISABLED) { diff --git a/crates/jit/codegen/src/bytecode/interner.rs b/crates/jit/codegen/src/bytecode/interner.rs index 73342cad1..4bd1bd1b0 100644 --- a/crates/jit/codegen/src/bytecode/interner.rs +++ b/crates/jit/codegen/src/bytecode/interner.rs @@ -55,7 +55,7 @@ impl std::ops::Index for Interner { #[inline] fn index(&self, idx: I) -> &T { - &self.set[idx.index()] + self.get(idx) } } diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index bd3cc75e5..66b0db1cb 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -503,7 +503,7 @@ impl<'a> Bytecode<'a> { #[inline(never)] pub(crate) fn recompute_reachable_jumpdests(&mut self) { for data in &mut self.insts.raw { - if data.opcode == op::JUMPDEST { + if data.is_jumpdest() { data.clear_jumpdest_reachable(); } } @@ -529,7 +529,7 @@ impl<'a> Bytecode<'a> { while let Some(&next) = self.redirects.get(&target) { target = next; } - if self.insts[target].opcode == op::JUMPDEST { + if self.insts[target].is_jumpdest() { self.insts[target].set_jumpdest_reachable(); } } @@ -577,7 +577,7 @@ impl<'a> Bytecode<'a> { if data.is_reachable_jumpdest(self.has_dynamic_jumps) { break; } - if !data.flags.contains(InstFlags::DEAD_CODE) { + if !data.is_dead_code() { any_new = true; marked_jump_dead |= data.is_jump(); } @@ -716,6 +716,16 @@ impl<'a> Bytecode<'a> { self.multi_jump_targets.get(&inst).map(|v| v.as_slice()) } + /// Resolves an instruction through dead-block redirects created by dedup. + pub(crate) fn effective_inst(&self, inst: Inst) -> Inst { + self.redirects.get(&inst).copied().unwrap_or(inst) + } + + /// Resolves a jump target instruction to its CFG block index. + pub(crate) fn target_block(&self, target: Inst) -> Option { + self.cfg.inst_to_block.get(target).copied().flatten() + } + /// Returns the known constant value of a stack operand at the given instruction. /// /// `depth` 0 is TOS (first popped by this instruction), 1 is second, etc. diff --git a/crates/jit/codegen/src/bytecode/opcode.rs b/crates/jit/codegen/src/bytecode/opcode.rs index 3d7383353..0ae100db8 100644 --- a/crates/jit/codegen/src/bytecode/opcode.rs +++ b/crates/jit/codegen/src/bytecode/opcode.rs @@ -78,7 +78,7 @@ impl<'a> OpcodesIter<'a> { /// Create a new iterator over the given bytecode slice. #[inline] pub fn new(slice: &'a [u8], spec_id: SpecId) -> Self { - Self { iter: slice.iter(), info: op_info_map(spec_id) } + Self::with_infos(slice, op_info_map(spec_id)) } /// Create a new iterator over the given bytecode slice with the given opcode infos. diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index 2a7dc59f9..73efd0fee 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -1954,10 +1954,7 @@ impl Bytecode<'_> { return None; } - let resolve = |inst: Inst| { - let inst = self.redirects.get(&inst).copied().unwrap_or(inst); - self.cfg.inst_to_block.get(inst).copied().flatten() - }; + let resolve = |inst: Inst| self.target_block(self.effective_inst(inst)); let fallthrough = resolve(term + 1); let fallthrough_is_target = if data.flags.contains(InstFlags::MULTI_JUMP) { self.multi_jump_targets @@ -2757,7 +2754,7 @@ pub(crate) mod tests { // Build a stack with 70 items, where the deepest is the final JUMP target. // A dispatcher JUMP is resolvable; a later JUMP in a block that uses DUPN 70 // must also be correctly resolved (not marked INVALID_JUMP). - let code = crate::parse_asm( + let bytecode = analyze_asm_spec( " PUSH %target ; 69 × PUSH0 @@ -2786,9 +2783,8 @@ pub(crate) mod tests { JUMPDEST STOP ", - ) - .unwrap(); - let bytecode = analyze_code_spec(code, SpecId::AMSTERDAM); + SpecId::AMSTERDAM, + ); // The "problem" JUMP must NOT be marked INVALID_JUMP. let jumps: Vec<_> = bytecode.iter_insts().filter(|(_, d)| d.opcode == op::JUMP).collect(); @@ -2857,7 +2853,7 @@ pub(crate) mod tests { assert!(!bytecode.has_dynamic_jumps, "expected all jumps to be resolved"); } - fn fixture_entry_code(json: &str) -> Vec { + pub(crate) fn fixture_entry_code(json: &str) -> Vec { let v: serde_json::Value = serde_json::from_str(json).unwrap(); let case = v.as_object().unwrap().values().next().unwrap(); let transaction = &case["transaction"]; @@ -3138,12 +3134,8 @@ mod tests_edge_cases { bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); let jump_block = bytecode.cfg.inst_to_block[jump_inst].unwrap(); let fallthrough_block = bytecode.cfg.inst_to_block[jump_inst + 1].unwrap(); - let target_inst = bytecode - .iter_all_insts() - .rev() - .find(|(_, data)| data.opcode == op::JUMPDEST) - .unwrap() - .0; + let target_inst = + bytecode.iter_all_insts().rev().find(|(_, data)| data.is_jumpdest()).unwrap().0; assert!(jump.has_const_jumpi_condition()); assert_eq!(jump.static_jump_target(), jump_inst + 1); @@ -3177,12 +3169,8 @@ mod tests_edge_cases { bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); let jump_block = bytecode.cfg.inst_to_block[jump_inst].unwrap(); let fallthrough_block = bytecode.cfg.inst_to_block[jump_inst + 1].unwrap(); - let target_inst = bytecode - .iter_all_insts() - .rev() - .find(|(_, data)| data.opcode == op::JUMPDEST) - .unwrap() - .0; + let target_inst = + bytecode.iter_all_insts().rev().find(|(_, data)| data.is_jumpdest()).unwrap().0; assert!(jump.has_const_jumpi_condition()); assert_eq!(jump.static_jump_target(), jump_inst + 1); @@ -4133,7 +4121,7 @@ mod tests_edge_cases { .iter_insts() .filter(|(_, d)| d.is_jump() && !d.is_dead_code()) // `tramp` is the first `JUMPDEST JUMP` block. - .find(|&(i, _)| bytecode.inst(i - 1).opcode == op::JUMPDEST) + .find(|&(i, _)| bytecode.inst(i - 1).is_jumpdest()) .unwrap(); assert!(!tramp_jump.1.flags.contains(InstFlags::STATIC_JUMP)); } diff --git a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs index b2c836623..6eeec18b5 100644 --- a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs +++ b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs @@ -15,7 +15,7 @@ use super::{ StackSectionAnalysis, block_analysis::{AbsValue, Snapshots}, }; -use crate::bytecode::{AnalysisConfig, Bytecode, Inst, InstFlags}; +use crate::bytecode::{Bytecode, Inst, InstFlags}; use bitvec::vec::BitVec; use evm2::interpreter::op; @@ -37,7 +37,7 @@ impl Bytecode<'_> { #[instrument(name = "dse", level = "debug", skip_all)] pub(crate) fn dead_store_elim(&mut self) { let mut eliminated = 0u32; - let inspect_stack = self.config.contains(AnalysisConfig::INSPECT_STACK); + let inspect_stack = self.stack_observed(); // Reusable buffers hoisted out of the per-block loop. let mut heights = Vec::new(); @@ -69,7 +69,7 @@ impl Bytecode<'_> { // If the block's terminator diverges, no successor will read the stack, // so all exit positions are dead unless `inspect_stack` is set. - let term_diverges = !inspect_stack && self.insts[block.terminator()].is_diverging(); + let term_diverges = !inspect_stack && self.is_instr_diverging(block.terminator()); live.clear(); live.resize(exit_height.max(0) as usize, !term_diverges); @@ -242,11 +242,11 @@ fn transfer_liveness( match opcode { op::SWAP1..=op::SWAP16 => { let depth = (opcode - op::SWAP1 + 1) as usize; - transfer_swap(live, h_before, depth); + transfer_exchange(live, h_before, 0, depth); } op::SWAPN => { if let Some(DecodedImm::Single(depth)) = imm { - transfer_swap(live, h_before, depth as usize); + transfer_exchange(live, h_before, 0, depth as usize); } } op::DUP1..=op::DUP16 => { @@ -260,12 +260,7 @@ fn transfer_liveness( } op::EXCHANGE => { if let Some(DecodedImm::Pair(n, m)) = imm { - let tos = h_before - 1; - let pos_a = tos - n as usize; - let pos_b = tos - m as usize; - let (a, b) = (live[pos_a], live[pos_b]); - live.set(pos_a, b); - live.set(pos_b, a); + transfer_exchange(live, h_before, n as usize, m as usize); } } op::POP => { @@ -315,13 +310,13 @@ fn generic_transfer( } } -/// SWAP liveness: permute liveness of TOS and the swapped position. -fn transfer_swap(live: &mut BitVec, h_before: usize, depth: usize) { +/// SWAP/EXCHANGE liveness: permute liveness of the positions `n` and `m` below TOS. +fn transfer_exchange(live: &mut BitVec, h_before: usize, n: usize, m: usize) { let tos = h_before - 1; - let other = tos - depth; - let (a, b) = (live[tos], live[other]); - live.set(tos, b); - live.set(other, a); + let (pos_a, pos_b) = (tos - n, tos - m); + let (a, b) = (live[pos_a], live[pos_b]); + live.set(pos_a, b); + live.set(pos_b, a); } /// Returns `true` if all positions actually written by the instruction are dead. @@ -340,16 +335,17 @@ fn all_outputs_dead( match (opcode, imm) { // SWAP: only TOS and the deep slot are written. (op::SWAP1..=op::SWAP16, _) => { - swap_all_dead(live, h_before, (opcode - op::SWAP1 + 1) as usize) + exchange_all_dead(live, h_before, 0, (opcode - op::SWAP1 + 1) as usize) + } + (op::SWAPN, Some(DecodedImm::Single(d))) => { + exchange_all_dead(live, h_before, 0, d as usize) } - (op::SWAPN, Some(DecodedImm::Single(d))) => swap_all_dead(live, h_before, d as usize), // DUP: only the new TOS is a real output. (op::DUP1..=op::DUP16, _) => !live[h_before], (op::DUPN, Some(DecodedImm::Single(_))) => !live[h_before], // EXCHANGE: only the two exchanged non-TOS positions are written. (op::EXCHANGE, Some(DecodedImm::Pair(n, m))) => { - let tos = h_before - 1; - !live[tos - n as usize] && !live[tos - m as usize] + exchange_all_dead(live, h_before, n as usize, m as usize) } // Generic: all output positions must be dead. _ if out > 0 => { @@ -363,10 +359,10 @@ fn all_outputs_dead( } } -/// SWAP dead check: both TOS and the swapped position must be dead. -fn swap_all_dead(live: &BitVec, h_before: usize, depth: usize) -> bool { +/// SWAP/EXCHANGE dead check: both written positions, `n` and `m` below TOS, must be dead. +fn exchange_all_dead(live: &BitVec, h_before: usize, n: usize, m: usize) -> bool { let tos = h_before - 1; - !live[tos] && !live[tos - depth] + !live[tos - n] && !live[tos - m] } /// DUP liveness: the new TOS is killed; if it was live, the source becomes live. diff --git a/crates/jit/codegen/src/bytecode/passes/dedup.rs b/crates/jit/codegen/src/bytecode/passes/dedup.rs index 944f81a91..ea5b43ea2 100644 --- a/crates/jit/codegen/src/bytecode/passes/dedup.rs +++ b/crates/jit/codegen/src/bytecode/passes/dedup.rs @@ -223,7 +223,8 @@ fn block_fingerprint(insts: &IndexVec, block: &BlockData) -> Sma #[cfg(test)] mod tests { use crate::bytecode::{ - AnalysisConfig, InstFlags, passes::block_analysis::tests::analyze_asm_with, + AnalysisConfig, InstFlags, + passes::block_analysis::tests::{analyze_asm_with, fixture_entry_code}, }; use evm2::interpreter::op; @@ -525,24 +526,6 @@ mod tests { assert_eq!(bytecode.redirects.len(), 20); } - fn fixture_entry_code(json: &str) -> Vec { - let v: serde_json::Value = serde_json::from_str(json).unwrap(); - let case = v.as_object().unwrap().values().next().unwrap(); - let transaction = &case["transaction"]; - let to = transaction - .get("to") - .or_else(|| { - transaction - .as_array() - .and_then(|transactions| transactions.first()) - .and_then(|transaction| transaction.get("to")) - }) - .and_then(|to| to.as_str()) - .unwrap(); - let code = case["pre"][to]["code"].as_str().unwrap().trim_start_matches("0x"); - alloy_primitives::hex::decode(code).unwrap() - } - #[test] fn dedup_skips_dynamic_jump_sstore_tail() { // Two non-JUMPDEST byte-identical `SSTORE ; JUMP` tails with unresolved dynamic diff --git a/crates/jit/codegen/src/bytecode/passes/sections.rs b/crates/jit/codegen/src/bytecode/passes/sections.rs index e7c19d497..64d55db3d 100644 --- a/crates/jit/codegen/src/bytecode/passes/sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/sections.rs @@ -97,7 +97,7 @@ impl GasSectionAnalysis { /// Accumulates a base gas cost. #[inline] pub(crate) const fn process(&mut self, base_gas: u16) { - self.gas_cost += base_gas as u64; + self.process_extra(base_gas as u64); } /// Accumulates extra gas (e.g. pre-computed dynamic gas for folded instructions). diff --git a/crates/jit/codegen/src/compiler/translate/mod.rs b/crates/jit/codegen/src/compiler/translate/mod.rs index cfaf5e903..6126e76dc 100644 --- a/crates/jit/codegen/src/compiler/translate/mod.rs +++ b/crates/jit/codegen/src/compiler/translate/mod.rs @@ -491,19 +491,14 @@ impl<'a, B: Backend> FunctionCx<'a, B> { Some(next) } - /// Resolves an instruction through dead-block redirects created by dedup. - fn effective_inst(&self, inst: Inst) -> Inst { - self.bytecode.redirects.get(&inst).copied().unwrap_or(inst) - } - /// Returns the IR block for an instruction after applying dedup redirects. fn effective_entry(&self, inst: Inst) -> B::BasicBlock { - self.inst_entries[self.effective_inst(inst)] + self.inst_entries[self.bytecode.effective_inst(inst)] } /// Returns the runtime fallthrough target after applying dedup redirects. fn effective_next_inst(&self, inst: Inst) -> Option { - self.lexical_next_inst(inst).map(|next| self.effective_inst(next)) + self.lexical_next_inst(inst).map(|next| self.bytecode.effective_inst(next)) } /// Returns the IR block for the runtime fallthrough target. @@ -569,7 +564,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } // Assert that we already skipped the block. - debug_assert!(!data.flags.contains(InstFlags::DEAD_CODE)); + debug_assert!(!data.is_dead_code()); // Disabled instructions don't pay gas. if data.flags.contains(InstFlags::DISABLED) { @@ -1502,14 +1497,19 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.load(self.word_type, ptr, name) } - fn load_interpreter_ptr(&mut self) -> B::Value { + /// Loads the pointer stored in the `EvmContext` field at the given offset. + fn load_ecx_ptr(&mut self, offset: usize, addr_name: &str, name: &str) -> B::Value { let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + let field = self.get_field(self.ecx, offset, addr_name); + self.bcx.load(ptr_type, field, name) + } + + fn load_interpreter_ptr(&mut self) -> B::Value { + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, interpreter), "ecx.interpreter.addr", - ); - self.bcx.load(ptr_type, field, "ecx.interpreter") + "ecx.interpreter", + ) } fn load_message_ptr(&mut self) -> B::Value { @@ -1524,23 +1524,19 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } fn load_block_env_ptr(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, block_env), "ecx.block_env.addr", - ); - self.bcx.load(ptr_type, field, "ecx.block_env") + "ecx.block_env", + ) } fn load_tx_env_ptr(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, tx_env), "ecx.tx_env.addr", - ); - self.bcx.load(ptr_type, field, "ecx.tx_env") + "ecx.tx_env", + ) } /// Gets a field at the given offset. @@ -1953,13 +1949,11 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } fn load_memory_base(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let mem_base_field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, mem_base), "ecx.mem_base.addr", - ); - self.bcx.load(ptr_type, mem_base_field, "ecx.mem_base") + "ecx.mem_base", + ) } /// Emits under/overflow bounds checks for a stack section. @@ -2119,7 +2113,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let jumpdests = bytecode .iter_insts() .filter(|(_, data)| { - data.opcode == op::JUMPDEST && bytecode.is_valid_jump(data.jumpdest_pc() as usize) + data.is_jumpdest() && bytecode.is_valid_jump(data.jumpdest_pc() as usize) }) .map(|(inst, data)| (data.jumpdest_pc() as usize, inst)) .collect::>(); diff --git a/crates/jit/codegen/src/compiler/translate/vstack.rs b/crates/jit/codegen/src/compiler/translate/vstack.rs index a7c0a0144..3678750be 100644 --- a/crates/jit/codegen/src/compiler/translate/vstack.rs +++ b/crates/jit/codegen/src/compiler/translate/vstack.rs @@ -118,28 +118,30 @@ impl VStack { /// Sets a slot to virtual at the given operand depth. pub(super) fn set(&mut self, depth: usize, value: T) { - let idx = self.idx(self.offset_at_depth(depth)); - self.slots[idx] = Some(VSlot::Virtual(value)); + self.set_slot(self.offset_at_depth(depth), VSlot::Virtual(value)); + } + + /// Sets the slot at the given section-relative offset. + fn set_slot(&mut self, offset: i32, slot: VSlot) { + let idx = self.idx(offset); + self.slots[idx] = Some(slot); } /// Records that memory at a section-relative offset holds `value`. pub(super) fn set_stored_at_offset(&mut self, offset: i32, value: T) { - let idx = self.idx(offset); - self.slots[idx] = Some(VSlot::Materialized(Some(value))); + self.set_slot(offset, VSlot::Materialized(Some(value))); } /// Pushes a virtual value onto the top of the stack. pub(super) fn push(&mut self, value: T) { - let idx = self.idx(self.top_offset); - self.slots[idx] = Some(VSlot::Virtual(value)); + self.set_slot(self.top_offset, VSlot::Virtual(value)); self.top_offset += 1; } /// Pushes a materialized marker onto the top of the stack. /// Used when a builtin writes directly to the physical stack slot. pub(super) fn push_mem(&mut self) { - let idx = self.idx(self.top_offset); - self.slots[idx] = Some(VSlot::Materialized(None)); + self.set_slot(self.top_offset, VSlot::Materialized(None)); self.top_offset += 1; } diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 37c431a1d..683d7a5d6 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -17,15 +17,19 @@ use evm2::{ }; use evm2_jit_builtins::gas; -/// `KECCAK256` opcode gas cost (base + dynamic). -const fn keccak256_cost(len: u64) -> Option { - let words = len.div_ceil(32); - match words.checked_mul(gas::KECCAK256WORD as u64) { - Some(dyn_cost) => Some((gas::KECCAK256 as u64).saturating_add(dyn_cost)), +/// Opcode gas cost of `base` plus `per_word` for each 32-byte word of `len`. +const fn word_cost(base: u64, per_word: u64, len: u64) -> Option { + match len.div_ceil(32).checked_mul(per_word) { + Some(dyn_cost) => Some(base.saturating_add(dyn_cost)), None => None, } } +/// `KECCAK256` opcode gas cost (base + dynamic). +const fn keccak256_cost(len: u64) -> Option { + word_cost(gas::KECCAK256 as u64, gas::KECCAK256WORD as u64, len) +} + /// `LOG` opcode gas cost (base + topics + dynamic). const fn log_cost(n_topics: u8, len: u64) -> Option { match (gas::LOGDATA as u64).checked_mul(len) { @@ -38,11 +42,7 @@ const fn log_cost(n_topics: u8, len: u64) -> Option { /// `CALLDATACOPY`, `CODECOPY`, `RETURNDATACOPY` opcode gas cost (base + dynamic). const fn verylowcopy_cost(len: u64) -> Option { - let words = len.div_ceil(32); - match words.checked_mul(gas::COPY as u64) { - Some(dyn_cost) => Some((gas::VERYLOW as u64).saturating_add(dyn_cost)), - None => None, - } + word_cost(gas::VERYLOW as u64, gas::COPY as u64, len) } #[macro_use] diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index 9d503edc7..32549475c 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -276,6 +276,21 @@ fn def_message(gas_limit: u64) -> Message { } } +fn test_message( + gas_limit: u64, + bytecode: &[u8], + modify_message: Option)>, +) -> Message { + let mut message = def_message(gas_limit); + if let Some(modify_message) = modify_message { + modify_message(&mut message); + } + Message:: { + code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), + ..message + } +} + fn def_database() -> InMemoryDB { let mut database = InMemoryDB::default(); database.insert_account_info( @@ -393,14 +408,7 @@ fn with_evm_context_and_host_mut< modify_message: Option)>, f: F, ) -> R { - let mut message = def_message(DEF_GAS_LIMIT); - if let Some(modify_message) = modify_message { - modify_message(&mut message); - } - let message = Message:: { - code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), - ..message - }; + let message = test_message(DEF_GAS_LIMIT, bytecode, modify_message); let mut interpreter = Interpreter::::new(&message); interpreter.prepare_run(host); @@ -503,15 +511,8 @@ fn run_compiled_test_case_with_context( } // Interpreter - run evm2 as the oracle - let mut message = def_message(gas_limit); - if let Some(modify_message) = modify_message { - modify_message(&mut message); - } + let mut message = test_message(gas_limit, bytecode, modify_message); message.caller_is_static = is_static; - let message = Message:: { - code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), - ..message - }; let mut interpreter = Interpreter::::new(&message); let mut int_host = prepare_host(spec_id); let int_result = interpreter.run(&mut int_host).unwrap(); diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 2cceb56a9..6eb9210be 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -296,12 +296,11 @@ impl GlobalOrcJit { /// Removes a JITDylib from the ExecutionSession, freeing all its resources. fn remove_jit_dylib(&self, jd: orc::JITDylibRef) { - let es = self.jit.get_execution_session(); - if let Err(e) = es.remove_jit_dylib(jd) { + if let Err(e) = self.jit.get_execution_session().remove_jit_dylib(jd) { error!("failed to remove JITDylib: {e}"); } // Reclaim unreferenced interned strings after removing symbols. - es.get_symbol_string_pool().clear_dead_entries(); + self.gc(); } /// Defines absolute symbols in the shared builtins JITDylib, skipping any @@ -434,7 +433,7 @@ impl OrcJitState { fn maybe_clear_dead_pool_entries(&mut self) { self.clear_pool_counter += 1; if self.clear_pool_counter.is_multiple_of(256) { - self.global.jit.get_execution_session().get_symbol_string_pool().clear_dead_entries(); + self.global.gc(); } } @@ -684,8 +683,10 @@ impl EvmLlvmBackend { let tracker = orc.jd().create_resource_tracker(); - let tsm = orc::ThreadSafeModule::create_in_context(old_module, tscx); - orc.global.jit.add_module_with_rt(tsm, &tracker).map_err(error_msg)?; + orc.global + .jit + .add_module_with_rt(tscx.create_module(old_module), &tracker) + .map_err(error_msg)?; let tracker_idx = orc.loaded_trackers.len(); for &id in orc.staged_functions.keys() { diff --git a/crates/jit/llvm/src/orc.rs b/crates/jit/llvm/src/orc.rs index bd04337d6..fec202773 100644 --- a/crates/jit/llvm/src/orc.rs +++ b/crates/jit/llvm/src/orc.rs @@ -293,7 +293,7 @@ impl SymbolFlags { /// Set the `Exported` flag. pub const fn set_exported(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsExported as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsExported); } /// Set the `Exported` flag. @@ -309,7 +309,7 @@ impl SymbolFlags { /// Set the `Weak` flag. pub const fn set_weak(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsWeak as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsWeak); } /// Set the `Weak` flag. @@ -325,7 +325,7 @@ impl SymbolFlags { /// Set the `Callable` flag. pub const fn set_callable(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsCallable as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsCallable); } /// Set the `Callable` flag. @@ -341,9 +341,9 @@ impl SymbolFlags { /// Set the `MaterializationSideEffectsOnly` flag. pub const fn set_materialization_side_effects_only(&mut self) { - self.generic |= - LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly - as u8; + self.set_generic( + LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly, + ); } /// Set the `MaterializationSideEffectsOnly` flag. @@ -1829,7 +1829,7 @@ mod tests { ) .unwrap(); - let jit = LLJITBuilder::new().build().unwrap(); + let jit = LLJIT::new().unwrap(); // Build module. let m = cx.create_module("test"); @@ -1920,7 +1920,7 @@ mod tests { ) .unwrap(); - let jit = LLJITBuilder::new().build().unwrap(); + let jit = LLJIT::new().unwrap(); // Create two custom JITDylibs with functions returning different values. let jd_a = jit.get_execution_session().create_bare_jit_dylib(c"test_a"); @@ -1940,8 +1940,7 @@ mod tests { bcx.build_return(Some(cx.i64_type().const_int(value, false))); m.verify().unwrap(); - let tsm = ThreadSafeModule::create_in_context(m, &tscx); - jit.add_module_with_dylib(tsm, unsafe { + jit.add_module_with_dylib(tscx.create_module(m), unsafe { JITDylibRef::from_inner_unchecked(jd.as_inner()) }) .unwrap(); diff --git a/crates/jit/llvm/src/wrap/ir.rs b/crates/jit/llvm/src/wrap/ir.rs index f78e1fcd0..f1366e7e3 100644 --- a/crates/jit/llvm/src/wrap/ir.rs +++ b/crates/jit/llvm/src/wrap/ir.rs @@ -438,7 +438,7 @@ impl Type { impl fmt::Debug for Type { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.print_to_string().to_string_lossy()) + fmt::Display::fmt(&self.print_to_string(), f) } } @@ -1032,6 +1032,5 @@ impl Drop for Builder { } fn fmt_value(value: LLVMValueRef, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let string = unsafe { LLVMString::from_raw(LLVMPrintValueToString(value)) }; - f.write_str(&string.to_string_lossy()) + fmt::Display::fmt(&unsafe { LLVMString::from_raw(LLVMPrintValueToString(value)) }, f) } diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index e62b0fa95..d5b28755b 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -128,6 +128,7 @@ mod tests { env::BlockEnv, evm::{AccountInfo, InMemoryDB, JournalEntry}, interpreter::{Host, MessageKind, Word}, + utils::address_to_word, version::GasId, }; @@ -136,7 +137,7 @@ mod tests { &[op::PUSH1, 0x42, op::PUSH0, op::MSTORE, op::PUSH1, 0x20, op::PUSH0, op::RETURN]; #[cfg(feature = "llvm")] - fn blocking_backend() -> JitBackend { + pub(super) fn blocking_backend() -> JitBackend { JitBackend::new(RuntimeConfig { enabled: true, blocking: true, ..RuntimeConfig::default() }) .unwrap() } @@ -329,7 +330,7 @@ mod tests { #[cfg(feature = "llvm")] fn rejected_gas_schedule_counts_fallback() { let spec = SpecId::CANCUN; - let mut version = *Version::base(spec); + let mut version = Version::new(spec); version.gas_params.set(GasId::SstoreStatic, 800); let host = Evm::::new_with_execution_config( ExecutionConfig::for_spec_and_version(spec, version), @@ -780,7 +781,7 @@ mod tests { #[cfg(feature = "llvm")] fn section_gas_too_large_to_compile_falls_back_to_interpreter() { let spec = SpecId::CANCUN; - let mut version = *Version::base(spec); + let mut version = Version::new(spec); version.gas_params.set(GasId::ExpByte, u32::MAX); let backend = JitBackend::new(RuntimeConfig { enabled: true, @@ -928,7 +929,7 @@ mod tests { let expected = if kind == MessageKind::Call { Word::from(1) } else { - Word::from_be_slice(destination.as_slice()) + address_to_word(&destination) }; assert_eq!(returned, expected); assert!(compiled.1); @@ -951,7 +952,7 @@ mod tests { #[cfg(all(test, feature = "llvm"))] mod custom_host_tests { - use super::*; + use super::{tests::blocking_backend, *}; use crate::runtime::{HostConfig, RuntimeConfig}; use alloy_primitives::{Address, Bytes, U256}; use evm2::{ @@ -1041,8 +1042,7 @@ mod custom_host_tests { assert_eq!(run_interpreter(&backend, &mut compiled, &mut host), Some(InstrStop::Return)); assert_eq!(compiled.output(), expected_output); assert_eq!(compiled.gas(), expected_gas); - let wrong = - JitBackend::new(RuntimeConfig { blocking: true, ..Default::default() }).unwrap(); + let wrong = blocking_backend(); let mut fresh = Interpreter::::new(&message); assert_eq!(run_interpreter(&wrong, &mut fresh, &mut host), None); let config = host.execution_config(); diff --git a/crates/jit/runtime/src/runtime/out_of_process.rs b/crates/jit/runtime/src/runtime/out_of_process.rs index a7356ebf5..07fb64d80 100644 --- a/crates/jit/runtime/src/runtime/out_of_process.rs +++ b/crates/jit/runtime/src/runtime/out_of_process.rs @@ -699,14 +699,18 @@ struct HelperPauseState { impl HelperPauseState { fn pause(&self) { - *self.paused.lock().unwrap() = true; + self.set_paused(true); } fn resume(&self) { - *self.paused.lock().unwrap() = false; + self.set_paused(false); self.resumed.notify_all(); } + fn set_paused(&self, paused: bool) { + *self.paused.lock().unwrap() = paused; + } + fn wait_resumed(&self) { let mut paused = self.paused.lock().unwrap(); while *paused { diff --git a/crates/jit/runtime/src/runtime/storage.rs b/crates/jit/runtime/src/runtime/storage.rs index 9cbb34066..96a06e666 100644 --- a/crates/jit/runtime/src/runtime/storage.rs +++ b/crates/jit/runtime/src/runtime/storage.rs @@ -244,13 +244,11 @@ mod tests { use std::io::Read; fn artifact_key(code_hash: B256) -> ArtifactKey { - ArtifactKey { - runtime: RuntimeCacheKey { code_hash, spec_id: SpecId::OSAKA }, - backend: BackendSelection::Llvm, - opt_level: OptimizationLevel::Default, - abi_version: crate::ABI_VERSION, - config_hash: B256::ZERO, - } + ArtifactKey::llvm( + RuntimeCacheKey { code_hash, spec_id: SpecId::OSAKA }, + OptimizationLevel::Default, + B256::ZERO, + ) } fn manifest(key: ArtifactKey, artifact_len: usize) -> ArtifactManifest { diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index 3f234f6a3..462711149 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -59,13 +59,28 @@ impl TestBackend { Self::new(RuntimeConfig { enabled: true, tuning, ..Default::default() }) } + /// Creates a backend with `enabled: false` and the given tuning overrides. + fn disabled_with_tuning(tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { enabled: false, tuning, ..Default::default() }) + } + + /// Creates a backend with `blocking: true` and the given tuning overrides. + fn blocking_with_tuning(tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { blocking: true, tuning, ..Default::default() }) + } + + /// Creates a backend with `enabled: true`, the given store, and the given tuning overrides. + #[cfg(feature = "llvm")] + fn with_store(store: Arc, tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { enabled: true, store: Some(store), tuning, ..Default::default() }) + } + /// Creates a backend with `enabled: true`, 1 worker, and the given tuning overrides. fn with_tuning_1w(tuning: RuntimeTuning) -> Self { Self::with_tuning(RuntimeTuning { jit_worker_count: 1, ..tuning }) } /// Creates a backend with `enabled: true`, 1 worker, and default tuning. - #[cfg(feature = "llvm")] fn default_1w() -> Self { Self::with_tuning_1w(RuntimeTuning::default()) } @@ -294,9 +309,8 @@ fn aot_out_of_process_is_rejected() { #[test] fn zero_workers_fails_sync_compile_without_blocking() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { jit_worker_count: 0, ..Default::default() }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 0, ..Default::default() }); @@ -311,9 +325,8 @@ fn zero_workers_fails_sync_compile_without_blocking() { #[test] fn blocking_pending_job_cap_counts_fallback() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { jit_max_pending_jobs: 0, ..Default::default() }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_max_pending_jobs: 0, ..Default::default() }); @@ -347,13 +360,9 @@ fn blocking_sync_compile_timeout_counts_fallback() { #[test] #[cfg(feature = "llvm")] fn blocking_mode_compiles_bytecode_over_max_len() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { - jit_worker_count: 1, - jit_max_bytecode_len: 4, - ..Default::default() - }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 1, + jit_max_bytecode_len: 4, ..Default::default() }); @@ -364,7 +373,7 @@ fn blocking_mode_compiles_bytecode_over_max_len() { #[test] fn lookup_disabled() { - let tb = TestBackend::new(RuntimeConfig { enabled: false, ..Default::default() }); + let tb = TestBackend::disabled_with_tuning(Default::default()); let req = TestBackend::req_cancun(&[0x00]); let decision = tb.lookup(req); @@ -377,7 +386,7 @@ fn lookup_disabled() { #[test] fn lookup_miss_when_enabled() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); let decision = tb.lookup(TestBackend::req_cancun(&[0x00])); assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::NotReady))); @@ -389,7 +398,7 @@ fn lookup_miss_when_enabled() { #[test] fn set_enabled_toggle() { - let tb = TestBackend::new(RuntimeConfig { enabled: false, ..Default::default() }); + let tb = TestBackend::disabled_with_tuning(Default::default()); let req = TestBackend::req_cancun(&[0x00]); assert!(matches!( @@ -438,7 +447,7 @@ fn pause_processes_lookup_events() { #[test] fn lookup_increments_miss_counter() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); for _ in 0..10 { let _ = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); @@ -462,7 +471,7 @@ fn drop_shuts_down_backend() { #[test] fn backend_clone() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); let b2 = tb.backend.clone(); let _ = tb.lookup(TestBackend::req_cancun(&[0x00])); @@ -583,7 +592,7 @@ fn prepare_aot_batch_never_blocks_when_startup_fails() { #[test] fn clear_resident() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); tb.clear_resident(); let decision = tb.lookup(TestBackend::req_cancun(&[0x00])); @@ -620,9 +629,8 @@ fn pause_resume_never_block_without_backend_thread() { // after `channel_capacity` sends and block the caller forever (observed in production as a // consensus-engine deadlock after exactly capacity/2 blocks). They must be skipped, not // queued, while the backend is not started. - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 2, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 2, ..Default::default() }); @@ -686,9 +694,8 @@ fn clear_commands_are_queued_after_startup_takes_config() { (JitBackend::clear_persisted, Command::ClearPersisted), (JitBackend::clear_all, Command::ClearAll), ] { - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 1, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 1, ..Default::default() }); let mut guard = tb.inner.lazy_spawn.lock().unwrap(); @@ -743,9 +750,8 @@ fn clear_commands_racing_with_startup_reach_store() { #[test] fn clear_commands_never_block_after_startup_receiver_disconnects() { - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 1, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 1, ..Default::default() }); drop(tb.inner.lazy_spawn.lock().unwrap().take().unwrap()); @@ -802,7 +808,7 @@ fn clear_commands_after_failed_spawn_reach_store_and_allow_retry() { #[test] fn pause_resume_depth_tracking_with_backend_thread() { // With the backend running, pause/resume still track depth and deliver commands. - let tb = TestBackend::with_tuning_1w(RuntimeTuning::default()); + let tb = TestBackend::default_1w(); tb.pause(); tb.pause(); @@ -851,9 +857,8 @@ fn blocking_out_of_process_pause_resume_does_not_stop_helper() { #[test] #[cfg(feature = "llvm")] fn blocking_mode() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 1, ..Default::default() }); @@ -1031,11 +1036,8 @@ fn compile_jit_sync_blocks() { #[test] fn sync_compile_wait_times_out() { - let tb = TestBackend::new(RuntimeConfig { - tuning: RuntimeTuning { - sync_compile_timeout: std::time::Duration::from_millis(1), - ..Default::default() - }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + sync_compile_timeout: std::time::Duration::from_millis(1), ..Default::default() }); let (_tx, rx) = chan::bounded(1); @@ -1524,11 +1526,7 @@ fn aot_artifacts_survive_restart() { // Second backend: should preload the artifact at startup. { - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(store), - ..Default::default() - }); + let tb = TestBackend::with_store(store, Default::default()); let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); assert!( @@ -1564,11 +1562,7 @@ fn preload_aot_seeds_resident() { // Second backend: preloaded AOT should be available immediately. { - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(store), - ..Default::default() - }); + let tb = TestBackend::with_store(store, Default::default()); assert_eq!(tb.stats().resident_entries, 1); } } @@ -1639,12 +1633,10 @@ fn preload_aot_skips_manifest_key_mismatch() { let mismatched_store = Arc::new(RuntimeArtifactStore::new().unwrap()); mismatched_store.store(&key, &manifest, &std::fs::read(&stored.object_path).unwrap()).unwrap(); - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(mismatched_store), - tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, - ..Default::default() - }); + let tb = TestBackend::with_store( + mismatched_store, + RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ); assert_eq!(tb.stats().resident_entries, 0); // The probe rejects it too, so the artifact is recompiled. From 4905b2dbe3d8ab59f7af8ec5193f8d2e1aa0ffc6 Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:11:14 +0100 Subject: [PATCH 108/159] fix(inspectors): pair steps for undefined opcodes (#994) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An undefined opcode in a child frame does not push a pending gas entry, but its step_end still pops one. This consumes the parent CALL’s entry and corrupts opcode gas attribution: the reproduction reports 913,444 gas for CALL instead of 2,600. Push a pending entry for every opcode, while counting and recording gas only for defined opcodes. The gas-cost formula is unchanged. Extend the nested-call regression to cover successful execution, INVALID, and an undefined opcode. Co-authored-by: Amp --- crates/inspectors/src/opcode.rs | 26 ++++++----- crates/inspectors/tests/it/opcode.rs | 67 ++++++++++++++++------------ 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index e899d33e7..8da762b6e 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -11,7 +11,7 @@ use evm2::{ #[derive(Clone, Copy, Debug)] struct PendingOpcodeGas { - opcode: OpCode, + opcode: Option, gas_remaining: u64, child_gas_used: u64, } @@ -71,7 +71,7 @@ impl OpcodeGasInspector { return; }; if let Some(pending) = self.pending_steps.last_mut() - && pending.opcode == opcode + && pending.opcode == Some(opcode) { pending.child_gas_used = pending.child_gas_used.saturating_add(child_gas_used); } @@ -81,22 +81,25 @@ impl OpcodeGasInspector { impl Inspector for OpcodeGasInspector { fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { let opcode_value = interp.opcode(); - if let Some(opcode) = OpCode::new(opcode_value) { + let opcode = OpCode::new(opcode_value); + if let Some(opcode) = opcode { // keep track of opcode counts *self.opcode_counts.entry(opcode).or_default() += 1; - - // keep track of the last opcode executed - self.pending_steps.push(PendingOpcodeGas { - opcode, - gas_remaining: interp.gas().remaining(), - child_gas_used: 0, - }); } + + // Undefined opcodes also receive step_end and must not pop a parent's step. + self.pending_steps.push(PendingOpcodeGas { + opcode, + gas_remaining: interp.gas().remaining(), + child_gas_used: 0, + }); } fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { // update gas usage for the last opcode - if let Some(pending) = self.pending_steps.pop() { + if let Some(pending) = self.pending_steps.pop() + && let Some(opcode) = pending.opcode + { // The sentry rejects SSTORE before charging gas, but out-of-gas hooks already // see the zeroed frame remainder. let gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { @@ -107,7 +110,6 @@ impl Inspector for OpcodeGasInspector { .saturating_sub(interp.gas().remaining()) .saturating_sub(pending.child_gas_used) }; - let opcode = pending.opcode; *self.opcode_gas.entry(opcode).or_default() += gas_cost; } } diff --git a/crates/inspectors/tests/it/opcode.rs b/crates/inspectors/tests/it/opcode.rs index 035e8529f..65016391a 100644 --- a/crates/inspectors/tests/it/opcode.rs +++ b/crates/inspectors/tests/it/opcode.rs @@ -8,7 +8,6 @@ fn opcode_gas_records_parent_call_after_child_execution() { let child = address!("0000000000000000000000000000000000001234"); let parent = address!("0000000000000000000000000000000000000022"); - let child_code = Bytecode::new_legacy([op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP].into()); let parent_code = Bytecode::new_legacy( [ op::PUSH1, @@ -33,35 +32,47 @@ fn opcode_gas_records_parent_call_after_child_execution() { .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &child, - AccountInfo { code: Some(child_code), ..Default::default() }, - ); - db.insert_account_info( - &parent, - AccountInfo { code: Some(parent_code), ..Default::default() }, - ); - }); + for (child_code, child_steps, adds) in [ + (vec![op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP], 4, 1), + (vec![op::INVALID], 1, 0), + (vec![0x0c], 0, 0), + ] { + let context = + Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + db.insert_account_info( + &child, + AccountInfo { + code: Some(Bytecode::new_legacy(child_code.into())), + ..Default::default() + }, + ); + db.insert_account_info( + &parent, + AccountInfo { code: Some(parent_code.clone()), ..Default::default() }, + ); + }); - let mut inspector = OpcodeGasInspector::new(); - let mut evm = context.build_mainnet().with_inspector(&mut inspector); - let res = evm - .inspect_tx(TxEnv { - caller: Address::ZERO, - gas_limit: 1_000_000, - kind: TransactTo::Call(parent), - ..Default::default() - }) - .unwrap(); - assert!(res.result.is_success(), "{res:#?}"); + let mut inspector = OpcodeGasInspector::new(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + caller: Address::ZERO, + gas_limit: 1_000_000, + kind: TransactTo::Call(parent), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); - let call = OpCode::new(op::CALL).unwrap(); - let add = OpCode::new(op::ADD).unwrap(); - assert_eq!(evm.inspector().opcode_counts().get(&call), Some(&1)); - assert_eq!(evm.inspector().opcode_counts().get(&add), Some(&1)); - assert!(evm.inspector().opcode_gas().get(&call).copied().unwrap_or_default() > 0); + let call = OpCode::new(op::CALL).unwrap(); + let add = OpCode::new(op::ADD).unwrap(); + assert_eq!(evm.inspector().opcode_counts().get(&call), Some(&1)); + assert_eq!(evm.inspector().opcode_counts().get(&add).copied().unwrap_or_default(), adds); + // Seven PUSHes, CALL and STOP in the parent; undefined bytes are not counted. + assert_eq!(evm.inspector().opcode_counts().values().sum::(), 9 + child_steps); + // The cold CALL costs 2600 regardless of how much gas its child consumes. + assert_eq!(evm.inspector().opcode_gas().get(&call), Some(&2600)); + } } #[test] From 5063c801297b538440287585e624e06ac1eb75a7 Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:11:32 +0100 Subject: [PATCH 109/159] docs: correct inspector and EEST behavior claims (#993) Correct the inspector documentation to describe geth opcode-step recording and distinguish explicitly supplied preimage limits from the capped Default implementation. Update the EEST README to explain that cargo st runs the whole workspace and that supported transition forks execute at their configured activation points. Remove the fuzzer instructions for a nonexistent protobuf importer and the unsupported claim that lossy imports receive feature tags. Co-authored-by: Amp --- crates/cli/fuzzer/README.md | 13 ------------- crates/eest/README.md | 7 ++++--- crates/inspectors/src/tracing/config.rs | 3 ++- crates/inspectors/src/tracing/keccak.rs | 4 ++-- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/crates/cli/fuzzer/README.md b/crates/cli/fuzzer/README.md index 264937de8..790de8482 100644 --- a/crates/cli/fuzzer/README.md +++ b/crates/cli/fuzzer/README.md @@ -55,19 +55,6 @@ Minimize a reproducing case: cargo run -q -p evm2-cli -- fuzzer minimize crates/cli/fuzzer/corpus/failures/case-....json ``` -Import the `evm-protobuf-fuzzer` corpus as replayable JSON cases: - -```sh -./scripts/import_evm_protobuf_corpus.py \ - /path/to/corp-evm-protobuf-fuzzer.tar.xz \ - --output tmp/evm-protobuf-import --clean -cargo run -q -p evm2-cli -- fuzzer corpus tmp/evm-protobuf-import -``` - -The import is intentionally lossy where the fuzzer case format has no field for -the original data, such as block coinbase, difficulty, and excess blob gas. Such -cases are tagged in `features`. - ## Coverage report Generate an HTML coverage report from fuzzer execution, excluding the fuzzer diff --git a/crates/eest/README.md b/crates/eest/README.md index 2d3b7f0e5..a2ce33944 100644 --- a/crates/eest/README.md +++ b/crates/eest/README.md @@ -52,7 +52,7 @@ state and blockchain fixture: cargo st ``` -This is equivalent to: +This runs the whole workspace with all features; to run only the EEST harness: ```sh cargo nextest run -p evm2-eest --all-features --test eest --ignore-default-filter @@ -111,8 +111,9 @@ cargo nextest run -p evm2-eest --test eest --ignore-default-filter blockchain_te This runs every downloaded blockchain-test root: `eest/blockchain_tests`, plus `main/develop/blockchain_tests` when the `main` suite has been downloaded. -Transition forks are skipped. Blocks with a `blockAccessList` build the block -access list and check it against the fixture, including the header hash. +Supported transition forks switch at block 5 or timestamp 15000. Blocks with a +`blockAccessList` build the block access list and check it against the fixture, +including the header hash. ## Filtering diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index a5a3054e5..a958afe41 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -130,7 +130,8 @@ impl TracingInspectorConfig { /// Returns a config for geth style traces. /// - /// This config does _not_ record opcode level traces and is suited for `debug_traceTransaction` + /// This config records opcode-level steps with full stack snapshots and is suited for + /// `debug_traceTransaction` with the default struct logger. /// /// This will configure the default output of geth's default /// [StructLogTracer](alloy_rpc_types_trace::geth::DefaultFrame). diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs index 6a0bf6e2b..2b9f8e420 100644 --- a/crates/inspectors/src/tracing/keccak.rs +++ b/crates/inspectors/src/tracing/keccak.rs @@ -50,8 +50,8 @@ impl Default for Keccak256PreimageInspector { } impl Keccak256PreimageInspector { - /// Returns an inspector that records preimages up to the given limits, which default to - /// [`DEFAULT_MAX_RECORDED_BYTES`]. + /// Returns an inspector that records preimages up to the given limits. + /// [`Default`] caps recorded bytes at [`DEFAULT_MAX_RECORDED_BYTES`]. pub fn new(limits: TraceLimits) -> Self { Self { inner: HashMap::default(), budget: TraceBudget { limits, ..Default::default() } } } From 9867cd5945d9c8a143a10539acbe3545674fe845 Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:11:45 +0100 Subject: [PATCH 110/159] fix(eest): check additional fixture receipt roots (#992) Additional blockchain fixtures check state roots but leave receipt-root comparison disabled, unlike the default blockchain runner. A valid fixture with only its expected receipt root corrupted therefore passes through the additional-fixture runner. Enable receipt-root comparison alongside state-root comparison so additional blockchain fixtures enforce both checks. Co-authored-by: Amp --- crates/eest/src/additional.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/eest/src/additional.rs b/crates/eest/src/additional.rs index 17f87710c..320415129 100644 --- a/crates/eest/src/additional.rs +++ b/crates/eest/src/additional.rs @@ -44,7 +44,11 @@ fn run_file(path: PathBuf) -> Result<(), Failed> { Some(FixtureKind::Blockchain) => execute_str( &path, &input, - BlockchainConfig { compare_state_root: true, ..Default::default() }, + BlockchainConfig { + compare_state_root: true, + compare_receipt_root: true, + ..Default::default() + }, &NameFilter::default(), &mut NoopHook, ) From 7ef9041501f739941ddacbb28ce94534bc15432c Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:11:58 +0100 Subject: [PATCH 111/159] perf(fuzzer): avoid temporary receipt log vectors (#991) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clone logs directly into the combined output instead of cloning each receipt’s log vector first. This removes one temporary allocation per nonempty receipt while preserving log order, individual log cloning, and retained receipts. ### Results Isolated aggregation microbenchmarks; these are not whole-fuzzer throughput measurements. | Receipt log counts | Before | After | Allocation calls | | --- | ---: | ---: | ---: | | [0,2,0,1], 64-byte log data | 122.55 ns | 90.59 ns | 9 → 7 | | [8,8,8,8], 256-byte log data | 1151.39 ns | 971.21 ns | 69 → 65 | Co-authored-by: Amp --- crates/cli/src/fuzzer/normalize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/fuzzer/normalize.rs b/crates/cli/src/fuzzer/normalize.rs index 2ab99391d..c5c93ca46 100644 --- a/crates/cli/src/fuzzer/normalize.rs +++ b/crates/cli/src/fuzzer/normalize.rs @@ -32,7 +32,7 @@ impl Outcome { kind: last.kind, gas_used: last.gas_used, output: last.output.clone(), - logs: receipts.iter().flat_map(|receipt| receipt.logs.clone()).collect(), + logs: receipts.iter().flat_map(|receipt| receipt.logs.iter()).cloned().collect(), state: last.state.clone(), error: last.error.clone(), receipts, From 176898328bd5ca6a695b2ccb36e3df904c78095f Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:12:09 +0100 Subject: [PATCH 112/159] fix(jit): back off AOT failures and count discards (#990) AOT artifact persistence, reload, and link failures remove the compilation entry, allowing subsequent hot lookups to repeatedly dispatch the same failing compilation. Retain the existing kind-scoped failed state instead, preserving waiter notifications and allowing a later JIT request to retry. Also count worker results discarded after a cache clear when calculating published pending jobs. Previously, a completed stale result left pending_jobs permanently inflated. Regression coverage exercises all five AOT failure paths, cross-kind retry, and in-flight compilation across both clear APIs. Co-authored-by: Amp --- crates/jit/runtime/src/runtime/backend.rs | 13 +- crates/jit/runtime/src/runtime/stats.rs | 9 +- crates/jit/runtime/src/runtime/tests.rs | 231 ++++++++++++++++++++-- 3 files changed, 228 insertions(+), 25 deletions(-) diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index fcc76a28c..53810d101 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -575,6 +575,7 @@ impl BackendState { current_gen = self.generation, "discarding stale worker result", ); + self.inner.stats.compilations_discarded.fetch_add(1, Ordering::Relaxed); result.sync_notifier.notify(); return; } @@ -708,7 +709,7 @@ impl BackendState { error = %e, "failed to persist AOT artifact", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); return; } @@ -740,8 +741,8 @@ impl BackendState { error = %e, "failed to link persisted AOT artifact", ); - // Persisted successfully but couldn't link — remove so JIT can retry. - self.remove_entry(&key); + // Back off AOT attempts; admission still allows a JIT request to retry. + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } }, @@ -750,7 +751,7 @@ impl BackendState { code_hash = %key.code_hash, "stored AOT artifact not found on reload", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } Err(e) => { @@ -759,7 +760,7 @@ impl BackendState { error = %e, "failed to reload persisted AOT artifact", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } } @@ -769,7 +770,7 @@ impl BackendState { code_hash = %key.code_hash, "AOT compilation completed but no artifact store configured", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } } diff --git a/crates/jit/runtime/src/runtime/stats.rs b/crates/jit/runtime/src/runtime/stats.rs index f40e8c6ef..1d8d00f5d 100644 --- a/crates/jit/runtime/src/runtime/stats.rs +++ b/crates/jit/runtime/src/runtime/stats.rs @@ -45,6 +45,8 @@ pub(crate) struct RuntimeStats { pub(crate) jit_helper_resume_requests: AtomicU64, /// Total number of out-of-process JIT helper resume request failures. pub(crate) jit_helper_resume_failures: AtomicU64, + /// Total compilation results discarded because the resident cache was cleared. + pub(crate) compilations_discarded: AtomicU64, } /// Gauge values sampled at snapshot time. @@ -131,6 +133,8 @@ pub struct RuntimeStatsSnapshot { pub jit_helper_resume_requests: u64, /// Total number of out-of-process JIT helper resume request failures. pub jit_helper_resume_failures: u64, + /// Total compilation results discarded because the resident cache was cleared while they ran. + pub compilations_discarded: u64, } impl RuntimeStatsSnapshot { @@ -152,7 +156,9 @@ impl RuntimeStats { let dispatched = self.compilations_dispatched.load(Ordering::Relaxed); let succeeded = self.compilations_succeeded.load(Ordering::Relaxed); let failed = self.compilations_failed.load(Ordering::Relaxed); - let pending_jobs = dispatched.saturating_sub(succeeded.saturating_add(failed)); + let discarded = self.compilations_discarded.load(Ordering::Relaxed); + let pending_jobs = + dispatched.saturating_sub(succeeded.saturating_add(failed).saturating_add(discarded)); RuntimeStatsSnapshot { lookup_hits: self.lookup_hits.load(Ordering::Relaxed), @@ -183,6 +189,7 @@ impl RuntimeStats { jit_helper_pause_timeouts: self.jit_helper_pause_timeouts.load(Ordering::Relaxed), jit_helper_resume_requests: self.jit_helper_resume_requests.load(Ordering::Relaxed), jit_helper_resume_failures: self.jit_helper_resume_failures.load(Ordering::Relaxed), + compilations_discarded: discarded, } } } diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index 462711149..385e10168 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -256,6 +256,74 @@ impl ArtifactStore for CountingStore { } } +#[cfg(feature = "llvm")] +#[derive(Clone, Copy, Debug)] +enum AotFailure { + Persist, + ReloadMissing, + ReloadError, + Link, + NoStore, +} + +#[cfg(feature = "llvm")] +struct AotFailureStore { + inner: RuntimeArtifactStore, + failure: AotFailure, +} + +#[cfg(feature = "llvm")] +impl ArtifactStore for AotFailureStore { + fn load_all(&self) -> eyre::Result> { + self.inner.load_all() + } + + fn load(&self, key: &ArtifactKey) -> eyre::Result> { + let Some(mut stored) = self.inner.load(key)? else { return Ok(None) }; + match self.failure { + AotFailure::ReloadMissing => return Ok(None), + AotFailure::ReloadError => eyre::bail!("injected AOT reload failure"), + // A malformed symbol makes linking fail without handing invalid object bytes to LLVM. + AotFailure::Link => stored.manifest.symbol_name.push('\0'), + _ => {} + } + Ok(Some(stored)) + } + + fn store( + &self, + key: &ArtifactKey, + manifest: &ArtifactManifest, + object_bytes: &[u8], + ) -> eyre::Result<()> { + if matches!(self.failure, AotFailure::Persist) { + eyre::bail!("injected AOT persist failure"); + } + self.inner.store(key, manifest, object_bytes) + } + + fn delete(&self, key: &ArtifactKey) -> eyre::Result<()> { + self.inner.delete(key) + } + + fn clear(&self) -> eyre::Result<()> { + self.inner.clear() + } +} + +#[cfg(feature = "llvm")] +static STALE_RESULT_GATE: Mutex, chan::Receiver<()>)>> = Mutex::new(None); + +#[cfg(feature = "llvm")] +fn gated_builtin_address(builtin: evm2_jit_builtins::Builtin) -> usize { + let gate = STALE_RESULT_GATE.lock().unwrap().take(); + if let Some((entered, release)) = gate { + entered.send(()).unwrap(); + release.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + } + builtin.addr::() +} + // =========================================================================== // Tests: startup / basic. // =========================================================================== @@ -983,27 +1051,53 @@ fn failed_compile_is_not_retried_until_cleared() { #[test] #[cfg(feature = "llvm")] fn clear_resident_discards_inflight_jit() { - let tb = - TestBackend::with_tuning_1w(RuntimeTuning { jit_hot_threshold: 1, ..Default::default() }); - - let req = || TestBackend::req_cancun(BYTECODE_RET42); - - // Trigger JIT promotion. - for _ in 0..5 { - let _ = tb.lookup(req()); - } + for clear in [JitBackend::clear_resident, JitBackend::clear_all] { + let (entered_tx, entered_rx) = chan::bounded(1); + let (release_tx, release_rx) = chan::bounded(1); + *STALE_RESULT_GATE.lock().unwrap() = Some((entered_tx, release_rx)); + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + // SAFETY: the resolver returns the base host's unchanged builtin addresses. + host: unsafe { + HostConfig::for_types::().with_resolver(gated_builtin_address) + }, + tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ..Default::default() + }); - // Immediately clear — in-flight JIT results should be discarded. - tb.clear_resident(); + std::thread::scope(|scope| { + let compiling = scope.spawn(|| { + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + }); + entered_rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + clear(&tb); + // An ineligible request completes without a worker. Its notification proves the + // earlier clear command was processed, while the real compilation is still held. + tb.compile_jit_sync(TestBackend::req_cancun(&[])).unwrap(); + assert_eq!(tb.stats().pending_jobs, 1); + release_tx.send(()).unwrap(); + compiling.join().unwrap(); + }); - // Give time for worker to finish and backend to process. - std::thread::sleep(std::time::Duration::from_millis(500)); + let stats = tb.stats(); + assert_eq!(stats.compilations_dispatched, 1); + assert_eq!(stats.compilations_succeeded, 0); + assert_eq!(stats.compilations_failed, 0); + assert_eq!(stats.pending_jobs, 0); + assert_eq!(stats.compilations_discarded, 1); + assert!( + tb.get_compiled(alloy_primitives::keccak256(BYTECODE_RET42), SpecId::CANCUN).is_none() + ); - // The result from the old generation should have been discarded. - assert!( - matches!(tb.lookup(req()), LookupDecision::Unavailable(LookupMissReason::NotReady)), - "stale JIT result should not appear after clear_resident", - ); + // The new generation still compiles and notifies normally. + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + assert!( + tb.get_compiled(alloy_primitives::keccak256(BYTECODE_RET42), SpecId::CANCUN).is_some() + ); + assert_eq!(tb.stats().pending_jobs, 0); + assert_eq!(tb.stats().compilations_succeeded, 1); + assert_eq!(tb.stats().compilations_discarded, 1); + } } #[test] @@ -1447,6 +1541,107 @@ fn prepare_aot_persist_and_load() { assert_eq!(store.len(), 1); } +#[test] +#[cfg(feature = "llvm")] +fn failed_aot_persist_is_not_retried_until_cleared() { + let mut dispatches = Vec::new(); + for failure in [ + AotFailure::Persist, + AotFailure::ReloadMissing, + AotFailure::ReloadError, + AotFailure::Link, + AotFailure::NoStore, + ] { + let store = (!matches!(failure, AotFailure::NoStore)).then(|| { + Arc::new(AotFailureStore { inner: RuntimeArtifactStore::new().unwrap(), failure }) + as Arc + }); + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + aot: store.is_some(), + store, + tuning: RuntimeTuning { + jit_worker_count: 1, + jit_hot_threshold: 1, + idle_evict_duration: None, + ..Default::default() + }, + ..Default::default() + }); + let req = || AotRequest { + code_hash: alloy_primitives::keccak256(BYTECODE_RET42), + code: Bytes::copy_from_slice(BYTECODE_RET42), + spec_id: SpecId::CANCUN, + }; + + // Duplicate requests must all be notified even when persistence/linking fails. + tb.prepare_aot_batch_sync(vec![req(), req(), req()]).unwrap(); + assert_eq!(tb.stats().compilations_failed, 1, "failure case {failure:?}"); + for _ in 0..10 { + if !matches!(failure, AotFailure::NoStore) { + let misses = tb.stats().lookup_misses; + let _ = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + tb.wait_stats(|s| s.lookup_misses > misses); + } + // Also acts as a completion barrier and checks suppressed requests notify callers. + tb.prepare_aot_sync(req()).unwrap(); + } + let attempts = tb.stats().compilations_dispatched; + eprintln!("{failure:?}: dispatched {attempts} AOT attempts before clear"); + dispatches.push(attempts); + + tb.clear_resident(); + tb.prepare_aot_sync(req()).unwrap(); + assert_eq!(tb.stats().compilations_dispatched, attempts + 1); + assert_eq!(tb.stats().compilations_failed, attempts + 1); + + // In JIT mode an explicit AOT failure must not suppress the other compilation kind. + // In AOT mode even compile_jit_sync deliberately requests AOT output. + if matches!(failure, AotFailure::NoStore) { + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + let program = tb.get_compiled(req().code_hash, SpecId::CANCUN).unwrap(); + assert_eq!(program.kind, ProgramKind::Jit); + assert_eq!(tb.stats().compilations_succeeded, 1); + } + assert_eq!(tb.stats().pending_jobs, 0); + } + assert_eq!(dispatches, vec![1; 5]); +} + +#[test] +#[cfg(feature = "llvm")] +fn failed_aot_persist_allows_jit_retry() { + for failure in + [AotFailure::Persist, AotFailure::ReloadMissing, AotFailure::ReloadError, AotFailure::Link] + { + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + store: Some(Arc::new(AotFailureStore { + inner: RuntimeArtifactStore::new().unwrap(), + failure, + })), + tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ..Default::default() + }); + let req = TestBackend::req_cancun(BYTECODE_RET42); + tb.prepare_aot_sync(AotRequest { + code_hash: req.key.code_hash, + code: req.code.clone(), + spec_id: req.key.spec_id, + }) + .unwrap(); + assert_eq!(tb.stats().compilations_failed, 1); + tb.compile_jit_sync(req.clone()).unwrap(); + assert_eq!( + tb.get_compiled(req.key.code_hash, req.key.spec_id).unwrap().kind, + ProgramKind::Jit + ); + assert_eq!(tb.stats().compilations_dispatched, 2); + assert_eq!(tb.stats().compilations_succeeded, 1); + assert_eq!(tb.stats().pending_jobs, 0); + } +} + #[test] #[cfg(feature = "llvm")] fn blocking_aot_mode_compiles_miss_to_aot() { From ee2717b03af6df3e37e218710f52d3967b9e739b Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:12:26 +0100 Subject: [PATCH 113/159] fix(ethereum): derive CREATE address from state nonce (#989) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When nonce checking is disabled, top-level CREATE derives its address from the transaction nonce rather than the sender’s account nonce. A sender at nonce 5 submitting a transaction with nonce 0 therefore deploys to the wrong address, or fails with CreateCollision if that address is occupied. Capture the sender’s nonce before incrementing it and carry it through transaction preparation to CREATE frame construction. Regression coverage exercises legacy, EIP-2930, and EIP-1559 transactions with mismatched transaction nonces and an occupied nonce-zero address. Co-authored-by: Amp --- crates/evm2/src/ethereum/eip1559.rs | 18 ++++-- crates/evm2/src/ethereum/eip2930.rs | 18 ++++-- crates/evm2/src/ethereum/eip4844.rs | 9 +-- crates/evm2/src/ethereum/eip7702.rs | 9 +-- crates/evm2/src/ethereum/legacy.rs | 18 ++++-- crates/evm2/src/ethereum/mod.rs | 7 ++- crates/evm2/src/tests.rs | 89 ++++++++++++++++++++++++++++- 7 files changed, 142 insertions(+), 26 deletions(-) diff --git a/crates/evm2/src/ethereum/eip1559.rs b/crates/evm2/src/ethereum/eip1559.rs index 0f26de59b..8fb93e81c 100644 --- a/crates/evm2/src/ethereum/eip1559.rs +++ b/crates/evm2/src/ethereum/eip1559.rs @@ -72,7 +72,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -85,7 +85,7 @@ pub fn prepare_with_hooks>( let effective_gas_cost = U256::from(tx.gas_limit) * gas_price; H::before_execution(req.host, req.envelope, caller, effective_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-1559 transaction. @@ -93,7 +93,8 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip1559>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = @@ -106,8 +107,15 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; + let frame = prepare_initial_frame( + req.host, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, + &mut tx_gas, + )?; let result = execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( diff --git a/crates/evm2/src/ethereum/eip2930.rs b/crates/evm2/src/ethereum/eip2930.rs index 7f04108d6..a9227f86c 100644 --- a/crates/evm2/src/ethereum/eip2930.rs +++ b/crates/evm2/src/ethereum/eip2930.rs @@ -67,7 +67,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * gas_price; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -79,7 +79,7 @@ pub fn prepare_with_hooks>( H::before_execution(req.host, req.envelope, caller, max_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-2930 transaction. @@ -87,7 +87,8 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip2930>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = @@ -100,8 +101,15 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; + let frame = prepare_initial_frame( + req.host, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, + &mut tx_gas, + )?; let result = execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( diff --git a/crates/evm2/src/ethereum/eip4844.rs b/crates/evm2/src/ethereum/eip4844.rs index 36c76f8c5..4c789bbe6 100644 --- a/crates/evm2/src/ethereum/eip4844.rs +++ b/crates/evm2/src/ethereum/eip4844.rs @@ -84,7 +84,7 @@ pub fn prepare_with_hooks>( let blob_gas_cost = U256::from(DATA_GAS_PER_BLOB) * U256::from(tx.blob_versioned_hashes.len()); let max_blob_gas_cost = blob_gas_cost * max_fee_per_blob_gas; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -98,7 +98,7 @@ pub fn prepare_with_hooks>( let blob_basefee_cost = blob_gas_cost * req.host.block.blob_basefee; H::before_execution(req.host, req.envelope, caller, effective_gas_cost + blob_basefee_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-4844 transaction. @@ -106,7 +106,8 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip4844Variant>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner().tx(); let (execution_gas_limit, reservoir) = @@ -124,7 +125,7 @@ pub fn execute_prepared>( let frame = prepare_initial_frame( req.host, caller, - tx.nonce, + sender_nonce, tx.to.into(), &tx.input, tx.value, diff --git a/crates/evm2/src/ethereum/eip7702.rs b/crates/evm2/src/ethereum/eip7702.rs index 53be3ff30..b5532a005 100644 --- a/crates/evm2/src/ethereum/eip7702.rs +++ b/crates/evm2/src/ethereum/eip7702.rs @@ -84,7 +84,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -97,7 +97,7 @@ pub fn prepare_with_hooks>( let effective_gas_cost = U256::from(tx.gas_limit) * gas_price; H::before_execution(req.host, envelope, caller, effective_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-7702 transaction. @@ -105,7 +105,8 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, super::LazyTxEip7702>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let envelope = req.envelope; let chain_id = req.host.version().chain_id; @@ -187,7 +188,7 @@ pub fn execute_prepared>( let Some(InitialFrame { mut message, charged_state_gas }) = prepare_initial_frame( req.host, caller, - tx.nonce, + sender_nonce, tx.to.into(), &tx.input, tx.value, diff --git a/crates/evm2/src/ethereum/legacy.rs b/crates/evm2/src/ethereum/legacy.rs index 12eec802c..2ed1c126b 100644 --- a/crates/evm2/src/ethereum/legacy.rs +++ b/crates/evm2/src/ethereum/legacy.rs @@ -59,7 +59,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * gas_price; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -70,7 +70,7 @@ pub fn prepare_with_hooks>( H::before_execution(req.host, req.envelope, caller, max_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared legacy transaction. @@ -78,7 +78,8 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxLegacy>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = @@ -91,8 +92,15 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; + let frame = prepare_initial_frame( + req.host, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, + &mut tx_gas, + )?; let result = execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 4032ef05e..102858f98 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -63,6 +63,7 @@ pub struct PreparedTx { pub(super) intrinsic: u64, pub(super) initial_state_gas: u64, pub(super) floor_gas: u64, + pub(super) sender_nonce: u64, } impl From> for TxEnvelope { @@ -425,6 +426,7 @@ pub const fn validate_floor_gas(gas_limit: u64, floor_gas: u64) -> HandlerResult } /// Loads and validates the sender account, then marks it warm and bumps its nonce. +/// Returns the sender's pre-bump state nonce. /// /// One account handle does all three, so the sender is looked up and journaled once. pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( @@ -432,7 +434,7 @@ pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( caller: Address, nonce: u64, max_upfront: U256, -) -> HandlerResult<()> { +) -> HandlerResult { let has_nonce_check = host.feature(EvmFeatures::NONCE_CHECK); let has_balance_check = host.feature(EvmFeatures::BALANCE_CHECK); let has_balance_top_up = host.feature(EvmFeatures::BALANCE_TOP_UP); @@ -459,8 +461,9 @@ pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( } // The sender is warm from the start of the transaction (EIP-2929). sender.warm(); + let sender_nonce = sender.nonce(); sender.bump_nonce(); - Ok(()) + Ok(sender_nonce) } /// Warms the accounts required by every transaction other than the sender, which diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 4934e0fbd..48d191bcd 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -14,7 +14,7 @@ use crate::{ version::GasId, }; use alloc::{vec, vec::Vec}; -use alloy_consensus::{TxEip7702, TxLegacy, transaction::Recovered}; +use alloy_consensus::{TxEip1559, TxEip2930, TxEip7702, TxLegacy, transaction::Recovered}; use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, Log, TxKind, U256, hex}; @@ -337,6 +337,93 @@ fn apply_auth_list_refunds_touched_absent_authority() { assert_eq!(refunds.execution_refund, per_auth); } +#[rstest::rstest] +#[case::legacy(0)] +#[case::eip2930(1)] +#[case::eip1559(2)] +fn top_level_create_uses_sender_state_nonce( + #[case] tx_type: u8, + #[values(false, true)] occupied_first_address: bool, + #[values((false, 0), (false, 9), (true, 5))] nonce_check_and_tx_nonce: (bool, u64), +) { + let (nonce_check, nonce) = nonce_check_and_tx_nonce; + let caller = Address::from([0xbb; 20]); + let destination = caller.create(5); + let first_address = caller.create(0); + let code = legacy_bytecode([op::STOP]); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo { nonce: 5, balance: U256::from(u64::MAX), ..Default::default() }, + ); + if occupied_first_address { + database.insert_account_info( + &first_address, + AccountInfo::default().with_nonce(1).with_code(code.clone()), + ); + } + let mut version = Version::new(SpecId::OSAKA); + version.features.set(EvmFeatures::NONCE_CHECK, nonce_check); + let chain_id = version.chain_id; + let mut evm = TestEvm::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::OSAKA, version), + SpecId::OSAKA, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::OSAKA), + database, + Precompiles::base(SpecId::OSAKA), + ); + // Return one zero byte as deployed STOP code. + let input = [op::PUSH1, 1, op::PUSH1, 0, op::RETURN].into(); + let gas_limit = 100_000; + let value = U256::from(7); + let tx = match tx_type { + 0 => TxEnvelope::Legacy(TxLegacy { + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + 1 => TxEnvelope::Eip2930(TxEip2930 { + chain_id, + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + 2 => TxEnvelope::Eip1559(TxEip1559 { + chain_id, + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + _ => unreachable!(), + }; + let result = evm.transact(&Recovered::new_unchecked(tx, caller)).unwrap().commit(); + assert!(result.status, "{result:?}"); + assert_eq!(result.created_address, Some(destination)); + let created = evm.state.account_info_untracked(&destination).unwrap().unwrap(); + assert_eq!(created.nonce, 1); + assert_eq!(created.balance, value); + assert_eq!(created.code_hash, code.hash_slow()); + assert_eq!(evm.state.account(&caller).unwrap().nonce(), 6); + let first = evm.state.account(&first_address).unwrap(); + if occupied_first_address { + assert_eq!(first.nonce(), 1); + assert_eq!(first.code_hash(), code.hash_slow()); + assert_eq!(first.balance(), U256::ZERO); + } else { + assert!(first.get().is_none()); + } +} + #[test] fn eip2780_touched_absent_authority_pays_no_new_account_state_gas() { let authority = Address::with_last_byte(0xa1); From df1a25f1257cc179714cb68a46e8797ae6584981 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:12:46 +0100 Subject: [PATCH 114/159] refactor(state): keep storage wipe marker when a block deletes an account (#945) `BlockStateAccumulator` dropped an account's storage wipe marker when a transaction deleted the account, on the grounds that a sink applying `current == None` already wipes storage. It then had to re-record the marker when a later transaction in the same block revived the account, because a live account does not wipe storage at the sink. The accumulator now keeps the marker when it deletes an account that existed before the block, and the revival fixup is gone. The marker is not reported while the account stays deleted, in either `visit` or `storage_wipes`, so every sink sees the same changes as before: only the deletion for an account deleted for good, the wipe and the live account for a revival, and the wipe, the new slots and the account for a deleted then recreated account. Tests in the `block.rs` module cover those three sequences and assert the changes a recording sink receives. --- crates/evm2/src/evm/state/block.rs | 188 ++++++++++++++++++++++++----- 1 file changed, 156 insertions(+), 32 deletions(-) diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 75ce25ab3..2ea8a8159 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -46,9 +46,13 @@ impl BlockStateAccumulator { } /// Returns storage-wipe addresses in arbitrary set order. + /// + /// An account deleted by the block is not listed: its deletion already wipes its storage. #[inline] pub fn storage_wipes(&self) -> impl Iterator + '_ { - self.storage_wipes.iter().copied() + self.storage_wipes.iter().copied().filter(|address| { + self.accounts.get(address).is_none_or(|delta| delta.current.is_some()) + }) } /// Returns storage deltas with storage keys in arbitrary map order. @@ -120,13 +124,6 @@ impl StateChangeSink for BlockStateAccumulator { match self.accounts.entry(change.address) { hash_map::Entry::Occupied(mut entry) => { let delta = entry.get_mut(); - // Reviving an account deleted earlier in the block re-records the deletion's - // implied storage wipe: the deletion dropped the wipe marker because applying - // `current == None` wipes storage at the sink, but applying a live account does - // not, so pre-block storage would otherwise leak through the revival. - if delta.current.is_none() && current.is_some() && delta.original.is_some() { - self.storage_wipes.insert(change.address); - } delta.set_current(current); if !delta.is_changed() { entry.remove(); @@ -141,7 +138,13 @@ impl StateChangeSink for BlockStateAccumulator { } if deletes_account { - self.storage_wipes.remove(&change.address); + // A delta left after a deletion means the account existed before the block, so its + // pre-block storage is destroyed. The marker stays so a later revival still wipes it. + if self.accounts.contains_key(&change.address) { + self.storage_wipes.insert(change.address); + } else { + self.storage_wipes.remove(&change.address); + } self.storage.remove(&change.address); } else if created { // Only a newly created delta can have slots recorded against pre-block storage: while @@ -203,13 +206,19 @@ impl StateChangeSink for BlockStateAccumulator { impl StateChangeSource for BlockStateAccumulator { #[inline] fn visit(&self, sink: &mut S) -> Result<(), S::Error> { - visit_block_changes(&self.accounts, &self.storage_wipes, &self.storage, &self.code, sink) + visit_block_changes( + &self.accounts, + &self.storage_wipes_sorted(), + &self.storage, + &self.code, + sink, + ) } } fn visit_block_changes( accounts: &AddressMap>>, - storage_wipes: &AddressSet, + storage_wipes: &[Address], storage: &AddressMap>>, code: &B256Map, sink: &mut S, @@ -220,10 +229,8 @@ fn visit_block_changes( sink.bytecode(code_hash, code)?; } - let mut storage_wipes = storage_wipes.iter().copied().collect::>(); - storage_wipes.sort_unstable(); - for address in &storage_wipes { - sink.storage_wipe(*address)?; + for &address in storage_wipes { + sink.storage_wipe(address)?; } let mut storage_accounts = storage.iter().collect::>(); @@ -261,13 +268,14 @@ fn visit_block_changes( mod tests { use super::{ super::{ - Account, AccountInfo, PendingState, StateChangeSource, StorageOverlay, StorageSlot, - Tracked, + Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSource, StorageChange, + StorageOverlay, StorageSlot, Tracked, }, BlockStateAccumulator, }; use crate::interpreter::Word; use alloy_primitives::{Address, map::U256Map}; + use core::convert::Infallible; use super::super::StateChangeSink; #[cfg(feature = "serde")] @@ -398,28 +406,99 @@ mod tests { assert_eq!(storage[0].1.current, Word::from(7)); } + /// A change a sink observed, in arrival order. + #[derive(Debug, Eq, PartialEq)] + enum Event { + Wipe(Address), + Storage { address: Address, key: Word, original: Word, current: Word }, + Account { address: Address, original: Option, current: Option }, + } + + #[derive(Default)] + struct Recorder(Vec); + + impl StateChangeSink for Recorder { + type Error = Infallible; + + fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { + self.0.push(Event::Account { + address: change.address, + original: change.original.cloned(), + current: change.current.cloned(), + }); + Ok(()) + } + + fn storage_wipe(&mut self, address: Address) -> Result<(), Self::Error> { + self.0.push(Event::Wipe(address)); + Ok(()) + } + + fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { + self.0.push(Event::Storage { + address: change.address, + key: change.key, + original: change.original, + current: change.current, + }); + Ok(()) + } + } + + fn emitted(accumulator: &BlockStateAccumulator) -> Vec { + let mut recorder = Recorder::default(); + accumulator.visit(&mut recorder).expect("recorder is infallible"); + recorder.0 + } + #[test] - fn block_accumulator_restores_wipe_when_deleted_account_is_revived() { + fn block_accumulator_emits_only_the_deletion_for_deleted_account() { + // The deletion wipes the account's storage at a sink by itself, so a sink sees no + // separate wipe even though the accumulator keeps the marker for a later revival. + let address = Address::from([0x58; 20]); + let original = AccountInfo::default().with_balance(Word::from(3)); + let mut accumulator = BlockStateAccumulator::new(); + + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + + assert!(accumulator.storage_wipes_sorted().is_empty()); + assert_eq!( + emitted(&accumulator), + [Event::Account { address, original: Some(original), current: None }] + ); + } + + #[test] + fn block_accumulator_keeps_wipe_when_deleted_account_is_revived() { // Selfdestruct followed by a revival in a later transaction of the same block (a plain - // transfer or a re-create) must keep the deletion's storage wipe: the revived account is - // applied as a live account, which does not wipe storage at the sink by itself. + // transfer or a re-create): the revived account is a live account, which does not wipe + // storage at a sink by itself, so the sink must still see the wipe. let address = Address::from([0x55; 20]); let original = AccountInfo::default().with_balance(Word::from(3)); let revived = AccountInfo::default().with_balance(Word::from(1)); let mut accumulator = BlockStateAccumulator::new(); - let delete = changes(address, Some(original.clone()), None, true, U256Map::default()); - delete.visit(&mut accumulator).expect("block accumulator is infallible"); - assert!(accumulator.storage_wipes_sorted().is_empty(), "deletion subsumes the wipe"); - - let revive = changes(address, None, Some(revived.clone()), false, U256Map::default()); - revive.visit(&mut accumulator).expect("block accumulator is infallible"); + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes(address, None, Some(revived.clone()), false, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); - let accounts = accumulator.accounts_sorted(); - assert_eq!(accounts.len(), 1); - assert_eq!(accounts[0].1.original.as_ref(), Some(&original)); - assert_eq!(accounts[0].1.current.as_ref(), Some(&revived)); assert_eq!(accumulator.storage_wipes_sorted(), [address]); + assert_eq!( + emitted(&accumulator), + [ + Event::Wipe(address), + Event::Account { + address, + original: Some(original.clone()), + current: Some(revived) + }, + ] + ); // Reviving back to a value equal to the original collapses the account delta, but the // wipe still stands: the account's pre-block storage was destroyed. @@ -430,8 +509,53 @@ mod tests { changes(address, None, Some(original), false, U256Map::default()) .visit(&mut collapsing) .expect("block accumulator is infallible"); - assert!(collapsing.accounts_sorted().is_empty()); - assert_eq!(collapsing.storage_wipes_sorted(), [address]); + assert_eq!(emitted(&collapsing), [Event::Wipe(address)]); + } + + #[test] + fn block_accumulator_emits_wipe_and_new_slots_when_deleted_account_is_recreated() { + let address = Address::from([0x59; 20]); + let (kept, rewritten) = (Word::from(1), Word::from(2)); + let original = AccountInfo::default().with_balance(Word::from(3)); + let recreated = AccountInfo::default().with_nonce(1); + let mut accumulator = BlockStateAccumulator::new(); + + // A pre-block slot changes, then the account is deleted, then re-created with a new slot. + changes( + address, + Some(original.clone()), + Some(original.clone()), + false, + slot(kept, Word::from(5), Word::from(6)), + ) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes( + address, + None, + Some(recreated.clone()), + true, + slot(rewritten, Word::ZERO, Word::from(7)), + ) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + + assert_eq!( + emitted(&accumulator), + [ + Event::Wipe(address), + Event::Storage { + address, + key: rewritten, + original: Word::ZERO, + current: Word::from(7), + }, + Event::Account { address, original: Some(original), current: Some(recreated) }, + ] + ); } #[test] From a394786a58d75c7597dc3a2a5fdfd8a75639ec7d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:17:48 +0100 Subject: [PATCH 115/159] refactor: share the create pre-access failure checks (#949) A create that fails before touching its destination (call depth over the limit, creator balance below the endowment, creator nonce at `u64::MAX`) was checked in three places: the host's create message path, the EIP-8037 branch of the CREATE instruction, and the JIT builtin for CREATE. Under Amsterdam a nested create must fail these before the state-gas charge, so the instruction repeated the host's checks by hand. This adds `create_preaccess_failure` next to `derive_create_destination` and makes all three call it. It returns the stop reason in the existing order (depth, balance, nonce). The nonce check still applies only to nested creates, because the transaction bumps the nonce of a top-level create. The host still runs the check for older forks and the top level, so pre-Amsterdam failures keep going through the message path and inspector create hooks fire where they did. A create deeper than the limit still returns before the host reads the creator account. --- crates/evm2/src/evm/mod.rs | 35 +++++--------- .../src/interpreter/instructions/system.rs | 13 +++-- crates/evm2/src/interpreter/message.rs | 48 ++++++++++++++++++- crates/evm2/src/interpreter/mod.rs | 4 +- crates/jit/builtins/src/lib.rs | 16 +++++-- 5 files changed, 84 insertions(+), 32 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 32632ef9d..449ca6209 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -124,7 +124,7 @@ use crate::{ env::{BlockEnv, TxEnv}, interpreter::{ Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Message, MessageKind, - MessageResult, MessageResultExt, Word, + MessageResult, MessageResultExt, Word, create_preaccess_failure, }, registry::{HandlerResult, TxRegistry}, trustme, @@ -1463,13 +1463,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { &mut self, message: &mut Message, ) -> Result, ExecutionError> { - if message.depth > self.version().max_call_depth() { - return Ok(Self::error_message_result( - InstrStop::CallTooDeep, - message.gas_limit, - message.reservoir, - )); - } if let Err(error) = self.prepare_create_message(message) { return match error { HostError::Halt(stop) => { @@ -1509,20 +1502,18 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn prepare_create_message(&mut self, message: &mut Message) -> Result<(), HostError> { let nested = message.depth > self.version().initial_call_depth; - let info = if message.value > 0 || nested { - self.state.account_info_untracked(&message.caller)? - } else { - None - }; - - if message.value > 0 && info.as_ref().is_none_or(|info| info.balance < message.value) { - return Err(InstrStop::OutOfFunds.into()); - } - - // EIP-2681 caps account nonces at u64::MAX; CREATE/CREATE2 fail the frame and return - // zero instead of wrapping or saturating the creator nonce. - if nested && info.as_ref().is_some_and(|info| info.nonce == u64::MAX) { - return Err(InstrStop::NonceOverflow.into()); + // A too-deep create fails on depth alone, without reading the creator. + let info = + if message.depth <= self.version().max_call_depth() && (message.value > 0 || nested) { + self.state.account_info_untracked(&message.caller)? + } else { + None + }; + let (balance, nonce) = info.as_ref().map_or((Word::ZERO, 0), |i| (i.balance, i.nonce)); + if let Some(stop) = + create_preaccess_failure(balance, nonce, message.value, message.depth, self.version()) + { + return Err(stop.into()); } // `destination` already holds the contract address (derived when the message was diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index c57af126e..f606427e4 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -5,7 +5,7 @@ use crate::{ bytecode::Bytecode, interpreter::{ Gas, Host, InstrStop, InterpreterState, Message, MessageExt, MessageKind, MessageResult, - Result, StackMut, Word, derive_create_destination, + Result, StackMut, Word, create_preaccess_failure, derive_create_destination, }, utils::WordExt, version::GasId, @@ -418,9 +418,14 @@ pub fn create_with( if state.feature(EvmFeatures::EIP8037) { // `caller_info` is always `Some` when EIP-8037 is enabled. if let Some(caller_info) = caller_info.as_ref() - && (caller_info.balance < value - || caller_info.nonce == u64::MAX - || depth > state.version().max_call_depth()) + && create_preaccess_failure( + caller_info.balance, + caller_info.nonce, + value, + depth, + state.version(), + ) + .is_some() { state.clear_return_data(); stack.push_u64(0)?; diff --git a/crates/evm2/src/interpreter/message.rs b/crates/evm2/src/interpreter/message.rs index 274a9e96f..c2da3b326 100644 --- a/crates/evm2/src/interpreter/message.rs +++ b/crates/evm2/src/interpreter/message.rs @@ -1,4 +1,8 @@ -use crate::{BaseEvmTypes, EvmTypesHost, bytecode::Bytecode}; +use crate::{ + BaseEvmTypes, EvmTypesHost, Version, + bytecode::Bytecode, + interpreter::{InstrStop, Word}, +}; use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; /// EVM message kind. @@ -172,9 +176,51 @@ pub fn derive_create_destination( } } +/// Returns the reason a create message fails before its destination is accessed, if any. +/// +/// `balance` and `nonce` are the creator's, and are zero for an absent account. Failures are +/// checked in order: call depth, insufficient balance for `value`, then, for a nested create +/// (`depth` above [`Version::initial_call_depth`]), a creator nonce at `u64::MAX` (EIP-2681). The +/// transaction itself bumps the nonce of a top-level create, so that one skips the nonce check. +#[inline] +pub fn create_preaccess_failure( + balance: Word, + nonce: u64, + value: Word, + depth: u16, + version: &Version, +) -> Option { + if depth > version.max_call_depth() { + Some(InstrStop::CallTooDeep) + } else if balance < value { + Some(InstrStop::OutOfFunds) + } else if depth > version.initial_call_depth && nonce == u64::MAX { + Some(InstrStop::NonceOverflow) + } else { + None + } +} + #[cfg(test)] mod tests { use super::*; + use crate::SpecId; + + #[test] + fn create_preaccess_failure_order_and_nesting() { + let mut version = Version::new(SpecId::CANCUN); + version.initial_call_depth = 5; + let max = version.max_call_depth(); + let check = |balance: u64, nonce: u64, value: u64, depth: u16| { + create_preaccess_failure(Word::from(balance), nonce, Word::from(value), depth, &version) + }; + + assert_eq!(check(0, u64::MAX, 1, max + 1), Some(InstrStop::CallTooDeep)); + assert_eq!(check(0, u64::MAX, 1, max), Some(InstrStop::OutOfFunds)); + assert_eq!(check(1, u64::MAX, 1, max), Some(InstrStop::NonceOverflow)); + assert_eq!(check(1, u64::MAX, 1, version.initial_call_depth), None); + assert_eq!(check(0, 0, 0, max), None); + } #[test] fn is_static_follows_caller_or_staticcall_kind() { diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index 5882aafba..026c43565 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -32,7 +32,9 @@ mod memory; pub use memory::Memory; mod message; -pub use message::{Message, MessageExt, MessageKind, derive_create_destination}; +pub use message::{ + Message, MessageExt, MessageKind, create_preaccess_failure, derive_create_destination, +}; mod host; pub use host::{Host, MessageResult, MessageResultExt}; diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index f0bd89cbe..5cfeb59c4 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -18,7 +18,10 @@ use evm2::{ BaseEvmTypes, EvmTypesHost, bytecode::Bytecode, constants::BLOCK_HASH_HISTORY, - interpreter::{Host, MessageExt, MessageKind, Word, derive_create_destination, i256}, + interpreter::{ + Host, MessageExt, MessageKind, Word, create_preaccess_failure, derive_create_destination, + i256, + }, utils::read_word_padded, version::{EvmFeatures, GasId}, }; @@ -681,9 +684,14 @@ pub unsafe fn __revmc_builtin_create( if ecx.enables(EvmFeatures::EIP8037) { // `caller_info` is always `Some` when EIP-8037 is enabled. if let Some(caller_info) = caller_info.as_ref() - && (caller_info.balance < value - || caller_info.nonce == u64::MAX - || depth > ecx.version().max_call_depth()) + && create_preaccess_failure( + caller_info.balance, + caller_info.nonce, + value, + depth, + ecx.version(), + ) + .is_some() { unsafe { sp.write(EvmWord::ZERO); From a8e9ded55d9ba2741b5f4d042bd6047e49b8079f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:19:38 +0100 Subject: [PATCH 116/159] refactor: move step gas cost from the interpreter to inspectors (#956) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The interpreter carried bookkeeping for one consumer, the step cost that inspectors report: a `step_gas_before` field written in `inspect_step`, a `step_gas_after_charge` marker set by `finish_step_charge` in `call_with` and `create_with` and `Interpreter::step_cost`. This PR deletes all of it from core and leaves `inspect_step` and `inspect_step_end` with the same signatures and the same pc and stack length stores. `inspect_step_end` still sets the remaining gas of an out-of-gas step to 0 before the hook runs, because an out-of-gas frame has burned all its gas. Each inspector now derives the cost from the gas it records itself. `TracingInspector` records the gas in `step`, takes the difference in `step_end`, and for CALL and CREATE reads the parent's gas in the `call` or `create` hook, which runs after the opcode charged and before the child runs. A call's cost includes the forwarded gas; a create's does not, so the hook adds the message's gas limit back. The JS tracer, `OpcodeGasInspector` and the EIP-3155 tracer in `evm2-eest` follow the same rule. A step that stops out of gas costs the whole remainder it started with, except the reentrancy sentry stop, which costs nothing. That value comes from the stop result and the gas recorded at `step`, never from the counter a failed charge leaves behind, so it does not change whether a failed `spend` wraps the counter or leaves it alone. The gas the hooks see was already current on every dispatch backend, so this PR adds no store to the message path. Instructions get `&mut Gas` as a split borrow of the interpreter's own gas, not a copy, and tco writes its register back before an inspected instruction. The step cost tests, which pin the call and create costs exactly, pass on tco, packed, single_return and unpacked, and on the default and packed backends in the bench profile (thin LTO, one codegen unit). Reported step costs are unchanged in the inspector tests (struct logs, parity vmTrace, JS tracer, opcode gas) and in the EEST suites. The geth parity harness of #859 merged with this branch reports 0 new differences and 0 stale known entries over its 360 case and fork pairs (5,470 comparisons). Two points are worth stating. The inspectors read an out-of-gas step from the stop result and the gas recorded at `step`, not from the counter a failed charge leaves behind, so the values come out the same whether a failed `spend` wraps the counter or leaves it unchanged; every inspector test passes with `spend` changed to leave it alone in a local scratch commit. The rule differs only for the reentrancy sentry stop, where `TracingInspector` and `OpcodeGasInspector` report 0 and the JS tracer and the EIP-3155 tracer report the whole remainder; those two values are as on main and the split is deliberate here. The second point is the `before` hook of `call_with` and `create_with`: it now runs before the inspector's `call` or `create` hook, so gas it charges counts toward the step cost, where it was excluded before. Nothing in the repository charges gas there. `Inspector::step_end` now documents that the remaining gas reads 0 after an out-of-gas stop. The no-inspector path does not get slower. Callgrind counts of the `evm` bench's measured section, taken one valgrind at a time on same-length base (main) and head checkouts (`cargo bench -p evm2-cli --bench evm`, built for x86-64-v3, all 100 cases in one run per backend). The default backend resolves to tco on the nightly used here. | Backend | Metric | Base | Head | Change | | --- | --- | ---: | ---: | ---: | | default (tco) | Ir | 6,986,205,887 | 6,985,654,403 | -0.008% 🟢 | | default (tco) | Dr | 1,790,396,551 | 1,790,862,149 | +0.026% 🔴 | | default (tco) | Dw | 774,898,704 | 774,905,549 | +0.001% 🔴 | | packed | Ir | 9,402,613,100 | 9,402,146,983 | -0.005% 🟢 | | packed | Dr | 2,045,748,225 | 2,045,769,017 | +0.001% 🔴 | | packed | Dw | 1,039,670,736 | 1,039,682,102 | +0.001% 🔴 | Instruction counts per case, one run each (default backend / packed): | Case | Base | Head | Change | Base | Head | Change | | --- | ---: | ---: | ---: | ---: | ---: | ---: | | snailtracer | 499,758,180 | 499,546,119 | -0.042% 🟢 | 745,694,466 | 745,444,037 | -0.034% 🟢 | | erc20_transfer | 29,364,848 | 29,337,183 | -0.094% 🟢 | 38,192,996 | 38,165,347 | -0.072% 🟢 | | uniswap_v2_pair | 58,640 | 58,928 | +0.491% 🔴 | 60,109 | 60,397 | +0.479% 🔴 | | counter | 58,609 | 58,685 | +0.130% 🔴 | 59,289 | 59,373 | +0.142% 🔴 | The few hundred instructions that the small cases gain are libc allocator work (`_int_malloc`, `malloc_consolidate`, `unlink_chunk`), which moves with the heap layout of the smaller `Interpreter`, not interpreter or dispatch code. An inspected run does not get slower either, measured before the zeroing line returned; that line runs only on an inspected out-of-gas step, which this transaction has none of. Callgrind counts the whole `evm2 t8n` process (built for x86-64-v3, profiling profile, same-length base and head checkouts with the geth harness of #859 merged into both) on a transaction that makes 10,000 calls to a child contract, 1.56 million gas, and the trace files are byte-identical on both sides. | Tracer | Base | Head | Change | | --- | ---: | ---: | ---: | | EIP-3155 struct logs (`--trace`) | 4,363,971,506 | 4,363,800,440 | -0.004% 🟢 | | struct logs (`--trace.structlog`) | 820,348,199 | 819,505,586 | -0.103% 🟢 | | JS step tracer | 4,842,099,776 | 4,840,921,087 | -0.024% 🟢 | | no tracer | 27,206,907 | 27,160,119 | -0.172% 🟢 | --- crates/eest/src/trace.rs | 9 ++- crates/evm2/src/evm/inspector.rs | 2 + .../src/interpreter/instructions/system.rs | 6 +- crates/evm2/src/interpreter/runtime.rs | 38 +---------- crates/inspectors/src/opcode.rs | 18 +++-- crates/inspectors/src/tracing/js/mod.rs | 8 ++- crates/inspectors/src/tracing/mod.rs | 66 ++++++++++++++++--- 7 files changed, 84 insertions(+), 63 deletions(-) diff --git a/crates/eest/src/trace.rs b/crates/eest/src/trace.rs index 3429198ff..21dfdd1b9 100644 --- a/crates/eest/src/trace.rs +++ b/crates/eest/src/trace.rs @@ -14,7 +14,7 @@ use alloy_primitives::{B256, Bytes, U256}; use evm2::{ EvmTypesHost, Inspector, - interpreter::{Interpreter, Message, MessageResult, opcode::OpCode}, + interpreter::{InstrStop, Interpreter, Message, MessageResult, opcode::OpCode}, }; use serde_json::json; use std::io::{self, Write}; @@ -65,7 +65,12 @@ impl Eip3155Tracer { let Some(step) = self.pending.take() else { return; }; - let gas_cost = step.gas.saturating_sub(interp.gas().remaining()); + // An out-of-gas step consumes the whole remainder it started with. + let gas_cost = if interp.result().is_err_and(InstrStop::is_out_of_gas) { + step.gas + } else { + step.gas.saturating_sub(interp.gas().remaining()) + }; self.emit(&step, gas_cost); } diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index a170cb452..4cf5ca7b3 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -136,6 +136,8 @@ pub trait Inspector: NonStaticAny { } /// Called after each instruction executes. + /// + /// If the instruction ran out of gas, the interpreter's remaining gas reads 0. #[inline] fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { let _ = interp; diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index f606427e4..575bf1182 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -135,7 +135,6 @@ fn load_acc_and_calc_gas( stack_gas_limit }; gas.spend(gas_limit)?; - state.finish_step_charge(gas); if transfers_value { gas_limit = gas_limit.saturating_add(state.gas_params().get(GasId::CallStipend)); @@ -274,8 +273,7 @@ fn call_inner( /// It may charge or credit `gas` or fail the instruction. A fatal child result returns before /// `after` runs. /// -/// An `Err` from either hook stops the instruction with that error. Gas the hooks charge is not -/// part of the step cost reported to inspectors. +/// An `Err` from either hook stops the instruction with that error. /// /// The result can be the stop for a config reload. Return it as the last step of the instruction. #[inline] @@ -444,8 +442,6 @@ pub fn create_with( charged_create_state_gas = true; } } - // The state gas charged above spills into regular gas but stays out of the step cost. - state.finish_step_charge(gas); let gas_limit = if state.feature(EvmFeatures::EIP150) { state.gas_params().call_stipend_reduction(gas.remaining()) } else { diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index f440032de..940835752 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -59,11 +59,6 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) stack: Box, pub(in crate::interpreter) gas: Gas, - /// Remaining gas plus spilled state gas at the start of the current inspected step. - step_gas_before: u64, - /// Remaining gas plus spilled state gas once the current step finished charging, if it - /// marked that point. - step_gas_after_charge: Option, pub(in crate::interpreter) result: Result, error: Option, spec: SpecId, @@ -103,8 +98,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { bytecode_ref: None, stack_len: 0, gas: Gas::new(0), - step_gas_before: 0, - step_gas_after_charge: None, memory: Memory::new(), result: Ok(()), error: None, @@ -259,26 +252,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.gas = gas; } - /// Returns the execution gas the current step charged, before its effects. - /// - /// This is the gas remaining at the start of the step minus the gas remaining once the step - /// finished charging, leaving out state gas that spills into the remaining gas because the - /// reservoir was short or is refilled from it. State gas is not part of the execution cost; - /// [`Gas::state_gas_spent`] reports it. Call and create steps finish charging right before - /// they hand gas to the child, so their cost includes the forwarded gas for calls and - /// excludes it for creates. Any other step finishes charging when it ends, which makes its - /// cost the execution gas it consumed. - /// - /// Only meaningful in [`Inspector::step_end`](crate::inspector::Inspector::step_end). - #[inline] - pub const fn step_cost(&self) -> u64 { - let after = match self.step_gas_after_charge { - Some(after) => after, - None => self.gas.remaining_excluding_state_spill(), - }; - self.step_gas_before.saturating_sub(after) - } - /// Returns the current linear memory. #[inline] pub const fn memory(&self) -> &Memory { @@ -868,8 +841,6 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { pub(crate) fn inspect_step(&mut self, pc: Pc, stack_len: usize) { self.0.pc = pc.as_ptr(); self.0.stack_len = stack_len; - self.0.step_gas_before = self.0.gas.remaining_excluding_state_spill(); - self.0.step_gas_after_charge = None; unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step(&mut self.0); @@ -890,7 +861,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { } else if self.0.result.is_err() { cold_path(); if self.0.result.is_err_and(InstrStop::is_out_of_gas) { - // Failed charges may leave a wrapped counter until frame settlement. + // An out-of-gas frame has burned all its gas, so 0 is the real remaining gas. self.0.gas.set_remaining(0); } } @@ -907,13 +878,6 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { } } - /// Marks that the current step finished charging gas, so [`Interpreter::step_cost`] stops - /// counting gas after this point. - #[inline] - pub(crate) const fn finish_step_charge(&mut self, gas: &Gas) { - self.0.step_gas_after_charge = Some(gas.remaining_excluding_state_spill()); - } - #[inline] pub(crate) fn inspect_selfdestruct( &mut self, diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index 8da762b6e..41eb39338 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -100,16 +100,14 @@ impl Inspector for OpcodeGasInspector { if let Some(pending) = self.pending_steps.pop() && let Some(opcode) = pending.opcode { - // The sentry rejects SSTORE before charging gas, but out-of-gas hooks already - // see the zeroed frame remainder. - let gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { - 0 - } else { - pending - .gas_remaining - .saturating_sub(interp.gas().remaining()) - .saturating_sub(pending.child_gas_used) - }; + // An out-of-gas step consumes the whole remainder it started with, except the + // reentrancy sentry, which rejects SSTORE before charging gas. + let gas_cost = match interp.result() { + Err(InstrStop::ReentrancySentryOOG) => 0, + Err(stop) if stop.is_out_of_gas() => pending.gas_remaining, + _ => pending.gas_remaining.saturating_sub(interp.gas().remaining()), + } + .saturating_sub(pending.child_gas_used); *self.opcode_gas.entry(opcode).or_default() += gas_cost; } } diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 3749074f5..53f2f0da1 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -567,8 +567,14 @@ impl JsInspector { }; let refund = call.enclosing_refund + 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 + } else { + interp.gas().spent().saturating_sub(pending.gas_spent_before) + }; let info = StepInfo { - cost: interp.gas().spent().saturating_sub(pending.gas_spent_before), + cost, refund: refund.max(0) as u64, // Geth reports the depth starting at 1 for the top-level call. depth: u64::from(depth) + 1, diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index bcf3d681b..e047167e4 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -99,7 +99,7 @@ pub struct TracingInspector { /// /// This is a stack because nested calls are executed between the `step` and `step_end` of the /// call instruction itself. A `usize::MAX` step index marks a step that was not recorded. - step_stack: Vec<(usize, usize)>, + step_stack: Vec, /// Number of opcode steps captured across all calls since the last reset. recorded_steps: u64, /// The spec id of the EVM. @@ -457,7 +457,7 @@ impl TracingInspector { && self.config.step_limit.is_none_or(|limit| self.recorded_steps < limit.get()); if !record { // Push a sentinel so that the upcoming `step_end` stays paired with this step. - self.step_stack.push((trace_idx, usize::MAX)); + self.step_stack.push(PendingStep::new(trace_idx, usize::MAX, 0)); return; } @@ -547,7 +547,11 @@ impl TracingInspector { }); node.ordering.push(TraceMemberOrder::Step(step_idx)); - self.step_stack.push((trace_idx, step_idx)); + self.step_stack.push(PendingStep::new( + trace_idx, + step_idx, + interp.gas().remaining_excluding_state_spill(), + )); if self.config.record_step_deltas { let write_range = memory_write_range(op.get(), interp.stack().as_slice()); @@ -568,12 +572,30 @@ impl TracingInspector { } } + /// Records the gas a call or create step has charged, when its message starts. + /// + /// Invoked on [Inspector::call] and [Inspector::create], where the parent has charged the step, + /// including the gas forwarded to the child, and the child has not returned any gas yet. A + /// call step costs the forwarded gas too, a create step does not, so `forwarded` is the gas to + /// give back. + fn finish_step_charge(&mut self, interp: &Interpreter<'_, '_, T>, forwarded: u64) { + // The step's own message is the first one it starts; a precompile may start more. + if let Some(pending) = self.step_stack.last_mut() + && pending.gas_after_charge.is_none() + { + pending.gas_after_charge = + Some(interp.gas().remaining_excluding_state_spill().saturating_add(forwarded)); + } + } + /// Fills the current trace with the output of a step. /// /// Invoked on [Inspector::step_end]. #[cold] fn fill_step_on_step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { - let Some((trace_idx, step_idx)) = self.step_stack.pop() else { + let Some(PendingStep { trace_idx, step_idx, gas_before, gas_after_charge }) = + self.step_stack.pop() + else { return; }; let node = &mut self.traces.arena[trace_idx]; @@ -605,12 +627,20 @@ impl TracingInspector { ); } - // An out-of-gas step consumes the whole remainder, except the reentrancy sentry, which - // halts before any charge. - step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { + // A call or create step is charged once its message starts, which includes the gas + // forwarded by a call and excludes it for a create. An out-of-gas step consumes the whole + // remainder it started with, except the reentrancy sentry, which halts before any charge. + // Any other step is charged when it ends. State gas that spills into the remaining gas is + // not part of the cost. + let result = interp.result(); + step.gas_cost = if result == Err(InstrStop::ReentrancySentryOOG) { 0 + } else if let Some(after) = gas_after_charge { + gas_before.saturating_sub(after) + } else if result.is_err_and(InstrStop::is_out_of_gas) { + step.gas_remaining } else { - interp.step_cost() + gas_before.saturating_sub(interp.gas().remaining_excluding_state_spill()) }; let state_gas_delta = interp.gas().state_gas_spent().saturating_sub(step.state_gas_spent); if step.state_gas_reservoir.is_some() && state_gas_delta != 0 { @@ -751,6 +781,7 @@ impl Inspector for TracingInspector { self.spec_id = Some(interp.spec()); } self.features = interp.version().features; + self.finish_step_charge(interp, 0); // determine correct `from` and `to` based on the call scheme let (from, to) = match message.kind { @@ -807,6 +838,7 @@ impl Inspector for TracingInspector { self.spec_id = Some(interp.spec()); } self.features = interp.version().features; + self.finish_step_charge(interp, message.gas_limit); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) @@ -955,3 +987,21 @@ struct TraceStackEntry { trace_idx: usize, log_count: usize, } + +/// A step waiting for `step_end`. +#[derive(Clone, Copy, Debug)] +struct PendingStep { + trace_idx: usize, + step_idx: usize, + /// The remaining gas plus spilled state gas when the step started. + gas_before: u64, + /// The same measure once a call or create step charged everything it charges before its + /// message starts. + gas_after_charge: Option, +} + +impl PendingStep { + const fn new(trace_idx: usize, step_idx: usize, gas_before: u64) -> Self { + Self { trace_idx, step_idx, gas_before, gas_after_charge: None } + } +} From 05308df3163fa5fd841576831d804250fb122634 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:21:25 +0100 Subject: [PATCH 117/159] fix(evm): fire frame hooks for a top-level frame that runs out of gas at runtime (#871) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the runtime gas phase of a transaction runs out of gas (EIP-2780 new-account state gas of a call, create state gas of a create transaction, EIP-7702 authorization charges or the delegation-target access), evm2 settled the transaction as an out-of-gas halt without a frame, so an installed inspector never saw a `call` or `create` hook and no matching end hook. The tracing inspector rebuilt the missing call-tracer root after the fact from the transaction, which left every other consumer without a root. go-ethereum's `haltTopFrame` (`core/state_transition.go:944-970`, called from `executeCall` and `executeCreate`, lines 836-900) fires `OnEnter` at depth 0 with the call or create the transaction would have run and then `OnExit` with `ErrOutOfGas` and `reverted = true`. `prepare_initial_frame` now returns the first frame in every case. When a charge fails, the frame comes from the new `halted_initial_frame` and has `runtime_oog` set; `execute_initial_frame` halts it through `Evm::execute_halted_message`, which fires the call or create hook with that message and gives the matching end hook the unchanged `runtime_oog_result`. The message carries the call or create kind, destination (the derived address for a create), caller, input, value and the budget the phase started with, and no bytecode. It reads no account state, so a recipient the budget could not afford stays out of the EIP-7928 block access list, and the EIP-7702 handler still reverts its authorization checkpoint before the hooks fire. The result handed to settlement is the one the transaction used before, so gas, state and logs do not change. A hook cannot replace the halted result. The tracing inspector's `recover_runtime_oog_root` and its callers in the debug and mux inspectors are removed, together with the test that covered the removed guard; the call-tracer regression test now expects the gas fields a recorded root carries. Parity harness (#859, geth master `a0cba163`), new case `runtime_oog_top_frame` on Amsterdam: a value call with calldata to an empty account, a create transaction, and a set-code transaction whose authorization charge fails, each with too little gas for its state charge. Same case on main and on this branch, fields that differ from geth: | tracer | field | main | this PR | | --- | --- | --- | --- | | `4byte` | `0x01020304-1` | 🔴 missing, evm2 never saw the call | 🟢 equal | | `jsonl` | `[].error`, `[].gasUsed` of the exit line | 🔴 no error, gas used 0 | 🟢 equal | | `mux` | `4byteTracer.0x01020304-1` | 🔴 missing | 🟢 equal | | `call`, `call-log`, `call-top`, `js-frames` | core fields | equal (the removed rebuild covered the call tracer) | equal | | `call`, `call-log`, `call-top`, `structlog`, `mux` | `executionGasUsed`, `stateGasUsed`, `gasRefund` | equal, the rebuilt root had none | 🔴 present, as on every frame; geth has no such fields (known difference) | The JavaScript tracer already matched, and the `flat` tracer keeps its two known differences (error text and `result: null`). The prestate tracer is built from the transaction's touched accounts rather than hooks, so it is unchanged: on the set-code transaction geth reports the recipient and evm2 does not, because evm2 deliberately never loads it. That stays a known difference of the case and is not part of this change. The case is not committed here because the harness is not on main yet; I add it, with the known-diffs entries for the classes above, once #859 lands. Benchmarks, `cargo bench -p evm2-cli --bench evm` interpreter shard, pinned to one core, median of three runs, main against this branch: | bench | main | this PR | ratio | | --- | --- | --- | --- | | weth | 1.938 µs | 1.928 µs | 0.995 | | erc20_transfer | 1280.5 µs | 1282.7 µs | 1.002 | | uniswap_v2_pair | 1.361 µs | 1.375 µs | 1.010 | | univ2_router | 1.400 µs | 1.402 µs | 1.001 | | seaport | 1.589 µs | 1.542 µs | 0.971 | | usdc_proxy | 1.472 µs | 1.498 µs | 1.018 | | universal_router | 29.931 µs | 30.059 µs | 1.004 | | uniswap_v3_pool | 9.063 µs | 9.126 µs | 1.007 | | snailtracer | 29201 µs | 29222 µs | 1.001 | | factorial | 28.602 µs | 28.559 µs | 0.998 | The ratios stay within ±3% in both directions with no consistent sign, which is the run-to-run noise on this machine. --- crates/evm2/src/ethereum/eip7702.rs | 69 +++++++------- crates/evm2/src/ethereum/mod.rs | 91 +++++++++++++++---- crates/evm2/src/evm/inspector.rs | 90 ++++++++++++++++++ crates/evm2/src/evm/mod.rs | 30 +++++- crates/evm2/src/tests.rs | 1 - crates/inspectors/src/tracing/debug.rs | 2 - crates/inspectors/src/tracing/erc7562.rs | 1 - crates/inspectors/src/tracing/mod.rs | 36 +------- crates/inspectors/src/tracing/mux.rs | 4 - .../inspectors/tests/it/geth/runtime_oog.rs | 83 +++++++++++++---- 10 files changed, 295 insertions(+), 112 deletions(-) diff --git a/crates/evm2/src/ethereum/eip7702.rs b/crates/evm2/src/ethereum/eip7702.rs index b5532a005..5a386b908 100644 --- a/crates/evm2/src/ethereum/eip7702.rs +++ b/crates/evm2/src/ethereum/eip7702.rs @@ -1,9 +1,9 @@ use super::{ - InitialFrame, LazyAuthorization, PreparedTx, access_list_counts, effective_gas_price, - initial_gas_and_reservoir, intrinsic_and_floor_gas, prepare_initial_frame, runtime_oog_result, - settle_initial_frame_gas, validate_block_gas_limit, validate_chain_id, - validate_create_initcode, validate_execution_gas_limit_cap, validate_floor_gas, - validate_gas_price, validate_intrinsic_gas, validate_nonce_not_overflow, validate_priority_fee, + LazyAuthorization, PreparedTx, access_list_counts, effective_gas_price, execute_initial_frame, + halted_initial_frame, initial_gas_and_reservoir, intrinsic_and_floor_gas, + prepare_initial_frame, validate_block_gas_limit, validate_chain_id, validate_create_initcode, + validate_execution_gas_limit_cap, validate_floor_gas, validate_gas_price, + validate_intrinsic_gas, validate_nonce_not_overflow, validate_priority_fee, validate_sender_and_bump_nonce, validate_tx_gas_limit_cap, warm_access_list, warm_base_accounts, }; @@ -11,7 +11,7 @@ use crate::{ Evm, EvmFeatures, EvmTypes, TxResult, Version, env::TxEnvExt, evm::handler::{DefaultTxHandlerHooks, GasSettlement, TxHandlerHooks}, - interpreter::{GasTracker, Host, InstrStop, MessageResult, gas::EIP8038_ACCOUNT_WRITE}, + interpreter::{GasTracker, InstrStop, MessageResult, gas::EIP8038_ACCOUNT_WRITE}, registry::{HandlerError, HandlerResult, TxRequest}, version::GasId, }; @@ -172,40 +172,43 @@ pub fn execute_prepared>( }, ) }; - // Settles the transaction as an out-of-gas halt when the runtime gas phase (the authorization - // charges or the first-frame recipient charge) runs out of gas: reverts the authorization - // checkpoint to drop the applied delegations, then consumes all execution gas and returns the - // reservoir. Unreachable pre-Amsterdam, where no runtime charge is attempted. - let settle_oog = |host: &mut Evm<'_, T>| { - let features = host.version().features; - host.state.rollback(runtime_checkpoint, features); - settle(host, runtime_oog_result(execution_gas_limit, reservoir)) + let frame = if auth_oog { + halted_initial_frame( + req.host, + caller, + tx.nonce, + tx.to.into(), + &tx.input, + tx.value, + &GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir), + ) + } else { + prepare_initial_frame( + req.host, + caller, + sender_nonce, + tx.to.into(), + &tx.input, + tx.value, + &mut tx_gas, + )? }; - - if auth_oog { - return settle_oog(req.host); + // A runtime gas phase that ran out of gas (the authorization charges or the first-frame + // recipient charge) settles the transaction as an out-of-gas halt: the authorization + // checkpoint reverts to drop the applied delegations, then the frame halts without executing, + // consuming all execution gas and returning the reservoir. Unreachable pre-Amsterdam, where no + // runtime charge is attempted. + if frame.runtime_oog { + let features = req.host.version().features; + req.host.state.rollback(runtime_checkpoint, features); } - let Some(InitialFrame { mut message, charged_state_gas }) = prepare_initial_frame( - req.host, - caller, - sender_nonce, - tx.to.into(), - &tx.input, - tx.value, - &mut tx_gas, - )? - else { - // A depth-0 recipient charge that ran out of gas is part of the runtime gas phase, so it - // drops the delegations too. - return settle_oog(req.host); - }; // Failed execution has already been rolled back to the message's own checkpoint (past the // applied delegations, which stay) inside `execute_message`. The settle merges the frame gas // into `tx_gas`, which carries the authorization state gas into the block state-gas // accounting. - let mut result = req.host.execute_message(&mut message)?; - settle_initial_frame_gas(&mut tx_gas, &mut result, charged_state_gas); + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; settle(req.host, result) } diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 102858f98..17adca16e 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -540,13 +540,17 @@ pub fn initial_gas_and_reservoir( /// The first frame of a transaction, prepared by [`prepare_initial_frame`]. #[derive(Clone, Debug)] pub struct InitialFrame { - /// Top-level call or create message, carrying the resolved bytecode to run. + /// Top-level call or create message, carrying the resolved bytecode to run unless + /// [`runtime_oog`](Self::runtime_oog). pub message: Message, /// EIP-8037 state gas charged on the transaction-level tracker for the account leaf this /// frame creates (the call recipient's new-account charge or the create target's /// account-creation charge), zero when nothing was charged. Refunded by /// [`settle_initial_frame_gas`] when the frame fails and the leaf is not created. pub charged_state_gas: u64, + /// Whether the runtime gas phase ran out of gas, so the frame halts without executing + /// ([`execute_initial_frame`]). Its message then carries no resolved bytecode. + pub runtime_oog: bool, } /// Completes the EIP-2780 runtime gas phase on the transaction-level `tx_gas` and builds the @@ -564,9 +568,9 @@ pub struct InitialFrame { /// the CREATE opcode instead. A delegated recipient is still resolved (for free) so the frame /// runs the delegate's code. /// -/// The frame's `gas_limit`/`reservoir` snapshot `tx_gas` after the charges. Returns `None` when -/// a charge runs out of gas: the transaction stays valid but is included as an out-of-gas halt -/// without entering execution ([`runtime_oog_result`]). +/// The frame's `gas_limit`/`reservoir` snapshot `tx_gas` after the charges. When a charge runs out +/// of gas the transaction stays valid but is included as an out-of-gas halt: the returned frame +/// is [`halted_initial_frame`], which [`execute_initial_frame`] halts without entering execution. pub fn prepare_initial_frame<'a, T: EvmTypes>( host: &mut Evm<'a, T>, caller: Address, @@ -575,7 +579,12 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( input: &Bytes, value: U256, tx_gas: &mut GasTracker, -) -> HandlerResult>> { +) -> HandlerResult> { + // A failed charge leaves `tx_gas` out of range, so the halted frame reports the budget the + // phase started with. + let budget = *tx_gas; + let halted = + |host: &Evm<'a, T>| halted_initial_frame(host, caller, nonce, to, input, value, &budget); let mut charged_state_gas = 0; let message = match to { TxKind::Call(to) => { @@ -589,7 +598,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( if host.feature(EvmFeatures::EIP2780) && !value.is_zero() && recipient_is_empty { let new_account_state_gas = host.version().gas_params.new_account_state_gas(); if tx_gas.spend_state(new_account_state_gas).is_err() { - return Ok(None); + return Ok(halted(host)); } charged_state_gas = new_account_state_gas; } @@ -606,17 +615,17 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( // list. let cold_additional = host.version().gas_params.cold_account_additional_cost(); if tx_gas.spend(u64::from(WARM_STORAGE_READ_COST)).is_err() { - return Ok(None); + return Ok(halted(host)); } let skip_cold_load = tx_gas.remaining() < cold_additional; let load = match Host::load_account(host, &delegated_address, true, skip_cold_load) { Ok(load) => load, - Err(HostError::Halt(_)) => return Ok(None), + Err(HostError::Halt(_)) => return Ok(halted(host)), Err(HostError::Execution(error)) => return Err(error.into()), }; if load.is_cold && tx_gas.spend(cold_additional).is_err() { - return Ok(None); + return Ok(halted(host)); } code = load.code; } else { @@ -652,7 +661,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( if !target_alive { let create_state_gas = host.version().gas_params.create_state_gas(); if tx_gas.spend_state(create_state_gas).is_err() { - return Ok(None); + return Ok(halted(host)); } charged_state_gas = create_state_gas; } @@ -677,7 +686,44 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } }; debug_assert_eq!(message.depth, host.version().initial_call_depth); - Ok(Some(InitialFrame { message, charged_state_gas })) + Ok(InitialFrame { message, charged_state_gas, runtime_oog: false }) +} + +/// Builds the first frame of a transaction whose runtime gas phase ran out of gas. +/// +/// The message names the call or create the transaction would have run, with the `budget` the +/// runtime gas phase started with and no resolved bytecode, and it reads no account state: the +/// recipient of an unaffordable authorization charge stays out of the EIP-7928 block access list. +/// [`execute_initial_frame`] halts it with out-of-gas, reporting the frame to the inspector. +pub fn halted_initial_frame( + host: &Evm<'_, T>, + caller: Address, + nonce: u64, + to: TxKind, + input: &Bytes, + value: U256, + budget: &GasTracker, +) -> InitialFrame { + let (kind, destination) = match to { + TxKind::Call(to) => (MessageKind::Call, to), + TxKind::Create => (MessageKind::Create, caller.create(nonce)), + }; + let message = MessageExt { + kind, + depth: host.version().initial_call_depth, + gas_limit: budget.remaining(), + reservoir: budget.reservoir(), + destination, + call_target: destination, + caller, + input: input.clone(), + value, + code_address: destination, + caller_is_static: false, + salt: B256::ZERO, + ..MessageExt::default() + }; + InitialFrame { message, charged_state_gas: 0, runtime_oog: true } } /// Settles the first frame's result into the transaction-level `tx_gas` and writes the settled @@ -708,16 +754,25 @@ pub const fn settle_initial_frame_gas( } /// Executes the prepared first frame and settles its gas into the transaction-level tracker. +/// +/// A frame whose runtime gas phase ran out of gas ([`InitialFrame::runtime_oog`]) is halted +/// without executing ([`Evm::execute_halted_message`]) and settled as [`runtime_oog_result`]. pub fn execute_initial_frame( host: &mut Evm<'_, T>, - frame: Option>, + frame: InitialFrame, tx_gas: &mut GasTracker, execution_gas_limit: u64, reservoir: u64, ) -> Result, crate::ExecutionError> { - let Some(InitialFrame { mut message, charged_state_gas }) = frame else { - return Ok(runtime_oog_result(execution_gas_limit, reservoir)); - }; + let InitialFrame { mut message, charged_state_gas, runtime_oog } = frame; + if runtime_oog { + // The hooks see the frame burn the budget it was given; the transaction itself burns all + // its execution gas and returns the reservoir, dropping the phase's partial charges. + let halted = runtime_oog_result(message.gas_limit, message.reservoir); + let mut result = host.execute_halted_message(&mut message, halted)?; + result.gas = runtime_oog_result::<()>(execution_gas_limit, reservoir).gas; + return Ok(result); + } // Failed execution has already been rolled back to the message's own checkpoint inside // `execute_message`; the settle merges the frame gas into the transaction-level gas. @@ -727,7 +782,8 @@ pub fn execute_initial_frame( } /// Builds the result for a transaction whose EIP-2780 runtime gas phase ran out of gas -/// ([`prepare_initial_frame`] returned `None`, or the EIP-7702 authorization charges bailed). +/// ([`prepare_initial_frame`] returned a [halted frame](halted_initial_frame), or the EIP-7702 +/// authorization charges bailed). /// /// The transaction is valid but cannot afford the state-dependent runtime charges: it is /// included as an out-of-gas halt that consumes all execution gas and returns the reservoir, @@ -1370,7 +1426,7 @@ mod tests { ); let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(100_000, 0); - let InitialFrame { mut message, charged_state_gas } = prepare_initial_frame( + let InitialFrame { mut message, charged_state_gas, .. } = prepare_initial_frame( &mut evm, caller, 0, @@ -1379,7 +1435,6 @@ mod tests { U256::ZERO, &mut tx_gas, ) - .unwrap() .unwrap(); assert_eq!(message.destination, target); assert_eq!(message.code_address, delegated); diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 4cf5ca7b3..86bbaef40 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -154,6 +154,10 @@ pub trait Inspector: NonStaticAny { /// /// The interpreter is the currently running frame whose instruction produced the message; for /// the top-level message it is a frame initialized with the message itself. + /// + /// A first frame that runs out of gas in the transaction's runtime gas phase halts without + /// executing: this hook still fires for it, with a message that carries no bytecode, a + /// returned result is ignored, and [`Inspector::call_end`] receives the out-of-gas result. #[inline] fn call( &mut self, @@ -182,6 +186,10 @@ pub trait Inspector: NonStaticAny { /// /// The interpreter is the currently running frame whose instruction produced the message; for /// the top-level message it is a frame initialized with the message itself. + /// + /// A first frame that runs out of gas in the transaction's runtime gas phase halts without + /// executing: this hook still fires for it, with a message that carries no bytecode, a + /// returned result is ignored, and [`Inspector::create_end`] receives the out-of-gas result. #[inline] fn create( &mut self, @@ -2407,6 +2415,88 @@ mod tests { assert_eq!(inspector.restored, Some(true)); } + #[test] + fn runtime_oog_first_frame_fires_call_hooks() { + #[derive(Default)] + struct HaltedFrameInspector { + calls: Vec<(u16, Address, u64)>, + ends: Vec<(InstrStop, u64)>, + } + impl Inspector for HaltedFrameInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + assert!(message.code.is_empty()); + self.calls.push((message.depth, message.destination, message.gas_limit)); + // A returned result must not replace the halt. + Some(MessageResultExt::default()) + } + + fn call_end( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + _message: &Message, + result: &mut MessageResult, + ) { + self.ends.push((result.stop, result.gas.spent())); + } + } + + let key = k256::ecdsa::SigningKey::from_slice(&[0x11; 32]).unwrap(); + let authorization = alloy_eips::eip7702::Authorization { + chain_id: U256::from(1), + address: Address::ZERO, + nonce: 0, + }; + let (signature, recovery_id) = + key.sign_prehash_recoverable(authorization.signature_hash().as_slice()); + let authorization = alloy_eips::eip7702::SignedAuthorization::new_unchecked( + authorization, + recovery_id.to_byte(), + U256::from_be_slice(&signature.r().to_bytes()), + U256::from_be_slice(&signature.s().to_bytes()), + ); + let caller = Address::from([0xaa; 20]); + let target = Address::from([0xbb; 20]); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000_u64)), + ); + let mut evm = Evm::::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::AMSTERDAM), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + evm.set_inspector(HaltedFrameInspector::default()); + // The gas covers the intrinsic cost but not the new authority's state gas. + let tx = Recovered::new_unchecked( + TxEnvelope::from(alloy_consensus::TxEip7702 { + chain_id: 1, + gas_limit: 60_000, + to: target, + authorization_list: vec![authorization], + ..Default::default() + }), + caller, + ); + + let result = evm.transact(&tx).expect("transaction should execute").discard(); + assert!(!result.status); + assert_eq!(result.stop, InstrStop::OutOfGas); + let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); + let [(0, destination, gas_limit)] = inspector.calls[..] else { + panic!("expected one depth-0 call hook, got {:?}", inspector.calls) + }; + assert_eq!(destination, target); + // The halt burns the whole budget the frame was given. + assert_eq!(inspector.ends, [(InstrStop::OutOfGas, gas_limit)]); + } + #[test] fn cooling_loaded_slot_respects_access_list() { struct CoolSlotInspector { diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 449ca6209..926363217 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1381,14 +1381,21 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// /// This is invoked for every message when an inspector is installed; hook overrides skip /// execution entirely, including the call depth check. + /// + /// A `halted` result stands in for the execution: the hooks fire around it, and a hook + /// override does not replace it. #[inline(never)] fn execute_message_inspected<'frame>( &mut self, message: &'frame mut Message, + halted: Option>, ) -> Result, ExecutionError> { let guard = self.enter_execution(); let Some(inspector) = guard.evm.active_inspector() else { - return guard.evm.execute_message_impl(message); + return match halted { + Some(result) => Ok(result), + None => guard.evm.execute_message_impl(message), + }; }; // SAFETY: The inspector is stored in `self`; the execution guard prevents inspector // replacement while the hooks are running. @@ -1435,6 +1442,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { let mut result = if let Some(error) = frame.take_error() { Err(error) + } else if let Some(halted) = halted { + Ok(halted) } else { inspected.map(Ok).unwrap_or_else(|| guard.evm.execute_message_impl(message)) }; @@ -1804,6 +1813,23 @@ impl<'a, T: EvmTypes> Evm<'a, T> { inspector.initialize_interp(interp); } } + + /// Reports a message that halts without executing, such as a transaction's first frame whose + /// runtime gas phase ran out of gas. + /// + /// With an inspector installed, the call or create hook fires and the matching end hook + /// receives `result`, as for an executed frame; the hook cannot replace `result`. `result` + /// must already have its gas settled for its stop reason. + pub fn execute_halted_message( + &mut self, + message: &mut Message, + result: MessageResult, + ) -> Result, ExecutionError> { + if self.inspector.is_some() { + return self.execute_message_inspected(message, Some(result)); + } + Ok(result) + } } impl<'a, T: EvmTypes> Host for Evm<'a, T> { @@ -1941,7 +1967,7 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { ) -> Result, ExecutionError> { let checkpoint = self.state.checkpoint(); let result = if self.inspector.is_some() { - self.execute_message_inspected(message) + self.execute_message_inspected(message, None) } else { self.execute_message_impl(message) }; diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 48d191bcd..4ecf47030 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -660,7 +660,6 @@ fn precompile_dispatch_uses_original_callee( U256::ZERO, &mut gas, ) - .unwrap() .unwrap(); let result = Host::execute_message(&mut evm, &mut frame.message).unwrap(); diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index ed9367fb7..de6ed571b 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -243,7 +243,6 @@ impl DebugInspector { let res = match self { Self::FourByte(inspector) => FourByteFrame::from(&*inspector).into(), Self::CallTracer(inspector, config) => { - inspector.recover_runtime_oog_root(tx, &res.result); inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); inspector @@ -271,7 +270,6 @@ impl DebugInspector { inspector.try_into_mux_frame(res, tx, block_env, tx_context, db)?.into() } Self::FlatCallTracer(inspector) => { - inspector.recover_runtime_oog_root(tx, &res.result); inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); inspector diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index e20b14375..279d75b62 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -168,7 +168,6 @@ impl Erc7562Inspector { if let Some(error) = &self.error { return Err(Error::Database(error.clone())); } - self.calls.recover_runtime_oog_root(tx, result); self.calls.set_transaction_gas_limit(tx.gas_limit()); self.calls.set_transaction_caller(tx.signer()); let nodes = self.calls.traces.nodes(); diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index e047167e4..3bc5df4e3 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -11,12 +11,11 @@ use crate::{ }, }; use alloc::{boxed::Box, vec::Vec}; -use alloy_consensus::{Transaction, transaction::Recovered}; -use alloy_primitives::{Address, B256, Bytes, Log, TxKind, U256}; +use alloy_primitives::{Address, B256, Bytes, Log, U256}; use alloy_rpc_types_trace::parity::StorageDelta; use core::{mem, ops::Range}; use evm2::{ - Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, TxResultExt, + Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -683,37 +682,6 @@ impl TracingInspector { } } } - - /// Restores a root omitted when evm2 halts during runtime gas charging, before its hooks run. - pub(crate) fn recover_runtime_oog_root( - &mut self, - tx: &Recovered, - result: &TxResultExt, - ) { - // The arena starts with a default root. After execution, call_end/create_end have set - // its status if the root was recorded, even when it executed no opcodes. - if self.traces.arena[0].trace.status.is_some() || result.stop != InstrStop::OutOfGas { - return; - } - - let (kind, address) = match tx.kind() { - TxKind::Call(address) => (CallKind::Call, address), - TxKind::Create => (CallKind::Create, tx.signer().create(tx.nonce())), - }; - self.traces.arena[0].trace = CallTrace { - caller: tx.signer(), - address, - kind, - value: tx.value(), - data: if self.config.record_inputs { tx.input().clone() } else { Bytes::new() }, - gas_limit: tx.gas_limit(), - gas_used: result.tx_gas_used(), - status: Some(InstrStop::OutOfGas), - ..Default::default() - }; - // Do not guess the EVM features from the halt or its gas accounting. The normal hooks did - // not run, and the transaction result does not identify the active fork. - } } impl Inspector for TracingInspector { diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index af8ee8302..be1d16868 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -177,9 +177,6 @@ impl MuxInspector { } /// Try converting this [MuxInspector] into a [MuxFrame]. - /// - /// Call traces recover the transaction root when runtime gas charging halts before evm2 - /// invokes the call/create hooks. Recovered roots report `out of gas`. pub fn try_into_mux_frame>( &mut self, result: &TxResultWithState, @@ -189,7 +186,6 @@ impl MuxInspector { db: &mut dyn DynDatabase, ) -> Result { if let Some(inspector) = &mut self.tracing { - inspector.recover_runtime_oog_root(tx, &result.result); inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); } diff --git a/crates/inspectors/tests/it/geth/runtime_oog.rs b/crates/inspectors/tests/it/geth/runtime_oog.rs index 9da7f99fc..45cda051f 100644 --- a/crates/inspectors/tests/it/geth/runtime_oog.rs +++ b/crates/inspectors/tests/it/geth/runtime_oog.rs @@ -1,8 +1,9 @@ -//! Call tracer regressions for halts before evm2 initializes the root frame. +//! Regressions for the root frame of a transaction that runs out of gas before execution. use super::*; use alloy_rpc_types_trace::geth::{CallFrame, CallLogFrame}; use evm2::interpreter::InstrStop; +use evm2_inspectors::tracing::TracingInspector; #[test] fn runtime_oog_call_frames_and_reuse() { @@ -29,7 +30,7 @@ fn runtime_oog_call_frames_and_reuse() { }); let mut evm = context.build_mainnet().with_inspector(&mut inspector); - // Alternate recorded and missing roots to catch stale state across fuse(). The + // Alternate halted and executed roots to catch stale state across fuse(). The // successful empty-code call also uses its full gas limit without executing opcodes. for (gas_limit, input, kind, halted) in [ (200_000, Bytes::new(), TxKind::Call(recipient), true), @@ -86,10 +87,9 @@ fn runtime_oog_call_frames_and_reuse() { index: Some(index as u64), }) .collect(), - // Only recorded roots currently capture the EVM features. - execution_gas_used: (!halted).then(|| U256::from(result.execution_gas_spent())), - state_gas_used: (!halted).then(|| U256::from(result.state_gas_spent())), - gas_refund: (!halted).then(|| U256::from(result.final_refunded())), + execution_gas_used: Some(U256::from(result.execution_gas_spent())), + state_gas_used: Some(U256::from(result.state_gas_spent())), + gas_refund: Some(U256::from(result.final_refunded())), ..Default::default() }; assert_eq!(frame, serde_json::to_value(expected).unwrap()); @@ -100,19 +100,68 @@ fn runtime_oog_call_frames_and_reuse() { } #[test] -fn missing_root_other_halt_is_not_out_of_gas() { - let mut inspector = - DebugInspector::new(GethDebugTracingOptions::call_tracer(CallConfig::default())).unwrap(); - let mut evm = Context::mainnet().build_mainnet().with_inspector(&mut inspector); - let mut res = evm.inspect_tx(TxEnv { gas_limit: 100_000, ..Default::default() }).unwrap(); - res.tx_result.result.status = false; - res.tx_result.result.stop = InstrStop::InvalidFEOpcode; +fn runtime_oog_frame_reaches_four_byte_tracer() { + let caller = address!("1000000000000000000000000000000000000001"); + let recipient = address!("1000000000000000000000000000000000000002"); + let options = serde_json::json!({"tracer": "4byteTracer"}); + let mut inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); + let context = Context::mainnet() + .with_db(CacheDB::::default()) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, + ); + }); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + // The gas covers the intrinsic cost but not the new account's state gas. + let res = evm + .inspect_tx(TxEnv { + caller, + kind: TxKind::Call(recipient), + value: U256::ONE, + data: hex!("0102030405").into(), + gas_limit: 200_000, + ..Default::default() + }) + .unwrap(); + assert!(!res.tx_result.result.status); let (ctx, inspector) = evm.ctx_inspector(); - inspector.fuse().unwrap(); let tx = ctx.tx().envelope(); let block = *ctx.block(); let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); - let GethTrace::CallTracer(frame) = trace else { panic!("expected call trace") }; - // An unknown missing frame must not be mislabeled as a runtime OOG. - assert_eq!(frame.error, None); + assert_eq!(serde_json::to_value(trace).unwrap(), serde_json::json!({"0x01020304-1": 1})); +} + +#[test] +fn runtime_oog_root_reports_the_execution_budget() { + let caller = address!("1000000000000000000000000000000000000001"); + let recipient = address!("1000000000000000000000000000000000000002"); + let context = Context::mainnet() + .with_db(CacheDB::::default()) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, + ); + }); + let mut evm = context.build_mainnet_with_inspector(TracingInspector::new(Default::default())); + evm.inspect_tx(TxEnv { + caller, + kind: TxKind::Call(recipient), + value: U256::ONE, + gas_limit: 200_000, + ..Default::default() + }) + .unwrap(); + let nodes = evm.inspector.traces().nodes(); + assert_eq!(nodes.len(), 1); + let root = &nodes[0].trace; + assert_eq!(root.status, Some(InstrStop::OutOfGas)); + assert_eq!(root.address, recipient); + // The halt burns the whole budget, which is what the first frame was given. + assert!(root.gas_limit < 200_000); + assert_eq!(root.gas_used, root.gas_limit); } From ddb35029c69351c4f547478f2611d77c0860ee89 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:25:05 +0100 Subject: [PATCH 118/159] fix(inspectors): exclude precompile calls from flat-call traces like geth (#857) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geth's flatCallTracer removes a frame when its type is CALL or STATICCALL and its target is an active precompile, whatever the value (`call_flat.go`, `OnExit`). It keeps DELEGATECALL and CALLCODE frames. evm2 instead dropped any zero-value call to a precompile, so it kept CALL frames that carry value and dropped zero-value DELEGATECALL frames. evm2 also decided the exclusion while recording. An excluded node was pushed into the arena with its parent set but left out of the parent's `children`. Inside a mux, merging the flat tracer's config (`exclude_precompile_calls |=`) turned that on for the shared recording, so callTracer's log positions counted the hidden call: geth reports position 1 for a LOG0 after a call to 0x01, evm2 reported 0. The inspector now always attaches every call to its parent and only records whether the call targets a precompile (`record_precompile_calls`, which merges harmlessly). `ParityTraceBuilder::new(nodes, exclusion)` prunes the excluded calls and everything they called from the children and ordering of their parents; it no longer takes the unused spec id and config parameters, and `into_parity_builder` passes `PrecompileExclusion::ZeroValue`. `trace_*` keeps its zero-value rule. The debug `flatCallTracer` and the mux `flatCallTracer` use geth's rule through `TracingInspector::into_flat_call_builder`, which also reads `includePrecompiles`, so the option now takes effect at the call sites instead of through the shared config. `TracingInspectorConfig::from_flat_call_config` is gone, and `DebugInspector::FlatCallTracer` carries its `FlatCallConfig`. Compared against go-ethereum master `a0cba163` with the parity harness of the `dani/geth-parity-harness` branch (Cancun, Prague, Osaka and Amsterdam, two new cases): | Case | geth master | origin/main | this PR | | --- | --- | --- | --- | | flatCallTracer: CALL with value 1, CALL with value 0, STATICCALL to 0x04 | no frame | 🔴 CALL with value 1 kept, subtraces 3 instead of 2 | 🟢 no frame | | flatCallTracer: CALLCODE to 0x04, DELEGATECALL to 0x02 | both kept | 🔴 frames shifted by the extra CALL | 🟢 both kept, identical output | | mux of callTracer `withLog` and flatCallTracer: call 0x01, then LOG0 | log position 1 | 🔴 log position 0 | 🟢 log position 1 | The flat output of both cases matches geth exactly on the branch. The only remaining difference in them is Amsterdam's callTracer `executionGasUsed`, `gasRefund` and `stateGasUsed`, which is outside this change. Across the 315 case and fork pairs of the harness with the flat, call, call-log, mux, mux-flat and prestate tracers, the differences are the same on origin/main and on this branch, except for the two cases above. The harness is not merged yet, so its cases are not part of this PR; I will add them once it lands. Rust tests pin the `trace_*` rule (a zero-value call to a precompile is left out, a call with value is kept, later trace addresses stay contiguous), the flat rule with and without `includePrecompiles`, and callTracer's log position inside a mux. --- crates/inspectors/src/tracing/arena.rs | 37 +---- .../inspectors/src/tracing/builder/parity.rs | 156 +++++++++++++++--- crates/inspectors/src/tracing/config.rs | 50 +----- crates/inspectors/src/tracing/debug.rs | 25 +-- crates/inspectors/src/tracing/mod.rs | 75 +++------ crates/inspectors/src/tracing/mux.rs | 7 +- crates/inspectors/src/tracing/types.rs | 4 +- crates/inspectors/tests/it/geth.rs | 127 +++++++++++++- crates/inspectors/tests/it/parity.rs | 40 +++++ 9 files changed, 352 insertions(+), 169 deletions(-) diff --git a/crates/inspectors/src/tracing/arena.rs b/crates/inspectors/src/tracing/arena.rs index acfb75166..5768a838a 100644 --- a/crates/inspectors/src/tracing/arena.rs +++ b/crates/inspectors/src/tracing/arena.rs @@ -56,16 +56,10 @@ impl CallTraceArena { /// Pushes a new trace into the arena as a child of `parent`, returning the trace ID /// /// This appends a new trace to the arena, and also inserts a new entry in the node's parent - /// node children set if `attach_to_parent` is `true`. E.g. if calls to precompiles should - /// not be included in the call graph this should be called with [PushTraceKind::PushOnly]. + /// node children set. /// /// If there is no parent, the trace replaces the root node, regardless of its visible depth. - pub(crate) fn push_trace( - &mut self, - parent: Option, - kind: PushTraceKind, - new_trace: CallTrace, - ) -> usize { + pub(crate) fn push_trace(&mut self, parent: Option, new_trace: CallTrace) -> usize { // The entry node, just update it. let Some(parent) = parent else { self.arena[0].trace = new_trace; @@ -87,30 +81,11 @@ impl CallTraceArena { }); // Also track the child in the parent node. - if kind.is_attach_to_parent() { - let parent = &mut self.arena[parent]; - let trace_location = parent.children.len(); - parent.ordering.push(TraceMemberOrder::Call(trace_location)); - parent.children.push(idx); - } + let parent = &mut self.arena[parent]; + let trace_location = parent.children.len(); + parent.ordering.push(TraceMemberOrder::Call(trace_location)); + parent.children.push(idx); idx } } - -/// How to push a trace into the arena -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum PushTraceKind { - /// This will _only_ push the trace into the arena. - PushOnly, - /// This will push the trace into the arena, and also insert a new entry in the node's parent - /// node children set. - PushAndAttachToParent, -} - -impl PushTraceKind { - #[inline] - const fn is_attach_to_parent(&self) -> bool { - matches!(self, Self::PushAndAttachToParent) - } -} diff --git a/crates/inspectors/src/tracing/builder/parity.rs b/crates/inspectors/src/tracing/builder/parity.rs index 41bd6f2c8..9182b23c3 100644 --- a/crates/inspectors/src/tracing/builder/parity.rs +++ b/crates/inspectors/src/tracing/builder/parity.rs @@ -1,6 +1,5 @@ use crate::tracing::{ - TracingInspectorConfig, - types::{CallTraceNode, CallTraceStep, StepDelta, TraceMemberOrder}, + types::{CallKind, CallTraceNode, CallTraceStep, StepDelta, TraceMemberOrder}, utils::load_account_code, }; use alloc::{string::ToString, vec, vec::Vec}; @@ -12,28 +11,53 @@ use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::parity::*; use core::iter::Peekable; use evm2::{ - AccountInfo, EvmTypesHost, SpecId, TxResultExt, TxResultWithState, + AccountInfo, EvmTypesHost, TxResultExt, TxResultWithState, evm::{DbResult, DynDatabase, PendingState}, interpreter::InstrStop, }; /// A type for creating parity style traces /// -/// Note: Parity style traces always ignore calls to precompiles. +/// Note: Parity style traces ignore calls to precompiles without value, see +/// [`PrecompileExclusion::ZeroValue`]. #[derive(Clone, Debug)] pub struct ParityTraceBuilder { /// Recorded trace nodes nodes: Vec, + /// Marks the calls to precompiles left out of the traces, and everything they called; their + /// parents no longer list them as children. + excluded: Vec, } impl ParityTraceBuilder { - /// Returns a new instance of the builder - pub const fn new( - nodes: Vec, - _spec_id: Option, - _config: TracingInspectorConfig, - ) -> Self { - Self { nodes } + /// Returns a new instance of the builder that leaves out the calls to precompiles selected by + /// `exclusion`. + pub fn new(mut nodes: Vec, exclusion: PrecompileExclusion) -> Self { + // Parents always precede their children in the arena. + let mut excluded = Vec::::with_capacity(nodes.len()); + for node in &nodes { + excluded.push(exclusion.excludes(node) || node.parent.is_some_and(|p| excluded[p])); + } + if !excluded.contains(&true) { + return Self { nodes, excluded }; + } + + for node in &mut nodes { + // Calls appear in `ordering` in the order of `children`. + let mut kept = 0; + node.ordering.retain_mut(|member| { + let TraceMemberOrder::Call(position) = member else { return true }; + let keep = !excluded[node.children[*position]]; + if keep { + *position = kept; + kept += 1; + } + keep + }); + node.children.retain(|&child| !excluded[child]); + } + + Self { nodes, excluded } } /// Returns a list of all addresses that appeared as callers. @@ -69,8 +93,8 @@ impl ParityTraceBuilder { /// Each entry in the returned vector represents the trace address of the corresponding node in /// the nodes set. /// - /// CAUTION: This also includes precompiles, which have an empty trace address, as they are not - /// tracked as children of their parent. + /// CAUTION: This also includes excluded precompile calls, which have an empty trace address, as + /// they are not tracked as children of their parent. fn trace_addresses(&self) -> Vec> { let mut all_addresses = vec![Vec::new(); self.nodes.len()]; // Parents always precede their children in the arena, so a single forward pass can derive @@ -89,9 +113,12 @@ impl ParityTraceBuilder { /// Returns an iterator over all nodes to trace /// - /// This excludes nodes that represent calls to precompiles. + /// This excludes the calls to precompiles selected by the builder's [`PrecompileExclusion`]. fn iter_traceable_nodes(&self) -> impl Iterator { - self.nodes.iter().filter(|node| !node.is_precompile()) + self.nodes + .iter() + .zip(&self.excluded) + .filter_map(|(node, &excluded)| (!excluded).then_some(node)) } /// Returns an iterator over all recorded traces for `trace_transaction` @@ -261,8 +288,11 @@ impl ParityTraceBuilder { .nodes .into_iter() .zip(trace_addresses) - .filter(|(node, _)| !node.is_precompile()) - .map(|(node, trace_address)| (node.parity_transaction_trace(trace_address), node)) + .zip(self.excluded) + .filter(|(_, excluded)| !excluded) + .map(|((node, trace_address), _)| { + (node.parity_transaction_trace(trace_address), node) + }) .peekable(), } } @@ -286,11 +316,12 @@ impl ParityTraceBuilder { let mut child_idx_stack = Vec::with_capacity(self.nodes.len()); let mut sub_stack = Vec::with_capacity(self.nodes.len()); - // Excluded precompile calls are recorded without being attached to their parent, in call - // order. + // Excluded precompile calls are not attached to their parent; collect them in call order. let mut precompiles = HashMap::>::default(); - for node in self.nodes.iter().filter(|node| node.is_precompile()) { - if let Some(parent) = node.parent { + for (node, _) in self.nodes.iter().zip(&self.excluded).filter(|(_, excluded)| **excluded) { + if let Some(parent) = node.parent + && !self.excluded[parent] + { precompiles.entry(parent).or_default().push(node); } } @@ -397,6 +428,35 @@ impl ParityTraceBuilder { } } +/// Which calls to precompiles a [`ParityTraceBuilder`] leaves out of its traces. +/// +/// A call is only left out if the inspector recorded it as a call to a precompile, see +/// [`TracingInspectorConfig::record_precompile_calls`](crate::tracing::TracingInspectorConfig::record_precompile_calls), and it is not the root call. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PrecompileExclusion { + /// Keeps every call to a precompile. + None, + /// Leaves out calls of any kind that transfer no value, as Parity does. + ZeroValue, + /// Leaves out `CALL` and `STATICCALL` regardless of value and keeps `DELEGATECALL` and + /// `CALLCODE`, as geth's flatCallTracer does. + CallAndStaticCall, +} + +impl PrecompileExclusion { + fn excludes(self, node: &CallTraceNode) -> bool { + node.parent.is_some() + && node.is_precompile() + && match self { + Self::None => false, + Self::ZeroValue => node.trace.value.is_zero(), + Self::CallAndStaticCall => { + matches!(node.kind(), CallKind::Call | CallKind::StaticCall) + } + } + } +} + /// Returns whether a step began executing, excluding operations rejected before execution and /// the synthetic `STOP` past the end of the recorded bytecode. fn began_executing(step: &CallTraceStep, bytecode: Option<&Bytes>) -> bool { @@ -570,7 +630,8 @@ pub fn populate_state_diff( #[cfg(test)] mod tests { use super::*; - use crate::tracing::types::{CallKind, CallTrace}; + use crate::tracing::types::CallTrace; + use alloy_primitives::U256; #[test] fn test_parity_suicide_simple_call() { @@ -583,7 +644,7 @@ mod tests { ..Default::default() }]; - let traces = ParityTraceBuilder::new(nodes, None, TracingInspectorConfig::default_parity()) + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::ZeroValue) .into_transaction_traces(); assert_eq!(traces.len(), 2); @@ -647,7 +708,7 @@ mod tests { }, ]; - let traces = ParityTraceBuilder::new(nodes, None, TracingInspectorConfig::default_parity()) + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::ZeroValue) .into_transaction_traces(); assert_eq!(traces.len(), 4); @@ -670,4 +731,51 @@ mod tests { assert_eq!(traces[3].trace_address, vec![0, 1]); assert!(traces[3].action.is_selfdestruct()); } + + #[test] + fn excluded_precompile_calls_take_their_nested_calls_along() { + let precompile = CallTrace { + depth: 1, + kind: CallKind::Call, + value: U256::from(1), + maybe_precompile: Some(true), + ..Default::default() + }; + let nodes = vec![ + CallTraceNode { + children: vec![1, 3], + idx: 0, + trace: CallTrace { depth: 0, ..Default::default() }, + ordering: vec![TraceMemberOrder::Call(0), TraceMemberOrder::Call(1)], + ..Default::default() + }, + CallTraceNode { + parent: Some(0), + children: vec![2], + idx: 1, + trace: precompile, + ordering: vec![TraceMemberOrder::Call(0)], + ..Default::default() + }, + CallTraceNode { + parent: Some(1), + idx: 2, + trace: CallTrace { depth: 2, ..Default::default() }, + ..Default::default() + }, + CallTraceNode { + parent: Some(0), + idx: 3, + trace: CallTrace { depth: 1, ..Default::default() }, + ..Default::default() + }, + ]; + + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::CallAndStaticCall) + .into_transaction_traces(); + + let addresses: Vec<_> = traces.iter().map(|trace| trace.trace_address.clone()).collect(); + assert_eq!(addresses, [vec![], vec![0]]); + assert_eq!(traces[0].subtraces, 1); + } } diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index a958afe41..5cf6382a1 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -1,7 +1,7 @@ use crate::tracing::builder::geth::MIN_STRUCT_LOG_JSON_LEN; use alloy_primitives::map::HashSet; use alloy_rpc_types_trace::{ - geth::{CallConfig, FlatCallConfig, GethDefaultTracingOptions, PreStateConfig}, + geth::{CallConfig, GethDefaultTracingOptions, PreStateConfig}, parity::TraceType, }; use core::num::NonZeroU64; @@ -36,8 +36,9 @@ pub struct TracingInspectorConfig { /// Optional filter for opcodes to record. If provided, only steps with opcode in this set will /// be recorded. pub record_opcodes_filter: Option, - /// Whether to ignore precompile calls. - pub exclude_precompile_calls: bool, + /// Whether to record which calls target a precompile, so that trace builders can leave them + /// out. + pub record_precompile_calls: bool, /// Whether to record logs pub record_logs: bool, /// Whether to record immediate bytes for opcodes. @@ -63,7 +64,7 @@ impl TracingInspectorConfig { record_state_diff: true, record_returndata_snapshots: true, record_opcodes_filter: None, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: true, record_immediate_bytes: true, record_step_deltas: true, @@ -81,7 +82,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::None, record_state_diff: false, record_returndata_snapshots: false, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -102,7 +103,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::None, record_state_diff: false, record_returndata_snapshots: false, - exclude_precompile_calls: true, + record_precompile_calls: true, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -145,7 +146,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::Full, record_state_diff: true, record_returndata_snapshots: false, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -207,19 +208,6 @@ impl TracingInspectorConfig { .set_record_logs(config.with_log.unwrap_or_default()) } - /// Returns a config for geth's - /// [FlatCallTracer](alloy_rpc_types_trace::geth::call::FlatCallFrame). - /// - /// This returns [Self::default_parity] and sets - /// [TracingInspectorConfig::exclude_precompile_calls] if configured in the given - /// [FlatCallConfig] - #[inline] - pub fn from_flat_call_config(config: &FlatCallConfig) -> Self { - Self::default_parity() - // call tracer is similar parity tracer with optional support for logs - .set_exclude_precompile_calls(!config.include_precompiles.unwrap_or_default()) - } - /// Returns a config for geth's [PrestateTracer](alloy_rpc_types_trace::geth::PreStateFrame). /// /// Note: This currently returns [Self::none] because the prestate tracer result currently @@ -252,7 +240,7 @@ impl TracingInspectorConfig { self.record_stack_snapshots = other.record_stack_snapshots; self.record_state_diff |= other.record_state_diff; self.record_returndata_snapshots |= other.record_returndata_snapshots; - self.exclude_precompile_calls |= other.exclude_precompile_calls; + self.record_precompile_calls |= other.record_precompile_calls; self.record_logs |= other.record_logs; self.record_opcodes_filter = self.record_opcodes_filter.or(other.record_opcodes_filter); self.record_immediate_bytes |= other.record_immediate_bytes; @@ -266,14 +254,6 @@ impl TracingInspectorConfig { self } - /// Configure whether calls to precompiles should be ignored. - /// - /// If set to `true`, calls to precompiles without value transfers will be ignored. - pub const fn set_exclude_precompile_calls(mut self, exclude_precompile_calls: bool) -> Self { - self.exclude_precompile_calls = exclude_precompile_calls; - self - } - /// Disable recording of individual opcode level steps pub const fn disable_steps(self) -> Self { self.set_steps(false) @@ -481,7 +461,6 @@ mod tests { TracingInspectorConfig::all(), TracingInspectorConfig::default_parity(), TracingInspectorConfig::from_geth_call_config(&CallConfig::default()), - TracingInspectorConfig::from_flat_call_config(&FlatCallConfig::default()), ] { assert!(config.record_inputs); } @@ -565,17 +544,6 @@ mod tests { assert!(!config.record_state_diff); } - #[test] - fn test_flat_call_config() { - let config = FlatCallConfig { include_precompiles: Some(true), ..Default::default() }; - let config = TracingInspectorConfig::from_flat_call_config(&config); - assert!(!config.exclude_precompile_calls); - - let config = FlatCallConfig { include_precompiles: Some(false), ..Default::default() }; - let config = TracingInspectorConfig::from_flat_call_config(&config); - assert!(config.exclude_precompile_calls); - } - #[test] fn test_all_records_all_stack_snapshots() { // `all()` enables everything, so the stack snapshot type should be the most diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index de6ed571b..7fee5d01a 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -7,7 +7,7 @@ use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256}; use alloy_rpc_types_trace::geth::{ - CallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, + CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, NoopFrame, PreStateConfig, StateGasTrace, erc7562::Erc7562Config, mux::MuxConfig, }; @@ -46,7 +46,7 @@ pub enum DebugInspector { /// Mux tracer Mux(MuxInspector, MuxConfig), /// FlatCallTracer - FlatCallTracer(TracingInspector), + FlatCallTracer(TracingInspector, FlatCallConfig), /// Erc7562Tracer Erc7562Tracer(Erc7562Inspector), /// Keccak256PreimageTracer @@ -70,7 +70,9 @@ impl DebugInspector { Self::Noop(inspector) => Self::Noop(inspector.clone()), Self::StateGasTracer(inspector) => Self::StateGasTracer(inspector.clone()), Self::Mux(inspector, config) => Self::Mux(inspector.try_clone()?, config.clone()), - Self::FlatCallTracer(inspector) => Self::FlatCallTracer(inspector.clone()), + Self::FlatCallTracer(inspector, config) => { + Self::FlatCallTracer(inspector.clone(), *config) + } Self::Erc7562Tracer(inspector) => Self::Erc7562Tracer(inspector.clone()), Self::Keccak256Preimage(inspector) => Self::Keccak256Preimage(inspector.clone()), Self::Default(inspector, config) => Self::Default(inspector.clone(), *config), @@ -127,9 +129,10 @@ impl DebugInspector { GethDebugBuiltInTracerType::FlatCallTracer => { let flat_call_config = tracer_config.into_flat_call_config()?; - Self::FlatCallTracer(TracingInspector::new( - TracingInspectorConfig::from_flat_call_config(&flat_call_config), - )) + Self::FlatCallTracer( + TracingInspector::new(TracingInspectorConfig::default_parity()), + flat_call_config, + ) } GethDebugBuiltInTracerType::Erc7562Tracer => { let config = if tracer_config.is_null() { @@ -184,7 +187,7 @@ impl DebugInspector { } Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) - | Self::FlatCallTracer(inspector) + | Self::FlatCallTracer(inspector, _) | Self::Default(inspector, _) => inspector.fuse(), Self::Erc7562Tracer(inspector) => inspector.fuse(), Self::Keccak256Preimage(inspector) => inspector.clear(), @@ -214,7 +217,7 @@ impl DebugInspector { match self { Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) - | Self::FlatCallTracer(inspector) + | Self::FlatCallTracer(inspector, _) | Self::Default(inspector, _) => inspector.set_next_log_index(index), Self::Erc7562Tracer(inspector) => inspector.set_next_log_index(index), Self::Mux(inspector, _) => inspector.set_next_log_index(index), @@ -269,12 +272,12 @@ impl DebugInspector { Self::Mux(inspector, _) => { inspector.try_into_mux_frame(res, tx, block_env, tx_context, db)?.into() } - Self::FlatCallTracer(inspector) => { + Self::FlatCallTracer(inspector, config) => { inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); inspector .clone() - .into_parity_builder() + .into_flat_call_builder(config) .with_transaction_gas_used(res.result.tx_gas_used()) .into_localized_transaction_traces(tx_info) .into() @@ -329,7 +332,7 @@ macro_rules! delegate { Self::FourByte($insp) => Inspector::::$method($insp, $($arg),*), Self::CallTracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::PreStateTracer($insp, _) => Inspector::::$method($insp, $($arg),*), - Self::FlatCallTracer($insp) => Inspector::::$method($insp, $($arg),*), + Self::FlatCallTracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::Erc7562Tracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Default($insp, _) => Inspector::::$method($insp, $($arg),*), Self::Keccak256Preimage($insp) => Inspector::::$method($insp, $($arg),*), diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 3bc5df4e3..78a44beb4 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -2,20 +2,17 @@ use crate::{ opcode::immediate_size, - tracing::{ - arena::PushTraceKind, - types::{ - CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, - TraceMemberOrder, - }, + tracing::types::{ + CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, + TraceMemberOrder, }, }; use alloc::{boxed::Box, vec::Vec}; use alloy_primitives::{Address, B256, Bytes, Log, U256}; -use alloy_rpc_types_trace::parity::StorageDelta; +use alloy_rpc_types_trace::{geth::FlatCallConfig, parity::StorageDelta}; use core::{mem, ops::Range}; use evm2::{ - Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, + EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -28,7 +25,7 @@ pub use arena::CallTraceArena; mod builder; pub use builder::{ geth::{self, GethTraceBuilder}, - parity::{self, ParityTraceBuilder}, + parity::{self, ParityTraceBuilder, PrecompileExclusion}, }; mod config; @@ -281,10 +278,25 @@ impl TracingInspector { } } - /// Consumes the Inspector and returns a [ParityTraceBuilder]. + /// Consumes the Inspector and returns a [ParityTraceBuilder] that leaves out calls to + /// precompiles without value, see [PrecompileExclusion::ZeroValue]. #[inline] pub fn into_parity_builder(self) -> ParityTraceBuilder { - ParityTraceBuilder::new(self.traces.arena, self.spec_id, self.config) + ParityTraceBuilder::new(self.traces.arena, PrecompileExclusion::ZeroValue) + } + + /// Consumes the Inspector and returns a [ParityTraceBuilder] for geth's flatCallTracer. + /// + /// Unlike [Self::into_parity_builder], this keeps or leaves out calls to precompiles as the + /// tracer's `includePrecompiles` option says. + #[inline] + pub fn into_flat_call_builder(self, config: &FlatCallConfig) -> ParityTraceBuilder { + let exclusion = if config.include_precompiles.unwrap_or_default() { + PrecompileExclusion::None + } else { + PrecompileExclusion::CallAndStaticCall + }; + ParityTraceBuilder::new(self.traces.arena, exclusion) } /// Consumes the Inspector and returns a [GethTraceBuilder]. @@ -303,29 +315,6 @@ impl TracingInspector { GethTraceBuilder::new_borrowed(&self.traces.arena).with_features(self.features) } - /// Returns true if we're no longer in the context of the root call. - const fn is_deep(&self) -> bool { - // the root call will always be the first entry in the trace stack - !self.trace_stack.is_empty() - } - - /// Returns true if this a call to a precompile contract. - /// - /// Returns true if the `to` address is a precompile contract and the value is zero. - #[inline] - fn is_precompile_call( - &self, - host: &Evm<'_, T>, - to: &Address, - value: &U256, - ) -> bool { - if host.precompiles().contains(to) { - // only if this is _not_ the root call - return self.is_deep() && value.is_zero(); - } - false - } - /// Returns the last trace [CallTrace] index from the stack. /// /// This will be the currently active call trace. @@ -372,15 +361,6 @@ impl TracingInspector { gas_limit: u64, maybe_precompile: Option, ) { - // This will only be true if the inspector is configured to exclude precompiles and the call - // is to a precompile - let push_kind = if maybe_precompile.unwrap_or(false) { - // We don't want to track precompiles - PushTraceKind::PushOnly - } else { - PushTraceKind::PushAndAttachToParent - }; - // find an empty steps vec or create a new one let steps = self.reusable_step_vecs.pop().unwrap_or_default(); @@ -389,7 +369,6 @@ impl TracingInspector { let trace_idx = self.traces.push_trace( parent, - push_kind, CallTrace { depth, address, @@ -759,11 +738,9 @@ impl Inspector for TracingInspector { _ => (message.caller, message.destination), }; - // if calls to precompiles should be excluded, check whether this is a call to a precompile - let maybe_precompile = self - .config - .exclude_precompile_calls - .then(|| self.is_precompile_call(interp.host(), &to, &message.value)); + // Builders decide whether to leave out calls to precompiles, so record which calls are. + let maybe_precompile = + self.config.record_precompile_calls.then(|| interp.host().precompiles().contains(&to)); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index be1d16868..4aa5b41bb 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -89,8 +89,7 @@ impl MuxInspector { let flatcall_config = tracer_config.unwrap_or_default().into_flat_call_config()?; - inspector_config - .merge(TracingInspectorConfig::from_flat_call_config(&flatcall_config)); + inspector_config.merge(TracingInspectorConfig::default_parity()); configs.push((builtin, TraceConfig::FlatCall(flatcall_config))); } GethDebugBuiltInTracerType::MuxTracer @@ -215,11 +214,11 @@ impl MuxInspector { continue; } } - TraceConfig::FlatCall(_flatcall_config) => { + TraceConfig::FlatCall(flatcall_config) => { if let Some(inspector) = &self.tracing { inspector .clone() - .into_parity_builder() + .into_flat_call_builder(flatcall_config) .with_transaction_gas_used(result.result.tx_gas_used()) .into_localized_transaction_traces(tx_info) .into() diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index cb670fd09..4c3e5fe78 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -67,7 +67,9 @@ pub struct CallTrace { pub address: Address, /// Whether this is a call to a precompile. /// - /// Note: This is optional because not all tracers make use of this. + /// Only recorded if + /// [`TracingInspectorConfig::record_precompile_calls`](crate::tracing::TracingInspectorConfig::record_precompile_calls) + /// is set, otherwise `None`. pub maybe_precompile: Option, /// The address of the selfdestructed contract. pub selfdestruct_address: Option
, diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 7f72fe73d..f1f52132f 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -12,10 +12,14 @@ use alloy_eips::eip7702::Authorization; use alloy_primitives::{ Address, B256, Bytes, Log, TxKind, U256, address, hex, keccak256, map::HashMap, }; -use alloy_rpc_types_trace::geth::{ - CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, - GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, - PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, +use alloy_rpc_types_eth::TransactionInfo; +use alloy_rpc_types_trace::{ + geth::{ + CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, + GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, + PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, + }, + parity::{Action, CallType}, }; use evm2::{ AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, EvmTypesHost, ExecutionConfig, @@ -2176,7 +2180,7 @@ fn test_geth_opcode_limit_end_to_end() { /// geth emits a child's steps right after the call step that entered it. A zero-value call to an /// excluded precompile attaches no child, so it must not take the next call's child. #[test] -fn test_geth_struct_logs_follow_calls_with_excluded_precompiles() { +fn test_geth_struct_logs_follow_calls_to_precompiles() { let account = address!("1000000000000000000000000000000000000001"); let child = address!("0000000000000000000000000000000000001234"); let call = |target: [u8; 2]| { @@ -2202,8 +2206,7 @@ fn test_geth_struct_logs_follow_calls_with_excluded_precompiles() { ); } }); - let config = TracingInspectorConfig::default_geth().set_exclude_precompile_calls(true); - let mut inspector = TracingInspector::new(config); + let mut inspector = TracingInspector::new(TracingInspectorConfig::default_geth()); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm .inspect_tx(TxEnv { @@ -2879,6 +2882,115 @@ fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { } } +fn precompile_call_contract(code: &[u8]) -> (Address, CacheDB) { + let contract = Address::with_last_byte(0x42); + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo::default() + .with_balance(U256::from(2)) + .with_code(Bytecode::new_raw(code.to_vec().into())), + ); + (contract, db) +} + +#[test] +fn flat_call_tracer_excludes_call_and_staticcall_to_precompiles() { + // CALL 0x04 with value 1, STATICCALL 0x04, CALLCODE 0x04 with value 1, DELEGATECALL 0x02 and + // CALLCODE 0x06 without value. + let (contract, db) = precompile_call_contract(&hex!( + "5f5f5f5f600160045af1505f5f5f5f60045afa505f5f5f5f600160045af2505f5f5f5f60025af450" + "5f5f5f5f5f60065af25000" + )); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + for (include_precompiles, expected) in [ + ( + None, + vec![CallType::Call, CallType::CallCode, CallType::DelegateCall, CallType::CallCode], + ), + ( + Some(true), + vec![ + CallType::Call, + CallType::Call, + CallType::StaticCall, + CallType::CallCode, + CallType::DelegateCall, + CallType::CallCode, + ], + ), + ] { + let config = FlatCallConfig { include_precompiles, ..Default::default() }; + let traces = evm + .inspector + .clone() + .into_flat_call_builder(&config) + .into_localized_transaction_traces(TransactionInfo::default()); + let call_types: Vec<_> = traces + .iter() + .map(|trace| match &trace.trace.action { + Action::Call(call) => call.call_type, + action => panic!("unexpected action {action:?}"), + }) + .collect(); + assert_eq!(call_types, expected); + assert_eq!(traces[0].trace.subtraces, expected.len() - 1); + } +} + +#[test] +fn mux_flat_call_tracer_does_not_shift_call_tracer_log_positions() { + // CALL 0x01, then LOG0. + let (contract, db) = precompile_call_contract(&hex!("5f5f5f5f5f60015af1505f5fa000")); + let call_config = CallConfig { only_top_call: Some(false), with_log: Some(true) }; + let config = MuxConfig(HashMap::from_iter([ + ( + GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer), + Some(GethDebugTracerConfig(serde_json::to_value(call_config).unwrap())), + ), + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FlatCallTracer), None), + ])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + let GethTrace::CallTracer(call_frame) = + &frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)] + else { + panic!("expected a call tracer frame"); + }; + assert_eq!(call_frame.calls.len(), 1); + assert_eq!(call_frame.logs[0].position, Some(1)); + + let GethTrace::FlatCallTracer(traces) = + &frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FlatCallTracer)] + else { + panic!("expected a flat call tracer frame"); + }; + assert_eq!(traces.len(), 1); +} + #[test] fn mux_runs_nested_mux_and_erc7562_tracers() { let (contract, db) = contract_with_code(&hex!("5f5f00")); @@ -3395,7 +3507,6 @@ fn tracing_inspector_steps_only_what_its_config_records() { ..Default::default() }), TracingInspectorConfig::from_geth_prestate_config(&PreStateConfig::default()), - TracingInspectorConfig::from_flat_call_config(&FlatCallConfig::default()), TracingInspectorConfig::default_parity(), TracingInspectorConfig::parity_statediff(), TracingInspectorConfig { record_opcodes_filter: Some(filter), ..Default::default() }, diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index d33086c4e..a5295c433 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -1453,6 +1453,46 @@ fn nested_calls_keep_each_forwarded_cost() { assert_eq!(inner.ex.as_ref().unwrap().used, child.ops[6].ex.as_ref().unwrap().used - 100 - 15); } +#[test] +fn parity_traces_leave_out_zero_value_precompile_calls() { + let contract = Address::with_last_byte(0x42); + let mut db = CacheDB::::default(); + db.insert_account_info( + &contract, + AccountInfo::default().with_balance(U256::from(1)).with_code(Bytecode::new_raw( + // CALL 0x04 with no value, CALL 0x04 with value 1, CALL 0x99 with no value. + hex!("5f5f5f5f5f60045af1505f5f5f5f600160045af1505f5f5f5f5f60995af15000").into(), + )), + ); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let traces = evm.inspector.clone().into_parity_builder().into_transaction_traces(); + let calls: Vec<_> = traces + .iter() + .map(|trace| { + let Action::Call(call) = &trace.action else { panic!("expected a call") }; + (call.to, call.value, trace.trace_address.clone()) + }) + .collect(); + assert_eq!( + calls, + [ + (contract, U256::ZERO, vec![]), + (Address::with_last_byte(4), U256::from(1), vec![0]), + (Address::with_last_byte(0x99), U256::ZERO, vec![1]), + ] + ); + assert_eq!(traces[0].subtraces, 2); +} + #[path = "parity/vm_bytecode.rs"] mod vm_bytecode; From 08e4dd89aeb6c08e8233be2fcf9b024655f44663 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:27:39 +0100 Subject: [PATCH 119/159] refactor(evm): build transfer log topics with Address::into_word (#1005) Both ERC20-style ETH transfer log builders now pad addresses into topics with `Address::into_word()` and construct the log with `Log::new_unchecked`. `eip7708_transfer_log` no longer pads through `B256::left_padding_from`, and the transfer inspector no longer allocates a `Vec` per topic through `abi_encode`. The emitted bytes are unchanged: a left-padded 32-byte address topic and a 32-byte big-endian value. --- crates/evm2/src/evm/mod.rs | 25 +++++++------------------ crates/inspectors/src/transfer.rs | 9 ++------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 926363217..6276611a4 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -133,7 +133,7 @@ use crate::{ use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec, vec::Vec}; use alloy_consensus::transaction::Recovered; use alloy_eips::eip2718::Typed2718; -use alloy_primitives::{Address, B256, Bytes, Log, LogData}; +use alloy_primitives::{Address, B256, Bytes, Log}; use core::ptr::NonNull; #[cfg(feature = "async")] use core::{any::TypeId, future::Future}; @@ -2136,15 +2136,11 @@ fn eip7708_transfer_log(from: &Address, to: &Address, value: &Word) -> Option())), - }) + Some(Log::new_unchecked( + SYSTEM_ADDRESS, + vec![EIP7708_TRANSFER_TOPIC, from.into_word(), to.into_word()], + Bytes::copy_from_slice(&value.to_be_bytes::<32>()), + )) } #[cfg(test)] @@ -4301,14 +4297,7 @@ mod tests { assert_eq!(logs.len(), 1); let log = &logs[0]; assert_eq!(log.address, SYSTEM_ADDRESS); - assert_eq!( - log.topics(), - &[ - EIP7708_TRANSFER_TOPIC, - B256::left_padding_from(caller.as_slice()), - B256::left_padding_from(target.as_slice()), - ] - ); + assert_eq!(log.topics(), &[EIP7708_TRANSFER_TOPIC, caller.into_word(), target.into_word()]); assert_eq!(log.data.data, Bytes::copy_from_slice(&U256::from(7).to_be_bytes::<32>())); } diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 400b33a5b..07b3639ed 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -2,7 +2,6 @@ use alloc::{vec, vec::Vec}; use alloy_primitives::{Address, B256, Log, U256, address, b256}; -use alloy_sol_types::SolValue; use evm2::{ Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, @@ -152,14 +151,10 @@ impl TransferInspector { && !host.feature(EvmFeatures::EIP7708) && let Some(frame) = self.frames.last_mut() { - let from = B256::from_slice(&from.abi_encode()); - let to = B256::from_slice(&to.abi_encode()); - let data = value.abi_encode(); - frame.logs.push(Log::new_unchecked( TRANSFER_LOG_EMITTER, - vec![TRANSFER_EVENT_TOPIC, from, to], - data.into(), + vec![TRANSFER_EVENT_TOPIC, from.into_word(), to.into_word()], + value.to_be_bytes::<32>().into(), )); } } From 20460e3dc1ff079168736952af358024fd09dbec Mon Sep 17 00:00:00 2001 From: Mablr <59505383+mablr@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:29:40 +0100 Subject: [PATCH 120/159] fix(inspectors): align ERC7562 operand overflow flag (#995) ERC7562 currently marks InvalidOperandOOG as outOfGas, whereas geth reports false for the corresponding memory-size overflow. A nested call executing MSTORE with an offset of U256::MAX demonstrates the mismatch. Exclude InvalidOperandOOG from this flag without changing execution or halt classification. Retain MemoryLimitOOG: executing the reference cases against geth confirms that excluding it would introduce another mismatch. Regression coverage includes operand overflow and two positive memory-OOG controls. Co-authored-by: Amp --- crates/inspectors/src/tracing/erc7562.rs | 8 +++- crates/inspectors/tests/it/geth.rs | 48 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index 279d75b62..35ab25279 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -200,8 +200,12 @@ impl Erc7562Inspector { .collect(), used_opcodes: counters.used_opcodes, contract_size: counters.contract_size, - // geth reports it for frames that exit with an out of gas error, not for the root. - out_of_gas: idx != 0 && node.trace.status.is_some_and(InstrStop::is_out_of_gas), + // geth's OnExit checks ErrOutOfGas / ErrCodeStoreOutOfGas, not the bare + // ErrGasUintOverflow of an oversized memory operand. The root is excluded. + out_of_gas: idx != 0 + && node.trace.status.is_some_and(|status| { + status.is_out_of_gas() && status != InstrStop::InvalidOperandOOG + }), keccak: if idx == 0 { self.keccak.iter().cloned().collect() } else { Vec::new() }, calls: Vec::new(), }; diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index f1f52132f..c6ff2c3a8 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -3384,6 +3384,54 @@ fn test_geth_prestate_selfdestruct_reports_slots_before_the_wipe() { ); } +#[test] +fn test_geth_erc7562_operand_overflow_is_not_out_of_gas() { + let root = address!("1000000000000000000000000000000000000001"); + let child = address!("1000000000000000000000000000000000000002"); + // geth rejects a >64-bit memory operand before metering dynamic gas, but an + // affordable-size expansion with insufficient gas is still an out-of-gas error. + for (offset, out_of_gas) in + [(U256::MAX, false), (U256::from(0x10_0000), true), (U256::from(u32::MAX), true)] + { + let mut child_code = vec![op::PUSH1, 0, op::PUSH32]; + child_code.extend_from_slice(&offset.to_be_bytes::<32>()); + child_code.push(op::MSTORE); + let mut root_code = hex!("60006000600060006000").to_vec(); + root_code.push(op::PUSH20); + root_code.extend_from_slice(child.as_slice()); + root_code.extend_from_slice(&hex!("61fffff15000")); + let mut db = CacheDB::::default(); + for (address, code) in [(root, root_code), (child, child_code)] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(root), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + assert!(!frame.out_of_gas); + assert_eq!(frame.calls.len(), 1); + let call = &frame.calls[0]; + assert!(call.error.is_some(), "offset={offset:#x}"); + assert_eq!(call.used_opcodes, HashMap::from_iter([(op::MSTORE, 1)])); + assert_eq!(call.out_of_gas, out_of_gas, "offset={offset:#x}"); + } +} + /// Forwards every hook to `inner` and records the opcodes it steps. Steps every opcode if `all`. struct SteppedSpy { inner: I, From ae946035a045f2df234ab5efab1b5b5968db8835 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:29:55 +0100 Subject: [PATCH 121/159] refactor(evm2): remove duplicated constants and helpers across the crate (#1006) Removes duplicated code in `crates/evm2` by calling helpers that already exist, without changing the public API or any constant. Hand-written copies now call the existing helper: `MessageResultExt::is_success`, `PrewarmSet::is_cold`, `Evm::read_account_info`, `Evm::call_tx`, `Evm::database_as`, the `GasTracker` and `GasParams` helpers (`create2_cost` through `keccak256_word_cost`), `BlockValidationError::other`, `Bytecode::from_valid_eip7702`, and the bundle state and account accessors. The two `require_non_staticcall` copies in the interpreter are one, and the CALL path uses the private `should_charge_new_account_gas`. Test helpers copied between modules now live in `test_utils`, and the const-fold bounds test names the fields of its cases instead of using a tuple alias. Values and behavior are unchanged. Suggestions that would change what a test checks, mix up similar-looking operations, or need a restructure were left out. --- crates/evm2/examples/custom_evm/config.rs | 2 +- crates/evm2/examples/custom_evm/main.rs | 21 +-- crates/evm2/examples/custom_evm/tx.rs | 2 +- .../src/block/bundle_state/bundle_state.rs | 24 +-- crates/evm2/src/block/error.rs | 4 +- crates/evm2/src/block/ethereum/execution.rs | 2 +- crates/evm2/src/block/ethereum/mod.rs | 6 +- crates/evm2/src/block/ethereum/tests.rs | 5 +- crates/evm2/src/block/state.rs | 14 +- crates/evm2/src/bytecode/mod.rs | 8 +- crates/evm2/src/ethereum/mod.rs | 39 ++--- crates/evm2/src/evm/async.rs | 21 +-- crates/evm2/src/evm/bal/account.rs | 6 +- crates/evm2/src/evm/bal/bal_context.rs | 4 +- crates/evm2/src/evm/bal/list.rs | 33 ++-- crates/evm2/src/evm/db/cache.rs | 2 +- crates/evm2/src/evm/inspector.rs | 36 +--- crates/evm2/src/evm/mod.rs | 65 +++---- crates/evm2/src/evm/prewarm_set.rs | 18 +- crates/evm2/src/evm/state/account.rs | 20 +-- crates/evm2/src/evm/state/block.rs | 11 +- crates/evm2/src/evm/state/mod.rs | 12 +- crates/evm2/src/evm/system.rs | 9 +- .../src/interpreter/dispatch/table/packed.rs | 2 +- crates/evm2/src/interpreter/gas.rs | 17 +- crates/evm2/src/interpreter/host.rs | 3 +- .../src/interpreter/instructions/block.rs | 8 +- .../evm2/src/interpreter/instructions/env.rs | 8 +- .../evm2/src/interpreter/instructions/host.rs | 9 +- .../interpreter/instructions/macro_tests.rs | 19 +-- .../evm2/src/interpreter/instructions/mod.rs | 13 ++ .../src/interpreter/instructions/stack.rs | 2 +- .../src/interpreter/instructions/system.rs | 61 +++---- crates/evm2/src/interpreter/opcode.rs | 4 +- crates/evm2/src/interpreter/runtime.rs | 11 +- crates/evm2/src/precompiles/modexp.rs | 11 +- crates/evm2/src/precompiles/table.rs | 4 +- crates/evm2/src/test_utils.rs | 74 +++++++- crates/evm2/src/tests.rs | 13 +- crates/evm2/src/version/gas_params.rs | 30 ++-- .../codegen/src/bytecode/passes/const_fold.rs | 161 +++++++++--------- 41 files changed, 365 insertions(+), 449 deletions(-) diff --git a/crates/evm2/examples/custom_evm/config.rs b/crates/evm2/examples/custom_evm/config.rs index 86e326914..e2e51e81f 100644 --- a/crates/evm2/examples/custom_evm/config.rs +++ b/crates/evm2/examples/custom_evm/config.rs @@ -102,7 +102,7 @@ impl EvmConfig } pub const fn custom_version(base_spec_id: SpecId) -> Version { - let mut version = *Version::base(base_spec_id); + let mut version = Version::new(base_spec_id); opcode::install_gas_params(&mut version.gas_params); version } diff --git a/crates/evm2/examples/custom_evm/main.rs b/crates/evm2/examples/custom_evm/main.rs index 1ab7d1e51..eb025a9f2 100644 --- a/crates/evm2/examples/custom_evm/main.rs +++ b/crates/evm2/examples/custom_evm/main.rs @@ -73,20 +73,13 @@ fn state_change_consumer() -> HandlerResult<()> { database.insert_account_info(&target, AccountInfo::default().with_nonce(1)); let mut evm = custom_evm_with_database(database); let mut consumer = ExampleStateConsumer::default(); - let tx = Recovered::new_unchecked( - CustomEnvelope::ExecuteCode(ExecuteCodeTx { - target, - gas_limit: 100_000, - code: Bytes::from_static(&[ - opcode::CUSTOM_OPCODE, - op::PUSH1, - 0x01, - op::SSTORE, - op::STOP, - ]), - }), - Address::ZERO, - ); + let tx = custom_opcode_tx(Bytes::from_static(&[ + opcode::CUSTOM_OPCODE, + op::PUSH1, + 0x01, + op::SSTORE, + op::STOP, + ])); let Ok(result) = evm.transact(&tx)?.commit_with(&mut consumer); let storage_change = consumer.storage_changes.first().expect("storage change observed"); diff --git a/crates/evm2/examples/custom_evm/tx.rs b/crates/evm2/examples/custom_evm/tx.rs index ed102995e..e27a348bf 100644 --- a/crates/evm2/examples/custom_evm/tx.rs +++ b/crates/evm2/examples/custom_evm/tx.rs @@ -71,7 +71,7 @@ pub fn execute_code( let mut result = req.host.execute_message(&mut message)?; result.ext = CustomMessageResultExt { handled_custom_message: true }; Ok(evm2::TxResult:: { - status: result.stop.is_success(), + status: result.is_success(), total_gas_spent: req.tx.gas_limit - result.gas.remaining(), stop: result.stop, output: result.output, diff --git a/crates/evm2/src/block/bundle_state/bundle_state.rs b/crates/evm2/src/block/bundle_state/bundle_state.rs index 0607575cc..8a459d83d 100644 --- a/crates/evm2/src/block/bundle_state/bundle_state.rs +++ b/crates/evm2/src/block/bundle_state/bundle_state.rs @@ -871,11 +871,7 @@ mod tests { let transition = TransitionAccount { info: Some(acc1), - status: AccountStatus::InMemoryChange, - previous_info: None, - previous_status: AccountStatus::LoadedNotExisting, - storage: StorageWithOriginalValues::default(), - storage_was_destroyed: false, + ..TransitionAccount::new_empty_eip161(StorageWithOriginalValues::default()) }; // Apply first transition @@ -1012,14 +1008,10 @@ mod tests { }; let recreate = TransitionAccount { info: Some(info), - status: AccountStatus::InMemoryChange, - previous_info: None, - previous_status: AccountStatus::LoadedNotExisting, - storage: HashMap::from_iter([( + ..TransitionAccount::new_empty_eip161(HashMap::from_iter([( slot2(), StorageSlot::new_changed(U256::ZERO, U256::from(5)), - )]), - storage_was_destroyed: false, + )])) }; let block_bundle = |transition| { let mut bundle = BundleState::default(); @@ -1239,12 +1231,12 @@ mod tests { state.revert_latest(); // State for account one was deleted - assert_eq!(state.state.get(&account1()), None); + assert_eq!(state.account(&account1()), None); state.revert_latest(); // State is set to assert_eq!( - state.state.get(&account1()), + state.account(&account1()), Some(&BundleAccount::new( None, Some(AccountInfo::default()), @@ -1338,13 +1330,13 @@ mod tests { test.prepend_state(previous_state); - assert_eq!(test.state.len(), 2); + assert_eq!(test.len(), 2); // The receiver keeps its own reverts. assert_eq!(test.reverts, present_reverts); // Account1 is not overwritten. - assert_eq!(test.state.get(&address1).unwrap().info, Some(account1_changed)); + assert_eq!(test.account(&address1).unwrap().info, Some(account1_changed)); // Account2 got inserted - assert_eq!(test.state.get(&address2).unwrap().info, Some(account2)); + assert_eq!(test.account(&address2).unwrap().info, Some(account2)); } #[test] diff --git a/crates/evm2/src/block/error.rs b/crates/evm2/src/block/error.rs index 8c27477fa..efd5dfedb 100644 --- a/crates/evm2/src/block/error.rs +++ b/crates/evm2/src/block/error.rs @@ -258,7 +258,7 @@ pub fn map_handler_error(error: HandlerError) -> BlockExecutionError { HandlerError::Fatal(_) | HandlerError::WrongTransactionType { .. } => { BlockExecutionError::other(error) } - error => BlockValidationError::Other(AnyError::new(error)).into(), + error => BlockValidationError::other(error).into(), } } @@ -267,6 +267,6 @@ pub fn map_database_error(error: DatabaseError) -> BlockExecutionError { if error.is_fatal() { BlockExecutionError::other(error) } else { - BlockValidationError::Other(AnyError::new(error)).into() + BlockValidationError::other(error).into() } } diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs index 814993dd8..22fc395aa 100644 --- a/crates/evm2/src/block/ethereum/execution.rs +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -486,7 +486,7 @@ fn execute_system_call( message: alloc::format!("{reason:?}"), } } - _ => BlockValidationError::Other(AnyError::new(SystemCallFailed { address, reason })), + _ => BlockValidationError::other(SystemCallFailed { address, reason }), } .into()); } diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index e6fabff6b..8c5aa7a4d 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -383,10 +383,10 @@ where } #[inline] - const fn set_transaction_block_access_index(&mut self) { + fn set_transaction_block_access_index(&mut self) { if self.block_access_list_builder_enabled() { let index = self.receipts.len() as u64 + 1 + self.bal_index_offset; - self.evm.state_mut().set_bal_index(BlockAccessIndex::new(index)); + self.set_block_access_index(BlockAccessIndex::new(index)); } } @@ -587,7 +587,7 @@ where ) -> Result<(BlockExecutionOutput, Option), BlockExecutionError> { let EthPostExecutionOutput { requests, gas_used, blob_gas_used } = self.apply_post_execution_changes(0)?; - let block_access_list = self.evm.state_mut().take_bal_builder(); + let block_access_list = self.take_block_access_list(); let output = BlockExecutionOutput::new( BlockExecutionResult { receipts: self.receipts, requests, gas_used, blob_gas_used }, self.block_state.into_bundle(), diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs index a9221dc3b..59830c1f9 100644 --- a/crates/evm2/src/block/ethereum/tests.rs +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -9,6 +9,7 @@ use crate::{ AccountInfo, BEACON_ROOTS_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS, HISTORY_STORAGE_ADDRESS, InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, }, + utils::b256_to_word, }; use alloc::vec; use alloy_consensus::{Signed, TxLegacy, constants::ETH_TO_WEI, transaction::Recovered}; @@ -128,10 +129,10 @@ fn executes_prague_block() { let state = &output.state; assert_eq!(balance(state, RECIPIENT), Some(U256::from(1))); // EIP-2935 stores the parent hash at slot `(number - 1) % 8191`. - let parent_hash = U256::from_be_bytes(PARENT_HASH.0); + let parent_hash = b256_to_word(PARENT_HASH); assert_eq!(storage(state, HISTORY_STORAGE_ADDRESS, U256::ZERO), Some(parent_hash)); // EIP-4788 stores the timestamp at `timestamp % 8191` and the root 8191 slots later. - let root = U256::from_be_bytes(PARENT_BEACON_BLOCK_ROOT.0); + let root = b256_to_word(PARENT_BEACON_BLOCK_ROOT); assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12)), Some(U256::from(12))); assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12 + 8191)), Some(root)); } diff --git a/crates/evm2/src/block/state.rs b/crates/evm2/src/block/state.rs index 820e19c8d..786255e41 100644 --- a/crates/evm2/src/block/state.rs +++ b/crates/evm2/src/block/state.rs @@ -307,7 +307,7 @@ impl crate::evm::StateChangeSource for BundleSource<'_> { } for (address, account) in &self.0.state { let current = account.info.as_ref(); - if account.status.was_destroyed() { + if account.was_destroyed() { sink.storage_wipe(*address)?; } sink.account(crate::evm::AccountChangeRef { @@ -343,19 +343,15 @@ mod tests { use super::*; use crate::{ block::bundle_state::OriginalValuesKnown, - evm::{ - AccountChangeRef, AccountChanges, StateChangeSink, StorageChange, StorageOverlay, - StorageSlot as NativeSlot, Tracked, - }, + evm::{AccountChangeRef, AccountChanges, StateChangeSink, StorageChange, StorageOverlay}, + test_utils::storage_slot, }; /// Builds a storage overlay from `(key, original, current)` slots. fn overlay(wiped: bool, slots: &[(U256, U256, U256)]) -> StorageOverlay { let mut storage = StorageOverlay { wiped, ..Default::default() }; for &(key, original, current) in slots { - let slot = - NativeSlot { value: Tracked::from_parts(original, current), ..Default::default() }; - storage.slots.insert(key, slot); + storage.slots.insert(key, storage_slot(original, current)); } storage } @@ -395,7 +391,7 @@ mod tests { assert_eq!(account.info.as_ref().unwrap().extension.as_ref(), b"updated extension"); assert!(bundle.revert_latest()); assert_eq!( - bundle.state.get(&address).unwrap().info.as_ref().unwrap().extension.as_ref(), + bundle.account(&address).unwrap().info.as_ref().unwrap().extension.as_ref(), b"original extension" ); } diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index 9e8079244..d87d23ce0 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -196,13 +196,7 @@ impl Bytecode { #[inline] pub fn new_eip7702(address: Address) -> Self { let raw: Bytes = [EIP7702_MAGIC_BYTES, &[EIP7702_VERSION], &address[..]].concat().into(); - Self(Some(Arc::new(BytecodeInner { - kind: BytecodeKind::Eip7702, - original_len: raw.len(), - bytecode: raw, - jump_table: OnceLock::new(), - hash: OnceLock::new(), - }))) + Self::from_valid_eip7702(raw) } /// Creates a new raw [`Bytecode`]. diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 17adca16e..76bbc4a18 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -26,7 +26,7 @@ use crate::{ MessageResultExt, gas::WARM_STORAGE_READ_COST, }, registry::{HandlerError, HandlerResult, TxRegistry, handler}, - utils::num_words, + utils::{b256_to_word, num_words}, version::GasId, }; use alloy_consensus::{ @@ -481,10 +481,8 @@ pub fn warm_base_accounts<'a, T: EvmTypes>(host: &mut Evm<'a, T>, to: TxKind) { /// Warms every account and storage key in an access list. pub fn warm_access_list<'a, T: EvmTypes>(host: &mut Evm<'a, T>, access_list: &AccessList) { for item in access_list.iter() { - host.state.prewarm_storage( - &item.address, - item.storage_keys.iter().map(|key| U256::from_be_bytes(key.0)), - ); + host.state + .prewarm_storage(&item.address, item.storage_keys.iter().map(|key| b256_to_word(*key))); } } @@ -744,7 +742,7 @@ pub const fn settle_initial_frame_gas( ) { tx_gas.spend_all(); tx_gas.merge_child_gas(result.gas, result.stop); - if charged_state_gas != 0 && !result.stop.is_success() { + if charged_state_gas != 0 && !result.is_success() { tx_gas.refill_reservoir(charged_state_gas); if result.stop.is_halt() { tx_gas.spend_all(); @@ -855,7 +853,7 @@ pub fn finalize_gas<'a, T: EvmTypes>( gas_spent_breakdown(tx_gas_limit, &result.gas, initial_state_gas); let state_gas_spent = state_gas_spent.saturating_sub(state_refund); Ok(TxResultExt { - status: result.stop.is_success(), + status: result.is_success(), total_gas_spent, state_gas_spent, refunded, @@ -1047,7 +1045,7 @@ mod tests { ); let validation = evm.validate_tx(&tx); - let result = evm.transact(&tx).map(crate::evm::ExecutedTx::discard); + let result = evm.call_tx(&tx); assert_eq!(validation, result.as_ref().map(|_| ()).map_err(Clone::clone)); if gas_limit < 31_050 { assert_eq!( @@ -1297,7 +1295,7 @@ mod tests { ); assert!(evm.transact(&tx).unwrap().commit().status); - let reward = evm.state.account_info_untracked(&beneficiary).unwrap().unwrap().balance; + let reward = evm.read_account_info(&beneficiary).unwrap().unwrap().balance; assert_eq!(reward, U256::from(21_000 * beneficiary_gas_price)); } @@ -1338,7 +1336,7 @@ mod tests { ); assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); - let sender = evm.state.account_info_untracked(&caller).unwrap().unwrap(); + let sender = evm.read_account_info(&caller).unwrap().unwrap(); assert_eq!((sender.balance, sender.nonce), (U256::ZERO, 1)); } @@ -1367,10 +1365,7 @@ mod tests { ); assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); - assert_eq!( - evm.state.account_info_untracked(&caller).unwrap().unwrap().balance, - U256::from(100) - ); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().balance, U256::from(100)); } #[test] @@ -1393,7 +1388,7 @@ mod tests { assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); charge_upfront(&mut evm, caller, U256::from(100)).unwrap(); - assert_eq!(evm.state.account_info_untracked(&caller).unwrap().unwrap().balance, U256::ZERO); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().balance, U256::ZERO); } #[test] @@ -1646,12 +1641,11 @@ mod tests { ); let tx = Recovered::new_unchecked(tx, caller); let case = format!("{spec:?} {to:?} value={value} type={tx_type}"); - let caller_before = evm.state.account_info_untracked(&caller).unwrap(); - let recipient_before = - evm.state.account_info_untracked(&recipient).unwrap(); + let caller_before = evm.read_account_info(&caller).unwrap(); + let recipient_before = evm.read_account_info(&recipient).unwrap(); if balance_check && value == 2 { assert_eq!( - evm.transact(&tx).map(crate::evm::ExecutedTx::discard), + evm.call_tx(&tx), Err(HandlerError::InsufficientFunds { fee: U256::from(2), balance: U256::ONE @@ -1659,12 +1653,12 @@ mod tests { "{case}" ); assert_eq!( - evm.state.account_info_untracked(&caller).unwrap(), + evm.read_account_info(&caller).unwrap(), caller_before, "{case}" ); assert_eq!( - evm.state.account_info_untracked(&recipient).unwrap(), + evm.read_account_info(&recipient).unwrap(), recipient_before, "{case}" ); @@ -1676,8 +1670,7 @@ mod tests { } else { assert_eq!(result.status, input[0] == op::STOP, "{case}"); } - let nonce = - evm.state.account_info_untracked(&caller).unwrap().unwrap().nonce; + let nonce = evm.read_account_info(&caller).unwrap().unwrap().nonce; assert_eq!(nonce, 11, "{case}"); } } diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index 123f61f7a..9cb4d7027 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -536,7 +536,7 @@ mod tests { use super::*; use crate::{ BaseEvmTypes, Evm, ExecutionConfig, Inspector, PrecompileError, Precompiles, SpecId, - TxResult, TxResultExt, Version, + Version, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, @@ -544,7 +544,8 @@ mod tests { interpreter::{GasTracker, InstrStop, Interpreter, Message, MessageResult, Word, op}, precompile::PrecompileOutput, precompiles::PrecompileId, - registry::{HandlerError, HandlerResult, TxRegistry, TxRequest, handler}, + registry::{HandlerError, TxRegistry, handler}, + test_utils::{handle_test_tx, test_tx}, }; use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, B256, Bytes, TxKind, hex}; @@ -1122,22 +1123,6 @@ mod tests { const TEST_TX_TYPE: u8 = 0x00; - fn test_tx(value: u64) -> crate::ethereum::RecoveredTxEnvelope { - Recovered::new_unchecked( - crate::ethereum::TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), - Address::ZERO, - ) - } - - fn handle_test_tx(req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>) -> HandlerResult { - let _ = req.host.spec_id(); - Ok(TxResultExt { - status: true, - total_gas_spent: req.tx.nonce + 1, - ..TxResultExt::default() - }) - } - fn poll_ready(future: F) -> F::Output { let mut future = core::pin::pin!(future); let waker = Waker::noop(); diff --git a/crates/evm2/src/evm/bal/account.rs b/crates/evm2/src/evm/bal/account.rs index 44dac58c7..33d7c7cf0 100644 --- a/crates/evm2/src/evm/bal/account.rs +++ b/crates/evm2/src/evm/bal/account.rs @@ -226,11 +226,11 @@ impl AccountInfoBal { self.nonce.update(index, &original.nonce, present.nonce); self.balance.update(index, &original.balance, present.balance); if original.code_hash != present.code_hash { - self.code.update_with_key( + self.code_update( index, &(original.code_hash, original.code.clone().unwrap_or_default()), - (present.code_hash, present.code.clone().unwrap_or_default()), - |i| &i.0, + present.code_hash, + present.code.clone().unwrap_or_default(), ); } } diff --git a/crates/evm2/src/evm/bal/bal_context.rs b/crates/evm2/src/evm/bal/bal_context.rs index f58a78888..fa491b8d6 100644 --- a/crates/evm2/src/evm/bal/bal_context.rs +++ b/crates/evm2/src/evm/bal/bal_context.rs @@ -69,7 +69,7 @@ impl BalContext { /// Attaches an EIP-7928 BAL consulted on reads, and returns `self`. #[inline] pub fn with_bal(mut self, bal: Arc) -> Self { - self.bal = Some(bal); + self.set_bal(bal); self } @@ -112,7 +112,7 @@ impl BalContext { /// Enables EIP-7928 BAL construction, installing an empty builder, and returns `self`. #[inline] pub fn with_bal_builder(mut self) -> Self { - self.bal_builder = Some(Bal::new()); + self.enable_bal_builder(); self } diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index 7e2093deb..e9cccb5d5 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -340,8 +340,9 @@ mod tests { bytecode::Bytecode, evm::{ bal::{AccountInfoBal, BalChanges, BalCodeChange, StorageBal}, - state::{Account, AccountFlags, AccountInfo, StorageOverlay, StorageSlot, Tracked}, + state::{Account, AccountFlags, AccountInfo, StorageOverlay}, }, + test_utils::storage_slot, }; use alloc::{vec, vec::Vec}; use alloy_eip7928::{ @@ -470,24 +471,19 @@ mod tests { assert!(AlloyBal::from(&bal).is_empty()); } - fn slot(original: U256, current: U256) -> StorageSlot { - StorageSlot { value: Tracked::from_parts(original, current), ..Default::default() } - } - #[test] fn commit_builds_bal_from_pending_overlay() { let address = Address::with_last_byte(1); // A freshly created account: no original info, present nonce/balance set, and one changed // storage slot plus one loaded-but-unchanged (read) slot. - let account = Account { - original: None, - present: Some(AccountInfo::default().with_nonce(1).with_balance(U256::from(100))), - ..Default::default() - }; + let account = Account::new( + None, + Some(AccountInfo::default().with_nonce(1).with_balance(U256::from(100))), + ); let mut overlay = StorageOverlay::default(); - overlay.slots.insert(U256::from(5), slot(U256::ZERO, U256::from(42))); - overlay.slots.insert(U256::from(6), slot(U256::from(7), U256::from(7))); + overlay.slots.insert(U256::from(5), storage_slot(U256::ZERO, U256::from(42))); + overlay.slots.insert(U256::from(6), storage_slot(U256::from(7), U256::from(7))); let account = Account { storage: overlay, ..account }; let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; @@ -519,13 +515,10 @@ mod tests { // (`present: None`), storage overlay wiped with the prior writes turned into // loaded-but-unchanged slots surfacing as reads. The BAL derives from that overlay // without special-casing. - let account = Account { - original: Some(AccountInfo::default().with_balance(U256::from(100))), - present: None, - ..Default::default() - }; + let account = + Account::new(Some(AccountInfo::default().with_balance(U256::from(100))), None); let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; - overlay.slots.insert(U256::from(5), slot(U256::from(42), U256::from(42))); + overlay.slots.insert(U256::from(5), storage_slot(U256::from(42), U256::from(42))); let account = Account { storage: overlay, flags: AccountFlags::DESTROYED, ..account }; let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; @@ -550,8 +543,8 @@ mod tests { // Only the slot that is nonzero after the wipe is recorded as one. let info = AccountInfo::default().with_balance(U256::from(100)); let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; - overlay.slots.insert(U256::from(5), slot(U256::from(42), U256::ZERO)); - overlay.slots.insert(U256::from(6), slot(U256::from(7), U256::from(8))); + overlay.slots.insert(U256::from(5), storage_slot(U256::from(42), U256::ZERO)); + overlay.slots.insert(U256::from(6), storage_slot(U256::from(7), U256::from(8))); let account = Account { original: Some(info.clone()), present: Some(info), diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 54f7d3c97..af81a2a9d 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -117,7 +117,7 @@ pub struct CacheDB { pub _non_exhaustive: (), } -impl Default for CacheDB { +impl Default for InMemoryDB { #[inline] fn default() -> Self { Self::new(EmptyDB::default()) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 86bbaef40..d848579fb 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -464,7 +464,10 @@ mod tests { MessageResult, MessageResultExt, Word, instructions as instr, op, }, registry::TxRegistry, - test_utils::{TestHost, TestTypes, legacy_bytecode, push, push_all}, + test_utils::{ + TestHost, TestTypes, call_code, create_code, legacy_bytecode, push, push_all, + return_top_word, + }, utils::address_to_word, }; use alloc::{boxed::Box, vec, vec::Vec}; @@ -708,34 +711,6 @@ mod tests { (result, inspector, evm) } - /// Appends code that returns the word at the top of the stack as the frame output. - fn return_top_word(code: &mut Vec) { - code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); - } - - fn call_code(target: Address) -> Vec { - let mut code = Vec::new(); - push_all( - &mut code, - [ - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - address_to_word(&target), - Word::from(1000), - ], - ); - code - } - - fn create_code() -> Vec { - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); - code - } - #[test] fn empty_calls_do_not_inspect_padding_stop() { for nested in [false, true] { @@ -2575,8 +2550,7 @@ mod tests { if marker == Address::with_last_byte(0x44) { self.snapshot = Some(interp.host().state().clone_with(EmptyDB::default())); } else if marker == Address::with_last_byte(0x55) { - *interp.host().state_mut() = - self.snapshot.as_ref().unwrap().clone_with(EmptyDB::default()); + *interp.host().state_mut() = self.snapshot.as_ref().unwrap().clone(); self.restored = true; } else { return None; diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 6276611a4..fae201da2 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -888,8 +888,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Replaces the precompile provider. #[inline] pub fn set_precompiles(&mut self, precompiles: impl PrecompileProvider + 'a) { - self.assert_precompiles_mutable(); - self.precompiles = boxed_precompile_provider(precompiles); + self.set_boxed_precompiles(boxed_precompile_provider(precompiles)); } /// Replaces the precompile provider with an already boxed one. @@ -1002,8 +1001,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Sets the active execution inspector. #[inline] pub fn set_inspector + 'a>(&mut self, inspector: I) { - self.assert_inspector_mutable(); - self.inspector = Some(InspectorSlot::Owned(boxed_inspector(inspector))); + self.set_boxed_inspector(boxed_inspector(inspector)); } /// Sets the active boxed execution inspector. @@ -1055,9 +1053,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Sets the optional external interpreter runner. #[inline] pub fn set_interpreter_runner>(&mut self, runner: R) { - self.assert_interpreter_runner_mutable(); - self.interpreter_runner = Some(Arc::new(runner)); - self.refresh_runner_support(); + self.set_shared_interpreter_runner(Arc::new(runner)); } /// Sets the optional shared external interpreter runner. @@ -1403,7 +1399,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { // `destination` already holds the create's contract address (set when the message was // constructed), so the create hook observes it directly. - let is_create = matches!(message.kind, MessageKind::Create | MessageKind::Create2); + let is_create = message.kind.is_create(); // Keeps the top-level frame's bytecode alive when a hook replaces `message.code`. let top_code; @@ -2150,11 +2146,12 @@ mod tests { BaseEvmConfigSelector, BaseEvmTypes, NoopInspector, Precompiles, SpecId, Version, bytecode::Bytecode, env::BlockEnvExt, - ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + ethereum::{TxEnvelope, ethereum_tx_registry}, interpreter::{GasTracker, Interpreter, Message, MessageExt, MessageKind, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileMap}, registry::{HandlerError, TxRequest, handler}, - test_utils::{legacy_bytecode, push_address}, + test_utils::{handle_test_tx, legacy_bytecode, push_address, test_tx}, + utils::address_to_word, }; use alloc::{borrow::Cow, string::ToString, sync::Arc, vec, vec::Vec}; use alloy_consensus::{TxLegacy, transaction::Recovered}; @@ -2201,22 +2198,6 @@ mod tests { assert_eq!(*evm.ext(), 42); } - fn test_tx(value: u64) -> RecoveredTxEnvelope { - Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), - Address::ZERO, - ) - } - - fn handle_test_tx(req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>) -> HandlerResult { - let _ = req.host.spec_id(); - Ok(TxResultExt { - status: true, - total_gas_spent: req.tx.nonce + 1, - ..TxResultExt::default() - }) - } - #[derive(Debug)] struct TestInterpreterRunner { stop: Option, @@ -2551,7 +2532,7 @@ mod tests { Host::load_account(&mut evm, &address, true, true), Err(HostError::Halt(InstrStop::OutOfGas)) )); - let stats = evm.database().downcast_ref::>().unwrap().counts(); + let stats = evm.database_as::>().unwrap().counts(); assert_eq!(stats.get_account, 0); assert_eq!(stats.get_code_by_hash, 0); @@ -2561,7 +2542,7 @@ mod tests { Host::sload(&mut evm, &address, &Word::ZERO, true), Err(HostError::Halt(InstrStop::OutOfGas)) )); - let stats = evm.database().downcast_ref::>().unwrap().counts(); + let stats = evm.database_as::>().unwrap().counts(); assert_eq!(stats.get_account, 1); assert_eq!(stats.get_storage, 0); assert_eq!(stats.get_code_by_hash, 0); @@ -2729,7 +2710,7 @@ mod tests { precompiles, ); - let error = evm.transact(&tx).map(ExecutedTx::discard).unwrap_err(); + let error = evm.call_tx(&tx).unwrap_err(); let HandlerError::Fatal(error) = error else { panic!("expected fatal precompile error") }; assert!(error.downcast_ref::().is_some()); assert_eq!(error.to_string(), "test precompile error"); @@ -3125,10 +3106,10 @@ mod tests { let executed = evm.transact_with_inspector(&tx, &mut OriginInspector(origin)).unwrap(); assert!(executed.commit().status); - let origin_word = Word::from_be_slice(origin.as_slice()); + let origin_word = address_to_word(&origin); assert_eq!(evm.state.storage_slot_untracked(&target, &Word::ZERO).unwrap(), origin_word); assert_eq!(evm.state.storage_slot_untracked(&inner, &Word::from(1)).unwrap(), origin_word); - assert_eq!(evm.state.account_info_untracked(&caller).unwrap().unwrap().nonce, 1); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().nonce, 1); } #[test] @@ -3430,7 +3411,7 @@ mod tests { set_spec(&mut evm, SpecId::PRAGUE); assert_eq!(run(&mut evm), (InstrStop::Return, 2)); - let mut version = *Version::base(SpecId::PRAGUE); + let mut version = Version::new(SpecId::PRAGUE); version.initial_call_depth = 1; evm.set_block_and_execution_config_within_spec( BlockEnvExt::default(), @@ -3519,7 +3500,7 @@ mod tests { TxEnvelope::as_legacy, handler(|_| Ok(()), |req, ()| fail(req)), ); - let error = evm.transact(&test_tx(7)).map(ExecutedTx::discard).unwrap_err(); + let error = evm.call_tx(&test_tx(7)).unwrap_err(); let HandlerError::Fatal(error) = error else { panic!("expected fatal handler error") }; assert_eq!(error.to_string(), "handler failed"); assert!(evm.logs().is_empty()); @@ -3821,7 +3802,7 @@ mod tests { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); } #[test] @@ -4032,12 +4013,11 @@ mod tests { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::FRONTIER)); let pending = evm.state.take_pending_state(); - let account = - pending.accounts.get(&created).and_then(|entry| entry.present.as_ref()).unwrap(); + let account = pending.account_info(&created).unwrap(); assert!(account.is_empty_code_hash()); } @@ -4188,7 +4168,7 @@ mod tests { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); let pending = evm.state.take_pending_state(); @@ -4223,7 +4203,7 @@ mod tests { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); let pending = evm.state.take_pending_state(); @@ -4289,7 +4269,7 @@ mod tests { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); let version = *evm.version(); evm.state.finalize_transaction_(&version); @@ -4872,7 +4852,7 @@ mod tests { assert_eq!(changes.account_info(&caller).unwrap().nonce, 1); let slot = changes.account_changes_for(&target).unwrap().storage.slots[&Word::ZERO].value; assert_eq!(slot.current, Word::from(1)); - let mut in_place = changes.account_changes().map(|c| c.address).collect::>(); + let mut in_place = executed.account_changes().map(|c| c.address).collect::>(); in_place.sort(); let _ = executed.discard(); @@ -4881,8 +4861,7 @@ mod tests { let executed = evm.transact(&tx).unwrap(); assert!(executed.result().status); let pending = executed.detach().pending_state; - let mut detached = - pending.changes().account_changes().map(|c| c.address).collect::>(); + let mut detached = pending.account_changes().map(|c| c.address).collect::>(); detached.sort(); assert_eq!(in_place, detached); assert_eq!(pending.changes().account_info(&caller).unwrap().nonce, 1); diff --git a/crates/evm2/src/evm/prewarm_set.rs b/crates/evm2/src/evm/prewarm_set.rs index be74165b0..7c87f639e 100644 --- a/crates/evm2/src/evm/prewarm_set.rs +++ b/crates/evm2/src/evm/prewarm_set.rs @@ -262,7 +262,7 @@ mod tests { assert!(prewarm_set.is_warm(&short_addr1)); assert!(prewarm_set.is_warm(&short_addr2)); assert!(prewarm_set.is_warm(&last_short_addr)); - assert!(!prewarm_set.is_warm(&Address::with_last_byte(20))); + assert!(prewarm_set.is_cold(&Address::with_last_byte(20))); assert!(prewarm_set.access_list.is_empty()); } @@ -284,7 +284,7 @@ mod tests { assert!(prewarm_set.is_warm(®ular_addr)); assert!(prewarm_set.is_warm(&boundary_addr)); - assert!(!prewarm_set.is_warm(&address!("0987654321098765432109876543210987654321"))); + assert!(prewarm_set.is_cold(&address!("0987654321098765432109876543210987654321"))); } #[test] @@ -303,7 +303,7 @@ mod tests { prewarm_set.clear(); assert_eq!(prewarm_set, PrewarmSet::new()); - assert!(addresses.iter().all(|address| !prewarm_set.is_warm(address))); + assert!(addresses.iter().all(|address| prewarm_set.is_cold(address))); } #[test] @@ -318,8 +318,8 @@ mod tests { assert!(prewarm_set.is_warm(®ular_addr)); prewarm_set.clear(); - assert!(!prewarm_set.is_warm(&short_addr)); - assert!(!prewarm_set.is_warm(®ular_addr)); + assert!(prewarm_set.is_cold(&short_addr)); + assert!(prewarm_set.is_cold(®ular_addr)); } #[test] @@ -336,9 +336,9 @@ mod tests { prewarm_set.remove(&short); prewarm_set.remove(&inline[0]); prewarm_set.remove(&mapped); - assert!(!prewarm_set.is_warm(&short)); - assert!(!prewarm_set.is_warm(&inline[0])); - assert!(!prewarm_set.is_warm(&mapped)); + assert!(prewarm_set.is_cold(&short)); + assert!(prewarm_set.is_cold(&inline[0])); + assert!(prewarm_set.is_cold(&mapped)); assert!(!prewarm_set.is_storage_warm(&mapped, &key)); assert!(prewarm_set.is_warm(&inline[1]) && prewarm_set.is_warm(&inline[2])); @@ -355,7 +355,7 @@ mod tests { prewarm_set.remove(&inline[0]); prewarm_set.remove(&short); for address in [inline[0], short] { - assert!(!prewarm_set.is_warm(&address)); + assert!(prewarm_set.is_cold(&address)); assert!(!prewarm_set.is_storage_warm(&address, &key)); } } diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 9216abbd1..a856fe12e 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -71,15 +71,7 @@ impl core::hash::Hash for AccountInfo { impl Default for AccountInfo { #[inline] fn default() -> Self { - Self { - balance: U256::ZERO, - nonce: 0, - code_hash: KECCAK256_EMPTY, - code: Some(Bytecode::default()), - #[cfg(feature = "account-ext")] - extension: super::AccountExtension::new(), - _non_exhaustive: (), - } + Self::new(U256::ZERO, 0, KECCAK256_EMPTY, Some(Bytecode::default())) } } @@ -445,7 +437,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Returns whether the account was created in the current transaction. #[inline] pub const fn is_created(&self) -> bool { - self.tracked.present.is_some() && self.tracked.is_created() + self.exists() && self.tracked.is_created() } /// Returns whether the account is dead by the EIP-161 definition while existing in the overlay. @@ -466,7 +458,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if features.contains(EvmFeatures::EIP161) { return self.tracked.present.as_ref().is_none_or(AccountInfo::is_empty); } - self.tracked.present.is_none() && !self.tracked.flags.contains(AccountFlags::TOUCHED) + self.tracked.present.is_none() && !self.is_touched() } /// Loads the account's bytecode, reading it from the backing database by code hash when it is @@ -519,7 +511,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// finalization even when no field changes. #[inline] pub fn touch(&mut self) { - if !self.tracked.flags.contains(AccountFlags::TOUCHED) { + if !self.is_touched() { self.record_change(); self.tracked.flags.insert(AccountFlags::TOUCHED); } @@ -546,9 +538,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// base warm set stay warm across rollback, so warming them again records nothing. #[inline] pub fn warm(&mut self) -> bool { - if self.tracked.flags.contains(AccountFlags::WARM) - || self.inner.prewarm_set.is_warm(&self.address) - { + if self.is_warm() { return false; } self.record_change(); diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 2ea8a8159..a3cf5006d 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -273,7 +273,7 @@ mod tests { }, BlockStateAccumulator, }; - use crate::interpreter::Word; + use crate::{interpreter::Word, test_utils::storage_slot}; use alloy_primitives::{Address, map::U256Map}; use core::convert::Infallible; @@ -326,14 +326,7 @@ mod tests { } fn slot(key: Word, original: Word, current: Word) -> U256Map { - U256Map::from_iter([( - key, - StorageSlot { - value: Tracked::from_parts(original, current), - is_warm: false, - _non_exhaustive: (), - }, - )]) + U256Map::from_iter([(key, storage_slot(original, current))]) } #[cfg(feature = "serde")] diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index aee788125..5c712271f 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -579,7 +579,7 @@ impl<'a> State<'a> { let account = entry.into_mut(); if skip_cold && !account.flags.contains(AccountFlags::WARM) - && !inner.prewarm_set.is_warm(address) + && inner.prewarm_set.is_cold(address) { return Err(LoadError::ColdLoadSkipped); } @@ -897,13 +897,7 @@ impl<'a> State<'a> { } }, JournalEntry::StorageWarmed { address, key } => { - if let Some(slot) = self - .accounts - .get_mut(&address) - .and_then(|account| account.storage.slots.get_mut(&key)) - { - slot.is_warm = false; - } + self.set_storage_warm(&address, key, false); } } } @@ -1056,7 +1050,7 @@ mod tests { state.tstore(&written, &Word::ZERO, &Word::from(9)); state.log(Log::default()); - let mut expected = state.clone_with(EmptyDB::default()); + let mut expected = state.clone(); expected.commit_transaction(); expected.clear_transaction_state(); let mut copy = state.committed_copy(EmptyDB::default()); diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index e64f5f8e9..0ab99a635 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -178,13 +178,13 @@ impl<'a, T: EvmTypes> Evm<'a, T> { // The reservoir is funded on top of `gas_limit`, so the transaction gas limit includes it. let (total_gas_spent, state_gas_spent) = gas_spent_breakdown(gas_limit + reservoir, &result.gas, 0); - let gas_refunded = if result.stop.is_success() && result.gas.refunded() > 0 { + let gas_refunded = if result.is_success() && result.gas.refunded() > 0 { result.gas.refunded() as u64 } else { 0 }; Ok(TxResultExt { - status: result.stop.is_success(), + status: result.is_success(), total_gas_spent, state_gas_spent, refunded: gas_refunded, @@ -256,6 +256,7 @@ mod tests { interpreter::{GasTracker, InstrStop, Interpreter, Message, MessageResult, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileResult}, registry::{HandlerError, TxRegistry}, + utils::address_to_word, }; type TestEvm = Evm<'static, BaseEvmTypes>; @@ -297,7 +298,7 @@ mod tests { assert!(unchanged(&beneficiary)); let storage = &result.pending_state.accounts.get(&contract).expect("storage changed").storage.slots; - let system_address = U256::from_be_slice(SYSTEM_ADDRESS.as_slice()); + let system_address = address_to_word(&SYSTEM_ADDRESS); assert_eq!(storage.get(&U256::ZERO).map(|slot| slot.value.current), Some(system_address)); assert_eq!(storage.get(&U256::ONE).map(|slot| slot.value.current), Some(system_address)); } @@ -335,7 +336,7 @@ mod tests { assert!(result.result.status); let storage = &result.pending_state.accounts.get(&contract).expect("storage changed").storage.slots; - let caller = U256::from_be_slice(caller.as_slice()); + let caller = address_to_word(&caller); assert_eq!(storage.get(&U256::ZERO).map(|slot| slot.value.current), Some(caller)); assert_eq!(storage.get(&U256::ONE).map(|slot| slot.value.current), Some(caller)); } diff --git a/crates/evm2/src/interpreter/dispatch/table/packed.rs b/crates/evm2/src/interpreter/dispatch/table/packed.rs index 4753e6d89..2d2fbd2f6 100644 --- a/crates/evm2/src/interpreter/dispatch/table/packed.rs +++ b/crates/evm2/src/interpreter/dispatch/table/packed.rs @@ -73,7 +73,7 @@ impl super::DispatchGas for RemainingGas { dynamic_gas: bool, ) { if dynamic_gas { - state.gas_mut().set_remaining(self.get()); + sync_loop_state(state, *self); } } diff --git a/crates/evm2/src/interpreter/gas.rs b/crates/evm2/src/interpreter/gas.rs index 0153bbb07..88e975360 100644 --- a/crates/evm2/src/interpreter/gas.rs +++ b/crates/evm2/src/interpreter/gas.rs @@ -160,7 +160,7 @@ impl GasTracker { /// Creates a gas tracker with `limit` execution gas. #[inline] pub const fn new(limit: u64) -> Self { - Self::from_parts(limit, limit, 0) + Self::new_with_execution_gas_and_reservoir(limit, 0) } /// Creates a gas tracker with execution gas and a state gas reservoir. @@ -349,7 +349,7 @@ impl GasTracker { #[inline] pub fn spend_state(&mut self, cost: u64) -> Result { if self.reservoir >= cost { - self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64); + self.add_state_gas_spent(cost as i64); self.reservoir -= cost; return Ok(()); } @@ -357,8 +357,8 @@ impl GasTracker { let spill = cost - self.reservoir; self.spend(spill)?; - self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64); - self.state_gas_spilled = self.state_gas_spilled.saturating_add(spill); + self.add_state_gas_spent(cost as i64); + self.add_state_gas_spilled(spill); self.reservoir = 0; Ok(()) } @@ -394,7 +394,7 @@ impl GasTracker { .reservoir .saturating_add_signed(self.state_gas_spent) .saturating_sub(self.state_gas_spilled); - self.remaining = self.remaining.saturating_add(self.state_gas_spilled); + self.remaining = self.remaining_excluding_state_spill(); self.state_gas_spent = 0; self.state_gas_spilled = 0; } @@ -959,11 +959,11 @@ mod tests { fn sibling_refill_restores_parent_regular_gas() { const STATE_GAS: u64 = 200; const CHILD_GAS: u64 = 500; - let mut parent = GasTracker::from_parts(1_000, 1_000, 0); + let mut parent = GasTracker::new(1_000); // A's state creation spills into execution gas and is merged into P. parent.spend(CHILD_GAS).unwrap(); - let mut child_a = GasTracker::from_parts(CHILD_GAS, CHILD_GAS, 0); + let mut child_a = GasTracker::new(CHILD_GAS); child_a.spend_state(STATE_GAS).unwrap(); child_a.settle_gas(InstrStop::Stop); parent.merge_child_gas(child_a, InstrStop::Stop); @@ -974,7 +974,8 @@ mod tests { // B clears A's slot, with no local spill counter to restore. parent.spend(CHILD_GAS).unwrap(); - let mut child_b = GasTracker::from_parts(CHILD_GAS, CHILD_GAS, parent.reservoir()); + let mut child_b = + GasTracker::new_with_execution_gas_and_reservoir(CHILD_GAS, parent.reservoir()); child_b.refill_reservoir(STATE_GAS); assert_eq!(child_b.reservoir(), STATE_GAS); assert_eq!(child_b.state_gas_spilled(), 0); diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index 648407ee2..cf02751d8 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -3,6 +3,7 @@ use crate::{ BaseEvmTypes, DatabaseError, EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, env::{BlockEnv, TxEnv}, evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, + utils::address_to_word, }; use alloc::borrow::Cow; use alloy_primitives::{Address, B256, Bytes}; @@ -60,7 +61,7 @@ impl MessageResultExt { pub fn created_address_for_parent(&self) -> Word { self.created_address .filter(|_| self.stop.is_success()) - .map(|address| Word::from_be_slice(address.as_slice())) + .map(|address| address_to_word(&address)) .unwrap_or_default() } diff --git a/crates/evm2/src/interpreter/instructions/block.rs b/crates/evm2/src/interpreter/instructions/block.rs index 7579c1ed7..dfb591888 100644 --- a/crates/evm2/src/interpreter/instructions/block.rs +++ b/crates/evm2/src/interpreter/instructions/block.rs @@ -90,19 +90,15 @@ pub fn slotnum(cx: _) -> Result { mod tests { use crate::{ SpecId, - env::{BlockEnv, BlockEnvExt, TxEnvExt}, + env::{BlockEnvExt, TxEnvExt}, interpreter::{InstrStop, MessageExt, Word, op}, - test_utils::{RunConfig, TestHost, TestTypes, push, run}, + test_utils::{RunConfig, TestHost, push, run, test_host}, utils::{address_to_word, b256_to_word}, }; use alloc::vec::Vec; use alloy_primitives::{Address, B256}; use core::assert_matches; - fn test_host(block: BlockEnv) -> TestHost { - TestHost { block, ..TestHost::default() } - } - #[test] fn blockhash_opcode() { let mut host = test_host(BlockEnvExt { number: Word::from(10), ..BlockEnvExt::default() }); diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index 085de3c03..055cd99cc 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -168,9 +168,9 @@ mod tests { use crate::{ SpecId, env::TxEnvExt, - interpreter::{InstrStop, Message, MessageExt, Word, op}, + interpreter::{InstrStop, MessageExt, Word, op}, test_utils::{ - RunConfig, TestHost, TestTypes, assert_stack, neg, push, run, run_stack, stack_code, + RunConfig, TestHost, assert_stack, neg, push, run, run_stack, stack_code, test_message, }, utils::{address_to_word, b256_to_word}, }; @@ -178,10 +178,6 @@ mod tests { use alloy_primitives::{Address, B256, Bytes}; use core::assert_matches; - fn test_message() -> Message { - MessageExt { gas_limit: 10_000, ..MessageExt::default() } - } - #[test] fn address_opcode() { let address = Address::from([0x11; 20]); diff --git a/crates/evm2/src/interpreter/instructions/host.rs b/crates/evm2/src/interpreter/instructions/host.rs index 2b7c37976..495e54fce 100644 --- a/crates/evm2/src/interpreter/instructions/host.rs +++ b/crates/evm2/src/interpreter/instructions/host.rs @@ -1,3 +1,4 @@ +use super::require_non_staticcall; use crate::{ EvmFeatures, EvmTypesHost, evm::SStore, @@ -10,14 +11,6 @@ use crate::{ use alloy_primitives::{Address, B256}; use evm2_macros::instruction; -#[inline] -const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { - if state.is_static() { - return Err(InstrStop::StateChangeDuringStaticCall); - } - Ok(()) -} - #[instruction(dynamic_gas)] pub fn sload(cx: _, [key]: [Word]) -> Result { // EIP-2929: SLOAD pays the warm read cost as static opcode gas, then only diff --git a/crates/evm2/src/interpreter/instructions/macro_tests.rs b/crates/evm2/src/interpreter/instructions/macro_tests.rs index eee94e981..aff8d1c53 100644 --- a/crates/evm2/src/interpreter/instructions/macro_tests.rs +++ b/crates/evm2/src/interpreter/instructions/macro_tests.rs @@ -2,7 +2,9 @@ use crate::{ BaseEvmConfig, EvmConfig, ExecutionConfig, SpecId, env::BlockEnvExt, interpreter::{Host, InstrStop, Interpreter, Message, Word, op}, - test_utils::{RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push}, + test_utils::{ + RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push, test_host, + }, version::OpcodeConfig, }; use alloc::vec::Vec; @@ -124,10 +126,7 @@ fn instruction_macro_no_stack_preamble_attribute() { #[test] fn instruction_macro_concrete_evm_types_equals_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(42), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(42), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([CONCRETE_EQ_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); @@ -136,10 +135,7 @@ fn instruction_macro_concrete_evm_types_equals_attribute() { #[test] fn instruction_macro_evm_types_colon_bound_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([TYPE_BOUND_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); @@ -148,10 +144,7 @@ fn instruction_macro_evm_types_colon_bound_attribute() { #[test] fn instruction_macro_evm_types_assoc_colon_bound_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([ASSOC_BOUND_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); diff --git a/crates/evm2/src/interpreter/instructions/mod.rs b/crates/evm2/src/interpreter/instructions/mod.rs index 9b34882c9..a03362eea 100644 --- a/crates/evm2/src/interpreter/instructions/mod.rs +++ b/crates/evm2/src/interpreter/instructions/mod.rs @@ -1,3 +1,8 @@ +use crate::{ + EvmTypesHost, + interpreter::{InstrStop, InterpreterState, Result}, +}; + mod arithmetic; pub use arithmetic::*; @@ -30,5 +35,13 @@ pub use system::*; pub mod i256; +#[inline] +const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { + if state.is_static() { + return Err(InstrStop::StateChangeDuringStaticCall); + } + Ok(()) +} + #[cfg(test)] mod macro_tests; diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index bee41ea37..227a6f4b0 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -34,7 +34,7 @@ pub fn dupn(cx: _) -> Result { pub fn swapn(cx: _) -> Result { let n = decode_single(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.exchange(0, n) + stack.swap(n) } #[instruction(no_stack_preamble)] diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 575bf1182..c24277192 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -1,5 +1,6 @@ //! System opcode implementations. +use super::require_non_staticcall; use crate::{ EvmFeatures, EvmTypesHost, bytecode::Bytecode, @@ -14,14 +15,6 @@ use alloy_primitives::{Address, B256, Bytes}; use core::{cmp::min, ops::Range}; use evm2_macros::instruction; -#[inline] -const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { - if state.is_static() { - return Err(InstrStop::StateChangeDuringStaticCall); - } - Ok(()) -} - #[inline] const fn should_charge_new_account_gas( eip161: bool, @@ -113,16 +106,18 @@ fn load_acc_and_calc_gas( code = delegated_account.code; code_address = delegated_address; } - let features = state.version().features; let mut new_account_state_gas = 0; if create_empty_account - && (!features.contains(EvmFeatures::EIP161) || transfers_value) - && account.is_empty_for_new_account_gas + && should_charge_new_account_gas( + state.feature(EvmFeatures::EIP161), + transfers_value, + account.is_empty_for_new_account_gas, + ) { cost += state.gas_params().get(GasId::NewAccountCost); // EIP-8037: value transfer to a new account also charges state gas from // the reservoir. Charged before the 63/64 child gas split below. - if features.contains(EvmFeatures::EIP8037) && transfers_value { + if state.feature(EvmFeatures::EIP8037) && transfers_value { new_account_state_gas = state.gas_params().new_account_state_gas(); } } @@ -307,7 +302,7 @@ pub fn call_with( // gas upfront on this frame. If the call does not succeed (depth/balance failure, // child revert or halt) the target is not created, so refund the upfront charge to // the reservoir (execution-specs `credit_state_gas_refund`), mirroring CREATE. - if new_account_state_gas != 0 && !result.stop.is_success() { + if new_account_state_gas != 0 && !result.is_success() { gas.refill_reservoir(new_account_state_gas); } after(gas, state, &message, &result)?; @@ -317,7 +312,7 @@ pub fn call_with( state.memory().set_unchecked(return_memory_range.start, output); } state.swap_return_data(&mut result.output); - stack.push_u64(result.stop.is_success() as u64)?; + stack.push_u64(result.is_success() as u64)?; // A hook's stop or a config reload. state.result() } @@ -481,13 +476,13 @@ pub fn create_with( // `created_address == None` — depth, out-of-funds, nonce overflow), no new account leaf is // created, so refund the charge to the reservoir via `refill_reservoir` (matching 0→x→0 storage // restoration). A successful deployment keeps the charge; an alive target was never charged. - let create_failed = result.created_address.is_none() || !result.stop.is_success(); + let create_failed = result.created_address.is_none() || !result.is_success(); if charged_create_state_gas && create_failed { gas.refill_reservoir(state.gas_params().create_state_gas()); } after(gas, state, &message, &result)?; // EIP-211 exposes CREATE failure data only for REVERT; other failures clear returndata. - if result.stop == InstrStop::Revert { + if result.stop.is_revert() { state.swap_return_data(&mut result.output); } else { state.clear_return_data(); @@ -535,7 +530,8 @@ mod tests { constants::{CALL_DEPTH_LIMIT, MAX_INITCODE_SIZE}, interpreter::{Interpreter, MessageResultExt, op}, test_utils::{ - RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push, push_all, run, + RunConfig, TestHost, TestInterpreter, TestTypes, create_code, legacy_bytecode, push, + push_all, run, }, utils::address_to_word, version::OpcodeConfig, @@ -581,19 +577,7 @@ mod tests { let target = Address::from([0x22; 20]); let error = ExecutionError::Fatal("child call failed".into()); let mut host = TestHost { execute_error: Some(error.clone()), ..Default::default() }; - let mut code = Vec::new(); - push_all( - &mut code, - [ - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - address_to_word(&target), - Word::from(1000), - ], - ); + let mut code = crate::test_utils::call_code(target); code.extend([op::CALL, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host)); @@ -932,8 +916,7 @@ mod tests { fn create_propagates_fatal_child_result() { let error = ExecutionError::Fatal("child create failed".into()); let mut host = TestHost { execute_error: Some(error.clone()), ..Default::default() }; - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -986,8 +969,7 @@ mod tests { #[test] fn create_too_deep_charges_create_gas() { let mut host = TestHost::default(); - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code) @@ -1007,8 +989,7 @@ mod tests { // the read would leak the address into the EIP-7928 block access list. let caller = Address::from([0x11; 20]); let mut host = TestHost { exists: false, ..Default::default() }; - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code) @@ -1112,7 +1093,7 @@ mod tests { Ok(()) }, |gas, _, _, result| { - if result.stop.is_success() { gas.spend(AFTER_FEE) } else { Err(InstrStop::Revert) } + if result.is_success() { gas.spend(AFTER_FEE) } else { Err(InstrStop::Revert) } }, ) } @@ -1244,8 +1225,7 @@ mod tests { created_address: Some(created), ..MessageResultExt::default() }; - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); let mut base_code = code.clone(); base_code.extend([op::CREATE, op::STOP]); code.extend([HOOKED_CREATE, op::STOP]); @@ -1268,8 +1248,7 @@ mod tests { #[test] fn create_with_skips_hooks_when_creation_is_refused_before_forward() { - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); let mut base_code = code.clone(); base_code.extend([op::CREATE, op::STOP]); code.extend([HOOKED_CREATE, op::STOP]); diff --git a/crates/evm2/src/interpreter/opcode.rs b/crates/evm2/src/interpreter/opcode.rs index 30a17e406..a5ad0fae5 100644 --- a/crates/evm2/src/interpreter/opcode.rs +++ b/crates/evm2/src/interpreter/opcode.rs @@ -200,7 +200,7 @@ impl fmt::Display for OpCode { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let n = self.get(); - if let Some(val) = OPCODE_INFO[n as usize] { + if let Some(val) = OPCODE_INFO[self.as_usize()] { f.write_str(val.name()) } else { write!(f, "UNKNOWN(0x{n:02X})") @@ -650,6 +650,6 @@ mod tests { assert_eq!(OpCode::SWAP16.input_output(), (17, 17)); assert!(OpCode::RETURN.info().is_terminating()); assert!(!OpCode::ADD.info().is_terminating()); - assert_eq!(OpCode::new_or_unknown(0x0C).info().name(), "UNKNOWN"); + assert_eq!(OpCode::new_or_unknown(0x0C).as_str(), "UNKNOWN"); } } diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 940835752..e0e0ae1e3 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -705,7 +705,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { // SAFETY: The inspector pointer is valid while the frame executes. && unsafe { inspector.as_ref() }.inspector_config().steps() != self.0.steps { - self.0.result = Err(InstrStop::Reload); + self.set_result(Err(InstrStop::Reload)); } } @@ -739,10 +739,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { /// Returns the host implementation. #[inline] pub const fn host(&mut self) -> &mut T::Host<'host> { - // SAFETY: `host` is initialized at the beginning of `run` and cleared before the - // method returns. Instruction execution is synchronous, so the pointer cannot outlive the - // `run` host borrow. - unsafe { self.0.host.unwrap_unchecked().as_mut() } + self.0.host() } /// Returns the active runtime version data. @@ -855,7 +852,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { if reload { cold_path(); // The hook sees the instruction as done, with the pc after it. - self.0.result = Ok(()); + self.set_result(Ok(())); // SAFETY: A next instruction follows the instruction at `pc`. self.0.pc = unsafe { pc.as_ptr().add(dispatch::instruction_len(pc.op())) }; } else if self.0.result.is_err() { @@ -873,7 +870,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { if reload { self.0.pc = pc.as_ptr(); if self.0.result.is_ok() { - self.0.result = Err(InstrStop::Reload); + self.set_result(Err(InstrStop::Reload)); } } } diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index 753ce57db..bcb7caaa4 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -4,7 +4,7 @@ use crate::{ interpreter::GasTracker, precompiles::{PrecompileHalt, PrecompileOutput, PrecompileResult, eip7823}, - utils::{left_pad, left_pad_vec_be, read_word_padded, right_pad_vec}, + utils::{b256_to_word, left_pad, left_pad_vec_be, read_word_padded, right_pad_vec}, }; #[cfg(feature = "gmp")] use alloc::vec; @@ -202,9 +202,9 @@ pub fn run_inner( const HEADER_LENGTH: usize = 96; // Extract the header - let base_len = U256::from_be_bytes(read_word_padded(input, 0).0); - let exp_len = U256::from_be_bytes(read_word_padded(input, 32).0); - let mod_len = U256::from_be_bytes(read_word_padded(input, 64).0); + let base_len = b256_to_word(read_word_padded(input, 0)); + let exp_len = b256_to_word(read_word_padded(input, 32)); + let mod_len = b256_to_word(read_word_padded(input, 64)); // Lengths that do not fit in 64 bits cost more gas than any call has. let [base_len64, exp_len64, mod_len64] = @@ -353,8 +353,7 @@ fn osaka_multiplication_complexity(max_len: u64) -> U256 { return U256::from(16); } - let words = U256::from(max_len.div_ceil(8)); - words * words * U256::from(2) + berlin_multiplication_complexity(max_len) * U256::from(2) } #[cfg(test)] diff --git a/crates/evm2/src/precompiles/table.rs b/crates/evm2/src/precompiles/table.rs index 37d491d81..22215c566 100644 --- a/crates/evm2/src/precompiles/table.rs +++ b/crates/evm2/src/precompiles/table.rs @@ -668,7 +668,7 @@ mod tests { map.warm_addresses(&mut warm); assert!(warm.is_warm(&identity_address)); assert!(warm.is_warm(&long)); - assert!(!warm.is_warm(&sha256_address)); + assert!(warm.is_cold(&sha256_address)); map.move_precompiles([(long, sha256_address)]).unwrap(); assert!(!map.contains(&long)); @@ -677,6 +677,6 @@ mod tests { let mut warm = PrewarmSet::new(); map.warm_addresses(&mut warm); assert!(warm.is_warm(&sha256_address)); - assert!(!warm.is_warm(&long)); + assert!(warm.is_cold(&long)); } } diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index df4aaf886..db3130324 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -1,16 +1,21 @@ use crate::{ - BaseEvmConfigSelector, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, SpecId, + BaseEvmConfigSelector, BaseEvmTypes, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, + SpecId, TxResult, TxResultExt, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::{BlockEnv, BlockEnvExt, TxEnv, TxEnvExt}, - evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, + ethereum::{RecoveredTxEnvelope, TxEnvelope}, + evm::{AccountLoad, SLoad, SStore, SelfDestructResult, StorageSlot, Tracked}, interpreter::{ Gas, GasTracker, Host, InstrStop, Interpreter, Memory, Message, MessageExt, MessageKind, MessageResult, MessageResultExt, StackBacking, Word, op, }, + registry::{HandlerResult, TxRequest}, storage_key::{StorageKey, StorageKeyMap}, + utils::address_to_word, }; use alloc::{borrow::Cow, boxed::Box, vec::Vec}; +use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, B256, Bytes, Log}; use core::{assert_matches, ops::Range}; @@ -119,7 +124,6 @@ impl Host for TestHost { return Err(InstrStop::OutOfGas.into()); } self.account_loads.push(*address); - let features = self.execution_config.version().features; Ok(AccountLoad { balance: address.into_word().into(), nonce: 0, @@ -130,7 +134,11 @@ impl Host for TestHost { Bytecode::default() }, is_empty: self.is_empty, - is_empty_for_new_account_gas: if features.contains(EvmFeatures::EIP161) { + is_empty_for_new_account_gas: if self + .execution_config + .version() + .feature(EvmFeatures::EIP161) + { !self.exists || self.is_empty } else { !self.exists && !self.is_touched @@ -330,13 +338,21 @@ impl Default for RunConfig<'_> { host: None, spec_id: SpecId::OSAKA, tx_env: TxEnvExt::default(), - message: MessageExt { gas_limit: 10_000, ..MessageExt::default() }, + message: test_message(), gas_limit: 10_000, return_data: Bytes::new(), } } } +pub(crate) fn test_message() -> Message { + MessageExt { gas_limit: 10_000, ..MessageExt::default() } +} + +pub(crate) fn test_host(block: BlockEnv) -> TestHost { + TestHost { block, ..TestHost::default() } +} + pub(crate) fn run(config: RunConfig<'_>) -> TestInterpreter { let RunConfig { code, host, spec_id, tx_env, message, gas_limit, return_data } = config; let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; @@ -441,7 +457,7 @@ pub(crate) fn push_all(code: &mut Vec, values: [T } pub(crate) fn push_address(code: &mut Vec, address: &Address) { - push(code, Word::from_be_slice(address.as_slice())); + push(code, address_to_word(address)); } pub(crate) fn neg(value: u64) -> Word { @@ -451,3 +467,49 @@ pub(crate) fn neg(value: u64) -> Word { pub(crate) fn legacy_bytecode(code: impl Into>) -> Bytecode { Bytecode::new_legacy(Bytes::from(code.into())) } + +pub(crate) fn test_tx(value: u64) -> RecoveredTxEnvelope { + Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), + Address::ZERO, + ) +} + +pub(crate) fn handle_test_tx( + req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, +) -> HandlerResult { + let _ = req.host.spec_id(); + Ok(TxResultExt { status: true, total_gas_spent: req.tx.nonce + 1, ..TxResultExt::default() }) +} + +/// Appends code that returns the word at the top of the stack as the frame output. +pub(crate) fn return_top_word(code: &mut Vec) { + code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); +} + +pub(crate) fn storage_slot(original: Word, current: Word) -> StorageSlot { + StorageSlot { value: Tracked::from_parts(original, current), ..Default::default() } +} + +pub(crate) fn call_code(target: Address) -> Vec { + let mut code = Vec::new(); + push_all( + &mut code, + [ + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + address_to_word(&target), + Word::from(1000), + ], + ); + code +} + +pub(crate) fn create_code() -> Vec { + let mut code = Vec::new(); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + code +} diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 4ecf47030..37dd83c3a 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -10,7 +10,7 @@ use crate::{ evm::{AccountInfo, InMemoryDB, SYSTEM_ADDRESS}, interpreter::{GasTracker, Host, InstrStop, MessageExt, Word, op}, registry::TxRegistry, - test_utils::{legacy_bytecode, push, push_address, push_all}, + test_utils::{legacy_bytecode, push, push_address, push_all, return_top_word}, version::GasId, }; use alloc::{vec, vec::Vec}; @@ -29,7 +29,7 @@ fn run_tx(evm: &mut TestEvm, destination: Address, code: impl Into>) { ..Default::default() }; let result = Host::execute_message(evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); } #[test] @@ -138,7 +138,7 @@ fn evm_propagates_child_sstore_negative_refund() { }; let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); assert_eq!(result.gas.refunded(), 0); } @@ -490,11 +490,6 @@ fn log_collection_tx() -> RecoveredTxEnvelope { ) } -/// Appends code that returns the word at the top of the stack. -fn return_top_word(code: &mut Vec) { - code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); -} - #[test] fn log_collection_off_keeps_log_semantics() { // LOG4 over memory that is already allocated, then LOG0 past it, which grows memory to four @@ -663,7 +658,7 @@ fn precompile_dispatch_uses_original_callee( .unwrap(); let result = Host::execute_message(&mut evm, &mut frame.message).unwrap(); - assert!(result.stop.is_success(), "{:?}", result.stop); + assert!(result.is_success(), "{:?}", result.stop); assert_eq!(result.output.as_ref(), expected_output); // Geth's Prague gas usage includes delegation resolution even for precompile callees. let expected_gas = match opcode { diff --git a/crates/evm2/src/version/gas_params.rs b/crates/evm2/src/version/gas_params.rs index 495800403..b33c20a93 100644 --- a/crates/evm2/src/version/gas_params.rs +++ b/crates/evm2/src/version/gas_params.rs @@ -211,7 +211,7 @@ impl Index for GasParams { impl IndexMut for GasParams { #[inline] fn index_mut(&mut self, id: GasId) -> &mut Self::Output { - &mut self.table[id.as_usize()] + self.get_mut(id) } } @@ -258,28 +258,34 @@ impl GasParams { self.get(GasId::ExpByte).saturating_mul(power.bit_len().div_ceil(8) as u64) } + /// Calculates the per-word gas `id` for `len` bytes. + #[inline] + const fn per_word_cost(&self, id: GasId, len: usize) -> u64 { + self.get(id).saturating_mul(num_words(len) as u64) + } + /// Calculates copy gas for `len` bytes. #[inline] pub const fn copy_cost(&self, len: usize) -> u64 { - self.get(GasId::CopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::CopyPerWord, len) } /// Calculates `EXTCODECOPY` copy gas for `len` bytes. #[inline] pub const fn extcodecopy_cost(&self, len: usize) -> u64 { - self.get(GasId::ExtcodecopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::ExtcodecopyPerWord, len) } /// Calculates `MCOPY` copy gas for `len` bytes. #[inline] pub const fn mcopy_cost(&self, len: usize) -> u64 { - self.get(GasId::McopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::McopyPerWord, len) } /// Calculates `KECCAK256` word gas for `len` bytes. #[inline] pub const fn keccak256_word_cost(&self, len: usize) -> u64 { - self.get(GasId::Keccak256PerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::Keccak256PerWord, len) } /// Calculates dynamic `LOG` gas. @@ -293,14 +299,13 @@ impl GasParams { /// Calculates initcode word gas for `len` bytes. #[inline] pub const fn initcode_cost(&self, len: usize) -> u64 { - self.get(GasId::InitcodePerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::InitcodePerWord, len) } /// Calculates dynamic `CREATE2` gas for `len` bytes. #[inline] pub const fn create2_cost(&self, len: usize) -> u64 { - self.get(GasId::Create) - .saturating_add(self.get(GasId::Keccak256PerWord).saturating_mul(num_words(len) as u64)) + self.get(GasId::Create).saturating_add(self.keccak256_word_cost(len)) } /// Returns `CALL` stipend reduction. @@ -427,8 +432,7 @@ impl GasParams { /// Returns `SELFDESTRUCT` cold account cost. #[inline] pub const fn selfdestruct_cold_cost(&self) -> u64 { - self.get(GasId::ColdAccountAdditionalCost) - .saturating_add(self.get(GasId::WarmStorageReadCost)) + self.cold_account_additional_cost().saturating_add(self.get(GasId::WarmStorageReadCost)) } /// Calculates `SELFDESTRUCT` dynamic gas. @@ -543,7 +547,7 @@ mod tests { fn gas_id_roundtrips_values() { assert_eq!(GasId::from_usize(0), Some(GasId::ExpByte)); assert_eq!(GasId::ExpByte.as_usize(), 0); - assert_eq!(GasId::from_usize(GasId::MAX as usize), Some(GasId::Custom7)); + assert_eq!(GasId::from_usize(GasId::MAX.as_usize()), Some(GasId::Custom7)); assert_eq!(GasId::from_usize(GasId::COUNT), None); } @@ -579,7 +583,7 @@ mod tests { let berlin = gas_params(SpecId::BERLIN); assert_eq!(berlin.get(GasId::SstoreStatic), 100); - assert_eq!(berlin.get(GasId::ColdAccountAdditionalCost), 2500); + assert_eq!(berlin.cold_account_additional_cost(), 2500); assert_eq!(berlin.get(GasId::ColdStorageCost), 2100); assert_eq!(berlin.get(GasId::MaxRefundQuotient), 2); @@ -607,7 +611,7 @@ mod tests { assert_eq!(amsterdam.get(GasId::Create), 12_000); assert_eq!(amsterdam.get(GasId::WarmStorageReadCost), 100); // unchanged assert_eq!(amsterdam.get(GasId::ColdStorageCost), 2000); - assert_eq!(amsterdam.get(GasId::ColdAccountAdditionalCost), 2900); + assert_eq!(amsterdam.cold_account_additional_cost(), 2900); assert_eq!(amsterdam.get(GasId::TransferValueCost), 11_300); // CALL folds the account-write surcharge into CALL_VALUE, so a new target // adds no extra execution gas (only NEW_ACCOUNT state gas). diff --git a/crates/jit/codegen/src/bytecode/passes/const_fold.rs b/crates/jit/codegen/src/bytecode/passes/const_fold.rs index 6292b656d..f50b3f777 100644 --- a/crates/jit/codegen/src/bytecode/passes/const_fold.rs +++ b/crates/jit/codegen/src/bytecode/passes/const_fold.rs @@ -971,85 +971,94 @@ mod tests { let (c_big, c_max) = (c(U256::from(1) << 226), c(U256::MAX)); let top = AbsValue::Top; - // (opcode, operands in natural order, expected bounds). - type Case<'a> = (u8, &'a [AbsValue], Option<(u32, u32)>); - let cases: &[Case<'_>] = &[ - (op::ADD, &[r0_3, r5_9], Some((5, 12))), - (op::ADD, &[r0_3, top], None), - (op::SUB, &[r5_9, r0_3], Some((2, 9))), + struct Case { + opcode: u8, + operands: Vec, + expected: Option<(u32, u32)>, + } + + let case = |opcode, operands: &[AbsValue], expected| Case { + opcode, + operands: operands.to_vec(), + expected, + }; + let cases = [ + case(op::ADD, &[r0_3, r5_9], Some((5, 12))), + case(op::ADD, &[r0_3, top], None), + case(op::SUB, &[r5_9, r0_3], Some((2, 9))), // `1 - 4` wraps. - (op::SUB, &[r1_4, r1_4], None), - (op::SUB, &[r0_3, c1], None), - (op::MUL, &[r0_3, c40], Some((0, 120))), - (op::DIV, &[r5_9, r1_4], Some((1, 9))), - (op::DIV, &[r5_9, r0_3], Some((0, 9))), - (op::DIV, &[r5_9, top], Some((0, 9))), - (op::DIV, &[top, c_big], Some((0, (1 << 30) - 1))), - (op::DIV, &[top, c224], None), - (op::DIV, &[top, c0], Some((0, 0))), - (op::SDIV, &[r5_9, r1_4], Some((1, 9))), - (op::SDIV, &[r5_9, top], None), + case(op::SUB, &[r1_4, r1_4], None), + case(op::SUB, &[r0_3, c1], None), + case(op::MUL, &[r0_3, c40], Some((0, 120))), + case(op::DIV, &[r5_9, r1_4], Some((1, 9))), + case(op::DIV, &[r5_9, r0_3], Some((0, 9))), + case(op::DIV, &[r5_9, top], Some((0, 9))), + case(op::DIV, &[top, c_big], Some((0, (1 << 30) - 1))), + case(op::DIV, &[top, c224], None), + case(op::DIV, &[top, c0], Some((0, 0))), + case(op::SDIV, &[r5_9, r1_4], Some((1, 9))), + case(op::SDIV, &[r5_9, top], None), // Solar's selector dispatch indexes with `selector % 16`. - (op::MOD, &[top, c16], Some((0, 15))), - (op::MOD, &[r0_3, c5], Some((0, 3))), - (op::MOD, &[r5_9, r0_3], Some((0, 2))), - (op::MOD, &[r5_9, c_max], Some((5, 9))), - (op::MOD, &[top, c0], Some((0, 0))), - (op::MOD, &[top, top], None), - (op::SMOD, &[r5_9, c3], Some((0, 2))), - (op::SMOD, &[r5_9, c_max], Some((0, 9))), - (op::SMOD, &[top, c16], None), - (op::ADDMOD, &[r0_3, r0_3, c40], Some((0, 6))), - (op::ADDMOD, &[top, top, c40], Some((0, 39))), - (op::ADDMOD, &[top, top, c0], Some((0, 0))), - (op::MULMOD, &[r1_4, r1_4, top], Some((0, 16))), - (op::EXP, &[c2, r0_3], Some((1, 8))), - (op::EXP, &[r0_3, c2], Some((0, 9))), - (op::EXP, &[r0_3, r0_3], Some((0, 27))), - (op::EXP, &[c2, r0_39], None), - (op::SIGNEXTEND, &[c0, r0_3], Some((0, 3))), - (op::SIGNEXTEND, &[c0, r0_255], None), - (op::SIGNEXTEND, &[c1, r0_255], Some((0, 255))), - (op::SIGNEXTEND, &[top, r0_39], Some((0, 39))), - (op::LT, &[r0_3, r5_9], Some((1, 1))), - (op::LT, &[r5_9, r0_3], Some((0, 0))), - (op::LT, &[r0_3, r1_4], Some((0, 1))), - (op::GT, &[r5_9, r0_3], Some((1, 1))), - (op::SLT, &[top, c0], Some((0, 1))), - (op::SGT, &[r0_3, r5_9], Some((0, 0))), - (op::EQ, &[r0_3, r5_9], Some((0, 0))), - (op::EQ, &[top, top], Some((0, 1))), - (op::ISZERO, &[r1_4], Some((0, 0))), - (op::ISZERO, &[r0_3], Some((0, 1))), - (op::ISZERO, &[top], Some((0, 1))), - (op::AND, &[top, c0xffff], Some((0, 0xffff))), - (op::AND, &[r5_9, r0_3], Some((0, 3))), - (op::AND, &[top, c_max], None), - (op::OR, &[r5_9, r1_4], Some((5, 15))), - (op::OR, &[r0_3, top], None), - (op::XOR, &[r5_9, r0_3], Some((0, 15))), - (op::NOT, &[r0_3], None), - (op::BYTE, &[r(28, 31), word], Some((0x00, 0x33))), - (op::BYTE, &[r(30, 40), word], Some((0, 0x33))), - (op::BYTE, &[top, top], Some((0, 255))), - (op::BYTE, &[r(0, 32), r0_39], Some((0, 39))), - (op::SHL, &[r0_3, c1], Some((1, 8))), - (op::SHL, &[c256, top], Some((0, 0))), - (op::SHL, &[r(0, 40), c1], None), - (op::SHR, &[c224, top], None), - (op::SHR, &[r(225, 230), top], Some((0, (1 << 31) - 1))), - (op::SHR, &[c255, top], Some((0, 1))), - (op::SHR, &[r0_3, r(16, 31)], Some((2, 31))), - (op::SHR, &[r(0, 16), c_big], None), - (op::SHR, &[r(222, 230), c_big], Some((0, 16))), - (op::SAR, &[r0_3, r(16, 31)], Some((2, 31))), - (op::SAR, &[r(220, 230), c_max], None), - (op::CLZ, &[r1_4], Some((253, 255))), - (op::CLZ, &[r0_3], Some((254, 256))), - (op::CLZ, &[top], Some((0, 256))), - (op::CALLDATALOAD, &[r0_3], None), + case(op::MOD, &[top, c16], Some((0, 15))), + case(op::MOD, &[r0_3, c5], Some((0, 3))), + case(op::MOD, &[r5_9, r0_3], Some((0, 2))), + case(op::MOD, &[r5_9, c_max], Some((5, 9))), + case(op::MOD, &[top, c0], Some((0, 0))), + case(op::MOD, &[top, top], None), + case(op::SMOD, &[r5_9, c3], Some((0, 2))), + case(op::SMOD, &[r5_9, c_max], Some((0, 9))), + case(op::SMOD, &[top, c16], None), + case(op::ADDMOD, &[r0_3, r0_3, c40], Some((0, 6))), + case(op::ADDMOD, &[top, top, c40], Some((0, 39))), + case(op::ADDMOD, &[top, top, c0], Some((0, 0))), + case(op::MULMOD, &[r1_4, r1_4, top], Some((0, 16))), + case(op::EXP, &[c2, r0_3], Some((1, 8))), + case(op::EXP, &[r0_3, c2], Some((0, 9))), + case(op::EXP, &[r0_3, r0_3], Some((0, 27))), + case(op::EXP, &[c2, r0_39], None), + case(op::SIGNEXTEND, &[c0, r0_3], Some((0, 3))), + case(op::SIGNEXTEND, &[c0, r0_255], None), + case(op::SIGNEXTEND, &[c1, r0_255], Some((0, 255))), + case(op::SIGNEXTEND, &[top, r0_39], Some((0, 39))), + case(op::LT, &[r0_3, r5_9], Some((1, 1))), + case(op::LT, &[r5_9, r0_3], Some((0, 0))), + case(op::LT, &[r0_3, r1_4], Some((0, 1))), + case(op::GT, &[r5_9, r0_3], Some((1, 1))), + case(op::SLT, &[top, c0], Some((0, 1))), + case(op::SGT, &[r0_3, r5_9], Some((0, 0))), + case(op::EQ, &[r0_3, r5_9], Some((0, 0))), + case(op::EQ, &[top, top], Some((0, 1))), + case(op::ISZERO, &[r1_4], Some((0, 0))), + case(op::ISZERO, &[r0_3], Some((0, 1))), + case(op::ISZERO, &[top], Some((0, 1))), + case(op::AND, &[top, c0xffff], Some((0, 0xffff))), + case(op::AND, &[r5_9, r0_3], Some((0, 3))), + case(op::AND, &[top, c_max], None), + case(op::OR, &[r5_9, r1_4], Some((5, 15))), + case(op::OR, &[r0_3, top], None), + case(op::XOR, &[r5_9, r0_3], Some((0, 15))), + case(op::NOT, &[r0_3], None), + case(op::BYTE, &[r(28, 31), word], Some((0x00, 0x33))), + case(op::BYTE, &[r(30, 40), word], Some((0, 0x33))), + case(op::BYTE, &[top, top], Some((0, 255))), + case(op::BYTE, &[r(0, 32), r0_39], Some((0, 39))), + case(op::SHL, &[r0_3, c1], Some((1, 8))), + case(op::SHL, &[c256, top], Some((0, 0))), + case(op::SHL, &[r(0, 40), c1], None), + case(op::SHR, &[c224, top], None), + case(op::SHR, &[r(225, 230), top], Some((0, (1 << 31) - 1))), + case(op::SHR, &[c255, top], Some((0, 1))), + case(op::SHR, &[r0_3, r(16, 31)], Some((2, 31))), + case(op::SHR, &[r(0, 16), c_big], None), + case(op::SHR, &[r(222, 230), c_big], Some((0, 16))), + case(op::SAR, &[r0_3, r(16, 31)], Some((2, 31))), + case(op::SAR, &[r(220, 230), c_max], None), + case(op::CLZ, &[r1_4], Some((253, 255))), + case(op::CLZ, &[r0_3], Some((254, 256))), + case(op::CLZ, &[top], Some((0, 256))), + case(op::CALLDATALOAD, &[r0_3], None), ]; - for &(opcode, operands, expected) in cases { + for Case { opcode, operands, expected } in cases { let inputs: Vec<_> = operands.iter().rev().copied().collect(); let folded = try_range_fold(opcode, &inputs, &mut interner).map(|v| { let r = v.bounds().unwrap(); From 244aa8df9207ad59c956023614b292b8b1cebef1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:31:08 +0100 Subject: [PATCH 122/159] perf(interpreter): pass the unpacked cursor as one u64 on wasm and remove single_return (#1000) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rust's wasm ABI returns a `(Pc, usize)` pair through a result pointer in linear memory. Every `unpacked` handler takes an extra pointer argument and stores both values, and the dispatch loop loads them back. On wasm32 the `unpacked` backend now carries the pc in the high 32 bits and the stack length in the low 32 bits of one `u64`, which wasm returns as a single `i64`; an `unpacked` handler goes from `(param i32 i32 i32 i32 i32)` to `(param i32 i32 i32 i32) (result i64)`. Other targets keep the pair. With the cheaper return, `unpacked` is faster than `single_return`, so `auto` now resolves to `unpacked` on wasm. `single_return` has no remaining use, so this PR removes that backend, its fuzzer matrix entry, and its docs; the numbers below use it only as the baseline. `cargo bench -p evm2-cli --bench evm` built for `wasm32-wasip1` and run on AMD EPYC 4244P (Zen 4), one process per binary, medians. Geomean time over cases of at least 5 µs, relative to `single_return` (the previous wasm default): | runtime | single_return | unpacked (before) | unpacked (this PR) | |---|---|---|---| | wasmtime 50, 2–3 rounds | 1.00 | 🔴 1.42 | 🟢 0.88 | | V8 (Node 24 WASI), 2 rounds | 1.00 | 🔴 1.27 | 🟢 0.87 | On wasmtime the cursor change alone is −38% geomean against the old `unpacked`. Per case, `single_return` → this PR: | case | wasmtime | V8 | |---|---|---| | snailtracer | 119.7 → 🟢 97.6 ms | 133.1 → 🟢 109.8 ms | | burntpix | 100.7 → 🟢 81.8 ms | 108.9 → 🟢 90.1 ms | | erc20_transfer | 5.92 → 🟢 5.06 ms | 6.68 → 🟢 5.53 ms | | mainnet replay | 424.7 → 🟢 356.8 ms | 376.8 → 🟢 355.3 ms | Instruction counts per iteration stay within ±6% of `single_return`; the gain is IPC. `single_return` keeps the stack length in memory and is latency-bound, and the old `unpacked` wrote `(pc, len)` to memory and read it back on every instruction. Zen 4 `perf stat` counters, medians over 6 cases: | runtime | backend | IPC | store-forwarding stalls per 1k instructions | branch-miss % | |---|---|---|---|---| | wasmtime | single_return | 2.90 | 7.3 | 0.42 | | wasmtime | unpacked (before) | 🔴 2.02 | 🔴 24.3 | 0.39 | | wasmtime | unpacked (this PR) | 🟢 3.38 | 🟢 6.3 | 0.44 | | V8 | single_return | 2.69 | 6.3 | 0.33 | | V8 | unpacked (before) | 🔴 1.37 | 3.7 | 🔴 2.81 | | V8 | unpacked (this PR) | 🟢 3.16 | 🟢 3.3 | 🟢 0.29 | --- .github/workflows/fuzzer.yml | 2 +- AGENTS.md | 4 +- crates/cli/fuzzer/README.md | 2 +- crates/evm2/build.rs | 22 +++----- .../src/interpreter/dispatch/table/mod.rs | 4 -- .../dispatch/table/single_return.rs | 54 ------------------- .../interpreter/dispatch/table/unpacked.rs | 44 +++++++++++++-- scripts/dump_asm.py | 2 - scripts/fuzzer_coverage.py | 2 +- 9 files changed, 50 insertions(+), 86 deletions(-) delete mode 100644 crates/evm2/src/interpreter/dispatch/table/single_return.rs diff --git a/.github/workflows/fuzzer.yml b/.github/workflows/fuzzer.yml index 26ea4cbbc..c2c2c6097 100644 --- a/.github/workflows/fuzzer.yml +++ b/.github/workflows/fuzzer.yml @@ -35,7 +35,7 @@ jobs: strategy: fail-fast: false matrix: - dispatch_backend: [tco, packed, single_return, unpacked] + dispatch_backend: [tco, packed, unpacked] steps: - name: Secure runner uses: tempoxyz/gh-actions/actions/secure-runner@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 diff --git a/AGENTS.md b/AGENTS.md index 8fd51794f..78ee5e773 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,8 +26,8 @@ cargo st # include EEST fixtures with interpreter, JIT, and AOT suites ``` Use `EVM2_DISPATCH_BACKEND` to force an interpreter dispatch backend for manual -testing. Accepted values are `auto` (default), `tco`, `packed`, `single_return`, -and `unpacked`, for example: +testing. Accepted values are `auto` (default), `tco`, `packed`, and `unpacked`, +for example: ```bash EVM2_DISPATCH_BACKEND=packed cargo nextest run -p evm2-eest --test eest --ignore-default-filter diff --git a/crates/cli/fuzzer/README.md b/crates/cli/fuzzer/README.md index 790de8482..86a40a456 100644 --- a/crates/cli/fuzzer/README.md +++ b/crates/cli/fuzzer/README.md @@ -27,7 +27,7 @@ Force an evm2 dispatch backend with `EVM2_DISPATCH_BACKEND`: EVM2_DISPATCH_BACKEND=packed cargo run -q -p evm2-cli -- fuzzer --duration 1m -j 0 ``` -Accepted backends are `auto`, `tco`, `packed`, `single_return`, and `unpacked`. +Accepted backends are `auto`, `tco`, `packed`, and `unpacked`. ## Corpus and replay diff --git a/crates/evm2/build.rs b/crates/evm2/build.rs index 0105646ed..fec262a35 100644 --- a/crates/evm2/build.rs +++ b/crates/evm2/build.rs @@ -3,9 +3,7 @@ use std::{ffi::OsString, process::Command}; fn main() { - for cfg in - ["cranelift", "dispatch_packed", "dispatch_single_return", "dispatch_unpacked", "tco"] - { + for cfg in ["cranelift", "dispatch_packed", "dispatch_unpacked", "tco"] { println!("cargo:rustc-check-cfg=cfg({cfg})"); } println!("cargo:rerun-if-changed=build.rs"); @@ -23,7 +21,6 @@ fn main() { DispatchBackend::Auto => unreachable!("auto backend must resolve to a concrete backend"), DispatchBackend::Tco => println!("cargo:rustc-cfg=tco"), DispatchBackend::Packed => println!("cargo:rustc-cfg=dispatch_packed"), - DispatchBackend::SingleReturn => println!("cargo:rustc-cfg=dispatch_single_return"), DispatchBackend::Unpacked => println!("cargo:rustc-cfg=dispatch_unpacked"), } } @@ -32,7 +29,6 @@ enum DispatchBackend { Auto, Tco, Packed, - SingleReturn, Unpacked, } @@ -46,26 +42,20 @@ impl DispatchBackend { "" | "auto" => Self::Auto, "tco" => Self::Tco, "packed" => Self::Packed, - "single_return" | "single-return" => Self::SingleReturn, "unpacked" => Self::Unpacked, _ => panic!( - "invalid EVM2_DISPATCH_BACKEND={value:?}; expected auto, tco, packed, single_return, or unpacked" + "invalid EVM2_DISPATCH_BACKEND={value:?}; expected auto, tco, packed, or unpacked" ), } } fn resolve(self, is_wasm: bool, target_pointer_width: Option, no_tco: bool) -> Self { match self { - Self::Auto => { - if is_wasm { - Self::SingleReturn - } else if target_pointer_width == Some(64) { - // `rust-preserve-none` passes arguments on the stack on 32-bit targets. - if !no_tco && rustc_is_nightly() { Self::Tco } else { Self::Packed } - } else { - Self::Unpacked - } + // `rust-preserve-none` passes arguments on the stack on 32-bit targets. + Self::Auto if !is_wasm && target_pointer_width == Some(64) => { + if !no_tco && rustc_is_nightly() { Self::Tco } else { Self::Packed } } + Self::Auto => Self::Unpacked, concrete => concrete, } } diff --git a/crates/evm2/src/interpreter/dispatch/table/mod.rs b/crates/evm2/src/interpreter/dispatch/table/mod.rs index 31970ae02..58cf1c3e1 100644 --- a/crates/evm2/src/interpreter/dispatch/table/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/table/mod.rs @@ -13,10 +13,6 @@ core::cfg_select! { mod packed; use packed as imp; } - dispatch_single_return => { - mod single_return; - use single_return as imp; - } _ => { mod unpacked; use unpacked as imp; diff --git a/crates/evm2/src/interpreter/dispatch/table/single_return.rs b/crates/evm2/src/interpreter/dispatch/table/single_return.rs deleted file mode 100644 index ada74a726..000000000 --- a/crates/evm2/src/interpreter/dispatch/table/single_return.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::{ - EvmConfig, EvmTypesHost, - interpreter::{InterpreterState, Pc, Stack, StackBacking, StackMut, gas::Gas}, -}; - -pub(super) type LoopState = (); - -pub(super) type Cursor = (Pc, usize); - -/// Single-return instruction function pointer. -pub(in crate::interpreter::dispatch) type RawInstrFn = - extern_table!(fn(pc: Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) -> Pc); - -#[inline(always)] -pub(super) fn dispatch_loop_call( - instr: RawInstrFn, - (pc, stack_len): Cursor, - stack: &mut StackBacking, - state: &mut InterpreterState<'_, '_, T>, - _loop_state: &mut LoopState, -) -> Cursor { - let mut stack = Stack::new(stack, stack_len); - let next_pc = instr(pc, stack.as_mut(), state); - (next_pc, stack.len) -} - -#[inline(always)] -pub(super) const fn loop_state(_gas: &Gas) -> LoopState {} - -#[inline(always)] -pub(super) const fn finish_loop(_gas: &mut Gas, _loop_state: LoopState) {} - -#[inline(always)] -pub(super) const fn sync_loop_state( - _state: &mut InterpreterState<'_, '_, T>, - _loop_state: LoopState, -) { -} - -extern_table! { - pub(in crate::interpreter::dispatch) fn dispatch< - T: EvmTypesHost, - C: EvmConfig, - M: super::InspectMode, - const OP: u8, - >( - pc: Pc, - stack: StackMut<'_>, - state: &mut InterpreterState<'_, '_, T>, - ) -> Pc { - let (pc, ()) = super::dispatch_inner::(pc, stack, (), state, OP); - pc - } -} diff --git a/crates/evm2/src/interpreter/dispatch/table/unpacked.rs b/crates/evm2/src/interpreter/dispatch/table/unpacked.rs index 7399fe647..b0de375a4 100644 --- a/crates/evm2/src/interpreter/dispatch/table/unpacked.rs +++ b/crates/evm2/src/interpreter/dispatch/table/unpacked.rs @@ -1,3 +1,4 @@ +use super::Cursor as _; use crate::{ EvmConfig, EvmTypesHost, interpreter::{InterpreterState, Pc, Stack, StackBacking, gas::Gas}, @@ -5,10 +6,43 @@ use crate::{ pub(super) type LoopState = (); -pub(super) type Cursor = (Pc, usize); +core::cfg_select! { + all(target_family = "wasm", target_pointer_width = "32") => { + pub(super) type Cursor = WasmCursor; + + /// Program counter in the high 32 bits and stack length in the low 32 bits. + /// + /// Rust's wasm ABI returns a `(Pc, usize)` pair through a result pointer in linear memory: + /// every handler takes an extra pointer argument and stores both values, and the dispatch + /// loop loads them back. A single `u64` is returned as one wasm `i64` result instead. + #[derive(Clone, Copy)] + #[repr(transparent)] + pub(crate) struct WasmCursor(u64); + + impl super::Cursor for WasmCursor { + #[inline(always)] + fn new(pc: Pc, stack_len: usize) -> Self { + Self(((pc.as_ptr() as usize as u64) << 32) | stack_len as u64) + } + + #[inline(always)] + fn pc(self) -> Pc { + Pc::new((self.0 >> 32) as usize as *const u8) + } + + #[inline(always)] + fn stack_len(self) -> usize { + self.0 as u32 as usize + } + } + } + _ => { + pub(super) type Cursor = (Pc, usize); + } +} /// Unpacked instruction return value. -type InstrFnRet = (Pc, usize); +type InstrFnRet = Cursor; /// Unpacked instruction function pointer. pub(in crate::interpreter::dispatch) type RawInstrFn = extern_table!( @@ -18,12 +52,12 @@ pub(in crate::interpreter::dispatch) type RawInstrFn = extern_table!( #[inline(always)] pub(super) fn dispatch_loop_call( instr: RawInstrFn, - (pc, stack_len): Cursor, + cursor: Cursor, stack: &mut StackBacking, state: &mut InterpreterState<'_, '_, T>, _loop_state: &mut LoopState, ) -> Cursor { - instr(pc, Stack::new(stack, stack_len), state) + instr(cursor.pc(), Stack::new(stack, cursor.stack_len()), state) } #[inline(always)] @@ -51,6 +85,6 @@ extern_table! { state: &mut InterpreterState<'_, '_, T>, ) -> InstrFnRet { let (pc, ()) = super::dispatch_inner::(pc, stack.as_mut(), (), state, OP); - (pc, stack.len) + Cursor::new(pc, stack.len) } } diff --git a/scripts/dump_asm.py b/scripts/dump_asm.py index a39731813..0455aaf7a 100755 --- a/scripts/dump_asm.py +++ b/scripts/dump_asm.py @@ -34,10 +34,8 @@ DEFAULT_OUT = ROOT / "tmp" / "dump" DISPATCH_SYMBOLS = ( "evm2::interpreter::dispatch::table::packed::dispatch::<", - "evm2::interpreter::dispatch::table::single_return::dispatch::<", "evm2::interpreter::dispatch::table::unpacked::dispatch::<", "evm2::interpreter::dispatch::packed::dispatch::<", - "evm2::interpreter::dispatch::single_return::dispatch::<", "evm2::interpreter::dispatch::unpacked::dispatch::<", "evm2::interpreter::dispatch::tco::dispatch::<", ) diff --git a/scripts/fuzzer_coverage.py b/scripts/fuzzer_coverage.py index 9c340efb3..b025189d6 100755 --- a/scripts/fuzzer_coverage.py +++ b/scripts/fuzzer_coverage.py @@ -24,7 +24,7 @@ ROOT = Path(repo_root()) DEFAULT_OUTPUT_DIR = ROOT / "target" / "llvm-cov" / "fuzzer-html" DEFAULT_IGNORE_REGEX = r"(^|/)crates/cli/src/fuzzer/" -BACKENDS = ("tco", "packed", "single_return", "unpacked") +BACKENDS = ("tco", "packed", "unpacked") def parse_args() -> argparse.Namespace: From c73b16364ca2125659c37684ab41ebbf47297c87 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:39:44 +0100 Subject: [PATCH 123/159] refactor: apply slapslop machine-applicable fixes (#1010) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Applies the machine-applicable fixes from a [slapslop](https://github.com/tempoxyz/slapslop) run (main at `b0aba885`) over the workspace. This is the first PR of two; the second swaps in the helpers slapslop only suggested. It swaps hand-written code for helpers that already exist: alloy's `U256::ONE` and `ZERO`, `saturating_to`, `wrapping_to`, `is_zero`, `B256` and `U256` conversions, `Address::repeat_byte`, `B256::with_last_byte`, `bytes!` and `hex::encode_prefixed`, eyre's `ensure!` and `ok_or_eyre`, and local helpers whose bodies the code repeated (`set_pc_stack_len`, `as_word_mut_ptr`). I left out the `default_struct_update_in_prod` fixes, which write out every field in place of `..Default::default()` and add noise to config and trace structs. I also left out the swaps in the CLI's comparison backends, which call that backend's own helpers, the `InstrStop::Stop` result in the empty-code path of `Evm::run` that slapslop pointed at `error_message_result` (the helper's name says error, the site is a success), and the transfer-log hunks that #1005 already changes. The swaps of `keccak256([])` to `KECCAK256_EMPTY` and of `10.pow(18)` to `ETH_TO_WEI` are out too, because they replace one constant with another of the same value. No public item is added, removed or renamed, and no public signature changes. Retired instructions per call (`perf stat -e instructions:u`, difference between two iteration counts, mean of 5 alternating runs, `profiling` profile with `RUSTFLAGS=-Csymbol-mangling-version=v0` to match the CI build instead of the local native `target-cpu`, AMD EPYC 4585PX; spread of the slower side in parentheses). Changes under 1% are code layout, not a performance claim. Main against this PR: | Benchmark | Interpreter | JIT | | --- | ---: | ---: | | `snailtracer` | 476,245,449 → 474,947,909 ⚪ -0.3% (±0.4%) | 193,798,365 → 193,589,440 ⚪ -0.1% (±0.4%) | | `hash_10k` | 17,033,172 → 17,032,810 ⚪ -0.0% (±0.0%) | 9,071,902 → 9,071,774 ⚪ -0.0% (±0.0%) | | `erc20_transfer` | 26,558,606 → 26,335,385 🟢 -0.8% (±0.0%) | 17,127,601 → 17,125,290 ⚪ -0.0% (±0.0%) | | `burntpix` | 384,565,945 → 384,326,275 ⚪ -0.1% (±0.0%) | 99,058,418 → 99,117,797 ⚪ +0.1% (±0.2%) | | `curve_stableswap` | 637,981 → 637,214 ⚪ -0.1% (±0.1%) | 247,704 → 247,746 ⚪ +0.0% (±0.2%) | | `aggregation_router_v6` | 785,430 → 778,396 ⚪ -0.9% (±0.5%) | 432,165 → 427,242 ⚪ -1.1% (±1.5%) | | `entry_point_v0_7` | 1,593,148 → 1,597,598 ⚪ +0.3% (±0.4%) | 1,371,187 → 1,368,777 ⚪ -0.2% (±0.6%) | --- crates/cli/benches/evm/fixture.rs | 2 +- crates/cli/benches/evm/jit.rs | 4 +- crates/cli/src/capture/builder.rs | 2 +- crates/cli/src/capture/export.rs | 2 +- crates/cli/src/fuzzer/case.rs | 23 ++-- crates/cli/src/fuzzer/program.rs | 2 +- crates/cli/src/run.rs | 13 +- crates/eest/src/blockchaintest/execute.rs | 18 +-- crates/eest/src/dump.rs | 4 +- crates/eest/src/execute.rs | 52 +++----- crates/evm2/examples/custom_evm/main.rs | 8 +- .../src/block/bundle_state/bundle_state.rs | 26 ++-- crates/evm2/src/block/ethereum/execution.rs | 2 +- crates/evm2/src/block/ethereum/tests.rs | 24 ++-- crates/evm2/src/block/state.rs | 4 +- crates/evm2/src/bytecode/analysis.rs | 2 +- crates/evm2/src/bytecode/mod.rs | 10 +- crates/evm2/src/bytecode/serde_impl.rs | 6 +- crates/evm2/src/ethereum/eip4844.rs | 4 +- crates/evm2/src/ethereum/lazy_eip7702.rs | 9 +- crates/evm2/src/ethereum/mod.rs | 6 +- crates/evm2/src/evm/async.rs | 21 ++- crates/evm2/src/evm/bal/bal_context.rs | 4 +- crates/evm2/src/evm/bal/changes.rs | 12 +- crates/evm2/src/evm/bal/list.rs | 6 +- crates/evm2/src/evm/db/mod.rs | 4 +- crates/evm2/src/evm/inspector.rs | 78 +++++------ crates/evm2/src/evm/mod.rs | 121 +++++++++--------- crates/evm2/src/evm/state/account.rs | 18 +-- crates/evm2/src/evm/state/block.rs | 48 +++---- crates/evm2/src/evm/state/journal.rs | 22 ++-- crates/evm2/src/evm/state/mod.rs | 80 ++++++------ crates/evm2/src/evm/state/pending.rs | 2 +- crates/evm2/src/evm/state/storage.rs | 52 ++++---- crates/evm2/src/evm/system.rs | 24 ++-- .../interpreter/instructions/arithmetic.rs | 2 +- .../src/interpreter/instructions/bitwise.rs | 2 +- .../src/interpreter/instructions/block.rs | 12 +- .../src/interpreter/instructions/control.rs | 10 +- .../src/interpreter/instructions/crypto.rs | 6 +- .../evm2/src/interpreter/instructions/env.rs | 54 ++++---- .../evm2/src/interpreter/instructions/host.rs | 43 +++---- .../src/interpreter/instructions/memory.rs | 10 +- .../src/interpreter/instructions/stack.rs | 12 +- .../src/interpreter/instructions/system.rs | 116 ++++++++--------- crates/evm2/src/interpreter/runtime.rs | 6 +- crates/evm2/src/interpreter/stack.rs | 34 ++--- .../evm2/src/precompiles/bls12_381/g1_msm.rs | 32 ++--- .../evm2/src/precompiles/bls12_381/g2_msm.rs | 26 ++-- .../src/precompiles/bls12_381/map_fp_to_g1.rs | 6 +- crates/evm2/src/precompiles/bn254/mod.rs | 5 +- crates/evm2/src/precompiles/hash.rs | 4 +- crates/evm2/src/precompiles/modexp.rs | 36 +++--- crates/evm2/src/precompiles/secp256k1/mod.rs | 2 +- crates/evm2/src/tests.rs | 39 +++--- crates/evm2/src/utils.rs | 10 +- crates/evm2/tests/eip7702_recovery_crypto.rs | 2 +- crates/inspectors/src/access_list.rs | 5 +- crates/inspectors/src/storage.rs | 2 +- crates/inspectors/src/tracing/builder/geth.rs | 4 +- .../src/tracing/builder/geth/steps_tests.rs | 4 +- crates/inspectors/src/tracing/erc7562.rs | 2 +- crates/inspectors/src/tracing/fourbyte.rs | 4 +- crates/inspectors/src/tracing/js/bindings.rs | 16 +-- crates/inspectors/src/tracing/js/builtins.rs | 12 +- crates/inspectors/src/tracing/js/mod.rs | 12 +- crates/inspectors/src/tracing/keccak.rs | 2 +- crates/inspectors/src/transfer.rs | 4 +- crates/inspectors/tests/it/accesslist.rs | 87 +++---------- crates/inspectors/tests/it/geth.rs | 92 ++++++------- .../inspectors/tests/it/geth/consistency.rs | 3 +- .../inspectors/tests/it/geth/runtime_oog.rs | 5 +- crates/inspectors/tests/it/geth_js.rs | 18 +-- crates/inspectors/tests/it/inputs.rs | 6 +- crates/inspectors/tests/it/opcode.rs | 6 +- crates/inspectors/tests/it/otterscan.rs | 27 ++-- crates/inspectors/tests/it/parity.rs | 43 +++---- .../tests/it/parity/state_diff_7702.rs | 2 +- .../it/parity/state_diff_selfdestruct.rs | 27 ++-- .../inspectors/tests/it/parity/vm_bytecode.rs | 14 +- crates/inspectors/tests/it/repro/prestate.rs | 6 +- crates/inspectors/tests/it/stack.rs | 12 +- .../inspectors/tests/it/test_native_bigint.rs | 6 +- crates/inspectors/tests/it/transfer.rs | 37 ++---- crates/inspectors/tests/it/writer.rs | 4 +- crates/jit/builtins/src/lib.rs | 42 +++--- crates/jit/builtins/src/utils.rs | 4 +- crates/jit/codegen/src/bytecode/asm.rs | 13 +- .../src/bytecode/passes/block_analysis.rs | 20 +-- .../codegen/src/bytecode/passes/const_fold.rs | 55 ++++---- crates/jit/codegen/src/tests/fibonacci.rs | 4 +- crates/jit/codegen/src/tests/mod.rs | 66 +++++----- crates/jit/codegen/src/tests/runner.rs | 26 ++-- crates/jit/llvm/src/lib.rs | 4 +- crates/jit/runtime/src/evm2_evm.rs | 32 ++--- crates/jit/runtime/src/runtime/backend.rs | 2 +- crates/jit/runtime/src/runtime/mod.rs | 18 +-- .../jit/runtime/src/runtime/out_of_process.rs | 12 +- crates/jit/runtime/src/runtime/tests.rs | 35 +++-- 99 files changed, 908 insertions(+), 1071 deletions(-) diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index de9d1956e..fc8bf7167 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -179,7 +179,7 @@ impl Case<'_> { } fn b256_to_u256(value: B256) -> U256 { - U256::from_be_bytes(value.0) + value.into() } #[cfg(feature = "jit")] diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index c737dab00..4e898b3d4 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -1,5 +1,5 @@ use crate::fixture::Suites; -use alloy_primitives::{B256, hex, map::B256Map}; +use alloy_primitives::{B256, map::B256Map}; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::{ BaseEvmTypes, Evm, InterpreterRunner, Precompiles, SpecId, @@ -84,7 +84,7 @@ impl PreparedBench { let mut functions = B256Map::default(); for account in accounts { let func = *compiler.functions.entry((account.code_hash, spec)).or_insert_with(|| { - let name = format!("contract_{}_{spec:?}", hex::encode(account.code_hash)); + let name = format!("contract_{:x}_{spec:?}", account.code_hash); let func = unsafe { compiler.compiler.jit(&name, account.bytecode.as_ref(), spec).unwrap_or_else( |err| panic!("{} benchmark JIT compilation failed: {err:?}", bench.name), diff --git a/crates/cli/src/capture/builder.rs b/crates/cli/src/capture/builder.rs index 3d0f4bde7..bf5946aa2 100644 --- a/crates/cli/src/capture/builder.rs +++ b/crates/cli/src/capture/builder.rs @@ -201,7 +201,7 @@ impl CaptureBuilder { withdrawal_balance, }, )?; - if balance.is_zero() && withdrawal_balance != U256::ZERO { + if balance.is_zero() && !withdrawal_balance.is_zero() { return Ok(()); } let state = self.account_state(address); diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index 0487af8ac..56d06e90e 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -173,7 +173,7 @@ fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { fn parent_header(first: &Block) -> BlockHeader { let mut header = first.block_header.clone().expect("exported blocks include headers"); header.hash = header.parent_hash; - header.number = header.number.saturating_sub(U256::from(1)); + header.number = header.number.saturating_sub(U256::ONE); header.gas_used = U256::ZERO; header.transactions_trie = B256::ZERO; header.receipt_trie = B256::ZERO; diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 511a96d88..38ddfcad0 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -27,12 +27,11 @@ use secp256k1::{Message, SecretKey, ecdsa::RecoverableSignature}; use serde::{Deserialize, Serialize}; use std::{fmt, sync::OnceLock}; -pub(crate) const CALLER: Address = Address::new([0x10; 20]); -pub(crate) const TARGET: Address = Address::new([0x20; 20]); -pub(crate) const BENEFICIARY: Address = Address::new([0x30; 20]); +pub(crate) const CALLER: Address = Address::repeat_byte(0x10); +pub(crate) const TARGET: Address = Address::repeat_byte(0x20); +pub(crate) const BENEFICIARY: Address = Address::repeat_byte(0x30); const CALLER_BALANCE: U256 = U256::from_limbs([0, 0, 1, 0]); -const EIP7702_DELEGATED_TARGET: Address = - Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6]); +const EIP7702_DELEGATED_TARGET: Address = Address::with_last_byte(6); #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] pub(crate) struct EvmCase { @@ -69,7 +68,7 @@ impl Default for CaseGenerator { fixed_eip7702_authority(), EIP7702_DELEGATED_TARGET, Address::ZERO, - Address::new([0xff; 20]), + Address::repeat_byte(0xff), ]; address_pool.extend((1..=10).map(Address::with_last_byte)); let mut call_pool = @@ -359,10 +358,10 @@ fn generate_access_list(rng: &mut Gen, accounts: &[CaseAccount], list: &mut Acce item.address = account.address; item.storage_keys.clear(); for key in account.storage.keys().take(rng.range_inclusive(0, 3)) { - item.storage_keys.push(B256::from(key.to_be_bytes::<32>())); + item.storage_keys.push(B256::from(*key)); } if item.storage_keys.is_empty() && rng.one_in(2) { - item.storage_keys.push(B256::from(rng.biased_word().to_be_bytes::<32>())); + item.storage_keys.push(B256::from(rng.biased_word())); } } } @@ -391,12 +390,12 @@ fn generate_eip7702_authorization(rng: &mut Gen) -> SignedAuthorization { nonce: 1, }), 3 => signed_eip7702_auth(Authorization { - chain_id: U256::from(1), + chain_id: U256::ONE, address: rng.pick(&[EIP7702_DELEGATED_TARGET, TARGET, Address::with_last_byte(8)]), nonce: 1, }), 4 => signed_eip7702_auth(Authorization { - chain_id: rng.pick(&[U256::from(1), U256::from(2)]), + chain_id: rng.pick(&[U256::ONE, U256::from(2)]), address: EIP7702_DELEGATED_TARGET, nonce: rng.pick(&[0, 2, u64::MAX]), }), @@ -431,7 +430,7 @@ fn add_eip7702_accounts<'a>( if auth.y_parity() > 1 { features.insert(FuzzFeatures::EIP7702_AUTH_BAD_SIGNATURE); } - if auth.chain_id() != &U256::ZERO && auth.chain_id() != &U256::from(1) { + if auth.chain_id() != &U256::ZERO && auth.chain_id() != &U256::ONE { features.insert(FuzzFeatures::EIP7702_AUTH_BAD_CHAIN); } if auth.nonce() != 1 { @@ -514,7 +513,7 @@ pub(crate) fn fixed_eip7702_auth() -> SignedAuthorization { static AUTH: OnceLock = OnceLock::new(); AUTH.get_or_init(|| { signed_eip7702_auth(Authorization { - chain_id: U256::from(1), + chain_id: U256::ONE, address: EIP7702_DELEGATED_TARGET, nonce: 1, }) diff --git a/crates/cli/src/fuzzer/program.rs b/crates/cli/src/fuzzer/program.rs index 823233075..d5a21e0b3 100644 --- a/crates/cli/src/fuzzer/program.rs +++ b/crates/cli/src/fuzzer/program.rs @@ -614,7 +614,7 @@ impl Program { } fn push_address(&mut self, address: Address) { - self.push_word(U256::from_be_slice(address.as_slice())); + self.push_word(address.into_word().into()); } fn push_bytes_word(&mut self, bytes: &[u8]) { diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 1abb9ef4f..34071ef52 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -14,7 +14,7 @@ use evm2_eest::{StateTestPost, StateTestSuite, StateTestUnit}; use evm2_jit_context::{EvmCompilerFn, can_run_compiled}; use evm2_jit_llvm::EvmLlvmBackend; use evm2_jit_runtime::{ - EvmCompiler, Linker, OptimizationLevel, eyre, parse_asm, shared_library_path, + EvmCompiler, Linker, OptimizationLevel, eyre, eyre::OptionExt, parse_asm, shared_library_path, }; use std::{ borrow::Cow, @@ -24,8 +24,8 @@ use std::{ sync::Arc, }; -const BENCH_CALLER: Address = Address::new([0x11; 20]); -const BENCH_TARGET: Address = Address::new([0xcc; 20]); +const BENCH_CALLER: Address = Address::repeat_byte(0x11); +const BENCH_TARGET: Address = Address::repeat_byte(0xcc); #[derive(Debug, clap::Args)] pub(crate) struct RunArgs { @@ -535,7 +535,7 @@ impl PreparedBench { if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { continue; } - let name = format!("contract_{}", hex::encode(account.code_hash)); + let name = format!("contract_{:x}", account.code_hash); let func = unsafe { compiler.jit(&name, account.bytecode.original_byte_slice(), bench.spec_id)? }; @@ -761,7 +761,7 @@ fn parse_block(env: &evm2_eest::StateTestEnv) -> BlockEnv { gas_limit: env.current_gas_limit, basefee: env.current_base_fee.unwrap_or_default(), difficulty: env.current_difficulty, - prevrandao: env.current_random.map_or(U256::ZERO, |value| U256::from_be_bytes(value.0)), + prevrandao: env.current_random.map_or(U256::ZERO, |value| value.into()), slot_num: env.slot_number.unwrap_or_default(), ..BlockEnvExt::default() } @@ -794,8 +794,7 @@ fn build_fixture_tx( .value .get(indexes.value) .ok_or_else(|| eyre::eyre!("fixture value index {} does not exist", indexes.value))?; - let caller = - raw.sender.ok_or_else(|| eyre::eyre!("benchmark transaction sender is required"))?; + let caller = raw.sender.ok_or_eyre("benchmark transaction sender is required")?; let tx = TxLegacy { chain_id: None, nonce: u64_value(raw.nonce)?, diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 3754059e0..e747123bf 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -1081,11 +1081,7 @@ fn block_env_from_header( gas_limit: header.gas_limit, basefee: header.base_fee_per_gas.unwrap_or_default(), difficulty: header.difficulty, - prevrandao: if header.difficulty.is_zero() { - U256::from_be_slice(header.mix_hash.as_slice()) - } else { - U256::ZERO - }, + prevrandao: if header.difficulty.is_zero() { header.mix_hash.into() } else { U256::ZERO }, blob_basefee, slot_num: header.slot_number.unwrap_or_default(), ext: (), @@ -1195,9 +1191,9 @@ fn validate_post_state( .unwrap_or_default(); if actual_code != expected_account.code.as_ref() { return Err(TestErrorKind::UnexpectedFailure(format!( - "code mismatch for {address}: got 0x{}, expected 0x{}", - alloy_primitives::hex::encode(actual_code), - alloy_primitives::hex::encode(&expected_account.code) + "code mismatch for {address}: got {}, expected {}", + alloy_primitives::hex::encode_prefixed(actual_code), + alloy_primitives::hex::encode_prefixed(&expected_account.code) ))); } @@ -2194,7 +2190,7 @@ mod tests { fn execute_simple_storage_block(mode: ExecutionMode) -> ExecuteSummary { let secret = B256::with_last_byte(1); let caller = recover_address(secret.as_slice()).unwrap(); - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let unsigned = TxLegacy { chain_id: Some(1), nonce: 0, @@ -2220,7 +2216,7 @@ mod tests { target, Account { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_STORE42), + code: Bytes::from_static(BYTECODE_STORE42), nonce: U256::ZERO, storage: BTreeMap::new(), }, @@ -2242,7 +2238,7 @@ mod tests { target, Account { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_STORE42), + code: Bytes::from_static(BYTECODE_STORE42), nonce: U256::ZERO, storage: target_storage, }, diff --git a/crates/eest/src/dump.rs b/crates/eest/src/dump.rs index ece4eb4b4..16a04c6d9 100644 --- a/crates/eest/src/dump.rs +++ b/crates/eest/src/dump.rs @@ -67,7 +67,7 @@ mod tests { #[test] fn dumps_accounts_and_non_zero_storage() { let mut db = InMemoryDB::default(); - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut info = AccountInfo::default().with_code(parse_bytecode(Bytes::new())); info.balance = U256::from(1_000u64); info.nonce = 7; @@ -75,7 +75,7 @@ mod tests { &mut db, address, info, - [(U256::from(1u64), U256::from(42u64)), (U256::from(2u64), U256::ZERO)], + [(U256::ONE, U256::from(42u64)), (U256::from(2u64), U256::ZERO)], ); let mut buf = Vec::new(); diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index dc0d3b845..332d8921f 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -483,22 +483,12 @@ fn pre_block_system_calls( if spec.enables(SpecId::CANCUN) && let Some(beacon_root) = env.current_beacon_root { - commit_system_call( - evm, - post, - BEACON_ROOTS_ADDRESS, - Bytes::copy_from_slice(beacon_root.as_slice()), - ); + commit_system_call(evm, post, BEACON_ROOTS_ADDRESS, Bytes::from(beacon_root)); } if spec.enables(SpecId::PRAGUE) && let Some(previous_hash) = env.previous_hash { - commit_system_call( - evm, - post, - HISTORY_STORAGE_ADDRESS, - Bytes::copy_from_slice(previous_hash.as_slice()), - ); + commit_system_call(evm, post, HISTORY_STORAGE_ADDRESS, Bytes::from(previous_hash)); } } @@ -561,9 +551,7 @@ fn parse_block(env: &Env, blob_params: Option<&BlobParams>) -> BlockEnv { gas_limit: env.current_gas_limit, basefee: env.current_base_fee.unwrap_or_default(), difficulty: env.current_difficulty, - prevrandao: env - .current_random - .map_or(U256::ZERO, |random| U256::from_be_slice(random.as_slice())), + prevrandao: env.current_random.map_or(U256::ZERO, |random| random.into()), blob_basefee: env .current_excess_blob_gas .zip(blob_params) @@ -718,7 +706,7 @@ mod tests { #[test] fn logs_hash_hashes_logs() { let log = Log { - address: Address::from([0x22; 20]), + address: Address::repeat_byte(0x22), data: LogData::new_unchecked(vec![B256::with_last_byte(1)], Bytes::from_static(&[2])), }; @@ -727,13 +715,13 @@ mod tests { #[test] fn build_tx_builds_legacy_transaction() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let raw = TransactionParts { data: vec![Bytes::from_static(&[0xaa])], gas_limit: vec![U256::from(21_000)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::from(3)], ..TransactionParts::default() }; @@ -749,15 +737,15 @@ mod tests { #[test] fn build_tx_builds_eip2930_transaction() { - let caller = Address::from([0x11; 20]); - let access_address = Address::from([0x33; 20]); + let caller = Address::repeat_byte(0x11); + let access_address = Address::repeat_byte(0x33); let raw = TransactionParts { tx_type: Some(1), data: vec![Bytes::new()], gas_limit: vec![U256::from(25_300)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::ZERO], access_lists: vec![Some(vec![crate::tx::AccessListItem { address: access_address, @@ -777,16 +765,16 @@ mod tests { #[test] fn build_tx_uses_indexed_access_list() { - let caller = Address::from([0x11; 20]); - let first_address = Address::from([0x33; 20]); - let second_address = Address::from([0x44; 20]); + let caller = Address::repeat_byte(0x11); + let first_address = Address::repeat_byte(0x33); + let second_address = Address::repeat_byte(0x44); let raw = TransactionParts { tx_type: Some(1), data: vec![Bytes::new(), Bytes::from_static(&[0xaa])], gas_limit: vec![U256::from(25_300)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::ZERO], access_lists: vec![ Some(vec![crate::tx::AccessListItem { @@ -811,8 +799,8 @@ mod tests { #[cfg(feature = "jit")] fn execute_simple_call(mode: ExecutionMode) -> SpecOutcome { - let caller = Address::from([0x11; 20]); - let target = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x22); let env = Env { current_chain_id: Some(U256::ONE), current_coinbase: Address::ZERO, @@ -842,7 +830,7 @@ mod tests { target, AccountInfo { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), nonce: 0, storage: BTreeMap::new(), }, @@ -878,9 +866,9 @@ mod tests { #[cfg(feature = "jit")] fn execute_amsterdam_target(mode: ExecutionMode, target_code: Bytes) -> SpecOutcome { - let caller = Address::from([0x11; 20]); - let target = Address::from([0x22; 20]); - let coinbase = Address::from([0xcb; 20]); + let caller = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x22); + let coinbase = Address::repeat_byte(0xcb); let env = Env { current_chain_id: Some(U256::ONE), current_coinbase: coinbase, @@ -946,7 +934,7 @@ mod tests { #[cfg(feature = "jit")] fn execute_amsterdam_value_call_to_empty_coinbase(mode: ExecutionMode) -> SpecOutcome { - execute_amsterdam_target(mode, call_value_to(Address::from([0xcb; 20]))) + execute_amsterdam_target(mode, call_value_to(Address::repeat_byte(0xcb))) } #[cfg(feature = "jit")] diff --git a/crates/evm2/examples/custom_evm/main.rs b/crates/evm2/examples/custom_evm/main.rs index eb025a9f2..8d7db4a0f 100644 --- a/crates/evm2/examples/custom_evm/main.rs +++ b/crates/evm2/examples/custom_evm/main.rs @@ -68,7 +68,7 @@ fn custom_opcode() -> HandlerResult<()> { } fn state_change_consumer() -> HandlerResult<()> { - let target = Address::from([0xcc; 20]); + let target = Address::repeat_byte(0xcc); let mut database = InMemoryDB::default(); database.insert_account_info(&target, AccountInfo::default().with_nonce(1)); let mut evm = custom_evm_with_database(database); @@ -99,7 +99,7 @@ fn state_change_consumer() -> HandlerResult<()> { assert_eq!(consumer.storage_wipes, 0); assert_eq!(consumer.storage_changes.len(), 1); assert_eq!(storage_change.address, target); - assert_eq!(storage_change.key, U256::from(1)); + assert_eq!(storage_change.key, U256::ONE); assert_eq!(storage_change.original, U256::ZERO); assert_eq!(storage_change.current, U256::from(0xdead_u64)); Ok(()) @@ -118,7 +118,7 @@ fn l1_blocknumber_opcode() -> HandlerResult<()> { ])); let result = evm.transact(&tx)?.discard(); - let expected = Bytes::copy_from_slice(&U256::from(CUSTOM_L1_BLOCK_NUMBER).to_be_bytes::<32>()); + let expected = Bytes::from(U256::from(CUSTOM_L1_BLOCK_NUMBER).to_be_bytes::<32>()); println!( "l1 blocknumber opcode: expected status=true stop=Return output={expected:?}; got status={} stop={:?} output={:?}", @@ -231,7 +231,7 @@ fn custom_execution_config() -> ExecutionConfig { fn custom_opcode_tx(code: Bytes) -> Recovered { Recovered::new_unchecked( CustomEnvelope::ExecuteCode(ExecuteCodeTx { - target: Address::from([0xcc; 20]), + target: Address::repeat_byte(0xcc), gas_limit: 100_000, code, }), diff --git a/crates/evm2/src/block/bundle_state/bundle_state.rs b/crates/evm2/src/block/bundle_state/bundle_state.rs index 8a459d83d..3a043d401 100644 --- a/crates/evm2/src/block/bundle_state/bundle_state.rs +++ b/crates/evm2/src/block/bundle_state/bundle_state.rs @@ -862,7 +862,7 @@ mod tests { #[test] fn transition_states() { // Dummy data - let address = Address::new([0x01; 20]); + let address = Address::repeat_byte(0x01); let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, ..Default::default() }; let mut bundle_state = BundleState::default(); @@ -882,11 +882,11 @@ mod tests { } const fn account1() -> Address { - Address::new([0x60; 20]) + Address::repeat_byte(0x60) } const fn account2() -> Address { - Address::new([0x61; 20]) + Address::repeat_byte(0x61) } fn slot1() -> U256 { @@ -907,8 +907,8 @@ mod tests { None, Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }), HashMap::from_iter([ - (slot1(), (U256::from(0), U256::from(10))), - (slot2(), (U256::from(0), U256::from(15))), + (slot1(), (U256::ZERO, U256::from(10))), + (slot2(), (U256::ZERO, U256::from(15))), ]), ), ( @@ -919,7 +919,7 @@ mod tests { ), ], vec![vec![ - (account1(), Some(None), vec![(slot1(), U256::from(0)), (slot2(), U256::from(0))]), + (account1(), Some(None), vec![(slot1(), U256::ZERO), (slot2(), U256::ZERO)]), (account2(), Some(None), vec![]), ]], vec![], @@ -934,7 +934,7 @@ mod tests { account1(), None, Some(AccountInfo { nonce: 3, balance: U256::from(20), ..Default::default() }), - HashMap::from_iter([(slot1(), (U256::from(0), U256::from(15)))]), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(15)))]), )], vec![vec![( account1(), @@ -954,7 +954,7 @@ mod tests { ) .state_storage( account1(), - HashMap::from_iter([(slot1(), (U256::from(0), U256::from(10)))]), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(10)))]), ) .state_address(account2()) .state_present_account_info( @@ -963,7 +963,7 @@ mod tests { ) .revert_address(0, account1()) .revert_account_info(0, account1(), Some(None)) - .revert_storage(0, account1(), vec![(slot1(), U256::from(0))]) + .revert_storage(0, account1(), vec![(slot1(), U256::ZERO)]) .revert_account_info(0, account2(), Some(None)) .build() } @@ -977,7 +977,7 @@ mod tests { ) .state_storage( account1(), - HashMap::from_iter([(slot1(), (U256::from(0), U256::from(15)))]), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(15)))]), ) .revert_address(0, account1()) .revert_account_info( @@ -993,7 +993,7 @@ mod tests { /// to the status accumulated over the earlier blocks. #[test] fn extend_reverts_to_accumulated_status() { - let address = Address::new([0x70; 20]); + let address = Address::repeat_byte(0x70); let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; let destroy = TransitionAccount { info: None, @@ -1054,7 +1054,7 @@ mod tests { /// blocks, not only the first one. #[test] fn extend_reverts_later_blocks_to_accumulated_status() { - let address = Address::new([0x71; 20]); + let address = Address::repeat_byte(0x71); let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; let changed = AccountInfo { balance: U256::from(11), ..info.clone() }; let transition = |previous: &AccountInfo, next: &AccountInfo, from, to| TransitionAccount { @@ -1378,7 +1378,7 @@ mod tests { // Test get_revert_storage_mut assert!(builder.get_revert_storage_mut().is_empty()); - builder.get_revert_storage_mut().insert((0, account1()), vec![(slot1(), U256::from(0))]); + builder.get_revert_storage_mut().insert((0, account1()), vec![(slot1(), U256::ZERO)]); assert!(builder.get_revert_storage_mut().contains_key(&(0, account1()))); // Test get_contracts_mut diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs index 22fc395aa..52e830aa0 100644 --- a/crates/evm2/src/block/ethereum/execution.rs +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -277,7 +277,7 @@ pub fn pre_execution_system_call_state_changes( .ok_or(BlockValidationError::MissingParentBeaconBlockRoot)?; if block_number == 0 { - if parent_beacon_block_root != B256::ZERO { + if !parent_beacon_block_root.is_zero() { return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero { parent_beacon_block_root, } diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs index 59830c1f9..112105eb2 100644 --- a/crates/evm2/src/block/ethereum/tests.rs +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -12,7 +12,9 @@ use crate::{ utils::b256_to_word, }; use alloc::vec; -use alloy_consensus::{Signed, TxLegacy, constants::ETH_TO_WEI, transaction::Recovered}; +use alloy_consensus::{ + SignableTransaction, TxLegacy, constants::ETH_TO_WEI, transaction::Recovered, +}; use alloy_eips::{ eip2935::HISTORY_STORAGE_CODE, eip4788::BEACON_ROOTS_CODE, eip7002::WITHDRAWAL_REQUEST_PREDEPLOY_CODE, eip7251::CONSOLIDATION_REQUEST_PREDEPLOY_CODE, @@ -47,7 +49,7 @@ fn evm(spec: SpecId, gas_limit: u64) -> Evm<'static, BaseEvmTypes> { fn evm_with(spec: SpecId, gas_limit: u64, database: InMemoryDB) -> Evm<'static, BaseEvmTypes> { let block = BlockEnvExt { - number: U256::from(1), + number: U256::ONE, timestamp: U256::from(12), beneficiary: BENEFICIARY, gas_limit: U256::from(gas_limit), @@ -80,14 +82,11 @@ fn transfer(gas_limit: u64) -> Recovered> { gas_price: 1, gas_limit, to: TxKind::Call(RECIPIENT), - value: U256::from(1), + value: U256::ONE, ..TxLegacy::default() }; - let signature = Signature::new(U256::from(1), U256::from(1), false); - Recovered::new_unchecked( - EthereumTxEnvelope::Legacy(Signed::new_unhashed(tx, signature)), - SENDER, - ) + let signature = Signature::new(U256::ONE, U256::ONE, false); + Recovered::new_unchecked(EthereumTxEnvelope::Legacy(tx.into_signed(signature)), SENDER) } fn blob_transfer() -> Recovered> { @@ -97,11 +96,8 @@ fn blob_transfer() -> Recovered> { blob_versioned_hashes: vec![B256::ZERO], ..TxEip4844::default() }; - let signature = Signature::new(U256::from(1), U256::from(1), false); - Recovered::new_unchecked( - EthereumTxEnvelope::Eip4844(Signed::new_unhashed(tx, signature)), - SENDER, - ) + let signature = Signature::new(U256::ONE, U256::ONE, false); + Recovered::new_unchecked(EthereumTxEnvelope::Eip4844(tx.into_signed(signature)), SENDER) } fn balance(state: &BundleState, address: Address) -> Option { @@ -127,7 +123,7 @@ fn executes_prague_block() { assert!(output.result.requests.is_empty()); let state = &output.state; - assert_eq!(balance(state, RECIPIENT), Some(U256::from(1))); + assert_eq!(balance(state, RECIPIENT), Some(U256::ONE)); // EIP-2935 stores the parent hash at slot `(number - 1) % 8191`. let parent_hash = b256_to_word(PARENT_HASH); assert_eq!(storage(state, HISTORY_STORAGE_ADDRESS, U256::ZERO), Some(parent_hash)); diff --git a/crates/evm2/src/block/state.rs b/crates/evm2/src/block/state.rs index 786255e41..85f57d56f 100644 --- a/crates/evm2/src/block/state.rs +++ b/crates/evm2/src/block/state.rs @@ -489,7 +489,7 @@ mod tests { #[test] fn created_account_keeps_its_storage_wipe() { let address = Address::with_last_byte(1); - let info = AccountInfo { balance: U256::from(1), ..Default::default() }; + let info = AccountInfo { balance: U256::ONE, ..Default::default() }; let storage = overlay(true, &[]); let mut block = BlockState::new(); block.commit_account_changes( @@ -545,7 +545,7 @@ mod tests { let info = AccountInfo { nonce: 1, ..Default::default() }; let mut block = BlockState::new(); for (wiped, slots) in [ - (false, &[(U256::from(3), U256::ZERO, U256::from(1))][..]), + (false, &[(U256::from(3), U256::ZERO, U256::ONE)][..]), (true, &[(U256::from(4), U256::ZERO, U256::from(2))][..]), ] { let storage = overlay(wiped, slots); diff --git a/crates/evm2/src/bytecode/analysis.rs b/crates/evm2/src/bytecode/analysis.rs index fa8c236f3..adea0263f 100644 --- a/crates/evm2/src/bytecode/analysis.rs +++ b/crates/evm2/src/bytecode/analysis.rs @@ -463,7 +463,7 @@ mod tests { #[test] fn padding_releases_shared_input() { - let raw = Bytes::copy_from_slice(&[op::PUSH32]); + let raw = Bytes::from([op::PUSH32]); let padded = pad_legacy(raw.clone()); assert!(raw.is_unique()); assert_ne!(padded.as_ptr(), raw.as_ptr()); diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index d87d23ce0..dec3b6f7e 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -505,13 +505,13 @@ mod tests { raw[..2].copy_from_slice(EIP7702_MAGIC_BYTES); raw[2] = 1; assert_eq!( - Bytecode::new_eip7702_raw(Bytes::copy_from_slice(&raw)), + Bytecode::new_eip7702_raw(Bytes::from(raw)), Err(Eip7702DecodeError::UnsupportedVersion) ); raw[2] = EIP7702_VERSION; raw[3..7].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]); - let raw = Bytes::copy_from_slice(&raw); + let raw = Bytes::from(raw); let bytecode = Bytecode::new_eip7702_raw(raw.clone()).unwrap(); assert!(bytecode.is_eip7702()); assert_eq!(bytecode.eip7702_address(), Some(Address::from_slice(&raw[3..]))); @@ -520,7 +520,7 @@ mod tests { #[test] fn eip7702_from_address() { - let address = Address::new([0x01; 20]); + let address = Address::repeat_byte(0x01); let bytecode = Bytecode::new_eip7702(address); assert_eq!(bytecode.eip7702_address(), Some(address)); assert_eq!(bytecode.original_bytes().len(), EIP7702_BYTECODE_LEN); @@ -529,7 +529,7 @@ mod tests { #[test] fn from_stored_accepts_non_designator_magic_as_legacy() { - let address = Address::new([0x01; 20]); + let address = Address::repeat_byte(0x01); let designator = Bytecode::new_eip7702(address).original_bytes(); let from_designator = Bytecode::from_stored(designator.clone()); assert_eq!(from_designator.kind(), BytecodeKind::Eip7702); @@ -645,7 +645,7 @@ mod tests { fn existing_zero_suffix_is_original_code() { let mut raw = [0; 34]; raw[0] = op::PUSH32; - let raw = Bytes::copy_from_slice(&raw); + let raw = Bytes::from(raw); let bytecode = Bytecode::new_legacy(raw.clone()); assert_eq!(bytecode.bytes().as_ptr(), raw.as_ptr()); assert_eq!(bytecode.len(), raw.len()); diff --git a/crates/evm2/src/bytecode/serde_impl.rs b/crates/evm2/src/bytecode/serde_impl.rs index 2cda097f4..7dc23f2eb 100644 --- a/crates/evm2/src/bytecode/serde_impl.rs +++ b/crates/evm2/src/bytecode/serde_impl.rs @@ -90,7 +90,7 @@ fn from_old(old: BytecodeSerdeOld) -> Result { mod tests { use super::*; use crate::bytecode::BytecodeKind; - use alloy_primitives::hex; + use alloy_primitives::{bytes, hex}; #[test] fn serde_roundtrip() { @@ -107,7 +107,7 @@ mod tests { #[test] fn serde_roundtrip_legacy_with_eip7702_magic() { - let bytes = Bytes::from_static(&hex!("ef0100")); + let bytes = bytes!("ef0100"); let bytecode = Bytecode::new_legacy(bytes.clone()); let json = serde_json::to_string(&bytecode).unwrap(); @@ -180,7 +180,7 @@ mod tests { #[test] fn messagepack_revm_cases() { - let legacy = Bytes::from_static(&hex!("5b6000")); + let legacy = bytes!("5b6000"); let delegated_address = Address::with_last_byte(0x42); let cases = [ (revm_bytecode::Bytecode::new_legacy(legacy.clone()), Bytecode::new_legacy(legacy)), diff --git a/crates/evm2/src/ethereum/eip4844.rs b/crates/evm2/src/ethereum/eip4844.rs index 4c789bbe6..4fb43c480 100644 --- a/crates/evm2/src/ethereum/eip4844.rs +++ b/crates/evm2/src/ethereum/eip4844.rs @@ -218,7 +218,7 @@ mod tests { gas_limit: 21_000, max_fee_per_gas: 2, max_fee_per_blob_gas, - blob_versioned_hashes: vec![B256::from([1; 32])], + blob_versioned_hashes: vec![B256::repeat_byte(1)], to: Address::with_last_byte(0xbb), ..Default::default() } @@ -250,7 +250,7 @@ mod tests { #[rstest::rstest] #[case(vec![], HandlerError::EmptyBlobs)] #[case(vec![B256::ZERO], HandlerError::BlobVersionNotSupported)] - #[case(vec![B256::from([1; 32]); 7], HandlerError::TooManyBlobs { have: 7, max: 6 })] + #[case(vec![B256::repeat_byte(1); 7], HandlerError::TooManyBlobs { have: 7, max: 6 })] fn blob_fee_simulation_still_validates_blobs( #[case] blobs: alloc::vec::Vec, #[case] expected: HandlerError, diff --git a/crates/evm2/src/ethereum/lazy_eip7702.rs b/crates/evm2/src/ethereum/lazy_eip7702.rs index 4e0bbb8f8..1071bff77 100644 --- a/crates/evm2/src/ethereum/lazy_eip7702.rs +++ b/crates/evm2/src/ethereum/lazy_eip7702.rs @@ -292,16 +292,13 @@ mod tests { let key = SigningKey::from_slice(&[0x11; 32]).unwrap(); let expected = Address::from_raw_public_key(&key.verifying_key().to_sec1_point(false).as_bytes()[1..]); - let inner = Authorization { - chain_id: U256::from(1), - address: Address::with_last_byte(0xbb), - nonce: 7, - }; + let inner = + Authorization { chain_id: U256::ONE, address: Address::with_last_byte(0xbb), nonce: 7 }; let (sig, recid) = key.sign_prehash_recoverable(inner.signature_hash().as_slice()); let r = U256::from_be_slice(&sig.r().to_bytes()); let s = U256::from_be_slice(&sig.s().to_bytes()); let y = recid.to_byte(); - let n = SECP256K1N_HALF * U256::from(2) + U256::from(1); + let n = SECP256K1N_HALF * U256::from(2) + U256::ONE; let signed = |y, r, s| SignedAuthorization::new_unchecked(inner.clone(), y, r, s); let valid = signed(y, r, s); diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 76bbc4a18..c7573dbd0 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -1529,7 +1529,7 @@ mod tests { #[rstest::rstest] #[case::call_zero_value(TxKind::Call(Address::ZERO), U256::ZERO, 15_000, 12_700)] - #[case::call_with_value(TxKind::Call(Address::ZERO), U256::from(1), 21_000, 18_700)] + #[case::call_with_value(TxKind::Call(Address::ZERO), U256::ONE, 21_000, 18_700)] #[case::self_transfer_zero_value( TxKind::Call(Address::with_last_byte(0xaa)), U256::ZERO, @@ -1538,12 +1538,12 @@ mod tests { )] #[case::self_transfer_with_value( TxKind::Call(Address::with_last_byte(0xaa)), - U256::from(1), + U256::ONE, 12_000, 10_000 )] #[case::create_zero_value(TxKind::Create, U256::ZERO, 24_000, 22_000)] - #[case::create_with_value(TxKind::Create, U256::from(1), 24_000, 22_000)] + #[case::create_with_value(TxKind::Create, U256::ONE, 24_000, 22_000)] fn eip2780_intrinsic_and_floor_gas_read_base_costs_from_gas_params( #[case] to: TxKind, #[case] value: U256, diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index 9cb4d7027..c961229fc 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -548,7 +548,7 @@ mod tests { test_utils::{handle_test_tx, test_tx}, }; use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{Address, B256, Bytes, TxKind, hex}; + use alloy_primitives::{Address, B256, Bytes, TxKind, bytes}; use core::{ assert_matches, convert::Infallible, fmt, future::Future, pin::Pin, ptr::NonNull, task::Poll, @@ -895,7 +895,7 @@ mod tests { #[test] fn system_call_async_to_missing_code_is_noop() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -914,7 +914,7 @@ mod tests { #[test] fn system_call_async_send_future_is_send() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -938,7 +938,7 @@ mod tests { #[should_panic = "async EVM execution requires EVM erased fields to be verified as Send with Evm::evm_is_send"] fn system_call_async_send_panics_after_overlay_db_mut_replaces_database() { let marker = Rc::new(()); - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -954,7 +954,7 @@ mod tests { #[test] fn system_call_async_succeeds_after_database_error() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -982,7 +982,7 @@ mod tests { #[test] fn dropping_transact_async_discards_nonce_after_cancelled_db_future() { let caller = Address::ZERO; - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 0, op::SLOAD, op::STOP])); let mut evm = Evm::::new( SpecId::OSAKA, @@ -1066,12 +1066,11 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0x11); // SLOAD 0, add 1, SSTORE 0, then CALL self with all but 1024 gas remaining. - let code = Bytecode::new_legacy(Bytes::from_static(&hex!( - "60005460010160005560006000600060006000306104005a03f100" - ))); + let code = + Bytecode::new_legacy(bytes!("60005460010160005560006000600060006000306104005a03f100")); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, diff --git a/crates/evm2/src/evm/bal/bal_context.rs b/crates/evm2/src/evm/bal/bal_context.rs index fa491b8d6..2d8bffe55 100644 --- a/crates/evm2/src/evm/bal/bal_context.rs +++ b/crates/evm2/src/evm/bal/bal_context.rs @@ -398,7 +398,7 @@ mod tests { let code = Bytecode::new_raw(bytes!("6001")); let mut account = AccountBal::default(); account.account_info.balance = vec![ - BalanceChange::new(BlockAccessIndex::new(0), U256::from(1)), + BalanceChange::new(BlockAccessIndex::new(0), U256::ONE), BalanceChange::new(BlockAccessIndex::new(1), U256::from(7)), // Post-execution for a two-transaction block. BalanceChange::new(BlockAccessIndex::new(3), U256::from(42)), @@ -414,7 +414,7 @@ mod tests { for (index, balance, nonce) in [ (0, None, None), - (1, Some(U256::from(1)), None), + (1, Some(U256::ONE), None), (2, Some(U256::from(7)), None), (3, Some(U256::from(7)), Some(5)), ] { diff --git a/crates/evm2/src/evm/bal/changes.rs b/crates/evm2/src/evm/bal/changes.rs index 25e443116..dfb86f84f 100644 --- a/crates/evm2/src/evm/bal/changes.rs +++ b/crates/evm2/src/evm/bal/changes.rs @@ -380,14 +380,14 @@ mod tests { #[test] fn update_drops_change_reverted_at_same_index() { let mut changes = BalChanges::::default(); - changes.update(idx(1), &U256::ZERO, U256::from(1)); - changes.update(idx(1), &U256::from(1), U256::ZERO); + changes.update(idx(1), &U256::ZERO, U256::ONE); + changes.update(idx(1), &U256::ONE, U256::ZERO); assert!(changes.is_empty()); - changes.update(idx(1), &U256::ZERO, U256::from(1)); - changes.update(idx(2), &U256::from(1), U256::from(2)); - changes.update(idx(2), &U256::from(2), U256::from(1)); - assert_eq!(changes.changes, vec![StorageChange::new(idx(1), U256::from(1))]); + changes.update(idx(1), &U256::ZERO, U256::ONE); + changes.update(idx(2), &U256::ONE, U256::from(2)); + changes.update(idx(2), &U256::from(2), U256::ONE); + assert_eq!(changes.changes, vec![StorageChange::new(idx(1), U256::ONE)]); let code = Bytecode::new_raw(bytes!("6001")); let mut changes = BalChanges::::default(); diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index e9cccb5d5..cd9e1d717 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -390,7 +390,7 @@ mod tests { AlloyStorageChange::new(idx(6), U256::from(60)), ]), ), - (U256::from(1), BalChanges::default()), + (U256::ONE, BalChanges::default()), ( U256::from(2), BalChanges::from(vec![ @@ -419,7 +419,7 @@ mod tests { ); let account = &alloy_bal[0]; - assert_eq!(account.storage_reads, vec![U256::from(1), U256::from(3)]); + assert_eq!(account.storage_reads, vec![U256::ONE, U256::from(3)]); assert_eq!( account.storage_changes.iter().map(|slot| slot.slot).collect::>(), vec![U256::from(2), U256::from(4)] @@ -588,7 +588,7 @@ mod tests { let alloy_bal = vec![AlloyAccountChanges { address, storage_changes: vec![AlloySlotChanges::new( - U256::from(1), + U256::ONE, vec![AlloyStorageChange::new(idx(1), U256::from(10))], )], storage_reads: vec![U256::from(2)], diff --git a/crates/evm2/src/evm/db/mod.rs b/crates/evm2/src/evm/db/mod.rs index 2ed888343..7e8a7cee6 100644 --- a/crates/evm2/src/evm/db/mod.rs +++ b/crates/evm2/src/evm/db/mod.rs @@ -547,7 +547,7 @@ mod tests { } fn get_block_hash(&mut self, _number: &Word) -> DbResult { - Ok(B256::from([*self.value; 32])) + Ok(B256::repeat_byte(*self.value)) } } @@ -556,7 +556,7 @@ mod tests { let value = 1; let mut erased = boxed_dyn_database(BorrowingDb { value: &value }); - assert_eq!(erased.get_block_hash(&Word::ZERO).unwrap(), B256::from([value; 32])); + assert_eq!(erased.get_block_hash(&Word::ZERO).unwrap(), B256::repeat_byte(value)); } #[test] diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index d848579fb..3e54b6ff1 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -915,7 +915,7 @@ mod tests { /// Code that stores, loads, logs, calls a child that loads a slot, and creates. fn config_test_code() -> (InMemoryDB, Vec) { - let child = Address::from([0x66; 20]); + let child = Address::repeat_byte(0x66); let mut db = InMemoryDB::default(); db.insert_account_info( &child, @@ -1142,7 +1142,7 @@ mod tests { message_op: u8, immediates: &[u8], ) -> (MessageResult, ConfigInspector) { - let mut code = call_code(Address::from([0x66; 20])); + let mut code = call_code(Address::repeat_byte(0x66)); code.push(message_op); code.extend(immediates); code.extend([op::GAS, op::POP, op::STOP]); @@ -1234,7 +1234,7 @@ mod tests { #[test] fn answered_call_can_change_the_parent_steps() { - let mut code = call_code(Address::from([0x66; 20])); + let mut code = call_code(Address::repeat_byte(0x66)); code.extend([op::CALL, op::GAS, op::POP, op::STOP]); let answer = MessageResultExt { stop: InstrStop::Return, @@ -1284,7 +1284,7 @@ mod tests { #[test] fn child_message_can_change_the_parent_steps() { - let mut code = call_code(Address::from([0x66; 20])); + let mut code = call_code(Address::repeat_byte(0x66)); code.extend([op::CALL, op::GAS, op::POP, op::STOP]); let run = |inspector| { run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector) @@ -1418,7 +1418,7 @@ mod tests { Some(MessageResultExt { stop: InstrStop::Return, gas: GasTracker::new(100), - created_address: Some(Address::from([0x77; 20])), + created_address: Some(Address::repeat_byte(0x77)), ..Default::default() }) } @@ -1455,7 +1455,7 @@ mod tests { } for (mut code, opcode) in - [(call_code(Address::from([0x66; 20])), op::CALL), (create_code(), op::CREATE)] + [(call_code(Address::repeat_byte(0x66)), op::CALL), (create_code(), op::CREATE)] { code.extend([opcode, op::GAS, op::STOP]); let (result, inspector, _) = run_evm_with_inspector( @@ -1542,7 +1542,7 @@ mod tests { ); assert_eq!(result.stop, InstrStop::Revert); - assert_eq!(inspector.stack, [Word::from(1), Word::from(2)]); + assert_eq!(inspector.stack, [Word::ONE, Word::from(2)]); assert_eq!(inspector.steps, 3); assert_eq!(inspector.step_ends, 2); assert_eq!(result.gas.remaining(), 123); @@ -1583,7 +1583,7 @@ mod tests { ); assert_eq!(result.stop, InstrStop::Revert); - assert_eq!(inspector.stack, [Word::from(1)]); + assert_eq!(inspector.stack, [Word::ONE]); assert_eq!(inspector.steps, 1); assert_eq!(inspector.step_ends, 1); assert_eq!(result.gas.remaining(), 123); @@ -1591,7 +1591,7 @@ mod tests { #[test] fn call_too_deep_is_inspected_without_executing() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut code = call_code(target); code.extend([op::CALL, op::STOP]); @@ -1624,7 +1624,7 @@ mod tests { #[test] fn call_inspector_override_skips_execution_and_still_calls_end() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let inspector = OverrideCallInspector { result: MessageResultExt { stop: InstrStop::Return, @@ -1651,7 +1651,7 @@ mod tests { #[test] fn call_inspector_override_wins_at_max_depth() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let inspector = OverrideCallInspector { result: MessageResultExt { stop: InstrStop::Return, ..Default::default() }, min_depth: CALL_DEPTH_LIMIT + 1, @@ -1671,7 +1671,7 @@ mod tests { assert_matches!(result.stop, InstrStop::Return); // The override wins over the call depth check: the call succeeds. - assert_eq!(Word::from_be_slice(&result.output), Word::from(1)); + assert_eq!(Word::from_be_slice(&result.output), Word::ONE); assert_eq!(inspector.call_depth, Some(CALL_DEPTH_LIMIT + 1)); assert_eq!(inspector.call_end_stop, Some(InstrStop::Return)); } @@ -1696,8 +1696,8 @@ mod tests { } } - let target = Address::from([0x22; 20]); - let replacement = Address::from([0x33; 20]); + let target = Address::repeat_byte(0x22); + let replacement = Address::repeat_byte(0x33); let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, @@ -1775,7 +1775,7 @@ mod tests { } } - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut code = call_code(target); code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); return_top_word(&mut code); @@ -1807,7 +1807,7 @@ mod tests { #[test] fn create_inspector_override_skips_execution_and_still_calls_end() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let inspector = OverrideCreateInspector { created, create_depth: None, create_end_stop: None }; let mut code = create_code(); @@ -1825,7 +1825,7 @@ mod tests { #[test] fn create_inspector_sees_derived_destination() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let expected = contract.create(0); let mut code = create_code(); code.extend([op::CREATE, op::STOP]); @@ -1843,10 +1843,10 @@ mod tests { #[test] fn amsterdam_create_preaccess_failure_does_not_fire_create_hook() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); for is_create2 in [false, true] { for (balance, nonce, value, depth) in [ - (Word::from(1), 0, Word::from(2), 0), + (Word::ONE, 0, Word::from(2), 0), (Word::MAX, u64::MAX, Word::ZERO, 0), (Word::MAX, 0, Word::ZERO, CALL_DEPTH_LIMIT), ] { @@ -1883,7 +1883,7 @@ mod tests { #[test] fn create_inspector_override_wins_at_max_depth() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let inspector = OverrideCreateInspector { created, create_depth: None, create_end_stop: None }; let mut code = create_code(); @@ -1933,7 +1933,7 @@ mod tests { } } - let created = Address::from([0x88; 20]); + let created = Address::repeat_byte(0x88); let mut code = create_code(); code.extend([op::CREATE]); return_top_word(&mut code); @@ -1951,7 +1951,7 @@ mod tests { #[test] fn log_opcode_is_inspected_and_emitted_to_host() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let code = Vec::from([op::PUSH1, 0, op::PUSH1, 0, op::LOG0, op::STOP]); let (result, inspector, evm) = run_evm_with_inspector( @@ -2012,8 +2012,8 @@ mod tests { #[test] fn selfdestruct_is_inspected_from_opcode() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let value = Word::from(0xbeef); let mut db = InMemoryDB::default(); db.insert_account_info(&contract, AccountInfo::default().with_balance(value)); @@ -2035,8 +2035,8 @@ mod tests { #[test] fn selfdestruct_dynamic_gas_oog_is_not_inspected() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let mut db = InMemoryDB::default(); db.insert_account_info(&contract, AccountInfo::default()); let mut code = Vec::new(); @@ -2058,7 +2058,7 @@ mod tests { #[test] fn selfdestruct_host_error_is_not_inspected() { // Host failures are injected through the mock host; this intentionally uses [`TestHost`]. - let target = Address::from([0x99; 20]); + let target = Address::repeat_byte(0x99); let mut host = TestHost { selfdestruct_error: Some( DatabaseError::new(crate::AnyError::from("selfdestruct failed"), true).into(), @@ -2084,8 +2084,8 @@ mod tests { #[test] fn evm_transaction_inspects_interpreter_steps_and_logs() { - let caller = Address::from([0xaa; 20]); - let contract = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let contract = Address::repeat_byte(0xbb); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 0, @@ -2133,8 +2133,8 @@ mod tests { #[test] fn evm_transaction_inspects_eip7708_transfer_log() { - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -2170,7 +2170,7 @@ mod tests { #[test] fn evm_create_transaction_initializes_interpreter_with_create_hook() { - let caller = Address::from([0xaa; 20]); + let caller = Address::repeat_byte(0xaa); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -2243,8 +2243,8 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); // CALL(gas, 0x44, 0, 0, 0, 0, 0), then STOP. let code = vec![ op::PUSH0, @@ -2340,8 +2340,8 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); // MSTORE8(0, 0), CREATE(0, 0, 1) with a one byte STOP initcode, then STOP. let code = vec![ op::PUSH0, @@ -2733,7 +2733,7 @@ mod tests { #[test] fn tuple_inspector_forwards_hooks_and_prefers_first_override() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut code = call_code(target); code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); return_top_word(&mut code); @@ -2767,12 +2767,12 @@ mod tests { 50_000, (override_inspector(&[0xaa]), override_inspector(&[0xaa, 0xbb, 0xcc])), ); - assert_eq!(Word::from_be_slice(&result.output), Word::from(1)); + assert_eq!(Word::from_be_slice(&result.output), Word::ONE); } #[test] fn option_inspector_forwards_hooks_only_when_present() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut code = call_code(target); code.extend([op::CALL, op::STOP]); diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index fae201da2..85d141e6c 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -2135,7 +2135,7 @@ fn eip7708_transfer_log(from: &Address, to: &Address, value: &Word) -> Option()), + Bytes::from(value.to_be_bytes::<32>()), )) } @@ -2256,11 +2256,11 @@ mod tests { req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ) -> HandlerResult { let account = req.host.state.account(&LIFECYCLE_ACCOUNT)?; - assert_eq!(account.balance(), Word::from(1)); + assert_eq!(account.balance(), Word::ONE); drop(account); let slot = req.host.state.storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)?; - assert_eq!(slot.current(), Word::from(1)); + assert_eq!(slot.current(), Word::ONE); Ok(TxResultExt { status: true, ..TxResultExt::default() }) } @@ -2449,9 +2449,9 @@ mod tests { let mut database = InMemoryDB::default(); database.insert_account_info( &LIFECYCLE_ACCOUNT, - AccountInfo::default().with_balance(Word::from(1)), + AccountInfo::default().with_balance(Word::ONE), ); - database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::from(1)); + database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::ONE); Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -2470,9 +2470,9 @@ mod tests { let mut database = InMemoryDB::default(); database.insert_account_info( &LIFECYCLE_ACCOUNT, - AccountInfo::default().with_balance(Word::from(1)), + AccountInfo::default().with_balance(Word::ONE), ); - database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::from(1)); + database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::ONE); Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -2563,7 +2563,7 @@ mod tests { impl Error for TestPrecompileError {} - let contract = Address::from([0xbb; 20]); + let contract = Address::repeat_byte(0xbb); let mut code = vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0]; push_address(&mut code, &FATAL_PRECOMPILE_ADDRESS); code.extend([ @@ -2613,7 +2613,7 @@ mod tests { !pending .accounts .get(&contract) - .is_some_and(|account| account.storage.slots.contains_key(&Word::from(1))) + .is_some_and(|account| account.storage.slots.contains_key(&Word::ONE)) ); } @@ -2621,7 +2621,7 @@ mod tests { fn precompile_oog_halt_remains_recoverable_by_parent_call() { const OOG_PRECOMPILE_ADDRESS: Address = Address::with_last_byte(0x44); - let contract = Address::from([0xbc; 20]); + let contract = Address::repeat_byte(0xbc); let mut code = vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0]; push_address(&mut code, &OOG_PRECOMPILE_ADDRESS); code.extend([ @@ -2668,7 +2668,7 @@ mod tests { pending .accounts .get(&contract) - .is_some_and(|account| account.storage.slots.contains_key(&Word::from(1))) + .is_some_and(|account| account.storage.slots.contains_key(&Word::ONE)) ); } @@ -2687,7 +2687,7 @@ mod tests { impl Error for TestPrecompileError {} - let caller = Address::from([0xaa; 20]); + let caller = Address::repeat_byte(0xaa); let tx = Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { gas_limit: 50_000, @@ -3108,7 +3108,7 @@ mod tests { let origin_word = address_to_word(&origin); assert_eq!(evm.state.storage_slot_untracked(&target, &Word::ZERO).unwrap(), origin_word); - assert_eq!(evm.state.storage_slot_untracked(&inner, &Word::from(1)).unwrap(), origin_word); + assert_eq!(evm.state.storage_slot_untracked(&inner, &Word::ONE).unwrap(), origin_word); assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().nonce, 1); } @@ -3232,7 +3232,7 @@ mod tests { assert_eq!(message.input, Bytes::from_static(b"message input")); assert_eq!(message.value, U256::from(99)); assert_eq!(message.code_address, TEST_PRECOMPILE); - Ok(PrecompileOutput::new(Bytes::copy_from_slice(&evm.block.number.to_be_bytes::<32>()))) + Ok(PrecompileOutput::new(Bytes::from(evm.block.number.to_be_bytes::<32>()))) })]); let mut evm = Evm::::new( SpecId::OSAKA, @@ -3506,7 +3506,7 @@ mod tests { assert!(evm.logs().is_empty()); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); evm.registry.register( TEST_TX_TYPE, @@ -3526,7 +3526,7 @@ mod tests { assert_eq!(outcome.logs.len(), 1); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3545,11 +3545,11 @@ mod tests { assert_eq!(outcome.logs.len(), 1); let storage = sink.storage_sorted(); assert_eq!(storage.len(), 1); - assert_eq!(storage[0].1.original, Word::from(1)); + assert_eq!(storage[0].1.original, Word::ONE); assert_eq!(storage[0].1.current, Word::from(7)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3594,11 +3594,11 @@ mod tests { .storage; let slot = overlay.slots.get(&LIFECYCLE_STORAGE_KEY).expect("storage slot should be present"); - assert_eq!(slot.value.original, Word::from(1)); + assert_eq!(slot.value.original, Word::ONE); assert_eq!(slot.value.current, Word::from(7)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3623,7 +3623,7 @@ mod tests { // Detaching accepted nothing into this EVM's overlay. assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3662,7 +3662,7 @@ mod tests { let storage = block_state.storage_sorted(); assert_eq!(storage.len(), 1); - assert_eq!(storage[0].1.original, Word::from(1)); + assert_eq!(storage[0].1.original, Word::ONE); assert_eq!(storage[0].1.current, Word::from(9)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), @@ -3760,7 +3760,7 @@ mod tests { let slots = changes.storage.changed_slots().collect::>(); assert_eq!(slots.len(), 1); assert_eq!(*slots[0].0, LIFECYCLE_STORAGE_KEY); - assert_eq!(slots[0].1.original, Word::from(1)); + assert_eq!(slots[0].1.original, Word::ONE); assert_eq!(slots[0].1.current, Word::from(7)); let _ = executed.commit(); @@ -3777,7 +3777,7 @@ mod tests { assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3790,7 +3790,7 @@ mod tests { InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::ADDRESS, op::STOP])); let mut message = MessageExt { kind: MessageKind::Call, @@ -3855,7 +3855,7 @@ mod tests { Db::new(FailingStorageDb), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH0, op::SLOAD])); let mut message = MessageExt { kind: MessageKind::Call, @@ -3887,7 +3887,7 @@ mod tests { InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let key = Word::ZERO; let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 0, op::SLOAD, op::STOP])); @@ -3936,7 +3936,7 @@ mod tests { self.target_reads.fetch_add(1, Ordering::SeqCst); return Err(SelfdestructTargetLoadError); } - Ok(Some(AccountInfo::default().with_balance(Word::from(1)))) + Ok(Some(AccountInfo::default().with_balance(Word::ONE))) } fn get_code_by_hash(&mut self, _code_hash: &B256) -> Result { @@ -3963,8 +3963,8 @@ mod tests { legacy_bytecode(code) } - let contract = Address::from([0xbb; 20]); - let target = Address::from([0xcc; 20]); + let contract = Address::repeat_byte(0xbb); + let target = Address::repeat_byte(0xcc); let target_reads = Arc::new(AtomicUsize::new(0)); let database = SelfdestructTargetLoadDb { target, target_reads: Arc::clone(&target_reads) }; let mut evm = Evm::::new( @@ -3990,10 +3990,10 @@ mod tests { #[test] fn frontier_code_deposit_oog_creates_empty_contract() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), @@ -4023,7 +4023,7 @@ mod tests { #[test] fn frontier_materialized_empty_account_is_created() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), @@ -4041,10 +4041,10 @@ mod tests { #[test] fn homestead_code_deposit_oog_fails_create() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::HOMESTEAD, BlockEnvExt::default(), @@ -4074,10 +4074,10 @@ mod tests { #[test] fn create_validation_failure_clears_output() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::LONDON, BlockEnvExt::default(), @@ -4113,10 +4113,10 @@ mod tests { #[test] fn create_size_limit_failure_clears_output() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -4148,10 +4148,10 @@ mod tests { #[test] fn staticcall_touches_empty_existing_destination() { - let target = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x11); let mut database = InMemoryDB::default(); database.insert_account_info(&target, AccountInfo::default()); - database.insert_account_storage(&target, &Word::ZERO, &Word::from(1)); + database.insert_account_storage(&target, &Word::ZERO, &Word::ONE); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -4180,13 +4180,12 @@ mod tests { #[test] fn delegatecall_does_not_touch_empty_code_address() { - let destination = Address::from([0x11; 20]); - let code_address = Address::from([0x22; 20]); + let destination = Address::repeat_byte(0x11); + let code_address = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database - .insert_account_info(&destination, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&destination, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_info(&code_address, AccountInfo::default()); - database.insert_account_storage(&code_address, &Word::ZERO, &Word::from(1)); + database.insert_account_storage(&code_address, &Word::ZERO, &Word::ONE); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -4220,8 +4219,8 @@ mod tests { #[test] fn transfer_moves_value() { - let from = Address::from([0x01; 20]); - let to = Address::from([0x02; 20]); + let from = Address::repeat_byte(0x01); + let to = Address::repeat_byte(0x02); let mut state = State::new(InMemoryDB::default()); state.account(&from).unwrap().add_balance(U256::from(10)); @@ -4246,8 +4245,8 @@ mod tests { #[test] fn amsterdam_call_value_emits_eip7708_log() { - let caller = Address::from([0x01; 20]); - let target = Address::from([0x02; 20]); + let caller = Address::repeat_byte(0x01); + let target = Address::repeat_byte(0x02); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default().with_balance(U256::from(10))); let mut evm = Evm::::new( @@ -4278,12 +4277,12 @@ mod tests { let log = &logs[0]; assert_eq!(log.address, SYSTEM_ADDRESS); assert_eq!(log.topics(), &[EIP7708_TRANSFER_TOPIC, caller.into_word(), target.into_word()]); - assert_eq!(log.data.data, Bytes::copy_from_slice(&U256::from(7).to_be_bytes::<32>())); + assert_eq!(log.data.data, Bytes::from(B256::with_last_byte(7))); } #[test] fn eip8246_selfdestruct_preserves_balance_at_finalization() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -4312,7 +4311,7 @@ mod tests { #[test] fn commit_transaction_clears_detached_destroyed_flag() { - let contract = Address::from([0x44; 20]); + let contract = Address::repeat_byte(0x44); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); @@ -4330,7 +4329,7 @@ mod tests { #[test] fn eip8246_selfdestruct_zero_balance_is_deleted() { - let contract = Address::from([0x22; 20]); + let contract = Address::repeat_byte(0x22); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); @@ -4345,7 +4344,7 @@ mod tests { #[test] fn pre_eip8246_selfdestruct_burns_balance_by_deleting_account() { - let contract = Address::from([0x33; 20]); + let contract = Address::repeat_byte(0x33); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -4576,7 +4575,7 @@ mod tests { evm.state .storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY) .unwrap(), - Word::from(1) + Word::ONE ); } } @@ -4851,7 +4850,7 @@ mod tests { assert!(!changes.is_empty()); assert_eq!(changes.account_info(&caller).unwrap().nonce, 1); let slot = changes.account_changes_for(&target).unwrap().storage.slots[&Word::ZERO].value; - assert_eq!(slot.current, Word::from(1)); + assert_eq!(slot.current, Word::ONE); let mut in_place = executed.account_changes().map(|c| c.address).collect::>(); in_place.sort(); let _ = executed.discard(); diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index a856fe12e..82d1cc0b6 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -728,7 +728,7 @@ mod tests { #[test] fn transfer_to_self_requires_balance() { - let address = Address::from([0x77; 20]); + let address = Address::repeat_byte(0x77); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(3))); let mut state = State::new(database); @@ -741,7 +741,7 @@ mod tests { fn journaled_account_mutations_journal_and_roll_back() { use crate::{SpecId, Version, bytecode::Bytecode}; - let address = Address::from([0x88; 20]); + let address = Address::repeat_byte(0x88); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -777,7 +777,7 @@ mod tests { fn account_info_set_code_is_published_on_commit() { use alloy_primitives::Bytes; - let address = Address::from([0x8c; 20]); + let address = Address::repeat_byte(0x8c); let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let mut state = State::new(CacheDB::default()); @@ -820,7 +820,7 @@ mod tests { // A hash that is not the keccak of the code proves `hash_slow` returns the seeded value // instead of hashing. let seed = B256::repeat_byte(0x5e); - let address = Address::from([0x8d; 20]); + let address = Address::repeat_byte(0x8d); let fresh = || Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); assert_ne!(fresh().hash_slow(), seed); @@ -832,7 +832,7 @@ mod tests { assert_eq!(state.account(&address).unwrap().load_code().unwrap().hash_slow(), seed); assert_eq!(state.account(&address).unwrap().original_code().unwrap().hash_slow(), seed); - let overlay_address = Address::from([0x8e; 20]); + let overlay_address = Address::repeat_byte(0x8e); state.account(&overlay_address).unwrap().set_code(seed, fresh()); assert_eq!(state.account(&overlay_address).unwrap().load_code().unwrap().hash_slow(), seed); } @@ -842,7 +842,7 @@ mod tests { use crate::{SpecId, Version, bytecode::Bytecode}; use alloy_primitives::Bytes; - let address = Address::from([0x8b; 20]); + let address = Address::repeat_byte(0x8b); let original_code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let original_code_hash = original_code.hash_slow(); let mut database = CacheDB::default(); @@ -881,7 +881,7 @@ mod tests { #[test] fn journaled_account_read_only_handle_journals_nothing() { - let address = Address::from([0x89; 20]); + let address = Address::repeat_byte(0x89); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); let mut state = State::new(database); @@ -901,7 +901,7 @@ mod tests { fn journaled_account_skip_cold_load_signals_skip() { use crate::LoadError; - let address = Address::from([0x8a; 20]); + let address = Address::repeat_byte(0x8a); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); let mut state = State::new(database); @@ -986,7 +986,7 @@ mod tests { assert_eq!((account.balance(), account.nonce()), (balance, nonce)); assert!(!account.is_touched()); assert!(!account.is_warm()); - account.set_balance(Word::from(1)); + account.set_balance(Word::ONE); account.set_nonce(1); } assert_eq!(state.checkpoint().journal_len(), checkpoint.journal_len() + 1); diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index a3cf5006d..665966bef 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -345,8 +345,8 @@ mod tests { #[test] fn block_accumulator_collapses_create_then_delete() { - let address = Address::from([0x50; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x50); + let key = Word::ONE; let created = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -369,8 +369,8 @@ mod tests { #[test] fn block_accumulator_preserves_original_for_delete_then_recreate() { - let address = Address::from([0x51; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x51); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let recreated = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -468,9 +468,9 @@ mod tests { // Selfdestruct followed by a revival in a later transaction of the same block (a plain // transfer or a re-create): the revived account is a live account, which does not wipe // storage at a sink by itself, so the sink must still see the wipe. - let address = Address::from([0x55; 20]); + let address = Address::repeat_byte(0x55); let original = AccountInfo::default().with_balance(Word::from(3)); - let revived = AccountInfo::default().with_balance(Word::from(1)); + let revived = AccountInfo::default().with_balance(Word::ONE); let mut accumulator = BlockStateAccumulator::new(); changes(address, Some(original.clone()), None, true, U256Map::default()) @@ -553,8 +553,8 @@ mod tests { #[test] fn block_accumulator_keeps_nonzero_write_after_storage_wipe() { - let address = Address::from([0x52; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x52); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let mut accumulator = BlockStateAccumulator::new(); @@ -578,8 +578,8 @@ mod tests { #[test] fn block_accumulator_reinserts_unchanged_slot_after_wiping_created_account() { - let address = Address::from([0x53; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x53); + let key = Word::ONE; let account = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -610,8 +610,8 @@ mod tests { #[test] fn block_accumulator_normalizes_storage_when_creation_arrives_after_slots() { - let address = Address::from([0x54; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x54); + let key = Word::ONE; let account = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -631,8 +631,8 @@ mod tests { #[test] fn block_accumulator_deletion_subsumes_storage_writes() { - let address = Address::from([0x56; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x56); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let mut accumulator = BlockStateAccumulator::new(); @@ -650,8 +650,8 @@ mod tests { #[test] fn block_accumulator_collapses_storage_wipe_write_wipe() { - let address = Address::from([0x52; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x52); + let key = Word::ONE; let mut accumulator = BlockStateAccumulator::new(); let first = changes(address, None, None, true, slot(key, Word::from(5), Word::from(7))); @@ -667,8 +667,8 @@ mod tests { #[test] fn block_accumulator_drops_storage_reverted_to_original() { - let address = Address::from([0x57; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x57); + let key = Word::ONE; let mut accumulator = BlockStateAccumulator::new(); changes(address, None, None, false, slot(key, Word::from(3), Word::from(4))) @@ -683,8 +683,8 @@ mod tests { #[test] fn block_accumulator_isolates_storage_between_accounts() { - let [a, b, c, d, e] = [0x61, 0x62, 0x63, 0x64, 0x65].map(|byte| Address::from([byte; 20])); - let (k1, k2) = (Word::from(1), Word::from(2)); + let [a, b, c, d, e] = [0x61, 0x62, 0x63, 0x64, 0x65].map(Address::repeat_byte); + let (k1, k2) = (Word::ONE, Word::from(2)); let info = AccountInfo::default().with_nonce(1); let w = Word::from; let mut accumulator = BlockStateAccumulator::new(); @@ -747,10 +747,10 @@ mod tests { #[test] fn block_accumulator_keeps_account_only_and_storage_only_changes_separate() { - let account_address = Address::from([0x53; 20]); - let storage_address = Address::from([0x54; 20]); - let key = Word::from(1); - let original = AccountInfo::default().with_balance(Word::from(1)); + let account_address = Address::repeat_byte(0x53); + let storage_address = Address::repeat_byte(0x54); + let key = Word::ONE; + let original = AccountInfo::default().with_balance(Word::ONE); let current = AccountInfo::default().with_balance(Word::from(2)); let mut accumulator = BlockStateAccumulator::new(); diff --git a/crates/evm2/src/evm/state/journal.rs b/crates/evm2/src/evm/state/journal.rs index 410849bc2..f7019f899 100644 --- a/crates/evm2/src/evm/state/journal.rs +++ b/crates/evm2/src/evm/state/journal.rs @@ -91,7 +91,7 @@ mod tests { #[test] fn destruct_change_rolls_back_to_checkpoint() { - let address = Address::from([0x33; 20]); + let address = Address::repeat_byte(0x33); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -108,11 +108,11 @@ mod tests { let mut state = State::new(CacheDB::default()); let kept = Log { - address: Address::from([0x44; 20]), + address: Address::repeat_byte(0x44), data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x01])), }; let reverted = Log { - address: Address::from([0x55; 20]), + address: Address::repeat_byte(0x55), data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), }; @@ -125,7 +125,7 @@ mod tests { &[ kept.clone(), Log { - address: Address::from([0x55; 20]), + address: Address::repeat_byte(0x55), data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), } ] @@ -172,7 +172,7 @@ mod tests { let frame_account = Address::with_last_byte(0x11); let base_storage = Address::with_last_byte(0x12); let frame_storage = Address::with_last_byte(0x13); - let key = Word::from(1); + let key = Word::ONE; let mut state = State::new(CacheDB::default()); state.prewarm(&base_account); @@ -198,7 +198,7 @@ mod tests { fn warm_only_entries_do_not_emit_state_changes() { let account = Address::with_last_byte(0x14); let storage_account = Address::with_last_byte(0x15); - let key = Word::from(1); + let key = Word::ONE; let mut state = State::new(CacheDB::default()); state.prewarm(&account); @@ -218,7 +218,7 @@ mod tests { fn rollback_preserves_non_revertible_account_warmth_after_load() { let account = Address::with_last_byte(0x16); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); state.prewarm(&account); @@ -237,9 +237,9 @@ mod tests { #[test] fn rollback_preserves_non_revertible_storage_warmth_after_write() { let account = Address::with_last_byte(0x17); - let key = Word::from(1); + let key = Word::ONE; let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); state.prewarm_storage_slot(&account, key); @@ -255,10 +255,10 @@ mod tests { #[test] fn rollback_reverts_storage_warmth_without_discarding_cached_value() { let account = Address::with_last_byte(0x18); - let key = Word::from(1); + let key = Word::ONE; let value = Word::from(9); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_storage(&account, &key, &value); let mut state = State::new(database); diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index 5c712271f..1e185a0d0 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -1096,10 +1096,10 @@ mod tests { fn created_account_reads_old_storage_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (loaded, unloaded, fresh) = (Word::from(1), Word::from(2), Word::from(3)); + let (loaded, unloaded, fresh) = (Word::ONE, Word::from(2), Word::from(3)); let features = EvmFeatures::empty(); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &loaded, &Word::from(5)); db.insert_account_storage(&address, &unloaded, &Word::from(6)); db.insert_account_storage(&address, &fresh, &Word::from(7)); @@ -1132,7 +1132,7 @@ mod tests { fn marked_created_account_reads_and_commits_its_baseline() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (written, unwritten) = (Word::from(1), Word::from(2)); + let (written, unwritten) = (Word::ONE, Word::from(2)); let features = EvmFeatures::empty(); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default().with_nonce(1)); @@ -1159,7 +1159,7 @@ mod tests { let other = Address::with_last_byte(3); state .overlay_db_mut() - .insert_account_info(&other, AccountInfo::default().with_balance(Word::from(1))); + .insert_account_info(&other, AccountInfo::default().with_balance(Word::ONE)); state.overlay_db_mut().insert_account_storage(&other, &unwritten, &Word::from(6)); state.create_account(&caller, &other, &Word::ZERO, features).unwrap().unwrap(); state.account(&other).unwrap().mark_created(); @@ -1174,7 +1174,7 @@ mod tests { let written = Word::from(2); let version = Version::base(crate::SpecId::CANCUN); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(db); state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); @@ -1191,10 +1191,10 @@ mod tests { fn eip8246_created_survivor_keeps_old_storage() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (written, old) = (Word::from(1), Word::from(2)); + let (written, old) = (Word::ONE, Word::from(2)); let version = Version::base(crate::SpecId::AMSTERDAM); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &old, &Word::from(6)); let mut state = State::new(db); @@ -1215,10 +1215,10 @@ mod tests { #[test] fn eip8246_marked_created_survivor_wipes_storage() { let address = Address::with_last_byte(2); - let (written, earlier) = (Word::from(1), Word::from(2)); + let (written, earlier) = (Word::ONE, Word::from(2)); let version = Version::base(crate::SpecId::AMSTERDAM); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); // A slot the account's creation wrote before the transaction was suspended. db.insert_account_storage(&address, &earlier, &Word::from(6)); let mut state = State::new(db); @@ -1238,10 +1238,10 @@ mod tests { #[test] fn rollback_of_storage_discard_restores_storage_baseline() { let address = Address::with_last_byte(2); - let key = Word::from(1); + let key = Word::ONE; let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); @@ -1260,10 +1260,10 @@ mod tests { fn rollback_of_creation_keeps_slots_read_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (read, written, empty) = (Word::from(1), Word::from(2), Word::from(3)); + let (read, written, empty) = (Word::ONE, Word::from(2), Word::from(3)); let version = Version::base(crate::SpecId::CANCUN); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &read, &Word::from(6)); db.insert_account_storage(&address, &written, &Word::from(7)); let mut state = State::new(db); @@ -1288,10 +1288,10 @@ mod tests { fn rollback_keeps_slot_read_as_zero_under_the_creation() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let key = Word::from(1); + let key = Word::ONE; let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); @@ -1308,10 +1308,10 @@ mod tests { #[test] fn rollback_of_creation_mark_leaves_slots_alone() { let address = Address::with_last_byte(2); - let (read, zero) = (Word::from(1), Word::from(2)); + let (read, zero) = (Word::ONE, Word::from(2)); let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &read, &Word::from(6)); let mut state = State::new(db); @@ -1329,10 +1329,10 @@ mod tests { fn finalization_wipe_after_rolled_back_creation_reads_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let key = Word::from(1); + let key = Word::ONE; let version = Version::base(crate::SpecId::LONDON); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); @@ -1352,10 +1352,10 @@ mod tests { fn rollback_of_creation_keeps_slots_loaded_under_it() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (before, under) = (Word::from(1), Word::from(2)); + let (before, under) = (Word::ONE, Word::from(2)); let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(db); assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); // Only a reread can observe these values, and rollback does not reread. @@ -1375,10 +1375,10 @@ mod tests { #[test] fn rollback_keeps_zero_of_slot_written_through_the_creating_handle() { let address = Address::with_last_byte(2); - let key = Word::from(1); + let key = Word::ONE; let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); @@ -1397,10 +1397,10 @@ mod tests { fn later_transactions_read_old_storage_of_created_account_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (cached, unloaded, written) = (Word::from(1), Word::from(2), Word::from(3)); + let (cached, unloaded, written) = (Word::ONE, Word::from(2), Word::from(3)); let version = Version::base(crate::SpecId::CANCUN); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &cached, &Word::from(5)); db.insert_account_storage(&address, &unloaded, &Word::from(6)); let mut state = State::new(db); @@ -1422,7 +1422,7 @@ mod tests { fn account_storage_handle_reuses_pooled_slot_map() { let address = Address::with_last_byte(1); let mut state = State::new(EmptyDB::default()); - let key = Word::from(1); + let key = Word::ONE; state.storage_slot(&address, key).unwrap(); let expected_capacity = state.accounts[&address].storage.slots.capacity(); assert!(expected_capacity > 0); @@ -1438,7 +1438,7 @@ mod tests { #[test] fn clone_preserves_state_and_detaches_database() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default()); db.insert_account_storage(&address, &key, &Word::from(10)); @@ -1464,13 +1464,13 @@ mod tests { fn clone_with_uses_database_and_preserves_state() { let address = Address::with_last_byte(42); let mut state = State::new(EmptyDB::default()); - state.tstore(&address, &Word::ZERO, &Word::from(1)); + state.tstore(&address, &Word::ZERO, &Word::ONE); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default()); let mut cloned = state.clone_with(db); - assert_eq!(cloned.tload(&address, &Word::ZERO), Word::from(1)); + assert_eq!(cloned.tload(&address, &Word::ZERO), Word::ONE); assert!(cloned.account_info_untracked(&address).unwrap().is_some()); } @@ -1492,7 +1492,7 @@ mod tests { #[test] fn set_storage_warm_preserves_loaded_values() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; for prewarmed in [false, true] { let mut state = State::new(EmptyDB::default()); state.set_storage_warm(&address, key, true); @@ -1520,7 +1520,7 @@ mod tests { #[test] fn set_account_warm_preserves_loaded_values() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; for prewarmed in [false, true] { let mut state = State::new(EmptyDB::default()); state.set_account_warm(&address, true); @@ -1667,23 +1667,23 @@ mod tests { #[test] fn loaded_accounts_include_reads_and_absent_accounts() { - let read = Address::from([0x74; 20]); - let absent = Address::from([0x75; 20]); - let accepted = Address::from([0x76; 20]); + let read = Address::repeat_byte(0x74); + let absent = Address::repeat_byte(0x75); + let accepted = Address::repeat_byte(0x76); let mut database = CacheDB::default(); - database.insert_account_info(&read, AccountInfo::default().with_balance(Word::from(1))); - database.insert_account_storage(&read, &Word::from(1), &Word::from(10)); + database.insert_account_info(&read, AccountInfo::default().with_balance(Word::ONE)); + database.insert_account_storage(&read, &Word::ONE, &Word::from(10)); let mut state = State::new(database); - state.account(&accepted).unwrap().set_balance(Word::from(1)); + state.account(&accepted).unwrap().set_balance(Word::ONE); state.commit_transaction(); - state.storage_slot(&read, Word::from(1)).unwrap(); + state.storage_slot(&read, Word::ONE).unwrap(); state.storage_slot(&read, Word::from(2)).unwrap(); state.account(&absent).unwrap(); assert_eq!( sorted_loaded_accounts(&state), - [(read, vec![Word::from(1), Word::from(2)]), (absent, vec![])] + [(read, vec![Word::ONE, Word::from(2)]), (absent, vec![])] ); } @@ -1691,7 +1691,7 @@ mod tests { fn loaded_accounts_include_reads_in_reverted_frames() { let read = Address::with_last_byte(0x77); let mut database = CacheDB::default(); - database.insert_account_info(&read, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&read, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); let checkpoint = state.checkpoint(); diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index a4da6e6ed..84c6db14d 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -193,7 +193,7 @@ mod tests { #[test] fn inserts_account_and_storage() { let address = Address::with_last_byte(0xaa); - let key = Word::from(1); + let key = Word::ONE; let original = AccountInfo { nonce: 1, ..AccountInfo::default() }; let current = AccountInfo { nonce: 2, ..AccountInfo::default() }; let mut state = PendingState::default(); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index 5ae04c274..4f8dd22a5 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -386,59 +386,59 @@ mod tests { #[test] fn storage_change_rolls_back_to_checkpoint() { - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); - database.insert_account_storage(&address, &Word::from(1), &Word::from(10)); + database.insert_account_storage(&address, &Word::ONE, &Word::from(10)); let mut state = State::new(database); let checkpoint = state.checkpoint(); - state.storage_slot(&address, Word::from(1)).unwrap().write(Word::from(20)); - state.storage_slot(&address, Word::from(1)).unwrap().write(Word::from(30)); + state.storage_slot(&address, Word::ONE).unwrap().write(Word::from(20)); + state.storage_slot(&address, Word::ONE).unwrap().write(Word::from(30)); - assert_eq!(state.storage_slot(&address, Word::from(1)).unwrap().current(), Word::from(30)); + assert_eq!(state.storage_slot(&address, Word::ONE).unwrap().current(), Word::from(30)); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); - assert_eq!(state.storage_slot(&address, Word::from(1)).unwrap().current(), Word::from(10)); + assert_eq!(state.storage_slot(&address, Word::ONE).unwrap().current(), Word::from(10)); } #[test] fn transient_storage_change_rolls_back_to_checkpoint() { - let address = Address::from([0x22; 20]); + let address = Address::repeat_byte(0x22); let mut state = State::new(CacheDB::default()); - state.tstore(&address, &Word::from(1), &Word::from(10)); + state.tstore(&address, &Word::ONE, &Word::from(10)); let checkpoint = state.checkpoint(); - state.tstore(&address, &Word::from(1), &Word::from(20)); + state.tstore(&address, &Word::ONE, &Word::from(20)); - assert_eq!(state.tload(&address, &Word::from(1)), Word::from(20)); + assert_eq!(state.tload(&address, &Word::ONE), Word::from(20)); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); - assert_eq!(state.tload(&address, &Word::from(1)), Word::from(10)); + assert_eq!(state.tload(&address, &Word::ONE), Word::from(10)); } #[test] fn take_transient_storage_only_drains_requested_account() { - let address = Address::from([0x22; 20]); - let other = Address::from([0x23; 20]); + let address = Address::repeat_byte(0x22); + let other = Address::repeat_byte(0x23); let mut state = State::new(CacheDB::default()); - state.tstore(&address, &Word::from(1), &Word::from(10)); + state.tstore(&address, &Word::ONE, &Word::from(10)); state.tstore(&address, &Word::from(2), &Word::from(20)); - state.tstore(&other, &Word::from(1), &Word::from(30)); + state.tstore(&other, &Word::ONE, &Word::from(30)); let mut slots = state.take_transient_storage(&address); slots.sort_unstable_by_key(|(key, _)| *key); - assert_eq!(slots, vec![(Word::from(1), Word::from(10)), (Word::from(2), Word::from(20))]); - assert_eq!(state.tload(&address, &Word::from(1)), Word::ZERO); - assert_eq!(state.tload(&other, &Word::from(1)), Word::from(30)); + assert_eq!(slots, vec![(Word::ONE, Word::from(10)), (Word::from(2), Word::from(20))]); + assert_eq!(state.tload(&address, &Word::ONE), Word::ZERO); + assert_eq!(state.tload(&other, &Word::ONE), Word::from(30)); } #[test] fn storage_wipe_preserves_warm_slots_in_merged_storage_map() { let account = Address::with_last_byte(0x19); - let warm_key = Word::from(1); + let warm_key = Word::ONE; let cold_key = Word::from(2); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_storage(&account, &warm_key, &Word::from(3)); database.insert_account_storage(&account, &cold_key, &Word::from(4)); let mut state = State::new(database); @@ -464,8 +464,8 @@ mod tests { #[test] fn journaled_storage_mutations_journal_and_roll_back() { - let address = Address::from([0x33; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x33); + let key = Word::ONE; let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); database.insert_account_storage(&address, &key, &Word::from(10)); @@ -493,7 +493,7 @@ mod tests { #[test] fn journaled_storage_read_only_handle_journals_nothing() { - let address = Address::from([0x34; 20]); + let address = Address::repeat_byte(0x34); let key = Word::from(7); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); @@ -512,7 +512,7 @@ mod tests { #[test] fn fused_slot_load_warms_once_and_reverts() { - let address = Address::from([0x35; 20]); + let address = Address::repeat_byte(0x35); let key = Word::from(8); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -551,7 +551,7 @@ mod tests { #[test] fn storage_handle_reports_warmth_before_loading() { - let address = Address::from([0x35; 20]); + let address = Address::repeat_byte(0x35); let key = Word::from(9); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); @@ -576,7 +576,7 @@ mod tests { #[test] fn storage_slot_warm_honors_prewarm_after_load() { - let address = Address::from([0x36; 20]); + let address = Address::repeat_byte(0x36); let key = Word::from(11); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index 0ab99a635..93d25075f 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -263,8 +263,8 @@ mod tests { #[test] fn system_call_uses_system_sender_without_fee_accounting() { - let contract = Address::from([0x42; 20]); - let beneficiary = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x42); + let beneficiary = Address::repeat_byte(0x99); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::CALLER, op::PUSH1, @@ -305,8 +305,8 @@ mod tests { #[test] fn system_call_uses_custom_caller() { - let caller = Address::from([0x11; 20]); - let contract = Address::from([0x42; 20]); + let caller = Address::repeat_byte(0x11); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::CALLER, op::PUSH1, @@ -343,7 +343,7 @@ mod tests { #[test] fn system_call_starts_with_warm_system_contract() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::ADDRESS, op::EXTCODESIZE, op::STOP])); let mut database = InMemoryDB::default(); @@ -368,7 +368,7 @@ mod tests { #[test] fn system_call_to_missing_code_is_noop() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -386,7 +386,7 @@ mod tests { #[test] fn system_call_reverts_state_changes() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -470,7 +470,7 @@ mod tests { #[case] gas_limit: Option, #[case] expected_limit: u64, ) { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); // Return the gas available at the start of execution (after GAS charges its own cost). let code = Bytecode::new_legacy(Bytes::from_static(&[ op::GAS, @@ -505,7 +505,7 @@ mod tests { #[test] fn system_call_exhausts_configured_gas_limit() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -536,7 +536,7 @@ mod tests { #[test] fn system_call_total_includes_reservoir_funded_state_gas() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -568,7 +568,7 @@ mod tests { #[test] fn system_call_total_covers_state_gas_with_unbounded_gas_limit() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -633,7 +633,7 @@ mod tests { #[case] expected_calls: usize, #[case] expected_steps: usize, ) { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 1, op::PUSH0, op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); diff --git a/crates/evm2/src/interpreter/instructions/arithmetic.rs b/crates/evm2/src/interpreter/instructions/arithmetic.rs index 2b34db1b4..d84c91c81 100644 --- a/crates/evm2/src/interpreter/instructions/arithmetic.rs +++ b/crates/evm2/src/interpreter/instructions/arithmetic.rs @@ -56,7 +56,7 @@ pub fn exp(cx: _, [a, b]: [Word]) -> Result { #[instruction] pub fn signextend([ext, value]: [Word]) -> out { if *ext < 31 { - let bit_index = (8 * ext.as_limbs()[0] + 7) as usize; + let bit_index = (8 * ext.wrapping_to::() + 7) as usize; let mask = (Word::ONE << bit_index) - Word::ONE; out.write(if value.bit(bit_index) { *value | !mask } else { *value & mask }); } diff --git a/crates/evm2/src/interpreter/instructions/bitwise.rs b/crates/evm2/src/interpreter/instructions/bitwise.rs index 20e92074a..f95193088 100644 --- a/crates/evm2/src/interpreter/instructions/bitwise.rs +++ b/crates/evm2/src/interpreter/instructions/bitwise.rs @@ -192,7 +192,7 @@ mod tests { #[test] fn sar_opcode() { - assert_stack!(SAR(1, neg(1) - Word::from(1)), neg(1)); + assert_stack!(SAR(1, neg(1) - Word::ONE), neg(1)); assert_stack!(SAR(1, 4), 2); assert_stack!(SAR(256, neg(1)), neg(1)); } diff --git a/crates/evm2/src/interpreter/instructions/block.rs b/crates/evm2/src/interpreter/instructions/block.rs index dfb591888..fa8efd485 100644 --- a/crates/evm2/src/interpreter/instructions/block.rs +++ b/crates/evm2/src/interpreter/instructions/block.rs @@ -2,7 +2,7 @@ use crate::{ EvmFeatures, constants::BLOCK_HASH_HISTORY, interpreter::{Host, Word}, - utils::{WordExt, address_to_word, b256_to_word}, + utils::{WordExt, b256_to_word}, }; use evm2_macros::instruction; @@ -22,7 +22,7 @@ pub fn blockhash(cx: _, [number]: [Word]) -> Result { #[instruction] pub fn coinbase(cx: _) -> out { - out.write(address_to_word(&cx.state.host().block_env().beneficiary)); + out.write(cx.state.host().block_env().beneficiary.into_word().into()); } #[instruction] @@ -135,7 +135,7 @@ mod tests { #[test] fn coinbase_opcode() { - let beneficiary = Address::from([0x44; 20]); + let beneficiary = Address::repeat_byte(0x44); let mut host = test_host(BlockEnvExt { beneficiary, ..BlockEnvExt::default() }); let interp = run(RunConfig::new([op::COINBASE, op::STOP]).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); @@ -190,18 +190,18 @@ mod tests { #[test] fn chainid_opcode() { let mut host = TestHost::default(); - let tx_env = TxEnvExt { chain_id: Word::from(1), ..TxEnvExt::default() }; + let tx_env = TxEnvExt { chain_id: Word::ONE, ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::CHAINID, op::STOP]) .host(&mut host) .tx_env(tx_env) .spec(SpecId::ISTANBUL)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] fn selfbalance_opcode() { - let address = Address::from([0x66; 20]); + let address = Address::repeat_byte(0x66); let mut host = TestHost::default(); let message = MessageExt { destination: address, gas_limit: 10_000, ..Default::default() }; let interp = diff --git a/crates/evm2/src/interpreter/instructions/control.rs b/crates/evm2/src/interpreter/instructions/control.rs index 9d48878e9..24b800b9e 100644 --- a/crates/evm2/src/interpreter/instructions/control.rs +++ b/crates/evm2/src/interpreter/instructions/control.rs @@ -208,7 +208,7 @@ mod tests { let interp = run(RunConfig::new([op::JUMPDEST, op::PC, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] @@ -257,10 +257,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Return); assert_eq!(interp.output(), [0xab, 0, 0]); - let interp = run_stack([Word::from(0), Word::MAX], op::RETURN); + let interp = run_stack([Word::ZERO, Word::MAX], op::RETURN); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); - let interp = run_stack([Word::from(u32::MAX), Word::from(1)], op::RETURN); + let interp = run_stack([Word::from(u32::MAX), Word::ONE], op::RETURN); assert_matches!(interp.err, InstrStop::MemoryLimitOOG); } @@ -287,10 +287,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Revert); assert_eq!(interp.output(), [0xcd, 0]); - let interp = run_stack([Word::from(0), Word::MAX], op::REVERT); + let interp = run_stack([Word::ZERO, Word::MAX], op::REVERT); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); - let interp = run_stack([Word::from(u32::MAX), Word::from(1)], op::REVERT); + let interp = run_stack([Word::from(u32::MAX), Word::ONE], op::REVERT); assert_matches!(interp.err, InstrStop::MemoryLimitOOG); } } diff --git a/crates/evm2/src/interpreter/instructions/crypto.rs b/crates/evm2/src/interpreter/instructions/crypto.rs index 46145357c..aa20513b5 100644 --- a/crates/evm2/src/interpreter/instructions/crypto.rs +++ b/crates/evm2/src/interpreter/instructions/crypto.rs @@ -42,7 +42,7 @@ mod tests { push(&mut code, Word::from(0x80)); push(&mut code, 0); code.push(op::MSTORE8); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, 0); code.push(op::KECCAK256); code.push(op::STOP); @@ -50,11 +50,11 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [b256_to_word(keccak256([0x80]))]); - let interp = run_stack([Word::MAX, Word::from(0)], op::KECCAK256); + let interp = run_stack([Word::MAX, Word::ZERO], op::KECCAK256); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [b256_to_word(keccak256([]))]); - let interp = run_stack([Word::MAX, Word::from(1)], op::KECCAK256); + let interp = run_stack([Word::MAX, Word::ONE], op::KECCAK256); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } } diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index 055cd99cc..333eb7939 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -2,7 +2,7 @@ use crate::{ EvmTypesHost, evm::AccountLoad, interpreter::{Gas, Host, InstrStop, Memory, Result, Word, private::GasInstructionCx}, - utils::{WordExt, address_to_word, b256_to_word, read_word_padded}, + utils::{WordExt, b256_to_word, read_word_padded}, version::GasParams, }; use evm2_macros::instruction; @@ -46,7 +46,7 @@ fn copy_data( #[instruction] pub fn address(cx: _) -> out { - out.write(address_to_word(&cx.state.message().destination)); + out.write(cx.state.message().destination.into_word().into()); } #[instruction(dynamic_gas)] @@ -56,12 +56,12 @@ pub fn balance(cx: _, [addr]: [Word]) -> Result { #[instruction] pub fn origin(cx: _) -> out { - out.write(address_to_word(&cx.state.host().tx_env().origin)); + out.write(cx.state.host().tx_env().origin.into_word().into()); } #[instruction] pub fn caller(cx: _) -> out { - out.write(address_to_word(&cx.state.message().caller)); + out.write(cx.state.message().caller.into_word().into()); } #[instruction] @@ -180,7 +180,7 @@ mod tests { #[test] fn address_opcode() { - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut host = TestHost::default(); let message = MessageExt { destination: address, ..test_message() }; let interp = run(RunConfig::new([op::ADDRESS, op::STOP]).host(&mut host).message(message)); @@ -192,7 +192,7 @@ mod tests { fn balance_opcode() { assert_stack!(BALANCE(0xbeef), 0xbeef); assert_stack!(BALANCE(0), 0); - assert_stack!(BALANCE(neg(1)), address_to_word(&Address::from([0xff; 20]))); + assert_stack!(BALANCE(neg(1)), address_to_word(&Address::repeat_byte(0xff))); } #[test] @@ -218,7 +218,7 @@ mod tests { #[test] fn origin_opcode() { - let origin = Address::from([0x22; 20]); + let origin = Address::repeat_byte(0x22); let mut host = TestHost::default(); let tx_env = TxEnvExt { origin, ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::ORIGIN, op::STOP]).host(&mut host).tx_env(tx_env)); @@ -228,7 +228,7 @@ mod tests { #[test] fn caller_opcode() { - let caller = Address::from([0x33; 20]); + let caller = Address::repeat_byte(0x33); let mut host = TestHost::default(); let message = MessageExt { caller, ..test_message() }; let interp = run(RunConfig::new([op::CALLER, op::STOP]).host(&mut host).message(message)); @@ -298,12 +298,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); - let interp = run(RunConfig::new(stack_code( - [Word::MAX, Word::MAX, Word::from(0)], - op::CALLDATACOPY, - )) - .host(&mut host) - .message(message)); + let interp = + run(RunConfig::new(stack_code([Word::MAX, Word::MAX, Word::ZERO], op::CALLDATACOPY)) + .host(&mut host) + .message(message)); assert_matches!(interp.err, InstrStop::Stop); } @@ -315,7 +313,7 @@ mod tests { let interp = run(RunConfig::new([op::PUSH1, 0x00, op::CODESIZE, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(0), Word::from(4)]); + assert_eq!(interp.stack(), [Word::ZERO, Word::from(4)]); } #[test] @@ -336,7 +334,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let mut code = Vec::new(); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, Word::from(usize::MAX)); push(&mut code, 0); code.push(op::CODECOPY); @@ -347,10 +345,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [0]); - let interp = run_stack([Word::MAX, Word::MAX, Word::from(0)], op::CODECOPY); + let interp = run_stack([Word::MAX, Word::MAX, Word::ZERO], op::CODECOPY); assert_matches!(interp.err, InstrStop::Stop); - let interp = run_stack([Word::MAX, Word::from(0), Word::from(1)], op::CODECOPY); + let interp = run_stack([Word::MAX, Word::ZERO, Word::ONE], op::CODECOPY); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -408,14 +406,14 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let interp = run(RunConfig::new(stack_code( - [Word::from(0xbeef), Word::MAX, Word::MAX, Word::from(0)], + [Word::from(0xbeef), Word::MAX, Word::MAX, Word::ZERO], op::EXTCODECOPY, )) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); let interp = run(RunConfig::new(stack_code( - [Word::from(0xbeef), Word::MAX, Word::from(0), Word::from(1)], + [Word::from(0xbeef), Word::MAX, Word::ZERO, Word::ONE], op::EXTCODECOPY, )) .host(&mut host)); @@ -452,7 +450,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(3), Word::from(0)], + [Word::ZERO, Word::from(3), Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::BYZANTIUM) @@ -460,23 +458,21 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(4), Word::from(0)], + [Word::ZERO, Word::from(4), Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::BYZANTIUM) .return_data(Bytes::from_static(&[0xaa, 0xbb, 0xcc]))); assert_matches!(interp.err, InstrStop::OutOfOffset); - let interp = run(RunConfig::new(stack_code( - [Word::MAX, Word::from(0), Word::from(1)], - op::RETURNDATACOPY, - )) - .spec(SpecId::BYZANTIUM) - .return_data(Bytes::from_static(&[0xaa]))); + let interp = + run(RunConfig::new(stack_code([Word::MAX, Word::ZERO, Word::ONE], op::RETURNDATACOPY)) + .spec(SpecId::BYZANTIUM) + .return_data(Bytes::from_static(&[0xaa]))); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(0), Word::from(0)], + [Word::ZERO, Word::ZERO, Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::FRONTIER)); diff --git a/crates/evm2/src/interpreter/instructions/host.rs b/crates/evm2/src/interpreter/instructions/host.rs index 495e54fce..506ab3218 100644 --- a/crates/evm2/src/interpreter/instructions/host.rs +++ b/crates/evm2/src/interpreter/instructions/host.rs @@ -143,7 +143,7 @@ fn log_common( let mut topics = [B256::ZERO; 4]; for (topic, word) in topics.iter_mut().zip(stack.popn_dyn(n)?) { - *topic = B256::from(word.to_be_bytes::<32>()); + *topic = B256::from(word); } let destination = &cx.state.message().destination; let (memory, host) = cx.state.0.memory_host(); @@ -171,7 +171,7 @@ mod tests { #[test] fn sload_opcode() { let mut host = TestHost::default(); - host.storage.insert(StorageKey::new(Address::ZERO, Word::from(1)), Word::from(0xbeef)); + host.storage.insert(StorageKey::new(Address::ZERO, Word::ONE), Word::from(0xbeef)); let mut code = Vec::new(); push(&mut code, 1); @@ -202,7 +202,7 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from(0xbeef)]); assert_eq!( - host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), + host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), Some(&Word::from(0xbeef)) ); } @@ -218,7 +218,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).staticcall()); assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -235,7 +235,7 @@ mod tests { assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -249,7 +249,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).spec(SpecId::ISTANBUL).gas_limit(2306)); assert_matches!(interp.err, InstrStop::ReentrancySentryOOG); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -297,7 +297,7 @@ mod tests { #[test] fn sstore_reset_to_original_records_refund() { let mut host = TestHost::default(); - host.storage.insert(StorageKey::new(Address::ZERO, Word::from(0)), Word::from(5)); + host.storage.insert(StorageKey::new(Address::ZERO, Word::ZERO), Word::from(5)); let mut code = Vec::new(); push(&mut code, 7); push(&mut code, 0); @@ -334,7 +334,7 @@ mod tests { fn tload_opcode() { let mut host = TestHost::default(); host.transient_storage - .insert(StorageKey::new(Address::ZERO, Word::from(1)), Word::from(0xcafe)); + .insert(StorageKey::new(Address::ZERO, Word::ONE), Word::from(0xcafe)); let mut code = Vec::new(); push(&mut code, 1); @@ -364,7 +364,7 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from(0xcafe)]); assert_eq!( - host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), + host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), Some(&Word::from(0xcafe)) ); @@ -386,10 +386,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).spec(SpecId::CANCUN).staticcall()); assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!( - host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), - None - ); + assert_eq!(host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } fn log_code(offset: usize, len: usize, topics: [Word; N]) -> Vec { @@ -410,7 +407,7 @@ mod tests { #[test] fn log0_opcode() { let mut host = TestHost::default(); - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let message = MessageExt { destination: address, gas_limit: 10_000, ..Default::default() }; let interp = run(RunConfig::new(log_code(30, 2, [])).host(&mut host).message(message)); assert_matches!(interp.err, InstrStop::Stop); @@ -424,9 +421,9 @@ mod tests { #[test] fn log1_opcode() { let mut host = TestHost::default(); - let interp = run(RunConfig::new(log_code(30, 2, [Word::from(1)])).host(&mut host)); + let interp = run(RunConfig::new(log_code(30, 2, [Word::ONE])).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(host.logs[0].topics(), &[B256::from(Word::from(1).to_be_bytes::<32>())]); + assert_eq!(host.logs[0].topics(), &[B256::with_last_byte(1)]); assert_eq!(host.logs[0].data.data, Bytes::from_static(&[0xbe, 0xef])); } @@ -434,15 +431,9 @@ mod tests { fn log2_opcode() { let mut host = TestHost::default(); let interp = - run(RunConfig::new(log_code(30, 0, [Word::from(1), Word::from(2)])).host(&mut host)); + run(RunConfig::new(log_code(30, 0, [Word::ONE, Word::from(2)])).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!( - host.logs[0].topics(), - &[ - B256::from(Word::from(1).to_be_bytes::<32>()), - B256::from(Word::from(2).to_be_bytes::<32>()), - ] - ); + assert_eq!(host.logs[0].topics(), &[B256::with_last_byte(1), B256::with_last_byte(2),]); assert!(host.logs[0].data.data.is_empty()); } @@ -450,7 +441,7 @@ mod tests { fn log3_opcode() { let mut host = TestHost::default(); let interp = - run(RunConfig::new(log_code(30, 1, [Word::from(1), Word::from(2), Word::from(3)])) + run(RunConfig::new(log_code(30, 1, [Word::ONE, Word::from(2), Word::from(3)])) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(host.logs[0].topics().len(), 3); @@ -463,7 +454,7 @@ mod tests { let interp = run(RunConfig::new(log_code( 30, 1, - [Word::from(1), Word::from(2), Word::from(3), Word::from(4)], + [Word::ONE, Word::from(2), Word::from(3), Word::from(4)], )) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); diff --git a/crates/evm2/src/interpreter/instructions/memory.rs b/crates/evm2/src/interpreter/instructions/memory.rs index f6131e490..379bdbe13 100644 --- a/crates/evm2/src/interpreter/instructions/memory.rs +++ b/crates/evm2/src/interpreter/instructions/memory.rs @@ -85,7 +85,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from(64)]); assert_eq!(interp.memory(38, 2), [0xfe, 0xed]); - let interp = run_stack([Word::MAX, Word::from(0)], op::MSTORE); + let interp = run_stack([Word::MAX, Word::ZERO], op::MSTORE); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -146,7 +146,7 @@ mod tests { assert_eq!(interp.memory(4, 1), [0xab]); assert_eq!(interp.stack()[0] >> 248, Word::from(0xab)); - let interp = run_stack([Word::MAX, Word::from(0)], op::MSTORE8); + let interp = run_stack([Word::MAX, Word::ZERO], op::MSTORE8); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -188,7 +188,7 @@ mod tests { let mut code = Vec::new(); push(&mut code, 0); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, 0); code.push(op::MCOPY); code.push(op::MSIZE); @@ -197,10 +197,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [0]); - let interp = run_stack([Word::MAX, Word::MAX, Word::from(0)], op::MCOPY); + let interp = run_stack([Word::MAX, Word::MAX, Word::ZERO], op::MCOPY); assert_matches!(interp.err, InstrStop::Stop); - let interp = run_stack([Word::MAX, Word::from(0), Word::from(1)], op::MCOPY); + let interp = run_stack([Word::MAX, Word::ZERO, Word::ONE], op::MCOPY); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index 227a6f4b0..5e579df78 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -76,7 +76,7 @@ mod tests { let interp = run(RunConfig::new([op::PUSH1, 0x01, op::PUSH1, 0x02, op::POP, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] @@ -291,8 +291,8 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 18); - assert_eq!(interp.stack()[17], Word::from(1)); - assert_eq!(interp.stack()[0], Word::from(1)); + assert_eq!(interp.stack()[17], Word::ONE); + assert_eq!(interp.stack()[0], Word::ONE); for i in 1..17 { assert_eq!(interp.stack()[i], 0); } @@ -305,7 +305,7 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 146); - assert_eq!(interp.stack()[145], Word::from(1)); + assert_eq!(interp.stack()[145], Word::ONE); } #[test] @@ -316,7 +316,7 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 18); - assert_eq!(interp.stack()[17], Word::from(1)); + assert_eq!(interp.stack()[17], Word::ONE); assert_eq!(interp.stack()[0], Word::from(2)); for i in 1..17 { assert_eq!(interp.stack()[i], 0); @@ -348,7 +348,7 @@ mod tests { ]) .spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1), Word::from(0), Word::from(2)]); + assert_eq!(interp.stack(), [Word::ONE, Word::ZERO, Word::from(2)]); let mut code = Vec::new(); for value in 0..23 { diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index c24277192..183e3c090 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -161,7 +161,7 @@ fn prepare_call( return Err(InstrStop::CallNotAllowedInsideStatic); } - let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); + let local_gas_limit = local_gas_limit.saturating_to::(); let (input_range, prepared_return_memory_range) = get_memory_input_and_out_ranges( gas, state, @@ -382,7 +382,7 @@ pub fn create_with( let current = state.message(); let caller = current.destination; let depth = current.depth.saturating_add(1); - let salt = salt.map(|salt| B256::from(salt.to_be_bytes())).unwrap_or_default(); + let salt = salt.map(B256::from).unwrap_or_default(); // Derive the created contract address once. CREATE needs the creator's (pre-bump) nonce; the // caller is the currently-executing contract, already warm and in the access list, so this read @@ -541,18 +541,18 @@ mod tests { #[test] fn call_opcode() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, address_to_word(&target), Word::from(1000), ], @@ -565,7 +565,7 @@ mod tests { ..Default::default() })); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls.len(), 1); assert_eq!(host.calls[0].kind, MessageKind::Call); assert_eq!(host.calls[0].destination, target); @@ -574,7 +574,7 @@ mod tests { #[test] fn call_propagates_fatal_child_result() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let error = ExecutionError::Fatal("child call failed".into()); let mut host = TestHost { execute_error: Some(error.clone()), ..Default::default() }; let mut code = crate::test_utils::call_code(target); @@ -589,16 +589,16 @@ mod tests { #[test] fn call_propagates_static_flag() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, Word::ZERO, address_to_word(&target), Word::from(1000), @@ -615,7 +615,7 @@ mod tests { #[test] fn call_too_deep_charges_dynamic_gas() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { is_cold: true, is_empty: true, ..Default::default() }; let mut code = Vec::new(); push_all( @@ -625,7 +625,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - Word::from(1), + Word::ONE, address_to_word(&target), Word::from(1000), ], @@ -678,7 +678,7 @@ mod tests { #[test] fn call_too_deep_charges_pre_spurious_empty_account() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { exists: false, is_empty: true, ..Default::default() }; let mut code = Vec::new(); push_all( @@ -708,7 +708,7 @@ mod tests { #[test] fn call_too_deep_skips_pre_spurious_touched_empty_account_cost() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { exists: false, is_empty: true, is_touched: true, ..Default::default() }; let mut code = Vec::new(); @@ -739,16 +739,16 @@ mod tests { #[test] fn callcode_propagates_static_flag_and_allows_apparent_value() { - let code_address = Address::from([0x33; 20]); + let code_address = Address::repeat_byte(0x33); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, Word::from(7), address_to_word(&code_address), Word::from(1000), @@ -766,17 +766,17 @@ mod tests { #[test] fn callcode_opcode() { - let code_address = Address::from([0x33; 20]); - let destination = Address::from([0x11; 20]); + let code_address = Address::repeat_byte(0x33); + let destination = Address::repeat_byte(0x11); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, Word::from(7), address_to_word(&code_address), Word::from(1000), @@ -789,7 +789,7 @@ mod tests { .message(MessageExt { destination, ..Default::default() }) .gas_limit(20_000)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::CallCode); assert_eq!(host.calls[0].destination, destination); assert_eq!(host.calls[0].code_address, code_address); @@ -798,17 +798,17 @@ mod tests { #[test] fn delegatecall_opcode() { - let code_address = Address::from([0x44; 20]); - let caller = Address::from([0x55; 20]); + let code_address = Address::repeat_byte(0x44); + let caller = Address::repeat_byte(0x55); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, address_to_word(&code_address), Word::from(1000), ], @@ -822,7 +822,7 @@ mod tests { ..Default::default() })); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::DelegateCall); assert_eq!(host.calls[0].caller, caller); assert_eq!(host.calls[0].value, Word::from(9)); @@ -849,16 +849,16 @@ mod tests { #[test] fn staticcall_opcode() { - let target = Address::from([0x66; 20]); + let target = Address::repeat_byte(0x66); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, address_to_word(&target), Word::from(1000), ], @@ -867,7 +867,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::StaticCall); assert!(host.call_static_flags[0]); @@ -892,7 +892,7 @@ mod tests { #[test] fn create_opcode() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -902,7 +902,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -928,7 +928,7 @@ mod tests { #[test] fn create_clears_return_data_on_success() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -939,7 +939,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -958,7 +958,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -987,7 +987,7 @@ mod tests { fn create_too_deep_clears_return_data_and_skips_destination_read_eip8037() { // EIP-8037: a create failing the depth pre-check must not access the destination — // the read would leak the address into the EIP-7928 block access list. - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let mut host = TestHost { exists: false, ..Default::default() }; let mut code = create_code(); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); @@ -1022,7 +1022,7 @@ mod tests { #[test] fn create2_opcode() { - let created = Address::from([0x88; 20]); + let created = Address::repeat_byte(0x88); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -1032,7 +1032,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE2, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -1057,8 +1057,8 @@ mod tests { #[test] fn selfdestruct_opcode() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let mut host = TestHost::default(); let mut code = Vec::new(); push(&mut code, address_to_word(&target)); @@ -1157,7 +1157,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::from(value), - address_to_word(&Address::from([0x22; 20])), + address_to_word(&Address::repeat_byte(0x22)), Word::from(5_000), ], ); @@ -1177,7 +1177,7 @@ mod tests { assert_matches!(base.err, InstrStop::Stop); assert_matches!(hooked.err, InstrStop::Stop); - assert_eq!(hooked.stack(), [Word::from(1)]); + assert_eq!(hooked.stack(), [Word::ONE]); assert_eq!(host.calls.len(), 1); assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); @@ -1219,7 +1219,7 @@ mod tests { #[test] fn create_with_hooks_adjust_child_and_caller_gas() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let execute_result = MessageResultExt { stop: InstrStop::Return, created_address: Some(created), diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index e0e0ae1e3..4fd0d7fe1 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -836,8 +836,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline] pub(crate) fn inspect_step(&mut self, pc: Pc, stack_len: usize) { - self.0.pc = pc.as_ptr(); - self.0.stack_len = stack_len; + self.set_pc_stack_len(pc.as_ptr(), stack_len); unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step(&mut self.0); @@ -846,8 +845,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline] pub(crate) fn inspect_step_end(&mut self, pc: Pc, stack_len: usize) { - self.0.pc = pc.as_ptr(); - self.0.stack_len = stack_len; + self.set_pc_stack_len(pc.as_ptr(), stack_len); let reload = self.0.result == Err(InstrStop::Reload); if reload { cold_path(); diff --git a/crates/evm2/src/interpreter/stack.rs b/crates/evm2/src/interpreter/stack.rs index 83279501a..5289b4b37 100644 --- a/crates/evm2/src/interpreter/stack.rs +++ b/crates/evm2/src/interpreter/stack.rs @@ -252,8 +252,8 @@ impl<'a> StackMut<'a> { /// The caller must keep the length at or below [`STACK_LIMIT`] and ensure the first `len` /// words are initialized before creating another stack view. #[inline] - pub const unsafe fn into_raw_parts(self) -> (*mut Word, &'a mut usize) { - (self.stack.0.as_mut_ptr().cast(), self.len) + pub const unsafe fn into_raw_parts(mut self) -> (*mut Word, &'a mut usize) { + (self.as_word_mut_ptr(), self.len) } #[inline] @@ -645,11 +645,11 @@ mod tests { #[test] fn push_and_pop() { run(|stack| { - stack.push(Word::from(1)).unwrap(); + stack.push(Word::ONE).unwrap(); stack.push(Word::from(2)).unwrap(); - assert_eq!(stack.as_slice(), [Word::from(1), Word::from(2)]); + assert_eq!(stack.as_slice(), [Word::ONE, Word::from(2)]); assert_eq!(stack.pop().unwrap(), Word::from(2)); - assert_eq!(stack.popn::<1>().unwrap(), [Word::from(1)]); + assert_eq!(stack.popn::<1>().unwrap(), [Word::ONE]); assert_matches!(stack.pop(), Err(InstrStop::StackUnderflow)); }); @@ -662,8 +662,8 @@ mod tests { fn popn_top() { run_with_len(3, |stack| { let (popped, top) = stack.popn_top::<2>().unwrap(); - assert_eq!(popped, [Word::from(2), Word::from(1)]); - assert_eq!(*top, Word::from(0)); + assert_eq!(popped, [Word::from(2), Word::ONE]); + assert_eq!(*top, Word::ZERO); *top = Word::from(9); assert_eq!(stack.as_slice(), [Word::from(9)]); }); @@ -677,13 +677,13 @@ mod tests { fn popn_dyn() { run_with_len(4, |stack| { let popped = stack.popn_dyn(3).unwrap().collect::>(); - assert_eq!(popped, [Word::from(3), Word::from(2), Word::from(1)]); - assert_eq!(stack.as_slice(), [Word::from(0)]); + assert_eq!(popped, [Word::from(3), Word::from(2), Word::ONE]); + assert_eq!(stack.as_slice(), [Word::ZERO]); }); run_with_len(2, |stack| { assert_matches!(stack.popn_dyn(3).map(|_| ()), Err(InstrStop::StackUnderflow)); - assert_eq!(stack.as_slice(), [Word::from(0), Word::from(1)]); + assert_eq!(stack.as_slice(), [Word::ZERO, Word::ONE]); }); } @@ -691,10 +691,10 @@ mod tests { fn peekn() { run_with_len(3, |stack| { assert_eq!(stack.peek(0), Some(Word::from(2))); - assert_eq!(stack.peek(1), Some(Word::from(1))); - assert_eq!(stack.peekn::<2>(), Some([Word::from(2), Word::from(1)])); + assert_eq!(stack.peek(1), Some(Word::ONE)); + assert_eq!(stack.peekn::<2>(), Some([Word::from(2), Word::ONE])); assert_eq!(stack.peekn::<4>(), None); - assert_eq!(stack.as_slice(), [Word::from(0), Word::from(1), Word::from(2)]); + assert_eq!(stack.as_slice(), [Word::ZERO, Word::ONE, Word::from(2)]); }); let mut backing = StackBacking([MaybeUninit::new(Word::ZERO); StackRef::CAPACITY]); @@ -702,7 +702,7 @@ mod tests { word.write(Word::from(i)); } let stack = StackRef::new(&backing, 3); - assert_eq!(stack.peekn::<3>(), Some([Word::from(2), Word::from(1), Word::from(0)])); + assert_eq!(stack.peekn::<3>(), Some([Word::from(2), Word::ONE, Word::ZERO])); assert_eq!(stack.peekn::<4>(), None); } @@ -728,19 +728,19 @@ mod tests { stack.dup(2).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(0), Word::from(1), Word::from(2), Word::from(3), Word::from(2)] + [Word::ZERO, Word::ONE, Word::from(2), Word::from(3), Word::from(2)] ); stack.swap(3).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(0), Word::from(2), Word::from(2), Word::from(3), Word::from(1)] + [Word::ZERO, Word::from(2), Word::from(2), Word::from(3), Word::ONE] ); stack.exchange(1, 4).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(3), Word::from(2), Word::from(2), Word::from(0), Word::from(1)] + [Word::from(3), Word::from(2), Word::from(2), Word::ZERO, Word::ONE] ); }); diff --git a/crates/evm2/src/precompiles/bls12_381/g1_msm.rs b/crates/evm2/src/precompiles/bls12_381/g1_msm.rs index b59b145aa..ad5905314 100644 --- a/crates/evm2/src/precompiles/bls12_381/g1_msm.rs +++ b/crates/evm2/src/precompiles/bls12_381/g1_msm.rs @@ -57,13 +57,13 @@ mod test { use super::*; use crate::precompiles::kzg_point_evaluation::RETURN_VALUE; use alloc::vec::Vec; - use alloy_primitives::{Bytes, U256, hex}; + use alloy_primitives::{U256, bytes, hex}; #[test] fn bls_g1multiexp_g1_not_on_curve_but_in_subgroup() { - let input = Bytes::from(hex!( + let input = bytes!( "000000000000000000000000000000000a2833e497b38ee3ca5c62828bf4887a9f940c9e426c7890a759c20f248c23a7210d2432f4c98a514e524b5184a0ddac00000000000000000000000000000000150772d56bf9509469f9ebcd6e47570429fd31b0e262b66d512e245c38ec37255529f2271fd70066473e393a8bead0c30000000000000000000000000000000000000000000000000000000000000000" - )); + ); let fail = run(&input, &mut GasTracker::new(G1_MSM_BASE_GAS_FEE)); assert_eq!( fail.err().and_then(|e| e.as_halt().cloned()), @@ -100,14 +100,14 @@ mod test { let scalars = [ U256::ZERO, - U256::from(1), + U256::ONE, U256::from(2), - r - U256::from(1), + r - U256::ONE, r, - r + U256::from(1), + r + U256::ONE, r * U256::from(2), - r * U256::from(2) + U256::from(1), - U256::from(1) << 255, + r * U256::from(2) + U256::ONE, + U256::ONE << 255, U256::MAX, ]; for scalar in scalars { @@ -127,21 +127,15 @@ mod test { (&non_subgroup, PrecompileHalt::Bls12381G1NotInSubgroup), ] { assert_eq!(msm(&[(bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, U256::from(1)), (bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, scalar), (bad, U256::from(1))]), Err(Some(err))); + assert_eq!(msm(&[(&point, U256::ONE), (bad, scalar)]), Err(Some(err.clone()))); + assert_eq!(msm(&[(&point, scalar), (bad, U256::ONE)]), Err(Some(err))); } } - assert_eq!(msm(&[(&point, U256::from(1))]), Ok(point.to_vec())); + assert_eq!(msm(&[(&point, U256::ONE)]), Ok(point.to_vec())); assert_eq!(msm(&[(&point, r), (&point, r * U256::from(2))]), Ok(infinity.to_vec())); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(1))]), - Ok(infinity.to_vec()) - ); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(2))]), - Ok(point.to_vec()) - ); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::ONE)]), Ok(infinity.to_vec())); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::from(2))]), Ok(point.to_vec())); // Many pairs with scalars just below `r` (bit 254 set) sum to `-n(n+1)/2 * P`. let pairs = (0..64u64).map(|i| (&point, r - U256::from(1 + i))).collect::>(); diff --git a/crates/evm2/src/precompiles/bls12_381/g2_msm.rs b/crates/evm2/src/precompiles/bls12_381/g2_msm.rs index a219bcc4b..1e1d8514a 100644 --- a/crates/evm2/src/precompiles/bls12_381/g2_msm.rs +++ b/crates/evm2/src/precompiles/bls12_381/g2_msm.rs @@ -84,14 +84,14 @@ mod test { let scalars = [ U256::ZERO, - U256::from(1), + U256::ONE, U256::from(2), - r - U256::from(1), + r - U256::ONE, r, - r + U256::from(1), + r + U256::ONE, r * U256::from(2), - r * U256::from(2) + U256::from(1), - U256::from(1) << 255, + r * U256::from(2) + U256::ONE, + U256::ONE << 255, U256::MAX, ]; for scalar in scalars { @@ -111,21 +111,15 @@ mod test { (&non_subgroup, PrecompileHalt::Bls12381G2NotInSubgroup), ] { assert_eq!(msm(&[(bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, U256::from(1)), (bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, scalar), (bad, U256::from(1))]), Err(Some(err))); + assert_eq!(msm(&[(&point, U256::ONE), (bad, scalar)]), Err(Some(err.clone()))); + assert_eq!(msm(&[(&point, scalar), (bad, U256::ONE)]), Err(Some(err))); } } - assert_eq!(msm(&[(&point, U256::from(1))]), Ok(point.to_vec())); + assert_eq!(msm(&[(&point, U256::ONE)]), Ok(point.to_vec())); assert_eq!(msm(&[(&point, r), (&point, r * U256::from(2))]), Ok(infinity.to_vec())); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(1))]), - Ok(infinity.to_vec()) - ); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(2))]), - Ok(point.to_vec()) - ); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::ONE)]), Ok(infinity.to_vec())); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::from(2))]), Ok(point.to_vec())); // Many pairs with scalars just below `r` (bit 254 set) sum to `-n(n+1)/2 * P`. let pairs = (0..64u64).map(|i| (&point, r - U256::from(1 + i))).collect::>(); diff --git a/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs b/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs index 53c0ca150..d1435bf35 100644 --- a/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs +++ b/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs @@ -32,13 +32,13 @@ pub fn run(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { #[cfg(test)] mod test { use super::*; - use alloy_primitives::{Bytes, hex}; + use alloy_primitives::bytes; #[test] fn sanity_test() { - let input = Bytes::from(hex!( + let input = bytes!( "000000000000000000000000000000006900000000000000636f6e7472616374595a603f343061cd305a03f40239f5ffff31818185c136bc2595f2aa18e08f17" - )); + ); let fail = run(&input, &mut GasTracker::new(MAP_FP_TO_G1_BASE_GAS_FEE)); assert_eq!( fail.err().and_then(|e| e.as_halt().cloned()), diff --git a/crates/evm2/src/precompiles/bn254/mod.rs b/crates/evm2/src/precompiles/bn254/mod.rs index cebe3c8b2..9d29fd4eb 100644 --- a/crates/evm2/src/precompiles/bn254/mod.rs +++ b/crates/evm2/src/precompiles/bn254/mod.rs @@ -262,7 +262,7 @@ mod tests { pair::{BYZANTIUM_PAIR_BASE, BYZANTIUM_PAIR_PER_POINT}, }, }; - use alloy_primitives::hex; + use alloy_primitives::{B256, hex}; use core::assert_matches; use super::*; @@ -630,8 +630,7 @@ mod tests { &mut GasTracker::new(260_000), ) }; - let mut one = [0u8; 32]; - one[31] = 1; + let one: [u8; 32] = B256::with_last_byte(1).into(); assert_eq!(pair_infinity_with(SUBGROUP_G2).unwrap().bytes(), &one); for g2 in TORSION_G2 { assert_matches!( diff --git a/crates/evm2/src/precompiles/hash.rs b/crates/evm2/src/precompiles/hash.rs index 9bcb229cc..58160da07 100644 --- a/crates/evm2/src/precompiles/hash.rs +++ b/crates/evm2/src/precompiles/hash.rs @@ -17,7 +17,7 @@ pub fn run_sha256(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let cost = calc_linear_cost(input.len(), 60, 12); gas.spend(cost)?; let output = crate::precompiles::crypto().sha256(input); - Ok(PrecompileOutput::new(output.to_vec().into())) + Ok(PrecompileOutput::new(output.into())) } /// Computes the RIPEMD-160 hash of the input data @@ -30,5 +30,5 @@ pub fn run_ripemd160(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let gas_used = calc_linear_cost(input.len(), 600, 120); gas.spend(gas_used)?; let output = crate::precompiles::crypto().ripemd160(input); - Ok(PrecompileOutput::new(output.to_vec().into())) + Ok(PrecompileOutput::new(output.into())) } diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index bcb7caaa4..797d5c0b5 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -208,7 +208,7 @@ pub fn run_inner( // Lengths that do not fit in 64 bits cost more gas than any call has. let [base_len64, exp_len64, mod_len64] = - [base_len, exp_len, mod_len].map(|len| u64::try_from(len).unwrap_or(u64::MAX)); + [base_len, exp_len, mod_len].map(|len| len.saturating_to::()); let [base_len, exp_len, mod_len] = [base_len64, exp_len64, mod_len64].map(|len| usize::try_from(len).unwrap_or(usize::MAX)); @@ -635,8 +635,8 @@ mod tests { expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { - base_len: U256::from(0), - exp_len: U256::from(0), + base_len: U256::ZERO, + exp_len: U256::ZERO, mod_len: U256::from(1025), gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), @@ -649,30 +649,30 @@ mod tests { expected: Some(PrecompileHalt::OutOfGas), }, TestInput { - base_len: U256::from(1) << 64, - exp_len: U256::from(1), - mod_len: U256::from(1), + base_len: U256::ONE << 64, + exp_len: U256::ONE, + mod_len: U256::ONE, gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { - base_len: U256::from(1), - exp_len: U256::from(1) << 64, - mod_len: U256::from(1), + base_len: U256::ONE, + exp_len: U256::ONE << 64, + mod_len: U256::ONE, gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { - base_len: U256::from(1), - exp_len: U256::from(1), - mod_len: U256::from(1) << 64, + base_len: U256::ONE, + exp_len: U256::ONE, + mod_len: U256::ONE << 64, gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { - base_len: U256::from(0), - exp_len: U256::from(0), - mod_len: U256::from(0), + base_len: U256::ZERO, + exp_len: U256::ZERO, + mod_len: U256::ZERO, gas: 100_000_000, expected: None, }, @@ -705,14 +705,14 @@ mod tests { }; // A length that does not fit in 64 bits costs more than any call can pay, on every fork. for position in 0..3 { - let mut lengths = [U256::from(1); 3]; - lengths[position] = U256::from(1) << 64; + let mut lengths = [U256::ONE; 3]; + lengths[position] = U256::ONE << 64; for run in [run_byzantium, run_berlin, run_osaka] { assert_eq!(halt(&input(lengths), run), PrecompileHalt::OutOfGas, "{position}"); } } // An Osaka length over the limit is out of gas when the gas does not cover its price. - let lengths = [U256::from(65_536), U256::from(1), U256::from(1)]; + let lengths = [U256::from(65_536), U256::ONE, U256::ONE]; assert_eq!(halt(&input(lengths), run_osaka), PrecompileHalt::OutOfGas); } diff --git a/crates/evm2/src/precompiles/secp256k1/mod.rs b/crates/evm2/src/precompiles/secp256k1/mod.rs index 7504fcac8..1cf4a4f87 100644 --- a/crates/evm2/src/precompiles/secp256k1/mod.rs +++ b/crates/evm2/src/precompiles/secp256k1/mod.rs @@ -46,7 +46,7 @@ pub fn run(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let sig = input.last_chunk().unwrap(); let res = crate::precompiles::crypto().secp256k1_ecrecover(sig, recid, msg).ok(); - let out = res.map(|o| o.to_vec().into()).unwrap_or_default(); + let out = res.map(|o| o.into()).unwrap_or_default(); Ok(PrecompileOutput::new(out)) } diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 37dd83c3a..39e57da80 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -16,7 +16,7 @@ use crate::{ use alloc::{vec, vec::Vec}; use alloy_consensus::{TxEip1559, TxEip2930, TxEip7702, TxLegacy, transaction::Recovered}; use alloy_eips::eip7702::{Authorization, SignedAuthorization}; -use alloy_primitives::{Address, Log, TxKind, U256, hex}; +use alloy_primitives::{Address, Log, TxKind, U256, bytes, hex}; type TestEvm = Evm<'static, BaseEvmTypes>; @@ -34,7 +34,7 @@ fn run_tx(evm: &mut TestEvm, destination: Address, code: impl Into>) { #[test] fn evm_executes_storage_transaction() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -52,18 +52,15 @@ fn evm_executes_storage_transaction() { run_tx(&mut evm, contract, [op::PUSH1, 0x2a, op::PUSH1, 0x01, op::SSTORE, op::STOP]); - assert_eq!( - evm.state.storage_slot(&contract, Word::from(1)).unwrap().current(), - Word::from(0x2a) - ); + assert_eq!(evm.state.storage_slot(&contract, Word::ONE).unwrap().current(), Word::from(0x2a)); } #[test] fn evm_runs_transactions_against_initial_state() { - let contract = Address::from([0x22; 20]); + let contract = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo { nonce: 1, ..Default::default() }); - database.insert_account_storage(&contract, &Word::from(1), &Word::from(40)); + database.insert_account_storage(&contract, &Word::ONE, &Word::from(40)); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -90,7 +87,7 @@ fn evm_runs_transactions_against_initial_state() { ); run_tx(&mut evm, contract, [op::PUSH1, 0x07, op::PUSH1, 0x01, op::SSTORE, op::STOP]); - assert_eq!(evm.state.storage_slot(&contract, Word::from(1)).unwrap().current(), Word::from(7)); + assert_eq!(evm.state.storage_slot(&contract, Word::ONE).unwrap().current(), Word::from(7)); assert_eq!(evm.state.storage_slot(&contract, Word::from(2)).unwrap().current(), Word::from(42)); } @@ -107,7 +104,7 @@ fn evm_propagates_child_sstore_negative_refund() { &contract, AccountInfo { nonce: 1, code: Some(legacy_bytecode(child_code)), ..Default::default() }, ); - database.insert_account_storage(&contract, &Word::from(0), &Word::from(5)); + database.insert_account_storage(&contract, &Word::ZERO, &Word::from(5)); let mut evm = TestEvm::new( SpecId::LONDON, BlockEnvExt::default(), @@ -144,7 +141,7 @@ fn evm_propagates_child_sstore_negative_refund() { #[test] fn evm_reports_invalid_transaction_execution() { - let contract = Address::from([0x33; 20]); + let contract = Address::repeat_byte(0x33); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -174,10 +171,10 @@ fn eip8037_sibling_refill_restores_parent_gas_left() { use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{TxKind, U256}; - let parent = Address::from([0x11; 20]); - let setter = Address::from([0xa1; 20]); - let clearer = Address::from([0xb2; 20]); - let caller = Address::from([0xcc; 20]); + let parent = Address::repeat_byte(0x11); + let setter = Address::repeat_byte(0xa1); + let clearer = Address::repeat_byte(0xb2); + let caller = Address::repeat_byte(0xcc); let mut parent_code = Vec::new(); for target in [setter, clearer] { // DELEGATECALL(gas, target, 0, 0, 0, 0). @@ -249,10 +246,10 @@ fn eip8037_sibling_refill_restores_parent_gas_left() { #[test] fn eip2780_authorization_charges_no_state_gas_without_eip8037() { - let caller = Address::from([0xbb; 20]); - let recipient = Address::from([0xaa; 20]); - let authority = Address::from([0xcc; 20]); - let delegate = Address::from([0xdd; 20]); + let caller = Address::repeat_byte(0xbb); + let recipient = Address::repeat_byte(0xaa); + let authority = Address::repeat_byte(0xcc); + let delegate = Address::repeat_byte(0xdd); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -504,7 +501,7 @@ fn log_collection_off_keeps_log_semantics() { return_top_word(&mut logs); let mut memory_oog = Vec::new(); - push_all(&mut memory_oog, [Word::from(1), Word::MAX]); + push_all(&mut memory_oog, [Word::ONE, Word::MAX]); memory_oog.push(op::LOG0); let mut underflow = Vec::new(); @@ -651,7 +648,7 @@ fn precompile_dispatch_uses_original_callee( caller, 0, TxKind::Call(target), - &hex!("01020304").into(), + &bytes!("01020304"), U256::ZERO, &mut gas, ) diff --git a/crates/evm2/src/utils.rs b/crates/evm2/src/utils.rs index 024c3770b..39d97695a 100644 --- a/crates/evm2/src/utils.rs +++ b/crates/evm2/src/utils.rs @@ -2,7 +2,7 @@ use crate::interpreter::{InstrStop, Result, Word}; use alloc::{borrow::Cow, vec}; -use alloy_primitives::{Address, B256, Bytes, b256}; +use alloy_primitives::{Address, B256, Bytes}; use core::hint::cold_path; /// Returns the number of EVM words needed for `len` bytes. @@ -14,7 +14,7 @@ pub const fn num_words(len: usize) -> usize { /// Converts an address to an EVM word. #[inline] pub fn address_to_word(address: &Address) -> Word { - Word::from_be_slice(address.as_slice()) + address.into_word().into() } /// Converts a 256-bit hash to an EVM word. @@ -287,10 +287,8 @@ pub const fn bool_to_bytes32(value: bool) -> Bytes { /// This is optimized to not allocate at runtime by using 2 static arrays. #[inline] const fn bool_to_b256(value: bool) -> &'static B256 { - const TRUE: &B256 = - &b256!("0x0000000000000000000000000000000000000000000000000000000000000001"); - const FALSE: &B256 = - &b256!("0x0000000000000000000000000000000000000000000000000000000000000000"); + const TRUE: &B256 = &B256::with_last_byte(1); + const FALSE: &B256 = &B256::ZERO; if value { TRUE } else { FALSE } } diff --git a/crates/evm2/tests/eip7702_recovery_crypto.rs b/crates/evm2/tests/eip7702_recovery_crypto.rs index 9e19d5ec6..b1be05d80 100644 --- a/crates/evm2/tests/eip7702_recovery_crypto.rs +++ b/crates/evm2/tests/eip7702_recovery_crypto.rs @@ -31,7 +31,7 @@ fn recovery_ignores_crypto_provider() { let key = SigningKey::from_slice(&[0x11; 32]).unwrap(); let signer = Address::from_raw_public_key(&key.verifying_key().to_sec1_point(false).as_bytes()[1..]); - let inner = Authorization { chain_id: U256::from(1), address: Address::ZERO, nonce: 7 }; + let inner = Authorization { chain_id: U256::ONE, address: Address::ZERO, nonce: 7 }; let (sig, recid) = key.sign_prehash_recoverable(inner.signature_hash().as_slice()); let r = U256::from_be_slice(&sig.r().to_bytes()); let s = U256::from_be_slice(&sig.s().to_bytes()); diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 07b37f22f..8d254c2ac 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -128,10 +128,7 @@ impl Inspector for AccessListInspector { op::SLOAD | op::SSTORE => { if let Some([slot]) = interp.stack().peekn() { let cur_contract = interp.message().destination; - self.touched_slots - .entry(cur_contract) - .or_default() - .insert(B256::from(slot.to_be_bytes())); + self.touched_slots.entry(cur_contract).or_default().insert(B256::from(slot)); } } op::EXTCODECOPY diff --git a/crates/inspectors/src/storage.rs b/crates/inspectors/src/storage.rs index c30fdcdd6..2d391610f 100644 --- a/crates/inspectors/src/storage.rs +++ b/crates/inspectors/src/storage.rs @@ -52,7 +52,7 @@ impl Inspector for StorageInspector { && let Some([slot]) = interp.stack().peekn() { let address = interp.message().destination; - let slot = B256::from(slot.to_be_bytes()); + let slot = B256::from(slot); let slot_access_count = self.accessed_slots.entry(address).or_default().entry(slot).or_default(); diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 0b8c90438..997ad7d93 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -687,7 +687,7 @@ mod tests { state.insert_account( prefunded_addr, Some(AccountInfo::default().with_balance(U256::from(10))), - Some(AccountInfo::default().with_balance(U256::from(1)).with_nonce(1)), + Some(AccountInfo::default().with_balance(U256::ONE).with_nonce(1)), ); state.insert_account(empty_addr, None, Some(AccountInfo::default().with_nonce(1))); @@ -779,7 +779,7 @@ mod tests { state.insert_account( address, None, - Some(AccountInfo::default().with_balance(U256::from(1))), + Some(AccountInfo::default().with_balance(U256::ONE)), ); } // An account emptied by execution must retain its original prestate. diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index abe0493df..e0fbf0738 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -250,7 +250,7 @@ fn storage_snapshots_accumulate_and_skip_failed_steps() { let mut sload = test_step(1, op::SLOAD); sload.storage_change = Some(Box::new(StorageChange { key: U256::ZERO, - value: U256::from(1), + value: U256::ONE, had_value: None, reason: StorageChangeReason::SLOAD, })); @@ -277,7 +277,7 @@ fn storage_snapshots_accumulate_and_skip_failed_steps() { assert!(frame.struct_logs[0].storage.is_none()); assert_eq!( frame.struct_logs[1].storage, - (!disable_storage).then(|| BTreeMap::from([(B256::ZERO, U256::from(1).into())])) + (!disable_storage).then(|| BTreeMap::from([(B256::ZERO, B256::with_last_byte(1))])) ); assert!(frame.struct_logs[2].storage.is_none()); assert!(frame.struct_logs[3].storage.is_none()); diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index 35ab25279..2158ce82e 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -238,7 +238,7 @@ impl Erc7562Inspector { length: Option, ) -> Option> { // The operands are truncated to 64 bits, and geth fails on a negative one. - let (offset, length) = (offset?.as_limbs()[0], length?.as_limbs()[0]); + let (offset, length) = (offset?.wrapping_to::(), length?.wrapping_to::()); i64::try_from(offset).ok()?; i64::try_from(length).ok()?; memory_copy_padded(interp.memory().as_slice(), offset, length) diff --git a/crates/inspectors/src/tracing/fourbyte.rs b/crates/inspectors/src/tracing/fourbyte.rs index 9acbb66af..579b7ed91 100644 --- a/crates/inspectors/src/tracing/fourbyte.rs +++ b/crates/inspectors/src/tracing/fourbyte.rs @@ -23,7 +23,7 @@ //! See also use alloc::format; -use alloy_primitives::{Selector, hex, map::HashMap}; +use alloy_primitives::{Selector, map::HashMap}; use alloy_rpc_types_trace::geth::FourByteFrame; use evm2::{ EvmTypes, Inspector, InspectorConfig, @@ -79,7 +79,7 @@ impl From<&FourByteInspector> for FourByteFrame { .inner .iter() .map(|((selector, calldata_size), count)| { - let key = format!("0x{}-{}", hex::encode(selector), *calldata_size); + let key = format!("0x{selector:x}-{}", *calldata_size); (key, *count) }) .collect(), diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 6252682ed..b2a4bf1e8 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -842,7 +842,7 @@ fn memory_write_range(op: u8, stack: &[U256]) -> Option<(usize, usize)> { let peek = |n: usize| stack.len().checked_sub(n + 1).map(|i| stack[i]); let (offset, size) = match op { opcode::MSTORE => (peek(0)?, U256::from(32)), - opcode::MSTORE8 => (peek(0)?, U256::from(1)), + opcode::MSTORE8 => (peek(0)?, U256::ONE), opcode::MCOPY | opcode::CALLDATACOPY | opcode::CODECOPY | opcode::RETURNDATACOPY => { (peek(0)?, peek(2)?) } @@ -1581,14 +1581,14 @@ mod tests { #[test] fn test_stack_view_reconstructs_pre_execution_stack() { // SWAP1 touches the top two items, DUP1 one item, ADD two items - let pre = [U256::from(1), U256::from(2), U256::from(3)]; + let pre = [U256::ONE, U256::from(2), U256::from(3)]; for (op, post) in [ - (opcode::SWAP1, vec![U256::from(1), U256::from(3), U256::from(2)]), - (opcode::DUP1, vec![U256::from(1), U256::from(2), U256::from(3), U256::from(3)]), - (opcode::ADD, vec![U256::from(1), U256::from(5)]), - (opcode::POP, vec![U256::from(1), U256::from(2)]), + (opcode::SWAP1, vec![U256::ONE, U256::from(3), U256::from(2)]), + (opcode::DUP1, vec![U256::ONE, U256::from(2), U256::from(3), U256::from(3)]), + (opcode::ADD, vec![U256::ONE, U256::from(5)]), + (opcode::POP, vec![U256::ONE, U256::from(2)]), // halted before pushing, e.g. out of gas after popping the inputs - (opcode::MSTORE, vec![U256::from(1)]), + (opcode::MSTORE, vec![U256::ONE]), (opcode::STOP, pre.to_vec()), ] { let mut view = StackView::default(); @@ -1637,7 +1637,7 @@ mod tests { let mut view = MemoryView::default(); view.record( opcode::EXTCODECOPY, - &[U256::from(2), U256::ZERO, U256::from(4), U256::from(1)], + &[U256::from(2), U256::ZERO, U256::from(4), U256::ONE], &pre, ); assert_eq!(view.patch_offset, 4); diff --git a/crates/inspectors/src/tracing/js/builtins.rs b/crates/inspectors/src/tracing/js/builtins.rs index 733fca014..7ae651ea3 100644 --- a/crates/inspectors/src/tracing/js/builtins.rs +++ b/crates/inspectors/src/tracing/js/builtins.rs @@ -495,6 +495,7 @@ unsafe impl Trace for PrecompileList { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::uint; #[test] fn test_install_bigint() { @@ -520,13 +521,10 @@ mod tests { // Test various U256 values through to_bigint let test_cases = vec![ (U256::ZERO, "0"), - (U256::from(1u64), "1"), + (U256::ONE, "1"), (U256::from(42u64), "42"), (U256::from(u64::MAX), "18446744073709551615"), - ( - U256::from_str_radix("123456789012345678901234567890", 10).unwrap(), - "123456789012345678901234567890", - ), + (uint!(123456789012345678901234567890_U256), "123456789012345678901234567890"), ]; for (value, expected) in test_cases { @@ -538,9 +536,9 @@ mod tests { // Values above 64 and 128 bits take different construction paths for value in [ - U256::from(u64::MAX) + U256::from(1), + U256::from(u64::MAX) + U256::ONE, U256::from(u128::MAX), - U256::from(u128::MAX) + U256::from(1), + U256::from(u128::MAX) + U256::ONE, U256::from_limbs([ 0x0123456789abcdef, 0xfedcba9876543210, diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 53f2f0da1..cd8e1c944 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -352,7 +352,7 @@ impl JsInspector { } }); - let base_fee = block.basefee.try_into().unwrap_or(u64::MAX); + let base_fee = block.basefee.saturating_to::(); let gas_tip = tx.effective_tip_per_gas(base_fee).unwrap_or_default(); let ctx = JsEvmContext { @@ -368,7 +368,7 @@ impl JsInspector { gas_used: result.tx_gas_used(), gas_price: U256::from(gas_tip), value: tx.value(), - block: block.number.try_into().unwrap_or(u64::MAX), + block: block.number.saturating_to::(), coinbase: block.beneficiary, output: output_bytes.unwrap_or_default(), transaction_ctx: self.transaction_context, @@ -1314,7 +1314,7 @@ mod tests { AccountInfo { code: Some(Bytecode::new_legacy( /* PUSH1 1, PUSH1 1, STOP */ - contract.unwrap_or_else(|| hex!("6001600100").into()), + contract.unwrap_or(bytes!("6001600100")), )), ..Default::default() }, @@ -1830,7 +1830,7 @@ mod tests { db.insert_account_info( &addr, AccountInfo { - code: Some(Bytecode::new_legacy(hex!("6001600100").into())), + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), ..Default::default() }, ); @@ -1901,7 +1901,7 @@ mod tests { db.insert_account_info( &child, AccountInfo { - code: Some(Bytecode::new_legacy(hex!("600100").into())), + code: Some(Bytecode::new_legacy(bytes!("600100"))), ..Default::default() }, ); @@ -2066,7 +2066,7 @@ mod tests { this.r=[a[0],a[32],capped.length,log.memory.slice(2**40,2**40).length,error]; },fault:function(){},result:function(){return this.r}}"#; assert_eq!( - run_trace(tracer, Some(hex!("60ff60005300").into()), true), + run_trace(tracer, Some(bytes!("60ff60005300")), true), json!([255, 0, 1024 * 1024, 0, "TypeError"]) ); } diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs index 2b9f8e420..59bb49303 100644 --- a/crates/inspectors/src/tracing/keccak.rs +++ b/crates/inspectors/src/tracing/keccak.rs @@ -98,7 +98,7 @@ impl Inspector for Keccak256PreimageInspector { // The stack can be too small if the operation is about to fail. let &[.., length, offset] = interp.stack().as_slice() else { return }; // The operands are truncated to 64 bits. - let (offset, length) = (offset.as_limbs()[0], length.as_limbs()[0]); + let (offset, length) = (offset.wrapping_to::(), length.wrapping_to::()); let memory = interp.memory().as_slice(); if let Some(preimage) = memory_copy_padded(memory, offset, length) { let hash = keccak256(&preimage); diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 07b3639ed..bcc586ece 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -1,7 +1,7 @@ //! Internal transfer inspector. use alloc::{vec, vec::Vec}; -use alloy_primitives::{Address, B256, Log, U256, address, b256}; +use alloy_primitives::{Address, B256, Log, U256, b256}; use evm2::{ Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, @@ -10,7 +10,7 @@ use evm2::{ /// Sender of ETH transfer log per `eth_simulateV1` spec. /// /// -pub const TRANSFER_LOG_EMITTER: Address = address!("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); +pub const TRANSFER_LOG_EMITTER: Address = Address::repeat_byte(0xee); /// Topic of `Transfer(address,address,uint256)` event. pub const TRANSFER_EVENT_TOPIC: B256 = diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index 1fb361c45..6d485bbb4 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -4,7 +4,7 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; -use alloy_primitives::{Address, B256, Signature, U256, address, b256, hex}; +use alloy_primitives::{Address, B256, Signature, U256, address, bytes, hex}; use evm2::ethereum::TxEnvelope; use evm2_inspectors::access_list::AccessListInspector; @@ -28,7 +28,7 @@ fn initial_access_list_is_normalized_and_pruned() { &callee, AccountInfo { // Read slot 1 so it is re-added after pruning the callee's stale seeded slot. - code: Some(Bytecode::new_raw(hex!("60015400").into())), + code: Some(Bytecode::new_raw(bytes!("60015400"))), ..Default::default() }, ); @@ -86,14 +86,14 @@ fn test_access_list_precompile() { caller, gas_limit: 1000000, kind: TransactTo::Call(account), - data: hex!("a5399705").into(), + data: bytes!("a5399705"), nonce: 0, ..Default::default() }) .unwrap(); assert!(res.result.is_success(), "{res:#?}"); - let erecover = address!("0x0000000000000000000000000000000000000001"); + let erecover = Address::with_last_byte(1); assert!(accesslist.excluded().contains(&erecover)); assert!(accesslist.into_access_list().is_empty()); } @@ -360,10 +360,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], - )], + expected: &[(a(0xc1), &[B256::with_last_byte(5)])], }, AccessListCase { name: "sstore_underflow", @@ -372,10 +369,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000007")], - )], + expected: &[(a(0xc1), &[B256::with_last_byte(7)])], }, AccessListCase { name: "balance_oog", @@ -393,10 +387,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 25353, seed: &[], - expected: &[( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], - )], + expected: &[(a(0xc1), &[B256::with_last_byte(5)])], }, AccessListCase { name: "sstore_oog_sentry", @@ -405,10 +396,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 27300, seed: &[], - expected: &[( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], - )], + expected: &[(a(0xc1), &[B256::with_last_byte(9)])], }, AccessListCase { name: "call_oog", @@ -437,10 +425,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[( - a(0xc2), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000003")], - )], + expected: &[(a(0xc2), &[B256::with_last_byte(3)])], }, AccessListCase { name: "delegatecall_sload", @@ -456,13 +441,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[ - ( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000004")], - ), - (a(0xc2), &[]), - ], + expected: &[(a(0xc1), &[B256::with_last_byte(4)]), (a(0xc2), &[])], }, AccessListCase { name: "callcode_sload", @@ -478,13 +457,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[ - ( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000004")], - ), - (a(0xc2), &[]), - ], + expected: &[(a(0xc1), &[B256::with_last_byte(4)]), (a(0xc2), &[])], }, AccessListCase { name: "selfdestruct", @@ -541,7 +514,7 @@ const CASES: &[AccessListCase] = &[ seed: &[], expected: &[( address!("0x3817e247023b4f489352758397040b1fd33b300a"), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + &[B256::with_last_byte(5)], )], }, AccessListCase { @@ -557,7 +530,7 @@ const CASES: &[AccessListCase] = &[ seed: &[], expected: &[( address!("0x724572a7f7b8eb3aa9763a66f2511269ec3764d5"), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], + &[B256::with_last_byte(5)], )], }, AccessListCase { @@ -567,30 +540,12 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[ - ( - a(0xd1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], - ), - ( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000007")], - ), - ( - a(0x1001), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000001")], - ), + (a(0xd1), &[B256::with_last_byte(9)]), + (a(0xc1), &[B256::with_last_byte(7)]), + (a(0x1001), &[B256::with_last_byte(1)]), (a(0x2), &[]), ], - expected: &[ - ( - a(0xc1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000005")], - ), - ( - a(0xd1), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000009")], - ), - ], + expected: &[(a(0xc1), &[B256::with_last_byte(5)]), (a(0xd1), &[B256::with_last_byte(9)])], }, AccessListCase { name: "reverted_frame_touch", @@ -606,13 +561,7 @@ const CASES: &[AccessListCase] = &[ input: &hex!(""), gas_limit: 5000000, seed: &[], - expected: &[ - ( - a(0xc2), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000006")], - ), - (a(0xb001), &[]), - ], + expected: &[(a(0xc2), &[B256::with_last_byte(6)]), (a(0xb001), &[])], }, AccessListCase { name: "extcode_ops", diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index c6ff2c3a8..5088db532 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -10,7 +10,7 @@ use crate::utils::{ use alloy_consensus::crypto::secp256k1::sign_message; use alloy_eips::eip7702::Authorization; use alloy_primitives::{ - Address, B256, Bytes, Log, TxKind, U256, address, hex, keccak256, map::HashMap, + Address, B256, Bytes, Log, TxKind, U256, address, bytes, hex, keccak256, map::HashMap, }; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::{ @@ -109,7 +109,7 @@ fn test_calltracer_eip7702_call_targets() { db.insert_account_info( &implementation, AccountInfo { - code: Some(Bytecode::new_raw(hex!("602a60005260206000f3").into())), + code: Some(Bytecode::new_raw(bytes!("602a60005260206000f3"))), ..Default::default() }, ); @@ -462,7 +462,7 @@ fn test_geth_erc7562_counts_like_geth() { } code.push(op::STOP); let target_code = Bytes::from_static(&[op::STOP]); - let failing_code = Bytes::from_static(&hex!("6001600020")); + let failing_code = bytes!("6001600020"); let mut db = CacheDB::::default(); for (address, code) in [ @@ -570,7 +570,7 @@ fn test_geth_erc7562_accessed_slots_and_ignored_opcodes() { &contract, AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, ); - db.insert_account_storage(&contract, &U256::from(1), &U256::from(0x2a)); + db.insert_account_storage(&contract, &U256::ONE, &U256::from(0x2a)); db.insert_account_storage(&contract, &U256::from(2), &U256::from(0x2b)); // Only `SLOAD` and `STOP` are ignored, which is not the default list. @@ -817,7 +817,7 @@ fn test_geth_erc7562_records_warm_first_reads() { }, ); } - db.insert_account_storage(&outer, &U256::from(1), &U256::from(0x2a)); + db.insert_account_storage(&outer, &U256::ONE, &U256::from(0x2a)); }); let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); @@ -839,8 +839,8 @@ fn test_geth_erc7562_records_warm_first_reads() { let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; - let slot = B256::from(U256::from(1)); - let value = B256::from(U256::from(0x2a)); + let slot = B256::with_last_byte(1); + let value = B256::with_last_byte(0x2a); assert_eq!(frame.accessed_slots.reads[&slot], vec![value]); assert_eq!(frame.calls.len(), 1); assert_eq!(frame.calls[0].accessed_slots.reads[&slot], vec![value]); @@ -935,7 +935,7 @@ fn test_geth_state_gas_tracer() { let (trace, gas) = trace_contract( SpecId::AMSTERDAM, GethDebugTracingOptions::state_gas_tracer(), - hex!("600160005500").into(), + bytes!("600160005500"), ); assert!(gas.state_gas_spent() > 0); @@ -1309,7 +1309,7 @@ fn test_geth_calltracer_top_call_reverting() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("0332ed13").into(), // nestedEmitWithFailureAfterNestedEmit() selector + data: bytes!("0332ed13"), // nestedEmitWithFailureAfterNestedEmit() selector nonce: 1, ..Default::default() }) @@ -1342,7 +1342,7 @@ fn test_geth_calltracer_top_call_reverting() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr2), - data: hex!("0332ed13").into(), // nestedEmitWithFailureAfterNestedEmit() selector + data: bytes!("0332ed13"), // nestedEmitWithFailureAfterNestedEmit() selector nonce: 1, ..Default::default() }) @@ -1399,7 +1399,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -1432,7 +1432,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr2), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -1469,7 +1469,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr3), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -1527,7 +1527,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("8384a002").into(), // nestedEmitWithSuccess() selector + data: bytes!("8384a002"), // nestedEmitWithSuccess() selector nonce: 1, ..Default::default() }) @@ -1663,7 +1663,7 @@ fn test_geth_calltracer_null_bytes_revert_reason_omitted() { .inspect_tx(TxEnv { caller: deployer, kind: TxKind::Call(addr), - data: hex!("3bc5de30").into(), // call function selector + data: bytes!("3bc5de30"), // call function selector gas_limit: 1_000_000, nonce: 1, // Set correct nonce after contract deployment ..Default::default() @@ -1807,7 +1807,7 @@ fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("43d726d6").into(), + data: bytes!("43d726d6"), nonce: 1, ..Default::default() }) @@ -2015,8 +2015,8 @@ fn test_geth_calltracer_logs_address_regular() { /// rather than reconstructing it from `execution_address()`. #[test] fn test_geth_calltracer_logs_delegatecall() { - let proxy = address!("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - let implementation = address!("0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); + let proxy = Address::repeat_byte(0xaa); + let implementation = Address::repeat_byte(0xbb); let caller = address!("0xa000000000000000000000000000000000000099"); // Implementation bytecode: LOG0(offset=0, size=0); STOP @@ -2106,7 +2106,7 @@ fn test_geth_opcode_limit_end_to_end() { use serde_json::{Value, json}; let account = address!("1000000000000000000000000000000000000001"); - let child = address!("00000000000000000000000000000000000000ff"); + let child = Address::with_last_byte(0xff); for terminal in [op::RETURN, op::REVERT] { let run = |options: Value| { let options: GethDebugTracingOptions = serde_json::from_value(options).unwrap(); @@ -2116,8 +2116,7 @@ fn test_geth_opcode_limit_end_to_end() { code.push(terminal); let context = Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (addr, code) in - [(account, Bytes::from(code)), (child, hex!("60015000").into())] + for (addr, code) in [(account, Bytes::from(code)), (child, bytes!("60015000"))] { db.insert_account_info( &addr, @@ -2250,7 +2249,7 @@ fn geth_json_len(log: &serde_json::Value) -> usize { #[test] fn test_geth_opcode_limit() { let account = address!("1000000000000000000000000000000000000001"); - let child = address!("00000000000000000000000000000000000000ff"); + let child = Address::with_last_byte(0xff); for terminal in [op::RETURN, op::REVERT, op::INVALID] { // Call a child, then return/revert with one byte (0x2a), or halt exceptionally. let mut code = hex!("6000600060006000600060ff61fffff150602a60005360016000").to_vec(); @@ -2259,7 +2258,7 @@ fn test_geth_opcode_limit() { let context = Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { for (addr, code) in - [(account, Bytes::from(code.clone())), (child, hex!("60015000").into())] + [(account, Bytes::from(code.clone())), (child, bytes!("60015000"))] { db.insert_account_info( &addr, @@ -2294,7 +2293,7 @@ fn test_geth_opcode_limit() { assert_eq!(full.failed, terminal != op::RETURN); assert_eq!( full.return_value, - if terminal == op::INVALID { Bytes::new() } else { hex!("2a").into() } + if terminal == op::INVALID { Bytes::new() } else { bytes!("2a") } ); // Includes caps before CALL, on CALL, in the child, and after returning to the parent. The // limit counts bytes of JSON, and the first `steps - 1` steps let `steps` steps through. @@ -2375,10 +2374,7 @@ fn test_geth_struct_log_out_of_gas_cost() { .modify_db_chained(|db| { db.insert_account_info( &contract, - AccountInfo { - code: Some(Bytecode::new_raw(code.to_vec().into())), - ..Default::default() - }, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, ); }); @@ -2512,7 +2508,7 @@ fn test_debug_call_log_index_seed_and_reset() { &account, AccountInfo { // PUSH0 PUSH0 LOG0 STOP. - code: Some(Bytecode::new_raw(hex!("5f5fa000").into())), + code: Some(Bytecode::new_raw(bytes!("5f5fa000"))), ..Default::default() }, ); @@ -2558,7 +2554,7 @@ fn test_debug_call_log_index_seed_and_reset() { #[test] fn test_geth_4byte_tracer_skips_precompiles() { // STATICCALL the sha256 precompile with 36 bytes, then an empty account with 4 bytes. - let code = hex!("600060006024600060025afa50600060006004600061dead5afa5000").into(); + let code = bytes!("600060006024600060025afa50600060006004600061dead5afa5000"); let opts = serde_json::from_value(serde_json::json!({ "tracer": "4byteTracer" })).unwrap(); let (trace, _) = trace_contract(SpecId::PRAGUE, opts, code); @@ -2768,8 +2764,7 @@ fn keccak256_preimage_tracer_collects_padded_preimages() { }) .unwrap(); - let mut word = [0; 32]; - word[31] = 0x2a; + let word: [u8; 32] = B256::with_last_byte(0x2a).into(); let mut padded = [0; 0x40]; padded[..0x10].copy_from_slice(&word[0x10..]); let expected = serde_json::Value::Object( @@ -3043,17 +3038,14 @@ fn mux_runs_nested_mux_and_erc7562_tracers() { fn struct_log_storage_snapshots_match_geth() { let account = address!("1000000000000000000000000000000000000001"); let slot = |slot: u8| B256::with_last_byte(slot); - let value = |value: u8| U256::from(value).into(); + let value = |value: u8| B256::with_last_byte(value); // SLOAD 5 (holds 7), SSTORE 5 = 7 (unchanged), SLOAD 5 (warm), SSTORE 6 = 1, SLOAD 6, and an // SSTORE that runs out of gas, which fails before it runs. let code = hex!("60055450600760055560055450600160065560065450600160075500"); let mut db = CacheDB::::default(); db.insert_account_info( &account, - AccountInfo { - code: Some(Bytecode::new_raw(Bytes::from(code.to_vec()))), - ..Default::default() - }, + AccountInfo { code: Some(Bytecode::new_raw(Bytes::from(code))), ..Default::default() }, ); db.insert_account_storage(&account, &U256::from(5), &U256::from(7)); let mut inspector = DebugInspector::new(Default::default()).unwrap(); @@ -3096,8 +3088,8 @@ fn struct_log_storage_snapshots_match_geth() { #[test] fn struct_log_storage_snapshots_follow_the_storage_context() { let parent = address!("1000000000000000000000000000000000000001"); - let writer = address!("00000000000000000000000000000000000000c0"); - let static_writer = address!("00000000000000000000000000000000000000d0"); + let writer = Address::with_last_byte(0xc0); + let static_writer = Address::with_last_byte(0xd0); // The parent delegatecalls the writer, loads the slot it wrote, then static calls the second // writer. let parent_code = hex!( @@ -3135,7 +3127,7 @@ fn struct_log_storage_snapshots_follow_the_storage_context() { let trace = inspector.get_result(None, &tx, &block, &result.tx_result, ctx.db_mut()).unwrap(); let GethTrace::Default(frame) = trace else { panic!("expected a default trace") }; - let snapshot = BTreeMap::from([(B256::with_last_byte(1), U256::from(2).into())]); + let snapshot = BTreeMap::from([(B256::with_last_byte(1), B256::with_last_byte(2))]); let logs: Vec<_> = frame .struct_logs .iter() @@ -3158,7 +3150,7 @@ fn struct_log_storage_snapshots_follow_the_storage_context() { fn test_geth_prestate_diff_omits_empty_accounts() { let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); let coinbase = address!("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"); - let recipient = address!("2222222222222222222222222222222222222222"); + let recipient = Address::repeat_byte(0x22); let mut db = CacheDB::new(EmptyDB::default()); db.insert_account_info( @@ -3223,7 +3215,7 @@ fn test_geth_prestate_diff_omits_empty_accounts() { fn test_geth_prestate_diff_reports_cleared_code() { let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); let authority = address!("7e5f4552091a69125d5dfcb7b8c2659029395bdf"); - let delegation = Bytes::from(hex!("ef0100000000000000000000000000000000000000aaaa")); + let delegation = bytes!("ef0100000000000000000000000000000000000000aaaa"); let mut db = CacheDB::new(EmptyDB::default()); db.insert_account_info( @@ -3233,7 +3225,7 @@ fn test_geth_prestate_diff_reports_cleared_code() { db.insert_account_info( &authority, AccountInfo { - balance: U256::from(1), + balance: U256::ONE, code_hash: alloy_primitives::keccak256(&delegation), code: Some(Bytecode::new_raw(delegation.clone())), ..Default::default() @@ -3245,7 +3237,7 @@ fn test_geth_prestate_diff_reports_cleared_code() { .build_mainnet(); // Signed by the key of `authority`; delegates to the zero address, which clears the code. - let authorization = Authorization { chain_id: U256::from(1), address: Address::ZERO, nonce: 0 }; + let authorization = Authorization { chain_id: U256::ONE, address: Address::ZERO, nonce: 0 }; let signature = sign_message(B256::with_last_byte(1), authorization.signature_hash()).unwrap(); let authorization = authorization.into_signed(signature); assert_eq!(authorization.recover_authority().unwrap(), authority); @@ -3259,7 +3251,7 @@ fn test_geth_prestate_diff_reports_cleared_code() { .inspect_tx(TxEnv { caller: sender, gas_limit: 100_000, - kind: TransactTo::Call(address!("2222222222222222222222222222222222222222")), + kind: TransactTo::Call(Address::repeat_byte(0x22)), authorization_list: vec![authorization], ..Default::default() }) @@ -3286,10 +3278,10 @@ fn test_geth_prestate_diff_reports_cleared_code() { /// selfdestructs it, and returns the contract and the prestate frame. fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreStateFrame) { let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); - let contract = address!("1111111111111111111111111111111111111111"); + let contract = Address::repeat_byte(0x11); // SLOAD 1, SLOAD 2, SELFDESTRUCT to the beneficiary. - let code = Bytes::from(hex!("6001545060025450733333333333333333333333333333333333333333ff")); + let code = bytes!("6001545060025450733333333333333333333333333333333333333333ff"); let mut db = CacheDB::new(EmptyDB::default()); db.insert_account_info( &sender, @@ -3304,7 +3296,7 @@ fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreSt Some(Bytecode::new_raw(code)), ), ); - db.insert_account_storage(&contract, &U256::from(1), &U256::from(0x2a)); + db.insert_account_storage(&contract, &U256::ONE, &U256::from(0x2a)); let evm = Context::mainnet() .with_db(db) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) @@ -3336,7 +3328,7 @@ fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreSt /// read, including unchanged and zero ones. #[test] fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { - let beneficiary = address!("3333333333333333333333333333333333333333"); + let beneficiary = Address::repeat_byte(0x33); let (contract, frame) = trace_london_selfdestruct(PreStateConfig { diff_mode: Some(true), ..Default::default() }); let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; @@ -3518,7 +3510,7 @@ fn run_spy(config: TracingInspectorConfig, all: bool) -> SteppedSpy (ExecutionResult, Vec::default()) @@ -274,9 +274,9 @@ fn test_parity_call_selfdestruct_create() { caller, gas_limit: 10000000, kind: TransactTo::Create, - data: code.to_vec().into(), + data: code.into(), nonce: 24, - value: U256::from(1), + value: U256::ONE, ..Default::default() }) .unwrap(); @@ -301,7 +301,7 @@ fn test_parity_call_selfdestruct_create() { let last = traces.len() - 1; let second_sd = Action::Selfdestruct(SelfdestructAction { address: address!("0x076af20059617d9bf2e49f4f19bd3f48d582dcef"), - balance: U256::from(0), + balance: U256::ZERO, refund_address: address!("0xf2719d5f7ec66c38abd64bc5efda6876590b3b30"), }); @@ -310,7 +310,7 @@ fn test_parity_call_selfdestruct_create() { let first_sd = Action::Selfdestruct(SelfdestructAction { address: address!("0x9f2e7bebe157dfd9f1322bd252f1a5af37629687"), - balance: U256::from(1), + balance: U256::ONE, refund_address: address!("0xf2719d5f7ec66c38abd64bc5efda6876590b3b30"), }); @@ -351,11 +351,9 @@ fn test_parity_statediff_blob_commit() { gas_limit: 1000000, kind: TransactTo::Call(to), gas_price: 150, - blob_hashes: vec![ + blob_hashes: vec![b256!( "0x01af2fd94f17364bc8ef371c4c90c3a33855ff972d10b9c03d0445b3fca063ea" - .parse() - .unwrap(), - ], + )], max_fee_per_blob_gas: 1000000000, ..Default::default() }) @@ -788,7 +786,7 @@ fn vmtrace_revert_keeps_its_execution_record() { fn vmtrace_call_pushes_result_and_copies_return_memory() { let trace = trace_vm_code(&hex!("602060205f5f5f604361fffff15f00"), &hex!("602a5f5260205ff3")); let call = &trace.ops[7]; - assert_eq!(call.ex.as_ref().unwrap().push.as_slice(), &[U256::from(1)]); + assert_eq!(call.ex.as_ref().unwrap().push.as_slice(), &[U256::ONE]); let mem = call.ex.as_ref().unwrap().mem.as_ref().unwrap(); assert_eq!(mem.off, 32); assert_eq!(mem.data.as_ref(), U256::from(42).to_be_bytes::<32>()); @@ -815,7 +813,7 @@ fn vmtrace_terminal_call_finalizes_delta_at_capture_limit() { let call = trace.ops.last().unwrap(); assert_eq!(call.op.as_deref(), Some("CALL")); let ex = call.ex.as_ref().unwrap(); - assert_eq!(ex.push, vec![U256::from(1)]); + assert_eq!(ex.push, vec![U256::ONE]); let mem = ex.mem.as_ref().unwrap(); assert_eq!(mem.off, 32); assert_eq!(mem.data.as_ref(), &[42]); @@ -913,7 +911,7 @@ fn vmtrace_precompile_does_not_steal_the_next_calls_subtrace() { trace_vm_code(&hex!("5f5f5f5f5f600461fffff1505f5f5f5f5f604361fffff100"), &hex!("60015000")); assert!(trace.ops[7].sub.as_ref().unwrap().ops.is_empty()); assert_eq!(trace.ops[16].sub.as_ref().unwrap().ops.len(), 3); - assert_eq!(trace.ops[7].ex.as_ref().unwrap().push, vec![U256::from(1)]); + assert_eq!(trace.ops[7].ex.as_ref().unwrap().push, vec![U256::ONE]); } #[test] @@ -971,9 +969,8 @@ fn reverted_create_reports_gas_and_revert_data() { let mut db = CacheDB::::default(); db.insert_account_info( &factory, - AccountInfo::default().with_code(Bytecode::new_raw( - hex!("6b63deadbeef5f526004601cfd5f52600c60145ff000").into(), - )), + AccountInfo::default() + .with_code(Bytecode::new_raw(bytes!("6b63deadbeef5f526004601cfd5f52600c60145ff000"))), ); let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( TracingInspector::new(TracingInspectorConfig::default_parity()), @@ -1092,7 +1089,7 @@ fn create_nonce_overflow_is_a_failed_frame() { &factory, AccountInfo { nonce, - ..AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())) + ..AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))) }, ); let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( @@ -1128,7 +1125,7 @@ fn create_nonce_overflow_runs_no_frame_since_amsterdam() { &factory, AccountInfo { nonce: u64::MAX, - ..AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())) + ..AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))) }, ); let mut evm = Context::mainnet() @@ -1154,7 +1151,7 @@ fn create_with_empty_initcode_runs_no_steps() { let mut db = CacheDB::::default(); db.insert_account_info( &factory, - AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))), ); let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( TracingInspector::new(TracingInspectorConfig::default_parity()), @@ -1171,7 +1168,7 @@ fn create_with_empty_initcode_runs_no_steps() { #[test] fn precompile_exclusion_uses_original_callee_with_delegation() { - let parent = Address::from([0x11; 20]); + let parent = Address::repeat_byte(0x11); for callee in [2, 0x55] { for opcode in [op::CALL, op::CALLCODE, op::DELEGATECALL, op::STATICCALL] { let mut code = vec![op::PUSH0, op::PUSH0, op::PUSH1, 4, op::PUSH0]; diff --git a/crates/inspectors/tests/it/parity/state_diff_7702.rs b/crates/inspectors/tests/it/parity/state_diff_7702.rs index 2638670c2..363c582f2 100644 --- a/crates/inspectors/tests/it/parity/state_diff_7702.rs +++ b/crates/inspectors/tests/it/parity/state_diff_7702.rs @@ -51,7 +51,7 @@ fn authorization_code_changes() { to: target, gas_limit: 100_000, authorization_list: vec![SignedAuthorization::new_unchecked( - Authorization { chain_id: U256::from(1), address: delegate, nonce: 1 }, + Authorization { chain_id: U256::ONE, address: delegate, nonce: 1 }, 0, U256::ZERO, U256::ZERO, diff --git a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs index b1f442a85..d354041ac 100644 --- a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs +++ b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs @@ -1,5 +1,5 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; -use alloy_primitives::{Address, B256, U64, U256, hex, map::HashSet}; +use alloy_primitives::{Address, B256, U64, U256, bytes, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, TraceType}; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; @@ -11,9 +11,8 @@ fn existing_account_selfdestruct_across_cancun() { let parent = Address::with_last_byte(0x43); // Read an unchanged slot, overwrite a nonzero slot, write a new slot, read zero, // then selfdestruct. - let code = Bytecode::new_raw( - hex!("600054506009600155600360025560035450600060045533ff").into(), - ); + let code = + Bytecode::new_raw(bytes!("600054506009600155600360025560035450600060045533ff")); let mut db = CacheDB::::default(); db.insert_account_info( &target, @@ -21,13 +20,13 @@ fn existing_account_selfdestruct_across_cancun() { .with_code(code.clone()), ); db.insert_account_storage(&target, &U256::ZERO, &U256::from(7)); - db.insert_account_storage(&target, &U256::from(1), &U256::from(8)); + db.insert_account_storage(&target, &U256::ONE, &U256::from(8)); db.insert_account_storage(&target, &U256::from(4), &U256::from(10)); db.insert_account_info( &parent, - AccountInfo::default().with_code(Bytecode::new_raw( - hex!("60006000600060006000604261fffff15060006000fd").into(), - )), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!( + "60006000600060006000604261fffff15060006000fd" + ))), ); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) @@ -77,16 +76,16 @@ fn existing_account_selfdestruct_across_cancun() { assert_eq!(diff.code, Delta::Unchanged); assert_eq!( diff.storage[&B256::with_last_byte(4)], - Delta::changed(U256::from(10).into(), U256::ZERO.into()) + Delta::changed(B256::with_last_byte(10), B256::ZERO) ); assert_eq!(diff.storage.len(), 3); assert_eq!( diff.storage[&B256::with_last_byte(1)], - Delta::changed(U256::from(8).into(), U256::from(9).into()) + Delta::changed(B256::with_last_byte(8), B256::with_last_byte(9)) ); assert_eq!( diff.storage[&B256::with_last_byte(2)], - Delta::changed(U256::ZERO.into(), U256::from(3).into()) + Delta::changed(B256::ZERO, B256::with_last_byte(3)) ); } } @@ -117,7 +116,7 @@ fn created_account_selfdestruct_across_cancun() { TxEnv::builder() .caller(caller) .kind(TransactTo::Create) - .data(hex!("33ff").into()) + .data(bytes!("33ff")) .gas_limit(100_000) .build_fill(), ) @@ -165,7 +164,7 @@ fn selfdestruct_preserves_balance_only_account() { TxEnv::builder() .caller(caller) .kind(TransactTo::Create) - .data(hex!("30ff").into()) + .data(bytes!("30ff")) .gas_limit(200_000) .build_fill(), ) @@ -201,7 +200,7 @@ fn deleted_account_diff_does_not_depend_on_accessed_slots() { AccountInfo { balance: U256::from(69), nonce: 1, ..Default::default() }.with_code(code), ); db.insert_account_storage(&target, &U256::ZERO, &U256::from(7)); - db.insert_account_storage(&target, &U256::from(1), &U256::from(8)); + db.insert_account_storage(&target, &U256::ONE, &U256::from(8)); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = SpecId::SHANGHAI) .with_db(db) diff --git a/crates/inspectors/tests/it/parity/vm_bytecode.rs b/crates/inspectors/tests/it/parity/vm_bytecode.rs index ac3191d21..3033de287 100644 --- a/crates/inspectors/tests/it/parity/vm_bytecode.rs +++ b/crates/inspectors/tests/it/parity/vm_bytecode.rs @@ -1,7 +1,7 @@ use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; use alloy_consensus::{TxEip7702, transaction::Recovered}; use alloy_eips::eip7702::{Authorization, SignedAuthorization}; -use alloy_primitives::{Address, Bytes, U256, hex, map::HashSet}; +use alloy_primitives::{Address, Bytes, U256, bytes, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{TraceResults, TraceType}; use evm2::{ BaseEvmTypes, Evm, Precompiles, @@ -87,15 +87,15 @@ fn authorization_resolves_executed_bytecode() { let delegate = Address::with_last_byte(0x44); for before in [Bytecode::default(), Bytecode::new_eip7702(old_delegate)] { for (delegate, code) in [ - (delegate, Bytes::from_static(&hex!("602a5f5260205ff3"))), - (delegate, Bytes::from_static(&hex!("5f5ffd"))), + (delegate, bytes!("602a5f5260205ff3")), + (delegate, bytes!("5f5ffd")), (Address::ZERO, Bytes::new()), ] { let mut db = CacheDB::default(); db.insert_account_info(&authority, AccountInfo::default().with_code(before.clone())); db.insert_account_info( &old_delegate, - AccountInfo::default().with_code(Bytecode::new_raw(hex!("60015000").into())), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("60015000"))), ); if !delegate.is_zero() { db.insert_account_info( @@ -112,11 +112,7 @@ fn authorization_resolves_executed_bytecode() { to: authority, gas_limit: 100_000, authorization_list: vec![SignedAuthorization::new_unchecked( - Authorization { - chain_id: U256::from(1), - address: delegate, - nonce: 0, - }, + Authorization { chain_id: U256::ONE, address: delegate, nonce: 0 }, 0, U256::ZERO, U256::ZERO, diff --git a/crates/inspectors/tests/it/repro/prestate.rs b/crates/inspectors/tests/it/repro/prestate.rs index 0a788cbc7..1cef9a37c 100644 --- a/crates/inspectors/tests/it/repro/prestate.rs +++ b/crates/inspectors/tests/it/repro/prestate.rs @@ -4,7 +4,7 @@ use super::ReproContext; use crate::utils::{Context, SpecId, TransactTo, TxEnv}; -use alloy_primitives::{Bytes, address, hex}; +use alloy_primitives::{address, bytes}; use alloy_rpc_types_trace::geth::{DiffMode, PreStateConfig, PreStateFrame}; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; @@ -23,9 +23,9 @@ fn tx_selfdestruct_env() -> TxEnv { TxEnv { caller: address!("a7fb5ca286fc3fd67525629048a4de3ba24cba2e"), kind: TransactTo::Call(address!("c77ad0a71008d7094a62cfbd250a2eb2afdf2776")), - data: Bytes::from_static(&hex!( + data: bytes!( "f3fef3a3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000f6b64" - )), + ), nonce: 236280, gas_limit: 150000, ..Default::default() diff --git a/crates/inspectors/tests/it/stack.rs b/crates/inspectors/tests/it/stack.rs index b96b40e71..7a00d520a 100644 --- a/crates/inspectors/tests/it/stack.rs +++ b/crates/inspectors/tests/it/stack.rs @@ -1,7 +1,7 @@ //! Stack snapshot tests use crate::utils::{CacheDB, Context, EmptyDB, SpecId, inspect_deploy_contract}; -use alloy_primitives::{Address, U256, hex}; +use alloy_primitives::{Address, U256, bytes}; use evm2_inspectors::tracing::{StackSnapshotType, TracingInspector, TracingInspectorConfig}; /// Runs `PUSH1 1; PUSH1 2; PUSH1 3; STOP` as create init code and returns the recorded stack @@ -16,12 +16,8 @@ fn stack_snapshots(snapshot_type: StackSnapshotType) -> Vec>> .build_mainnet() .with_inspector(&mut insp); - let res = inspect_deploy_contract( - &mut evm, - hex!("60016002600300").into(), - Address::ZERO, - SpecId::CANCUN, - ); + let res = + inspect_deploy_contract(&mut evm, bytes!("60016002600300"), Address::ZERO, SpecId::CANCUN); assert!(res.result.is_success()); let nodes = insp.traces().nodes(); @@ -39,7 +35,7 @@ fn test_top_stack_snapshots() { vec![ // The stack is recorded before each step executes. Some(Box::default()), - Some(Box::from([U256::from(1)])), + Some(Box::from([U256::ONE])), Some(Box::from([U256::from(2)])), Some(Box::from([U256::from(3)])), ] diff --git a/crates/inspectors/tests/it/test_native_bigint.rs b/crates/inspectors/tests/it/test_native_bigint.rs index 232949a88..1cff213a9 100644 --- a/crates/inspectors/tests/it/test_native_bigint.rs +++ b/crates/inspectors/tests/it/test_native_bigint.rs @@ -1,6 +1,6 @@ //! Tests for native BigInt support in Boa -use alloy_primitives::U256; +use alloy_primitives::{U256, uint}; use boa_engine::{Context, JsValue, Source, js_string}; #[test] @@ -45,9 +45,9 @@ fn test_u256_to_boa_bigint() { // Test converting U256 values to BigInt let test_values = [ U256::ZERO, - U256::from(1u64), + U256::ONE, U256::from(u64::MAX), - U256::from_str_radix("123456789012345678901234567890", 10).unwrap(), + uint!(123456789012345678901234567890_U256), U256::MAX, ]; diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index 65610ff96..2f88b0247 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -4,7 +4,7 @@ use crate::utils::{ AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, SpecId, TestDbExt, TransactTo, TxEnv, a, }; -use alloy_primitives::{Address, B256, Log, U256, address, b256, hex}; +use alloy_primitives::{Address, B256, Log, U256, address, bytes, hex}; use evm2_inspectors::{ tracing::{TracingInspector, TracingInspectorConfig}, transfer::{ @@ -62,8 +62,7 @@ fn test_internal_transfers() { caller: deployer, gas_limit: 100000000, kind: TransactTo::Call(addr), - data: hex!("830c29ae0000000000000000000000000000000000000000000000000000000000000000") - .into(), + data: bytes!("830c29ae0000000000000000000000000000000000000000000000000000000000000000"), value: U256::from(10), nonce: 0, ..Default::default() @@ -123,7 +122,7 @@ fn discards_failed_create_transfer() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Create, - data: hex!("fe").into(), + data: bytes!("fe"), value: U256::from(10), nonce: 0, ..Default::default() @@ -139,7 +138,7 @@ fn discards_failed_call_transfers_and_logs() { let deployer = Address::ZERO; let caller = address!("0x1000000000000000000000000000000000000000"); let reverter = address!("0x2000000000000000000000000000000000000000"); - let recipient = address!("0x00000000000000000000000000000000000000ff"); + let recipient = Address::with_last_byte(0xff); // Calls `reverter` with 1 wei, `recipient` with 100 wei (more than the balance), and // `recipient` with 2 wei. `reverter` forwards 1 wei to `recipient` and then reverts. @@ -158,9 +157,7 @@ fn discards_failed_call_transfers_and_logs() { db.insert_account_info( &reverter, AccountInfo { - code: Some(Bytecode::new_legacy( - hex!("6000600060006000600160ff5af15060006000fd").into(), - )), + code: Some(Bytecode::new_legacy(bytes!("6000600060006000600160ff5af15060006000fd"))), ..Default::default() }, ); @@ -396,9 +393,9 @@ fn simulate_transfer_logs_use_eip7708_logs() { &contract, AccountInfo { balance: U256::from(7), - code: Some(Bytecode::new_legacy( - hex!("73000000000000000000000000000000000000a002ff").into(), - )), + code: Some(Bytecode::new_legacy(bytes!( + "73000000000000000000000000000000000000a002ff" + ))), ..Default::default() }, ); @@ -467,11 +464,7 @@ const CASES: &[TransferCase] = &[ to: Some(a(0xc4)), value: 0, input: &hex!(""), - expected: &[ExpectedLog::Emitted( - a(0xc4), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000011")], - &hex!(""), - )], + expected: &[ExpectedLog::Emitted(a(0xc4), &[B256::with_last_byte(0x11)], &hex!(""))], }, TransferCase { name: "selfdestruct_balance", @@ -558,19 +551,11 @@ const CASES: &[TransferCase] = &[ value: 0, input: &hex!(""), expected: &[ - ExpectedLog::Emitted( - a(0xcb), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000021")], - &hex!(""), - ), + ExpectedLog::Emitted(a(0xcb), &[B256::with_last_byte(0x21)], &hex!("")), ExpectedLog::Transfer(a(0xcb), a(0xa001), 1), ExpectedLog::Emitted(a(0xcb), &[], &hex!("")), ExpectedLog::Transfer(a(0xcb), a(0xa001), 2), - ExpectedLog::Emitted( - a(0xcb), - &[b256!("0x0000000000000000000000000000000000000000000000000000000000000022")], - &hex!(""), - ), + ExpectedLog::Emitted(a(0xcb), &[B256::with_last_byte(0x22)], &hex!("")), ], }, TransferCase { diff --git a/crates/inspectors/tests/it/writer.rs b/crates/inspectors/tests/it/writer.rs index 981a6191b..d4a411996 100644 --- a/crates/inspectors/tests/it/writer.rs +++ b/crates/inspectors/tests/it/writer.rs @@ -312,7 +312,7 @@ fn test_receive_vs_fallback_empty_calldata() { "00" // Runtime code: STOP ); - let caller = address!("0x0000000000000000000000000000000000000001"); + let caller = Address::with_last_byte(1); let mut db = CacheDB::new(EmptyDB::default()); db.insert_account_info( &caller, @@ -334,7 +334,7 @@ fn test_receive_vs_fallback_empty_calldata() { TxEnv::builder() .caller(caller) .data(bytes!()) // Empty calldata. - .value(U256::from(1)) // Nonzero value. + .value(U256::ONE) // Nonzero value. .kind(TransactTo::Call(address)) .gas_priority_fee(None) .nonce(1) diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 5cfeb59c4..12a8c0bde 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -783,14 +783,14 @@ pub unsafe fn __revmc_builtin_call( CallKind::Call | CallKind::CallCode => { pop!(sp; value); let value = value.to_u256(); - if call_kind == CallKind::Call && ecx.is_static() && value != U256::ZERO { + if call_kind == CallKind::Call && ecx.is_static() && !value.is_zero() { return Err(InstrStop::CallNotAllowedInsideStatic.into()); } value } CallKind::DelegateCall | CallKind::StaticCall => U256::ZERO, }; - let transfers_value = value != U256::ZERO; + let transfers_value = !value.is_zero(); pop!(sp; in_offset, in_len, out_offset, out_len); @@ -1232,8 +1232,8 @@ mod tests { #[test] fn call_builtin_executes_message() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let child_output = Bytes::from_static(&[0xaa, 0xbb, 0xcc]); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector { @@ -1268,7 +1268,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::Call) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); assert_eq!(frame.ecx.return_data(), child_output.as_ref()); assert_eq!(frame.ecx.memory().slice(8, 2), &[0xaa, 0xbb]); let output = b"frame-output"; @@ -1290,9 +1290,9 @@ mod tests { #[test] fn callcode_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let stack_value = Word::from(0x12); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); @@ -1313,7 +1313,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::CallCode) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1328,9 +1328,9 @@ mod tests { #[test] fn delegatecall_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let current_value = Word::from(0x99); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); @@ -1351,7 +1351,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::DelegateCall) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1366,9 +1366,9 @@ mod tests { #[test] fn staticcall_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); let message = MessageExt { @@ -1388,7 +1388,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::StaticCall) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1403,7 +1403,7 @@ mod tests { #[test] fn create_builtin_executes_message() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let initcode = [op::STOP]; let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector { @@ -1445,7 +1445,7 @@ mod tests { #[test] fn create2_builtin_maps_salt() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let initcode = [op::STOP]; let salt = EvmWord::from(Word::from(0xabcdu64)); let mut host = base_evm(SpecId::CANCUN); @@ -1490,10 +1490,10 @@ mod tests { #[test] fn create_builtin_preaccess_failure_skips_child_message() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); for create_kind in [CreateKind::Create, CreateKind::Create2] { for (balance, nonce, value, depth) in [ - (Word::ZERO, 0, Word::from(1), 0), + (Word::ZERO, 0, Word::ONE, 0), (Word::MAX, u64::MAX, Word::ZERO, 0), (Word::MAX, 0, Word::ZERO, CALL_DEPTH_LIMIT), ] { diff --git a/crates/jit/builtins/src/utils.rs b/crates/jit/builtins/src/utils.rs index 33557266b..90ba7aece 100644 --- a/crates/jit/builtins/src/utils.rs +++ b/crates/jit/builtins/src/utils.rs @@ -38,7 +38,7 @@ pub(crate) fn require_non_staticcall( #[inline] pub(crate) fn word_to_u64_saturated(value: U256) -> u64 { - value.try_into().unwrap_or(u64::MAX) + value.saturating_to::() } // JIT code writes stack words with 8-byte stores, so read them back with scalar loads. The @@ -56,7 +56,7 @@ pub(crate) fn word_to_usize(value: U256) -> Result { /// Converts a word to `usize`, saturating on overflow. #[inline] pub(crate) fn word_to_usize_saturated(value: U256) -> usize { - value.try_into().unwrap_or(usize::MAX) + value.saturating_to::() } /// Loads an account, handling cold load gas accounting. diff --git a/crates/jit/codegen/src/bytecode/asm.rs b/crates/jit/codegen/src/bytecode/asm.rs index ddd23c55b..942ea1547 100644 --- a/crates/jit/codegen/src/bytecode/asm.rs +++ b/crates/jit/codegen/src/bytecode/asm.rs @@ -562,12 +562,13 @@ fn parse_items<'a>(tokens: &[Token<'a>]) -> Result>> { push_kind: PushKind::Fixed(imm_len), })); } else { - if matches!( - tokens.get(i), - Some(Token { kind: TokenKind::Number(_), .. }) - ) { - eyre::bail!("unexpected immediate for opcode {opc}"); - } + eyre::ensure!( + !matches!( + tokens.get(i), + Some(Token { kind: TokenKind::Number(_), .. }) + ), + "unexpected immediate for opcode {opc}" + ); items.push(Item::Inst(Inst { opcode, imm: None, diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index 73efd0fee..6617fbd6d 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -2428,10 +2428,10 @@ pub(crate) mod tests { ", ); // At inst 2 (ADD), operand 0 (TOS) = 0x01, operand 1 = 0x42. - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::from(0x01))); + assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::ONE)); assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); // At inst 4 (MSTORE), operand 0 (TOS) = 0x00, operand 1 = 0x43 (folded ADD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 0), Some(U256::from(0x00))); + assert_eq!(bytecode.const_operand(Inst::from_usize(4), 0), Some(U256::ZERO)); assert_eq!(bytecode.const_operand(Inst::from_usize(4), 1), Some(U256::from(0x43))); } @@ -2471,7 +2471,7 @@ pub(crate) mod tests { ", ); assert_eq!(bytecode.const_output(Inst::from_usize(0)), Some(U256::from(0x42))); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), Some(U256::from(0x01))); + assert_eq!(bytecode.const_output(Inst::from_usize(1)), Some(U256::ONE)); assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); assert_eq!(bytecode.const_output(Inst::from_usize(3)), Some(U256::ZERO)); // MSTORE has no outputs. @@ -2543,12 +2543,12 @@ pub(crate) mod tests { assert_eq!(bytecode.const_operand(Inst::from_usize(5), 0), None); // ADD 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::from(0x01))); + assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::ONE)); assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); // ADD 2 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::from(0x01))); + assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::ONE)); assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), None); assert_eq!(bytecode.const_output(Inst::from_usize(8)), None); @@ -3688,7 +3688,7 @@ mod tests_edge_cases { // DUP1 at inst 2: counter is loop-variant. assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), None); // ADD at inst 9: TOS=1 (const), second=counter (Top). - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(1))); + assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ONE)); assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); } @@ -3900,9 +3900,9 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Purely local push output preserved. - assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::from(0x01))); + assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::ONE)); // ADD: local operand preserved, inherited operand restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(0x01))); + assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ONE)); assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); assert_eq!(bytecode.const_output(Inst::from_usize(9)), None); } @@ -3986,7 +3986,7 @@ mod tests_edge_cases { assert_eq!(bytecode.const_operand(Inst::from_usize(6), 1), Some(U256::from(0x0A))); assert_eq!(bytecode.const_output(Inst::from_usize(6)), Some(U256::from(0x15))); // MSTORE 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::from(0))); + assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::ZERO)); assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), Some(U256::from(0x15))); // MLOAD: single predecessor (no JUMPDEST), so cross-block value resolves. assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(0x69))); @@ -4406,7 +4406,7 @@ mod tests_edge_cases { fn mload_known_word_after_mstore() { let known = Some(U256::from(0x80)); assert_eq!(mload_0x40_after("PUSH 1 PUSH0 CALLDATALOAD MSTORE"), None); - assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x40 MSTORE"), Some(U256::from(1))); + assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x40 MSTORE"), Some(U256::ONE)); assert_eq!(mload_0x40_after("PUSH0 CALLDATALOAD PUSH 0x40 MSTORE"), None); assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x21 MSTORE"), None); assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x5f MSTORE"), None); diff --git a/crates/jit/codegen/src/bytecode/passes/const_fold.rs b/crates/jit/codegen/src/bytecode/passes/const_fold.rs index f50b3f777..cba7c85d0 100644 --- a/crates/jit/codegen/src/bytecode/passes/const_fold.rs +++ b/crates/jit/codegen/src/bytecode/passes/const_fold.rs @@ -129,10 +129,10 @@ pub(crate) fn try_const_fold( op::EXP => a.pow(b), op::SIGNEXTEND => { if a < U256::from(31) { - let ext = a.as_limbs()[0]; + let ext = a.wrapping_to::(); let bit_index = (8 * ext + 7) as usize; let bit = b.bit(bit_index); - let mask = (U256::from(1) << bit_index) - U256::from(1); + let mask = (U256::ONE << bit_index) - U256::ONE; if bit { b | !mask } else { b & mask } } else { b @@ -510,7 +510,7 @@ mod tests { #[test] fn const_fold_1_to_1() { // ISZERO. - assert_eq!(const_fold(op::ISZERO, &[U256::ZERO]), Some(U256::from(1))); + assert_eq!(const_fold(op::ISZERO, &[U256::ZERO]), Some(U256::ONE)); assert_eq!(const_fold(op::ISZERO, &[U256::from(42)]), Some(U256::ZERO)); // NOT. @@ -521,7 +521,7 @@ mod tests { #[test] fn const_fold_add() { assert_eq!(const_fold(op::ADD, &[U256::from(3), U256::from(4)]), Some(U256::from(7))); - assert_eq!(const_fold(op::ADD, &[U256::MAX, U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::ADD, &[U256::MAX, U256::ONE]), Some(U256::ZERO)); } #[test] @@ -534,7 +534,7 @@ mod tests { fn const_fold_sub() { // SUB(10, 3) = 7. assert_eq!(const_fold(op::SUB, &[U256::from(10), U256::from(3)]), Some(U256::from(7))); - assert_eq!(const_fold(op::SUB, &[U256::ZERO, U256::from(1)]), Some(U256::MAX)); + assert_eq!(const_fold(op::SUB, &[U256::ZERO, U256::ONE]), Some(U256::MAX)); } #[test] @@ -557,7 +557,7 @@ mod tests { #[test] fn const_fold_mod() { // MOD(10, 3) = 1. - assert_eq!(const_fold(op::MOD, &[U256::from(10), U256::from(3)]), Some(U256::from(1))); + assert_eq!(const_fold(op::MOD, &[U256::from(10), U256::from(3)]), Some(U256::ONE)); assert_eq!(const_fold(op::MOD, &[U256::from(7), U256::ZERO]), Some(U256::ZERO)); } @@ -580,7 +580,7 @@ mod tests { Some(U256::from(4)), ); assert_eq!( - const_fold(op::ADDMOD, &[U256::from(1), U256::from(2), U256::ZERO]), + const_fold(op::ADDMOD, &[U256::ONE, U256::from(2), U256::ZERO]), Some(U256::ZERO), ); } @@ -599,7 +599,7 @@ mod tests { // EXP(2, 10) = 1024. assert_eq!(const_fold(op::EXP, &[U256::from(2), U256::from(10)]), Some(U256::from(1024))); // EXP(5, 0) = 1. - assert_eq!(const_fold(op::EXP, &[U256::from(5), U256::ZERO]), Some(U256::from(1))); + assert_eq!(const_fold(op::EXP, &[U256::from(5), U256::ZERO]), Some(U256::ONE)); } #[test] @@ -621,12 +621,12 @@ mod tests { #[test] fn const_fold_lt_gt_eq() { // LT(1, 2) = 1. - assert_eq!(const_fold(op::LT, &[U256::from(1), U256::from(2)]), Some(U256::from(1))); - assert_eq!(const_fold(op::LT, &[U256::from(2), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::LT, &[U256::ONE, U256::from(2)]), Some(U256::ONE)); + assert_eq!(const_fold(op::LT, &[U256::from(2), U256::ONE]), Some(U256::ZERO)); // GT(2, 1) = 1. - assert_eq!(const_fold(op::GT, &[U256::from(2), U256::from(1)]), Some(U256::from(1))); - assert_eq!(const_fold(op::GT, &[U256::from(1), U256::from(2)]), Some(U256::ZERO)); - assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(5)]), Some(U256::from(1))); + assert_eq!(const_fold(op::GT, &[U256::from(2), U256::ONE]), Some(U256::ONE)); + assert_eq!(const_fold(op::GT, &[U256::ONE, U256::from(2)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(5)]), Some(U256::ONE)); assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(6)]), Some(U256::ZERO)); } @@ -634,11 +634,11 @@ mod tests { fn const_fold_slt_sgt() { let neg1 = U256::MAX; // SLT(-1, 1) = 1. - assert_eq!(const_fold(op::SLT, &[neg1, U256::from(1)]), Some(U256::from(1))); + assert_eq!(const_fold(op::SLT, &[neg1, U256::ONE]), Some(U256::ONE)); // SLT(1, -1) = 0. - assert_eq!(const_fold(op::SLT, &[U256::from(1), neg1]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SLT, &[U256::ONE, neg1]), Some(U256::ZERO)); // SGT(1, -1) = 1. - assert_eq!(const_fold(op::SGT, &[U256::from(1), neg1]), Some(U256::from(1))); + assert_eq!(const_fold(op::SGT, &[U256::ONE, neg1]), Some(U256::ONE)); } #[test] @@ -670,26 +670,23 @@ mod tests { #[test] fn const_fold_shl_shr() { // SHL(1, 0x80) = 0x100. - assert_eq!( - const_fold(op::SHL, &[U256::from(1), U256::from(0x80)]), - Some(U256::from(0x100)), - ); - assert_eq!(const_fold(op::SHL, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SHL, &[U256::ONE, U256::from(0x80)]), Some(U256::from(0x100)),); + assert_eq!(const_fold(op::SHL, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); // SHR(1, 0x80) = 0x40. - assert_eq!(const_fold(op::SHR, &[U256::from(1), U256::from(0x80)]), Some(U256::from(0x40)),); - assert_eq!(const_fold(op::SHR, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SHR, &[U256::ONE, U256::from(0x80)]), Some(U256::from(0x40)),); + assert_eq!(const_fold(op::SHR, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); } #[test] fn const_fold_sar() { // SAR(1, 2) = 1. - assert_eq!(const_fold(op::SAR, &[U256::from(1), U256::from(2)]), Some(U256::from(1))); + assert_eq!(const_fold(op::SAR, &[U256::ONE, U256::from(2)]), Some(U256::ONE)); // SAR(4, -1) = -1. assert_eq!(const_fold(op::SAR, &[U256::from(4), U256::MAX]), Some(U256::MAX)); // SAR(256, -1) = -1. assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::MAX]), Some(U256::MAX)); // SAR(256, 1) = 0. - assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); } /// Builds bytecode for N repetitions of `PUSH U256::MAX, PUSH U256::MAX, EXP, POP`. @@ -836,7 +833,7 @@ mod tests { #[test] fn const_fold_gas_sync() { let mut interner: crate::bytecode::U256Interner = crate::bytecode::Interner::new(); - let one = U256Imm::new(U256::from(1), &mut interner); + let one = U256Imm::new(U256::ONE, &mut interner); let two = U256Imm::new(U256::from(2), &mut interner); let three = U256Imm::new(U256::from(3), &mut interner); let c1 = AbsValue::Const(one); @@ -879,7 +876,7 @@ mod tests { (250, 258), ((1 << 31) - 4, (1 << 31) - 1), ]; - let consts = [U256::ZERO, U256::from(7), U256::from(225), U256::from(1) << 224, U256::MAX]; + let consts = [U256::ZERO, U256::from(7), U256::from(225), U256::ONE << 224, U256::MAX]; let mut inputs: Vec<_> = ranges.into_iter().map(|(lo, hi)| AbsValue::Range((lo..=hi).into())).collect(); inputs.extend(consts.into_iter().map(|c| AbsValue::Const(U256Imm::new(c, interner)))); @@ -895,7 +892,7 @@ mod tests { AbsValue::Top => [0u64, 1, 2, 7, 31, 32, 255, 256, (1 << 31) - 1, 1 << 31] .into_iter() .map(U256::from) - .chain([U256::from(1) << 224, U256::from(1) << 255, U256::MAX - U256::from(1)]) + .chain([U256::ONE << 224, U256::ONE << 255, U256::MAX - U256::ONE]) .chain([U256::MAX]) .collect(), AbsValue::ConstSet(_) => unreachable!(), @@ -968,7 +965,7 @@ mod tests { let (c16, c40, c224, c255, c256) = (c(16), c(40), c(224), c(255), c(256)); let (c0xffff, word) = (c(0xffff), c(0x1100_2233)); let mut c = |v: U256| AbsValue::Const(U256Imm::new(v, &mut interner)); - let (c_big, c_max) = (c(U256::from(1) << 226), c(U256::MAX)); + let (c_big, c_max) = (c(U256::ONE << 226), c(U256::MAX)); let top = AbsValue::Top; struct Case { diff --git a/crates/jit/codegen/src/tests/fibonacci.rs b/crates/jit/codegen/src/tests/fibonacci.rs index e18028fd1..b1742cb1f 100644 --- a/crates/jit/codegen/src/tests/fibonacci.rs +++ b/crates/jit/codegen/src/tests/fibonacci.rs @@ -91,8 +91,8 @@ const FIBONACCI_CODE: &[u8] = &[ ]; fn fibonacci_rust(n: u16) -> U256 { - let mut a = U256::from(0); - let mut b = U256::from(1); + let mut a = U256::ZERO; + let mut b = U256::ONE; for _ in 0..n { let tmp = a; a = b; diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 683d7a5d6..e68667a06 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -798,8 +798,8 @@ tests! { ].concat()), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A return through `JUMPI` whose condition is opaque and whose target has two callers. @@ -840,7 +840,7 @@ tests! { inspect_stack: Some(false), expected_return: InstrStop::Return, expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Jumps into a table whose index the analysis only knows to be at most 3. @@ -848,8 +848,8 @@ tests! { bytecode: &asm(&range_jump_table(2)), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x22).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x22).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x22).as_slice(), + expected_output: Some(B256::with_last_byte(0x22).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), range_jump_table_non_jumpdest_entry(@raw { @@ -898,8 +898,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Like the above, but pc 0 is a `JUMPDEST` and one caller passes 0, so the condition @@ -941,8 +941,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x07).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x07).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x07).as_slice(), + expected_output: Some(B256::with_last_byte(0x07).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The constant folding budget runs out between the fixpoint and the snapshot pass: 20 @@ -977,8 +977,8 @@ tests! { inspect_stack: Some(false), gas_limit: 1_000_000, expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Here the budget runs out before the fold inside `b` in the snapshot pass: the fixpoint @@ -1012,8 +1012,8 @@ tests! { inspect_stack: Some(false), gas_limit: 1_000_000, expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A function returns to pc 0, which is a `JUMPDEST`, so the entry block is also a @@ -1045,8 +1045,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // An unreached block ends in a `JUMPI` with an opaque target; it must not disturb the @@ -1079,8 +1079,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A function pointer chosen by `JUMPI`, called through a shared call block, where the @@ -1125,8 +1125,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0xf0).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0xf0).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0xf0).as_slice(), + expected_output: Some(B256::with_last_byte(0xf0).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // EXP folds to 1 in both call contexts, but its exponent differs, so its gas does too. @@ -1229,8 +1229,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(140).to_be_bytes::<32>(), - expected_output: Some(&U256::from(140).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(140).as_slice(), + expected_output: Some(B256::with_last_byte(140).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // One caller passes an invalid return address, so the return must stay dynamic and @@ -1283,8 +1283,8 @@ tests! { spec_id: SpecId::AMSTERDAM, inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Same with `DUPN` copying the deep return address. @@ -1317,8 +1317,8 @@ tests! { spec_id: SpecId::AMSTERDAM, inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Two callers of a function that moves its return address with `EXCHANGE`. @@ -2563,7 +2563,7 @@ tests! { address: DEF_ADDR, data: LogData::new( vec![0xffffffffffffffffffffffffffffffffffffffff_U256.into()], - Bytes::copy_from_slice(&[0; 50]), + Bytes::from([0; 50]), ).unwrap(), }]); }), @@ -3033,7 +3033,7 @@ tests! { ], spec_id: SpecId::CANCUN, modify_message: Some(|message| { - message.input = Bytes::copy_from_slice(&U256::from(0x68).to_be_bytes::<32>()); + message.input = Bytes::from(B256::with_last_byte(0x68)); message.value = U256::from(10); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, @@ -3327,9 +3327,9 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x2a).to_be_bytes::<32>(), + expected_memory: B256::with_last_byte(0x2a).as_slice(), expected_gas: GAS_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x2a).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x2a).as_slice()), }), // Same class of bug but triggered by a deduped reachable JUMPDEST block rather than @@ -3378,9 +3378,9 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x2a).to_be_bytes::<32>(), + expected_memory: B256::with_last_byte(0x2a).as_slice(), expected_gas: GAS_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x2a).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x2a).as_slice()), }), dedup_false_jumpi_non_jumpdest_fallthrough(@raw { @@ -3536,7 +3536,7 @@ tests! { spec_id: SpecId::ISTANBUL, modify_message: Some(|message| { message.value = 1_U256; - message.input = Bytes::from(&hex!("c0406226")); + message.input = alloy_primitives::bytes!("c0406226"); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, expected_stack: STACK_WHAT_INTERPRETER_SAYS, @@ -3628,7 +3628,7 @@ tests! { modify_message: Some(|message| { let mut input = [0; 96]; input[63] = 1; - message.input = Bytes::copy_from_slice(&input); + message.input = Bytes::from(input); }), inspect_stack: Some(false), expected_return: InstrStop::Stop, diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index 32549475c..9fe0e3415 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -1,4 +1,5 @@ use super::*; +use alloy_primitives::address; use evm2::{ BaseEvmTypes, Evm, Precompiles, bytecode::Bytecode, @@ -58,11 +59,11 @@ impl DefEnv { pub fn def_env() -> DefEnv { DefEnv { tx: DefTx { - caller: Address::new(hex!("cc0102030405060708090a0b0c0d0e0f101112dd")), + caller: address!("cc0102030405060708090a0b0c0d0e0f101112dd"), blob_hashes: vec![B256::repeat_byte(0x01), B256::repeat_byte(0x02)], }, block: DefBlock { - coinbase: Address::new(hex!("cb1112131415161718191a1b1c1d1e1f202122ee")), + coinbase: address!("cb1112131415161718191a1b1c1d1e1f202122ee"), timestamp: uint!(0x01_0000000000000002_0000000000001234_U256), number: DEF_BN, difficulty: uint!(0x03_0000000000000004_000000000000cdef_U256), @@ -187,8 +188,8 @@ pub const GAS_WHAT_INTERPRETER_SAYS: u64 = 0x4682e332d6612de1; pub fn def_storage() -> &'static HashMap { DEF_STORAGE.get_or_init(|| { let mut map = HashMap::default(); - map.insert(U256::from(0), U256::from(1)); - map.insert(U256::from(1), U256::from(2)); + map.insert(U256::ZERO, U256::ONE); + map.insert(U256::ONE, U256::from(2)); map.insert(U256::from(69), U256::from(42)); map }) @@ -318,7 +319,7 @@ fn def_database() -> InMemoryDB { for (key, value) in def_storage() { database.insert_account_storage(&DEF_ADDR, key, value); } - for number in [DEF_BN - U256::from(1), DEF_BN - U256::from(255), DEF_BN - U256::from(256)] { + for number in [DEF_BN - U256::ONE, DEF_BN - U256::from(255), DEF_BN - U256::from(256)] { database.insert_block_hash(&number, &number.into()); } database @@ -334,8 +335,8 @@ pub struct HostState { impl HostState { fn from_evm(evm: &mut Evm<'_, BaseEvmTypes>) -> Self { let keys = [ - U256::from(0), - U256::from(1), + U256::ZERO, + U256::ONE, U256::from(69), U256::from(70), U256::from(200), @@ -375,14 +376,9 @@ fn prepare_host(spec_id: SpecId) -> Evm<'static, BaseEvmTypes> { { evm.state_mut().prewarm(&address); } - for key in [ - U256::from(0), - U256::from(1), - U256::from(69), - U256::from(70), - U256::from(200), - U256::from(0xff), - ] { + for key in + [U256::ZERO, U256::ONE, U256::from(69), U256::from(70), U256::from(200), U256::from(0xff)] + { evm.state_mut().prewarm_storage_slot(&DEF_ADDR, key); } evm diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 6eb9210be..61dc8b332 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -8,7 +8,7 @@ extern crate tracing; use alloy_primitives::map::{FxBuildHasher, HashSet}; use evm2_jit_backend::{ Backend, BackendConfig, BackendTypes, Builder, CallConv, IntCC, OptimizationLevel, Result, - TailCallKind, TypeMethods, U256, eyre, format_bytes, + TailCallKind, TypeMethods, U256, eyre, eyre::OptionExt, format_bytes, }; use llvm_sys::{ LLVMCallConv, LLVMIntPredicate, LLVMLinkage, LLVMModuleFlagBehavior, LLVMTailCallKind, @@ -510,7 +510,7 @@ impl EvmLlvmBackend { if aot { LLVMRelocMode::LLVMRelocPIC } else { LLVMRelocMode::LLVMRelocStatic }, LLVMCodeModel::LLVMCodeModelDefault, ) - .ok_or_else(|| eyre::eyre!("failed to create target machine"))?; + .ok_or_eyre("failed to create target machine")?; // In JIT mode, ORC owns the context via a ThreadSafeContext so that modules can be // safely transferred to the JIT without double-ownership issues with the TLS context. diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index d5b28755b..37ba7d2fc 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -369,14 +369,14 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_call_executes_message() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default()); database.insert_account_info( &target, AccountInfo::default() - .with_code(Bytecode::new_legacy(Bytes::copy_from_slice(BYTECODE_RET42))), + .with_code(Bytecode::new_legacy(Bytes::from_static(BYTECODE_RET42))), ); let backend = blocking_backend(); let mut host = Evm::::new( @@ -421,7 +421,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_create_executes_message() { - let creator = Address::from([0x33; 20]); + let creator = Address::repeat_byte(0x33); let mut database = InMemoryDB::default(); database.insert_account_info(&creator, AccountInfo::default()); let backend = blocking_backend(); @@ -465,7 +465,7 @@ mod tests { let message = MessageExt { gas_limit: 1_000_000, destination: creator, - code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), + code: Bytecode::new_legacy(Bytes::from(code)), ..MessageExt::default() }; let mut interpreter = Interpreter::::new(&message); @@ -478,7 +478,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_staticcall_zeros_child_callvalue() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let target = Address::with_last_byte(0x68); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default()); @@ -538,7 +538,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_staticcall_child_callvalue_does_not_transfer() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let child = Address::with_last_byte(0x68); let beneficiary = Address::with_last_byte(0x69); let initial_beneficiary_balance = Word::from(7); @@ -607,18 +607,17 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_dynamic_call_to_staticcall_loop_matches_interpreter_gas() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let outer = Address::with_last_byte(0xfd); let middle = Address::with_last_byte(0xed); let leaf = Address::with_last_byte(0xdd); let outer_code = dynamic_call_outer_code(); - let mut input = [0u8; 32]; - input[12..].copy_from_slice(middle.as_slice()); + let input = middle.into_word().0; let message = MessageExt { gas_limit: 0xcd79195900 - 21_368, destination: outer, caller, - input: Bytes::copy_from_slice(&input), + input: Bytes::from(input), value: Word::from(10), code: Bytecode::new_legacy(Bytes::copy_from_slice(&outer_code)), code_address: outer, @@ -630,7 +629,7 @@ mod tests { .insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(100))); database .insert_account_info(&outer, AccountInfo::default().with_balance(Word::from(10))); - database.insert_account_storage(&outer, &Word::from(1), &Word::from(1)); + database.insert_account_storage(&outer, &Word::ONE, &Word::ONE); database.insert_account_info( &middle, AccountInfo::default() @@ -672,6 +671,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_precompile_dispatch_uses_original_callee() { + use alloy_primitives::bytes; let sha256 = Address::with_last_byte(2); let delegate = Address::with_last_byte(0x33); let parent = Address::with_last_byte(0x44); @@ -709,7 +709,7 @@ mod tests { call_target: parent, code_address: parent, caller, - input: hex!("01020304").into(), + input: bytes!("01020304"), code: Bytecode::new_legacy(code.into()), ..Default::default() }; @@ -927,7 +927,7 @@ mod tests { let returned = Word::from_be_slice(&compiled.0.output); if descendants == 1023 { let expected = if kind == MessageKind::Call { - Word::from(1) + Word::ONE } else { address_to_word(&destination) }; @@ -954,7 +954,7 @@ mod tests { mod custom_host_tests { use super::{tests::blocking_backend, *}; use crate::runtime::{HostConfig, RuntimeConfig}; - use alloy_primitives::{Address, Bytes, U256}; + use alloy_primitives::{Address, B256, Bytes, U256}; use evm2::{ BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, ExecutionError, OpcodeConfig, SpecId, @@ -1083,7 +1083,7 @@ mod custom_host_tests { cx.gas.spend(7)?; // Rewrites existing memory, which compiled code must not assume it still knows. if cx.state.memory().len() >= 32 { - cx.state.memory().set(0, &Word::from(2_u64).to_be_bytes::<32>()); + cx.state.memory().set(0, B256::with_last_byte(2).as_slice()); } *out = Word::from(0xbeef_u64); } diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index 53810d101..14d67ffb1 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -1572,7 +1572,7 @@ mod tests { let job = CompileJob { kind: CompilationKind::Jit, key: RuntimeCacheKey { code_hash: keccak256(BYTECODE_RET42), spec_id: SpecId::CANCUN }, - bytecode: Bytes::copy_from_slice(BYTECODE_RET42), + bytecode: Bytes::from_static(BYTECODE_RET42), symbol_name: symbol_name.to_owned(), opt_level: config.tuning.jit_opt_level, sync_notifier: SyncNotifier::none(), diff --git a/crates/jit/runtime/src/runtime/mod.rs b/crates/jit/runtime/src/runtime/mod.rs index 7e40c4365..7a40060a8 100644 --- a/crates/jit/runtime/src/runtime/mod.rs +++ b/crates/jit/runtime/src/runtime/mod.rs @@ -179,16 +179,16 @@ impl JitBackend { if config.jit_mode == JitMode::OutOfProcess { eyre::bail!("out-of-process JIT is only available on Unix"); } - if config.aot && config.store.is_none() { - eyre::bail!("AOT mode requires an artifact store"); - } - if config.aot && config.jit_mode == JitMode::OutOfProcess { - eyre::bail!("AOT mode does not support out-of-process compilation"); - } + eyre::ensure!(!config.aot || config.store.is_some(), "AOT mode requires an artifact store"); + eyre::ensure!( + !config.aot || config.jit_mode != JitMode::OutOfProcess, + "AOT mode does not support out-of-process compilation" + ); - if !config.host.is_default() && (config.aot || config.store.is_some()) { - eyre::bail!("custom hosts require JIT without an artifact store"); - } + eyre::ensure!( + config.host.is_default() || (!config.aot && config.store.is_none()), + "custom hosts require JIT without an artifact store" + ); let enabled = config.enabled; let (tx, rx) = chan::bounded::(config.tuning.channel_capacity); let events = ArrayQueue::new(config.tuning.channel_capacity); diff --git a/crates/jit/runtime/src/runtime/out_of_process.rs b/crates/jit/runtime/src/runtime/out_of_process.rs index 07fb64d80..3a773a9b2 100644 --- a/crates/jit/runtime/src/runtime/out_of_process.rs +++ b/crates/jit/runtime/src/runtime/out_of_process.rs @@ -19,6 +19,7 @@ use evm2::{ version::{GasId, GasParams}, }; use evm2_jit_codegen::HostLayout; +use eyre::OptionExt; use rayon::ThreadPoolBuilder; use std::{ cell::RefCell, @@ -797,8 +798,7 @@ fn read_helper_request(stdin: &mut BufReader) -> eyre::Resu eyre::bail!("JIT helper received duplicate init"); } }; - let spec_id = SpecId::try_from_u32(u32::from(req.spec_id)) - .ok_or_else(|| eyre::eyre!("invalid spec id"))?; + let spec_id = SpecId::try_from_u32(u32::from(req.spec_id)).ok_or_eyre("invalid spec id")?; let opt_level = opt_level_from_u8(req.opt_level)?; let job = CompileJob { @@ -900,9 +900,7 @@ fn gas_params_to_pairs(gas_params: &GasParams) -> GasParamPairs { } fn gas_params_from_pairs(pairs: GasParamPairs) -> eyre::Result { - if pairs.len() != GAS_PARAM_COUNT { - eyre::bail!("invalid gas params length: {}", pairs.len()); - } + eyre::ensure!(pairs.len() == GAS_PARAM_COUNT, "invalid gas params length: {}", pairs.len()); let mut seen = [false; GAS_PARAM_COUNT]; let mut gas_params = Version::base(SpecId::default()).gas_params; @@ -911,9 +909,7 @@ fn gas_params_from_pairs(pairs: GasParamPairs) -> eyre::Result { let Some(gas_id) = GasId::from_usize(index) else { eyre::bail!("invalid gas param id: {id}"); }; - if seen[index] { - eyre::bail!("duplicate gas param id: {id}"); - } + eyre::ensure!(!seen[index], "duplicate gas param id: {id}"); seen[index] = true; gas_params.set(gas_id, value); } diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index 385e10168..393eda012 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -598,7 +598,7 @@ fn prepare_aot_enqueue() { let tb = TestBackend::with_tuning(RuntimeTuning { jit_worker_count: 0, ..Default::default() }); tb.prepare_aot(AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }); } @@ -609,12 +609,12 @@ fn prepare_aot_batch_enqueue() { let reqs = vec![ AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }, AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_ADD), - code: Bytes::copy_from_slice(BYTECODE_ADD), + code: Bytes::from_static(BYTECODE_ADD), spec_id: SpecId::CANCUN, }, ]; @@ -636,7 +636,7 @@ fn prepare_aot_batch_never_blocks_when_startup_fails() { let worker = std::thread::spawn(move || { let reqs = vec![AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }]; // More requests than the channel can hold: deadlocks without the started check. @@ -1300,7 +1300,7 @@ fn memory_budget_evicts_aot_entries() { tb.prepare_aot_sync(AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1531,7 +1531,7 @@ fn prepare_aot_persist_and_load() { let code_hash = alloy_primitives::keccak256(BYTECODE_RET42); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1711,7 +1711,7 @@ fn aot_artifacts_survive_restart() { tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1749,7 +1749,7 @@ fn preload_aot_seeds_resident() { tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1777,7 +1777,7 @@ fn preload_aot_skips_stale_abi_version() { }); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1816,7 +1816,7 @@ fn preload_aot_skips_manifest_key_mismatch() { }); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1837,7 +1837,7 @@ fn preload_aot_skips_manifest_key_mismatch() { // The probe rejects it too, so the artifact is recompiled. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1875,7 +1875,7 @@ fn artifacts_are_scoped_to_compile_config() { let code_hash = alloy_primitives::keccak256(BYTECODE_RET42); let req = || AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }; let config = |gas_params| RuntimeConfig { @@ -1923,7 +1923,7 @@ fn prepare_aot_already_persisted_not_resident() { // Compile and persist an AOT artifact. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1938,7 +1938,7 @@ fn prepare_aot_already_persisted_not_resident() { // Requesting prepare_aot again should load from store without recompiling. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1980,11 +1980,8 @@ fn prepare_aot_persists_jit_resident() { tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); assert_eq!(tb.stats().compilations_dispatched, dispatched_before); - let req = AotRequest { - code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), - spec_id: SpecId::CANCUN, - }; + let req = + AotRequest { code_hash, code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN }; tb.prepare_aot_sync(req.clone()).unwrap(); assert_eq!(store.len(), 1); From 687e83e9dc8c2bc21e42afda8da7cdea69c8afb5 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:53:28 +0100 Subject: [PATCH 124/159] refactor: apply slapslop helper swaps (#1011) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Applies the helper swaps from the same [slapslop](https://github.com/tempoxyz/slapslop) run that slapslop only suggested, on top of #1010. They replace hand-written code with an existing helper: getters and setters for fields (`is_success()`, EIP-7928 accessors, `present_value()`, `set_current`), `Inst::new` and the other index and constructor aliases, the `InMemoryDB` and `RecoveredTxEnvelope` aliases, `AddressMap`, `B256Map`, `U256Map` and their set and index-map variants, `saturating_to`, `Address::left_padding_from`, `hex!` for literal strings, `Log::new_unchecked`, `.concat()` for byte buffers, whole-struct `assert_eq!`, and short names for paths a module already imports. Every map I replaced already hashed with a per-process random seed (the alloy `HashMap` and `HashSet` and the std `HashMap` in the JIT test runner), and `IndexMap` keeps insertion order whatever the hasher, so iteration order is as unpredictable as before and nothing changes. I left out the suggestions that do not hold: the `as_slice()` removals in the four `PartialEq for StackRef` impls (they would recurse), `wrap_err` in the CLI (it changes the error's `Display`), the `boxed_inspector` swaps (none of the boxed values is an inspector), `assert_precompiles_downcast_mutable` (the call sites are not downcasts), the `TestEvm` and `BenchEvm` aliases (they fix the lifetime to `'static`), `transaction_request_set_gas_price` (no dependency provides `TransactionBuilder`), `as_contract_error` in `maybe_revert_reason` (it needs a clone), `Bytes::from` on a `Bloom` (no such impl), two `hex!` literals whose callee takes a `Vec`, and the `address_to_word` and `b256_to_word` calls in tests where `.into()` needs a type annotation, so those helpers stay. The transfer-log hunks stay out for #1005. To keep the public API unchanged, the `HashMap` and `HashSet` types stay in the getters of `AccessListInspector`, `StorageInspector` and `ParityTraceBuilder::callers` and in the `BundleState::new` bound, so their fixed-bytes map swaps are out. Retired instructions per call (`perf stat -e instructions:u`, difference between two iteration counts, mean of 5 alternating runs, `profiling` profile with `RUSTFLAGS=-Csymbol-mangling-version=v0` to match the CI build instead of the local native `target-cpu`, AMD EPYC 4585PX; spread of the slower side in parentheses). Changes under 1% are code layout, not a performance claim. #1010 against this PR: | Benchmark | Interpreter | JIT | | --- | ---: | ---: | | `snailtracer` | 474,947,909 → 475,262,511 ⚪ +0.1% (±0.4%) | 193,589,440 → 194,544,010 ⚪ +0.5% (±0.4%) | | `hash_10k` | 17,032,810 → 17,032,879 ⚪ +0.0% (±0.0%) | 9,071,774 → 9,071,637 ⚪ -0.0% (±0.0%) | | `erc20_transfer` | 26,335,385 → 26,302,172 ⚪ -0.1% (±0.0%) | 17,125,290 → 17,127,038 ⚪ +0.0% (±0.0%) | | `burntpix` | 384,326,275 → 384,368,312 ⚪ +0.0% (±0.1%) | 99,117,797 → 99,004,410 ⚪ -0.1% (±0.2%) | | `curve_stableswap` | 637,214 → 637,207 ⚪ -0.0% (±0.0%) | 247,746 → 247,645 ⚪ -0.0% (±0.3%) | | `aggregation_router_v6` | 778,396 → 780,480 ⚪ +0.3% (±0.5%) | 427,242 → 431,283 ⚪ +0.9% (±1.9%) | | `entry_point_v0_7` | 1,597,598 → 1,589,419 ⚪ -0.5% (±0.3%) | 1,368,777 → 1,371,976 ⚪ +0.2% (±0.6%) | --- crates/cli/benches/evm/jit.rs | 7 +- crates/cli/benches/evm/support.rs | 7 +- crates/cli/src/capture/export.rs | 5 +- crates/cli/src/capture/mod.rs | 5 +- crates/cli/src/fuzzer/backend.rs | 8 +- crates/cli/src/fuzzer/case.rs | 7 +- crates/cli/src/fuzzer/normalize.rs | 16 +- crates/cli/src/fuzzer/precompile.rs | 4 +- crates/cli/src/replay/mod.rs | 4 +- crates/cli/src/run.rs | 6 +- crates/eest/src/blockchaintest/execute.rs | 23 +- crates/eest/src/execute.rs | 12 +- crates/eest/src/tx.rs | 5 +- crates/evm2/examples/bal_build.rs | 12 +- crates/evm2/examples/bal_parallel.rs | 4 +- crates/evm2/examples/custom_evm/main.rs | 36 ++- .../src/block/bundle_state/bundle_account.rs | 10 +- .../src/block/bundle_state/bundle_state.rs | 12 +- crates/evm2/src/block/bundle_state/reverts.rs | 4 +- crates/evm2/src/block/ethereum/execution.rs | 12 +- crates/evm2/src/block/ethereum/mod.rs | 2 +- crates/evm2/src/block/ethereum/tests.rs | 2 +- crates/evm2/src/block/mod.rs | 2 +- crates/evm2/src/block/state.rs | 2 +- crates/evm2/src/ethereum/eip4844.rs | 5 +- crates/evm2/src/ethereum/mod.rs | 6 +- crates/evm2/src/evm/async.rs | 8 +- crates/evm2/src/evm/bal/account.rs | 6 +- crates/evm2/src/evm/bal/changes.rs | 2 +- crates/evm2/src/evm/bal/list.rs | 30 +- crates/evm2/src/evm/db/cache.rs | 6 +- crates/evm2/src/evm/inspector.rs | 12 +- crates/evm2/src/evm/mod.rs | 51 ++- crates/evm2/src/evm/prewarm_set.rs | 4 +- crates/evm2/src/evm/state/account.rs | 28 +- crates/evm2/src/evm/state/journal.rs | 23 +- crates/evm2/src/evm/state/mod.rs | 6 +- crates/evm2/src/evm/state/storage.rs | 2 +- crates/evm2/src/evm/system.rs | 26 +- .../src/interpreter/instructions/block.rs | 9 +- .../src/interpreter/instructions/crypto.rs | 4 +- .../evm2/src/interpreter/instructions/env.rs | 6 +- .../src/interpreter/instructions/stack.rs | 5 +- .../src/interpreter/instructions/system.rs | 22 +- crates/evm2/src/precompiles/bn254/mod.rs | 239 ++++++-------- .../kzg_point_evaluation/arkworks.rs | 2 +- .../precompiles/kzg_point_evaluation/mod.rs | 7 +- crates/evm2/src/precompiles/modexp.rs | 85 +++-- crates/evm2/src/precompiles/table.rs | 18 +- crates/evm2/src/tests.rs | 14 +- crates/evm2/src/utils.rs | 2 +- crates/inspectors/src/tracing/builder/geth.rs | 12 +- .../inspectors/src/tracing/builder/parity.rs | 7 +- crates/inspectors/src/tracing/erc7562.rs | 2 +- crates/inspectors/src/tracing/js/bindings.rs | 10 +- crates/inspectors/src/tracing/js/builtins.rs | 6 +- .../src/tracing/js/interrupt_tests.rs | 14 +- crates/inspectors/src/tracing/js/mod.rs | 18 +- .../src/tracing/js/snapshot_tests.rs | 4 +- crates/inspectors/src/tracing/keccak.rs | 7 +- crates/inspectors/src/tracing/mod.rs | 6 +- crates/inspectors/src/tracing/utils.rs | 2 +- crates/inspectors/tests/it/accesslist.rs | 40 ++- crates/inspectors/tests/it/compat.rs | 22 +- crates/inspectors/tests/it/geth.rs | 305 +++++++++--------- .../inspectors/tests/it/geth/consistency.rs | 5 +- .../inspectors/tests/it/geth/runtime_oog.rs | 10 +- crates/inspectors/tests/it/geth_js.rs | 135 ++++---- crates/inspectors/tests/it/geth_js/errors.rs | 21 +- crates/inspectors/tests/it/geth_js/values.rs | 6 +- crates/inspectors/tests/it/inputs.rs | 15 +- crates/inspectors/tests/it/opcode.rs | 54 ++-- crates/inspectors/tests/it/otterscan.rs | 7 +- crates/inspectors/tests/it/parity.rs | 56 ++-- .../tests/it/parity/state_diff_7702.rs | 6 +- .../tests/it/parity/state_diff_birth.rs | 15 +- .../it/parity/state_diff_selfdestruct.rs | 9 +- .../inspectors/tests/it/parity/vm_bytecode.rs | 7 +- crates/inspectors/tests/it/repro/mod.rs | 9 +- crates/inspectors/tests/it/stack.rs | 9 +- crates/inspectors/tests/it/transfer.rs | 19 +- crates/inspectors/tests/it/writer.rs | 13 +- crates/jit/builtins/src/lib.rs | 16 +- crates/jit/builtins/src/utils.rs | 13 +- crates/jit/codegen/src/bytecode/mod.rs | 26 +- .../src/bytecode/passes/block_analysis.rs | 137 ++++---- .../codegen/src/bytecode/passes/const_fold.rs | 6 +- .../src/bytecode/passes/dead_store_elim.rs | 90 +++--- .../src/bytecode/passes/memory_sections.rs | 2 +- .../codegen/src/bytecode/passes/sections.rs | 22 +- .../jit/codegen/src/compiler/translate/mod.rs | 6 +- crates/jit/codegen/src/tests/mod.rs | 15 +- crates/jit/codegen/src/tests/runner.rs | 17 +- crates/jit/context/src/lib.rs | 4 +- crates/jit/llvm/src/lib.rs | 50 +-- crates/jit/llvm/src/orc.rs | 2 +- crates/jit/llvm/src/wrap/di.rs | 2 +- crates/jit/llvm/src/wrap/ir.rs | 10 +- .../jit/runtime/src/runtime/out_of_process.rs | 2 +- crates/jit/runtime/src/runtime/worker.rs | 2 +- 100 files changed, 1019 insertions(+), 1089 deletions(-) diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index 4e898b3d4..13e5f6f12 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -115,9 +115,10 @@ impl PreparedBench { panic!("{} JIT benchmark transaction must execute: {err:?}", self.name) }); assert!( - interpreter.status, + interpreter.is_success(), "{} benchmark transaction must succeed: {:?}", - self.name, interpreter.stop + self.name, + interpreter.stop ); assert_eq!(interpreter, jit, "{} interpreter and JIT results differ", self.name); } @@ -219,7 +220,7 @@ impl InterpreterRunner for FixedJitRunner { } fn new_evm(spec: SpecId, block: BlockEnv, db: InMemoryDB) -> BenchEvm { - Evm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) + BenchEvm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) } fn new_compiler(opt_level: OptimizationLevel) -> LlvmCompiler { diff --git a/crates/cli/benches/evm/support.rs b/crates/cli/benches/evm/support.rs index a18e6024b..401348d2b 100644 --- a/crates/cli/benches/evm/support.rs +++ b/crates/cli/benches/evm/support.rs @@ -40,9 +40,10 @@ impl PreparedBench { panic!("{} benchmark transaction must execute: {err:?}", self.name) }); assert!( - result.status, + result.is_success(), "{} benchmark transaction must succeed: {:?}", - self.name, result.stop + self.name, + result.stop ); } @@ -80,5 +81,5 @@ impl Runner { } fn new_evm(spec: SpecId, block: BlockEnv, db: InMemoryDB) -> BenchEvm { - Evm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) + BenchEvm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) } diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index 56d06e90e..4924e105a 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -191,8 +191,7 @@ fn export_block(input: &CapturedBlock) -> Result { transactions: Some( block .body - .transactions - .iter() + .transactions() .zip(input.transactions.iter()) .map(|(tx, input_tx)| export_transaction(tx, input_tx.signer)) .collect(), @@ -265,7 +264,7 @@ fn export_transaction(tx: &EthereumTxEnvelope, signer: Address) -> Tr s: signature.s(), v: U256::from(signature_v(tx_type, signature.v(), tx.chain_id())), value: tx.value(), - to: tx.kind().into_to(), + to: tx.to(), chain_id: tx.chain_id().map(U256::from), access_list: tx.access_list().map(export_access_list), max_fee_per_gas: tx.tx_type().is_dynamic_fee().then(|| U256::from(tx.max_fee_per_gas())), diff --git a/crates/cli/src/capture/mod.rs b/crates/cli/src/capture/mod.rs index 720ad77f6..d150b6992 100644 --- a/crates/cli/src/capture/mod.rs +++ b/crates/cli/src/capture/mod.rs @@ -109,7 +109,7 @@ async fn capture( for (tx_index, ((pre_trace, diff_trace), tx)) in pre_traces .iter() .zip(diff_traces.iter()) - .zip(consensus_block.body.transactions.iter()) + .zip(consensus_block.body.transactions()) .enumerate() { let pre = parse::trace_result(pre_trace)?; @@ -214,8 +214,7 @@ async fn prepare_block(block: FetchedBlock) -> std::result::Result { let mut state = CanonicalState::default(); let Ok(tx_result) = result.commit_with(&mut state); - let output = if tx_result.status || tx_result.stop == InstrStop::Revert { + let output = if tx_result.is_success() || tx_result.stop == InstrStop::Revert { Some(tx_result.output.to_vec()) } else { None }; receipts.push(TxReceipt { - kind: if tx_result.status { + kind: if tx_result.is_success() { FuzzOutcomeKind::Success } else { FuzzOutcomeKind::RevertOrHalt @@ -99,7 +99,7 @@ type RevmExecutor = revm::MainnetEvm, - accounts: HashMap, + accounts: AddressMap, } impl EvmBackend for RevmBackend { diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 38ddfcad0..18783caa6 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -12,7 +12,10 @@ use alloy_eips::{ eip2930::{AccessList, AccessListItem}, eip7702::{Authorization, SignedAuthorization}, }; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, map::IndexMap}; +use alloy_primitives::{ + Address, B256, Bytes, TxKind, U256, + map::{IndexMap, U256IndexMap}, +}; use evm2::{ SpecId, env::{BlockEnv, BlockEnvExt}, @@ -735,7 +738,7 @@ pub(crate) struct CaseAccount { pub(crate) balance: U256, pub(crate) nonce: u64, pub(crate) code: Bytes, - pub(crate) storage: IndexMap, + pub(crate) storage: U256IndexMap, } mod spec_serde { diff --git a/crates/cli/src/fuzzer/normalize.rs b/crates/cli/src/fuzzer/normalize.rs index c5c93ca46..efc8d3aaa 100644 --- a/crates/cli/src/fuzzer/normalize.rs +++ b/crates/cli/src/fuzzer/normalize.rs @@ -1,5 +1,8 @@ use crate::fuzzer::case::EvmCase; -use alloy_primitives::{Address, B256, U256, keccak256, map::HashMap}; +use alloy_primitives::{ + Address, B256, U256, keccak256, + map::{AddressMap, HashMap}, +}; use core::convert::Infallible; use evm2::evm::{AccountChangeRef, StateChangeSink, StorageChange, registry::HandlerError}; use revm::{ @@ -95,7 +98,7 @@ impl fmt::Display for FuzzOutcomeKind { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct CanonicalState { - pub(crate) accounts: HashMap>, + pub(crate) accounts: AddressMap>, pub(crate) storage: HashMap<(Address, U256), U256>, } @@ -244,7 +247,7 @@ impl StateChangeSink for CanonicalState { pub(crate) fn state_from_revm( state: &revm::state::EvmState, - original_accounts: &HashMap, + original_accounts: &AddressMap, ) -> CanonicalState { let mut canonical = CanonicalState::default(); for (&address, account) in state { @@ -295,10 +298,7 @@ pub(crate) fn state_from_revm( canonical } -pub(crate) fn canonical_accounts( - case: &EvmCase, - accounts: &mut HashMap, -) { +pub(crate) fn canonical_accounts(case: &EvmCase, accounts: &mut AddressMap) { accounts.clear(); accounts.extend(case.accounts.iter().map(|account| { ( @@ -313,7 +313,7 @@ pub(crate) fn canonical_accounts( } pub(crate) fn apply_account_changes( - accounts: &mut HashMap, + accounts: &mut AddressMap, state: &CanonicalState, ) { for (&address, account) in &state.accounts { diff --git a/crates/cli/src/fuzzer/precompile.rs b/crates/cli/src/fuzzer/precompile.rs index aad18f1d4..468acadc5 100644 --- a/crates/cli/src/fuzzer/precompile.rs +++ b/crates/cli/src/fuzzer/precompile.rs @@ -10,9 +10,7 @@ pub(crate) struct PrecompileTarget { impl PrecompileTarget { pub(crate) fn address(self) -> Address { - let mut bytes = [0; 20]; - bytes[18..].copy_from_slice(&self.number.to_be_bytes()); - Address::new(bytes) + Address::left_padding_from(&self.number.to_be_bytes()) } pub(crate) const fn feature(self) -> FuzzFeatures { diff --git a/crates/cli/src/replay/mod.rs b/crates/cli/src/replay/mod.rs index fad320f08..58caa6e7a 100644 --- a/crates/cli/src/replay/mod.rs +++ b/crates/cli/src/replay/mod.rs @@ -466,14 +466,14 @@ fn ggas(gas_used: u128) -> f64 { #[cfg(test)] mod collect_tests { - use super::collect_json_files; + use super::{PathBuf, collect_json_files}; use std::{ fs, sync::atomic::{AtomicUsize, Ordering}, }; /// Creates a unique scratch directory under the system temp dir. - fn scratch_dir() -> std::path::PathBuf { + fn scratch_dir() -> PathBuf { static COUNTER: AtomicUsize = AtomicUsize::new(0); let unique = COUNTER.fetch_add(1, Ordering::Relaxed); let dir = std::env::temp_dir() diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 34071ef52..a614546e3 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -599,7 +599,7 @@ impl PreparedBench { let jit = self.run_jit()?; if self.assert_success { eyre::ensure!( - interpreter.status, + interpreter.is_success(), "{} benchmark transaction failed:\n interpreter: {:?}\n JIT: {:?}", self.name, interpreter, @@ -607,7 +607,7 @@ impl PreparedBench { ); } eyre::ensure!( - interpreter.status == jit.status + interpreter.is_success() == jit.is_success() && (!compare_gas || (interpreter.output == jit.output && interpreter.total_gas_spent == jit.total_gas_spent)), @@ -676,7 +676,7 @@ fn parse_fixture_bench(bench: &Bench, fixture_path: &str) -> eyre::Result( gas_used: gas.tx_gas_used(), execution_gas_used: gas.regular_gas_used(), state_gas_used: gas.state_gas_used(), - success: receipt.status(), + success: receipt.is_success(), logs: receipt.logs(), }); if let Some(base) = &receipt_base { @@ -867,7 +867,7 @@ impl ReceiptBuilder for HarnessReceiptBuilder { ) -> ReceiptEnvelope { let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; let receipt = Receipt { - status: Eip658Value::Eip658(result.status), + status: Eip658Value::Eip658(result.is_success()), cumulative_gas_used, logs: result.logs, }; @@ -1046,9 +1046,7 @@ fn reject_block( } } -fn parse_state( - accounts: &std::collections::BTreeMap, -) -> Result { +fn parse_state(accounts: &BTreeMap) -> Result { let mut database = InMemoryDB::default(); for (address, account) in accounts { let nonce = account.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; @@ -1166,7 +1164,7 @@ const fn base_block_reward(spec: SpecId) -> u128 { fn validate_post_state( database: &InMemoryDB, - expected: &std::collections::BTreeMap, + expected: &BTreeMap, ) -> Result<(), TestErrorKind> { for (address, expected_account) in expected { let info = database.cache.accounts.get(address).cloned().flatten().unwrap_or_default(); @@ -1287,7 +1285,7 @@ fn check_block_access_list( } } - if built.as_slice() == expected.as_slice() { + if built == *expected { return Ok(()); } Err(TestErrorKind::BlockAccessListMismatch { @@ -1297,18 +1295,15 @@ fn check_block_access_list( } /// Renders a compact per-account summary of how the built and expected block access lists differ. -fn format_bal_diff( - built: &alloy_eip7928::BlockAccessList, - expected: &alloy_eip7928::BlockAccessList, -) -> String { +fn format_bal_diff(built: &BlockAccessList, expected: &BlockAccessList) -> String { use std::{ collections::{BTreeMap, BTreeSet}, fmt::Write, }; - let built: BTreeMap<_, _> = built.iter().map(|account| (account.address, account)).collect(); + let built: BTreeMap<_, _> = built.iter().map(|account| (account.address(), account)).collect(); let expected: BTreeMap<_, _> = - expected.iter().map(|account| (account.address, account)).collect(); + expected.iter().map(|account| (account.address(), account)).collect(); let addresses: BTreeSet<_> = built.keys().chain(expected.keys()).copied().collect(); let mut out = String::new(); @@ -2346,7 +2341,7 @@ mod tests { assert_eq!(pass["errorMsg"], ""); let failure: Result, TestError> = Err(TestError::case( - std::path::Path::new("f.json"), + Path::new("f.json"), "case-b", TestErrorKind::UnexpectedFailure("boom".to_string()), )); diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index 332d8921f..76c3f914f 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -499,7 +499,7 @@ fn commit_system_call( data: Bytes, ) { let executed = evm.system_call(SystemTx::new(address, data)).expect("system call errored"); - assert!(executed.result().status, "pre-block system call failed: {address}"); + assert!(executed.result().is_success(), "pre-block system call failed: {address}"); let Ok(_) = executed.commit_with(post); } @@ -643,7 +643,6 @@ fn access_list( #[cfg(test)] mod tests { use super::*; - use alloy_primitives::LogData; use evm2::interpreter::op; #[cfg(feature = "jit")] @@ -705,10 +704,11 @@ mod tests { #[test] fn logs_hash_hashes_logs() { - let log = Log { - address: Address::repeat_byte(0x22), - data: LogData::new_unchecked(vec![B256::with_last_byte(1)], Bytes::from_static(&[2])), - }; + let log = Log::new_unchecked( + Address::repeat_byte(0x22), + vec![B256::with_last_byte(1)], + Bytes::from_static(&[2]), + ); assert_ne!(logs_hash(&[log]), B256::ZERO); } diff --git a/crates/eest/src/tx.rs b/crates/eest/src/tx.rs index b93aaa6df..aa3a126bc 100644 --- a/crates/eest/src/tx.rs +++ b/crates/eest/src/tx.rs @@ -1,6 +1,5 @@ use alloy_consensus::{ - EthereumTxEnvelope, SignableTransaction, TxEip4844, TxEnvelope as AlloyTxEnvelope, - TypedTransaction, + EthereumTxEnvelope, SignableTransaction, TxEip4844, TypedTransaction, crypto::{RecoveryError, secp256k1}, transaction::Recovered, }; @@ -151,7 +150,7 @@ pub(crate) fn build_signed_recovered_tx( ) -> Result<(RecoveredTxEnvelope, EthereumTxEnvelope), TxBuildError> { let tx = consensus_tx(fields)?; let caller = tx_caller(&tx, signer)?; - let signed = AlloyTxEnvelope::from(tx.clone().into_signed(signature)).into(); + let signed = tx.clone().into_signed(signature).into(); Ok((recovered_envelope(tx, caller), signed)) } diff --git a/crates/evm2/examples/bal_build.rs b/crates/evm2/examples/bal_build.rs index 97dc754f7..9aabf2daa 100644 --- a/crates/evm2/examples/bal_build.rs +++ b/crates/evm2/examples/bal_build.rs @@ -37,7 +37,11 @@ fn main() { let executed = evm .system_call(SystemTx::new(PRE_SYSTEM_CONTRACT, word(0xbeef))) .expect("pre-block system call should execute"); - assert!(executed.result().status, "pre-block system call failed: {:?}", executed.result()); + assert!( + executed.result().is_success(), + "pre-block system call failed: {:?}", + executed.result() + ); let _ = executed.commit(); // Transaction 0 is recorded at index 1: a plain value transfer to ALICE. @@ -45,7 +49,7 @@ fn main() { assert_eq!(evm.state_mut().bal_index(), idx(1)); let tx0 = transfer(CALLER, ALICE, 1_000_000, 0); let result = evm.transact(&tx0).expect("transaction 0 should execute").commit(); - assert!(result.status, "transaction 0 failed: {result:?}"); + assert!(result.is_success(), "transaction 0 failed: {result:?}"); // Transaction 1 is recorded at index 2: a call that stores 42 at slot 5. evm.state_mut().bump_bal_index(); @@ -61,7 +65,7 @@ fn main() { CALLER, ); let result = evm.transact(&tx1).expect("transaction 1 should execute").commit(); - assert!(result.status); + assert!(result.is_success()); // The post-block transition is recorded at the final index 3. A system call // commits through the same path as a transaction; block rewards and withdrawals, @@ -72,7 +76,7 @@ fn main() { let executed = evm .system_call(SystemTx::new(POST_SYSTEM_CONTRACT, word(0x22))) .expect("post-block system call should execute"); - assert!(executed.result().status); + assert!(executed.result().is_success()); let _ = executed.commit(); // Take the built BAL. Writes carry the block access index they happened at. diff --git a/crates/evm2/examples/bal_parallel.rs b/crates/evm2/examples/bal_parallel.rs index 567cae67c..6c01518ac 100644 --- a/crates/evm2/examples/bal_parallel.rs +++ b/crates/evm2/examples/bal_parallel.rs @@ -73,7 +73,7 @@ fn main() { for (i, handle) in handles.into_iter().enumerate() { let TxResultWithState { result, pending_state: pending, .. } = handle.join().expect("worker thread panicked"); - assert!(result.status); + assert!(result.is_success()); // The pending state still carries the change-set that persistence // consumers (e.g. reth) stream to the database through a @@ -109,7 +109,7 @@ fn build_bal(transactions: &[RecoveredTxEnvelope]) -> Bal { for tx in transactions { evm.state_mut().bump_bal_index(); let result = evm.transact(tx).expect("transaction should execute").commit(); - assert!(result.status); + assert!(result.is_success()); } evm.state_mut().take_bal_builder().expect("builder was enabled") } diff --git a/crates/evm2/examples/custom_evm/main.rs b/crates/evm2/examples/custom_evm/main.rs index 8d7db4a0f..2f104cadf 100644 --- a/crates/evm2/examples/custom_evm/main.rs +++ b/crates/evm2/examples/custom_evm/main.rs @@ -55,13 +55,13 @@ fn custom_opcode() -> HandlerResult<()> { println!( "custom opcode: tx_type=0x{:02x} expected status=true stop=Stop gas_used={expected_gas}; got status={} stop={:?} gas_used={}", tx.ty(), - result.status, + result.is_success(), result.stop, result.tx_gas_used(), ); assert_eq!(result.stop, InstrStop::Stop); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), expected_gas); assert!(result.ext.handled_custom_tx); Ok(()) @@ -86,22 +86,27 @@ fn state_change_consumer() -> HandlerResult<()> { println!( "state consumer: expected status=true accounts=0 storage_writes=1 slot=1 value=0xdead; got status={} accounts={} storage_writes={} slot={} value={:#x}", - result.status, + result.is_success(), consumer.account_changes, consumer.storage_changes.len(), storage_change.key, storage_change.current, ); - assert!(result.status); + assert!(result.is_success()); assert_eq!(consumer.bytecodes, 0); assert_eq!(consumer.account_changes, 0); assert_eq!(consumer.storage_wipes, 0); assert_eq!(consumer.storage_changes.len(), 1); - assert_eq!(storage_change.address, target); - assert_eq!(storage_change.key, U256::ONE); - assert_eq!(storage_change.original, U256::ZERO); - assert_eq!(storage_change.current, U256::from(0xdead_u64)); + assert_eq!( + *storage_change, + StorageChange { + address: target, + key: U256::ONE, + original: U256::ZERO, + current: U256::from(0xdead_u64), + } + ); Ok(()) } @@ -122,11 +127,13 @@ fn l1_blocknumber_opcode() -> HandlerResult<()> { println!( "l1 blocknumber opcode: expected status=true stop=Return output={expected:?}; got status={} stop={:?} output={:?}", - result.status, result.stop, result.output, + result.is_success(), + result.stop, + result.output, ); assert_eq!(result.stop, InstrStop::Return); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.output, expected); assert!(result.ext.handled_custom_tx); Ok(()) @@ -140,11 +147,12 @@ fn mainnet_fallback() -> HandlerResult<()> { println!( "mainnet fallback: expected status=false stop=OpcodeNotFound; got status={} stop={:?}", - result.status, result.stop, + result.is_success(), + result.stop, ); assert_eq!(result.stop, InstrStop::OpcodeNotFound); - assert!(!result.status); + assert!(!result.is_success()); Ok(()) } @@ -170,7 +178,7 @@ fn inspector() -> HandlerResult<()> { println!( "inspector: expected status=true initialized=1 steps=5 step_ends=5 logs=1 calls=1 opcodes={expected_opcodes:?}; got status={} initialized={} steps={} step_ends={} logs={} calls={} opcodes={:?}", - result.status, + result.is_success(), inspector_state.initialized, inspector_state.steps, inspector_state.step_ends, @@ -179,7 +187,7 @@ fn inspector() -> HandlerResult<()> { inspector_state.opcodes, ); - assert!(result.status); + assert!(result.is_success()); assert_eq!(inspector_state.initialized, 1); assert_eq!(inspector_state.steps, expected_opcodes.len()); assert_eq!(inspector_state.step_ends, expected_opcodes.len()); diff --git a/crates/evm2/src/block/bundle_state/bundle_account.rs b/crates/evm2/src/block/bundle_state/bundle_account.rs index ceb12a555..11563bc97 100644 --- a/crates/evm2/src/block/bundle_state/bundle_account.rs +++ b/crates/evm2/src/block/bundle_state/bundle_account.rs @@ -59,7 +59,7 @@ impl BundleAccount { /// In case we know that account is newly created or destroyed, return /// `Some(U256::ZERO)` pub fn storage_slot(&self, slot: U256) -> Option { - let slot = self.storage.get(&slot).map(|s| s.present_value); + let slot = self.storage.get(&slot).map(|s| s.present_value()); if slot.is_some() { slot } else if self.status.is_storage_known() { @@ -149,7 +149,7 @@ impl BundleAccount { storage_update: StorageWithOriginalValues| { this_storage.reserve(storage_update.len()); for (key, value) in storage_update { - this_storage.entry(key).or_insert(value).present_value = value.present_value; + this_storage.entry(key).or_insert(value).present_value = value.present_value(); } }; @@ -158,9 +158,7 @@ impl BundleAccount { updated_storage .iter() .filter(|s| s.1.is_changed()) - .map(|(key, value)| { - (*key, RevertToSlot::Some(value.previous_or_original_value)) - }) + .map(|(key, value)| (*key, RevertToSlot::Some(value.original_value()))) .collect() }; @@ -297,7 +295,7 @@ impl BundleAccount { let previous_storage = if transition.storage_was_destroyed { let mut storage = core::mem::take(&mut self.storage) .into_iter() - .map(|t| (t.0, RevertToSlot::Some(t.1.present_value))) + .map(|t| (t.0, RevertToSlot::Some(t.1.present_value()))) .collect::>(); for key in updated_storage.keys() { // As it is not existing inside Destroyed storage this means diff --git a/crates/evm2/src/block/bundle_state/bundle_state.rs b/crates/evm2/src/block/bundle_state/bundle_state.rs index 3a043d401..ac033a35a 100644 --- a/crates/evm2/src/block/bundle_state/bundle_state.rs +++ b/crates/evm2/src/block/bundle_state/bundle_state.rs @@ -600,7 +600,7 @@ impl BundleState { for (&key, &slot) in account.storage.iter() { // If storage was destroyed that means that storage was wiped. // In that case we need to check if present storage value is different then ZERO. - let destroyed_and_not_zero = was_destroyed && !slot.present_value.is_zero(); + let destroyed_and_not_zero = was_destroyed && !slot.present_value().is_zero(); // If account is not destroyed check if original values was changed, // so we can update it. @@ -610,7 +610,7 @@ impl BundleState { || destroyed_and_not_zero || not_destroyed_and_changed { - account_storage_changed.push((key, slot.present_value)); + account_storage_changed.push((key, slot.present_value())); } } @@ -674,7 +674,7 @@ impl BundleState { for (key, storage_slot) in other_account.storage { // Update present value or insert storage slot. this.storage.entry(key).or_insert(storage_slot).present_value = - storage_slot.present_value; + storage_slot.present_value(); } } this.info = other_account.info; @@ -728,7 +728,7 @@ impl BundleState { // A `Destroyed` slot in `other` means the value before the wipe, which is // this bundle's present value, not the database value. for (key, value) in this_account.storage.drain() { - let previous = RevertToSlot::Some(value.present_value); + let previous = RevertToSlot::Some(value.present_value()); revert .storage .entry(key) @@ -1223,7 +1223,7 @@ mod tests { #[test] fn test_multi_reverts_with_delete() { - let mut state = BundleBuilder::new(0..=3) + let mut state = BundleState::builder(0..=3) .revert_address(0, account1()) .revert_account_info(2, account1(), Some(Some(AccountInfo::default()))) .revert_account_info(3, account1(), Some(None)) @@ -1341,7 +1341,7 @@ mod tests { #[test] fn test_getters() { - let mut builder = BundleBuilder::new(0..=3); + let mut builder = BundleState::builder(0..=3); // Test get_states and get_states_mut assert!(builder.get_states().is_empty()); diff --git a/crates/evm2/src/block/bundle_state/reverts.rs b/crates/evm2/src/block/bundle_state/reverts.rs index 418a5c8d9..7eff71826 100644 --- a/crates/evm2/src/block/bundle_state/reverts.rs +++ b/crates/evm2/src/block/bundle_state/reverts.rs @@ -172,7 +172,7 @@ impl AccountRevert { // As those values got destroyed. let mut previous_storage: U256Map = previous_storage .drain() - .map(|(key, value)| (key, RevertToSlot::Some(value.present_value))) + .map(|(key, value)| (key, RevertToSlot::Some(value.present_value()))) .collect(); for (key, _) in updated_storage { previous_storage.entry(key).or_insert(RevertToSlot::Destroyed); @@ -220,7 +220,7 @@ impl AccountRevert { .iter_mut() .map(|(key, value)| { // Take previous value and set ZERO as storage got destroyed. - (*key, RevertToSlot::Some(value.present_value)) + (*key, RevertToSlot::Some(value.present_value())) }) .collect(); diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs index 52e830aa0..8782e6231 100644 --- a/crates/evm2/src/block/ethereum/execution.rs +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -472,7 +472,7 @@ fn execute_system_call( ) -> Result, BlockExecutionError> { let executed = evm.system_call(SystemTx::new(address, data)).map_err(map_handler_error)?; // History and beacon-root calls accept reverts and halts, unlike request-producing calls. - if !executed.result().status + if !executed.result().is_success() && !matches!(address, BEACON_ROOTS_ADDRESS | HISTORY_STORAGE_ADDRESS) { let reason = executed.result().stop; @@ -671,13 +671,13 @@ impl core::error::Error for SystemCallFailed {} mod tests { use super::*; use alloc::vec; - use alloy_primitives::LogData; fn deposit_log(data: Vec) -> Log { - Log { - address: MAINNET_DEPOSIT_CONTRACT_ADDRESS, - data: LogData::new_unchecked(vec![DEPOSIT_EVENT_SIGNATURE_HASH], data.into()), - } + Log::new_unchecked( + MAINNET_DEPOSIT_CONTRACT_ADDRESS, + vec![DEPOSIT_EVENT_SIGNATURE_HASH], + data.into(), + ) } /// Encodes a canonical `DepositEvent` payload whose fields are filled with `fill`. diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index 8c5aa7a4d..58901993e 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -100,7 +100,7 @@ impl ReceiptBuilder for EthReceiptBuilder { ) -> ReceiptEnvelope { let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; let receipt = Receipt { - status: Eip658Value::Eip658(result.status), + status: Eip658Value::Eip658(result.is_success()), cumulative_gas_used, logs: result.logs, }; diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs index 112105eb2..ca3a72941 100644 --- a/crates/evm2/src/block/ethereum/tests.rs +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -117,7 +117,7 @@ fn executes_prague_block() { let output = executor.finish().unwrap(); let [receipt] = output.result.receipts.as_slice() else { panic!("expected one receipt") }; - assert!(receipt.status()); + assert!(receipt.is_success()); assert_eq!(receipt.cumulative_gas_used(), 21_000); assert_eq!(output.result.gas_used, 21_000); assert!(output.result.requests.is_empty()); diff --git a/crates/evm2/src/block/mod.rs b/crates/evm2/src/block/mod.rs index edf6f27cb..ea72fcae4 100644 --- a/crates/evm2/src/block/mod.rs +++ b/crates/evm2/src/block/mod.rs @@ -149,7 +149,7 @@ where type Tx = &'a E::Transaction; fn into_parts(self) -> (Recovered>, &'a E::Transaction) { - let tx = Recovered::new_unchecked(self.inner().clone().into(), self.signer()); + let tx = Recovered::new_unchecked(self.clone_inner().into(), self.signer()); (tx, self.inner()) } } diff --git a/crates/evm2/src/block/state.rs b/crates/evm2/src/block/state.rs index 85f57d56f..6e2b45b07 100644 --- a/crates/evm2/src/block/state.rs +++ b/crates/evm2/src/block/state.rs @@ -305,7 +305,7 @@ impl crate::evm::StateChangeSource for BundleSource<'_> { for (hash, code) in &self.0.contracts { sink.bytecode(*hash, code)?; } - for (address, account) in &self.0.state { + for (address, account) in self.0.state() { let current = account.info.as_ref(); if account.was_destroyed() { sink.storage_wipe(*address)?; diff --git a/crates/evm2/src/ethereum/eip4844.rs b/crates/evm2/src/ethereum/eip4844.rs index 4fb43c480..c72ac33ab 100644 --- a/crates/evm2/src/ethereum/eip4844.rs +++ b/crates/evm2/src/ethereum/eip4844.rs @@ -13,7 +13,6 @@ use crate::{ evm::handler::{DefaultTxHandlerHooks, GasSettlement, TxHandlerHooks}, interpreter::GasTracker, registry::{HandlerError, HandlerResult, TxRequest}, - utils::b256_to_word, }; use alloy_consensus::transaction::TxEip4844Variant; use alloy_eips::eip4844::{DATA_GAS_PER_BLOB, VERSIONED_HASH_VERSION_KZG}; @@ -116,7 +115,7 @@ pub fn execute_prepared>( origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), - blob_hashes: tx.blob_versioned_hashes.iter().copied().map(b256_to_word).collect(), + blob_hashes: tx.blob_versioned_hashes.iter().copied().map(Into::into).collect(), ext: T::TxEnvExt::default(), _non_exhaustive: (), }; @@ -238,7 +237,7 @@ mod tests { let result = evm.transact(&tx).map(|executed| executed.commit()); assert_eq!(result.as_ref().map(|_| ()).map_err(Clone::clone), expected); if let Ok(result) = result { - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 21_000); // Disabling validation does not disable charging the actual blob base fee. let account = evm.state_mut().account(&caller).unwrap(); diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index c7573dbd0..cb96577eb 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -1054,7 +1054,7 @@ mod tests { ); } else { let result = result.unwrap(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.total_gas_spent, 31_020); assert_eq!(result.floor_gas, 31_050); assert_eq!(result.tx_gas_used(), 31_050); @@ -1294,7 +1294,7 @@ mod tests { caller, ); - assert!(evm.transact(&tx).unwrap().commit().status); + assert!(evm.transact(&tx).unwrap().commit().is_success()); let reward = evm.read_account_info(&beneficiary).unwrap().unwrap().balance; assert_eq!(reward, U256::from(21_000 * beneficiary_gas_price)); } @@ -1668,7 +1668,7 @@ mod tests { if value == 2 { assert_eq!(result.stop, InstrStop::OutOfFunds, "{case}"); } else { - assert_eq!(result.status, input[0] == op::STOP, "{case}"); + assert_eq!(result.is_success(), input[0] == op::STOP, "{case}"); } let nonce = evm.read_account_info(&caller).unwrap().unwrap().nonce; assert_eq!(nonce, 11, "{case}"); diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index c961229fc..4ed00bcf0 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -908,7 +908,7 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 0); } @@ -930,7 +930,7 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 0); } @@ -976,7 +976,7 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); } #[test] @@ -1102,7 +1102,7 @@ mod tests { let result = poll_ready(evm.transact_async(&tx)).unwrap().commit(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 5_291_175); assert_eq!( evm.state_mut().storage_slot_untracked(&target, &Word::ZERO).unwrap(), diff --git a/crates/evm2/src/evm/bal/account.rs b/crates/evm2/src/evm/bal/account.rs index 33d7c7cf0..ca26d5f50 100644 --- a/crates/evm2/src/evm/bal/account.rs +++ b/crates/evm2/src/evm/bal/account.rs @@ -108,10 +108,10 @@ impl From for AlloyAccountChanges { .collect::>(); let mut balance_changes = account.account_info.balance.changes; - balance_changes.sort_unstable_by_key(|change| change.block_access_index); + balance_changes.sort_unstable_by_key(|change| change.block_access_index()); let mut nonce_changes = account.account_info.nonce.changes; - nonce_changes.sort_unstable_by_key(|change| change.block_access_index); + nonce_changes.sort_unstable_by_key(|change| change.block_access_index()); let mut code_changes = account .account_info @@ -120,7 +120,7 @@ impl From for AlloyAccountChanges { .into_iter() .map(AlloyCodeChange::from) .collect::>(); - code_changes.sort_unstable_by_key(|change| change.block_access_index); + code_changes.sort_unstable_by_key(|change| change.block_access_index()); Self { address: Address::ZERO, diff --git a/crates/evm2/src/evm/bal/changes.rs b/crates/evm2/src/evm/bal/changes.rs index dfb86f84f..3257fcfb8 100644 --- a/crates/evm2/src/evm/bal/changes.rs +++ b/crates/evm2/src/evm/bal/changes.rs @@ -134,7 +134,7 @@ impl TryFrom<&AlloyCodeChange> for BalCodeChange { fn try_from(change: &AlloyCodeChange) -> Result { let bytecode = Bytecode::new_raw_checked(change.new_code().clone())?; let hash = bytecode.hash_slow(); - Ok(Self { block_access_index: change.block_access_index, code: (hash, bytecode) }) + Ok(Self { block_access_index: change.block_access_index(), code: (hash, bytecode) }) } } diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index cd9e1d717..0c90ea69a 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -76,7 +76,7 @@ impl Bal { changes.sort(); changes })); - alloy_bal.sort_unstable_by_key(|account| account.address); + alloy_bal.sort_unstable_by_key(|account| account.address()); alloy_bal } @@ -191,7 +191,7 @@ impl From for AlloyBal { .into_iter() .map(|(address, account)| AlloyAccountChanges { address, ..account.into() }), ); - alloy_bal.sort_unstable_by_key(|a| a.address); + alloy_bal.sort_unstable_by_key(|a| a.address()); alloy_bal } } @@ -213,7 +213,7 @@ impl TryFrom<&[AlloyAccountChanges]> for Bal { let mut accounts = AddressMap::with_capacity_and_hasher(alloy_bal.len(), Default::default()); for alloy_account in alloy_bal { - accounts.insert(alloy_account.address, AccountBal::try_from(alloy_account)?); + accounts.insert(alloy_account.address(), AccountBal::try_from(alloy_account)?); } Ok(Self { accounts }) @@ -236,7 +236,7 @@ impl TryFrom for Bal { let mut accounts = AddressMap::with_capacity_and_hasher(alloy_bal.len(), Default::default()); for alloy_account in alloy_bal { - let address = alloy_account.address; + let address = alloy_account.address(); accounts.insert(address, AccountBal::try_from(alloy_account)?); } @@ -269,7 +269,12 @@ impl core::fmt::Display for Bal { } else { writeln!(f, " Nonce writes:")?; for change in &account.account_info.nonce.changes { - writeln!(f, " [{}] -> {}", change.block_access_index, change.new_nonce)?; + writeln!( + f, + " [{}] -> {}", + change.block_access_index(), + change.new_nonce() + )?; } } @@ -282,7 +287,8 @@ impl core::fmt::Display for Bal { writeln!( f, " [{}] -> {}", - change.block_access_index, change.post_balance + change.block_access_index(), + change.post_balance() )?; } } @@ -414,7 +420,7 @@ mod tests { assert_eq!(alloy_bal, AlloyBal::from(bal)); assert_eq!( - alloy_bal.iter().map(|account| account.address).collect::>(), + alloy_bal.iter().map(|account| account.address()).collect::>(), vec![low_address, high_address] ); @@ -444,7 +450,7 @@ mod tests { account .balance_changes .iter() - .map(|change| change.block_access_index) + .map(|change| change.block_access_index()) .collect::>(), vec![idx(2), idx(5)] ); @@ -452,12 +458,16 @@ mod tests { account .nonce_changes .iter() - .map(|change| change.block_access_index) + .map(|change| change.block_access_index()) .collect::>(), vec![idx(4), idx(9)] ); assert_eq!( - account.code_changes.iter().map(|change| change.block_access_index).collect::>(), + account + .code_changes + .iter() + .map(|change| change.block_access_index()) + .collect::>(), vec![idx(3), idx(7)] ); assert_eq!(account.code_changes[0].new_code().as_ref(), &[3]); diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index af81a2a9d..670163a7d 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -561,7 +561,7 @@ mod tests { } impl crate::evm::Database for CountingDB { - type Error = core::convert::Infallible; + type Error = Infallible; fn get_account(&mut self, _address: &Address) -> Result, Self::Error> { self.account_loads += 1; @@ -599,13 +599,13 @@ mod tests { let loaded = cache.get_account(&address).unwrap().unwrap(); assert_eq!(loaded.code_hash, KECCAK256_EMPTY); - let mut cache = CacheDB::new(crate::evm::EmptyDB::default()); + let mut cache = InMemoryDB::default(); cache.insert_account_info(&address, zero_hash.clone()); assert_eq!(cache.account_info(&address).unwrap().code_hash, KECCAK256_EMPTY); let mut incoming = Cache::default(); incoming.accounts.insert(address, Some(zero_hash)); - let mut cache = CacheDB::new(crate::evm::EmptyDB::default()); + let mut cache = InMemoryDB::default(); cache.cache.merge(incoming); let loaded = cache.get_account(&address).unwrap().unwrap(); assert_eq!(loaded.code_hash, KECCAK256_EMPTY); diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 3e54b6ff1..9456efe12 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -1340,7 +1340,7 @@ mod tests { fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { let mut gas = interp.gas(); gas.set_remaining(100); - gas.set_refunded(7); + gas.set_refund(7); interp.set_gas(gas); } @@ -2121,7 +2121,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.status); + assert!(result.is_success()); assert_eq!(state.initialized, 1); assert_eq!(state.steps, 4); assert_eq!(state.step_ends, 4); @@ -2162,7 +2162,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!(result.result.logs.len(), 1); assert_eq!(state.logs, result.result.logs); assert_eq!(state.logs[0].address, SYSTEM_ADDRESS); @@ -2198,7 +2198,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.status); + assert!(result.is_success()); assert_eq!(state.initialized, 1); assert_eq!(state.steps, 1); assert_eq!(state.step_ends, 1); @@ -2290,7 +2290,7 @@ mod tests { ); let result = evm.transact(&tx).expect("transaction should execute").discard(); - assert!(result.status); + assert!(result.is_success()); let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let (before_hook, caller_balance, target_exists) = inspector.observed.unwrap(); // The value transfer, and for a create the account creation, follow the checkpoint. @@ -2381,7 +2381,7 @@ mod tests { ); let result = evm.transact(&tx).expect("transaction should execute").discard(); - assert!(result.status); + assert!(result.is_success()); let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); // The create frame's checkpoint follows the creator's nonce bump and precedes the // account creation. diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 85d141e6c..e10fe3fea 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -592,7 +592,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { "execution config spec mismatch" ); let precompiles = boxed_precompile_provider(precompiles); - self.block = block; + self.set_block(block); self.replace_execution_config(execution_config, spec_id, registry, precompiles); } @@ -621,7 +621,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { execution_config.base_spec_id(), "execution config spec mismatch" ); - self.block = block; + self.set_block(block); self.features = execution_config.version().features; self.execution_config = execution_config; self.refresh_runner_support(); @@ -697,8 +697,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[cfg(feature = "async")] #[inline] - fn async_stack(&mut self) -> core::ptr::NonNull { - core::ptr::NonNull::from(&mut self.async_stack) + fn async_stack(&mut self) -> NonNull { + NonNull::from(&mut self.async_stack) } #[cfg(feature = "async")] @@ -2245,10 +2245,7 @@ mod tests { ) -> HandlerResult { let value = Word::from(req.tx.nonce); req.host.state.storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)?.write(value); - req.host.state.log(Log { - address: LIFECYCLE_ACCOUNT, - data: LogData::new_unchecked(vec![], Bytes::new()), - }); + req.host.state.log(Log::new_unchecked(LIFECYCLE_ACCOUNT, vec![], Bytes::new())); Ok(TxResultExt { status: true, total_gas_spent: req.tx.nonce, ..TxResultExt::default() }) } @@ -2318,10 +2315,11 @@ mod tests { } let precompiles = precompiles_with([test_precompile(TEST_PRECOMPILE, |evm, _, _| { - evm.log(Log { - address: TEST_PRECOMPILE, - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(b"precompile")), - }); + evm.log(Log::new_unchecked( + TEST_PRECOMPILE, + Vec::new(), + Bytes::from_static(b"precompile"), + )); Ok(PrecompileOutput::new(Bytes::new())) })]); let mut evm = Evm::::new( @@ -2339,7 +2337,7 @@ mod tests { assert_eq!(result.stop, InstrStop::Return); let inspector = evm.clear_inspector_as::().unwrap(); assert_eq!(inspector.0.len(), 1); - assert_eq!(inspector.0.as_slice(), evm.logs()); + assert_eq!(inspector.0, evm.logs()); } #[test] @@ -2716,7 +2714,7 @@ mod tests { assert_eq!(error.to_string(), "test precompile error"); evm.set_precompiles(Precompiles::base(SpecId::OSAKA)); let next = evm.transact(&tx).unwrap().discard(); - assert!(next.status); + assert!(next.is_success()); assert!(error.downcast_ref::().is_some()); } @@ -3104,7 +3102,7 @@ mod tests { caller, ); let executed = evm.transact_with_inspector(&tx, &mut OriginInspector(origin)).unwrap(); - assert!(executed.commit().status); + assert!(executed.commit().is_success()); let origin_word = address_to_word(&origin); assert_eq!(evm.state.storage_slot_untracked(&target, &Word::ZERO).unwrap(), origin_word); @@ -3513,7 +3511,7 @@ mod tests { TxEnvelope::as_legacy, handler(|_| Ok(()), |req, ()| handle_lifecycle_tx(req)), ); - assert!(evm.transact(&test_tx(9)).unwrap().commit().status); + assert!(evm.transact(&test_tx(9)).unwrap().commit().is_success()); } #[test] @@ -3707,10 +3705,10 @@ mod tests { // Taking the BAL yields a canonical EIP-7928 list and resets the index. let alloy = BlockAccessList::from(evm.state.take_bal_builder().expect("bal is present")); assert_eq!(alloy.len(), 1); - assert_eq!(alloy[0].address, LIFECYCLE_ACCOUNT); + assert_eq!(alloy[0].address(), LIFECYCLE_ACCOUNT); assert_eq!(alloy[0].storage_changes.len(), 1); assert_eq!(alloy[0].storage_changes[0].slot, LIFECYCLE_STORAGE_KEY); - assert_eq!(alloy[0].storage_changes[0].changes.len(), 2); + assert_eq!(alloy[0].storage_changes[0].len(), 2); assert_eq!(evm.state.bal_index(), BlockAccessIndex::PRE_EXECUTION); assert!(evm.state.bal_builder().is_none()); } @@ -4528,7 +4526,7 @@ mod tests { { assert_eq!(steps.load(Ordering::Relaxed), 0); } - assert!(evm.call_tx(&tx).unwrap().status); + assert!(evm.call_tx(&tx).unwrap().is_success()); } #[test] @@ -4551,10 +4549,7 @@ mod tests { .storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)? .write(Word::from(99)); req.host.state.account(&LIFECYCLE_ACCOUNT)?.bump_nonce(); - req.host.state.log(Log { - address: LIFECYCLE_ACCOUNT, - data: LogData::new_unchecked(vec![], Bytes::new()), - }); + req.host.state.log(Log::new_unchecked(LIFECYCLE_ACCOUNT, vec![], Bytes::new())); if let Some(error) = errors.get(req.tx.nonce as usize) { return Err(error.clone()); } @@ -4732,7 +4727,7 @@ mod tests { caller, ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let pending = executed.detach().pending_state; assert_eq!(pending.account_info(&caller).unwrap().nonce, nonce + 1); assert_eq!(pending.account_info(&created).unwrap().nonce, 2); @@ -4788,7 +4783,7 @@ mod tests { caller, ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); for (address, touched) in [(target, true), (read, false), (child, false), (leaf, false)] { let changes = executed.account_changes().find(|c| c.address == address).unwrap(); assert_eq!(changes.touched, touched, "{address}"); @@ -4813,7 +4808,7 @@ mod tests { caller, ); let next = evm.transact(&tx).unwrap(); - assert!(next.result().status); + assert!(next.result().is_success()); assert!(!next.account_changes().find(|c| c.address == target).unwrap().touched); } @@ -4845,7 +4840,7 @@ mod tests { ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let changes = executed.changes(); assert!(!changes.is_empty()); assert_eq!(changes.account_info(&caller).unwrap().nonce, 1); @@ -4858,7 +4853,7 @@ mod tests { // Discarding after inspection leaves the accepted state unchanged, so the same transaction // executes again, and its detached state reports the same accounts. let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let pending = executed.detach().pending_state; let mut detached = pending.account_changes().map(|c| c.address).collect::>(); detached.sort(); diff --git a/crates/evm2/src/evm/prewarm_set.rs b/crates/evm2/src/evm/prewarm_set.rs index 7c87f639e..3ec99d72f 100644 --- a/crates/evm2/src/evm/prewarm_set.rs +++ b/crates/evm2/src/evm/prewarm_set.rs @@ -20,7 +20,7 @@ use crate::{interpreter::Word, utils::BitArray}; use alloy_primitives::{ Address, - map::{AddressMap, HashSet}, + map::{AddressMap, U256Set}, }; /// Short-address optimization cap. Addresses with 18 leading zero bytes whose last two bytes are @@ -68,7 +68,7 @@ pub struct PrewarmSet { /// [`Self::warm`] and [`Self::warm_storage`]: they are warmed /// before or alongside execution and survive [`crate::evm::State::rollback`]. An address /// present with an empty slot set is warm with no warm storage slots. - access_list: AddressMap>, + access_list: AddressMap, /// Bit vector of warm short addresses. An address shorter than [`SHORT_ADDRESS_CAP`] sets /// its bit here for faster access; precompiles and other low-numbered addresses land here. short_addresses: ShortAddresses, diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 82d1cc0b6..df827c60d 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -692,7 +692,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { #[inline] pub(crate) fn reset_selfdestructed_for_finalization(&mut self) { let balance = self.balance(); - self.tracked.present = Some(AccountInfo::default().with_balance(balance)); + self.tracked.present = Some(empty_account().with_balance(balance)); } /// Overrides the live balance and the balance restored by this handle's revert snapshot, @@ -730,7 +730,7 @@ mod tests { fn transfer_to_self_requires_balance() { let address = Address::repeat_byte(0x77); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(3))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(3))); let mut state = State::new(database); assert!(!state.transfer(&address, &address, &Word::from(4)).unwrap()); @@ -793,8 +793,8 @@ mod tests { use alloy_primitives::Bytes; let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); - let with_code = AccountInfo::default().with_code(code.clone()); - let other = AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from_static(&[0]))); + let with_code = empty_account().with_code(code.clone()); + let other = empty_account().with_code(Bytecode::new_raw(Bytes::from_static(&[0]))); let created = Account::new(None, Some(with_code.clone())); assert_eq!(created.changed_code(), Some((with_code.code_hash, &code))); @@ -806,11 +806,8 @@ mod tests { Account::new(Some(with_code.clone()), Some(with_code.clone())).changed_code(), None ); - assert_eq!( - Account::new(Some(with_code), Some(AccountInfo::default())).changed_code(), - None - ); - assert_eq!(Account::new(None, Some(AccountInfo::default())).changed_code(), None); + assert_eq!(Account::new(Some(with_code), Some(empty_account())).changed_code(), None); + assert_eq!(Account::new(None, Some(empty_account())).changed_code(), None); } #[test] @@ -848,10 +845,7 @@ mod tests { let mut database = CacheDB::default(); database.insert_account_info( &address, - AccountInfo::default() - .with_balance(Word::from(10)) - .with_nonce(2) - .with_code(original_code), + empty_account().with_balance(Word::from(10)).with_nonce(2).with_code(original_code), ); let mut state = State::new(database); @@ -883,7 +877,7 @@ mod tests { fn journaled_account_read_only_handle_journals_nothing() { let address = Address::repeat_byte(0x89); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(5))); let mut state = State::new(database); let checkpoint = state.checkpoint(); @@ -903,7 +897,7 @@ mod tests { let address = Address::repeat_byte(0x8a); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(5))); let mut state = State::new(database); // A cold, not-yet-loaded account signals the skip instead of reading the database. @@ -921,7 +915,7 @@ mod tests { #[test] fn account_overrides_update_only_selected_snapshot_fields() { let address = Address::with_last_byte(0x90); - let original = AccountInfo::default().with_balance(Word::from(100)).with_nonce(7); + let original = empty_account().with_balance(Word::from(100)).with_nonce(7); for (override_balance, override_nonce) in [(true, false), (false, true), (true, true)] { let mut db = CacheDB::default(); db.insert_account_info(&address, original.clone()); @@ -1004,7 +998,7 @@ mod tests { #[test] fn account_overrides_preserve_earlier_handles_snapshots() { let address = Address::with_last_byte(0x92); - let original = AccountInfo::default().with_balance(Word::from(100)).with_nonce(7); + let original = empty_account().with_balance(Word::from(100)).with_nonce(7); let mut db = CacheDB::default(); db.insert_account_info(&address, original.clone()); let mut state = State::new(db); diff --git a/crates/evm2/src/evm/state/journal.rs b/crates/evm2/src/evm/state/journal.rs index f7019f899..72bdd30b6 100644 --- a/crates/evm2/src/evm/state/journal.rs +++ b/crates/evm2/src/evm/state/journal.rs @@ -104,17 +104,13 @@ mod tests { #[test] fn log_rolls_back_to_checkpoint() { - use alloy_primitives::{Bytes, LogData}; + use alloy_primitives::Bytes; let mut state = State::new(CacheDB::default()); - let kept = Log { - address: Address::repeat_byte(0x44), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x01])), - }; - let reverted = Log { - address: Address::repeat_byte(0x55), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), - }; + let kept = + Log::new_unchecked(Address::repeat_byte(0x44), Vec::new(), Bytes::from_static(&[0x01])); + let reverted = + Log::new_unchecked(Address::repeat_byte(0x55), Vec::new(), Bytes::from_static(&[0x02])); state.log(kept.clone()); let checkpoint = state.checkpoint(); @@ -124,10 +120,11 @@ mod tests { state.logs(), &[ kept.clone(), - Log { - address: Address::repeat_byte(0x55), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), - } + Log::new_unchecked( + Address::repeat_byte(0x55), + Vec::new(), + Bytes::from_static(&[0x02]) + ) ] ); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index 1e185a0d0..738e7de3e 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -862,8 +862,8 @@ impl<'a> State<'a> { pub fn rollback(&mut self, checkpoint: StateCheckpoint, features: EvmFeatures) { // Restoring an older snapshot can leave active cursors past the current lengths. Preserve // shorter prefixes and revert only entries beyond each cursor. - self.logs.truncate(checkpoint.logs_len); - while self.journal.len() > checkpoint.journal_len { + self.logs.truncate(checkpoint.logs_len()); + while self.journal.len() > checkpoint.journal_len() { let Some(entry) = self.journal.pop() else { unreachable!("journal length is checked above") }; @@ -885,7 +885,7 @@ impl<'a> State<'a> { .get_mut(&address) .and_then(|account| account.storage.slots.get_mut(&key)) { - slot.value.current = previous; + slot.value.set_current(previous); } } JournalEntry::TransientStorageChange { address, key, previous } => match previous { diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index 4f8dd22a5..f566ba122 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -348,7 +348,7 @@ impl StorageSlotHandle<'_, '_> { if previous == value { return; } - self.slot.value.current = value; + self.slot.value.set_current(value); self.inner.push_journal(|| JournalEntry::StorageChange { address: *self.address, key: self.key, diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index 93d25075f..5167e5b33 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -289,7 +289,7 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); assert!(result.result.tx_gas_used() < SYSTEM_CALL_GAS_LIMIT); let unchanged = |address| { result.pending_state.accounts.get(address).is_none_or(|entry| !entry.is_changed()) @@ -333,7 +333,7 @@ mod tests { .unwrap() .detach(); - assert!(result.result.status); + assert!(result.result.is_success()); let storage = &result.pending_state.accounts.get(&contract).expect("storage changed").storage.slots; let caller = address_to_word(&caller); @@ -358,7 +358,7 @@ mod tests { let outcome = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().discard(); - assert!(outcome.status); + assert!(outcome.is_success()); assert!( outcome.tx_gas_used() < 1_000, "system contract should be warm before execution, got {} gas used", @@ -379,7 +379,7 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!(result.result.tx_gas_used(), 0); assert!(!result.pending_state.is_changed()); } @@ -411,7 +411,7 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(!result.result.status); + assert!(!result.result.is_success()); assert_eq!(result.result.stop, InstrStop::Revert); assert!(!result.pending_state.is_changed()); } @@ -497,10 +497,10 @@ mod tests { let result = evm.system_call(tx).unwrap().discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(U256::from_be_slice(&result.output), U256::from(expected_limit - 2)); assert_eq!(result.total_gas_spent, 15); - assert_eq!(result.state_gas_spent, 0); + assert_eq!(result.state_gas_spent(), 0); } #[test] @@ -528,7 +528,7 @@ mod tests { .unwrap() .detach(); - assert!(!result.result.status); + assert!(!result.result.is_success()); assert_eq!(result.result.stop, InstrStop::OutOfGas); assert_eq!(result.result.total_gas_spent, 3_000); assert!(!result.pending_state.is_changed()); @@ -560,8 +560,8 @@ mod tests { // (PUSH1, PUSH0, cold SSTORE) from `gas_limit`. let state_gas = evm.version().gas_params.get(GasId::SstoreSetState); let regular_gas = 3 + 2 + 2_100 + 10_000; - assert!(result.status); - assert_eq!(result.state_gas_spent, state_gas); + assert!(result.is_success()); + assert_eq!(result.state_gas_spent(), state_gas); assert_eq!(result.total_gas_spent, regular_gas + state_gas); assert_eq!(result.execution_gas_spent(), regular_gas); } @@ -589,8 +589,8 @@ mod tests { let tx = SystemTx::new(contract, Bytes::new()).with_gas_limit(u64::MAX); let result = evm.system_call(tx).unwrap().discard(); - assert!(result.status); - assert!(result.total_gas_spent >= result.state_gas_spent); + assert!(result.is_success()); + assert!(result.total_gas_spent >= result.state_gas_spent()); assert_eq!(result.execution_gas_spent(), 3 + 2 + 2_100 + 10_000); } @@ -648,7 +648,7 @@ mod tests { evm.set_inspect_system_calls(inspect); let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); let inspector = evm.clear_inspector_as::().expect("inspector kept"); assert_eq!(inspector.calls, expected_calls); diff --git a/crates/evm2/src/interpreter/instructions/block.rs b/crates/evm2/src/interpreter/instructions/block.rs index fa8efd485..d760e239b 100644 --- a/crates/evm2/src/interpreter/instructions/block.rs +++ b/crates/evm2/src/interpreter/instructions/block.rs @@ -2,7 +2,7 @@ use crate::{ EvmFeatures, constants::BLOCK_HASH_HISTORY, interpreter::{Host, Word}, - utils::{WordExt, b256_to_word}, + utils::WordExt, }; use evm2_macros::instruction; @@ -12,7 +12,7 @@ pub fn blockhash(cx: _, [number]: [Word]) -> Result { if diff == 0 || diff > BLOCK_HASH_HISTORY { Word::ZERO } else { - b256_to_word(cx.state.host().block_hash(number).map_err(|error| cx.state.fail(error))?) + cx.state.host().block_hash(number).map_err(|error| cx.state.fail(error))?.into() } } else { Word::ZERO @@ -164,7 +164,7 @@ mod tests { let randao = B256::with_last_byte(0x55); let mut host = test_host(BlockEnvExt { difficulty: Word::from(14), - prevrandao: b256_to_word(randao), + prevrandao: randao.into(), ..BlockEnvExt::default() }); let interp = @@ -223,8 +223,7 @@ mod tests { fn blobhash_opcode() { let hash = B256::with_last_byte(0x42); let mut host = TestHost::default(); - let tx_env = - TxEnvExt { blob_hashes: Vec::from([b256_to_word(hash)]), ..TxEnvExt::default() }; + let tx_env = TxEnvExt { blob_hashes: Vec::from([hash.into()]), ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::PUSH0, op::BLOBHASH, op::STOP]) .host(&mut host) diff --git a/crates/evm2/src/interpreter/instructions/crypto.rs b/crates/evm2/src/interpreter/instructions/crypto.rs index aa20513b5..200cbebbb 100644 --- a/crates/evm2/src/interpreter/instructions/crypto.rs +++ b/crates/evm2/src/interpreter/instructions/crypto.rs @@ -1,4 +1,4 @@ -use crate::utils::{WordExt, b256_to_word}; +use crate::utils::WordExt; use alloy_primitives::{KECCAK256_EMPTY, keccak256 as keccak256_hash}; use evm2_macros::instruction; @@ -13,7 +13,7 @@ pub fn keccak256(cx: _, [offset, len]: [Word]) -> Result { cx.state.resize_memory(cx.gas, offset, len)?; keccak256_hash(cx.state.memory().slice(offset, len)) }; - *out = b256_to_word(hash); + *out = hash.into(); } #[cfg(test)] diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index 333eb7939..71a3f7339 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -2,7 +2,7 @@ use crate::{ EvmTypesHost, evm::AccountLoad, interpreter::{Gas, Host, InstrStop, Memory, Result, Word, private::GasInstructionCx}, - utils::{WordExt, b256_to_word, read_word_padded}, + utils::{WordExt, read_word_padded}, version::GasParams, }; use evm2_macros::instruction; @@ -72,7 +72,7 @@ pub fn callvalue(cx: _) -> out { #[instruction] pub fn calldataload(cx: _, [offset]: [Word]) -> out { let offset = offset.to_usize_saturated(); - *out = b256_to_word(read_word_padded(&cx.state.message().input, offset)); + *out = read_word_padded(&cx.state.message().input, offset).into(); } #[instruction] @@ -118,7 +118,7 @@ pub fn extcodesize(cx: _, [addr]: [Word]) -> Result { #[instruction(dynamic_gas)] pub fn extcodehash(cx: _, [addr]: [Word]) -> Result { let account = load_account(&mut cx, *addr, false)?; - *out = if account.is_empty { Word::ZERO } else { b256_to_word(account.code_hash) }; + *out = if account.is_empty { Word::ZERO } else { account.code_hash.into() }; } #[instruction(dynamic_gas)] diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index 5e579df78..45748cf32 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -133,10 +133,7 @@ mod tests { fn assert_push_opcode(opcode: u8, n: usize) { for bytes in [vec![0; n], vec![n as u8; n], (1..=n as u8).collect::>()] { - let mut code = Vec::new(); - code.push(opcode); - code.extend_from_slice(&bytes); - code.push(op::STOP); + let code = [&[opcode][..], &bytes[..], &[op::STOP][..]].concat(); let interp = run(RunConfig::new(code)); assert_matches!(interp.err, InstrStop::Stop); diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 183e3c090..429db793d 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -553,7 +553,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::from(1000), ], ); @@ -600,7 +600,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::from(1000), ], ); @@ -626,7 +626,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ONE, - address_to_word(&target), + target.into_word().into(), Word::from(1000), ], ); @@ -689,7 +689,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::ZERO, ], ); @@ -720,7 +720,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::ZERO, ], ); @@ -750,7 +750,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::from(7), - address_to_word(&code_address), + code_address.into_word().into(), Word::from(1000), ], ); @@ -778,7 +778,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::from(7), - address_to_word(&code_address), + code_address.into_word().into(), Word::from(1000), ], ); @@ -809,7 +809,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&code_address), + code_address.into_word().into(), Word::from(1000), ], ); @@ -859,7 +859,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::from(1000), ], ); @@ -944,7 +944,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [address_to_word(&created), Word::ZERO]); + assert_eq!(interp.stack(), [created.into_word().into(), Word::ZERO]); } #[test] @@ -1157,7 +1157,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::from(value), - address_to_word(&Address::repeat_byte(0x22)), + Address::repeat_byte(0x22).into_word().into(), Word::from(5_000), ], ); diff --git a/crates/evm2/src/precompiles/bn254/mod.rs b/crates/evm2/src/precompiles/bn254/mod.rs index 9d29fd4eb..330166489 100644 --- a/crates/evm2/src/precompiles/bn254/mod.rs +++ b/crates/evm2/src/precompiles/bn254/mod.rs @@ -269,92 +269,85 @@ mod tests { #[test] fn test_bn254_add() { - let input = hex::decode( + let input = hex!( "\ 18b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9\ 063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f37266\ 07c2b7f58a84bd6145f00c9c2bc0bb1a187f20ff2c92963a88019e7c6a014eed\ - 06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7", - ) - .unwrap(); - let expected = hex::decode( + 06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7" + ); + let expected = hex!( "\ 2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703\ - 301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915", - ) - .unwrap(); + 301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Zero sum test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); - let expected = hex::decode( + 0000000000000000000000000000000000000000000000000000000000000000" + ); + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(499)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(499)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::OutOfGas))); // No input test let input = [0u8; 0]; - let expected = hex::decode( + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 1111111111111111111111111111111111111111111111111111111111111111", - ) - .unwrap(); + 1111111111111111111111111111111111111111111111111111111111111111" + ); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!( res, Err(PrecompileError::Halt(PrecompileHalt::Bn254AffineGFailedToCreate)), ); // Short input is right-padded. This makes the first field element non-canonical. - let res = run_add(&[0x40], BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&[0x40], BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); // A coordinate equal to the field modulus is non-canonical. let input = hex!("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); // The largest canonical coordinate, p - 1, is accepted: (p - 1, sqrt(2)) is on the curve. @@ -365,133 +358,122 @@ mod tests { input[32..64].copy_from_slice(&hex!( "279d7bc4e184e3a57f5fa684690c6df6b484a7f1daa1de608d266a2a4be6593f" )); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), &input[..64]); - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45\ 0000000000000000000000000000000000000000000000000000000000000001\ - 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45", - ) - .unwrap(); - let expected = hex::decode( + 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45" + ); + let expected = hex!( "\ 030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd3\ - 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83", - ) - .unwrap(); + 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); } #[test] fn test_bn254_mul() { - let input = hex::decode( + let input = hex!( "\ 2bd3e6d0f3b142924f5ca7b49ce5b9d54c4703d7ae5648e61d02268b1a0a9fb7\ 21611ce0a6af85915e2f1d70300909ce2e49dfad4a4619c8390cae66cefdb204\ - 00000000000000000000000000000000000000000000000011138ce750fa15c2", - ) - .unwrap(); - let expected = hex::decode( + 00000000000000000000000000000000000000000000000011138ce750fa15c2" + ); + let expected = hex!( "\ 070a8d6a982153cae4be29d434e8faef8a47b274a053f5a4ee2a6c9c13c31e5c\ - 031b8ce914eba3a9ffb989f9cdd5b0f01943074bf4f0f315690ec3cec6981afc", - ) - .unwrap(); + 031b8ce914eba3a9ffb989f9cdd5b0f01943074bf4f0f315690ec3cec6981afc" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0200000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0200000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(39_999)); + let res = mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(39_999)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::OutOfGas))); // Zero multiplication test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0200000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); - let expected = hex::decode( + 0200000000000000000000000000000000000000000000000000000000000000" + ); + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // No input test let input = [0u8; 0]; - let expected = hex::decode( + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 0f00000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0f00000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); + let res = mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); assert_matches!( res, Err(PrecompileError::Halt(PrecompileHalt::Bn254AffineGFailedToCreate)), ); // Short input is right-padded. This makes the point x-coordinate non-canonical. - let res = run_mul(&[0x40], BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); + let res = mul::run(&[0x40], BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45\ - 0000000000000000000000000000000000000000000000000000000000000002", - ) - .unwrap(); - let expected = hex::decode( + 0000000000000000000000000000000000000000000000000000000000000002" + ); + let expected = hex!( "\ 030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd3\ - 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83", - ) - .unwrap(); + 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); } #[test] fn test_bn254_pair() { - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ @@ -504,14 +486,11 @@ mod tests { 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\ 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\ 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\ - 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", - ) - .unwrap(); - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa" + ); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -521,7 +500,7 @@ mod tests { assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ @@ -534,11 +513,10 @@ mod tests { 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\ 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\ 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\ - 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", - ) - .unwrap(); + 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -548,11 +526,9 @@ mod tests { // No input test let input = [0u8; 0]; - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -562,18 +538,17 @@ mod tests { assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 1111111111111111111111111111111111111111111111111111111111111111", - ) - .unwrap(); + 1111111111111111111111111111111111111111111111111111111111111111" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -585,18 +560,17 @@ mod tests { ); // G2 point on the curve but not in the subgroup fail - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000001\ 0d1271953ed9ea0836846e70a1934187998c7f790cb4d7511b7f8da82de048a4\ - 2869111d5381f072f8e2728fdb825a51aadd70e52c9830e9ab4b871c0531f1bb", - ) - .unwrap(); + 2869111d5381f072f8e2728fdb825a51aadd70e52c9830e9ab4b871c0531f1bb" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -623,7 +597,7 @@ mod tests { let pair_infinity_with = |g2: &str| { let mut input = [0u8; PAIR_ELEMENT_LEN]; hex::decode_to_slice(g2, &mut input[G1_LEN..]).unwrap(); - run_pair( + pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -641,7 +615,7 @@ mod tests { let mut input = [0u8; PAIR_ELEMENT_LEN]; input[0] = 0x40; - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -650,16 +624,15 @@ mod tests { assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember))); // Invalid input length - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 111111111111111111111111111111\ - ", - ) - .unwrap(); + " + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -669,21 +642,18 @@ mod tests { // Test with point at infinity - should return true (identity element) // G1 point at infinity (0,0) followed by a valid G2 point - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7\ 04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678\ 2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d\ - 120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550", - ) - .unwrap(); - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + 120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550" + ); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -694,18 +664,17 @@ mod tests { // Test with G2 point at infinity - should also return true // Valid G1 point followed by G2 point at infinity (0,0,0,0) - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, diff --git a/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs b/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs index 19397da7f..e085f4d93 100644 --- a/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs +++ b/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs @@ -72,7 +72,7 @@ fn read_scalar_canonical(bytes: &[u8; 32]) -> Result { // Check if the field element is canonical by serializing back and comparing let bytes_roundtrip = fr.into_bigint().to_bytes_be(); - if bytes_roundtrip.as_slice() != bytes { + if bytes_roundtrip != bytes { return Err(PrecompileHalt::NonCanonicalFp); } diff --git a/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs b/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs index e709dd5a3..5fc62a04f 100644 --- a/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs +++ b/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs @@ -109,6 +109,7 @@ pub(crate) fn verify_kzg_proof( #[cfg(test)] mod tests { use super::*; + use alloy_primitives::B256; #[test] fn basic_test() { @@ -239,11 +240,7 @@ mod tests { "a62ad71d14c5719385c0686f1871430475bf3a00f0aa3f7b8dd99a9abc2160744faf0070725e00b60ad9a026a15b1a8c" ); let wrong_y = hex!("1522a4a7f34e1ea350ae07c29c96c7e79655aa926122e95fe69fcbd932ca49ea"); - let scalar = |n: u8| { - let mut bytes = [0u8; 32]; - bytes[31] = n; - bytes - }; + let scalar = |n: u8| B256::with_last_byte(n).0; let cases = [ ("c-kzg valid", valid_commitment, r_minus_one, valid_y, valid_proof, true), diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index 797d5c0b5..0a931d18f 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -719,54 +719,50 @@ mod tests { #[test] fn test_modexp_edge_cases() { // Test case 1: Zero base with non-zero exponent and modulus - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 05\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "0^5 mod 7 should be 0"); // Test case 2: Base equals modulus - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 07\ 03\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "7^3 mod 7 should be 0"); // Test case 3: Exponent is zero (result should always be 1) - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000001\ 05\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x01], "5^0 mod 7 should be 1"); // Test case 4: Large base with small modulus // Actually, (2^256 - 1) mod 3 = 0, so 0^2 = 0 - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ 02\ - 03", - ) - .unwrap(); + 03" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); // (2^256 - 1) = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff // This is divisible by 3, so (2^256 - 1) mod 3 = 0 @@ -789,15 +785,14 @@ mod tests { assert_eq!(gas.spent(), 200, "Empty input should use minimum gas 200 for Berlin"); // Test gas consumption with very small inputs - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 01\ 01\ - 01", - ) - .unwrap(); + 01" + ); // For Byzantium, check that it computes correctly let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "1^1 mod 1 = 0"); @@ -811,15 +806,14 @@ mod tests { #[test] fn test_modexp_padding() { // Test that results are properly padded to modulus length - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000020\ 02\ 03\ - 0000000000000000000000000000000000000000000000000000000000000101", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000101" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes().len(), 32, "Result should be padded to modulus length"); assert_eq!(res.bytes()[31], 8, "2^3 mod 257 = 8"); @@ -828,15 +822,14 @@ mod tests { #[test] fn test_modexp_berlin_vs_byzantium_gas() { // Test that Berlin gas costs are lower than Byzantium for the same input - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000020\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ - fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", - ) - .unwrap(); + fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd" + ); let gas_limit = 10_000_000u64; let mut byzantium_gas = GasTracker::new(gas_limit); @@ -856,30 +849,28 @@ mod tests { #[test] fn test_modexp_osaka_size_limits() { // Test Osaka's 1024-byte limit per parameter - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000400\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 01\ - 01", - ) - .unwrap(); + 01" + ); // Should succeed with exactly 1024 bytes let res = run_osaka(&input, &mut GasTracker::new(100_000_000)); assert!(res.is_ok(), "1024-byte base should be allowed"); // Test with 1025 bytes - should fail - let input_fail = hex::decode( + let input_fail = hex!( "0000000000000000000000000000000000000000000000000000000000000401\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 01\ - 01", - ) - .unwrap(); + 01" + ); let res = run_osaka(&input_fail, &mut GasTracker::new(100_000_000)); assert_matches!( @@ -892,13 +883,13 @@ mod tests { #[test] fn test_modexp_input_truncation() { // Test behavior when input is shorter than expected - let input = hex::decode( + // Only 1 byte provided for 2-byte base + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000002\ - ff", // Only 1 byte provided for 2-byte base - ) - .unwrap(); + ff" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); // Should pad with zeros and compute ff00^ff00 mod ff00 @@ -908,15 +899,14 @@ mod tests { #[test] fn test_modexp_maximum_values() { // Test with maximum U256 values - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000020\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ 01\ - fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", - ) - .unwrap(); + fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" + ); let res = run_byzantium(&input, &mut GasTracker::new(10_000_000)).unwrap(); assert_eq!(res.bytes().len(), 32, "Result should be 32 bytes"); @@ -967,12 +957,11 @@ mod tests { #[test] fn test_modexp_out_of_gas() { // Test that large inputs properly return out of gas error - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000100\ 0000000000000000000000000000000000000000000000000000000000000100\ - 0000000000000000000000000000000000000000000000000000000000000100", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000100" + ); // Provide insufficient gas let res = run_byzantium(&input, &mut GasTracker::new(1000)); diff --git a/crates/evm2/src/precompiles/table.rs b/crates/evm2/src/precompiles/table.rs index 22215c566..a843b1e63 100644 --- a/crates/evm2/src/precompiles/table.rs +++ b/crates/evm2/src/precompiles/table.rs @@ -163,7 +163,7 @@ impl PrecompileMap { pub fn with_capacity(capacity: usize) -> Self { Self { inner: AddressMap::with_capacity_and_hasher(capacity, Default::default()), - ..Self::default() + ..Self::new() } } @@ -522,7 +522,7 @@ define_precompiles! { #[cfg(test)] mod tests { use super::*; - use crate::evm::precompile::PrecompileOutput; + use crate::{evm::precompile::PrecompileOutput, precompiles::Precompiles}; use alloy_primitives::{Bytes, address}; use core::assert_matches; @@ -530,7 +530,7 @@ mod tests { fn map_precompile_updates_data_at_target_address() { let identity = IDENTITY::(); let address = identity.address(); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); map.map_precompile(&address, |precompile| { precompile @@ -568,7 +568,7 @@ mod tests { fn map_precompiles_preserves_existing_addresses() { let identity = IDENTITY::(); let sha256 = SHA256::(); - let mut map = PrecompileMap::from_precompiles([identity.clone(), sha256.clone()]); + let mut map = Precompiles::map([identity.clone(), sha256.clone()]); map.map_precompiles(|_, precompile| { assert_matches!(precompile.id(), PrecompileId::Identity | PrecompileId::Sha256); @@ -588,7 +588,7 @@ mod tests { let source = identity.address(); let missing = address!("0x0000000000000000000000000000000000000999"); let dest = address!("0x0000000000000000000000000000000000001000"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); let err = map.move_precompiles([(source, dest), (missing, sha256.address())]); @@ -605,7 +605,7 @@ mod tests { let sha256_address = sha256.address(); let new_identity = address!("0x0000000000000000000000000000000000001001"); let new_sha256 = address!("0x0000000000000000000000000000000000001002"); - let mut map = PrecompileMap::from_precompiles([identity, sha256]); + let mut map = Precompiles::map([identity, sha256]); map.move_precompiles([(identity_address, new_identity), (sha256_address, new_sha256)]) .unwrap(); @@ -622,7 +622,7 @@ mod tests { let identity_address = identity.address(); let first = address!("0x0000000000000000000000000000000000001001"); let second = address!("0x0000000000000000000000000000000000001002"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); map.move_precompiles([(identity_address, first), (identity_address, second)]).unwrap(); @@ -637,7 +637,7 @@ mod tests { let short_miss = address!("0x0000000000000000000000000000000000000999"); let long = address!("0x1000000000000000000000000000000000000004"); let long_miss = address!("0x1000000000000000000000000000000000000005"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); assert_eq!(map.get_data(&identity_address).unwrap().id(), &PrecompileId::Identity); assert!(map.get_data(&short_miss).is_none()); @@ -661,7 +661,7 @@ mod tests { let identity_address = identity.address(); let sha256_address = sha256.address(); let long = address!("0x1000000000000000000000000000000000000004"); - let mut map = PrecompileMap::from_precompiles([identity, sha256]); + let mut map = Precompiles::map([identity, sha256]); map.move_precompiles([(sha256_address, long)]).unwrap(); let mut warm = PrewarmSet::new(); diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 39e57da80..40cac2fc0 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -237,9 +237,9 @@ fn eip8037_sibling_refill_restores_parent_gas_left() { let baseline = run(false); let result = run(true); - assert!(baseline.status); - assert!(result.status); - assert_eq!(result.state_gas_spent, 0); + assert!(baseline.is_success()); + assert!(result.is_success()); + assert_eq!(result.state_gas_spent(), 0); assert_eq!(result.output.len(), 32); assert_eq!(result.output, baseline.output); } @@ -288,8 +288,8 @@ fn eip2780_authorization_charges_no_state_gas_without_eip8037() { ); let result = evm.transact(&tx).unwrap().detach(); - assert!(result.result.status); - assert_eq!(result.result.state_gas_spent, 0); + assert!(result.result.is_success()); + assert_eq!(result.result.state_gas_spent(), 0); let authority = result.pending_state.account_info(&authority).unwrap(); assert_eq!(authority.nonce, 1); assert_eq!(authority.code_hash, Bytecode::new_eip7702(delegate).hash_slow()); @@ -530,7 +530,7 @@ fn log_collection_off_keeps_log_semantics() { let mut on = run(true); let off = run(false); - assert_eq!(on.status, status, "{name}"); + assert_eq!(on.is_success(), status, "{name}"); if let Some(output) = output { assert_eq!(on.output[..], output.to_be_bytes::<32>(), "{name}"); } @@ -560,7 +560,7 @@ fn log_collection_off_still_inspects_logs() { let inspected = evm.transact(&log_collection_tx()).unwrap().discard(); let inspector = evm.clear_inspector_as::().unwrap(); - assert!(inspected.status); + assert!(inspected.is_success()); assert!(inspected.logs.is_empty()); assert_eq!( inspector.0.iter().map(|log| log.address).collect::>(), diff --git a/crates/evm2/src/utils.rs b/crates/evm2/src/utils.rs index 39d97695a..6e2b936b4 100644 --- a/crates/evm2/src/utils.rs +++ b/crates/evm2/src/utils.rs @@ -279,7 +279,7 @@ pub fn left_pad_vec_be(data: &[u8], len: usize) -> Cow<'_, [u8]> { /// This is optimized to not allocate at runtime by using 2 static arrays. #[inline] pub const fn bool_to_bytes32(value: bool) -> Bytes { - Bytes::from_static(&bool_to_b256(value).0) + Bytes::from_static(bool_to_b256(value).as_slice()) } /// Converts a boolean to a left-padded [`B256`] value. diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 997ad7d93..4fdeaf80e 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -6,7 +6,7 @@ use crate::tracing::{ use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, vec, vec::Vec}; use alloy_primitives::{ Address, B256, Bytes, U256, - map::{AddressSet, HashMap}, + map::{AddressMap, AddressSet, HashMap}, }; use alloy_rpc_types_trace::geth::{ AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, GethDefaultTracingOptions, @@ -141,7 +141,7 @@ impl<'a> GethTraceBuilder<'a> { &self, main_trace_node: &CallTraceNode, opts: &GethDefaultTracingOptions, - storage: &mut HashMap>, + storage: &mut AddressMap>, struct_logs: &mut Vec, mut stacks: Option<&mut [StackSnapshots]>, ) { @@ -569,7 +569,7 @@ mod tests { use evm2::{ AccountInfo, DatabaseError, bytecode::Bytecode, - evm::{CacheDB, DbResult, DynDatabase, EmptyDB}, + evm::{DbResult, DynDatabase, InMemoryDB}, interpreter::{InstrStop, Word, op}, }; @@ -691,7 +691,7 @@ mod tests { ); state.insert_account(empty_addr, None, Some(AccountInfo::default().with_nonce(1))); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &prefunded_addr, AccountInfo::default().with_balance(U256::from(10)), @@ -725,7 +725,7 @@ mod tests { state.insert_account(address, None, Some(account)); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); let builder = GethTraceBuilder::new(Vec::new()); let frame = builder.geth_prestate_diff_traces(&state, &mut db, true, false).unwrap(); @@ -765,7 +765,7 @@ mod tests { let nonce_account = Address::with_last_byte(4); let contract = Address::with_last_byte(5); let code = Bytecode::new_legacy(Bytes::from_static(&[0x00])); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info(&empty_beneficiary, AccountInfo::default()); db.insert_account_info( &funded_beneficiary, diff --git a/crates/inspectors/src/tracing/builder/parity.rs b/crates/inspectors/src/tracing/builder/parity.rs index 9182b23c3..642b7d47c 100644 --- a/crates/inspectors/src/tracing/builder/parity.rs +++ b/crates/inspectors/src/tracing/builder/parity.rs @@ -609,17 +609,14 @@ pub fn populate_state_diff( entry.balance = if original.balance == info.balance { Delta::Unchanged } else { - Delta::Changed(ChangedType { from: original.balance, to: info.balance }) + Delta::changed(original.balance, info.balance) }; // this is relevant for the caller and contracts entry.nonce = if original.nonce == info.nonce { Delta::Unchanged } else { - Delta::Changed(ChangedType { - from: U64::from(original.nonce), - to: U64::from(info.nonce), - }) + Delta::changed(U64::from(original.nonce), U64::from(info.nonce)) }; } } diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs index 2158ce82e..e3937d249 100644 --- a/crates/inspectors/src/tracing/erc7562.rs +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -203,7 +203,7 @@ impl Erc7562Inspector { // geth's OnExit checks ErrOutOfGas / ErrCodeStoreOutOfGas, not the bare // ErrGasUintOverflow of an oversized memory operand. The root is excluded. out_of_gas: idx != 0 - && node.trace.status.is_some_and(|status| { + && node.status().is_some_and(|status| { status.is_out_of_gas() && status != InstrStop::InvalidOperandOOG }), keccak: if idx == 0 { self.keccak.iter().cloned().collect() } else { Vec::new() }, diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index b2a4bf1e8..13f89f010 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -1118,7 +1118,7 @@ mod tests { use alloc::vec; use boa_engine::{Source, object::builtins::JsUint8Array}; use core::convert::Infallible; - use evm2::evm::{CacheDB, Database, Db, EmptyDB}; + use evm2::evm::{Database, Db, InMemoryDB}; /// Records and enters a step with the given pre- and post-execution stack. fn with_step<'a, R>( @@ -1237,7 +1237,7 @@ mod tests { let f = result.as_callable().unwrap(); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); let state = PendingState::default(); let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); { @@ -1290,7 +1290,7 @@ mod tests { let result_fn = obj.get(js_string!("result"), &mut context).unwrap().as_object().unwrap(); let setup_fn = obj.get(js_string!("setup"), &mut context).unwrap().as_object().unwrap(); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); let state = PendingState::default(); { let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); @@ -1539,7 +1539,7 @@ mod tests { let memory = RefCell::new(vec![7u8; 32]); let contract = Contract::default(); let state = PendingState::default(); - let mut database = CacheDB::new(EmptyDB::default()); + let mut database = InMemoryDB::default(); let result = catch_unwind(AssertUnwindSafe(|| { db.with_changes_scope(&state, &mut database, || { with_step(&log, opcode::STOP, &stack, &memory, &contract, || { @@ -1619,7 +1619,7 @@ mod tests { view.post = Some(post.leak()); assert_eq!(view.len(), 64); - assert_eq!(view.bytes(0..64).unwrap().as_slice(), pre.as_slice()); + assert_eq!(view.bytes(0..64).unwrap().as_slice(), pre); assert_eq!(view.bytes(10..20).unwrap().as_slice(), &pre[10..20]); // writes beyond the pre-execution length only expand memory diff --git a/crates/inspectors/src/tracing/js/builtins.rs b/crates/inspectors/src/tracing/js/builtins.rs index 7ae651ea3..96b77600e 100644 --- a/crates/inspectors/src/tracing/js/builtins.rs +++ b/crates/inspectors/src/tracing/js/builtins.rs @@ -2,7 +2,7 @@ use crate::tracing::{js::bindings::type_error, types::CallKind}; use alloc::{borrow::Cow, format, rc::Rc, vec::Vec}; -use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::HashSet}; +use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::AddressSet}; use boa_engine::{ Context, JsArgs, JsBigInt, JsError, JsNativeError, JsResult, JsString, JsValue, NativeFunction, Source, @@ -225,7 +225,7 @@ pub(crate) fn address_to_uint8_array( addr: Address, context: &mut Context, ) -> JsResult { - JsUint8Array::from_iter(addr, context) + to_uint8_array(addr, context) } /// Create a new [JsUint8Array] array buffer from the address' bytes. @@ -449,7 +449,7 @@ pub(crate) fn slice(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResul /// /// Unset until the transaction starts, like geth's list of active precompiles. #[derive(Clone, Debug, Default)] -pub(crate) struct PrecompileList(Rc>>>); +pub(crate) struct PrecompileList(Rc>>); impl PrecompileList { /// Registers the global callable `isPrecompiled`, which reads this list. diff --git a/crates/inspectors/src/tracing/js/interrupt_tests.rs b/crates/inspectors/src/tracing/js/interrupt_tests.rs index 1a3b23cbe..727326bdb 100644 --- a/crates/inspectors/src/tracing/js/interrupt_tests.rs +++ b/crates/inspectors/src/tracing/js/interrupt_tests.rs @@ -10,8 +10,8 @@ use evm2::{ block::ethereum::execution::execute_transaction_without_commit, bytecode::Bytecode, env::BlockEnvExt, - ethereum::{TxEnvelope, ethereum_tx_registry}, - evm::{AccountInfo, CacheDB, EmptyDB}, + ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + evm::{AccountInfo, EmptyDB, InMemoryDB}, registry::HandlerError, }; @@ -20,14 +20,14 @@ const TARGET: Address = Address::with_last_byte(0x42); #[test] fn unset_interrupt_preserves_results() { - for interrupt in [None, Some(JsInspectorInterrupt::default())] { + for interrupt in [None, Some(JsInspectorInterrupt::new())] { let mut inspector = JsInspector::new(TRACER.into(), serde_json::Value::Null).unwrap(); if let Some(interrupt) = interrupt { inspector = inspector.with_interrupt(interrupt); } let (mut inspector, result) = run(inspector, &hex!("60015000"), TxKind::Call(TARGET)); let result = result.unwrap(); - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!( inspector .json_result( @@ -248,7 +248,7 @@ fn run( code: &[u8], kind: TxKind, ) -> (JsInspector, Result) { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &TARGET, AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), @@ -272,7 +272,7 @@ fn assert_interrupted(result: Result) { )); } -pub(super) fn transaction(kind: TxKind) -> Recovered { +pub(super) fn transaction(kind: TxKind) -> RecoveredTxEnvelope { Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { gas_limit: 1_000_000, to: kind, ..Default::default() }), Address::ZERO, @@ -285,7 +285,7 @@ fn hook_error_takes_precedence_over_a_later_interrupt() { let (mut inspector, result) = run(interruptible(script), &hex!("60015000"), TxKind::Call(TARGET)); let result = result.unwrap(); - assert!(result.result.status); + assert!(result.result.is_success()); inspector.interrupt.as_ref().unwrap().interrupt(); let error = inspector .json_result( diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index cd8e1c944..807589178 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -335,7 +335,7 @@ impl JsInspector { let mut output_bytes = None; let mut error = None; - if result.status { + if result.is_success() { output_bytes = Some(result.output.clone()); } else if result.stop.is_frame_revert() { error = Some("execution reverted".to_string()); @@ -1137,7 +1137,7 @@ mod tests { BaseEvmTypes, Evm, Precompiles, SpecId, Version, bytecode::Bytecode, ethereum::{TxEnvelope, ethereum_tx_registry, intrinsic_and_floor_gas}, - evm::{AccountInfo, CacheDB, EmptyDB}, + evm::{AccountInfo, InMemoryDB}, interpreter::{Host, op}, }; use serde_json::json; @@ -1301,7 +1301,7 @@ mod tests { tx: TxEnvelope, ) -> Result { let addr = Address::repeat_byte(0x01); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); // Insert the caller db.insert_account_info( @@ -1332,7 +1332,7 @@ mod tests { let tx = Recovered::new_unchecked(tx, Address::ZERO); let res = evm.transact(&tx).expect("pass without error").detach(); - assert_eq!(res.result.status, success); + assert_eq!(res.result.is_success(), success); let mut inspector = evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); inspector.json_result(&res, &tx, &block, evm.database_mut()) @@ -1822,7 +1822,7 @@ mod tests { result: function() { return this.count; } }"#; let addr = Address::repeat_byte(0x01); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1e18), ..Default::default() }, @@ -1847,7 +1847,7 @@ mod tests { for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); - assert!(res.result.status); + assert!(res.result.is_success()); inspector = *evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); assert_eq!( @@ -1893,7 +1893,7 @@ mod tests { bytecode.extend_from_slice(&hex!("61fffff150")); // PUSH2 gas, CALL, POP } bytecode.push(0x00); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &addr, AccountInfo { code: Some(Bytecode::new_legacy(bytecode.into())), ..Default::default() }, @@ -1917,7 +1917,7 @@ mod tests { for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); - assert!(res.result.status); + assert!(res.result.is_success()); inspector = *evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); assert_eq!( @@ -2007,7 +2007,7 @@ mod tests { ] { let caller = Address::repeat_byte(0xaa); let nonce = 7; - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info(&caller, AccountInfo { nonce, ..AccountInfo::default() }); let script = r#"{ fault: function() {}, diff --git a/crates/inspectors/src/tracing/js/snapshot_tests.rs b/crates/inspectors/src/tracing/js/snapshot_tests.rs index 59e1bc446..899a1552a 100644 --- a/crates/inspectors/src/tracing/js/snapshot_tests.rs +++ b/crates/inspectors/src/tracing/js/snapshot_tests.rs @@ -7,7 +7,7 @@ use evm2::{ bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, - evm::{AccountInfo, CacheDB, EmptyDB}, + evm::{AccountInfo, InMemoryDB}, interpreter::Host, }; use serde_json::{Value, json}; @@ -74,7 +74,7 @@ impl Inspector for SnapshotInspector { fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec { let address = Address::repeat_byte(0x11); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &address, AccountInfo { code: Some(Bytecode::new_legacy(code.clone())), ..Default::default() }, diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs index 59bb49303..62b9bb3cf 100644 --- a/crates/inspectors/src/tracing/keccak.rs +++ b/crates/inspectors/src/tracing/keccak.rs @@ -11,7 +11,10 @@ use crate::tracing::{TraceLimits, limits::TraceBudget}; use alloc::{borrow::Cow, vec}; -use alloy_primitives::{B256, Bytes, hex, keccak256, map::HashMap}; +use alloy_primitives::{ + Bytes, hex, keccak256, + map::{B256Map, HashMap}, +}; use evm2::{ EvmTypes, Inspector, InspectorConfig, OpcodeSet, interpreter::{Interpreter, opcode::op}, @@ -38,7 +41,7 @@ pub struct LimitExceeded; #[derive(Clone, Debug)] pub struct Keccak256PreimageInspector { /// The map of hash to preimage. - inner: HashMap, + inner: B256Map, /// The bytes of the recorded preimages. budget: TraceBudget, } diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 78a44beb4..2a127a179 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -131,7 +131,7 @@ impl TracingInspector { /// Returns whether recording omitted a byte buffer after reaching the budget. pub const fn limit_exceeded(&self) -> bool { - self.budget.exceeded + self.budget.exceeded() } /// Resets the inspector to its initial state of [Self::new]. @@ -156,7 +156,7 @@ impl TracingInspector { // if we record steps we can reuse the individual calltracestep vecs if config.record_steps { - for node in &mut traces.arena { + for node in traces.nodes_mut() { // move out and store the reusable steps vec let mut steps = mem::take(&mut node.trace.steps); // ensure steps are cleared @@ -312,7 +312,7 @@ impl TracingInspector { /// starting a new transaction: [`Self::fuse`] #[inline] pub fn geth_builder(&self) -> GethTraceBuilder<'_> { - GethTraceBuilder::new_borrowed(&self.traces.arena).with_features(self.features) + GethTraceBuilder::new_borrowed(self.traces.nodes()).with_features(self.features) } /// Returns the last trace [CallTrace] index from the stack. diff --git a/crates/inspectors/src/tracing/utils.rs b/crates/inspectors/src/tracing/utils.rs index 859c3aa5f..fe8897b54 100644 --- a/crates/inspectors/src/tracing/utils.rs +++ b/crates/inspectors/src/tracing/utils.rs @@ -279,7 +279,7 @@ mod tests { "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000214661696c656420746f2076657269667920424950333232207369676e617475726500000000000000000000000000000000000000000000000000000000000000" ); let reason = maybe_revert_reason(&err[..]).unwrap(); - assert_eq!(reason, "Failed to verify BIP322 signature".to_string()); + assert_eq!(reason, "Failed to verify BIP322 signature"); } #[test] diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index 6d485bbb4..60dca894c 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -1,11 +1,11 @@ //! Accesslist tests -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv, a}; +use crate::utils::{AccountInfo, Bytecode, Context, TransactTo, TxEnv, a}; use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; use alloy_primitives::{Address, B256, Signature, U256, address, bytes, hex}; -use evm2::ethereum::TxEnvelope; +use evm2::{ethereum::TxEnvelope, evm::InMemoryDB}; use evm2_inspectors::access_list::AccessListInspector; #[test] @@ -22,17 +22,16 @@ fn initial_access_list_is_normalized_and_pruned() { item(lower, vec![]), ]); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &callee, - AccountInfo { - // Read slot 1 so it is re-added after pruning the callee's stale seeded slot. - code: Some(Bytecode::new_raw(bytes!("60015400"))), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &callee, + AccountInfo { + // Read slot 1 so it is re-added after pruning the callee's stale seeded slot. + code: Some(Bytecode::new_raw(bytes!("60015400"))), + ..Default::default() + }, + ); + }); let mut inspector = AccessListInspector::new(initial); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let result = evm @@ -71,13 +70,12 @@ fn test_access_list_precompile() { let account = address!("341348115259a8bf69f1f50101c227fced83bac6"); let caller = address!("341348115259a8bf69f1f50101c227fced83bac5"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let mut accesslist = AccessListInspector::default(); let mut evm = context.build_mainnet().with_inspector(&mut accesslist); @@ -169,7 +167,7 @@ fn access_list(items: &[(Address, &[B256])]) -> AccessList { fn access_list_matches_geth() { let sender = a(0x1001); for case in CASES { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &sender, AccountInfo { balance: U256::from(10u128.pow(18)), ..Default::default() }, diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index 91d10fb91..d8b6d33b7 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -8,7 +8,7 @@ use evm2::{ BaseEvmTypes, Evm, EvmTypesHost, Inspector, InspectorConfig, NoopInspector, Precompiles, TxResult, TxResultWithState, env as evm_env, ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, - evm::PendingState, + evm::{InMemoryDB, PendingState}, interpreter::{Interpreter, Message, MessageResult}, registry::HandlerError, }; @@ -127,7 +127,7 @@ impl TxEnvBuilder { #[derive(Clone, Debug)] pub struct Context { - pub db: CacheDB, + pub db: InMemoryDB, pub(crate) tx: TxEnv, block: evm_env::BlockEnv, pub(crate) spec: SpecId, @@ -142,19 +142,19 @@ impl Default for Context { impl Context { pub fn mainnet() -> Self { Self { - db: CacheDB::new(EmptyDB::default()), + db: InMemoryDB::default(), tx: TxEnv::default(), block: evm_env::BlockEnvExt::default(), spec: SpecId::OSAKA, } } - pub fn with_db(mut self, db: CacheDB) -> Self { + pub fn with_db(mut self, db: InMemoryDB) -> Self { self.db = db; self } - pub fn modify_db_chained(mut self, f: impl FnOnce(&mut CacheDB)) -> Self { + pub fn modify_db_chained(mut self, f: impl FnOnce(&mut InMemoryDB)) -> Self { f(&mut self.db); self } @@ -285,15 +285,15 @@ impl Context { &self.block } - pub const fn db(&self) -> &CacheDB { + pub const fn db(&self) -> &InMemoryDB { &self.db } - pub const fn db_ref(&self) -> &CacheDB { + pub const fn db_ref(&self) -> &InMemoryDB { &self.db } - pub const fn db_mut(&mut self) -> &mut CacheDB { + pub const fn db_mut(&mut self) -> &mut InMemoryDB { &mut self.db } @@ -420,7 +420,7 @@ pub enum ExecutionResult { impl ExecutionResult { fn from_tx_result(result: TxResult) -> Self { let gas_used = result.tx_gas_used(); - if result.status { + if result.is_success() { let output = if result.created_address.is_some() { Output::Create(result.output, result.created_address) } else { @@ -495,7 +495,7 @@ pub trait DatabaseCommit { fn commit(&mut self, changes: PendingState); } -impl DatabaseCommit for CacheDB { +impl DatabaseCommit for InMemoryDB { fn commit(&mut self, changes: PendingState) { self.commit_source(&changes); } @@ -510,7 +510,7 @@ pub trait TestDbExt { fn load_account(&mut self, address: Address) -> LoadedAccountMut<'_>; } -impl TestDbExt for CacheDB { +impl TestDbExt for InMemoryDB { fn load_account(&mut self, address: Address) -> LoadedAccountMut<'_> { let info = self.cache.accounts.entry(address).or_default().get_or_insert_default(); LoadedAccountMut { info } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 5088db532..23fe1c64b 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -4,8 +4,8 @@ mod consistency; mod runtime_oog; use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, ETH_TRANSFER_LOG_ADDRESS, EmptyDB, SpecId, TransactTo, - TxEnv, deploy_contract, op, + AccountInfo, Bytecode, Context, ETH_TRANSFER_LOG_ADDRESS, SpecId, TransactTo, TxEnv, + deploy_contract, op, }; use alloy_consensus::crypto::secp256k1::sign_message; use alloy_eips::eip7702::Authorization; @@ -17,7 +17,9 @@ use alloy_rpc_types_trace::{ geth::{ CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, - PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, + PreStateConfig, PreStateFrame, StateGasTrace, + erc7562::{AccessedSlots, Erc7562Config}, + mux::MuxConfig, }, parity::{Action, CallType}, }; @@ -25,7 +27,7 @@ use evm2::{ AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, EvmTypesHost, ExecutionConfig, Inspector, InspectorConfig, OpcodeSet, Precompiles, Version, ethereum::ethereum_tx_registry, - evm::{DbResult, DynDatabase}, + evm::{DbResult, DynDatabase, InMemoryDB}, interpreter::{Interpreter, Message, MessageResult, Word}, }; use evm2_inspectors::tracing::{ @@ -48,7 +50,7 @@ fn test_calltracer_invalid_opcode_errors() { let options = serde_json::from_value(serde_json::json!({"tracer": "callTracer"})).unwrap(); let mut inspector = DebugInspector::new(options).unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &account, AccountInfo { @@ -56,10 +58,13 @@ fn test_calltracer_invalid_opcode_errors() { ..Default::default() }, ); - let mut parent_code = hex!("60006000600060006000").to_vec(); - parent_code.push(op::PUSH20); - parent_code.extend_from_slice(account.as_slice()); - parent_code.extend_from_slice(&hex!("61fffff15000")); + let parent_code = [ + &hex!("60006000600060006000")[..], + &[op::PUSH20][..], + account.as_slice(), + &hex!("61fffff15000")[..], + ] + .concat(); db.insert_account_info( &parent, AccountInfo { @@ -98,7 +103,7 @@ fn test_calltracer_eip7702_call_targets() { let implementation = address!("3000000000000000000000000000000000000003"); for opcode in [op::CALL, op::CALLCODE, op::DELEGATECALL, op::STATICCALL] { let trace = |tracer: &str| { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { @@ -186,16 +191,12 @@ fn test_debug_empty_tracer() { // Write memory and storage, and call the identity precompile to populate return data. let code = hex!("602a6000526020600060206000600461fffffa50602a60005560206000f3"); let account = address!("1000000000000000000000000000000000000001"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { - code: Some(Bytecode::new_raw(code.into())), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm .inspect_tx(TxEnv { @@ -209,7 +210,7 @@ fn test_debug_empty_tracer() { let tx = ctx.tx().envelope(); let block = *ctx.block(); let result = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); - assert!(matches!(result, GethTrace::Default(_))); + assert!(result.is_default()); let result = serde_json::to_value(result).unwrap(); let logs = result["structLogs"].as_array().unwrap(); assert!(!logs.is_empty()); @@ -323,7 +324,7 @@ fn test_geth_calltracer_logs() { } } */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let code = hex!( "608060405234801561001057600080fd5b506103ac806100206000396000f3fe60806040526004361061003f5760003560e01c80630332ed131461014d5780636ae1ad40146101625780638384a00214610177578063de7eb4f31461018c575b60405134815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316636ae1ad406040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561009d57600080fd5b505af19250505080156100ae575060015b50306001600160a01b0316630332ed136040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156100ea57600080fd5b505af19250505080156100fb575060015b50306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561013757600080fd5b505af115801561014b573d6000803e3d6000fd5b005b34801561015957600080fd5b5061014b6101a1565b34801561016e57600080fd5b5061014b610253565b34801561018357600080fd5b5061014b6102b7565b34801561019857600080fd5b5061014b6102dd565b306001600160a01b031663de7eb4f36040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156101dc57600080fd5b505af11580156101f0573d6000803e3d6000fd5b505060405162461bcd60e51b8152602060048201526024808201527f6e6573746564456d6974576974684661696c75726541667465724e6573746564604482015263115b5a5d60e21b6064820152608401915061024a9050565b60405180910390fd5b6040516000815233906000805160206103578339815191529060200160405180910390a260405162461bcd60e51b81526020600482015260156024820152746e6573746564456d6974576974684661696c75726560581b604482015260640161024a565b6040516000815233906000805160206103578339815191529060200160405180910390a2565b6040516000815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561033c57600080fd5b505af1158015610350573d6000803e3d6000fd5b5050505056fef950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23ea2646970667358221220090a696b9fbd22c7d1cc2a0b6d4a48c32d3ba892480713689a3145b73cfeb02164736f6c63430008130033" ); @@ -387,13 +388,12 @@ fn test_geth_erc7562_tracer() { let account = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); let mut inspector = DebugInspector::new(opts).unwrap(); @@ -464,7 +464,7 @@ fn test_geth_erc7562_counts_like_geth() { let target_code = Bytes::from_static(&[op::STOP]); let failing_code = bytes!("6001600020"); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [ (root, Bytes::from(code)), (target, target_code), @@ -565,7 +565,7 @@ fn test_geth_erc7562_accessed_slots_and_ignored_opcodes() { op::TSTORE, op::STOP, ]); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, @@ -598,15 +598,17 @@ fn test_geth_erc7562_accessed_slots_and_ignored_opcodes() { let slot = |slot: u8| B256::with_last_byte(slot); let slots = &frame.accessed_slots; assert_eq!( - slots.reads, - HashMap::from_iter([ - (slot(1), vec![B256::with_last_byte(0x2a)]), - (slot(2), vec![B256::with_last_byte(0x2b)]), - ]) + *slots, + AccessedSlots { + reads: HashMap::from_iter([ + (slot(1), vec![B256::with_last_byte(0x2a)]), + (slot(2), vec![B256::with_last_byte(0x2b)]), + ]), + writes: HashMap::from_iter([(slot(1), 1), (slot(3), 1)]), + transient_reads: HashMap::from_iter([(slot(4), 1)]), + transient_writes: HashMap::from_iter([(slot(4), 1)]), + } ); - assert_eq!(slots.writes, HashMap::from_iter([(slot(1), 1), (slot(3), 1)])); - assert_eq!(slots.transient_reads, HashMap::from_iter([(slot(4), 1)])); - assert_eq!(slots.transient_writes, HashMap::from_iter([(slot(4), 1)])); // The opcodes of the config are not counted, and every other opcode is, including `PUSH1`. assert_eq!( frame.used_opcodes, @@ -637,7 +639,7 @@ fn test_geth_erc7562_bounds_retained_preimages() { op::KECCAK256, op::STOP, ]); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, @@ -673,7 +675,7 @@ fn test_geth_erc7562_bounds_retained_preimages() { } /// A database whose storage reads fail. -struct NoStorageDb(CacheDB); +struct NoStorageDb(InMemoryDB); impl DynDatabase for NoStorageDb { fn get_account(&mut self, address: &Address) -> DbResult> { @@ -697,7 +699,7 @@ impl DynDatabase for NoStorageDb { fn test_geth_erc7562_reports_a_failed_state_read() { let contract = address!("1000000000000000000000000000000000000001"); let spec = SpecId::CANCUN; - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo { @@ -753,7 +755,7 @@ fn test_geth_erc7562_contract_size_reads_accepted_state() { let mut code = vec![op::PUSH20]; code.extend_from_slice(target.as_slice()); code.extend_from_slice(&[op::EXTCODESIZE, op::STOP]); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &root, AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, @@ -806,19 +808,15 @@ fn test_geth_erc7562_records_warm_first_reads() { let outer_code = hex!("6001545060006000600060007310000000000000000000000000000000000000035af45000").to_vec(); let inner_code = hex!("6001545000").to_vec(); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (address, code) in [(outer, outer_code), (inner, inner_code)] { - db.insert_account_info( - &address, - AccountInfo { - code: Some(Bytecode::new_raw(code.into())), - ..Default::default() - }, - ); - } - db.insert_account_storage(&outer, &U256::ONE, &U256::from(0x2a)); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (address, code) in [(outer, outer_code), (inner, inner_code)] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + db.insert_account_storage(&outer, &U256::ONE, &U256::from(0x2a)); + }); let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); let mut inspector = DebugInspector::new(opts).unwrap(); @@ -854,7 +852,7 @@ fn trace_contract( let contract = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = spec) .modify_db_chained(|db| { db.insert_account_info( @@ -893,13 +891,12 @@ fn trace_contract_with_version( ) -> (GethTrace, evm2::TxResult) { let contract = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); - let mut context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &contract, - AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, - ); - }); + let mut context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + }); let block = *context.block(); let mut inspector = DebugInspector::new(opts).unwrap(); let mut evm = Evm::::new_with_execution_config( @@ -941,10 +938,15 @@ fn test_geth_state_gas_tracer() { match trace { GethTrace::StateGasTracer(frame) => { - assert_eq!(frame.gas_used, gas.tx_gas_used()); - assert_eq!(frame.execution_gas_used, gas.execution_gas_spent()); - assert_eq!(frame.state_gas_used, gas.state_gas_spent()); - assert_eq!(frame.gas_refund, gas.refunded); + assert_eq!( + frame, + StateGasTrace { + gas_used: gas.tx_gas_used(), + execution_gas_used: gas.execution_gas_spent(), + state_gas_used: gas.state_gas_spent(), + gas_refund: gas.refunded, + } + ); } _ => panic!("Expected StateGasTracer"), } @@ -1089,7 +1091,7 @@ fn test_geth_mux_tracer() { } */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let code = hex!( "608060405234801561001057600080fd5b506103ac806100206000396000f3fe60806040526004361061003f5760003560e01c80630332ed131461014d5780636ae1ad40146101625780638384a00214610177578063de7eb4f31461018c575b60405134815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316636ae1ad406040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561009d57600080fd5b505af19250505080156100ae575060015b50306001600160a01b0316630332ed136040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156100ea57600080fd5b505af19250505080156100fb575060015b50306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561013757600080fd5b505af115801561014b573d6000803e3d6000fd5b005b34801561015957600080fd5b5061014b6101a1565b34801561016e57600080fd5b5061014b610253565b34801561018357600080fd5b5061014b6102b7565b34801561019857600080fd5b5061014b6102dd565b306001600160a01b031663de7eb4f36040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156101dc57600080fd5b505af11580156101f0573d6000803e3d6000fd5b505060405162461bcd60e51b8152602060048201526024808201527f6e6573746564456d6974576974684661696c75726541667465724e6573746564604482015263115b5a5d60e21b6064820152608401915061024a9050565b60405180910390fd5b6040516000815233906000805160206103578339815191529060200160405180910390a260405162461bcd60e51b81526020600482015260156024820152746e6573746564456d6974576974684661696c75726560581b604482015260640161024a565b6040516000815233906000805160206103578339815191529060200160405180910390a2565b6040516000815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561033c57600080fd5b505af1158015610350573d6000803e3d6000fd5b5050505056fef950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23ea2646970667358221220090a696b9fbd22c7d1cc2a0b6d4a48c32d3ba892480713689a3145b73cfeb02164736f6c63430008130033" @@ -1220,10 +1222,15 @@ fn test_geth_mux_tracer() { .clone(); match state_gas_frame { GethTrace::StateGasTracer(state_gas) => { - assert_eq!(state_gas.gas_used, res.tx_result.result.tx_gas_used()); - assert_eq!(state_gas.execution_gas_used, res.tx_result.result.execution_gas_spent()); - assert_eq!(state_gas.state_gas_used, res.tx_result.result.state_gas_spent()); - assert_eq!(state_gas.gas_refund, res.tx_result.result.refunded); + assert_eq!( + state_gas, + StateGasTrace { + gas_used: res.tx_result.result.tx_gas_used(), + execution_gas_used: res.tx_result.result.execution_gas_spent(), + state_gas_used: res.tx_result.result.state_gas_spent(), + gas_refund: res.tx_result.result.refunded, + } + ); } _ => panic!("Expected StateGasTracer"), } @@ -1234,7 +1241,7 @@ fn test_geth_inspector_reset() { let insp = TracingInspector::new(TracingInspectorConfig::default_geth()); let context = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON); assert_eq!(insp.traces().nodes().first().unwrap().trace.gas_limit, 0); @@ -1289,7 +1296,7 @@ fn test_geth_calltracer_top_call_reverting() { Test that verifies the behavior of only_top_call with a reverting transaction. Uses the LogTracing contract which has functions that make nested calls and revert. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Use the LogTracing contract from test_geth_calltracer_logs let code = hex!( @@ -1330,7 +1337,7 @@ fn test_geth_calltracer_top_call_reverting() { // Now test with only_top_call = false to verify we see the nested structure let mut insp2 = TracingInspector::new(TracingInspectorConfig::default_geth()); - let mut evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr2 = deploy_contract(&mut evm2, code.into(), deployer, SpecId::LONDON) .created_address() @@ -1379,7 +1386,7 @@ fn test_geth_calltracer_nested_revert() { Test that verifies the behavior of only_top_call with the nestedRevert function. This function calls nestedEmitWithFailure which emits a log and then reverts. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Use the LogTracing contract with nestedRevert function let code = hex!( @@ -1420,7 +1427,7 @@ fn test_geth_calltracer_nested_revert() { // Now test with only_top_call = false let mut insp2 = TracingInspector::new(TracingInspectorConfig::default_geth()); - let mut evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr2 = deploy_contract(&mut evm2, code.into(), deployer, SpecId::LONDON) .created_address() @@ -1457,7 +1464,7 @@ fn test_geth_calltracer_nested_revert() { // Test revert with topcall let mut insp3 = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); - let mut evm3 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm3 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr3 = deploy_contract(&mut evm3, code.into(), deployer, SpecId::LONDON) .created_address() @@ -1498,7 +1505,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { Test that verifies the disableCode flag works correctly in diff mode for PreStateTracer. When disableCode is true, code should be excluded from both pre and post states. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy a simple contract let code = hex!( @@ -1574,7 +1581,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { let mut insp2 = TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config( &prestate_config_with_code, )); - let evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let mut db2 = evm2.ctx.db.clone(); let res2 = { @@ -1642,7 +1649,7 @@ fn test_geth_calltracer_null_bytes_revert_reason_omitted() { This simulates scenarios where contract reverts with empty or null-byte-only data, ensuring compatibility with Geth behavior of omitting such meaningless revert reasons. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy a simple contract that reverts with empty or null bytes // This bytecode reverts with 32 null bytes: revert(0, 32) @@ -1708,7 +1715,7 @@ fn test_geth_default_tracer_omits_empty_return_data() { let code = hex!("00"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -1717,10 +1724,7 @@ fn test_geth_default_tracer_omits_empty_return_data() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); @@ -1776,11 +1780,11 @@ fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { ); let deployer = alloy_primitives::address!("341348115259a8bf69f1f50101c227fced83bac6"); - let value = alloy_primitives::U256::from(69); + let value = U256::from(69); // Deploy the contract in a separate transaction first let mut context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); }) @@ -1869,14 +1873,14 @@ fn test_geth_calltracer_logs_eip7708() { let caller = address!("0xa000000000000000000000000000000000000001"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { // Fund the contract so it can send 1 wei db.insert_account_info( &contract, AccountInfo { - balance: alloy_primitives::U256::from(1_000_000), + balance: U256::from(1_000_000), code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, @@ -1884,10 +1888,7 @@ fn test_geth_calltracer_logs_eip7708() { // Fund the caller db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); @@ -1960,7 +1961,7 @@ fn test_geth_calltracer_logs_address_regular() { let caller = address!("0xa000000000000000000000000000000000000002"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -1969,10 +1970,7 @@ fn test_geth_calltracer_logs_address_regular() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); @@ -2024,18 +2022,20 @@ fn test_geth_calltracer_logs_delegatecall() { // Proxy bytecode: DELEGATECALL(gas=0xFFFF, addr=implementation, 0, 0, 0, 0); STOP let proxy_code = { - let mut code = Vec::new(); - code.extend_from_slice(&hex!("6000600060006000")); // retSize, retOffset, argsSize, argsOffset - code.push(0x73); // PUSH20 - code.extend_from_slice(implementation.as_slice()); // implementation address - code.extend_from_slice(&hex!("61FFFF")); // PUSH2 gas - code.push(0xF4); // DELEGATECALL - code.push(0x00); // STOP + let code = [ + &hex!("6000600060006000")[..], // retSize, retOffset, argsSize, argsOffset + &[0x73][..], // PUSH20 + implementation.as_slice(), // implementation address + &hex!("61FFFF")[..], // PUSH2 gas + &[0xF4][..], // DELEGATECALL + &[0x00][..], // STOP + ] + .concat(); Bytes::from(code) }; let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -2051,10 +2051,7 @@ fn test_geth_calltracer_logs_delegatecall() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); @@ -2115,7 +2112,7 @@ fn test_geth_opcode_limit_end_to_end() { let mut code = hex!("6000600060006000600060ff61fffff150602a60005360016000").to_vec(); code.push(terminal); let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { for (addr, code) in [(account, Bytes::from(code)), (child, bytes!("60015000"))] { db.insert_account_info( @@ -2193,18 +2190,14 @@ fn test_geth_struct_logs_follow_calls_to_precompiles() { let code = [call([0, 4]), call([0x12, 0x34]), vec![op::STOP]].concat(); let child_code = vec![op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP]; - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (addr, code) in [(account, code), (child, child_code)] { - db.insert_account_info( - &addr, - AccountInfo { - code: Some(Bytecode::new_raw(code.into())), - ..Default::default() - }, - ); - } - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (addr, code) in [(account, code), (child, child_code)] { + db.insert_account_info( + &addr, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + }); let mut inspector = TracingInspector::new(TracingInspectorConfig::default_geth()); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm @@ -2256,7 +2249,7 @@ fn test_geth_opcode_limit() { code.push(terminal); let run = |inspector: &mut TracingInspector, opts: GethDefaultTracingOptions| { let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { for (addr, code) in [(account, Bytes::from(code.clone())), (child, bytes!("60015000"))] { @@ -2369,7 +2362,7 @@ fn test_geth_struct_log_out_of_gas_cost() { let code = [op::PUSH1, 0x01, op::PUSH1, 0x00, op::SSTORE]; let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -2418,7 +2411,7 @@ fn test_geth_calltracer_log_index_eip7708_revert() { let reverts = address!("0000000000000000000000000000000000002222"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( @@ -2503,7 +2496,7 @@ fn test_debug_call_log_index_seed_and_reset() { inspector.set_next_log_index(seed); } let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { db.insert_account_info( &account, AccountInfo { @@ -2560,7 +2553,7 @@ fn test_geth_4byte_tracer_skips_precompiles() { match trace { GethTrace::FourByteTracer(frame) => { - assert_eq!(frame.0, std::collections::BTreeMap::from([("0x00000000-0".into(), 1)])); + assert_eq!(frame.0, BTreeMap::from([("0x00000000-0".into(), 1)])); } _ => panic!("Expected FourByteTracer"), } @@ -2572,9 +2565,9 @@ fn test_debug_default_tracer_moves_stacks() { let caller = address!("1000000000000000000000000000000000000001"); let callee = address!("1000000000000000000000000000000000000002"); // Call the callee with zero arguments, then push a value. - let mut caller_code = hex!("6000600060006000600073").to_vec(); - caller_code.extend_from_slice(callee.as_slice()); - caller_code.extend_from_slice(&hex!("5af150600700")); + let caller_code = + [&hex!("6000600060006000600073")[..], callee.as_slice(), &hex!("5af150600700")[..]] + .concat(); for disable_stack in [false, true] { for limit in [None, Some(0), Some(1000)] { @@ -2586,7 +2579,7 @@ fn test_debug_default_tracer_moves_stacks() { let mut inspector = DebugInspector::new(GethDebugTracingOptions { config, ..Default::default() }) .unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [(caller, caller_code.clone()), (callee, hex!("6001600201").to_vec())] { @@ -2659,7 +2652,7 @@ fn test_geth_failed_root_logs() { let code = code.parse::().unwrap(); for create in [false, true] { let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -2737,9 +2730,9 @@ fn test_geth_failed_root_logs() { } } -fn contract_with_code(code: &[u8]) -> (Address, CacheDB) { +fn contract_with_code(code: &[u8]) -> (Address, InMemoryDB) { let contract = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), @@ -2877,9 +2870,9 @@ fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { } } -fn precompile_call_contract(code: &[u8]) -> (Address, CacheDB) { +fn precompile_call_contract(code: &[u8]) -> (Address, InMemoryDB) { let contract = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo::default() @@ -3021,14 +3014,14 @@ fn mux_runs_nested_mux_and_erc7562_tracers() { panic!("expected a nested mux frame"); }; assert_eq!(nested.0.len(), 2); - assert!(matches!( - nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)], - GethTrace::CallTracer(_) - )); - assert!(matches!( - nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::NoopTracer)], - GethTrace::NoopTracer(_) - )); + assert!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)] + .is_call() + ); + assert!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::NoopTracer)] + .is_noop() + ); } /// The expected snapshots come from go-ethereum master a0cba163: the struct logger adds the slot @@ -3042,7 +3035,7 @@ fn struct_log_storage_snapshots_match_geth() { // SLOAD 5 (holds 7), SSTORE 5 = 7 (unchanged), SLOAD 5 (warm), SSTORE 6 = 1, SLOAD 6, and an // SSTORE that runs out of gas, which fails before it runs. let code = hex!("60055450600760055560055450600160065560065450600160075500"); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &account, AccountInfo { code: Some(Bytecode::new_raw(Bytes::from(code))), ..Default::default() }, @@ -3070,7 +3063,7 @@ fn struct_log_storage_snapshots_match_geth() { let snapshots: Vec<_> = frame .struct_logs .iter() - .filter(|log| matches!(log.op.as_ref(), "SLOAD" | "SSTORE")) + .filter(|log| matches!(log.opcode(), "SLOAD" | "SSTORE")) .map(|log| (log.op.to_string(), log.storage.clone(), log.error.is_some())) .collect(); let after_first = BTreeMap::from([(slot(5), value(7))]); @@ -3097,7 +3090,7 @@ fn struct_log_storage_snapshots_follow_the_storage_context() { "60015450" "600060006000600060d05afa5000" ); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [ (parent, parent_code.to_vec()), (writer, hex!("6002600155").to_vec()), @@ -3131,7 +3124,7 @@ fn struct_log_storage_snapshots_follow_the_storage_context() { let logs: Vec<_> = frame .struct_logs .iter() - .filter(|log| matches!(log.op.as_ref(), "SLOAD" | "SSTORE")) + .filter(|log| matches!(log.opcode(), "SLOAD" | "SSTORE")) .map(|log| (log.depth, log.op.to_string(), log.storage.clone())) .collect(); assert_eq!( @@ -3152,7 +3145,7 @@ fn test_geth_prestate_diff_omits_empty_accounts() { let coinbase = address!("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"); let recipient = Address::repeat_byte(0x22); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &sender, AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, @@ -3217,7 +3210,7 @@ fn test_geth_prestate_diff_reports_cleared_code() { let authority = address!("7e5f4552091a69125d5dfcb7b8c2659029395bdf"); let delegation = bytes!("ef0100000000000000000000000000000000000000aaaa"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &sender, AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, @@ -3226,7 +3219,7 @@ fn test_geth_prestate_diff_reports_cleared_code() { &authority, AccountInfo { balance: U256::ONE, - code_hash: alloy_primitives::keccak256(&delegation), + code_hash: keccak256(&delegation), code: Some(Bytecode::new_raw(delegation.clone())), ..Default::default() }, @@ -3282,7 +3275,7 @@ fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreSt // SLOAD 1, SLOAD 2, SELFDESTRUCT to the beneficiary. let code = bytes!("6001545060025450733333333333333333333333333333333333333333ff"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &sender, AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, @@ -3392,7 +3385,7 @@ fn test_geth_erc7562_operand_overflow_is_not_out_of_gas() { root_code.push(op::PUSH20); root_code.extend_from_slice(child.as_slice()); root_code.extend_from_slice(&hex!("61fffff15000")); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [(root, root_code), (child, child_code)] { db.insert_account_info( &address, diff --git a/crates/inspectors/tests/it/geth/consistency.rs b/crates/inspectors/tests/it/geth/consistency.rs index 77b3e13a6..ff0c3571b 100644 --- a/crates/inspectors/tests/it/geth/consistency.rs +++ b/crates/inspectors/tests/it/geth/consistency.rs @@ -2,6 +2,7 @@ use super::*; use alloy_primitives::bytes; +use evm2::evm::InMemoryDB; use serde_json::{Value, json}; #[test] @@ -29,7 +30,7 @@ fn mux_config_matches_standalone() { fn flat_call_root_gas_matches_mux() { let trace = |options| { let target = Address::repeat_byte(0xaa); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { @@ -65,7 +66,7 @@ fn flat_call_runtime_oog_root_matches_mux() { let recipient = address!("1000000000000000000000000000000000000002"); let trace = |options| { let inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, diff --git a/crates/inspectors/tests/it/geth/runtime_oog.rs b/crates/inspectors/tests/it/geth/runtime_oog.rs index 1605df0ff..1a654e42e 100644 --- a/crates/inspectors/tests/it/geth/runtime_oog.rs +++ b/crates/inspectors/tests/it/geth/runtime_oog.rs @@ -3,7 +3,7 @@ use super::*; use alloy_primitives::bytes; use alloy_rpc_types_trace::geth::{CallFrame, CallLogFrame}; -use evm2::interpreter::InstrStop; +use evm2::{evm::InMemoryDB, interpreter::InstrStop}; use evm2_inspectors::tracing::TracingInspector; #[test] @@ -21,7 +21,7 @@ fn runtime_oog_call_frames_and_reuse() { let mut inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( @@ -51,7 +51,7 @@ fn runtime_oog_call_frames_and_reuse() { }) .unwrap(); let result = &res.tx_result.result; - assert_eq!(result.status, !halted); + assert_eq!(result.is_success(), !halted); assert_eq!(result.tx_gas_used(), gas_limit); let (ctx, inspector) = evm.ctx_inspector(); let tx = ctx.tx().envelope(); @@ -107,7 +107,7 @@ fn runtime_oog_frame_reaches_four_byte_tracer() { let options = serde_json::json!({"tracer": "4byteTracer"}); let mut inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( @@ -140,7 +140,7 @@ fn runtime_oog_root_reports_the_execution_budget() { let caller = address!("1000000000000000000000000000000000000001"); let recipient = address!("1000000000000000000000000000000000000002"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( diff --git a/crates/inspectors/tests/it/geth_js.rs b/crates/inspectors/tests/it/geth_js.rs index 8943ba6a9..a91d6cd9d 100644 --- a/crates/inspectors/tests/it/geth_js.rs +++ b/crates/inspectors/tests/it/geth_js.rs @@ -4,14 +4,15 @@ mod errors; mod values; use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, EmptyDB, ResultAndState, SpecId, TestDbExt, - TransactTo, TxEnv, deploy_contract, + AccountInfo, Bytecode, Context, ResultAndState, SpecId, TestDbExt, TransactTo, TxEnv, + deploy_contract, }; use alloy_primitives::{Address, U256, address, bytes, hex, map::HashMap}; use alloy_rpc_types_trace::geth::{ GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethTrace, mux::MuxConfig, }; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{ DebugInspector, MuxInspector, js::{JsInspector, JsInspectorInterrupt}, @@ -41,10 +42,8 @@ fn test_geth_jstracer_bigint_compatibility() { } }"#; let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); - let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) - .build_mainnet() - .with_inspector(insp); + let mut evm = + Context::mainnet().with_db(InMemoryDB::default()).build_mainnet().with_inspector(insp); let res = evm .inspect_tx(TxEnv { caller: Address::ZERO, @@ -85,7 +84,7 @@ fn test_geth_jstracer_revert() { let deployer = Address::ZERO; let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) .build_mainnet(); @@ -173,7 +172,7 @@ fn test_geth_jstracer_proxy_contract() { let deployer = address!("f077b491b355e64048ce21e3a6fc4751eeea77fa"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) .build_mainnet(); @@ -190,9 +189,8 @@ fn test_geth_jstracer_proxy_contract() { evm.ctx().db_mut().load_account(deployer).info.balance = U256::from(1_000_000); // Set input data for ProxyFactory.transfer(address) - let mut input_data = hex!("1a695230").to_vec(); // keccak256("transfer(address)")[:4] - input_data.extend_from_slice(&[0u8; 12]); // Pad with zeros - input_data.extend_from_slice(token_addr.as_slice()); + // keccak256("transfer(address)")[:4], zero padding, then the address. + let input_data = [&hex!("1a695230")[..], &[0u8; 12][..], token_addr.as_slice()].concat(); println!("token {token_addr:?} proxy: {proxy_addr:?}"); let code = r#" @@ -250,17 +248,16 @@ fn test_geth_jstracer_records_call_step_before_nested_execution() { .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &child, - AccountInfo { code: Some(child_code), ..Default::default() }, - ); - db.insert_account_info( - &parent, - AccountInfo { code: Some(parent_code), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &child, + AccountInfo { code: Some(child_code), ..Default::default() }, + ); + db.insert_account_info( + &parent, + AccountInfo { code: Some(parent_code), ..Default::default() }, + ); + }); let code = r#" { @@ -331,18 +328,17 @@ fn test_geth_jstracer_call_step_cost_and_depth() { "5f5f5f5f600161111161fffff15000" ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (address, code) in [ - (leaf, &leaf_code[..]), - (mid, &mid_code[..]), - (reverter, &reverter_code[..]), - (main, &main_code[..]), - ] { - let code = Some(Bytecode::new_legacy(code.to_vec().into())); - db.insert_account_info(&address, AccountInfo { code, ..Default::default() }); - } - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (address, code) in [ + (leaf, &leaf_code[..]), + (mid, &mid_code[..]), + (reverter, &reverter_code[..]), + (main, &main_code[..]), + ] { + let code = Some(Bytecode::new_legacy(code.to_vec().into())); + db.insert_account_info(&address, AccountInfo { code, ..Default::default() }); + } + }); let code = r#" { @@ -437,20 +433,19 @@ fn test_geth_debug_inspector_jstracer() { let caller = address!("1000000000000000000000000000000000000002"); let coinbase = address!("1000000000000000000000000000000000000003"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &caller, - AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), - ); - db.insert_account_info( - &account, - AccountInfo { - code: Some(Bytecode::new_legacy(bytes!("6001600100"))), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), + ); + db.insert_account_info( + &account, + AccountInfo { + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), + ..Default::default() + }, + ); + }); let context = context .modify_block_chained(|block| { block.number = U256::from(42); @@ -518,27 +513,23 @@ fn test_geth_jstracer_get_code_does_not_load_the_account() { let observed = address!("1000000000000000000000000000000000000002"); let caller = address!("1000000000000000000000000000000000000003"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &caller, - AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), - ); - db.insert_account_info( - &target, - AccountInfo { - code: Some(Bytecode::new_legacy(bytes!("00"))), - ..Default::default() - }, - ); - db.insert_account_info( - &observed, - AccountInfo { - code: Some(Bytecode::new_legacy(bytes!("6001600100"))), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), + ); + db.insert_account_info( + &target, + AccountInfo { code: Some(Bytecode::new_legacy(bytes!("00"))), ..Default::default() }, + ); + db.insert_account_info( + &observed, + AccountInfo { + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), + ..Default::default() + }, + ); + }); let context = context.modify_tx_chained(|tx| { tx.gas_limit = 1_000_000; tx.gas_price = 1; @@ -580,7 +571,7 @@ fn mux_runs_a_js_tracer_next_to_a_built_in_tracer() { let code = "{ steps: 0, step: function() { this.steps++; }, fault: function() {}, \ result: function() { return this.steps; } }"; let contract = Address::with_last_byte(0x42); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f00"))), @@ -606,13 +597,13 @@ fn mux_runs_a_js_tracer_next_to_a_built_in_tracer() { let GethTrace::JS(steps) = &frame.0[&js_key] else { panic!("expected a JS result") }; assert_eq!(steps.as_u64(), Some(3)); - assert!(matches!(frame.0[&call_key], GethTrace::CallTracer(_))); + assert!(frame.0[&call_key].is_call()); } #[test] fn mux_interrupts_its_js_tracers() { let code = "{ fault: function() {}, result: function() { return 1; } }"; - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info(&Address::ZERO, AccountInfo::default()); let config = MuxConfig(HashMap::from_iter([(GethDebugTracerType::JsTracer(code.into()), None)])); diff --git a/crates/inspectors/tests/it/geth_js/errors.rs b/crates/inspectors/tests/it/geth_js/errors.rs index bab91e8b4..16697b1e0 100644 --- a/crates/inspectors/tests/it/geth_js/errors.rs +++ b/crates/inspectors/tests/it/geth_js/errors.rs @@ -21,7 +21,7 @@ fn execute_with_child( child: &[u8], gas_limit: u64, ) -> (Result, ResultAndState) { - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); for (address, code, balance) in [ (Address::ZERO, &[][..], U256::from(1e18)), (TARGET, code, U256::from(5)), @@ -125,9 +125,8 @@ fn oversized_initcode_is_reported_by_step() { #[test] fn static_write_errors_follow_the_opcode_validation_stage() { - let mut parent = hex!("600060006000600073").to_vec(); - parent.extend_from_slice(CHILD.as_slice()); - parent.extend_from_slice(&hex!("61fffffa00")); + let parent = + [&hex!("600060006000600073")[..], CHILD.as_slice(), &hex!("61fffffa00")[..]].concat(); for (child, opcode, callback) in [ (&hex!("6000600055")[..], "SSTORE", "step"), (&hex!("600060006000f0")[..], "CREATE", "step"), @@ -159,9 +158,12 @@ fn static_write_errors_follow_the_opcode_validation_stage() { #[test] fn hook_errors_preserve_execution() { - let mut call = hex!("6000600060006000600073").to_vec(); - call.extend_from_slice(CHILD.as_slice()); - call.extend_from_slice(&hex!("61fffff150600160015500")); + let call = [ + &hex!("6000600060006000600073")[..], + CHILD.as_slice(), + &hex!("61fffff150600160015500")[..], + ] + .concat(); let baseline = "{fault:function(){},result:function(){return 42}}"; for hook in ["step", "enter", "exit", "fault"] { let child = if hook == "fault" { &hex!("60ff56")[..] } else { &hex!("600160005500")[..] }; @@ -182,9 +184,8 @@ fn hook_errors_preserve_execution() { #[test] fn call_step_errors_preserve_execution() { - let mut call = hex!("6000600060006000600073").to_vec(); - call.extend_from_slice(CHILD.as_slice()); - call.extend_from_slice(&hex!("61fffff100")); + let call = + [&hex!("6000600060006000600073")[..], CHILD.as_slice(), &hex!("61fffff100")[..]].concat(); let baseline = "{fault:function(){},result:function(){return 42}}"; let script = "{fault:function(){},result:function(){return 42},step:function(l){var o=l.op.toString();if(o=='CALL'||o=='CREATE')throw new Error('boom')}}"; // The step ends before the child runs, which must still run with the gas it was given. diff --git a/crates/inspectors/tests/it/geth_js/values.rs b/crates/inspectors/tests/it/geth_js/values.rs index b35d4cde2..af1a0f8e9 100644 --- a/crates/inspectors/tests/it/geth_js/values.rs +++ b/crates/inspectors/tests/it/geth_js/values.rs @@ -2,7 +2,7 @@ use super::*; -fn trace_tx(code: &str, db: CacheDB, tx: TxEnv) -> serde_json::Value { +fn trace_tx(code: &str, db: InMemoryDB, tx: TxEnv) -> serde_json::Value { let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) @@ -13,8 +13,8 @@ fn trace_tx(code: &str, db: CacheDB, tx: TxEnv) -> serde_json::Value { js_result(inspector, &res, ctx) } -fn funded_db() -> CacheDB { - let mut db = CacheDB::new(EmptyDB::default()); +fn funded_db() -> InMemoryDB { + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1e18), ..Default::default() }, diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index dee4ec804..2bde15800 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -1,10 +1,11 @@ //! Recording inputs is optional and does not affect execution. -use crate::utils::{AccountInfo, Bytecode, CacheDB, EmptyDB, SpecId, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, SpecId, TransactTo, TxEnv}; use alloy_primitives::{Address, Bytes, U256, bytes, hex}; use evm2::{ BaseEvmTypes, Evm, ExecutionConfig, Inspector, Precompiles, Version, - constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, interpreter::opcode::op, + constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, evm::InMemoryDB, + interpreter::opcode::op, }; use evm2_inspectors::{ access_list::AccessListInspector, @@ -41,7 +42,7 @@ fn inspect_with_limits( limits: TraceLimits, ) -> (TracingInspector, bool) { let target = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [(target, code), (Address::with_last_byte(0x43), &hex!("00")[..])] { db.insert_account_info( &address, @@ -264,10 +265,10 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { let code = bytes!("5f5f5f5f600160435af100"); for initial_call_depth in [0, 7, u16::MAX - CALL_DEPTH_LIMIT - 1] { for kind in [TransactTo::Call(Address::with_last_byte(0x42)), TransactTo::Create] { - let target = kind.to().copied().unwrap_or_else(|| caller.create(0)); + let target = kind.into_to().unwrap_or_else(|| caller.create(0)); let data = if kind.is_create() { code.clone() } else { Bytes::from_static(&[1, 2]) }; let run = |inspector: &mut dyn Inspector| { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo::default().with_balance(U256::from(100)), @@ -301,7 +302,7 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { data: data.clone(), ..Default::default() }; - assert!(evm.transact(&tx.envelope()).unwrap().result().status); + assert!(evm.transact(&tx.envelope()).unwrap().result().is_success()); }; let mut tracer = TracingInspector::new( TracingInspectorConfig::default_geth().set_record_inputs(true), @@ -337,7 +338,7 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { assert_eq!(transfers.transfers().len(), 1); assert_eq!(transfers.transfers()[0].from, target); assert_eq!(transfers.transfers()[0].to, child); - let mut opcodes = OpcodeGasInspector::default(); + let mut opcodes = OpcodeGasInspector::new(); run(&mut opcodes); assert!( opcodes diff --git a/crates/inspectors/tests/it/opcode.rs b/crates/inspectors/tests/it/opcode.rs index 9f1a7b963..3ae1b8f01 100644 --- a/crates/inspectors/tests/it/opcode.rs +++ b/crates/inspectors/tests/it/opcode.rs @@ -1,6 +1,9 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, Context, TransactTo, TxEnv}; use alloy_primitives::{Address, address}; -use evm2::interpreter::opcode::{OpCode, op}; +use evm2::{ + evm::InMemoryDB, + interpreter::opcode::{OpCode, op}, +}; use evm2_inspectors::opcode::OpcodeGasInspector; #[test] @@ -37,20 +40,19 @@ fn opcode_gas_records_parent_call_after_child_execution() { (vec![op::INVALID], 1, 0), (vec![0x0c], 0, 0), ] { - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &child, - AccountInfo { - code: Some(Bytecode::new_legacy(child_code.into())), - ..Default::default() - }, - ); - db.insert_account_info( - &parent, - AccountInfo { code: Some(parent_code.clone()), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &child, + AccountInfo { + code: Some(Bytecode::new_legacy(child_code.into())), + ..Default::default() + }, + ); + db.insert_account_info( + &parent, + AccountInfo { code: Some(parent_code.clone()), ..Default::default() }, + ); + }); let mut inspector = OpcodeGasInspector::new(); let mut evm = context.build_mainnet().with_inspector(&mut inspector); @@ -82,13 +84,12 @@ fn opcode_gas_excludes_uncharged_sentry_gas() { for (gas_limit, expected_cost, success) in [(23_306, 0, false), (24_000, 2_994, false), (100_000, 22_100, true)] { - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &target, - AccountInfo { code: Some(code.clone()), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &target, + AccountInfo { code: Some(code.clone()), ..Default::default() }, + ); + }); let mut inspector = OpcodeGasInspector::new(); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm @@ -131,10 +132,9 @@ fn opcode_gas_preserves_cold_call_out_of_gas() { ] .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info(&target, AccountInfo { code: Some(code), ..Default::default() }); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info(&target, AccountInfo { code: Some(code), ..Default::default() }); + }); let mut inspector = OpcodeGasInspector::new(); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm diff --git a/crates/inspectors/tests/it/otterscan.rs b/crates/inspectors/tests/it/otterscan.rs index b9d689c21..64f749dc6 100644 --- a/crates/inspectors/tests/it/otterscan.rs +++ b/crates/inspectors/tests/it/otterscan.rs @@ -1,16 +1,15 @@ //! Otterscan internal operations tests. -use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, EmptyDB, ExecutionResult, SpecId, TxEnv, -}; +use crate::utils::{AccountInfo, Bytecode, Context, ExecutionResult, SpecId, TxEnv}; use alloy_primitives::{Address, Bytes, TxKind, U256, bytes}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::otterscan::InternalOperationsInspector; const CONTRACT: Address = Address::repeat_byte(0x11); fn execute(code: Bytes, spec: SpecId, kind: TxKind) -> (ExecutionResult, Vec) { - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index 352e8d31b..c3aa9f278 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -1,10 +1,10 @@ //! Parity tests use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, - SpecId, TransactTo, TxEnv, deploy_contract, inspect_deploy_contract, op, print_traces, + AccountInfo, Bytecode, Context, DatabaseCommit, ExecutionResult, Output, SpecId, TransactTo, + TxEnv, deploy_contract, inspect_deploy_contract, op, print_traces, }; -use alloy_primitives::{Address, U256, address, b256, bytes, hex, map::HashSet}; +use alloy_primitives::{Address, B256, U256, address, b256, bytes, hex, map::HashSet}; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::{ filter::TraceFilter, @@ -13,7 +13,7 @@ use alloy_rpc_types_trace::{ TransactionTrace, VmTrace, }, }; -use evm2::utils::address_to_word; +use evm2::{evm::InMemoryDB, utils::address_to_word}; use evm2_inspectors::tracing::{ TracingInspector, TracingInspectorConfig, parity::populate_state_diff, }; @@ -46,10 +46,9 @@ fn test_parity_selfdestruct(spec_id: SpecId) { let deployer = address!("341348115259a8bf69f1f50101c227fced83bac6"); let value = U256::from(69); - let mut context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); - }); + let mut context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); + }); context.modify_tx(|tx| tx.value = value); let mut evm = context.build_mainnet(); @@ -119,7 +118,7 @@ fn test_parity_constructor_selfdestruct() { let deployer = Address::ZERO; let mut evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_tx_chained(|tx| tx.caller = deployer) .build_mainnet_with_inspector(TracingInspector::new( TracingInspectorConfig::default_parity(), @@ -180,7 +179,7 @@ fn test_parity_call_selfdestruct() { let value = U256::from(69); let mut evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); }) @@ -253,7 +252,7 @@ fn test_parity_call_selfdestruct_create() { let value = U256::ONE; let evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info( &caller, @@ -325,7 +324,7 @@ fn test_parity_statediff_blob_commit() { let caller = address!("283b5b7d75e3e6b84b8e2161e8a468d733bbbe8d"); let to = address!("15dd773dad3f630773a0e771e9b221f4c8b9b939"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(u64::MAX), ..Default::default() }, @@ -397,7 +396,7 @@ fn test_parity_delegatecall_selfdestruct() { let deployer = address!("341348115259a8bf69f1f50101c227fced83bac6"); - let mut evm = Context::mainnet().with_db(CacheDB::::default()).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy DelegateCall contract let delegate_addr = @@ -411,9 +410,8 @@ fn test_parity_delegatecall_selfdestruct() { .unwrap(); // Prepare the input data for the close(address) function call - let mut input_data = hex!("c74073a1").to_vec(); // keccak256("close(address)")[:4] - input_data.extend_from_slice(&[0u8; 12]); // Pad with zeros - input_data.extend_from_slice(target_addr.as_slice()); + // keccak256("close(address)")[:4], zero padding, then the address. + let input_data = [&hex!("c74073a1")[..], &[0u8; 12][..], target_addr.as_slice()].concat(); // Call DelegateCall contract with SelfDestructTarget address let mut evm = @@ -465,7 +463,7 @@ fn test_parity_delegatecall_selfdestruct() { assert_eq!(action2.refund_address, deployer); } -fn trace_vm_code(code: &[u8], child: &[u8]) -> alloy_rpc_types_trace::parity::VmTrace { +fn trace_vm_code(code: &[u8], child: &[u8]) -> VmTrace { let config = TracingInspectorConfig::from_parity_config(&HashSet::from_iter([TraceType::VmTrace])); inspect_code(code, child, SpecId::default(), config).into_parity_builder().vm_trace() @@ -492,7 +490,7 @@ fn inspect_funded_code( use crate::utils::Bytecode; let target = Address::with_last_byte(0x42); let child_address = Address::with_last_byte(0x43); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code, balance) in [(target, code, balance), (child_address, child, 0)] { db.insert_account_info( &address, @@ -515,7 +513,7 @@ fn vmtrace_memory_is_attached_to_the_writing_instruction() { let trace = trace_vm_code(&hex!("60016000525f00"), &[]); let mem = trace.ops[2].ex.as_ref().unwrap().mem.as_ref().unwrap(); assert_eq!(mem.off, 0); - assert_eq!(mem.data.as_ref(), U256::from(1).to_be_bytes::<32>()); + assert_eq!(mem.data.as_ref(), B256::with_last_byte(1).0); assert!(trace.ops[3].ex.as_ref().unwrap().mem.is_none()); } @@ -786,10 +784,10 @@ fn vmtrace_revert_keeps_its_execution_record() { fn vmtrace_call_pushes_result_and_copies_return_memory() { let trace = trace_vm_code(&hex!("602060205f5f5f604361fffff15f00"), &hex!("602a5f5260205ff3")); let call = &trace.ops[7]; - assert_eq!(call.ex.as_ref().unwrap().push.as_slice(), &[U256::ONE]); + assert_eq!(call.ex.as_ref().unwrap().push, &[U256::ONE]); let mem = call.ex.as_ref().unwrap().mem.as_ref().unwrap(); assert_eq!(mem.off, 32); - assert_eq!(mem.data.as_ref(), U256::from(42).to_be_bytes::<32>()); + assert_eq!(mem.data.as_ref(), B256::with_last_byte(42).0); } #[test] @@ -878,7 +876,7 @@ fn vmtrace_nested_sibling_calls_keep_their_own_subtraces() { root.extend(hex!("505f5f5f5f5f604461fffff100")); let mut branch = hex!("5f5f5f5f5f604561fffff1").to_vec(); branch.push(0x00); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (last, code) in [ (0x42, root), (0x43, hex!("60015000").to_vec()), @@ -928,7 +926,7 @@ fn vmtrace_memory_copy_and_terminal_writes() { .find_map(|op| op.ex.as_ref().and_then(|ex| ex.mem.as_ref())) .unwrap(); assert_eq!(mem.off, offset); - assert_eq!(mem.data.as_ref(), expected); + assert_eq!(mem.data, expected); } } @@ -966,7 +964,7 @@ fn frame_errors(inspector: &TracingInspector) -> (Vec, Vec) { fn reverted_create_reports_gas_and_revert_data() { let initcode = hex!("63deadbeef5f526004601cfd"); let factory = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, AccountInfo::default() @@ -1084,7 +1082,7 @@ fn failed_frames_use_parity_labels() { fn create_nonce_overflow_is_a_failed_frame() { for nonce in [u64::MAX, u64::MAX - 1] { let factory = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, AccountInfo { @@ -1120,7 +1118,7 @@ fn create_nonce_overflow_is_a_failed_frame() { #[test] fn create_nonce_overflow_runs_no_frame_since_amsterdam() { let factory = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, AccountInfo { @@ -1148,7 +1146,7 @@ fn create_nonce_overflow_runs_no_frame_since_amsterdam() { fn create_with_empty_initcode_runs_no_steps() { let factory = Address::with_last_byte(0x42); for (kind, depth_nodes) in [(TransactTo::Create, 1), (TransactTo::Call(factory), 2)] { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))), @@ -1176,7 +1174,7 @@ fn precompile_exclusion_uses_original_callee_with_delegation() { code.push(op::PUSH0); } code.extend([op::PUSH1, callee, op::GAS, opcode, op::POP, op::STOP]); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &parent, AccountInfo::default().with_code(Bytecode::new_raw(code.into())), @@ -1453,7 +1451,7 @@ fn nested_calls_keep_each_forwarded_cost() { #[test] fn parity_traces_leave_out_zero_value_precompile_calls() { let contract = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &contract, AccountInfo::default().with_balance(U256::from(1)).with_code(Bytecode::new_raw( diff --git a/crates/inspectors/tests/it/parity/state_diff_7702.rs b/crates/inspectors/tests/it/parity/state_diff_7702.rs index 363c582f2..539df7025 100644 --- a/crates/inspectors/tests/it/parity/state_diff_7702.rs +++ b/crates/inspectors/tests/it/parity/state_diff_7702.rs @@ -6,7 +6,7 @@ use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, ethereum::{LazyTxEip7702, TxEnvelope, ethereum_tx_registry}, - evm::{AccountInfo, CacheDB, EmptyDB}, + evm::{AccountInfo, InMemoryDB}, }; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; @@ -24,7 +24,7 @@ fn authorization_code_changes() { for revert in [false, true] { let authority = Address::with_last_byte(0x43); let target = Address::with_last_byte(0x44); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &authority, AccountInfo { nonce: 1, ..Default::default() }.with_code(before.clone()), @@ -64,7 +64,7 @@ fn authorization_code_changes() { ); let result = evm.transact(&tx).unwrap().detach(); drop(evm); - assert_eq!(result.result.status, !revert); + assert_eq!(result.result.is_success(), !revert); let after = if delegate.is_zero() { Bytecode::default() } else { diff --git a/crates/inspectors/tests/it/parity/state_diff_birth.rs b/crates/inspectors/tests/it/parity/state_diff_birth.rs index f7b708064..3af1c0098 100644 --- a/crates/inspectors/tests/it/parity/state_diff_birth.rs +++ b/crates/inspectors/tests/it/parity/state_diff_birth.rs @@ -1,11 +1,10 @@ -use crate::utils::{ - AccountInfo, CacheDB, Context, DatabaseCommit, EmptyDB, SpecId, TransactTo, TxEnv, -}; +use crate::utils::{AccountInfo, Context, DatabaseCommit, SpecId, TransactTo, TxEnv}; use alloy_primitives::{Address, U64, U256, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, StateDiff, TraceType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; -fn trace(db: &mut CacheDB, tx: TxEnv, beneficiary: Address, spec: SpecId) -> StateDiff { +fn trace(db: &mut InMemoryDB, tx: TxEnv, beneficiary: Address, spec: SpecId) -> StateDiff { let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) .modify_block_chained(|block| block.beneficiary = beneficiary) @@ -33,7 +32,7 @@ fn transfer_creates_account_then_changes_existing_account() { let caller = Address::with_last_byte(0x41); let recipient = Address::with_last_byte(0x42); let beneficiary = Address::with_last_byte(0x43); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, @@ -73,7 +72,7 @@ fn transfer_creates_account_then_changes_existing_account() { #[test] fn zero_value_call_does_not_create_account() { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); let recipient = Address::with_last_byte(0x42); let beneficiary = Address::with_last_byte(0x43); let diff = trace( @@ -92,7 +91,7 @@ fn creation_uses_account_existence_not_initial_balance() { let target = caller.create(0); for initial_balance in [None, Some(0), Some(9)] { for runtime in [false, true] { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); if let Some(balance) = initial_balance { db.insert_account_info( &target, @@ -139,7 +138,7 @@ fn creation_uses_account_existence_not_initial_balance() { fn frontier_materializes_empty_account() { let caller = Address::with_last_byte(0x41); let recipient = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(100_000), ..Default::default() }, diff --git a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs index d354041ac..e8200ab03 100644 --- a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs +++ b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs @@ -1,6 +1,7 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, SpecId, TransactTo, TxEnv}; use alloy_primitives::{Address, B256, U64, U256, bytes, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, TraceType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; #[test] @@ -13,7 +14,7 @@ fn existing_account_selfdestruct_across_cancun() { // then selfdestruct. let code = Bytecode::new_raw(bytes!("600054506009600155600360025560035450600060045533ff")); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { balance: U256::from(69), nonce: 1, ..Default::default() } @@ -98,7 +99,7 @@ fn created_account_selfdestruct_across_cancun() { for prefunded in [false, true] { let caller = Address::with_last_byte(0x42); let target = caller.create(0); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); if prefunded { db.insert_account_info( &target, @@ -194,7 +195,7 @@ fn deleted_account_diff_does_not_depend_on_accessed_slots() { let target = Address::with_last_byte(0x42); let diffs = [&hex!("60005450600960015533ff")[..], &hex!("33ff")[..]].map(|code| { let code = Bytecode::new_raw(code.to_vec().into()); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { balance: U256::from(69), nonce: 1, ..Default::default() }.with_code(code), diff --git a/crates/inspectors/tests/it/parity/vm_bytecode.rs b/crates/inspectors/tests/it/parity/vm_bytecode.rs index 3033de287..1f693acdf 100644 --- a/crates/inspectors/tests/it/parity/vm_bytecode.rs +++ b/crates/inspectors/tests/it/parity/vm_bytecode.rs @@ -1,4 +1,4 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, SpecId, TransactTo, TxEnv}; use alloy_consensus::{TxEip7702, transaction::Recovered}; use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, Bytes, U256, bytes, hex, map::HashSet}; @@ -6,14 +6,15 @@ use alloy_rpc_types_trace::parity::{TraceResults, TraceType}; use evm2::{ BaseEvmTypes, Evm, Precompiles, ethereum::{LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + evm::InMemoryDB, }; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; -fn trace(db: CacheDB, tx: TxEnv) -> TraceResults { +fn trace(db: InMemoryDB, tx: TxEnv) -> TraceResults { trace_envelope(db, tx.envelope()) } -fn trace_envelope(mut db: CacheDB, tx: RecoveredTxEnvelope) -> TraceResults { +fn trace_envelope(mut db: InMemoryDB, tx: RecoveredTxEnvelope) -> TraceResults { let types = HashSet::from_iter([TraceType::VmTrace]); let mut inspector = TracingInspector::new(TracingInspectorConfig::from_parity_config(&types)); let mut evm = Evm::::new( diff --git a/crates/inspectors/tests/it/repro/mod.rs b/crates/inspectors/tests/it/repro/mod.rs index b2e094201..69ec0fcf9 100644 --- a/crates/inspectors/tests/it/repro/mod.rs +++ b/crates/inspectors/tests/it/repro/mod.rs @@ -47,10 +47,11 @@ mod prestate; -use crate::utils::{AccountInfo, Bytecode, CacheDB, EmptyDB, SpecId}; +use crate::utils::{AccountInfo, Bytecode, SpecId}; use alloy_hardforks::EthereumHardfork; use alloy_primitives::Address; use alloy_rpc_types_trace::geth::AccountState; +use evm2::evm::InMemoryDB; use serde::Deserialize; use std::collections::BTreeMap; @@ -89,8 +90,8 @@ pub const fn spec_id_from_block(block_number: u64) -> SpecId { } /// Build a CacheDB from prestate AccountState map. -pub fn build_db_from_prestate(prestate: &BTreeMap) -> CacheDB { - let mut db = CacheDB::new(EmptyDB::default()); +pub fn build_db_from_prestate(prestate: &BTreeMap) -> InMemoryDB { + let mut db = InMemoryDB::default(); for (addr, state) in prestate { let balance = state.balance.unwrap_or_default(); @@ -148,7 +149,7 @@ pub struct ReproContext { /// The EVM spec to use for execution. pub spec_id: SpecId, /// The database populated with prestate. - pub db: CacheDB, + pub db: InMemoryDB, } impl ReproContext { diff --git a/crates/inspectors/tests/it/stack.rs b/crates/inspectors/tests/it/stack.rs index 7a00d520a..e4acb513c 100644 --- a/crates/inspectors/tests/it/stack.rs +++ b/crates/inspectors/tests/it/stack.rs @@ -1,7 +1,8 @@ //! Stack snapshot tests -use crate::utils::{CacheDB, Context, EmptyDB, SpecId, inspect_deploy_contract}; +use crate::utils::{Context, SpecId, inspect_deploy_contract}; use alloy_primitives::{Address, U256, bytes}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{StackSnapshotType, TracingInspector, TracingInspectorConfig}; /// Runs `PUSH1 1; PUSH1 2; PUSH1 3; STOP` as create init code and returns the recorded stack @@ -11,10 +12,8 @@ fn stack_snapshots(snapshot_type: StackSnapshotType) -> Vec>> TracingInspectorConfig::none().steps().set_stack_snapshots(snapshot_type), ); - let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) - .build_mainnet() - .with_inspector(&mut insp); + let mut evm = + Context::mainnet().with_db(InMemoryDB::default()).build_mainnet().with_inspector(&mut insp); let res = inspect_deploy_contract(&mut evm, bytes!("60016002600300"), Address::ZERO, SpecId::CANCUN); diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index 2f88b0247..241c9108f 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -1,10 +1,11 @@ //! Transfer tests use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, - SpecId, TestDbExt, TransactTo, TxEnv, a, + AccountInfo, Bytecode, Context, DatabaseCommit, ExecutionResult, Output, SpecId, TestDbExt, + TransactTo, TxEnv, a, }; use alloy_primitives::{Address, B256, Log, U256, address, bytes, hex}; +use evm2::evm::InMemoryDB; use evm2_inspectors::{ tracing::{TracingInspector, TracingInspectorConfig}, transfer::{ @@ -29,7 +30,7 @@ fn test_internal_transfers() { ); let deployer = Address::ZERO; - let db = CacheDB::new(EmptyDB::default()); + let db = InMemoryDB::default(); let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::LONDON); @@ -112,7 +113,7 @@ fn test_internal_transfers() { #[test] fn discards_failed_create_transfer() { let deployer = Address::ZERO; - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::LONDON); @@ -148,7 +149,7 @@ fn discards_failed_call_transfers_and_logs() { "6000600060006000600260ff5af150" "00" ); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &caller, @@ -207,7 +208,7 @@ fn discards_failed_internal_create_transfer_and_log() { // Creates a contract with 1 wei and init code `INVALID`. let factory_code = hex!("60fe600053600160006001f05000"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &factory, @@ -248,7 +249,7 @@ fn discards_internal_create_transfer_on_nonce_overflow() { // Creates a contract with 1 wei from a factory whose nonce cannot be bumped. let factory_code = hex!("60fe600053600160006001f05000"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &factory, @@ -328,7 +329,7 @@ struct TransferCase { fn simulate_transfer_logs_match_geth() { let sender = a(0x1001); for case in CASES { - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(sender).info.balance = U256::from(10u128.pow(18)); for &(address, code, balance, nonce) in case.accounts { db.insert_account_info( @@ -387,7 +388,7 @@ fn simulate_transfer_logs_match_geth() { fn simulate_transfer_logs_use_eip7708_logs() { let sender = a(0x1001); let contract = a(0xc6); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(sender).info.balance = U256::from(10u128.pow(18)); db.insert_account_info( &contract, diff --git a/crates/inspectors/tests/it/writer.rs b/crates/inspectors/tests/it/writer.rs index d4a411996..5bcdafeba 100644 --- a/crates/inspectors/tests/it/writer.rs +++ b/crates/inspectors/tests/it/writer.rs @@ -1,10 +1,11 @@ use crate::utils::{ - AccountInfo, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv, inspect_deploy_contract, - write_traces, write_traces_with, + AccountInfo, Context, SpecId, TransactTo, TxEnv, inspect_deploy_contract, write_traces, + write_traces_with, }; use alloy_primitives::{Address, B256, U256, address, b256, bytes, hex}; use alloy_sol_types::{SolCall, sol}; use colorchoice::ColorChoice; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{ TraceWriterConfig, TracingInspector, TracingInspectorConfig, types::{DecodedCallData, DecodedInternalCall, DecodedTraceStep}, @@ -23,7 +24,7 @@ fn test_trace_printing() { let base_path = &Path::new(OUT_DIR).join("test_trace_printing"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); //let address = evm.deploy(CREATION_CODE.parse().unwrap(), &mut tracer).unwrap(); @@ -92,7 +93,7 @@ fn deploy_fail() { let base_path = &Path::new(OUT_DIR).join("deploy_fail"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); inspect_deploy_contract( @@ -313,7 +314,7 @@ fn test_receive_vs_fallback_empty_calldata() { ); let caller = Address::with_last_byte(1); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, @@ -366,7 +367,7 @@ fn test_fallback_short_calldata() { ); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); let address = inspect_deploy_contract(&mut evm, initcode, Address::default(), SpecId::CANCUN) diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 12a8c0bde..664a69fb5 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -210,7 +210,7 @@ pub unsafe fn __revmc_builtin_calldataload( ecx: &EvmContext<'_, '_, '_, T>, offset_ptr: &mut EvmWord, ) { - do_calldataload(ecx, offset_ptr, word_to_usize_saturated(offset_ptr.to_u256())); + do_calldataload(ecx, offset_ptr, offset_ptr.to_u256().saturating_to::()); } #[inline] @@ -241,7 +241,7 @@ pub unsafe fn __revmc_builtin_calldatacopy( ecx.gas.spend(ecx.gas_params().copy_cost(len))?; let memory_offset = word_to_usize(memory_offset.to_u256())?; ensure_memory(ecx, memory_offset, len)?; - let data_offset = word_to_usize_saturated(data_offset.to_u256()); + let data_offset = data_offset.to_u256().saturating_to::(); let input = ecx.input().as_ref(); let input = unsafe { core::slice::from_raw_parts(input.as_ptr(), input.len()) }; ecx.memory_mut().set_data(memory_offset, data_offset, len, input); @@ -287,7 +287,7 @@ pub unsafe fn __revmc_builtin_extcodecopy( let code = account.code.original_bytes(); let code_offset_usize = - core::cmp::min(word_to_usize_saturated(code_offset.to_u256()), code.len()); + core::cmp::min(code_offset.to_u256().saturating_to::(), code.len()); ecx.memory_mut().set_data(memory_offset_usize, code_offset_usize, len, &code); Ok(()) } @@ -299,7 +299,7 @@ pub unsafe fn __revmc_builtin_returndatacopy( ) -> BuiltinResult { let rev![memory_offset, offset, len] = sp; let len = word_to_usize(len.to_u256())?; - let data_offset = word_to_usize_saturated(offset.to_u256()); + let data_offset = offset.to_u256().saturating_to::(); // Bounds check before charging gas. let data_end = data_offset.saturating_add(len); @@ -344,7 +344,7 @@ pub unsafe fn __revmc_builtin_blockhash( return Ok(()); }; - let diff = word_to_u64_saturated(diff); + let diff = diff.saturating_to::(); // Current block returns 0 if diff == 0 { @@ -398,7 +398,7 @@ pub unsafe fn __revmc_builtin_blob_hash( index_ptr: &mut EvmWord, ) { let index = index_ptr.to_u256(); - let index_usize = word_to_usize_saturated(index); + let index_usize = index.saturating_to::(); *index_ptr = ecx.tx_env().blob_hashes.get(index_usize).copied().unwrap_or_default().into(); } @@ -525,7 +525,7 @@ pub unsafe fn __revmc_builtin_host_instruction( ecx.interpreter_mut().set_gas(gas); let result = unsafe { ecx.interpreter_mut().run_external_instruction(pc, len) }; ecx.gas = ecx.interpreter().gas(); - ecx.return_data_len = ecx.interpreter().return_data().len(); + ecx.return_data_len = ecx.return_data().len(); ecx.refresh_memory_cache(); let new_len = result?; // Compiled code relies on the opcode's stack effect, and on memory never shrinking. @@ -777,7 +777,7 @@ pub unsafe fn __revmc_builtin_call( let local_gas_limit = local_gas_limit.to_u256(); let to = to.to_address(); - let local_gas_limit = word_to_u64_saturated(local_gas_limit); + let local_gas_limit = local_gas_limit.saturating_to::(); let value = match call_kind { CallKind::Call | CallKind::CallCode => { diff --git a/crates/jit/builtins/src/utils.rs b/crates/jit/builtins/src/utils.rs index 90ba7aece..b74cadedc 100644 --- a/crates/jit/builtins/src/utils.rs +++ b/crates/jit/builtins/src/utils.rs @@ -36,11 +36,6 @@ pub(crate) fn require_non_staticcall( Ok(()) } -#[inline] -pub(crate) fn word_to_u64_saturated(value: U256) -> u64 { - value.saturating_to::() -} - // JIT code writes stack words with 8-byte stores, so read them back with scalar loads. The // interpreter's helpers use one 32-byte load, which can't take its data from those stores. @@ -53,12 +48,6 @@ pub(crate) fn word_to_usize(value: U256) -> Result { }) } -/// Converts a word to `usize`, saturating on overflow. -#[inline] -pub(crate) fn word_to_usize_saturated(value: U256) -> usize { - value.saturating_to::() -} - /// Loads an account, handling cold load gas accounting. /// /// Pre-Berlin, `cold_account_additional_cost` is 0, so the cold load logic is a no-op. @@ -113,7 +102,7 @@ pub(crate) unsafe fn copy_operation( ecx.gas.spend(ecx.gas_params().copy_cost(len))?; let memory_offset = word_to_usize(memory_offset.to_u256())?; ensure_memory(ecx, memory_offset, len)?; - let data_offset = word_to_usize_saturated(data_offset.to_u256()); + let data_offset = data_offset.to_u256().saturating_to::(); ecx.memory_mut().set_data(memory_offset, data_offset, len, data); } Ok(()) diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index 66b0db1cb..929365567 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -107,7 +107,7 @@ impl U256Imm { if self.0 & Self::INLINE_TAG != 0 { U256::from(self.0 & !Self::INLINE_TAG) } else { - *interner.get(U256Idx::from_usize(self.0 as usize)) + *interner.get(U256Idx::new(self.0 as usize)) } } @@ -1185,7 +1185,7 @@ impl InstData { #[inline] pub(crate) fn static_jump_target(&self) -> Inst { debug_assert!(self.is_static_jump()); - Inst::from_usize(self.data as usize) + Inst::new(self.data as usize) } /// Sets the static jump target. Caller must also set [`InstFlags::STATIC_JUMP`]. @@ -1361,7 +1361,7 @@ mod tests { // EVM spec: missing bytes are zero, so the value is 0x4200. let code = [op::PUSH2, 0x42]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), Some([0x42].as_slice())); assert_eq!(bc.get_push_value(data), U256::from(0x4200)); @@ -1369,7 +1369,7 @@ mod tests { // PUSH3 followed by two bytes — truncated at EOF. let code = [op::PUSH3, 0xAB, 0xCD]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), Some([0xAB, 0xCD].as_slice())); assert_eq!(bc.get_push_value(data), U256::from(0xABCD00)); @@ -1377,7 +1377,7 @@ mod tests { // PUSH1 with no immediate bytes at all. let code = [op::PUSH1]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), None); assert_eq!(bc.get_push_value(data), U256::ZERO); @@ -1385,7 +1385,7 @@ mod tests { // Non-truncated PUSH2 — full immediate. let code = [op::PUSH2, 0x42, 0xFF]; let bc = Bytecode::test(&code); - let data = bc.inst(Inst::from_usize(0)); + let data = bc.inst(Inst::new(0)); assert_eq!(bc.get_push_value(data), U256::from(0x42FF)); } @@ -1399,9 +1399,9 @@ mod tests { op::CALLDATALOAD, op::JUMP, ]); - let jumpi = Inst::from_usize(2); + let jumpi = Inst::new(2); let fallthrough = jumpi + 1; - let dynamic_jump = Inst::from_usize(5); + let dynamic_jump = Inst::new(5); bc.inst_mut(jumpi).flags |= InstFlags::STATIC_JUMP | InstFlags::CONST_JUMP_CONDITION; bc.inst_mut(jumpi).set_static_jump_target(fallthrough); @@ -1409,7 +1409,7 @@ mod tests { bc.mark_dead_code(); assert!(!bc.inst(fallthrough).is_dead_code()); - assert!(bc.inst(Inst::from_usize(4)).is_dead_code()); + assert!(bc.inst(Inst::new(4)).is_dead_code()); assert!(bc.inst(dynamic_jump).is_dead_code()); assert!(!bc.has_dynamic_jumps); } @@ -1430,10 +1430,10 @@ mod tests { op::JUMPDEST, op::STOP, ]); - let jumpi = Inst::from_usize(2); - let dead_jump = Inst::from_usize(4); - let live_target = Inst::from_usize(5); - let stale_target = Inst::from_usize(7); + let jumpi = Inst::new(2); + let dead_jump = Inst::new(4); + let live_target = Inst::new(5); + let stale_target = Inst::new(7); bc.inst_mut(jumpi).flags |= InstFlags::STATIC_JUMP | InstFlags::CONST_JUMP_CONDITION; bc.inst_mut(jumpi).set_static_jump_target(live_target); diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index 6617fbd6d..aab805033 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -766,7 +766,7 @@ impl BlockData { pub(crate) fn insts( &self, ) -> impl DoubleEndedIterator + ExactSizeIterator + use<> { - (self.insts.start.index()..self.insts.end.index()).map(Inst::from_usize) + (self.insts.start.index()..self.insts.end.index()).map(Inst::new) } } @@ -1067,12 +1067,12 @@ impl Bytecode<'_> { let finish_block = |cfg: &mut Cfg, start: usize, end: usize| { debug_assert!(start < end, "empty block range: {start}..{end}"); let bid = cfg.blocks.push(BlockData { - insts: Inst::from_usize(start)..Inst::from_usize(end), + insts: Inst::new(start)..Inst::new(end), preds: SmallVec::new(), succs: SmallVec::new(), }); for i in start..end { - cfg.inst_to_block[Inst::from_usize(i)] = Some(bid); + cfg.inst_to_block[Inst::new(i)] = Some(bid); } }; @@ -1240,7 +1240,7 @@ impl Bytecode<'_> { } else { // Blocks the fixpoint never reached are unreachable, even when the static jumps of // other such blocks, such as a loop's back edge, target them. - for bid in reached.iter_zeros().map(Block::from_usize) { + for bid in reached.iter_zeros().map(Block::new) { for i in self.cfg.blocks[bid].insts() { self.insts[i].flags |= InstFlags::DEAD_CODE; } @@ -1532,7 +1532,7 @@ impl Bytecode<'_> { // pass a stack. let no_mem = MemWords::default(); let entry_mem = MemWords::entry(self.stack_observed()); - let entry = Block::from_usize(0); + let entry = Block::new(0); parts.join(entry, 0, &[], self.stack_observed(), &[], &entry_mem, &mut const_sets, &budget); // Discovered dynamic-jump target edges per block. @@ -2428,11 +2428,11 @@ pub(crate) mod tests { ", ); // At inst 2 (ADD), operand 0 (TOS) = 0x01, operand 1 = 0x42. - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::ONE)); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(2), 1), Some(U256::from(0x42))); // At inst 4 (MSTORE), operand 0 (TOS) = 0x00, operand 1 = 0x43 (folded ADD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 1), Some(U256::from(0x43))); + assert_eq!(bytecode.const_operand(Inst::new(4), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(4), 1), Some(U256::from(0x43))); } #[test] @@ -2448,8 +2448,8 @@ pub(crate) mod tests { ", ); // At inst 3 (MSTORE), operand 0 (TOS) = 0x00, operand 1 = unknown (CALLDATALOAD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(3), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(3), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(3), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(3), 1), None); } #[test] @@ -2470,12 +2470,12 @@ pub(crate) mod tests { STOP ", ); - assert_eq!(bytecode.const_output(Inst::from_usize(0)), Some(U256::from(0x42))); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), Some(U256::ONE)); - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); - assert_eq!(bytecode.const_output(Inst::from_usize(3)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(0)), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(1)), Some(U256::ONE)); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0x43))); + assert_eq!(bytecode.const_output(Inst::new(3)), Some(U256::ZERO)); // MSTORE has no outputs. - assert_eq!(bytecode.const_output(Inst::from_usize(4)), None); + assert_eq!(bytecode.const_output(Inst::new(4)), None); } #[test] @@ -2488,8 +2488,8 @@ pub(crate) mod tests { STOP ", ); - assert_eq!(bytecode.const_output(Inst::from_usize(0)), Some(U256::ZERO)); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), None); + assert_eq!(bytecode.const_output(Inst::new(0)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(1)), None); } /// DUPN, SWAPN, and EXCHANGE should propagate constants like their fixed counterparts. @@ -2503,9 +2503,9 @@ pub(crate) mod tests { code.push(op::STOP); // inst 18 let bytecode = analyze_code_spec(code, SpecId::AMSTERDAM); // DUPN duplicates the 17th item (bottom PUSH0 = 0x00). - assert_eq!(bytecode.const_output(Inst::from_usize(17)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(17)), Some(U256::ZERO)); // The PUSH1 0xAA is still there. - assert_eq!(bytecode.const_output(Inst::from_usize(16)), Some(U256::from(0xAA))); + assert_eq!(bytecode.const_output(Inst::new(16)), Some(U256::from(0xAA))); } #[test] @@ -2536,26 +2536,26 @@ pub(crate) mod tests { ); // The JUMP at inst 5 should remain dynamic. assert!(bytecode.has_dynamic_jumps, "expected unresolved dynamic jump"); - let jump = bytecode.inst(Inst::from_usize(5)); + let jump = bytecode.inst(Inst::new(5)); assert!(jump.is_jump()); assert!(!jump.flags.contains(InstFlags::STATIC_JUMP)); // JUMP's TOS operand is Top (CALLDATALOAD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(5), 0), None); // ADD 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::ONE)); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(2), 1), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0x43))); // ADD 2 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::ONE)); - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(8)), None); + assert_eq!(bytecode.const_operand(Inst::new(8), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(8), 1), None); + assert_eq!(bytecode.const_output(Inst::new(8)), None); // SUB - assert_eq!(bytecode.const_operand(Inst::from_usize(12), 0), Some(U256::from(0x02))); - assert_eq!(bytecode.const_operand(Inst::from_usize(12), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(12)), None); + assert_eq!(bytecode.const_operand(Inst::new(12), 0), Some(U256::from(0x02))); + assert_eq!(bytecode.const_operand(Inst::new(12), 1), None); + assert_eq!(bytecode.const_output(Inst::new(12)), None); } #[test] @@ -2867,8 +2867,7 @@ pub(crate) mod tests { }) .and_then(|to| to.as_str()) .unwrap(); - let code = case["pre"][to]["code"].as_str().unwrap().trim_start_matches("0x"); - hex::decode(code).unwrap() + hex::decode(case["pre"][to]["code"].as_str().unwrap()).unwrap() } } @@ -2977,10 +2976,10 @@ mod tests_edge_cases { ", ); // DUP2 output should be 0xAA. - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0xAA))); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0xAA))); // At MSTORE (inst 5): TOS = 0x00, second = 0xBB (swapped back). - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 1), Some(U256::from(0xBB))); + assert_eq!(bytecode.const_operand(Inst::new(5), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(5), 1), Some(U256::from(0xBB))); } /// JUMPI with a constant condition should still record both operands. @@ -3090,7 +3089,7 @@ mod tests_edge_cases { let (_, jump) = bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); assert!(jump.flags.contains(InstFlags::STATIC_JUMP)); assert!(!jump.flags.contains(InstFlags::INVALID_JUMP)); - assert_eq!(jump.static_jump_target(), Inst::from_usize(3)); + assert_eq!(jump.static_jump_target(), Inst::new(3)); assert!(!bytecode.has_dynamic_jumps); } @@ -3111,7 +3110,7 @@ mod tests_edge_cases { let (_, jump) = bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); assert!(jump.flags.contains(InstFlags::STATIC_JUMP)); assert!(!jump.flags.contains(InstFlags::INVALID_JUMP)); - assert_eq!(jump.static_jump_target(), Inst::from_usize(3)); + assert_eq!(jump.static_jump_target(), Inst::new(3)); assert!(!bytecode.has_dynamic_jumps); } @@ -3686,10 +3685,10 @@ mod tests_edge_cases { ); // DUP1 at inst 2: counter is loop-variant. - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), None); // ADD at inst 9: TOS=1 (const), second=counter (Top). - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ONE)); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); } /// Loop counter incremented via single-caller subroutine must be Top. @@ -3749,7 +3748,7 @@ mod tests_edge_cases { ", ); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), None); } /// Loop counter incremented via multi-caller subroutine must be Top. @@ -3821,7 +3820,7 @@ mod tests_edge_cases { ); // DUP1 at inst 11: counter is loop-variant. - assert_eq!(bytecode.const_operand(Inst::from_usize(11), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(11), 0), None); } /// loopExp statetest: loop counters through checked_add subroutine must be Top. @@ -3870,10 +3869,10 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Block-local constant survives. - assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(8)), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ZERO)); // Cross-block value must be restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); } /// Same as above but asserting const_output: block-local PUSH output survives, @@ -3900,11 +3899,11 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Purely local push output preserved. - assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::ONE)); + assert_eq!(bytecode.const_output(Inst::new(8)), Some(U256::ONE)); // ADD: local operand preserved, inherited operand restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ONE)); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(9)), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); + assert_eq!(bytecode.const_output(Inst::new(9)), None); } /// Fallthrough block after a JUMPI in a suspect JUMPDEST block becomes @@ -3939,10 +3938,10 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Block-local constant in propagated-suspect fallthrough survives. - assert_eq!(bytecode.const_output(Inst::from_usize(13)), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(14), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(13)), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(14), 0), Some(U256::ZERO)); // Cross-block inherited value restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(14), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(14), 1), None); } /// Non-suspect blocks must retain their fixpoint-derived snapshots even when @@ -3982,16 +3981,16 @@ mod tests_edge_cases { assert!(bytecode.has_dynamic_jumps); // Fallthrough block: block-local constants survive. // ADD - assert_eq!(bytecode.const_operand(Inst::from_usize(6), 0), Some(U256::from(0x0B))); - assert_eq!(bytecode.const_operand(Inst::from_usize(6), 1), Some(U256::from(0x0A))); - assert_eq!(bytecode.const_output(Inst::from_usize(6)), Some(U256::from(0x15))); + assert_eq!(bytecode.const_operand(Inst::new(6), 0), Some(U256::from(0x0B))); + assert_eq!(bytecode.const_operand(Inst::new(6), 1), Some(U256::from(0x0A))); + assert_eq!(bytecode.const_output(Inst::new(6)), Some(U256::from(0x15))); // MSTORE 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), Some(U256::from(0x15))); + assert_eq!(bytecode.const_operand(Inst::new(8), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(8), 1), Some(U256::from(0x15))); // MLOAD: single predecessor (no JUMPDEST), so cross-block value resolves. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(0x69))); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::from(0x69))); // Target block: block-local PUSH survives. - assert_eq!(bytecode.const_output(Inst::from_usize(18)), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(18)), Some(U256::from(0x42))); } /// JUMPI in a suspect JUMPDEST block: the fallthrough block inherits @@ -4036,9 +4035,9 @@ mod tests_edge_cases { assert!(bytecode.has_dynamic_jumps); // ADD at inst 18: TOS (depth 0) = 0xBB (block-local), depth 1 = Top // (incoming from suspect predecessor, block-local pass saw it as Top). - assert_eq!(bytecode.const_operand(Inst::from_usize(18), 0), Some(U256::from(0xBB))); - assert_eq!(bytecode.const_operand(Inst::from_usize(18), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(18)), None); + assert_eq!(bytecode.const_operand(Inst::new(18), 0), Some(U256::from(0xBB))); + assert_eq!(bytecode.const_operand(Inst::new(18), 1), None); + assert_eq!(bytecode.const_output(Inst::new(18)), None); } /// Bytecode that is just STOP — minimal edge case. @@ -4249,11 +4248,11 @@ mod tests_edge_cases { ", ); assert!(!bytecode.has_dynamic_jumps); - assert_eq!(bytecode.const_operand(Inst::from_usize(16), 1), Some(U256::from(7))); - assert_eq!(bytecode.const_operand(Inst::from_usize(16), 0), None); - assert_eq!(bytecode.const_output(Inst::from_usize(16)), None); - let targets = bytecode.multi_jump_targets(Inst::from_usize(18)).unwrap(); - assert_eq!(targets, [Inst::from_usize(5), Inst::from_usize(12)]); + assert_eq!(bytecode.const_operand(Inst::new(16), 1), Some(U256::from(7))); + assert_eq!(bytecode.const_operand(Inst::new(16), 0), None); + assert_eq!(bytecode.const_output(Inst::new(16)), None); + let targets = bytecode.multi_jump_targets(Inst::new(18)).unwrap(); + assert_eq!(targets, [Inst::new(5), Inst::new(12)]); } /// A recursive function's return must resolve to all of its return sites. @@ -4399,7 +4398,7 @@ mod tests_edge_cases { // Memory that was never written is not assumed to be zero. assert_eq!(mload_0x40_after("PUSH 0x60 MLOAD POP"), known); let bytecode = analyze_asm("PUSH 0x60 MLOAD PUSH0 MSTORE STOP"); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), None); + assert_eq!(bytecode.const_output(Inst::new(1)), None); } #[test] diff --git a/crates/jit/codegen/src/bytecode/passes/const_fold.rs b/crates/jit/codegen/src/bytecode/passes/const_fold.rs index cba7c85d0..f74e12327 100644 --- a/crates/jit/codegen/src/bytecode/passes/const_fold.rs +++ b/crates/jit/codegen/src/bytecode/passes/const_fold.rs @@ -495,7 +495,7 @@ mod tests { let bytecode = analyze_asm(&src); // The folded result is operand 1 (second from top) at MSTORE. - bytecode.const_operand(Inst::from_usize(mstore_inst), 1) + bytecode.const_operand(Inst::new(mstore_inst), 1) } #[test] @@ -808,7 +808,7 @@ mod tests { assert_eq!(bytecode.compiler_gas_used, 0); // inst layout: PUSH(0), PUSH(1), ADD(2), PUSH0(3), MSTORE(4), STOP(5). // The ADD result should NOT be folded — operand 1 at MSTORE should be None. - assert!(bytecode.const_operand(Inst::from_usize(4), 1).is_none()); + assert!(bytecode.const_operand(Inst::new(4), 1).is_none()); } /// Verify that a very large gas limit allows all folding to proceed. @@ -824,7 +824,7 @@ mod tests { assert!(bytecode.compiler_gas_used > 0); // inst layout: PUSH(0), PUSH(1), ADD(2), PUSH0(3), MSTORE(4), STOP(5). - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 1), Some(U256::from(5))); + assert_eq!(bytecode.const_operand(Inst::new(4), 1), Some(U256::from(5))); } /// Exhaustively verify that `const_fold_gas` and `try_const_fold` agree on which opcodes diff --git a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs index 6eeec18b5..510df28eb 100644 --- a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs +++ b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs @@ -409,7 +409,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped (dead store)" ); } @@ -427,7 +427,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 3"), (1, "PUSH 4"), (2, "ADD")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -447,11 +447,11 @@ mod tests { ); // PUSH 1 feeds MSTORE's value input; known constant, so codegen pre-writes it. assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH 1 should be skipped (const input)" ); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "PUSH 2 should be skipped" ); } @@ -473,11 +473,11 @@ mod tests { ); // A is swapped to TOS then popped → dead. B is consumed by RET_WORD → live. assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD A should be skipped (dead after swap+pop)" ); assert!( - !bytecode.inst(Inst::from_usize(3)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(3)).flags.contains(InstFlags::NOOP), "CALLDATALOAD B should NOT be skipped (consumed by RET_WORD)" ); } @@ -496,7 +496,7 @@ mod tests { ); for i in 0..4 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "inst at {i} should be skipped (exit stack dead)" ); } @@ -517,7 +517,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped (const input)" ); } @@ -536,7 +536,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped when both DUP copy and source are dead" ); } @@ -552,7 +552,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "ADDRESS should be skipped when dead" ); } @@ -567,7 +567,7 @@ mod tests { STOP ", ); - assert!(!bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP)); + assert!(!bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP)); } #[test] @@ -576,7 +576,7 @@ mod tests { // CDL at bottom, 17 × PUSH0 above → SWAP 17 swaps TOS with CDL. let bytecode = with_dynamic_and_prefix(17, "SWAP 17 RET_WORD"); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped (consumed via SWAPN + MSTORE)" ); } @@ -587,7 +587,7 @@ mod tests { // CDL at bottom, 16 × PUSH0 above → DUP 17 copies CDL to TOS. let bytecode = with_dynamic_and_prefix(16, "DUP 17 RET_WORD"); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -598,11 +598,11 @@ mod tests { // remains on the stack and feeds RET_WORD. let bytecode = analyze_asm("PUSH0 CALLDATALOAD DUP1 POP RET_WORD"); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "DUP1 should be skipped (copy is dead)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -618,7 +618,7 @@ mod tests { SpecId::AMSTERDAM, ); assert!( - !bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped (consumed via EXCHANGE + SWAP + MSTORE)" ); } @@ -635,8 +635,8 @@ mod tests { STOP ", ); - assert!(bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP)); - assert!(bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP)); } #[test] @@ -654,7 +654,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 1"), (1, "PUSH 2"), (2, "SWAP1")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -687,7 +687,7 @@ mod tests { .enumerate() { assert_eq!( - !bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), alive, "inst {i}" ); @@ -704,7 +704,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "RETURNDATASIZE should be skipped when dead" ); } @@ -721,7 +721,7 @@ mod tests { ", ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "RETURNDATASIZE should NOT be skipped when live" ); } @@ -738,11 +738,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH0 should be skipped" ); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "BLOBHASH should be skipped when dead" ); } @@ -760,7 +760,7 @@ mod tests { ", ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "BLOBHASH should NOT be skipped when live" ); } @@ -772,12 +772,12 @@ mod tests { let bytecode = with_prefix(17, &format!("DUP 17 {pops} STOP")); for i in 0..17 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(17)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(17)).flags.contains(InstFlags::NOOP), "DUPN should be skipped" ); } @@ -792,7 +792,7 @@ mod tests { SpecId::AMSTERDAM, ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "DUPN with invalid immediate should NOT be skipped" ); } @@ -804,7 +804,7 @@ mod tests { // perspective the immediate is valid and the instruction is eliminable. let bytecode = with_prefix(1, "DUP 17 POP POP STOP"); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "DUPN with valid immediate should be skipped when dead" ); } @@ -816,12 +816,12 @@ mod tests { let bytecode = with_prefix(18, &format!("SWAP 17 {pops} STOP")); for i in 0..18 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(18)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(18)).flags.contains(InstFlags::NOOP), "SWAPN should be skipped" ); } @@ -832,12 +832,12 @@ mod tests { let bytecode = with_prefix(3, "EXCHANGE 1 2 POP POP POP STOP"); for i in 0..3 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(3)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(3)).flags.contains(InstFlags::NOOP), "EXCHANGE should be skipped" ); } @@ -876,10 +876,10 @@ mod tests { ", ); for dead in [0, 1, 2, 3] { - assert!(bytecode.inst(Inst::from_usize(dead)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(dead)).flags.contains(InstFlags::NOOP)); } for not_dead in [4, 5] { - assert!(!bytecode.inst(Inst::from_usize(not_dead)).flags.contains(InstFlags::NOOP)); + assert!(!bytecode.inst(Inst::new(not_dead)).flags.contains(InstFlags::NOOP)); } } @@ -901,7 +901,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 2"), (1, "PUSH 3"), (2, "ADD"), (3, "PUSH 4")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -922,11 +922,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH 1 (addend) should be skipped (const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -946,11 +946,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH0 (addend) should be skipped (const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -970,11 +970,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH0 (deeper const input) should be skipped" ); assert!( - !bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -996,11 +996,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH 2 (divisor) should be skipped (builtin const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -1019,7 +1019,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH 0 should be skipped (const input)" ); } diff --git a/crates/jit/codegen/src/bytecode/passes/memory_sections.rs b/crates/jit/codegen/src/bytecode/passes/memory_sections.rs index dc25a8872..358e95999 100644 --- a/crates/jit/codegen/src/bytecode/passes/memory_sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/memory_sections.rs @@ -186,7 +186,7 @@ impl MemorySectionAnalysis { } fn compute_block_entry_states(&mut self, bytecode: &Bytecode<'_>) { - let entry = Block::from_usize(0); + let entry = Block::new(0); self.block_entry_states[entry] = BlockMemoryState { known_size: Some(0), exact_known_size: Some(0) }; diff --git a/crates/jit/codegen/src/bytecode/passes/sections.rs b/crates/jit/codegen/src/bytecode/passes/sections.rs index 64d55db3d..c5f021e92 100644 --- a/crates/jit/codegen/src/bytecode/passes/sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/sections.rs @@ -89,7 +89,7 @@ pub(crate) struct GasSectionAnalysis { impl Default for GasSectionAnalysis { fn default() -> Self { - Self { gas_cost: 0, start_inst: Inst::from_usize(0), gas_insts: SmallVec::new() } + Self { gas_cost: 0, start_inst: Inst::new(0), gas_insts: SmallVec::new() } } } @@ -162,7 +162,7 @@ pub(crate) struct StackSectionAnalysis { impl Default for StackSectionAnalysis { fn default() -> Self { - Self { inputs: 0, diff: 0, max_growth: 0, start_inst: Inst::from_usize(0) } + Self { inputs: 0, diff: 0, max_growth: 0, start_inst: Inst::new(0) } } } @@ -341,7 +341,7 @@ mod tests { /// Returns the gas section cost for the first non-dead instruction (the section head). fn section_gas(src: &str) -> u32 { let bytecode = analyze_asm(src); - bytecode.inst(Inst::from_usize(0)).gas_section.gas_cost + bytecode.inst(Inst::new(0)).gas_section.gas_cost } /// EXP with an asymmetric base/exponent: base is 1 byte (2), exponent is 2 bytes (256). @@ -401,10 +401,10 @@ mod tests { ], SpecId::CANCUN, ); - let head = bytecode.inst(Inst::from_usize(0)); - let gas = bytecode.inst(Inst::from_usize(7)); - let call = bytecode.inst(Inst::from_usize(8)); - let after_call = bytecode.inst(Inst::from_usize(9)); + let head = bytecode.inst(Inst::new(0)); + let gas = bytecode.inst(Inst::new(7)); + let call = bytecode.inst(Inst::new(8)); + let after_call = bytecode.inst(Inst::new(9)); // `GAS` does not end the section: 4 * PUSH1 + CALLVALUE + PUSH1 + CALLDATALOAD + GAS + // CALL. assert_eq!(head.gas_section.gas_cost, 4 * 3 + 2 + 3 + 3 + 2 + 100); @@ -422,7 +422,7 @@ mod tests { "PUSH0 GAS PUSH 256 PUSH 2 EXP GAS POP SLOAD PUSH0 STOP", SpecId::CANCUN, ); - let head = bytecode.inst(Inst::from_usize(0)); + let head = bytecode.inst(Inst::new(0)); assert_eq!(head.gas_section.gas_cost, 2 + 2 + 3 + 3 + 110 + 2 + 2 + 100); let owed = bytecode .iter_insts() @@ -468,7 +468,7 @@ mod tests { fn disabled_opcode_does_not_poison_section() { // CALLDATASIZE(0→1) ; TSTORE(2→0, disabled before Cancun) let bytecode = analyze_asm_spec("CALLDATASIZE TSTORE", SpecId::SHANGHAI); - let head = bytecode.inst(Inst::from_usize(0)); + let head = bytecode.inst(Inst::new(0)); assert_eq!( head.stack_section.inputs, 0, "CALLDATASIZE needs 0 inputs; disabled TSTORE must not inflate the section" @@ -485,12 +485,12 @@ mod tests { vec![op::PUSH0, op::DUPN, 0x5b, op::PUSH0, op::SSTORE, op::STOP], SpecId::AMSTERDAM, ); - let dupn = bytecode.inst(Inst::from_usize(1)); + let dupn = bytecode.inst(Inst::new(1)); assert!(dupn.is_diverging(), "DUPN with invalid immediate must diverge"); assert_eq!(dupn.stack_io(), (0, 0), "DUPN with invalid immediate must have zero stack I/O"); // The instructions after it are dead, so they add no stack inputs or gas to a section. - let after = bytecode.inst(Inst::from_usize(3)); + let after = bytecode.inst(Inst::new(3)); assert!(after.is_dead_code()); } } diff --git a/crates/jit/codegen/src/compiler/translate/mod.rs b/crates/jit/codegen/src/compiler/translate/mod.rs index 6126e76dc..5c2580f82 100644 --- a/crates/jit/codegen/src/compiler/translate/mod.rs +++ b/crates/jit/codegen/src/compiler/translate/mod.rs @@ -398,7 +398,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } // The bytecode is guaranteed to have at least one instruction. - let first_inst_block = fx.inst_entries[Inst::from_usize(0)]; + let first_inst_block = fx.inst_entries[Inst::new(0)]; let post_entry_block = fx.bcx.create_block_after(entry_block, "entry.post"); fx.bcx.br(post_entry_block); @@ -1292,7 +1292,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // Walk backward to find the section head for diagnostic context. let mut head = inst; for i in (0..inst.index()).rev() { - let idx = crate::Inst::from_usize(i); + let idx = Inst::new(i); let d = self.bytecode.inst(idx); if d.is_dead_code() { continue; @@ -1305,7 +1305,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // Dump the section from head to current inst. let mut section_dump = String::new(); for i in head.index()..=inst.index() { - let idx = crate::Inst::from_usize(i); + let idx = Inst::new(i); let d = self.bytecode.inst(idx); if d.is_dead_code() { continue; diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index e68667a06..746566263 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -2125,13 +2125,12 @@ tests! { expected_return: InstrStop::InvalidOperandOOG, }), mload_const_offset_too_large(@raw { - bytecode: &{ - let mut code = Vec::new(); - code.push(op::PUSH9); - code.extend_from_slice(&0x1_0000_0000_0000_0000_U256.to_be_bytes::<32>()[23..]); - code.push(op::MLOAD); - code - }, + bytecode: &[ + &[op::PUSH9][..], + &0x1_0000_0000_0000_0000_U256.to_be_bytes::<32>()[23..], + &[op::MLOAD][..], + ] + .concat(), expected_return: InstrStop::InvalidOperandOOG, }), mstore1(@raw { @@ -2535,7 +2534,7 @@ tests! { assert_host: Some(|host| { assert_eq!(host.logs, [Log { address: DEF_ADDR, - data: LogData::new(vec![], Bytes::copy_from_slice(&0x6942_U256.to_be_bytes::<32>())).unwrap(), + data: LogData::new(vec![], Bytes::from(0x6942_U256.to_be_bytes::<32>())).unwrap(), }]); }), }), diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index 9fe0e3415..8902d449c 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -1,5 +1,8 @@ use super::*; -use alloy_primitives::address; +use alloy_primitives::{ + address, + map::{AddressMap, U256Map}, +}; use evm2::{ BaseEvmTypes, Evm, Precompiles, bytecode::Bytecode, @@ -174,8 +177,8 @@ pub static DEF_CD: &[u8] = &[0xaa; 64]; pub static DEF_RD: &[u8] = &[0xbb; 64]; pub static DEF_DATA: &[u8] = &[0xcc; 64]; pub const DEF_VALUE: U256 = uint!(123_456_789_U256); -pub static DEF_STORAGE: OnceLock> = OnceLock::new(); -pub static DEF_CODEMAP: OnceLock> = OnceLock::new(); +pub static DEF_STORAGE: OnceLock> = OnceLock::new(); +pub static DEF_CODEMAP: OnceLock> = OnceLock::new(); pub const OTHER_ADDR: Address = Address::repeat_byte(0x69); pub const DEF_BN: U256 = uint!(500_U256); @@ -185,7 +188,7 @@ pub const STACK_WHAT_INTERPRETER_SAYS: &[U256] = pub const MEMORY_WHAT_INTERPRETER_SAYS: &[u8] = &GAS_WHAT_INTERPRETER_SAYS.to_be_bytes(); pub const GAS_WHAT_INTERPRETER_SAYS: u64 = 0x4682e332d6612de1; -pub fn def_storage() -> &'static HashMap { +pub fn def_storage() -> &'static U256Map { DEF_STORAGE.get_or_init(|| { let mut map = HashMap::default(); map.insert(U256::ZERO, U256::ONE); @@ -195,7 +198,7 @@ pub fn def_storage() -> &'static HashMap { }) } -pub fn def_codemap() -> &'static HashMap { +pub fn def_codemap() -> &'static AddressMap { DEF_CODEMAP.get_or_init(|| { let mut map = HashMap::default(); map.insert( @@ -327,8 +330,8 @@ fn def_database() -> InMemoryDB { /// Host state snapshot for codegen assertions. pub struct HostState { - pub storage: HashMap, - pub transient_storage: HashMap, + pub storage: U256Map, + pub transient_storage: U256Map, pub logs: Vec, } diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index c279665de..4dc141280 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -919,8 +919,8 @@ mod tests { #[test] fn extern_macro() { - let f1 = EvmCompilerFn::new(test_fn).0; - let f2 = EvmCompilerFn::new(__test_fn).0; + let f1 = EvmCompilerFn::new(test_fn).into_inner(); + let f2 = EvmCompilerFn::new(__test_fn).into_inner(); assert!(core::ptr::fn_addr_eq(f1, f2), "{f1:?} != {f2:?}"); } } diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 61dc8b332..05d5d874c 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -973,9 +973,9 @@ impl Backend for EvmLlvmBackend { // Can be overridden with `EVM2_JIT_PASSES` env var for experimentation. // From `opt --help`, `-passes`. - static PASSES_OVERRIDE: std::sync::OnceLock> = std::sync::OnceLock::new(); - static PASSES: std::sync::OnceLock = std::sync::OnceLock::new(); - static PASSES_WITH_LICM: std::sync::OnceLock = std::sync::OnceLock::new(); + static PASSES_OVERRIDE: OnceLock> = OnceLock::new(); + static PASSES: OnceLock = OnceLock::new(); + static PASSES_WITH_LICM: OnceLock = OnceLock::new(); let passes = PASSES_OVERRIDE.get_or_init(|| std::env::var("EVM2_JIT_PASSES").ok()); let passes = passes.as_deref().unwrap_or_else(|| match self.backend_config.opt_level { @@ -1184,7 +1184,7 @@ impl EvmLlvmBuilder<'_> { fn get_overflow_function(&mut self, name: &str, ty: Type) -> Function { let name = format!("llvm.{name}.with.overflow.{}", fmt_ty(ty)); self.get_or_add_function(&name, |this| { - this.fn_type(Some(this.cx.struct_type(&[ty, this.ty_i1], false)), &[ty, ty]) + this.fn_type(Some(this.cx().struct_type(&[ty, this.ty_i1], false)), &[ty, ty]) }) } @@ -1210,12 +1210,12 @@ impl EvmLlvmBuilder<'_> { fn set_branch_weights(&self, inst: Inst, weights: impl IntoIterator) { let weights = weights.into_iter(); let mut values = Vec::::with_capacity(1 + weights.size_hint().1.unwrap()); - values.push(self.cx.metadata_string("branch_weights")); + values.push(self.cx().metadata_string("branch_weights")); for weight in weights { values.push(self.ty_i32.const_int(weight as u64, false).as_metadata()); } - let metadata = self.cx.metadata_node(&values); - let kind_id = self.cx.get_kind_id("prof"); + let metadata = self.cx().metadata_node(&values); + let kind_id = self.cx().get_kind_id("prof"); inst.set_metadata(metadata, kind_id); } } @@ -1253,11 +1253,11 @@ impl TypeMethods for EvmLlvmBuilder<'_> { impl Builder for EvmLlvmBuilder<'_> { fn create_block(&mut self, name: &str) -> Self::BasicBlock { - self.cx.append_basic_block(self.function, self.name(name)) + self.cx().append_basic_block(self.function, self.name(name)) } fn create_block_after(&mut self, after: Self::BasicBlock, name: &str) -> Self::BasicBlock { - self.cx.insert_basic_block_after(after, self.name(name)) + self.cx().insert_basic_block_after(after, self.name(name)) } fn switch_to_block(&mut self, block: Self::BasicBlock) { @@ -1287,15 +1287,15 @@ impl Builder for EvmLlvmBuilder<'_> { } let Some(block) = self.current_block() else { return }; let Some(ins) = block.get_last_instruction() else { return }; - let metadata = self.cx.metadata_string(comment); - let metadata = self.cx.metadata_node(&[metadata]); - ins.set_metadata(metadata, self.cx.get_kind_id("annotation")); + let metadata = self.cx().metadata_string(comment); + let metadata = self.cx().metadata_node(&[metadata]); + ins.set_metadata(metadata, self.cx().get_kind_id("annotation")); } fn set_debug_location(&mut self, line: u32, col: u32) { let Some(scope) = self.debug_scope else { return }; let Some(di) = &self.di_state else { return }; - let loc = di.dibuilder.create_debug_location(self.cx, line, col, scope); + let loc = di.dibuilder.create_debug_location(self.cx(), line, col, scope); self.bcx.set_current_debug_location(loc); } @@ -1731,7 +1731,7 @@ impl Builder for EvmLlvmBuilder<'_> { cpp::set_dso_local(function); let prev_function = std::mem::replace(&mut self.function, function); - let entry = self.cx.append_basic_block(function, self.name("entry")); + let entry = self.cx().append_basic_block(function, self.name("entry")); self.bcx.position_at_end(entry); build(self); if let Some(before) = before { @@ -1753,7 +1753,7 @@ impl Builder for EvmLlvmBuilder<'_> { return function; } - let ty = self.cx.void_type().fn_type(&[self.ty_ptr], true); + let ty = self.cx().void_type().fn_type(&[self.ty_ptr], true); let function = self.module().add_function(name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); cpp::set_dso_local(function); function @@ -1806,7 +1806,7 @@ impl Builder for EvmLlvmBuilder<'_> { let debug_location = self.debug_scope.and_then(|_| self.bcx.get_current_debug_location()); self.bcx.unset_current_debug_location(); - let entry = self.cx.append_basic_block(stub, self.name("stub")); + let entry = self.cx().append_basic_block(stub, self.name("stub")); self.bcx.position_at_end(entry); let args = (0..stub.count_params()).map(|i| stub.get_nth_param(i).unwrap()).collect::>(); @@ -1880,7 +1880,7 @@ fn init() -> Result<()> { fn init_() -> Result<()> { extern "C" fn report_fatal_error(msg: *const std::ffi::c_char) { - let msg_cstr = unsafe { std::ffi::CStr::from_ptr(msg) }; + let msg_cstr = unsafe { CStr::from_ptr(msg) }; let msg = msg_cstr.to_string_lossy(); error!(target: "llvm", "LLVM fatal error: {msg}"); } @@ -2019,7 +2019,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::NoSync => ("nosync", AttrValue::Enum(0)), OurAttr::NoUnwind => ("nounwind", AttrValue::Enum(0)), OurAttr::NonLazyBind => ("nonlazybind", AttrValue::Enum(0)), - OurAttr::UWTable => return cpp::create_async_uwtable_attr(bcx.cx), + OurAttr::UWTable => return cpp::create_async_uwtable_attr(bcx.cx()), OurAttr::AllFramePointers => ("frame-pointer", AttrValue::String("all")), OurAttr::NativeTargetCpu => ( "target-cpu", @@ -2036,7 +2036,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::Speculatable => ("speculatable", AttrValue::Enum(0)), OurAttr::NoAlias => ("noalias", AttrValue::Enum(0)), - OurAttr::NoCapture => return cpp::create_no_captures_attr(bcx.cx), + OurAttr::NoCapture => return cpp::create_no_captures_attr(bcx.cx()), OurAttr::NoUndef => ("noundef", AttrValue::Enum(0)), OurAttr::Align(n) => ("align", AttrValue::Enum(n)), OurAttr::NonNull => ("nonnull", AttrValue::Enum(0)), @@ -2046,26 +2046,26 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::ReadOnly => ("readonly", AttrValue::Enum(0)), OurAttr::WriteOnly => ("writeonly", AttrValue::Enum(0)), OurAttr::Writable => ("writable", AttrValue::Enum(0)), - OurAttr::ArgMemOnly => return cpp::create_arg_mem_only_attr(bcx.cx), + OurAttr::ArgMemOnly => return cpp::create_arg_mem_only_attr(bcx.cx()), // `u64::MAX` marks all reachable memory as dead; `0` would mark zero bytes. OurAttr::DeadOnReturn => ("dead_on_return", AttrValue::Enum(u64::MAX)), OurAttr::DeadOnUnwind => ("dead_on_unwind", AttrValue::Enum(0)), OurAttr::Initializes(size) => { - return cpp::create_initializes_attr(bcx.cx, 0, size as i64); + return cpp::create_initializes_attr(bcx.cx(), 0, size as i64); } attr => unimplemented!("llvm attribute conversion: {attr:?}"), }; match value { - AttrValue::String(value) => bcx.cx.create_string_attribute(key, value), + AttrValue::String(value) => bcx.cx().create_string_attribute(key, value), AttrValue::Enum(value) => { let id = Attribute::get_named_enum_kind_id(key); - bcx.cx.create_enum_attribute(id, value) + bcx.cx().create_enum_attribute(id, value) } AttrValue::Type(ty) => { let id = Attribute::get_named_enum_kind_id(key); - bcx.cx.create_type_attribute(id, ty) + bcx.cx().create_type_attribute(id, ty) } } } @@ -2134,7 +2134,7 @@ mod tests { let mut backend = EvmLlvmBackend::new(true).unwrap(); unsafe { - let cx = backend.cx.raw(); + let cx = backend.cx().raw(); let void = LLVMVoidTypeInContext(cx); let i32 = LLVMInt32TypeInContext(cx); let function_ty = LLVMFunctionType(void, std::ptr::null_mut(), 0, 0); diff --git a/crates/jit/llvm/src/orc.rs b/crates/jit/llvm/src/orc.rs index fec202773..aecc6434d 100644 --- a/crates/jit/llvm/src/orc.rs +++ b/crates/jit/llvm/src/orc.rs @@ -1340,7 +1340,7 @@ impl LLJIT { /// Creates a new ORC JIT, auto-detecting the host target. pub fn new() -> Result { - LLJITBuilder::new().build() + Self::builder().build() } /// Wraps a raw pointer. diff --git a/crates/jit/llvm/src/wrap/di.rs b/crates/jit/llvm/src/wrap/di.rs index b2f1190a1..3b5fc50a8 100644 --- a/crates/jit/llvm/src/wrap/di.rs +++ b/crates/jit/llvm/src/wrap/di.rs @@ -143,7 +143,7 @@ impl DebugInfoBuilder { scope: Metadata, ) -> DILocation { DILocation(unsafe { - LLVMDIBuilderCreateDebugLocation(cx.raw(), line, column, scope.0, ptr::null_mut()) + LLVMDIBuilderCreateDebugLocation(cx.raw(), line, column, scope.raw(), ptr::null_mut()) }) } diff --git a/crates/jit/llvm/src/wrap/ir.rs b/crates/jit/llvm/src/wrap/ir.rs index f1366e7e3..54ad1b5e1 100644 --- a/crates/jit/llvm/src/wrap/ir.rs +++ b/crates/jit/llvm/src/wrap/ir.rs @@ -123,7 +123,9 @@ impl Context { /// Appends a basic block to `function`. pub(crate) fn append_basic_block(&self, function: Function, name: &str) -> Block { let name = to_c_str(name); - Block(unsafe { LLVMAppendBasicBlockInContext(self.raw, function.0, name.as_ptr()) }) + Block(unsafe { + LLVMAppendBasicBlockInContext(self.raw, function.as_value_ref(), name.as_ptr()) + }) } /// Inserts a basic block right after `block`. @@ -279,7 +281,9 @@ impl Module { /// Iterates over the functions of the module. pub(crate) fn get_functions(&self) -> impl Iterator { let first = Function::new(unsafe { LLVMGetFirstFunction(self.raw) }); - std::iter::successors(first, |f| Function::new(unsafe { LLVMGetNextFunction(f.0) })) + std::iter::successors(first, |f| { + Function::new(unsafe { LLVMGetNextFunction(f.as_value_ref()) }) + }) } /// Adds a global variable in the default address space. @@ -792,7 +796,7 @@ impl Builder { LLVMBuildCall2( self.raw, fn_ty.0, - function.0, + function.as_value_ref(), args.as_ptr().cast_mut().cast(), args.len() as u32, name.as_ptr(), diff --git a/crates/jit/runtime/src/runtime/out_of_process.rs b/crates/jit/runtime/src/runtime/out_of_process.rs index 3a773a9b2..05eb6aa9a 100644 --- a/crates/jit/runtime/src/runtime/out_of_process.rs +++ b/crates/jit/runtime/src/runtime/out_of_process.rs @@ -841,7 +841,7 @@ fn write_pause_ack(stdout: &mut BufWriter, id: u64) -> eyr fn write_message(w: &mut BufWriter, message: &T) -> std::io::Result<()> where - T: wincode::SchemaWrite, + T: SchemaWrite, { wincode::serialize_into(w, message) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index eb4e05b49..92fe7b5a2 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -586,7 +586,7 @@ struct CompilerProfile { layout: evm2_jit_codegen::HostLayout, resolver: Option, symbol_suffix: Option, - opcode_view: Option>, + opcode_view: Option>, opt_level: crate::OptimizationLevel, gas_params: Option, debug_assertions: bool, From 6bc1c09acae7157ecb377362d872f2c95cd26007 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 12:15:53 +0100 Subject: [PATCH 125/159] refactor(evm): define the modexp minimum gas once (#943) The modexp precompile took each fork's minimum gas (0, 200, 500) as a literal argument to `run_inner`, and set the same number again as `min_price` in the fork's gas calculation. It charged the literal first, then `gas_cost.saturating_sub(min_gas)`, so the code was only right while the two values agreed. Each fork's `GasCalcParams` is now a constant that the gas calculation and `run_inner` both use. `run_inner` still charges `min_price` before it parses the lengths, so a call that cannot afford the minimum fails before that work, then charges `cost - min_price` after the calculation, and before the EIP-7823 size check. Charges and results do not change. The Osaka doc also repeated the Berlin line; it now links EIP-7883. --- crates/evm2/src/precompiles/modexp.rs | 41 +++++++++++---------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index 0a931d18f..330242c53 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -154,19 +154,19 @@ fn modexp_big(base: &[u8], exponent: &[u8], modulus: &[u8]) -> Vec { /// See: /// See: pub fn run_byzantium(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 0, false, byzantium_gas_calc) + run_inner(input, gas, BYZANTIUM_PARAMS, false, byzantium_gas_calc) } /// See: /// Gas cost of berlin is modified from byzantium. pub fn run_berlin(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 200, false, berlin_gas_calc) + run_inner(input, gas, BERLIN_PARAMS, false, berlin_gas_calc) } /// See: -/// Gas cost of berlin is modified from byzantium. +/// See: pub fn run_osaka(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 500, true, osaka_gas_calc) + run_inner(input, gas, OSAKA_PARAMS, true, osaka_gas_calc) } /// Calculate the iteration count for the modexp precompile. @@ -186,14 +186,15 @@ pub(crate) fn calculate_iteration_count(exp_length: u64, exp_highp: &U256, multi } /// Run the modexp precompile. -pub fn run_inner( +fn run_inner( input: &[u8], gas: &mut GasTracker, - min_gas: u64, + params: GasCalcParams, osaka: bool, calc_gas: fn(u64, u64, u64, &U256) -> u64, ) -> PrecompileResult { - gas.spend(min_gas)?; + // Fail before parsing the lengths when the minimum price is unaffordable. + gas.spend(params.min_price)?; // The format of input is: // @@ -229,7 +230,7 @@ pub fn run_inner( // Check if we have enough gas. let gas_cost = calc_gas(base_len64, exp_len64, mod_len64, &exp_highp); - gas.spend(gas_cost.saturating_sub(min_gas))?; + gas.spend(gas_cost - params.min_price)?; // EIP-7823 limits the size of the inputs, after the gas is charged. if osaka @@ -269,7 +270,7 @@ pub(crate) fn byzantium_gas_calc( exp_len, mod_len, exp_highp, - GasCalcParams { min_price: 0, multiplier: 8, divisor: 20 }, + BYZANTIUM_PARAMS, byzantium_multiplication_complexity, ) } @@ -277,14 +278,7 @@ pub(crate) fn byzantium_gas_calc( /// Calculate gas cost according to EIP 2565: /// pub(crate) fn berlin_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { - gas_calc( - base_len, - exp_len, - mod_len, - exp_highp, - GasCalcParams { min_price: 200, multiplier: 8, divisor: 3 }, - berlin_multiplication_complexity, - ) + gas_calc(base_len, exp_len, mod_len, exp_highp, BERLIN_PARAMS, berlin_multiplication_complexity) } /// Calculate gas cost according to EIP-7883: @@ -295,14 +289,7 @@ pub(crate) fn berlin_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_hig /// 2. Increase cost when exponent is larger than 32 bytes /// 3. Increase cost when base or modulus is larger than 32 bytes pub(crate) fn osaka_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { - gas_calc( - base_len, - exp_len, - mod_len, - exp_highp, - GasCalcParams { min_price: 500, multiplier: 16, divisor: 1 }, - osaka_multiplication_complexity, - ) + gas_calc(base_len, exp_len, mod_len, exp_highp, OSAKA_PARAMS, osaka_multiplication_complexity) } #[derive(Clone, Copy, Debug)] @@ -312,6 +299,10 @@ pub(crate) struct GasCalcParams { divisor: u64, } +const BYZANTIUM_PARAMS: GasCalcParams = GasCalcParams { min_price: 0, multiplier: 8, divisor: 20 }; +const BERLIN_PARAMS: GasCalcParams = GasCalcParams { min_price: 200, multiplier: 8, divisor: 3 }; +const OSAKA_PARAMS: GasCalcParams = GasCalcParams { min_price: 500, multiplier: 16, divisor: 1 }; + /// Calculate gas cost. #[inline] pub(crate) fn gas_calc( From ab7d38799727e90ff14cf9a2d939b454b89252b4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 12:53:03 +0100 Subject: [PATCH 126/159] refactor(jit): reuse core decode_single, decode_pair, and analyze_legacy (#941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The JIT kept line-for-line copies of core's private `decode_single` and `decode_pair` (DUPN/SWAPN/EXCHANGE immediate decoding), of the matching encoders, and of the legacy `JUMPDEST` scan. This makes the core functions public and documented, and deletes the copies. `decode_single` and `decode_pair` now return stack indices as `u8`, as the JIT already did; the interpreter casts them to `usize` at the call sites. The JIT now stores the `JumpTable` that core's `analyze_legacy` returns, instead of converting it to a `BitVec`. ## Benchmarks Codegen is unchanged: `opt.s` changed in 0/31 benchmarks, and compile instructions are within noise (TOTAL -0.1%). Head `c05708ba0a` vs base `origin/main` (`55c72ebb2f`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **-0.1% 🟢** | **+0.1% 🔴** | **+0.1% 🔴** | | **GEOMEAN** | **-0.1% 🟢** | **=** | **+0.2% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.49M
37.42M
-0.2% 🟢 | 37.54M
37.30M
-0.6% 🟢 | 11.2ms
11.2ms
-0.1% 🟢 | 181.7µs
162.4µs
-10.6% 🟢 | 415.1µs
405.1µs
-2.4% 🟢 | 1.39ms
1.37ms
-1.5% 🟢 | 2.93ms
3.00ms
+2.7% 🔴 | | factorial | 41.71M
41.70M
= | 38.62M
38.78M
+0.4% 🔴 | 11.1ms
11.4ms
+2.7% 🔴 | 141.4µs
149.9µs
+6.1% 🔴 | 348.7µs
354.3µs
+1.6% 🔴 | 1.17ms
1.20ms
+2.3% 🔴 | 3.14ms
3.24ms
+3.1% 🔴 | | counter | 54.16M
54.19M
+0.1% 🔴 | 38.50M
39.06M
+1.4% 🔴 | 9.00ms
9.13ms
+1.4% 🔴 | 288.6µs
311.5µs
+7.9% 🔴 | 445.7µs
474.4µs
+6.4% 🔴 | 1.33ms
1.38ms
+3.9% 🔴 | 3.60ms
3.65ms
+1.3% 🔴 | | snailtracer | 5.29G
5.24G
-0.9% 🟢 | 2.00G
1.99G
-0.7% 🟢 | 381.8ms
379.2ms
-0.7% 🟢 | 32.3ms
30.6ms
-5.3% 🟢 | 6.77ms
6.81ms
+0.6% 🔴 | 67.7ms
66.8ms
-1.3% 🟢 | 308.7ms
309.6ms
+0.3% 🔴 | | weth | 538.92M
538.99M
= | 243.04M
243.41M
+0.2% 🔴 | 48.2ms
48.2ms
= | 2.23ms
2.18ms
-2.0% 🟢 | 1.91ms
1.96ms
+2.7% 🔴 | 11.0ms
11.1ms
+1.6% 🔴 | 33.0ms
33.0ms
= | | hash_10k | 80.44M
80.43M
= | 51.86M
51.76M
-0.2% 🟢 | 11.9ms
12.0ms
+0.4% 🔴 | 310.7µs
268.5µs
-13.6% 🟢 | 548.7µs
453.6µs
-17.3% 🟢 | 2.33ms
1.81ms
-22.5% 🟢 | 5.49ms
5.14ms
-6.4% 🟢 | | erc20_transfer | 576.84M
577.02M
= | 252.23M
251.64M
-0.2% 🟢 | 50.2ms
49.7ms
-1.0% 🟢 | 2.27ms
2.53ms
+11.6% 🔴 | 1.97ms
1.97ms
-0.2% 🟢 | 11.8ms
12.1ms
+2.2% 🔴 | 34.0ms
33.6ms
-0.9% 🟢 | | push0_proxy | 34.69M
34.75M
+0.2% 🔴 | 32.43M
32.37M
-0.2% 🟢 | 10.1ms
10.4ms
+3.0% 🔴 | 135.9µs
129.8µs
-4.5% 🟢 | 345.7µs
325.4µs
-5.9% 🟢 | 845.7µs
799.9µs
-5.4% 🟢 | 2.19ms
2.12ms
-2.9% 🟢 | | usdc_proxy | 244.00M
244.01M
= | 122.51M
121.73M
-0.6% 🟢 | 25.3ms
24.9ms
-1.2% 🟢 | 1.33ms
1.30ms
-2.4% 🟢 | 1.17ms
1.14ms
-3.1% 🟢 | 6.33ms
5.81ms
-8.3% 🟢 | 14.6ms
14.6ms
+0.2% 🔴 | | fiat_token | 3.98G
3.97G
-0.1% 🟢 | 1.59G
1.60G
+0.7% 🔴 | 305.1ms
305.9ms
+0.3% 🔴 | 13.8ms
13.8ms
+0.1% 🔴 | 9.94ms
10.1ms
+1.7% 🔴 | 69.0ms
69.0ms
= | 244.0ms
246.1ms
+0.9% 🔴 | | uniswap_v2_pair | 2.06G
2.06G
= | 853.77M
854.61M
+0.1% 🔴 | 164.8ms
165.4ms
+0.4% 🔴 | 7.64ms
7.62ms
-0.2% 🟢 | 6.11ms
5.86ms
-4.1% 🟢 | 34.1ms
34.3ms
+0.7% 🔴 | 129.4ms
130.4ms
+0.8% 🔴 | | univ2_router | 4.38G
4.38G
-0.1% 🟢 | 1.80G
1.80G
+0.1% 🔴 | 344.1ms
344.6ms
+0.1% 🔴 | 15.8ms
15.7ms
-0.8% 🟢 | 10.5ms
11.1ms
+6.0% 🔴 | 68.9ms
69.8ms
+1.2% 🔴 | 284.9ms
287.3ms
+0.8% 🔴 | | seaport | 9.73G
9.72G
-0.1% 🟢 | 4.24G
4.25G
+0.2% 🔴 | 803.3ms
807.7ms
+0.6% 🔴 | 26.3ms
26.1ms
-1.1% 🟢 | 17.8ms
17.9ms
+0.6% 🔴 | 174.9ms
176.1ms
+0.7% 🔴 | 688.3ms
694.3ms
+0.9% 🔴 | | airdrop | 1.42G
1.42G
= | 590.25M
589.12M
-0.2% 🟢 | 114.3ms
114.2ms
-0.1% 🟢 | 4.73ms
4.71ms
-0.5% 🟢 | 3.93ms
4.08ms
+3.9% 🔴 | 29.5ms
29.4ms
-0.2% 🟢 | 83.4ms
84.0ms
+0.7% 🔴 | | bswap64 | 67.07M
67.02M
-0.1% 🟢 | 43.83M
43.77M
-0.1% 🟢 | 10.4ms
10.4ms
-0.1% 🟢 | 473.4µs
474.9µs
+0.3% 🔴 | 572.3µs
578.1µs
+1.0% 🔴 | 1.66ms
1.66ms
+0.2% 🔴 | 3.80ms
3.91ms
+2.8% 🔴 | | bswap64_opt | 73.78M
73.76M
= | 45.02M
44.61M
-0.9% 🟢 | 10.3ms
10.2ms
-1.4% 🟢 | 398.7µs
411.5µs
+3.2% 🔴 | 498.1µs
502.7µs
+0.9% 🔴 | 1.60ms
1.64ms
+2.3% 🔴 | 4.17ms
4.35ms
+4.3% 🔴 | | eip4788 | 48.60M
48.56M
-0.1% 🟢 | 36.11M
36.25M
+0.4% 🔴 | 8.64ms
8.76ms
+1.4% 🔴 | 182.5µs
185.1µs
+1.4% 🔴 | 376.5µs
379.8µs
+0.9% 🔴 | 995.1µs
1.04ms
+4.8% 🔴 | 3.36ms
3.44ms
+2.3% 🔴 | | eip2935 | 42.43M
42.41M
= | 38.34M
37.94M
-1.1% 🟢 | 11.2ms
11.0ms
-1.6% 🟢 | 155.7µs
161.2µs
+3.5% 🔴 | 349.1µs
365.7µs
+4.8% 🔴 | 894.1µs
937.8µs
+4.9% 🔴 | 2.91ms
2.93ms
+0.8% 🔴 | | burntpix | 6.70G
6.70G
= | 2.66G
2.66G
+0.2% 🔴 | 505.7ms
504.9ms
-0.2% 🟢 | 13.8ms
13.6ms
-1.5% 🟢 | 1.59ms
1.58ms
-0.4% 🟢 | 9.99ms
10.1ms
+0.9% 🔴 | 28.3ms
28.4ms
+0.5% 🔴 | | curve_stableswap | 1.14G
1.14G
= | 484.08M
484.92M
+0.2% 🔴 | 93.9ms
94.3ms
+0.4% 🔴 | 3.29ms
3.32ms
+1.0% 🔴 | 2.60ms
2.49ms
-4.5% 🟢 | 21.3ms
21.3ms
+0.2% 🔴 | 47.8ms
48.4ms
+1.3% 🔴 | | onchain_lm_v2 | 3.67G
3.66G
-0.3% 🟢 | 1.42G
1.41G
-0.7% 🟢 | 273.0ms
271.6ms
-0.5% 🟢 | 10.5ms
10.6ms
+1.2% 🔴 | 7.48ms
7.22ms
-3.5% 🟢 | 46.8ms
47.1ms
+0.5% 🔴 | 189.7ms
190.7ms
+0.5% 🔴 | | usdt | 1.34G
1.34G
= | 550.53M
550.51M
= | 107.3ms
106.9ms
-0.4% 🟢 | 6.48ms
6.49ms
+0.1% 🔴 | 4.93ms
4.90ms
-0.6% 🟢 | 25.4ms
25.4ms
= | 78.8ms
78.8ms
-0.1% 🟢 | | universal_router | 11.57G
11.56G
= | 4.81G
4.81G
= | 908.3ms
909.5ms
+0.1% 🔴 | 14.1ms
13.8ms
-2.1% 🟢 | 11.2ms
10.5ms
-5.9% 🟢 | 81.4ms
81.7ms
+0.4% 🔴 | 319.4ms
320.4ms
+0.3% 🔴 | | aggregation_router_v6 | 35.12G
35.11G
= | 14.41G
14.44G
+0.2% 🔴 | 2.710s
2.716s
+0.2% 🔴 | 22.3ms
21.8ms
-2.0% 🟢 | 17.4ms
16.9ms
-3.0% 🟢 | 137.4ms
140.0ms
+1.9% 🔴 | 524.6ms
523.2ms
-0.3% 🟢 | | entry_point_v0_7 | 17.89G
17.88G
-0.1% 🟢 | 7.37G
7.40G
+0.4% 🔴 | 1.390s
1.398s
+0.5% 🔴 | 10.5ms
10.5ms
+0.1% 🔴 | 9.21ms
9.22ms
+0.1% 🔴 | 63.0ms
63.6ms
+1.0% 🔴 | 270.3ms
271.0ms
+0.2% 🔴 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.56G
2.57G
+0.4% 🔴 | 489.5ms
491.0ms
+0.3% 🔴 | 21.8ms
22.7ms
+4.5% 🔴 | 15.6ms
16.7ms
+7.4% 🔴 | 111.4ms
112.2ms
+0.7% 🔴 | 402.2ms
404.9ms
+0.7% 🔴 | | uniswap_v4_pool_manager | 6.70G
6.70G
= | 2.74G
2.74G
-0.1% 🟢 | 520.8ms
521.1ms
+0.1% 🔴 | 15.5ms
15.6ms
+0.7% 🔴 | 12.9ms
13.1ms
+1.1% 🔴 | 107.5ms
107.9ms
+0.3% 🔴 | 396.6ms
397.2ms
+0.2% 🔴 | | curve_3pool | 6.59G
6.59G
= | 2.55G
2.56G
+0.4% 🔴 | 487.5ms
490.4ms
+0.6% 🔴 | 21.4ms
21.5ms
+0.4% 🔴 | 13.5ms
13.5ms
= | 86.5ms
86.6ms
+0.1% 🔴 | 437.8ms
440.7ms
+0.7% 🔴 | | curve_tricrypto2 | 9.40G
9.40G
= | 3.64G
3.65G
+0.5% 🔴 | 690.7ms
693.7ms
+0.4% 🔴 | 19.9ms
19.7ms
-0.7% 🟢 | 14.9ms
15.0ms
+0.5% 🔴 | 96.6ms
96.8ms
+0.2% 🔴 | 426.1ms
431.6ms
+1.3% 🔴 | | balancer_vault | 19.02G
18.94G
-0.4% 🟢 | 8.21G
8.15G
-0.7% 🟢 | 1.543s
1.534s
-0.6% 🟢 | 28.3ms
28.7ms
+1.3% 🔴 | 18.5ms
19.1ms
+3.2% 🔴 | 203.9ms
205.8ms
+0.9% 🔴 | 653.0ms
660.0ms
+1.1% 🔴 | | succinct_staking | 10.85G
10.84G
-0.1% 🟢 | 4.43G
4.45G
+0.5% 🔴 | 837.5ms
837.8ms
= | 19.8ms
20.4ms
+2.9% 🔴 | 11.2ms
12.0ms
+7.4% 🔴 | 87.6ms
88.0ms
+0.5% 🔴 | 317.9ms
319.8ms
+0.6% 🔴 | | **TOTAL** | **164.87G
164.68G
-0.1% 🟢** | **67.88G
67.94G
+0.1% 🔴** | **12.888s
12.903s
+0.1% 🔴** | **316.2ms
315.5ms
-0.2% 🟢** | **205.0ms
207.0ms
+1.0% 🔴** | **1.564s
1.573s
+0.5% 🔴** | **5.944s
5.980s
+0.6% 🔴** |
--- crates/evm2/src/bytecode/analysis.rs | 8 +- crates/evm2/src/bytecode/mod.rs | 3 +- .../src/interpreter/instructions/stack.rs | 119 ++++++++++++++++-- crates/jit/codegen/src/bytecode/fmt.rs | 3 +- crates/jit/codegen/src/bytecode/mod.rs | 51 ++------ crates/jit/codegen/src/bytecode/opcode.rs | 116 +---------------- .../src/bytecode/passes/block_analysis.rs | 18 +-- 7 files changed, 140 insertions(+), 178 deletions(-) diff --git a/crates/evm2/src/bytecode/analysis.rs b/crates/evm2/src/bytecode/analysis.rs index adea0263f..e31f9b112 100644 --- a/crates/evm2/src/bytecode/analysis.rs +++ b/crates/evm2/src/bytecode/analysis.rs @@ -10,9 +10,13 @@ mod aarch64; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86; -/// Analyzes the original bytecode to produce a jump table. +/// Analyzes the original, unpadded legacy bytecode to produce its jump table. +/// +/// A `JUMPDEST` byte is a valid jump destination unless it is part of the data of a +/// `PUSH1..=PUSH32` instruction. This applies to every fork: the immediates of +/// `DUPN`/`SWAPN`/`EXCHANGE` are scanned as if they were opcodes. #[inline] -pub(super) fn analyze_legacy(bytecode: &[u8]) -> JumpTable { +pub fn analyze_legacy(bytecode: &[u8]) -> JumpTable { let mut jumps = JumpTable::new(bytecode.len()); let table = jumps.as_mut_slice(); let pc = analyze_simd(bytecode, table); diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index dec3b6f7e..145b49e84 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -7,7 +7,7 @@ use crate::{ }; use alloc::sync::Arc; use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, keccak256}; -use analysis::{analyze_legacy, pad_legacy}; +use analysis::pad_legacy; use core::{cmp::Ordering, fmt, hash}; use thiserror::Error; @@ -17,6 +17,7 @@ mod jump_table; #[cfg(feature = "serde")] mod serde_impl; +pub use analysis::analyze_legacy; pub use jump_table::{JumpTable, JumpTableRef}; /// EIP-7702 decode errors. diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index 45748cf32..c8dd9ca77 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -27,39 +27,80 @@ pub fn swap() -> Result { pub fn dupn(cx: _) -> Result { let n = decode_single(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.dup(n) + stack.dup(n as usize) } #[instruction(no_stack_preamble)] pub fn swapn(cx: _) -> Result { let n = decode_single(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.swap(n) + stack.swap(n as usize) } #[instruction(no_stack_preamble)] pub fn exchange(cx: _) -> Result { let (n, m) = decode_pair(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.exchange(n, m) + stack.exchange(n as usize, m as usize) } -const fn decode_single(x: u8) -> Option { - if x <= 90 || x >= 128 { Some(x.wrapping_add(145) as usize) } else { None } +/// Decodes a `DUPN`/`SWAPN` immediate byte into a stack index. +/// +/// Returns `None` if the immediate is in the invalid range `[91, 127]`. +pub const fn decode_single(x: u8) -> Option { + if x <= 90 || x >= 128 { Some(x.wrapping_add(145)) } else { None } } -const fn decode_pair(x: u8) -> Option<(usize, usize)> { +/// Decodes an `EXCHANGE` immediate byte into a pair of stack indices `(n, m)`. +/// +/// Returns `None` if the immediate is in the invalid range `[82, 127]`. +pub const fn decode_pair(x: u8) -> Option<(u8, u8)> { if x > 81 && x < 128 { return None; } - let k = (x ^ 143) as usize; - let q = k / 16; - let r = k % 16; + let k = (x ^ 143) as u16; + let q = (k / 16) as u8; + let r = (k % 16) as u8; if q < r { Some((q + 1, r + 1)) } else { Some((r + 1, 29 - q)) } } +/// Encodes a stack index into a DUPN/SWAPN immediate byte. +/// +/// Returns `None` if the value is outside the valid range. +pub fn encode_single(n: u8) -> Option { + let x = n.wrapping_sub(145); + if decode_single(x) == Some(n) { Some(x) } else { None } +} + +/// Encodes a pair of stack indices into an EXCHANGE immediate byte. +/// +/// Returns `None` if the pair cannot be encoded. +pub fn encode_pair(n: u8, m: u8) -> Option { + if n == 0 || m == 0 { + return None; + } + // Try both (q,r) orderings and check round-trip. + let try_k = |q: u8, r: u8| -> Option { + if q >= 16 || r >= 16 { + return None; + } + let k = q as u16 * 16 + r as u16; + let x = (k as u8) ^ 143; + if decode_pair(x) == Some((n, m)) { Some(x) } else { None } + }; + + // Case 1: q < r → n = q+1, m = r+1. + if n < m { + try_k(n - 1, m - 1) + } else { + // Case 2: q >= r → n = r+1, m = 29-q. + if let Some(q) = 29u8.checked_sub(m) { try_k(q, n - 1) } else { None } + } +} + #[cfg(test)] mod tests { + use super::*; use crate::{ SpecId, interpreter::{InstrStop, StackMut, Word, op}, @@ -370,4 +411,64 @@ mod tests { let interp = run(RunConfig::new([op::EXCHANGE, 0x8e]).spec(SpecId::OSAKA)); assert_matches!(interp.err, InstrStop::NotActivated); } + + #[test] + fn encode_decode_single_roundtrip() { + for x in 0..=255u8 { + if let Some(n) = decode_single(x) { + assert_eq!(encode_single(n), Some(x), "roundtrip failed for x={x}, n={n}"); + } + } + for n in 0..=255u8 { + if let Some(x) = encode_single(n) { + assert_eq!(decode_single(x), Some(n), "roundtrip failed for n={n}, x={x}"); + } + } + // Out-of-range: values in [236,255] ∪ [0,16] have no valid encoding. + assert_eq!(encode_single(0), None); + assert_eq!(encode_single(16), None); + assert_eq!(encode_single(236), None); + // Invalid raw bytes. + assert_eq!(decode_single(91), None); + assert_eq!(decode_single(127), None); + } + + #[test] + fn encode_decode_pair_roundtrip() { + // encode → decode is always exact. + for n in 1..=30u8 { + for m in 1..=30u8 { + if let Some(x) = encode_pair(n, m) { + assert_eq!( + decode_pair(x), + Some((n, m)), + "roundtrip failed for (n,m)=({n},{m}), x={x}" + ); + } + } + } + // decode → encode: some pairs have multiple encodings, so the raw byte may differ, + // but decoding the result must produce the same pair. + // Some decoded pairs may have values too large to re-encode. + for x in 0..=255u8 { + if let Some((n, m)) = decode_pair(x) + && let Some(re) = encode_pair(n, m) + { + assert_eq!( + decode_pair(re), + Some((n, m)), + "decode roundtrip failed for x={x}, re={re}, (n,m)=({n},{m})" + ); + } + } + // Zero indices are invalid. + assert_eq!(encode_pair(0, 1), None); + assert_eq!(encode_pair(1, 0), None); + // Invalid raw bytes: [82, 127]. + assert_eq!(decode_pair(82), None); + assert_eq!(decode_pair(127), None); + // Edge: 80 and 81 are now valid. + assert!(decode_pair(80).is_some()); + assert!(decode_pair(81).is_some()); + } } diff --git a/crates/jit/codegen/src/bytecode/fmt.rs b/crates/jit/codegen/src/bytecode/fmt.rs index 85e8ed1b5..7a814af5a 100644 --- a/crates/jit/codegen/src/bytecode/fmt.rs +++ b/crates/jit/codegen/src/bytecode/fmt.rs @@ -1,7 +1,6 @@ use super::{ Bytecode, Inst, InstData, InstFlags, asm::{TokenKind, Tokenizer}, - bitvec_as_bytes, }; use alloy_primitives::hex; use evm2::interpreter::{op, opcode::OpCode}; @@ -282,7 +281,7 @@ impl fmt::Debug for Bytecode<'_> { f.debug_struct("Bytecode") .field("code", &hex::encode(&*self.code)) .field("insts", &self.insts) - .field("jumpdests", &hex::encode(bitvec_as_bytes(&self.jumpdests))) + .field("jumpdests", &hex::encode(self.jumpdests.as_slice())) .field("spec_id", &self.spec_id) .field("has_dynamic_jumps", &self.has_dynamic_jumps) .finish() diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index 929365567..b258a41a9 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -2,9 +2,9 @@ use crate::FxHashMap; use alloy_primitives::U256; -use bitvec::vec::BitVec; use evm2::{ OpcodeView, SpecId, + bytecode::{JumpTable, analyze_legacy}, interpreter::{op, opcode::OpCode}, version::GasParams, }; @@ -161,8 +161,9 @@ pub struct Bytecode<'a> { pub(crate) code: Cow<'a, [u8]>, /// The instructions. insts: IndexVec, - /// `JUMPDEST` opcode map. `jumpdests[pc]` is `true` if `code[pc] == op::JUMPDEST`. - jumpdests: BitVec, + /// `JUMPDEST` opcode map: `jumpdests.is_valid(pc)` is `true` if `code[pc] == op::JUMPDEST` + /// and `pc` is not inside `PUSH` data. + jumpdests: JumpTable, /// The [`SpecId`]. pub(crate) spec_id: SpecId, /// Gas parameters for dynamic gas folding. Defaults to the evm2 spec gas schedule. @@ -238,10 +239,10 @@ impl<'a> Bytecode<'a> { let mut inst_to_pc = IndexVec::with_capacity(code.len() + 8); // EIP-8024: JUMPDEST analysis is unchanged by DUPN/SWAPN/EXCHANGE: only PUSH // data is skipped, their immediates are not. Compute the valid-jump-target - // bitmap with the legacy scan over the raw bytes, independent of instruction + // bitmap with the legacy analysis over the raw bytes, independent of instruction // decoding below (which does consume DUPN/SWAPN/EXCHANGE immediates and can // therefore desynchronize from the analysis). - let jumpdests = legacy_jumpdests(&code); + let jumpdests = analyze_legacy(&code); let mut pc_to_inst = FxHashMap::with_capacity_and_hasher(code.len(), Default::default()); let mut u256_interner = U256Interner::new(); let owned_infos = opcodes.map(|opcodes| opcode_infos(opcodes, spec_id)); @@ -335,7 +336,7 @@ impl<'a> Bytecode<'a> { min_imm_len(opcode) as usize }; let imm_end = (pc + 1 + imm_len).min(code.len()); - if let Some(target) = (pc + 1..imm_end).find(|&p| jumpdests[p]) { + if let Some(target) = (pc + 1..imm_end).find(|&p| jumpdests.is_valid(p)) { // SAFETY: we just consumed the immediate bytes at `pc + 1..imm_end`. unsafe { iter.rewind(imm_end - target) }; } @@ -664,11 +665,8 @@ impl<'a> Bytecode<'a> { } /// Returns `true` if the given program counter is a valid jump destination. - pub(crate) fn is_valid_jump(&self, pc: usize) -> bool { - // Reads the raw words: `BitVec::get` costs several times more in the jump analyses. - let bits = usize::BITS as usize; - pc < self.jumpdests.len() - && self.jumpdests.as_raw_slice()[pc / bits] >> (pc % bits) & 1 != 0 + pub(crate) const fn is_valid_jump(&self, pc: usize) -> bool { + self.jumpdests.is_valid(pc) } /// Returns `true` if the bytecode has dynamic jumps. @@ -1321,37 +1319,6 @@ bitflags::bitflags! { } } -/// Computes the legacy `JUMPDEST` analysis bitmap: a `0x5b` byte is a valid jump -/// target unless it is part of the data of a `PUSH1..=PUSH32` instruction. -/// -/// This is the analysis of every fork: EIP-8024 explicitly leaves it unchanged, so -/// DUPN/SWAPN/EXCHANGE immediates are scanned as if they were opcodes. -fn legacy_jumpdests(code: &[u8]) -> BitVec { - let mut jumpdests = BitVec::repeat(false, code.len()); - let mut pc = 0; - while pc < code.len() { - let opcode = code[pc]; - if opcode == op::JUMPDEST { - jumpdests.set(pc, true); - } - pc += 1; - if let op::PUSH1..=op::PUSH32 = opcode { - pc += (opcode - op::PUSH1 + 1) as usize; - } - } - jumpdests -} - -fn bitvec_as_bytes( - bitvec: &BitVec, -) -> &[u8] { - slice_as_bytes(bitvec.as_raw_slice()) -} - -const fn slice_as_bytes(a: &[T]) -> &[u8] { - unsafe { std::slice::from_raw_parts(a.as_ptr().cast(), std::mem::size_of_val(a)) } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/jit/codegen/src/bytecode/opcode.rs b/crates/jit/codegen/src/bytecode/opcode.rs index 0ae100db8..c34edc8a1 100644 --- a/crates/jit/codegen/src/bytecode/opcode.rs +++ b/crates/jit/codegen/src/bytecode/opcode.rs @@ -5,6 +5,8 @@ use evm2::{ }; use std::{fmt, slice}; +pub use evm2::interpreter::instructions::{decode_pair, decode_single, encode_pair, encode_single}; + /// A bytecode iterator that yields opcodes and their immediate data, alongside the program counter. /// /// Created by calling [`OpcodesIter::with_pc`]. @@ -220,60 +222,6 @@ pub(crate) fn compute_stack_io(op: u8, immediate: Option<&[u8]>) -> Option<(u8, } } -/// Decodes a DUPN/SWAPN immediate byte into a stack index. -/// -/// Returns `None` if the immediate is in the invalid range `[91, 127]`. -pub const fn decode_single(x: u8) -> Option { - if x <= 90 || x >= 128 { Some(x.wrapping_add(145)) } else { None } -} - -/// Encodes a stack index into a DUPN/SWAPN immediate byte. -/// -/// Returns `None` if the value is outside the valid range. -pub fn encode_single(n: u8) -> Option { - let x = n.wrapping_sub(145); - if decode_single(x) == Some(n) { Some(x) } else { None } -} - -/// Decodes an EXCHANGE immediate byte into a pair of stack indices `(n, m)`. -/// -/// Returns `None` if the value is outside the valid range. -pub const fn decode_pair(x: u8) -> Option<(u8, u8)> { - if x > 81 && x < 128 { - return None; - } - let k = (x ^ 143) as u16; - let q = (k / 16) as u8; - let r = (k % 16) as u8; - if q < r { Some((q + 1, r + 1)) } else { Some((r + 1, 29 - q)) } -} - -/// Encodes a pair of stack indices into an EXCHANGE immediate byte. -/// -/// Returns `None` if the pair cannot be encoded. -pub fn encode_pair(n: u8, m: u8) -> Option { - if n == 0 || m == 0 { - return None; - } - // Try both (q,r) orderings and check round-trip. - let try_k = |q: u8, r: u8| -> Option { - if q >= 16 || r >= 16 { - return None; - } - let k = q as u16 * 16 + r as u16; - let x = (k as u8) ^ 143; - if decode_pair(x) == Some((n, m)) { Some(x) } else { None } - }; - - // Case 1: q < r → n = q+1, m = r+1. - if n < m { - try_k(n - 1, m - 1) - } else { - // Case 2: q >= r → n = r+1, m = 29-q. - if let Some(q) = 29u8.checked_sub(m) { try_k(q, n - 1) } else { None } - } -} - /// Returns a string representation of the given bytecode. pub fn format_bytecode(bytecode: &[u8], spec_id: SpecId) -> String { let mut w = String::new(); @@ -335,64 +283,4 @@ mod tests { let s = format_bytecode(&bytecode, DEF_SPEC); assert_eq!(s, "PUSH0 PUSH1 0x69 PUSH2 0x0102"); } - - #[test] - fn encode_decode_single_roundtrip() { - for x in 0..=255u8 { - if let Some(n) = decode_single(x) { - assert_eq!(encode_single(n), Some(x), "roundtrip failed for x={x}, n={n}"); - } - } - for n in 0..=255u8 { - if let Some(x) = encode_single(n) { - assert_eq!(decode_single(x), Some(n), "roundtrip failed for n={n}, x={x}"); - } - } - // Out-of-range: values in [236,255] ∪ [0,16] have no valid encoding. - assert_eq!(encode_single(0), None); - assert_eq!(encode_single(16), None); - assert_eq!(encode_single(236), None); - // Invalid raw bytes. - assert_eq!(decode_single(91), None); - assert_eq!(decode_single(127), None); - } - - #[test] - fn encode_decode_pair_roundtrip() { - // encode → decode is always exact. - for n in 1..=30u8 { - for m in 1..=30u8 { - if let Some(x) = encode_pair(n, m) { - assert_eq!( - decode_pair(x), - Some((n, m)), - "roundtrip failed for (n,m)=({n},{m}), x={x}" - ); - } - } - } - // decode → encode: some pairs have multiple encodings, so the raw byte may differ, - // but decoding the result must produce the same pair. - // Some decoded pairs may have values too large to re-encode. - for x in 0..=255u8 { - if let Some((n, m)) = decode_pair(x) - && let Some(re) = encode_pair(n, m) - { - assert_eq!( - decode_pair(re), - Some((n, m)), - "decode roundtrip failed for x={x}, re={re}, (n,m)=({n},{m})" - ); - } - } - // Zero indices are invalid. - assert_eq!(encode_pair(0, 1), None); - assert_eq!(encode_pair(1, 0), None); - // Invalid raw bytes: [82, 127]. - assert_eq!(decode_pair(82), None); - assert_eq!(decode_pair(127), None); - // Edge: 80 and 81 are now valid. - assert!(decode_pair(80).is_some()); - assert!(decode_pair(81).is_some()); - } } diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index aab805033..d8d7b03ed 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -61,7 +61,7 @@ use crate::{ use alloy_primitives::{U256, map::FxHasher}; use bitvec::vec::BitVec; use either::Either; -use evm2::{constants::STACK_LIMIT, interpreter::op}; +use evm2::{bytecode::JumpTable, constants::STACK_LIMIT, interpreter::op}; use oxc_index::IndexVec; use smallvec::SmallVec; use std::{ @@ -172,22 +172,22 @@ impl AbsValue { struct ConstSetInterner { interner: Interner>, /// `JUMPDEST` map: sets only keep valid jump targets. - jumpdests: BitVec, + jumpdests: JumpTable, /// Number of constants merged by [`Self::join`], for the fixpoint budget. merged: u64, } impl ConstSetInterner { - fn new(jumpdests: BitVec) -> Self { + fn new(jumpdests: JumpTable) -> Self { Self { interner: Interner::new(), jumpdests, merged: 0 } } /// Returns `true` if `value` may be part of a set. fn is_set_member(&self, value: AbsValue) -> bool { match value { - AbsValue::Const(imm) => imm - .get_inline() - .is_some_and(|pc| self.jumpdests.get(pc as usize).is_some_and(|bit| *bit)), + AbsValue::Const(imm) => { + imm.get_inline().is_some_and(|pc| self.jumpdests.is_valid(pc as usize)) + } AbsValue::ConstSet(_) => true, AbsValue::Range(..) | AbsValue::Top => false, } @@ -882,7 +882,7 @@ impl Bytecode<'_> { pub(crate) fn block_analysis_local(&mut self) { self.init_snapshots(); - let empty_sets = ConstSetInterner::new(BitVec::new()); + let empty_sets = ConstSetInterner::new(JumpTable::default()); let mut resolved = Vec::new(); let mut stack = Vec::new(); let trace_logs = enabled!(tracing::Level::TRACE); @@ -1382,7 +1382,9 @@ impl Bytecode<'_> { let end = (last + 1).min(self.jumpdests.len()); let start = first.min(end); (last - first < MAX_RANGE_JUMP_WIDTH).then(|| { - self.jumpdests[start..end].iter_ones().map(move |i| self.pc_to_inst(start + i)) + (start..end) + .filter(move |&pc| self.jumpdests.is_valid(pc)) + .map(move |pc| self.pc_to_inst(pc)) }) } From 85395c5440bef1f685142e79c66051014f46f9b5 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:35:36 +0000 Subject: [PATCH 127/159] refactor: move function-local `use`s to module scope (#1017) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Moved function-local `use`s to module scope (or the enclosing `mod tests`), per slapslop `fn_local_use`; redundant ones already covered by `use super::*` / top-level imports are just removed - Fixed 53 of 58 hit locations (69 raw hits, incl. duplicates across test/non-test builds) - Kept enum renames and variant imports function-local (false positives of the lint): `crates/jit/llvm/src/lib.rs` — `use evm2_jit_backend::Attribute as OurAttr` in `convert_attribute` - Left (hazy, would hurt readability or shadow names module-wide): - `crates/jit/codegen/src/bytecode/fmt.rs:254` — `use op::*` glob would shadow module names - `crates/jit/codegen/src/bytecode/fmt.rs:342` — `use dot_colors::*` glob would shadow module names - `crates/jit/codegen/src/bytecode/mod.rs:821` — `use op::*` glob would shadow module names - `crates/eest/src/blockchaintest/execute.rs:1516` — `use Activation::{Block, Timestamp}` enum variants; `Block` clashes with `types::Block` - Checked: `warmd check` (clippy, fmt, slapslop `fn_local_use`): CI ok --- crates/cli/src/replay/mod.rs | 6 +- crates/eest/src/blockchaintest/execute.rs | 75 ++----------------- crates/evm2/src/ethereum/mod.rs | 4 +- crates/evm2/src/evm/mod.rs | 1 - crates/evm2/src/evm/state/account.rs | 20 ++--- crates/evm2/src/evm/state/block.rs | 7 +- crates/evm2/src/evm/state/journal.rs | 4 +- crates/evm2/src/precompiles/crypto.rs | 3 +- crates/evm2/src/tests.rs | 8 -- crates/inspectors/src/tracing/js/bindings.rs | 3 +- crates/inspectors/src/tracing/writer.rs | 3 +- crates/inspectors/tests/it/geth.rs | 3 +- crates/inspectors/tests/it/parity.rs | 1 - crates/jit/codegen/src/bytecode/mod.rs | 7 +- .../codegen/src/bytecode/passes/const_fold.rs | 1 - crates/jit/llvm/src/orc.rs | 13 +--- 16 files changed, 26 insertions(+), 133 deletions(-) diff --git a/crates/cli/src/replay/mod.rs b/crates/cli/src/replay/mod.rs index 58caa6e7a..0f3734847 100644 --- a/crates/cli/src/replay/mod.rs +++ b/crates/cli/src/replay/mod.rs @@ -466,9 +466,10 @@ fn ggas(gas_used: u128) -> f64 { #[cfg(test)] mod collect_tests { - use super::{PathBuf, collect_json_files}; + use super::{NameFilter, PathBuf, collect_json_files, file_selected}; use std::{ fs, + path::Path, sync::atomic::{AtomicUsize, Ordering}, }; @@ -509,9 +510,6 @@ mod collect_tests { #[test] fn file_filter_matches_path_or_file_name() { - use super::{NameFilter, file_selected}; - use std::path::Path; - let path = Path::new("test-fixtures/frontier/create/create_one_byte.json"); // An empty filter selects everything. diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 07b57e3b9..b584c32b7 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -53,7 +53,13 @@ use evm2::{ }; #[cfg(feature = "jit")] use std::path::PathBuf; -use std::{borrow::Cow, collections::BTreeMap, mem, path::Path}; +use std::{ + borrow::Cow, + collections::{BTreeMap, BTreeSet}, + fmt::Write, + mem, + path::Path, +}; pub use crate::execution::ExecutionMode; @@ -1296,11 +1302,6 @@ fn check_block_access_list( /// Renders a compact per-account summary of how the built and expected block access lists differ. fn format_bal_diff(built: &BlockAccessList, expected: &BlockAccessList) -> String { - use std::{ - collections::{BTreeMap, BTreeSet}, - fmt::Write, - }; - let built: BTreeMap<_, _> = built.iter().map(|account| (account.address(), account)).collect(); let expected: BTreeMap<_, _> = expected.iter().map(|account| (account.address(), account)).collect(); @@ -1577,7 +1578,7 @@ const fn fork_schedule(fork: ForkSpec) -> Option { #[cfg(test)] mod tests { use super::{ - super::types::{ChainConfig, SealEngine, State}, + super::types::{ChainConfig, DecodedBlock, SealEngine, State}, *, }; use alloy_eips::eip7840::BLOB_BASE_COST; @@ -1823,17 +1824,6 @@ mod tests { #[test] fn blockchain_tests_apply_ommer_rewards_before_merge() { - use super::{ - super::types::{ - Account, Block, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, ONE_ETHER, execute_suite, - }; - use crate::filter::NameFilter; - use alloy_primitives::{Address, B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let miner = Address::with_last_byte(1); let ommer_miner = Address::with_last_byte(2); let genesis_hash = B256::with_last_byte(1); @@ -1922,16 +1912,6 @@ mod tests { #[test] fn receipt_root_check_rejects_invalid_empty_block_root() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, ExecutionMode, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let suite = BlockchainTest(BTreeMap::from([( "invalid-receipt-root".to_string(), @@ -2008,16 +1988,6 @@ mod tests { #[test] fn rejects_block_with_wrong_parent_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let suite = BlockchainTest(BTreeMap::from([( @@ -2067,16 +2037,6 @@ mod tests { #[test] fn discards_expected_block_with_wrong_parent_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let suite = BlockchainTest(BTreeMap::from([( "expected-wrong-parent-hash".to_string(), @@ -2121,16 +2081,6 @@ mod tests { #[test] fn rejects_case_with_wrong_last_block_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let suite = BlockchainTest(BTreeMap::from([( @@ -2324,15 +2274,6 @@ mod tests { #[test] fn json_outcome_reports_pass_and_failure() { - use super::{ - super::{ - error::{TestError, TestErrorKind}, - types::ForkSpec, - }, - json_outcome_value, - }; - use alloy_primitives::B256; - let root = B256::with_last_byte(7); let pass = json_outcome_value("case-a", ForkSpec::Byzantium, &Ok(Some(root))); assert_eq!(pass["pass"], true); diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index cb96577eb..7688b4ca6 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -985,7 +985,7 @@ mod tests { BaseEvmTypes, ExecutionConfig, Precompiles, constants::MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, env::BlockEnvExt, - evm::InMemoryDB, + evm::{InMemoryDB, handler::TxHandlerHooks}, interpreter::{Host, InstrStop, op}, registry::TxRegistry, }; @@ -997,8 +997,6 @@ mod tests { fn adjusted_floor_gas_controls_validation_and_settlement( #[values(31_049, 31_050)] gas_limit: u64, ) { - use crate::evm::handler::TxHandlerHooks; - struct CalldataSurcharge; impl TxHandlerHooks for CalldataSurcharge { diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index e10fe3fea..17d32bac4 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -2482,7 +2482,6 @@ mod tests { #[test] fn owned_database_error_survives_nested_precompile_calls() { - use crate::{DatabaseError, evm::bal::BalError}; let precompiles = precompiles_with([ test_precompile(TEST_PRECOMPILE, |evm, _, _| { let mut child = precompile_message(INNER_TEST_PRECOMPILE); diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index df827c60d..1e8eeb94c 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -723,8 +723,11 @@ impl<'a, 'db> AccountHandle<'a, 'db> { #[cfg(test)] mod tests { use super::*; - use crate::evm::{CacheDB, state::State}; - use alloy_primitives::Address; + use crate::{ + LoadError, SpecId, Version, + evm::{CacheDB, state::State}, + }; + use alloy_primitives::{Address, Bytes}; #[test] fn transfer_to_self_requires_balance() { @@ -739,8 +742,6 @@ mod tests { #[test] fn journaled_account_mutations_journal_and_roll_back() { - use crate::{SpecId, Version, bytecode::Bytecode}; - let address = Address::repeat_byte(0x88); let mut state = State::new(CacheDB::default()); @@ -775,8 +776,6 @@ mod tests { #[test] fn account_info_set_code_is_published_on_commit() { - use alloy_primitives::Bytes; - let address = Address::repeat_byte(0x8c); let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let mut state = State::new(CacheDB::default()); @@ -790,8 +789,6 @@ mod tests { #[test] fn changed_code_requires_new_non_empty_code_hash() { - use alloy_primitives::Bytes; - let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let with_code = empty_account().with_code(code.clone()); let other = empty_account().with_code(Bytecode::new_raw(Bytes::from_static(&[0]))); @@ -812,8 +809,6 @@ mod tests { #[test] fn loaded_code_is_seeded_with_account_code_hash() { - use alloy_primitives::Bytes; - // A hash that is not the keccak of the code proves `hash_slow` returns the seeded value // instead of hashing. let seed = B256::repeat_byte(0x5e); @@ -836,9 +831,6 @@ mod tests { #[test] fn journaled_existing_account_field_changes_roll_back_granularly() { - use crate::{SpecId, Version, bytecode::Bytecode}; - use alloy_primitives::Bytes; - let address = Address::repeat_byte(0x8b); let original_code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let original_code_hash = original_code.hash_slow(); @@ -893,8 +885,6 @@ mod tests { #[test] fn journaled_account_skip_cold_load_signals_skip() { - use crate::LoadError; - let address = Address::repeat_byte(0x8a); let mut database = CacheDB::default(); database.insert_account_info(&address, empty_account().with_balance(Word::from(5))); diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 665966bef..e0c79b6f8 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -278,17 +278,12 @@ mod tests { use core::convert::Infallible; use super::super::StateChangeSink; - #[cfg(feature = "serde")] use crate::bytecode::Bytecode; use alloc::{vec, vec::Vec}; - #[cfg(feature = "serde")] - use alloy_primitives::B256; + use alloy_primitives::{B256, Bytes}; #[test] fn code_sorted_orders_by_hash() { - use crate::bytecode::Bytecode; - use alloy_primitives::{B256, Bytes}; - let mut block = BlockStateAccumulator::new(); // Inserted descending, so map order cannot be mistaken for sorted order. for byte in [0xcc_u8, 0x11, 0x77] { diff --git a/crates/evm2/src/evm/state/journal.rs b/crates/evm2/src/evm/state/journal.rs index 72bdd30b6..64a3b767d 100644 --- a/crates/evm2/src/evm/state/journal.rs +++ b/crates/evm2/src/evm/state/journal.rs @@ -87,7 +87,7 @@ mod tests { }, }; use alloc::vec::Vec; - use alloy_primitives::Log; + use alloy_primitives::{Bytes, Log}; #[test] fn destruct_change_rolls_back_to_checkpoint() { @@ -104,8 +104,6 @@ mod tests { #[test] fn log_rolls_back_to_checkpoint() { - use alloy_primitives::Bytes; - let mut state = State::new(CacheDB::default()); let kept = Log::new_unchecked(Address::repeat_byte(0x44), Vec::new(), Bytes::from_static(&[0x01])); diff --git a/crates/evm2/src/precompiles/crypto.rs b/crates/evm2/src/precompiles/crypto.rs index 9c7dc94f5..aab72f023 100644 --- a/crates/evm2/src/precompiles/crypto.rs +++ b/crates/evm2/src/precompiles/crypto.rs @@ -7,13 +7,13 @@ use crate::{ }; use alloc::{boxed::Box, vec::Vec}; use core::fmt::Debug; +use sha2::Digest; /// Crypto operations trait for precompiles. pub trait Crypto: Send + Sync + Debug { /// Compute SHA-256 hash #[inline] fn sha256(&self, input: &[u8]) -> [u8; 32] { - use sha2::Digest; let output = sha2::Sha256::digest(input); output.into() } @@ -21,7 +21,6 @@ pub trait Crypto: Send + Sync + Debug { /// Compute RIPEMD-160 hash #[inline] fn ripemd160(&self, input: &[u8]) -> [u8; 32] { - use ripemd::Digest; let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 40cac2fc0..8d829d940 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -163,14 +163,6 @@ fn evm_reports_invalid_transaction_execution() { #[test] fn eip8037_sibling_refill_restores_parent_gas_left() { - use crate::{ - EvmFeatures, ExecutionConfig, Version, - ethereum::{TxEnvelope, ethereum_tx_registry}, - version::GasId, - }; - use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{TxKind, U256}; - let parent = Address::repeat_byte(0x11); let setter = Address::repeat_byte(0xa1); let clearer = Address::repeat_byte(0xb2); diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 13f89f010..06d2ac9f3 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -1119,6 +1119,7 @@ mod tests { use boa_engine::{Source, object::builtins::JsUint8Array}; use core::convert::Infallible; use evm2::evm::{Database, Db, InMemoryDB}; + use std::panic::{AssertUnwindSafe, catch_unwind}; /// Records and enters a step with the given pre- and post-execution stack. fn with_step<'a, R>( @@ -1525,8 +1526,6 @@ mod tests { #[test] fn test_scopes_revoke_access_on_return_error_and_unwind() { - use std::panic::{AssertUnwindSafe, catch_unwind}; - let mut ctx = Context::default(); register_builtins(&mut ctx).unwrap(); let log = ReusableStepLog::new(&mut ctx, OpcodeNames::new()).unwrap(); diff --git a/crates/inspectors/src/tracing/writer.rs b/crates/inspectors/src/tracing/writer.rs index 7b1f8b130..81edc4fd9 100644 --- a/crates/inspectors/src/tracing/writer.rs +++ b/crates/inspectors/src/tracing/writer.rs @@ -10,7 +10,7 @@ use alloy_primitives::{Address, B256, U256, address, hex, map::HashMap}; use anstyle::{AnsiColor, Color, Style}; use colorchoice::ColorChoice; use evm2::interpreter::InstrStop; -use std::io::{self, Write}; +use std::io::{self, IsTerminal, Write}; const CHEATCODE_ADDRESS: Address = address!("7109709ECfa91a80626fF3989D68f67F5b1DD12D"); @@ -554,7 +554,6 @@ impl TraceWriter { } fn use_colors(choice: ColorChoice) -> bool { - use io::IsTerminal; match choice { ColorChoice::Auto => io::stdout().is_terminal(), ColorChoice::AlwaysAnsi | ColorChoice::Always => true, diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 23fe1c64b..6cc2e6704 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -35,6 +35,7 @@ use evm2_inspectors::tracing::{ Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimitBehavior, TraceLimits, TracingInspector, TracingInspectorConfig, }; +use serde_json::{Value, json}; use std::collections::{BTreeMap, BTreeSet}; #[test] @@ -2100,8 +2101,6 @@ fn test_geth_calltracer_logs_delegatecall() { /// Exercise JSON options, tracer dispatch, EVM execution, and serialized trace output together. #[test] fn test_geth_opcode_limit_end_to_end() { - use serde_json::{Value, json}; - let account = address!("1000000000000000000000000000000000000001"); let child = Address::with_last_byte(0xff); for terminal in [op::RETURN, op::REVERT] { diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index c3aa9f278..948dc6945 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -487,7 +487,6 @@ fn inspect_funded_code( balance: u64, gas_limit: u64, ) -> TracingInspector { - use crate::utils::Bytecode; let target = Address::with_last_byte(0x42); let child_address = Address::with_last_byte(0x43); let mut db = InMemoryDB::default(); diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index b258a41a9..9a4568e35 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -11,7 +11,7 @@ use evm2::{ use evm2_jit_backend::{Result, eyre::bail}; use oxc_index::IndexVec; use smallvec::SmallVec; -use std::{borrow::Cow, cell::RefCell}; +use std::{borrow::Cow, cell::RefCell, fmt::Write}; mod passes; pub(crate) use passes::{ @@ -819,7 +819,6 @@ impl<'a> Bytecode<'a> { #[inline(never)] fn log_const_input_stats(&self) { use op::*; - use std::fmt::Write; // per_input[depth] = [total, const_count, fits_usize_count]. struct Entry { @@ -944,8 +943,6 @@ impl<'a> Bytecode<'a> { /// Logs IR statistics and top 5 longest blocks at trace level. #[inline(never)] fn log_ir_stats(&self) { - use std::fmt::Write; - let s = self.ir_stats(); trace!( total_insts = s.total, @@ -978,8 +975,6 @@ impl<'a> Bytecode<'a> { /// Returns the name for a basic block. pub(crate) fn op_block_name(&self, inst: Option, name: &str) -> String { - use std::fmt::Write; - let Some(inst) = inst else { return format!("entry.{name}"); }; diff --git a/crates/jit/codegen/src/bytecode/passes/const_fold.rs b/crates/jit/codegen/src/bytecode/passes/const_fold.rs index f74e12327..497a39a66 100644 --- a/crates/jit/codegen/src/bytecode/passes/const_fold.rs +++ b/crates/jit/codegen/src/bytecode/passes/const_fold.rs @@ -479,7 +479,6 @@ mod tests { /// Operands are in natural order matching the yellow paper specification: /// for `SUB(a, b)` = `a - b`, pass `&[a, b]` (first popped = first element). fn const_fold(opcode: u8, operands: &[U256]) -> Option { - use std::fmt::Write; let mut src = String::new(); let mut num_insts = 0usize; // Push in reverse so that operands[0] ends up on TOS. diff --git a/crates/jit/llvm/src/orc.rs b/crates/jit/llvm/src/orc.rs index aecc6434d..67d480436 100644 --- a/crates/jit/llvm/src/orc.rs +++ b/crates/jit/llvm/src/orc.rs @@ -16,8 +16,8 @@ use crate::wrap::{Context, LLVMString, Module, TargetMachine}; use llvm_sys::{ core::{ - LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, - LLVMModuleCreateWithNameInContext, + LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMDisposeMemoryBuffer, + LLVMGetBufferSize, LLVMGetBufferStart, LLVMModuleCreateWithNameInContext, }, error::*, orc2::{lljit::*, *}, @@ -1570,10 +1570,6 @@ impl ObjectTransformLayerRef { /// Returning `Err` aborts materialization. pub fn set_transform(&self, f: ObjTransformFn) { extern "C" fn shim(ctx: *mut c_void, obj_in_out: *mut LLVMMemoryBufferRef) -> LLVMErrorRef { - use llvm_sys::core::{ - LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMDisposeMemoryBuffer, - LLVMGetBufferSize, LLVMGetBufferStart, - }; let f: ObjTransformFn = unsafe { mem::transmute(ctx) }; let buf = unsafe { *obj_in_out }; let data = unsafe { @@ -1801,11 +1797,10 @@ mod tests { core::LLVMGetModuleContext, target_machine::{LLVMCodeGenOptLevel, LLVMCodeModel, LLVMRelocMode}, }; + use std::mem::ManuallyDrop; #[test] fn e2e_with_tsc() { - use mem::ManuallyDrop; - Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); @@ -1895,8 +1890,6 @@ mod tests { #[test] fn lookup_in_custom_jd() { - use mem::ManuallyDrop; - Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); From 031d5c82fcae3a813a730b00eb3492b04cfcdd7e Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:59:09 +0000 Subject: [PATCH 128/159] chore: replace Option matches with combinators (#1019) - Replace `Option` `match`/`if let` expressions with std combinators (`map`, `unwrap_or_else`, `expect`, `and_then`) per slapslop `match_on_option` - Fixed 4 of 5 hits across 3 files; `take_result` is now `const fn` (clippy `missing_const_for_fn`) - Left: `crates/evm2/src/interpreter/opcode.rs` `OpCode::info` is a `const fn`; `unwrap_or_else`/`unwrap_or` aren't usable in const - Checked: warmd check (fmt, clippy, slapslop): CI ok --- crates/evm2/src/evm/tx.rs | 7 ++----- .../src/interpreter/instructions/stack.rs | 2 +- crates/jit/llvm/src/lib.rs | 19 +++++++------------ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/crates/evm2/src/evm/tx.rs b/crates/evm2/src/evm/tx.rs index c9dcc3f50..eeac119ef 100644 --- a/crates/evm2/src/evm/tx.rs +++ b/crates/evm2/src/evm/tx.rs @@ -216,11 +216,8 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { } #[inline] - fn take_result(&mut self) -> TxResult { - match self.result.take() { - Some(result) => result, - None => unreachable!("executed transaction result was already taken"), - } + const fn take_result(&mut self) -> TxResult { + self.result.take().expect("executed transaction result was already taken") } #[inline] diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index c8dd9ca77..65bdc01d5 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -94,7 +94,7 @@ pub fn encode_pair(n: u8, m: u8) -> Option { try_k(n - 1, m - 1) } else { // Case 2: q >= r → n = r+1, m = 29-q. - if let Some(q) = 29u8.checked_sub(m) { try_k(q, n - 1) } else { None } + 29u8.checked_sub(m).and_then(|q| try_k(q, n - 1)) } } diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 05d5d874c..1abd50574 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -936,17 +936,15 @@ impl Backend for EvmLlvmBackend { // Attach debug info subprogram if debug is active. self.ensure_di_state(); - let debug_scope = if let Some(di) = &self.di_state { + let debug_scope = self.di_state.as_ref().map(|di| { let subprogram = di.dibuilder.create_subprogram( di.compile_unit, name, self.backend_config.opt_level != OptimizationLevel::None, ); function.set_subprogram(subprogram); - Some(subprogram) - } else { - None - }; + subprogram + }); let builder = EvmLlvmBuilder { backend: self, function, debug_scope }; Ok((builder, id)) @@ -1198,13 +1196,10 @@ impl EvmLlvmBuilder<'_> { name: &str, mk_ty: impl FnOnce(&mut Self) -> Type, ) -> Function { - match self.module().get_function(name) { - Some(function) => function, - None => { - let ty = mk_ty(self); - self.module().add_function(name, ty, None) - } - } + self.module().get_function(name).unwrap_or_else(|| { + let ty = mk_ty(self); + self.module().add_function(name, ty, None) + }) } fn set_branch_weights(&self, inst: Inst, weights: impl IntoIterator) { From 09429048326ffea7fa589ebbc86b88bfc6be3935 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:13:56 +0100 Subject: [PATCH 129/159] feat(jit): trap on unreachable in compiled code (#1022) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `RETURN`, `REVERT`, and `SELFDESTRUCT` call builtins that always leave through `evm2_jit_exit`, so codegen ends their blocks with `unreachable`. LLVM emits nothing after such a call, and the return address points at whatever block comes next: in `erc20_transfer`, `call __revmc_builtin_do_return` is directly followed by an unrelated `JUMPDEST` block. If one of these builtins ever returned, through a bug or a host that changes it, compiled code would keep running EVM code with the wrong stack and gas instead of failing. This passes `-trap-unreachable` to LLVM, as rustc does by default, so these paths end in `ud2`. The trap never runs on correct paths; tail merging of the calls even shrinks a few functions. ## Benchmarks JIT size and compile instructions +0.1% geomean, `TOTAL` runtime instructions unchanged; the per-benchmark runtime deltas are within the noise seen on unchanged code. Head `031d5c82fc` with uncommitted changes vs base `origin/main` (`031d5c82fc`). ### Codegen statistics `opt.s` changed in 31/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|------------:|------------:|-------------:|--------------:|----------:|---------:|----------:| | push0_proxy | = | = | +0.7% 🔴 | +1.1% 🔴 | = | = | - | - | - | | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **+0.1% 🔴** | **+0.1% 🔴** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 329
329
= | 114
114
= | 355
356
+0.3% 🔴 | 348 B
350 B
+0.6% 🔴 | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 325
325
= | 116
116
= | 391
392
+0.3% 🔴 | 478 B
480 B
+0.4% 🔴 | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 986
986
= | 341
341
= | 603
604
+0.2% 🔴 | 785 B
781 B
-0.5% 🟢 | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 59703
59703
= | 26888
26888
= | 33501
33502
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11558
11558
= | 2840
2840
= | 4398
4401
+0.1% 🔴 | 13.4 KiB
13.4 KiB
-0.1% 🟢 | 188
188
= | 237
237
= | 25
25
= | 13
13
= | 9
9
= | | hash_10k | 982
982
= | 354
354
= | 670
672
+0.3% 🔴 | 1.4 KiB
1.4 KiB
+0.3% 🔴 | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12026
12026
= | 3815
3815
= | 4975
4977
= | 14.0 KiB
14.0 KiB
= | 160
160
= | 324
324
= | 53
53
= | 8
8
= | 7
7
= | | push0_proxy | 307
307
= | 125
125
= | 286
288
+0.7% 🔴 | 350 B
354 B
+1.1% 🔴 | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6047
6047
= | 1581
1581
= | 2277
2279
+0.1% 🔴 | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 7
7
= | 6
6
= | | fiat_token | 75310
75310
= | 21440
21440
= | 29239
29231
= | 92.8 KiB
92.8 KiB
-0.1% 🟢 | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41568
41568
= | 12037
12037
= | 16688
16686
= | 52.4 KiB
52.4 KiB
-0.1% 🟢 | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 433
433
= | | univ2_router | 80165
80165
= | 24414
24414
= | 32740
32735
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
437
+0.2% 🔴 | | seaport | 132264
132264
= | 51832
51832
= | 68657
68636
= | 233.6 KiB
233.5 KiB
-0.1% 🟢 | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26516
26516
= | 8424
8424
= | 10800
10797
= | 30.7 KiB
30.7 KiB
-0.1% 🟢 | 432
432
= | 667
667
= | 91
91
= | 38
38
= | 42
42
= | | bswap64 | 1751
1751
= | 361
361
= | 622
624
+0.3% 🔴 | 951 B
955 B
+0.4% 🔴 | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1490
1490
= | 346
346
= | 632
634
+0.3% 🔴 | 1008 B
1012 B
+0.4% 🔴 | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 625
625
= | 249
249
= | 459
461
+0.4% 🔴 | 670 B
674 B
+0.6% 🔴 | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 482
482
= | 190
190
= | 420
421
+0.2% 🔴 | 554 B
556 B
+0.4% 🔴 | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9152
9152
= | 3038
3038
= | 3858
3860
+0.1% 🔴 | 11.7 KiB
11.7 KiB
-0.1% 🟢 | 165
165
= | 282
282
= | 26
26
= | 5
5
= | 5
5
= | | curve_stableswap | 16328
16328
= | 5692
5692
= | 7043
7045
= | 20.0 KiB
20.0 KiB
= | 324
324
= | 506
506
= | 44
44
= | 10
10
= | 13
13
= | | onchain_lm_v2 | 53109
53109
= | 17441
17441
= | 25264
25265
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34549
34549
= | 8005
8005
= | 10792
10794
= | 31.7 KiB
31.7 KiB
= | 473
473
= | 657
657
= | 52
52
= | 41
41
= | 51
51
= | | universal_router | 77412
77412
= | 29450
29450
= | 36915
36913
= | 121.0 KiB
120.9 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 124769
124769
= | 45234
45234
= | 52441
52428
= | 171.4 KiB
171.3 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66480
66480
= | 25010
25010
= | 31306
31289
-0.1% 🟢 | 103.9 KiB
103.8 KiB
-0.1% 🟢 | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 116922
116922
= | 33907
33907
= | 44303
44292
= | 146.1 KiB
146.0 KiB
-0.1% 🟢 | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93186
93186
= | 32124
32124
= | 42527
42516
= | 141.6 KiB
141.5 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 104646
104646
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 113933
113933
= | 31974
31974
= | 49484
49486
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145361
145361
= | 51478
51478
= | 64838
64836
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85681
85681
= | 26294
26294
= | 38378
38368
= | 117.3 KiB
117.2 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1493962
1493962
=** | **495704
495704
=** | **662166
662092
=** | **2.1 MiB
2.1 MiB
=** | **32200
32200
=** | **42287
42287
=** | **5830
5830
=** | **2518
2518
=** | **19794
19795
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **+0.1% 🔴** | **-6.1% 🟢** | **-7.5% 🟢** | | **GEOMEAN** | **+0.1% 🔴** | **-1.1% 🟢** | **-0.6% 🟢** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.49M
37.50M
= | 40.38M
38.23M
-5.3% 🟢 | 12.1ms
12.3ms
+2.2% 🔴 | 172.0µs
189.5µs
+10.2% 🔴 | 358.1µs
362.5µs
+1.2% 🔴 | 1.51ms
1.32ms
-12.4% 🟢 | 2.83ms
2.82ms
-0.4% 🟢 | | factorial | 41.62M
41.65M
+0.1% 🔴 | 41.22M
41.39M
+0.4% 🔴 | 12.5ms
12.3ms
-1.7% 🟢 | 166.6µs
192.4µs
+15.5% 🔴 | 309.0µs
320.5µs
+3.7% 🔴 | 1.11ms
1.18ms
+6.8% 🔴 | 3.00ms
4.36ms
+45.1% 🔴 | | counter | 54.11M
54.18M
+0.1% 🔴 | 39.42M
45.88M
+16.4% 🔴 | 9.38ms
13.2ms
+40.2% 🔴 | 304.1µs
336.3µs
+10.6% 🔴 | 405.0µs
456.4µs
+12.7% 🔴 | 1.42ms
1.72ms
+21.2% 🔴 | 3.59ms
4.17ms
+16.1% 🔴 | | snailtracer | 5.24G
5.24G
= | 2.46G
2.06G
-16.2% 🟢 | 478.2ms
397.2ms
-16.9% 🟢 | 32.1ms
32.5ms
+1.3% 🔴 | 6.64ms
6.80ms
+2.5% 🔴 | 65.8ms
68.4ms
+3.9% 🔴 | 299.8ms
312.5ms
+4.2% 🔴 | | weth | 505.11M
505.14M
= | 248.35M
259.31M
+4.4% 🔴 | 51.9ms
52.9ms
+2.1% 🔴 | 2.18ms
2.26ms
+3.6% 🔴 | 1.85ms
1.95ms
+5.2% 🔴 | 8.79ms
9.03ms
+2.7% 🔴 | 31.0ms
31.6ms
+1.9% 🔴 | | hash_10k | 80.33M
80.40M
+0.1% 🔴 | 51.61M
58.17M
+12.7% 🔴 | 12.8ms
13.7ms
+6.4% 🔴 | 307.7µs
276.8µs
-10.0% 🟢 | 416.0µs
422.4µs
+1.5% 🔴 | 1.78ms
1.83ms
+2.5% 🔴 | 4.97ms
5.23ms
+5.3% 🔴 | | erc20_transfer | 562.74M
563.08M
+0.1% 🔴 | 281.56M
285.24M
+1.3% 🔴 | 57.2ms
60.0ms
+4.9% 🔴 | 2.29ms
2.27ms
-1.1% 🟢 | 1.88ms
1.89ms
+0.6% 🔴 | 9.89ms
9.87ms
-0.3% 🟢 | 35.5ms
34.2ms
-3.5% 🟢 | | push0_proxy | 34.62M
34.68M
+0.2% 🔴 | 33.06M
38.79M
+17.3% 🔴 | 11.8ms
13.4ms
+13.4% 🔴 | 182.6µs
163.1µs
-10.7% 🟢 | 310.6µs
287.5µs
-7.4% 🟢 | 866.5µs
909.7µs
+5.0% 🔴 | 2.15ms
2.09ms
-2.7% 🟢 | | usdc_proxy | 250.61M
250.96M
+0.1% 🔴 | 155.44M
150.26M
-3.3% 🟢 | 34.5ms
32.6ms
-5.5% 🟢 | 1.32ms
1.35ms
+2.0% 🔴 | 1.11ms
1.15ms
+3.1% 🔴 | 6.01ms
6.25ms
+3.9% 🔴 | 15.4ms
14.9ms
-3.4% 🟢 | | fiat_token | 3.97G
3.98G
+0.2% 🔴 | 1.98G
1.92G
-3.3% 🟢 | 397.0ms
377.9ms
-4.8% 🟢 | 14.2ms
13.9ms
-2.0% 🟢 | 10.00ms
9.86ms
-1.4% 🟢 | 69.8ms
67.4ms
-3.4% 🟢 | 243.8ms
246.1ms
+1.0% 🔴 | | uniswap_v2_pair | 2.11G
2.11G
= | 994.10M
918.74M
-7.6% 🟢 | 200.1ms
184.4ms
-7.8% 🟢 | 7.56ms
7.58ms
+0.3% 🔴 | 6.58ms
5.56ms
-15.5% 🟢 | 34.4ms
34.7ms
+0.8% 🔴 | 131.8ms
133.5ms
+1.3% 🔴 | | univ2_router | 4.38G
4.38G
+0.1% 🔴 | 2.21G
2.08G
-6.0% 🟢 | 437.1ms
417.5ms
-4.5% 🟢 | 15.1ms
15.3ms
+1.1% 🔴 | 10.2ms
10.2ms
-0.1% 🟢 | 68.4ms
67.1ms
-2.0% 🟢 | 280.9ms
283.6ms
+1.0% 🔴 | | seaport | 9.72G
9.73G
+0.1% 🔴 | 5.12G
5.42G
+6.0% 🔴 | 1.003s
1.071s
+6.8% 🔴 | 25.8ms
25.4ms
-1.5% 🟢 | 18.9ms
16.8ms
-10.9% 🟢 | 171.5ms
174.2ms
+1.6% 🔴 | 695.3ms
688.7ms
-0.9% 🟢 | | airdrop | 1.33G
1.33G
= | 582.27M
558.99M
-4.0% 🟢 | 117.2ms
114.8ms
-2.0% 🟢 | 5.17ms
4.61ms
-10.9% 🟢 | 3.73ms
3.79ms
+1.7% 🔴 | 22.5ms
22.3ms
-0.7% 🟢 | 83.1ms
81.9ms
-1.5% 🟢 | | bswap64 | 67.05M
67.09M
+0.1% 🔴 | 43.07M
44.87M
+4.2% 🔴 | 11.0ms
11.3ms
+2.6% 🔴 | 472.3µs
529.9µs
+12.2% 🔴 | 530.9µs
516.2µs
-2.8% 🟢 | 1.70ms
1.62ms
-4.8% 🟢 | 3.89ms
3.86ms
-0.7% 🟢 | | bswap64_opt | 73.64M
73.72M
+0.1% 🔴 | 49.39M
44.20M
-10.5% 🟢 | 12.4ms
10.8ms
-12.7% 🟢 | 446.6µs
407.8µs
-8.7% 🟢 | 486.9µs
449.0µs
-7.8% 🟢 | 1.98ms
1.55ms
-21.8% 🟢 | 4.08ms
4.12ms
+1.0% 🔴 | | eip4788 | 48.52M
48.52M
= | 40.17M
39.35M
-2.0% 🟢 | 10.4ms
9.86ms
-5.6% 🟢 | 208.8µs
204.5µs
-2.0% 🟢 | 360.4µs
333.2µs
-7.6% 🟢 | 1.12ms
946.1µs
-15.8% 🟢 | 4.08ms
3.37ms
-17.5% 🟢 | | eip2935 | 42.30M
42.34M
+0.1% 🔴 | 39.83M
37.55M
-5.7% 🟢 | 12.0ms
11.2ms
-6.5% 🟢 | 309.7µs
170.8µs
-44.8% 🟢 | 403.7µs
307.6µs
-23.8% 🟢 | 1.02ms
824.4µs
-19.4% 🟢 | 2.86ms
2.87ms
+0.2% 🔴 | | burntpix | 6.29G
6.29G
= | 2.65G
2.65G
-0.1% 🟢 | 518.4ms
516.4ms
-0.4% 🟢 | 13.3ms
13.5ms
+1.4% 🔴 | 1.56ms
1.58ms
+1.4% 🔴 | 7.82ms
8.03ms
+2.8% 🔴 | 27.3ms
27.6ms
+1.3% 🔴 | | curve_stableswap | 1.07G
1.07G
= | 483.94M
465.97M
-3.7% 🟢 | 96.6ms
93.7ms
-3.0% 🟢 | 3.68ms
3.15ms
-14.4% 🟢 | 2.53ms
2.38ms
-6.0% 🟢 | 16.0ms
16.4ms
+2.1% 🔴 | 48.6ms
47.7ms
-1.8% 🟢 | | onchain_lm_v2 | 3.65G
3.65G
= | 1.57G
1.46G
-6.8% 🟢 | 306.2ms
288.6ms
-5.8% 🟢 | 10.8ms
10.8ms
= | 7.49ms
7.66ms
+2.3% 🔴 | 48.6ms
48.9ms
+0.5% 🔴 | 189.3ms
187.9ms
-0.7% 🟢 | | usdt | 1.36G
1.36G
= | 610.56M
588.13M
-3.7% 🟢 | 122.4ms
118.2ms
-3.4% 🟢 | 6.82ms
6.26ms
-8.2% 🟢 | 4.69ms
4.75ms
+1.3% 🔴 | 25.8ms
26.0ms
+0.9% 🔴 | 78.6ms
78.7ms
+0.1% 🔴 | | universal_router | 11.55G
11.56G
+0.1% 🔴 | 6.50G
5.95G
-8.4% 🟢 | 1.286s
1.168s
-9.1% 🟢 | 13.8ms
13.2ms
-4.7% 🟢 | 10.2ms
10.1ms
-0.6% 🟢 | 79.0ms
80.6ms
+2.1% 🔴 | 318.0ms
313.7ms
-1.4% 🟢 | | aggregation_router_v6 | 35.01G
35.11G
+0.3% 🔴 | 26.89G
20.73G
-22.9% 🟢 | 6.250s
4.311s
-31.0% 🟢 | 22.7ms
21.3ms
-6.3% 🟢 | 18.5ms
16.3ms
-11.8% 🟢 | 143.5ms
136.3ms
-5.0% 🟢 | 537.3ms
517.9ms
-3.6% 🟢 | | entry_point_v0_7 | 17.95G
17.98G
+0.2% 🔴 | 8.41G
10.16G
+20.9% 🔴 | 1.585s
2.392s
+50.9% 🔴 | 12.7ms
10.5ms
-17.1% 🟢 | 10.6ms
8.91ms
-16.2% 🟢 | 74.1ms
64.5ms
-13.0% 🟢 | 334.3ms
269.5ms
-19.4% 🟢 | | uniswap_v3_pool | 6.20G
6.20G
+0.1% 🔴 | 2.79G
2.82G
+1.1% 🔴 | 530.2ms
538.1ms
+1.5% 🔴 | 22.4ms
21.0ms
-6.2% 🟢 | 16.3ms
15.2ms
-6.7% 🟢 | 113.3ms
108.2ms
-4.5% 🟢 | 407.7ms
403.3ms
-1.1% 🟢 | | uniswap_v4_pool_manager | 6.67G
6.69G
+0.2% 🔴 | 3.05G
2.91G
-4.6% 🟢 | 602.5ms
574.6ms
-4.6% 🟢 | 15.5ms
15.7ms
+1.3% 🔴 | 12.5ms
12.7ms
+1.8% 🔴 | 107.0ms
106.2ms
-0.7% 🟢 | 399.8ms
397.8ms
-0.5% 🟢 | | curve_3pool | 6.59G
6.59G
= | 2.60G
2.83G
+9.1% 🔴 | 509.1ms
555.1ms
+9.0% 🔴 | 21.1ms
20.6ms
-2.1% 🟢 | 13.1ms
13.0ms
-0.8% 🟢 | 84.9ms
85.6ms
+0.8% 🔴 | 442.2ms
436.4ms
-1.3% 🟢 | | curve_tricrypto2 | 9.33G
9.33G
= | 3.91G
3.81G
-2.4% 🟢 | 761.7ms
738.0ms
-3.1% 🟢 | 20.1ms
19.3ms
-3.9% 🟢 | 15.0ms
14.4ms
-3.6% 🟢 | 99.7ms
93.9ms
-5.8% 🟢 | 429.1ms
420.1ms
-2.1% 🟢 | | balancer_vault | 18.94G
18.94G
= | 8.99G
8.97G
-0.3% 🟢 | 1.733s
1.712s
-1.2% 🟢 | 27.6ms
27.8ms
+0.9% 🔴 | 17.8ms
18.2ms
+2.3% 🔴 | 207.1ms
202.8ms
-2.1% 🟢 | 679.2ms
652.7ms
-3.9% 🟢 | | succinct_staking | 10.84G
10.84G
= | 5.10G
5.22G
+2.4% 🔴 | 1.004s
1.006s
+0.2% 🔴 | 21.9ms
20.3ms
-7.4% 🟢 | 13.6ms
12.1ms
-11.3% 🟢 | 98.5ms
86.8ms
-11.9% 🟢 | 386.6ms
314.9ms
-18.5% 🟢 | | **TOTAL** | **163.98G
164.19G
+0.1% 🔴** | **87.94G
82.61G
-6.1% 🟢** | **18.185s
16.828s
-7.5% 🟢** | **320.6ms
311.0ms
-3.0% 🟢** | **208.2ms
198.7ms
-4.6% 🟢** | **1.575s
1.535s
-2.5% 🟢** | **6.126s
5.928s
-3.2% 🟢** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:------------|--------------------:|--------------:| | counter | -1.2% 🟢 | -3.0% 🟢 | | weth | -1.8% 🟢 | -1.1% 🟢 | | push0_proxy | -1.3% 🟢 | +2.5% 🔴 | | usdc_proxy | -1.8% 🟢 | +5.4% 🔴 | | airdrop | +0.8% 🔴 | +7.4% 🔴 | | bswap64_opt | +1.6% 🔴 | +1.6% 🔴 | | curve_3pool | +0.2% 🔴 | +8.4% 🔴 | | **TOTAL** | **=** | **-0.4% 🟢** | | **GEOMEAN** | **-0.1% 🟢** | **+1.3% 🔴** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 41.92K
41.92K
= | 10.53K
10.78K
+2.4% 🔴 | | factorial | 77.90K
77.39K
-0.7% 🟢 | 18.04K
18.03K
= | | counter | 21.23K
20.98K
-1.2% 🟢 | 7.53K
7.31K
-3.0% 🟢 | | snailtracer | 199.40M
199.40M
= | 57.72M
58.75M
+1.8% 🔴 | | weth | 26.28K
25.81K
-1.8% 🟢 | 9.34K
9.24K
-1.1% 🟢 | | hash_10k | 7.10M
7.10M
= | 2.70M
2.68M
-0.5% 🟢 | | erc20_transfer | 19.57M
19.58M
+0.1% 🔴 | 5.74M
5.76M
+0.4% 🔴 | | push0_proxy | 20.08K
19.82K
-1.3% 🟢 | 7.68K
7.88K
+2.5% 🔴 | | usdc_proxy | 22.67K
22.25K
-1.8% 🟢 | 8.09K
8.53K
+5.4% 🔴 | | fiat_token | 20.63K
20.47K
-0.8% 🟢 | 8.17K
8.13K
-0.5% 🟢 | | uniswap_v2_pair | 21.02K
20.98K
-0.2% 🟢 | 7.89K
7.98K
+1.2% 🔴 | | univ2_router | 18.38K
18.54K
+0.9% 🔴 | 6.94K
7.27K
+4.7% 🔴 | | seaport | 21.16K
21.30K
+0.7% 🔴 | 7.96K
8.27K
+4.0% 🔴 | | airdrop | 20.82K
20.98K
+0.8% 🔴 | 7.60K
8.16K
+7.4% 🔴 | | bswap64 | 17.83K
17.76K
-0.4% 🟢 | 7.48K
7.59K
+1.5% 🔴 | | bswap64_opt | 17.74K
18.03K
+1.6% 🔴 | 6.72K
6.83K
+1.6% 🔴 | | eip4788 | 22.05K
22.01K
-0.2% 🟢 | 8.03K
8.15K
+1.5% 🔴 | | eip2935 | 21.00K
21.09K
+0.4% 🔴 | 7.61K
7.86K
+3.4% 🔴 | | burntpix | 105.03M
105.02M
= | 27.84M
27.73M
-0.4% 🟢 | | curve_stableswap | 236.16K
236.38K
+0.1% 🔴 | 101.58K
100.08K
-1.5% 🟢 | | onchain_lm_v2 | 521.32M
521.32M
= | 215.55M
213.39M
-1.0% 🟢 | | usdt | 36.59K
36.80K
+0.6% 🔴 | 12.54K
12.75K
+1.7% 🔴 | | universal_router | 138.16K
137.68K
-0.3% 🟢 | 49.49K
49.82K
+0.7% 🔴 | | aggregation_router_v6 | 434.21K
435.83K
+0.4% 🔴 | 163.74K
163.56K
-0.1% 🟢 | | entry_point_v0_7 | 1.25M
1.25M
= | 324.09K
325.02K
+0.3% 🔴 | | uniswap_v3_pool | 82.22K
82.06K
-0.2% 🟢 | 27.24K
27.11K
-0.5% 🟢 | | uniswap_v4_pool_manager | 58.03K
57.87K
-0.3% 🟢 | 19.64K
19.98K
+1.7% 🔴 | | curve_3pool | 77.08K
77.20K
+0.2% 🔴 | 28.25K
30.61K
+8.4% 🔴 | | curve_tricrypto2 | 202.24K
202.37K
+0.1% 🔴 | 78.08K
77.17K
-1.2% 🟢 | | balancer_vault | 116.16K
115.96K
-0.2% 🟢 | 39.87K
39.62K
-0.6% 🟢 | | succinct_staking | 60.12K
60.17K
+0.1% 🔴 | 23.17K
23.77K
+2.6% 🔴 | | **TOTAL** | **855.50M
855.51M
=** | **310.55M
309.32M
-0.4% 🟢** |
--- crates/jit/llvm/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 1abd50574..bfc294744 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -1938,6 +1938,10 @@ fn init_() -> Result<()> { // functions alias analysis lets EVM memory and builtin calls alias the stack, and LICM // cannot promote loop-carried stack slots to registers. c"-capture-tracking-max-uses-to-explore=1000000".as_ptr(), + // Emit a trap for `unreachable`, as rustc does, so that a builtin call that is expected + // to leave through `evm2_jit_exit` but returns faults instead of running into the next + // block. + c"-trap-unreachable".as_ptr(), ]; args.extend(extra.iter().map(|s| s.as_ptr())); unsafe { LLVMParseCommandLineOptions(args.len() as i32, args.as_ptr(), std::ptr::null()) } From a026a5a3455cb22da9a48f5e7fc4add74746bc2e Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:33:17 +0100 Subject: [PATCH 130/159] feat(jit): probe stack frames larger than a page (#1021) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compiled functions run nested calls on the native stack, but they had no stack probes. A frame larger than the 4 KiB guard page could move the stack pointer past the guard, so a stack overflow wrote into whatever mapping lies below it, often another thread's stack, instead of faulting. Bytecode controls the frame size. A 7 KB contract that keeps 1000 derived words live in one section compiles to a 31,928-byte frame, even when that section never runs. Self-calling through such a contract under Prague rules (36M gas, about 75k gas spent) on a 16 MiB thread crashed with a plain SIGSEGV outside the guard; with a writable mapping placed below the guard page, the transaction finished normally after writing 589,240 bytes into that mapping. The interpreter needs about 0.7 MiB for the same transaction. This marks compiled functions with `"probe-stack"="inline-asm"`, as rustc does for Rust code. LLVM only probes frames larger than 4 KiB, so no benchmark's code changes (the largest frame is 1,240 bytes at the default opt level). With the change, the same reproducer faults on the guard page and aborts with Rust's "has overflowed its stack" message, and the mapping below stays untouched. Frames still use far more native stack than the interpreter's, so this turns silent corruption into a clean abort but does not stop the overflow itself. ## Benchmarks `opt.s` unchanged in 0/31 benchmarks; compile instructions unchanged. Head `031d5c82fc` with uncommitted changes vs base `origin/main` (`031d5c82fc`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 329
329
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 325
325
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 986
986
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 59703
59703
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11558
11558
= | 2840
2840
= | 4398
4398
= | 13.4 KiB
13.4 KiB
= | 188
188
= | 237
237
= | 25
25
= | 13
13
= | 9
9
= | | hash_10k | 982
982
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12026
12026
= | 3815
3815
= | 4975
4975
= | 14.0 KiB
14.0 KiB
= | 160
160
= | 324
324
= | 53
53
= | 8
8
= | 7
7
= | | push0_proxy | 307
307
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6047
6047
= | 1581
1581
= | 2277
2277
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 7
7
= | 6
6
= | | fiat_token | 75310
75310
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41568
41568
= | 12037
12037
= | 16688
16688
= | 52.4 KiB
52.4 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 433
433
= | | univ2_router | 80165
80165
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 132264
132264
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26516
26516
= | 8424
8424
= | 10800
10800
= | 30.7 KiB
30.7 KiB
= | 432
432
= | 667
667
= | 91
91
= | 38
38
= | 42
42
= | | bswap64 | 1751
1751
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1490
1490
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 625
625
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 482
482
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9152
9152
= | 3038
3038
= | 3858
3858
= | 11.7 KiB
11.7 KiB
= | 165
165
= | 282
282
= | 26
26
= | 5
5
= | 5
5
= | | curve_stableswap | 16328
16328
= | 5692
5692
= | 7043
7043
= | 20.0 KiB
20.0 KiB
= | 324
324
= | 506
506
= | 44
44
= | 10
10
= | 13
13
= | | onchain_lm_v2 | 53109
53109
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34549
34549
= | 8005
8005
= | 10792
10792
= | 31.7 KiB
31.7 KiB
= | 473
473
= | 657
657
= | 52
52
= | 41
41
= | 51
51
= | | universal_router | 77412
77412
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 124769
124769
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66480
66480
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 116922
116922
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93186
93186
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 104646
104646
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 113933
113933
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145361
145361
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85681
85681
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1493962
1493962
=** | **495704
495704
=** | **662166
662166
=** | **2.1 MiB
2.1 MiB
=** | **32200
32200
=** | **42287
42287
=** | **5830
5830
=** | **2518
2518
=** | **19794
19794
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **-0.6% 🟢** | **-0.6% 🟢** | | **GEOMEAN** | **=** | **-0.2% 🟢** | **+0.6% 🔴** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.44M
37.44M
= | 37.34M
33.86M
-9.3% 🟢 | 10.7ms
11.0ms
+2.6% 🔴 | 196.6µs
194.6µs
-1.0% 🟢 | 366.7µs
351.7µs
-4.1% 🟢 | 1.33ms
1.29ms
-3.2% 🟢 | 2.81ms
2.71ms
-3.6% 🟢 | | factorial | 41.65M
41.66M
= | 35.14M
33.85M
-3.7% 🟢 | 10.3ms
10.3ms
+0.3% 🔴 | 181.2µs
172.5µs
-4.8% 🟢 | 340.2µs
310.0µs
-8.9% 🟢 | 1.27ms
1.09ms
-13.8% 🟢 | 3.29ms
3.00ms
-8.8% 🟢 | | counter | 54.13M
54.11M
= | 33.27M
34.60M
+4.0% 🔴 | 8.22ms
8.79ms
+6.9% 🔴 | 530.4µs
320.1µs
-39.7% 🟢 | 507.9µs
409.6µs
-19.4% 🟢 | 1.30ms
1.28ms
-1.5% 🟢 | 3.54ms
4.49ms
+26.9% 🔴 | | snailtracer | 5.25G
5.24G
-0.1% 🟢 | 2.00G
1.99G
-0.4% 🟢 | 379.6ms
378.1ms
-0.4% 🟢 | 29.9ms
30.1ms
+0.8% 🔴 | 6.56ms
6.72ms
+2.4% 🔴 | 65.8ms
70.5ms
+7.2% 🔴 | 303.2ms
307.2ms
+1.3% 🔴 | | weth | 505.25M
505.17M
= | 227.97M
226.69M
-0.6% 🟢 | 45.5ms
46.7ms
+2.6% 🔴 | 2.09ms
2.16ms
+3.5% 🔴 | 1.79ms
1.82ms
+1.9% 🔴 | 9.54ms
10.5ms
+10.3% 🔴 | 31.6ms
31.5ms
-0.3% 🟢 | | hash_10k | 80.31M
80.22M
-0.1% 🟢 | 45.04M
48.32M
+7.3% 🔴 | 10.9ms
11.5ms
+5.6% 🔴 | 298.8µs
302.3µs
+1.1% 🔴 | 423.8µs
451.3µs
+6.5% 🔴 | 1.85ms
1.88ms
+1.7% 🔴 | 4.99ms
5.07ms
+1.7% 🔴 | | erc20_transfer | 562.85M
562.87M
= | 243.37M
242.11M
-0.5% 🟢 | 48.8ms
48.2ms
-1.3% 🟢 | 2.25ms
2.25ms
+0.2% 🔴 | 1.86ms
2.13ms
+14.3% 🔴 | 9.83ms
9.86ms
+0.3% 🔴 | 33.7ms
33.5ms
-0.6% 🟢 | | push0_proxy | 34.66M
34.66M
= | 31.53M
31.43M
-0.3% 🟢 | 9.91ms
10.3ms
+3.5% 🔴 | 137.7µs
174.9µs
+27.0% 🔴 | 279.1µs
319.0µs
+14.3% 🔴 | 766.3µs
869.6µs
+13.5% 🔴 | 2.06ms
2.18ms
+5.9% 🔴 | | usdc_proxy | 250.67M
250.62M
= | 123.57M
122.56M
-0.8% 🟢 | 26.0ms
25.9ms
-0.7% 🟢 | 1.39ms
1.29ms
-7.6% 🟢 | 1.15ms
1.09ms
-4.8% 🟢 | 5.85ms
5.99ms
+2.3% 🔴 | 14.7ms
14.7ms
+0.2% 🔴 | | fiat_token | 3.98G
3.97G
= | 1.61G
1.59G
-0.9% 🟢 | 306.8ms
304.6ms
-0.7% 🟢 | 13.9ms
13.8ms
-0.6% 🟢 | 9.84ms
9.63ms
-2.1% 🟢 | 69.4ms
68.1ms
-1.9% 🟢 | 243.2ms
240.5ms
-1.1% 🟢 | | uniswap_v2_pair | 2.11G
2.11G
= | 869.99M
890.82M
+2.4% 🔴 | 167.6ms
172.0ms
+2.6% 🔴 | 7.42ms
7.55ms
+1.8% 🔴 | 5.60ms
5.53ms
-1.3% 🟢 | 34.9ms
34.4ms
-1.4% 🟢 | 131.6ms
129.6ms
-1.5% 🟢 | | univ2_router | 4.38G
4.38G
= | 1.83G
1.80G
-1.9% 🟢 | 351.5ms
342.8ms
-2.5% 🟢 | 15.6ms
15.5ms
-1.0% 🟢 | 10.2ms
10.1ms
-1.6% 🟢 | 67.9ms
69.2ms
+1.8% 🔴 | 284.5ms
282.9ms
-0.6% 🟢 | | seaport | 9.72G
9.72G
= | 4.23G
4.27G
+0.9% 🔴 | 794.4ms
807.6ms
+1.7% 🔴 | 26.3ms
26.0ms
-1.0% 🟢 | 17.8ms
17.6ms
-1.4% 🟢 | 176.5ms
178.7ms
+1.3% 🔴 | 714.6ms
687.6ms
-3.8% 🟢 | | airdrop | 1.33G
1.33G
= | 550.48M
547.52M
-0.5% 🟢 | 109.2ms
105.7ms
-3.2% 🟢 | 4.74ms
4.59ms
-3.1% 🟢 | 4.02ms
3.75ms
-6.6% 🟢 | 24.5ms
22.4ms
-8.6% 🟢 | 81.8ms
81.7ms
-0.1% 🟢 | | bswap64 | 67.03M
67.10M
+0.1% 🔴 | 37.84M
41.03M
+8.4% 🔴 | 9.44ms
10.1ms
+7.3% 🔴 | 487.3µs
477.5µs
-2.0% 🟢 | 525.3µs
528.3µs
+0.6% 🔴 | 1.60ms
1.62ms
+1.2% 🔴 | 3.75ms
3.77ms
+0.4% 🔴 | | bswap64_opt | 73.73M
73.69M
= | 38.84M
39.78M
+2.4% 🔴 | 8.89ms
9.41ms
+5.8% 🔴 | 429.1µs
425.1µs
-0.9% 🟢 | 470.7µs
462.8µs
-1.7% 🟢 | 1.57ms
1.57ms
+0.1% 🔴 | 4.07ms
4.07ms
= | | eip4788 | 48.49M
48.49M
= | 30.00M
29.57M
-1.4% 🟢 | 7.65ms
7.49ms
-2.1% 🟢 | 437.1µs
343.9µs
-21.3% 🟢 | 662.3µs
461.2µs
-30.4% 🟢 | 1.01ms
1.04ms
+3.1% 🔴 | 3.27ms
3.33ms
+1.7% 🔴 | | eip2935 | 42.38M
42.33M
-0.1% 🟢 | 29.98M
30.40M
+1.4% 🔴 | 9.45ms
9.40ms
-0.5% 🟢 | 172.5µs
190.2µs
+10.3% 🔴 | 348.9µs
329.9µs
-5.4% 🟢 | 945.2µs
1.20ms
+27.2% 🔴 | 3.01ms
2.85ms
-5.5% 🟢 | | burntpix | 6.29G
6.29G
= | 2.52G
2.51G
-0.6% 🟢 | 476.2ms
473.8ms
-0.5% 🟢 | 13.3ms
13.3ms
+0.2% 🔴 | 1.55ms
1.59ms
+2.1% 🔴 | 7.69ms
7.73ms
+0.5% 🔴 | 27.3ms
27.4ms
+0.5% 🔴 | | curve_stableswap | 1.07G
1.07G
= | 458.39M
456.75M
-0.4% 🟢 | 89.5ms
88.7ms
-1.0% 🟢 | 3.08ms
3.17ms
+2.8% 🔴 | 2.36ms
2.60ms
+10.3% 🔴 | 16.4ms
15.7ms
-4.1% 🟢 | 48.1ms
47.4ms
-1.4% 🟢 | | onchain_lm_v2 | 3.65G
3.65G
= | 1.44G
1.43G
-0.7% 🟢 | 278.2ms
274.6ms
-1.3% 🟢 | 10.1ms
10.3ms
+1.9% 🔴 | 6.81ms
6.78ms
-0.5% 🟢 | 46.4ms
46.9ms
+1.0% 🔴 | 186.5ms
186.2ms
-0.2% 🟢 | | usdt | 1.36G
1.36G
= | 583.68M
562.72M
-3.6% 🟢 | 112.5ms
111.1ms
-1.2% 🟢 | 6.93ms
6.56ms
-5.4% 🟢 | 5.53ms
4.78ms
-13.6% 🟢 | 25.9ms
26.0ms
+0.4% 🔴 | 78.4ms
79.0ms
+0.8% 🔴 | | universal_router | 11.55G
11.55G
= | 4.82G
4.79G
-0.6% 🟢 | 911.7ms
922.6ms
+1.2% 🔴 | 14.2ms
14.0ms
-1.1% 🟢 | 10.5ms
10.4ms
-1.1% 🟢 | 79.8ms
79.4ms
-0.6% 🟢 | 314.6ms
313.6ms
-0.3% 🟢 | | aggregation_router_v6 | 35.00G
35.01G
= | 14.68G
14.41G
-1.8% 🟢 | 2.761s
2.693s
-2.5% 🟢 | 21.2ms
22.8ms
+7.2% 🔴 | 17.1ms
16.5ms
-3.6% 🟢 | 135.9ms
135.7ms
-0.1% 🟢 | 512.3ms
517.7ms
+1.0% 🔴 | | entry_point_v0_7 | 17.95G
17.95G
= | 7.39G
7.39G
= | 1.392s
1.388s
-0.3% 🟢 | 10.5ms
10.3ms
-1.6% 🟢 | 9.65ms
9.26ms
-4.0% 🟢 | 66.3ms
64.7ms
-2.4% 🟢 | 269.1ms
268.1ms
-0.4% 🟢 | | uniswap_v3_pool | 6.20G
6.20G
= | 2.58G
2.56G
-0.6% 🟢 | 499.9ms
485.2ms
-2.9% 🟢 | 22.8ms
21.2ms
-6.9% 🟢 | 15.9ms
15.1ms
-5.0% 🟢 | 114.0ms
110.4ms
-3.1% 🟢 | 404.1ms
403.7ms
-0.1% 🟢 | | uniswap_v4_pool_manager | 6.67G
6.68G
+0.1% 🔴 | 2.74G
2.72G
-0.7% 🟢 | 525.1ms
521.1ms
-0.8% 🟢 | 15.4ms
15.6ms
+1.4% 🔴 | 12.7ms
13.3ms
+4.7% 🔴 | 105.8ms
109.5ms
+3.5% 🔴 | 388.9ms
391.6ms
+0.7% 🔴 | | curve_3pool | 6.59G
6.59G
= | 2.58G
2.56G
-0.6% 🟢 | 488.4ms
487.0ms
-0.3% 🟢 | 21.0ms
20.5ms
-2.1% 🟢 | 13.6ms
13.3ms
-2.1% 🟢 | 83.1ms
84.9ms
+2.1% 🔴 | 430.8ms
436.1ms
+1.2% 🔴 | | curve_tricrypto2 | 9.33G
9.33G
= | 3.63G
3.64G
+0.2% 🔴 | 690.6ms
687.3ms
-0.5% 🟢 | 19.3ms
19.9ms
+3.6% 🔴 | 14.4ms
15.0ms
+4.1% 🔴 | 92.8ms
96.0ms
+3.4% 🔴 | 423.1ms
420.1ms
-0.7% 🟢 | | balancer_vault | 18.95G
18.93G
-0.1% 🟢 | 8.13G
8.10G
-0.3% 🟢 | 1.526s
1.537s
+0.7% 🔴 | 27.7ms
27.0ms
-2.3% 🟢 | 18.3ms
17.7ms
-3.2% 🟢 | 200.3ms
200.6ms
+0.1% 🔴 | 641.7ms
650.7ms
+1.4% 🔴 | | succinct_staking | 10.84G
10.84G
= | 4.41G
4.41G
-0.1% 🟢 | 829.8ms
829.5ms
= | 19.5ms
20.1ms
+2.9% 🔴 | 10.8ms
10.7ms
-0.7% 🟢 | 87.7ms
86.3ms
-1.5% 🟢 | 316.8ms
315.9ms
-0.3% 🟢 | | **TOTAL** | **164.00G
163.98G
=** | **67.97G
67.55G
-0.6% 🟢** | **12.896s
12.820s
-0.6% 🟢** | **311.5ms
310.9ms
-0.2% 🟢** | **201.9ms
198.9ms
-1.5% 🟢** | **1.538s
1.545s
+0.5% 🔴** | **5.911s
5.898s
-0.2% 🟢** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:------------|--------------------:|--------------:| | usdc_proxy | -1.1% 🟢 | +4.0% 🔴 | | airdrop | -1.9% 🟢 | +1.5% 🔴 | | **TOTAL** | **=** | **-0.1% 🟢** | | **GEOMEAN** | **-0.1% 🟢** | **+1.2% 🔴** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 41.92K
41.79K
-0.3% 🟢 | 10.48K
10.67K
+1.8% 🔴 | | factorial | 77.90K
78.05K
+0.2% 🔴 | 17.97K
18.28K
+1.7% 🔴 | | counter | 21.23K
21.26K
+0.1% 🔴 | 7.39K
7.54K
+2.0% 🔴 | | snailtracer | 199.40M
199.40M
= | 56.11M
55.87M
-0.4% 🟢 | | weth | 25.97K
25.80K
-0.6% 🟢 | 9.36K
9.37K
+0.2% 🔴 | | hash_10k | 7.10M
7.10M
= | 2.71M
2.71M
+0.2% 🔴 | | erc20_transfer | 19.57M
19.58M
+0.1% 🔴 | 5.71M
5.66M
-0.8% 🟢 | | push0_proxy | 20.09K
20.05K
-0.2% 🟢 | 7.75K
7.99K
+3.1% 🔴 | | usdc_proxy | 22.67K
22.42K
-1.1% 🟢 | 7.97K
8.28K
+4.0% 🔴 | | fiat_token | 20.47K
20.47K
= | 7.51K
7.65K
+1.9% 🔴 | | uniswap_v2_pair | 21.02K
21.02K
= | 7.67K
7.78K
+1.5% 🔴 | | univ2_router | 18.55K
18.54K
= | 6.94K
7.23K
+4.2% 🔴 | | seaport | 21.30K
21.14K
-0.8% 🟢 | 7.77K
7.99K
+2.8% 🔴 | | airdrop | 21.22K
20.82K
-1.9% 🟢 | 7.75K
7.87K
+1.5% 🔴 | | bswap64 | 18.13K
18.13K
= | 6.62K
6.89K
+4.1% 🔴 | | bswap64_opt | 17.74K
17.74K
= | 6.60K
6.80K
+3.0% 🔴 | | eip4788 | 22.05K
22.20K
+0.7% 🔴 | 7.84K
8.17K
+4.1% 🔴 | | eip2935 | 21.00K
20.96K
-0.2% 🟢 | 7.61K
7.91K
+4.0% 🔴 | | burntpix | 105.02M
104.98M
= | 26.41M
26.48M
+0.3% 🔴 | | curve_stableswap | 236.32K
236.07K
-0.1% 🟢 | 100.36K
100.16K
-0.2% 🟢 | | onchain_lm_v2 | 521.32M
521.32M
= | 214.39M
214.16M
-0.1% 🟢 | | usdt | 36.79K
36.82K
+0.1% 🔴 | 12.55K
12.86K
+2.5% 🔴 | | universal_router | 138.72K
138.63K
-0.1% 🟢 | 49.51K
49.89K
+0.8% 🔴 | | aggregation_router_v6 | 435.26K
435.15K
= | 162.67K
163.28K
+0.4% 🔴 | | entry_point_v0_7 | 1.25M
1.25M
= | 322.71K
323.71K
+0.3% 🔴 | | uniswap_v3_pool | 82.22K
82.30K
+0.1% 🔴 | 27.39K
27.47K
+0.3% 🔴 | | uniswap_v4_pool_manager | 57.96K
57.90K
-0.1% 🟢 | 20.04K
19.88K
-0.8% 🟢 | | curve_3pool | 77.37K
77.39K
= | 26.86K
26.95K
+0.3% 🔴 | | curve_tricrypto2 | 202.34K
202.10K
-0.1% 🟢 | 77.37K
75.93K
-1.9% 🟢 | | balancer_vault | 116.05K
116.13K
+0.1% 🔴 | 40.68K
39.67K
-2.5% 🟢 | | succinct_staking | 60.00K
60.12K
+0.2% 🔴 | 23.12K
22.71K
-1.8% 🟢 | | **TOTAL** | **855.50M
855.46M
=** | **306.32M
305.89M
-0.1% 🟢** |
--- crates/jit/backend/src/traits.rs | 3 +++ crates/jit/codegen/src/compiler/mod.rs | 11 ++++++----- crates/jit/llvm/src/lib.rs | 1 + 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/jit/backend/src/traits.rs b/crates/jit/backend/src/traits.rs index e5d760f21..b17d96fb8 100644 --- a/crates/jit/backend/src/traits.rs +++ b/crates/jit/backend/src/traits.rs @@ -134,6 +134,9 @@ pub enum Attribute { AlwaysInline, NoInline, Speculatable, + /// `"probe-stack"="inline-asm"` — touches every page of a frame larger than a page, so that + /// a stack overflow faults on the guard page instead of skipping past it. + InlineStackProbes, // Parameter attributes. NoAlias, diff --git a/crates/jit/codegen/src/compiler/mod.rs b/crates/jit/codegen/src/compiler/mod.rs index ec4f48503..47c53721e 100644 --- a/crates/jit/codegen/src/compiler/mod.rs +++ b/crates/jit/codegen/src/compiler/mod.rs @@ -1100,11 +1100,12 @@ mod default_attrs { pub(crate) fn for_fn() -> impl Iterator { [ - Attribute::WillReturn, // Always returns. - Attribute::NoSync, // No thread synchronization. - Attribute::NativeTargetCpu, // Optimization. - Attribute::NonLazyBind, // Skip PLT indirection. - Attribute::UWTable, // Unwind tables for profilers/debuggers. + Attribute::WillReturn, // Always returns. + Attribute::NoSync, // No thread synchronization. + Attribute::NativeTargetCpu, // Optimization. + Attribute::NonLazyBind, // Skip PLT indirection. + Attribute::UWTable, // Unwind tables for profilers/debuggers. + Attribute::InlineStackProbes, // Overflow faults on the guard page, as in Rust code. ] .into_iter() } diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index bfc294744..9d80a93ea 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -2033,6 +2033,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::AlwaysInline => ("alwaysinline", AttrValue::Enum(0)), OurAttr::NoInline => ("noinline", AttrValue::Enum(0)), OurAttr::Speculatable => ("speculatable", AttrValue::Enum(0)), + OurAttr::InlineStackProbes => ("probe-stack", AttrValue::String("inline-asm")), OurAttr::NoAlias => ("noalias", AttrValue::Enum(0)), OurAttr::NoCapture => return cpp::create_no_captures_attr(bcx.cx()), From cf43e45f4cab3f0c86cf8646eee46c0a52cf8c5d Mon Sep 17 00:00:00 2001 From: 0xrusowsky <90208954+0xrusowsky@users.noreply.github.com> Date: Thu, 5 Nov 2026 13:00:42 +0100 Subject: [PATCH 131/159] chore(deps): ignore RUSTSEC-2026-0441 pin-utils advisory (#1028) Ignores RUSTSEC-2026-0441 (`pin-utils` unmaintained), which fails `cargo deny check` on main. `pin-utils` only reaches us through `wasmtimer`, and the advisory has no patched release, so there's nothing to bump on our side. --- deny.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/deny.toml b/deny.toml index 3d6a19675..f193798f7 100644 --- a/deny.toml +++ b/deny.toml @@ -9,6 +9,8 @@ ignore = [ # Its uses here verify public transaction/precompile inputs; signing uses fixed test keys. # Remove this exception when their RustCrypto dependencies are upgraded. { id = "RUSTSEC-2026-0387", reason = "Unpatched 0.5.5 is required by Alloy/revm; vulnerable newer versions are banned below." }, + # `pin-utils` is unmaintained - transitive dependency + "RUSTSEC-2026-0441", ] [bans] From 72a31908ec108e83b773938e0fda16ff3f35b81c Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Thu, 5 Nov 2026 22:46:16 -0500 Subject: [PATCH 132/159] feat(block): add BlockTransactionResult::into_result (#1029) Adds `BlockTransactionResult::into_result`, so callers that own a detached transaction output can take its `TxResultWithState` without cloning the pending state. reth needs it to replay block transactions through the block executor (paradigmxyz/reth#28266) without copying each traced transaction's write-set. --- crates/evm2/src/block/ethereum/mod.rs | 4 ++++ crates/evm2/src/block/mod.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index 58901993e..cc3fe2424 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -137,6 +137,10 @@ impl BlockTransactionResult for EthTransactionResultWithStat fn result(&self) -> &TxResultWithState { &self.result } + + fn into_result(self) -> TxResultWithState { + self.result + } } type StateUpdateHook = Option>; diff --git a/crates/evm2/src/block/mod.rs b/crates/evm2/src/block/mod.rs index ea72fcae4..3501fb08e 100644 --- a/crates/evm2/src/block/mod.rs +++ b/crates/evm2/src/block/mod.rs @@ -158,6 +158,9 @@ where pub trait BlockTransactionResult { /// Returns the raw transaction execution result. fn result(&self) -> &TxResultWithState; + + /// Consumes the output and returns the raw transaction execution result. + fn into_result(self) -> TxResultWithState; } /// The result of executing a block. From 22e3906bc333c07169f674ddbbb7805d26a26a98 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:27:04 +0100 Subject: [PATCH 133/159] feat(block): allow empty request contracts on chains without them (#1031) The block executor rejects a Prague block when the EIP-7002 or EIP-7251 request contract has no code, as the EIPs and geth do. Chains that enable Prague without deploying those contracts, such as Tempo before T13, ran the calls as no-ops before evm2's executor replaced reth's own, and every such block is now invalid. `EthBlockRules::allow_empty_request_contracts`, off by default, skips only that code check, so the system calls still run exactly as they did before. --- crates/evm2/src/block/ethereum/execution.rs | 20 +++++++++++++++----- crates/evm2/src/block/ethereum/mod.rs | 6 ++++++ crates/evm2/src/block/ethereum/tests.rs | 13 +++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs index 8782e6231..1dc9450b8 100644 --- a/crates/evm2/src/block/ethereum/execution.rs +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -138,6 +138,9 @@ pub struct BlockExecutionContext<'a> { pub withdrawals: Option<&'a [Withdrawal]>, /// Deposit contract address used to derive EIP-6110 deposit requests from receipts. pub deposit_contract_address: Option
, + /// Whether a request system call to a contract without code runs as a no-op instead of making + /// the block invalid. + pub allow_empty_request_contracts: bool, } /// Inputs required by Ethereum pre-block system calls. @@ -399,6 +402,7 @@ pub fn post_execution_system_call_state_changes( stream_state, on_state_update, WITHDRAWAL_REQUEST_ADDRESS, + context.allow_empty_request_contracts, )?; requests.push_request_with_type( WITHDRAWAL_REQUEST_TYPE, @@ -411,6 +415,7 @@ pub fn post_execution_system_call_state_changes( stream_state, on_state_update, CONSOLIDATION_REQUEST_ADDRESS, + context.allow_empty_request_contracts, )?; requests.push_request_with_type( CONSOLIDATION_REQUEST_TYPE, @@ -424,6 +429,7 @@ pub fn post_execution_system_call_state_changes( stream_state, on_state_update, BUILDER_DEPOSIT_REQUEST_ADDRESS, + context.allow_empty_request_contracts, )?; requests.push_request_with_type( BUILDER_DEPOSIT_REQUEST_TYPE, @@ -436,6 +442,7 @@ pub fn post_execution_system_call_state_changes( stream_state, on_state_update, BUILDER_EXIT_REQUEST_ADDRESS, + context.allow_empty_request_contracts, )?; requests.push_request_with_type( BUILDER_EXIT_REQUEST_TYPE, @@ -446,18 +453,21 @@ pub fn post_execution_system_call_state_changes( Ok(()) } -/// Runs a request system call. A target without code makes the block invalid: the EVM itself -/// treats a call to it as a successful no-op. +/// Runs a request system call. Unless `allow_empty` is set, a target without code makes the block +/// invalid: the EVM itself treats a call to it as a successful no-op. fn execute_request_system_call( evm: &mut Evm<'_, T>, block_state: &mut BlockState, stream_state: bool, on_state_update: &mut impl FnMut(StateUpdate), address: Address, + allow_empty: bool, ) -> Result, BlockExecutionError> { - let info = evm.read_account_info(&address).map_err(map_database_error)?; - if info.is_none_or(|info| info.is_empty_code_hash()) { - return Err(BlockValidationError::SystemContractEmpty { address }.into()); + if !allow_empty { + let info = evm.read_account_info(&address).map_err(map_database_error)?; + if info.is_none_or(|info| info.is_empty_code_hash()) { + return Err(BlockValidationError::SystemContractEmpty { address }.into()); + } } execute_system_call(evm, block_state, stream_state, on_state_update, address, Bytes::new()) } diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index cc3fe2424..0f408f6ba 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -62,6 +62,10 @@ pub struct EthBlockRules { pub deposit_contract_address: Option
, /// Blob gas the block's transactions may use in total (EIP-4844). `None` leaves it unchecked. pub max_blob_gas_per_block: Option, + /// Whether a request system call to a contract without code runs as a no-op instead of making + /// the block invalid. Ethereum requires the EIP-7002 and EIP-7251 contracts; set this for a + /// chain that enables Prague without deploying them. + pub allow_empty_request_contracts: bool, } impl EthBlockRules { @@ -78,6 +82,7 @@ impl EthBlockRules { .transitions_at_block(block_number), deposit_contract_address, max_blob_gas_per_block: None, + allow_empty_request_contracts: false, } } } @@ -234,6 +239,7 @@ where ommers: Some(ommers), withdrawals, deposit_contract_address: rules.deposit_contract_address, + allow_empty_request_contracts: rules.allow_empty_request_contracts, } } diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs index ca3a72941..58f9b75a2 100644 --- a/crates/evm2/src/block/ethereum/tests.rs +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -233,6 +233,19 @@ fn rejects_request_contract_without_code() { )); } +#[test] +fn allowed_empty_request_contract_is_a_no_op() { + let mut database = database(); + database.insert_account_info(&WITHDRAWAL_REQUEST_ADDRESS, AccountInfo::default()); + database.insert_account_info(&CONSOLIDATION_REQUEST_ADDRESS, AccountInfo::default()); + let evm = evm_with(SpecId::PRAGUE, 30_000_000, database); + let rules = EthBlockRules { allow_empty_request_contracts: true, ..EthBlockRules::default() }; + let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + let output = executor.finish().unwrap(); + assert!(output.result.requests.is_empty()); +} + #[test] fn rejects_transaction_over_block_blob_gas() { let rules = EthBlockRules { max_blob_gas_per_block: Some(0), ..EthBlockRules::default() }; From 10088e0d9de02e6332d43bd1972e7f0df8d41ec1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:28:31 +0100 Subject: [PATCH 134/159] feat(block): add EthBlockExecutor::rules_mut (#1032) Lets a chain that wraps `EthBlockExecutor`, such as Tempo through reth's executor, change the block rules after construction, for example to turn on `allow_empty_request_contracts` from #1031. --- crates/evm2/src/block/ethereum/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index 0f408f6ba..a3e1f43b6 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -257,6 +257,11 @@ where &self.rules } + /// Returns a mutable reference to the chain rules of the block. + pub const fn rules_mut(&mut self) -> &mut EthBlockRules { + &mut self.rules + } + /// Returns the accumulated block state. pub const fn block_state(&self) -> &BlockState { &self.block_state From 41fd72242e88e4e9e02dd89434c560e8d79242f3 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:36:23 +0100 Subject: [PATCH 135/159] fix(block): run request system calls without a code check (#1033) The block executor rejected a Prague block when the EIP-7002 or EIP-7251 request contract had no code. Reth's executor before #872, alloy-evm and the EVM it wraps all run those system calls without that check, so a call to an address without code succeeds and adds no requests. Chains that enable Prague without deploying the contracts, such as Tempo before T13, depend on that, and every such block became invalid once reth switched to this executor. This reverts #1031 and #1032, runs the request calls through `execute_system_call` like the other system calls, and removes `BlockValidationError::SystemContractEmpty`. The EEST harness checks the request contracts' code before the post-execution calls, as it did before #872, so the spec's empty-contract fixtures still fail. --- crates/eest/src/blockchaintest/execute.rs | 31 ++++++++++++++++- crates/evm2/src/block/error.rs | 8 +---- crates/evm2/src/block/ethereum/execution.rs | 38 +++++---------------- crates/evm2/src/block/ethereum/mod.rs | 11 ------ crates/evm2/src/block/ethereum/tests.rs | 20 ++--------- 5 files changed, 41 insertions(+), 67 deletions(-) diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index b584c32b7..fdb00c282 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -47,7 +47,8 @@ use evm2::{ env::{BlockEnv, BlockEnvExt}, ethereum::{RecoveredTxEnvelope, ethereum_tx_registry}, evm::{ - AccountInfo as EvmAccountInfo, Bal, CONSOLIDATION_REQUEST_ADDRESS, DbStats, DbStatsCounts, + AccountInfo as EvmAccountInfo, BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, Bal, CONSOLIDATION_REQUEST_ADDRESS, DbStats, DbStatsCounts, InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, }, }; @@ -676,6 +677,34 @@ fn execute_block<'a>( } } + // A request system contract without code makes the block invalid (execution-specs + // `SYSTEM_CONTRACT_EMPTY`); the executor runs the call as a no-op. + if spec.enables(SpecId::PRAGUE) { + let request_contracts: &[Address] = if spec.enables(SpecId::AMSTERDAM) { + &[ + WITHDRAWAL_REQUEST_ADDRESS, + CONSOLIDATION_REQUEST_ADDRESS, + BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, + ] + } else { + &[WITHDRAWAL_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS] + }; + for address in request_contracts { + let info = executor.evm.read_account_info(address).map_err(|err| { + TestError::case(path, name, TestErrorKind::UnexpectedFailure(err.to_string())) + })?; + if info.is_none_or(|info| info.is_empty_code_hash()) { + return reject_block( + should_fail, + path, + name, + format!("system contract {address} has no code"), + ); + } + } + } + let post_execution = match executor.apply_post_execution_changes(0) { Ok(output) => output, Err(err) => return reject_block(should_fail, path, name, err.to_string()), diff --git a/crates/evm2/src/block/error.rs b/crates/evm2/src/block/error.rs index efd5dfedb..f97aa8d98 100644 --- a/crates/evm2/src/block/error.rs +++ b/crates/evm2/src/block/error.rs @@ -5,7 +5,7 @@ use alloc::{ boxed::Box, string::{String, ToString}, }; -use alloy_primitives::{Address, B256}; +use alloy_primitives::B256; use core::{any::Any, error::Error}; /// Abstraction over transaction validation errors. @@ -108,12 +108,6 @@ pub enum BlockValidationError { /// The error message. message: String, }, - /// A request system contract has no code, so the block cannot produce its requests. - #[error("system contract {address} has no code")] - SystemContractEmpty { - /// The address of the system contract. - address: Address, - }, /// Error when decoding deposit requests from receipts. #[error("failed to decode deposit requests from receipts: {_0}")] DepositRequestDecode(String), diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs index 1dc9450b8..b8124f14a 100644 --- a/crates/evm2/src/block/ethereum/execution.rs +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -138,9 +138,6 @@ pub struct BlockExecutionContext<'a> { pub withdrawals: Option<&'a [Withdrawal]>, /// Deposit contract address used to derive EIP-6110 deposit requests from receipts. pub deposit_contract_address: Option
, - /// Whether a request system call to a contract without code runs as a no-op instead of making - /// the block invalid. - pub allow_empty_request_contracts: bool, } /// Inputs required by Ethereum pre-block system calls. @@ -396,26 +393,26 @@ pub fn post_execution_system_call_state_changes( return Ok(()); } - let withdrawal_requests = execute_request_system_call( + let withdrawal_requests = execute_system_call( evm, block_state, stream_state, on_state_update, WITHDRAWAL_REQUEST_ADDRESS, - context.allow_empty_request_contracts, + Bytes::new(), )?; requests.push_request_with_type( WITHDRAWAL_REQUEST_TYPE, withdrawal_requests.output.iter().copied(), ); - let consolidation_requests = execute_request_system_call( + let consolidation_requests = execute_system_call( evm, block_state, stream_state, on_state_update, CONSOLIDATION_REQUEST_ADDRESS, - context.allow_empty_request_contracts, + Bytes::new(), )?; requests.push_request_with_type( CONSOLIDATION_REQUEST_TYPE, @@ -423,26 +420,26 @@ pub fn post_execution_system_call_state_changes( ); if spec_id.enables(SpecId::AMSTERDAM) { - let builder_deposit_requests = execute_request_system_call( + let builder_deposit_requests = execute_system_call( evm, block_state, stream_state, on_state_update, BUILDER_DEPOSIT_REQUEST_ADDRESS, - context.allow_empty_request_contracts, + Bytes::new(), )?; requests.push_request_with_type( BUILDER_DEPOSIT_REQUEST_TYPE, builder_deposit_requests.output.iter().copied(), ); - let builder_exit_requests = execute_request_system_call( + let builder_exit_requests = execute_system_call( evm, block_state, stream_state, on_state_update, BUILDER_EXIT_REQUEST_ADDRESS, - context.allow_empty_request_contracts, + Bytes::new(), )?; requests.push_request_with_type( BUILDER_EXIT_REQUEST_TYPE, @@ -453,25 +450,6 @@ pub fn post_execution_system_call_state_changes( Ok(()) } -/// Runs a request system call. Unless `allow_empty` is set, a target without code makes the block -/// invalid: the EVM itself treats a call to it as a successful no-op. -fn execute_request_system_call( - evm: &mut Evm<'_, T>, - block_state: &mut BlockState, - stream_state: bool, - on_state_update: &mut impl FnMut(StateUpdate), - address: Address, - allow_empty: bool, -) -> Result, BlockExecutionError> { - if !allow_empty { - let info = evm.read_account_info(&address).map_err(map_database_error)?; - if info.is_none_or(|info| info.is_empty_code_hash()) { - return Err(BlockValidationError::SystemContractEmpty { address }.into()); - } - } - execute_system_call(evm, block_state, stream_state, on_state_update, address, Bytes::new()) -} - fn execute_system_call( evm: &mut Evm<'_, T>, block_state: &mut BlockState, diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs index a3e1f43b6..cc3fe2424 100644 --- a/crates/evm2/src/block/ethereum/mod.rs +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -62,10 +62,6 @@ pub struct EthBlockRules { pub deposit_contract_address: Option
, /// Blob gas the block's transactions may use in total (EIP-4844). `None` leaves it unchecked. pub max_blob_gas_per_block: Option, - /// Whether a request system call to a contract without code runs as a no-op instead of making - /// the block invalid. Ethereum requires the EIP-7002 and EIP-7251 contracts; set this for a - /// chain that enables Prague without deploying them. - pub allow_empty_request_contracts: bool, } impl EthBlockRules { @@ -82,7 +78,6 @@ impl EthBlockRules { .transitions_at_block(block_number), deposit_contract_address, max_blob_gas_per_block: None, - allow_empty_request_contracts: false, } } } @@ -239,7 +234,6 @@ where ommers: Some(ommers), withdrawals, deposit_contract_address: rules.deposit_contract_address, - allow_empty_request_contracts: rules.allow_empty_request_contracts, } } @@ -257,11 +251,6 @@ where &self.rules } - /// Returns a mutable reference to the chain rules of the block. - pub const fn rules_mut(&mut self) -> &mut EthBlockRules { - &mut self.rules - } - /// Returns the accumulated block state. pub const fn block_state(&self) -> &BlockState { &self.block_state diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs index 58f9b75a2..01fa9a70a 100644 --- a/crates/evm2/src/block/ethereum/tests.rs +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -218,30 +218,14 @@ fn reads_rules_from_hardforks() { } #[test] -fn rejects_request_contract_without_code() { +fn request_contract_without_code_produces_no_requests() { let mut database = database(); database.insert_account_info(&WITHDRAWAL_REQUEST_ADDRESS, AccountInfo::default()); + database.insert_account_info(&CONSOLIDATION_REQUEST_ADDRESS, AccountInfo::default()); let evm = evm_with(SpecId::PRAGUE, 30_000_000, database); let mut executor = EthBlockExecutor::new(evm, ctx(), EthBlockRules::default(), EthReceiptBuilder); executor.apply_pre_execution_changes().unwrap(); - let error = executor.finish().unwrap_err(); - assert!(matches!( - error, - BlockExecutionError::Validation(BlockValidationError::SystemContractEmpty { address }) - if address == WITHDRAWAL_REQUEST_ADDRESS - )); -} - -#[test] -fn allowed_empty_request_contract_is_a_no_op() { - let mut database = database(); - database.insert_account_info(&WITHDRAWAL_REQUEST_ADDRESS, AccountInfo::default()); - database.insert_account_info(&CONSOLIDATION_REQUEST_ADDRESS, AccountInfo::default()); - let evm = evm_with(SpecId::PRAGUE, 30_000_000, database); - let rules = EthBlockRules { allow_empty_request_contracts: true, ..EthBlockRules::default() }; - let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); - executor.apply_pre_execution_changes().unwrap(); let output = executor.finish().unwrap(); assert!(output.result.requests.is_empty()); } From baf0d38f3b4f971e59b4aeb51de65e44d868d703 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:19:11 +0100 Subject: [PATCH 136/159] perf: read call input from the caller's memory (#1030) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `CALL`, `CALLCODE`, `DELEGATECALL` and `STATICCALL` copied their input out of the caller's memory into a new `Bytes` for every call and freed it when the call returned. The caller frame is suspended until its call returns, so the child can read that memory in place, the way geth passes `Memory.GetPtr` as the callee's input. `Message::input` is now a `MessageInput`: a `Bytes` that either owns its bytes or borrows them, with a flag saying which. It derefs to `[u8]` through the same pointer and length as before, so `CALLDATALOAD`, `CALLDATASIZE`, `CALLDATACOPY` and the JIT context read it without a branch. The JIT context does not change, and `Interpreter` stays 304 bytes: its one new flag fits in padding. `Message` grows by 8 bytes, which moves the fields that AOT code reads, so `ABI_VERSION` goes up. `Clone` and `to_bytes` copy borrowed bytes, so an input that anyone keeps always owns them. Only `Interpreter::call_input` creates a borrowed input. The interpreter's call instructions and the JIT `CALL` builtin use it, and it borrows only in frames that `Evm` runs, which it marks when it starts them; with any other host it copies as before. `call_with` copies the input before its hooks run, since they get the caller frame. What makes the borrow sound is what `Evm` does with the message until `execute_message` returns. With an inspector installed it copies the input before the first hook, because the call hooks, the EIP-7708 transfer log hook and the hooks of messages that a hook or precompile sends all reach the caller frame and the message with `&mut`. Without an inspector nothing can reach the caller frame, and the message goes only by shared reference to the child frame or a precompile, which cannot move the input out. So the borrowed bytes cannot change while anything reads them and cannot outlive the call, and inspectors see the same bytes as before: a hook that overwrites the caller's memory does not change what the child or a precompile reads, which new tests check for both, and for `call_with` hooks. ## Measurements Callgrind instructions of one measured iteration of the CodSpeed benchmarks (`cargo codspeed build --profile profiling -p evm2-cli --features jit --bench evm`, stable, `-Ctarget-cpu=x86-64`), each benchmark in its own process, base origin/main cf43e45f. | Benchmark | Base | Head | | | --- | ---: | ---: | :-: | | aggregation_router_v6 | 979,409 | 968,086 | 🟢 -1.16% | | succinct_staking | 99,508 | 98,747 | 🟢 -0.76% | | uniswap_v4_pool_manager | 110,563 | 109,938 | 🟢 -0.57% | | balancer_vault | 321,911 | 320,408 | 🟢 -0.47% | | curve_tricrypto2 | 622,995 | 620,321 | 🟢 -0.43% | | mainnet replay | 1,319,292,230 | 1,313,813,487 | 🟢 -0.42% | | aggregation_router_v6/jit/run | 419,658 | 408,751 | 🟢 -2.60% | | universal_router/jit/run | 130,531 | 128,109 | 🟢 -1.86% | | curve_tricrypto2/jit/run | 192,788 | 189,887 | 🟢 -1.50% | | succinct_staking/jit/run | 55,700 | 54,919 | 🟢 -1.40% | Across all 174 cases the geometric means are +0.04% for the 100 interpreter cases, -0.41% for JIT runs, and +0.03% and -0.02% for JIT translate and compile, whose cases all stay within ±0.6%. Benchmarks under about 30,000 instructions that make no call move by up to ±5% between any two builds; in `weth` (+1.7%) the whole difference is in glibc `_int_malloc` and friends while every evm2 function runs the same instructions. `scripts/jit/bench.py --diff origin/main` shows no change in codegen or compile instructions, and runtime instructions of -3.3%, -2.0% and -2.1% for aggregation_router_v6, universal_router and succinct_staking (geomean -0.2%). --- crates/evm2/src/ethereum/mod.rs | 6 +- crates/evm2/src/evm/inspector.rs | 124 ++++++++++++++++++ crates/evm2/src/evm/mod.rs | 14 +- .../evm2/src/interpreter/instructions/env.rs | 6 +- .../src/interpreter/instructions/system.rs | 50 ++++++- crates/evm2/src/interpreter/message.rs | 103 ++++++++++++++- crates/evm2/src/interpreter/mod.rs | 3 +- crates/evm2/src/interpreter/runtime.rs | 43 +++++- crates/inspectors/src/tracing/js/mod.rs | 4 +- crates/inspectors/src/tracing/mod.rs | 4 +- crates/jit/builtins/src/lib.rs | 12 +- crates/jit/codegen/src/lib.rs | 2 +- crates/jit/codegen/src/tests/mod.rs | 8 +- crates/jit/codegen/src/tests/runner.rs | 2 +- crates/jit/context/src/lib.rs | 2 +- crates/jit/runtime/src/evm2_evm.rs | 6 +- 16 files changed, 350 insertions(+), 39 deletions(-) diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 7688b4ca6..045eeb78f 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -641,7 +641,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( destination: to, call_target: to, caller, - input: input.clone(), + input: input.clone().into(), value, code, code_address, @@ -672,7 +672,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( destination, call_target: destination, caller, - input: input.clone(), + input: input.clone().into(), value, code: Bytecode::new_legacy(input.clone()), code_address: destination, @@ -714,7 +714,7 @@ pub fn halted_initial_frame( destination, call_target: destination, caller, - input: input.clone(), + input: input.clone().into(), value, code_address: destination, caller_is_static: false, diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 9456efe12..5d2150d80 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -1135,6 +1135,35 @@ mod tests { }; } + /// A call whose `before` hook overwrites the caller's memory. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn overwriting_call(cx: _) -> Result { + instr::call_with( + stack, + cx.gas, + cx.state, + MessageKind::Call, + |_, state, _| { + state.memory().set(0, &[0xbb; 32]); + Ok(()) + }, + |_, _, _, _| Ok(()), + ) + } + + /// The base config with [`overwriting_call`] installed at [`CUSTOM_CALL`]. + struct OverwritingCallConfig; + + impl EvmConfig for OverwritingCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + /// Runs a call to a child through `message_op`, which `C` installs as a call, followed by /// `immediates`. fn run_custom_call>( @@ -1788,6 +1817,101 @@ mod tests { assert_eq!(Word::from_be_slice(&result.output), Word::from(2)); } + /// An account that returns its call data in [`echo_db`]. + const ECHO: Address = Address::repeat_byte(0x22); + + /// Returns a database with an account at [`ECHO`] that returns its call data. + fn echo_db() -> InMemoryDB { + let mut db = InMemoryDB::default(); + db.insert_account_info( + &ECHO, + AccountInfo::default().with_code(legacy_bytecode(vec![ + op::CALLDATASIZE, + op::PUSH0, + op::PUSH0, + op::CALLDATACOPY, + op::CALLDATASIZE, + op::PUSH0, + op::RETURN, + ])), + ); + db + } + + /// Calls `target` through `call_op` with memory 0..32 as input and returns its 32-byte output. + fn memory_input_call_code(target: Address, call_op: u8) -> Vec { + let mut code = Vec::new(); + push_all(&mut code, [Word::from_be_bytes([0xaa; 32]), Word::ZERO]); + code.push(op::MSTORE); + push_all( + &mut code, + [ + Word::from(32), + Word::from(32), + Word::from(32), + Word::ZERO, + Word::ZERO, + address_to_word(&target), + Word::from(50_000), + ], + ); + code.extend([call_op, op::POP]); + push_all(&mut code, [Word::from(32), Word::from(32)]); + code.push(op::RETURN); + code + } + + #[test] + fn call_hook_overwriting_caller_memory_keeps_child_input() { + struct OverwriteCallerMemory; + + impl Inspector for OverwriteCallerMemory { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth > 0 { + interp.memory_mut().set(0, &[0xbb; 32]); + } + None + } + } + + for target in [ECHO, Address::with_last_byte(4)] { + let (result, _, _) = run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(target, op::CALL), + &MessageExt::default(), + 100_000, + OverwriteCallerMemory, + ); + + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32], "{target}"); + } + } + + #[test] + fn call_with_hook_overwriting_caller_memory_keeps_child_input() { + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_config::(), + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + echo_db(), + Precompiles::base(SpecId::OSAKA), + ); + let code = memory_input_call_code(ECHO, CUSTOM_CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32]); + } + #[test] fn create_too_deep_is_inspected_without_executing() { let mut code = create_code(); diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 17d32bac4..9264f8002 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -1354,6 +1354,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } impl<'a, T: EvmTypes> Evm<'a, T> { + /// The input of a call message may borrow the caller's memory (see + /// [`Interpreter::call_input`]), so this must not keep or clone out a borrowed input. #[inline] fn execute_message_impl( &mut self, @@ -1396,6 +1398,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { // SAFETY: The inspector is stored in `self`; the execution guard prevents inspector // replacement while the hooks are running. let inspector = unsafe { trustme::decouple_lt_mut(inspector) }; + // The hooks can change the caller's memory and keep the message, so the input must not + // borrow that memory. + message.input.make_owned(); // `destination` already holds the create's contract address (set when the message was // constructed), so the create hook observes it directly. @@ -1772,6 +1777,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { let guard = self.enter_execution(); let mut interp: Box> = guard.evm.interpreter_pool.pop(message); let interp_ref = interp.as_mut(); + interp_ref.lend_memory(); guard.evm.inspect_initialize_interp(interp_ref); let inspector = guard.evm.active_inspector().map(|inspector| { // SAFETY: The inspector is stored in `self` and remains alive for the duration of the @@ -2292,7 +2298,7 @@ mod tests { destination: address, call_target: address, caller: Address::ZERO, - input: Bytes::new(), + input: Default::default(), value: U256::ZERO, code: Bytecode::default(), code_address: address, @@ -2383,7 +2389,7 @@ mod tests { evm.set_inspector(LogInspector::default()); let mut message = precompile_message(TEST_PRECOMPILE); if revert { - message.input = Bytes::from_static(b"revert"); + message.input = Bytes::from_static(b"revert").into(); } let result = Host::execute_message(&mut evm, &mut message).unwrap(); @@ -3226,7 +3232,7 @@ mod tests { assert_eq!(message.depth, 67); assert_eq!(message.destination, TEST_PRECOMPILE); assert_eq!(message.caller, Address::with_last_byte(0x7a)); - assert_eq!(message.input, Bytes::from_static(b"message input")); + assert_eq!(&*message.input, b"message input"); assert_eq!(message.value, U256::from(99)); assert_eq!(message.code_address, TEST_PRECOMPILE); Ok(PrecompileOutput::new(Bytes::from(evm.block.number.to_be_bytes::<32>()))) @@ -3246,7 +3252,7 @@ mod tests { destination: address, call_target: address, caller: Address::with_last_byte(0x7a), - input: Bytes::from_static(b"message input"), + input: Bytes::from_static(b"message input").into(), value: U256::from(99), code: Bytecode::default(), code_address: address, diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index 71a3f7339..b98fcc3c9 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -250,7 +250,7 @@ mod tests { fn calldataload_opcode() { let input = Bytes::from(Vec::from([1_u8, 2, 3])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let interp = run(RunConfig::new([op::PUSH0, op::CALLDATALOAD, op::STOP]) .host(&mut host) @@ -271,7 +271,7 @@ mod tests { fn calldatasize_opcode() { let input = Bytes::from(Vec::from([1_u8, 2, 3, 4])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let interp = run(RunConfig::new([op::CALLDATASIZE, op::STOP]).host(&mut host).message(message)); assert_matches!(interp.err, InstrStop::Stop); @@ -282,7 +282,7 @@ mod tests { fn calldatacopy_opcode() { let input = Bytes::from(Vec::from([0xaa_u8, 0xbb, 0xcc])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let mut code = Vec::new(); push(&mut code, 2); push(&mut code, 1); diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 429db793d..3bb492db9 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -138,8 +138,12 @@ fn load_acc_and_calc_gas( Ok((gas_limit, new_account_state_gas, code, code_address)) } +/// # Safety +/// +/// The message must be passed to the host as in +/// [`Interpreter::call_input`](crate::interpreter::Interpreter::call_input). #[inline(never)] -fn prepare_call( +unsafe fn prepare_call( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, @@ -179,7 +183,8 @@ fn prepare_call( kind == MessageKind::Call, local_gas_limit, )?; - let input = memory_range_bytes(state, input_range)?; + // SAFETY: Upheld by the caller. + let input = unsafe { state.0.call_input(input_range.start, input_range.len()) }; let current = state.message(); let (destination, caller, call_value, code_address) = match kind { @@ -247,7 +252,8 @@ fn call_inner( state: &mut InterpreterState<'_, '_, T>, kind: MessageKind, ) -> Result { - call_with(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) + // SAFETY: The hooks do nothing. + unsafe { call_impl(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) } } /// Executes a call opcode of `kind` with hooks around its child message. @@ -273,6 +279,35 @@ fn call_inner( /// The result can be the stop for a config reload. Return it as the last step of the instruction. #[inline] pub fn call_with( + stack: StackMut<'_>, + gas: &mut Gas, + state: &mut InterpreterState<'_, '_, T>, + kind: MessageKind, + before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, + after: impl FnOnce( + &mut Gas, + &mut InterpreterState<'_, '_, T>, + &Message, + &MessageResult, + ) -> Result, +) -> Result { + let before = + |gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, message: &mut Message| { + message.input.make_owned(); + before(gas, state, message) + }; + // SAFETY: `before` copies the input before the hooks can reach it. + unsafe { call_impl(stack, gas, state, kind, before, after) } +} + +/// Body of [`call_with`]. The child message may borrow its input from memory. +/// +/// # Safety +/// +/// `before` and `after` must not read a borrowed input after changing memory, or let it outlive +/// the call. +#[inline] +unsafe fn call_impl( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, @@ -287,8 +322,11 @@ pub fn call_with( ) -> Result { let mut message = Message::::default(); let mut return_memory_range = 0..0; - let new_account_state_gas = - prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range)?; + // SAFETY: Upheld by the caller for the hooks. Nothing else reads the message after the host + // returns. + let new_account_state_gas = unsafe { + prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range) + }?; before(gas, state, &mut message)?; let mut result = @@ -453,7 +491,7 @@ pub fn create_with( call_target: destination, caller, code: Bytecode::new_legacy(input.clone()), - input, + input: input.into(), value, code_address: destination, // CREATE is rejected in a static context (see `require_non_staticcall`). diff --git a/crates/evm2/src/interpreter/message.rs b/crates/evm2/src/interpreter/message.rs index c2da3b326..4d2f609d8 100644 --- a/crates/evm2/src/interpreter/message.rs +++ b/crates/evm2/src/interpreter/message.rs @@ -2,8 +2,10 @@ use crate::{ BaseEvmTypes, EvmTypesHost, Version, bytecode::Bytecode, interpreter::{InstrStop, Word}, + trustme, }; use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; +use core::{fmt, ops::Deref}; /// EVM message kind. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] @@ -68,7 +70,7 @@ pub struct MessageExt { /// Immediate caller. pub caller: Address, /// Call input data, or initcode for create messages. - pub input: Bytes, + pub input: MessageInput, /// Value transferred with the message. pub value: U256, /// Bytecode resolved for this frame: the code at [`MessageExt::code_address`] (the resolved @@ -201,6 +203,87 @@ pub fn create_preaccess_failure( } } +/// The input of a message: call data, or initcode for create messages. +/// +/// It derefs to the input bytes. When [`Evm`](crate::Evm) runs without an inspector, the child of +/// a call instruction reads its input from the caller's memory instead of a copy, since the caller +/// waits for the call to return. [`Clone`] and [`to_bytes`](Self::to_bytes) copy borrowed bytes, so +/// a kept input always owns them. +#[derive(Default)] +pub struct MessageInput { + /// The owned bytes, or the borrowed bytes with their lifetime erased. + bytes: Bytes, + /// Whether `bytes` borrows. If set, `bytes` must never be cloned. + borrowed: bool, +} + +impl MessageInput { + /// Borrows `bytes` without copying. + /// + /// # Safety + /// + /// `bytes` must stay valid and unchanged for as long as the input is read. + #[inline] + pub(crate) unsafe fn borrowed(bytes: &[u8]) -> Self { + // SAFETY: The caller keeps the bytes alive while they are read, and `borrowed` keeps + // them from being cloned out. + Self { bytes: Bytes::from_static(unsafe { trustme::decouple_lt(bytes) }), borrowed: true } + } + + /// Returns the input as owned bytes, copying borrowed ones. + #[inline] + pub fn to_bytes(&self) -> Bytes { + if self.borrowed { Bytes::copy_from_slice(&self.bytes) } else { self.bytes.clone() } + } + + /// Copies borrowed bytes into an owned buffer. + #[inline] + pub(crate) fn make_owned(&mut self) { + if self.borrowed { + *self = Self::from(Bytes::copy_from_slice(&self.bytes)); + } + } +} + +impl Clone for MessageInput { + #[inline] + fn clone(&self) -> Self { + Self::from(self.to_bytes()) + } +} + +impl fmt::Debug for MessageInput { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.bytes.fmt(f) + } +} + +impl PartialEq for MessageInput { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.bytes == other.bytes + } +} + +impl Eq for MessageInput {} + +impl Deref for MessageInput { + type Target = [u8]; + + #[inline] + fn deref(&self) -> &[u8] { + &self.bytes + } +} + +impl From for MessageInput { + #[inline] + fn from(bytes: Bytes) -> Self { + Self { bytes, borrowed: false } + } +} + #[cfg(test)] mod tests { use super::*; @@ -260,4 +343,22 @@ mod tests { // Address-based: a delegate call to the executing account's own address is direct. assert!(delegated(MessageKind::DelegateCall, target).is_direct_call()); } + + #[test] + fn kept_borrowed_input_owns_its_bytes() { + let mut memory = [1, 2, 3]; + // SAFETY: `memory` is unchanged until `input` is dropped. + let input = unsafe { MessageInput::borrowed(&memory) }; + let clone = input.clone(); + let bytes = input.to_bytes(); + // SAFETY: `memory` is unchanged until `owned` owns its bytes. + let mut owned = unsafe { MessageInput::borrowed(&memory) }; + owned.make_owned(); + drop(input); + memory.fill(0); + + assert_eq!(&*clone, [1, 2, 3]); + assert_eq!(bytes[..], [1, 2, 3]); + assert_eq!(&*owned, [1, 2, 3]); + } } diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index 026c43565..dfcc08e64 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -33,7 +33,8 @@ pub use memory::Memory; mod message; pub use message::{ - Message, MessageExt, MessageKind, create_preaccess_failure, derive_create_destination, + Message, MessageExt, MessageInput, MessageKind, create_preaccess_failure, + derive_create_destination, }; mod host; diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 4fd0d7fe1..b9b423ded 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -1,6 +1,6 @@ use super::{ - BytecodeRef, Gas, Host, InstrStop, Memory, Message, Pc, Result, StackBacking, StackMut, - StackRef, Word, + BytecodeRef, Gas, Host, InstrStop, Memory, Message, MessageInput, Pc, Result, StackBacking, + StackMut, StackRef, Word, }; use crate::{ EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, Version, @@ -64,6 +64,10 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { spec: SpecId, features: EvmFeatures, is_static: bool, + /// Whether [`Self::call_input`] borrows memory. Set only for frames that [`Evm`](crate::Evm) + /// runs, whose host keeps a borrowed input from being read after the call or while the memory + /// changes. + lends_memory: bool, } // SAFETY: The interpreter's internal pointers are always valid. `pc` and `bytecode_ref` point into @@ -104,6 +108,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { output: 0..0, message: None, is_static: false, + lends_memory: false, return_data: Bytes::new(), host: None, inspector: None, @@ -130,6 +135,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.output = 0..0; self.message = Some(message); self.is_static = is_static; + self.lends_memory = false; self.return_data = Bytes::new(); } @@ -264,6 +270,39 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { &mut self.memory } + /// Returns `len` bytes of memory at `offset` as the input of a call message, borrowed when + /// this frame is run by [`Evm`](crate::Evm) and copied otherwise. + /// + /// # Safety + /// + /// The input must only be read as the input of a message passed to this frame's + /// [`Host::execute_message`](crate::interpreter::Host::execute_message), and memory must not + /// change and no other code may access this frame in between. It must not be read after + /// `execute_message` returns. + /// + /// # Panics + /// + /// Panics if the bytes are out of bounds of memory. + #[inline] + #[doc(hidden)] + pub unsafe fn call_input(&self, offset: usize, len: usize) -> MessageInput { + let bytes = self.memory.slice(offset, len); + if !self.lends_memory { + return Bytes::copy_from_slice(bytes).into(); + } + // SAFETY: The caller keeps memory unchanged until the host returns, and the host is + // `Evm`, which copies the input before any inspector hook can reach this frame or the + // message. Otherwise it only reads the message through shared references, which cannot + // move the input out, and cloning copies it. + unsafe { MessageInput::borrowed(bytes) } + } + + /// Lets [`Self::call_input`] borrow memory for the rest of this frame. + #[inline] + pub(crate) const fn lend_memory(&mut self) { + self.lends_memory = true; + } + /// Returns the current interpreter result. #[inline] pub const fn result(&self) -> Result { diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 807589178..cbeda5408 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -723,7 +723,7 @@ impl Inspector for JsInspector { self.push_call( message.depth, - Contract { caller, contract, value: message.value, input: message.input.clone() }, + Contract { caller, contract, value: message.value, input: message.input.to_bytes() }, message.kind.into(), message.gas_limit, interp.gas().refunded(), @@ -785,7 +785,7 @@ impl Inspector for JsInspector { caller: message.caller, contract: message.destination, value: message.value, - input: message.input.clone(), + input: message.input.to_bytes(), }, message.kind.into(), message.gas_limit, diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 2a127a179..1e4bf476b 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -745,7 +745,7 @@ impl Inspector for TracingInspector { let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.clone() + message.input.to_bytes() } else { Bytes::new() }; @@ -788,7 +788,7 @@ impl Inspector for TracingInspector { let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.clone() + message.input.to_bytes() } else { Bytes::new() }; diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 664a69fb5..bce84effd 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -242,7 +242,7 @@ pub unsafe fn __revmc_builtin_calldatacopy( let memory_offset = word_to_usize(memory_offset.to_u256())?; ensure_memory(ecx, memory_offset, len)?; let data_offset = data_offset.to_u256().saturating_to::(); - let input = ecx.input().as_ref(); + let input = ecx.input(); let input = unsafe { core::slice::from_raw_parts(input.as_ptr(), input.len()) }; ecx.memory_mut().set_data(memory_offset, data_offset, len, input); } @@ -728,7 +728,7 @@ pub unsafe fn __revmc_builtin_create( call_target: destination, caller, code: Bytecode::new_legacy(code.clone()), - input: code, + input: code.into(), value, code_address: destination, caller_is_static: false, @@ -795,12 +795,12 @@ pub unsafe fn __revmc_builtin_call( pop!(sp; in_offset, in_len, out_offset, out_len); let in_len = word_to_usize(in_len.to_u256())?; - let input = if in_len != 0 { + let in_offset = if in_len != 0 { let in_offset = word_to_usize(in_offset.to_u256())?; ensure_memory(ecx, in_offset, in_len)?; - Bytes::copy_from_slice(ecx.memory().slice(in_offset, in_len)) + in_offset } else { - Bytes::new() + 0 }; let out_len = word_to_usize(out_len.to_u256())?; @@ -820,6 +820,8 @@ pub unsafe fn __revmc_builtin_call( call_kind == CallKind::Call, local_gas_limit, )?; + // SAFETY: The message goes straight to the host, and nothing reads it after the host returns. + let input = unsafe { ecx.interpreter().call_input(in_offset, in_len) }; let current = ecx.message(); let (destination, caller, call_value, code_address) = match call_kind { diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index df1abf7d7..9bcc1ddf8 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -20,7 +20,7 @@ pub use linker::{Linker, shared_library_path}; /// ABI version of compiled artifacts. Bump when the calling convention, the execution layout, the /// `InstrStop` discriminants, or the stored artifact format change. -pub const ABI_VERSION: u32 = 6; +pub const ABI_VERSION: u32 = 7; /// Internal tests and testing utilities. Not public API. #[cfg(test)] diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 746566263..9c83fd64b 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -1366,7 +1366,7 @@ tests! { bytecode: &asm(&binary_choice_chain(11)), inspect_stack: Some(false), modify_message: Some(|message| { - message.input = Bytes::from_static(&[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]); + message.input = Bytes::from_static(&[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]).into(); }), expected_return: InstrStop::Return, expected_memory: &U256::from(606).to_be_bytes::<32>(), @@ -3032,7 +3032,7 @@ tests! { ], spec_id: SpecId::CANCUN, modify_message: Some(|message| { - message.input = Bytes::from(B256::with_last_byte(0x68)); + message.input = Bytes::from(B256::with_last_byte(0x68)).into(); message.value = U256::from(10); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, @@ -3535,7 +3535,7 @@ tests! { spec_id: SpecId::ISTANBUL, modify_message: Some(|message| { message.value = 1_U256; - message.input = alloy_primitives::bytes!("c0406226"); + message.input = alloy_primitives::bytes!("c0406226").into(); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, expected_stack: STACK_WHAT_INTERPRETER_SAYS, @@ -3627,7 +3627,7 @@ tests! { modify_message: Some(|message| { let mut input = [0; 96]; input[63] = 1; - message.input = Bytes::from(input); + message.input = Bytes::from(input).into(); }), inspect_stack: Some(false), expected_return: InstrStop::Stop, diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index 8902d449c..b50cc74b6 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -272,7 +272,7 @@ fn def_message(gas_limit: u64) -> Message { Message:: { destination: DEF_ADDR, caller: DEF_CALLER, - input: Bytes::from_static(DEF_CD), + input: Bytes::from_static(DEF_CD).into(), value: DEF_VALUE, code_address: DEF_ADDR, gas_limit, diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index 4dc141280..9fac08850 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -180,7 +180,7 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { /// Returns calldata bytes. #[inline] - pub const fn input(&self) -> &Bytes { + pub fn input(&self) -> &[u8] { &self.message().input } diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 37ba7d2fc..f4369f411 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -617,7 +617,7 @@ mod tests { gas_limit: 0xcd79195900 - 21_368, destination: outer, caller, - input: Bytes::from(input), + input: Bytes::from(input).into(), value: Word::from(10), code: Bytecode::new_legacy(Bytes::copy_from_slice(&outer_code)), code_address: outer, @@ -709,7 +709,7 @@ mod tests { call_target: parent, code_address: parent, caller, - input: bytes!("01020304"), + input: bytes!("01020304").into(), code: Bytecode::new_legacy(code.into()), ..Default::default() }; @@ -1005,7 +1005,7 @@ mod custom_host_tests { destination: Address::repeat_byte(0x11), caller: Address::repeat_byte(0x22), value: U256::from(0x33), - input: Bytes::from_static(&[0x44; 32]), + input: Bytes::from_static(&[0x44; 32]).into(), code: Bytecode::new_legacy(Bytes::from_static(&[ op::ADDRESS, op::PUSH0, From 06c288eb3b8fe537287f9371934044a4eb3fcbad Mon Sep 17 00:00:00 2001 From: grandizzy <38490174+grandizzy@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:26:50 +0200 Subject: [PATCH 137/159] chore: update .gitignore (#1035) --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4fd772a08..d3963b304 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/target +target/ .vscode .idea /tmp From 37fb326b91a76dd07308c703b3f3c85152a6230b Mon Sep 17 00:00:00 2001 From: Darius Crisan <87009391+crisand2@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:13:27 +0200 Subject: [PATCH 138/159] fix(inspectors): ignore logs emitted outside a call frame in TracingInspector (#1036) ## Problem `TracingInspector::log` calls `last_trace()`, which panics with `can't start step without starting a trace first` when no call frame is on the trace stack. Embedders can emit logs outside any frame. Tempo settles fees after the top-level call returns and emits a TIP-20 `Transfer` from `collect_fee_post_tx` via `evm.log(..)`. With `record_logs` enabled (e.g. reth `debug_traceTransaction` / `debug_traceBlockByNumber` with `callTracer` + `withLog: true`), every such transaction panics and the RPC returns `-32603 internal eth error`. Reproduces on Tempo mainnet v1.18.2, including Tempo's public RPC: ``` thread 'tokio-rt' panicked at crates/inspectors/src/tracing/mod.rs:765:30: can't start step without starting a trace first 5: last_trace_idx 6: last_trace 8: inspect_log 10: emit_event 17: transfer_fee_post_tx 18: collect_fee_post_tx 26: settle_transaction 37: reth_rpc::debug::debug_trace_transaction ``` ## Fix Return early from `log` and `selfdestruct` when the trace stack is empty. Behaviour with an active frame is unchanged; `log_count` is not bumped for ignored logs, so `CallLog` indices still match the logs recorded in the trace. Dropping the log matches pre-evm2 behaviour, where these post-execution logs never reached the inspector. Attaching them to the root frame instead is possible if you prefer trace logs to mirror receipts; happy to switch. ## Tests - Unit test: `log` and `selfdestruct` with no active frame and `record_logs` on do not panic and record nothing. - `cargo test -p evm2-inspectors`: 206 passed. - `cargo clippy -p evm2-inspectors --all-targets -- -D warnings`, `cargo fmt --check`: clean. --------- Co-authored-by: Matthias Seitz --- crates/inspectors/src/tracing/mod.rs | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 1e4bf476b..535793490 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -709,6 +709,11 @@ impl Inspector for TracingInspector { if self.config.record_logs { let log_count = self.log_count; self.log_count += 1; + // Embedders can emit logs outside any call, before the first or after the last. They + // still take a log index but have no trace to attach to. + if self.trace_stack.is_empty() { + return; + } let trace = self.last_trace(); trace.ordering.push(TraceMemberOrder::Log(trace.logs.len())); trace.logs.push( @@ -822,6 +827,9 @@ impl Inspector for TracingInspector { value: &U256, _host: &mut T::Host<'_>, ) { + if self.trace_stack.is_empty() { + return; + } let node = self.last_trace(); node.trace.selfdestruct_address = Some(*contract); node.trace.selfdestruct_refund_target = Some(*target); @@ -950,3 +958,38 @@ impl PendingStep { Self { trace_idx, step_idx, gas_before, gas_after_charge: None } } } + +#[cfg(test)] +mod tests { + use super::*; + use evm2::{BaseEvmTypes, Evm, Precompiles, ethereum::ethereum_tx_registry, evm::InMemoryDB}; + + #[test] + fn log_and_selfdestruct_without_active_frame() { + let mut inspector = TracingInspector::new(TracingInspectorConfig::none().record_logs()); + let mut host = Evm::::new( + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + InMemoryDB::default(), + Precompiles::base(SpecId::CANCUN), + ); + let traces = inspector.traces.clone(); + + Inspector::::log(&mut inspector, &Log::default(), &mut host); + + assert_eq!(inspector.log_count, 1); + assert!(inspector.traces.nodes().iter().all(|node| node.logs.is_empty())); + assert_eq!(inspector.traces, traces); + + Inspector::::selfdestruct( + &mut inspector, + &Address::ZERO, + &Address::ZERO, + &U256::ZERO, + &mut host, + ); + + assert_eq!(inspector.traces, traces); + } +} From 445ffd5db1fca4884cdbff7f89b59edebf4142ce Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:55:17 +0100 Subject: [PATCH 139/159] fix(inspector): reload the steps after the hooked instruction returns (#1001) --- crates/evm2/src/evm/inspector.rs | 208 ++++++++++-------- .../src/interpreter/dispatch/table/mod.rs | 13 +- crates/evm2/src/interpreter/dispatch/tco.rs | 13 +- .../src/interpreter/instructions/system.rs | 9 +- crates/evm2/src/interpreter/mod.rs | 6 +- crates/evm2/src/interpreter/runtime.rs | 46 ++-- crates/inspectors/src/tracing/limits.rs | 5 +- crates/inspectors/src/tracing/mux.rs | 48 ++-- crates/inspectors/tests/it/geth.rs | 141 ++++-------- crates/macros/src/lib.rs | 13 +- 10 files changed, 227 insertions(+), 275 deletions(-) diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 5d2150d80..f271387ea 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -32,12 +32,6 @@ impl OpcodeSet { self.0.set_bit(opcode as usize, true); } - /// Removes `opcode` from the set. - #[inline] - pub const fn remove(&mut self, opcode: u8) { - self.0.set_bit(opcode as usize, false); - } - /// Returns the opcodes in `self` or `other`. #[inline] pub fn union(self, other: Self) -> Self { @@ -58,8 +52,8 @@ impl FromIterator for OpcodeSet { /// What an [`Inspector`] observes of execution. /// /// Returned by [`Inspector::inspector_config`]. The EVM reads it when a frame starts and after each -/// child message returns into the frame, and runs the frame so that it only pays for what the -/// config observes. The default observes everything. +/// `hooked` instruction of the frame, such as a CALL or CREATE, and runs the frame so that it only +/// pays for what the config observes. The default observes everything. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InspectorConfig { steps: OpcodeSet, @@ -121,9 +115,10 @@ pub trait Inspector: NonStaticAny { /// Returns what this inspector observes, as an [`InspectorConfig`]. /// - /// Read when a frame starts and when a child message of the frame returns, so a child message - /// may change the answer. A change that a hook makes itself, such as in [`Self::step`], takes - /// effect when the next child message of the frame returns. The default observes everything. + /// Read when a frame starts and after each `hooked` instruction of the frame returns, such as a + /// CALL or CREATE, so a child message may change the answer. A change that a hook makes + /// itself, such as in [`Self::step`], takes effect after the next `hooked` instruction of the + /// frame. The default observes everything. #[inline] fn inspector_config(&self) -> InspectorConfig { InspectorConfig::new() @@ -468,7 +463,7 @@ mod tests { TestHost, TestTypes, call_code, create_code, legacy_bytecode, push, push_all, return_top_word, }, - utils::address_to_word, + utils::{WordExt, address_to_word}, }; use alloc::{boxed::Box, vec, vec::Vec}; use alloy_consensus::{TxLegacy, transaction::Recovered}; @@ -1109,28 +1104,64 @@ mod tests { }; } - /// A call that is not `hooked`, so a config reload would resume from a stale pc. - #[instruction(no_stack_preamble, dynamic_gas, EvmTypes = BaseEvmTypes)] - fn unflagged_call(cx: _) -> Result { + /// A hooked call that pushes a word after `call_with`. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn call_then_push(cx: _) -> Result { instr::call_with( - stack, + stack.reborrow(), cx.gas, cx.state, MessageKind::Call, |_, _, _| Ok(()), |_, _, _, _| Ok(()), - ) + )?; + stack.push_u64(0x77)?; + Ok(()) + } + + /// The base config with the call that pushes a word installed at [`CUSTOM_CALL`]. + struct CallThenPushConfig; + + impl EvmConfig for CallThenPushConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + + /// A hooked call that runs its child message through the host instead of `call_with`. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn host_call(cx: _) -> Result { + let [_, to, _, _, _, _, _] = stack.popn::<7>()?; + let to = to.to_address(); + let current = cx.state.message(); + let mut message = MessageExt { + kind: MessageKind::Call, + depth: current.depth + 1, + gas_limit: 10_000, + destination: to, + call_target: to, + caller: current.destination, + code_address: to, + ..Default::default() + }; + let result = + cx.state.host().execute_message(&mut message).map_err(|error| cx.state.fail(error))?; + stack.push_u64(result.stop.is_success() as u64) } - /// The base config with the call without the flag installed at [`CUSTOM_CALL`]. - struct UnflaggedCallConfig; + /// The base config with the call through the host installed at [`CUSTOM_CALL`]. + struct HostCallConfig; - impl EvmConfig for UnflaggedCallConfig { + impl EvmConfig for HostCallConfig { const BASE_SPEC_ID: SpecId = SpecId::OSAKA; const OPCODE_CONFIG: &'static OpcodeConfig = &{ let mut config = OpcodeConfig::::base::>(); - config.set_instruction::(CUSTOM_CALL, 100); + config.set_instruction::(CUSTOM_CALL, 100); config }; } @@ -1190,16 +1221,14 @@ mod tests { (result, *evm.clear_inspector_as::().unwrap()) } - #[test] - fn custom_call_opcode_rereads_the_steps() { - let (expected, full) = run_custom_call::( - ConfigInspector::new(OpcodeSet::ALL), - CUSTOM_CALL, - &[], - ); + /// Asserts that the frame steps the set that the child message of [`CUSTOM_CALL`] switches to, + /// from the opcode after it. + fn assert_custom_call_rereads_the_steps>() { + let (expected, full) = + run_custom_call::(ConfigInspector::new(OpcodeSet::ALL), CUSTOM_CALL, &[]); // The child message of the custom opcode switches the steps to `GAS`, and no steps before. - let (result, inspector) = run_custom_call::( + let (result, inspector) = run_custom_call::( ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), CUSTOM_CALL, &[], @@ -1210,13 +1239,13 @@ mod tests { } #[test] - #[should_panic(expected = "an instruction that runs a message must be `hooked`")] - fn message_instruction_that_is_not_hooked_is_rejected() { - run_custom_call::( - ConfigInspector::new(OpcodeSet::EMPTY), - CUSTOM_CALL, - &[], - ); + fn custom_call_opcode_rereads_the_steps() { + assert_custom_call_rereads_the_steps::(); + } + + #[test] + fn hooked_opcode_that_runs_a_message_through_the_host_rereads_the_steps() { + assert_custom_call_rereads_the_steps::(); } #[test] @@ -1261,38 +1290,37 @@ mod tests { assert_eq!(inspector.logs.len(), 1); } - #[test] - fn answered_call_can_change_the_parent_steps() { + /// Runs a `CALL` followed by `GAS`, `POP` and `STOP` through inspectors that switch their steps + /// when the child message starts, with the call answered by `answer` in the hook if given. + /// Asserts that the caller steps the new set from the opcode after the `CALL`, and returns the + /// result and the inspector of the run that steps every opcode. + fn assert_steps_follow_the_child( + answer: Option>, + ) -> (MessageResult, ConfigInspector) { let mut code = call_code(Address::repeat_byte(0x66)); code.extend([op::CALL, op::GAS, op::POP, op::STOP]); - let answer = MessageResultExt { - stop: InstrStop::Return, - output: Bytes::from_static(&[0xaa]), - ..Default::default() - }; let run = |inspector: ConfigInspector| { - run_evm_with_inspector( - code.clone(), - &MessageExt::default(), - 100_000, - inspector.answering_calls(answer.clone()), - ) + let inspector = match &answer { + Some(answer) => inspector.answering_calls(answer.clone()), + None => inspector, + }; + run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector) }; let (expected, full, _) = run(ConfigInspector::new(OpcodeSet::ALL)); - // The hook answers the call, so every step belongs to the caller. - assert_eq!(full.calls, 2); let call = full.steps.iter().position(|record| record.0 == op::CALL).unwrap(); let end = full.steps.len(); for (before, after, range, kept) in [ + // Starts stepping after the CALL. (OpcodeSet::EMPTY, OpcodeSet::ALL, call + 1..end, OpcodeSet::ALL), + // Stops stepping after the CALL. + (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), ( OpcodeSet::EMPTY, opcode_set([op::GAS, op::STOP]), call + 1..end, opcode_set([op::GAS, op::STOP]), ), - (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), ( opcode_set([op::CALL]), opcode_set([op::POP]), @@ -1309,52 +1337,52 @@ mod tests { assert_eq!(inspector.steps, keep(&full.steps), "{before:?} {after:?}"); assert_eq!(inspector.step_ends, keep(&full.step_ends), "{before:?} {after:?}"); } + // The first step after the CALL sees its success flag on the stack. + let (_, inspector, _) = + run(ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL)); + assert_eq!(inspector.steps[0], (op::GAS, full.steps[call + 1].1, vec![Word::ONE])); + (expected, *full) } #[test] fn child_message_can_change_the_parent_steps() { let mut code = call_code(Address::repeat_byte(0x66)); code.extend([op::CALL, op::GAS, op::POP, op::STOP]); - let run = |inspector| { - run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector) + let (uninspected, _) = run_evm_uninspected(InMemoryDB::default(), code, 100_000); + let (result, _) = assert_steps_follow_the_child(None); + assert_eq!(result, uninspected); + } + + #[test] + fn answered_call_can_change_the_parent_steps() { + let answer = MessageResultExt { + stop: InstrStop::Return, + output: Bytes::from_static(&[0xaa]), + ..Default::default() }; - let (expected, _) = run_evm_uninspected(InMemoryDB::default(), code.clone(), 100_000); - let (result, full, _) = run(ConfigInspector::new(OpcodeSet::ALL)); - assert_eq!(result, expected); - let call = full.steps.iter().position(|record| record.0 == op::CALL).unwrap(); + let (_, full) = assert_steps_follow_the_child(Some(answer)); + // The hook answers the call, so every step belongs to the caller. + assert_eq!(full.calls, 2); + } - let all = 0..full.steps.len(); - for (before, after, range, kept) in [ - // Starts stepping after the CALL. - (OpcodeSet::EMPTY, OpcodeSet::ALL, call + 1..all.end, OpcodeSet::ALL), - // Stops stepping after the CALL. - (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), - ( - OpcodeSet::EMPTY, - opcode_set([op::GAS, op::STOP]), - call + 1..all.end, - opcode_set([op::GAS, op::STOP]), - ), - ( - opcode_set([op::CALL]), - opcode_set([op::POP]), - call..all.end, - opcode_set([op::CALL, op::POP]), - ), - ] { - let (result, inspector, _) = run(ConfigInspector::new(before).switching_to(after)); - assert_eq!(result, expected, "{before:?} {after:?}"); - let keep = |records: &[(u8, u64, Vec)]| { - let records = records[range.clone()].iter(); - records.filter(|record| kept.contains(record.0)).cloned().collect::>() - }; - assert_eq!(inspector.steps, keep(&full.steps)); - assert_eq!(inspector.step_ends, keep(&full.step_ends)); - } - // The first step after the CALL sees its success flag on the stack. - let (_, inspector, _) = - run(ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL)); - assert_eq!(inspector.steps[0], (op::GAS, full.steps[call + 1].1, vec![Word::ONE])); + #[test] + fn code_after_call_with_runs_when_the_steps_change() { + let (expected, full) = run_custom_call::( + ConfigInspector::new(OpcodeSet::ALL), + CUSTOM_CALL, + &[], + ); + let gas = full.steps.iter().find(|record| record.0 == op::GAS).unwrap(); + // The instruction pushes a word after `call_with`, below the one that `GAS` pushes. + assert_eq!(gas.2, [Word::ONE, Word::from(0x77)]); + + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + CUSTOM_CALL, + &[], + ); + assert_eq!(result, expected); + assert_eq!(inspector.steps, core::slice::from_ref(gas)); } #[test] diff --git a/crates/evm2/src/interpreter/dispatch/table/mod.rs b/crates/evm2/src/interpreter/dispatch/table/mod.rs index 58cf1c3e1..a0d6c738e 100644 --- a/crates/evm2/src/interpreter/dispatch/table/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/table/mod.rs @@ -116,14 +116,13 @@ fn dispatch_inner, M: InspectMode, G: Dispat gas.sync_before_exec(state, dynamic_gas); if hooked { state.set_pc_stack_len(pc.as_ptr(), *stack.len); - state.set_hooked_running(true); } r = instr(&mut pc, stack.reborrow(), state); - if hooked { - state.set_hooked_running(false); - } if r.is_ok() { inc_pc(&mut pc, op); + if hooked { + state.note_inspector_config_change(); + } } gas.sync_after_exec(state, dynamic_gas); } @@ -138,6 +137,12 @@ fn dispatch_inner, M: InspectMode, G: Dispat state.set_result(Err(e)); cold_path(); return (Pc::new(core::ptr::null()), gas); + } else if hooked && state.reload_requested() { + // The frame continues after the instruction on the table for the new config. + cold_path(); + state.set_result(Err(InstrStop::Reload)); + state.set_pc_stack_len(pc.as_ptr(), *stack.len); + return (Pc::new(core::ptr::null()), gas); } (pc, gas) } diff --git a/crates/evm2/src/interpreter/dispatch/tco.rs b/crates/evm2/src/interpreter/dispatch/tco.rs index 984934d83..c0f974ec4 100644 --- a/crates/evm2/src/interpreter/dispatch/tco.rs +++ b/crates/evm2/src/interpreter/dispatch/tco.rs @@ -82,7 +82,6 @@ extern_table! { } if hooked { state.set_pc_stack_len(pc.as_ptr(), stack.len()); - state.set_hooked_running(true); } let (r, stack_len) = { // SAFETY: This is the only stack borrow outside inspector callbacks. @@ -91,9 +90,6 @@ extern_table! { (r, stack_view.len) }; stack.set_len(stack_len); - if hooked { - state.set_hooked_running(false); - } if dynamic_gas { remaining_gas.set(state.gas_mut().remaining()); } @@ -107,6 +103,15 @@ extern_table! { tail_return!(tail_call_restore(pc, stack, remaining_gas, state, instructions)); } super::inc_pc(&mut pc, OP); + if hooked { + state.note_inspector_config_change(); + if !M::INSPECT && state.reload_requested() { + // The frame continues after the instruction on the table for the new config. + cold_path(); + state.set_result(Err(InstrStop::Reload)); + tail_return!(tail_call_restore(pc, stack, remaining_gas, state, instructions)); + } + } if M::INSPECT { M::step_end(state, pc, stack.len()); remaining_gas.set(state.gas_mut().remaining()); diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 3bb492db9..79468ae02 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -275,8 +275,6 @@ fn call_inner( /// `after` runs. /// /// An `Err` from either hook stops the instruction with that error. -/// -/// The result can be the stop for a config reload. Return it as the last step of the instruction. #[inline] pub fn call_with( stack: StackMut<'_>, @@ -334,7 +332,6 @@ unsafe fn call_impl( if result.stop.is_fatal() { return Err(result.stop); } - state.note_inspector_config_change(); gas.merge_child_gas(result.gas, result.stop); // EIP-8037: a value-bearing CALL that creates the target charges NEW_ACCOUNT state // gas upfront on this frame. If the call does not succeed (depth/balance failure, @@ -351,7 +348,7 @@ unsafe fn call_impl( } state.swap_return_data(&mut result.output); stack.push_u64(result.is_success() as u64)?; - // A hook's stop or a config reload. + // A stop a hook set. state.result() } @@ -380,7 +377,6 @@ fn create_inner( /// A fatal child result returns before `after` runs. /// /// Both hooks may charge or credit `gas` or fail the instruction, as in [`call_with`]. -/// Its result can be the stop for a config reload, as in [`call_with`]. #[inline] pub fn create_with( mut stack: StackMut<'_>, @@ -506,7 +502,6 @@ pub fn create_with( if result.stop.is_fatal() { return Err(result.stop); } - state.note_inspector_config_change(); gas.merge_child_gas(result.gas, result.stop); // EIP-8037: when the opcode charged the conditional `create_state_gas` @@ -526,7 +521,7 @@ pub fn create_with( state.clear_return_data(); } stack.push(result.created_address_for_parent())?; - // A hook's stop or a config reload. + // A stop a hook set. state.result() } diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index dfcc08e64..7b880aa96 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -116,9 +116,9 @@ pub enum InstrStop { FatalExternalError, /// Invalid encoding of an instruction's immediate operand. InvalidImmediateEncoding, - /// The inspector's config changed while a child message ran. The frame leaves the dispatch, - /// continues after the instruction on the table for the new config, and never reports this - /// stop. + /// The inspector's config changed while a `hooked` instruction ran. The dispatch exits with + /// this stop after the instruction, and the frame continues on the table for the new config. No + /// instruction returns it, and a frame never reports it. #[doc(hidden)] Reload, } diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index b9b423ded..de4866774 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -48,8 +48,9 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { inspector: Option + 'host>>, /// Opcodes the inspector steps on the table this frame runs on. steps: OpcodeSet, - /// Whether the running instruction is `hooked`. - hooked_running: bool, + /// Whether the inspector's config changed while the last `hooked` instruction ran. The frame + /// leaves the dispatch after the instruction and continues on the table for the new config. + reload: bool, /// The last sparse inspected run's step set, the config table it was built from, and the /// table. #[derive_where(skip)] @@ -113,7 +114,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { host: None, inspector: None, steps: OpcodeSet::EMPTY, - hooked_running: false, + reload: false, inspect_table: None, spec: SpecId::DEFAULT, features: EvmFeatures::empty(), @@ -481,6 +482,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.features = version.features; self.host = Some(NonNull::from(host)); self.inspector = None; + self.reload = false; } /// Runs the host's instruction implementation for the opcode at `pc` on the interpreter @@ -605,8 +607,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { } cold_path(); self.result = Ok(()); - // SAFETY: The stop left the pc at the message instruction, which a next one follows. - self.pc = unsafe { self.pc.add(dispatch::instruction_len(*self.pc)) }; + self.reload = false; } } @@ -726,9 +727,8 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { self.0.steps } - /// Stops the instruction with [`InstrStop::Reload`] if the inspector's config no longer - /// matches the table this frame runs on. A message instruction calls it after its child - /// message and returns the result. + /// Requests a reload if the inspector's config no longer matches the table this frame runs on. + /// The dispatch calls it after each `hooked` instruction returns, and leaves for the reload. #[inline(always)] pub(crate) fn note_inspector_config_change(&mut self) { if self.0.inspector.is_some() { @@ -738,21 +738,18 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline(never)] fn reload_if_config_changed(&mut self) { - assert!(self.0.hooked_running, "an instruction that runs a message must be `hooked`"); - if self.0.result.is_ok() - && let Some(inspector) = self.0.inspector + if let Some(inspector) = self.0.inspector // SAFETY: The inspector pointer is valid while the frame executes. && unsafe { inspector.as_ref() }.inspector_config().steps() != self.0.steps { - self.set_result(Err(InstrStop::Reload)); + self.0.reload = true; } } - /// Records whether the running instruction is `hooked`, so that the dispatch stored the latest - /// state into the interpreter before it ran. + /// Returns whether the running instruction requested a reload. #[inline] - pub(crate) const fn set_hooked_running(&mut self, running: bool) { - self.0.hooked_running = running; + pub(crate) const fn reload_requested(&self) -> bool { + self.0.reload } #[inline] @@ -885,14 +882,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline] pub(crate) fn inspect_step_end(&mut self, pc: Pc, stack_len: usize) { self.set_pc_stack_len(pc.as_ptr(), stack_len); - let reload = self.0.result == Err(InstrStop::Reload); - if reload { - cold_path(); - // The hook sees the instruction as done, with the pc after it. - self.set_result(Ok(())); - // SAFETY: A next instruction follows the instruction at `pc`. - self.0.pc = unsafe { pc.as_ptr().add(dispatch::instruction_len(pc.op())) }; - } else if self.0.result.is_err() { + if self.0.result.is_err() { cold_path(); if self.0.result.is_err_and(InstrStop::is_out_of_gas) { // An out-of-gas frame has burned all its gas, so 0 is the real remaining gas. @@ -904,11 +894,9 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { inspector.as_mut().step_end(&mut self.0); } // A stop the hook set wins over the reload. - if reload { - self.0.pc = pc.as_ptr(); - if self.0.result.is_ok() { - self.set_result(Err(InstrStop::Reload)); - } + if self.0.reload && self.0.result.is_ok() { + cold_path(); + self.set_result(Err(InstrStop::Reload)); } } diff --git a/crates/inspectors/src/tracing/limits.rs b/crates/inspectors/src/tracing/limits.rs index 5f9a971ab..64552da76 100644 --- a/crates/inspectors/src/tracing/limits.rs +++ b/crates/inspectors/src/tracing/limits.rs @@ -70,9 +70,8 @@ impl TraceBudget { } /// Returns whether exceeding the budget can halt execution. - pub(crate) const fn can_halt(&self) -> bool { - self.limits.max_recorded_bytes.is_some() - && matches!(self.limits.behavior, TraceLimitBehavior::Halt) + pub(crate) fn can_halt(&self) -> bool { + self.limits.max_recorded_bytes.is_some() && self.limits.behavior == TraceLimitBehavior::Halt } /// Returns whether the budget was exceeded and the limits halt execution. diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index 4aa5b41bb..bf97e5d30 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -257,39 +257,21 @@ impl MuxInspector { } } -/// Runs `$body` with `$member` bound to each inspector of the mux, borrowed as the pattern says. -macro_rules! each_member { - (&mut $mux:ident, | $member:ident | $body:expr) => {{ - for (_, $member) in &mut $mux.children { - $body; - } - if let Some($member) = &mut $mux.four_byte { - $body; - } - if let Some($member) = &mut $mux.tracing { - $body; - } - }}; - (& $mux:ident, | $member:ident | $body:expr) => {{ - for (_, $member) in &$mux.children { - $body; +/// Calls a hook on every inspector of the mux. +macro_rules! fan_out { + ($mux:ident.$hook:ident($($arg:expr),*)) => {{ + for (_, child) in &mut $mux.children { + Inspector::::$hook(child, $($arg),*); } - if let Some($member) = &$mux.four_byte { - $body; + if let Some(inspector) = &mut $mux.four_byte { + Inspector::::$hook(inspector, $($arg),*); } - if let Some($member) = &$mux.tracing { - $body; + if let Some(inspector) = &mut $mux.tracing { + Inspector::::$hook(inspector, $($arg),*); } }}; } -/// Calls a hook on every inspector of the mux. -macro_rules! fan_out { - ($mux:ident.$hook:ident($($arg:expr),*)) => { - each_member!(&mut $mux, |member| Inspector::::$hook(member, $($arg),*)) - }; -} - impl Inspector for MuxInspector { #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { @@ -298,10 +280,14 @@ impl Inspector for MuxInspector { #[inline] fn inspector_config(&self) -> InspectorConfig { - let mut config = InspectorConfig::without_steps(); - each_member!(&self, |member| config = - config.union(Inspector::::inspector_config(member))); - config + let children = + self.children.iter().map(|(_, child)| Inspector::::inspector_config(child)); + let four_byte = self.four_byte.iter().map(Inspector::::inspector_config); + let tracing = self.tracing.iter().map(Inspector::::inspector_config); + children + .chain(four_byte) + .chain(tracing) + .fold(InspectorConfig::without_steps(), InspectorConfig::union) } #[inline] diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 6cc2e6704..4a46491a6 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -10,7 +10,7 @@ use crate::utils::{ use alloy_consensus::crypto::secp256k1::sign_message; use alloy_eips::eip7702::Authorization; use alloy_primitives::{ - Address, B256, Bytes, Log, TxKind, U256, address, bytes, hex, keccak256, map::HashMap, + Address, B256, Bytes, TxKind, U256, address, bytes, hex, keccak256, map::HashMap, }; use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::{ @@ -24,11 +24,11 @@ use alloy_rpc_types_trace::{ parity::{Action, CallType}, }; use evm2::{ - AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, EvmTypesHost, ExecutionConfig, - Inspector, InspectorConfig, OpcodeSet, Precompiles, Version, + AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, ExecutionConfig, Inspector, + InspectorConfig, OpcodeSet, Precompiles, Version, ethereum::ethereum_tx_registry, evm::{DbResult, DynDatabase, InMemoryDB}, - interpreter::{Interpreter, Message, MessageResult, Word}, + interpreter::{Interpreter, Word}, }; use evm2_inspectors::tracing::{ DebugInspector, Erc7562Error, Erc7562Inspector, KECCAK256_PREIMAGE_TRACER, @@ -36,7 +36,7 @@ use evm2_inspectors::tracing::{ TracingInspector, TracingInspectorConfig, }; use serde_json::{Value, json}; -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::BTreeMap; #[test] fn test_calltracer_invalid_opcode_errors() { @@ -2809,14 +2809,18 @@ fn mux_and_debug_inspector_forward_the_steps_of_their_members() { }; // The 4byte tracer only needs message hooks, and the keccak tracer steps `KECCAK256`. - let keccak_steps: OpcodeSet = [op::KECCAK256].into_iter().collect(); + let keccak_steps = [op::KECCAK256].into_iter().collect::(); assert_eq!(steps(&mux(&[&four_byte])), OpcodeSet::EMPTY); assert_eq!(steps(&mux(&[&four_byte, &keccak])), keccak_steps); - let options: GethDebugTracingOptions = - serde_json::from_value(serde_json::json!({"tracer": "4byteTracer"})).unwrap(); + let options = serde_json::from_value::( + serde_json::json!({"tracer": "4byteTracer"}), + ) + .unwrap(); assert_eq!(steps(&DebugInspector::new(options).unwrap()), OpcodeSet::EMPTY); - let options: GethDebugTracingOptions = - serde_json::from_value(serde_json::json!({"tracer": "keccak256PreimageTracer"})).unwrap(); + let options = serde_json::from_value::( + serde_json::json!({"tracer": "keccak256PreimageTracer"}), + ) + .unwrap(); assert_eq!(steps(&DebugInspector::new(options).unwrap()), keccak_steps); } @@ -3416,83 +3420,29 @@ fn test_geth_erc7562_operand_overflow_is_not_out_of_gas() { } } -/// Forwards every hook to `inner` and records the opcodes it steps. Steps every opcode if `all`. -struct SteppedSpy { - inner: I, - all: bool, - stepped: BTreeSet, +/// Asks to step `steps` and records the opcodes it is stepped for, which are those of every +/// inspector it runs with. +struct StepRecorder { + steps: OpcodeSet, + stepped: OpcodeSet, } -impl> Inspector for SteppedSpy { - fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { - self.inner.initialize_interp(interp); - } - +impl Inspector for StepRecorder { fn inspector_config(&self) -> InspectorConfig { - if self.all { InspectorConfig::new() } else { self.inner.inspector_config() } + InspectorConfig::new().with_steps(self.steps) } fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { self.stepped.insert(interp.opcode()); - self.inner.step(interp); - } - - fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { - self.inner.step_end(interp); - } - - fn log(&mut self, log: &Log, host: &mut ::Host<'_>) { - self.inner.log(log, host); - } - - fn call( - &mut self, - interp: &mut Interpreter<'_, '_, BaseEvmTypes>, - message: &mut Message, - ) -> Option> { - self.inner.call(interp, message) - } - - fn call_end( - &mut self, - interp: &mut Interpreter<'_, '_, BaseEvmTypes>, - message: &Message, - result: &mut MessageResult, - ) { - self.inner.call_end(interp, message, result); - } - - fn create( - &mut self, - interp: &mut Interpreter<'_, '_, BaseEvmTypes>, - message: &mut Message, - ) -> Option> { - self.inner.create(interp, message) - } - - fn create_end( - &mut self, - interp: &mut Interpreter<'_, '_, BaseEvmTypes>, - message: &Message, - result: &mut MessageResult, - ) { - self.inner.create_end(interp, message, result); - } - - fn selfdestruct( - &mut self, - contract: &Address, - target: &Address, - value: &U256, - host: &mut ::Host<'_>, - ) { - self.inner.selfdestruct(contract, target, value, host); } } /// Traces a contract that stores, loads, writes memory, logs and calls a second contract that /// stores and loads. -fn run_spy(config: TracingInspectorConfig, all: bool) -> SteppedSpy { +fn run_with_recorder( + config: TracingInspectorConfig, + recorder_steps: OpcodeSet, +) -> (TracingInspector, OpcodeSet) { let (contract, mut db) = contract_with_code(&hex!( // SSTORE(0, 5), SLOAD(0), MSTORE8(0, 0xaa), LOG0(0, 1), CALL(0x2000, 0x43), STOP. "6005" "5f55" "5f54" "50" "60aa" "5f53" "6001" "5f" "a0" @@ -3505,18 +3455,20 @@ fn run_spy(config: TracingInspectorConfig, all: bool) -> SteppedSpy::inspector_config(&TracingInspector::new(config)).steps() }; - let filter: OpcodeSet = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect(); + let filter = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect::(); let filtered = TracingInspectorConfig { record_opcodes_filter: Some(filter), ..TracingInspectorConfig::default_geth() @@ -3566,7 +3518,7 @@ fn tracing_inspector_steps_only_what_its_config_records() { #[test] fn tracing_inspector_traces_the_same_with_the_steps_it_asks_for_and_with_every_step() { - let filter: OpcodeSet = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect(); + let filter = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect::(); for (config, stepped) in [ (TracingInspectorConfig::none(), Some(OpcodeSet::EMPTY)), ( @@ -3589,24 +3541,17 @@ fn tracing_inspector_traces_the_same_with_the_steps_it_asks_for_and_with_every_s Some(filter), ), ] { - let asked = run_spy(config, false); - let every = run_spy(config, true); - assert_eq!( - format!("{:?}", asked.inner.traces()), - format!("{:?}", every.inner.traces()), - "{config:?}" - ); - let expected: OpcodeSet = match stepped { + // The recorder asks for nothing in the first run, so the tracer's own set decides what + // both are stepped for. In the second run the recorder asks for every opcode. + let (asked, asked_steps) = run_with_recorder(config, OpcodeSet::EMPTY); + let (every, every_steps) = run_with_recorder(config, OpcodeSet::ALL); + assert_eq!(format!("{:?}", asked.traces()), format!("{:?}", every.traces()), "{config:?}"); + match stepped { Some(set) => { - assert!(every.stepped.len() > set_len(set), "{config:?}"); - set + assert_eq!(asked_steps, set, "{config:?}"); + assert_ne!(every_steps, set, "{config:?}"); } - None => every.stepped.iter().copied().collect(), - }; - assert_eq!(asked.stepped.iter().copied().collect::(), expected, "{config:?}"); + None => assert_eq!(asked_steps, every_steps, "{config:?}"), + } } } - -fn set_len(set: OpcodeSet) -> usize { - (0..=u8::MAX).filter(|op| set.contains(*op)).count() -} diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index bba8e9c74..351b250ce 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -41,12 +41,13 @@ use syn::{ /// mutable gas state. /// - `#[instruction(hooked)]`: Marks an instruction that may run inspector hooks or host code that /// read the interpreter. Before the instruction runs, the dispatch stores the latest pc, stack -/// length and gas into the interpreter, so `hooked` implies `dynamic_gas`. Set it on an -/// instruction that runs a child message. The `call`, `call_end`, `create` and `create_end` hooks -/// get the interpreter from inside such an instruction. `step` and `step_end` get the current -/// state from the dispatch, and `initialize_interp` runs at frame start, so they need no flag. An -/// instruction that only reaches `log` or `selfdestruct` needs none either, because those hooks -/// get the host, which has no current frame. Flag it if a hook starts receiving the interpreter. +/// length and gas into the interpreter, so `hooked` implies `dynamic_gas`. After it returns, the +/// dispatch picks up a change of the inspector's config. Set it on an instruction that runs a +/// child message. The `call`, `call_end`, `create` and `create_end` hooks get the interpreter +/// from inside such an instruction. `step` and `step_end` get the current state from the +/// dispatch, and `initialize_interp` runs at frame start, so they need no flag. An instruction +/// that only reaches `log` or `selfdestruct` needs none either, because those hooks get the host, +/// which has no current frame. Flag it if a hook starts receiving the interpreter. /// - `#[instruction(EvmTypes = CustomTypes)]`: Implements the instruction for a concrete EVM type /// family instead of generating a generic implementation. /// - `#[instruction(EvmTypes: CustomTypesTrait)]`: Adds a trait bound to the generated generic EVM From fdc1598e250873f946aed12c6e295e864c48e771 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:45:08 +0100 Subject: [PATCH 140/159] perf(interpreter): keep the tco stack length in bytes on x86-64 (#1043) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tail-call dispatch backend passes `RawStack`'s length between handlers in a register. On x86-64 an address scale stops at 8, so with a word count every handler shifts the length left by 5 to address the top of the stack and keeps a copy of the old length for the update. `RawStack` now stores the length in bytes for the tco backend on x86-64, and `set_len` adds the instruction's word delta to it. Handlers address `[base + len - 64]` directly and update the length with an add or sub of 32. `Stack`, `StackMut` and all public APIs keep word lengths. Everything else is unchanged: the packed and unpacked backends, aarch64 (where `lsl #5` addressing is free) and other targets keep the word length and compile as on main. Adding the word delta rather than storing `len * 32` matters, because the latter leaves `((bytes >> 5) - 1) << 5` in JUMP, JUMPI and MSTORE and makes them longer than before. Handlers that call out of line through `&mut usize` (EXP, KECCAK256, the copy opcodes, SLOAD, LOG, CALL) pay a few extra instructions to convert at the boundary. Callgrind instructions, as CodSpeed builds it (`cargo codspeed build --profile profiling -p evm2-cli --bench evm`, generic x86-64), main against this PR: | Bench | main | PR | Δ | | --- | ---: | ---: | ---: | | factorial | 387,306 | 360,965 | 🟢 −6.80% | | fibonacci-calldata | 325,235 | 304,120 | 🟢 −6.49% | | onchain_lm_v2 | 3,153,957,573 | 2,968,577,124 | 🟢 −5.88% | | burntpix | 391,058,625 | 369,770,519 | 🟢 −5.44% | | snailtracer | 485,181,094 | 466,511,319 | 🟢 −3.85% | | mainnet replay | 1,118,688,510 | 1,100,677,541 | 🟢 −1.61% | | geomean of 32 benches | | | 🟢 −2.83% | Criterion wall time on an AMD EPYC 4484PX (Zen 5), medians of 5 alternating rounds with each binary pinned to one core, `80f24751` against its base `37fb326b`: | Build | Geomean of 32 cases | Best | Worst | | --- | ---: | --- | --- | | x86-64-v3, `-align-all-functions=6` | 🟢 −1.27% | factorial −12.0%, snailtracer −8.2%, burntpix −6.1% | 🔴 balancer_vault +3.4%, counter +1.9% | | generic x86-64 | 🟢 −2.03% | fibonacci −13.7%, balancer_vault −5.9%, uniswap_v3_pool −4.4% | 🔴 hash_10k +0.4% | The large precompile cases are flat within ±0.2%. The ~1 µs identity and sha256 cases move by up to +3.9%, which is noise on transactions that short. --- crates/evm2/src/interpreter/stack.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/evm2/src/interpreter/stack.rs b/crates/evm2/src/interpreter/stack.rs index 5289b4b37..7425ee38a 100644 --- a/crates/evm2/src/interpreter/stack.rs +++ b/crates/evm2/src/interpreter/stack.rs @@ -11,6 +11,12 @@ use core::{ /// EVM stack word. pub type Word = U256; +/// Units of [`RawStack`]'s stored length per stack word: the word size in bytes for the tail-call +/// dispatch on x86-64, where an address scale stops at 8 and a word index costs a shift, and 1 +/// elsewhere, where the scale is free or the table dispatch only reads the length at its entry. +const RAW_LEN_SCALE: usize = + if cfg!(all(tco, target_arch = "x86_64")) { size_of::() } else { 1 }; + /// Operand stack storage. /// /// Aligned to the word size so that no word crosses a cache line. Loads and stores that cross a @@ -35,6 +41,7 @@ pub struct Stack<'a> { #[derive(Clone, Copy)] pub(crate) struct RawStack<'a> { stack: *mut StackBacking, + /// Length in units of [`RAW_LEN_SCALE`] per word. len: usize, marker: PhantomData<&'a mut StackBacking>, } @@ -203,19 +210,28 @@ impl<'a> RawStack<'a> { #[inline(always)] pub(crate) const fn new(stack: *mut StackBacking, len: usize) -> Self { debug_assert!(len <= Stack::CAPACITY); - Self { stack, len, marker: PhantomData } + Self { stack, len: len * RAW_LEN_SCALE, marker: PhantomData } } #[inline(always)] pub(crate) const fn len(self) -> usize { - self.len + let raw = self.len; + // SAFETY: Type invariant. + unsafe { + core::hint::assert_unchecked(raw.is_multiple_of(RAW_LEN_SCALE)); + core::hint::assert_unchecked(raw <= Stack::CAPACITY * RAW_LEN_SCALE); + } + raw / RAW_LEN_SCALE } #[inline(always)] #[cfg(tco)] pub(crate) const fn set_len(&mut self, len: usize) { debug_assert!(len <= Stack::CAPACITY); - self.len = len; + // Add the word delta to the stored length instead of scaling `len`, so that a push or pop + // compiles to `stored ± RAW_LEN_SCALE` rather than `(stored / RAW_LEN_SCALE ± 1) * + // RAW_LEN_SCALE`. + self.len = self.len.wrapping_add(len.wrapping_sub(self.len()).wrapping_mul(RAW_LEN_SCALE)); } /// Borrows the stack backing. @@ -226,7 +242,7 @@ impl<'a> RawStack<'a> { #[inline(always)] pub(crate) unsafe fn borrow(&mut self) -> Stack<'_> { // SAFETY: The caller upholds exclusive access to the stack backing. - Stack::new(unsafe { &mut *self.stack }, self.len) + Stack::new(unsafe { &mut *self.stack }, self.len()) } } From 4ff2d86ae26a7deecf3a3501448e6b41051cbff8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:49:51 +0100 Subject: [PATCH 141/159] chore(deps): bump the ci-weekly group with 3 updates (#1044) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumps the ci-weekly group with 3 updates: [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv), [taiki-e/install-action](https://github.com/taiki-e/install-action) and [CodSpeedHQ/action](https://github.com/codspeedhq/action). Updates `astral-sh/setup-uv` from 10.3.2 to 10.3.3
Commits
  • 68559e9 Use Astral mirror for version manifests with GitHub fallback (#1119)
  • 0192b69 chore: update known checksums for 0.13.6 (#1117)
  • 2f29784 docs: use JSDoc for compatible release specifier comment (#1113)
  • 9c73321 fix: allow whitespace in uv requirement specifiers (#1112)
  • 80fbe01 fix: resolve ~= specifiers with PEP 440 semantics (#1111)
  • 36b7b57 fix: only report a saved cache when saving succeeded (#1110)
  • 56618ca docs: update version references to v10.3.2 (#1104)
  • See full diff in compare view

Updates `taiki-e/install-action` from 2.87.31 to 2.89.0
Release notes

Sourced from taiki-e/install-action's releases.

2.89.0

  • Support yq. (#2036, thanks @​FirelightFlagboy)

  • Support mold. (#2044, thanks @​FirelightFlagboy)

  • Fix an error when old cargo-binstall had already been installed at non-standard location. (#2048)

  • Update wasm-tools@latest to 1.263.0.

  • Update wasm-pack@latest to 0.15.1.

  • Update wasm-bindgen@latest to 0.2.130.

  • Update vacuum@latest to 0.36.1.

  • Update prek@latest to 0.5.9.

  • Update mise@latest to 2026.10.25.

  • Update kingfisher@latest to 2.16.0.

  • Update cargo-binstall@latest to 1.26.2.

2.88.1

  • Update wasmtime@latest to 50.0.1.

  • Update vacuum@latest to 0.36.0.

  • Update tombi@latest to 1.9.8.

  • Update mise@latest to 2026.10.23.

  • Update kingfisher@latest to 2.15.0.

  • Update espup@latest to 0.19.0.

  • Update deepsource@latest to 0.10.2.

  • Update biome@latest to 2.6.4.

2.88.0

2.87.33

  • Update vacuum@latest to 0.35.4.

  • Update uv@latest to 0.13.6.

  • Update trunk@latest to 0.22.1.

... (truncated)

Changelog

Sourced from taiki-e/install-action's changelog.

Changelog

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

[Unreleased]

[2.90.3] - 2026-11-08

  • Update yq@latest to 4.54.2.

  • Update vacuum@latest to 0.39.3.

  • Update syft@latest to 1.57.0.

  • Update rclone@latest to 1.76.0.

  • Update protoc-gen-connect-openapi@latest to 0.30.0.

  • Update mise@latest to 2026.11.3.

  • Update kingfisher@latest to 2.17.0.

  • Update biome@latest to 2.6.6.

[2.90.2] - 2026-11-06

  • Update trivy@latest to 0.76.2.

  • Update tombi@latest to 1.10.0.

  • Update prek@latest to 0.5.10.

  • Update mise@latest to 2026.11.2.

  • Update mdbook-d2@latest to 0.3.10.

  • Update cargo-zigbuild@latest to 0.24.1.

  • Update cargo-tarpaulin@latest to 0.37.6.

  • Update biome@latest to 2.6.5.

[2.90.1] - 2026-11-04

... (truncated)

Commits

Updates `CodSpeedHQ/action` from 5.6.0 to 5.6.1
Release notes

Sourced from CodSpeedHQ/action's releases.

v5.6.1

Bumps the CodSpeed runner to v5.6.1.

Fixes

  • Walltime: the runner no longer hangs after the benchmark exits when a Python process forks and leaves a corrupted perf jitdump (#599)
  • Simulation: stop the libpython symlink warning from firing falsely (#594)

🐛 Bug Fixes

💼 Other

Full Runner Changelog: https://github.com/CodSpeedHQ/codspeed/blob/main/CHANGELOG.md

What's Changed

New Contributors

Full Changelog: https://github.com/CodSpeedHQ/action/compare/v5.6.0...v5.6.1

Commits

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore ` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore ` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore ` will remove the ignore condition of the specified dependency and ignore conditions
Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/bench-comment.yml | 2 +- .github/workflows/bench.yml | 6 +++--- .github/workflows/ci.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/bench-comment.yml b/.github/workflows/bench-comment.yml index b120f7fa4..82f31bcee 100644 --- a/.github/workflows/bench-comment.yml +++ b/.github/workflows/bench-comment.yml @@ -68,7 +68,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 38f82514a..01e3dc867 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -48,7 +48,7 @@ jobs: with: cache-on-failure: true cache-bin: false - - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" @@ -249,13 +249,13 @@ jobs: cache-on-failure: true cache-bin: false - name: Install cargo-codspeed - uses: taiki-e/install-action@1e29709c0ec63babbe8f266397a465f29a825f91 # v2.87.31 + uses: taiki-e/install-action@1919c4a95903d2c33e4a221102edca87309fc14b # v2.89.0 with: tool: cargo-codspeed - name: Build the benchmark target run: cargo codspeed build --profile profiling -p evm2-cli --features jit --bench evm - name: Run the benchmarks - uses: CodSpeedHQ/action@78102a468f96d8e0ccb92d0ec49425753fc42062 # v5.6.0 + uses: CodSpeedHQ/action@61efe07e57e55bedeab750c434504de272a56e8c # v5.6.1 with: run: cargo codspeed run -p evm2-cli --bench evm mode: simulation diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bfe480496..c8cd5bc95 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,7 +105,7 @@ jobs: checksum: true fallback: none if: ${{ matrix.command == 'wasm-test' }} - - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" if: ${{ matrix.kind == 'eest' }} @@ -248,7 +248,7 @@ jobs: tool: nextest@0.9.140 checksum: true fallback: none - - uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" - name: Download EEST fixtures From a4e9f14ea4d5c4fd867817bc7ec51647f00c0a76 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:40:07 +0100 Subject: [PATCH 142/159] perf(interpreter): skip the RawStack length asserts when unscaled (#1047) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #1043. `RawStack::len` asserts that the stored length is a multiple of the scale and at most `CAPACITY * scale`. On the table backends the scale is 1, so these asserts hand LLVM range facts that it did not have before #1043. That changes register allocation in `Interpreter::run_inner` and `run_inspected_table` and the padding in front of the packed dispatch loop, which moves the hot loop within its cache line. The asserts now apply only when the scale is above 1, which is the tco backend on x86-64, so the table backends compile as they did before the byte length and the tco code is untouched. With this change, the packed binaries (generic `-Copt-level=3` and x86-64-v3 with `-align-all-functions=6`, for both the criterion bench binary and the `evm2` CLI) have 0 differing lines in every `evm2::interpreter` function against the code before #1043, `.text` size is identical, and the hot dispatch call in `run_inner` sits at the same offset in its 64-byte line. The cycle drift below is therefore removed. It was measured on the merged #1043 state against its base, with `evm2 run N --interpret` under `perf stat`, pinned to 4.0 GHz, 7 rounds. Instructions were unchanged (±0.00%) in every case. | Case | Cycles generic | Cycles v3a | | --- | ---: | ---: | | snailtracer | 🔴 +0.84% | 🟢 −0.55% | | burntpix | 🔴 +0.41% | 🟢 −0.65% | | factorial | 🔴 +0.03% | 🔴 +0.36% | | fibonacci-calldata | 🔴 +0.02% | 🟢 −0.02% | | onchain_lm_v2 | 🔴 +0.25% | 🔴 +0.01% | | hash_10k | 🟢 −0.01% | 🟢 −0.02% | | erc20_transfer | 🔴 +0.42% | 🔴 +0.69% | | uniswap_v3_pool | 🟢 −0.43% | 🟢 −0.36% | | mainnet replay | 🔴 +0.19% | | --- crates/evm2/src/interpreter/stack.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/evm2/src/interpreter/stack.rs b/crates/evm2/src/interpreter/stack.rs index 7425ee38a..4c39d1eb4 100644 --- a/crates/evm2/src/interpreter/stack.rs +++ b/crates/evm2/src/interpreter/stack.rs @@ -216,10 +216,13 @@ impl<'a> RawStack<'a> { #[inline(always)] pub(crate) const fn len(self) -> usize { let raw = self.len; - // SAFETY: Type invariant. - unsafe { - core::hint::assert_unchecked(raw.is_multiple_of(RAW_LEN_SCALE)); - core::hint::assert_unchecked(raw <= Stack::CAPACITY * RAW_LEN_SCALE); + // Only the scaled length gets these facts, so that the table backends compile unchanged. + if RAW_LEN_SCALE > 1 { + // SAFETY: Type invariant. + unsafe { + core::hint::assert_unchecked(raw.is_multiple_of(RAW_LEN_SCALE)); + core::hint::assert_unchecked(raw <= Stack::CAPACITY * RAW_LEN_SCALE); + } } raw / RAW_LEN_SCALE } From 014388e37f091042653a2b04353406fccc28cfd1 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 Nov 2026 17:14:00 +0100 Subject: [PATCH 143/159] fix(inspectors): keep logs emitted outside any frame in TransferInspector (#1040) `TransferInspector` only recorded logs inside a call frame. Embedders can emit logs before the first call or after the last, like Tempo's post-transaction fee transfer, and reth's `eth_simulateV1` with `traceTransfers` replaces a call's logs with the inspector's, so those logs vanished from the response. They now go straight to the transaction's logs, in emission order; no frame failure reverts them. --- crates/inspectors/src/transfer.rs | 24 +++++++++++++++++------- crates/inspectors/tests/it/transfer.rs | 23 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index bcc586ece..b2f824e09 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -27,6 +27,10 @@ pub const TRANSFER_EVENT_TOPIC: B256 = /// Like `go-ethereum`'s `eth_simulateV1` tracer, the logs are kept by the inspector and are never /// added to the EVM's own logs, so receipts and log blooms do not include them. /// +/// The inspector has no view of transaction errors: if a transaction stops on an error, the +/// transfers and logs recorded so far are kept and its open frames are only dropped at the next +/// top-level call. Discard the inspector after such an error instead of reusing it. +/// /// This can be used to construct an `eth_simulateV1` response. For `ots_getInternalOperations`, /// use [InternalOperationsInspector](crate::otterscan::InternalOperationsInspector). #[derive(Debug, Default, Clone)] @@ -40,7 +44,7 @@ pub struct TransferInspector { /// /// Can be used for [eth_simulateV1](https://github.com/ethereum/execution-apis/pull/484) execution. collect_logs: bool, - /// Logs of the top-level frames that succeeded. + /// Logs of the top-level frames that succeeded, and logs emitted outside any frame. logs: Vec, } @@ -78,12 +82,14 @@ impl TransferInspector { self } - /// Returns a reference to the logs of the succeeded top-level frames, in emission order. + /// Returns a reference to the logs of the succeeded top-level frames and the logs emitted + /// outside any frame, in emission order. pub fn logs(&self) -> &[Log] { &self.logs } - /// Takes the logs of the succeeded top-level frames, in emission order. + /// Takes the logs of the succeeded top-level frames and the logs emitted outside any frame, + /// in emission order. pub fn take_logs(&mut self) -> Vec { core::mem::take(&mut self.logs) } @@ -166,10 +172,14 @@ impl Inspector for TransferInspector { } fn log(&mut self, log: &Log, _host: &mut T::Host<'_>) { - if self.collect_logs - && let Some(frame) = self.frames.last_mut() - { - frame.logs.push(log.clone()); + if !self.collect_logs { + return; + } + match self.frames.last_mut() { + Some(frame) => frame.logs.push(log.clone()), + // Embedders can emit logs before the first call or after the last; no frame failure + // reverts them. + None => self.logs.push(log.clone()), } } diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index 241c9108f..d2ea975aa 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -4,8 +4,10 @@ use crate::utils::{ AccountInfo, Bytecode, Context, DatabaseCommit, ExecutionResult, Output, SpecId, TestDbExt, TransactTo, TxEnv, a, }; -use alloy_primitives::{Address, B256, Log, U256, address, bytes, hex}; -use evm2::evm::InMemoryDB; +use alloy_primitives::{Address, B256, Bytes, Log, U256, address, bytes, hex}; +use evm2::{ + BaseEvmTypes, Evm, Inspector, Precompiles, ethereum::ethereum_tx_registry, evm::InMemoryDB, +}; use evm2_inspectors::{ tracing::{TracingInspector, TracingInspectorConfig}, transfer::{ @@ -735,3 +737,20 @@ const CASES: &[TransferCase] = &[ expected: &[], }, ]; + +#[test] +fn logs_outside_any_frame_are_kept() { + // Embedders emit logs before the first call or after the last, e.g. a fee transfer. + let spec = evm2::SpecId::CANCUN; + let mut host = Evm::::new( + spec, + Default::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + let mut inspector = TransferInspector::new(false).with_logs(true); + let log = Log::new_unchecked(a(1), vec![TRANSFER_EVENT_TOPIC], Bytes::new()); + Inspector::::log(&mut inspector, &log, &mut host); + assert_eq!(inspector.logs(), [log]); +} From 31c64ae310bfdd2016ac509da0fcb6a86db933e5 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:15:53 +0100 Subject: [PATCH 144/159] perf(interpreter): align tco handlers to 64 bytes on x86-64 (#1053) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Aligns every tco instruction handler to 64 bytes on x86-64. Without it, each handler's start offset inside its 64-byte line depends on unrelated code size, so run time shifts with link layout. Aligned handlers run faster and vary less across layouts. Instruction counts are unchanged and `.text` grows by 16.9 KB (0.04%). Aligning all functions with `-Cllvm-args=-align-all-functions=6` gives about the same gain but costs 291 KB. On Apple M4 Pro (aarch64) the same attribute was neutral to slightly slower (+0.0% to +0.4%), so it applies on x86-64 only. Measured on an AMD EPYC 4484PX (Zen 4) pinned at 4.0 GHz with `perf stat` cycles, `evm2 run N --interpret`, bench profile. Each build was relinked with 6 lld `--shuffle-sections` seeds, over 4 rounds. The table shows the median over the 6 layouts, against main. | case | cycles | main's spread across layouts | aligned spread | |---|---:|---:|---:| | snailtracer | 🟢 −1.64% | 3.3% | 2.9% | | burntpix | 🟢 −4.68% | 5.1% | 3.3% | | factorial | 🟢 −1.26% | 2.6% | 0.6% | | fibonacci-calldata | 🟢 −7.00% | 16.5% | 1.9% | | onchain_lm_v2 | 🟢 −2.06% | 3.5% | 1.7% | | hash_10k | 🟢 −0.19% | | | | erc20_transfer | 🟢 −0.30% | | | | uniswap_v3_pool | 🟢 −2.12% | | | --- crates/evm2/src/interpreter/dispatch/tco.rs | 3 +++ crates/evm2/src/lib.rs | 1 + 2 files changed, 4 insertions(+) diff --git a/crates/evm2/src/interpreter/dispatch/tco.rs b/crates/evm2/src/interpreter/dispatch/tco.rs index c0f974ec4..868b89eec 100644 --- a/crates/evm2/src/interpreter/dispatch/tco.rs +++ b/crates/evm2/src/interpreter/dispatch/tco.rs @@ -41,6 +41,9 @@ pub(in crate::interpreter) fn run( } extern_table! { + // Without alignment, each handler's offset inside its 64-byte line depends on unrelated code + // size. Aligned handlers run faster and vary less across layouts. + #[cfg_attr(target_arch = "x86_64", rustc_align(64))] pub(super) fn dispatch< T: EvmTypesHost, C: EvmConfig, diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index 04841b159..c054b3c6e 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -1,5 +1,6 @@ #![doc = include_str!("../README.md")] #![cfg_attr(tco, feature(explicit_tail_calls), allow(incomplete_features))] +#![cfg_attr(tco, feature(fn_align))] #![cfg_attr(all(tco, not(cranelift)), feature(rust_preserve_none_cc))] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(test), warn(unused_crate_dependencies))] From 73d082e87a847dba0ab7fa7804640f302d8838db Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:18:16 +0100 Subject: [PATCH 145/159] perf(interpreter): align the table dispatch loop to 64 bytes (#1052) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The packed, unpacked and single_return backends spin in `run_inner`'s dispatch loop, which is about 32 bytes. Where it starts inside its 64-byte cache line depends on the size of unrelated code before it. On x86-64, a loop that starts at offset 48 crosses the line and runs 12% to 24% slower with identical instructions. This caused #1043's earlier packed regression. This PR puts `.p2align 6` in the loop preheader on x86-64 and aarch64, so the loop head always starts a line. The padding nops run once per `run_inner` call, and the function section becomes 64-byte aligned, which keeps the alignment under any link order. I measured on a Zen 4 (EPYC 4484PX, 4.0 GHz, `perf stat` cycles), `evm2 run N --interpret`, packed backend, generic x86-64. I relinked each build 6 times with lld `--shuffle-sections=.text*` and report the median over 4 rounds. On main, 1 of 6 layouts puts the loop at offset 48. With the change, the spread across layouts is 1.4% to 3.5% and instructions are unchanged. Worst layout, cycles: | case | main | this PR | change | | :-- | --: | --: | --: | | snailtracer | 248.2M | 214.8M | 🟢 −13.5% | | burntpix | 214.7M | 179.8M | 🟢 −16.3% | | factorial | 210.1k | 174.8k | 🟢 −16.8% | | fibonacci | 207.0k | 171.1k | 🟢 −17.3% | The unpacked backend behaves the same. On main, one layout in six runs 8.9% to 19.7% slower (snailtracer +17.3%, burntpix +19.7%, fibonacci +16.5%), a 10% to 22% spread across layouts. With the change the spread is 0.3% to 2.7% (6.1% for factorial), the medians move by −0.7% to +0.03%, and instructions are unchanged. On an Apple M4 Pro (aarch64) main shows no such cliff, and the change is neutral (snailtracer −0.03%, burntpix −0.5%, fibonacci +0.1%, erc20 +0.8%). The tco backend and the handlers are unchanged. --- crates/evm2/src/interpreter/dispatch/table/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/evm2/src/interpreter/dispatch/table/mod.rs b/crates/evm2/src/interpreter/dispatch/table/mod.rs index a0d6c738e..fcc14d459 100644 --- a/crates/evm2/src/interpreter/dispatch/table/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/table/mod.rs @@ -173,6 +173,13 @@ fn run_inner>( let mut loop_state = imp::loop_state(state.gas_mut()); let mut cursor = imp::Cursor::new(pc, stack.len()); let steps = if M::INSPECT { state.steps() } else { OpcodeSet::EMPTY }; + // Without this, the loop's offset in its 64-byte line depends on unrelated code size, and a + // loop that crosses a line costs about 20%. + #[cfg(all(not(miri), any(target_arch = "x86_64", target_arch = "aarch64")))] + // SAFETY: The directive only pads with nops. It reads and writes no memory or registers. + unsafe { + core::arch::asm!(".p2align 6", options(nomem, nostack, preserves_flags)); + } loop { let op = cursor.pc().op(); // A sparse set's table holds inspect handlers at the stepped opcodes and plain handlers From 00a59243c6622d5aeea83e2f5ca63bbabba07afc Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:49:25 +0100 Subject: [PATCH 146/159] refactor(jit): list all builtin symbols for JIT objects directly (#938) `compile_jit_object_artifact` collected the pending ORC symbol names and fell back to every builtin name when that list was empty. JIT objects compile on the AOT backend, which has no ORC state, so the list was always empty and the fallback always ran. The function now calls `builtin_symbol_names` directly. I confirmed the premise first: with an assertion that the pending list is empty, the `evm2-jit-runtime` tests and the EEST JIT and AOT state and blockchain suites all passed. --- crates/jit/runtime/src/runtime/worker.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index 92fe7b5a2..597a7fb41 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -707,15 +707,7 @@ pub(super) fn compile_jit_object_artifact( compiler: &mut EvmCompiler, ) -> Result { let object_bytes = compile_object(job, compiler)?; - let mut builtin_symbols: Vec = compiler - .backend() - .pending_symbol_names() - .into_iter() - .map(|name| name.to_string_lossy().into_owned()) - .collect(); - if builtin_symbols.is_empty() { - builtin_symbols = builtin_symbol_names(compiler.builtin_symbol_suffix()); - } + let builtin_symbols = builtin_symbol_names(compiler.builtin_symbol_suffix()); debug!( bytecode_len = job.bytecode.len(), From d1044b9ca477ebce028260a0ea3252030dc6243b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:50:34 +0100 Subject: [PATCH 147/159] test(inspectors): cover the erc7562 code size of an account created in the transaction (#976) `Erc7562Inspector` reads the code size of the account an `EXTCODE*` or call instruction addresses when the instruction runs, like geth, so an account that an earlier instruction of the same transaction created has a size. This adds the test for that case: a `CREATE` of a contract with 5 bytes of runtime code, then `EXTCODESIZE` of the created address, which reports size 5 for the `EXTCODESIZE` opcode. It passes on main, so the PR changes no inspector code. The test comes from #915, which #936 superseded. --- crates/inspectors/tests/it/geth.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index 4a46491a6..4c19d59b9 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -845,6 +845,25 @@ fn test_geth_erc7562_records_warm_first_reads() { assert_eq!(frame.calls[0].accessed_slots.reads[&slot], vec![value]); } +/// geth reads the code size when the opcode runs, so an account created earlier in the same +/// transaction has a size. +#[test] +fn test_geth_erc7562_contract_size_of_account_created_in_same_transaction() { + // CREATE a contract whose runtime code is 5 bytes, then EXTCODESIZE the created address. + let code = hex!("6460056000f36000526005601b6000f03b00"); + let (trace, _) = trace_contract( + SpecId::CANCUN, + GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()), + code.into(), + ); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let created = frame.calls[0].to.unwrap(); + assert_eq!(frame.contract_size.len(), 1); + assert_eq!(frame.contract_size[&created].contract_size, 5); + assert_eq!(frame.contract_size[&created].opcode, op::EXTCODESIZE); +} + fn trace_contract( spec: SpecId, opts: GethDebugTracingOptions, From ab55921eddcf2641d7016c15dea176c1e77e5080 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:56:43 +0100 Subject: [PATCH 148/159] refactor(jit): share the return body between do_return builtins (#954) `__revmc_builtin_do_return_cc` was a hand-written copy of the body of `__revmc_builtin_do_return`. Both now convert their arguments (stack words or raw `u64`s) and call one private `do_return_range`, so the memory check and output setup cannot drift apart. The builtin signatures are unchanged. The generated builtins do not regress: `do_return_cc` disassembles to the same instructions as on `origin/main`, and `do_return` has the same instruction count with only the empty-output block moved. `bench.py --diff origin/main` reports `opt.s changed in 0/31`. --- crates/jit/builtins/src/lib.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index bce84effd..8d85a3326 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -964,18 +964,8 @@ pub unsafe fn __revmc_builtin_do_return( ) -> BuiltinResult { let rev![offset, len] = sp; let len = word_to_usize(len.to_u256())?; - let output = if len != 0 { - let offset = word_to_usize(offset.to_u256())?; - let Some(end) = offset.checked_add(len) else { - return Err(InstrStop::MemoryOOG.into()); - }; - ensure_memory(ecx, offset, len)?; - offset as u32..end as u32 - } else { - 0..0 - }; - ecx.interpreter_mut().set_output(output); - Err(result.into()) + let offset = if len != 0 { word_to_usize(offset.to_u256())? } else { 0 }; + do_return_range(ecx, offset, len, result) } #[inline] @@ -985,8 +975,16 @@ pub unsafe fn __revmc_builtin_do_return_cc( len: u64, result: InstrStop, ) -> BuiltinResult { - let offset = offset as usize; - let len = len as usize; + do_return_range(ecx, offset as usize, len as usize, result) +} + +#[inline] +fn do_return_range( + ecx: &mut EvmContext<'_, '_, '_, T>, + offset: usize, + len: usize, + result: InstrStop, +) -> BuiltinResult { let output = if len != 0 { let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG.into()); From 71a736c96d1dae3bde96936cbb89cbcee7336793 Mon Sep 17 00:00:00 2001 From: RealiCZ Date: Tue, 10 Nov 2026 00:58:00 +0800 Subject: [PATCH 149/159] feat(evm): make the EIP-8037 total transaction gas cap configurable (#1023) EIP-8037 caps the total transaction gas limit, including the state-gas reservoir, at 2^32-1 (`TX_MAX_TOTAL_GAS_LIMIT`, issue #807). #862 enforces that cap in `Version::tx_gas_caps`, which rejects larger `tx.gas` with `TxGasLimitGreaterThanCap`. The cap is hard-coded. A chain whose block gas limit goes above 2^32-1 cannot use EIP-8037 at all: any transaction large enough to fill such a block is rejected before execution. This adds `Version::tx_total_gas_cap` so the limit is configurable per chain. - The field defaults to `MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM` in every base version, so Ethereum behaviour from #862/#807 is unchanged and still enforced by default. - `tx_gas_caps` reads it under EIP-8037, where it bounds `tx.gas`; the 2^24 execution cap and the execution/reservoir split are untouched. - `u64::MAX` disables the total cap, matching how the other caps are disabled. - Without EIP-8037 the field is unused, and `tx_gas_limit_cap` (EIP-7825) still bounds `tx.gas`. Tests in `crates/evm2/src/version/mod.rs` and `crates/evm2/src/ethereum/mod.rs` cover the default rejecting `2^32`, a raised cap accepting it, `u64::MAX` disabling it, the cap still applying only with EIP-8037, and the base version exposing the default value. Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/evm2/src/ethereum/mod.rs | 45 +++++++++++++++++++++++++++++++++ crates/evm2/src/version/mod.rs | 34 +++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 045eeb78f..9c2e56317 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -1492,6 +1492,51 @@ mod tests { ); } + #[test] + fn amsterdam_total_gas_cap_is_configurable() { + let gas_limit = 1u64 << 32; + + // The default keeps the EIP-8037 cap from #862. + let amsterdam = Version::base(SpecId::AMSTERDAM); + assert_eq!( + validate_tx_gas_limit_cap(amsterdam, gas_limit), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + }) + ); + + // A chain with larger blocks can raise the cap above the execution cap. + let mut raised = Version::new(SpecId::AMSTERDAM); + raised.tx_total_gas_cap = 10_000_000_000; + assert_eq!(validate_tx_gas_limit_cap(&raised, gas_limit), Ok(())); + assert_eq!( + validate_tx_gas_limit_cap(&raised, raised.tx_total_gas_cap + 1), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit: raised.tx_total_gas_cap + 1, + cap: raised.tx_total_gas_cap + }) + ); + + // `u64::MAX` disables the total cap. + let mut disabled = Version::new(SpecId::AMSTERDAM); + disabled.tx_total_gas_cap = u64::MAX; + assert_eq!(validate_tx_gas_limit_cap(&disabled, gas_limit), Ok(())); + assert_eq!(validate_tx_gas_limit_cap(&disabled, u64::MAX), Ok(())); + + // Without EIP-8037 the field is unused and the execution cap bounds `tx.gas`. + let mut without_eip8037 = Version::new(SpecId::AMSTERDAM); + without_eip8037.features.remove(EvmFeatures::EIP8037); + without_eip8037.tx_total_gas_cap = u64::MAX; + assert_eq!( + validate_tx_gas_limit_cap(&without_eip8037, gas_limit), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: without_eip8037.tx_gas_limit_cap + }) + ); + } + #[rstest::rstest] fn amsterdam_handler_rejects_gas_limit_above_total_cap( #[values(MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1)] diff --git a/crates/evm2/src/version/mod.rs b/crates/evm2/src/version/mod.rs index 015c51d14..b31acfa5e 100644 --- a/crates/evm2/src/version/mod.rs +++ b/crates/evm2/src/version/mod.rs @@ -37,6 +37,12 @@ pub struct Version { pub chain_id: u64, /// Transaction gas limit cap. pub tx_gas_limit_cap: u64, + /// EIP-8037 total transaction gas limit cap. + /// + /// Bounds `tx.gas` when [`EvmFeatures::EIP8037`] is active. `u64::MAX` disables the cap. The + /// default is [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`] (`2^32 - 1`). Without EIP-8037 this field + /// is unused and [`tx_gas_limit_cap`](Self::tx_gas_limit_cap) bounds `tx.gas` instead. + pub tx_total_gas_cap: u64, /// Hard memory limit in bytes. /// /// Frame output ranges are `u32`, so @@ -89,7 +95,8 @@ impl Version { /// Without EIP-8037, [`tx_gas_limit_cap`](Self::tx_gas_limit_cap) (EIP-7825) bounds `tx.gas` /// itself, which also bounds the execution gas. With EIP-8037, `tx.gas` may exceed it because /// the excess funds the state-gas reservoir, so it only bounds the required execution gas, and - /// `tx.gas` is bounded by [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`] instead. + /// `tx.gas` is bounded by [`tx_total_gas_cap`](Self::tx_total_gas_cap) instead. `u64::MAX` + /// disables that total cap; the default is [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`]. /// /// The Ethereum transaction handlers enforce these caps through /// [`validate_tx_gas_limit_cap`](crate::ethereum::validate_tx_gas_limit_cap) and @@ -99,7 +106,7 @@ impl Version { #[inline] pub const fn tx_gas_caps(&self) -> (u64, u64) { if self.feature(EvmFeatures::EIP8037) { - (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, self.tx_gas_limit_cap) + (self.tx_total_gas_cap, self.tx_gas_limit_cap) } else { (self.tx_gas_limit_cap, u64::MAX) } @@ -168,6 +175,7 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { features: EvmFeatures::empty(), chain_id: DEFAULT_CHAIN_ID, tx_gas_limit_cap: u64::MAX, + tx_total_gas_cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, memory_limit: DEFAULT_MEMORY_LIMIT, max_code_size: MAX_CODE_SIZE, max_initcode_size: MAX_INITCODE_SIZE, @@ -184,6 +192,7 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { features: base_features(spec_id), chain_id: DEFAULT_CHAIN_ID, tx_gas_limit_cap: base_tx_gas_limit_cap(spec_id), + tx_total_gas_cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, memory_limit: DEFAULT_MEMORY_LIMIT, max_code_size: base_max_code_size(spec_id), max_initcode_size: base_max_initcode_size(spec_id), @@ -344,6 +353,7 @@ mod tests { assert!(amsterdam.feature(EvmFeatures::EIP8246)); assert_eq!(amsterdam.chain_id, DEFAULT_CHAIN_ID); assert_eq!(amsterdam.tx_gas_limit_cap, MAX_TX_GAS_LIMIT_OSAKA); + assert_eq!(amsterdam.tx_total_gas_cap, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM); assert_eq!(amsterdam.memory_limit, DEFAULT_MEMORY_LIMIT); assert_eq!(amsterdam.max_code_size, MAX_CODE_SIZE_AMSTERDAM); assert_eq!(amsterdam.max_initcode_size, MAX_INITCODE_SIZE_AMSTERDAM); @@ -423,6 +433,26 @@ mod tests { assert_eq!(amsterdam_without_eip8037.tx_gas_caps(), (MAX_TX_GAS_LIMIT_OSAKA, u64::MAX)); } + #[test] + fn tx_total_gas_cap_is_configurable() { + // Without EIP-8037 the total cap is unused, so a disabled value changes nothing. + let mut osaka = Version::new(SpecId::OSAKA); + osaka.tx_total_gas_cap = u64::MAX; + assert_eq!(osaka.tx_gas_caps(), (MAX_TX_GAS_LIMIT_OSAKA, u64::MAX)); + + let mut amsterdam = Version::new(SpecId::AMSTERDAM); + assert_eq!( + amsterdam.tx_gas_caps(), + (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_GAS_LIMIT_OSAKA) + ); + + amsterdam.tx_total_gas_cap = 10_000_000_000; + assert_eq!(amsterdam.tx_gas_caps(), (10_000_000_000, MAX_TX_GAS_LIMIT_OSAKA)); + + amsterdam.tx_total_gas_cap = u64::MAX; + assert_eq!(amsterdam.tx_gas_caps(), (u64::MAX, MAX_TX_GAS_LIMIT_OSAKA)); + } + #[test] #[should_panic(expected = "initial call depth must leave room")] fn execution_config_rejects_unrepresentable_call_budget() { From 52c57092b973157aa51f71e13c224223bd09c06b Mon Sep 17 00:00:00 2001 From: RealiCZ Date: Tue, 10 Nov 2026 00:58:34 +0800 Subject: [PATCH 150/159] feat(state): expose transaction-original account information (#1025) Closes #1024 The pending state already keeps each account's start-of-transaction info (the `original` side of `AccountChanges`/`AccountChangeRef`, exposed through `TxStateChanges` by #840 and used by the `stateDiff` inspector after #899). Callers that hold an `AccountHandle` during execution cannot see it: `get`/`balance`/`nonce`/`code_hash` all report the live overlay, and `original_code` is the only way to reach the transaction boundary, for bytecode only. This adds `AccountHandle::original()`, returning the account info at the transaction boundary as `Option<&AccountInfo>` (`None` when the account did not exist then). It is the account's own baseline, so it is unaffected by mutations made through any frame and by loading bytecode into the present cache. State-diff tooling and chain rules that compare an account against its start-of-transaction state can use it instead of re-reading the backing database mid-transaction, without detaching the pending state. The existing `is_changed` helper on the internal account entry is left private: `original()` plus `get()` is enough to compute the same transition, so no extra API is added. A unit test in `crates/evm2/src/evm/state/account.rs` covers an unchanged account, changed balance and nonce, an account created during the transaction (`None`), and code loaded into the cache not moving the baseline. --- crates/evm2/src/evm/state/account.rs | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 1e8eeb94c..90e8c6d04 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -382,6 +382,17 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.present.as_ref() } + /// Returns the account information at the transaction boundary, before any frame mutated it. + /// + /// `None` means the account did not exist at the start of the transaction. Loading bytecode + /// into the present overlay does not change this baseline; committing and starting another + /// transaction does. Use [`Self::original_code`] to load the account's start-of-transaction + /// bytecode. + #[inline] + pub const fn original(&self) -> Option<&AccountInfo> { + self.tracked.original.as_ref() + } + /// Returns whether the account currently exists in the overlay. #[inline] pub const fn exists(&self) -> bool { @@ -829,6 +840,53 @@ mod tests { assert_eq!(state.account(&overlay_address).unwrap().load_code().unwrap().hash_slow(), seed); } + #[test] + fn account_original_reports_transaction_baseline() { + let address = Address::repeat_byte(0x93); + let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let original = + empty_account().with_balance(Word::from(10)).with_nonce(3).with_code(code.clone()); + let mut database = CacheDB::default(); + database.insert_account_info(&address, original.clone()); + let mut state = State::new(database); + + // An unchanged account reports its start-of-transaction info. + { + let account = state.account(&address).unwrap(); + assert_eq!(account.original(), Some(&original)); + assert_eq!(account.get(), Some(&original)); + } + + // Mutations move the present overlay but leave the baseline alone. + { + let mut account = state.account(&address).unwrap(); + account.set_balance(Word::from(99)); + account.set_nonce(4); + assert_eq!(account.original(), Some(&original)); + assert_eq!(account.balance(), Word::from(99)); + assert_eq!(account.nonce(), 4); + } + + // Loading bytecode into the present cache does not move the baseline either. + { + let mut account = state.account(&address).unwrap(); + assert_eq!(account.load_code().unwrap(), code); + assert_eq!(account.original(), Some(&original)); + } + + // An account absent at the transaction boundary keeps no baseline, including after the + // transaction creates it. + let created = Address::repeat_byte(0x94); + { + let mut account = state.account(&created).unwrap(); + assert_eq!(account.original(), None); + account.get_or_insert().balance = Word::ONE; + account.mark_created(); + assert_eq!(account.original(), None); + assert_eq!(account.balance(), Word::ONE); + } + } + #[test] fn journaled_existing_account_field_changes_roll_back_granularly() { let address = Address::repeat_byte(0x8b); From 24dd3770044cd29b84b2207da307ba58fa623846 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 Nov 2026 18:11:51 +0100 Subject: [PATCH 151/159] fix(inspectors): keep access list slots touched before a later top-level call (#1048) AccessListInspector pruned excluded addresses before every top-level call, so in a transaction with several top-level calls, such as an account-abstraction batch, a later call to a contract dropped the slots earlier calls had touched in it. Seeded entries are now pruned only before the first top-level call. --- crates/inspectors/src/access_list.rs | 21 ++++++--- crates/inspectors/tests/it/accesslist.rs | 58 +++++++++++++++++++++++- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 8d254c2ac..203d53fad 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -20,6 +20,9 @@ pub struct AccessListInspector { excluded: HashSet
, /// All addresses and touched slots touched_slots: HashMap>, + /// Whether the seeded entries of excluded addresses were removed, which happens once, before + /// the first top-level call. + seeds_pruned: bool, } impl From for AccessListInspector { @@ -37,7 +40,7 @@ impl AccessListInspector { for item in access_list.0 { touched_slots.entry(item.address).or_default().extend(item.storage_keys); } - Self { excluded: Default::default(), touched_slots } + Self { excluded: Default::default(), touched_slots, seeds_pruned: false } } /// Excludes additional addresses from the final access list. @@ -109,16 +112,22 @@ impl AccessListInspector { access_list } - /// Collects addresses which should be excluded from the access list. Must be called before the - /// top-level call. + /// Collects addresses which should be excluded from the access list. Must be called before + /// each top-level call. /// - /// Those include caller, callee and precompiles. + /// Those include caller, callee and precompiles. In a transaction with several top-level + /// calls, such as an account-abstraction batch, entries touched by earlier calls are kept even + /// if a later call targets their address. fn collect_excluded_addresses(&mut self, message: &Message, host: &Evm<'_, T>) { self.excluded.extend( [message.caller, message.destination].into_iter().chain(host.precompiles().addresses()), ); - // Remove seeded excluded entries; SLOAD/SSTORE can re-add slots accessed during execution. - self.touched_slots.retain(|address, _| !self.excluded.contains(address)); + if !self.seeds_pruned { + self.seeds_pruned = true; + // Remove seeded excluded entries; SLOAD/SSTORE can re-add slots accessed during + // execution. + self.touched_slots.retain(|address, _| !self.excluded.contains(address)); + } } } diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index 60dca894c..007311c4f 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -4,8 +4,13 @@ use crate::utils::{AccountInfo, Bytecode, Context, TransactTo, TxEnv, a}; use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; -use alloy_primitives::{Address, B256, Signature, U256, address, bytes, hex}; -use evm2::{ethereum::TxEnvelope, evm::InMemoryDB}; +use alloy_primitives::{Address, B256, Bytes, Signature, TxKind, U256, address, bytes, hex}; +use evm2::{ + BaseEvmTypes, Evm, ExecutionConfig, Precompiles, SpecId, Version, + ethereum::{TxEnvelope, ethereum_tx_registry, execute_initial_frame, prepare_initial_frame}, + evm::InMemoryDB, + interpreter::GasTracker, +}; use evm2_inspectors::access_list::AccessListInspector; #[test] @@ -137,6 +142,55 @@ fn test_access_list_authorization_exclusions() { assert_eq!(excluded.len(), 2); } +#[test] +fn later_top_level_call_keeps_slots_touched_by_earlier_calls() { + let caller = Address::repeat_byte(0xaa); + // Calls `token` with no input. + let router = Address::with_last_byte(0x42); + // Reads the slot keyed by its caller. + let token = Address::with_last_byte(0x43); + let mut db = InMemoryDB::default(); + let code = |code: &[u8]| AccountInfo { + code: Some(Bytecode::new_raw(Bytes::copy_from_slice(code))), + ..Default::default() + }; + db.insert_account_info(&router, code(&hex!("5f5f5f5f5f60435af100"))); + db.insert_account_info(&token, code(&hex!("335400"))); + let mut inspector = AccessListInspector::default(); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::CANCUN, Version::new(SpecId::CANCUN)), + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + db, + Precompiles::base(SpecId::CANCUN), + ); + evm.set_inspector(&mut inspector); + + // Two top-level calls of one transaction, as in an account-abstraction batch. + for to in [router, token] { + let mut gas = GasTracker::new(100_000); + let frame = prepare_initial_frame( + &mut evm, + caller, + 0, + TxKind::Call(to), + &Bytes::new(), + U256::ZERO, + &mut gas, + ) + .unwrap(); + assert!(execute_initial_frame(&mut evm, frame, &mut gas, 100_000, 0).unwrap().is_success()); + } + drop(evm); + + // The slot `token` read under `router` stays after the second call targets `token`. + assert_eq!( + inspector.into_access_list(), + access_list(&[(token, &[router.into_word(), caller.into_word()])]) + ); +} + /// A call run through `go-ethereum`'s `eth_createAccessList`, and the list it returned. struct AccessListCase { name: &'static str, From 53fe0ef86b87ae0e6fd3068ad6262dd39a3b37f7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:13:27 +0100 Subject: [PATCH 152/159] refactor(inspectors): share the out-of-gas step cost rule (#1020) The out-of-gas step cost rule was written three times in `crates/inspectors`: a step that stops out of gas costs the whole gas remaining at step, except `ReentrancySentryOOG`, which costs 0, and any other step costs what it charged. A crate-private `step_cost` helper now holds the rule, and `TracingInspector` and `OpcodeGasInspector` call it. Each keeps its own gas measure, and `TracingInspector` keeps its precedence: the sentry first, then `gas_after_charge`, then out of gas, then the plain difference. No reported value changes. The JS tracer keeps its copy. It has no sentry arm and reports the whole remainder for the sentry, so the helper would change its output. The EIP-3155 tracer in `crates/eest/src/trace.rs` keeps its own copy because `evm2-eest` does not depend on `evm2-inspectors`. Follows #956. --- crates/inspectors/src/opcode.rs | 15 ++++++------ crates/inspectors/src/tracing/mod.rs | 34 ++++++++++++++------------ crates/inspectors/src/tracing/utils.rs | 15 +++++++++++- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index 41eb39338..0220c1434 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -1,10 +1,11 @@ +use crate::tracing::utils::step_cost; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::map::HashMap; use alloy_rpc_types_trace::opcode::OpcodeGas; use evm2::{ EvmTypesHost, Inspector, interpreter::{ - InstrStop, Interpreter, Message, MessageKind, MessageResult, + Interpreter, Message, MessageKind, MessageResult, opcode::{OpCode, op}, }, }; @@ -100,13 +101,11 @@ impl Inspector for OpcodeGasInspector { if let Some(pending) = self.pending_steps.pop() && let Some(opcode) = pending.opcode { - // An out-of-gas step consumes the whole remainder it started with, except the - // reentrancy sentry, which rejects SSTORE before charging gas. - let gas_cost = match interp.result() { - Err(InstrStop::ReentrancySentryOOG) => 0, - Err(stop) if stop.is_out_of_gas() => pending.gas_remaining, - _ => pending.gas_remaining.saturating_sub(interp.gas().remaining()), - } + let gas_cost = step_cost( + interp.result(), + pending.gas_remaining, + pending.gas_remaining.saturating_sub(interp.gas().remaining()), + ) .saturating_sub(pending.child_gas_used); *self.opcode_gas.entry(opcode).or_default() += gas_cost; } diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 535793490..2c4cae9cf 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -2,9 +2,12 @@ use crate::{ opcode::immediate_size, - tracing::types::{ - CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, - TraceMemberOrder, + tracing::{ + types::{ + CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, + TraceMemberOrder, + }, + utils::step_cost, }, }; use alloc::{boxed::Box, vec::Vec}; @@ -53,7 +56,7 @@ pub use opcount::OpcodeCountInspector; pub mod types; use types::{CallLog, CallTrace, CallTraceStep}; -mod utils; +pub(crate) mod utils; #[cfg(feature = "std")] mod writer; @@ -606,19 +609,18 @@ impl TracingInspector { } // A call or create step is charged once its message starts, which includes the gas - // forwarded by a call and excludes it for a create. An out-of-gas step consumes the whole - // remainder it started with, except the reentrancy sentry, which halts before any charge. - // Any other step is charged when it ends. State gas that spills into the remaining gas is - // not part of the cost. + // forwarded by a call and excludes it for a create. Any other step is charged when it + // ends. State gas that spills into the remaining gas is not part of the cost. let result = interp.result(); - step.gas_cost = if result == Err(InstrStop::ReentrancySentryOOG) { - 0 - } else if let Some(after) = gas_after_charge { - gas_before.saturating_sub(after) - } else if result.is_err_and(InstrStop::is_out_of_gas) { - step.gas_remaining - } else { - gas_before.saturating_sub(interp.gas().remaining_excluding_state_spill()) + step.gas_cost = match gas_after_charge { + Some(after) if result != Err(InstrStop::ReentrancySentryOOG) => { + gas_before.saturating_sub(after) + } + _ => step_cost( + result, + step.gas_remaining, + gas_before.saturating_sub(interp.gas().remaining_excluding_state_spill()), + ), }; let state_gas_delta = interp.gas().state_gas_spent().saturating_sub(step.state_gas_spent); if step.state_gas_reservoir.is_some() && state_gas_delta != 0 { diff --git a/crates/inspectors/src/tracing/utils.rs b/crates/inspectors/src/tracing/utils.rs index fe8897b54..57fd57b35 100644 --- a/crates/inspectors/src/tracing/utils.rs +++ b/crates/inspectors/src/tracing/utils.rs @@ -12,7 +12,7 @@ use evm2::{ AccountInfo, EvmTypesHost, env::BlockEnv, evm::{DbResult, DynDatabase}, - interpreter::InstrStop, + interpreter::{InstrStop, Result}, }; /// Converts a non successful [`InstrStop`] to an error message. @@ -187,6 +187,19 @@ pub(crate) fn transaction_info( } } +/// Returns the gas cost of a step that stopped with `result`. +/// +/// A step that ran out of gas costs the whole `gas_remaining` it started with, except the +/// reentrancy sentry, which rejects SSTORE before charging any gas and costs 0. Any other step +/// costs what it `charged`. +pub(crate) const fn step_cost(result: Result, gas_remaining: u64, charged: u64) -> u64 { + match result { + Err(InstrStop::ReentrancySentryOOG) => 0, + Err(stop) if stop.is_out_of_gas() => gas_remaining, + _ => charged, + } +} + #[cfg(test)] mod tests { use super::*; From e0d398a6fd7ef5bb5ad88838ee43112dfc49d9cb Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Mon, 9 Nov 2026 14:56:24 -0300 Subject: [PATCH 153/159] perf(ethereum): recover EIP-7702 authorities with the installed crypto provider (#963) This PR makes EIP-7702 authority recovery use the crypto provider, like the ecrecover precompile: the installed `Crypto` provider, or the default one when none is installed. I found this while comparing the SP1 proving cost of evm2 against reth's guest on the zkevm benchmark blocks ([comment](https://github.com/paradigmxyz/stateless/pull/59#issuecomment-6543545273)). The 16 test_auth_transaction blocks cost about 8.6x as much, since recover_authority() called ecrecover_bytes(), which uses the secp256k1 backend picked at compile time (k256 in our guests). So authority recovery ran in software inside the zkVM, without using the guest's provider. #692 and #707 also argued that a custom provider could change which authorizations are valid. I don't think that's a problem, since the provider already decides the ecrecover precompile, and the y-parity and low-s checks still run outside of it. With the default provider the result is the same as before. cc @figtracer --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/evm2/src/ethereum/lazy_eip7702.rs | 14 ++++++++------ crates/evm2/tests/eip7702_recovery_crypto.rs | 17 ++++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/crates/evm2/src/ethereum/lazy_eip7702.rs b/crates/evm2/src/ethereum/lazy_eip7702.rs index 1071bff77..7824fee82 100644 --- a/crates/evm2/src/ethereum/lazy_eip7702.rs +++ b/crates/evm2/src/ethereum/lazy_eip7702.rs @@ -1,4 +1,4 @@ -use crate::precompiles::secp256k1::ecrecover_bytes; +use crate::precompiles::crypto; use alloc::vec::Vec; use alloy_consensus::{Transaction, TxEip7702, TxType}; use alloy_eips::{ @@ -266,19 +266,21 @@ impl Transaction for LazyTxEip7702 { } } -/// Recovers the authority of a signed authorization with the same secp256k1 backend as the -/// ecrecover precompile, instead of whichever backend alloy-primitives was built with. +/// Recovers the authority of a signed authorization with the installed +/// [`Crypto`](crate::precompiles::Crypto) provider, like the ecrecover precompile does, instead of +/// whichever backend alloy-primitives was built with. /// /// Keeps the checks of [`SignedAuthorization::recover_authority`]: the y-parity must be 0 or 1, -/// `s` must not exceed secp256k1n/2, and the backend rejects a zero or out-of-range `r` or `s`. +/// `s` must not exceed secp256k1n/2, and the provider rejects a zero or out-of-range `r` or `s`. fn recover_authority(authorization: &SignedAuthorization) -> Option
{ let signature = authorization.signature().ok()?; if signature.s() > SECP256K1N_HALF { return None; } let rsy = signature.as_rsy(); - let hash = ecrecover_bytes(rsy.first_chunk()?, rsy[64], &authorization.signature_hash())?; - Some(Address::from_word(hash.into())) + let hash = + crypto().secp256k1_ecrecover(rsy.first_chunk()?, rsy[64], &authorization.signature_hash()); + Some(Address::from_word(hash.ok()?.into())) } #[cfg(test)] diff --git a/crates/evm2/tests/eip7702_recovery_crypto.rs b/crates/evm2/tests/eip7702_recovery_crypto.rs index b1be05d80..778eb8a87 100644 --- a/crates/evm2/tests/eip7702_recovery_crypto.rs +++ b/crates/evm2/tests/eip7702_recovery_crypto.rs @@ -1,4 +1,4 @@ -//! Authority recovery must not go through, or initialize, the global crypto provider. +//! Authority recovery must go through the installed crypto provider. //! //! The provider is a process-wide `OnceLock`, so this lives in its own test binary. @@ -27,10 +27,8 @@ impl Crypto for BogusCrypto { } #[test] -fn recovery_ignores_crypto_provider() { +fn recovery_uses_installed_crypto_provider() { let key = SigningKey::from_slice(&[0x11; 32]).unwrap(); - let signer = - Address::from_raw_public_key(&key.verifying_key().to_sec1_point(false).as_bytes()[1..]); let inner = Authorization { chain_id: U256::ONE, address: Address::ZERO, nonce: 7 }; let (sig, recid) = key.sign_prehash_recoverable(inner.signature_hash().as_slice()); let r = U256::from_be_slice(&sig.r().to_bytes()); @@ -38,13 +36,10 @@ fn recovery_ignores_crypto_provider() { let authorization = SignedAuthorization::new_unchecked(inner, recid.to_byte(), r, s); let tx = TxEip7702 { authorization_list: vec![authorization.clone()], ..Default::default() }; - assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(signer)); - let recovered = LazyTxEip7702::from_recovered_authorizations(tx.clone()); - assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(signer)); + assert!(install_crypto(BogusCrypto)); - assert!(install_crypto(BogusCrypto), "recovery initialized the crypto provider"); - - assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(signer)); + let bogus = Address::repeat_byte(0xff); + assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(bogus)); let recovered = LazyTxEip7702::from_recovered_authorizations(tx); - assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(signer)); + assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(bogus)); } From ee95bc9bf7a92ff2072d5527f9911c78ceac2d0f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 Nov 2026 20:55:41 +0100 Subject: [PATCH 154/159] fix(inspectors): trace calls to unlisted precompiles like contracts (#1039) --- crates/inspectors/src/tracing/fourbyte.rs | 15 ++- crates/inspectors/src/tracing/mod.rs | 19 ++- crates/inspectors/tests/it/main.rs | 2 + .../tests/it/unlisted_precompiles.rs | 118 ++++++++++++++++++ 4 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 crates/inspectors/tests/it/unlisted_precompiles.rs diff --git a/crates/inspectors/src/tracing/fourbyte.rs b/crates/inspectors/src/tracing/fourbyte.rs index 579b7ed91..9ae298893 100644 --- a/crates/inspectors/src/tracing/fourbyte.rs +++ b/crates/inspectors/src/tracing/fourbyte.rs @@ -27,6 +27,7 @@ use alloy_primitives::{Selector, map::HashMap}; use alloy_rpc_types_trace::geth::FourByteFrame; use evm2::{ EvmTypes, Inspector, InspectorConfig, + evm::PrewarmSet, interpreter::{Interpreter, Message, MessageResult}, }; @@ -35,6 +36,11 @@ use evm2::{ pub struct FourByteInspector { /// The map of SELECTOR to number of occurrences entries inner: HashMap<(Selector, usize), u64>, + /// The precompiles the host warms, read on the first call. + /// + /// Addresses a provider only dispatches to on demand, such as Tempo's token precompiles, + /// count like contracts. + precompiles: Option, } impl FourByteInspector { @@ -56,7 +62,14 @@ impl Inspector for FourByteInspector { ) -> Option> { // Skip precompile invocations, matching geth's 4byteTracer. if let Some((selector, calldata)) = message.input.split_first_chunk() - && !interp.host().precompiles().contains(&message.call_target) + && !self + .precompiles + .get_or_insert_with(|| { + let mut precompiles = PrewarmSet::new(); + interp.host().precompiles().warm_addresses(&mut precompiles); + precompiles + }) + .is_warm(&message.call_target) { let selector = Selector::from(*selector); let calldata_size = calldata.len(); diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 2c4cae9cf..42c27a7f5 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -16,6 +16,7 @@ use alloy_rpc_types_trace::{geth::FlatCallConfig, parity::StorageDelta}; use core::{mem, ops::Range}; use evm2::{ EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, + evm::PrewarmSet, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -113,6 +114,11 @@ pub struct TracingInspector { /// /// All `Vec` are always empty but may have capacity. reusable_step_vecs: Vec>, + /// The precompiles the host warms, read on the first call that records precompile calls. + /// + /// Addresses a provider only dispatches to on demand, such as Tempo's token precompiles, + /// trace like contracts, as with revm-inspectors' warm precompile set. + precompiles: Option, } impl TracingInspector { @@ -151,6 +157,7 @@ impl TracingInspector { recorded_steps, spec_id, features, + precompiles, // kept config, budget, @@ -175,6 +182,7 @@ impl TracingInspector { *recorded_steps = 0; spec_id.take(); *features = EvmFeatures::empty(); + precompiles.take(); budget.recorded = 0; budget.exceeded = false; } @@ -746,8 +754,15 @@ impl Inspector for TracingInspector { }; // Builders decide whether to leave out calls to precompiles, so record which calls are. - let maybe_precompile = - self.config.record_precompile_calls.then(|| interp.host().precompiles().contains(&to)); + let maybe_precompile = self.config.record_precompile_calls.then(|| { + self.precompiles + .get_or_insert_with(|| { + let mut precompiles = PrewarmSet::new(); + interp.host().precompiles().warm_addresses(&mut precompiles); + precompiles + }) + .is_warm(&to) + }); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) diff --git a/crates/inspectors/tests/it/main.rs b/crates/inspectors/tests/it/main.rs index ca161642c..254e7d0d1 100644 --- a/crates/inspectors/tests/it/main.rs +++ b/crates/inspectors/tests/it/main.rs @@ -25,6 +25,8 @@ mod test_native_bigint; #[cfg(feature = "std")] mod transfer; #[cfg(feature = "std")] +mod unlisted_precompiles; +#[cfg(feature = "std")] mod writer; mod compat; diff --git a/crates/inspectors/tests/it/unlisted_precompiles.rs b/crates/inspectors/tests/it/unlisted_precompiles.rs new file mode 100644 index 000000000..0675f2740 --- /dev/null +++ b/crates/inspectors/tests/it/unlisted_precompiles.rs @@ -0,0 +1,118 @@ +//! Precompiles a provider dispatches to without listing them, like Tempo's token precompiles, +//! trace like contracts. + +use alloy_primitives::{Address, Bytes, TxKind, U256, hex}; +use alloy_rpc_types_eth::TransactionInfo; +use alloy_rpc_types_trace::parity::Action; +use evm2::{ + BaseEvmTypes, Evm, ExecutionConfig, Precompiles, SpecId, Version, + bytecode::Bytecode, + ethereum::{ethereum_tx_registry, execute_initial_frame, prepare_initial_frame}, + evm::{AccountInfo, InMemoryDB}, + interpreter::{GasTracker, Message}, + precompile::{PrecompileOutput, PrecompileProvider}, + precompiles::{MovePrecompileError, PrecompileId, PrecompileResult}, +}; +use evm2_inspectors::tracing::{FourByteInspector, TracingInspector, TracingInspectorConfig}; + +const CALLER: Address = Address::repeat_byte(0xaa); +/// Calls `ON_DEMAND` and the identity precompile with the selector `0xaabbccdd`, then stops. +const PROBE: Address = Address::with_last_byte(0x45); +/// A precompile the provider dispatches to without listing it. +const ON_DEMAND: Address = Address::repeat_byte(0x20); + +/// Runs a call to `PROBE` under `inspector`. +fn run_probe>(inspector: &mut I) { + let mut db = InMemoryDB::default(); + let code = hex!( + // mem[0..4] = 0xaabbccdd + "63aabbccdd60e01b5f52" + // CALL(gas, ON_DEMAND, 0, 0, 4, 0, 0) + "5f5f60045f5f7320202020202020202020202020202020202020205af150" + // CALL(gas, identity, 0, 0, 4, 0, 0) + "5f5f60045f5f60045af15000" + ); + db.insert_account_info( + &PROBE, + AccountInfo::default().with_code(Bytecode::new_raw(Bytes::copy_from_slice(&code))), + ); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::CANCUN, Version::new(SpecId::CANCUN)), + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + db, + OnDemandPrecompiles(Precompiles::base(SpecId::CANCUN)), + ); + evm.set_inspector(inspector); + let mut gas = GasTracker::new(100_000); + let frame = prepare_initial_frame( + &mut evm, + CALLER, + 0, + TxKind::Call(PROBE), + &Bytes::new(), + U256::ZERO, + &mut gas, + ) + .unwrap(); + assert!(execute_initial_frame(&mut evm, frame, &mut gas, 100_000, 0).unwrap().is_success()); +} + +#[test] +fn unlisted_precompiles_trace_like_contracts() { + let mut tracer = TracingInspector::new(TracingInspectorConfig::default_parity()); + run_probe(&mut tracer); + let targets: Vec<_> = tracer + .into_parity_builder() + .into_localized_transaction_traces(TransactionInfo::default()) + .into_iter() + .map(|trace| match trace.trace.action { + Action::Call(call) => (trace.trace.trace_address, call.to), + action => panic!("unexpected action {action:?}"), + }) + .collect(); + // The zero-value identity call is left out; the unlisted precompile is not. + assert_eq!(targets, [(vec![], PROBE), (vec![0], ON_DEMAND)]); + + let mut fourbyte = FourByteInspector::default(); + run_probe(&mut fourbyte); + let selectors: Vec<_> = fourbyte.inner().iter().map(|(key, count)| (*key, *count)).collect(); + assert_eq!(selectors, [((hex!("aabbccdd").into(), 0), 1)]); +} + +/// The base precompiles, plus [`ON_DEMAND`], which is dispatched to but not listed. +struct OnDemandPrecompiles(Precompiles); + +impl PrecompileProvider for OnDemandPrecompiles { + fn addresses(&self) -> Vec
{ + self.0.addresses() + } + + fn precompile_ids(&self) -> Vec<(Address, PrecompileId)> { + self.0.precompile_ids() + } + + fn contains(&self, address: &Address) -> bool { + *address == ON_DEMAND || self.0.contains(address) + } + + fn move_precompiles( + &mut self, + moves: &[(Address, Address)], + ) -> Result<(), MovePrecompileError> { + self.0.move_precompiles(moves) + } + + fn execute( + &mut self, + evm: &mut Evm<'_, BaseEvmTypes>, + message: &Message, + gas: &mut GasTracker, + ) -> Option { + if message.call_target == ON_DEMAND { + return Some(Ok(PrecompileOutput::new(Bytes::new()))); + } + self.0.execute(evm, message, gas) + } +} From 57b55daf04f8920ff9a258db907174f8728e1246 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Mon, 9 Nov 2026 15:31:19 -0500 Subject: [PATCH 155/159] refactor(evm): move initial_call_depth from Version to Evm (#1056) Moves `initial_call_depth` from `Version` to `Evm` and lets transaction handlers and callers set it at runtime. ### Why `Version` is fork and chain configuration, but `initial_call_depth` describes where execution is nested. Callers already have to change it per transaction: Foundry builds a modified `Version` for every isolated call or nested transaction. Tempo needs the same thing per AA batch. Under revm, Tempo's batch journal checkpoint made AA calls appear at depth 1 to inspectors, while the frame depth that enforces the limit stayed at 0. Under evm2 they appear at depth 0. Running the batch with an initial depth of 1 restores revm's traces without shrinking the 1024 call budget. ### Changes - `Evm::initial_call_depth()` and `Evm::set_initial_call_depth(depth)`. The setter panics inside a running interpreter frame, or for a depth above `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). The value persists until set again, so a handler that changes it should restore it. - `Host::initial_call_depth()`, a new required method: the CREATE opcode, the JIT CREATE builtin and inspectors read the depth through it. - Removed `Version::initial_call_depth` and `Version::max_call_depth()`. `Version::assert_valid` no longer checks the depth (the setter does). - `create_preaccess_failure` takes `initial_call_depth: u16` instead of `&Version`. - evm2-inspectors (opcode, access list, otterscan, transfer) compare against the host's initial depth. - Tests that set the depth through `Version` now call the setter. The runner-support test varies `chain_id` instead. ### Downstream impact Checked against the default branches of reth, tempo and foundry-rs/evm2-networks, and against foundry's `mablr/evm2-migration` branch: - **reth, tempo:** no references to the removed items and no `Host` impls. Tempo's `tempo-evm` (with tests) builds against this PR unchanged (`cargo check -p tempo-evm --tests` with the evm2 patch pointed here); reth was checked by search only. - **evm2-networks:** one line, `crates/base/src/eip8130.rs`: `host.version().initial_call_depth` becomes `host.initial_call_depth()`. With that change it builds against this PR (`cargo check --workspace --all-targets`). - **foundry (`mablr/evm2-migration`):** sets `env.version.initial_call_depth` before building nested EVMs (`inspectors/stack.rs` 3 sites, `backend/mod.rs` 1), reads `interp.version().initial_call_depth` in `cheatcodes/src/inspector.rs`, and destructures `Version` in `executors/mod.rs::same_execution_config`. Those sites need to carry the depth next to the env and call `set_initial_call_depth` on the nested `Evm`. ### Tests `cargo +nightly cl` (workspace), `cargo +nightly test -p evm2 -p evm2-inspectors --all-features`, `cargo +nightly test -p evm2-jit-runtime --all-features`, `cargo +nightly docs`. Requested from Pyramid thread https://pyramid.tehq.net/threads/bfd18995-32f1-4c24-a869-f2e95dc3f853 --- crates/evm2/src/ethereum/mod.rs | 8 +- crates/evm2/src/evm/async.rs | 9 +- crates/evm2/src/evm/inspector.rs | 8 +- crates/evm2/src/evm/mod.rs | 232 ++++++++++++++++-- crates/evm2/src/evm/registry.rs | 4 + crates/evm2/src/interpreter/host.rs | 7 + .../src/interpreter/instructions/system.rs | 2 +- crates/evm2/src/interpreter/message.rs | 45 ++-- crates/evm2/src/test_utils.rs | 4 + crates/evm2/src/version/mod.rs | 36 +-- crates/inspectors/src/access_list.rs | 4 +- crates/inspectors/src/opcode.rs | 6 +- crates/inspectors/src/otterscan.rs | 6 +- .../src/tracing/js/snapshot_tests.rs | 8 +- crates/inspectors/src/tracing/types.rs | 2 +- crates/inspectors/src/transfer.rs | 4 +- crates/inspectors/tests/it/inputs.rs | 11 +- crates/jit/builtins/src/lib.rs | 2 +- crates/jit/runtime/src/evm2_evm.rs | 6 +- 19 files changed, 291 insertions(+), 113 deletions(-) diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 9c2e56317..052876d53 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -635,7 +635,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } MessageExt { kind: MessageKind::Call, - depth: host.version().initial_call_depth, + depth: host.initial_call_depth(), gas_limit: tx_gas.remaining(), reservoir: tx_gas.reservoir(), destination: to, @@ -666,7 +666,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } MessageExt { kind: MessageKind::Create, - depth: host.version().initial_call_depth, + depth: host.initial_call_depth(), gas_limit: tx_gas.remaining(), reservoir: tx_gas.reservoir(), destination, @@ -683,7 +683,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } } }; - debug_assert_eq!(message.depth, host.version().initial_call_depth); + debug_assert_eq!(message.depth, host.initial_call_depth()); Ok(InitialFrame { message, charged_state_gas, runtime_oog: false }) } @@ -708,7 +708,7 @@ pub fn halted_initial_frame( }; let message = MessageExt { kind, - depth: host.version().initial_call_depth, + depth: host.initial_call_depth(), gas_limit: budget.remaining(), reservoir: budget.reservoir(), destination, diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index 4ed00bcf0..2b84b34f2 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -535,8 +535,7 @@ impl fmt::Debug for AsyncDb { mod tests { use super::*; use crate::{ - BaseEvmTypes, Evm, ExecutionConfig, Inspector, PrecompileError, Precompiles, SpecId, - Version, + BaseEvmTypes, Evm, Inspector, PrecompileError, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, @@ -1080,16 +1079,14 @@ mod tests { &target, AccountInfo { nonce: 1, ..AccountInfo::default().with_code(code) }, ); - let mut version = Version::new(SpecId::FRONTIER); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::FRONTIER, version), + let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), crate::ethereum::ethereum_tx_registry(SpecId::FRONTIER), database, Precompiles::base(SpecId::FRONTIER), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(DepthInspector::default()); let tx = Recovered::new_unchecked( crate::ethereum::TxEnvelope::Legacy(TxLegacy { diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index f271387ea..d15d6fcb3 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -448,7 +448,7 @@ mod tests { use super::*; use crate::{ BaseEvmConfig, BaseEvmTypes, DatabaseError, Evm, EvmConfig, EvmTypesHost, ExecutionConfig, - OpcodeConfig, Precompiles, SpecId, Version, + OpcodeConfig, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, @@ -1653,16 +1653,14 @@ mod tests { code.extend([op::CALL, op::STOP]); for initial_call_depth in [0, 7] { - let mut version = Version::new(SpecId::OSAKA); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::OSAKA, version), + let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), TxRegistry::new(), InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(HookInspector::default()); let depth = initial_call_depth + CALL_DEPTH_LIMIT; let mut message = MessageExt { diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 9264f8002..1a1ff9af7 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -120,7 +120,7 @@ use crate::{ DatabaseError, EvmConfigSelector, EvmTypes, EvmTypesHost, ExecutionConfig, ExecutionError, HostError, LoadError, PrecompileError, PrecompileHalt, SpecId, bytecode::Bytecode, - constants::EIP7708_TRANSFER_TOPIC, + constants::{CALL_DEPTH_LIMIT, EIP7708_TRANSFER_TOPIC}, env::{BlockEnv, TxEnv}, interpreter::{ Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Message, MessageKind, @@ -235,6 +235,7 @@ pub struct Evm<'a, T: EvmTypesHost> { #[derive_where(skip)] inspector: Option>, inspect_system_calls: bool, + initial_call_depth: u16, #[derive_where(skip)] interpreter_runner: Option>>, /// Whether `interpreter_runner` is set and supports `execution_config`. @@ -374,6 +375,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { interpreter_pool: InterpreterPool::new(), inspector: None, inspect_system_calls: false, + initial_call_depth: 0, interpreter_runner: None, runner_supported: false, current_frame: None, @@ -1050,6 +1052,54 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.inspect_system_calls = inspect; } + /// Returns the inspector-visible depth of top-level messages. Defaults to zero. + /// + /// The call depth limit and the nested-create nonce check are measured from this depth, so a + /// top-level message started at a non-zero depth still gets the full [`CALL_DEPTH_LIMIT`] + /// budget below it. Inspectors compare against it to find the top-level message. + #[inline] + pub const fn initial_call_depth(&self) -> u16 { + self.initial_call_depth + } + + /// Sets [`Self::initial_call_depth`] for top-level messages started afterwards. + /// + /// A caller can set it before running a nested transaction, such as an isolated call of a + /// test framework that keeps its outer depth, and a transaction handler can set it around the + /// messages it runs below an implicit parent, such as the calls of a batch transaction. The + /// value stays until it is set again, so a handler that changes it should restore it before + /// returning. + /// + /// It must not be called while a message is executing, such as from an inspector hook or a + /// precompile: the running message would then be checked against the new depth. As with + /// [`Self::set_execution_config`], only a running interpreter frame is detected. + /// + /// # Panics + /// + /// Panics if called from inside a running interpreter frame, or if `depth` does not leave room + /// in `u16` for the call depth budget and the rejected too-deep message: it must be at most + /// `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). + #[inline] + pub fn set_initial_call_depth(&mut self, depth: u16) { + assert!( + self.current_frame.is_none(), + "initial call depth cannot be modified while a frame is running" + ); + assert!( + depth < u16::MAX - CALL_DEPTH_LIMIT, + "initial call depth must leave room for 1024 descendants and the rejected attempt" + ); + self.initial_call_depth = depth; + } + + /// Returns the deepest permitted inspector-visible call depth. + /// + /// [`Self::set_initial_call_depth`] keeps this from overflowing. + #[inline] + const fn max_call_depth(&self) -> u16 { + self.initial_call_depth + CALL_DEPTH_LIMIT + } + /// Sets the optional external interpreter runner. #[inline] pub fn set_interpreter_runner>(&mut self, runner: R) { @@ -1511,18 +1561,21 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn prepare_create_message(&mut self, message: &mut Message) -> Result<(), HostError> { - let nested = message.depth > self.version().initial_call_depth; + let nested = message.depth > self.initial_call_depth; // A too-deep create fails on depth alone, without reading the creator. - let info = - if message.depth <= self.version().max_call_depth() && (message.value > 0 || nested) { - self.state.account_info_untracked(&message.caller)? - } else { - None - }; + let info = if message.depth <= self.max_call_depth() && (message.value > 0 || nested) { + self.state.account_info_untracked(&message.caller)? + } else { + None + }; let (balance, nonce) = info.as_ref().map_or((Word::ZERO, 0), |i| (i.balance, i.nonce)); - if let Some(stop) = - create_preaccess_failure(balance, nonce, message.value, message.depth, self.version()) - { + if let Some(stop) = create_preaccess_failure( + balance, + nonce, + message.value, + message.depth, + self.initial_call_depth, + ) { return Err(stop.into()); } @@ -1639,7 +1692,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { &mut self, message: &mut Message, ) -> Result, ExecutionError> { - if message.depth > self.version().max_call_depth() { + if message.depth > self.max_call_depth() { return Ok(Self::error_message_result( InstrStop::CallTooDeep, message.gas_limit, @@ -1851,6 +1904,10 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { &self.tx_env } + fn initial_call_depth(&self) -> u16 { + self.initial_call_depth + } + fn load_account( &mut self, address: &Address, @@ -2151,6 +2208,7 @@ mod tests { use crate::{ BaseEvmConfigSelector, BaseEvmTypes, NoopInspector, Precompiles, SpecId, Version, bytecode::Bytecode, + constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, interpreter::{GasTracker, Interpreter, Message, MessageExt, MessageKind, op}, @@ -3350,7 +3408,7 @@ mod tests { #[derive(Debug)] struct SpecBoundRunner { spec_id: SpecId, - initial_call_depth: u16, + chain_id: u64, calls: Arc, } @@ -3365,8 +3423,7 @@ mod tests { } fn supports(&self, config: &ExecutionConfig) -> bool { - config.base_spec_id() == self.spec_id - && config.version().initial_call_depth == self.initial_call_depth + config.base_spec_id() == self.spec_id && config.version().chain_id == self.chain_id } } @@ -3400,7 +3457,7 @@ mod tests { evm.set_shared_interpreter_runner(Arc::new(SpecBoundRunner { spec_id: SpecId::PRAGUE, - initial_call_depth: 0, + chain_id: Version::new(SpecId::PRAGUE).chain_id, calls: Arc::clone(&calls), })); assert_eq!(run(&mut evm), (InstrStop::Stop, 0)); @@ -3415,7 +3472,7 @@ mod tests { assert_eq!(run(&mut evm), (InstrStop::Return, 2)); let mut version = Version::new(SpecId::PRAGUE); - version.initial_call_depth = 1; + version.chain_id += 1; evm.set_block_and_execution_config_within_spec( BlockEnvExt::default(), ExecutionConfig::for_spec_and_version(SpecId::PRAGUE, version), @@ -4710,16 +4767,14 @@ mod tests { let created = caller.create(nonce); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default().with_nonce(nonce)); - let mut version = Version::new(spec); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(spec, version), + let mut evm = Evm::::new( spec, BlockEnvExt::default(), ethereum_tx_registry(spec), database, Precompiles::base(spec), ); + evm.set_initial_call_depth(initial_call_depth); // The nested transaction root also creates a child, which must still bump its creator. let tx = Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { @@ -5056,4 +5111,139 @@ mod tests { let _ = evm.transact_with_inspector(&test_tx(0), &mut CallCounter::default()); } + + #[test] + fn set_initial_call_depth_offsets_message_depths_and_limit() { + #[derive(Default)] + struct DepthInspector(Vec); + + impl Inspector for DepthInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.0.push(message.depth); + None + } + } + + let target = Address::repeat_byte(0x11); + let child = Address::repeat_byte(0x22); + let mut code = vec![op::PUSH0; 5]; + push_address(&mut code, &child); + code.extend([op::GAS, op::CALL, op::POP, op::STOP]); + let mut database = InMemoryDB::default(); + database + .insert_account_info(&target, AccountInfo::default().with_code(legacy_bytecode(code))); + let mut evm = Evm::::new( + SpecId::CANCUN, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::CANCUN), + database, + Precompiles::base(SpecId::CANCUN), + ); + evm.set_initial_call_depth(7); + assert_eq!(evm.initial_call_depth(), 7); + + let tx = Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { + to: TxKind::Call(target), + gas_limit: 100_000, + ..Default::default() + }), + Address::repeat_byte(0xaa), + ); + let mut inspector = DepthInspector::default(); + let executed = evm.transact_with_inspector(&tx, &mut inspector).unwrap(); + assert!(executed.result().is_success()); + drop(executed); + assert_eq!(inspector.0, [7, 8]); + + // The full call depth budget remains below the new initial depth. + for (depth, stop) in [ + (7 + CALL_DEPTH_LIMIT, InstrStop::Stop), + (7 + CALL_DEPTH_LIMIT + 1, InstrStop::CallTooDeep), + ] { + let mut message = MessageExt { + depth, + gas_limit: 50_000, + code: legacy_bytecode(vec![op::STOP]), + ..MessageExt::default() + }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert_eq!(result.stop, stop); + } + } + + #[test] + fn set_initial_call_depth_from_transaction_handler() { + let registry = TxRegistry::new().with_handler( + TEST_TX_TYPE, + TxEnvelope::as_legacy, + handler( + |_| Ok(()), + |req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ()| { + req.host.set_initial_call_depth(1); + let depth = req.host.initial_call_depth(); + req.host.set_initial_call_depth(0); + Ok(TxResultExt { + status: true, + total_gas_spent: depth.into(), + ..TxResultExt::default() + }) + }, + ), + ); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + registry, + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + + let executed = evm.transact(&test_tx(0)).unwrap(); + assert_eq!(executed.discard().tx_gas_used(), 1); + assert_eq!(evm.initial_call_depth(), 0); + } + + #[test] + #[should_panic(expected = "initial call depth cannot be modified while a frame is running")] + fn set_initial_call_depth_panics_while_frame_is_running() { + struct DepthSettingInspector; + + impl Inspector for DepthSettingInspector { + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + interp.host().set_initial_call_depth(1); + } + } + + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(DepthSettingInspector); + let message = MessageExt { + code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), + ..MessageExt::default() + }; + let _ = evm.run_interpreter(&message); + } + + #[test] + #[should_panic] + fn set_initial_call_depth_rejects_invalid_depth() { + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_initial_call_depth(u16::MAX - CALL_DEPTH_LIMIT); + } } diff --git a/crates/evm2/src/evm/registry.rs b/crates/evm2/src/evm/registry.rs index 6fae6f6a1..990662018 100644 --- a/crates/evm2/src/evm/registry.rs +++ b/crates/evm2/src/evm/registry.rs @@ -514,6 +514,10 @@ mod tests { unimplemented!() } + fn initial_call_depth(&self) -> u16 { + unimplemented!() + } + fn load_account( &mut self, _address: &Address, diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index cf02751d8..e44eb6079 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -133,6 +133,13 @@ pub trait Host { /// must keep it at a stable address while the interpreter runs. fn tx_env(&mut self) -> &TxEnv; + /// Returns the inspector-visible depth of top-level messages. + /// + /// The call depth limit and the nested-create nonce check are measured from this depth, and + /// inspectors compare against it to find the top-level message. It should be at most + /// `u16::MAX - CALL_DEPTH_LIMIT - 1` so the deepest message stays representable. + fn initial_call_depth(&self) -> u16; + /// Loads account information. fn load_account( &mut self, diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 79468ae02..8daac5fc9 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -450,7 +450,7 @@ pub fn create_with( caller_info.nonce, value, depth, - state.version(), + state.host().initial_call_depth(), ) .is_some() { diff --git a/crates/evm2/src/interpreter/message.rs b/crates/evm2/src/interpreter/message.rs index 4d2f609d8..aa5d17295 100644 --- a/crates/evm2/src/interpreter/message.rs +++ b/crates/evm2/src/interpreter/message.rs @@ -1,6 +1,7 @@ use crate::{ - BaseEvmTypes, EvmTypesHost, Version, + BaseEvmTypes, EvmTypesHost, bytecode::Bytecode, + constants::CALL_DEPTH_LIMIT, interpreter::{InstrStop, Word}, trustme, }; @@ -43,7 +44,7 @@ pub struct MessageExt { /// Message kind. pub kind: MessageKind, /// Inspector-visible call depth, starting at - /// [`Version::initial_call_depth`](crate::Version::initial_call_depth). + /// [`Host::initial_call_depth`](crate::interpreter::Host::initial_call_depth). pub depth: u16, /// Gas available to this message. pub gas_limit: u64, @@ -181,22 +182,23 @@ pub fn derive_create_destination( /// Returns the reason a create message fails before its destination is accessed, if any. /// /// `balance` and `nonce` are the creator's, and are zero for an absent account. Failures are -/// checked in order: call depth, insufficient balance for `value`, then, for a nested create -/// (`depth` above [`Version::initial_call_depth`]), a creator nonce at `u64::MAX` (EIP-2681). The -/// transaction itself bumps the nonce of a top-level create, so that one skips the nonce check. +/// checked in order: call depth (more than [`CALL_DEPTH_LIMIT`] below `initial_call_depth`), +/// insufficient balance for `value`, then, for a nested create (`depth` above +/// `initial_call_depth`), a creator nonce at `u64::MAX` (EIP-2681). The transaction itself bumps +/// the nonce of a top-level create, so that one skips the nonce check. #[inline] pub fn create_preaccess_failure( balance: Word, nonce: u64, value: Word, depth: u16, - version: &Version, + initial_call_depth: u16, ) -> Option { - if depth > version.max_call_depth() { + if depth.saturating_sub(initial_call_depth) > CALL_DEPTH_LIMIT { Some(InstrStop::CallTooDeep) } else if balance < value { Some(InstrStop::OutOfFunds) - } else if depth > version.initial_call_depth && nonce == u64::MAX { + } else if depth > initial_call_depth && nonce == u64::MAX { Some(InstrStop::NonceOverflow) } else { None @@ -287,22 +289,37 @@ impl From for MessageInput { #[cfg(test)] mod tests { use super::*; - use crate::SpecId; #[test] fn create_preaccess_failure_order_and_nesting() { - let mut version = Version::new(SpecId::CANCUN); - version.initial_call_depth = 5; - let max = version.max_call_depth(); + let initial_call_depth = 5; + let max = initial_call_depth + CALL_DEPTH_LIMIT; let check = |balance: u64, nonce: u64, value: u64, depth: u16| { - create_preaccess_failure(Word::from(balance), nonce, Word::from(value), depth, &version) + create_preaccess_failure( + Word::from(balance), + nonce, + Word::from(value), + depth, + initial_call_depth, + ) }; assert_eq!(check(0, u64::MAX, 1, max + 1), Some(InstrStop::CallTooDeep)); assert_eq!(check(0, u64::MAX, 1, max), Some(InstrStop::OutOfFunds)); assert_eq!(check(1, u64::MAX, 1, max), Some(InstrStop::NonceOverflow)); - assert_eq!(check(1, u64::MAX, 1, version.initial_call_depth), None); + assert_eq!(check(1, u64::MAX, 1, initial_call_depth), None); assert_eq!(check(0, 0, 0, max), None); + + // A host-supplied initial depth near `u16::MAX` must not overflow the limit. + let check_at = |initial_call_depth: u16, depth: u16| { + create_preaccess_failure(Word::ZERO, 0, Word::ZERO, depth, initial_call_depth) + }; + assert_eq!(check_at(u16::MAX, u16::MAX), None); + assert_eq!(check_at(u16::MAX - CALL_DEPTH_LIMIT, u16::MAX), None); + assert_eq!( + check_at(u16::MAX - CALL_DEPTH_LIMIT - 1, u16::MAX), + Some(InstrStop::CallTooDeep) + ); } #[test] diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index db3130324..b12419932 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -114,6 +114,10 @@ impl Host for TestHost { &self.tx_env } + fn initial_call_depth(&self) -> u16 { + 0 + } + fn load_account( &mut self, address: &Address, diff --git a/crates/evm2/src/version/mod.rs b/crates/evm2/src/version/mod.rs index b31acfa5e..dbd1156fe 100644 --- a/crates/evm2/src/version/mod.rs +++ b/crates/evm2/src/version/mod.rs @@ -3,8 +3,8 @@ use crate::{ EvmConfig, EvmTypesHost, SpecId, constants::{ - CALL_DEPTH_LIMIT, MAX_CODE_SIZE, MAX_CODE_SIZE_AMSTERDAM, MAX_INITCODE_SIZE, - MAX_INITCODE_SIZE_AMSTERDAM, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, + MAX_CODE_SIZE, MAX_CODE_SIZE_AMSTERDAM, MAX_INITCODE_SIZE, MAX_INITCODE_SIZE_AMSTERDAM, + MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, }, interpreter::{instructions as instr, op}, }; @@ -55,12 +55,6 @@ pub struct Version { pub max_initcode_size: usize, /// Maximum blobs allowed in a single blob transaction. pub max_blobs_per_tx: usize, - /// Inspector-visible depth of the transaction root. Defaults to zero. - /// - /// Nested transaction roots can retain their outer call depth while receiving a fresh - /// [`CALL_DEPTH_LIMIT`]-descendant budget. This must leave room in `u16` for that budget - /// and the attempted-too-deep message: at most `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). - pub initial_call_depth: u16, #[doc(hidden)] // Not public API. Please use an existing constructor. pub _non_exhaustive: (), @@ -117,30 +111,14 @@ impl Version { /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) calls this when it /// installs a version, so instructions can rely on these invariants: /// - /// - [`initial_call_depth`](Self::initial_call_depth) is at most `u16::MAX - CALL_DEPTH_LIMIT - - /// 1` (64510). /// - [`GasId::MaxRefundQuotient`] is non-zero. #[inline] pub const fn assert_valid(&self) { - assert!( - self.initial_call_depth < u16::MAX - CALL_DEPTH_LIMIT, - "initial call depth must leave room for 1024 descendants and the rejected attempt" - ); assert!( self.gas_params.get(GasId::MaxRefundQuotient) != 0, "max refund quotient must be non-zero" ); } - - /// Returns the deepest permitted inspector-visible call depth. - /// - /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) validates - /// [`Self::initial_call_depth`] through [`Self::assert_valid`], so this cannot overflow for an - /// installed version. - #[inline] - pub const fn max_call_depth(&self) -> u16 { - self.initial_call_depth + CALL_DEPTH_LIMIT - } } const fn base_tx_gas_limit_cap(spec_id: SpecId) -> u64 { @@ -180,7 +158,6 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { max_code_size: MAX_CODE_SIZE, max_initcode_size: MAX_INITCODE_SIZE, max_blobs_per_tx: MAX_BLOBS_PER_BLOCK_DENCUN, - initial_call_depth: 0, _non_exhaustive: (), } }; SpecId::COUNT]; @@ -197,7 +174,6 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { max_code_size: base_max_code_size(spec_id), max_initcode_size: base_max_initcode_size(spec_id), max_blobs_per_tx: base_max_blobs_per_tx(spec_id), - initial_call_depth: 0, _non_exhaustive: (), }; i += 1; @@ -453,14 +429,6 @@ mod tests { assert_eq!(amsterdam.tx_gas_caps(), (u64::MAX, MAX_TX_GAS_LIMIT_OSAKA)); } - #[test] - #[should_panic(expected = "initial call depth must leave room")] - fn execution_config_rejects_unrepresentable_call_budget() { - let mut version = Version::new(SpecId::CANCUN); - version.initial_call_depth = u16::MAX - CALL_DEPTH_LIMIT; - crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); - } - #[test] fn execution_config_clamps_memory_limit_to_u32() { let mut version = Version::new(SpecId::CANCUN); diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 203d53fad..6a123521b 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -172,7 +172,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.version().initial_call_depth { + if message.depth == interp.host().initial_call_depth() { self.collect_excluded_addresses(message, interp.host()); } None @@ -184,7 +184,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.version().initial_call_depth { + if message.depth == interp.host().initial_call_depth() { self.collect_excluded_addresses(message, interp.host()); } None diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index 0220c1434..dbcda84ff 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -5,7 +5,7 @@ use alloy_rpc_types_trace::opcode::OpcodeGas; use evm2::{ EvmTypesHost, Inspector, interpreter::{ - Interpreter, Message, MessageKind, MessageResult, + Host, Interpreter, Message, MessageKind, MessageResult, opcode::{OpCode, op}, }, }; @@ -117,7 +117,7 @@ impl Inspector for OpcodeGasInspector { message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.version().initial_call_depth { + if message.depth == interp.host().initial_call_depth() { // skip the root call return; } @@ -142,7 +142,7 @@ impl Inspector for OpcodeGasInspector { message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.version().initial_call_depth { + if message.depth == interp.host().initial_call_depth() { // skip the root create return; } diff --git a/crates/inspectors/src/otterscan.rs b/crates/inspectors/src/otterscan.rs index cab8fd632..0cab1582c 100644 --- a/crates/inspectors/src/otterscan.rs +++ b/crates/inspectors/src/otterscan.rs @@ -5,7 +5,7 @@ use alloy_primitives::{Address, U256}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; use evm2::{ EvmTypesHost, Inspector, InspectorConfig, - interpreter::{Interpreter, Message, MessageKind, MessageResult}, + interpreter::{Host, Interpreter, Message, MessageKind, MessageResult}, }; /// Records attempted internal operations for Otterscan's `ots_getInternalOperations`. @@ -61,7 +61,7 @@ impl Inspector for InternalOperationsInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.version().initial_call_depth); + self.on_operation::(message, interp.host().initial_call_depth()); None } @@ -70,7 +70,7 @@ impl Inspector for InternalOperationsInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.version().initial_call_depth); + self.on_operation::(message, interp.host().initial_call_depth()); None } diff --git a/crates/inspectors/src/tracing/js/snapshot_tests.rs b/crates/inspectors/src/tracing/js/snapshot_tests.rs index 899a1552a..dd20abe3c 100644 --- a/crates/inspectors/src/tracing/js/snapshot_tests.rs +++ b/crates/inspectors/src/tracing/js/snapshot_tests.rs @@ -3,7 +3,7 @@ use super::{interrupt_tests::transaction, *}; use alloy_primitives::hex; use evm2::{ - BaseEvmTypes, ExecutionConfig, Precompiles, SpecId, Version, + BaseEvmTypes, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, @@ -100,16 +100,14 @@ fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::CANCUN, version), + let mut evm = Evm::::new( SpecId::CANCUN, evm2::env::BlockEnvExt::default(), ethereum_tx_registry(SpecId::CANCUN), db, Precompiles::base(SpecId::CANCUN), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(inspector); let tx = transaction(TxKind::Call(address)); let res = evm.transact(&tx).unwrap().detach(); diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 4c3e5fe78..f95ca252c 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -51,7 +51,7 @@ pub struct DecodedCallTrace { pub struct CallTrace { /// The inspector-visible depth of the call. /// - /// The root starts at [`evm2::Version::initial_call_depth`], normally zero. + /// The root starts at [`evm2::Evm::initial_call_depth`], normally zero. /// /// The depth of a call's steps is the depth of the call plus one. pub depth: usize, diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index b2f824e09..83ae3b9a9 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -106,7 +106,7 @@ impl TransferInspector { fn on_transfer(&mut self, message: &Message, host: &mut Evm<'_, T>) { // A transaction that stopped on an execution error never ran its end hooks. - if message.depth == host.version().initial_call_depth { + if message.depth == host.initial_call_depth() { self.frames.clear(); } self.frames.push(TransferFrame { transfers: self.transfers.len(), logs: Vec::new() }); @@ -119,7 +119,7 @@ impl TransferInspector { }; // skip top level transfers - if self.internal_only && message.depth == host.version().initial_call_depth { + if self.internal_only && message.depth == host.initial_call_depth() { return; } diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index 2bde15800..7d6d87243 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -3,9 +3,8 @@ use crate::utils::{AccountInfo, Bytecode, SpecId, TransactTo, TxEnv}; use alloy_primitives::{Address, Bytes, U256, bytes, hex}; use evm2::{ - BaseEvmTypes, Evm, ExecutionConfig, Inspector, Precompiles, Version, - constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, evm::InMemoryDB, - interpreter::opcode::op, + BaseEvmTypes, Evm, Inspector, Precompiles, constants::CALL_DEPTH_LIMIT, + ethereum::ethereum_tx_registry, evm::InMemoryDB, interpreter::opcode::op, }; use evm2_inspectors::{ access_list::AccessListInspector, @@ -283,16 +282,14 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { AccountInfo::default().with_code(Bytecode::new_raw(code.clone())), ); } - let mut version = Version::new(SpecId::CANCUN); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::CANCUN, version), + let mut evm = Evm::::new( SpecId::CANCUN, Default::default(), ethereum_tx_registry(SpecId::CANCUN), db, Precompiles::base(SpecId::CANCUN), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(inspector); let tx = TxEnv { caller, diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 8d85a3326..828c5d64a 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -689,7 +689,7 @@ pub unsafe fn __revmc_builtin_create( caller_info.nonce, value, depth, - ecx.version(), + ecx.host().initial_call_depth(), ) .is_some() { diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index f4369f411..f8a5ba519 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -887,16 +887,14 @@ mod tests { database .insert_account_info(&creator, AccountInfo::default().with_nonce(1)); database.insert_account_info(&callee, AccountInfo::default().with_nonce(1)); - let mut version = Version::new(spec); - version.initial_call_depth = initial_call_depth; - let mut host = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(spec, version), + let mut host = Evm::::new( spec, BlockEnvExt::default(), ethereum_tx_registry(spec), database, Precompiles::base(spec), ); + host.set_initial_call_depth(initial_call_depth); if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); } From 0b448ca75d9f9a86ef27b700921f1c45d933e5ea Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Mon, 9 Nov 2026 15:52:54 -0500 Subject: [PATCH 156/159] fix(inspectors): attach parentless traces above depth zero under an empty root, as in revm-inspectors (#1059) Restores revm-inspectors' root handling in the call tracer arena. ### Why revm-inspectors' arena starts with an empty root node and lets a call replace it only at depth 0. A call with no parent at a greater depth attaches under that empty root. Tempo relied on this under revm: its AA batch checkpoint put every batch call at depth 1, so each call became a child of one empty `to=0x0` root. evm2-inspectors instead let any parentless call replace the root, whatever its depth. So even with the batch running at initial depth 1 (alloy-rs/evm2#1056), every AA call overwrote the root, and the trace kept only the last call (verified on mainnet replays: 0/1101 `trace_get` and 0/1101 callTracer matches against v1.17). ### Change - `CallTraceArena::push_trace`: a parentless trace at depth 0 replaces the root; one at a greater depth attaches under the root, which stays an empty placeholder. Traces with a parent are unchanged. - As with revm-inspectors, struct logs and the default frame's `failed` flag follow the root's own steps and status, so a trace whose top-level calls start above depth 0 has no struct logs and reports `failed: true` (v1.17's behavior for AA transactions). - Tests: two arena unit tests (parentless depth-1 traces share an empty root; a depth-0 trace replaces it), and the nested-root input test now expects the placeholder root above depth 0. ### Impact Only tracers whose top-level call starts above depth 0 with no outer frame change. That is the Tempo AA batch with #1056, and a nested root (such as Foundry's isolated calls) traced by a fresh tracer with no outer frame; nested roots traced under an outer frame already have a parent and are unchanged. Requested from Pyramid thread https://pyramid.tehq.net/threads/bfd18995-32f1-4c24-a869-f2e95dc3f853 --- crates/inspectors/src/access_list.rs | 4 +- crates/inspectors/src/opcode.rs | 10 ++--- crates/inspectors/src/otterscan.rs | 14 +++--- crates/inspectors/src/tracing/arena.rs | 58 ++++++++++++++++++++----- crates/inspectors/src/tracing/types.rs | 3 +- crates/inspectors/src/transfer.rs | 2 +- crates/inspectors/tests/it/inputs.rs | 60 +++++++++++++++++--------- 7 files changed, 103 insertions(+), 48 deletions(-) diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 6a123521b..5298e5cd8 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -172,7 +172,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.host().initial_call_depth() { + if message.depth == 0 { self.collect_excluded_addresses(message, interp.host()); } None @@ -184,7 +184,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.host().initial_call_depth() { + if message.depth == 0 { self.collect_excluded_addresses(message, interp.host()); } None diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index dbcda84ff..b23f7784c 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -5,7 +5,7 @@ use alloy_rpc_types_trace::opcode::OpcodeGas; use evm2::{ EvmTypesHost, Inspector, interpreter::{ - Host, Interpreter, Message, MessageKind, MessageResult, + Interpreter, Message, MessageKind, MessageResult, opcode::{OpCode, op}, }, }; @@ -113,11 +113,11 @@ impl Inspector for OpcodeGasInspector { fn call_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.host().initial_call_depth() { + if message.depth == 0 { // skip the root call return; } @@ -138,11 +138,11 @@ impl Inspector for OpcodeGasInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.host().initial_call_depth() { + if message.depth == 0 { // skip the root create return; } diff --git a/crates/inspectors/src/otterscan.rs b/crates/inspectors/src/otterscan.rs index 0cab1582c..202e1878c 100644 --- a/crates/inspectors/src/otterscan.rs +++ b/crates/inspectors/src/otterscan.rs @@ -5,7 +5,7 @@ use alloy_primitives::{Address, U256}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; use evm2::{ EvmTypesHost, Inspector, InspectorConfig, - interpreter::{Host, Interpreter, Message, MessageKind, MessageResult}, + interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; /// Records attempted internal operations for Otterscan's `ots_getInternalOperations`. @@ -32,8 +32,8 @@ impl InternalOperationsInspector { self.operations } - fn on_operation(&mut self, message: &Message, initial_call_depth: u16) { - if message.depth == initial_call_depth { + fn on_operation(&mut self, message: &Message) { + if message.depth == 0 { return; } let r#type = match message.kind { @@ -58,19 +58,19 @@ impl Inspector for InternalOperationsInspector { fn call( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.host().initial_call_depth()); + self.on_operation::(message); None } fn create( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.host().initial_call_depth()); + self.on_operation::(message); None } diff --git a/crates/inspectors/src/tracing/arena.rs b/crates/inspectors/src/tracing/arena.rs index 5768a838a..57bbaa543 100644 --- a/crates/inspectors/src/tracing/arena.rs +++ b/crates/inspectors/src/tracing/arena.rs @@ -58,20 +58,28 @@ impl CallTraceArena { /// This appends a new trace to the arena, and also inserts a new entry in the node's parent /// node children set. /// - /// If there is no parent, the trace replaces the root node, regardless of its visible depth. + /// A trace without a parent at depth 0 replaces the root node. A trace without a parent at a + /// greater depth is attached to the root node instead, which stays an empty placeholder, as in + /// revm-inspectors: top-level messages that start below an implicit parent, such as the calls + /// of a batch transaction, then share one root. pub(crate) fn push_trace(&mut self, parent: Option, new_trace: CallTrace) -> usize { - // The entry node, just update it. - let Some(parent) = parent else { - self.arena[0].trace = new_trace; - return 0; + let parent = match parent { + Some(parent) => { + debug_assert_eq!( + self.arena[parent].trace.depth + 1, + new_trace.depth, + "parent must be exactly one level above the new trace" + ); + parent + } + // The entry node, just update it. + None if new_trace.depth == 0 => { + self.arena[0].trace = new_trace; + return 0; + } + None => 0, }; - debug_assert_eq!( - self.arena[parent].trace.depth + 1, - new_trace.depth, - "parent must be exactly one level above the new trace" - ); - let idx = self.arena.len(); self.arena.push(CallTraceNode { parent: Some(parent), @@ -89,3 +97,31 @@ impl CallTraceArena { idx } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parentless_traces_above_depth_zero_share_an_empty_root() { + let mut arena = CallTraceArena::default(); + let first = arena.push_trace(None, CallTrace { depth: 1, ..Default::default() }); + arena.push_trace(Some(first), CallTrace { depth: 2, ..Default::default() }); + let second = arena.push_trace(None, CallTrace { depth: 1, ..Default::default() }); + + let nodes = arena.nodes(); + assert_eq!(nodes[0].trace, CallTrace::default()); + assert_eq!(nodes[0].children, [first, second]); + assert_eq!(nodes[first].parent, Some(0)); + assert_eq!(nodes[second].parent, Some(0)); + } + + #[test] + fn parentless_trace_at_depth_zero_replaces_the_root() { + let mut arena = CallTraceArena::default(); + let address = Address::repeat_byte(0x11); + assert_eq!(arena.push_trace(None, CallTrace { address, ..Default::default() }), 0); + assert_eq!(arena.nodes().len(), 1); + assert_eq!(arena.nodes()[0].trace.address, address); + } +} diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index f95ca252c..e8e6b31c4 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -51,7 +51,8 @@ pub struct DecodedCallTrace { pub struct CallTrace { /// The inspector-visible depth of the call. /// - /// The root starts at [`evm2::Evm::initial_call_depth`], normally zero. + /// Top-level calls start at [`evm2::Evm::initial_call_depth`], normally zero. Above zero, + /// they are children of an empty root at depth zero. /// /// The depth of a call's steps is the depth of the call plus one. pub depth: usize, diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 83ae3b9a9..9b73dd374 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -119,7 +119,7 @@ impl TransferInspector { }; // skip top level transfers - if self.internal_only && message.depth == host.initial_call_depth() { + if self.internal_only && message.depth == 0 { return; } diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index 7d6d87243..f3097ef74 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -10,7 +10,9 @@ use evm2_inspectors::{ access_list::AccessListInspector, opcode::OpcodeGasInspector, otterscan::InternalOperationsInspector, - tracing::{TraceLimitBehavior, TraceLimits, TracingInspector, TracingInspectorConfig}, + tracing::{ + TraceLimitBehavior, TraceLimits, TracingInspector, TracingInspectorConfig, types::CallTrace, + }, transfer::TransferInspector, }; @@ -257,7 +259,7 @@ fn byte_budget_halt_before_sstore_reports_no_store_delta() { } #[test] -fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { +fn nested_roots_match_revm_inspectors() { let caller = Address::repeat_byte(0xaa); let child = Address::with_last_byte(0x43); // Send 1 wei to a child that executes STOP. @@ -307,34 +309,50 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { .with_limits(TraceLimits::default().set_max_recorded_bytes(Some(0))); run(&mut tracer); let nodes = tracer.traces().nodes(); - assert_eq!(nodes.len(), 2); - assert_eq!(nodes[0].parent, None); - assert_eq!(nodes[0].trace.depth, usize::from(initial_call_depth)); - assert_eq!(nodes[0].trace.data, data); - assert_eq!(nodes[0].children, [1]); - assert_eq!(nodes[1].parent, Some(0)); - assert_eq!(nodes[1].trace.depth, usize::from(initial_call_depth) + 1); + // Like revm-inspectors, a top-level call above depth zero hangs under an empty root. + let root = usize::from(initial_call_depth > 0); + assert_eq!(nodes.len(), 2 + root); + if root == 1 { + assert_eq!(nodes[0].trace, CallTrace::default()); + assert_eq!(nodes[0].children, [1]); + } + assert_eq!(nodes[root].parent, root.checked_sub(1)); + assert_eq!(nodes[root].trace.depth, usize::from(initial_call_depth)); + assert_eq!(nodes[root].trace.data, data); + assert_eq!(nodes[root].children, [root + 1]); + assert_eq!(nodes[root + 1].parent, Some(root)); + assert_eq!(nodes[root + 1].trace.depth, usize::from(initial_call_depth) + 1); let geth = tracer.geth_builder().geth_traces(0, Bytes::new(), Default::default()); - assert_eq!(geth.struct_logs[0].depth, u64::from(initial_call_depth) + 1); - assert!( - geth.struct_logs.iter().any(|log| log.depth == u64::from(initial_call_depth) + 2) - ); + if root == 1 { + // As with revm-inspectors, struct logs only follow the root's own steps. + assert!(geth.struct_logs.is_empty()); + } else { + assert_eq!(geth.struct_logs[0].depth, u64::from(initial_call_depth) + 1); + assert!( + geth.struct_logs + .iter() + .any(|log| log.depth == u64::from(initial_call_depth) + 2) + ); + } + // Like revm-inspectors, only a call at depth zero counts as top-level. let mut access = AccessListInspector::default(); run(&mut access); - assert!(access.excluded().contains(&caller)); - assert!(access.excluded().contains(&target)); + assert_eq!(access.excluded().contains(&caller), root == 0); + assert_eq!(access.excluded().contains(&target), root == 0); assert!(access.touched_slots().contains_key(&child)); let mut operations = InternalOperationsInspector::default(); run(&mut operations); - assert_eq!(operations.operations().len(), 1); - assert_eq!(operations.operations()[0].from, target); - assert_eq!(operations.operations()[0].to, child); + let operations = &operations.operations()[root..]; + assert_eq!(operations.len(), 1); + assert_eq!(operations[0].from, target); + assert_eq!(operations[0].to, child); let mut transfers = TransferInspector::internal_only(); run(&mut transfers); - assert_eq!(transfers.transfers().len(), 1); - assert_eq!(transfers.transfers()[0].from, target); - assert_eq!(transfers.transfers()[0].to, child); + let transfers = &transfers.transfers()[root..]; + assert_eq!(transfers.len(), 1); + assert_eq!(transfers[0].from, target); + assert_eq!(transfers[0].to, child); let mut opcodes = OpcodeGasInspector::new(); run(&mut opcodes); assert!( From 272e09fb8814ba05087179e9b885126f1cb1eb09 Mon Sep 17 00:00:00 2001 From: RealiCZ Date: Tue, 10 Nov 2026 20:45:56 +0800 Subject: [PATCH 157/159] fix: enable `fn_align` only on x86-64, where the tco handlers use it (#1061) #1053 aligns the tco handlers with `#[rustc_align(64)]` on x86-64 only, but `crates/evm2/src/lib.rs` enables the feature for every tco build: ```rust #![cfg_attr(tco, feature(fn_align))] ``` Other targets also use tco by default on a nightly toolchain, for example aarch64-apple-darwin. On those the feature is declared but never used, so `unused_features` fires and `cargo clippy -p evm2 --all-targets -- -D warnings` fails: ``` error: feature `fn_align` is declared but not used --> crates/evm2/src/lib.rs:3:26 | 3 | #![cfg_attr(tco, feature(fn_align))] | ^^^^^^^^ | = note: `-D unused-features` implied by `-D warnings` ``` CI doesn't hit this because clippy and the test matrix run on x86-64 Linux only. This PR enables the feature under the same condition as its use: `#![cfg_attr(all(tco, target_arch = "x86_64"), feature(fn_align))]`. Tested on aarch64-apple-darwin with `nightly-2026-04-23`. `cargo clippy -p evm2 --all-targets -- -D warnings` fails on `main` and passes with this change, and `cargo test -p evm2 --lib` passes. --- crates/evm2/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index c054b3c6e..2918f26a7 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -1,6 +1,6 @@ #![doc = include_str!("../README.md")] #![cfg_attr(tco, feature(explicit_tail_calls), allow(incomplete_features))] -#![cfg_attr(tco, feature(fn_align))] +#![cfg_attr(all(tco, target_arch = "x86_64"), feature(fn_align))] #![cfg_attr(all(tco, not(cranelift)), feature(rust_preserve_none_cc))] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(test), warn(unused_crate_dependencies))] From 2190c064d845dfab946e5bb39ebb10b20e698c87 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:59:13 +0100 Subject: [PATCH 158/159] fix(inspectors): show SWAPN and EXCHANGE pre-execution stack to JS step (#910) The JS tracer's `step` hook has to see the stack as it was before the opcode ran. evm2 reports `step_end`, so `StackView` rebuilds that stack from the post-execution stack plus the top `OpCode::inputs()` items it saved before the opcode. SWAPN and EXCHANGE (EIP-8024) declare `stack_io(0, 0)` but swap two items in place at depths taken from their immediate, so nothing was saved and `log.stack.peek(i)` returned the swapped values at the top and at the swapped depth. Geth emits the opcode hook before it executes the operation (`core/vm/interpreter.go`, `EmitOpcode` at line 236 precedes `operation.execute` at line 246, go-ethereum a0cba163), and the JS tracer takes `scope.StackData()` in that hook (`eth/tracers/js/goja.go`, `OnOpcode`), so `step` sees the pre-execution stack. The step snapshot now receives the instruction's immediate, and `StackView::record` decodes it with core's `decode_single` and `decode_pair` to save the items down to the deepest depth SWAPN or EXCHANGE swaps. An invalid immediate halts the opcode before it touches the stack, so nothing is saved for it. DUPN only pushes, so it was already correct. The JS tracer snapshot tests now run on a chosen spec, and new Amsterdam cases compare the reconstructed steps for DUPN, SWAPN and EXCHANGE with valid, invalid and underflowing immediates and an EXCHANGE at the end of the code. They come from #1065 by @mattsse. --------- Co-authored-by: mattsse <19890894+mattsse@users.noreply.github.com> --- crates/inspectors/src/tracing/js/bindings.rs | 33 ++++++++++--- crates/inspectors/src/tracing/js/mod.rs | 5 ++ .../src/tracing/js/snapshot_tests.rs | 46 +++++++++++++++---- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 06d2ac9f3..c1e3ec46b 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -36,6 +36,7 @@ use evm2::{ evm::{AccountInfo, DbResult, DynDatabase, PendingState, State}, interpreter::{ Word, + instructions::{decode_pair, decode_single}, opcode::{ OpCode, op as opcode, op::{PUSH0, PUSH32}, @@ -240,6 +241,8 @@ pub(crate) struct PreStep<'a> { pub(crate) pc: u64, /// Opcode to be executed pub(crate) op: u8, + /// Byte after the opcode, or 0 if the bytecode ends there + pub(crate) immediate: u8, /// Remaining gas before step execution pub(crate) gas_remaining: u64, /// Stack before step execution @@ -647,12 +650,12 @@ pub(crate) struct StepSnapshot { impl StepSnapshot { /// Records everything about a step that must be captured before the opcode executes. pub(crate) fn record(&mut self, step: PreStep<'_>) { - let PreStep { pc, op, gas_remaining, stack, memory } = step; + let PreStep { pc, op, immediate, gas_remaining, stack, memory } = step; let state = self; state.pc = pc; state.op = op; state.gas_remaining = gas_remaining; - state.stack.record(op, stack); + state.stack.record(op, immediate, stack); state.memory.record(op, stack, memory); } } @@ -729,6 +732,9 @@ impl OpcodeNames { /// An opcode only touches its top `inputs` items, so the pre-execution stack is the post-execution /// stack below those items followed by the saved inputs. This holds for halted opcodes too, which /// pop at most `inputs` items. +/// +/// SWAPN and EXCHANGE declare no inputs but swap items at depths decoded from their immediate, so +/// they save every item down to the deepest one they touch. #[derive(Debug, Default)] struct StackView { /// Pre-execution length. @@ -740,9 +746,17 @@ struct StackView { } impl StackView { - fn record(&mut self, op: u8, stack: &[U256]) { + fn record(&mut self, op: u8, immediate: u8, stack: &[U256]) { self.len = stack.len(); - let inputs = OpCode::new(op).map_or(0, |op| op.inputs() as usize).min(stack.len()); + let inputs = match op { + // An invalid immediate halts the opcode before it touches the stack. + opcode::SWAPN => decode_single(immediate).map_or(0, |n| usize::from(n) + 1), + opcode::EXCHANGE => { + decode_pair(immediate).map_or(0, |(n, m)| usize::from(n.max(m)) + 1) + } + _ => OpCode::new(op).map_or(0, |op| op.inputs() as usize), + } + .min(stack.len()); self.saved.clear(); self.saved.extend_from_slice(&stack[stack.len() - inputs..]); } @@ -1131,7 +1145,14 @@ mod tests { f: impl FnOnce() -> R, ) -> R { let mut snapshot = StepSnapshot::default(); - snapshot.record(PreStep { pc: 0, op, gas_remaining: 0, stack, memory: &memory.borrow() }); + snapshot.record(PreStep { + pc: 0, + op, + immediate: 0, + gas_remaining: 0, + stack, + memory: &memory.borrow(), + }); log.with_scope( &mut snapshot, stack, @@ -1591,7 +1612,7 @@ mod tests { (opcode::STOP, pre.to_vec()), ] { let mut view = StackView::default(); - view.record(op, &pre); + view.record(op, 0, &pre); let post: &'static [U256] = post.leak(); view.post = Some(post); assert_eq!(view.len(), 3); diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index cbeda5408..b43902206 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -669,6 +669,11 @@ impl Inspector for JsInspector { pending.snapshot.record(PreStep { pc: pending.instruction.pc, op: pending.instruction.op, + immediate: interp + .original_bytecode_slice() + .get(interp.pc() + 1) + .copied() + .unwrap_or_default(), gas_remaining: pending.instruction.gas, stack: interp.stack().as_slice(), memory: interp.memory().as_slice(), diff --git a/crates/inspectors/src/tracing/js/snapshot_tests.rs b/crates/inspectors/src/tracing/js/snapshot_tests.rs index dd20abe3c..f2471e80b 100644 --- a/crates/inspectors/src/tracing/js/snapshot_tests.rs +++ b/crates/inspectors/src/tracing/js/snapshot_tests.rs @@ -72,7 +72,7 @@ impl Inspector for SnapshotInspector { } } -fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec { +fn compare(code: Bytes, child: Option, initial_call_depth: u16, spec: SpecId) -> Vec { let address = Address::repeat_byte(0x11); let mut db = InMemoryDB::default(); db.insert_account_info( @@ -101,11 +101,11 @@ fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec::new( - SpecId::CANCUN, + spec, evm2::env::BlockEnvExt::default(), - ethereum_tx_registry(SpecId::CANCUN), + ethereum_tx_registry(spec), db, - Precompiles::base(SpecId::CANCUN), + Precompiles::base(spec), ); evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(inspector); @@ -130,7 +130,7 @@ fn reconstructed_steps_match_all_opcode_bytes() { code.extend_from_slice(&[op::PUSH1, value]); } code.extend_from_slice(&[opcode, op::STOP]); - compare(code.into(), None, 0); + compare(code.into(), None, 0, SpecId::CANCUN); } } @@ -153,7 +153,7 @@ fn parent_snapshot_survives_calls_and_return_data() { parent.push(op::STOP); let mut child = hex!("60aa60005260206000").to_vec(); child.push(stop); - let steps = compare(parent.into(), Some(child.into()), 0); + let steps = compare(parent.into(), Some(child.into()), 0, SpecId::CANCUN); assert!(steps.iter().any(|step| step[3] == 2), "child must execute"); } } @@ -169,7 +169,7 @@ fn parent_snapshot_survives_create() { } code.extend_from_slice(&hex!("600a60166000")); code.extend_from_slice(&[opcode, op::POP, op::STOP]); - let steps = compare(code.into(), None, 0); + let steps = compare(code.into(), None, 0, SpecId::CANCUN); assert!(steps.iter().any(|step| step[3] == 2), "initcode must execute"); } } @@ -178,9 +178,9 @@ fn parent_snapshot_survives_create() { fn nested_root_steps_keep_visible_depth() { let parent = hex!("5f5f5f5f5f7322222222222222222222222222222222222222225af100"); let child = Bytes::from_static(&[op::STOP]); - let root_steps = compare(parent.into(), Some(child.clone()), 0); + let root_steps = compare(parent.into(), Some(child.clone()), 0, SpecId::CANCUN); for depth in [0, 7, u16::MAX - CALL_DEPTH_LIMIT - 1] { - let steps = compare(parent.into(), Some(child.clone()), depth); + let steps = compare(parent.into(), Some(child.clone()), depth, SpecId::CANCUN); let depths = steps.iter().map(|step| step[3].as_u64().unwrap()).collect::>(); let depth = u64::from(depth); let mut expected = vec![depth + 1; 8]; @@ -194,3 +194,31 @@ fn nested_root_steps_keep_visible_depth() { assert_eq!(steps, shifted); } } + +/// `DUPN`, `SWAPN` and `EXCHANGE` touch the stack items their immediate selects, not the ones their +/// opcode-table entry declares. +#[test] +fn reconstructed_steps_match_stack_immediates() { + let cases: &[&[u8]] = &[ + &[op::DUPN, 0x80], + &[op::SWAPN, 0x80], + &[op::SWAPN, 0x90], + // An invalid immediate, then one that underflows the stack. + &[op::SWAPN, 0x5b], + &[op::SWAPN, 0x00], + &[op::EXCHANGE, 0x8e], + &[op::EXCHANGE, 0x51], + &[op::EXCHANGE, 0xff], + &[op::EXCHANGE, 0x52], + // The interpreter reads the zero padding past the end of the code as the immediate. + &[op::EXCHANGE], + ]; + for case in cases { + let mut code = Vec::new(); + for value in 0..40u8 { + code.extend_from_slice(&[op::PUSH1, value]); + } + code.extend_from_slice(case); + compare(code.into(), None, 0, SpecId::AMSTERDAM); + } +} From 66687c18b34d3bdbb899f5eb7605070615c03475 Mon Sep 17 00:00:00 2001 From: onbjerg Date: Tue, 10 Nov 2026 16:41:58 +0100 Subject: [PATCH 159/159] fix(inspector): read call input from the caller's memory in hooks (#1046) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With an inspector installed, `Evm` copied every call's input out of the caller's memory before the hooks ran (`make_owned` in `execute_message_inspected`), and `call_with` did the same. That kept the O(input size) copy that #1030 removed from the plain path: a frame pays for memory expansion once, and then each warm `CALL` costs about 100 gas while copying the whole range. That is the incident's DoS, and it stayed open on every inspected path (tracing RPCs). This PR makes the input of a call instruction's message a range of the caller frame's memory on every path. `MessageInput` is a private enum, `Owned(Bytes)` or `Borrowed(Range)`, so it never holds a pointer that can outlive the memory it names. Inspector hooks read a borrowed input with `message.input.as_bytes(interp.memory())`, or `to_bytes` to keep it, and change it by copying it out and assigning an owned input: ```rust let mut input = message.input.to_bytes(interp.memory()).to_vec(); patch(&mut input); message.input = Bytes::from(input).into(); ``` The child frame resolves the range once, when `Evm` enters it, against the running caller frame and stores the slice in `Interpreter::input`. CALLDATALOAD, CALLDATASIZE, CALLDATACOPY and the JIT's `EvmContext::input` read that slice, so they carry no branch on the kind of input. Nothing can change the caller's memory while the slice is live: the caller is suspended until the child returns, and the hooks of the messages the child sends get the child, which is the running frame, never the caller. The hooks of the child's own message (`call`, `call_end`), the `call_with` hooks and the EIP-7708 transfer log run before the frame is entered or after it returns, so they see and may change the caller's memory, and what they leave there is what the child reads. A precompile reads its input through `Evm::message_input`, which borrows the EVM, so the compiler stops a precompile from sending a message, and so from running hooks that write the caller's memory, while it holds the bytes. A precompile that sends a message and needs its input afterwards copies it first. Resolving a range checks its bounds in release builds too, and a borrowed input records the entry number `Evm` gave the frame that made it, so a clone forwarded from another frame, a sibling at the same depth included, panics instead of reading that frame's memory. The top-level frame that only runs the hooks of a top-level message has an empty input slice, since a hook may replace the message's input. `Interpreter::call_input` no longer needs `unsafe`, and `call_with` is a single function again. `Interpreter` grows by 24 bytes for the slice and the frame's entry number, and `Message` shrinks by 8, so the field offsets the JIT reads move and `ABI_VERSION` goes from 7 to 8. Codegen is otherwise unchanged: `opt.s` totals are equal, and a few `unopt.ll` and `opt.ll` lines appear where a `Message` field offset is no longer zero. ## Behaviour changes A `call` hook, or a `call_with` `before` hook, that writes the caller's memory without replacing the input now changes what the child reads. Two #1030 tests asserted the opposite, one for a `call` hook and one for a `call_with` hook; both now assert the new behaviour, and `call_hook_replacing_input_keeps_caller_memory` covers replacing the input. `MessageInput` no longer derefs to its bytes or converts into `Bytes`; use `as_bytes`, `to_bytes` or `as_owned`. `MessageInput::to_bytes` takes the caller's `&Memory`, and precompiles read `Evm::message_input(message)` instead of `message.input`. ## Benchmarks Callgrind instructions, one process per benchmark, `profiling` profile, over all 174 cases of the `evm` bench, against `origin/main` at 53fe0ef8. The mainnet replay and the ten mainnet contracts are the primary workloads. | Workload | origin/main | this PR | Change | | :--- | ---: | ---: | :--- | | Mainnet replay, 10 blocks | 1,100,516,499 | 1,101,074,560 | +0.05% 🔴 | | Interpreter, 10 mainnet contracts (geomean) | | | -2.1% 🟢 | | Interpreter, all 99 cases (geomean) | | | -0.78% 🟢 | | JIT run, 10 mainnet contracts (geomean) | | | -3.6% 🟢 | | JIT run, all 29 cases (geomean) | | | -2.8% 🟢 | | JIT translate, 29 cases (geomean) | | | +0.09% 🔴 | | JIT compile, 16 cases (geomean) | | | +0.05% 🔴 | The replay is the one workload that does not improve. In its function-level difference the `evm2` functions this change touches net to about +70,000 instructions: frame entry gains what `prepare_call` and `call_inner` lose, plus a little. The other +490,000 is `FbHasher` hash calls and `read_unloaded_slot` that the compiler inlines differently in the two builds, and glibc `_int_malloc`. The same effect moves single cases by up to ±4% between builds of this branch, and adding 16 bytes of padding to `Interpreter` on `origin/main` alone moves `weth` by -1.0% and `succinct_staking` by -0.3%, in the same functions, so the per-contract figures are as much a layout effect as a measure of this change. `scripts/jit/bench.py` reports equal totals for instructions per iteration and for compile instructions. --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/evm2/examples/precompile_subcall.rs | 2 +- crates/evm2/src/evm/inspector.rs | 168 +++++++++++++-- crates/evm2/src/evm/mod.rs | 194 ++++++++++++++++-- .../evm2/src/interpreter/instructions/env.rs | 6 +- .../src/interpreter/instructions/system.rs | 48 +---- crates/evm2/src/interpreter/message.rs | 170 +++++++++------ crates/evm2/src/interpreter/runtime.rs | 80 +++++--- crates/evm2/src/precompiles/table.rs | 7 +- crates/evm2/src/test_utils.rs | 23 +++ crates/inspectors/src/tracing/fourbyte.rs | 4 +- crates/inspectors/src/tracing/js/mod.rs | 9 +- crates/inspectors/src/tracing/mod.rs | 4 +- crates/jit/builtins/src/lib.rs | 13 +- crates/jit/codegen/src/lib.rs | 2 +- crates/jit/context/src/lib.rs | 7 +- 15 files changed, 547 insertions(+), 190 deletions(-) diff --git a/crates/evm2/examples/precompile_subcall.rs b/crates/evm2/examples/precompile_subcall.rs index 48c8aa382..8d40fd92b 100644 --- a/crates/evm2/examples/precompile_subcall.rs +++ b/crates/evm2/examples/precompile_subcall.rs @@ -77,7 +77,7 @@ fn staticcall_precompile( destination: SUBCALL_TARGET, call_target: SUBCALL_TARGET, caller: message.destination, - input: message.input.clone(), + input: Bytes::copy_from_slice(evm.message_input(message)).into(), value: U256::ZERO, code: loaded.code, code_address: SUBCALL_TARGET, diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index d15d6fcb3..495e565ec 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -455,13 +455,13 @@ mod tests { ethereum::{TxEnvelope, ethereum_tx_registry}, evm::{AccountInfo, EmptyDB, InMemoryDB, SYSTEM_ADDRESS, State, StateCheckpoint}, interpreter::{ - GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageKind, - MessageResult, MessageResultExt, Word, instructions as instr, op, + GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageInput, + MessageKind, MessageResult, MessageResultExt, Word, instructions as instr, op, }, registry::TxRegistry, test_utils::{ - TestHost, TestTypes, call_code, create_code, legacy_bytecode, push, push_all, - return_top_word, + TestHost, TestTypes, call_code, create_code, legacy_bytecode, memory_input_call_code, + push, push_all, return_top_word, }, utils::{WordExt, address_to_word}, }; @@ -1864,11 +1864,115 @@ mod tests { db } - /// Calls `target` through `call_op` with memory 0..32 as input and returns its 32-byte output. - fn memory_input_call_code(target: Address, call_op: u8) -> Vec { - let mut code = Vec::new(); - push_all(&mut code, [Word::from_be_bytes([0xaa; 32]), Word::ZERO]); - code.push(op::MSTORE); + #[test] + fn call_hook_overwriting_caller_memory_changes_child_input() { + struct OverwriteCallerMemory; + + impl Inspector for OverwriteCallerMemory { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth > 0 { + // The input is a range of the caller's memory, read through the caller. + assert_eq!(message.input.as_owned(), None); + assert_eq!(message.input.len(), 32); + assert_eq!(message.input.as_bytes(interp.memory()), [0xaa; 32]); + interp.memory_mut().set(0, &[0xbb; 32]); + } + None + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + _result: &mut MessageResult, + ) { + if message.depth > 0 { + assert_eq!(message.input.to_bytes(interp.memory())[..], [0xbb; 32]); + } + } + } + + for target in [ECHO, Address::with_last_byte(4)] { + let (result, _, _) = run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(target, op::CALL), + &MessageExt::default(), + 100_000, + OverwriteCallerMemory, + ); + + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xbb; 32], "{target}"); + } + } + + #[test] + #[should_panic = "a call input must be resolved against the frame that made it"] + fn forwarding_a_borrowed_call_input_from_another_frame_panics() { + // Forwards the borrowed input of the child, a range of the caller's memory, from the + // child's own frame, where the range would name the child's memory. + struct ForwardInput; + + impl Inspector for ForwardInput { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().depth == 1 { + let mut message = MessageExt { + depth: 2, + gas_limit: 1_000, + input: interp.message().input.clone(), + code: legacy_bytecode([op::STOP]), + ..Default::default() + }; + let _ = Host::execute_message(interp.host(), &mut message); + } + } + } + + run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(ECHO, op::CALL), + &MessageExt::default(), + 100_000, + ForwardInput, + ); + } + + #[test] + #[should_panic = "a call input must be resolved against the frame that made it"] + fn forwarding_a_borrowed_call_input_from_a_sibling_frame_panics() { + // Keeps the borrowed input of the first child and forwards it from the second child, a + // frame at the same depth whose memory the range would name. + #[derive(Default)] + struct ForwardSiblingInput(Option); + + impl Inspector for ForwardSiblingInput { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().depth != 1 { + return; + } + match self.0.take() { + None => self.0 = Some(interp.message().input.clone()), + Some(input) => { + let mut message = MessageExt { + depth: 2, + gas_limit: 1_000, + input, + code: legacy_bytecode([op::STOP]), + ..Default::default() + }; + let _ = Host::execute_message(interp.host(), &mut message); + } + } + } + } + + // Calls `ECHO` twice with memory 0..32 as input. + let mut code = memory_input_call_code(ECHO, op::CALL); + code.truncate(code.len() - 5); push_all( &mut code, [ @@ -1877,31 +1981,50 @@ mod tests { Word::from(32), Word::ZERO, Word::ZERO, - address_to_word(&target), + address_to_word(&ECHO), Word::from(50_000), ], ); - code.extend([call_op, op::POP]); - push_all(&mut code, [Word::from(32), Word::from(32)]); - code.push(op::RETURN); - code + code.extend([op::CALL, op::POP, op::STOP]); + + run_evm_with_inspector_db( + echo_db(), + code, + &MessageExt::default(), + 100_000, + ForwardSiblingInput::default(), + ); } #[test] - fn call_hook_overwriting_caller_memory_keeps_child_input() { - struct OverwriteCallerMemory; + fn call_hook_replacing_input_keeps_caller_memory() { + struct ReplaceInput; - impl Inspector for OverwriteCallerMemory { + impl Inspector for ReplaceInput { fn call( &mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>, message: &mut Message, ) -> Option> { if message.depth > 0 { - interp.memory_mut().set(0, &[0xbb; 32]); + let mut input = message.input.to_bytes(interp.memory()).to_vec(); + input[..16].fill(0xcc); + message.input = Bytes::from(input).into(); } None } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + _result: &mut MessageResult, + ) { + if message.depth > 0 { + assert_eq!(interp.memory().slice(0, 32), [0xaa; 32]); + assert_eq!(message.input.as_owned().unwrap()[..16], [0xcc; 16]); + } + } } for target in [ECHO, Address::with_last_byte(4)] { @@ -1910,16 +2033,17 @@ mod tests { memory_input_call_code(target, op::CALL), &MessageExt::default(), 100_000, - OverwriteCallerMemory, + ReplaceInput, ); assert_matches!(result.stop, InstrStop::Return); - assert_eq!(result.output[..], [0xaa; 32], "{target}"); + assert_eq!(result.output[..16], [0xcc; 16], "{target}"); + assert_eq!(result.output[16..], [0xaa; 16], "{target}"); } } #[test] - fn call_with_hook_overwriting_caller_memory_keeps_child_input() { + fn call_with_hook_overwriting_caller_memory_changes_child_input() { let mut evm = Evm::::new_with_execution_config( ExecutionConfig::for_config::(), SpecId::OSAKA, @@ -1935,7 +2059,7 @@ mod tests { let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_matches!(result.stop, InstrStop::Return); - assert_eq!(result.output[..], [0xaa; 32]); + assert_eq!(result.output[..], [0xbb; 32]); } #[test] diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 1a1ff9af7..b41e8f9d6 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -123,8 +123,8 @@ use crate::{ constants::{CALL_DEPTH_LIMIT, EIP7708_TRANSFER_TOPIC}, env::{BlockEnv, TxEnv}, interpreter::{ - Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Message, MessageKind, - MessageResult, MessageResultExt, Word, create_preaccess_failure, + Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Memory, Message, + MessageKind, MessageResult, MessageResultExt, Word, create_preaccess_failure, }, registry::{HandlerResult, TxRegistry}, trustme, @@ -248,6 +248,9 @@ pub struct Evm<'a, T: EvmTypesHost> { /// This is passed to the inspector call and create hooks as the parent frame. #[derive_where(skip)] current_frame: Option>>, + /// The number of frames this EVM has entered, which numbers the frame it enters next. + #[derive_where(skip)] + frames_entered: u64, /// The checkpoint the currently running interpreter frame rolls back to if it fails. #[derive_where(skip)] frame_checkpoint: Option, @@ -379,6 +382,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { interpreter_runner: None, runner_supported: false, current_frame: None, + frames_entered: 0, frame_checkpoint: None, running: false, #[cfg(feature = "async")] @@ -1404,8 +1408,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } impl<'a, T: EvmTypes> Evm<'a, T> { - /// The input of a call message may borrow the caller's memory (see - /// [`Interpreter::call_input`]), so this must not keep or clone out a borrowed input. #[inline] fn execute_message_impl( &mut self, @@ -1448,10 +1450,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { // SAFETY: The inspector is stored in `self`; the execution guard prevents inspector // replacement while the hooks are running. let inspector = unsafe { trustme::decouple_lt_mut(inspector) }; - // The hooks can change the caller's memory and keep the message, so the input must not - // borrow that memory. - message.input.make_owned(); - // `destination` already holds the create's contract address (set when the message was // constructed), so the create hook observes it directly. let is_create = message.kind.is_create(); @@ -1478,6 +1476,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { // `top_frame` and dropped after the frame is returned to the pool, which resets // its bytecode borrow. frame.set_bytecode(unsafe { trustme::decouple_lt(&top_code) }); + // The frame only runs hooks, which can replace the message's input. + frame.set_input(&[]); frame.prepare_run(guard.evm); frame } @@ -1830,17 +1830,28 @@ impl<'a, T: EvmTypes> Evm<'a, T> { let guard = self.enter_execution(); let mut interp: Box> = guard.evm.interpreter_pool.pop(message); let interp_ref = interp.as_mut(); + if let Some((range, frame)) = message.input.borrowed_range() { + let memory = guard.evm.caller_memory(frame); + // SAFETY: The caller frame is suspended until this frame returns, and `current_frame` + // becomes this one below, so nothing can reach the caller's memory in between. + interp_ref + .set_input(unsafe { trustme::decouple_lt(&memory.as_slice()[range.clone()]) }); + } interp_ref.lend_memory(); + guard.evm.frames_entered += 1; + interp_ref.set_frame(guard.evm.frames_entered); + // The initialize hook can send messages, whose hooks must get this frame and not the + // caller, whose memory the input slice points into. + let prev_frame = guard + .evm + .current_frame + .replace(NonNull::from(&mut *interp_ref).cast::>()); guard.evm.inspect_initialize_interp(interp_ref); let inspector = guard.evm.active_inspector().map(|inspector| { // SAFETY: The inspector is stored in `self` and remains alive for the duration of the // interpreter run. unsafe { trustme::decouple_lt_mut(inspector) } }); - let prev_frame = guard - .evm - .current_frame - .replace(NonNull::from(&mut *interp_ref).cast::>()); let runner = guard.evm.interpreter_runner.as_ref().filter(|_| guard.evm.runner_supported).cloned(); let stop = if let Some(inspector) = inspector { @@ -1887,6 +1898,44 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } } +impl Evm<'_, T> { + /// Returns the bytes of `message`'s input, reading a range of the caller's memory from the + /// running caller frame. + /// + /// The bytes borrow the EVM, so a precompile cannot send a message, whose hooks could change + /// the caller's memory, while it holds them. + /// + /// # Panics + /// + /// Panics if the input is a range of memory and no frame runs, the running frame is not the + /// frame that made the input, or the range is out of bounds of its memory. Each means that the + /// message did not come from a call instruction of the running frame: copy a borrowed input + /// with [`MessageInput::to_bytes`](crate::interpreter::MessageInput::to_bytes) before + /// forwarding it. + #[inline] + pub fn message_input<'m>(&'m self, message: &'m Message) -> &'m [u8] { + match message.input.borrowed_range() { + None => message.input.as_owned().map_or(&[][..], |input| &input[..]), + Some((range, frame)) => &self.caller_memory(frame).as_slice()[range.clone()], + } + } + + /// Returns the memory of the running frame, which must be the frame with the entry number + /// `frame`. + #[inline] + fn caller_memory(&self, frame: u64) -> &Memory { + let caller = self.current_frame.expect("a call input must come from a running frame"); + // SAFETY: The running frame is suspended on this call stack. + let caller = unsafe { caller.as_ref() }; + assert_eq!( + caller.frame(), + frame, + "a call input must be resolved against the frame that made it", + ); + caller.memory() + } +} + impl<'a, T: EvmTypes> Host for Evm<'a, T> { fn spec_id(&self) -> SpecId { self.spec_id() @@ -2214,7 +2263,9 @@ mod tests { interpreter::{GasTracker, Interpreter, Message, MessageExt, MessageKind, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileMap}, registry::{HandlerError, TxRequest, handler}, - test_utils::{handle_test_tx, legacy_bytecode, push_address, test_tx}, + test_utils::{ + handle_test_tx, legacy_bytecode, memory_input_call_code, push_address, test_tx, + }, utils::address_to_word, }; use alloc::{borrow::Cow, string::ToString, sync::Arc, vec, vec::Vec}; @@ -3290,7 +3341,7 @@ mod tests { assert_eq!(message.depth, 67); assert_eq!(message.destination, TEST_PRECOMPILE); assert_eq!(message.caller, Address::with_last_byte(0x7a)); - assert_eq!(&*message.input, b"message input"); + assert_eq!(message.input.as_owned().unwrap()[..], *b"message input"); assert_eq!(message.value, U256::from(99)); assert_eq!(message.code_address, TEST_PRECOMPILE); Ok(PrecompileOutput::new(Bytes::from(evm.block.number.to_be_bytes::<32>()))) @@ -5246,4 +5297,119 @@ mod tests { ); evm.set_initial_call_depth(u16::MAX - CALL_DEPTH_LIMIT); } + + #[test] + fn hook_resizing_caller_memory_keeps_precompile_input_readable() { + // Grows and overwrites the caller's memory in the hook of a message that a precompile + // sends, after the precompile read its input from that memory. + struct OverwriteCallerMemory; + + impl Inspector for OverwriteCallerMemory { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.destination == INNER_TEST_PRECOMPILE { + let memory = interp.memory_mut(); + memory.resize(0, 1 << 20).unwrap(); + memory.set(0, &[0xbb; 32]); + } + None + } + } + + let precompiles = precompiles_with([ + test_precompile(TEST_PRECOMPILE, |evm, message, _| { + let input = Bytes::copy_from_slice(evm.message_input(message)); + let mut child = precompile_message(INNER_TEST_PRECOMPILE); + child.depth = message.depth + 1; + Host::execute_message(evm, &mut child)?; + // The input is a range of the caller's memory, which the hook changed. + assert_eq!(evm.message_input(message), [0xbb; 32]); + Ok(PrecompileOutput::new(input)) + }), + test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { + Ok(PrecompileOutput::new(Bytes::new())) + }), + ]); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + precompiles, + ); + evm.set_inspector(OverwriteCallerMemory); + let code = memory_input_call_code(TEST_PRECOMPILE, op::CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_eq!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32]); + } + + #[test] + fn initialize_hook_message_keeps_caller_memory() { + const CALLEE: Address = Address::with_last_byte(0xca); + + struct SendOnInitialize; + + impl Inspector for SendOnInitialize { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().destination == CALLEE { + let mut message = precompile_message(INNER_TEST_PRECOMPILE); + message.depth = interp.message().depth + 1; + Host::execute_message(interp.host(), &mut message).unwrap(); + } + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.destination == INNER_TEST_PRECOMPILE { + let memory = interp.memory_mut(); + memory.resize(0, 1 << 20).unwrap(); + memory.set(0, &[0xbb; 64]); + } + None + } + } + + let precompiles = precompiles_with([test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { + Ok(PrecompileOutput::new(Bytes::new())) + })]); + let mut database = InMemoryDB::default(); + let echo = [ + op::CALLDATASIZE, + op::PUSH0, + op::PUSH0, + op::CALLDATACOPY, + op::CALLDATASIZE, + op::PUSH0, + op::RETURN, + ]; + database + .insert_account_info(&CALLEE, AccountInfo::default().with_code(legacy_bytecode(echo))); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + database, + precompiles, + ); + evm.set_inspector(SendOnInitialize); + let code = memory_input_call_code(CALLEE, op::CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_eq!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32]); + } } diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index b98fcc3c9..deb064caf 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -72,12 +72,12 @@ pub fn callvalue(cx: _) -> out { #[instruction] pub fn calldataload(cx: _, [offset]: [Word]) -> out { let offset = offset.to_usize_saturated(); - *out = read_word_padded(&cx.state.message().input, offset).into(); + *out = read_word_padded(cx.state.input(), offset).into(); } #[instruction] pub fn calldatasize(cx: _) -> out { - out.write_u64(cx.state.message().input.len() as u64); + out.write_u64(cx.state.input().len() as u64); } #[instruction(dynamic_gas)] @@ -86,7 +86,7 @@ pub fn calldatacopy(cx: _, [memory_offset, data_offset, len]: [Word]) -> Result // SAFETY: Charging and copying data make no host calls. let gas_params = unsafe { cx.state.gas_params_detached() }; cx.gas.spend(gas_params.copy_cost(len))?; - let input = cx.state.message().input.as_ref(); + let input = cx.state.0.input; copy_data(cx.gas, &mut cx.state.0.memory, gas_params, memory_offset, data_offset, len, input) } diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 8daac5fc9..b7914e112 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -138,12 +138,8 @@ fn load_acc_and_calc_gas( Ok((gas_limit, new_account_state_gas, code, code_address)) } -/// # Safety -/// -/// The message must be passed to the host as in -/// [`Interpreter::call_input`](crate::interpreter::Interpreter::call_input). #[inline(never)] -unsafe fn prepare_call( +fn prepare_call( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, @@ -183,8 +179,7 @@ unsafe fn prepare_call( kind == MessageKind::Call, local_gas_limit, )?; - // SAFETY: Upheld by the caller. - let input = unsafe { state.0.call_input(input_range.start, input_range.len()) }; + let input = state.0.call_input(input_range.start, input_range.len()); let current = state.message(); let (destination, caller, call_value, code_address) = match kind { @@ -252,8 +247,7 @@ fn call_inner( state: &mut InterpreterState<'_, '_, T>, kind: MessageKind, ) -> Result { - // SAFETY: The hooks do nothing. - unsafe { call_impl(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) } + call_with(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) } /// Executes a call opcode of `kind` with hooks around its child message. @@ -277,35 +271,6 @@ fn call_inner( /// An `Err` from either hook stops the instruction with that error. #[inline] pub fn call_with( - stack: StackMut<'_>, - gas: &mut Gas, - state: &mut InterpreterState<'_, '_, T>, - kind: MessageKind, - before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, - after: impl FnOnce( - &mut Gas, - &mut InterpreterState<'_, '_, T>, - &Message, - &MessageResult, - ) -> Result, -) -> Result { - let before = - |gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, message: &mut Message| { - message.input.make_owned(); - before(gas, state, message) - }; - // SAFETY: `before` copies the input before the hooks can reach it. - unsafe { call_impl(stack, gas, state, kind, before, after) } -} - -/// Body of [`call_with`]. The child message may borrow its input from memory. -/// -/// # Safety -/// -/// `before` and `after` must not read a borrowed input after changing memory, or let it outlive -/// the call. -#[inline] -unsafe fn call_impl( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, @@ -320,11 +285,8 @@ unsafe fn call_impl( ) -> Result { let mut message = Message::::default(); let mut return_memory_range = 0..0; - // SAFETY: Upheld by the caller for the hooks. Nothing else reads the message after the host - // returns. - let new_account_state_gas = unsafe { - prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range) - }?; + let new_account_state_gas = + prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range)?; before(gas, state, &mut message)?; let mut result = diff --git a/crates/evm2/src/interpreter/message.rs b/crates/evm2/src/interpreter/message.rs index aa5d17295..97689096b 100644 --- a/crates/evm2/src/interpreter/message.rs +++ b/crates/evm2/src/interpreter/message.rs @@ -2,11 +2,10 @@ use crate::{ BaseEvmTypes, EvmTypesHost, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, - interpreter::{InstrStop, Word}, - trustme, + interpreter::{InstrStop, Memory, Word}, }; use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; -use core::{fmt, ops::Deref}; +use core::ops::Range; /// EVM message kind. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] @@ -207,82 +206,111 @@ pub fn create_preaccess_failure( /// The input of a message: call data, or initcode for create messages. /// -/// It derefs to the input bytes. When [`Evm`](crate::Evm) runs without an inspector, the child of -/// a call instruction reads its input from the caller's memory instead of a copy, since the caller -/// waits for the call to return. [`Clone`] and [`to_bytes`](Self::to_bytes) copy borrowed bytes, so -/// a kept input always owns them. -#[derive(Default)] -pub struct MessageInput { - /// The owned bytes, or the borrowed bytes with their lifetime erased. - bytes: Bytes, - /// Whether `bytes` borrows. If set, `bytes` must never be cloned. - borrowed: bool, +/// The input of a call instruction's message is a range of the caller frame's memory instead of a +/// copy, since the caller waits for the call to return. The child frame reads it from there, and +/// the inspector hooks that hold the message read it with [`as_bytes`](Self::as_bytes) and the +/// caller's memory, which is the `interp.memory()` of the hook, and a precompile reads it with +/// [`Evm::message_input`](crate::Evm::message_input). To change an input, copy it out and assign +/// an owned one, as in `message.input = bytes.into()`. A clone of such an input is only the range, +/// and does not record whose memory it names: copy the bytes out with +/// [`to_bytes`](Self::to_bytes) to keep or forward them. +/// +/// A running frame reads its own input with +/// [`Interpreter::input`](crate::interpreter::Interpreter::input), and only [`Evm`](crate::Evm) +/// resolves a borrowed input when it enters a frame. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct MessageInput(Repr); + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Repr { + /// The input's bytes. + Owned(Bytes), + /// The input's range in the memory of the caller frame with the entry number `frame`. + Borrowed { range: Range, frame: u64 }, } -impl MessageInput { - /// Borrows `bytes` without copying. - /// - /// # Safety - /// - /// `bytes` must stay valid and unchanged for as long as the input is read. +impl Default for Repr { #[inline] - pub(crate) unsafe fn borrowed(bytes: &[u8]) -> Self { - // SAFETY: The caller keeps the bytes alive while they are read, and `borrowed` keeps - // them from being cloned out. - Self { bytes: Bytes::from_static(unsafe { trustme::decouple_lt(bytes) }), borrowed: true } + fn default() -> Self { + Self::Owned(Bytes::new()) } +} - /// Returns the input as owned bytes, copying borrowed ones. +impl MessageInput { + /// Creates an input that is `range` of the memory of the caller frame with the entry number + /// `frame`. #[inline] - pub fn to_bytes(&self) -> Bytes { - if self.borrowed { Bytes::copy_from_slice(&self.bytes) } else { self.bytes.clone() } + pub(crate) const fn borrowed(range: Range, frame: u64) -> Self { + Self(Repr::Borrowed { range, frame }) } - /// Copies borrowed bytes into an owned buffer. + /// Returns the range and the caller frame's entry number of a borrowed input. #[inline] - pub(crate) fn make_owned(&mut self) { - if self.borrowed { - *self = Self::from(Bytes::copy_from_slice(&self.bytes)); + pub(crate) const fn borrowed_range(&self) -> Option<(&Range, u64)> { + match &self.0 { + Repr::Owned(_) => None, + Repr::Borrowed { range, frame } => Some((range, *frame)), } } -} -impl Clone for MessageInput { + /// Returns the length of the input. #[inline] - fn clone(&self) -> Self { - Self::from(self.to_bytes()) + pub fn len(&self) -> usize { + match &self.0 { + Repr::Owned(bytes) => bytes.len(), + Repr::Borrowed { range, .. } => range.len(), + } } -} -impl fmt::Debug for MessageInput { + /// Returns whether the input is empty. #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.bytes.fmt(f) + pub fn is_empty(&self) -> bool { + self.len() == 0 } -} -impl PartialEq for MessageInput { + /// Returns the bytes of an input that owns them, and `None` for a range of the caller's + /// memory. #[inline] - fn eq(&self, other: &Self) -> bool { - self.bytes == other.bytes + pub const fn as_owned(&self) -> Option<&Bytes> { + match &self.0 { + Repr::Owned(bytes) => Some(bytes), + Repr::Borrowed { .. } => None, + } } -} -impl Eq for MessageInput {} - -impl Deref for MessageInput { - type Target = [u8]; + /// Returns the input bytes, reading them from `memory`, the caller frame's memory, unless the + /// input owns them. + /// + /// # Panics + /// + /// Panics if the input's range is out of bounds of `memory`. + #[inline] + pub fn as_bytes<'a>(&'a self, memory: &'a Memory) -> &'a [u8] { + match &self.0 { + Repr::Owned(bytes) => bytes, + Repr::Borrowed { range, .. } => &memory.as_slice()[range.clone()], + } + } + /// Returns the input as owned bytes, copying them from `memory`, the caller frame's memory, + /// unless the input owns them. + /// + /// # Panics + /// + /// Panics if the input's range is out of bounds of `memory`. #[inline] - fn deref(&self) -> &[u8] { - &self.bytes + pub fn to_bytes(&self, memory: &Memory) -> Bytes { + match &self.0 { + Repr::Owned(bytes) => bytes.clone(), + Repr::Borrowed { .. } => Bytes::copy_from_slice(self.as_bytes(memory)), + } } } impl From for MessageInput { #[inline] fn from(bytes: Bytes) -> Self { - Self { bytes, borrowed: false } + Self(Repr::Owned(bytes)) } } @@ -362,20 +390,32 @@ mod tests { } #[test] - fn kept_borrowed_input_owns_its_bytes() { - let mut memory = [1, 2, 3]; - // SAFETY: `memory` is unchanged until `input` is dropped. - let input = unsafe { MessageInput::borrowed(&memory) }; - let clone = input.clone(); - let bytes = input.to_bytes(); - // SAFETY: `memory` is unchanged until `owned` owns its bytes. - let mut owned = unsafe { MessageInput::borrowed(&memory) }; - owned.make_owned(); - drop(input); - memory.fill(0); - - assert_eq!(&*clone, [1, 2, 3]); - assert_eq!(bytes[..], [1, 2, 3]); - assert_eq!(&*owned, [1, 2, 3]); + fn caller_memory_input() { + let mut memory = Memory::new(); + memory.resize(0, 3).unwrap(); + memory.set(0, &[1, 2, 3]); + let input = MessageInput::borrowed(1..3, 0); + let owned = MessageInput::from(Bytes::from_static(&[9])); + let kept = input.clone(); + memory.set(1, &[7]); + + assert_eq!((input.len(), owned.len()), (2, 1)); + assert_eq!(input.as_owned(), None); + assert_eq!(owned.as_owned(), Some(&Bytes::from_static(&[9]))); + // A borrowed input, and a clone of it, read the caller's memory. + assert_eq!(input.as_bytes(&memory), [7, 3]); + assert_eq!(kept.as_bytes(&memory), [7, 3]); + assert_eq!(input.to_bytes(&memory)[..], [7, 3]); + assert_eq!(owned.as_bytes(&memory), [9]); + assert_eq!(MessageInput::default().len(), 0); + } + + #[test] + #[should_panic = "range end index 3 out of range for slice of length 2"] + fn caller_memory_input_out_of_bounds_panics() { + let mut memory = Memory::new(); + memory.resize(0, 2).unwrap(); + + MessageInput::borrowed(1..3, 0).as_bytes(&memory); } } diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index de4866774..966318baf 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -44,6 +44,13 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { output: Range, #[derive_where(skip)] message: Option<&'frame Message>, + /// The bytes of the message's input: the message's own bytes, or the caller's memory for an + /// input that is a range of it, with the lifetime erased. The suspended caller does not change + /// its memory until this frame returns. + pub(in crate::interpreter) input: &'frame [u8], + /// The number [`Evm`](crate::Evm) gave this frame when it entered it, which tells a range of + /// this frame's memory from one of another frame. Zero for a frame that `Evm` does not run. + frame: u64, host: Option>>, inspector: Option + 'host>>, /// Opcodes the inspector steps on the table this frame runs on. @@ -65,9 +72,8 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { spec: SpecId, features: EvmFeatures, is_static: bool, - /// Whether [`Self::call_input`] borrows memory. Set only for frames that [`Evm`](crate::Evm) - /// runs, whose host keeps a borrowed input from being read after the call or while the memory - /// changes. + /// Whether [`Self::call_input`] returns a range of memory. Set only for frames that + /// [`Evm`](crate::Evm) runs, which resolves the range against this frame's memory. lends_memory: bool, } @@ -108,6 +114,8 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { error: None, output: 0..0, message: None, + input: &[], + frame: 0, is_static: false, lends_memory: false, return_data: Bytes::new(), @@ -135,8 +143,10 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.error = None; self.output = 0..0; self.message = Some(message); + self.input = message.input.as_owned().map_or(&[][..], |input| &input[..]); self.is_static = is_static; self.lends_memory = false; + self.frame = 0; self.return_data = Bytes::new(); } @@ -151,6 +161,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.bytecode = Default::default(); self.bytecode_ref = None; self.message = None; + self.input = &[]; self.host = None; self.inspector = None; } @@ -271,34 +282,49 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { &mut self.memory } - /// Returns `len` bytes of memory at `offset` as the input of a call message, borrowed when - /// this frame is run by [`Evm`](crate::Evm) and copied otherwise. + /// Returns the range of memory at `offset` of `len` bytes as the input of a call message: a + /// range of this frame's memory when this frame is run by [`Evm`](crate::Evm), and a copy + /// otherwise. /// - /// # Safety - /// - /// The input must only be read as the input of a message passed to this frame's - /// [`Host::execute_message`](crate::interpreter::Host::execute_message), and memory must not - /// change and no other code may access this frame in between. It must not be read after - /// `execute_message` returns. - /// - /// # Panics - /// - /// Panics if the bytes are out of bounds of memory. + /// The range is only meaningful for the host that runs this frame, which reads it from this + /// frame's memory when it executes the message, so it must be in bounds of memory. #[inline] #[doc(hidden)] - pub unsafe fn call_input(&self, offset: usize, len: usize) -> MessageInput { - let bytes = self.memory.slice(offset, len); + pub fn call_input(&self, offset: usize, len: usize) -> MessageInput { + if len == 0 { + return MessageInput::default(); + } if !self.lends_memory { - return Bytes::copy_from_slice(bytes).into(); + return Bytes::copy_from_slice(self.memory.slice(offset, len)).into(); } - // SAFETY: The caller keeps memory unchanged until the host returns, and the host is - // `Evm`, which copies the input before any inspector hook can reach this frame or the - // message. Otherwise it only reads the message through shared references, which cannot - // move the input out, and cloning copies it. - unsafe { MessageInput::borrowed(bytes) } + MessageInput::borrowed(offset..offset + len, self.frame) } - /// Lets [`Self::call_input`] borrow memory for the rest of this frame. + /// Returns the bytes of the active message's input. + #[inline] + pub const fn input(&self) -> &[u8] { + self.input + } + + /// Returns the number [`Evm`](crate::Evm) gave this frame when it entered it. + #[inline] + pub(crate) const fn frame(&self) -> u64 { + self.frame + } + + /// Sets the number [`Evm`](crate::Evm) gave this frame when it entered it. + #[inline] + pub(crate) const fn set_frame(&mut self, frame: u64) { + self.frame = frame; + } + + /// Sets the bytes of the active message's input. + #[inline] + pub(crate) const fn set_input(&mut self, input: &'frame [u8]) { + self.input = input; + } + + /// Lets [`Self::call_input`] return ranges of this frame's memory for the rest of this frame. #[inline] pub(crate) const fn lend_memory(&mut self) { self.lends_memory = true; @@ -792,6 +818,12 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { unsafe { self.0.message.unwrap_unchecked() } } + /// Returns the bytes of the active message's input. + #[inline] + pub const fn input(&self) -> &[u8] { + self.0.input() + } + /// Returns whether the active frame forbids state-changing operations. #[inline] pub const fn is_static(&self) -> bool { diff --git a/crates/evm2/src/precompiles/table.rs b/crates/evm2/src/precompiles/table.rs index a843b1e63..4bed41a25 100644 --- a/crates/evm2/src/precompiles/table.rs +++ b/crates/evm2/src/precompiles/table.rs @@ -16,6 +16,9 @@ use core::fmt::{self, Display}; use derive_where::derive_where; /// Precompile implementation function. +/// +/// It reads the message's input with [`Evm::message_input`], since the input of a call +/// instruction's message is a range of the caller's memory. pub type PrecompileFn = fn(&mut Evm<'_, T>, &Message, &mut GasTracker) -> PrecompileResult; @@ -449,11 +452,11 @@ macro_rules! define_precompiles { #[allow(non_snake_case)] $vis const fn $name() -> $crate::precompiles::Precompile { fn run( - _evm: &mut $crate::Evm<'_, T>, + evm: &mut $crate::Evm<'_, T>, message: &$crate::interpreter::Message, gas: &mut $crate::interpreter::GasTracker, ) -> $crate::precompiles::PrecompileResult { - $f(message.input.as_ref(), gas) + $f(evm.message_input(message), gas) } $crate::precompiles::Precompile::new( diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index b12419932..e51d3d43c 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -468,6 +468,29 @@ pub(crate) fn neg(value: u64) -> Word { Word::ZERO.wrapping_sub(Word::from(value)) } +/// Calls `target` through `call_op` with memory 0..32 as input and returns its 32-byte output. +pub(crate) fn memory_input_call_code(target: Address, call_op: u8) -> Vec { + let mut code = Vec::new(); + push_all(&mut code, [Word::from_be_bytes([0xaa; 32]), Word::ZERO]); + code.push(op::MSTORE); + push_all( + &mut code, + [ + Word::from(32), + Word::from(32), + Word::from(32), + Word::ZERO, + Word::ZERO, + address_to_word(&target), + Word::from(50_000), + ], + ); + code.extend([call_op, op::POP]); + push_all(&mut code, [Word::from(32), Word::from(32)]); + code.push(op::RETURN); + code +} + pub(crate) fn legacy_bytecode(code: impl Into>) -> Bytecode { Bytecode::new_legacy(Bytes::from(code.into())) } diff --git a/crates/inspectors/src/tracing/fourbyte.rs b/crates/inspectors/src/tracing/fourbyte.rs index 9ae298893..3ce2c33d6 100644 --- a/crates/inspectors/src/tracing/fourbyte.rs +++ b/crates/inspectors/src/tracing/fourbyte.rs @@ -61,7 +61,7 @@ impl Inspector for FourByteInspector { message: &mut Message, ) -> Option> { // Skip precompile invocations, matching geth's 4byteTracer. - if let Some((selector, calldata)) = message.input.split_first_chunk() + if message.input.len() >= 4 && !self .precompiles .get_or_insert_with(|| { @@ -70,6 +70,8 @@ impl Inspector for FourByteInspector { precompiles }) .is_warm(&message.call_target) + && let Some((selector, calldata)) = + message.input.as_bytes(interp.memory()).split_first_chunk() { let selector = Selector::from(*selector); let calldata_size = calldata.len(); diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index b43902206..4b5aa88bd 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -728,7 +728,12 @@ impl Inspector for JsInspector { self.push_call( message.depth, - Contract { caller, contract, value: message.value, input: message.input.to_bytes() }, + Contract { + caller, + contract, + value: message.value, + input: message.input.to_bytes(interp.memory()), + }, message.kind.into(), message.gas_limit, interp.gas().refunded(), @@ -790,7 +795,7 @@ impl Inspector for JsInspector { caller: message.caller, contract: message.destination, value: message.value, - input: message.input.to_bytes(), + input: message.input.to_bytes(interp.memory()), }, message.kind.into(), message.gas_limit, diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index 42c27a7f5..039960b34 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -767,7 +767,7 @@ impl Inspector for TracingInspector { let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.to_bytes() + message.input.to_bytes(interp.memory()) } else { Bytes::new() }; @@ -810,7 +810,7 @@ impl Inspector for TracingInspector { let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.to_bytes() + message.input.to_bytes(interp.memory()) } else { Bytes::new() }; diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 828c5d64a..c0f5709fc 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -820,8 +820,7 @@ pub unsafe fn __revmc_builtin_call( call_kind == CallKind::Call, local_gas_limit, )?; - // SAFETY: The message goes straight to the host, and nothing reads it after the host returns. - let input = unsafe { ecx.interpreter().call_input(in_offset, in_len) }; + let input = ecx.interpreter().call_input(in_offset, in_len); let current = ecx.message(); let (destination, caller, call_value, code_address) = match call_kind { @@ -1167,7 +1166,9 @@ mod tests { ) -> Option> { self.call_static_flags .push(interp.is_static() || message.kind == MessageKind::StaticCall); - self.calls.push(message.clone()); + let mut message = message.clone(); + message.input = message.input.to_bytes(interp.memory()).into(); + self.calls.push(message); Some(self.execute_result.clone()) } @@ -1284,7 +1285,7 @@ mod tests { assert_eq!(inspector.calls[0].kind, MessageKind::Call); assert_eq!(inspector.calls[0].destination, target); assert_eq!(inspector.calls[0].caller, caller); - assert_eq!(inspector.calls[0].input.as_ref(), b"in"); + assert_eq!(inspector.calls[0].input.as_owned().unwrap()[..], *b"in"); assert!(!inspector.call_static_flags[0]); } @@ -1440,7 +1441,7 @@ mod tests { let inspector = host.clear_inspector_as::().unwrap(); assert_eq!(inspector.creates.len(), 1); assert_eq!(inspector.creates[0].kind, MessageKind::Create); - assert_eq!(inspector.creates[0].input.as_ref(), initcode); + assert_eq!(inspector.creates[0].input.as_owned().unwrap()[..], initcode); } #[test] @@ -1484,7 +1485,7 @@ mod tests { let inspector = host.clear_inspector_as::().unwrap(); assert_eq!(inspector.creates.len(), 1); assert_eq!(inspector.creates[0].kind, MessageKind::Create2); - assert_eq!(inspector.creates[0].input.as_ref(), initcode); + assert_eq!(inspector.creates[0].input.as_owned().unwrap()[..], initcode); assert_eq!(inspector.creates[0].salt, B256::from(salt.to_be_bytes())); } diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index 9bcc1ddf8..4133fca93 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -20,7 +20,7 @@ pub use linker::{Linker, shared_library_path}; /// ABI version of compiled artifacts. Bump when the calling convention, the execution layout, the /// `InstrStop` discriminants, or the stored artifact format change. -pub const ABI_VERSION: u32 = 7; +pub const ABI_VERSION: u32 = 8; /// Internal tests and testing utilities. Not public API. #[cfg(test)] diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index 9fac08850..c89e06711 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -107,9 +107,8 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { let interpreter_ptr = unsafe { NonNull::new_unchecked(interpreter_ptr) }; let block_env = NonNull::from(interpreter.host().block_env()); let tx_env = NonNull::from(interpreter.host().tx_env()); - let message = interpreter.message(); let gas = interpreter.gas(); - let calldatasize = message.input.len(); + let calldatasize = interpreter.input().len(); let return_data_len = interpreter.return_data().len(); let (stack_ptr, stack_len) = unsafe { interpreter.stack_mut().into_raw_parts() }; let stack = unsafe { EvmStack::from_mut_ptr(stack_ptr.cast()) }; @@ -180,8 +179,8 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { /// Returns calldata bytes. #[inline] - pub fn input(&self) -> &[u8] { - &self.message().input + pub const fn input(&self) -> &[u8] { + self.interpreter().input() } /// Returns the current block environment.
Release notes

Sourced from astral-sh/setup-uv's releases.

v10.3.3 🌈 Use Astral mirror for version manifests with GitHub fallback

Changes

This release contains something that should have been included a very long time ago. Sorry for sleeping on that.

This action now retrieves the release manifest that contains the available versions, their download links and checksums from the Astral mirror, rather than trying GitHub first. This should present much more stable. (Known checksums are still checked, so the security impact does not change)

Also a big thank you to @​soujasK for a set of bugfixes 👏

🐛 Bug fixes

🧰 Maintenance

📚 Documentation