Skip to content

db/state: skip DB entries within file range in debugIteratePrefixLatest#20444

Merged
AskAlexSharov merged 3 commits into
mainfrom
dipl_db_files
Apr 11, 2026
Merged

db/state: skip DB entries within file range in debugIteratePrefixLatest#20444
AskAlexSharov merged 3 commits into
mainfrom
dipl_db_files

Conversation

@sudeepdino008

@sudeepdino008 sudeepdino008 commented Apr 9, 2026

Copy link
Copy Markdown
Member

Fixes #20355

Follow-up: #20474 applies the same fix to DomainLatestIterFile.initCursorMDBX() and advanceInFiles()

Test plan

  • New test passes with fix, fails without it
  • make lint clean
  • make erigon integration builds

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_CURSOR heap push to use step.ToTxNum(dt.stepSize) instead of math.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.

@sudeepdino008 sudeepdino008 changed the title db/state: fix debugIteratePrefixLatest DB cursor endTxNum priority over files db/state: skip DB entries within file range in debugIteratePrefixLatest Apr 10, 2026
@sudeepdino008 sudeepdino008 requested a review from Copilot April 10, 2026 12:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sudeepdino008 sudeepdino008 marked this pull request as ready for review April 10, 2026 13:06
@AskAlexSharov AskAlexSharov added this pull request to the merge queue Apr 11, 2026
Merged via the queue into main with commit 4a901ec Apr 11, 2026
35 checks passed
@AskAlexSharov AskAlexSharov deleted the dipl_db_files branch April 11, 2026 01:23
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
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>
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.

debugIteratePrefixLatest: skip DB entries within file range

3 participants