From f79af461d32feaa53881c761cabb05972d6b6148 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 5 Apr 2018 16:19:44 -0700 Subject: [PATCH] lnwallet: in NewBreachRetribution create the htlcRetributions slice to capacity, not length In this commit, we fix an existing bug in the NewBreachRetribution method. Rather than creating the slice to the proper length, we instead now create it to the proper _capacity_. As we'll now properly filter out any dust HTLCs, before this commit, even if no HTLCs were added, then the slice would still have a full length, meaning callers could actually interact with _blank_ HtlcRetribution structs. The fix is simple: create the slice with the proper capacity, and append to the end of it. --- lnwallet/channel.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 71fe56e3..0aa81435 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1969,8 +1969,8 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, // With the commitment outputs located, we'll now generate all the // retribution structs for each of the HTLC transactions active on the // remote commitment transaction. - htlcRetributions := make([]HtlcRetribution, len(revokedSnapshot.Htlcs)) - for i, htlc := range revokedSnapshot.Htlcs { + htlcRetributions := make([]HtlcRetribution, 0, len(revokedSnapshot.Htlcs)) + for _, htlc := range revokedSnapshot.Htlcs { var ( htlcScript []byte err error @@ -2023,7 +2023,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, } } - htlcRetributions[i] = HtlcRetribution{ + htlcRetributions = append(htlcRetributions, HtlcRetribution{ SignDesc: SignDescriptor{ KeyDesc: chanState.LocalChanCfg.RevocationBasePoint, DoubleTweak: commitmentSecret, @@ -2039,7 +2039,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, }, SecondLevelWitnessScript: secondLevelWitnessScript, IsIncoming: htlc.Incoming, - } + }) } // Finally, with all the necessary data constructed, we can create the