11#! /usr/bin/env bash
22#
3- # Run local regtest `bitcoind` nodes.
4- #
5- # shell: alias bt18='bitcoin-cli -rpcconnect=localhost:18149 -rpcuser=user -rpcpassword=password'
3+ # Run local regtest `bitcoind` nodes using versions specified in the config file .
4+ # Config file default: ~/.run-bitcoind.conf
5+ # Config file optional: ./run-bitcoind.conf
66
77set -euo pipefail
88
@@ -12,189 +12,185 @@ RPC_USER="user"
1212RPC_PASSWORD=" password"
1313
1414usage () {
15- cat << EOF
15+ cat << ' EOF '
1616Usage:
1717
1818 ./run-bitcoind.sh [COMMAND]
1919
2020COMMAND
21- - all Start all known bitcoind versions.
22- - start [KNOWN_VERSION] Start bitcoind nodes, defaults to v22.
23- - stop Kill all bitcoind nodes using 'pkill bitcoind'.
24-
25- KNOWN_VERSION
26- - v28 Bitcoin Core v28.0
27- - v27 Bitcoin Core v27.1
28- - v26 Bitcoin Core v26.2
29- - v25 Bitcoin Core v25.2
30- - v24 Bitcoin Core v24.2
31- - v23 Bitcoin Core v23.2
32- - v22 Bitcoin Core v22.1
33- - v21 Bitcoin Core v0.21.2
34- - v20 Bitcoin Core v0.20.2
35- - v19 Bitcoin Core v0.19.1
36- - v18 Bitcoin Core v0.18.1
37- - v17 Bitcoin Core v0.17.1
21+
22+ - all Start all bitcoind versions defined in the config file.
23+ - start [VERSION_ALIAS] Start bitcoind nodes for the specified version alias (default: v22).
24+ - stop Kill all bitcoind nodes and clean up test directories.
25+
26+ CONFIGURATION
27+
28+ - Priority
29+
30+ 1. RUN_BITCOIND_CONF environment variable
31+ 2. ./run-bitcoind.conf (script directory)
32+ 3. ~/.run-bitcoind.conf (home directory)
33+
34+ - Config format
35+
36+ <VERSION_ALIAS> <VERSION_NUMBER> <VERSION_ID> <BITCOIND_PATH>
37+
38+ VERSION_ALIAS Passed as command line argument to $(start).
39+ VERISON_NUMBER The Bitocin Core version number.
40+ VERSION_ID Used as part of port numbers.
41+ BITCOIND_PATH Path to $(bitcoind) binary.
42+
43+ - Examples
44+
45+ v28 28.1 281 /opt/bitcoin-28.0/bin/bitcoind
46+ v24 24.2 242 /opt/bitcoin-24.2/bin/bitcoind
47+ v21 0.21.2 212 /opt/bitcoin-0.21.2/bin/bitcoind
48+
3849EOF
3950}
4051
4152main () {
4253 local cmd=" ${1:- usage} "
43- local version=" ${2:- v22 } "
54+ local version=" ${2:- } "
4455
4556 # FIXME: This is a hackish way to get the help flag.
4657 if [ " $cmd " = " usage" ] || [ " $cmd " = " -h" ] || [ " $cmd " = " --help" ] || [ " $cmd " = " help" ]; then
4758 usage
4859 exit 0
4960 fi
5061
62+ case $cmd in
63+ all|start)
64+ # Config loading logic
65+ local config_file=${RUN_BITCOIND_CONF:- }
66+
67+ if [ -z " $config_file " ]; then
68+ local script_dir
69+
70+ script_dir=$( dirname " $0 " )
71+ local local_config=" ${script_dir} /run-bitcoind.conf"
72+
73+ if [ -f " $local_config " ]; then
74+ config_file=" $local_config "
75+ else
76+ config_file=" $HOME /.run-bitcoind.conf"
77+ fi
78+ fi
79+
80+ if [ ! -f " $config_file " ]; then
81+ err " Config file $config_file not found. Please create it."
82+ fi
83+
84+ # Load config into parallel arrays
85+ VERSION_ALIASES=()
86+ VERSION_NUMBERS=()
87+ VERSION_IDS=()
88+ BITCOIND_PATHS=()
89+
90+ while IFS= read -r line; do
91+ line=$( echo " $line " | sed -e ' s/#.*//' -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' )
92+ [ -z " $line " ] && continue
93+ read -r alias version_number version_id path <<< " $line"
94+ VERSION_ALIASES+=(" $alias " )
95+ VERSION_NUMBERS+=(" $version_number " )
96+ VERSION_IDS+=(" $version_id " )
97+ BITCOIND_PATHS+=(" $path " )
98+ done < " $config_file "
99+ ;;
100+ esac
101+
51102 case $cmd in
52103 all)
53- start " v28" # 28.0
54- start " v27" # 27.1
55- start " v26" # 26.2
56- start " v25" # 25.2
57- start " v24" # 24.2
58- start " v23" # 23.2
59- start " v22" # 22.1
60- start " v21" # 0.21.2
61- start " v20" # 0.20.2
62- start " v19" # 0.19.1
63- start " v18" # 0.18.1
64- start " v17" # 0.17.1
104+ for index in " ${! VERSION_ALIASES[@]} " ; do
105+ start " ${VERSION_ALIASES[$index]} " \
106+ " ${VERSION_NUMBERS[$index]} " \
107+ " ${VERSION_IDS[$index]} " \
108+ " ${BITCOIND_PATHS[$index]} "
109+ done
65110 ;;
66111
67112 start)
68- start " $version "
113+ if [ -z " $version " ]; then
114+ version=" v22" # Default version
115+ fi
116+
117+ found=false
118+ for index in " ${! VERSION_ALIASES[@]} " ; do
119+ if [ " ${VERSION_ALIASES[$index]} " = " $version " ]; then
120+ start " $version " \
121+ " ${VERSION_NUMBERS[$index]} " \
122+ " ${VERSION_IDS[$index]} " \
123+ " ${BITCOIND_PATHS[$index]} "
124+ found=true
125+ break
126+ fi
127+ done
128+
129+ if [ " $found " = false ]; then
130+ err " Version '$version ' not found in config file."
131+ fi
69132 ;;
70133
71134 stop)
72- pkill bitcoind
73- rm -rf " /tmp/corepc-0.17.1/2/regtest/wallets" > /dev/null
74- rm -rf " /tmp/corepc-0.18.1/2/regtest/wallets" > /dev/null
75- rm -rf " /tmp/corepc-22.1/2/regtest/wallets" > /dev/null
135+ pkill bitcoind || true
136+ rm -rf /tmp/corepc-* /2/regtest/wallets > /dev/null 2>&1
137+ echo " Stopped all bitcoind instances and cleaned wallets."
76138 ;;
77139 * )
78140 usage
79- say " Error: unknown command $cmd "
141+ say " Error: unknown command ' $cmd ' "
80142 ;;
81-
82143 esac
83144}
84145
85146start () {
86147 local version=" $1 "
148+ local version_number=" $2 "
149+ local version_id=" $3 "
150+ local bitcoind_path=" $4 "
87151
88- case $version in
89- v28)
90- local version_number=" 28.0"
91- local version_id=" 280"
92- ;;
93-
94- v27)
95- local version_number=" 27.1"
96- local version_id=" 271"
97- ;;
98-
99- v26)
100- local version_number=" 26.2"
101- local version_id=" 262"
102- ;;
103-
104- v25)
105- local version_number=" 25.2"
106- local version_id=" 252"
107- ;;
108-
109- v24)
110- local version_number=" 24.2"
111- local version_id=" 242"
112- ;;
113-
114- v23)
115- local version_number=" 23.2"
116- local version_id=" 232"
117- ;;
118-
119- v22)
120- local version_number=" 22.1"
121- local version_id=" 221"
122- ;;
123-
124- v21)
125- local version_number=" 0.21.2"
126- local version_id=" 212"
127- ;;
128-
129- v20)
130- local version_number=" 0.20.2"
131- local version_id=" 202"
132- ;;
133-
134- v19)
135- local version_number=" 0.19.1"
136- local version_id=" 191"
137- ;;
138-
139- v18)
140- local version_number=" 0.18.1"
141- local version_id=" 181"
142- ;;
143-
144- v17)
145- local version_number=" 0.17.1"
146- local version_id=" 172"
147- ;;
148-
149- * )
150- usage
151- err " Error: unknown version $version "
152- ;;
153- esac
152+ if [ ! -x " $bitcoind_path " ]; then
153+ err " bitcoind binary not found or not executable at '$bitcoind_path '"
154+ fi
154155
155- run_bitcoind " $version " " $version_number " " $version_id "
156+ run_bitcoind " $version " " $version_number " " $version_id " " $bitcoind_path "
156157}
157158
158159run_bitcoind () {
159- local version=" $1 " # eg, v22
160- local version_number=" $2 " # eg, 22.1
161- local version_id=" $3 " # eg, 221
160+ local version=" $1 " # e.g., v28
161+ local version_number=" $2 " # e.g., 28.1.0
162+ local version_id=" $3 " # e.g., 281
163+ local bitcoind=" $4 "
162164
163165 local test_dir=" /tmp/corepc-${version_number} "
164- local bitcoind=" /opt/bitcoin-${version_number} /bin/bitcoind"
165- # RPC port number of the node we hit when testing (xyz49 where xyz is the bitcoind version identifier).
166166 local rpc_port=" ${version_id} 49"
167167
168- if " $bitcoind " -version | grep -q " ${version_number} " ; then
169- echo " Starting two bitcoind v${version_number} instances"
170- else
171- echo " Wrong bitcoind version, expected ${version_number} "
172- " $bitcoind " -version
168+ if ! " $bitcoind " -version | grep -q " $version_number " ; then
169+ echo " Version mismatch: Expected $version_number , got $( " $bitcoind " -version | head -n1) "
173170 exit 1
174171 fi
175172
176173 rm -rf " ${test_dir} "
177174 mkdir -p " ${test_dir} /1" " ${test_dir} /2"
178175
179-
180176 local block_filter_arg=" "
181- if echo " ${ version_number} " | grep -q " 0\.\ (19\|2\) " ; then
177+ if [[ " $version_number " =~ ^ 0\. (19| 2) ]] ; then
182178 block_filter_arg=" -blockfilterindex=1"
183179 fi
184180
185181 local fallback_fee_arg=" "
186- if echo " ${ version_number} " | grep -q " 2.\. " ; then
182+ if [[ " $version_number " =~ ^[0-9]+ \. ]] ; then
187183 fallback_fee_arg=" -fallbackfee=0.00001000"
188184 fi
189185
186+ echo " Starting bitcoind v${version_number} (alias: ${version} )..."
190187 " $bitcoind " -regtest $fallback_fee_arg $block_filter_arg \
191188 -datadir=" ${test_dir} /1" \
192189 -port=" ${version_id} 48" \
193190 -server=0 \
194191 -printtoconsole=0 &
195192
196- # Make sure it's listening on its p2p port.
197- sleep 1
193+ sleep 1 # Allow first node to start
198194
199195 " $bitcoind " -regtest $fallback_fee_arg $block_filter_arg \
200196 -datadir=" ${test_dir} /2" \
@@ -208,21 +204,13 @@ run_bitcoind() {
208204 -zmqpubrawblock=tcp://0.0.0.0:" ${version_id} 32" \
209205 -zmqpubrawtx=tcp://0.0.0.0:" ${version_id} 33" &
210206
211- # Let it connect to the other node.
212- sleep 1
213-
207+ sleep 1 # Let nodes connect
214208 echo " Two connected bitcoind v${version_number} instances running, one node has JSON-RPC listening on port ${rpc_port} "
215209}
216210
217211say () {
218212 echo " run-bitcoind: $1 "
219213}
220-
221- err () {
222- echo " $1 " >&2
223- exit 1
224- }
225-
226214#
227215# Main script
228216#
0 commit comments