Skip to content

db/state: RangeAsOf DB iterator starts from files.EndTxNum()#20360

Merged
sudeepdino008 merged 5 commits intomainfrom
sudeep/rao_db_files
Apr 8, 2026
Merged

db/state: RangeAsOf DB iterator starts from files.EndTxNum()#20360
sudeepdino008 merged 5 commits intomainfrom
sudeep/rao_db_files

Conversation

@sudeepdino008
Copy link
Copy Markdown
Member

  • In RangeAsOf, the DB iterator (HistoryRangeAsOfDB) now starts from max(startTxNum, files.EndTxNum()) instead of startTxNum directly
  • This avoids reading DB entries within the file range, which are redundant (served by HistoryRangeAsOfFiles) and prunable
  • Added TestRangeAsOf_DBIteratorSkipsFileRange to verify correctness after pruning DB entries in the file range

Closes #20357

@AskAlexSharov AskAlexSharov enabled auto-merge April 6, 2026 12:24
@AskAlexSharov AskAlexSharov requested a review from Copilot April 6, 2026 12:25
@AskAlexSharov AskAlexSharov disabled auto-merge April 6, 2026 12:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adjusts HistoryRoTx.RangeAsOf to avoid redundant DB iteration over the tx range already covered by frozen files, enabling safe pruning of DB entries in that range.

Changes:

  • Start the DB-side RangeAsOf iterator from max(startTxNum, files.EndTxNum()) to skip the frozen-file-covered range.
  • Add a regression test that exercises RangeAsOf behavior after pruning DB entries within the file range.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
db/state/history.go Shifts DB iterator start txNum to max(startTxNum, ht.iit.files.EndTxNum()) to avoid reading prunable/redundant DB data covered by files.
db/state/history_test.go Adds TestRangeAsOf_DBIteratorSkipsFileRange to validate RangeAsOf results remain correct after pruning DB entries within the file range.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread db/state/history_test.go Outdated
sudeepdino008 and others added 2 commits April 6, 2026 23:27
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@sudeepdino008 sudeepdino008 enabled auto-merge April 7, 2026 06:30
@sudeepdino008 sudeepdino008 added this pull request to the merge queue Apr 8, 2026
Merged via the queue into main with commit 12a1a1b Apr 8, 2026
34 of 35 checks passed
@sudeepdino008 sudeepdino008 deleted the sudeep/rao_db_files branch April 8, 2026 17:12
AskAlexSharov added a commit that referenced this pull request Apr 9, 2026
The optimization to start the DB iterator at max(startTxNum,
files.EndTxNum()) assumed files always cover all history entries below
EndTxNum. In practice, files may be incomplete (during segment merges,
partial rebuilds, etc.), causing the DB iterator to skip real history
entries. When history misses a key, DomainRoTx.RangeAsOf falls back to
the latest state, incorrectly returning current values for historical
queries (e.g., accounts appearing at block 1M that were created later).

This caused flaky failures in debug_accountRange and
debug_storageRangeAt RPC integration tests on mainnet.
github-merge-queue Bot pushed a commit that referenced this pull request Apr 11, 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,
#20361, #20362
- 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()`

## Test plan
- [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
- In `RangeAsOf`, the DB iterator (`HistoryRangeAsOfDB`) now starts from
`max(startTxNum, files.EndTxNum())` instead of `startTxNum` directly
- This avoids reading DB entries within the file range, which are
redundant (served by `HistoryRangeAsOfFiles`) and prunable
- Added `TestRangeAsOf_DBIteratorSkipsFileRange` to verify correctness
after pruning DB entries in the file range

Closes #20357

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
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
- In `RangeAsOf`, the DB iterator (`HistoryRangeAsOfDB`) now starts from
`max(startTxNum, files.EndTxNum())` instead of `startTxNum` directly
- This avoids reading DB entries within the file range, which are
redundant (served by `HistoryRangeAsOfFiles`) and prunable
- Added `TestRangeAsOf_DBIteratorSkipsFileRange` to verify correctness
after pruning DB entries in the file range

Closes #20357

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
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
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.

RangeAsOf (history): DB iterator reads data within file range (prunable data)

3 participants