-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_toolbox_lib.sh
273 lines (233 loc) · 5.83 KB
/
_toolbox_lib.sh
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# shellcheck shell=bash
# this is a shell library, it should be sourced by scripts in toolbox
set -e -o pipefail
export REAL_TOOLBOX_DIR
REAL_TOOLBOX_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
pushd "$REAL_TOOLBOX_DIR" > /dev/null
source _toolbox_config.sh
popd > /dev/null
echo_err() {
printf "\e[31m%s\e[0m\n" "$*" >&2;
}
# https://gist.github.com/ahendrix/7030300
report_error() {
local err=$?
set +o xtrace
local code="${1:-1}"
echo_err "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}' exited with status $err"
# Print out the stack trace described by $function_stack
if [[ ${#FUNCNAME[@]} -gt 2 ]]
then
echo_err "Call tree:"
for ((i=1;i<${#FUNCNAME[@]}-1;i++))
do
echo_err " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
done
fi
echo_err "Exiting with status ${code}"
exit "${code}"
}
trap report_error ERR
realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
BC_ARGS=(-l "$REAL_TOOLBOX_DIR/_toolbox.bc")
trim() {
local str
str=$(cat)
if [[ "$str" =~ [^[:space:]](.*[^[:space:]])? ]]; then
printf "%s" "${BASH_REMATCH[0]}"
else
echo -n "$str"
fi
}
sat2btc() {
local number=${1}
if [[ -z "$number" ]]; then
read -r number
fi
echo "${number} / 100000000" | bc "${BC_ARGS[@]}" | xargs printf "%.*f\n" 8
}
btc2sat() {
local number=${1}
if [[ -z "$number" ]]; then
read -r number
fi
echo "${number} * 100000000" | bc "${BC_ARGS[@]}" | xargs printf "%.*f\n" 0
}
btc2msat() {
local number=${1}
if [[ -z "$number" ]]; then
read -r number
fi
echo "${number} * 100000000000" | bc "${BC_ARGS[@]}" | xargs printf "%.*f\n" 0
}
msat2btc() {
local number=${1}
if [[ -z "$number" ]]; then
read -r number
fi
echo "${number} / 100000000000" | bc "${BC_ARGS[@]}" | xargs printf "%.*f\n" 8
}
unquote() {
tr -d '"'
}
is() {
test "$(echo "$1" | bc "${BC_ARGS[@]}")" -eq 1
}
compute() {
echo "$1" | bc "${BC_ARGS[@]}"
}
uppercase() {
tr '[:lower:]' '[:upper:]'
}
ln_connect_string() {
local person=${1:-alice}
local pubkey
if ! pubkey=$(pubkey "$person"); then
return 1
fi
echo "$pubkey@$person"
}
# checks whether btcd is our master bitcoin node
# we branch some code based on this info
is_btcd_master() {
local container
container=$(lookup_container 1 role bitcoin)
if [[ -z "$container" ]]; then
echo_err "unable to lookup first container with role bitcoin"
exit 1
fi
local flavor
flavor=$(inspect_container "${container}" flavor)
if [[ -z "$flavor" ]]; then
echo_err "unable to determine flavor of service in container '$container'"
exit 1
fi
test "$flavor" == "btcd"
}
get_flavor() {
local person=${1:?required}
local container
container=$(docker-compose ps -q "$person")
if [[ -z "$container" ]]; then
echo_err "unable to lookup container for for '$person'"
exit 1
fi
local flavor
flavor=$(inspect_container "${container}" flavor)
if [[ -z "$flavor" ]]; then
echo_err "unable to determine flavor of service for '$person'"
exit 1
fi
echo -n "$flavor"
}
get_role() {
local person=${1:?required}
local container
container=$(docker-compose ps -q "$person")
if [[ -z "$container" ]]; then
echo_err "unable to lookup container for for '$person'"
exit 1
fi
local role
role=$(inspect_container "${container}" role)
if [[ -z "$role" ]]; then
echo_err "unable to determine role of service for '$person'"
exit 1
fi
echo -n "$role"
}
wait_for() {
local msg=${1:?required}
local cmd=${2:?required}
local cmd2=${3}
local max=${4:-100}
local cmd2_interval=${5:-5}
local interval=${6:-1}
local counter=1
local status
local saved_opts
while true; do
saved_opts="set -$-"
set +e
eval "${cmd}" > /dev/null 2>&1;
status=$?
eval "${saved_opts}"
if [[ "$status" -eq 0 ]]; then
if [[ "$counter" -ne 1 ]]; then
echo
fi
return 0
fi
if [[ "$counter" -eq 1 ]]; then
echo -n "Waiting for $msg. Zzz.."
fi
sleep "${interval}"
echo -n "."
if [[ -n "$cmd2" ]]; then
if ! (( "$counter" % "$cmd2_interval" )); then
echo
saved_opts="set -$-"
set +e
eval "${cmd2}"
eval "${saved_opts}"
echo -n "Waiting for $msg. Zzz.."
fi
fi
((++counter))
if [[ ${counter} -gt ${max} ]]; then
echo
echo_err "FATAL: wait_for stuck waiting for '$msg' (tried $max times with interval of $interval sec)"
exit 1
fi
done
}
wait_for_onchain_balance() {
local person=${1:?required}
local expected_amount=${2:?required}
local cmd="is \"\$(onchain_balance \"$person\") >= $expected_amount\""
wait_for "$person to receive onchain balance of $expected_amount BTC" "$cmd"
}
wait_for_route() {
local from_person=${1:?required}
local to_person=${2:?required}
local amt_btc=${3}
local cmd="get_route \"$from_person\" \"$to_person\" ${amt_btc}"
local cmd2="generate 1"
wait_for "route between $from_person and $to_person" "$cmd" "$cmd2"
}
wait_sync_one() {
local person=${1:?required}
local cmd="\"$person\" getinfo"
wait_for "$person to become available" "$cmd"
local flavor
flavor=$(get_flavor "$person")
local cmd
case "$flavor" in
lnd)
cmd="[[ \$(\"$person\" getinfo | jq \".synced_to_chain\") == \"true\" ]]"
wait_for "$person to sync" "$cmd"
;;
lightning)
cmd="[[ \$(chain_height) == \$(\"$person\" getinfo | jq .blockheight) ]]"
wait_for "$person to sync" "$cmd"
;;
*)
echo_err "unsupported flavor type '$flavor' for '$person'"
return 1
;;
esac
}
wait_sync() {
for person in "$@"; do
wait_sync_one "$person"
done
}
wait_simnet_ready() {
wait_for "simnet to get ready" "simnet_ready"
}
wait_is() {
local condition=${1:?required}
wait_for "condition '${condition}' to be met" "is \"$condition\""
}