#35799·go-ethereum

triedb/pathdb: stale history index after crash recovery onto a different fork returns wrong historical state

Author: 0xShadowXCreated Sep 25, 2026Updated Sep 25, 2026

System information

Commit hash: 920c077 (master)

Found by reading the code, not seen on a running node yet.

Expected behaviour

After an unclean shutdown, the state history index either matches the histories in the freezer or gets rebuilt, so historical state reads (eth_getBalance/eth_getStorageAt at an old block with path scheme + state indexing) stay correct.

Actual behaviour

The history metadata and per-account/slot index are written as each block is committed to the disk layer, but the trie state is only persisted when the buffer flushes. After a crash, repairHistory truncates the freezer back to the persisted state id, and nothing touches the index. newIndexIniter sees metadata.Last ahead of the freezer and goes into indexIniter.recover(). That loop only moves i.last up and down as histories are written or rolled back again. Once it reaches metadata.Last it treats the old index as valid.

The comment above recover() already says this assumes the regenerated histories are identical to the lost ones, and that fork awareness is missing. If the node reprocesses a different branch over that range (the CL moved to another head while the node was down), any account or slot touched only on the old branch keeps index entries for history ids that now hold different data.

On the read side, checkStateAvail only compares the global metadata against the freezer tail/head. stateHistoryReader.read trusts the per-ident index, so when an entry is stale it can conclude the value never changed after the queried id and return the latest value. No error comes back, just a wrong historical result.

Steps to reproduce the behaviour

I haven't driven a real crash plus reorg end to end. What I did check is the read side: in a triedb/pathdb test, remove one account's newest index entry with indexDeleter while leaving the freezer untouched, which leaves the same kind of mismatch recovery can produce. A query for a state id before that change then returns the account's current value instead of the historical one. I can share the test if useful.

One option is to store the block hash of the last indexed history next to metadata.Last and have recover() compare it with the regenerated history. On a mismatch it would unindex back to the fork point, or drop and rebuild the index, rather than trusting it.