Skip to content

Commit 718d9f2

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22660: contrib: catch bitcoin-cli RPC call errors in getcoins.py
42dbd90 contrib: return non-zero status if getcoins.py errors (Sebastian Falbesoner) 8c203cf contrib: catch bitcoin-cli RPC call errors in getcoins.py (Sebastian Falbesoner) 0eca5eb contrib: refactor: introduce bitcoin-cli RPC call helper in getcoins.py (Sebastian Falbesoner) Pull request description: 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 bitcoin/bitcoin#22565 (comment)). 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). ``` ACKs for top commit: kallewoof: Code ACK 42dbd90 Zero-1729: tACK 42dbd90 🧪 Tree-SHA512: 912240a4ed03c87035e370602f4095c7ffe26806421bbbd6cf86588126f2310a01a6a61606e9e2918fb2c1a0debdd0ce768c69ba2e4b8e7750fa3474a56d01a0
2 parents cea38b4 + 42dbd90 commit 718d9f2

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

contrib/signet/getcoins.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,37 @@
2323
if args.bitcoin_cli_args == []:
2424
args.bitcoin_cli_args = ['-signet']
2525

26-
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
27-
# Get the hash of the block at height 1 of the currently active signet chain
26+
27+
def bitcoin_cli(rpc_command_and_params):
28+
argv = [args.cmd] + args.bitcoin_cli_args + rpc_command_and_params
2829
try:
29-
curr_signet_hash = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getblockhash', '1']).strip().decode()
30+
return subprocess.check_output(argv).strip().decode()
3031
except FileNotFoundError:
3132
print('The binary', args.cmd, 'could not be found.')
32-
exit()
33+
exit(1)
34+
except subprocess.CalledProcessError:
35+
cmdline = ' '.join(argv)
36+
print(f'-----\nError while calling "{cmdline}" (see output above).')
37+
exit(1)
38+
39+
40+
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
41+
# Get the hash of the block at height 1 of the currently active signet chain
42+
curr_signet_hash = bitcoin_cli(['getblockhash', '1'])
3343
if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
3444
print('The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet to use this functionality.\n')
35-
exit()
45+
exit(1)
3646

3747
if args.addr == '':
3848
# get address for receiving coins
39-
try:
40-
args.addr = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getnewaddress', 'faucet', 'bech32']).strip()
41-
except FileNotFoundError:
42-
print('The binary', args.cmd, 'could not be found.')
43-
exit()
49+
args.addr = bitcoin_cli(['getnewaddress', 'faucet', 'bech32'])
4450

4551
data = {'address': args.addr, 'password': args.password}
4652
try:
4753
res = requests.post(args.faucet, data=data)
4854
except:
4955
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
50-
exit()
56+
exit(1)
5157

5258
# Display the output as per the returned status code
5359
if res:

0 commit comments

Comments
 (0)