Skip to content

Commit 25cf7b1

Browse files
GeorgeTsagkRoasbeef
authored andcommitted
tapchannel: aux invoice manager uses new tolerance model
1 parent 0914819 commit 25cf7b1

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tapchannel/aux_invoice_manager.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,6 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
290290
// is not yet in that list.
291291
allowedMarginAssetUnits := uint64(len(req.Invoice.Htlcs) + 1)
292292

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-
299293
// Convert the allowed margin asset units to milli-satoshis.
300294
marginAssetUnits := rfqmath.NewBigIntFixedPoint(
301295
allowedMarginAssetUnits, 0,
@@ -304,10 +298,17 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
304298
marginAssetUnits, *assetRate,
305299
)
306300

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+
307308
// If the sum of the accepted HTLCs plus the current HTLC amount plus
308309
// the error margin is greater than the invoice amount, we'll accept it.
309310
totalInbound := acceptedHtlcSum + resp.AmtPaid
310-
totalInboundWithMargin := totalInbound + allowedMarginMSat + 1
311+
totalInboundWithMargin := totalInbound + allowedMarginMSat
311312
invoiceValue := lnwire.MilliSatoshi(req.Invoice.ValueMsat)
312313

313314
iLog.Debugf("accepted HTLC sum: %v, current HTLC amount: %v, allowed "+
@@ -318,6 +319,12 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
318319
// If we're within the error margin, we'll increase the current HTLCs
319320
// amount to cover the error rate and make the total sum match the
320321
// 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.
321328
if totalInboundWithMargin >= invoiceValue {
322329
resp.AmtPaid = invoiceValue - acceptedHtlcSum
323330
}

0 commit comments

Comments
 (0)