Skip to content

Commit 343b196

Browse files
committed
tapchannel: aux invoice manager uses new tolerance model
1 parent 10ae40f commit 343b196

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
@@ -270,12 +270,6 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
270270
// is not yet in that list.
271271
allowedMarginAssetUnits := uint64(len(req.Invoice.Htlcs) + 1)
272272

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-
279273
// Convert the allowed margin asset units to milli-satoshis.
280274
marginAssetUnits := rfqmath.NewBigIntFixedPoint(
281275
allowedMarginAssetUnits, 0,
@@ -284,10 +278,17 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
284278
marginAssetUnits, *assetRate,
285279
)
286280

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+
287288
// If the sum of the accepted HTLCs plus the current HTLC amount plus
288289
// the error margin is greater than the invoice amount, we'll accept it.
289290
totalInbound := acceptedHtlcSum + resp.AmtPaid
290-
totalInboundWithMargin := totalInbound + allowedMarginMSat + 1
291+
totalInboundWithMargin := totalInbound + allowedMarginMSat
291292
invoiceValue := lnwire.MilliSatoshi(req.Invoice.ValueMsat)
292293

293294
iLog.Debugf("accepted HTLC sum: %v, current HTLC amount: %v, allowed "+
@@ -298,6 +299,12 @@ func (s *AuxInvoiceManager) handleInvoiceAccept(ctx context.Context,
298299
// If we're within the error margin, we'll increase the current HTLCs
299300
// amount to cover the error rate and make the total sum match the
300301
// 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.
301308
if totalInboundWithMargin >= invoiceValue {
302309
resp.AmtPaid = invoiceValue - acceptedHtlcSum
303310
}

0 commit comments

Comments
 (0)