[r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground#20594
Merged
Merged
Conversation
…lesInBackground Port of #20445 to release/3.4. Fixes two related bugs causing gas mismatches during execution (#20169). Bug 1: Collation/pruning race — BuildFilesInBackground opened a new read-transaction per domain/index, so execution could commit step S+1 data between reads. The collated file for step S then contained wrong values; after pruning, GetLatest returned stale file values. Fix: restructure buildFiles into two phases — Phase 1 collates all domains and indices using a single MDBX read-transaction; Phase 2 builds files in parallel from the collations (no DB access). Also adds a committedTxNum guard: don't collate step S until ComputeCommitment has confirmed all data through end of step S is flushed. Bug 2: Unwind entry visibility — after a reorg, restored entries were tagged with the natural step, which may already be covered by files. getLatestFromDb discarded those entries and fell through to stale file values. Fix: pass Aggregator.EndTxNumMinimax() into unwind and tag restored entries with max(naturalStep, currentFilesEndStep). Also: export DecodeTxBlockNums; fix minUnwindale typo; short-value guard.
Three unit tests that each FAIL against the current implementation, proving the PR has correctness issues: 1. TestStepFullyCommitted_ZeroIsGenesisNotSentinel stepFullyCommitted(committedTxNum=0, ...) returns true (bypass) for any input, conflating "genesis committed at txNum=0" with "no commitment yet". When genesis writes committedTxNum=0, ALL steps are bypassed — the guard is completely ineffective for the step that genesis belongs to. 2. TestDomain_CollationRaceNotTestedByIsolationTest TestDomain_CollationIsolatedFromLaterSteps tests cross-step filtering (trivially correct from bytes.Equal check), NOT the actual partial-commit race described in #20169. The real race: step S data in two separate DB commits, collation opening its read tx between them → second batch invisible → data permanently lost after pruning. Single-tx collation alone doesn't prevent this when committedTxNum=0 bypass allows premature collation. 3. TestDomain_UnwindOverBumpedStepMasksNewWrites AggregatorRoTx.Unwind uses at.a.EndTxNumMinimax()/StepSize() (current global minimax) rather than the per-domain snapshot FirstStepNotInFiles(). If BuildFilesInBackground filed a new step AFTER the AggregatorRoTx was opened, currentFilesEndStep is over-bumped (e.g. 2 instead of 1). The restored unwind entry lands at step 2. Post-reorg writes at step 1 do NOT replace the step-2 entry (different step byte encoding). getLatestFromDb then returns the stale step-2 restored value instead of the correct post-reorg step-1 value.
Collaborator
Author
|
i had couple servers which experienced gas missmatch and they working well on current pr. |
yperbasis
approved these changes
Apr 16, 2026
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.
Port of #20445 to release/3.4.
Summary
Fixes two related bugs in the domain state layer that cause gas mismatches during execution (#20169).
Bug 1: Collation/pruning race
BuildFilesInBackgroundcollates domain files by reading values from the DB via per-worker read-transactions opened at collation time. Between the moment a step is deemed ready and the collation reads, execution can commit new batches that overwrite step S values with step S+1 data. The collated file for step S then contains wrong values. After pruning removes the DB entries,GetLatestreturns the stale file values, causing SSTORE gas mispricing.Fix: Restructure
buildFilesinto two phases:Additionally, add a committed txNum guard: don't collate step S until
ComputeCommitmenthas confirmed all data through the end of step S is flushed.Bug 2: Unwind entry visibility after filing
After a reorg, the unwind restores domain entries tagged with a step derived from the unwind-target txNum. If
BuildFilesInBackgroundhas filed that step,getLatestFromDbdiscards the restored entry (step covered by files) and falls through togetLatestFromFiles, returning the stale end-of-step value instead of the changeset-restored value.Fix: Pass
Aggregator.EndTxNumMinimax()into the unwind and tag restored entries withmax(naturalStep, currentFilesEndStep).Changes
db/state/aggregator.go: single-tx collation + parallel file building; committed txNum guard;stepFullyCommittedhelper; pass current file boundary to unwinddb/state/domain.go: bump unwind step tag past current filed rangedb/state/aggregator_test.go:TestAggregator_CommittedTxNumGuarddb/state/domain_test.go:TestDomain_CollationIsolatedFromLaterSteps,TestDomain_UnwindRestoredEntryVisibilityexecution/commitment/commitmentdb/commitment_context.go: exportDecodeTxBlockNums; fixminUnwindaletypo; short-value length guard