Skip to content

fix(indexer): index the configured start height on a fresh database - #66

Open
setavenger wants to merge 1 commit into
integrationfrom
fix/initial-sync-first-height
Open

setavenger wants to merge 1 commit into
integrationfrom
fix/initial-sync-first-height

Conversation

@setavenger

Copy link
Copy Markdown
Owner

Problem

On a fresh database InitialSyncToTip computed syncTip = max(dbTip, SyncStartHeight) and then synced from syncTip+1. GetChainTip reports height 0 for an empty database, so the configured start height itself was never pulled. A fresh regtest oracle logs Starting initial sync chaintip=101 syncTip=1, never indexes height 1, and answers height 1 with an empty hash.

Clients that refuse to scan past an unindexed height (blindbit-rs #20) therefore never get past height 1, which breaks the testkit's regtest (T2) stack. The same +1 also skipped the start height when the database tip was below it.

Fix

The new initialSyncStartHeight (internal/indexer/builder.go) returns:

  • the configured start height when the database is empty (GetChainTip returned no hash)
  • max(tip+1, configured) otherwise

Tests

internal/indexer/fakechain_test.go adds a fake Bitcoin Core REST server. It serves chaininfo, blockhashbyheight, block and spenttxouts from an in-memory chain. Two tests use it with an in-memory pebble store:

  • TestInitialSyncIndexesConfiguredStartHeight runs the real InitialSyncToTip with start heights 1 and 3. It requires every height from the start height to the tip to carry the node's hash, and nothing below the start height to be indexed.
  • TestInitialSyncStartHeight is a table test of the rule, covering the empty database, resuming after the tip, and a tip below the configured start.

Both tests fail on integration:

initialsync_test.go:38: start 1: height 1: indexed hash , node hash 56d34bd6…
initialsync_test.go:38: start 3: height 3: indexed hash , node hash 90be3c1e…
initialsync_test.go:63: empty db starts at configured height: got 2, want 1
initialsync_test.go:63: tip below configured start: got 51, want 50

go build ./... && go vet ./... && go test ./... passes, both with the default build and with CGO_ENABLED=1 -tags libsecp256k1.

Overlap

🤖 Generated with Claude Code

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>
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.

1 participant