bugfix: throw an error if an invalid parameter is passed to getnetworkhashps RPC #28554

pull jlopp wants to merge 1 commits into bitcoin:master from jlopp:getnetworkhashps_height_validation changing 2 files +45 −8
  1. jlopp commented at 7:10 pm on September 30, 2023: contributor

    When writing some scripts that iterated over many blocks to generate hashrate estimates I realized that my script was going out of range of the current chain tip height but was not encountering any errors.

    I believe that passing an invalid block height to this function but receiving the hashrate estimate for the chain tip instead should be considered unexpected behavior.

  2. DrahtBot commented at 7:10 pm on September 30, 2023: contributor

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

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK Sjors, kevkevinpal, achow101
    Concept ACK luke-jr
    Stale ACK maflcko

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

    Conflicts

    No conflicts as of last run.

  3. jlopp renamed this:
    throw an error if an invalid block height is passed to getnetworkhashps RPC endpoint
    bugfix: throw an error if an invalid block height is passed to getnetworkhashps RPC
    on Sep 30, 2023
  4. in test/functional/rpc_blockchain.py:454 in 057b3e05c2 outdated
    447@@ -448,6 +448,11 @@ def _test_getnetworkhashps(self):
    448             """).strip(),
    449             lambda: self.nodes[0].getnetworkhashps("a", []),
    450         )
    451+        assert_raises_rpc_error(
    452+            -8,
    453+            textwrap.dedent("Block does not exist at specified height").strip(),
    454+            lambda: self.nodes[0].getnetworkhashps(100, 99999999),
    


    kevkevinpal commented at 7:50 pm on September 30, 2023:
    instead of using the hardcoded 99999999 we can use self.nodes[0].getblockcount() + 1 which might be better

    jlopp commented at 8:17 pm on September 30, 2023:
    Good point; implemented.
  5. kevkevinpal commented at 7:51 pm on September 30, 2023: contributor
    ACK 057b3e05c233512cce78eadc88cb424276d04b45
  6. jlopp force-pushed on Sep 30, 2023
  7. kevkevinpal commented at 8:20 pm on September 30, 2023: contributor
    reACK 6074de6
  8. in src/rpc/mining.cpp:56 in 6074de667c outdated
    58@@ -59,6 +59,10 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch
    59         pb = active_chain[height];
    60     }
    61 
    62+    if (height > active_chain.Height()) {
    


    maflcko commented at 7:52 am on October 2, 2023:

    nit: Could move this early return earlier?

    Maybe also remove the && height < active_chain.Height() check as well, then?


    jlopp commented at 3:04 pm on October 2, 2023:
    Sure, done.
  9. in test/functional/rpc_blockchain.py:453 in 6074de667c outdated
    447@@ -448,6 +448,11 @@ def _test_getnetworkhashps(self):
    448             """).strip(),
    449             lambda: self.nodes[0].getnetworkhashps("a", []),
    450         )
    451+        assert_raises_rpc_error(
    452+            -8,
    453+            textwrap.dedent("Block does not exist at specified height").strip(),
    


    maflcko commented at 7:53 am on October 2, 2023:
    0            "Block does not exist at specified height",
    

    nit: Should be identical?


    jlopp commented at 3:04 pm on October 2, 2023:
    Confirmed.
  10. maflcko approved
  11. maflcko commented at 7:54 am on October 2, 2023: member
    Seems fine to do this. Not sure if a release note is needed, because strictly speaking this could be a breaking change.
  12. jlopp requested review from maflcko on Oct 2, 2023
  13. jlopp requested review from kevkevinpal on Oct 2, 2023
  14. kevkevinpal commented at 5:45 pm on October 2, 2023: contributor
    I would prefer 6074de6 and 786ba64 be squashed together in one commit
  15. jlopp force-pushed on Oct 2, 2023
  16. luke-jr approved
  17. luke-jr commented at 11:40 pm on October 2, 2023: member

    utACK

    nit: Remove pb == nullptr check below, as it is no longer possible.

  18. kevkevinpal commented at 5:53 am on October 3, 2023: contributor
    reACK 1fd3715
  19. DrahtBot removed review request from kevkevinpal on Oct 3, 2023
  20. maflcko commented at 7:55 am on October 3, 2023: member
    lgtm ACK 1fd371535648254eafcd3221478471e8c6db759b
  21. DrahtBot removed review request from maflcko on Oct 3, 2023
  22. jlopp force-pushed on Oct 3, 2023
  23. jlopp commented at 12:54 pm on October 3, 2023: contributor
    Upon further inspection I realized that the “lookup” nblocks parameter also had insufficient validation so I’ve added checks to ensure that it’s either a positive number or -1.
  24. jlopp renamed this:
    bugfix: throw an error if an invalid block height is passed to getnetworkhashps RPC
    bugfix: throw an error if an invalid parameter is passed to getnetworkhashps RPC
    on Oct 3, 2023
  25. jlopp requested review from kevkevinpal on Oct 3, 2023
  26. jlopp requested review from luke-jr on Oct 3, 2023
  27. jlopp requested review from maflcko on Oct 3, 2023
  28. in src/rpc/mining.cpp:78 in ec54efd7d0 outdated
    74     // If lookup is -1, then use blocks since last difficulty change.
    75-    if (lookup <= 0)
    76+    if (lookup == -1)
    77         lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
    78 
    79     // If lookup is larger than chain, then set it to chain length.
    


    maflcko commented at 1:02 pm on October 3, 2023:
    Will have to add CHECK_NONFATAL(pb) if you remove the nullptr check. Also, does this change the result for the genesis block, because you skip !pb->nHeight?

    jlopp commented at 1:49 pm on October 3, 2023:

    If pb->nHeight == 0 then lookup should get set to 0, then minTime and maxTime will both be the genesis block time, thus it will return 0.

    If removing the nullptr check doesn’t actually simplify the logic then let’s add it back.

  29. maflcko commented at 1:02 pm on October 3, 2023: member
    not sure about the last change
  30. in src/rpc/mining.cpp:57 in ec54efd7d0 outdated
    52@@ -53,17 +53,22 @@ using node::UpdateTime;
    53  * If 'height' is nonnegative, compute the estimate at the time when a given block was found.
    54  */
    55 static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) {
    56+    if (height > active_chain.Height()) {
    57+        throw JSONRPCError(RPC_INVALID_PARAMETER, "Block does not exist at specified height");
    58+    }
    59+
    60+    if (lookup < -1 || lookup == 0) {
    


    maflcko commented at 1:59 pm on October 3, 2023:
    If you wanted to change that, you’d also have to change the docstring above and do the same for height, no?

    jlopp commented at 3:14 pm on October 3, 2023:
    Yep, I’ve changed them to match the RPC help documentation
  31. jlopp force-pushed on Oct 3, 2023
  32. jlopp force-pushed on Oct 3, 2023
  33. jlopp force-pushed on Oct 3, 2023
  34. jlopp requested review from maflcko on Oct 3, 2023
  35. in src/rpc/mining.cpp:109 in 565ad11dd1 outdated
    105@@ -97,7 +106,7 @@ static RPCHelpMan getnetworkhashps()
    106                 "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
    107                 "Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
    108                 {
    109-                    {"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of blocks, or -1 for blocks since last difficulty change."},
    110+                    {"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of trailing blocks, or -1 for blocks since last difficulty change."},
    


    luke-jr commented at 6:24 pm on October 3, 2023:
    0                    {"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of previous blocks to calculate estimate from, or -1 for blocks since last difficulty change."},
    
  36. jlopp force-pushed on Oct 3, 2023
  37. jlopp requested review from luke-jr on Oct 4, 2023
  38. jlopp commented at 2:08 pm on October 4, 2023: contributor

    Not sure what’s going on with the windows build; it failed on a wallet address test with a socket timeout. Seems unrelated to my PR.

    Looks like it’s a known issue. https://github.com/bitcoin/bitcoin/pull/28509

  39. Sjors approved
  40. Sjors commented at 1:57 pm on October 10, 2023: member
    Lightly tested ACK 435ff29c5be0bd2ad2e89d9a37ecdb5da7c5d063
  41. DrahtBot removed review request from kevkevinpal on Oct 10, 2023
  42. DrahtBot requested review from kevkevinpal on Oct 10, 2023
  43. jlopp commented at 4:23 pm on October 17, 2023: contributor
    Any further concerns?
  44. DrahtBot removed review request from kevkevinpal on Oct 17, 2023
  45. DrahtBot requested review from kevkevinpal on Oct 17, 2023
  46. luke-jr referenced this in commit 319e6c2bed on Oct 19, 2023
  47. DrahtBot added the label Needs rebase on Nov 2, 2023
  48. Throw error if invalid parameters passed to getnetworkhashps RPC endpoint 9ac114e5cd
  49. jlopp force-pushed on Nov 7, 2023
  50. DrahtBot removed the label Needs rebase on Nov 7, 2023
  51. jlopp requested review from Sjors on Nov 8, 2023
  52. Sjors commented at 1:15 am on November 9, 2023: member

    Thanks for rebasing.

    re-utACK 9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658

  53. DrahtBot removed review request from kevkevinpal on Nov 9, 2023
  54. DrahtBot removed review request from Sjors on Nov 9, 2023
  55. DrahtBot requested review from kevkevinpal on Nov 9, 2023
  56. kevkevinpal commented at 10:44 pm on November 20, 2023: contributor
    reACK 9ac114e
  57. DrahtBot removed review request from kevkevinpal on Nov 20, 2023
  58. achow101 commented at 9:20 pm on November 28, 2023: member
    ACK 9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658
  59. achow101 merged this on Nov 28, 2023
  60. achow101 closed this on Nov 28, 2023

  61. jlopp deleted the branch on Nov 29, 2023
  62. glozow added the label Bug on Apr 5, 2024
  63. glozow added the label RPC/REST/ZMQ on Apr 5, 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-12-21 15:12 UTC

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