From 7571acbe81f56439a14aad9dfba8324eeae6c9dd Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 5 Jun 2025 11:07:46 +0200 Subject: [PATCH] channeldb: remove unnecessary for loop from Query method This commit simplifies the code of the ForwardingLog.Query method by removing a confusing for-loop. The for-loop makes it seem as though multiple events could be encoded under a single timestamp. But from the time that this forwarding log was introduced, it was never possible to encode multiple events under the same timestamp and so this loop will never execute successfully more than once per timestamp and can thus be removed. This paves the way such that future expansions of the method can be added easily. See the initial commit that introduced this code [here](https://github.com/lightningnetwork/lnd/commit/f2cd668bcfb423fb212e78282c786dea166e0168). In this commit you can see that from the start it was never possible to have more than one event in a single timestamp since any previous event in that timestamp would be overwritten. Then see [this commit](https://github.com/lightningnetwork/lnd/pull/4475/commits/97c73706b55fb5520d2f3001dfa13dc1da986972) where even more protection was added to ensure that each event had a unique timestamp. --- channeldb/forwarding_log.go | 45 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/channeldb/forwarding_log.go b/channeldb/forwarding_log.go index f527b78dd..2aa816a4a 100644 --- a/channeldb/forwarding_log.go +++ b/channeldb/forwarding_log.go @@ -227,7 +227,9 @@ type ForwardingLogTimeSlice struct { // the number of events to be returned. // // TODO(roasbeef): rename? -func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, error) { +func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, + error) { + var resp ForwardingLogTimeSlice // If the user provided an index offset, then we'll not know how many @@ -256,8 +258,9 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, e // We'll continue until either we reach the end of the range, // or reach our max number of events. logCursor := logBucket.ReadCursor() - timestamp, events := logCursor.Seek(startTime[:]) - for ; timestamp != nil && bytes.Compare(timestamp, endTime[:]) <= 0; timestamp, events = logCursor.Next() { + timestamp, eventBytes := logCursor.Seek(startTime[:]) + //nolint:ll + for ; timestamp != nil && bytes.Compare(timestamp, endTime[:]) <= 0; timestamp, eventBytes = logCursor.Next() { // If our current return payload exceeds the max number // of events, then we'll exit now. if uint32(len(resp.ForwardingEvents)) >= q.NumMaxEvents { @@ -271,27 +274,31 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, e continue } - currentTime := time.Unix( - 0, int64(byteOrder.Uint64(timestamp)), - ) - // At this point, we've skipped enough records to start // to collate our query. For each record, we'll // increment the final record offset so the querier can // utilize pagination to seek further. - readBuf := bytes.NewReader(events) - for readBuf.Len() != 0 { - var event ForwardingEvent - err := decodeForwardingEvent(readBuf, &event) - if err != nil { - return err - } - - event.Timestamp = currentTime - resp.ForwardingEvents = append(resp.ForwardingEvents, event) - - recordOffset++ + readBuf := bytes.NewReader(eventBytes) + if readBuf.Len() == 0 { + continue } + + currentTime := time.Unix( + 0, int64(byteOrder.Uint64(timestamp)), + ) + + var event ForwardingEvent + err := decodeForwardingEvent(readBuf, &event) + if err != nil { + return err + } + + event.Timestamp = currentTime + resp.ForwardingEvents = append( + resp.ForwardingEvents, event, + ) + + recordOffset++ } return nil