Skip to content

Commit 1bbe289

Browse files
committed
Merge bitcoin/bitcoin#22565: [script] signet's getcoins.py improvements
b0c8246 Add cleaner errors for unsuccessful faucet transactions (NikhilBartwal) 1c612b2 [script] Update signet getcoins.py for custom network (NikhilBartwal) Pull request description: Currently, using the getcoins.py with a custom signet executes successfully and shows the transfer of 0.001 testBTC as complete, however for obvious reasons, it should not. In fact, upon verification it does not actually execute the transaction, but rather gives the output that it did, as shown below which can be misleading: ``` [nikhilb@nikhil-PC bitcoin]$ echo $datadir /home/nikhilb/signet-custom [nikhilb@nikhil-PC bitcoin]$ contrib/signet/getcoins.py -- -datadir=$datadir Payment of 0.00100000 BTC sent with txid dd22c7d996e95f3e5baf20f73140d517ff48f1b26d0e4fefd61e3c37991b8f86 [nikhilb@nikhil-PC bitcoin]$ bitcoin-cli -datadir=$datadir getrawtransaction dd22c7d996e95f3e5baf20f73140d517ff48f1b26d0e4fefd61e3c37991b8f86 error code: -5 error message: No such mempool or blockchain transaction. Use gettransaction for wallet transactions. [nikhilb@nikhil-PC bitcoin]$ bitcoin-cli -datadir=$datadir gettransaction dd22c7d996e95f3e5baf20f73140d517ff48f1b26d0e4fefd61e3c37991b8f86 error code: -5 error message: Invalid or non-wallet transaction id ``` This PR adds a sanity check for custom signet by comparing the current network's first block hash (the block after the genesis block) with global signet's respective block hash (since all signet networks share the same genesis block) and if a custom network is detected, the user is prompted to either work on the global signet or setup their own faucet. The PR was checked to be working successfully, giving the output as below: ``` [nikhilb@nikhil-PC bitcoin]$ git checkout update_signet_getcoins Switched to branch 'update_signet_getcoins' Your branch is ahead of 'upstream/master' by 1 commit. (use "git push" to publish your local commits) [nikhilb@nikhil-PC bitcoin]$ contrib/signet/getcoins.py -- -datadir=$datadir The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet for the same. You can have a look here for setting up your own faucet: https://en.bitcoin.it/wiki/Signet ``` ACKs for top commit: prayank23: utACK bitcoin/bitcoin@b0c8246 kallewoof: ACK b0c8246 arnabsen1729: utACK b0c8246 prakash1512: utACK b0c8246 0xB10C: Tested ACK b0c8246 theStack: Tested ACK b0c8246 Zero-1729: crACK b0c8246 🧉 Tree-SHA512: 144b47a83008521a5cda13f4c1b12809a125a744f865a8e0f792132d52fdb88926d4f4f4d7230452c2e129b5879892cdbeda981b8af10b789e9fc0cda2905a5d
2 parents dbcb574 + b0c8246 commit 1bbe289

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

contrib/signet/getcoins.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,36 @@
55

66
import argparse
77
import subprocess
8-
import requests
98
import sys
9+
import requests
10+
11+
DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim'
12+
GLOBAL_FIRST_BLOCK_HASH = '00000086d6b2636cb2a392d45edc4ec544a10024d30141c9adf4bfd9de533b53'
1013

1114
parser = argparse.ArgumentParser(description='Script to get coins from a faucet.', epilog='You may need to start with double-dash (--) when providing bitcoin-cli arguments.')
1215
parser.add_argument('-c', '--cmd', dest='cmd', default='bitcoin-cli', help='bitcoin-cli command to use')
13-
parser.add_argument('-f', '--faucet', dest='faucet', default='https://signetfaucet.com/claim', help='URL of the faucet')
16+
parser.add_argument('-f', '--faucet', dest='faucet', default=DEFAULT_GLOBAL_FAUCET, help='URL of the faucet')
1417
parser.add_argument('-a', '--addr', dest='addr', default='', help='Bitcoin address to which the faucet should send')
1518
parser.add_argument('-p', '--password', dest='password', default='', help='Faucet password, if any')
1619
parser.add_argument('bitcoin_cli_args', nargs='*', help='Arguments to pass on to bitcoin-cli (default: -signet)')
1720

1821
args = parser.parse_args()
1922

23+
if args.bitcoin_cli_args == []:
24+
args.bitcoin_cli_args = ['-signet']
25+
26+
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
27+
# Get the hash of the block at height 1 of the currently active signet chain
28+
try:
29+
curr_signet_hash = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getblockhash', '1']).strip().decode()
30+
except FileNotFoundError:
31+
print('The binary', args.cmd, 'could not be found.')
32+
exit()
33+
if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
34+
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()
36+
2037
if args.addr == '':
21-
if args.bitcoin_cli_args == []:
22-
args.bitcoin_cli_args = ['-signet']
2338
# get address for receiving coins
2439
try:
2540
args.addr = subprocess.check_output([args.cmd] + args.bitcoin_cli_args + ['getnewaddress', 'faucet', 'bech32']).strip()
@@ -33,4 +48,15 @@
3348
except:
3449
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
3550
exit()
36-
print(res.text)
51+
52+
# Display the output as per the returned status code
53+
if res:
54+
# When the return code is in between 200 and 400 i.e. successful
55+
print(res.text)
56+
elif res.status_code == 404:
57+
print('The specified faucet URL does not exist. Please check for any server issues/typo.')
58+
elif res.status_code == 429:
59+
print('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')
60+
else:
61+
print(f'Returned Error Code {res.status_code}\n{res.text}\n')
62+
print('Please check the provided arguments for their validity and/or any possible typo.')

0 commit comments

Comments
 (0)