rest: bugfix, fix crash error when calling /deploymentinfo #27853

pull brunoerg wants to merge 2 commits into bitcoin:master from brunoerg:2023-06-bugfix-rest-deploymentinfo changing 2 files +5 −1
  1. brunoerg commented at 2:34 am on June 11, 2023: contributor

    Calling /deploymentinfo passing a valid blockhash makes bitcoind to crash. It happens because we’re pushing a JSON value of type array when it expects type object. See:

    0jsonRequest.params = UniValue(UniValue::VARR);
    
    0jsonRequest.params.pushKV("blockhash", hash_str);
    

    This PR fixes it by changing pushKV to push_back and adds more test coverage.

  2. DrahtBot commented at 2:35 am on June 11, 2023: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK stickies-v, achow101
    Stale ACK andrewtoth

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #27788 (rpc: Be able to access RPC parameters by name by achow101)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. DrahtBot added the label RPC/REST/ZMQ on Jun 11, 2023
  4. brunoerg commented at 2:36 am on June 11, 2023: contributor
    Credits to @andrewtoth for noticing it!
  5. bitcoin deleted a comment on Jun 11, 2023
  6. bitcoin deleted a comment on Jun 11, 2023
  7. bitcoin deleted a comment on Jun 11, 2023
  8. andrewtoth approved
  9. andrewtoth commented at 1:16 pm on June 11, 2023: contributor
    ACK b550e9a12de97f4f682d1ed019dbc72f901dcbe5
  10. fanquake requested review from stickies-v on Jun 12, 2023
  11. in src/rest.cpp:616 in b550e9a12d outdated
    612@@ -613,7 +613,7 @@ static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const
    613     case RESTResponseFormat::JSON: {
    614         JSONRPCRequest jsonRequest;
    615         jsonRequest.context = context;
    616-        jsonRequest.params = UniValue(UniValue::VARR);
    617+        jsonRequest.params = UniValue(UniValue::VOBJ);
    


    stickies-v commented at 12:54 pm on June 12, 2023:

    We don’t do any named argument transformation when calling getdeploymentinfo().HandleRequest(jsonRequest) directly, so the jsonRequest is passed as is. If you look at the implementation of getdeploymentinfo(), you’ll see it fetches the argument by position: request.params[0].

    Even though this implementation works, it’s pretty fragile: it would also work if you’d used jsonRequest.params.pushKV("not-a-blockhash", hash_str);, for example.

    I think sticking to a VARR is best:

     0diff --git a/src/rest.cpp b/src/rest.cpp
     1index 580511b9c..ba149c1a9 100644
     2--- a/src/rest.cpp
     3+++ b/src/rest.cpp
     4@@ -613,7 +613,7 @@ static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const
     5     case RESTResponseFormat::JSON: {
     6         JSONRPCRequest jsonRequest;
     7         jsonRequest.context = context;
     8-        jsonRequest.params = UniValue(UniValue::VOBJ);
     9+        jsonRequest.params = UniValue(UniValue::VARR);
    10 
    11         if (!hash_str.empty()) {
    12             uint256 hash;
    13@@ -627,7 +627,7 @@ static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const
    14                 return RESTERR(req, HTTP_BAD_REQUEST, "Block not found");
    15             }
    16 
    17-            jsonRequest.params.pushKV("blockhash", hash_str);
    18+            jsonRequest.params.push_back(hash_str);
    19         }
    20 
    21         req->WriteHeader("Content-Type", "application/json");
    

    brunoerg commented at 1:25 pm on June 12, 2023:
    Agreed, gonna address it!
  12. in test/functional/interface_rest.py:427 in b550e9a12d outdated
    419@@ -420,6 +420,11 @@ def run_test(self):
    420 
    421         deployment_info = self.nodes[0].getdeploymentinfo()
    422         assert_equal(deployment_info, self.test_rest_request('/deploymentinfo'))
    423+        assert_equal(deployment_info, self.test_rest_request(f"/deploymentinfo/{bb_hash}"))
    424+
    425+        self.generate(self.wallet, 10, sync_fun=self.no_op)
    426+        deployment_info = self.nodes[0].getdeploymentinfo(bb_hash)
    427+        assert_equal(deployment_info, self.test_rest_request(f"/deploymentinfo/{bb_hash}"))
    


    stickies-v commented at 1:02 pm on June 12, 2023:
    Why do we need this repeated test? At the very least it seems orthogonal to the PR?

    brunoerg commented at 1:21 pm on June 12, 2023:
    I did it to ensure it is fetching data for old blocks correctly.

    stickies-v commented at 1:43 pm on June 12, 2023:

    Yeah I get that, but I don’t see why we wouldn’t just run the hash-specified test immediately on the older hash to avoid doing this twice? Also just querying the previous hash seems more efficient than running generate?

    Upon further thought, I think it does indeed make sense to include it in this PR.

     0diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py
     1index 559249101..1ba8f60d9 100755
     2--- a/test/functional/interface_rest.py
     3+++ b/test/functional/interface_rest.py
     4@@ -420,11 +420,10 @@ class RESTTest (BitcoinTestFramework):
     5 
     6         deployment_info = self.nodes[0].getdeploymentinfo()
     7         assert_equal(deployment_info, self.test_rest_request('/deploymentinfo'))
     8-        assert_equal(deployment_info, self.test_rest_request(f"/deploymentinfo/{bb_hash}"))
     9 
    10-        self.generate(self.wallet, 10, sync_fun=self.no_op)
    11-        deployment_info = self.nodes[0].getdeploymentinfo(bb_hash)
    12-        assert_equal(deployment_info, self.test_rest_request(f"/deploymentinfo/{bb_hash}"))
    13+        previous_bb_hash = self.nodes[0].getblockhash(self.nodes[0].getblockcount() - 1)
    14+        deployment_info = self.nodes[0].getdeploymentinfo(previous_bb_hash)
    15+        assert_equal(deployment_info, self.test_rest_request(f"/deploymentinfo/{previous_bb_hash}"))
    16 
    17         non_existing_blockhash = '42759cde25462784395a337460bde75f58e73d3f08bd31fdc3507cbac856a2c4'
    18         resp = self.test_rest_request(f'/deploymentinfo/{non_existing_blockhash}', ret_type=RetType.OBJ, status=400)
    

    brunoerg commented at 4:21 pm on June 12, 2023:

    Also just querying the previous hash seems more efficient than running generate?

    For sure, and for what we’d like to test may be enough, I’m gonna change it.

  13. rest: bugfix, fix crash error when calling `/deploymentinfo` ce887eaf49
  14. brunoerg force-pushed on Jun 12, 2023
  15. brunoerg commented at 1:27 pm on June 12, 2023: contributor
    Force-pushed addressing #27853 (review)
  16. test: add coverage for `/deploymentinfo` passing a blockhash 7d452d826a
  17. brunoerg force-pushed on Jun 12, 2023
  18. brunoerg commented at 4:31 pm on June 12, 2023: contributor

    Force-pushed addressing https://github.com/bitcoin/bitcoin/pull/27853/files#r1226690678

    Thanks @stickies-v for the review.

  19. stickies-v approved
  20. stickies-v commented at 5:43 pm on June 12, 2023: contributor
    ACK 7d452d826a7056411077b870efc3872bb2fa45e4
  21. DrahtBot requested review from andrewtoth on Jun 12, 2023
  22. achow101 commented at 10:28 pm on June 12, 2023: member
    ACK 7d452d826a7056411077b870efc3872bb2fa45e4
  23. DrahtBot removed review request from andrewtoth on Jun 12, 2023
  24. DrahtBot requested review from andrewtoth on Jun 12, 2023
  25. achow101 merged this on Jun 12, 2023
  26. achow101 closed this on Jun 12, 2023

  27. fanquake referenced this in commit 1d42ac6e1e on Jun 14, 2023
  28. fanquake referenced this in commit f74ffc03b4 on Jun 14, 2023
  29. fanquake commented at 12:55 pm on June 14, 2023: member
    Backported in #27887.
  30. fanquake referenced this in commit 72ead8699f on Jun 15, 2023
  31. fanquake referenced this in commit d845a3ed21 on Jun 15, 2023
  32. sidhujag referenced this in commit 9e70a3f7e9 on Jun 15, 2023
  33. fanquake referenced this in commit 642b5dd1b4 on Jun 16, 2023
  34. bitcoin locked this on Jun 13, 2024

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-22 03:12 UTC

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