Skip to content

Commit d2d5064

Browse files
committed
lnwallet: add HTLC index to commitment sorting function
To avoid sorting issues with identical HTLCs (equal size, equal payment hash, equal CLTV), we need to also use the HTLC index to be able to distinguish between them.
1 parent a626007 commit d2d5064

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lnwallet/commitment.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
613613
// commitment transaction outputs. The second parameter is a list of CLTV
614614
// timeouts that must correspond to the number of transaction outputs, with the
615615
// value of 0 for non-HTLC outputs.
616-
type CommitSortFunc func(*wire.MsgTx, []uint32) error
616+
type CommitSortFunc func(*wire.MsgTx, []uint32, []input.HtlcIndex) error
617617

618618
// CommitAuxLeaves stores two potential auxiliary leaves for the remote and
619619
// local output that may be used to argument the final tapscript trees of the
@@ -1003,6 +1003,7 @@ func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
10031003
// commitment outputs and should correspond to zero values for the
10041004
// purposes of sorting.
10051005
cltvs := make([]uint32, len(commitTx.TxOut))
1006+
htlcIndexes := make([]input.HtlcIndex, len(commitTx.TxOut))
10061007
for _, htlc := range filteredHTLCView.OurUpdates {
10071008
if HtlcIsDust(
10081009
cb.chanState.ChanType, false, isOurs, feePerKw,
@@ -1025,7 +1026,11 @@ func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
10251026
if err != nil {
10261027
return nil, err
10271028
}
1028-
cltvs = append(cltvs, htlc.Timeout) // nolint:makezero
1029+
1030+
// We want to add the CLTV and HTLC index to their respective
1031+
// slices, even if we already pre-allocated them.
1032+
cltvs = append(cltvs, htlc.Timeout) //nolint
1033+
htlcIndexes = append(htlcIndexes, htlc.HtlcIndex) //nolint
10291034
}
10301035
for _, htlc := range filteredHTLCView.TheirUpdates {
10311036
if HtlcIsDust(
@@ -1049,7 +1054,11 @@ func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
10491054
if err != nil {
10501055
return nil, err
10511056
}
1052-
cltvs = append(cltvs, htlc.Timeout) // nolint:makezero
1057+
1058+
// We want to add the CLTV and HTLC index to their respective
1059+
// slices, even if we already pre-allocated them.
1060+
cltvs = append(cltvs, htlc.Timeout) //nolint
1061+
htlcIndexes = append(htlcIndexes, htlc.HtlcIndex) //nolint
10531062
}
10541063

10551064
// Set the state hint of the commitment transaction to facilitate
@@ -1071,7 +1080,7 @@ func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
10711080
"sorting function")
10721081
}
10731082

1074-
err = customCommitSort(commitTx, cltvs)
1083+
err = customCommitSort(commitTx, cltvs, htlcIndexes)
10751084
if err != nil {
10761085
return nil, fmt.Errorf("unable to sort commitment "+
10771086
"transaction by custom order: %w", err)

0 commit comments

Comments
 (0)