Skip to content

Commit 90f997c

Browse files
authored
Merge pull request #8908 from lightningnetwork/traffic-shaper-htlc-amount-fix
routing: don't assume HTLC amount for custom channel
2 parents 7ffadde + d80bca8 commit 90f997c

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

htlcswitch/interceptable_switch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ func (f *interceptedForward) ResumeModified(
658658
switch htlc := f.packet.htlc.(type) {
659659
case *lnwire.UpdateAddHTLC:
660660
outgoingAmountMsat.WhenSome(func(amount lnwire.MilliSatoshi) {
661+
f.packet.amount = amount
661662
htlc.Amount = amount
662663
})
663664

itest/lnd_forward_interceptor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) {
407407
customRecords[crKey] = crValue
408408

409409
action := routerrpc.ResolveHoldForwardAction_RESUME_MODIFIED
410-
newOutgoingAmountMsat := packet.OutgoingAmountMsat + 4000
410+
newOutgoingAmountMsat := packet.OutgoingAmountMsat
411411

412412
err := bobInterceptor.Send(&routerrpc.ForwardHtlcInterceptResponse{
413413
IncomingCircuitKey: packet.IncomingCircuitKey,
@@ -539,7 +539,7 @@ func testForwardInterceptorWireRecords(ht *lntest.HarnessTest) {
539539
require.Equal(ht, []byte("test"), val)
540540

541541
action := routerrpc.ResolveHoldForwardAction_RESUME_MODIFIED
542-
newOutgoingAmountMsat := packet.OutgoingAmountMsat + 800
542+
newOutgoingAmountMsat := packet.OutgoingAmountMsat
543543

544544
err := bobInterceptor.Send(&routerrpc.ForwardHtlcInterceptResponse{
545545
IncomingCircuitKey: packet.IncomingCircuitKey,

routing/bandwidth.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID,
138138

139139
auxBandwidth lnwire.MilliSatoshi
140140
auxBandwidthDetermined bool
141+
142+
// htlcAmount is the amount we're going to use to check if we
143+
// can add another HTLC to the channel. If the external traffic
144+
// shaper is handling the channel, we'll use 0 to just sanity
145+
// check the number of HTLCs on the channel, since we don't know
146+
// the actual HTLC amount that will be sent.
147+
htlcAmount = amount
141148
)
142149
err = fn.MapOptionZ(b.trafficShaper, func(ts TlvTrafficShaper) error {
143150
fundingBlob := link.FundingCustomBlob()
@@ -171,6 +178,15 @@ func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID,
171178

172179
auxBandwidthDetermined = true
173180

181+
// We don't know the actual HTLC amount that will be sent using
182+
// the custom channel. But we'll still want to make sure we can
183+
// add another HTLC, using the MayAddOutgoingHtlc method below.
184+
// Passing 0 into that method will use the minimum HTLC value
185+
// for the channel, which is okay to just check we don't exceed
186+
// the max number of HTLCs on the channel. A proper balance
187+
// check is done elsewhere.
188+
htlcAmount = 0
189+
174190
return nil
175191
})
176192
if err != nil {
@@ -180,11 +196,11 @@ func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID,
180196
return 0
181197
}
182198

183-
// If our link isn't currently in a state where it can add
184-
// another outgoing htlc, treat the link as unusable.
185-
if err := link.MayAddOutgoingHtlc(amount); err != nil {
199+
// If our link isn't currently in a state where it can add another
200+
// outgoing htlc, treat the link as unusable.
201+
if err := link.MayAddOutgoingHtlc(htlcAmount); err != nil {
186202
log.Warnf("ShortChannelID=%v: cannot add outgoing "+
187-
"htlc: %v", cid, err)
203+
"htlc with amount %v: %v", cid, htlcAmount, err)
188204
return 0
189205
}
190206

routing/pathfind.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,18 +1007,22 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
10071007
continue
10081008
}
10091009

1010-
firstHopTLVs := lnwire.CustomRecords(
1011-
r.FirstHopCustomRecords,
1012-
)
1013-
firstHopData, err := firstHopTLVs.Serialize()
1014-
if err != nil {
1015-
return nil, 0, err
1010+
var firstHopBlob fn.Option[tlv.Blob]
1011+
if r.FirstHopCustomRecords != nil {
1012+
firstHopTLVs := lnwire.CustomRecords(
1013+
r.FirstHopCustomRecords,
1014+
)
1015+
firstHopData, err := firstHopTLVs.Serialize()
1016+
if err != nil {
1017+
return nil, 0, err
1018+
}
1019+
1020+
firstHopBlob = fn.Some(firstHopData)
10161021
}
10171022

10181023
edge := edgeUnifier.getEdge(
10191024
netAmountReceived, g.bandwidthHints,
1020-
partialPath.outboundFee,
1021-
fn.Some[tlv.Blob](firstHopData),
1025+
partialPath.outboundFee, firstHopBlob,
10221026
)
10231027

10241028
if edge == nil {

routing/unified_edges.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (u *edgeUnifier) getEdgeLocal(netAmtReceived lnwire.MilliSatoshi,
234234
amt := netAmtReceived + lnwire.MilliSatoshi(inboundFee)
235235

236236
// Check valid amount range for the channel.
237-
if !edge.amtInRange(amt) {
237+
if htlcBlob.IsNone() && !edge.amtInRange(amt) {
238238
log.Debugf("Amount %v not in range for edge %v",
239239
netAmtReceived, edge.policy.ChannelID)
240240

0 commit comments

Comments
 (0)