Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions agent/local/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/aftermath2/hydrus/lightning"

"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
)

const (
oneDay = time.Hour * 24
oneWeek = oneDay * 7
oneMonth = oneDay * 30
)

Expand Down Expand Up @@ -48,10 +48,6 @@ func getChannels(
peers []*lnrpc.Peer,
) (Channels, error) {
oneMonthAgo := uint64(time.Now().Add(-oneMonth).Unix())
forwards, err := ListForwards(ctx, lnd, 0, oneMonthAgo, 0)
if err != nil {
return Channels{}, err
}

heuristics := NewHeuristics(closeWeights)
chans := make([]Channel, 0, len(channels))
Expand All @@ -61,6 +57,11 @@ func getChannels(
continue
}

forwards, err := ListForwards(ctx, lnd, channel.ChanId, oneMonthAgo, 0)
if err != nil {
return Channels{}, err
}

numForwards, forwardsAmount, fees := getForwardsInfo(channel, forwards)
pingTime, flapCount := getPeerInfo(channel, peers)

Expand Down Expand Up @@ -97,8 +98,10 @@ func ListForwards(
events := make([]*lnrpc.ForwardingEvent, 0)
now := uint64(time.Now().Unix())

scid := lnwire.NewShortChanIDFromInt(channelID).ToUint64()

for {
forwards, err := lnd.ListForwards(ctx, channelID, startTime, now, offset)
forwards, err := lnd.ListForwards(ctx, scid, startTime, now, offset)
if err != nil {
return nil, err
}
Expand All @@ -119,15 +122,17 @@ func ListForwards(
func getForwardsInfo(channel *lnrpc.Channel, forwards []*lnrpc.ForwardingEvent) (uint64, uint64, uint64) {
var numForwards, forwardsAmount, fees uint64
for _, forward := range forwards {
if forward.ChanIdIn == channel.ChanId {
scid := lnwire.NewShortChanIDFromInt(channel.ChanId).ToUint64()

if forward.ChanIdIn == scid {
numForwards++
forwardsAmount += forward.AmtInMsat
// Even though we collect fees in the other part of the circuit, we are counting fees for this
// channel as well for opening it
fees += forward.FeeMsat
}

if forward.ChanIdOut == channel.ChanId {
if forward.ChanIdOut == scid {
numForwards++
forwardsAmount += forward.AmtOutMsat
fees += forward.FeeMsat
Expand Down
4 changes: 3 additions & 1 deletion agent/local/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func TestGetChannels(t *testing.T) {
LastOffsetIndex: 3,
}

lndMock.On("ListForwards", ctx, uint64(0), mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil)
lndMock.On("ListForwards", ctx, ch1.ChanId, mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil).Once()
lndMock.On("ListForwards", ctx, ch2.ChanId, mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil).Once()
lndMock.On("ListForwards", ctx, ch3.ChanId, mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil).Once()

weights := config.CloseWeights{
Capacity: 0.2,
Expand Down
2 changes: 1 addition & 1 deletion agent/local/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestGetNode(t *testing.T) {
lndMock.On("ListPeers", ctx).Return(peersResp, nil)
lndMock.On("ClosedChannels", ctx).Return(closedChannelsResp, nil)
lndMock.On("EstimateTxFee", ctx, config.TargetConf).Return(feeResp, nil)
lndMock.On("ListForwards", ctx, uint64(0), mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil)
lndMock.On("ListForwards", ctx, channelID, mock.Anything, mock.Anything, uint32(0)).Return(forwardsResp, nil)

expectedNode := local.Node{
PublicKey: infoResp.IdentityPubkey,
Expand Down
8 changes: 4 additions & 4 deletions lightning/lightning.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lightning

Check warning on line 1 in lightning/lightning.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

import (
"context"
Expand Down Expand Up @@ -56,7 +56,7 @@
GetChanInfo(ctx context.Context, channelID uint64) (*lnrpc.ChannelEdge, error)
GetInfo(ctx context.Context) (*lnrpc.GetInfoResponse, error)
ListChannels(ctx context.Context) ([]*lnrpc.Channel, error)
ListForwards(ctx context.Context, channelID uint64, startTime, endTime uint64, indexOffset uint32) (*lnrpc.ForwardingHistoryResponse, error)
ListForwards(ctx context.Context, scid uint64, startTime, endTime uint64, indexOffset uint32) (*lnrpc.ForwardingHistoryResponse, error)
ListPeers(ctx context.Context) ([]*lnrpc.Peer, error)
QueryRoute(ctx context.Context, publicKey string) (*lnrpc.QueryRoutesResponse, error)
UpdateChannelPolicy(ctx context.Context, channelPoint string, baseFeeMsat, feeRatePPM, maxHTLCMsat, timeLockDelta uint64) error
Expand Down Expand Up @@ -303,13 +303,13 @@
// ListForwards returns list of successful HTLC forwarding events.
func (c *client) ListForwards(
ctx context.Context,
channelID uint64,
scid uint64,
startTime,
endTime uint64,
indexOffset uint32,
) (*lnrpc.ForwardingHistoryResponse, error) {
channelIDs := []uint64{channelID}
if channelID == 0 {
channelIDs := []uint64{scid}
if scid == 0 {
channelIDs = nil
}

Expand Down
Loading