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