contrib: catch bitcoin-cli RPC call errors in getcoins.py #22660

pull theStack wants to merge 3 commits into bitcoin:master from theStack:202107-script-signet_getcoins_catch_rpc_errors changing 1 files +17 −11
  1. theStack commented at 7:11 PM on August 7, 2021: member

    This PR is based on #22565 ("[script] signet's getcoins.py improvements"), which should be reviewed first.

    The signet faucet script contrib/signet/getcoins.py currently issues bitcoin-cli RPC calls without catching errors -- the only case tackled is if there is no bitcoin-cli file found. Instead of crashing with a stack-trace on a failed RPC call, the changes in this PR aim to produce a more user-friendly output (see also #22565 (review)). Additionally, in case of any error, a non-zero status is now returned (instead of 0, indicating success), which could be useful for other scripts taking use of signet faucet script.

    The most straight-forward way to test this is invoking the script without a bitcoind running on signet:

    PR22565 branch:

    $ ./contrib/signet/getcoins.py
    error: Could not connect to the server 127.0.0.1:8332
    
    Make sure the bitcoind server is running and that you are connecting to the correct RPC port.
    Traceback (most recent call last):
      File "./contrib/signet/getcoins.py", line 26, in <module>
        curr_signet_hash = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getblockhash', '1']).strip().decode()
      File "/usr/local/lib/python3.8/subprocess.py", line 415, in check_output
        return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
      File "/usr/local/lib/python3.8/subprocess.py", line 516, in run
        raise CalledProcessError(retcode, process.args,
    subprocess.CalledProcessError: Command '['bitcoin-cli', 'getblockhash', '1']' returned non-zero exit status 1.
    

    this PR branch:

    $ ./contrib/signet/getcoins.py
    error: Could not connect to the server 127.0.0.1:38332
    
    Make sure the bitcoind server is running and that you are connecting to the correct RPC port.
    -----
    Error while calling "bitcoin-cli -signet getblockhash 1" (see output above).
    
  2. contrib: refactor: introduce bitcoin-cli RPC call helper in getcoins.py 0eca5ebace
  3. contrib: catch bitcoin-cli RPC call errors in getcoins.py 8c203cf0e1
  4. contrib: return non-zero status if getcoins.py errors 42dbd9025a
  5. 0xB10C commented at 8:23 PM on August 7, 2021: member

    Concept ACK, thanks for further improving this :)

  6. NikhilBartwal approved
  7. NikhilBartwal commented at 8:43 PM on August 7, 2021: contributor

    Concept ACK. Thanks, better error handling and less repetitive code now! :)

  8. DrahtBot added the label Scripts and tools on Aug 7, 2021
  9. fanquake deleted a comment on Aug 8, 2021
  10. kallewoof commented at 7:12 AM on August 17, 2021: member

    Code ACK 42dbd9025adc200012c103d2c091cf026f1d50b1

  11. in contrib/signet/getcoins.py:55 in 42dbd9025a
      58 |  
      59 |  data = {'address': args.addr, 'password': args.password}
      60 |  try:
      61 |      res = requests.post(args.faucet, data=data)
      62 |  except:
      63 |      print('Unexpected error when contacting faucet:', sys.exc_info()[0])
    


    Zero-1729 commented at 10:21 AM on August 21, 2021:

    Currently, the message printed here might be cryptic to a non-Python savvy user. For example, when attempting to connect to a URL with no schema (e.g. test.com) you get the following:

    $ ./contrib/signet/getcoins.py --faucet="test.com"
    Unexpected error when contacting faucet: <class 'requests.exceptions.MissingSchema'>
    

    While we're at it, I'd suggest the following for a more digestible error message:

        print('Unexpected error when contacting faucet:', sys.exc_info()[1])
    

    which results in the following message:

    $ ./contrib/signet/getcoins.py --faucet="test.com"
    Unexpected error when contacting faucet: Invalid URL 'test.com': No schema supplied. Perhaps you meant http://test.com?
    

    Note: this would also affect other similar error messages such as a connectionError (tested with --faucet="http://fhjvjhj.com/").

  12. Zero-1729 commented at 10:25 AM on August 21, 2021: contributor

    tACK 42dbd90 🧪

    Tested on macOS v11.5.2

    Nice addition! Tested the errors for the case when ran without bitcoind running, signet wallet not loaded, rate limit reached, and faucet does not exist (output below).

    Note: Left a non-blocking minor nit as well.

    Bitcoind not running

    $ contrib/signet/getcoins.py 
    error: timeout on transient error: Could not connect to the server 127.0.0.1:38332
    
    Make sure the bitcoind server is running and that you are connecting to the correct RPC port.
    -----
    Error while calling "bitcoin-cli -signet getblockhash 1" (see output above).
    

    With no -signet wallet loaded

    $ contrib/signet/getcoins.py
    error code: -18
    error message:
    No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)
    -----
    Error while calling "bitcoin-cli -signet getnewaddress faucet bech32" (see output above).
    

    Attempting to exceed 1 day per IP payout

    $ ./contrib/signet/getcoins.py
    The script does not allow for repeated transactions as the global faucet is rate-limitied to 1 request/IP/day. You can access the faucet website to get more coins manually
    
    

    Faucet does not exist (i.e. E404)

    $ ./contrib/signet/getcoins.py --faucet="https://bitcoincore.org/nothing"
    The specified faucet URL does not exist. Please check for any server issues/typo.
    
  13. theStack force-pushed on Aug 26, 2021
  14. MarcoFalke merged this on Aug 26, 2021
  15. MarcoFalke closed this on Aug 26, 2021

  16. sidhujag referenced this in commit a3c57b8052 on Aug 28, 2021
  17. DrahtBot locked this on Aug 26, 2022

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-04-13 21:14 UTC

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