Motivation: getblockheader() has the count parameter to fetch more than one header in a single GET via REST.
This patch extends this functionality to bitcoin-cli and properly formats output arrays based on
the verbose parameter. This is a follow-up to an up-for-grabs issue #23330.
Implementation: I took into account the review comments and previous implementation. The CLI method is used as follows:
bitcoin-cli getblockheader <block_hash> <verbose=true> <count=0>
countdefaults to 0.
- If
countis excluded orcount = 0, the preexisting functionality is used to return the single block header of block <hash> in either hex or JSON format based onverbose.
- If
0 < count <= 2000, an array of block headers starting with block <hash> is returned in either hex or JSON format based onverbose.
The previous implementation traversed the
ActiveChainupwards (towards the genesis block), but again, this differs from the REST functionality that traverses theActiveChaindownwards (away from the genesis block), which I replicated instead. Ifcountsurpasses the number of blocks from the desired <HASH> to the active tip block, then the final block header in the array is simply that of the newest block on theActiveChain.
No regression impact.
The previous implementation defaulted to 0, but in the REST syntax, this is actually an invalidcountvalue. I believe that it makes sense to return a single block header by default.
- If
count < 0orcount > 2000, return anINVALID_PARAMETERerror. - If
countis not an integer, report a generic JSON parsing error.
Changes:
- The argument has been added to
rpc/client.cpp. - The
getblockheader()help message has been updated accordingly. - The
getblockheader()functionality has been expanded, as described above.