Skip to content

Commit b8ece69

Browse files
committed
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 b073752 commit b8ece69

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

tapchannel/aux_traffic_shaper_test.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,45 @@ 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.
105+
for range numHTLCs - 1 {
106+
shardPartialSum := rfqmath.UnitsToMilliSatoshi(
107+
shardSizeFP, rate,
108+
)
100109

101-
newMarginAssetUnits := rfqmath.NewBigIntFixedPoint(
102-
numHTLCs+1, 0,
110+
inboundAmountMSat += shardPartialSum
111+
}
112+
113+
// Make a single pass over the last shard, which may also carry
114+
// the remainder of the payment amt.
115+
lastShardMsat = rfqmath.UnitsToMilliSatoshi(lastShardFP, rate)
116+
inboundAmountMSat += lastShardMsat
117+
118+
allowedMarginUnits := rfqmath.NewBigIntFixedPoint(
119+
numHTLCs, 0,
103120
)
104-
newAllowedMarginMSat := rfqmath.UnitsToMilliSatoshi(
105-
newMarginAssetUnits, rate,
121+
marginAmt := rfqmath.UnitsToMilliSatoshi(
122+
allowedMarginUnits, rate,
106123
)
107124

125+
marginAmt += lnwire.MilliSatoshi(numHTLCs)
126+
108127
// The difference should be within the newly allowed margin.
109128
require.LessOrEqual(
110129
t,
111-
invoiceAmtMsat-inboundAmountMSat, newAllowedMarginMSat,
130+
invoiceAmtMsat-inboundAmountMSat, marginAmt,
112131
)
113132
})
114133
}

0 commit comments

Comments
 (0)