Skip to content

Commit

Permalink
clightning: use listpeerchannels
Browse files Browse the repository at this point in the history
CLN 24.05 removed channels list from ListPeers response.
The listpeers command in the clightning package to use
the listpeerchannels function instead of listpeers.
  • Loading branch information
YusukeShimizu committed Sep 10, 2024
1 parent 879285c commit 15c6c4d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 68 deletions.
132 changes: 66 additions & 66 deletions clightning/clightning_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
return nil, ErrWaitingForReady
}

peers, err := l.cl.glightning.ListPeers()
res, err := l.cl.glightning.ListPeerChannels("")
if err != nil {
return nil, err
}
Expand All @@ -576,78 +576,78 @@ func (l *ListPeers) Call() (jrpc2.Result, error) {
}

peerSwappers := []*PeerSwapPeer{}
for _, peer := range peers {
if p, ok := polls[peer.Id]; ok {
swaps, err := l.cl.swaps.ListSwapsByPeer(peer.Id)
if err != nil {
return nil, err
}

var paidFees uint64
var ReceiverSwapsOut, ReceiverSwapsIn, ReceiverSatsOut, ReceiverSatsIn uint64
var SenderSwapsOut, SenderSwapsIn, SenderSatsOut, SenderSatsIn uint64
for _, s := range swaps {
// We only list successful swaps. They all end in an
// State_ClaimedPreimage state.
if s.Current == swap.State_ClaimedPreimage {
if s.Role == swap.SWAPROLE_SENDER {
paidFees += s.Data.OpeningTxFee
if s.Type == swap.SWAPTYPE_OUT {
SenderSwapsOut++
SenderSatsOut += s.Data.GetAmount()
} else {
SenderSwapsIn++
SenderSatsIn += s.Data.GetAmount()
}
for _, pc := range res.Channels {
p, ok := polls[pc.PeerId]
if !ok {
continue
}
swaps, err := l.cl.swaps.ListSwapsByPeer(pc.PeerId)
if err != nil {
return nil, err
}
var paidFees uint64
var ReceiverSwapsOut, ReceiverSwapsIn, ReceiverSatsOut, ReceiverSatsIn uint64
var SenderSwapsOut, SenderSwapsIn, SenderSatsOut, SenderSatsIn uint64
for _, s := range swaps {
// We only list successful swaps. They all end in an
// State_ClaimedPreimage state.
if s.Current == swap.State_ClaimedPreimage {
if s.Role == swap.SWAPROLE_SENDER {
paidFees += s.Data.OpeningTxFee
if s.Type == swap.SWAPTYPE_OUT {
SenderSwapsOut++
SenderSatsOut += s.Data.GetAmount()
} else {
if s.Type == swap.SWAPTYPE_OUT {
ReceiverSwapsOut++
ReceiverSatsOut += s.Data.GetAmount()
} else {
ReceiverSwapsIn++
ReceiverSatsIn += s.Data.GetAmount()
}
SenderSwapsIn++
SenderSatsIn += s.Data.GetAmount()
}
} else {
if s.Type == swap.SWAPTYPE_OUT {
ReceiverSwapsOut++
ReceiverSatsOut += s.Data.GetAmount()
} else {
ReceiverSwapsIn++
ReceiverSatsIn += s.Data.GetAmount()
}
}
}
}

peerSwapPeer := &PeerSwapPeer{
NodeId: peer.Id,
SwapsAllowed: p.PeerAllowed,
SupportedAssets: p.Assets,
AsSender: &SwapStats{
SwapsOut: SenderSwapsOut,
SwapsIn: SenderSwapsIn,
SatsOut: SenderSatsOut,
SatsIn: SenderSatsIn,
},
AsReceiver: &SwapStats{
SwapsOut: ReceiverSwapsOut,
SwapsIn: ReceiverSwapsIn,
SatsOut: ReceiverSatsOut,
SatsIn: ReceiverSatsIn,
},
PaidFee: paidFees,
}
channels, err := l.cl.glightning.ListChannelsBySource(peer.Id)
if err != nil {
return nil, err
}
peerSwapPeerChannels := []*PeerSwapPeerChannel{}
for _, channel := range channels {
if c, ok := fundingChannels[channel.ShortChannelId]; ok {
peerSwapPeerChannels = append(peerSwapPeerChannels, &PeerSwapPeerChannel{
ChannelId: c.ShortChannelId,
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
State: c.State,
})
}
peerSwapPeer := &PeerSwapPeer{
NodeId: pc.PeerId,
SwapsAllowed: p.PeerAllowed,
SupportedAssets: p.Assets,
AsSender: &SwapStats{
SwapsOut: SenderSwapsOut,
SwapsIn: SenderSwapsIn,
SatsOut: SenderSatsOut,
SatsIn: SenderSatsIn,
},
AsReceiver: &SwapStats{
SwapsOut: ReceiverSwapsOut,
SwapsIn: ReceiverSwapsIn,
SatsOut: ReceiverSatsOut,
SatsIn: ReceiverSatsIn,
},
PaidFee: paidFees,
}
channels, err := l.cl.glightning.ListChannelsBySource(pc.PeerId)
if err != nil {
return nil, err
}
peerSwapPeerChannels := []*PeerSwapPeerChannel{}
for _, channel := range channels {
if c, ok := fundingChannels[channel.ShortChannelId]; ok {
peerSwapPeerChannels = append(peerSwapPeerChannels, &PeerSwapPeerChannel{
ChannelId: c.ShortChannelId,
LocalBalance: c.OurAmountMilliSatoshi.MSat() / 1000,
RemoteBalance: (c.AmountMilliSatoshi.MSat() - c.OurAmountMilliSatoshi.MSat()) / 1000,
State: c.State,
})
}

peerSwapPeer.Channels = peerSwapPeerChannels
peerSwappers = append(peerSwappers, peerSwapPeer)
}
peerSwapPeer.Channels = peerSwapPeerChannels
peerSwappers = append(peerSwappers, peerSwapPeer)
}
return peerSwappers, nil
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ require (

// This fork contains some options we need to reconnect to lnd.
replace github.com/grpc-ecosystem/go-grpc-middleware => github.com/nepet/go-grpc-middleware v1.3.1-0.20220824133300-340e95267339

replace github.com/elementsproject/glightning => github.com/Impa10r/glightning v0.0.0-20240908125131-efaaf78adc9f
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Impa10r/glightning v0.0.0-20240908125131-efaaf78adc9f h1:ofsoNQsieulA4OE8YQaxCO9W6JX1snU7jy5IX+UaT3E=
github.com/Impa10r/glightning v0.0.0-20240908125131-efaaf78adc9f/go.mod h1:YAdIeSyx8VEhDCtEaGOJLmWNpPaQ3x4vYSAj9Vrppdo=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -188,8 +190,6 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6 h1:sE4tvxWw01v7K3MAHwKF2UF3xQbgy23PRURntuV1CkU=
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/elementsproject/glightning v0.0.0-20240224063423-55240d61b52a h1:xnVQmVqGmSs3m8zPQF4iYEYiUAmJx8MlT9vJ3lAaOjc=
github.com/elementsproject/glightning v0.0.0-20240224063423-55240d61b52a/go.mod h1:YAdIeSyx8VEhDCtEaGOJLmWNpPaQ3x4vYSAj9Vrppdo=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down

0 comments on commit 15c6c4d

Please sign in to comment.