@@ -89,26 +89,45 @@ func TestUnitConversionToleranceRapid(t *testing.T) {
89
89
90
90
shardSizeMSat := invoiceAmtMsat / lnwire .MilliSatoshi (numHTLCs )
91
91
shardSizeFP := rfqmath .MilliSatoshiToUnits (shardSizeMSat , rate )
92
- shardSizeUnit := shardSizeFP .ScaleTo (0 ).ToUint64 ()
93
92
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
+ )
100
109
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 ,
103
120
)
104
- newAllowedMarginMSat := rfqmath .UnitsToMilliSatoshi (
105
- newMarginAssetUnits , rate ,
121
+ marginAmt := rfqmath .UnitsToMilliSatoshi (
122
+ allowedMarginUnits , rate ,
106
123
)
107
124
125
+ marginAmt += lnwire .MilliSatoshi (numHTLCs )
126
+
108
127
// The difference should be within the newly allowed margin.
109
128
require .LessOrEqual (
110
129
t ,
111
- invoiceAmtMsat - inboundAmountMSat , newAllowedMarginMSat ,
130
+ invoiceAmtMsat - inboundAmountMSat , marginAmt ,
112
131
)
113
132
})
114
133
}
0 commit comments