lnrpc: add filters to forwardhistoryrequest

This commit adds incoming and outgoing channel ids filter to forwarding history request to filter events received/forwarded from/to a particular channel
This commit is contained in:
Shivam Chawla committed 2025-06-11 09:10:13 +05:30
1 parent 92a5d35cff
commit bfe1edf4ec
8 files changed
+1089 -815

No files matched your search

+48 -4
View File
@@ -256,6 +256,16 @@ type ForwardingEventQuery struct {
// NumMaxEvents is the max number of events to return.
NumMaxEvents uint32
// IncomingChanIds is the list of channels to filter HTLCs being
// received from a particular channel.
// If the list is empty, then it is ignored.
IncomingChanIDs fn.Set[uint64]
// OutgoingChanIds is the list of channels to filter HTLCs being
// forwarded to a particular channel.
// If the list is empty, then it is ignored.
OutgoingChanIDs fn.Set[uint64]
}
// ForwardingLogTimeSlice is the response to a forwarding query. It includes
@@ -323,9 +333,13 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice,
return nil
}
// If we're not yet past the user defined offset, then
// If no incoming or outgoing channel IDs were provided
// and we're not yet past the user defined offset, then
// we'll continue to seek forward.
if recordsToSkip > 0 {
if recordsToSkip > 0 &&
q.IncomingChanIDs.IsEmpty() &&
q.OutgoingChanIDs.IsEmpty() {
recordsToSkip--
continue
}
@@ -349,11 +363,41 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice,
return err
}
// Check if the incoming channel ID matches the
// filter criteria. Either no filtering is
// applied (IsEmpty), or the ID is explicitly
// included.
incomingMatch := q.IncomingChanIDs.IsEmpty() ||
q.IncomingChanIDs.Contains(
event.IncomingChanID.ToUint64(),
)
// Check if the outgoing channel ID matches the
// filter criteria. Either no filtering is
// applied (IsEmpty), or the ID is explicitly
// included.
outgoingMatch := q.OutgoingChanIDs.IsEmpty() ||
q.OutgoingChanIDs.Contains(
event.OutgoingChanID.ToUint64(),
)
// Skip this event if it doesn't match the
// filters.
if !incomingMatch || !outgoingMatch {
continue
}
// If we're not yet past the user defined offset
// then we'll continue to seek forward.
if recordsToSkip > 0 {
recordsToSkip--
continue
}
event.Timestamp = currentTime
resp.ForwardingEvents = append(
resp.ForwardingEvents, event,
resp.ForwardingEvents,
event,
)
recordOffset++
}
+131
View File
@@ -475,3 +475,134 @@ func writeOldFormatEvents(db *DB, events []ForwardingEvent) error {
return nil
})
}
// TestForwardingLogQueryChanIDs tests that querying the forwarding log with
// various combinations of incoming and/or outgoing channel IDs returns the
// correct subset of forwarding events.
func TestForwardingLogQueryChanIDs(t *testing.T) {
t.Parallel()
db, err := MakeTestDB(t)
require.NoError(t, err, "unable to make test db")
log := ForwardingLog{db: db}
initialTime := time.Unix(1234, 0)
endTime := initialTime
numEvents := 10
incomingChanIDs := []lnwire.ShortChannelID{
lnwire.NewShortChanIDFromInt(2001),
lnwire.NewShortChanIDFromInt(2002),
lnwire.NewShortChanIDFromInt(2003),
}
outgoingChanIDs := []lnwire.ShortChannelID{
lnwire.NewShortChanIDFromInt(3001),
lnwire.NewShortChanIDFromInt(3002),
lnwire.NewShortChanIDFromInt(3003),
}
events := make([]ForwardingEvent, numEvents)
for i := 0; i < numEvents; i++ {
events[i] = ForwardingEvent{
Timestamp: endTime,
IncomingChanID: incomingChanIDs[i%len(incomingChanIDs)],
OutgoingChanID: outgoingChanIDs[i%len(outgoingChanIDs)],
AmtIn: lnwire.MilliSatoshi(rand.Int63()),
AmtOut: lnwire.MilliSatoshi(rand.Int63()),
IncomingHtlcID: fn.Some(uint64(i)),
OutgoingHtlcID: fn.Some(uint64(i)),
}
endTime = endTime.Add(10 * time.Minute)
}
require.NoError(
t,
log.AddForwardingEvents(events),
"unable to add events",
)
tests := []struct {
name string
query ForwardingEventQuery
expected func(e ForwardingEvent) bool
}{
{
name: "only incomingChanIDs filter",
query: ForwardingEventQuery{
StartTime: initialTime,
EndTime: endTime,
IncomingChanIDs: fn.NewSet(
incomingChanIDs[0].ToUint64(),
incomingChanIDs[1].ToUint64(),
),
IndexOffset: 0,
NumMaxEvents: 10,
},
expected: func(e ForwardingEvent) bool {
return e.IncomingChanID == incomingChanIDs[0] ||
e.IncomingChanID == incomingChanIDs[1]
},
},
{
name: "only outgoingChanIDs filter",
query: ForwardingEventQuery{
StartTime: initialTime,
EndTime: endTime,
OutgoingChanIDs: fn.NewSet(
outgoingChanIDs[0].ToUint64(),
outgoingChanIDs[1].ToUint64(),
),
IndexOffset: 0,
NumMaxEvents: 10,
},
expected: func(e ForwardingEvent) bool {
return e.OutgoingChanID == outgoingChanIDs[0] ||
e.OutgoingChanID == outgoingChanIDs[1]
},
},
{
name: "incoming and outgoingChanIDs filter",
query: ForwardingEventQuery{
StartTime: initialTime,
EndTime: endTime,
IncomingChanIDs: fn.NewSet(
incomingChanIDs[0].ToUint64(),
incomingChanIDs[1].ToUint64(),
),
OutgoingChanIDs: fn.NewSet(
outgoingChanIDs[0].ToUint64(),
outgoingChanIDs[1].ToUint64(),
),
IndexOffset: 0,
NumMaxEvents: 10,
},
expected: func(e ForwardingEvent) bool {
return e.IncomingChanID ==
incomingChanIDs[0] ||
e.IncomingChanID ==
incomingChanIDs[1] ||
e.OutgoingChanID ==
outgoingChanIDs[0] ||
e.OutgoingChanID ==
outgoingChanIDs[1]
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := log.Query(tc.query)
require.NoError(t, err, "query failed")
expected := make([]ForwardingEvent, 0)
for _, e := range events {
if tc.expected(e) {
expected = append(expected, e)
}
}
require.Equal(t, expected, result.ForwardingEvents)
})
}
}