@@ -89,26 +89,46 @@ 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. 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
+ }
100
113
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 ,
103
121
)
104
- newAllowedMarginMSat := rfqmath .UnitsToMilliSatoshi (
105
- newMarginAssetUnits , rate ,
122
+ marginAmt := rfqmath .UnitsToMilliSatoshi (
123
+ allowedMarginUnits , rate ,
106
124
)
107
125
126
+ marginAmt += lnwire .MilliSatoshi (numHTLCs )
127
+
108
128
// The difference should be within the newly allowed margin.
109
129
require .LessOrEqual (
110
130
t ,
111
- invoiceAmtMsat - inboundAmountMSat , newAllowedMarginMSat ,
131
+ invoiceAmtMsat - inboundAmountMSat , marginAmt ,
112
132
)
113
133
})
114
134
}
0 commit comments