forked from RGB-Tools/rgb-lightning-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregtest.sh
More file actions
executable file
·157 lines (143 loc) · 4.06 KB
/
Copy pathregtest.sh
File metadata and controls
executable file
·157 lines (143 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
#
# utility script to run and command regtest services
#
name="./$(basename "$0")"
COMPOSE="docker compose"
if ! $COMPOSE >/dev/null; then
echo "could not call docker compose (hint: install docker compose plugin)"
exit 1
fi
BITCOIN_CLI="$COMPOSE exec -u blits bitcoind bitcoin-cli -regtest"
INITIAL_BLOCKS=103
TIMEOUT=100
_die () {
echo "ERR: $*"
exit 1
}
_is_port_bound() {
local port=$1
case "$(uname)" in
"Linux")
[ -n "$(ss -HOant "sport = :$port")" ] && return 0
ss -Oant "sport = :$port"
return 1
;;
"Darwin")
lsof -i "tcp:${port}" -sTCP:LISTEN -t >/dev/null && return 0
;;
*)
_die "port check unsupported on this OS ($(uname))"
;;
esac
return 1
}
_wait_for_bitcoind() {
# wait until bitcoind RPC is actually ready
start_time=$(date +%s)
until $BITCOIN_CLI getblockchaininfo >/dev/null 2>&1; do
current_time=$(date +%s)
if [ $((current_time - start_time)) -gt $TIMEOUT ]; then
echo "Timeout waiting for bitcoind RPC to become ready"
$COMPOSE logs bitcoind
exit 1
fi
sleep 1
done
}
_wait_for_electrs() {
# wait for electrs to have completed startup
start_time=$(date +%s)
until $COMPOSE logs electrs |grep -q 'finished full compaction'; do
current_time=$(date +%s)
if [ $((current_time - start_time)) -gt $TIMEOUT ]; then
echo "Timeout waiting for electrs to start"
$COMPOSE logs electrs
exit 1
fi
sleep 1
done
}
_start_services() {
_stop_services
mkdir -p data{core,index,ldk0,ldk1,ldk2,rgs}
# see compose.yaml for the exposed ports
EXPOSED_PORTS=(3000 50001)
for port in "${EXPOSED_PORTS[@]}"; do
if _is_port_bound "$port"; then
_die "port $port is already bound, services can't be started"
fi
done
# start bitcoind first to avoid depending services failing due to RPC unavailability
$COMPOSE up -d bitcoind
echo && echo "preparing bitcoind wallet"
_wait_for_bitcoind
$BITCOIN_CLI createwallet miner >/dev/null
$BITCOIN_CLI -rpcwallet=miner -generate $INITIAL_BLOCKS >/dev/null
$COMPOSE up -d
echo "waiting for electrs to have completed startup"
_wait_for_electrs
# optionally start VSS server
if [ "${VSS:-}" = "1" ]; then
echo "starting VSS server..."
$COMPOSE --profile vss up -d
echo "VSS server available at http://localhost:8081/vss"
fi
}
_stop_services() {
$COMPOSE --profile gossip down -v --remove-orphans
rm -rf data{core,index,ldk0,ldk1,ldk2,rgs} 2>/dev/null || sudo rm -rf data{core,index,ldk0,ldk1,ldk2,rgs}
}
_mine() {
local num_blocks="$1"
$BITCOIN_CLI -rpcwallet=miner -generate "$num_blocks" >/dev/null
}
_sendtoaddress() {
local address="$1"
local amount="$2"
$BITCOIN_CLI sendtoaddress "$address" "$amount"
}
_help() {
echo "$name [-h|--help]"
echo " show this help message"
echo
echo "$name start"
echo " stop services, clean up, start services,"
echo " wait for services to have completed startup,"
echo " create bitcoind wallet used for mining,"
echo " generate initial blocks"
echo
echo "$name stop"
echo " stop services and clean up"
echo
echo "$name mine <blocks>"
echo " mine the requested number of blocks"
echo
echo "$name sendtoaddress <address> <amount>"
echo " send the requested amount to the specified bitcoin address"
}
# cmdline arguments
[ -z "$1" ] && _help
case $1 in
-h|--help)
_help
;;
start)
_start_services
;;
stop)
_stop_services
;;
mine)
[ -n "$2" ] || _die "num blocks is required"
_mine "$2"
;;
sendtoaddress)
[ -n "$2" ] || _die "address is required"
[ -n "$3" ] || _die "amount is required"
_sendtoaddress "$2" "$3"
;;
*)
_die "unsupported argument \"$1\""
;;
esac