Skip to content

Commit 1f9cac5

Browse files
committed
htlcswitch: refactor dust handling to use ChannelParty
1 parent 1d65f5b commit 1f9cac5

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

htlcswitch/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type dustHandler interface {
6363
// getDustSum returns the dust sum on either the local or remote
6464
// commitment. An optional fee parameter can be passed in which is used
6565
// to calculate the dust sum.
66-
getDustSum(remote bool,
66+
getDustSum(whoseCommit lntypes.ChannelParty,
6767
fee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi
6868

6969
// getFeeRate returns the current channel feerate.

htlcswitch/link.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,15 +2727,10 @@ func (l *channelLink) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
27272727
// method.
27282728
//
27292729
// NOTE: Part of the dustHandler interface.
2730-
func (l *channelLink) getDustSum(remote bool,
2730+
func (l *channelLink) getDustSum(whoseCommit lntypes.ChannelParty,
27312731
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {
27322732

2733-
party := lntypes.Local
2734-
if remote {
2735-
party = lntypes.Remote
2736-
}
2737-
2738-
return l.channel.GetDustSum(party, dryRunFee)
2733+
return l.channel.GetDustSum(whoseCommit, dryRunFee)
27392734
}
27402735

27412736
// getFeeRate is a wrapper method that retrieves the underlying channel's
@@ -2789,8 +2784,8 @@ func (l *channelLink) exceedsFeeExposureLimit(
27892784

27902785
// Get the sum of dust for both the local and remote commitments using
27912786
// this "dry-run" fee.
2792-
localDustSum := l.getDustSum(false, dryRunFee)
2793-
remoteDustSum := l.getDustSum(true, dryRunFee)
2787+
localDustSum := l.getDustSum(lntypes.Local, dryRunFee)
2788+
remoteDustSum := l.getDustSum(lntypes.Remote, dryRunFee)
27942789

27952790
// Calculate the local and remote commitment fees using this dry-run
27962791
// fee.
@@ -2831,12 +2826,16 @@ func (l *channelLink) isOverexposedWithHtlc(htlc *lnwire.UpdateAddHTLC,
28312826
amount := htlc.Amount.ToSatoshis()
28322827

28332828
// See if this HTLC is dust on both the local and remote commitments.
2834-
isLocalDust := dustClosure(feeRate, incoming, true, amount)
2835-
isRemoteDust := dustClosure(feeRate, incoming, false, amount)
2829+
isLocalDust := dustClosure(feeRate, incoming, lntypes.Local, amount)
2830+
isRemoteDust := dustClosure(feeRate, incoming, lntypes.Remote, amount)
28362831

28372832
// Calculate the dust sum for the local and remote commitments.
2838-
localDustSum := l.getDustSum(false, fn.None[chainfee.SatPerKWeight]())
2839-
remoteDustSum := l.getDustSum(true, fn.None[chainfee.SatPerKWeight]())
2833+
localDustSum := l.getDustSum(
2834+
lntypes.Local, fn.None[chainfee.SatPerKWeight](),
2835+
)
2836+
remoteDustSum := l.getDustSum(
2837+
lntypes.Remote, fn.None[chainfee.SatPerKWeight](),
2838+
)
28402839

28412840
// Grab the larger of the local and remote commitment fees w/o dust.
28422841
commitFee := l.getCommitFee(false)
@@ -2887,25 +2886,26 @@ func (l *channelLink) isOverexposedWithHtlc(htlc *lnwire.UpdateAddHTLC,
28872886
// the HTLC is incoming (i.e. one that the remote sent), a boolean denoting
28882887
// whether to evaluate on the local or remote commit, and finally an HTLC
28892888
// amount to test.
2890-
type dustClosure func(chainfee.SatPerKWeight, bool, bool, btcutil.Amount) bool
2889+
type dustClosure func(feerate chainfee.SatPerKWeight, incoming bool,
2890+
whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool
28912891

28922892
// dustHelper is used to construct the dustClosure.
28932893
func dustHelper(chantype channeldb.ChannelType, localDustLimit,
28942894
remoteDustLimit btcutil.Amount) dustClosure {
28952895

2896-
isDust := func(feerate chainfee.SatPerKWeight, incoming,
2897-
localCommit bool, amt btcutil.Amount) bool {
2896+
isDust := func(feerate chainfee.SatPerKWeight, incoming bool,
2897+
whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool {
28982898

2899-
if localCommit {
2900-
return lnwallet.HtlcIsDust(
2901-
chantype, incoming, lntypes.Local, feerate, amt,
2902-
localDustLimit,
2903-
)
2899+
var dustLimit btcutil.Amount
2900+
if whoseCommit.IsLocal() {
2901+
dustLimit = localDustLimit
2902+
} else {
2903+
dustLimit = remoteDustLimit
29042904
}
29052905

29062906
return lnwallet.HtlcIsDust(
2907-
chantype, incoming, lntypes.Remote, feerate, amt,
2908-
remoteDustLimit,
2907+
chantype, incoming, whoseCommit, feerate, amt,
2908+
dustLimit,
29092909
)
29102910
}
29112911

htlcswitch/mailbox.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/lightningnetwork/lnd/clock"
12+
"github.com/lightningnetwork/lnd/lntypes"
1213
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1314
"github.com/lightningnetwork/lnd/lnwire"
1415
)
@@ -660,15 +661,17 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,
660661

661662
// Evaluate whether this HTLC is dust on the local commitment.
662663
if m.isDust(
663-
m.feeRate, false, true, addPkt.amount.ToSatoshis(),
664+
m.feeRate, false, lntypes.Local,
665+
addPkt.amount.ToSatoshis(),
664666
) {
665667

666668
localDustSum += addPkt.amount
667669
}
668670

669671
// Evaluate whether this HTLC is dust on the remote commitment.
670672
if m.isDust(
671-
m.feeRate, false, false, addPkt.amount.ToSatoshis(),
673+
m.feeRate, false, lntypes.Remote,
674+
addPkt.amount.ToSatoshis(),
672675
) {
673676

674677
remoteDustSum += addPkt.amount

htlcswitch/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ func (f *mockChannelLink) handleSwitchPacket(pkt *htlcPacket) error {
814814
return nil
815815
}
816816

817-
func (f *mockChannelLink) getDustSum(remote bool,
817+
func (f *mockChannelLink) getDustSum(whoseCommit lntypes.ChannelParty,
818818
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {
819819

820820
return 0

htlcswitch/switch.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,8 +2788,12 @@ func (s *Switch) dustExceedsFeeThreshold(link ChannelLink,
27882788
isDust := link.getDustClosure()
27892789

27902790
// Evaluate if the HTLC is dust on either sides' commitment.
2791-
isLocalDust := isDust(feeRate, incoming, true, amount.ToSatoshis())
2792-
isRemoteDust := isDust(feeRate, incoming, false, amount.ToSatoshis())
2791+
isLocalDust := isDust(
2792+
feeRate, incoming, lntypes.Local, amount.ToSatoshis(),
2793+
)
2794+
isRemoteDust := isDust(
2795+
feeRate, incoming, lntypes.Remote, amount.ToSatoshis(),
2796+
)
27932797

27942798
if !(isLocalDust || isRemoteDust) {
27952799
// If the HTLC is not dust on either commitment, it's fine to
@@ -2807,7 +2811,7 @@ func (s *Switch) dustExceedsFeeThreshold(link ChannelLink,
28072811
// sum for it.
28082812
if isLocalDust {
28092813
localSum := link.getDustSum(
2810-
false, fn.None[chainfee.SatPerKWeight](),
2814+
lntypes.Local, fn.None[chainfee.SatPerKWeight](),
28112815
)
28122816
localSum += localMailDust
28132817

@@ -2827,7 +2831,7 @@ func (s *Switch) dustExceedsFeeThreshold(link ChannelLink,
28272831
// reached this point.
28282832
if isRemoteDust {
28292833
remoteSum := link.getDustSum(
2830-
true, fn.None[chainfee.SatPerKWeight](),
2834+
lntypes.Remote, fn.None[chainfee.SatPerKWeight](),
28312835
)
28322836
remoteSum += remoteMailDust
28332837

htlcswitch/switch_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,7 +4319,7 @@ func TestSwitchDustForwarding(t *testing.T) {
43194319
}
43204320

43214321
checkAlmostDust := func(link *channelLink, mbox MailBox,
4322-
remote bool) bool {
4322+
whoseCommit lntypes.ChannelParty) bool {
43234323

43244324
timeout := time.After(15 * time.Second)
43254325
pollInterval := 300 * time.Millisecond
@@ -4335,12 +4335,12 @@ func TestSwitchDustForwarding(t *testing.T) {
43354335
}
43364336

43374337
linkDust := link.getDustSum(
4338-
remote, fn.None[chainfee.SatPerKWeight](),
4338+
whoseCommit, fn.None[chainfee.SatPerKWeight](),
43394339
)
43404340
localMailDust, remoteMailDust := mbox.DustPackets()
43414341

43424342
totalDust := linkDust
4343-
if remote {
4343+
if whoseCommit.IsRemote() {
43444344
totalDust += remoteMailDust
43454345
} else {
43464346
totalDust += localMailDust
@@ -4359,7 +4359,11 @@ func TestSwitchDustForwarding(t *testing.T) {
43594359
n.firstBobChannelLink.ChanID(),
43604360
n.firstBobChannelLink.ShortChanID(),
43614361
)
4362-
require.True(t, checkAlmostDust(n.firstBobChannelLink, bobMbox, false))
4362+
require.True(
4363+
t, checkAlmostDust(
4364+
n.firstBobChannelLink, bobMbox, lntypes.Local,
4365+
),
4366+
)
43634367

43644368
// Sending one more HTLC should fail. SendHTLC won't error, but the
43654369
// HTLC should be failed backwards.
@@ -4408,7 +4412,9 @@ func TestSwitchDustForwarding(t *testing.T) {
44084412
aliceBobFirstHop, uint64(bobAttemptID), nondustHtlc,
44094413
)
44104414
require.NoError(t, err)
4411-
require.True(t, checkAlmostDust(n.firstBobChannelLink, bobMbox, false))
4415+
require.True(t, checkAlmostDust(
4416+
n.firstBobChannelLink, bobMbox, lntypes.Local,
4417+
))
44124418

44134419
// Check that the HTLC failed.
44144420
bobResultChan, err = n.bobServer.htlcSwitch.GetAttemptResult(
@@ -4486,7 +4492,11 @@ func TestSwitchDustForwarding(t *testing.T) {
44864492
aliceMbox := aliceOrch.GetOrCreateMailBox(
44874493
n.aliceChannelLink.ChanID(), n.aliceChannelLink.ShortChanID(),
44884494
)
4489-
require.True(t, checkAlmostDust(n.aliceChannelLink, aliceMbox, true))
4495+
require.True(
4496+
t, checkAlmostDust(
4497+
n.aliceChannelLink, aliceMbox, lntypes.Remote,
4498+
),
4499+
)
44904500

44914501
err = n.aliceServer.htlcSwitch.SendHTLC(
44924502
n.aliceChannelLink.ShortChanID(), uint64(aliceAttemptID),

0 commit comments

Comments
 (0)