Skip to content

Commit 1fbefbc

Browse files
committed
tapchannel: add chan reserve check on PaymentBandwidth method
1 parent e160a9a commit 1fbefbc

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func (s *Server) HandleTraffic(cid lnwire.ShortChannelID,
990990
//
991991
// NOTE: This method is part of the routing.TlvTrafficShaper interface.
992992
func (s *Server) PaymentBandwidth(htlcBlob, commitmentBlob lfn.Option[tlv.Blob],
993-
linkBandwidth lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
993+
linkBandwidth, htlcAmt lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
994994

995995
srvrLog.Debugf("PaymentBandwidth called, htlcBlob=%v, "+
996996
"commitmentBlob=%v", spew.Sdump(htlcBlob),
@@ -1001,7 +1001,7 @@ func (s *Server) PaymentBandwidth(htlcBlob, commitmentBlob lfn.Option[tlv.Blob],
10011001
}
10021002

10031003
return s.cfg.AuxTrafficShaper.PaymentBandwidth(
1004-
htlcBlob, commitmentBlob, linkBandwidth,
1004+
htlcBlob, commitmentBlob, linkBandwidth, htlcAmt,
10051005
)
10061006
}
10071007

tapchannel/aux_traffic_shaper.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (s *AuxTrafficShaper) HandleTraffic(_ lnwire.ShortChannelID,
121121
// called first.
122122
func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
123123
commitmentBlob lfn.Option[tlv.Blob],
124-
linkBandwidth lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
124+
linkBandwidth, htlcAmt lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
125125

126126
// If the commitment or HTLC blob is not set, we don't have any
127127
// information about the channel and cannot determine the available
@@ -140,6 +140,35 @@ func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
140140
return linkBandwidth, nil
141141
}
142142

143+
// Get the minimum HTLC amount, which is just above dust.
144+
minHtlcAmt := lnwire.NewMSatFromSatoshis(DefaultOnChainHtlcAmount)
145+
146+
// LND calls this hook twice. Once to see if the overall budget of the
147+
// node is enough, and then during pathfinding to actually see if
148+
// there's enough balance in the channel to make the payment attempt.
149+
//
150+
// When doing the overall balance check, we don't know what the actual
151+
// htlcAmt is in satoshis, so a value of 0 will be passed here. Let's at
152+
// least check if we can afford the min amount above dust. If the actual
153+
// htlc amount ends up being greater when calling this method during
154+
// pathfinding, we will still check it below.
155+
156+
// If the passed htlcAmt is below dust, then assume the dust amount. At
157+
// this point we know we are sending assets, so we cannot anchor them to
158+
// dust amounts. Dust HTLCs are added to the fees and aren't
159+
// materialized in an on-chain output, so we wouldn't have anything
160+
// to anchor the asset commitment to.
161+
if htlcAmt < minHtlcAmt {
162+
htlcAmt = minHtlcAmt
163+
}
164+
165+
// Check if the current link bandwidth can afford sending out the htlc
166+
// amount without dipping into the channel reserve. If it goes below the
167+
// reserve, we report zero bandwdith as we cannot push the htlc amount.
168+
if linkBandwidth < htlcAmt {
169+
return 0, nil
170+
}
171+
143172
commitment, err := cmsg.DecodeCommitment(commitmentBytes)
144173
if err != nil {
145174
return 0, fmt.Errorf("error decoding commitment blob: %w", err)

0 commit comments

Comments
 (0)