Skip to content

fix(index): defer blocks that spend outputs stored in a later blk file - #268

Open
shanurwan wants to merge 1 commit into
Blockstream:new-indexfrom
shanurwan:fix/blk-import-out-of-order-spends
Open

shanurwan wants to merge 1 commit into
Blockstream:new-indexfrom
shanurwan:fix/blk-import-out-of-order-spends

Conversation

@shanurwan

Copy link
Copy Markdown

Fixes #214.

bitcoind writes blocks to blk*.dat in download order, not height order. Since f66e7fb adds and indexes each file's blocks in one pass, a block stored in an earlier file than the block that funds one of its inputs fails the prevout lookup, and _index panics on the unwrap() with MissingTxo. @nikicat traced this in #214. The fetcher sends one batch per file, so blocks within a file are fine, and the bug only shows when the funding block is in a later file.

Change

  • With the blk*.dat fetcher, a batch whose indexing hits MissingTxo is retried block by block. Blocks whose prevouts are not added yet stay added but unindexed, and the other blocks in the file are indexed as before.
  • If any blocks were deferred, the file import returns an error at the end. The existing RPC fallback from 6d5d5dd then fetches only the blocks that are not both added and indexed, which are exactly the deferred ones, and indexes them in height order.
  • Indexer::index / _index now return the lookup error instead of unwrapping it. On the RPC path a MissingTxo is now a returned error rather than a panic. The RPC path delivers blocks in order, so it should not hit this.

This keeps the single-pass add+index (and its write-buffer locality) for every block except the deferred ones. It does not touch the RPC or REST paths, so it should not conflict with #217.

Tests

  • New tests/index.rs::file_import_defers_blocks_that_spend_outputs_stored_later: two blk files, [0, 1, 2, 4] and [3], where block 4 spends block 3's coinbase. On new-index it panics with the same MissingTxo at schema.rs:744 as in the issue. With this change the import reaches tip 4, fetches only block 4 over RPC, and lookup_spend and the script stats find the spend. write_file now goes through a small write_files helper.
  • cargo fmt --check and cargo check --all-targets are clean (plus --features liquid). The lib, index, query, rest, config, scan_limits, rpc_max_connections and electrum_response_limits tests pass, and so do the electrum tests apart from the three wallet ones (balance, history, payment). Those hang in my container because electrumd doesn't start there, which looks like what test(electrum): replace electrumd with an in-process BDK wallet #267 addresses, so CI will be the check for them.

Real run, testnet4

This was a fresh index from Bitcoin Core 32.1's own blk files (94 files, tip 161,336), using --blocks-dir with defaults otherwise.

  • new-index (4e8e6fe): panics about 30 s in, at processing blocks 28237/161323, with MissingTxo("5ddd9284…611c01:0").
  • This branch: 3,687 of 161,336 blocks are deferred. The fallback logs blk-file import incomplete, fetching remaining='3687' of total='161336' blocks over RPC, and the sync reaches the same tip hash as Core in about 20 minutes. GET /tx/5ddd9284…611c01/outspend/0 returns the spend in block 42,519, and Core confirms its input. The funding tx is in block 42,518, so the two blocks are adjacent in height but stored in different files.

bitcoind writes blocks to blk*.dat in download order, so a block can sit in
an earlier file than the block that funds one of its inputs. Since adding and
indexing happen in one pass per file, indexing such a block failed the prevout
lookup and panicked with MissingTxo (Blockstream#214).

With the blk*.dat fetcher, a batch that hits MissingTxo is now indexed block by
block. Blocks whose prevouts are missing are left added but unindexed, and the
import returns an error at the end, so the existing RPC fallback indexes only
those blocks, in height order. Indexer::index now returns the lookup error
instead of unwrapping it.
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.

Regression: indexing bitcoin mainnet from scratch makes electrs panic

1 participant