@@ -290,12 +290,6 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
290
290
// is not yet in that list.
291
291
allowedMarginAssetUnits := uint64 (len (req .Invoice .Htlcs ) + 1 )
292
292
293
- // Because we do a round trip of units -> milli-sats, then split into
294
- // shards, then back to units and then back to milli-sats again, we lose
295
- // up to numHTLCs+1 asset units in the process. So we allow for an
296
- // additional unit here.
297
- allowedMarginAssetUnits ++
298
-
299
293
// Convert the allowed margin asset units to milli-satoshis.
300
294
marginAssetUnits := rfqmath .NewBigIntFixedPoint (
301
295
allowedMarginAssetUnits , 0 ,
@@ -304,10 +298,17 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
304
298
marginAssetUnits , * assetRate ,
305
299
)
306
300
301
+ // Every HTLC makes a roundtrip from msats to units and then back to
302
+ // msats right here. Above we account for the asset units that may have
303
+ // been lost by our peer when converting their incoming HTLC amount to
304
+ // asset units, now we need to account for our loss for converting each
305
+ // HTLC to msats.
306
+ allowedMarginMSat += lnwire .MilliSatoshi (len (req .Invoice .Htlcs ) + 1 )
307
+
307
308
// If the sum of the accepted HTLCs plus the current HTLC amount plus
308
309
// the error margin is greater than the invoice amount, we'll accept it.
309
310
totalInbound := acceptedHtlcSum + resp .AmtPaid
310
- totalInboundWithMargin := totalInbound + allowedMarginMSat + 1
311
+ totalInboundWithMargin := totalInbound + allowedMarginMSat
311
312
invoiceValue := lnwire .MilliSatoshi (req .Invoice .ValueMsat )
312
313
313
314
iLog .Debugf ("accepted HTLC sum: %v, current HTLC amount: %v, allowed " +
@@ -318,6 +319,12 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
318
319
// If we're within the error margin, we'll increase the current HTLCs
319
320
// amount to cover the error rate and make the total sum match the
320
321
// invoice amount exactly.
322
+ //
323
+ // Note: The total calculated margin should match what is implemented in
324
+ // our TestUnitConversionToleranceRapid test. That test serves as a
325
+ // sanity check for our tolerance model, which should allow all
326
+ // combinations of shards and asset rates to be captured by the total
327
+ // calculated tolerance.
321
328
if totalInboundWithMargin >= invoiceValue {
322
329
resp .AmtPaid = invoiceValue - acceptedHtlcSum
323
330
}
0 commit comments