Skip to content

triedb/pathdb: fix ID assignment in history inspection - #33103

Merged
rjl493456442 merged 5 commits into
ethereum:masterfrom
Forostovec:fix/pathdb-history-last-id-off-by-one
Nov 13, 2025
Merged

triedb/pathdb: fix ID assignment in history inspection#33103
rjl493456442 merged 5 commits into
ethereum:masterfrom
Forostovec:fix/pathdb-history-last-id-off-by-one

Conversation

@Forostovec

Copy link
Copy Markdown
Contributor

State history uses 1-based IDs, and rawdb.ReadStateHistory maps an ID to the ancient index by subtracting one (id-1). Therefore, the last ID equals the number of ancient items (head), not head-1.
Using last := head - 1 incorrectly treats Ancients() as the last 0-based index, which causes:

  • Skipping the newest history (ID head) when iterating [start, end].
  • Rejecting valid single-ID ranges due to first >= last being true when only one item exists (first == last).
  • Potential invalid reads or underflow handling in empty-store scenarios.

The corrected logic aligns with 1-based ID semantics used across pathdb (e.g., oldest ID is tail+1) and with rawdb’s accessor contracts.

@rjl493456442 rjl493456442 self-assigned this Nov 5, 2025
Comment thread triedb/pathdb/history_inspect.go Outdated
}
// Make sure the range is valid
if first >= last {
if first > last {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

first == last is allowed for querying single item

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

first == last is allowed for querying single item

added test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What I meant is you shouldn't change the condition here.
Please revert to first >= last

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What I meant is you shouldn't change the condition here. Please revert to first >= last

sorry for misunderstanding

@rjl493456442 rjl493456442 changed the title triedb/pathdb: fix off-by-one for last state history ID and handle empty ranges triedb/pathdb: fix ID assignment in history inspection Nov 13, 2025
@rjl493456442 rjl493456442 added this to the 1.16.8 milestone Nov 13, 2025
@rjl493456442
rjl493456442 merged commit eb8f325 into ethereum:master Nov 13, 2025
4 of 5 checks passed
atkinsonholly pushed a commit to atkinsonholly/ephemery-geth that referenced this pull request Nov 24, 2025
prestoalvarez pushed a commit to prestoalvarez/go-ethereum that referenced this pull request Nov 27, 2025
fjl pushed a commit to lightclient/go-ethereum that referenced this pull request Nov 28, 2025
weiihann pushed a commit to weiihann/go-ethereum that referenced this pull request Jan 14, 2026
gballet pushed a commit to BZO95/go-ethereum that referenced this pull request May 21, 2026
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