Skip to content

Commit ab53876

Browse files
committed
fixup! lnrpc: add filters to forwardhistoryrequest
1 parent fafcad3 commit ab53876

File tree

4 files changed

+88
-106
lines changed

4 files changed

+88
-106
lines changed

channeldb/forwarding_log.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -303,41 +303,41 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, e
303303
}
304304

305305
// Check if the incoming channel ID matches the
306-
// filter criteria.
307-
// Either no filtering is applied (IsEmpty), or
308-
// the ID is explicitly included.
306+
// filter criteria. Either no filtering is
307+
// applied (IsEmpty), or the ID is explicitly
308+
// included.
309309
incomingMatch := q.IncomingChanIDs.IsEmpty() ||
310310
q.IncomingChanIDs.Contains(
311311
event.IncomingChanID.ToUint64(),
312312
)
313313

314314
// Check if the outgoing channel ID matches the
315-
// filter criteria.
316-
// Either no filtering is applied (IsEmpty), or
317-
// the ID is explicitly included.
315+
// filter criteria. Either no filtering is
316+
// applied (IsEmpty), or the ID is explicitly
317+
// included.
318318
outgoingMatch := q.OutgoingChanIDs.IsEmpty() ||
319319
q.OutgoingChanIDs.Contains(
320320
event.OutgoingChanID.ToUint64(),
321321
)
322322

323-
// If both conditions are met, then we'll add
324-
// the event to our return payload.
325-
if incomingMatch && outgoingMatch {
326-
// If we're not yet past the user
327-
// defined offset , then we'll continue
328-
// to seek forward.
329-
if recordsToSkip > 0 {
330-
recordsToSkip--
331-
continue
332-
}
333-
334-
event.Timestamp = currentTime
335-
resp.ForwardingEvents = append(
336-
resp.ForwardingEvents,
337-
event,
338-
)
339-
recordOffset++
323+
// Skip this event if it doesn't match the
324+
// filters.
325+
if !incomingMatch || !outgoingMatch {
326+
continue
327+
}
328+
// If we're not yet past the user defined offset
329+
// then we'll continue to seek forward.
330+
if recordsToSkip > 0 {
331+
recordsToSkip--
332+
continue
340333
}
334+
335+
event.Timestamp = currentTime
336+
resp.ForwardingEvents = append(
337+
resp.ForwardingEvents,
338+
event,
339+
)
340+
recordOffset++
341341
}
342342
}
343343

channeldb/forwarding_log_test.go

Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ func TestForwardingLogStoreEvent(t *testing.T) {
362362
}
363363
}
364364

365-
// TestForwardingLogQueryIncomingChanIDs tests that querying the forwarding log
366-
// using only incoming channel IDs returns the correct subset of events.
367-
func TestForwardingLogQueryIncomingChanIDs(t *testing.T) {
365+
// TestForwardingLogQueryIncomingOrOutgoingChanIDs tests that querying the
366+
// forwarding log using only incoming channel IDs or outgoing channel IDs
367+
// returns the correct subset of events.
368+
func TestForwardingLogQueryIncomingOrOutgoingChanIDs(t *testing.T) {
368369
t.Parallel()
369370

370371
// Set up a test database.
@@ -378,7 +379,7 @@ func TestForwardingLogQueryIncomingChanIDs(t *testing.T) {
378379
initialTime := time.Unix(1234, 0)
379380
endTime := time.Unix(1234, 0)
380381

381-
// Create 10 random events with varying incoming ChanIDs.
382+
// Create 10 random events with varying incoming and outgoing ChanIDs.
382383
numEvents := 10
383384
events := make([]ForwardingEvent, numEvents)
384385
incomingChanIDs := []lnwire.ShortChannelID{
@@ -387,111 +388,95 @@ func TestForwardingLogQueryIncomingChanIDs(t *testing.T) {
387388
lnwire.NewShortChanIDFromInt(2003),
388389
}
389390

391+
outgoingChanIDs := []lnwire.ShortChannelID{
392+
lnwire.NewShortChanIDFromInt(3001),
393+
lnwire.NewShortChanIDFromInt(3002),
394+
lnwire.NewShortChanIDFromInt(3003),
395+
}
396+
390397
for i := 0; i < numEvents; i++ {
391398
events[i] = ForwardingEvent{
392399
Timestamp: endTime,
393400
IncomingChanID: incomingChanIDs[i%len(incomingChanIDs)],
401+
OutgoingChanID: outgoingChanIDs[i%len(outgoingChanIDs)],
394402
AmtIn: lnwire.MilliSatoshi(rand.Int63()),
395403
AmtOut: lnwire.MilliSatoshi(rand.Int63()),
396404
}
397405
endTime = endTime.Add(time.Minute * 10)
398406
}
399407

400408
// Add events to the database.
401-
require.NoError(t, log.AddForwardingEvents(events),
402-
"unable to add events")
409+
require.NoError(
410+
t,
411+
log.AddForwardingEvents(events),
412+
"unable to add events",
413+
)
403414

404415
// Query with multiple incoming channel IDs.
405-
eventQuery := ForwardingEventQuery{
416+
incomingEventQuery := ForwardingEventQuery{
406417
StartTime: initialTime,
407418
EndTime: endTime,
408-
IncomingChanIDs: fn.NewSet(incomingChanIDs[0].ToUint64(),
409-
incomingChanIDs[1].ToUint64()),
419+
IncomingChanIDs: fn.NewSet(
420+
incomingChanIDs[0].ToUint64(),
421+
incomingChanIDs[1].ToUint64(),
422+
),
410423
IndexOffset: 0,
411424
NumMaxEvents: 10,
412425
}
413-
timeSlice, err := log.Query(eventQuery)
426+
incomingEventsTimeSlice, err := log.Query(incomingEventQuery)
414427
require.NoError(t, err, "unable to query for events")
415428

416429
// Verify that only events with the specified incomingChanIDs are
417430
// returned.
418-
expectedEvents := []ForwardingEvent{}
431+
expectedIncomingEvents := []ForwardingEvent{}
419432
for _, e := range events {
420433
if e.IncomingChanID == incomingChanIDs[0] ||
421434
e.IncomingChanID == incomingChanIDs[1] {
422435

423-
expectedEvents = append(expectedEvents, e)
424-
}
425-
}
426-
427-
require.Equal(t, expectedEvents, timeSlice.ForwardingEvents,
428-
"unexpected events returned")
429-
}
430-
431-
// TestForwardingLogQueryOutgoingChanIDs tests that querying the forwarding log
432-
// using only outgoing channel IDs returns the correct subset of events.
433-
func TestForwardingLogQueryOutgoingChanIDs(t *testing.T) {
434-
t.Parallel()
435-
436-
// Set up a test database.
437-
db, err := MakeTestDB(t)
438-
require.NoError(t, err, "unable to make test db")
439-
440-
log := ForwardingLog{
441-
db: db,
442-
}
443-
444-
initialTime := time.Unix(1234, 0)
445-
endTime := time.Unix(1234, 0)
446-
447-
// Create 10 random events with varying outgoing ChanIDs.
448-
numEvents := 10
449-
events := make([]ForwardingEvent, numEvents)
450-
outgoingChanIDs := []lnwire.ShortChannelID{
451-
lnwire.NewShortChanIDFromInt(2001),
452-
lnwire.NewShortChanIDFromInt(2002),
453-
lnwire.NewShortChanIDFromInt(2003),
454-
}
455-
456-
for i := 0; i < numEvents; i++ {
457-
events[i] = ForwardingEvent{
458-
Timestamp: endTime,
459-
OutgoingChanID: outgoingChanIDs[i%len(outgoingChanIDs)],
460-
AmtIn: lnwire.MilliSatoshi(rand.Int63()),
461-
AmtOut: lnwire.MilliSatoshi(rand.Int63()),
436+
expectedIncomingEvents = append(
437+
expectedIncomingEvents, e,
438+
)
462439
}
463-
endTime = endTime.Add(time.Minute * 10)
464440
}
465441

466-
// Add events to the database.
467-
require.NoError(t, log.AddForwardingEvents(events),
468-
"unable to add events")
442+
require.Equal(
443+
t,
444+
expectedIncomingEvents,
445+
incomingEventsTimeSlice.ForwardingEvents,
446+
)
469447

470448
// Query with multiple outgoing channel IDs.
471-
eventQuery := ForwardingEventQuery{
449+
outgoingEventQuery := ForwardingEventQuery{
472450
StartTime: initialTime,
473451
EndTime: endTime,
474-
OutgoingChanIDs: fn.NewSet(outgoingChanIDs[0].ToUint64(),
475-
outgoingChanIDs[1].ToUint64()),
452+
OutgoingChanIDs: fn.NewSet(
453+
outgoingChanIDs[0].ToUint64(),
454+
outgoingChanIDs[1].ToUint64(),
455+
),
476456
IndexOffset: 0,
477457
NumMaxEvents: 10,
478458
}
479-
timeSlice, err := log.Query(eventQuery)
459+
outgoingEventsTimeSlice, err := log.Query(outgoingEventQuery)
480460
require.NoError(t, err, "unable to query for events")
481461

482462
// Verify that only events with the specified outgoingChanIDs are
483463
// returned.
484-
expectedEvents := []ForwardingEvent{}
464+
expectedOutgoingEvents := []ForwardingEvent{}
485465
for _, e := range events {
486466
if e.OutgoingChanID == outgoingChanIDs[0] ||
487467
e.OutgoingChanID == outgoingChanIDs[1] {
488468

489-
expectedEvents = append(expectedEvents, e)
469+
expectedOutgoingEvents = append(
470+
expectedOutgoingEvents, e,
471+
)
490472
}
491473
}
492474

493-
require.Equal(t, expectedEvents, timeSlice.ForwardingEvents,
494-
"unexpected events returned")
475+
require.Equal(
476+
t,
477+
expectedOutgoingEvents,
478+
outgoingEventsTimeSlice.ForwardingEvents,
479+
)
495480
}
496481

497482
// TestForwardingLogQueryIncomingAndOutgoingChanIDs tests that querying the
@@ -545,10 +530,14 @@ func TestForwardingLogQueryIncomingAndOutgoingChanIDs(t *testing.T) {
545530
eventQuery := ForwardingEventQuery{
546531
StartTime: initialTime,
547532
EndTime: endTime,
548-
OutgoingChanIDs: fn.NewSet(outgoingChanIDs[0].ToUint64(),
549-
outgoingChanIDs[1].ToUint64()),
550-
IncomingChanIDs: fn.NewSet(incomingChanIDs[0].ToUint64(),
551-
incomingChanIDs[1].ToUint64()),
533+
OutgoingChanIDs: fn.NewSet(
534+
outgoingChanIDs[0].ToUint64(),
535+
outgoingChanIDs[1].ToUint64(),
536+
),
537+
IncomingChanIDs: fn.NewSet(
538+
incomingChanIDs[0].ToUint64(),
539+
incomingChanIDs[1].ToUint64(),
540+
),
552541
IndexOffset: 0,
553542
NumMaxEvents: 10,
554543
}
@@ -568,6 +557,5 @@ func TestForwardingLogQueryIncomingAndOutgoingChanIDs(t *testing.T) {
568557
}
569558
}
570559

571-
require.Equal(t, expectedEvents, timeSlice.ForwardingEvents,
572-
"unexpected events returned")
560+
require.Equal(t, expectedEvents, timeSlice.ForwardingEvents)
573561
}

cmd/commands/cmd_payments.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,13 +1523,15 @@ var forwardingHistoryCommand = cli.Command{
15231523
},
15241524
cli.Int64SliceFlag{
15251525
Name: "incoming_chan_ids",
1526-
Usage: "the short channel ids of the incoming " +
1527-
"channels to filter events by",
1526+
Usage: "the short channel id of the incoming " +
1527+
"channel to filter events by; can be " +
1528+
"specified multiple times in the same command",
15281529
},
15291530
cli.Int64SliceFlag{
15301531
Name: "outgoing_chan_ids",
1531-
Usage: "the short channel ids of the outgoing " +
1532-
"channels to filter events by",
1532+
Usage: "the short channel id of the outgoing " +
1533+
"channel to filter events by; can be " +
1534+
"specified multiple times in the same command",
15331535
},
15341536
},
15351537
Action: actionDecorator(forwardingHistory),

rpcserver.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7954,8 +7954,7 @@ func (r *rpcServer) ForwardingHistory(ctx context.Context,
79547954
var (
79557955
startTime, endTime time.Time
79567956

7957-
numEvents uint32
7958-
incomingChanIDs, outgoingChanIDs fn.Set[uint64]
7957+
numEvents uint32
79597958
)
79607959

79617960
// startTime defaults to the Unix epoch (0 unixtime, or
@@ -7979,15 +7978,8 @@ func (r *rpcServer) ForwardingHistory(ctx context.Context,
79797978

79807979
// Create sets of incoming and outgoing channel IDs from the request
79817980
// for faster lookups for filtering.
7982-
incomingChanIDs = make(fn.Set[uint64], len(req.IncomingChanIds))
7983-
outgoingChanIDs = make(fn.Set[uint64], len(req.OutgoingChanIds))
7984-
for _, chanID := range req.IncomingChanIds {
7985-
incomingChanIDs.Add(chanID)
7986-
}
7987-
7988-
for _, chanID := range req.OutgoingChanIds {
7989-
outgoingChanIDs.Add(chanID)
7990-
}
7981+
incomingChanIDs := fn.NewSet(req.IncomingChanIds...)
7982+
outgoingChanIDs := fn.NewSet(req.OutgoingChanIds...)
79917983

79927984
// Next, we'll map the proto request into a format that is understood by
79937985
// the forwarding log.

0 commit comments

Comments
 (0)