From 96afe000170cdc3cc91f0c7a2dbc9f85980a74a8 Mon Sep 17 00:00:00 2001 From: openoms Date: Fri, 11 Jun 2021 09:44:53 +0100 Subject: [PATCH] cln: add CLOSEALL option --- home.admin/99clnMenu.sh | 12 +++- home.admin/BBcloseAllChannels.sh | 101 +++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/home.admin/99clnMenu.sh b/home.admin/99clnMenu.sh index ff9aff7cc..4065285ef 100644 --- a/home.admin/99clnMenu.sh +++ b/home.admin/99clnMenu.sh @@ -11,6 +11,8 @@ else NETWORK=${chain}net fi +source <(/home/admin/config.scripts/network.aliases.sh getvars cln $1) + # get the local network IP to be displayed on the LCD source <(/home/admin/config.scripts/internet.sh status local) @@ -39,9 +41,13 @@ fi #TODO OPTIONS+=(NAME "Change Name/Alias of Node") -openChannels=$(sudo -u bitcoin /usr/local/bin/lncli --chain=${network} --network=${chain}net listchannels 2>/dev/null | jq '.[] | length') +ln_getInfo=$($lightningcli_alias getinfo 2>/dev/null) +ln_channels_online="$(echo "${ln_getInfo}" | jq -r '.num_active_channels')" 2>/dev/null +cln_num_inactive_channels="$(echo "${ln_getInfo}" | jq -r '.num_inactive_channels')" 2>/dev/null +openChannels=$((ln_channels_online+cln_num_inactive_channels)) + if [ ${#openChannels} -gt 0 ] && [ ${openChannels} -gt 0 ]; then -#TODO OPTIONS+=(CLOSEALL "Close all open Channels") +OPTIONS+=(CLOSEALL "Close all open Channels") HEIGHT=$((HEIGHT+1)) CHOICE_HEIGHT=$((CHOICE_HEIGHT+1)) fi @@ -120,7 +126,7 @@ case $CHOICE in fi ;; CLOSEALL) - /home/admin/BBcloseAllChannels.sh + /home/admin/BBcloseAllChannels.sh cln $NETWORK echo "Press ENTER to return to main menu." read key ;; diff --git a/home.admin/BBcloseAllChannels.sh b/home.admin/BBcloseAllChannels.sh index af849a1a6..8f026e74b 100755 --- a/home.admin/BBcloseAllChannels.sh +++ b/home.admin/BBcloseAllChannels.sh @@ -3,42 +3,93 @@ # load raspiblitz config data (with backup from old config) source /home/admin/raspiblitz.info source /mnt/hdd/raspiblitz.conf -if [ ${#network} -eq 0 ]; then network=`cat .network`; fi +if [ ${#network} -eq 0 ]; then network=$(cat .network); fi if [ ${#network} -eq 0 ]; then network="bitcoin"; fi if [ ${#chain} -eq 0 ]; then chain=$(${network}-cli getblockchaininfo | jq -r '.chain') fi -# precheck: AutoPilot -if [ "${autoPilot}" = "on" ]; then - dialog --title 'Info' --msgbox 'You need to turn OFF the LND AutoPilot first,\nso that closed channels are not opening up again.\nYou find the AutoPilot -----> SERVICES section' 7 55 - exit 1 -fi +source <(/home/admin/config.scripts/network.aliases.sh getvars $1 $2) +shopt -s expand_aliases +alias bitcoincli_alias="$bitcoincli_alias" +alias lncli_alias="$lncli_alias" +alias lightningcli_alias="$lightningcli_alias" -command="lncli --chain=${network} --network=${chain}net closeallchannels --force" +if [ $LNTYPE = cln ];then + # https://lightning.readthedocs.io/lightning-close.7.html + peerlist=$($lightningcli_alias listpeers|grep '"id":'|awk '{print $2}'|cut -d, -f1) + # to display + function cln_closeall_command { + for i in $peerlist; do + # close id [unilateraltimeout] [destination] [fee_negotiation_step] [*wrong_funding*] + echo "$lightningcli_alias close $i 30;" + done + } + command=$(cln_closeall_command) + # to run + function cln_closeall { + for i in $peerlist; do + # close id [unilateraltimeout] [destination] [fee_negotiation_step] [*wrong_funding*] + lightningcli_alias close $i 30 + done + } +elif [ $LNTYPE = lnd ];then + # precheck: AutoPilot + if [ "${autoPilot}" = "on" ]; then + dialog --title 'Info' --msgbox 'You need to turn OFF the LND AutoPilot first,\nso that closed channels are not opening up again.\nYou find the AutoPilot -----> SERVICES section' 7 55 + exit 1 + fi + command="$lncli_alias closeallchannels --force" +fi clear -echo "***********************************" -echo "Closing All Channels (EXPERIMENTAL)" -echo "***********************************" -echo "" -echo "COMMAND LINE: " -echo $command -echo "" -echo "RESULT:" - -# PRECHECK) check if chain is in sync -chainInSync=$(lncli --chain=${network} --network=${chain}net getinfo | grep '"synced_to_chain": true' -c) -if [ ${chainInSync} -eq 0 ]; then - command="" - result="FAIL PRECHECK - lncli getinfo shows 'synced_to_chain': false - wait until chain is sync " +echo +echo "# Precheck" # PRECHECK) check if chain is in sync +if [ $LNTYPE = cln ];then + BLOCKHEIGHT=$($bitcoincli_alias getblockchaininfo|grep blocks|awk '{print $2}'|cut -d, -f1) + CLHEIGHT=$($lightningcli_alias getinfo | jq .blockheight) + if [ $BLOCKHEIGHT -eq $CLHEIGHT ];then + chainOutSync=0 + else + chainOutSync=1 + fi +elif [ $LNTYPE = lnd ];then + chainOutSync=$($lncli_alias getinfo | grep '"synced_to_chain": false' -c) fi +if [ ${chainOutSync} -eq 1 ]; then + if [ $LNTYPE = cln ];then + echo "# FAIL PRECHECK - '${netprefix}lightning-cli getinfo' blockheight is different from '${netprefix}bitcoind getblockchaininfo' - wait until chain is sync " + elif [ $LNTYPE = lnd ];then + echo "# FAIL PRECHECK - ${netprefix}lncli getinfo shows 'synced_to_chain': false - wait until chain is sync " + fi + echo + echo "# PRESS ENTER to return to menu" + read key + exit 1 +else + echo "# OK - the chain is synced" +fi + +echo "#####################################" +echo "# Closing All Channels (EXPERIMENTAL)" +echo "#####################################" +echo +echo "# COMMAND LINE: " +echo $command +echo +echo "# RESULT:" # execute command if [ ${#command} -gt 0 ]; then - ${command} + if [ $LNTYPE = cln ];then + cln_closeall + elif [ $LNTYPE = lnd ];then + ${command} + fi fi - -echo "" -echo "OK - please recheck if channels really closed" + +echo +echo "# OK - please recheck if channels really closed" sleep 5 + +#TODO exits to CLI, not returning to menu \ No newline at end of file