Skip to content

[r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground#20594

Merged
AskAlexSharov merged 7 commits into
release/3.4from
alex/collation_race_fix_34
Apr 16, 2026
Merged

[r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground#20594
AskAlexSharov merged 7 commits into
release/3.4from
alex/collation_race_fix_34

Conversation

@AskAlexSharov

Copy link
Copy Markdown
Collaborator

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

BuildFilesInBackground collates 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, GetLatest returns the stale file values, causing SSTORE gas mispricing.

Fix: Restructure buildFiles into two phases:

  1. Phase 1 (sequential, single read-tx): Collate all domains and indices using one MDBX read-transaction. All collations see the exact same DB snapshot — zero race window.
  2. Phase 2 (parallel, no DB access): Build files from collations. This is the expensive part and remains fully parallel.

Additionally, add a committed txNum guard: don't collate step S until ComputeCommitment has 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 BuildFilesInBackground has filed that step, getLatestFromDb discards the restored entry (step covered by files) and falls through to getLatestFromFiles, returning the stale end-of-step value instead of the changeset-restored value.

Fix: Pass Aggregator.EndTxNumMinimax() into the unwind and tag restored entries with max(naturalStep, currentFilesEndStep).

Changes

  • db/state/aggregator.go: single-tx collation + parallel file building; committed txNum guard; stepFullyCommitted helper; pass current file boundary to unwind
  • db/state/domain.go: bump unwind step tag past current filed range
  • db/state/aggregator_test.go: TestAggregator_CommittedTxNumGuard
  • db/state/domain_test.go: TestDomain_CollationIsolatedFromLaterSteps, TestDomain_UnwindRestoredEntryVisibility
  • execution/commitment/commitmentdb/commitment_context.go: export DecodeTxBlockNums; fix minUnwindale typo; short-value length guard

…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.
@AskAlexSharov AskAlexSharov changed the title [r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground [wip] [r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground Apr 16, 2026
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.
@AskAlexSharov AskAlexSharov changed the title [wip] [r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground [r3.4] db/state: fix collation/pruning race and unwind visibility in BuildFilesInBackground Apr 16, 2026
@AskAlexSharov AskAlexSharov enabled auto-merge (squash) April 16, 2026 07:49
@AskAlexSharov

Copy link
Copy Markdown
Collaborator Author

i had couple servers which experienced gas missmatch and they working well on current pr.

@AskAlexSharov AskAlexSharov requested a review from JkLondon April 16, 2026 08:27
@AskAlexSharov AskAlexSharov merged commit 8d8e9c3 into release/3.4 Apr 16, 2026
23 of 24 checks passed
@AskAlexSharov AskAlexSharov deleted the alex/collation_race_fix_34 branch April 16, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants