db/state: skip DB entries within file range in debugIteratePrefixLatest#20444
Merged
Conversation
… instead of MaxUint64
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an ordering bug in debugIteratePrefixLatest’s cursor-merge heap so that stale DB entries no longer incorrectly override authoritative file entries for the first key returned, which can happen after partial (lexicographic) pruning scenarios.
Changes:
- Fix initial
DB_CURSORheap push to usestep.ToTxNum(dt.stepSize)instead ofmath.MaxUint64, aligning it with the existing advance-loop logic. - Add an integration test reproducing the stale-DB-vs-file precedence case and asserting that file values win within the file-covered range.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| db/state/domain_stream.go | Corrects initial DB cursor endTxNum so heap priority matches intended “files over stale DB within range” behavior. |
| db/state/domain_stream_test.go | Adds an integration test that fails pre-fix and passes post-fix, covering the reported regression case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
788def9 to
5344576
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
AskAlexSharov
approved these changes
Apr 10, 2026
sudeepdino008
added a commit
that referenced
this pull request
Apr 13, 2026
…st (#20444) Fixes #20355 - Skip DB entries whose step falls within the file range (`step.ToTxNum < files.EndTxNum()`) in both the initial DB cursor push and the advance loop — they are never loaded into the heap - Matches the "don't query DB for data in files" pattern from #20360, - Add `TestDomain_IteratePrefix_PrefersFilesOverDB` that simulates partial lexicographic prune and verifies file values win Follow-up: #20474 applies the same fix to `DomainLatestIterFile.initCursorMDBX()` and `advanceInFiles()` - [x] New test passes with fix, fails without it - [x] `make lint` clean - [x] `make erigon integration` builds
sudeepdino008
added a commit
that referenced
this pull request
Apr 13, 2026
…st (#20444) Fixes #20355 - Skip DB entries whose step falls within the file range (`step.ToTxNum < files.EndTxNum()`) in both the initial DB cursor push and the advance loop — they are never loaded into the heap - Matches the "don't query DB for data in files" pattern from #20360, - Add `TestDomain_IteratePrefix_PrefersFilesOverDB` that simulates partial lexicographic prune and verifies file values win Follow-up: #20474 applies the same fix to `DomainLatestIterFile.initCursorMDBX()` and `advanceInFiles()` - [x] New test passes with fix, fails without it - [x] `make lint` clean - [x] `make erigon integration` builds
5 tasks
Sahil-4555
pushed a commit
to Sahil-4555/erigon
that referenced
this pull request
Apr 26, 2026
…rigontech#20674) ## Summary - `DomainLatestIterFile.initCursorMDBX()` and `advanceInFiles()` now skip DB entries whose step falls within the file range, since files are authoritative for those entries - Applies the same "skip stale DB entries" pattern already merged in erigontech#20444 (`debugIteratePrefixLatest`) to `DomainLatestIterFile` - Improves clarity in the DupSort path (`val := value[8:]` instead of reassigning `value` directly) Fixes erigontech#20474 Part of erigontech#16239 ## Context After a partial lexicographic prune, the DB may retain stale entries with steps already covered by snapshot files. `DomainLatestIterFile` pushed these into the priority heap without checking, relying on a comment that said "DB entries will be ahead of files" — which is incorrect for prunable data. The fix adds a `filesEndTxNum` field (set from `domainRoTx.files.EndTxNum()`) and checks `endTxNum >= filesEndTxNum` before pushing. Entries within the file range are skipped, reducing unnecessary heap pushes and DB reads. All 4 affected locations (LargeValues + DupSort × initCursorMDBX + advanceInFiles) are fixed. Note: `DomainLatestIterFile` already derived `endTxNum` from the step, so the heap comparator was already picking file entries over stale DB entries at the same key. The primary value of this fix is consistency with the erigontech#16239 family and fewer unnecessary heap pushes / DB reads. ## Test plan - [x] New test `TestDomainLatestIterFile_PrefersFilesOverDB` — writes at two steps, builds files, deletes latest step from DB leaving stale entry, verifies `DebugRangeLatest` returns file value (DupSort path via AccountsDomain) - [x] New test `TestDomainLatestIterFile_PrefersFilesOverDB_LargeValues` — same scenario using CodeDomain to exercise LargeValues branches in both `initCursorMDBX` and `advanceInFiles` - [x] `go build ./db/state/...` - [x] `go test ./db/state/... -short` - [x] `make lint` (run twice, clean) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
Sahil-4555
pushed a commit
to Sahil-4555/erigon
that referenced
this pull request
Apr 27, 2026
…rigontech#20674) ## Summary - `DomainLatestIterFile.initCursorMDBX()` and `advanceInFiles()` now skip DB entries whose step falls within the file range, since files are authoritative for those entries - Applies the same "skip stale DB entries" pattern already merged in erigontech#20444 (`debugIteratePrefixLatest`) to `DomainLatestIterFile` - Improves clarity in the DupSort path (`val := value[8:]` instead of reassigning `value` directly) Fixes erigontech#20474 Part of erigontech#16239 ## Context After a partial lexicographic prune, the DB may retain stale entries with steps already covered by snapshot files. `DomainLatestIterFile` pushed these into the priority heap without checking, relying on a comment that said "DB entries will be ahead of files" — which is incorrect for prunable data. The fix adds a `filesEndTxNum` field (set from `domainRoTx.files.EndTxNum()`) and checks `endTxNum >= filesEndTxNum` before pushing. Entries within the file range are skipped, reducing unnecessary heap pushes and DB reads. All 4 affected locations (LargeValues + DupSort × initCursorMDBX + advanceInFiles) are fixed. Note: `DomainLatestIterFile` already derived `endTxNum` from the step, so the heap comparator was already picking file entries over stale DB entries at the same key. The primary value of this fix is consistency with the erigontech#16239 family and fewer unnecessary heap pushes / DB reads. ## Test plan - [x] New test `TestDomainLatestIterFile_PrefersFilesOverDB` — writes at two steps, builds files, deletes latest step from DB leaving stale entry, verifies `DebugRangeLatest` returns file value (DupSort path via AccountsDomain) - [x] New test `TestDomainLatestIterFile_PrefersFilesOverDB_LargeValues` — same scenario using CodeDomain to exercise LargeValues branches in both `initCursorMDBX` and `advanceInFiles` - [x] `go build ./db/state/...` - [x] `go test ./db/state/... -short` - [x] `make lint` (run twice, clean) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
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.
Fixes #20355
step.ToTxNum < files.EndTxNum()) in both the initial DB cursor push and the advance loop — they are never loaded into the heapTestDomain_IteratePrefix_PrefersFilesOverDBthat simulates partial lexicographic prune and verifies file values winFollow-up: #20474 applies the same fix to
DomainLatestIterFile.initCursorMDBX()andadvanceInFiles()Test plan
make lintcleanmake erigon integrationbuilds