Scanning blocks
Requirements
- A Bitcoin Core node.
txindexis not needed for scanning (whole blocks are read); it is needed only for the/api/txlookup of arbitrary transactions. - RPC method
getblock <hash> 3(verbosity 3 includesprevoutfor every input: value and script of the spent output). Available since Bitcoin Core 23.0. - Read-only RPC access. The indexer never signs or broadcasts.
Loop
every 2 seconds:
tip ← getblockcount()
if state.height ≥ 0 and getblockhash(state.height) ≠ state.tip:
state ← EMPTY # reorg: replay from scratch
while state.height < tip:
h ← state.height + 1
blk ← getblock(getblockhash(h), 3)
if h < H_start:
record (h, blk.hash); continue # no transactions examined
APPLY_BLOCK(blk, h) # see State transition
Inputs needed per transaction
| Data | From verbosity-3 block | Used by |
|---|---|---|
| input outpoints | vin[i].txid, vin[i].vout |
ledger lookup |
| spent value | vin[i].prevout.value |
line offsets, condition 8 |
| spent script | vin[i].prevout.scriptPubKey.hex |
candidate test (b), condition 2 |
| witness | vin[i].txinwitness |
conditions 2–4 |
| version, locktime, sequences, outputs | top level | txid recomputation (optional), claim parsing |
Values arrive as BTC decimals; convert with round(value · 10^8) to integer satoshis.
Cost
Full replay on the private test network (a few hundred blocks) takes seconds. On mainnet, replay time grows with the number of blocks since H_start and with block size; an undo log is advisable for a public deployment.
Known issue
If the node's height drops below the indexer's height (a reorg to a shorter chain), getblockhash(state.height) fails and the reference loop stops advancing until the node's chain grows back. A conforming indexer SHOULD treat this as a reorg.