Skip to content

Commit 6872da4

Browse files
committed
signet-miner-scripts
1 parent db1c408 commit 6872da4

File tree

250 files changed

+2667
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+2667
-0
lines changed

bitcoin_signet_setup/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "bitcoin_signet_setup"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
bitcoincore-rpc = "0.18.0"
8+
serde = { version = "1.0", features = ["derive"] }
9+
serde_json = "1.0"
10+
tokio = { version = "1", features = ["full"] }
11+
anyhow = "1.0"
12+
regex = "1"
13+
chrono = "0.4"
14+
15+
[profile.dev]
16+
# Enable optimizations for faster builds during development
17+
opt-level = 1
18+
19+
[profile.release]
20+
# Enable full optimizations for release builds
21+
opt-level = 3

bitcoin_signet_setup/src/main.rs

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

mine.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
ln -sf /Users/git/Library/Application\ Support /Users/git/Library/Application_Support
2+
#ls /Users/git/Library/Application_Support/Bitcoin/
3+
ls /Users/git/Library/Application_Support/Signet/
4+
#ls /Users/git/Library/Application_Support/Bitcoin/signet/
5+
#cat /Users/git/Library/Application_Support/Bitcoin/bitcoin.conf
6+
cat /Users/git/Library/Application_Support/Signet/bitcoin.conf
7+
mv /Users/git/Library/Application_Support/Signet/bitcoin.conf /Users/git/Library/Application_Support/Signet/bitcoin-$(date +%s).conf
8+
mv /Users/git/Library/Application_Support/Signet/signet/peers.dat /Users/git/Library/Application_Support/Signet/signet/peers-$(date +%s).dat
9+
mv /Users/git/Library/Application_Support/Signet/regtest /Users/git/Library/Application_Support/Signet/regtest-$(date +%s)
10+
# Bitcoin client
11+
SIGNET_DATADIR="/Users/git/Library/Application_Support/Signet/"
12+
export SIGNET_DATADIR
13+
mkdir -p $SIGNET_DATADIR
14+
btcd="./bin/bitcoind -datadir=$SIGNET_DATADIR"
15+
16+
ls /Users/git/Library/Application_Support/Signet
17+
18+
# Bitcoin CLI
19+
bcli="./bin/bitcoin-cli -datadir=$SIGNET_DATADIR"
20+
21+
# Mining script
22+
miner="../contrib/signet/miner"
23+
24+
# Bitcoin-util, a tool that computes proof of work
25+
grinder="./bin/bitcoin-util grind"
26+
27+
# datadir cleanup, in case we need to start the network from scratch
28+
miner_datadir_cleanup="rm $SIGNET_DATADIR; mkdir $SIGNET_DATADIR"
29+
30+
$btcd -regtest -daemon && echo "sleep 10" && sleep 10
31+
$bcli -regtest createwallet "signer"
32+
DESCRIPTORS=$($bcli -regtest listdescriptors true | jq -r .descriptors)
33+
echo $DESCRIPTORS
34+
ADDR=$($bcli -regtest -named getnewaddress address_type="bech32")
35+
echo $ADDR
36+
SIGNET_CHALLENGE=$($bcli -regtest -named getaddressinfo $ADDR | jq -r .scriptPubKey)
37+
echo $SIGNET_CHALLENGE
38+
$bcli -regtest stop
39+
40+
#exit;
41+
42+
echo "
43+
rpcuser=signetuser
44+
rpcpassword=signetpassword
45+
signet=1
46+
[signet]
47+
rpcport=38332
48+
daemon=1
49+
signetchallenge=$SIGNET_CHALLENGE" > $SIGNET_DATADIR/bitcoin.conf
50+
51+
cat /Users/git/Library/Application_Support/Signet/bitcoin.conf
52+
53+
#exit;
54+
55+
(\
56+
$btcd -signet; sleep 5;
57+
#./bin/bitcoin-qt --datadir="/Users/git/Library/Application_Support/Signet/"; sleep 10;
58+
)
59+
60+
$bcli createwallet "miner"
61+
$bcli importdescriptors "$DESCRIPTORS"
62+
63+
echo "Checking for remaining bitcoind processes..."
64+
pids=$(lsof -t -i :38332)
65+
if [ -n "$pids" ]; then
66+
echo "Found processes using port 38332. Killing them now: $pids"
67+
kill -9 $pids
68+
else
69+
echo "No bitcoind process found on port 38332."
70+
fi
71+
72+
(\
73+
$btcd; sleep 5;
74+
##./bin/bitcoin-qt --datadir="/Users/git/Library/Application_Support/Signet/"; sleep 1;
75+
)
76+
$bcli loadwallet "miner"
77+
78+
#$bcli -signet stop
79+
#exit;
80+
81+
#$bcli --signet --datadir=/Users/git/Library/Application_Support/Signet/ loadwallet "miner"
82+
83+
#./bin/bitcoin-qt --datadir=/Users/git/Library/Application_Support/Signet --signet
84+
85+
MINER_ADDR=$($bcli -rpcwallet=miner -named getnewaddress address_type="bech32")
86+
$miner --cli "$bcli" generate --address $MINER_ADDR --grind-cmd "grinder" --min-nbits --set-block-time $(date +%s) && sleep 10
87+
$miner --cli "$bcli" generate --address $MINER_ADDR --grind-cmd "grinder" --min-nbits --ongoing
88+

miner2.sh

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

miner3.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
mkdir -p "$SIGNET_DATADIR-$TIMESTAMP"
28+
rsync -r "$SIGNET_DATADIR/bitcoin.conf" "$SIGNET_DATADIR-$TIMESTAMP/" 2>/dev/null
29+
rsync -r "$SIGNET_DATADIR/*" "$SIGNET_DATADIR-$TIMESTAMP/" 2>/dev/null
30+
rsync -r "$SIGNET_DATADIR/signet" "$SIGNET_DATADIR-$TIMESTAMP/signet" 2>/dev/null
31+
cat "$SIGNET_DATADIR-$TIMESTAMP/bitcoin.conf"
32+
33+
# --- 4. Phase 2: Signet Execution and Mining Setup ---
34+
35+
echo "--- Phase 2: Starting Custom Signet and Miner Setup ---"
36+
37+
# Start bitcoind in Signet mode as a daemon
38+
echo "Starting bitcoind in Signet mode as a daemon..."
39+
./bin/bitcoin-qt -port=8080 -signet -daemon -datadir=$SIGNET_DATADIR-$TIMESTAMP || { echo "Error: Failed to start bitcoind in Signet mode."; exit 1; }
40+
sleep 5

0 commit comments

Comments
 (0)