Repository navigation
fix(index): remove a reorged-out block's data when its height is replaced - #67
Open
setavenger wants to merge 2 commits into
Open
setavenger wants to merge 2 commits into
setavenger wants to merge 2 commits into
Conversation
InitialSyncToTip started at max(dbTip, SyncStartHeight)+1. On an empty database GetChainTip reports height 0, so the first pulled height was SyncStartHeight+1 and the configured start height itself was never indexed; a regtest oracle logged "syncTip=1" and answered height 1 with an empty hash. The same +1 skipped the start height whenever the database tip was below it. The start height is now the configured height on an empty database and max(tip+1, configured) otherwise. Tests run the initial sync against a fake Bitcoin Core REST server on an in-memory pebble store and require every height from the start height to the tip to carry the node's hash. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…aced Nothing was deleted on reorg: attachBlockToBatch only Sets, so the block that a reorg displaced kept every row it had written. - The compute index is keyed by height+txid and served by height, so rows of txs that were only in the displaced block were served mixed with the new block's rows. - Its blockhash->height row made heightIfOnBestChain treat it as best chain: a spend that existed only in the orphan kept marking the output spent under cut-through, and BlockhashInDB let the continuous-sync walk-back stop at an orphaned parent. - Tweaks and outputs of evicted txs stayed reachable. ApplyBlock now detects that a height already holds a different block and replaces it: in one batch it deletes the displaced block's KCIBlock, block->tx, tx-occurrence, spend, spent-outputs-short and txid-outpoints rows, every compute-index row at the height, and the tweak and outputs of its txs that no other best-chain block contains, then writes the new block. It flushes and waits for background commits first, so deletes are computed from complete state, and commits the batch synchronously, so no later batch can overtake it. Initial sync only writes unindexed heights and keeps its asynchronous batching. The continuous-sync walk-back now finds the fork point by comparing the index's hash at each height with the node's, then applies the new branch in ascending order, pulling one block at a time. It also triggers when the node is at the index's height on a different best block, a reorg that does not lengthen the chain. Databases indexed before this fix are repaired at the start of the initial sync: PurgeOrphanedBlocks finds blockhash->height rows whose height maps to another block, deletes those blocks' rows the same way, and rebuilds the compute index of their heights. No reindex is needed. Co-Authored-By: Claude Opus 5.5 (1M context) <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.
Stacked on #66. This branch contains #66's commit (the start-height fix and the fake-chain test helper it adds). Merge #66 first; after that, this PR's diff is only the reorg commit.
Problem
Nothing was deleted on a reorg.
attachBlockToBatchonly callsSet, so a block displaced by a reorg kept every row it had written:height+txidand served by height, so rows of txs that were only in the displaced block were served mixed with the new block's rows. A client acting on compute-index data could see outputs that do not exist.KCIBlock(blockhash→height) row, soheightIfOnBestChainandBlockhashInDBstill treated it as best chain. A spend that existed only in the orphan kept marking its output spent under cut-through and spent checks. The continuous-sync walk-back could also stop early at an orphaned parent.Fix
Store:
ApplyBlock→replaceBlock(newinternal/database/dbpebble/reorg.go). When a height already holds a different block, one pebble batch:KCIBlock(old)KBlockTx(old,*)KTxOccur(*,old)KSpend(*,*,old), enumerated throughKTxidOutpoints(old,*)KTxidOutpoints(old,*)KSpentOutputsShort(old)KComputeIndex(height,*)row.KTx/KOutof the displaced block's txs that no other best-chain block contains, checked viaKTxOccurplus a height↔hash round trip. A tx re-confirmed elsewhere keeps its rows. A tx re-confirmed in the replacing block has its rows deleted and then rewritten in the same batch.KCIHeightis overwritten as before.Ordering, and why it is safe:
replaceBlockfirst callsFlushBatch(true). This PR also makes that wait for in-flight background commits. Every block handed to the store earlier is therefore visible when the deletes are computed from committed state.Sets in the batch. Pebble applies a batch in order, so rewritten keys end up set.Walk-back (
internal/indexer/pull.go).SingleBlockPullAndHandlenow finds the true fork point by comparing the index's hash with the node's hash at each height, going down frommin(node, index tip). It no longer asks "is the parent hash inKCIBlock".ContinuousSyncnow also triggers when the node is at the index's height on a different best block (nodeAheadOrForked). Before, a reorg that did not lengthen the chain went unnoticed until the next block.Existing databases.
InitialSyncToTipfirst runsPurgeOrphanedBlocks. It findsKCIBlockrows whose height maps to another block, which is the trace every past reorg left. It deletes those blocks' rows the same way and rebuilds the compute index of their heights from the best block. Mainnet and signet databases therefore do not need a reindex.KCIBlockrows plus one point read each, and it writes nothing.server-only, or undersync --start-height/--end-height, which skipsInitialSyncToTip.Tests
In-memory pebble:
dbpebble.TestReorgRemovesDisconnectedBlockData, with the new branch applied both ascending and descending. The fixture reorgsA2(R,E) A3(S spends X:0)intoB2(N) B3(R). After the reorg:BlockhashInDB(A2/A3)returns falseTestReorgSameTxSameHeightandTestReapplySameBlockIsNoopcover the remaining cases.TestPurgeOrphanedBlocksreproduces a pre-fix database after the same reorg. The purge must leave it equal to a fresh index of the new chain, and a second run must be a no-op.indexer.TestContinuousSyncReorguses the fake Bitcoin Core REST server from fix(indexer): index the configured start height on a fresh database #66. It reorgs the node at the same height, to a longer chain, and at the tip only. One continuous-sync step must leave every height on the node's hash and no orphanKCIBlockrow.Red on
integration, with the store and pull changes reverted and the tests kept (excerpt):go build ./... && go vet ./... && go test ./...passes, both with the default build and withCGO_ENABLED=1 -tags libsecp256k1.go test -race ./internal/...passes.Performance
heightIfOnBestChainis on the hot path of every spent check and still does exactly one point read. I measured a variant that re-verified height→hash on every call at +~20% on a 2,000-spent-output cut-through fetch (8.6 ms vs 7.2 ms), so correctness relies on the delete invariant plus the startup purge instead. The round trip is only used where it is cheap:BlockhashInDB, and the reorg-time "tx elsewhere" check.ApplyBlockadds one point read per block. The delete work only happens on an actual reorg.Not covered
PurgeOrphanedBlocksbut does not compare heights against the node.Overlap
internal/server/(service.goand friends).database.DBinterface gainsPurgeOrphanedBlocks. Neither fix(grpc): NOT_FOUND for unindexed heights; document real FullTxItem byte order #64 nor fix(grpc): end streams with NOT_FOUND at unindexed heights #65 implements the interface.🤖 Generated with Claude Code