Fix underflow rounding issue:

Very small payment could fail when STAmount::mulRound underflowed
and returned zero, when it should have rounded up to the smallest
representable value.
This commit is contained in:
seelabs
2015-11-04 16:18:43 -05:00
committed by Nik Bougalis
parent 4626992474
commit 999701e384
15 changed files with 200 additions and 50 deletions

View File

@@ -67,12 +67,13 @@ Quality::operator-- (int)
}
Amounts
Quality::ceil_in (Amounts const& amount, STAmount const& limit) const
Quality::ceil_in (Amounts const& amount, STAmount const& limit,
STAmountCalcSwitchovers const& switchovers) const
{
if (amount.in > limit)
{
Amounts result (limit, divRound (
limit, rate(), amount.out.issue (), true));
limit, rate(), amount.out.issue (), true, switchovers));
// Clamp out
if (result.out > amount.out)
result.out = amount.out;
@@ -84,12 +85,13 @@ Quality::ceil_in (Amounts const& amount, STAmount const& limit) const
}
Amounts
Quality::ceil_out (Amounts const& amount, STAmount const& limit) const
Quality::ceil_out (Amounts const& amount, STAmount const& limit,
STAmountCalcSwitchovers const& switchovers) const
{
if (amount.out > limit)
{
Amounts result (mulRound (
limit, rate(), amount.in.issue (), true), limit);
limit, rate(), amount.in.issue (), true, switchovers), limit);
// Clamp in
if (result.in > amount.in)
result.in = amount.in;
@@ -101,7 +103,8 @@ Quality::ceil_out (Amounts const& amount, STAmount const& limit) const
}
Quality
composed_quality (Quality const& lhs, Quality const& rhs)
composed_quality (Quality const& lhs, Quality const& rhs,
STAmountCalcSwitchovers const& switchovers)
{
STAmount const lhs_rate (lhs.rate ());
assert (lhs_rate != zero);
@@ -110,7 +113,7 @@ composed_quality (Quality const& lhs, Quality const& rhs)
assert (rhs_rate != zero);
STAmount const rate (mulRound (
lhs_rate, rhs_rate, lhs_rate.issue (), true));
lhs_rate, rhs_rate, lhs_rate.issue (), true, switchovers));
std::uint64_t const stored_exponent (rate.exponent () + 100);
std::uint64_t const stored_mantissa (rate.mantissa());