|
| 1 | +#!/bin/bash |
| 2 | +set -x |
| 3 | +# |
| 4 | +# Script to set up a custom Signet network, generate a challenge, |
| 5 | +# start the daemon, create a wallet, and start a local miner. |
| 6 | +# |
| 7 | + |
| 8 | +# --- 1. Setup and Cleanup --- |
| 9 | + |
| 10 | +# Define the data directory path |
| 11 | +SIGNET_DATADIR="./signet_data" |
| 12 | +export SIGNET_DATADIR |
| 13 | + |
| 14 | +# Ensure the data directory exists |
| 15 | +mkdir -p "$SIGNET_DATADIR" |
| 16 | + |
| 17 | +# Define executable paths relative to the current working directory |
| 18 | +btcd="./bin/bitcoind -datadir=$SIGNET_DATADIR" |
| 19 | +bcli="./bin/bitcoin-cli -datadir=$SIGNET_DATADIR" |
| 20 | +miner="../contrib/signet/miner" |
| 21 | +grinder="./bin/bitcoin-util grind" |
| 22 | + |
| 23 | +# Cleanup old configuration/data files for a fresh start (Crucial for debugging) |
| 24 | +echo "Archiving old configuration and data files..." |
| 25 | +TIMESTAMP=$(date +%s) |
| 26 | +# Note: Renaming files ensures you don't lose previous configurations. |
| 27 | +mv "$SIGNET_DATADIR/bitcoin.conf" "$SIGNET_DATADIR/bitcoin-$TIMESTAMP.conf" 2>/dev/null |
| 28 | +mv "$SIGNET_DATADIR/signet/peers.dat" "$SIGNET_DATADIR/signet/peers-$TIMESTAMP.dat" 2>/dev/null |
| 29 | +mv "$SIGNET_DATADIR/regtest" "$SIGNET_DATADIR/regtest-$TIMESTAMP" 2>/dev/null |
| 30 | + |
| 31 | +# Explicitly remove wallet directories to ensure a clean start |
| 32 | +echo "Removing existing wallet directories if they exist..." |
| 33 | +rm -rf "$SIGNET_DATADIR/regtest/wallets/signer" 2>/dev/null |
| 34 | +rm -rf "$SIGNET_DATADIR/signet/wallets/miner" 2>/dev/null |
| 35 | + |
| 36 | + |
| 37 | +# --- 2. Phase 1: Regtest Setup (Generate Signet Challenge) --- |
| 38 | + |
| 39 | +echo "--- Phase 1: Generating Signet Challenge using Regtest ---" |
| 40 | + |
| 41 | +# 1. Stop any potentially running bitcoind processes (using the default regtest port 18443) |
| 42 | +echo "Checking for and stopping any residual bitcoind processes..." |
| 43 | +pids=$(lsof -t -i :18443) |
| 44 | +if [ -n "$pids" ]; then |
| 45 | + echo "Found processes on port 18443. Killing them now: $pids" |
| 46 | + kill -9 $pids |
| 47 | +fi |
| 48 | +pids=$(lsof -t -i :18444) |
| 49 | +if [ -n "$pids" ]; then |
| 50 | + echo "Found processes on port 18443. Killing them now: $pids" |
| 51 | + kill -9 $pids |
| 52 | +fi |
| 53 | +pids=$(lsof -t -i :18332) |
| 54 | +if [ -n "$pids" ]; then |
| 55 | + echo "Found processes on port 18443. Killing them now: $pids" |
| 56 | + kill -9 $pids |
| 57 | +fi |
| 58 | +pids=$(lsof -t -i :38333) |
| 59 | +if [ -n "$pids" ]; then |
| 60 | + echo "Found processes on port 18443. Killing them now: $pids" |
| 61 | + kill -9 $pids |
| 62 | +fi |
| 63 | +pids=$(lsof -t -i :38334) |
| 64 | +if [ -n "$pids" ]; then |
| 65 | + echo "Found processes on port 18443. Killing them now: $pids" |
| 66 | + kill -9 $pids |
| 67 | +fi |
| 68 | + |
| 69 | + |
| 70 | +# 2. Check for the lock file in your Regtest directory (which is temporarily the main data directory) |
| 71 | +LOCK_FILE="$SIGNET_DATADIR/.lock" |
| 72 | +if [ -f "$LOCK_FILE" ]; then |
| 73 | + echo "Lock file found: $LOCK_FILE. Removing it now." |
| 74 | + rm "$LOCK_FILE" |
| 75 | +else |
| 76 | + echo "No lock file found." |
| 77 | +fi |
| 78 | + |
| 79 | +# Start bitcoind in Regtest mode as a daemon |
| 80 | +echo "Starting bitcoind in regtest mode..." |
| 81 | +$btcd -regtest -daemon || { echo "Error: Failed to start bitcoind in regtest mode."; exit 1; } |
| 82 | +sleep 5 |
| 83 | + |
| 84 | +# Wait until the daemon is ready for RPC calls |
| 85 | +MAX_RETRIES=10 |
| 86 | +RETRY_COUNT=0 |
| 87 | +while ! $bcli -regtest getblockchaininfo 2>/dev/null; do |
| 88 | + if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then |
| 89 | + echo "Error: bitcoind failed to start after $MAX_RETRIES attempts." |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + echo "Waiting for regtest daemon... (Attempt $RETRY_COUNT/$MAX_RETRIES)" |
| 93 | + sleep 2 |
| 94 | + RETRY_COUNT=$((RETRY_COUNT + 1)) |
| 95 | +done |
| 96 | + |
| 97 | +# Create a temporary 'signer' wallet |
| 98 | +echo "Creating temporary 'signer' wallet..." |
| 99 | +$bcli -regtest createwallet "signer" |
| 100 | + |
| 101 | +# Extract descriptors for later import |
| 102 | +echo "Extracting descriptors..." |
| 103 | +DESCRIPTORS=$($bcli -regtest listdescriptors true | jq -r .descriptors) |
| 104 | +echo "Descriptors: $DESCRIPTORS" |
| 105 | + |
| 106 | +# Generate a new address and get its scriptPubKey for the challenge |
| 107 | +ADDR=$($bcli -regtest -named getnewaddress address_type="bech32") |
| 108 | +echo "Temporary Regtest Address: $ADDR" |
| 109 | + |
| 110 | +SIGNET_CHALLENGE=$($bcli -regtest -named getaddressinfo "$ADDR" | jq -r .scriptPubKey) |
| 111 | +echo "Generated SIGNET_CHALLENGE (scriptPubKey): $SIGNET_CHALLENGE" |
| 112 | + |
| 113 | +# Stop the regtest daemon gracefully |
| 114 | +echo "Stopping regtest daemon..." |
| 115 | +$bcli -regtest stop |
| 116 | +sleep 5 # Wait for shutdown |
| 117 | + |
| 118 | +# --- 3. Write Custom Signet Configuration --- |
| 119 | + |
| 120 | +echo "Writing new bitcoin.conf for custom Signet..." |
| 121 | +cat <<EOF > "$SIGNET_DATADIR/bitcoin.conf" |
| 122 | +rpcuser=signetuser |
| 123 | +rpcpassword=signetpassword |
| 124 | +signet=1 |
| 125 | +[signet] |
| 126 | +rpcport=38332 |
| 127 | +add-node=8080 |
| 128 | +daemon=1 |
| 129 | +signetchallenge=$SIGNET_CHALLENGE |
| 130 | +EOF |
| 131 | + |
| 132 | +cat "$SIGNET_DATADIR/bitcoin.conf" |
| 133 | + |
| 134 | +# --- 4. Phase 2: Signet Execution and Mining Setup --- |
| 135 | + |
| 136 | +echo "--- Phase 2: Starting Custom Signet and Miner Setup ---" |
| 137 | + |
| 138 | +# Start bitcoind in Signet mode as a daemon |
| 139 | +echo "Starting bitcoind in Signet mode as a daemon..." |
| 140 | +$btcd -signet -daemon || { echo "Error: Failed to start bitcoind in Signet mode."; exit 1; } |
| 141 | +sleep 5 |
| 142 | + |
| 143 | +# Robust check for Signet daemon readiness |
| 144 | +MAX_RETRIES=10 |
| 145 | +RETRY_COUNT=0 |
| 146 | +while ! $bcli -signet getblockchaininfo 2>/dev/null; do |
| 147 | + if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then |
| 148 | + echo "Error: Signet bitcoind failed to start after $MAX_RETRIES attempts." |
| 149 | + exit 1 |
| 150 | + fi |
| 151 | + echo "Waiting for Signet daemon... (Attempt $RETRY_COUNT/$MAX_RETRIES)" |
| 152 | + sleep 2 |
| 153 | + RETRY_COUNT=$((RETRY_COUNT + 1)) |
| 154 | +done |
| 155 | + |
| 156 | +# Create and load the 'miner' wallet |
| 157 | +echo "Creating and loading 'miner' wallet..." |
| 158 | +# Use -signet and the RPC is already configured via the config file |
| 159 | +$bcli -signet createwallet "miner" |
| 160 | + |
| 161 | +# Import the descriptors (keys) from the regtest wallet into the new miner wallet |
| 162 | +echo "Importing descriptors into 'miner' wallet..." |
| 163 | +$bcli -signet -rpcwallet=miner importdescriptors "$DESCRIPTORS" |
| 164 | + |
| 165 | +# Generate a mining address from the 'miner' wallet |
| 166 | +MINER_ADDR=$($bcli -signet -rpcwallet=miner -named getnewaddress address_type="bech32") |
| 167 | +echo "Miner Block Reward Address: $MINER_ADDR" |
| 168 | + |
| 169 | +# --- 5. Start Mining --- |
| 170 | + |
| 171 | +echo "Generating initial block with custom nBits..." |
| 172 | +$miner --cli "$bcli" generate --address "$MINER_ADDR" --grind-cmd "$grinder" --min-nbits --set-block-time $(date +%s) && sleep 10 |
| 173 | + |
| 174 | +# Start ongoing mining loop |
| 175 | +echo "Starting ongoing mining loop..." |
| 176 | +$miner --cli "$bcli" generate --address "$MINER_ADDR" --grind-cmd "$grinder" --min-nbits --ongoing |
0 commit comments