From 9c80088abda9a2ac34b79087376d72d98b6ae897 Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Sun, 8 Sep 2024 17:00:13 -0400 Subject: [PATCH] lnwallet: sort sig jobs before submission To make sure we attempt to read the results of the sig batches in the same order they're processed, we sort them _before_ submitting them to the batch processor. Otherwise it might happen that we try to read on a result channel that was never sent on because we aborted due to an error. We also use slices.SortFunc now which doesn't use reflection and might be slightly faster. --- lnwallet/channel.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index b344b543b..0bd1938a7 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -6,7 +6,7 @@ import ( "errors" "fmt" "math" - "sort" + "slices" "sync" "github.com/btcsuite/btcd/blockchain" @@ -3963,6 +3963,17 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) { if err != nil { return nil, err } + + // We'll need to send over the signatures to the remote party in the + // order as they appear on the commitment transaction after BIP 69 + // sorting. + slices.SortFunc(sigBatch, func(i, j SignJob) int { + return int(i.OutputIndex - j.OutputIndex) + }) + slices.SortFunc(auxSigBatch, func(i, j AuxSigJob) int { + return int(i.OutputIndex - j.OutputIndex) + }) + lc.sigPool.SubmitSignBatch(sigBatch) err = fn.MapOptionZ(lc.auxSigner, func(a AuxSigner) error { @@ -4013,18 +4024,8 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) { } } - // We'll need to send over the signatures to the remote party in the - // order as they appear on the commitment transaction after BIP 69 - // sorting. - sort.Slice(sigBatch, func(i, j int) bool { - return sigBatch[i].OutputIndex < sigBatch[j].OutputIndex - }) - sort.Slice(auxSigBatch, func(i, j int) bool { - return auxSigBatch[i].OutputIndex < auxSigBatch[j].OutputIndex - }) - - // With the jobs sorted, we'll now iterate through all the responses to - // gather each of the signatures in order. + // Iterate through all the responses to gather each of the signatures + // in the order they were submitted. htlcSigs = make([]lnwire.Sig, 0, len(sigBatch)) auxSigs := make([]fn.Option[tlv.Blob], 0, len(auxSigBatch)) for i := range sigBatch {