From 0cb52111a8347103a48d45bd7c602b49a9571cf7 Mon Sep 17 00:00:00 2001 From: aftermath2 Date: Mon, 22 Dec 2025 19:46:29 +0000 Subject: [PATCH] Use SCID in forwarding events operations --- agent/local/channel.go | 21 +++++++++++++-------- agent/local/channel_test.go | 4 +++- agent/local/node_test.go | 2 +- lightning/lightning.go | 8 ++++---- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/agent/local/channel.go b/agent/local/channel.go index c472ba8..1fc313e 100644 --- a/agent/local/channel.go +++ b/agent/local/channel.go @@ -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 ) @@ -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)) @@ -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) @@ -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 } @@ -119,7 +122,9 @@ 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 @@ -127,7 +132,7 @@ func getForwardsInfo(channel *lnrpc.Channel, forwards []*lnrpc.ForwardingEvent) fees += forward.FeeMsat } - if forward.ChanIdOut == channel.ChanId { + if forward.ChanIdOut == scid { numForwards++ forwardsAmount += forward.AmtOutMsat fees += forward.FeeMsat diff --git a/agent/local/channel_test.go b/agent/local/channel_test.go index cde1f53..d9838a3 100644 --- a/agent/local/channel_test.go +++ b/agent/local/channel_test.go @@ -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, diff --git a/agent/local/node_test.go b/agent/local/node_test.go index b1f5e07..13a3d01 100644 --- a/agent/local/node_test.go +++ b/agent/local/node_test.go @@ -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, diff --git a/lightning/lightning.go b/lightning/lightning.go index 74dc255..903c95f 100644 --- a/lightning/lightning.go +++ b/lightning/lightning.go @@ -56,7 +56,7 @@ type Client interface { 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 @@ -303,13 +303,13 @@ func (c *client) ListChannels(ctx context.Context) ([]*lnrpc.Channel, error) { // 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 }