Skip to content

fix(state): re-baseline storage original_value only across tx boundaries - #3746

Merged
rakita merged 1 commit into
bluealloy:mainfrom
asdv23:fix/storage-original-value-tx-scoped-rebaseline
Jun 18, 2026
Merged

rakita merged 1 commit into
bluealloy:mainfrom
asdv23:fix/storage-original-value-tx-scoped-rebaseline

Conversation

@asdv23

@asdv23 asdv23 commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

EvmStorageSlot::mark_warm_with_transaction_id re-baselines the EIP-2200 original_value to present_value whenever the slot is cold:

let is_cold = self.is_cold_transaction_id(transaction_id);
if is_cold {
    self.original_value = self.present_value;
}

But is_cold_transaction_id is self.transaction_id.get() != transaction_id.get() || self.is_cold — it is true both on a genuine transaction boundary (id mismatch) and when the is_cold flag is set within the same transaction (a reverted StorageWarmed entry, or an explicit mark_cold()).

The EIP-2200 original (committed) value is transaction-scoped and independent of the EIP-2929 access list. Re-baselining it whenever a slot is cold conflates two orthogonal concepts. This PR keys the re-baseline on the actual transaction boundary instead:

if self.transaction_id.get() != transaction_id.get() {
    self.original_value = self.present_value;
}

Why this is correct (go-ethereum / spec)

In go-ethereum the two concepts are deliberately separate:

  • EIP-2200 original comes from GetCommittedState (state_object.go), backed by pendingStorage (committed by earlier txs in the block) / originStorage (trie value). It advances only across transaction/block boundaries and never consults the access list.
  • EIP-2929 warm/cold is a separate, independently-journaled access list (SlotInAccessList / accessListAddSlotChange), reverted on call revert.

makeGasSStoreFunc (operations_acl.go) reads them independently:

current, original = evm.StateDB.GetStateAndCommittedState(addr, slot) // EIP-2200 original
...
if _, slotPresent := evm.StateDB.SlotInAccessList(addr, slot); !slotPresent { // EIP-2929, separate
    cost += params.ColdSloadCostEIP2929
}

So original_value here (the EIP-2200 committed value) should mirror GetCommittedState: tx-scoped, decoupled from warm/cold. Keying its re-baseline on transaction_id does exactly that; keying it on is_cold does not.

Behavioral impact

None for standard execution. When is_cold is set within the same transaction (revert-induced un-warm, or mark_cold), the slot's original_value already equals present_value at that point, so the old re-baseline was a no-op. Verified:

  • cargo test across revm-state / revm-context / revm-interpreter / revm-handler / revm — pass.
  • execution-spec-tests state_tests (stable + develop) and legacy GeneralStateTests (Cancun + Constantinople) — all pass.
  • blockchain_tests: stable 39161 passed / 0 failed, develop 57488 passed / 0 failed (these exercise the genuine cross-transaction re-baseline that this PR keeps).

This is a robustness/precision change: it makes the re-baseline condition match its intent and prevents silent original_value corruption for any caller that marks a slot cold mid-transaction via the public mark_cold API.

`EvmStorageSlot::mark_warm_with_transaction_id` resets `original_value` to
`present_value` whenever the slot is cold. But `is_cold_transaction_id` is true
both on a genuine transaction-id mismatch AND when the `is_cold` flag is set
within the same transaction (e.g. a reverted `StorageWarmed` entry, or an
explicit `mark_cold`).

The EIP-2200 "original"/committed value is transaction-scoped and independent of
the EIP-2929 access list, so it should only be re-baselined when the slot
genuinely belongs to a previous transaction. Key the re-baseline on the
transaction-id mismatch instead of `is_cold`.

This matches go-ethereum, where the EIP-2200 original (`GetCommittedState`,
backed by pending/origin storage) is fully decoupled from the access list
(`SlotInAccessList`). No observable change for standard execution — all state
tests and ~97k blockchain tests pass identically — but it removes a latent
coupling that would corrupt `original_value` for any caller that marks a slot
cold mid-transaction.
@asdv23
asdv23 force-pushed the fix/storage-original-value-tx-scoped-rebaseline branch from 9d5718e to bab5895 Compare June 17, 2026 03:48
@asdv23

asdv23 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Hi @rakita — friendly ping on this one (open since Jun 8, CI hasn't been triggered yet). Just rebased onto latest main.

Short version: mark_warm_with_transaction_id re-baselines the EIP-2200 original_value whenever a slot is cold, but is_cold is also true for a slot flagged cold within the same tx (a reverted StorageWarmed, or an explicit mark_cold). This PR keys the re-baseline on a genuine transaction-id mismatch instead — matching go-ethereum, where the committed value (GetCommittedState) is decoupled from the access list (SlotInAccessList).

No observable change for standard execution: all state tests and the full blockchain_tests suite (stable 39161 / develop 57488) pass identically. It only removes a latent coupling that corrupts original_value for callers that mark a slot cold mid-transaction.

Could you approve the CI run and take a look when you have a moment? Happy to rebase again or adjust naming if you'd prefer.

@rakita

rakita commented Jun 17, 2026

Copy link
Copy Markdown
Member

Tru, on transition_id change, original value should be updated, multi_tx exec is kinda not used and will be deprecated in future so this is not propagating to reth.

@codspeed

codspeed Bot commented Jun 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing asdv23:fix/storage-original-value-tx-scoped-rebaseline (bab5895) with main (303ddbf)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@rakita
rakita merged commit 84fe60c into bluealloy:main Jun 18, 2026
32 of 34 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 17, 2026
@asdv23
asdv23 deleted the fix/storage-original-value-tx-scoped-rebaseline branch June 22, 2026 02:15
This was referenced Jun 29, 2026
This was referenced Jul 22, 2026
panos-xyz added a commit to morph-l2/morph-reth that referenced this pull request Sep 7, 2026
…e instructions (#194)

`sload_morph` and `sstore_morph` existed to undo an `original_value` clobber in
revm, not to express any Morph rule. `sstore_morph` in particular re-implemented
the whole upstream SSTORE lifecycle — static-call check, reentrancy sentry,
Berlin cold loading, dynamic gas and refund accounting — to insert one repair in
the middle of it.

Why they were needed: token fee deduction leaves the caller's fee-token slot
with the DB-committed `original_value`, the fee-deducted `present_value` and
`is_cold = true`. That is the same triple morph-geth has, where the EIP-2200
committed value (`GetCommittedState`) and the EIP-2929 access list are
independent. revm <= 41 conflated them in
`EvmStorageSlot::mark_warm_with_transaction_id`, re-baselining
`original_value = present_value` whenever a slot was cold — including a slot
marked cold within the same transaction. EIP-2200 then saw a clean slot and
charged `SSTORE_RESET` (2900) instead of the dirty-slot 100, diverging from
morph-geth by 2800 gas per write.

Why they are no longer needed: bluealloy/revm#3746 (released in revm-state
42.0.0, still present in 43) keys that re-baseline on the transaction boundary
instead of on the cold flag, so the committed value survives an explicit
`mark_cold()`. The native instructions now produce geth-equivalent gas on their
own. Verified as a controlled comparison against the golden e2e test:

  revm 41 + overrides   48_128  (previous state)
  revm 41 - overrides   50_928  (+2800: the divergence these overrides hid)
  revm 42 - overrides   48_128  (this commit)

The middle row also confirms
`morph_tx_v0_token_fee_transfer_to_fee_token_contract_gas_regression` is a real
tripwire rather than a permanently green assertion, so it is left untouched as
the gate for this change.

Removing the copies also retires three drifts that had already accumulated in
them:

- the EIP-8037 branch of upstream `sstore_default_gas_accounting` (`state_gas!`
  plus `refill_reservoir`) was never mirrored; dormant only because every Morph
  hardfork maps below AMSTERDAM.
- the SSTORE cold-load skip threshold stayed at revm 41's
  `cold_storage_additional_cost()` (2000) after upstream 42 moved it to
  `cold_storage_cost()` (2100); unreachable because the Istanbul reentrancy
  sentry already requires `remaining > call_stipend` (2300).
- the `original_value` repair ran for every cold slot, including slots of an
  account created within the same transaction, where revm reports `ZERO` while
  the DB may still hold residual storage.

`blockhash_morph` (0x40) and the disabled SELFDESTRUCT / BLOBHASH / BLOBBASEFEE
opcodes stay: those are genuine Morph semantics, unrelated to revm's version.

Closes #187
panos-xyz added a commit to morph-l2/morph-reth that referenced this pull request Sep 7, 2026
…gas (#191)

* fix: close reth 2.5.2 upgrade gaps in engine validator and intrinsic gas

Follow-ups to the v2.4.0 -> v2.5.2 upgrade, found while auditing that diff
against the upstream sources.

Forward `EngineValidator::on_canonical_head_changed`. reth 2.5.0 added this
method with an empty default body, so `MorphTreeEngineValidator` — a wrapper
that must delegate every method to `inner` — silently replaced
`BasicEngineValidator`'s implementation (txpool prewarming, opt-in via
`--engine.txpool-prewarming`) with the no-op default instead of failing to
compile. Document that forwarding contract on the type so the next upgrade
re-checks it: a method with a default body is exactly the case the compiler
cannot catch.

Derive the EIP-2780 intrinsic-gas info in `validate_initial_tx_gas` from
`Cfg::is_amsterdam_eip2780_enabled()` the way revm's own handler does, instead
of hardcoding the new `None` argument. `CfgEnv::with_spec_and_mainnet_gas_params`
(used by `MorphEvmConfig`) derives that flag from the spec, so a hardcoded
`None` would silently diverge from upstream intrinsic gas if a Morph hardfork
ever mapped to AMSTERDAM. Behaviour is unchanged today: every Morph hardfork
maps below AMSTERDAM.

Extend `test_morph_hardforks_do_not_enable_amsterdam_state_gas` to assert the
EIP-2780 flag stays off for every Morph hardfork, with a positive control at
AMSTERDAM so the guard cannot become vacuous if the flag stops being
spec-derived.

Restore the intent of `test_eip7702_refund_stays_regular_for_morph_specs`.
revm 42 removed `tx_eip7702_state_refund` / `tx_eip7702_auth_refund`, so the
test had been reduced to a duplicate of the state-gas assertion above it while
keeping a message about refunds, plus a hardcoded 12500. It now asserts the
surviving equivalent — the full per-auth refund equals
`PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST` — against revm's constants.

Drop `reth-trie-db`: it lost its last use when the `ChangesetCache` parameter
was replaced by `OverlayManager`.

Also record why `evm_call` hands a fresh journal checkpoint to
`Handler::execution` and why its `None` arm is unreachable for Morph.

* refactor(revm): drop custom SLOAD/SSTORE overrides in favour of native instructions (#194)

`sload_morph` and `sstore_morph` existed to undo an `original_value` clobber in
revm, not to express any Morph rule. `sstore_morph` in particular re-implemented
the whole upstream SSTORE lifecycle — static-call check, reentrancy sentry,
Berlin cold loading, dynamic gas and refund accounting — to insert one repair in
the middle of it.

Why they were needed: token fee deduction leaves the caller's fee-token slot
with the DB-committed `original_value`, the fee-deducted `present_value` and
`is_cold = true`. That is the same triple morph-geth has, where the EIP-2200
committed value (`GetCommittedState`) and the EIP-2929 access list are
independent. revm <= 41 conflated them in
`EvmStorageSlot::mark_warm_with_transaction_id`, re-baselining
`original_value = present_value` whenever a slot was cold — including a slot
marked cold within the same transaction. EIP-2200 then saw a clean slot and
charged `SSTORE_RESET` (2900) instead of the dirty-slot 100, diverging from
morph-geth by 2800 gas per write.

Why they are no longer needed: bluealloy/revm#3746 (released in revm-state
42.0.0, still present in 43) keys that re-baseline on the transaction boundary
instead of on the cold flag, so the committed value survives an explicit
`mark_cold()`. The native instructions now produce geth-equivalent gas on their
own. Verified as a controlled comparison against the golden e2e test:

  revm 41 + overrides   48_128  (previous state)
  revm 41 - overrides   50_928  (+2800: the divergence these overrides hid)
  revm 42 - overrides   48_128  (this commit)

The middle row also confirms
`morph_tx_v0_token_fee_transfer_to_fee_token_contract_gas_regression` is a real
tripwire rather than a permanently green assertion, so it is left untouched as
the gate for this change.

Removing the copies also retires three drifts that had already accumulated in
them:

- the EIP-8037 branch of upstream `sstore_default_gas_accounting` (`state_gas!`
  plus `refill_reservoir`) was never mirrored; dormant only because every Morph
  hardfork maps below AMSTERDAM.
- the SSTORE cold-load skip threshold stayed at revm 41's
  `cold_storage_additional_cost()` (2000) after upstream 42 moved it to
  `cold_storage_cost()` (2100); unreachable because the Istanbul reentrancy
  sentry already requires `remaining > call_stipend` (2300).
- the `original_value` repair ran for every cold slot, including slots of an
  account created within the same transaction, where revm reports `ZERO` while
  the DB may still hold residual storage.

`blockhash_morph` (0x40) and the disabled SELFDESTRUCT / BLOBHASH / BLOBBASEFEE
opcodes stay: those are genuine Morph semantics, unrelated to revm's version.

Closes #187
panos-xyz added a commit to morph-l2/morph-reth that referenced this pull request Sep 8, 2026
* chore: upgrade reth dependencies to v2.5.2

* fix: close reth 2.5.2 upgrade gaps in engine validator and intrinsic gas (#191)

* fix: close reth 2.5.2 upgrade gaps in engine validator and intrinsic gas

Follow-ups to the v2.4.0 -> v2.5.2 upgrade, found while auditing that diff
against the upstream sources.

Forward `EngineValidator::on_canonical_head_changed`. reth 2.5.0 added this
method with an empty default body, so `MorphTreeEngineValidator` — a wrapper
that must delegate every method to `inner` — silently replaced
`BasicEngineValidator`'s implementation (txpool prewarming, opt-in via
`--engine.txpool-prewarming`) with the no-op default instead of failing to
compile. Document that forwarding contract on the type so the next upgrade
re-checks it: a method with a default body is exactly the case the compiler
cannot catch.

Derive the EIP-2780 intrinsic-gas info in `validate_initial_tx_gas` from
`Cfg::is_amsterdam_eip2780_enabled()` the way revm's own handler does, instead
of hardcoding the new `None` argument. `CfgEnv::with_spec_and_mainnet_gas_params`
(used by `MorphEvmConfig`) derives that flag from the spec, so a hardcoded
`None` would silently diverge from upstream intrinsic gas if a Morph hardfork
ever mapped to AMSTERDAM. Behaviour is unchanged today: every Morph hardfork
maps below AMSTERDAM.

Extend `test_morph_hardforks_do_not_enable_amsterdam_state_gas` to assert the
EIP-2780 flag stays off for every Morph hardfork, with a positive control at
AMSTERDAM so the guard cannot become vacuous if the flag stops being
spec-derived.

Restore the intent of `test_eip7702_refund_stays_regular_for_morph_specs`.
revm 42 removed `tx_eip7702_state_refund` / `tx_eip7702_auth_refund`, so the
test had been reduced to a duplicate of the state-gas assertion above it while
keeping a message about refunds, plus a hardcoded 12500. It now asserts the
surviving equivalent — the full per-auth refund equals
`PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST` — against revm's constants.

Drop `reth-trie-db`: it lost its last use when the `ChangesetCache` parameter
was replaced by `OverlayManager`.

Also record why `evm_call` hands a fresh journal checkpoint to
`Handler::execution` and why its `None` arm is unreachable for Morph.

* refactor(revm): drop custom SLOAD/SSTORE overrides in favour of native instructions (#194)

`sload_morph` and `sstore_morph` existed to undo an `original_value` clobber in
revm, not to express any Morph rule. `sstore_morph` in particular re-implemented
the whole upstream SSTORE lifecycle — static-call check, reentrancy sentry,
Berlin cold loading, dynamic gas and refund accounting — to insert one repair in
the middle of it.

Why they were needed: token fee deduction leaves the caller's fee-token slot
with the DB-committed `original_value`, the fee-deducted `present_value` and
`is_cold = true`. That is the same triple morph-geth has, where the EIP-2200
committed value (`GetCommittedState`) and the EIP-2929 access list are
independent. revm <= 41 conflated them in
`EvmStorageSlot::mark_warm_with_transaction_id`, re-baselining
`original_value = present_value` whenever a slot was cold — including a slot
marked cold within the same transaction. EIP-2200 then saw a clean slot and
charged `SSTORE_RESET` (2900) instead of the dirty-slot 100, diverging from
morph-geth by 2800 gas per write.

Why they are no longer needed: bluealloy/revm#3746 (released in revm-state
42.0.0, still present in 43) keys that re-baseline on the transaction boundary
instead of on the cold flag, so the committed value survives an explicit
`mark_cold()`. The native instructions now produce geth-equivalent gas on their
own. Verified as a controlled comparison against the golden e2e test:

  revm 41 + overrides   48_128  (previous state)
  revm 41 - overrides   50_928  (+2800: the divergence these overrides hid)
  revm 42 - overrides   48_128  (this commit)

The middle row also confirms
`morph_tx_v0_token_fee_transfer_to_fee_token_contract_gas_regression` is a real
tripwire rather than a permanently green assertion, so it is left untouched as
the gate for this change.

Removing the copies also retires three drifts that had already accumulated in
them:

- the EIP-8037 branch of upstream `sstore_default_gas_accounting` (`state_gas!`
  plus `refill_reservoir`) was never mirrored; dormant only because every Morph
  hardfork maps below AMSTERDAM.
- the SSTORE cold-load skip threshold stayed at revm 41's
  `cold_storage_additional_cost()` (2000) after upstream 42 moved it to
  `cold_storage_cost()` (2100); unreachable because the Istanbul reentrancy
  sentry already requires `remaining > call_stipend` (2300).
- the `original_value` repair ran for every cold slot, including slots of an
  account created within the same transaction, where revm reports `ZERO` while
  the DB may still hold residual storage.

`blockhash_morph` (0x40) and the disabled SELFDESTRUCT / BLOBHASH / BLOBBASEFEE
opcodes stay: those are genuine Morph semantics, unrelated to revm's version.

Closes #187

* fix: close remaining reth 2.5.2 upgrade gaps

* test: cover token fee access list gas regression

Exercise Emerald and Jade with normal and tight gas limits. Assert block and receipt gas, transfer logs, balances, and exact token fee settlement. The access-list case fails on the PR base with 53,228 gas instead of 50,428.

* test(rpc): verify native eth_getProof and eth_getMultiProof on reth 2.5.2

* fix(proofs): complete reth 2.5 state provider migration
RealiCZ added a commit to megaeth-labs/revm that referenced this pull request Sep 18, 2026
…lloy#3746 bluealloy#3802 bluealloy#3864 (#54)

Co-authored-by: Prajjwal Chittori <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Anders Brownworth <[email protected]>
Co-authored-by: Ivan Wang <[email protected]>
Co-authored-by: cui <[email protected]>
@RealiCZ RealiCZ mentioned this pull request Sep 18, 2026
43 tasks done
panos-xyz added a commit to morph-l2/morph-reth that referenced this pull request Nov 11, 2026
…ing mid-transaction (#215)

The call-mode fee deduction emulated go-ethereum's `StateDB.Prepare` by calling
`evm.finalize()` in the middle of the transaction and re-inserting the state as
cold. Because `finalize()` also clears the journal's logs, the deduction and
refund Transfer logs were moved out into `pre_fee_logs`/`post_fee_logs` and
stitched back together by the receipt builder and the statetest runner. That
bypass dates from revm 33, when `ExecutionResult::Revert` carried no logs;
since revm 35 (bluealloy/revm#3424) reverts and halts return the journal's logs.

Do only what `Prepare` does: clear transient storage and mark every loaded
account and slot cold. Transaction ids are left alone, and revm-state 42
(bluealloy/revm#3746) re-baselines EIP-2200 original values only when they
change, so gas is unchanged. The fee logs stay in the journal, every
`ExecutionResult` carries them in go-ethereum's order, and the receipt builder
uses `result.into_logs()` directly.

This also removes a latent divergence: the mid-transaction `finalize()` reset
the journal's transaction id, so under revm's own `transact_many` the second
token-fee transaction re-baselined the payer's balance slot and paid 2,800
more gas than when executed on its own.

Slot-mode fee tokens never reach the changed code (no EVM frame, no logs);
their receipts were already the result's logs alone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants