fix(evm): bound store-cache depth amplification from stacked EVM snapshots#3713
Draft
codchen wants to merge 1 commit into
Draft
fix(evm): bound store-cache depth amplification from stacked EVM snapshots#3713codchen wants to merge 1 commit into
codchen wants to merge 1 commit into
Conversation
…shots Each successful EVM call frame stacks a new nested CacheMultiStore layer (DBImpl.Snapshot), and geth never signals success so the layers are never discarded. After N sequential successful calls the current context sits atop ~N nested cachekv layers, and Cosmos reads at that depth (e.g. the distribution `rewards()` precompile -> IterateDelegations + per-delegation reward math) walk every layer. That makes a linear number of reads O(N^2) in wall-clock while gas stays linear. Fix, entirely opt-in so non-EVM code is unchanged: - cachekv / giga store: add frozen+dirty state, Freeze()/Unfreeze(), and a memoized readThroughParent() that skips runs of frozen empty layers on the Get path. iterator() also returns the parent iterator directly when the layer is empty in-range (closing the unused cache iterator), collapsing the cacheMergeIterator chain. Reads become O(1) in depth instead of O(depth). - cachemulti: propagate Freeze()/Unfreeze() to sub-stores via a shared flag so lazily-materialized stores inherit the state. - DBImpl.Snapshot (x/evm/state and the giga fork) freezes the just-superseded layer, never the base layer (snapshottedCtxs[0], the flush target read by GetCommittedState). RevertToSnapshot unfreezes the re-exposed layer so a writable top is never treated as a skippable frozen layer. Safety: a layer only takes writes while it is the newest layer; skips are gated on `frozen && !dirty`, so a re-exposed-and-written layer is always consulted again. Benchmark: deep-stack Get goes from ~16us (depth 1024) to a flat ~134ns. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3713 +/- ##
==========================================
- Coverage 59.30% 58.38% -0.93%
==========================================
Files 2274 2187 -87
Lines 188706 178980 -9726
==========================================
- Hits 111917 104500 -7417
+ Misses 66716 65206 -1510
+ Partials 10073 9274 -799
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Each successful EVM call frame stacks a new nested
CacheMultiStorelayer inDBImpl.Snapshot(). The sei go-ethereum fork only callsRevertToSnapshoton failure — the success-pathDiscardSnapshotis a commented-out TODO, andvm.StateDBexposes no discard hook — so geth cannot tell sei-chain a frame succeeded and the layers are never released.After
Nsequential successful calls the current context sits atop ~Nnestedcachekvlayers. A Cosmos read at that depth then walks every layer:rewards()precompile (reachable viaCALL/STATICCALL) →DelegationTotalRewards→stakingKeeper.IterateDelegations(an iterator through the merge-iterator chain) plus ~8–10 per-delegation point reads (Validator,IncrementValidatorPeriod,GetDelegatorStartingInfo,GetValidatorHistoricalRewards).Nlayers. Done at growing depth across the attacker's call loop, a linear number of reads becomes O(N²) wall-clock while gas stays linear.Staking/distribution route through the regular nested
cachekv(not the giga store), and those layers are empty for EVM txs — yet the merge iterators andgetFromCachestill traverse them.Fix
Entirely opt-in, so all non-EVM code paths are unchanged (they never call
Freeze()):cachekv/ giga store — addfrozen+dirtystate,Freeze()/Unfreeze(), and a memoizedreadThroughParent()that skips runs of frozen empty layers on theGetpath.iterator()returns the parent iterator directly when the layer is empty in-range (closing the unused cache iterator), collapsing thecacheMergeIteratorchain. Reads become O(1) in depth.cachemulti— propagateFreeze()/Unfreeze()to sub-stores via a shared flag so lazily-materialized stores inherit the state.DBImpl.Snapshot(x/evm/stateand the giga fork) freezes the just-superseded layer — never the base layer (snapshottedCtxs[0], the flush target read directly byGetCommittedState).RevertToSnapshotunfreezes the re-exposed layer, since a writable top must never be treated as a skippable frozen layer.Why it's correct
A layer only receives writes while it is the newest layer; once superseded it is frozen and, if empty, stays empty. Skips are gated on
frozen && !dirty, so a re-exposed-and-written layer (revert → write → snapshot) is always consulted again. Within one DBImpl,RevertToSnapshotalso discards every layer above the target (so no live skip memo can go stale) and now unfreezes the target. The concurrentstatedb.Copy()trace path shares layers, but is safe because the EVM never reverts below a tx's starting layer, and the unfreeze gate covers the immediate-parent case.Verification
frozen_skip_test.goincachekvand giga store): read/iterate equivalence through deep empty stacks, shadowing/deletes through frozen layers (must not skip), the exact revert → write → snapshot sequence, unfreeze bypassing a stale skip memo, andWrite()reset.Getgoes from O(depth) (251 ns → 3.9 µs → 16.2 µs at depth 16/256/1024) to a flat ~134 ns — ~120× faster at depth 1024.-race:cachekv,cachemulti, both giga stores,x/evm/state,giga/deps/xevm/state,x/evm/keeper, the distribution + staking precompiles, and the full giga integration suite.gofmt,goimports,go vetclean.🤖 Generated with Claude Code