[REST] added blockhash api, tests and documentation #11765

pull aaron-hanson wants to merge 1 commits into bitcoin:master from aaron-hanson:rest-blockhash-endpoint changing 3 files +108 −0
  1. aaron-hanson commented at 8:01 am on November 25, 2017: contributor

    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.

  2. jonasschnelli added the label RPC/REST/ZMQ on Nov 25, 2017
  3. jonasschnelli commented at 8:22 am on November 25, 2017: contributor

    Nice. Thanks for contributing. Concept ACK.

    What holds you back in completing this with supporting hex/bin? Should be trivial.

  4. aaron-hanson commented at 8:34 am on November 25, 2017: contributor

    @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!

  5. jonasschnelli commented at 8:39 am on November 25, 2017: contributor

    @aaron-hanson. You can just create a data stream and push the hashes onto the stream. A rest client could save ~50% brutto bandwidth over a JSON/hexstring.

    0CDataStream ssFooBar(SER_NETWORK, PROTOCOL_VERSION);
    1ssGetUTXOResponse << pindex->GetBlockHash();
    
  6. aaron-hanson force-pushed on Nov 25, 2017
  7. aaron-hanson commented at 10:16 am on November 25, 2017: contributor
    Added bin/hex formats, associated tests and documentation.
  8. promag commented at 11:01 am on November 25, 2017: member

    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.

  9. in src/rest.cpp:305 in e02f4c3425 outdated
    300+        CDataStream ssGetBlockHashResponse(SER_NETWORK, PROTOCOL_VERSION);
    301+        ssGetBlockHashResponse << pindex->GetBlockHash();
    302+        std::string strHex = HexStr(ssGetBlockHashResponse.begin(), ssGetBlockHashResponse.end()) + "\n";
    303+
    304+        req->WriteHeader("Content-Type", "text/plain");
    305+        req->WriteReply(HTTP_OK, strHex);
    


    jonasschnelli commented at 7:48 pm on November 25, 2017:
    Instead of forming the stream, use req->WriteReply(HTTP_OK, pindex->GetBlockHash()->GetHex());?

    aaron-hanson commented at 8:20 pm on November 25, 2017:
    Yeah I was wondering about this…I had just followed the pattern for the other endpoints’ hex formats. Directly writing the output of pindex->GetBlockHash().GetHex() reverses the order of the bytes as compared to HexStr(). I’m not sure which is correct?
  10. aaron-hanson commented at 11:28 pm on November 25, 2017: contributor

    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.

  11. promag commented at 0:52 am on November 26, 2017: member

    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).

  12. promag commented at 0:54 am on November 26, 2017: member
    BTW nice first contribution.
  13. in src/rest.cpp:317 in bef7f92092 outdated
    312+        req->WriteHeader("Content-Type", "application/json");
    313+        req->WriteReply(HTTP_OK, strJSON);
    314+        return true;
    315+    }
    316+    default: {
    317+        return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
    


    promag commented at 3:03 pm on November 26, 2017:
    Only json format? 😄

    aaron-hanson commented at 4:18 pm on November 26, 2017:
    Ah, nice catch…details!
  14. promag commented at 3:06 pm on November 26, 2017: member
    New tests looks good.
  15. promag commented at 9:38 am on November 27, 2017: member
    utACK b026c5f. Needs squash.
  16. promag commented at 3:01 pm on February 3, 2018: member
    Needs rebase.
  17. jnewbery commented at 9:54 pm on April 3, 2018: member

    @aaron-hanson - are you still working on this? It needs rebasing and squashing.

    Suggest we close with ‘up for grabs’ if there’s no activity on this PR soon.

  18. aaron-hanson commented at 10:29 pm on April 3, 2018: contributor
    @jnewbery - ah sorry, for some reason I missed the previous comments… I’ll squash/rebase and retest tonight.
  19. [REST] added blockhash api, tests and documentation
    Added a /rest/blockhash/<HEIGHT>.<bin|hex|json> endpoint, so that the user can fetch a block hash by height via REST (analogous to the 'getblockhash' RPC method).
    1323df9ff1
  20. aaron-hanson force-pushed on Apr 4, 2018
  21. aaron-hanson commented at 2:24 am on April 4, 2018: contributor
    @jnewbery @promag - squashed and rebased.
  22. in src/rest.cpp:292 in 1323df9ff1
    287+    const CBlockIndex *pindex = chainActive[nHeight];
    288+
    289+    switch (rf) {
    290+    case RetFormat::BINARY: {
    291+        CDataStream ssGetBlockHashResponse(SER_NETWORK, PROTOCOL_VERSION);
    292+        ssGetBlockHashResponse << pindex->GetBlockHash();
    


    jnewbery commented at 1:45 pm on April 4, 2018:
    Could the call to pindex->GetBlockHash() be refactored out of the switch statement, both to reduce code repetition and to minimise the scope of where cs_main is held?
  23. in test/functional/interface_rest.py:226 in 1323df9ff1
    222@@ -223,6 +223,56 @@ def run_test(self):
    223         self.nodes[0].generate(1) #generate block to not affect upcoming tests
    224         self.sync_all()
    225 
    226+
    


    jnewbery commented at 1:47 pm on April 4, 2018:
    Be aware of #12766, which completely refactors this test (and makes it much better IMO). That obviously conflicts heavily with this change.
  24. jnewbery commented at 1:50 pm on April 4, 2018: member

    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.)

  25. MarcoFalke added the label Needs rebase on Jun 6, 2018
  26. aaron-hanson commented at 9:01 pm on June 18, 2018: contributor
    @jnewbery thanks for the tips! I should have time this week to make the style adjustments and do a new rebase.
  27. fanquake added the label Up for grabs on Oct 28, 2018
  28. fanquake closed this on Oct 28, 2018

  29. adamjonas commented at 6:07 pm on October 21, 2019: member
    This was replaced by #14353. Up for grabs label can be removed.
  30. jnewbery removed the label Up for grabs on Oct 21, 2019
  31. laanwj removed the label Needs rebase on Oct 24, 2019
  32. MarcoFalke locked this on Dec 16, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 09:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me