cli: report mining errors from -generate #37026

pull l0rinc wants to merge 2 commits into bitcoin:master from l0rinc:l0rinc/cli-generate-rpc-error changing 2 files +3 −0
  1. l0rinc commented at 12:58 PM on December 1, 2026: contributor

    Problem: After getnewaddress succeeds, bitcoin-cli -generate drops any RPC error returned by generatetoaddress and exits successfully. For example, -generate 1.5 prints "blocks": null even though the node rejects the fractional block count.

    Fix: Forward the mining RPC error so the CLI prints it and exits nonzero. -generate 1 0 sets maxtries to zero and still succeeds with "blocks": [].

  2. test: characterize mining RPC errors from -generate
    A fractional block count reaches `generatetoaddress`, which rejects it. The CLI currently exits successfully with a null `blocks` field.
    With zero mining attempts, the RPC succeeds and the CLI returns an empty list of blocks.
    17260935b8
  3. cli: report mining errors from -generate
    The CLI already reports errors from `getnewaddress`, but it wraps every `generatetoaddress` reply in a success response.
    Forward the mining RPC error before formatting the address and block hashes so scripts can detect failure from the exit status.
    3f3df24605
  4. DrahtBot added the label Scripts and tools on Dec 1, 2026
  5. DrahtBot commented at 12:58 PM on December 1, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK pablomartin4btc

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  6. DrahtBot added the label CI failed on Dec 1, 2026
  7. DrahtBot commented at 1:58 PM on December 1, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/46651488623/job/142656575111</sub> <sub>LLM reason (✨ experimental): intermittent_network: apt-get update failed because the Ubuntu security repository connection failed.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening.

    • A functional or integration test failure may be intermittent or unrelated to this pull request. If no dedicated issue thread exists, create a new one to track it.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  8. l0rinc closed this on Dec 1, 2026

  9. l0rinc reopened this on Dec 1, 2026

  10. DrahtBot removed the label CI failed on Dec 1, 2026
  11. in test/functional/interface_bitcoin_cli.py:383 in 3f3df24605
     379 | @@ -380,6 +380,7 @@ def run_test(self):
     380 |              assert_raises_process_error(1, JSON_PARSING_ERROR, self.nodes[0].cli('-generate', 'foo').echo)
     381 |              assert_raises_process_error(1, BLOCKS_VALUE_OF_ZERO, self.nodes[0].cli('-generate', 0).echo)
     382 |              assert_raises_process_error(1, TOO_MANY_ARGS, self.nodes[0].cli('-generate', 1, 2, 3).echo)
     383 | +            assert_raises_rpc_error(-1, 'JSON integer out of range', self.nodes[0].cli('-generate', '1.5').send_cli)
    


    pablomartin4btc commented at 3:40 AM on December 3, 2026:

    nit: this only covers a bad nblocks; a bad maxtries goes through the same path and might be worth a line...

                assert_raises_rpc_error(-1, 'JSON integer out of range', self.nodes[0].cli('-generate', '1.5').send_cli)
                assert_raises_rpc_error(-1, 'JSON integer out of range', self.nodes[0].cli('-generate', 1, '1.5').send_cli)
    

    pablomartin4btc commented at 3:57 AM on December 3, 2026:

    nit: since any value the RPC rejects is now reported, maybe also cover the type-error path (e.g. a JSON string), which previously also returned "blocks": null...

                assert_raises_rpc_error(-1, 'JSON integer out of range', self.nodes[0].cli('-generate', '1.5').send_cli)
                assert_raises_rpc_error(-3, 'Wrong type passed', self.nodes[0].cli('-generate', '"1"').send_cli)
    
  12. pablomartin4btc commented at 3:59 AM on December 3, 2026: member

    tACK 3f3df246050dc0b6005461f7f2816f9f1c6f7d82

    Tested manually on regtest, master vs this PR's bitcoin-cli:

    • -generate 1.5, -generate 1 1.5, -generate 1e3, -generate 99999999999: master prints "blocks": null and exits 0; with this PR it reports error code: -1 / JSON integer out of range and exits 1.
    • -generate '"1"': now reports the -3 type error (exit 3) instead of "blocks": null. (Nice improvement! Please check follow-up ideas section below)
    • -generate 1 0 still returns "blocks": [] with exit 0, matching the RPC.
    • Normal -generate / -generate N output unchanged.

    The fix follows the same pattern as AddrinfoRequestHandler (and the per-call checks in GetinfoRequestHandler/NetinfoRequestHandler): pass the RPC error reply through ProcessReply (returning early) so CommandLineRPC reports it and exits nonzero. Nice to have the characterization test first, it makes the behaviour change explicit in the second commit.

    Follow-up ideas (not introduced here):

    • A failing -generate still burns a wallet address, since getnewaddress runs first. For the CLI-side checks (nblocks == "0", too many args) this could be avoided by running them before getnewaddress, e.g. splitting them out of SetGenerateToAddressArgs.
    • SetGenerateToAddressArgs rejects only the literal string "0", so e.g. -generate ' 0' still returns "blocks": [] with exit 0, since generatetoaddress itself accepts nblocks=0. Now that RPC errors are forwarded, rejecting nblocks < 1 in the RPC would cover every spelling, and the CLI string check could then be dropped.

    Left a couple of non-blocking nits.


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: 2026-12-04 12:51 UTC

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