Changeset View
Changeset View
Standalone View
Standalone View
contrib/seeds/test-seeds.sh
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/bin/bash | |||||
set -u | |||||
TOPLEVEL=`git rev-parse --show-toplevel` | |||||
DEFAULT_BUILD_DIR="${TOPLEVEL}/build" | |||||
help_message() { | |||||
echo "Test connecting to seed nodes. Outputs the seeds that were successfully connected to." | |||||
echo "" | |||||
echo "Example usages:" | |||||
echo "Mainnet: $0 < nodes_main.txt" | |||||
echo "Testnet: $0 --testnet < nodes_test.txt" | |||||
echo "" | |||||
echo "Options:" | |||||
echo "-t, --testnet Connect to testnet seeds (mainnet is default)." | |||||
echo "-h, --help Display this help message." | |||||
echo "" | |||||
echo "Environment Variables:" | |||||
echo "BUILD_DIR Default: ${DEFAULT_BUILD_DIR}" | |||||
exit 0 | |||||
} | |||||
: ${BUILD_DIR:=$DEFAULT_BUILD_DIR} | |||||
OPTION_TESTNET="" | |||||
# Parse command line arguments | |||||
while [[ $# -gt 0 ]]; do | |||||
case $1 in | |||||
-t|--testnet) | |||||
OPTION_TESTNET="--testnet" | |||||
shift # shift past argument | |||||
;; | |||||
-h|--help) | |||||
help_message | |||||
shift # shift past argument | |||||
;; | |||||
*) | |||||
echo "Unknown argument: $1" | |||||
help_message | |||||
shift # shift past argument | |||||
;; | |||||
esac | |||||
done | |||||
BITCOIND="${BUILD_DIR}/src/bitcoind" | |||||
BITCOIN_CLI="${BUILD_DIR}/src/bitcoin-cli" | |||||
if [ ! -x "$BITCOIND" ]; then | |||||
Fabien: Nit: `${BITCOIND}` with braces. | |||||
echo "${BITCOIND} does not exist or has incorrect permissions." | |||||
exit 10 | |||||
fi | |||||
if [ ! -x "$BITCOIN_CLI" ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
echo "${BITCOIN_CLI} does not exist or has incorrect permissions." | |||||
exit 11 | |||||
fi | |||||
BITCOIND="${BITCOIND} -connect=0 $OPTION_TESTNET" | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
BITCOIN_CLI="${BITCOIN_CLI} $OPTION_TESTNET" | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
>&2 echo "Spinning up bitcoind..." | |||||
${BITCOIND} & | |||||
BITCOIND_PID=$! | |||||
cleanup() { | |||||
# Cleanup background processes spawned by this script. | |||||
>&2 echo "Cleaning up bitcoin daemon (PID: ${BITCOIND_PID})." | |||||
kill ${BITCOIND_PID} | |||||
} | |||||
trap "cleanup" EXIT | |||||
# Short sleep to make sure the RPC server is available | |||||
sleep 0.1 | |||||
# Wait until bitcoind is fully spun up | |||||
WARMUP_TIMEOUT=60 | |||||
for TRIES in $(seq 1 $WARMUP_TIMEOUT); do | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
${BITCOIN_CLI} getconnectioncount &> /dev/null | |||||
RPC_READY=$? | |||||
if [ "$RPC_READY" -ne 0 ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
>&2 printf "." | |||||
sleep 1 | |||||
else | |||||
break | |||||
fi | |||||
done | |||||
>&2 echo "" | |||||
if [ "$RPC_READY" -ne 0 ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
>&2 echo "RPC was not ready after $WARMUP_TIMEOUT seconds." | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
exit 20 | |||||
fi | |||||
while read -r SEED; do | |||||
>&2 echo "Testing seed '$SEED'..." | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
# Immediately connect to the seed peer | |||||
${BITCOIN_CLI} addnode "$SEED" onetry | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
# Fetch peer's connection status | |||||
CONNECTION_COUNT=$(${BITCOIN_CLI} getconnectioncount) | |||||
if [ "$CONNECTION_COUNT" -eq 1 ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
# Cleanup peer connection | |||||
${BITCOIN_CLI} disconnectnode "$SEED" | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
# Wait for peer to be disconnected | |||||
CONNECTED_TIMEOUT=5 | |||||
for TRIES in $(seq 1 $CONNECTED_TIMEOUT); do | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
CONNECTION_COUNT=$(${BITCOIN_CLI} getconnectioncount) | |||||
if [ "$CONNECTION_COUNT" -eq 0 ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
break | |||||
fi | |||||
sleep 0.1 | |||||
done | |||||
if [ "$CONNECTION_COUNT" -ne 0 ]; then | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
>&2 echo "Seed failed to disconnect. Something else could be wrong (check logs)." | |||||
exit 30 | |||||
fi | |||||
echo "$SEED" | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
else | |||||
>&2 echo "Failed to connect to '$SEED'" | |||||
FabienUnsubmitted Not Done Inline ActionsDito. Fabien: Dito. | |||||
fi | |||||
done |
Nit: ${BITCOIND} with braces.