Is there an existing issue for this?
- I have searched the existing issues
Current behaviour
GET /rest/headers/<BLOCK-HASH>.json?count=1 returns [] when the block is known but no longer part of the active chain. rest_headers finds the block in the block index, but only adds it to the response if active_chain.Contains(*pindex).
This prevents clients using REST for chain syncing from following a reorg. To find the common ancestor, they need to walk backward from both the new tip and the previously synced tip. Once the latter is on a valid stale fork, its headers can no longer be fetched through REST.
getblockheader still returns these headers. The only REST workaround I see is fetching the complete block through /rest/block, which is wasteful and also depends on the block data still being available.
This came up while fixing reorg handling for the REST chain source in LDK Node: https://github.com/lightningdevkit/ldk-node/pull/1022
This is also somewhat related to #33752.
Expected behaviour
There should be a way to fetch a known block header by hash through REST regardless of whether it is part of the active chain, similar to getblockheader.
Maybe /rest/headers/<BLOCK-HASH>?count=1 could return the requested header even when it is stale. Alternatively, a separate endpoint for fetching a single header by hash could be added if changing the range endpoint semantics isn't desirable.
Steps to reproduce
Start a fresh regtest node with -rest, then run:
bitcoin-cli -regtest createwallet test >/dev/null
addr=$(bitcoin-cli -regtest -rpcwallet=test getnewaddress)
bitcoin-cli -regtest generatetoaddress 3 "$addr" >/dev/null
old_branch_start=$(bitcoin-cli -regtest getblockhash 2)
old_tip=$(bitcoin-cli -regtest getbestblockhash)
bitcoin-cli -regtest invalidateblock "$old_branch_start"
bitcoin-cli -regtest generatetoaddress 3 "$addr" >/dev/null
bitcoin-cli -regtest reconsiderblock "$old_branch_start"
getchaintips now lists $old_tip as a valid-fork.
curl -s "http://127.0.0.1:18443/rest/headers/$old_tip.json?count=1"
This returns [], while the following succeeds and reports confirmations: -1:
bitcoin-cli -regtest getblockheader "$old_tip"
Relevant log output
No response
How did you obtain Bitcoin Core
Compiled from source
What version of Bitcoin Core are you using?
master
Operating system and version
Debian
Machine specifications
No response