Repository navigation
feat(amsterdam): EIP-8037 state-gas reservoir + EIP-2780/7976/7981/8038 gas model - #206
Conversation
Port the EIP-8037 dual-limit gas model from revm: state gas (storage/ account/code creation) draws from a reservoir threaded through call frames, separate from regular execution gas. - gas.rs: state_gas_spent is now i64 (negative within a frame from 0→x→0 refills propagating up); add refill_reservoir. - handle_reservoir_remaining_gas (system.rs): reconcile a child frame's reservoir back into its parent on return — take child reservoir and accumulate state_gas_spent on success; restore reservoir + state_gas_spent (signed) on revert/halt. Wired into call_inner and create_inner. - Thread the reservoir into child frames via Message.reservoir and the interpreter Gas constructor; preserve it across precompile/error results. - Charge sites: CREATE/CREATE2 upfront create_state_gas (+ refill on child failure), CALL new-account state gas, SELFDESTRUCT topup, code-deposit state gas + keccak hash cost, and SSTORE 0→x→0 reservoir refill. - Top level: initial_gas_and_reservoir splits the tx gas into a regular budget and reservoir across all tx handlers; leftover reservoir is reimbursed to the caller. - Use revm CPSB values: 1530 cost-per-state-byte, 64 SSTORE bytes, 120 account/create bytes; SstoreSetRefund is now the regular-only 2800 (the state portion is restored via refill_reservoir). Validated against the devnet eip8037 state fixtures (99/134 pass; remaining failures are EIP-7702 set_code/delegation and top-level CREATE-tx accounting, which depend on other unimplemented Amsterdam EIPs). Amsterdam stays skipped in eest forks.rs so the full suite remains green.
…sageResult Replace the free `handle_reservoir_remaining_gas` helper with methods on MessageResult that a returning child frame uses to reconcile EIP-8037 gas into its parent: - `reservoir_to_parent()` — child's final reservoir on success, else `reservoir + state_gas_spent` (matching revm; correct even when the child spilled state gas out of an exhausted reservoir into regular gas) - `state_gas_to_parent()` — child's net state gas on success, else 0 - `Gas::add_state_gas_spent` replaces `set_state_gas_spent` so the call site accumulates directly Also drop a redundant `refill > 0` guard in sstore and add `InterpreterState::clear_return_data`.
…d with tx_gas_used Add the revm `ResultGas` gas breakdown to `TxResult`: - fields: `total_gas_spent`, `state_gas_spent`, `refunded`, `floor_gas` - getters: `tx_gas_used`, `block_regular_gas_used`, `block_state_gas_used`, `spent_sub_refunded`, `final_refunded` `total_gas_spent` is defined as `tx_gas_limit - remaining - reservoir_reimbursed` so `tx_gas_used() == max(total_gas_spent - refunded, floor_gas)` reproduces the previous `gas_used`. `initial_state_gas` is threaded out of `initial_gas_and_reservoir` through the tx handlers into `settle_gas` so the top-level create's state gas is included. The `gas_used` field/getter is removed; all callers use `tx_gas_used()`. Not integrated (needs bigger work): the EIP-7702 per-authorization state-gas refund that revm subtracts from `state_gas_spent` is not modeled in evm2.
…d_post_floor - inline `total_gas_spent - refunded` into its two callers instead of exposing a `spent_sub_refunded` getter - rename `final_refunded` to `refund_post_floor` to describe the post-EIP-7623 adjustment more explicitly
…branch Reword TxResult field docs and the settle_gas comment to describe the gas breakdown directly instead of cross-referencing revm's ResultGas, and rename the test accordingly.
…iled-create refund Implement the EIP-8037 state-gas pieces that were missing for EIP-7702 and top-level CREATE transactions: - EIP-7702 per-auth state gas (account + bytecode) is charged as initial_state gas; existing authorities / already-delegated bytecodes earn a state-gas refund credited back to the reservoir. apply_auth_list now returns (refunded_accounts, refunded_bytecodes). - initial_gas_and_reservoir takes explicit initial_state_gas + state_refund; settle_gas subtracts the refund from reported state gas. - Intrinsic-gas validation now requires gas_limit >= intrinsic + initial_state gas (revm's initial_total_gas check). - A reverted/halted top-level CREATE refunds its intrinsic create_state_gas to the reservoir, and a regular-gas halt no longer forfeits the state-gas reservoir (reservoir_reimbursed treats halt like revert). eip8037_state_creation_gas_cost_increase: 97/134 -> 131/134. No regressions in the supported-fork EEST suite. Also enable Amsterdam EEST fixtures by default.
…e-gas breakdown Add block-header gas validation to the blockchain test runner, mirroring revm: accumulate tx_gas_used / block_regular_gas_used / block_state_gas_used per tx and check the header (Amsterdam: max(regular, state); earlier forks: cumulative tx gas). New BlockGasUsedMismatch error. The check surfaced gas-breakdown bugs in settle_gas, now fixed: - EIP-7623 floor is folded into total_gas_spent so block-level regular gas reflects it (tx_gas_used unchanged). - Execution state gas contributes only on success; a failed top-level CREATE nets out its intrinsic create_state_gas. is_create threaded into settle_gas. eip8037 blockchain gas mismatches 20 -> 3 (remaining are the separate max-initcode metering issue). No regressions in state tests or the supported-fork EEST suite.
Implement the latest EIP-8037 failure-handling semantics for state gas that spills from the reservoir into regular gas (`gas_left`). - Add `GasTracker.state_gas_spilled` (EIP's `state_gas_from_gas_left`), incremented by `spend_state` when a charge spills past the reservoir. - `unwind_state_gas`: on revert/halt, refill in LIFO order — spilled portion back to `remaining`, remainder restoring the reservoir to its frame-start value. Applied from `message_gas`; halt then zeroes `remaining`, burning the spill while leaving the parent reservoir untouched. - `refill_reservoir` (0→x→0 SSTORE restoration, failed-CREATE refund) is now LIFO too: credits `remaining` up to the spill first, remainder to the reservoir. - Propagate the child's spill to the parent on success via `spilled_to_parent`, so a later parent rollback returns it. The downloaded devnet EEST fixtures still encode the prior revision (failure refunds the full state gas to the reservoir, incl. the spill), so the EIP-8037 and spill-related halt/revert fixtures diverge until they are regenerated.
…child_gas Replace the per-field `*_to_parent` MessageResult accessors and the `message_gas` normalizer with a single `GasTracker::merge_child_gas(child, stop)` that settles a returning frame's gas for its stop reason: refill state gas in LIFO order on failure, return regular gas only on success/revert, always adopt the shared reservoir, and propagate net state gas, spill, and refund only on success. `execute_message_impl` settles each returning frame by merging it into a fresh accumulator (reusing the same function), so finish_*/precompile no longer normalize the tracker and every consumer — parent merge, top-level accounting, inspectors — reads the settled gas. This keeps the settlement at message-return, before the EIP-7702 auth refund is folded in.
`MessageResult::reservoir_reimbursed` just returned `self.gas.reservoir()` (the reservoir is already settled to its frame-start value on failure by `unwind_state_gas`). Inline it at both call sites and drop the method.
… execution-specs Implements the Glamsterdam state-access gas changes for AMSTERDAM, taking execution-specs (EELS, [email protected]) as the authority rather than revm, which has several gas bugs here that the EELS fixtures reject. EIP-8038 (state-access gas cost update, ethereum/EIPs#11802): WARM_ACCESS 100 (unchanged), COLD_ACCOUNT_ACCESS/COLD_STORAGE_ACCESS 3000, ACCOUNT_WRITE 8000, STORAGE_WRITE 10000, STORAGE_CLEAR_REFUND 12480, CREATE_ACCESS 11000. EXTCODESIZE/EXTCODECOPY get a second-read WARM_ACCESS in the static table. Cold SSTORE access is the full COLD_STORAGE_ACCESS (3000), so the cold add-on is the premium (2900) on top of the always-charged warm base, dropping the pre-8038 EIP-2929 warm double-count. EIP-2780 (reduce intrinsic transaction gas): Decomposed intrinsic TX_BASE(12000) + to-cost + value-cost, replacing the flat 21000 base; the EIP-7623 calldata floor base also drops to TX_BASE. Call value-cost is TRANSFER_LOG_COST + TX_VALUE_COST(4244); self-transfers pay neither to- nor value-cost. New `EIP2780` feature (auto-on at AMSTERDAM) and GasIds TxTransferLogCost/TxValueCost/TxCreateAccessCost. Top-level (depth-0) charges: an extra COLD_ACCOUNT_ACCESS for EIP-7702-delegated recipients (and warming the delegation target), and new_account_state_gas for empty recipients receiving value; precompiles are exempt. Diverges from revm per EELS: TX_BASE 12000 (revm 12300), floor base 12000 (revm 21000), value-cost via TX_VALUE_COST 4244 (revm ACCOUNT_WRITE 8000), SSTORE cold 3000 (revm 3100), and warming the delegation target.
…ate-gas # Conflicts: # crates/evm2/src/ethereum/eip1559.rs # crates/evm2/src/ethereum/eip2930.rs # crates/evm2/src/ethereum/eip4844.rs # crates/evm2/src/ethereum/eip7702.rs # crates/evm2/src/ethereum/legacy.rs # crates/evm2/src/evm/mod.rs # crates/evm2/src/interpreter/instructions/system.rs
The EIP-3529 refund cap is a fraction of the gas the transaction truly consumed. Under EIP-8037 the unused state-gas reservoir is reimbursed to the caller, so it must be excluded from `spent`; otherwise a large idle reservoir inflates `spent` and effectively disables the cap.
…as to execution-specs Two amsterdam gas bugs surfaced by the eip7981_increase_access_list_cost EEST fixtures: - EIP-7976: the calldata floor counts every byte uniformly (4 tokens each); zero bytes weighed only 1. Add gas param TxFloorZeroByteMultiplier (prague=1, amsterdam=4) and use it in floor_gas. The prior unit test only exercised non-zero bytes, masking the bug. - EIP-7702: the per-auth regular gas was a stale 9200 (from a wrong 'EIP-8037 base 7500'). execution-specs defines it as ACCOUNT_WRITE + REGULAR_PER_AUTH_BASE_COST = 8000 + (101*16 + 3000 + 3000 + 2*100) = 15816. Rewrite the constant to the full decomposition. Aligned to execution-specs (forks/amsterdam), not revm, which still uses placeholder EIP-8038 values here. eip7981 suite now 12/12; the prague eip7702_set_code_tx suite under amsterdam improves 13->29 passing.
- eest forks test asserted Amsterdam is skipped, but UNSUPPORTED_FORKS is now empty (Amsterdam is targeted). Test now checks is_fork_skipped agrees with the constant, so it won't go stale as the list changes. - Reflow doc comments to satisfy nightly rustfmt (wrap_comments).
…tion-specs - EIP-7702: rewrite auth-list refunds to follow execution-specs set_delegation per-authorization (granular AUTH_BASE/NEW_ACCOUNT refunds, refund rejected auths, ACCOUNT_WRITE regular refund for existing accounts). - CALL: drop the extra regular new-account cost on amsterdam (folded into CALL_VALUE); refund NEW_ACCOUNT state gas to the reservoir when the call does not create the target. - CREATE: refund the upfront create_state_gas when a create succeeds at a pre-existing alive (balance-only) target, at both opcode and tx level. - EIP-7954: raise amsterdam MAX_CODE_SIZE to 0x10000 (initcode 0x20000). - Resolve all TODO(ai) cleanups (rename unwind_state_gas->rollback_state_gas, drop dead refund_post_floor, dedupe EIP8038_WARM_ACCESS, etc.). amsterdam state_tests 257->313/313; full fixtures-glam-v600 state_tests 10515/10515.
Make ./scripts/setup_test_fixtures.py download the glamsterdam (Amsterdam) devnet-6 fixtures ([email protected]) plus the legacy Cancun/Constantinople tests by default, so the Amsterdam tests run without extra configuration. The glamsterdam devnet fixtures cover frontier..amsterdam, superseding the EEST main develop suite by fork coverage, so main develop/stable is now opt-in via EVM2_STATETEST_MAIN=1. DEVNET_VERSION/DEVNET_TAR default to the v6.0.0 release and can be overridden, or cleared to disable devnet; EVM2_STATETEST_DEVNET_ONLY=1 now only skips legacy.
evm2 does not build block access lists (EIP-7928) or block-level gas accounting without refunds (EIP-7778) yet, so every Amsterdam blockchain fixture fails on block structure/gas regardless of the EIP under test (see the todo! in assert_block_access_list). State tests have no block layer, so the Amsterdam state suite (313/313) keeps running. Add a blockchain-only is_blockchain_fork_skipped that skips Amsterdam and later in execute_suite, leaving the shared forks::is_fork_skipped untouched so state tests are unaffected. Drop the now-redundant per-fixture Amsterdam entries from the blockchain IGNORED_TESTS list. Full default suite (glamsterdam-devnet-6 + legacy, state + blockchain): 26809 passed, 0 failed, 341 skipped.
The existing-authority ACCOUNT_WRITE regular refund is identical before and under EIP-8037, so hoist it out of the per-regime branches and gate only the state-gas math behind a single is_eip8037 check.
v6.0.1 picks up the finalized EIP-8037 spill-burn semantics, so the amsterdam state tests that diverged against the stale v6.0.0 fixtures now pass. Full EEST suite: 26946 passed, 341 skipped.
# Conflicts: # crates/evm2/src/evm/mod.rs # crates/evm2/src/test_utils.rs
| pub const fn block_regular_gas_used(&self) -> u64 { | ||
| self.total_gas_spent.saturating_sub(self.state_gas_spent) | ||
| } | ||
|
|
||
| /// Returns the block-level state gas, equal to [`Self::state_gas_spent`]. | ||
| #[inline] | ||
| pub const fn block_state_gas_used(&self) -> u64 { | ||
| self.state_gas_spent |
There was a problem hiding this comment.
It is per transaction, but it represents value that needs to be added to the block gas counter.
But as you are pointing out, this name can be confusing
There was a problem hiding this comment.
Have consulted with clanker, we can drop block_* and just use regular_gas_used and state_gas_used it becomes more concrete.
| pub const fn returns_unused_gas(&self) -> bool { | ||
| self.stop.is_success() || self.stop.is_revert() | ||
| } | ||
|
|
||
| /// Returns unused gas that should be returned to the parent frame. | ||
| #[inline] | ||
| pub const fn gas_returned_to_parent(&self) -> u64 { | ||
| if self.returns_unused_gas() { self.gas.remaining() } else { 0 } | ||
| } | ||
|
|
||
| /// Returns the refund counter delta that should be propagated to the parent frame. | ||
| #[inline] | ||
| pub const fn refund_propagated_to_parent(&self) -> i64 { | ||
| if self.stop.is_success() { self.gas.refunded() } else { 0 } |
There was a problem hiding this comment.
are these no longer applicable?
There was a problem hiding this comment.
Have moved these to merge_child_gas
evm2/crates/evm2/src/interpreter/gas.rs
Lines 413 to 424 in 6b78c5b
…pile Bumps glamsterdam devnet fixtures to v6.1.0, which removed the precompile carve-out from the EIP-2780 top-frame NEW_ACCOUNT state charge (execution-specs #3048): an unfunded precompile is empty per EIP-161 and is created by a value transfer like any other account. Precompile recipients took execute_call_precompile, which never received the eip2780 charges, so the charge was silently skipped. Build and charge the frame gas once in execute_call_message before the precompile/interpreter split, then hand it to whichever path runs. settle_gas refills the charge on any non-success stop; the rollback unwinds the value transfer and its EIP-7708 log. Full EEST suite: 26948 passed, 341 skipped.
block_regular_gas_used/block_state_gas_used on TxResult read as block-scope but are per-transaction values (the split each tx adds to the block's separate regular/state gas counters). Rename to regular_gas_spent()/ state_gas_spent(), matching the total_gas_spent/state_gas_spent fields, and update the blockchain-test consumer. The block-level accumulators keep their block_* names.
onbjerg
left a comment
There was a problem hiding this comment.
looks fine to me, cc @DaniPopes
# Conflicts: # crates/evm2/src/evm/async.rs
|
cyclops audit |
| // These validate block/transaction gas allowance rules, not EVM execution. | ||
| "amsterdam/eip8037_state_creation_gas_cost_increase/block_2d_gas_accounting/tx_rejected_when_regular_gas_exceeds_block_limit_small.json", | ||
| "amsterdam/eip8037_state_creation_gas_cost_increase/state_gas_reservoir/creation_tx_state_check_exceeded.json", | ||
| // Amsterdam and later blockchain tests are skipped wholesale by fork in `execute_suite` |
There was a problem hiding this comment.
do these pass now? also remove the comment
There was a problem hiding this comment.
Those are blockchain tests that we skip rn, but should pass them in future. Will rm comment
| assert!(is_fork_skipped(SpecId::AMSTERDAM)); | ||
| assert!(!is_fork_skipped(SpecId::OSAKA)); | ||
| assert!(!is_fork_skipped(SpecId::FRONTIER)); | ||
| // Absent `EVM2_SKIP_FORKS`, a fork is skipped iff it is in `UNSUPPORTED_FORKS`. |
| /// Maximum deployed contract bytecode size since Amsterdam. | ||
| pub(crate) const MAX_CODE_SIZE_AMSTERDAM: usize = 0x8000; | ||
| /// | ||
| /// EIP-7954 raises the EIP-170 limit to 0x10000 (64 KiB); initcode stays at twice |
# Conflicts: # crates/evm2/src/evm/mod.rs # crates/evm2/src/interpreter/instructions/system.rs
GitHub's author_association can classify private org members as CONTRIBUTOR when the workflow token fetches PR metadata. This keeps the existing trusted PR-author gate, but falls back to repository permission and allows write, maintain, or admin users before rejecting. Fixes the Cyclops audit trigger on #206.
|
cyclops audit |
1 similar comment
|
cyclops audit |
The wholesale amsterdam blockchain-test skip and the empty UNSUPPORTED_FORKS list no longer apply, so drop the now-inaccurate comments and the test that asserted against them.
Implements the Amsterdam (Glamsterdam) gas model, aligned to
ethereum/execution-specs(EELS). The core is the EIP-8037 dual-limit reservoir; the remaining Amsterdam gas EIPs are layered on top.EIP-8037 — state-gas reservoir
State gas (storage / account / code creation) draws from a reservoir threaded through call frames, separate from regular execution gas.
gas.rs—state_gas_spentisi64(can go negative within a frame when 0→x→0 restorations refill more than the frame charged);refill_reservoir.MessageResult/merge_child_gas: on success the parent takes the child's final reservoir and accumulatesstate_gas_spent; on revert/halt it restoresreservoir + state_gas_spent(signed). Wired intocall_inner/create_inner.Message.reservoir, interpreterGasvianew_with_regular_gas_and_reservoir, preserved across precompile/error results.create_state_gas(+ refill on child failure), CALL new-account state gas, SELFDESTRUCT topup, code-deposit state gas + keccak hash cost, SSTORE 0→x→0 reservoir refill.initial_gas_and_reservoirsplits tx gas into a regular budget + reservoir across all 5 tx handlers; leftover reservoir reimbursed to the caller.CPSB=1530, 64 SSTORE bytes, 120 account/create bytes.EIP-7702 (Amsterdam) — set-code transactions
ACCOUNT_WRITE + REGULAR_PER_AUTH_BASE_COST=8000 + (101·16 + 3000 + 3000 + 2·100)= 15816 per EELS (was a stale 9200).EIP-2780 / EIP-8038 — intrinsic + state-access gas
ethereum/EIPs#11802) — COLD_ACCOUNT_ACCESS 3000, ACCOUNT_WRITE 8000, STORAGE_WRITE 10000, etc. (WARM_ACCESS unchanged at 100).tx.to- andtx.value-based charges), aligned to EELS.EIP-7976 / EIP-7981 — calldata floor + access list
TxFloorZeroByteMultipliergas param.EIP-3529
spent = gas_limit − remaining − reservoir); otherwise a large idle reservoir inflatesspentand disables the cap.Validation
EVM2_STATETEST_DEVNET_ONLY=1): 26948 / 26948 pass (341 skip).eip7981_increase_access_list_cost: 12/12.evm2unit tests pass;cargo clandcargo fmtclean.