This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
forked from lncm/docker-lnd-neutrino-switch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
switch.sh
executable file
·98 lines (80 loc) · 3.04 KB
/
switch.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
#!/bin/sh -e
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Directories
# Allow access to /secrets/rpcpass.txt
# Allow access to LND directory (use /lnd/lnd.conf)
# Allow access to 'statuses'. /statuses/
# Output: /statuses/node-status-bitcoind-ready (when ready, where a service can pick it up)
RPCUSER="${RPCUSER:-umbrelrpc}"
RPCPASS="${RPCPASS:-$(cat /secrets/rpcpass.txt)}" # Default password location: /secrets/rpcpass.txt
SLEEPTIME="${SLEEPTIME:-3600}" # Default sleep: 3600
JSONRPCURL="${JSONRPCURL:-http://10.254.2.2:8332}" # Default RPC endpoint: http://10.254.2.2:8332
LND_CONTAINER_NAME="${LND_CONTAINER_NAME:-lnd}" # Default Docker container name: lnd
PREV_MATCH=
switch_on_sync_done() {
# Node not pruned so lets do the switching
echo 'Checking if synced...'
if [ -f /statuses/node-status-bitcoind-ready ]; then
echo 'LND is already switched to bitcoind!'
return 1
fi
if ! grep -q 'bitcoin.node=neutrino' /lnd/lnd.conf; then
echo 'Neutrino mode has been disabled'
echo 'Switchback is not supported in this version'
return 1
fi
echo 'If set to neutrino then lets check bitcoind'
if ! INFO="$(curl --silent --user "$RPCUSER:$RPCPASS" --data-binary '{"jsonrpc": "1.0", "id":"switchme", "method": "getblockchaininfo", "params": [] }' "$JSONRPCURL")"; then
echo "Error: 'getblockchaininfo' request to bitcoind failed"
return
fi
if [ -z "$INFO" ] || err="$(jq -ner "$INFO | .error")"; then
echo 'Error: from bitcoind'
echo "${err:-Unknown error}"
return
fi
INFO="$(jq -ne "$INFO | .result")"
# Check if pruned
if jq -ne "$INFO | .pruned == true"; then
echo 'No need to switch from neutrino in pruned mode'
return 1
fi
echo 'Not pruned'
if jq -ne "$INFO | .headers - .blocks > 10"; then
echo "Node isn't full synced yet"
PREV_MATCH=
return
fi
if [ -z "$PREV_MATCH" ]; then
PREV_MATCH="$(jq -ne "$INFO | .headers")"
echo 'Sync seems complete! Will switch on next check.'
return
fi
# Skip switch, if headers number didn't change since last check
# (possible network issue).
if jq -ne "$INFO | .headers == $PREV_MATCH"; then
echo 'Skipping switch for now: headers seem stale'
return
fi
echo 'Bitcoind has been switched across to neutrino'
touch /statuses/node-status-bitcoind-ready
sed -Ei 's|(bitcoin.node)=neutrino|\1=bitcoind|g' /lnd/lnd.conf
sed -i "s/^feeurl=.*//g;" /lnd/lnd.conf
echo "Restarting LND"
docker stop "$LND_CONTAINER_NAME"
docker start "$LND_CONTAINER_NAME"
}
while true; do
if ! switch_on_sync_done; then
echo 'Checking not necessary. Exiting.'
break
fi
# Run every every 1 hour by default or as per configurable
sleep "$SLEEPTIME"
done