db/state: invalidate BranchCache entries on Flush to fix stale trie reads - #21885
Open
erigon-copilot[bot] wants to merge 1 commit into
Open
erigon-copilot[bot] wants to merge 1 commit into
erigon-copilot[bot] wants to merge 1 commit into
Conversation
…eads Flush() did not touch the BranchCache, so the from-0 integration-path loop (Flush + ClearRam + tx.Commit) left stale commitment branches in the cache across batch boundaries. Subsequent reads hit the cache instead of MDBX, returning outdated branch nodes and producing wrong trie roots at blocks 263641 (parallel) / 513814 (serial). Flush now uses flushMemWithCallback to invalidate every commitment key it writes, forcing the next read to fall through to the DB. Co-authored-by: Giulio Rebuffo <giulio.rebuffo@gmail.com>
erigon-copilot
Bot
requested review from
AskAlexSharov and
sudeepdino008
as code owners
June 18, 2026 13:04
This branch has not been deployed
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.
Summary
stage-exec-test (from-0, parallel)at block 263641 and(from-0, serial)at block 513814SharedDomains.Flush()did not invalidate theBranchCache, so the integration-path from-0 loop (Flush+ClearRam+tx.Commit()) left stale commitment branches in the cache across batch boundariesFlush()now usesflushMemWithCallbackto invalidate every commitment-domain key it writes, forcing subsequent reads to fall through to MDBXRoot cause
The
BranchCacheintroduced in #21380 is checked before MDBX inGetLatestforCommitmentDomain.Commit()updates the cache after a successful commit, butFlush()— used by theintegration stage_execfrom-0 loop — left the cache untouched. When the same commitment branch was read (cached), updated, flushed, and then read again in the next batch,GetLatestreturned the stale cached value instead of the freshly committed DB value.Test plan
TestBranchCacheFlushInvalidatesStaleEntries— new test that reproduces the exact stale-cache scenario; confirmed FAILS without the fix, PASSES with itTestBranchCacheCommitRefreshesAfterReadThrough— existing test, still passesTestFromZero_GenesisAllocPreservedAfterResetReExec— passes (serial + parallel)db/state/execctxtests pass with-count=3 -raceexecution/commitmenttests pass with-count=3 -racemake lintcleanmake erigon integrationbuilds clean