Backport upstream #563: Detect reorgs that occur while the server is down#4
Merged
Merged
Conversation
NewBlockCache did not initialize latestHash from the last block in the disk cache, so HashMatch vacuously accepted the first block ingested after every restart. If the backing node reorged across a lightwalletd restart, the new chain was appended on top of the orphaned blocks, which then remained in the compact-block cache permanently, serving wallets an internally inconsistent block stream. Initialize latestHash in NewBlockCache (using the existing setLatestHash helper, previously only called from Reorg) so the ingestor walks back orphaned blocks on the first ingest after a restart, the same way it does for reorgs detected while running. This also avoids the unnecessary drop and re-fetch of the cache tip on every restart when no reorg occurred. Co-Authored-By: Claude Fable 5 <noreply@anthropic.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.
Backports zcash#563 (single commit, cherry-picked clean).
Review
The bug is real and the fix is correct. Verified directly against the code:
NewBlockCacherebuilds the block index from disk but never setslatestHash, leaving it at the zero valuehash32.Nil(common/cache.go).HashMatchreturnstrueunconditionally whilelatestHash == hash32.Nil, so the first block ingested after every restart skips the prev-hash continuity check against the cache tip.The fix calls the existing
setLatestHash()helper (previously only invoked fromReorg) at the end ofNewBlockCache. Edge cases check out:latestHashstaysNil→ vacuous accept of the very first block, same as before (correct).setLatestHashalready routes throughrecoverFromCorruption.setLatestHashis satisfied:NewBlockCacheis documented single-threaded.REORG: dropping block <tip>and re-fetch on every restart (Nil never matchesgetbestblockhash); now it syncs immediately.Test verification: the new regression test fails on master (
latestHash not initialized after restart) and passes with the fix; fullgo test ./...passes on this branch.Interaction with #3 (parallel ingest backport): no file overlap, and the parallel ingestor uses the same
HashMatchgate, so this fix protects both ingest paths.🤖 Generated with Claude Code