Browse Source

Flatten the curve!

master
Damian Mee 4 years ago
parent
commit
7f0bc6114e
No known key found for this signature in database GPG Key ID: 2F961EAB8789725D
  1. 14
      Dockerfile
  2. 124
      switch.sh

14
Dockerfile

@ -1,12 +1,10 @@
FROM alpine:3.10
FROM alpine:3.12
RUN apk add --no-cache curl jq bash
RUN apk add --no-cache curl jq
RUN mkdir /lnd/
RUN mkdir /secrets/
RUN mkdir /statuses/
RUN mkdir /lnd/ /secrets/ /statuses/
COPY switch.sh /bin/switch
RUN chmod +x /bin/switch
COPY switch.sh /usr/local/bin/switch
RUN chmod +x /usr/local/bin/switch
ENTRYPOINT ["switch"]
ENTRYPOINT ["/usr/local/bin/switch"]

124
switch.sh

@ -1,4 +1,4 @@
#!/bin/bash -e
#!/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
@ -15,59 +15,81 @@
# Output: /statuses/node-status-bitcoind-ready (when ready, where a service can pick it up)
# Then
PASSWORD=`cat /secrets/rpcpass.txt`
PASSWORD="$(cat /secrets/rpcpass.txt)"
# If JSONRPCURL doesn't exist then set it
if [ -z $JSONRPCURL ]; then
JSONRPCURL="http://10.254.2.2:8332"
if [ -z "$JSONRPCURL" ]; then
JSONRPCURL='http://10.254.2.2:8332'
fi
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 "lncm:$PASSWORD" --data-binary '{"jsonrpc": "1.0", "id":"switchme", "method": "getblockchaininfo", "params": [] }' $JSONRPCURL)"; then
echo "Error: 'getblockchaininfo' request to bitcoind failed"
return
fi
if 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
}
while true; do
IS_NEUTRINO=`grep -c 'bitcoin.node=neutrino' /lnd/lnd.conf`
if [ $IS_NEUTRINO -eq 1 ]; then
echo "If set to neutrino then lets check bitcoind"
INFO=`curl --user lncm:$PASSWORD --data-binary '{"jsonrpc": "1.0", "id":"switchme", "method": "getblockchaininfo", "params": [] }' $JSONRPCURL 2>/dev/null`
# check for errors
ERROR=`echo $INFO | jq .error`
if [ ! -z $ERROR ]; then
# if no errors
# Check prune mode
PRUNE_MODE=`echo $INFO | jq .result.pruned`
# check headers
HEADERS=`echo $INFO | jq .result.headers`
# check blocks
BLOCKS=`echo $INFO | jq .result.blocks`
if [ $PRUNE_MODE != "true" ]; then
echo "Not pruned"
# Node pruned so lets do the switching
echo "Checking if synced...."
if [ ! -f /statuses/node-status-bitcoind-ready ]; then
if [ $HEADERS -eq $BLOCKS ]; then
echo "Bitcoind has been switched across to neutrino"
touch /statuses/node-status-bitcoind-ready
sed -i 's/bitcoin.node\=neutrino/bitcoin.node\=bitcoind/g; ' /lnd/lnd.conf
else
echo "Node isn't full synched yet"
fi
else
echo "LND is already switched to bitcoind!"
fi
else
echo "No need to switch from neutrino in pruned mode"
fi
else
# if bitcoind error
echo "Error from bitcoind"
echo $ERROR
fi
else
echo "Neutrino mode has been disabled"
echo "Switchback is not supported in this version"
#TODO: Lets maybe try to switch back
fi
# Run every every 1 hour
sleep 3600
if ! switch_on_sync_done; then
echo 'Checking not necessary. Exiting.'
break
fi
# Run every every 1 hour
sleep 3600
done

Loading…
Cancel
Save