Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
[REST] added blockhash api, tests and documentation #11765
Added a /rest/blockhash/.json endpoint, so that the user can fetch a block hash by height via REST (analogous to the 'getblockhash' RPC method).
For someone wanting to gather block or header data via REST only, there was no way to begin fetching blocks/headers at specific heights without knowing the block hashes at those heights. This endpoint might also come in handy for someone wanting to quickly verify a block existing at a specific height in the active chain.
@jonasschnelli Yeah I noticed the hex/bin pattern but wasn't entirely sure if I should use those here too. I suppose I wasn't sure exactly what should be serialized in this case, as the other endpoints using bin/hex are serializing whole class/struct instances like CBlockHeader or CBlock, whereas this is a simple hash string. I can certainly add that support.
Thanks for taking a look!
Concept ACK. I agree it should be possible to query block by height.
There are only tests for successful calls. IMHO there should be tests for the errors too:
RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
RESTERR(req, HTTP_BAD_REQUEST, "Block height out of range: " + strHeight).
Another option would be to overload
/rest/block/[hash|height].[bin,hex.json]. Note that
json response includes the hash.
@promag - I thought about this too... After researching a bit I found an older issue (#6011) about essentially the same thing, but in RPC. I assumed from the opinions there that I should probably just add this blockhash endpoint and not overload the block endpoint.
As others say in #6011, it was possible to query block by height but with 2 calls. With REST call there is no way unless you walk back from the tip. Unless we want to mirror the RPC interface, I think overloading sounds cooler.
In that scenario, the difference between the 2 endpoints would be cache headers, since by hash the block is immutable but not by height (at least near the tip).
Generally looks great. Thanks for the contribution and the good tests.
There are a few code style nits, which I haven't commented on inline. Take a look at https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#coding-style. Notably:
- variables should be snake case
- switch code block should be indented
- the then clause for if statements should either be on the same line or enclosed in braces.
(I realise that you were probably copying surrounding style, but new code should adhere to the style guildelines where possible.)