Skip to content

Use journal instead of CacheKV for giga snapshot/rollback#3295

Open
codchen wants to merge 1 commit intomainfrom
tony/inter-block-cache-2
Open

Use journal instead of CacheKV for giga snapshot/rollback#3295
codchen wants to merge 1 commit intomainfrom
tony/inter-block-cache-2

Conversation

@codchen
Copy link
Copy Markdown
Collaborator

@codchen codchen commented Apr 22, 2026

Problem

giga/deps/xevm/state implemented Snapshot()/RevertToSnapshot() by stacking CacheMultiStore layers: each Snapshot() call wrapped the current context in a new cache, and RevertToSnapshot() restored an older context (dropping the outer cache) to undo KV-store writes. This meant N nested EVM snapshots within a single transaction produced N nested CacheMultiStore layers, with all the associated allocation and copy-on-write overhead. The transient/in-memory state (access lists, logs, refunds, transient storage, account status) was already journal-driven; only the persisted KV state relied on the CacheMultiStore trick.

Solution

Adopt the go-ethereum journal pattern for all state—both KV-stored and in-memory:

  • One CacheMultiStore per stateDB, created at NewDBImpl. It isolates the transaction's writes from the underlying store and is flushed once at Finalize(). No additional layers are created per snapshot.
  • Snapshot() = record journal length + revision ID. No context replacement, no cache allocation.
  • RevertToSnapshot() = binary-search for the revision, replay journal entries backward. Every KV mutation records the previous value in a new journal entry type; reverting calls the inverse keeper operation.

Event-manager isolation

Snapshot() pushes the current EventManager onto a stack and attaches a fresh one to the context. RevertToSnapshot() pops back to the snapshot's EM, discarding any events emitted inside the reverted range. Finalize() drains all surviving EMs (in order) into committedCtx.EventManager().

GetCommittedState

Previously read from snapshottedCtxs[0] (the context before the first stateDB-level CMS layer). Now reads from committedCtx, the original context saved at NewDBImpl time, which sits below the single stateDB CMS and therefore always reflects the pre-transaction committed state.

Other changes

  • EVMKeeper interface gains SetAddressMapping so deleteMappingChange can restore address associations on revert.
  • clearAccountState (used by Finalize) is unchanged and un-journaled; a new clearAccountStateJournaled wrapper (called from CreateAccount) captures prev state before clearing.
  • DeepCopy now uses maps.Copy instead of manual loops.

Tests (snapshot_test.go, 20 new cases)

@codeinputmachine
Copy link
Copy Markdown

Hey @codchen, Code Input detected this PR has a merge conflict. Some of the conflicts of this PR can be resolved with a semantic merge driver. Code Input can do that automatically: https://codeinput.com/r/EESR7EMMZd3 Let me know if you need more help with this conflict or how Code Input works.

@codchen codchen force-pushed the tony/inter-block-cache-2 branch from 1608a35 to 48a18ad Compare April 23, 2026 04:37
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 23, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedApr 23, 2026, 5:13 AM

@codchen codchen force-pushed the tony/inter-block-cache-2 branch from 48a18ad to 00974b3 Compare April 23, 2026 05:12
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

❌ Patch coverage is 75.90361% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.58%. Comparing base (8a0da0e) to head (00974b3).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
giga/deps/xevm/state/statedb.go 36.20% 37 Missing ⚠️
giga/deps/xevm/state/state.go 97.01% 1 Missing and 1 partial ⚠️
giga/deps/xevm/state/nonce.go 66.66% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3295      +/-   ##
==========================================
+ Coverage   58.56%   58.58%   +0.01%     
==========================================
  Files        2072     2072              
  Lines      208020   208095      +75     
==========================================
+ Hits       121833   121909      +76     
  Misses      77413    77413              
+ Partials     8774     8773       -1     
Flag Coverage Δ
sei-chain-pr 71.87% <75.90%> (?)
sei-db 69.36% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
giga/deps/xevm/state/balance.go 58.62% <100.00%> (+0.72%) ⬆️
giga/deps/xevm/state/code.go 64.70% <100.00%> (+2.20%) ⬆️
giga/deps/xevm/state/journal.go 90.32% <100.00%> (+5.57%) ⬆️
giga/deps/xevm/state/nonce.go 60.00% <66.66%> (+10.00%) ⬆️
giga/deps/xevm/state/state.go 97.36% <97.01%> (+0.74%) ⬆️
giga/deps/xevm/state/statedb.go 37.20% <36.20%> (-1.56%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants