-
Notifications
You must be signed in to change notification settings - Fork 0
/
regtest_manager
executable file
·200 lines (178 loc) · 7.18 KB
/
regtest_manager
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
if [ "$0" = "$BASH_SOURCE" ];then
echo "You should source this script to benefit of aliases and functions."
exit 0
fi
# Just spin up two nodes by default
n_nodes=2
start_regtest () {
# Use the global env lightningd by default
if [ -z "$LIGHTNINGD_PATH" ];then LIGHTNINGD_PATH="/usr/local/bin/lightningd";fi
LIGHTNINGD="$LIGHTNINGD_PATH --daemon --network regtest --bitcoin-rpcuser test --bitcoin-rpcpassword test --log-level debug --log-file debug.log"
# Use the global env bitcoind by default
if [ -z "$BITCOIND_PATH" ];then BITCOIND_PATH="/usr/local/bin/bitcoind";fi
BITCOIND="$BITCOIND_PATH -daemon -regtest -txindex -whitelist=\"127.0.0.1\" -rpcpassword=\"test\" -rpcuser=\"test\" -debug"
PREFIX_DIR="$PWD/regtest"
if [ "$#" == "1" ];then
n_nodes=$1
fi
for i in $(seq $n_nodes);do
bc_rpc=$((9000 + $i))
bc_port=$((10000 + $i))
bc_dir="$PREFIX_DIR/bcdir$i"
ln_port=$((11000 + $i))
ln_dir="$PREFIX_DIR/lndir$i"
mkdir -p "$bc_dir" "$ln_dir"
eval "$BITCOIND -connect=\"127.0.0.1:$(($bc_port - 1))\" -rpcport=\"$bc_rpc\" -datadir=\"$bc_dir\" -bind=\"127.0.0.1:$bc_port\"" > /dev/null
alias "bdreg$i"="$BITCOIND -connect=127.0.0.1:$(($bc_port - 1)) -rpcport=$bc_rpc -datadir=$bc_dir -bind=127.0.0.1:$bc_port"
echo "Started bitcoind #$i with P2P port $bc_port, RPC port $bc_rpc and datadir $bc_dir"
sleep 0.2
eval "$LIGHTNINGD --bitcoin-datadir $bc_dir --bitcoin-rpcport $bc_rpc --bind-addr 127.0.0.1:$ln_port --lightning-dir $ln_dir --alias testnode$i"
alias "lnreg$i"="$LIGHTNINGD --bitcoin-datadir $bc_dir --bitcoin-rpcport $bc_rpc --bind-addr 127.0.0.1:$ln_port --lightning-dir $ln_dir --alias testnode$i"
echo "Started lightningd on top of it with directory $ln_dir"
alias "bcreg$i"="bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$bc_rpc"
alias "lcreg$i"="lightning-cli --lightning-dir $ln_dir --network regtest"
echo "==> You can access the lighntingd startup command line with 'lnreg$i', the bitcoind startup command line with 'bdreg$i', the lightning CLI with 'lcreg$i', and the bitcoin CLI with 'bcreg$i'."
echo ""
done
echo ""
echo "Started $n_nodes pairs of bitcoind and lightningd nodes with rpc user \"test\" and pass \"test\" (and poor alias names)."
}
# Generates blocks not too quickly and keep everyone in sync
generate_regtest () {
while true;do
for i in $(seq $n_nodes);do
bcli="bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$((9000 + $i))"
if [ $($bcli generatetoaddress 1 $($bcli getnewaddress) &> /dev/null; echo $?) -ne 0 ];then
return
fi
sleep 3
done
sleep 15
done
}
# First param is CLI, second is the number of blocks to wait for
wait_for_blocks () {
desired_height=$(($($1 getblockcount) + $2));
while test $($1 getblockcount) -lt $desired_height;do
sleep 1
done
}
# With A, B, C three lightning daemons, this funds channels such as A => B => C
fund_regtest () {
if [ $(jq --help > /dev/null; echo $?) -ne 0 ];then
echo "I need the 'jq' utility in order to fund channels."
return
fi
# We cannot brutally generate blocks here or we'll fork
# FIXME: Or maybe if there is only one generator this would be ok ?
echo "Getting some bitcoins on each bitcoin daemon"
echo "Oh, they are immature :-("
echo "Let's try to water them so they grow up"
sleep 0.2
echo "This didn't work.. What can we do ?"
sleep 0.2
echo "Hmmm let's try to generate more than 50 blocks to get some mature ones"
for n in $(seq 60);do
for i in $(seq $n_nodes);do
bcli="bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$((9000 + $i))"
if [ "$n" = "1" ];then
while [ $($bcli getblockchaininfo > /dev/null; echo $?) -ne 0 ];do
echo "Waiting for bitcoind to warmup.."
sleep 1
done
fi
bcli="bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$((9000 + $i))"
$bcli generatetoaddress 1 $($bcli getnewaddress) > /dev/null
sleep 0.01
done
sleep 0.1
echo -en "\r$n/60 blocks generated"
done
sleep 2
echo ""
echo "This worked !!"
echo "(We start generating blocks in background)..."
generate_regtest &
echo "Transferring some to each lightning daemon"
for i in $(seq $n_nodes);do
bcli="bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$((9000 + $i))"
lcli="lightning-cli --lightning-dir $PREFIX_DIR/lndir$i --network regtest"
$bcli sendtoaddress $($lcli newaddr | jq -r .address) 10
done
wait_for_blocks "$bcli" "1"
echo "Now let's fund channels !"
for i in $(seq $(($n_nodes - 1)));do
# i current, ii next one
lcli_i="lightning-cli --lightning-dir $PREFIX_DIR/lndir$i --network regtest"
lcli_ii="lightning-cli --lightning-dir $PREFIX_DIR/lndir$(($i + 1)) --network regtest"
echo "Funding a channel between node $i and node $(($i + 1)) :"
id_ii=$($lcli_ii getinfo | jq -r .id)
$lcli_i connect "$id_ii@127.0.0.1:$((11000 + $i + 1))" > /dev/null
echo "Waiting for lightningd to be aware of its bitcoins.."
list=$($lcli_i listfunds | jq .outputs[0])
while [ "$list" = "null" ];do
sleep 1
list=$($lcli_i listfunds | jq .outputs[0])
done
sleep 1
$lcli_i fundchannel $id_ii 100000 253perkw
echo ""
done
wait_for_blocks "$bcli" "1"
echo "Ok, you should be all set!"
}
# For a network of at least 3 nodes, this funds a channel between the nodes i and i-2 for
# i >= 3.
fund_many_regtest () {
if [ $n_nodes -lt 3 ];then
echo "Your regtest network is not large enough to fund many channels."
return
fi
for i in $(seq 3 $n_nodes);do
lcli_i="lightning-cli --lightning-dir $PREFIX_DIR/lndir$i --network regtest"
lcli_ii="lightning-cli --lightning-dir $PREFIX_DIR/lndir$(($i - 2)) --network regtest"
echo "Funding a channel between node $i and node $(($i - 2)) :"
id_ii=$($lcli_ii getinfo | jq -r .id)
$lcli_i connect "$id_ii@127.0.0.1:$((11000 + $i - 2))" > /dev/null
$lcli_i fundchannel $id_ii 100000 253perkw
echo ""
done
echo "All done, now wait for channels to be locked"
}
# This funds a channel between two running lightning daemons
# First param is the indice of the funder (e.g. 2)
# Second param is the indice of the fundee (e.g. 4)
fund_channel_regtest () {
lcli_i="lightning-cli --lightning-dir $PREFIX_DIR/lndir$1 --network regtest"
lcli_ii="lightning-cli --lightning-dir $PREFIX_DIR/lndir$2 --network regtest"
id_ii=$($lcli_ii getinfo | jq -r .id)
$lcli_i connect "$id_ii@127.0.0.1:$((11000 + $2))" > /dev/null
$lcli_i fundchannel $id_ii 100000 253perkw
}
# Stop the regression testing network and __deletes the data directories__
stop_regtest () {
PREFIX_DIR="$PWD/regtest"
if ! test -d "$PREFIX_DIR";then
echo "No regtest/ directory here..."
return
fi
for i in $(seq $n_nodes);do
lightning-cli --lightning-dir "$PREFIX_DIR/lndir$i" --network regtest stop > /dev/null
rm -rf "$PREFIX_DIR/lndir$i"
echo "lightningd #$i stopped"
sleep 0.1
bitcoin-cli -regtest -rpcpassword=test -rpcuser=test -rpcport=$((9000 + $i)) stop > /dev/null
rm -rf "$PREFIX_DIR/bcdir$i"
echo "bitcoind #$i stopped"
done
}
# Don't use if you're up-to-date
kill_regtest () {
echo "Getting bitcoin and lightning daemons PID with a hacky hack, and killing them :
prefer using 'stop_regtest' if you run lightningd > 0.7.2";
for i in $(ps -edf |grep -E 'bitcoind|lightningd' |grep regtest |cut -c 10-15);do
kill -9 $i
done
rm -rf $PWD/regtest
}