Skip to content

Commit 3141e83

Browse files
GeorgeTsagkRoasbeef
authored andcommitted
tapchannel(rapid-only): update invoice tolerance model
We update the tolerance model in the rapid test to account for 1 unit plus 1 msat, both per HTLC.
1 parent a5bd060 commit 3141e83

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

tapchannel/aux_traffic_shaper_test.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,46 @@ func TestUnitConversionToleranceRapid(t *testing.T) {
8989

9090
shardSizeMSat := invoiceAmtMsat / lnwire.MilliSatoshi(numHTLCs)
9191
shardSizeFP := rfqmath.MilliSatoshiToUnits(shardSizeMSat, rate)
92-
shardSizeUnit := shardSizeFP.ScaleTo(0).ToUint64()
9392

94-
shardSumFP := rfqmath.NewBigIntFixedPoint(
95-
shardSizeUnit*numHTLCs, 0,
96-
)
97-
inboundAmountMSat := rfqmath.UnitsToMilliSatoshi(
98-
shardSumFP, rate,
99-
)
93+
// In order to account for the full invoice amt we need to also
94+
// somehow carry the remainder of invoiceAmt/numHTLCs. We do so
95+
// by appending it to the last shard.
96+
invoiceMod := invoiceAmtMsat % lnwire.MilliSatoshi(numHTLCs)
97+
lastShardMsat := shardSizeMSat + invoiceMod
98+
lastShardFP := rfqmath.MilliSatoshiToUnits(lastShardMsat, rate)
99+
100+
var inboundAmountMSat lnwire.MilliSatoshi
101+
102+
// Each shard calls individually into the rfqmath library. This
103+
// allows for a total error that scales with the number of
104+
// shards. We go up to numHTLCS-1 because we want to add any
105+
// leftovers to the last shard.
106+
for range numHTLCs - 1 {
107+
shardPartialSum := rfqmath.UnitsToMilliSatoshi(
108+
shardSizeFP, rate,
109+
)
110+
111+
inboundAmountMSat += shardPartialSum
112+
}
100113

101-
newMarginAssetUnits := rfqmath.NewBigIntFixedPoint(
102-
numHTLCs+1, 0,
114+
// Make a single pass over the last shard, which may also carry
115+
// the remainder of the payment amt.
116+
lastShardMsat = rfqmath.UnitsToMilliSatoshi(lastShardFP, rate)
117+
inboundAmountMSat += lastShardMsat
118+
119+
allowedMarginUnits := rfqmath.NewBigIntFixedPoint(
120+
numHTLCs, 0,
103121
)
104-
newAllowedMarginMSat := rfqmath.UnitsToMilliSatoshi(
105-
newMarginAssetUnits, rate,
122+
marginAmt := rfqmath.UnitsToMilliSatoshi(
123+
allowedMarginUnits, rate,
106124
)
107125

126+
marginAmt += lnwire.MilliSatoshi(numHTLCs)
127+
108128
// The difference should be within the newly allowed margin.
109129
require.LessOrEqual(
110130
t,
111-
invoiceAmtMsat-inboundAmountMSat, newAllowedMarginMSat,
131+
invoiceAmtMsat-inboundAmountMSat, marginAmt,
112132
)
113133
})
114134
}

0 commit comments

Comments
 (0)