Skip to content

Commit

Permalink
Merge pull request ElementsProject#188 from nepet/202305-fix-listpeers
Browse files Browse the repository at this point in the history
Fix `listpeers` response
  • Loading branch information
nepet committed Jun 9, 2023
2 parents 7a6e177 + af54063 commit 791fdff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
11 changes: 5 additions & 6 deletions clightning/clightning_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,10 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
for _, channel := range peer.Channels {
if c, ok := fundingChannels[channel.ShortChannelId]; ok {
peerSwapPeerChannels = append(peerSwapPeerChannels, &PeerSwapPeerChannel{
ChannelId: c.ShortChannelId,
LocalBalance: c.ChannelSatoshi,
RemoteBalance: uint64(c.ChannelTotalSatoshi - c.ChannelSatoshi),
LocalPercentage: float64(c.ChannelSatoshi) / float64(c.ChannelTotalSatoshi),
State: c.State,
ChannelId: c.ShortChannelId,
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
State: c.State,
})
}
}
Expand Down Expand Up @@ -1138,7 +1137,7 @@ type PeerSwapPeer struct {
Channels []*PeerSwapPeerChannel `json:"channels"`
AsSender *SwapStats `json:"sent,omitempty"`
AsReceiver *SwapStats `json:"received,omitempty"`
PaidFee uint64 `json:"total_fee_paid"`
PaidFee uint64 `json:"total_fee_paid,omitempty"`
}

// checkFeatures checks if a node runs the peerswap Plugin
Expand Down
16 changes: 16 additions & 0 deletions clightning/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -355,6 +356,20 @@ func ElementsFallback() Processor {
}
}

func CheckBitcoinRpcIsUrl() Processor {
return func(c *Config) (*Config, error) {
_, err := url.Parse(fmt.Sprintf("%s:%d", c.Bitcoin.RpcHost, c.Bitcoin.RpcPort))
if err != nil && strings.Contains(err.Error(), "first path segment in URL cannot contain colon") {
// We are missing a http or https in front of the rpc address.
if !strings.HasPrefix(c.Bitcoin.RpcHost, "http://") && !strings.HasPrefix(c.Bitcoin.RpcHost, "https://") {
c.Bitcoin.RpcHost = fmt.Sprintf("http://%s", c.Bitcoin.RpcHost)
return c, nil
}
}
return c, err
}
}

// BitcoinCookieConnect deflates a cookie file to override rpc user
// and password.
func BitcoinCookieConnect() Processor {
Expand Down Expand Up @@ -404,6 +419,7 @@ func GetConfig(client *ClightningClient) (*Config, error) {
Add(SetBitcoinNetwork(client)).
Add(BitcoinFallback()).
Add(ElementsFallback()).
Add(CheckBitcoinRpcIsUrl()).
Add(BitcoinCookieConnect()).
Add(ElementsCookieConnect())

Expand Down
2 changes: 1 addition & 1 deletion cmd/peerswap-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
return err
}
if !ok {
log.Debugf("Core-lighting version %s is not supported, min version is v%s", nodeInfo.Version, minClnVersion)
log.Infof("Core-lighting version %s is not supported, min version is v%s", nodeInfo.Version, minClnVersion)
return fmt.Errorf("Core-Lightning version %s is incompatible", nodeInfo.Version)
}

Expand Down
43 changes: 27 additions & 16 deletions contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ LND_CLI_BASE_ALIAS="lncli"
SETUP_CHANNEL_SIZE=10000000

start_docker_env() {
docker-compose -f .ci/docker/docker-compose.yml up -d --remove-orphans
docker compose -f .ci/docker/docker-compose.yml up -d --remove-orphans
}

stop_docker_env() {
docker-compose -f .ci/docker/docker-compose.yml down
docker compose -f .ci/docker/docker-compose.yml down
}

prefixwith() {
Expand All @@ -47,6 +47,7 @@ start_cln_node() {
local network="regtest"
local addr="127.0.0.1:$((7070 + ${id} * 101))"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -84,12 +85,11 @@ funder-fuzz-percent=0
EOF
fi

# Write alternate peerswap config file
Write alternate peerswap config file
prefixwith $prefix echo "writing alternate peerswap config file"
touch ${dir}/peerswap.conf
cat <<-EOF >"${dir}/peerswap.conf"
policypath="${dir}/policy.conf"
mkdir -p ${dir}/peerswap
touch ${dir}/peerswap/peerswap.conf
cat <<-EOF >"${dir}/peerswap/peerswap.conf"
[Liquid]
rpchost="http://127.0.0.1"
rpcport=${LIQUID_RPC_PORT}
Expand All @@ -100,8 +100,8 @@ EOF

# Write policy config
prefixwith $prefix echo "writing policy file"
touch ${dir}/policy.conf
echo "accept_all_peers=${ACCEPT_ALL_PEERS}" >> ${dir}/policy.conf
touch ${dir}/peerswap/policy.conf
echo "accept_all_peers=${ACCEPT_ALL_PEERS}" >> ${dir}/peerswap/policy.conf

# Start node
prefixwith $prefix echo "starting node"
Expand All @@ -112,8 +112,7 @@ EOF
${LIGHTNINGD}\
--lightning-dir="${dir}" \
--daemon \
--plugin="$(pwd)/out/peerswap" \
--peerswap-config-path="${dir}/peerswap.conf"
--plugin="$(pwd)/out/peerswap"
if [ $? -eq 1 ]; then
prefixwith $prefix echo "cln node crashed"
rm ${dir}/lightningd-${network}.pid
Expand All @@ -137,6 +136,7 @@ stop_cln_node() {
local prefix="cln-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -165,6 +165,7 @@ remove_cln_node() {
local prefix="cln-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand All @@ -184,6 +185,7 @@ setup_cln_network() {
local n_nodes=2
local network="regtest"
if [ -z ${1} ]; then
network="regtest"
else
network=${1}
fi
Expand Down Expand Up @@ -215,7 +217,7 @@ setup_cln_network() {
-rpcconnect=127.0.0.1 \
-rpcport=18443 \
getblockcount)
while; do
while true; do
blockheight1=$(eval ${LIGHTNING_CLI}-1 getinfo | jq -r .'blockheight')
blockheight2=$(eval ${LIGHTNING_CLI}-2 getinfo | jq -r .'blockheight')
if [ $blockheight1 -ge $blockcount ] && [ $blockheight2 -ge $blockcount ]; then
Expand All @@ -232,7 +234,7 @@ setup_cln_network() {
generate 12

# Await channel active
while; do
while true; do
local state=$(eval ${LIGHTNING_CLI}-1 listfunds | jq -r '.channels[0].state')
if [ "$state" = "CHANNELD_NORMAL" ]; then
prefixwith $prefix echo "Channel is active"
Expand Down Expand Up @@ -314,6 +316,7 @@ start_lnd_node() {
local rpc="127.0.0.1:$((10101 + ${id} * 101))"
local listen="127.0.0.1:$((10102 + ${id} * 101))"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -393,6 +396,7 @@ stop_lnd_node() {
local prefix="lnd-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -421,6 +425,7 @@ remove_lnd_node() {
local prefix="lnd-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand All @@ -440,6 +445,7 @@ setup_lnd_network() {
local n_nodes=2
local network="regtest"
if [ -z ${1} ]; then
network="regtest"
else
network=${1}
fi
Expand All @@ -456,7 +462,7 @@ setup_lnd_network() {
done

# Connect nodes
while; do
while true; do
prefixwith $prefix echo "Connecting nodes"
local to=$(eval ${LND_CLI}-2 getinfo | jq -r '.uris[0]')
eval ${LND_CLI}-1 connect $to > /dev/null
Expand All @@ -480,7 +486,7 @@ setup_lnd_network() {
-rpcconnect=127.0.0.1 \
-rpcport=18443 \
getblockcount)
while; do
while true; do
blockheight1=$(eval ${LND_CLI}-1 getinfo | jq -r .'block_height')
blockheight2=$(eval ${LND_CLI}-2 getinfo | jq -r .'block_height')
if [ $blockheight1 -ge $blockcount ] && [ $blockheight2 -ge $blockcount ]; then
Expand All @@ -497,7 +503,7 @@ setup_lnd_network() {
generate 12 $LND_SETUP_NETWORK

# Await channel active
while; do
while true; do
local active=$(eval ${LND_CLI}-1 listchannels | jq -r '.channels[0].active')
if [ "$active" = "true" ]; then
prefixwith $prefix echo "Channel is active"
Expand Down Expand Up @@ -560,6 +566,7 @@ fund_lnd_node() {
else
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand All @@ -584,6 +591,7 @@ start_peerswapd() {
local id=${1}
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -649,6 +657,7 @@ stop_peerswapd() {
local prefix="peerswap-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -676,6 +685,7 @@ remove_peerswapd() {
local prefix="peerswap-${id}"
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down Expand Up @@ -718,6 +728,7 @@ generate() {
fi
local network="regtest"
if [ -z ${2} ]; then
network="regtest"
else
network=${2}
fi
Expand Down

0 comments on commit 791fdff

Please sign in to comment.