Skip to content

Commit de1b916

Browse files
committed
tapchannel: fix edge case with BTC only HTLCs
This commit fixes an issue where we wouldn't count non-asset HTLCs when deciding whether the commitment transaction would require a commitment anchor output for the remote party, so the number of allocations would be out of sync with the number of outputs on the commitment transaction.
1 parent bee25e0 commit de1b916

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

tapchannel/commitment.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,47 @@ func processAddEntry(htlc *DecodedDescriptor, ourBalance, theirBalance uint64,
363363
// non-dust satoshi balance. It also checks and returns whether we need a local
364364
// and/or remote anchor output.
365365
func SanityCheckAmounts(ourBalance, theirBalance btcutil.Amount,
366-
ourAssetBalance, theirAssetBalance uint64, view *DecodedView,
367-
chanType channeldb.ChannelType, whoseCommit lntypes.ChannelParty,
368-
dustLimit btcutil.Amount) (bool, bool, error) {
366+
ourAssetBalance, theirAssetBalance uint64, assetView,
367+
nonAssetView *DecodedView, chanType channeldb.ChannelType,
368+
whoseCommit lntypes.ChannelParty, dustLimit btcutil.Amount) (bool, bool,
369+
error) {
369370

370371
log.Tracef("Sanity checking amounts, whoseCommit=%v, ourBalance=%d, "+
371372
"theirBalance=%d, ourAssetBalance=%d, theirAssetBalance=%d",
372373
whoseCommit, ourBalance, theirBalance, ourAssetBalance,
373374
theirAssetBalance)
374375

375376
var (
376-
numHTLCs int64
377-
feePerKw = view.FeePerKw
377+
numHTLCs uint64
378+
feePerKw = assetView.FeePerKw
378379
)
379-
for _, entry := range view.OurUpdates {
380+
381+
// We need to count any non-dust BTC-only HTLCs too for determining
382+
// whether we need a commitment anchor output. The assetView and
383+
// nonAssetView are non-overlapping, so we can just sum the number of
384+
// non-dust HTLCs in both.
385+
for _, entry := range nonAssetView.OurUpdates {
386+
if !lnwallet.HtlcIsDust(
387+
chanType, false, whoseCommit, feePerKw,
388+
entry.Amount.ToSatoshis(), dustLimit,
389+
) {
390+
391+
numHTLCs++
392+
}
393+
}
394+
for _, entry := range nonAssetView.TheirUpdates {
395+
if !lnwallet.HtlcIsDust(
396+
chanType, true, whoseCommit, feePerKw,
397+
entry.Amount.ToSatoshis(), dustLimit,
398+
) {
399+
400+
numHTLCs++
401+
}
402+
}
403+
404+
// And finally we check the asset HTLCs. Here we also enforce that an
405+
// HTLC that's carrying an asset must be above dust.
406+
for _, entry := range assetView.OurUpdates {
380407
isDust := lnwallet.HtlcIsDust(
381408
chanType, false, whoseCommit, feePerKw,
382409
entry.Amount.ToSatoshis(), dustLimit,
@@ -391,7 +418,7 @@ func SanityCheckAmounts(ourBalance, theirBalance btcutil.Amount,
391418

392419
numHTLCs++
393420
}
394-
for _, entry := range view.TheirUpdates {
421+
for _, entry := range assetView.TheirUpdates {
395422
isDust := lnwallet.HtlcIsDust(
396423
chanType, true, whoseCommit, feePerKw,
397424
entry.Amount.ToSatoshis(), dustLimit,
@@ -488,7 +515,7 @@ func GenerateCommitmentAllocations(prevState *cmsg.Commitment,
488515
// corresponding non-dust BTC output.
489516
wantLocalAnchor, wantRemoteAnchor, err := SanityCheckAmounts(
490517
ourBalance.ToSatoshis(), theirBalance.ToSatoshis(),
491-
ourAssetBalance, theirAssetBalance, filteredView,
518+
ourAssetBalance, theirAssetBalance, filteredView, nonAssetView,
492519
chanState.ChanType, whoseCommit, dustLimit,
493520
)
494521
if err != nil {

0 commit comments

Comments
 (0)