From 0eca5ebaced264d041de815316eaca8cf6fef92f Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 6 Aug 2021 22:33:48 +0200 Subject: contrib: refactor: introduce bitcoin-cli RPC call helper in getcoins.py --- contrib/signet/getcoins.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'contrib') diff --git a/contrib/signet/getcoins.py b/contrib/signet/getcoins.py index c50c2cc16a..d0a12beb29 100755 --- a/contrib/signet/getcoins.py +++ b/contrib/signet/getcoins.py @@ -23,24 +23,26 @@ args = parser.parse_args() if args.bitcoin_cli_args == []: args.bitcoin_cli_args = ['-signet'] -if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: - # Get the hash of the block at height 1 of the currently active signet chain + +def bitcoin_cli(rpc_command_and_params): + argv = [args.cmd] + args.bitcoin_cli_args + rpc_command_and_params try: - curr_signet_hash = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getblockhash', '1']).strip().decode() + return subprocess.check_output(argv).strip().decode() except FileNotFoundError: print('The binary', args.cmd, 'could not be found.') exit() + + +if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: + # Get the hash of the block at height 1 of the currently active signet chain + curr_signet_hash = bitcoin_cli(['getblockhash', '1']) if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH: 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') exit() if args.addr == '': # get address for receiving coins - try: - args.addr = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getnewaddress', 'faucet', 'bech32']).strip() - except FileNotFoundError: - print('The binary', args.cmd, 'could not be found.') - exit() + args.addr = bitcoin_cli(['getnewaddress', 'faucet', 'bech32']) data = {'address': args.addr, 'password': args.password} try: -- cgit v1.2.3 From 8c203cf0e1b9558b54030c284ddf7706d64cdde2 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Sat, 7 Aug 2021 13:47:57 +0200 Subject: contrib: catch bitcoin-cli RPC call errors in getcoins.py --- contrib/signet/getcoins.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'contrib') diff --git a/contrib/signet/getcoins.py b/contrib/signet/getcoins.py index d0a12beb29..86755e6305 100755 --- a/contrib/signet/getcoins.py +++ b/contrib/signet/getcoins.py @@ -31,6 +31,10 @@ def bitcoin_cli(rpc_command_and_params): except FileNotFoundError: print('The binary', args.cmd, 'could not be found.') exit() + except subprocess.CalledProcessError: + cmdline = ' '.join(argv) + print(f'-----\nError while calling "{cmdline}" (see output above).') + exit() if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: -- cgit v1.2.3 From 42dbd9025adc200012c103d2c091cf026f1d50b1 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Sat, 7 Aug 2021 20:48:15 +0200 Subject: contrib: return non-zero status if getcoins.py errors --- contrib/signet/getcoins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'contrib') diff --git a/contrib/signet/getcoins.py b/contrib/signet/getcoins.py index 86755e6305..dc203f1254 100755 --- a/contrib/signet/getcoins.py +++ b/contrib/signet/getcoins.py @@ -30,11 +30,11 @@ def bitcoin_cli(rpc_command_and_params): return subprocess.check_output(argv).strip().decode() except FileNotFoundError: print('The binary', args.cmd, 'could not be found.') - exit() + exit(1) except subprocess.CalledProcessError: cmdline = ' '.join(argv) print(f'-----\nError while calling "{cmdline}" (see output above).') - exit() + exit(1) if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: @@ -42,7 +42,7 @@ if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: curr_signet_hash = bitcoin_cli(['getblockhash', '1']) if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH: 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') - exit() + exit(1) if args.addr == '': # get address for receiving coins @@ -53,7 +53,7 @@ try: res = requests.post(args.faucet, data=data) except: print('Unexpected error when contacting faucet:', sys.exc_info()[0]) - exit() + exit(1) # Display the output as per the returned status code if res: -- cgit v1.2.3