From 8cc217b5260a5d34ba63c1df362a48b51e0e06df Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 19:02:36 -0700 Subject: [PATCH 1/2] lnwire/lnwire_test: use nil slice when 0 sids Modifies the behavior of the quick test for MsgQueryShortChanIDs, such that the generated slice of expected short chan ids is always nil if no elements are returned. This mimics the behavior of the zlib decompression, where elements are appended to the slice, instead of assigning to preallocated slice. --- lnwire/lnwire_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index f1311c620..8a1e82f6b 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -668,11 +668,9 @@ func TestLightningWireProtocol(t *testing.T) { numChanIDs := rand.Int31n(5000) - req.ShortChanIDs = make([]ShortChannelID, numChanIDs) for i := int32(0); i < numChanIDs; i++ { - req.ShortChanIDs[i] = NewShortChanIDFromInt( - uint64(r.Int63()), - ) + req.ShortChanIDs = append(req.ShortChanIDs, + NewShortChanIDFromInt(uint64(r.Int63()))) } v[0] = reflect.ValueOf(req) From 61c2493b7da8ad7c10f777be35b9c49e6be07437 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 28 Jun 2018 19:04:55 -0700 Subject: [PATCH 2/2] lnwire/query_short_chan_ids: short circuit if 0 sids In this commit, we alter the behavior of the regular short channel id encoding, such that it returns a nil slice if the decoded number of elements is 0. This is done so that it matches the behavior of the zlib decompression, allowing us to test both in using the same corpus. --- lnwire/query_short_chan_ids.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lnwire/query_short_chan_ids.go b/lnwire/query_short_chan_ids.go index fd959c433..401aa1186 100644 --- a/lnwire/query_short_chan_ids.go +++ b/lnwire/query_short_chan_ids.go @@ -146,10 +146,13 @@ func decodeShortChanIDs(r io.Reader) (ShortChanIDEncoding, []ShortChannelID, err // compute the number of bytes encoded based on the size of the // query body. numShortChanIDs := len(queryBody) / 8 - shortChanIDs := make([]ShortChannelID, numShortChanIDs) + if numShortChanIDs == 0 { + return encodingType, nil, nil + } // Finally, we'll read out the exact number of short channel // ID's to conclude our parsing. + shortChanIDs := make([]ShortChannelID, numShortChanIDs) bodyReader := bytes.NewReader(queryBody) for i := 0; i < numShortChanIDs; i++ { if err := readElements(bodyReader, &shortChanIDs[i]); err != nil {