From 4872010779cc6d3b52c9874bf7aae8dda69ec8ab Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 19 Sep 2023 20:49:39 +0200 Subject: [PATCH] lnwire: sort scids before encoding This is for later when the timestamps also need to be sorted according to the SCIDs. --- lnwire/query_short_chan_ids.go | 24 ++++++++++++------------ lnwire/reply_channel_range.go | 13 ++++++++++++- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/lnwire/query_short_chan_ids.go b/lnwire/query_short_chan_ids.go index 281c10eae..6a90bed75 100644 --- a/lnwire/query_short_chan_ids.go +++ b/lnwire/query_short_chan_ids.go @@ -280,9 +280,19 @@ func (q *QueryShortChanIDs) Encode(w *bytes.Buffer, pver uint32) error { return err } + // For both of the current encoding types, the channel ID's are to be + // sorted in place, so we'll do that now. The sorting is applied unless + // we were specifically requested not to for testing purposes. + if !q.noSort { + sort.Slice(q.ShortChanIDs, func(i, j int) bool { + return q.ShortChanIDs[i].ToUint64() < + q.ShortChanIDs[j].ToUint64() + }) + } + // Base on our encoding type, we'll write out the set of short channel // ID's. - err := encodeShortChanIDs(w, q.EncodingType, q.ShortChanIDs, q.noSort) + err := encodeShortChanIDs(w, q.EncodingType, q.ShortChanIDs) if err != nil { return err } @@ -293,17 +303,7 @@ func (q *QueryShortChanIDs) Encode(w *bytes.Buffer, pver uint32) error { // encodeShortChanIDs encodes the passed short channel ID's into the passed // io.Writer, respecting the specified encoding type. func encodeShortChanIDs(w *bytes.Buffer, encodingType QueryEncoding, - shortChanIDs []ShortChannelID, noSort bool) error { - - // For both of the current encoding types, the channel ID's are to be - // sorted in place, so we'll do that now. The sorting is applied unless - // we were specifically requested not to for testing purposes. - if !noSort { - sort.Slice(shortChanIDs, func(i, j int) bool { - return shortChanIDs[i].ToUint64() < - shortChanIDs[j].ToUint64() - }) - } + shortChanIDs []ShortChannelID) error { switch encodingType { diff --git a/lnwire/reply_channel_range.go b/lnwire/reply_channel_range.go index e02ccd474..2a461cded 100644 --- a/lnwire/reply_channel_range.go +++ b/lnwire/reply_channel_range.go @@ -4,6 +4,7 @@ import ( "bytes" "io" "math" + "sort" "github.com/btcsuite/btcd/chaincfg/chainhash" ) @@ -103,7 +104,17 @@ func (c *ReplyChannelRange) Encode(w *bytes.Buffer, pver uint32) error { return err } - err := encodeShortChanIDs(w, c.EncodingType, c.ShortChanIDs, c.noSort) + // For both of the current encoding types, the channel ID's are to be + // sorted in place, so we'll do that now. The sorting is applied unless + // we were specifically requested not to for testing purposes. + if !c.noSort { + sort.Slice(c.ShortChanIDs, func(i, j int) bool { + return c.ShortChanIDs[i].ToUint64() < + c.ShortChanIDs[j].ToUint64() + }) + } + + err := encodeShortChanIDs(w, c.EncodingType, c.ShortChanIDs) if err != nil { return err }