c-lightning-REST improvements, balance display fix (#4005)

* improve handling aliases when no network given
* clrest install to use absolute paths
* fix cln balance display
* remove misplaced line
* improve jq expressions
* convert msat balances to sat

discussed in: https://github.com/raspiblitz/raspiblitz/issues/3837
This commit is contained in:
openoms 2023-07-29 13:48:11 +02:00 committed by GitHub
parent 237570af44
commit 8c87bee0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 65 deletions

View File

@ -2,16 +2,16 @@
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "monitor and troubleshot the c-lightning network"
echo "cl.monitor.sh [mainnet|testnet|signet] status"
echo "cl.monitor.sh [mainnet|testnet|signet] config"
echo "cl.monitor.sh [mainnet|testnet|signet] info"
echo "cl.monitor.sh [mainnet|testnet|signet] wallet"
exit 1
echo "monitor and troubleshot the c-lightning network"
echo "cl.monitor.sh [mainnet|testnet|signet] status"
echo "cl.monitor.sh [mainnet|testnet|signet] config"
echo "cl.monitor.sh [mainnet|testnet|signet] info"
echo "cl.monitor.sh [mainnet|testnet|signet] wallet"
exit 1
fi
# check if started with sudo
if [ "$EUID" -ne 0 ]; then
if [ "$EUID" -ne 0 ]; then
echo "error='run as root'"
exit 1
fi
@ -51,7 +51,7 @@ if [ "$2" = "status" ]; then
cl_locked="0"
cl_error_short=""
cl_error_full=""
if [ "${cl_running}" = "0" ]; then
# check if error because wallet is locked
# the next release will have soecific error code for decryption error
@ -59,23 +59,24 @@ if [ "$2" = "status" ]; then
source /mnt/hdd/raspiblitz.conf
# password file is on the disk if encrypted and auto-unlock is enabled
passwordFile="/dev/shm/.${netprefix}cl.pw"
if grep -Eq "${netprefix}clEncryptedHSM=on" /mnt/hdd/raspiblitz.conf;then
if grep -Eq "${netprefix}clAutoUnlock=on" /mnt/hdd/raspiblitz.conf;then
if grep -Eq "${netprefix}clEncryptedHSM=on" /mnt/hdd/raspiblitz.conf; then
if grep -Eq "${netprefix}clAutoUnlock=on" /mnt/hdd/raspiblitz.conf; then
passwordFile=/home/bitcoin/.${netprefix}cl.pw
fi
fi
clError=$(sudo journalctl -n5 -u ${netprefix}lightningd)
# cases from 'cl.hsmtool.sh unlock'
if \
[ "$(eval echo \$${netprefix}clEncryptedHSM)" = "on" ] && [ ! -f $passwordFile ] || \
[ $(echo "${clError}" | \
grep -c 'encrypted-hsm: Could not read pass from stdin.') -gt 0 ] || \
[ $(echo "${clError}" | \
grep -c 'hsm_secret is encrypted, you need to pass the --encrypted-hsm startup option.') -gt 0 ] || \
[ $(echo "${clError}" | \
grep -c 'Wrong password for encrypted hsm_secret.') -gt 0 ]; then
if
[ "$(eval echo \$${netprefix}clEncryptedHSM)" = "on" ] && [ ! -f $passwordFile ] ||
[ $(echo "${clError}" |
grep -c 'encrypted-hsm: Could not read pass from stdin.') -gt 0 ] ||
[ $(echo "${clError}" |
grep -c 'hsm_secret is encrypted, you need to pass the --encrypted-hsm startup option.') -gt 0 ] ||
[ $(echo "${clError}" |
grep -c 'Wrong password for encrypted hsm_secret.') -gt 0 ]
then
# signal wallet locked
cl_locked="1"
# dont report it as error
@ -108,13 +109,13 @@ if [ "$2" = "status" ]; then
# check results if proof for online
else
cl_ready="1"
connections=$( echo "${winData}" | grep "num_peers\"" | tr -cd '[[:digit:]]')
connections=$(echo "${winData}" | grep "num_peers\"" | tr -cd '[[:digit:]]')
if [ "${connections}" != "" ] && [ "${connections}" != "0" ]; then
cl_online="1"
fi
fi
fi
fi
# print results
echo "ln_cl_version='${cl_version}'"
@ -126,7 +127,7 @@ if [ "$2" = "status" ]; then
echo "ln_cl_error_full='${cl_error_full}'"
exit 0
fi
fi
######################################################
# CONFIG
@ -146,7 +147,6 @@ if [ "$2" = "config" ]; then
exit 1
fi
######################################################
# INFO
######################################################
@ -178,9 +178,9 @@ if [ "$2" = "info" ]; then
cl_channels_pending=$(echo "${ln_getInfo}" | jq -r '.num_pending_channels')
cl_channels_active=$(echo "${ln_getInfo}" | jq -r '.num_active_channels')
cl_channels_inactive=$(echo "${ln_getInfo}" | jq -r '.num_inactive_channels')
cl_channels_total=$(( cl_channels_pending + cl_channels_active + cl_channels_inactive ))
cl_channels_total=$((cl_channels_pending + cl_channels_active + cl_channels_inactive))
cl_peers=$(echo "${ln_getInfo}" | jq -r '.num_peers')
cl_fees_collected_msat=$(echo "${ln_getInfo}" | jq -r '.fees_collected_msat')
cl_fees_collected_msat=$(echo "${ln_getInfo}" | jq -r '.fees_collected_msat')
# calculate with cached value if c-lightning is fully synced
source <(/home/admin/_cache.sh get ${blockchainHeightKey})
@ -195,7 +195,7 @@ if [ "$2" = "info" ]; then
cl_sync_progress=$(echo "scale=2; $cl_sync_height*100/$blockheight" | bc)
# needs to be at least "two blocks behind" to be considered not synced
blockheight=$(($blockheight - 1))
if [ ${blockheight} -gt ${cl_sync_height} ];then
if [ ${blockheight} -gt ${cl_sync_height} ]; then
cl_sync_chain=0
else
cl_sync_chain=1
@ -228,7 +228,7 @@ if [ "$2" = "info" ]; then
echo "ln_cl_recovery_mode='${cl_recovery_mode}'"
echo "ln_cl_recovery_done='${cl_recovery_done}'"
exit 0
fi
######################################################
@ -241,7 +241,6 @@ if [ "$2" = "wallet" ]; then
# /usr/local/bin/lightning-cli --lightning-dir=/home/bitcoin/.lightning --conf=/home/bitcoin/.lightning/config listfunds
# get data
sudo -u bitcoin
command="sudo -u bitcoin $lightningcli_alias listfunds"
cl_listfunds=$(${command} 2>/dev/null)
if [ "${cl_listfunds}" == "" ]; then
@ -251,33 +250,38 @@ if [ "$2" = "wallet" ]; then
fi
ln_walletbalance=0
for i in $(echo "$cl_listfunds" | jq .outputs[] | jq 'select(.status=="confirmed")' | grep value | awk '{print $2}' | cut -d, -f1);do
ln_walletbalance=$((ln_walletbalance+i))
for i in $(echo "$cl_listfunds" | jq '.outputs[] | select(.status == "confirmed").amount_msat'); do
sat=$((i / 1000))
ln_walletbalance=$((ln_walletbalance + sat))
done
ln_walletbalance_wait=0
for i in $(echo "$cl_listfunds" | jq .outputs[] | jq 'select(.status=="unconfirmed")' | grep value | awk '{print $2}' | cut -d, -f1);do
ln_walletbalance_wait=$((ln_walletbalance_wait+i))
for i in $(echo "$cl_listfunds" | jq '.outputs[] | select(.status == "unconfirmed").amount_msat'); do
sat=$((i / 1000))
ln_walletbalance_wait=$((ln_walletbalance_wait + sat))
done
ln_closedchannelbalance=0
for i in $(echo "$cl_listfunds" | jq .channels[] | jq 'select(.state=="ONCHAIN")' | grep channel_sat | awk '{print $2}' | cut -d, -f1);do
ln_closedchannelbalance=$((ln_closedchannelbalance+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | select(.state=="ONCHAIN") | .our_amount_msat'); do
sat=$((i / 1000))
ln_closedchannelbalance=$((ln_closedchannelbalance + sat))
done
ln_pendingonchain=$((ln_walletbalance_wait+ln_closedchannelbalance))
ln_pendingonchain=$((ln_walletbalance_wait + ln_closedchannelbalance))
if [ ${#ln_pendingonchain} -gt 0 ]; then ln_pendingonchain="(+${ln_pendingonchain})"; fi
ln_channelbalance=0
for i in $(echo "$cl_listfunds" |jq .channels[]|jq 'select(.state=="CHANNELD_NORMAL")'|grep channel_sat|awk '{print $2}'|cut -d, -f1);do
ln_channelbalance=$((ln_channelbalance+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | select(.state=="CHANNELD_NORMAL") | .our_amount_msat'); do
sat=$((i / 1000))
ln_channelbalance=$((ln_channelbalance + sat))
done
if [ ${#ln_channelbalance} -eq 0 ];then
if [ ${#ln_channelbalance} -eq 0 ]; then
ln_channelbalance=0
fi
ln_channelbalance_all=0
for i in $(echo "$cl_listfunds" |jq .channels[]|grep channel_sat|awk '{print $2}'|cut -d, -f1);do
ln_channelbalance_all=$((ln_channelbalance_all+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | .our_amount_msat'); do
sat=$((i / 1000))
ln_channelbalance_all=$((ln_channelbalance_all + sat))
done
ln_channelbalance_pending=$((ln_channelbalance_all-ln_channelbalance-ln_closedchannelbalance))
ln_channelbalance_pending=$((ln_channelbalance_all - ln_channelbalance - ln_closedchannelbalance))
if [ ${#ln_channelbalance_pending} -gt 0 ]; then ln_channelbalance_pending=" (+${ln_channelbalance_pending})"; fi
# print data
echo "ln_cl_wallet_onchain_balance='${ln_walletbalance//[^0-9.]/}'"
echo "ln_cl_wallet_onchain_pending='${ln_pendingonchain//[^0-9.]/}'"

View File

@ -142,7 +142,7 @@ if [ "$1" = on ]; then
if [ ! -f /home/bitcoin/c-lightning-REST/cl-rest.js ]; then
cd /home/bitcoin || exit 1
sudo -u bitcoin git clone https://github.com/saubyk/c-lightning-REST
cd c-lightning-REST || exit 1
cd /home/bitcoin/c-lightning-REST || exit 1
sudo -u bitcoin git reset --hard $CLRESTVERSION
sudo -u bitcoin /home/admin/config.scripts/blitz.git-verify.sh \
@ -152,8 +152,11 @@ if [ "$1" = on ]; then
sudo -u bitcoin npm install
fi
cd /home/bitcoin/c-lightning-REST || exit 1
sudo -u bitcoin mkdir ${CLNETWORK}
# copy clrest to a CLNETWORK subdir to make parallel networks possible
sudo -u bitcoin mkdir /home/bitcoin/c-lightning-REST/${CLNETWORK}
sudo -u bitcoin cp -r /home/bitcoin/c-lightning-REST/* \
/home/bitcoin/c-lightning-REST/${CLNETWORK}
echo "
{
\"PORT\": ${portprefix}6100,
@ -162,12 +165,7 @@ if [ "$1" = on ]; then
\"EXECMODE\": \"production\",
\"LNRPCPATH\": \"/home/bitcoin/.lightning/${CLNETWORK}/lightning-rpc\",
\"RPCCOMMANDS\": [\"*\"]
}" | sudo -u bitcoin tee ./${CLNETWORK}/cl-rest-config.json
# copy clrest to a CLNETWORK subdir to make parallel networks possible
sudo -u bitcoin mkdir /home/bitcoin/c-lightning-REST/${CLNETWORK}
sudo -u bitcoin cp -r /home/bitcoin/c-lightning-REST/* \
/home/bitcoin/c-lightning-REST/${CLNETWORK}
}" | sudo -u bitcoin tee /home/bitcoin/c-lightning-REST/${CLNETWORK}/cl-rest-config.json || exit 1
echo "
# systemd unit for c-lightning-REST for ${CHAIN}

View File

@ -1,7 +1,7 @@
#!/bin/bash
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ];then
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "# Usage:"
echo "# source <(/home/admin/config.scripts/network.aliases.sh getvars <lnd|cl> <mainnet|testnet|signet>)"
echo "# if no values given uses the default values from the raspiblitz.conf"
@ -18,20 +18,20 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ];then
exit 1
fi
if [ "$1" = getvars ];then
if [ "$1" = getvars ]; then
source /home/admin/raspiblitz.info
source /mnt/hdd/raspiblitz.conf 2>/dev/null
if [ ${#network} -eq 0 ];then
if [ ${#network} -eq 0 ]; then
network=bitcoin
fi
# LNTYPE is: lnd | cl
if [ $# -gt 1 ];then
if [ $# -gt 1 ]; then
LNTYPE=$2
else
if [ ${#lightning} -gt 0 ];then
if [ ${#lightning} -gt 0 ]; then
LNTYPE=${lightning}
else
LNTYPE=lnd
@ -44,30 +44,30 @@ if [ "$1" = getvars ];then
chain=main
fi
# CHAIN is: signet | testnet | mainnet
if [ $# -gt 2 ]&&[ "$3" != net ]&&[ "$3" != "" ];then
if [ $# -gt 2 ] && { [ "$3" = "signet" ] || [ "$3" = "testnet" ] || [ "$3" = "mainnet" ]; }; then
CHAIN=$3
chain=${CHAIN::-3}
else
CHAIN=${chain}net
fi
echo "CHAIN=${chain}net"
echo "CHAIN=${CHAIN}"
echo "chain=${chain}"
# netprefix is: "" | t | s
# portprefix is: "" | 1 | 3
# L2rpcportmod is: 0 | 1 | 3
# zmqprefix is: 28 | 21 | 23
if [ "${chain}" == "main" ];then
if [ "${chain}" == "main" ]; then
netprefix=""
L2rpcportmod=0
portprefix=""
zmqprefix=28
elif [ "${chain}" == "test" ];then
elif [ "${chain}" == "test" ]; then
netprefix="t"
L2rpcportmod=1
portprefix=1
zmqprefix=21
elif [ "${chain}" == "sig" ];then
elif [ "${chain}" == "sig" ]; then
netprefix="s"
L2rpcportmod=3
portprefix=3
@ -78,9 +78,9 @@ if [ "$1" = getvars ];then
echo "L2rpcportmod=${L2rpcportmod}"
echo "zmqprefix=${zmqprefix}"
if [ "${LNTYPE}" == "cl" ];then
if [ "${LNTYPE}" == "cl" ]; then
# CLNETWORK is: bitcoin / signet / testnet
if [ "${chain}" == "main" ];then
if [ "${chain}" == "main" ]; then
CLNETWORK=${network}
else
CLNETWORK=${chain}net
@ -100,7 +100,7 @@ if [ "$1" = getvars ];then
fi
# typeprefix is: "" | c
if [ "${LNTYPE}" == "lnd" ];then
if [ "${LNTYPE}" == "lnd" ]; then
typeprefix=''
lndConfFile="/mnt/hdd/lnd/${netprefix}lnd.conf"
fi