Skip to content

Commit 56f1c48

Browse files
committed
now looks old/new covered correctly. added CHAIN_TIP_AGE check to avoid mining until we are at least close to tip.
1 parent 21c8e2d commit 56f1c48

File tree

6 files changed

+117
-61
lines changed

6 files changed

+117
-61
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ ENV NBITS=${NBITS}
3030
ENV SIGNETCHALLENGE=${SIGNETCHALLENGE}
3131
ENV PRIVKEY=${PRIVKEY}
3232
ENV PRIVKEY_MODE=${PRIVKEY_MODE:-"descriptor"}
33-
33+
ENV CHAIN_TIP_AGE=${CHAIN_TIP_AGE:-"7200"}
34+
ENV MINE_GENESIS=${MINE_GENESIS:-"0"}
3435
ENV RPCUSER=${RPCUSER:-"bitcoin"}
3536
ENV RPCPASSWORD=${RPCPASSWORD:-"bitcoin"}
3637
ENV COOKIEFILE=${COOKIEFILE:-"false"}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Bitcoin Signet provides a sandboxed environment for Bitcoin, allowing developers and testers to simulate the Bitcoin network without the risks associated with the main or test networks. This Docker image facilitates the deployment and configuration of a Signet node, offering a range of customizable options through environment variables.
2323

2424
Versions prior to 29.0 were using BDB wallet, system will automaticly update your wallet to new descriptor format.
25-
`PRIVKEY` prior to 29.0 was a WIF, now is descriptor on new wallets. will only import new style keys.
25+
`PRIVKEY` prior to 29.0 was a WIF, now is descriptor on new wallets.
2626

2727
## 🛠 Environment Variables
2828

@@ -33,10 +33,12 @@ Versions prior to 29.0 were using BDB wallet, system will automaticly update you
3333
- `MINERENABLED`: Flag to enable the mining chain. (**mining mode only**)
3434
- `NBITS`: Sets the minimum difficulty in mining. (**mining mode only**)
3535
- `PRIVKEY`: Private key of the Signet signer. If `MINERENABLED=1` and not provided, it will be generated. (**mining mode only**)
36-
- `PRIVKEY_MODE`: `descriptor` - provide descriptors (wallets from >=v29.0) or `legacy` - provide WIF (method used prior to <v29.0)
36+
- `PRIVKEY_MODE`: `descriptor` - provide descriptors (wallets from >=v29.0) or `legacy` - provide WIF (method used prior to <v29.0 to init signet challenge)
3737
- `MINETO`: Address to mine to. If not provided, a new address will be generated for each block. (**mining mode only**)
3838
- `SIGNETCHALLENGE`: Sets the valid block producer for this Signet. Required for client-mode.
3939
- If `MINERENABLED=1` and not provided, it will be generated. If provided, `PRIVKEY` must also be populated.
40+
- `CHAIN_TIP_AGE`: 7200s (2hrs), will not start miner until chain-tip timestamp vs local is <= this value, allows things to sync up before trying to mine bad blacks.
41+
- `MINE_GENESIS`: set to 1 if you are starting a new chain, will mine first block so passes `CHAIN_TIP_AGE` check.
4042

4143
### 🌐 RPC Configuration:
4244

gen-signet-keys.sh

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,55 @@ DATADIR=${DATADIR:-"regtest-temp"}
22
BITCOINCLI=${BITCOINCLI:-"bitcoin-cli -regtest -datadir=$DATADIR "}
33
BITCOIND=${BITCOIND:-"bitcoind -datadir=$DATADIR -regtest -daemon"}
44

5-
write_files() {
6-
# echo "ADDR=" $ADDR
5+
write_files() {
76
echo "PRIVKEY="
8-
cat ~/.bitcoin/PRIVKEY.txt
9-
# echo "PUBKEY=" $PUBKEY
7+
cat $BITCOIN_DIR/PRIVKEY.txt
108
echo "SIGNETCHALLENGE=" $SIGNETCHALLENGE
11-
echo $ADDR > ~/.bitcoin/ADDR.txt
12-
# echo $PRIVKEY >~/.bitcoin/PRIVKEY.txt
13-
# echo $PUBKEY > ~/.bitcoin/PUBKEY.txt
14-
echo $SIGNETCHALLENGE >~/.bitcoin/SIGNETCHALLENGE.txt
9+
echo $ADDR > $BITCOIN_DIR/ADDR.txt
10+
echo $PUBKEY > $BITCOIN_DIR/PUBKEY.txt
11+
echo $SIGNETCHALLENGE >$BITCOIN_DIR/SIGNETCHALLENGE.txt
1512
}
1613

17-
if [[ "$MINERENABLED" == "1" && ("$SIGNETCHALLENGE" == "" || "$PRIVKEY" == "") ]]; then
18-
echo "Generating new signetchallange and privkey."
19-
#clean if exists
20-
rm -rf $DATADIR
21-
#make it fresh
22-
mkdir $DATADIR
23-
#kill any daemon running stuff
24-
pkill bitcoind
25-
#minimal config file (hardcode bitcoin:bitcoin for rpc)
26-
echo "
27-
regtest=1
28-
server=1
29-
rpcauth=bitcoin:c8c8b9740a470454255b7a38d4f38a52\$e8530d1c739a3bb0ec6e9513290def11651afbfd2b979f38c16ec2cf76cf348a
30-
rpcuser=bitcoin
31-
rpcpassword=bitcoin
32-
" >$DATADIR/bitcoin.conf
33-
#start daemon
34-
$BITCOIND -wallet="temp"
35-
#wait a bit for startup
36-
sleep 5s
37-
38-
#create wallet
39-
$BITCOINCLI -named createwallet wallet_name="temp" descriptors=true
40-
#Le
41-
##export future signet seeding key data
42-
#ADDR=$($BITCOINCLI getnewaddress "" legacy)
43-
#PRIVKEY=$($BITCOINCLI dumpprivkey $ADDR)
44-
#PUBKEY=$($BITCOINCLI getaddressinfo $ADDR | jq .pubkey | tr -d '""')
45-
46-
47-
48-
#Get the signet script from the 86 descriptor
49-
ADDR=$($BITCOINCLI getnewaddress)
50-
SIGNETCHALLENGE=$($BITCOINCLI getaddressinfo $ADDR | jq -r ".scriptPubKey")
51-
# Dumping descriptor wallet privatekey
52-
$BITCOINCLI listdescriptors true | jq -r ".descriptors | .[].desc" >> ~/.bitcoin/PRIVKEY.txt
53-
54-
#don't need regtest anymore
55-
$BITCOINCLI stop
56-
# SIGNETCHALLENGE=$(echo '5121'$PUBKEY'51ae')
57-
58-
#cleanup
59-
rm -rf $DATADIR
60-
else
61-
echo "$PRIVKEY" > ~/.bitcoin/PRIVKEY.txt
62-
echo "$SIGNETCHALLENGE" > ~/.bitcoin/SIGNETCHALLENGE.txt
63-
echo "Imported signetchallange and privkey being used."
64-
14+
if [[ "$MINERENABLED" == "1" ]]; then
15+
if [[ ("$SIGNETCHALLENGE" == "" || "$PRIVKEY" == "") ]]; then
16+
echo "Generating new signetchallange and privkey."
17+
#clean if exists
18+
rm -rf $DATADIR
19+
#make it fresh
20+
mkdir $DATADIR
21+
#kill any daemon running stuff
22+
pkill bitcoind
23+
#minimal config file (hardcode bitcoin:bitcoin for rpc)
24+
echo "
25+
regtest=1
26+
server=1
27+
rpcauth=bitcoin:c8c8b9740a470454255b7a38d4f38a52\$e8530d1c739a3bb0ec6e9513290def11651afbfd2b979f38c16ec2cf76cf348a
28+
rpcuser=bitcoin
29+
rpcpassword=bitcoin
30+
" >$DATADIR/bitcoin.conf
31+
#start daemon
32+
$BITCOIND -wallet="temp"
33+
#wait a bit for startup
34+
sleep 5s
35+
36+
#create wallet
37+
$BITCOINCLI -named createwallet wallet_name="temp" descriptors=true
38+
#Get the signet script from the 86 descriptor
39+
ADDR=$($BITCOINCLI getnewaddress)
40+
SIGNETCHALLENGE=$($BITCOINCLI getaddressinfo $ADDR | jq -r ".scriptPubKey")
41+
PUBKEY=$($BITCOINCLI getaddressinfo $ADDR | jq .pubkey | tr -d '""')
42+
# Dumping descriptor wallet privatekey
43+
$BITCOINCLI listdescriptors true | jq -r ".descriptors | .[].desc" >> $BITCOIN_DIR/PRIVKEY.txt
44+
45+
#don't need regtest anymore
46+
$BITCOINCLI stop
47+
#cleanup
48+
rm -rf $DATADIR
49+
else
50+
echo "$PRIVKEY" > $BITCOIN_DIR/PRIVKEY.txt
51+
echo "$SIGNETCHALLENGE" > $BITCOIN_DIR/SIGNETCHALLENGE.txt
52+
echo "Imported signetchallange and privkey being used."
53+
fi
6554
fi
6655

6756
write_files

mine-genesis.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
ADDR=${ADDR:-$(bitcoin-cli getnewaddress "gen0")}
2+
ADDR=${ADDR:-$(bitcoin-cli getnewaddress "gen0" bech32)}
33
NBITS=${NBITS:-"1e0377ae"} #minimum difficulty in signet
44
miner --cli="bitcoin-cli" generate --address=$ADDR --grind-cmd="bitcoin-util grind" --nbits=$NBITS --set-block-time=$(date +%s)
55

mine.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ GRIND="bitcoin-util grind"
66

77
NBITS=${NBITS:-"1e0377ae"} #minimum difficulty in signet
88

9+
echo "Waiting until Chain tip age is < $CHAIN_TIP_AGE seconds before mining start..."
10+
wait_chain_sync.sh $CHAIN_TIP_AGE
911
while true; do
1012
ADDR=${MINETO:-$(bitcoin-cli getnewaddress)}
1113
echo "Mining to address:" $ADDR
@@ -21,7 +23,8 @@ while true; do
2123
fi
2224
fi
2325
#echo "Mine To:" $ADDR --addr=$ADDR
24-
miner --cli="bitcoin-cli" generate --grind-cmd="bitcoin-util grind" --addr=$ADDR --nbits=$NBITS --set-block-time=$(date +%s)
26+
miner --cli="bitcoin-cli" generate --grind-cmd="bitcoin-util grind" --addr=$ADDR --nbits=$NBITS --set-block-time=$(date +%s) || true
27+
2528
done
2629

2730

wait_chain_sync.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
set -Eeuo pipefail
3+
4+
# Maximum allowed age of the latest block (2 hours in seconds)
5+
MAX_AGE=7200
6+
7+
# Function to get the latest block timestamp
8+
get_latest_block_timestamp() {
9+
local count
10+
count=$(bitcoin-cli getblockcount 2>/dev/null) || {
11+
echo "Error: Failed to get block count. Is bitcoind running?" >&2
12+
return 1
13+
}
14+
local hash
15+
hash=$(bitcoin-cli getblockhash "$count" 2>/dev/null) || {
16+
echo "Error: Failed to get block hash for block $count" >&2
17+
return 1
18+
}
19+
local timestamp
20+
timestamp=$(bitcoin-cli getblock "$hash" 2>/dev/null | grep '"time"' | awk '{print $2}' | sed 's/,//g') || {
21+
echo "Error: Failed to get timestamp for block $hash" >&2
22+
return 1
23+
}
24+
if ! [[ "$timestamp" =~ ^[0-9]+$ ]]; then
25+
echo "Error: Invalid timestamp format: $timestamp" >&2
26+
return 1
27+
fi
28+
echo "$timestamp"
29+
}
30+
31+
# Function to check if the latest block is recent enough
32+
check_block_recency() {
33+
local timestamp
34+
timestamp=$(get_latest_block_timestamp) || return 1
35+
local current_time
36+
current_time=$(date +%s) || {
37+
echo "Error: Failed to get current time" >&2
38+
return 1
39+
}
40+
local time_diff=$((current_time - timestamp))
41+
echo "Latest block timestamp: $timestamp ($(date -d "@$timestamp"))"
42+
echo "Current time: $current_time ($(date))"
43+
echo "Time difference: $time_diff seconds"
44+
if (( time_diff <= MAX_AGE )); then
45+
echo "Latest block is recent enough (less than 2 hours old). Starting miner..."
46+
return 0
47+
else
48+
echo "Latest block is too old ($time_diff seconds). Waiting..."
49+
return 1
50+
fi
51+
}
52+
53+
# Main loop to wait until the block is recent enough
54+
while true; do
55+
if check_block_recency; then
56+
echo "Chain tip is recent enough. Proceeding with mining..."
57+
exit 0
58+
fi
59+
echo "Retrying in 30 seconds..."
60+
sleep 30
61+
done

0 commit comments

Comments
 (0)