Repository navigation
Description
Description
Currently, MorphEvm in crates/revm/src/evm.rs overrides standard EVM opcodes SLOAD (0x54) and SSTORE (0x55) via custom instruction implementations (sload_morph and sstore_morph). Notably, sstore_morph duplicates ~80 lines of revm's internal SSTORE execution logic (static check, reentrancy sentry, Berlin cold loading, dynamic gas, and refund accounting).
While this workaround successfully prevents a ~2800 gas divergence vs morph-geth, maintaining custom implementations for fundamental EVM storage opcodes introduces significant technical debt and high consensus-divergence risk during upstream revm/reth upgrades.
Background & Root Cause
In Morph's alternative token fee model (feeTokenID > 0), the caller's ERC20 balance storage slot is deducted during validate_against_state_and_deduct_caller prior to transaction execution. To ensure that the subsequent transaction execution pays the EIP-2929 cold access fee (2100 gas) if it touches the same fee-token slot (matching morph-geth), the account and storage slot are marked cold (slot.mark_cold()).
In revm-state (40+ / 42.0.0), StorageSlot::mark_warm_with_transaction_id handles cold-to-warm transitions. If self.transaction_id.get() != transaction_id.get(), revm assumes the slot belonged to a previous transaction and re-baselines:
self.original_value = self.present_value;Because present_value was already mutated by the token fee deduction, resetting original_value = present_value causes EIP-2200 to treat the slot as "clean" rather than "dirty", charging 2900 gas (SSTORE_RESET) instead of 100 gas (WARM_STORAGE_READ_COST), creating a 2800 gas discrepancy vs go-ethereum.
Current Workaround & Risks
To fix this, sload_morph and sstore_morph were introduced to re-fetch the committed value from the DB on cold access and restore slot.original_value.
However, sstore_morph re-implements the full SSTORE lifecycle:
- Upstream Fragility: Any upstream refactors in
gas_params,sstore_dynamic_gas, or hardfork rules (Prague, Osaka, EOF) require manual synchronization. - Consensus Risk: If upstream fixes an edge case in SSTORE and
sstore_morphis not updated, a consensus divergence / chain split could occur.
Proposed Solution
Re-inspecting revm-state-42.0.0/src/lib.rs:620-633:
pub const fn mark_warm_with_transaction_id(&mut self, transaction_id: TransactionId) -> bool {
let is_cold = self.is_cold_transaction_id(transaction_id);
// Re-baseline the EIP-2200 original_value only when the slot belongs to a *previous* transaction
if self.transaction_id.get() != transaction_id.get() {
self.original_value = self.present_value;
}
self.transaction_id = transaction_id;
self.is_cold = false;
is_cold
}revm already protects against resetting original_value if self.transaction_id == transaction_id. The reset was triggered because the slot's transaction_id was not properly aligned with the main transaction (e.g., after sub-execution evm.finalize() or pre-execution slot loading).
We can resolve this cleanly at the state/handler level without touching opcodes:
- In
crates/revm/src/handler.rsat the end ofvalidate_and_deduct_token_fee:- Keep the committed DB value as
slot.original_value; - Keep the fee-deducted balance as
slot.present_value; - Explicitly sync
slot.transaction_id = evm.ctx().journal().transaction_id; - Set
slot.is_cold = trueand ensure warm cache/access-list entries are purged so the cold check succeeds.
- Keep the committed DB value as
- When the main transaction executes, revm's native
mark_warm_with_transaction_idsees matchingtransaction_ids and skips overwritingoriginal_value. - Completely remove
sload_morphandsstore_morphfromcrates/revm/src/evm.rs, restoring nativeEthInstructions::new_mainnet_with_spec(...).
Activity
panos-xyz commentedon Sep 7, 2026
Verdict: the proposed solution cannot work as written on the revm version we actually pin
I verified this both at unit level and end-to-end. Summary: the motivation is valid and this issue should stay open, but the proposed mechanism is based on a version mismatch and would silently reintroduce the 2800 gas divergence.
1. The quoted revm code is 42.0.0; we are pinned to 41.0.0
The issue quotes:
That is revm-state 42.0.0. The root
Cargo.tomlpinsrevm = "41.0.0"andCargo.lockresolves a single revm 41.0.0 (no[patch]section). revm-state 41.0.0 reads:The narrowed condition landed via our own upstream PR bluealloy/revm#3746 (merged 2026-06-18), first released in revm-state 42.0.0 (still present in 43.0.0). So "revm already protects against resetting
original_valueifself.transaction_id == transaction_id" is true for 42+, false for 41.Unit-level check against the pinned crate — slot with
original=100,present=90, matchingtransaction_id,mark_cold()applied:2. The stated root cause is wrong: the trigger is
is_cold, not atransaction_idmismatchInstrumented run at the first cold SLOAD of the fee slot:
The ids are already equal, and
original_valuehas already been clobbered by the time the instruction body runs. Why the ids can never diverge here:commit_txcall anywhere; each transaction goes throughExecuteEvm::transact=transact_one+finalize().JournalInner::finalize()resetstransaction_idtoTransactionId::ZERO(it does not increment it).So
transaction_idis a constant0throughout morph's execution path. The only thing that makesis_cold_transaction_id()return true is theis_coldflag set byslot.mark_cold()— and that flag is exactly what we need in order to charge the EIP-2929 2100 cold cost like morph-geth. On revm 41 this is a deadlock: charging the cold cost requires settingis_cold, and settingis_coldforces theoriginal_valuere-baseline.Consequently step 1's "explicitly sync
slot.transaction_id" is a no-op on thebalance_slotpath — they are already in sync.3. End-to-end: implementing the proposal costs exactly +2800 gas
I implemented the proposal literally (dropped the
0x54/0x55overrides, and invalidate_and_deduct_token_feesetslot.transaction_id = journal.inner.transaction_id; slot.is_cold = truewhile keeping the DB-committedoriginal_valueand the fee-deductedpresent_value):Good news: the existing golden test (
crates/node/tests/it/morph_tx.rs:459) does catch it, so this class of regression cannot reach mainnet silently.4. One step of the proposal would introduce a new divergence
Cold determination in revm is
slot.is_cold_transaction_id(tx_id) && access_list lookup misses. Thataccess_listis the transaction's own EIP-2930 list — morph-geth treats those slots as warm too (SlotInAccessList). Purging it would make us charge 2100 where geth charges 100. It is also unnecessary:mark_cold()alone already makes the cold check succeed (the experiment above did charge the cold cost).5. The motivation is sound — the upstream drift has already happened
This is not a hypothetical:
sstore_morphis missing the EIP-8037 branch that upstreamsstore_default_gas_accountinghas (state_gas!plusrefill_reservoir). It is dormant only because everyMorphHardforkmaps belowSpecId::AMSTERDAM, guarded bytest_morph_hardforks_do_not_enable_amsterdam_state_gas.sload_morphunconditionally takes the Berlin path, while upstreamsloadstill has a pre-Berlin branch. Safe only because morph's earliest fork maps to CANCUN.So the concern about hand-maintained SSTORE drifting from upstream is real and has already materialised once. The issue should stay — the plan needs rewriting.
6. Suggested replacement plan
Track A — the actual precondition for deleting the overrides: revm >= 42.
Note this cannot be done by bumping revm alone: reth v2.4.0 depends on revm
^41, so a standalone bump resolves two semver-incompatible revm versions and the types no longer line up across the reth API boundary. It has to ride a reth upgrade (reth main is already on revm 43). Two invariants to keep when we get there:48_128golden e2e test. It is the tripwire.transfer_erc20_with_evmbranch (handler.rs:628,evm.finalize()) stays fragile even on 42:finalize()resets the journal'stransaction_idto ZERO while the slots re-inserted viastate.extend()keep their old id. Both are0today, but if upstream ever switches tocommit_tx(incrementing ids), that branch gets a genuine id mismatch andoriginal_valueis re-baselined again — on 42 as well. The issue's "explicitly synctransaction_id" idea is genuinely useful here, as an asserted invariant, even though it is a no-op on the slot path.account.set_current_info_as_original()unconditionally on a cold account load, gated on the sameis_cold_transaction_id. Harmless for us today (token fee deduction only touches storage, never account info), but the same bug class remains one layer up.Track B — removes the duplication the issue complains about, today, without any revm bump.
revm 41 already exposes the extension point upstream intended for exactly this:
revm::interpreter::instructions::host::sstore_with_gas_accounting, documented as "Custom instruction sets can override the SSTORE opcode and call this helper with their own gas accounting closure", plus the publicsstore_default_gas_accounting. SLOAD can delegate tohost::sloadthe same way.Mechanics that make it work:
StackTr::data().last()before delegating;InstructionContext { interpreter, host }has public fields, so it can be reborrowed to keep access after the delegated call;original_valuebefore the access, restore it after if it changed — a local, opcode-scoped implementation of PR #3746's semantics, with no DB lookup.The static check, reentrancy sentry, cold loading, EIP-8037 state gas and refund accounting all go back to upstream; morph keeps roughly a dozen lines of
original_valuerepair instead of ~80 lines of copied SSTORE lifecycle.I prototyped this locally and it is green:
Happy to open a PR for Track B.
7. Side finding (low severity, separate issue)
sload_morph/sstore_morphcurrently repairoriginal_valuefrom the DB for every cold slot, including slots of an account created within the same transaction. revm returnsZEROwithoriginal = ZEROforis_newly_createdaccounts; if the DB holds residual storage for that(address, key), the repair writes a non-zerooriginal_valueand EIP-2200 gas/refund come out wrong. Practically unreachable (SELFDESTRUCT is disabled in Morph, so it needs an address with pre-existing storage and no code), but worth noting — the Track B snapshot/restore approach avoids it by construction.panos-xyz commentedon Sep 7, 2026
PR #194 implements this — deleting
sload_morph/sstore_morphand restoring nativeEthInstructions, stacked behind #188 (which brings revm 42 and therefore bluealloy/revm#3746) and #191.Not via the mechanism proposed in the description: on revm 41 the
transaction_idsync is a no-op (the ids already match — the trigger is theis_coldflag), and implementing it as written reintroduced the divergence at exactly +2800 gas. Details in the comment above.Verified as a controlled comparison on the
morph_tx_v0_token_fee_transfer_to_fee_token_contract_gas_regressiongolden test — revm 41 + overrides 48_128, revm 41 − overrides 50_928, revm 42 − overrides 48_128 — so the golden test is confirmed to be a real tripwire and is kept unchanged as the gate.refactor(revm): drop custom SLOAD/SSTORE overrides in favour of nativ…
fix: close reth 2.5.2 upgrade gaps in engine validator and intrinsic …