mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-04 17:27:00 +00:00
Compare commits
158 Commits
a1q123456/
...
ximinez/nu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ce2752cad | ||
|
|
8c1b12c5f2 | ||
|
|
4ac4ed179a | ||
|
|
1e2d25b273 | ||
|
|
647685695e | ||
|
|
6e1ee4720a | ||
|
|
f6a26ca34f | ||
|
|
c0569037f8 | ||
|
|
be9ae88d48 | ||
|
|
cd21d74538 | ||
|
|
2fdfd2b420 | ||
|
|
06a3f76ccd | ||
|
|
dadf4d737d | ||
|
|
7b66b42713 | ||
|
|
f622707b36 | ||
|
|
18ac8a0583 | ||
|
|
de2efa5cb9 | ||
|
|
8dcd88e83c | ||
|
|
5333422402 | ||
|
|
4ec049e727 | ||
|
|
ae9c72bb7c | ||
|
|
5abecb9fcb | ||
|
|
7c45a3b197 | ||
|
|
12670b0c3f | ||
|
|
1e7876a03c | ||
|
|
e851e80de0 | ||
|
|
a963035f76 | ||
|
|
8ab904de57 | ||
|
|
100ec464d9 | ||
|
|
e89e6f50e8 | ||
|
|
27456fa439 | ||
|
|
d6844311c0 | ||
|
|
fbee0349f5 | ||
|
|
84ca271d95 | ||
|
|
75dfc65f5f | ||
|
|
48b1716e6f | ||
|
|
4ab886bcbc | ||
|
|
7f64c337d8 | ||
|
|
61bdd6fb78 | ||
|
|
19c60924a5 | ||
|
|
8e06e78f11 | ||
|
|
42fda85fbc | ||
|
|
3a4b92b050 | ||
|
|
aea19df3c1 | ||
|
|
8b56749ca3 | ||
|
|
71cf996fc6 | ||
|
|
7cacb3cce5 | ||
|
|
4c7ea64b6c | ||
|
|
c8947c6f75 | ||
|
|
09ae5b719f | ||
|
|
09f2d06dd4 | ||
|
|
6964013941 | ||
|
|
ad32568f7e | ||
|
|
70c6e01d7e | ||
|
|
ddfb7ee69c | ||
|
|
29eb9a6df4 | ||
|
|
b69b9242e2 | ||
|
|
cd2fcf0a5e | ||
|
|
69656d6b67 | ||
|
|
06b9e18333 | ||
|
|
46b946b22e | ||
|
|
ae03b30f29 | ||
|
|
974f36fc72 | ||
|
|
4c7c019add | ||
|
|
47f30c913d | ||
|
|
e50bb3d307 | ||
|
|
d7d5b83f6d | ||
|
|
abc5f59fed | ||
|
|
e22938d69f | ||
|
|
dae0943dc3 | ||
|
|
7c9a56ff24 | ||
|
|
5a40416673 | ||
|
|
f483118498 | ||
|
|
30334cd1f4 | ||
|
|
257da7972f | ||
|
|
5558e1b522 | ||
|
|
cd0f49a003 | ||
|
|
22d2703ce8 | ||
|
|
501b027a76 | ||
|
|
d03274b731 | ||
|
|
1b6047afe1 | ||
|
|
668fa65384 | ||
|
|
175a04160d | ||
|
|
b050c151f8 | ||
|
|
a2b21d75ce | ||
|
|
b40d2a8e7d | ||
|
|
54db82dc42 | ||
|
|
1b67c2260c | ||
|
|
257e568cb6 | ||
|
|
fd2040a56d | ||
|
|
c06504353c | ||
|
|
51ab048b97 | ||
|
|
fc569b9410 | ||
|
|
c2f25c2a34 | ||
|
|
ee33d98f50 | ||
|
|
1226255662 | ||
|
|
d914b633da | ||
|
|
e3b390f949 | ||
|
|
5a7be26402 | ||
|
|
25e0b4eeb5 | ||
|
|
c4ef1e6997 | ||
|
|
ac20f3221f | ||
|
|
1e764cd172 | ||
|
|
371d3a6f30 | ||
|
|
018e36f1ca | ||
|
|
ee9b486f8b | ||
|
|
47f6422ee7 | ||
|
|
8201d0330e | ||
|
|
9ac062c5a0 | ||
|
|
f631d95585 | ||
|
|
efa3328aba | ||
|
|
9daa985bf1 | ||
|
|
d3b1ee9ec0 | ||
|
|
e160b95aef | ||
|
|
14843e15d8 | ||
|
|
f8359d9b0c | ||
|
|
8ed8b52dfe | ||
|
|
62d0b07ee8 | ||
|
|
44ea0b24c8 | ||
|
|
e443a76d83 | ||
|
|
eef1f791e8 | ||
|
|
2191ef8d75 | ||
|
|
ea4f922492 | ||
|
|
9250ba9e27 | ||
|
|
61f38ba068 | ||
|
|
3d5ff2c8a2 | ||
|
|
e27249134a | ||
|
|
d79fdec886 | ||
|
|
024d05b70c | ||
|
|
ffb3e1da53 | ||
|
|
aef7e5b335 | ||
|
|
e2c09e79d0 | ||
|
|
c6f854bbd8 | ||
|
|
6a1e0b0f5a | ||
|
|
01f5ae0927 | ||
|
|
9b4587f9af | ||
|
|
fbc6f87983 | ||
|
|
0871eb0cb6 | ||
|
|
2ccf132f79 | ||
|
|
6600153958 | ||
|
|
fff73dac51 | ||
|
|
06ff77458a | ||
|
|
f19ecb3b80 | ||
|
|
cc2406bf3f | ||
|
|
30c65320e4 | ||
|
|
569d9ea94e | ||
|
|
02b7bcfa2b | ||
|
|
07c0c320a7 | ||
|
|
d57e37c34b | ||
|
|
154bb65c35 | ||
|
|
111eda22e9 | ||
|
|
f7b6834d2a | ||
|
|
e464adaee6 | ||
|
|
cca92dedca | ||
|
|
3d6f57a4df | ||
|
|
fc29fbe946 | ||
|
|
5e0a8d5c8a | ||
|
|
d27788f12a |
@@ -8,7 +8,6 @@ libxrpl.crypto > xrpl.basics
|
||||
libxrpl.json > xrpl.basics
|
||||
libxrpl.json > xrpl.json
|
||||
libxrpl.ledger > xrpl.basics
|
||||
libxrpl.ledger > xrpl.core
|
||||
libxrpl.ledger > xrpl.json
|
||||
libxrpl.ledger > xrpl.ledger
|
||||
libxrpl.ledger > xrpl.nodestore
|
||||
@@ -196,7 +195,6 @@ tests.libxrpl > xrpl.net
|
||||
tests.libxrpl > xrpl.nodestore
|
||||
tests.libxrpl > xrpl.protocol
|
||||
tests.libxrpl > xrpl.protocol_autogen
|
||||
tests.libxrpl > xrpl.rdb
|
||||
tests.libxrpl > xrpl.server
|
||||
tests.libxrpl > xrpl.shamap
|
||||
tests.libxrpl > xrpl.tx
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
@@ -13,6 +14,10 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#endif // !defined(_MSC_VER)
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
class Number;
|
||||
@@ -20,18 +25,39 @@ class Number;
|
||||
std::string
|
||||
to_string(Number const& amount);
|
||||
|
||||
/** Returns a rough estimate of log10(value).
|
||||
*
|
||||
* The return value is a pair (log, rem), where log is the estimated
|
||||
* base-10 logarithm (roughly floor(log10(value))), and rem is value with
|
||||
* all trailing 0s removed (i.e., divided by the largest power of 10 that
|
||||
* evenly divides value). If rem is 1, then value is an exact power of ten, and
|
||||
* log is the exact log10(value).
|
||||
*
|
||||
* This function only works for positive values.
|
||||
*/
|
||||
template <std::unsigned_integral T>
|
||||
constexpr std::pair<int, T>
|
||||
logTenEstimate(T value)
|
||||
{
|
||||
int log = 0;
|
||||
T remainder = value;
|
||||
while (value >= 10)
|
||||
{
|
||||
if (value % 10 == 0)
|
||||
remainder = remainder / 10;
|
||||
value /= 10;
|
||||
++log;
|
||||
}
|
||||
return {log, remainder};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr std::optional<int>
|
||||
logTen(T value)
|
||||
{
|
||||
int log = 0;
|
||||
while (value >= 10 && value % 10 == 0)
|
||||
{
|
||||
value /= 10;
|
||||
++log;
|
||||
}
|
||||
if (value == 1)
|
||||
return log;
|
||||
auto const est = logTenEstimate(value);
|
||||
if (est.second == 1)
|
||||
return est.first;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -86,12 +112,9 @@ static_assert(
|
||||
/** MantissaRange defines a range for the mantissa of a normalized Number.
|
||||
*
|
||||
* The mantissa is in the range [min, max], where
|
||||
* * min is a power of 10, and
|
||||
* * max = min * 10 - 1.
|
||||
*
|
||||
* The MantissaScale enum indicates properties of the range: size, and some behavioral
|
||||
* options. This intentionally restricts the number of unique MantissaRanges that can
|
||||
* be instantiated: one for each scale.
|
||||
* The MantissaScale enum indicates properties of the range: size, and some behavioral options.
|
||||
* This intentionally prevents the creation of any MantissaRanges representing other values.
|
||||
*
|
||||
* The "Small" scale is based on the behavior of STAmount for IOUs. It has a min
|
||||
* value of 10^15, and a max value of 10^16-1. This was sufficient for
|
||||
@@ -105,12 +128,14 @@ static_assert(
|
||||
* "large" scale.
|
||||
*
|
||||
* The "Large" scales are intended to represent all values that can be represented
|
||||
* by an STAmount - IOUs, XRP, and MPTs. It has a min value of 10^18, and a max
|
||||
* value of 10^19-1. "LargeLegacy" is like "Large", but preserves
|
||||
* a rounding error when a computation results in a mantissa of
|
||||
* Number::kMaxRep that needs to be rounded up, but rounds down
|
||||
* instead. It will maintain consistent behavior until the fixCleanup3_2_0
|
||||
* amendment is enabled.
|
||||
* by an STAmount - IOUs, XRP, and MPTs.
|
||||
*
|
||||
* They have a min value of 2^63/10+1 (truncated), and a max value of 2^63-1.
|
||||
*
|
||||
* "LargeLegacy" is like "Large", but preserves a rounding error when
|
||||
* a computation results in a mantissa of Number::kLargestMantissa that needs to
|
||||
* be rounded up, but rounds down instead. It will maintain consistent
|
||||
* behavior until the fixCleanup3_2_0 amendment is enabled.
|
||||
*
|
||||
* Note that if the mentioned amendments are eventually retired, this class
|
||||
* should be left in place, but the "Small" scale option should be removed. This
|
||||
@@ -135,12 +160,39 @@ struct MantissaRange final
|
||||
|
||||
explicit constexpr MantissaRange(MantissaScale sc) : scale(sc)
|
||||
{
|
||||
// Keep the error messages terse. Since this is constexpr, if any of these throw, it won't
|
||||
// compile, so there's no real need to worry about runtime exceptions here.
|
||||
if (min * 10 <= max)
|
||||
throw std::out_of_range("Invalid mantissa range: min * 10 <= max");
|
||||
if (max / 10 >= min)
|
||||
throw std::out_of_range("Invalid mantissa range: max / 10 >= min");
|
||||
if ((min - 1) * 10 > max)
|
||||
throw std::out_of_range("Invalid mantissa range: (min - 1) * 10 > max");
|
||||
// This is a little hacky
|
||||
if ((max + 10) / 10 < min)
|
||||
throw std::out_of_range("Invalid mantissa range: (max + 10) / 10 < min");
|
||||
if (internalMin != kPowerOfTen[log])
|
||||
throw std::out_of_range("Invalid mantissa range: internalMin != kPowersOfTen[log]");
|
||||
}
|
||||
|
||||
// Explicitly delete copy and move operations
|
||||
MantissaRange(MantissaRange const&) = delete;
|
||||
MantissaRange(MantissaRange&&) = delete;
|
||||
MantissaRange&
|
||||
operator=(MantissaRange const&) = delete;
|
||||
MantissaRange&
|
||||
operator=(MantissaRange&&) = delete;
|
||||
|
||||
MantissaScale const scale;
|
||||
int const log{getExponent(scale)};
|
||||
rep const min{getMin(scale, log)};
|
||||
rep const max{(min * 10) - 1};
|
||||
rep const max{getMax(scale, log)};
|
||||
rep const min{computeMin(max)};
|
||||
/* Used to determine if mantissas are in range, but have fewer digits than max.
|
||||
*
|
||||
* Unlike min, internalMin is always an exact power of 10, so a mantissa in the internal
|
||||
* representation will always have a consistent number of digits.
|
||||
*/
|
||||
rep const internalMin{getInternalMin(scale, log)};
|
||||
CuspRoundingFix const cuspRoundingFixEnabled{isCuspFixEnabled(scale)};
|
||||
|
||||
static MantissaRange const&
|
||||
@@ -169,13 +221,39 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
// Keep this function for future use with different ways to compute
|
||||
// the ranges.
|
||||
static constexpr rep
|
||||
getMin(MantissaScale scale, int exponent)
|
||||
getMax(MantissaScale scale, int log)
|
||||
{
|
||||
switch (scale)
|
||||
{
|
||||
case MantissaScale::Small:
|
||||
return kPowerOfTen[log + 1] - 1;
|
||||
case MantissaScale::LargeLegacy:
|
||||
case MantissaScale::Large:
|
||||
return std::numeric_limits<std::int64_t>::max();
|
||||
default:
|
||||
// If called in a constexpr context, this throw assures that the build fails if an
|
||||
// invalid scale is used.
|
||||
throw std::runtime_error("Unknown mantissa scale");
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr rep
|
||||
computeMin(rep max)
|
||||
{
|
||||
return (max / 10) + 1;
|
||||
}
|
||||
|
||||
static constexpr rep
|
||||
getInternalMin(MantissaScale scale, int exponent)
|
||||
{
|
||||
if (exponent < 0 || exponent >= kPowerOfTen.size())
|
||||
{
|
||||
// If called in a constexpr context, this throw assures that the build fails if an
|
||||
// invalid exponent is used.
|
||||
throw std::runtime_error("Invalid exponent"); // LCOV_EXCL_LINE
|
||||
}
|
||||
return kPowerOfTen[exponent];
|
||||
}
|
||||
|
||||
@@ -204,13 +282,26 @@ private:
|
||||
template <class T>
|
||||
concept Integral64 = std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::uint64_t>;
|
||||
|
||||
namespace detail {
|
||||
#ifdef _MSC_VER
|
||||
using uint128_t = boost::multiprecision::uint128_t;
|
||||
using int128_t = boost::multiprecision::int128_t;
|
||||
#else // !defined(_MSC_VER)
|
||||
using uint128_t = __uint128_t;
|
||||
using int128_t = __int128_t;
|
||||
#endif // !defined(_MSC_VER)
|
||||
|
||||
template <class T>
|
||||
concept UnsignedMantissa = std::is_unsigned_v<T> || std::is_same_v<T, uint128_t>;
|
||||
} // namespace detail
|
||||
|
||||
/** Number is a floating point type that can represent a wide range of values.
|
||||
*
|
||||
* It can represent all values that can be represented by an STAmount -
|
||||
* regardless of asset type - XRPAmount, MPTAmount, and IOUAmount, with at least
|
||||
* as much precision as those types require.
|
||||
*
|
||||
* ---- Internal Representation ----
|
||||
* ---- Internal Operational Representation ----
|
||||
*
|
||||
* Internally, Number is represented with three values:
|
||||
* 1. a bool sign flag,
|
||||
@@ -219,40 +310,45 @@ concept Integral64 = std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::u
|
||||
*
|
||||
* The internal mantissa is an unsigned integer in the range defined by the
|
||||
* current MantissaRange. The exponent is an integer in the range
|
||||
* [minExponent, maxExponent].
|
||||
* [kMinExponent, kMaxExponent].
|
||||
*
|
||||
* See the description of MantissaRange for more details on the ranges.
|
||||
*
|
||||
* A non-zero mantissa is (almost) always normalized, meaning it and the
|
||||
* exponent are grown or shrunk until the mantissa is in the range
|
||||
* [MantissaRange.min, MantissaRange.max].
|
||||
* [MantissaRange.internalMin, MantissaRange.internalMin * 10 - 1].
|
||||
*
|
||||
* This internal representation is only used during some operations to ensure
|
||||
* that the mantissa is a known, predictable size. The class itself stores the
|
||||
* values using the external representation described below.
|
||||
*
|
||||
* Note:
|
||||
* 1. Normalization can be disabled by using the "unchecked" ctor tag. This
|
||||
* should only be used at specific conversion points, some constexpr
|
||||
* values, and in unit tests.
|
||||
* 2. The max of the "large" range, 10^19-1, is the largest 10^X-1 value that
|
||||
* fits in an unsigned 64-bit number. (10^19-1 < 2^64-1 and
|
||||
* 10^20-1 > 2^64-1). This avoids under- and overflows.
|
||||
* 2. Unlike MantissaRange.min, internalMin is always an exact power of 10,
|
||||
* so a mantissa in the internal representation will always have a
|
||||
* consistent number of digits.
|
||||
* 3. The functions toInternal() and fromInternal() are used to convert
|
||||
* between the two representations.
|
||||
*
|
||||
* ---- External Interface ----
|
||||
*
|
||||
* The external interface of Number consists of a std::int64_t mantissa, which
|
||||
* is restricted to 63-bits, and an int exponent, which must be in the range
|
||||
* [minExponent, maxExponent]. The range of the mantissa depends on which
|
||||
* [kMinExponent, kMaxExponent]. The range of the mantissa depends on which
|
||||
* MantissaRange is currently active. For the "short" range, the mantissa will
|
||||
* be between 10^15 and 10^16-1. For the "large" range, the mantissa will be
|
||||
* between -(2^63-1) and 2^63-1. As noted above, the "large" range is needed to
|
||||
* represent the full range of valid XRP and MPT integer values accurately.
|
||||
*
|
||||
* Note:
|
||||
* 1. 2^63-1 is between 10^18 and 10^19-1, which are the limits of the "large"
|
||||
* mantissa range.
|
||||
* 1. The "large" mantissa range is (2^63/10+1) to 2^63-1. 2^63-1 is between
|
||||
* 10^18 and 10^19-1, and (2^63/10+1) is between 10^17 and 10^18-1. Thus,
|
||||
* the mantissa may have 18 or 19 digits. This value will be modified to
|
||||
* always have 19 digits before some operations to ensure consistency.
|
||||
* 2. The functions mantissa() and exponent() return the external view of the
|
||||
* Number value, specifically using a signed 63-bit mantissa. This may
|
||||
* require altering the internal representation to fit into that range
|
||||
* before the value is returned. The interface guarantees consistency of
|
||||
* the two values.
|
||||
* Number value, specifically using a signed 63-bit mantissa.
|
||||
* 3. Number cannot represent -2^63 (std::numeric_limits<std::int64_t>::min())
|
||||
* as an exact integer, but it doesn't need to, because all asset values
|
||||
* on-ledger are non-negative. This is due to implementation details of
|
||||
@@ -307,8 +403,7 @@ class Number final
|
||||
using rep = std::int64_t;
|
||||
using internalrep = MantissaRange::rep;
|
||||
|
||||
bool negative_{false};
|
||||
internalrep mantissa_{0};
|
||||
rep mantissa_{0};
|
||||
int exponent_{std::numeric_limits<int>::lowest()};
|
||||
|
||||
public:
|
||||
@@ -316,10 +411,6 @@ public:
|
||||
static constexpr int kMinExponent = -32768;
|
||||
static constexpr int kMaxExponent = 32768;
|
||||
|
||||
static constexpr internalrep kMaxRep = std::numeric_limits<rep>::max();
|
||||
static_assert(kMaxRep == 9'223'372'036'854'775'807);
|
||||
static_assert(-kMaxRep == std::numeric_limits<rep>::min() + 1);
|
||||
|
||||
// May need to make unchecked private
|
||||
struct Unchecked
|
||||
{
|
||||
@@ -397,8 +488,7 @@ public:
|
||||
friend constexpr bool
|
||||
operator==(Number const& x, Number const& y) noexcept
|
||||
{
|
||||
return x.negative_ == y.negative_ && x.mantissa_ == y.mantissa_ &&
|
||||
x.exponent_ == y.exponent_;
|
||||
return x.mantissa_ == y.mantissa_ && x.exponent_ == y.exponent_;
|
||||
}
|
||||
|
||||
friend constexpr bool
|
||||
@@ -412,8 +502,8 @@ public:
|
||||
{
|
||||
// If the two amounts have different signs (zero is treated as positive)
|
||||
// then the comparison is true iff the left is negative.
|
||||
bool const lneg = x.negative_;
|
||||
bool const rneg = y.negative_;
|
||||
bool const lneg = x.mantissa_ < 0;
|
||||
bool const rneg = y.mantissa_ < 0;
|
||||
|
||||
if (lneg != rneg)
|
||||
return lneg;
|
||||
@@ -441,9 +531,11 @@ public:
|
||||
[[nodiscard]] constexpr int
|
||||
signum() const noexcept
|
||||
{
|
||||
if (negative_)
|
||||
if (mantissa_ < 0)
|
||||
{
|
||||
return -1;
|
||||
return (mantissa_ != 0u) ? 1 : 0;
|
||||
}
|
||||
return (mantissa_ != 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
[[nodiscard]] Number
|
||||
@@ -482,6 +574,9 @@ public:
|
||||
friend Number
|
||||
root2(Number f);
|
||||
|
||||
friend Number
|
||||
power(Number const& f, unsigned n, unsigned d);
|
||||
|
||||
// Thread local rounding control. Default is to_nearest
|
||||
enum class RoundingMode { ToNearest, TowardsZero, Downward, Upward };
|
||||
|
||||
@@ -535,6 +630,18 @@ public:
|
||||
normalizeToRange() const;
|
||||
|
||||
private:
|
||||
/** May use ranges that don't fit the restrictions of the "real"
|
||||
* normalizeToRange().
|
||||
*
|
||||
*/
|
||||
template <Integral64 T>
|
||||
[[nodiscard]]
|
||||
std::pair<T, int>
|
||||
normalizeToRangeImpl(T minMantissa, T maxMantissa, MantissaRange::CuspRoundingFix fix) const;
|
||||
|
||||
// Number_test needs to use normalizeToRangeImpl
|
||||
friend class Number_test;
|
||||
|
||||
static thread_local RoundingMode mode;
|
||||
// The available ranges for mantissa
|
||||
|
||||
@@ -543,6 +650,14 @@ private:
|
||||
// changing the values inside the range.
|
||||
static thread_local std::reference_wrapper<MantissaRange const> kRange;
|
||||
|
||||
// And one is needed because it needs to choose between oneSmall and
|
||||
// oneLarge based on the current range
|
||||
static Number
|
||||
one(MantissaRange const& range);
|
||||
|
||||
static Number
|
||||
root(MantissaRange const& range, Number f, unsigned d);
|
||||
|
||||
void
|
||||
normalize(MantissaRange const& range);
|
||||
|
||||
@@ -573,6 +688,10 @@ private:
|
||||
MantissaRange::CuspRoundingFix cuspRoundingFixEnabled,
|
||||
bool dropped);
|
||||
|
||||
[[nodiscard]]
|
||||
bool
|
||||
isnormal(MantissaRange const& range) const noexcept;
|
||||
|
||||
[[nodiscard]] bool
|
||||
isnormal() const noexcept;
|
||||
|
||||
@@ -582,18 +701,66 @@ private:
|
||||
[[nodiscard]] Number
|
||||
shiftExponent(int exponentDelta) const;
|
||||
|
||||
// Safely convert rep (int64) mantissa to internalrep (uint64). If the rep
|
||||
// is negative, returns the positive value. This takes a little extra work
|
||||
// because converting std::numeric_limits<std::int64_t>::min() flirts with
|
||||
// UB, and can vary across compilers.
|
||||
// Safely return the absolute value of a rep (int64) mantissa as an internalrep (uint64).
|
||||
static internalrep
|
||||
externalToInternal(rep mantissa);
|
||||
|
||||
/** Breaks down the number into components, potentially de-normalizing it.
|
||||
*
|
||||
* Ensures that the mantissa always has kRange.log + 1 digits.
|
||||
*
|
||||
*/
|
||||
template <detail::UnsignedMantissa Rep = internalrep>
|
||||
std::tuple<bool, Rep, int>
|
||||
toInternal(MantissaRange const& range) const;
|
||||
|
||||
/** Breaks down the number into components, potentially de-normalizing it.
|
||||
*
|
||||
* Ensures that the mantissa always has kRange.log + 1 digits.
|
||||
*
|
||||
*/
|
||||
template <detail::UnsignedMantissa Rep = internalrep>
|
||||
std::tuple<bool, Rep, int>
|
||||
toInternal() const;
|
||||
|
||||
/** Rebuilds the number from components.
|
||||
*
|
||||
* If "expectNormal" is true, the values are expected to be normalized - all
|
||||
* in their valid ranges.
|
||||
*
|
||||
* If "expectNormal" is false, the values are expected to be "near
|
||||
* normalized", meaning that the mantissa has to be modified at most once to
|
||||
* bring it back into range.
|
||||
*
|
||||
*/
|
||||
template <bool ExpectNormal = true, detail::UnsignedMantissa Rep = internalrep>
|
||||
void
|
||||
fromInternal(bool negative, Rep mantissa, int exponent, MantissaRange const* pRange);
|
||||
|
||||
/** Rebuilds the number from components.
|
||||
*
|
||||
* If "expectNormal" is true, the values are expected to be normalized - all
|
||||
* in their valid ranges.
|
||||
*
|
||||
* If "expectNormal" is false, the values are expected to be "near
|
||||
* normalized", meaning that the mantissa has to be modified at most once to
|
||||
* bring it back into range.
|
||||
*
|
||||
*/
|
||||
template <bool ExpectNormal = true, detail::UnsignedMantissa Rep = internalrep>
|
||||
void
|
||||
fromInternal(bool negative, Rep mantissa, int exponent);
|
||||
|
||||
class Guard;
|
||||
|
||||
public:
|
||||
constexpr static internalrep kLargestMantissa =
|
||||
MantissaRange{MantissaRange::MantissaScale::Large}.max;
|
||||
};
|
||||
|
||||
constexpr Number::Number(bool negative, internalrep mantissa, int exponent, Unchecked) noexcept
|
||||
: negative_(negative), mantissa_{mantissa}, exponent_{exponent}
|
||||
: mantissa_{negative ? -static_cast<rep>(mantissa) : static_cast<rep>(mantissa)}
|
||||
, exponent_{exponent}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -604,12 +771,6 @@ constexpr Number::Number(internalrep mantissa, int exponent, Unchecked) noexcept
|
||||
|
||||
static constexpr Number kNumZero{};
|
||||
|
||||
inline Number::Number(bool negative, internalrep mantissa, int exponent, Normalized)
|
||||
: Number(negative, mantissa, exponent, Unchecked{})
|
||||
{
|
||||
normalize(kRange);
|
||||
}
|
||||
|
||||
inline Number::Number(internalrep mantissa, int exponent, Normalized)
|
||||
: Number(false, mantissa, exponent, Normalized{})
|
||||
{
|
||||
@@ -632,17 +793,7 @@ inline Number::Number(rep mantissa) : Number{mantissa, 0}
|
||||
constexpr Number::rep
|
||||
Number::mantissa() const noexcept
|
||||
{
|
||||
auto m = mantissa_;
|
||||
if (m > kMaxRep)
|
||||
{
|
||||
XRPL_ASSERT_PARTS(
|
||||
!isnormal() || (m % 10 == 0 && m / 10 <= kMaxRep),
|
||||
"xrpl::Number::mantissa",
|
||||
"large normalized mantissa has no remainder");
|
||||
m /= 10;
|
||||
}
|
||||
auto const sign = negative_ ? -1 : 1;
|
||||
return sign * static_cast<Number::rep>(m);
|
||||
return mantissa_;
|
||||
}
|
||||
|
||||
/** Returns the exponent of the external view of the Number.
|
||||
@@ -653,16 +804,7 @@ Number::mantissa() const noexcept
|
||||
constexpr int
|
||||
Number::exponent() const noexcept
|
||||
{
|
||||
auto e = exponent_;
|
||||
if (mantissa_ > kMaxRep)
|
||||
{
|
||||
XRPL_ASSERT_PARTS(
|
||||
!isnormal() || (mantissa_ % 10 == 0 && mantissa_ / 10 <= kMaxRep),
|
||||
"xrpl::Number::exponent",
|
||||
"large normalized mantissa has no remainder");
|
||||
++e;
|
||||
}
|
||||
return e;
|
||||
return exponent_;
|
||||
}
|
||||
|
||||
constexpr Number
|
||||
@@ -677,7 +819,7 @@ Number::operator-() const noexcept
|
||||
if (mantissa_ == 0)
|
||||
return Number{};
|
||||
auto x = *this;
|
||||
x.negative_ = !x.negative_;
|
||||
x.mantissa_ = -x.mantissa_;
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -758,23 +900,29 @@ Number::min() noexcept
|
||||
inline Number
|
||||
Number::max() noexcept
|
||||
{
|
||||
return Number{false, std::min(kRange.get().max, kMaxRep), kMaxExponent, Unchecked{}};
|
||||
return Number{false, kRange.get().max, kMaxExponent, Unchecked{}};
|
||||
}
|
||||
|
||||
inline Number
|
||||
Number::lowest() noexcept
|
||||
{
|
||||
return Number{true, std::min(kRange.get().max, kMaxRep), kMaxExponent, Unchecked{}};
|
||||
return Number{true, kRange.get().max, kMaxExponent, Unchecked{}};
|
||||
}
|
||||
|
||||
inline bool
|
||||
Number::isnormal(MantissaRange const& range) const noexcept
|
||||
{
|
||||
auto const absM = externalToInternal(mantissa_);
|
||||
|
||||
return *this == Number{} ||
|
||||
(range.min <= absM && absM <= range.max && //
|
||||
kMinExponent <= exponent_ && exponent_ <= kMaxExponent);
|
||||
}
|
||||
|
||||
inline bool
|
||||
Number::isnormal() const noexcept
|
||||
{
|
||||
MantissaRange const& range = kRange;
|
||||
auto const absM = mantissa_;
|
||||
return *this == Number{} ||
|
||||
(range.min <= absM && absM <= range.max && (absM <= kMaxRep || absM % 10 == 0) &&
|
||||
kMinExponent <= exponent_ && exponent_ <= kMaxExponent);
|
||||
return isnormal(kRange);
|
||||
}
|
||||
|
||||
template <auto MinMantissa, auto MaxMantissa, Integral64 T>
|
||||
@@ -788,12 +936,28 @@ Number::normalizeToRange() const
|
||||
auto constexpr kMAX = static_cast<T>(MaxMantissa);
|
||||
static_assert(kMIN > 0);
|
||||
static_assert(kMIN % 10 == 0);
|
||||
static_assert(isPowerOfTen(kMIN));
|
||||
static_assert(isPowerOfTen(static_cast<std::make_unsigned_t<T>>(kMIN)));
|
||||
static_assert(kMAX % 10 == 9);
|
||||
static_assert((kMAX + 1) / 10 == kMIN);
|
||||
|
||||
bool negative = negative_;
|
||||
internalrep mantissa = mantissa_;
|
||||
// Don't need to worry about the cuspRounding fix because rounding up will never take the
|
||||
// mantissa over maxMantissa with a ones digit value other than 0. 0 can safely be truncated.
|
||||
return normalizeToRangeImpl(kMIN, kMAX, MantissaRange::CuspRoundingFix::Disabled);
|
||||
}
|
||||
|
||||
/** Only intended to be used in tests
|
||||
*
|
||||
* May use ranges that don't fit the restrictions of the "real"
|
||||
* normalizeToRange().
|
||||
*
|
||||
*/
|
||||
template <Integral64 T>
|
||||
[[nodiscard]]
|
||||
std::pair<T, int>
|
||||
Number::normalizeToRangeImpl(T minMantissa, T maxMantissa, MantissaRange::CuspRoundingFix fix) const
|
||||
{
|
||||
bool negative = mantissa_ < 0;
|
||||
internalrep mantissa = externalToInternal(mantissa_);
|
||||
int exponent = exponent_;
|
||||
|
||||
if constexpr (std::is_unsigned_v<T>)
|
||||
@@ -802,14 +966,21 @@ Number::normalizeToRange() const
|
||||
!negative,
|
||||
"xrpl::Number::normalizeToRange",
|
||||
"Number is non-negative for unsigned range.");
|
||||
// To avoid logical errors in release builds, throw if the Number is
|
||||
// negative for an unsigned range.
|
||||
if (negative)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Number::normalizeToRange: Number is negative for "
|
||||
"unsigned range.");
|
||||
}
|
||||
}
|
||||
// Don't need to worry about the cuspRounding fix because rounding up will never take the
|
||||
// mantissa over maxMantissa with a ones digit value other than 0. 0 can safely be truncated.
|
||||
Number::normalize(
|
||||
negative, mantissa, exponent, kMIN, kMAX, MantissaRange::CuspRoundingFix::Disabled);
|
||||
Number::normalize(negative, mantissa, exponent, minMantissa, maxMantissa, fix);
|
||||
|
||||
auto const sign = negative ? -1 : 1;
|
||||
return std::make_pair(static_cast<T>(sign * mantissa), exponent);
|
||||
// Cast mantissa to signed type first (if T is a signed type) to avoid
|
||||
// unsigned integer overflow when multiplying by negative sign
|
||||
T signedMantissa = negative ? -static_cast<T>(mantissa) : static_cast<T>(mantissa);
|
||||
return std::make_pair(signedMantissa, exponent);
|
||||
}
|
||||
|
||||
constexpr Number
|
||||
|
||||
@@ -231,8 +231,8 @@ constexpr std::size_t kMaxPermissionedDomainCredentialsArraySize = 10;
|
||||
constexpr std::size_t kMaxMpTokenMetadataLength = 1024;
|
||||
|
||||
/** The maximum amount of MPTokenIssuance */
|
||||
constexpr std::uint64_t kMaxMpTokenAmount = 0x7FFF'FFFF'FFFF'FFFFull;
|
||||
static_assert(Number::kMaxRep >= kMaxMpTokenAmount);
|
||||
std::uint64_t constexpr kMaxMpTokenAmount = 0x7FFF'FFFF'FFFF'FFFFull;
|
||||
static_assert(Number::kLargestMantissa >= kMaxMpTokenAmount);
|
||||
|
||||
/** The maximum length of Data payload */
|
||||
constexpr std::size_t kMaxDataPayloadLength = 256;
|
||||
|
||||
@@ -559,6 +559,8 @@ STAmount::fromNumber(A const& a, Number const& number)
|
||||
return STAmount{asset, intValue, 0, negative};
|
||||
}
|
||||
|
||||
XRPL_ASSERT_PARTS(
|
||||
working.signum() >= 0, "xrpl::STAmount::fromNumber", "non-negative Number to normalize");
|
||||
auto const [mantissa, exponent] = working.normalizeToRange<kMinValue, kMaxValue>();
|
||||
|
||||
return STAmount{asset, mantissa, exponent, negative};
|
||||
|
||||
@@ -23,7 +23,7 @@ systemName()
|
||||
/** Number of drops in the genesis account. */
|
||||
constexpr XRPAmount kInitialXrp{100'000'000'000 * kDropsPerXrp};
|
||||
static_assert(kInitialXrp.drops() == 100'000'000'000'000'000);
|
||||
static_assert(Number::kMaxRep >= kInitialXrp.drops());
|
||||
static_assert(Number::kLargestMantissa >= kInitialXrp.drops());
|
||||
|
||||
/** Returns true if the amount does not exceed the initial XRP in existence. */
|
||||
inline bool
|
||||
|
||||
@@ -8,24 +8,22 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma message("Using boost::multiprecision::uint128_t and int128_t")
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
using uint128_t = boost::multiprecision::uint128_t;
|
||||
using int128_t = boost::multiprecision::int128_t;
|
||||
#else // !defined(_MSC_VER)
|
||||
using uint128_t = __uint128_t;
|
||||
using int128_t = __int128_t;
|
||||
#endif // !defined(_MSC_VER)
|
||||
#endif
|
||||
|
||||
using uint128_t = xrpl::detail::uint128_t;
|
||||
using int128_t = xrpl::detail::int128_t;
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -60,33 +58,39 @@ MantissaRange::getRanges()
|
||||
[[maybe_unused]]
|
||||
constexpr static MantissaRange kRange{MantissaRange::MantissaScale::Small};
|
||||
static_assert(isPowerOfTen(kRange.min));
|
||||
static_assert(isPowerOfTen(kRange.internalMin));
|
||||
static_assert(kRange.min == 1'000'000'000'000'000LL);
|
||||
static_assert(kRange.internalMin == kRange.min);
|
||||
static_assert(kRange.max == 9'999'999'999'999'999LL);
|
||||
static_assert(kRange.log == 15);
|
||||
static_assert(kRange.min < Number::kMaxRep);
|
||||
static_assert(kRange.max < Number::kMaxRep);
|
||||
static_assert(kRange.min < Number::kLargestMantissa);
|
||||
static_assert(kRange.max < Number::kLargestMantissa);
|
||||
static_assert(kRange.cuspRoundingFixEnabled == CuspRoundingFix::Disabled);
|
||||
}
|
||||
{
|
||||
[[maybe_unused]]
|
||||
constexpr static MantissaRange kRange{MantissaRange::MantissaScale::LargeLegacy};
|
||||
static_assert(isPowerOfTen(kRange.min));
|
||||
static_assert(kRange.min == 1'000'000'000'000'000'000ULL);
|
||||
static_assert(kRange.max == rep(9'999'999'999'999'999'999ULL));
|
||||
static_assert(!isPowerOfTen(kRange.min));
|
||||
static_assert(isPowerOfTen(kRange.internalMin));
|
||||
static_assert(kRange.min == 922'337'203'685'477'581ULL);
|
||||
static_assert(kRange.internalMin == 1'000'000'000'000'000'000ULL);
|
||||
static_assert(kRange.max == rep(9'223'372'036'854'775'807ULL));
|
||||
static_assert(kRange.log == 18);
|
||||
static_assert(kRange.min < Number::kMaxRep);
|
||||
static_assert(kRange.max > Number::kMaxRep);
|
||||
static_assert(kRange.min < Number::kLargestMantissa);
|
||||
static_assert(kRange.max == Number::kLargestMantissa);
|
||||
static_assert(kRange.cuspRoundingFixEnabled == CuspRoundingFix::Disabled);
|
||||
}
|
||||
{
|
||||
[[maybe_unused]]
|
||||
constexpr static MantissaRange kRange{MantissaRange::MantissaScale::Large};
|
||||
static_assert(isPowerOfTen(kRange.min));
|
||||
static_assert(kRange.min == 1'000'000'000'000'000'000ULL);
|
||||
static_assert(kRange.max == rep(9'999'999'999'999'999'999ULL));
|
||||
static_assert(!isPowerOfTen(kRange.min));
|
||||
static_assert(isPowerOfTen(kRange.internalMin));
|
||||
static_assert(kRange.min == 922'337'203'685'477'581ULL);
|
||||
static_assert(kRange.internalMin == 1'000'000'000'000'000'000ULL);
|
||||
static_assert(kRange.max == rep(9'223'372'036'854'775'807ULL));
|
||||
static_assert(kRange.log == 18);
|
||||
static_assert(kRange.min < Number::kMaxRep);
|
||||
static_assert(kRange.max > Number::kMaxRep);
|
||||
static_assert(kRange.min < Number::kLargestMantissa);
|
||||
static_assert(kRange.max == Number::kLargestMantissa);
|
||||
static_assert(kRange.cuspRoundingFixEnabled == CuspRoundingFix::Enabled);
|
||||
}
|
||||
return map;
|
||||
@@ -161,9 +165,6 @@ divu10(uint128_t& u)
|
||||
// precision to an operation. This enables the final result
|
||||
// to be correctly rounded to the internal precision of Number.
|
||||
|
||||
template <class T>
|
||||
concept UnsignedMantissa = std::is_unsigned_v<T> || std::is_same_v<T, uint128_t>;
|
||||
|
||||
class Number::Guard
|
||||
{
|
||||
std::uint64_t digits_{0}; // 16 decimal guard digits
|
||||
@@ -213,7 +214,7 @@ public:
|
||||
round() const noexcept;
|
||||
|
||||
// Modify the result to the correctly rounded value
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
doRoundUp(
|
||||
bool& negative,
|
||||
@@ -222,22 +223,22 @@ public:
|
||||
internalrep const& minMantissa,
|
||||
internalrep const& maxMantissa,
|
||||
MantissaRange::CuspRoundingFix cuspRoundingFixEnabled,
|
||||
std::string location);
|
||||
std::string_view location);
|
||||
|
||||
// Modify the result to the correctly rounded value
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
doRoundDown(bool& negative, T& mantissa, int& exponent, internalrep const& minMantissa);
|
||||
|
||||
// Modify the result to the correctly rounded value
|
||||
void
|
||||
doRound(rep& drops, std::string location) const;
|
||||
doRound(internalrep& drops, std::string_view location) const;
|
||||
|
||||
private:
|
||||
void
|
||||
doPush(unsigned d) noexcept;
|
||||
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
bringIntoRange(bool& negative, T& mantissa, int& exponent, internalrep const& minMantissa);
|
||||
};
|
||||
@@ -351,7 +352,7 @@ Number::Guard::round() const noexcept
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
Number::Guard::bringIntoRange(
|
||||
bool& negative,
|
||||
@@ -370,13 +371,11 @@ Number::Guard::bringIntoRange(
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
|
||||
negative = kZero.negative_;
|
||||
mantissa = kZero.mantissa_;
|
||||
exponent = kZero.exponent_;
|
||||
std::tie(negative, mantissa, exponent) = kZero.toInternal();
|
||||
}
|
||||
}
|
||||
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
Number::Guard::doRoundUp(
|
||||
bool& negative,
|
||||
@@ -385,13 +384,13 @@ Number::Guard::doRoundUp(
|
||||
internalrep const& minMantissa,
|
||||
internalrep const& maxMantissa,
|
||||
MantissaRange::CuspRoundingFix cuspRoundingFixEnabled,
|
||||
std::string location)
|
||||
std::string_view location)
|
||||
{
|
||||
auto r = round();
|
||||
if (r == 1 || (r == 0 && (mantissa & 1) == 1))
|
||||
{
|
||||
auto const safeToIncrement = [&maxMantissa](auto const& mantissa) {
|
||||
return mantissa < maxMantissa && mantissa < kMaxRep;
|
||||
return mantissa < maxMantissa && mantissa < kLargestMantissa;
|
||||
};
|
||||
if (cuspRoundingFixEnabled == MantissaRange::CuspRoundingFix::Enabled)
|
||||
{
|
||||
@@ -407,8 +406,8 @@ Number::Guard::doRoundUp(
|
||||
// Incrementing the mantissa will require dividing, which will require rounding. So
|
||||
// _don't_ increment the mantissa. Instead, divide and round recursively. It should
|
||||
// be impossible to recurse more than once, because once the mantissa is divided by
|
||||
// 10, it will be _well_ under maxMantissa and kMaxRep, so adding 1 will have no
|
||||
// chance of bringing it back over.
|
||||
// 10, it will be _well_ under maxMantissa and kLargestMantissa, so adding 1 will
|
||||
// have no chance of bringing it back over.
|
||||
doDropDigit(mantissa, exponent);
|
||||
XRPL_ASSERT_PARTS(
|
||||
safeToIncrement(mantissa),
|
||||
@@ -432,7 +431,7 @@ Number::Guard::doRoundUp(
|
||||
++mantissa;
|
||||
// Ensure mantissa after incrementing fits within both the
|
||||
// min/maxMantissa range and is a valid "rep".
|
||||
if (mantissa > maxMantissa || mantissa > kMaxRep)
|
||||
if (mantissa > maxMantissa || mantissa > kLargestMantissa)
|
||||
{
|
||||
// Don't use doDropDigit here
|
||||
mantissa /= 10;
|
||||
@@ -445,7 +444,7 @@ Number::Guard::doRoundUp(
|
||||
Throw<std::overflow_error>(std::string(location));
|
||||
}
|
||||
|
||||
template <UnsignedMantissa T>
|
||||
template <detail::UnsignedMantissa T>
|
||||
void
|
||||
Number::Guard::doRoundDown(
|
||||
bool& negative,
|
||||
@@ -468,26 +467,25 @@ Number::Guard::doRoundDown(
|
||||
|
||||
// Modify the result to the correctly rounded value
|
||||
void
|
||||
Number::Guard::doRound(rep& drops, std::string location) const
|
||||
Number::Guard::doRound(internalrep& drops, std::string_view location) const
|
||||
{
|
||||
auto r = round();
|
||||
if (r == 1 || (r == 0 && (drops & 1) == 1))
|
||||
{
|
||||
if (drops >= kMaxRep)
|
||||
auto const& range = kRange.get();
|
||||
if (drops >= range.max)
|
||||
{
|
||||
static_assert(sizeof(internalrep) == sizeof(rep));
|
||||
// This should be impossible, because it's impossible to represent
|
||||
// "kMaxRep + 0.6" in Number, regardless of the scale. There aren't
|
||||
// enough digits available. You'd either get a mantissa of "kMaxRep"
|
||||
// or "(kMaxRep + 1) / 10", neither of which will round up when
|
||||
// "kLargestMantissa + 0.6" in Number, regardless of the scale. There aren't
|
||||
// enough digits available. You'd either get a mantissa of "kLargestMantissa"
|
||||
// or "kLargestMantissa / 10 + 1", neither of which will round up when
|
||||
// converting to rep, though the latter might overflow _before_
|
||||
// rounding.
|
||||
Throw<std::overflow_error>(std::string(location)); // LCOV_EXCL_LINE
|
||||
}
|
||||
++drops;
|
||||
}
|
||||
if (isNegative())
|
||||
drops = -drops;
|
||||
}
|
||||
|
||||
// Number
|
||||
@@ -502,10 +500,6 @@ Number::externalToInternal(rep mantissa)
|
||||
// If the mantissa is already positive, just return it
|
||||
if (mantissa >= 0)
|
||||
return mantissa;
|
||||
// If the mantissa is negative, but fits within the positive range of rep,
|
||||
// return it negated
|
||||
if (mantissa >= -std::numeric_limits<rep>::max())
|
||||
return -mantissa;
|
||||
|
||||
// If the mantissa doesn't fit within the positive range, convert to
|
||||
// int128_t, negate that, and cast it back down to the internalrep
|
||||
@@ -515,11 +509,135 @@ Number::externalToInternal(rep mantissa)
|
||||
return static_cast<internalrep>(-temp);
|
||||
}
|
||||
|
||||
/** Breaks down the number into components, potentially de-normalizing it.
|
||||
*
|
||||
* Ensures that the mantissa always has kRange.log + 1 digits.
|
||||
*
|
||||
*/
|
||||
template <detail::UnsignedMantissa Rep>
|
||||
std::tuple<bool, Rep, int>
|
||||
Number::toInternal(MantissaRange const& range) const
|
||||
{
|
||||
auto exponent = exponent_;
|
||||
bool const negative = mantissa_ < 0;
|
||||
// It should be impossible for mantissa_ to be INT64_MIN, but use externalToInternal just in
|
||||
// case.
|
||||
Rep mantissa = static_cast<Rep>(externalToInternal(mantissa_));
|
||||
|
||||
auto const internalMin = range.internalMin;
|
||||
auto const minMantissa = range.min;
|
||||
|
||||
if (mantissa != 0 && mantissa >= minMantissa && mantissa < internalMin)
|
||||
{
|
||||
// Ensure the mantissa has the correct number of digits
|
||||
mantissa *= 10;
|
||||
--exponent;
|
||||
XRPL_ASSERT_PARTS(
|
||||
mantissa >= internalMin && mantissa < internalMin * 10,
|
||||
"xrpl::Number::toInternal()",
|
||||
"Number is within reference range and has 'log' digits");
|
||||
}
|
||||
|
||||
return {negative, mantissa, exponent};
|
||||
}
|
||||
|
||||
/** Breaks down the number into components, potentially de-normalizing it.
|
||||
*
|
||||
* Ensures that the mantissa always has exactly kRange.log + 1 digits.
|
||||
*
|
||||
*/
|
||||
template <detail::UnsignedMantissa Rep>
|
||||
std::tuple<bool, Rep, int>
|
||||
Number::toInternal() const
|
||||
{
|
||||
return toInternal(kRange);
|
||||
}
|
||||
|
||||
/** Rebuilds the number from components.
|
||||
*
|
||||
* If "expectNormal" is true, the values are expected to be normalized - all
|
||||
* in their valid ranges.
|
||||
*
|
||||
* If "expectNormal" is false, the values are expected to be "near
|
||||
* normalized", meaning that the mantissa has to be modified at most once to
|
||||
* bring it back into range.
|
||||
*
|
||||
*/
|
||||
template <bool ExpectNormal, detail::UnsignedMantissa Rep>
|
||||
void
|
||||
Number::fromInternal(bool negative, Rep mantissa, int exponent, MantissaRange const* pRange)
|
||||
{
|
||||
if constexpr (std::is_same_v<std::bool_constant<ExpectNormal>, std::false_type>)
|
||||
{
|
||||
if (!pRange)
|
||||
throw std::runtime_error("Missing range to Number::fromInternal!");
|
||||
auto const& range = *pRange;
|
||||
|
||||
auto const maxMantissa = range.max;
|
||||
auto const minMantissa = range.min;
|
||||
|
||||
XRPL_ASSERT_PARTS(
|
||||
mantissa >= minMantissa, "xrpl::Number::fromInternal", "mantissa large enough");
|
||||
|
||||
if (mantissa > maxMantissa || mantissa < minMantissa)
|
||||
{
|
||||
normalize(negative, mantissa, exponent, range.min, maxMantissa);
|
||||
}
|
||||
|
||||
XRPL_ASSERT_PARTS(
|
||||
mantissa >= minMantissa && mantissa <= maxMantissa,
|
||||
"xrpl::Number::fromInternal",
|
||||
"mantissa in range");
|
||||
}
|
||||
|
||||
// mantissa is unsigned, but it might not be uint64
|
||||
mantissa_ = static_cast<rep>(static_cast<internalrep>(mantissa));
|
||||
if (negative)
|
||||
mantissa_ = -mantissa_;
|
||||
exponent_ = exponent;
|
||||
|
||||
XRPL_ASSERT_PARTS(
|
||||
(pRange && isnormal(*pRange)) || isnormal(),
|
||||
"xrpl::Number::fromInternal",
|
||||
"Number is normalized");
|
||||
}
|
||||
|
||||
/** Rebuilds the number from components.
|
||||
*
|
||||
* If "expectNormal" is true, the values are expected to be normalized - all in
|
||||
* their valid ranges.
|
||||
*
|
||||
* If "expectNormal" is false, the values are expected to be "near normalized",
|
||||
* meaning that the mantissa has to be modified at most once to bring it back
|
||||
* into range.
|
||||
*
|
||||
*/
|
||||
template <bool ExpectNormal, detail::UnsignedMantissa Rep>
|
||||
void
|
||||
Number::fromInternal(bool negative, Rep mantissa, int exponent)
|
||||
{
|
||||
MantissaRange const* pRange = nullptr;
|
||||
if constexpr (std::is_same_v<std::bool_constant<ExpectNormal>, std::false_type>)
|
||||
{
|
||||
pRange = &Number::kRange.get();
|
||||
}
|
||||
|
||||
fromInternal(negative, mantissa, exponent, pRange);
|
||||
}
|
||||
|
||||
Number
|
||||
Number::one(MantissaRange const& range)
|
||||
{
|
||||
XRPL_ASSERT(isPowerOfTen(range.internalMin), "Number::one : valid range internalMin");
|
||||
auto const result = Number{false, range.internalMin, -range.log, Number::Unchecked{}};
|
||||
XRPL_ASSERT(result == 1, "Number::one : One == 1");
|
||||
return result;
|
||||
}
|
||||
|
||||
Number
|
||||
Number::one()
|
||||
{
|
||||
auto const& range = kRange.get();
|
||||
return Number{false, range.min, -range.log, Number::Unchecked{}};
|
||||
return one(kRange);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -533,20 +651,19 @@ doNormalize(
|
||||
MantissaRange::CuspRoundingFix cuspRoundingFixEnabled,
|
||||
bool dropped)
|
||||
{
|
||||
static constexpr auto kMinExponent = Number::kMinExponent;
|
||||
static constexpr auto kMaxExponent = Number::kMaxExponent;
|
||||
static constexpr auto kMaxRep = Number::kMaxRep;
|
||||
auto constexpr kMinExponent = Number::kMinExponent;
|
||||
auto constexpr kMaxExponent = Number::kMaxExponent;
|
||||
|
||||
using Guard = Number::Guard;
|
||||
|
||||
static constexpr Number kZero = Number{};
|
||||
if (mantissa == 0)
|
||||
constexpr Number kZero = Number{};
|
||||
auto const& range = Number::kRange.get();
|
||||
if (mantissa == 0 || (mantissa < minMantissa && exponent <= kMinExponent))
|
||||
{
|
||||
mantissa = kZero.mantissa_;
|
||||
exponent = kZero.exponent_;
|
||||
negative = kZero.negative_;
|
||||
std::tie(negative, mantissa, exponent) = kZero.toInternal(range);
|
||||
return;
|
||||
}
|
||||
|
||||
auto m = mantissa;
|
||||
while ((m < minMantissa) && (exponent > kMinExponent))
|
||||
{
|
||||
@@ -564,38 +681,13 @@ doNormalize(
|
||||
throw std::overflow_error("Number::normalize 1");
|
||||
g.doDropDigit(m, exponent);
|
||||
}
|
||||
if ((exponent < kMinExponent) || (m < minMantissa))
|
||||
if ((exponent < kMinExponent) || (m == 0))
|
||||
{
|
||||
mantissa = kZero.mantissa_;
|
||||
exponent = kZero.exponent_;
|
||||
negative = kZero.negative_;
|
||||
std::tie(negative, mantissa, exponent) = kZero.toInternal(range);
|
||||
return;
|
||||
}
|
||||
|
||||
// When using the largeRange, "m" needs fit within an int64, even if
|
||||
// the final mantissa is going to end up larger to fit within the
|
||||
// MantissaRange. Cut it down here so that the rounding will be done while
|
||||
// it's smaller.
|
||||
//
|
||||
// Example: 9,900,000,000,000,123,456 > 9,223,372,036,854,775,807,
|
||||
// so "m" will be modified to 990,000,000,000,012,345. Then that value
|
||||
// will be rounded to 990,000,000,000,012,345 or
|
||||
// 990,000,000,000,012,346, depending on the rounding mode. Finally,
|
||||
// mantissa will be "m*10" so it fits within the range, and end up as
|
||||
// 9,900,000,000,000,123,450 or 9,900,000,000,000,123,460.
|
||||
// mantissa() will return mantissa / 10, and exponent() will return
|
||||
// exponent + 1.
|
||||
if (m > kMaxRep)
|
||||
{
|
||||
if (exponent >= kMaxExponent)
|
||||
throw std::overflow_error("Number::normalize 1.5");
|
||||
g.doDropDigit(m, exponent);
|
||||
}
|
||||
// Before modification, m should be within the min/max range. After
|
||||
// modification, it must be less than kMaxRep. In other words, the original
|
||||
// value should have been no more than kMaxRep * 10.
|
||||
// (kMaxRep * 10 > maxMantissa)
|
||||
XRPL_ASSERT_PARTS(m <= kMaxRep, "xrpl::doNormalize", "intermediate mantissa fits in int64");
|
||||
XRPL_ASSERT_PARTS(m <= maxMantissa, "xrpl::doNormalize", "intermediate mantissa fits in int64");
|
||||
mantissa = m;
|
||||
|
||||
g.doRoundUp(
|
||||
@@ -606,10 +698,15 @@ doNormalize(
|
||||
maxMantissa,
|
||||
cuspRoundingFixEnabled,
|
||||
"Number::normalize 2");
|
||||
|
||||
XRPL_ASSERT_PARTS(
|
||||
mantissa >= minMantissa && mantissa <= maxMantissa,
|
||||
"xrpl::doNormalize",
|
||||
"final mantissa fits in range");
|
||||
XRPL_ASSERT_PARTS(
|
||||
exponent >= kMinExponent && exponent <= kMaxExponent,
|
||||
"xrpl::doNormalize",
|
||||
"final exponent fits in range");
|
||||
}
|
||||
|
||||
template <>
|
||||
@@ -665,7 +762,11 @@ Number::normalize<unsigned long>(
|
||||
void
|
||||
Number::normalize(MantissaRange const& range)
|
||||
{
|
||||
normalize(negative_, mantissa_, exponent_, range.min, range.max, range.cuspRoundingFixEnabled);
|
||||
auto [negative, mantissa, exponent] = toInternal(range);
|
||||
|
||||
normalize(negative, mantissa, exponent, range.min, range.max, range.cuspRoundingFixEnabled);
|
||||
|
||||
fromInternal(negative, mantissa, exponent, &range);
|
||||
}
|
||||
|
||||
// Copy the number, but set a new exponent. Because the mantissa doesn't change,
|
||||
@@ -675,22 +776,34 @@ Number
|
||||
Number::shiftExponent(int exponentDelta) const
|
||||
{
|
||||
XRPL_ASSERT_PARTS(isnormal(), "xrpl::Number::shiftExponent", "normalized");
|
||||
auto const newExponent = exponent_ + exponentDelta;
|
||||
if (newExponent >= kMaxExponent)
|
||||
|
||||
Number result = *this;
|
||||
|
||||
result.exponent_ += exponentDelta;
|
||||
|
||||
if (result.exponent_ >= kMaxExponent)
|
||||
throw std::overflow_error("Number::shiftExponent");
|
||||
if (newExponent < kMinExponent)
|
||||
if (result.exponent_ < kMinExponent)
|
||||
{
|
||||
return Number{};
|
||||
}
|
||||
Number const result{negative_, mantissa_, newExponent, Unchecked{}};
|
||||
XRPL_ASSERT_PARTS(result.isnormal(), "xrpl::Number::shiftExponent", "result is normalized");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Number::Number(bool negative, internalrep mantissa, int exponent, Normalized)
|
||||
{
|
||||
auto const& range = kRange.get();
|
||||
normalize(negative, mantissa, exponent, range.min, range.max, range.cuspRoundingFixEnabled);
|
||||
fromInternal(negative, mantissa, exponent, &range);
|
||||
}
|
||||
|
||||
Number&
|
||||
Number::operator+=(Number const& y)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const& range = kRange.get();
|
||||
|
||||
constexpr Number kZero = Number{};
|
||||
if (y == kZero)
|
||||
return *this;
|
||||
if (*this == kZero)
|
||||
@@ -704,7 +817,8 @@ Number::operator+=(Number const& y)
|
||||
return *this;
|
||||
}
|
||||
|
||||
XRPL_ASSERT(isnormal() && y.isnormal(), "xrpl::Number::operator+=(Number) : is normal");
|
||||
XRPL_ASSERT(
|
||||
isnormal(range) && y.isnormal(range), "xrpl::Number::operator+=(Number) : is normal");
|
||||
// *n = negative
|
||||
// *s = sign
|
||||
// *m = mantissa
|
||||
@@ -712,13 +826,10 @@ Number::operator+=(Number const& y)
|
||||
|
||||
// Need to use uint128_t, because large mantissas can overflow when added
|
||||
// together.
|
||||
bool xn = negative_;
|
||||
uint128_t xm = mantissa_;
|
||||
auto xe = exponent_;
|
||||
auto [xn, xm, xe] = toInternal<uint128_t>(range);
|
||||
|
||||
auto [yn, ym, ye] = y.toInternal<uint128_t>(range);
|
||||
|
||||
bool const yn = y.negative_;
|
||||
uint128_t ym = y.mantissa_;
|
||||
auto ye = y.exponent_;
|
||||
Guard g;
|
||||
if (xe < ye)
|
||||
{
|
||||
@@ -739,7 +850,6 @@ Number::operator+=(Number const& y)
|
||||
} while (xe > ye);
|
||||
}
|
||||
|
||||
auto const& range = kRange.get();
|
||||
auto const& minMantissa = range.min;
|
||||
auto const& maxMantissa = range.max;
|
||||
auto const cuspRoundingFixEnabled = range.cuspRoundingFixEnabled;
|
||||
@@ -747,7 +857,7 @@ Number::operator+=(Number const& y)
|
||||
if (xn == yn)
|
||||
{
|
||||
xm += ym;
|
||||
if (xm > maxMantissa || xm > kMaxRep)
|
||||
if (xm > maxMantissa)
|
||||
{
|
||||
g.doDropDigit(xm, xe);
|
||||
}
|
||||
@@ -772,7 +882,7 @@ Number::operator+=(Number const& y)
|
||||
xe = ye;
|
||||
xn = yn;
|
||||
}
|
||||
while (xm < minMantissa && xm * 10 <= kMaxRep)
|
||||
while (xm < minMantissa)
|
||||
{
|
||||
xm *= 10;
|
||||
xm -= g.pop();
|
||||
@@ -781,17 +891,17 @@ Number::operator+=(Number const& y)
|
||||
g.doRoundDown(xn, xm, xe, minMantissa);
|
||||
}
|
||||
|
||||
negative_ = xn;
|
||||
mantissa_ = static_cast<internalrep>(xm);
|
||||
exponent_ = xe;
|
||||
normalize(range);
|
||||
normalize(xn, xm, xe, minMantissa, maxMantissa, cuspRoundingFixEnabled);
|
||||
fromInternal(xn, xm, xe, &range);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Number&
|
||||
Number::operator*=(Number const& y)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const& range = kRange.get();
|
||||
|
||||
constexpr Number kZero = Number{};
|
||||
if (*this == kZero)
|
||||
return *this;
|
||||
if (y == kZero)
|
||||
@@ -804,15 +914,11 @@ Number::operator*=(Number const& y)
|
||||
// *m = mantissa
|
||||
// *e = exponent
|
||||
|
||||
bool const xn = negative_;
|
||||
auto [xn, xm, xe] = toInternal(range);
|
||||
int const xs = xn ? -1 : 1;
|
||||
internalrep xm = mantissa_;
|
||||
auto xe = exponent_;
|
||||
|
||||
bool const yn = y.negative_;
|
||||
auto [yn, ym, ye] = y.toInternal(range);
|
||||
int const ys = yn ? -1 : 1;
|
||||
internalrep const ym = y.mantissa_;
|
||||
auto ye = y.exponent_;
|
||||
|
||||
auto zm = uint128_t(xm) * uint128_t(ym);
|
||||
auto ze = xe + ye;
|
||||
@@ -822,12 +928,11 @@ Number::operator*=(Number const& y)
|
||||
if (zn)
|
||||
g.setNegative();
|
||||
|
||||
auto const& range = kRange.get();
|
||||
auto const& minMantissa = range.min;
|
||||
auto const& maxMantissa = range.max;
|
||||
auto const cuspRoundingFixEnabled = range.cuspRoundingFixEnabled;
|
||||
|
||||
while (zm > maxMantissa || zm > kMaxRep)
|
||||
while (zm > maxMantissa)
|
||||
{
|
||||
g.doDropDigit(zm, ze);
|
||||
}
|
||||
@@ -842,18 +947,18 @@ Number::operator*=(Number const& y)
|
||||
maxMantissa,
|
||||
cuspRoundingFixEnabled,
|
||||
"Number::multiplication overflow : exponent is " + std::to_string(xe));
|
||||
negative_ = zn;
|
||||
mantissa_ = xm;
|
||||
exponent_ = xe;
|
||||
|
||||
normalize(range);
|
||||
normalize(zn, xm, xe, minMantissa, maxMantissa, cuspRoundingFixEnabled);
|
||||
fromInternal(zn, xm, xe, &range);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Number&
|
||||
Number::operator/=(Number const& y)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const& range = kRange.get();
|
||||
|
||||
constexpr Number kZero = Number{};
|
||||
if (y == kZero)
|
||||
throw std::overflow_error("Number: divide by 0");
|
||||
if (*this == kZero)
|
||||
@@ -867,19 +972,14 @@ Number::operator/=(Number const& y)
|
||||
// *m = mantissa
|
||||
// *e = exponent
|
||||
|
||||
bool const np = negative_;
|
||||
int const ns = (np ? -1 : 1);
|
||||
auto nm = mantissa_;
|
||||
auto ne = exponent_;
|
||||
|
||||
bool const dp = y.negative_;
|
||||
int const ds = (dp ? -1 : 1);
|
||||
// Create the denominator as 128-bit unsigned, since that's what we
|
||||
// Create the mantissas as 128-bit unsigned, since that's what we
|
||||
// need to work with.
|
||||
uint128_t const dm = static_cast<uint128_t>(y.mantissa_);
|
||||
auto const de = y.exponent_;
|
||||
auto const [np, nm, ne] = toInternal<uint128_t>(range);
|
||||
int const ns = (np ? -1 : 1);
|
||||
|
||||
auto const [dp, dm, de] = y.toInternal<uint128_t>(range);
|
||||
int const ds = (dp ? -1 : 1);
|
||||
|
||||
auto const& range = kRange.get();
|
||||
auto const& minMantissa = range.min;
|
||||
auto const& maxMantissa = range.max;
|
||||
auto const cuspRoundingFixEnabled = range.cuspRoundingFixEnabled;
|
||||
@@ -1022,10 +1122,8 @@ Number::operator/=(Number const& y)
|
||||
}
|
||||
}
|
||||
doNormalize(zp, zm, ze, minMantissa, maxMantissa, cuspRoundingFixEnabled, dropped);
|
||||
negative_ = zp;
|
||||
mantissa_ = static_cast<internalrep>(zm);
|
||||
exponent_ = ze;
|
||||
XRPL_ASSERT_PARTS(isnormal(), "xrpl::Number::operator/=", "result is normalized");
|
||||
fromInternal(zp, zm, ze, &range);
|
||||
XRPL_ASSERT_PARTS(isnormal(range), "xrpl::Number::operator/=", "result is normalized");
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -1033,27 +1131,35 @@ Number::operator/=(Number const& y)
|
||||
Number::
|
||||
operator rep() const
|
||||
{
|
||||
rep drops = mantissa();
|
||||
auto const m = mantissa();
|
||||
// drops will always be non-negative
|
||||
internalrep drops = externalToInternal(m);
|
||||
|
||||
if (drops == 0)
|
||||
return drops;
|
||||
|
||||
int offset = exponent();
|
||||
Guard g;
|
||||
if (drops != 0)
|
||||
|
||||
if (m < 0)
|
||||
{
|
||||
if (negative_)
|
||||
{
|
||||
g.setNegative();
|
||||
drops = -drops;
|
||||
}
|
||||
while (offset < 0)
|
||||
{
|
||||
g.doDropDigit(drops, offset);
|
||||
}
|
||||
for (; offset > 0; --offset)
|
||||
{
|
||||
if (drops > kMaxRep / 10)
|
||||
throw std::overflow_error("Number::operator rep() overflow");
|
||||
drops *= 10;
|
||||
}
|
||||
g.doRound(drops, "Number::operator rep() rounding overflow");
|
||||
g.setNegative();
|
||||
}
|
||||
while (offset < 0)
|
||||
{
|
||||
g.doDropDigit(drops, offset);
|
||||
}
|
||||
for (; offset > 0; --offset)
|
||||
{
|
||||
if (drops > kLargestMantissa / 10)
|
||||
throw std::overflow_error("Number::operator rep() overflow");
|
||||
drops *= 10;
|
||||
}
|
||||
g.doRound(drops, "Number::operator rep() rounding overflow");
|
||||
|
||||
if (g.isNegative())
|
||||
{
|
||||
return -drops;
|
||||
}
|
||||
return drops;
|
||||
}
|
||||
@@ -1079,19 +1185,22 @@ Number::truncate() const noexcept
|
||||
std::string
|
||||
to_string(Number const& amount)
|
||||
{
|
||||
auto const& range = Number::kRange.get();
|
||||
|
||||
// keep full internal accuracy, but make more human friendly if possible
|
||||
static constexpr Number kZero = Number{};
|
||||
if (amount == kZero)
|
||||
return "0";
|
||||
|
||||
auto exponent = amount.exponent_;
|
||||
auto mantissa = amount.mantissa_;
|
||||
bool const negative = amount.negative_;
|
||||
// The mantissa must have a set number of decimal places for this to work
|
||||
auto [negative, mantissa, exponent] = amount.toInternal(range);
|
||||
|
||||
// Use scientific notation for exponents that are too small or too large
|
||||
auto const rangeLog = Number::mantissaLog();
|
||||
if (((exponent != 0) && ((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10)))))
|
||||
auto const rangeLog = range.log;
|
||||
if (((exponent != 0 && amount.exponent() != 0) &&
|
||||
((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10)))))
|
||||
{
|
||||
// Remove trailing zeroes from the mantissa.
|
||||
while (mantissa != 0 && mantissa % 10 == 0 && exponent < Number::kMaxExponent)
|
||||
{
|
||||
mantissa /= 10;
|
||||
@@ -1099,8 +1208,11 @@ to_string(Number const& amount)
|
||||
}
|
||||
std::string ret = negative ? "-" : "";
|
||||
ret.append(std::to_string(mantissa));
|
||||
ret.append(1, 'e');
|
||||
ret.append(std::to_string(exponent));
|
||||
if (exponent != 0)
|
||||
{
|
||||
ret.append(1, 'e');
|
||||
ret.append(std::to_string(exponent));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1188,20 +1300,11 @@ power(Number const& f, unsigned n)
|
||||
return r;
|
||||
}
|
||||
|
||||
// Returns f^(1/d)
|
||||
// Uses Newton–Raphson iterations until the result stops changing
|
||||
// to find the non-negative root of the polynomial g(x) = x^d - f
|
||||
|
||||
// This function, and power(Number f, unsigned n, unsigned d)
|
||||
// treat corner cases such as 0 roots as advised by Annex F of
|
||||
// the C standard, which itself is consistent with the IEEE
|
||||
// floating point standards.
|
||||
|
||||
Number
|
||||
root(Number f, unsigned d)
|
||||
Number::root(MantissaRange const& range, Number f, unsigned d)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const one = Number::one();
|
||||
constexpr Number kZero = Number{};
|
||||
auto const one = Number::one(range);
|
||||
|
||||
if (f == one || d == 1)
|
||||
return f;
|
||||
@@ -1218,21 +1321,28 @@ root(Number f, unsigned d)
|
||||
if (f == kZero)
|
||||
return f;
|
||||
|
||||
// Scale f into the range (0, 1) such that f's exponent is a multiple of d
|
||||
auto e = f.exponent_ + Number::mantissaLog() + 1;
|
||||
auto const di = static_cast<int>(d);
|
||||
auto ex = [e = e, di = di]() // Euclidean remainder of e/d
|
||||
{
|
||||
int const k = (e >= 0 ? e : e - (di - 1)) / di;
|
||||
int const k2 = e - (k * di);
|
||||
if (k2 == 0)
|
||||
return 0;
|
||||
return di - k2;
|
||||
}();
|
||||
e += ex;
|
||||
f = f.shiftExponent(-e); // f /= 10^e;
|
||||
auto const [e, di] = [&]() {
|
||||
auto const exponent = std::get<2>(f.toInternal(range));
|
||||
|
||||
XRPL_ASSERT_PARTS(f.isnormal(), "xrpl::root(Number, unsigned)", "f is normalized");
|
||||
// Scale f into the range (0, 1) such that the scale change (e) is a
|
||||
// multiple of the root (d)
|
||||
auto e = exponent + range.log + 1;
|
||||
auto const di = static_cast<int>(d);
|
||||
auto ex = [e = e, di = di]() // Euclidean remainder of e/d
|
||||
{
|
||||
int const k = (e >= 0 ? e : e - (di - 1)) / di;
|
||||
int const k2 = e - (k * di);
|
||||
if (k2 == 0)
|
||||
return 0;
|
||||
return di - k2;
|
||||
}();
|
||||
e += ex;
|
||||
f = f.shiftExponent(-e); // f /= 10^e;
|
||||
return std::make_tuple(e, di);
|
||||
}();
|
||||
|
||||
XRPL_ASSERT_PARTS(e % di == 0, "xrpl::root(Number, unsigned)", "e is divisible by d");
|
||||
XRPL_ASSERT_PARTS(f.isnormal(range), "xrpl::root(Number, unsigned)", "f is normalized");
|
||||
bool neg = false;
|
||||
if (f < kZero)
|
||||
{
|
||||
@@ -1265,15 +1375,33 @@ root(Number f, unsigned d)
|
||||
|
||||
// return r * 10^(e/d) to reverse scaling
|
||||
auto const result = r.shiftExponent(e / di);
|
||||
XRPL_ASSERT_PARTS(result.isnormal(), "xrpl::root(Number, unsigned)", "result is normalized");
|
||||
XRPL_ASSERT_PARTS(
|
||||
result.isnormal(range), "xrpl::root(Number, unsigned)", "result is normalized");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns f^(1/d)
|
||||
// Uses Newton–Raphson iterations until the result stops changing
|
||||
// to find the non-negative root of the polynomial g(x) = x^d - f
|
||||
|
||||
// This function, and power(Number f, unsigned n, unsigned d)
|
||||
// treat corner cases such as 0 roots as advised by Annex F of
|
||||
// the C standard, which itself is consistent with the IEEE
|
||||
// floating point standards.
|
||||
|
||||
Number
|
||||
root(Number f, unsigned d)
|
||||
{
|
||||
auto const& range = Number::kRange.get();
|
||||
return Number::root(range, f, d);
|
||||
}
|
||||
|
||||
Number
|
||||
root2(Number f)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const one = Number::one();
|
||||
auto const& range = Number::kRange.get();
|
||||
constexpr Number kZero = Number{};
|
||||
auto const one = Number::one(range);
|
||||
|
||||
if (f == one)
|
||||
return f;
|
||||
@@ -1282,12 +1410,18 @@ root2(Number f)
|
||||
if (f == kZero)
|
||||
return f;
|
||||
|
||||
// Scale f into the range (0, 1) such that f's exponent is a multiple of d
|
||||
auto e = f.exponent_ + Number::mantissaLog() + 1;
|
||||
if (e % 2 != 0)
|
||||
++e;
|
||||
f = f.shiftExponent(-e); // f /= 10^e;
|
||||
XRPL_ASSERT_PARTS(f.isnormal(), "xrpl::root2(Number)", "f is normalized");
|
||||
auto const e = [&]() {
|
||||
auto const exponent = std::get<2>(f.toInternal(range));
|
||||
|
||||
// Scale f into the range (0, 1) such that f's exponent is a
|
||||
// multiple of d
|
||||
auto e = exponent + range.log + 1;
|
||||
if (e % 2 != 0)
|
||||
++e;
|
||||
f = f.shiftExponent(-e); // f /= 10^e;
|
||||
return e;
|
||||
}();
|
||||
XRPL_ASSERT_PARTS(f.isnormal(range), "xrpl::root2(Number)", "f is normalized");
|
||||
|
||||
// Quadratic least squares curve fit of f^(1/d) in the range [0, 1]
|
||||
auto const D = 105; // NOLINT(readability-identifier-naming)
|
||||
@@ -1309,7 +1443,7 @@ root2(Number f)
|
||||
|
||||
// return r * 10^(e/2) to reverse scaling
|
||||
auto const result = r.shiftExponent(e / 2);
|
||||
XRPL_ASSERT_PARTS(result.isnormal(), "xrpl::root2(Number)", "result is normalized");
|
||||
XRPL_ASSERT_PARTS(result.isnormal(range), "xrpl::root2(Number)", "result is normalized");
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1319,8 +1453,10 @@ root2(Number f)
|
||||
Number
|
||||
power(Number const& f, unsigned n, unsigned d)
|
||||
{
|
||||
static constexpr Number kZero = Number{};
|
||||
auto const one = Number::one();
|
||||
auto const& range = Number::kRange.get();
|
||||
|
||||
constexpr Number kZero = Number{};
|
||||
auto const one = Number::one(range);
|
||||
|
||||
if (f == one)
|
||||
return f;
|
||||
@@ -1342,7 +1478,7 @@ power(Number const& f, unsigned n, unsigned d)
|
||||
d /= g;
|
||||
if ((n % 2) == 1 && (d % 2) == 0 && f < kZero)
|
||||
throw std::overflow_error("Number::power nan");
|
||||
return root(power(f, n), d);
|
||||
return Number::root(range, power(f, n), d);
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
1228
src/test/app/AmendmentTable_test.cpp
Normal file
1228
src/test/app/AmendmentTable_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
@@ -98,9 +99,10 @@ public:
|
||||
testLimits()
|
||||
{
|
||||
auto const scale = Number::getMantissaScale();
|
||||
testcase << "test_limits " << to_string(scale);
|
||||
bool caught = false;
|
||||
auto const minMantissa = Number::minMantissa();
|
||||
|
||||
testcase << "test_limits " << to_string(scale) << ", " << minMantissa;
|
||||
bool caught = false;
|
||||
try
|
||||
{
|
||||
[[maybe_unused]] Number const x =
|
||||
@@ -125,8 +127,9 @@ public:
|
||||
__LINE__);
|
||||
test(Number{false, minMantissa, -32769, Number::Normalized{}}, Number{}, __LINE__);
|
||||
test(
|
||||
// Use 1501 to force rounding up
|
||||
Number{false, minMantissa, 32000, Number::Normalized{}} * 1'000 +
|
||||
Number{false, 1'500, 32000, Number::Normalized{}},
|
||||
Number{false, 1'501, 32000, Number::Normalized{}},
|
||||
Number{false, minMantissa + 2, 32003, Number::Normalized{}},
|
||||
__LINE__);
|
||||
// 9,223,372,036,854,775,808
|
||||
@@ -236,7 +239,9 @@ public:
|
||||
{Number{true, 9'999'999'999'999'999'999ULL, -37, Number::Normalized{}},
|
||||
Number{1'000'000'000'000'000'000, -18},
|
||||
Number{false, 9'999'999'999'999'999'990ULL, -19, Number::Normalized{}}},
|
||||
{Number{Number::kMaxRep - 1}, Number{1, 0}, Number{Number::kMaxRep}},
|
||||
{Number{Number::kLargestMantissa - 1},
|
||||
Number{1, 0},
|
||||
Number{Number::kLargestMantissa}},
|
||||
// Test extremes
|
||||
{
|
||||
// Each Number operand rounds up, so the actual mantissa is
|
||||
@@ -246,21 +251,32 @@ public:
|
||||
Number{2, 19},
|
||||
},
|
||||
{
|
||||
// Does not round. Mantissas are going to be > kMaxRep, so if
|
||||
// added together as uint64_t's, the result will overflow.
|
||||
// With addition using uint128_t, there's no problem. After
|
||||
// normalizing, the resulting mantissa ends up less than
|
||||
// kMaxRep.
|
||||
// Does not round. Mantissas are going to be >
|
||||
// largestMantissa, so if added together as uint64_t's, the
|
||||
// result will overflow. With addition using uint128_t,
|
||||
// there's no problem. After normalizing, the resulting
|
||||
// mantissa ends up less than largestMantissa.
|
||||
Number{false, Number::kLargestMantissa, 0, Number::Normalized{}},
|
||||
Number{false, Number::kLargestMantissa, 0, Number::Normalized{}},
|
||||
Number{false, Number::kLargestMantissa * 2, 0, Number::Normalized{}},
|
||||
},
|
||||
{
|
||||
// These mantissas round down, so adding them together won't
|
||||
// have any consequences.
|
||||
Number{false, 9'999'999'999'999'999'990ULL, 0, Number::Normalized{}},
|
||||
Number{false, 9'999'999'999'999'999'990ULL, 0, Number::Normalized{}},
|
||||
Number{false, 1'999'999'999'999'999'998ULL, 1, Number::Normalized{}},
|
||||
},
|
||||
});
|
||||
auto const cLargeLegacy = std::to_array<Case>({
|
||||
{Number{Number::kMaxRep}, Number{6, -1}, Number{Number::kMaxRep / 10, 1}},
|
||||
{Number{Number::kLargestMantissa},
|
||||
Number{6, -1},
|
||||
Number{Number::kLargestMantissa / 10, 1}},
|
||||
});
|
||||
auto const cLargeCorrected = std::to_array<Case>({
|
||||
{Number{Number::kMaxRep}, Number{6, -1}, Number{(Number::kMaxRep / 10) + 1, 1}},
|
||||
{Number{Number::kLargestMantissa},
|
||||
Number{6, -1},
|
||||
Number{(Number::kLargestMantissa / 10) + 1, 1}},
|
||||
});
|
||||
auto test = [this](auto const& c) {
|
||||
for (auto const& [x, y, z] : c)
|
||||
@@ -357,14 +373,16 @@ public:
|
||||
{Number{1'000'000'000'000'000'001, -18},
|
||||
Number{1'000'000'000'000'000'000, -18},
|
||||
Number{1'000'000'000'000'000'000, -36}},
|
||||
{Number{Number::kMaxRep}, Number{6, -1}, Number{Number::kMaxRep - 1}},
|
||||
{Number{false, Number::kMaxRep + 1, 0, Number::Normalized{}},
|
||||
{Number{Number::kLargestMantissa},
|
||||
Number{6, -1},
|
||||
Number{Number::kLargestMantissa - 1}},
|
||||
{Number{false, Number::kLargestMantissa + 1, 0, Number::Normalized{}},
|
||||
Number{1, 0},
|
||||
Number{(Number::kMaxRep / 10) + 1, 1}},
|
||||
{Number{false, Number::kMaxRep + 1, 0, Number::Normalized{}},
|
||||
Number{(Number::kLargestMantissa / 10) + 1, 1}},
|
||||
{Number{false, Number::kLargestMantissa + 1, 0, Number::Normalized{}},
|
||||
Number{3, 0},
|
||||
Number{Number::kMaxRep}},
|
||||
{power(2, 63), Number{3, 0}, Number{Number::kMaxRep}},
|
||||
Number{Number::kLargestMantissa}},
|
||||
{power(2, 63), Number{3, 0}, Number{Number::kLargestMantissa}},
|
||||
});
|
||||
auto test = [this](auto const& c) {
|
||||
for (auto const& [x, y, z] : c)
|
||||
@@ -385,20 +403,30 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
static std::uint64_t
|
||||
getMaxInternalMantissa()
|
||||
{
|
||||
return (static_cast<std::uint64_t>(
|
||||
static_cast<std::int64_t>(power(10, Number::mantissaLog()))) *
|
||||
10) -
|
||||
1;
|
||||
}
|
||||
|
||||
void
|
||||
testMul()
|
||||
{
|
||||
auto const scale = Number::getMantissaScale();
|
||||
testcase << "test_mul " << to_string(scale);
|
||||
|
||||
using Case = std::tuple<Number, Number, Number>;
|
||||
// Case: Factor 1, Factor 2, Expected product, Line number
|
||||
using Case = std::tuple<Number, Number, Number, int>;
|
||||
auto test = [this](auto const& c) {
|
||||
for (auto const& [x, y, z] : c)
|
||||
for (auto const& [x, y, z, line] : c)
|
||||
{
|
||||
auto const result = x * y;
|
||||
std::stringstream ss;
|
||||
ss << x << " * " << y << " = " << result << ". Expected: " << z;
|
||||
BEAST_EXPECTS(result == z, ss.str());
|
||||
BEAST_EXPECTS(result == z, ss.str() + " line: " + std::to_string(line));
|
||||
}
|
||||
};
|
||||
auto tests = [&](auto const& cSmall, auto const& cLarge) {
|
||||
@@ -412,70 +440,97 @@ public:
|
||||
}
|
||||
};
|
||||
auto const maxMantissa = Number::maxMantissa();
|
||||
auto const maxInternalMantissa = getMaxInternalMantissa();
|
||||
|
||||
SaveNumberRoundMode const save{Number::setround(Number::RoundingMode::ToNearest)};
|
||||
{
|
||||
auto const cSmall = std::to_array<Case>({
|
||||
{Number{7}, Number{8}, Number{56}},
|
||||
{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{2000000000000000, -15}},
|
||||
Number{2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-2000000000000000, -15}},
|
||||
Number{-2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{2000000000000000, -15}},
|
||||
Number{2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{1000000000000000, -14}},
|
||||
{Number{1000000000000000, -32768}, Number{1000000000000000, -32768}, Number{0}},
|
||||
Number{1000000000000000, -14},
|
||||
__LINE__},
|
||||
{Number{1000000000000000, -32768},
|
||||
Number{1000000000000000, -32768},
|
||||
Number{0},
|
||||
__LINE__},
|
||||
// Maximum mantissa range
|
||||
{Number{9'999'999'999'999'999, 0},
|
||||
Number{9'999'999'999'999'999, 0},
|
||||
Number{9'999'999'999'999'998, 16}},
|
||||
Number{9'999'999'999'999'998, 16},
|
||||
__LINE__},
|
||||
});
|
||||
auto const cLarge = std::to_array<Case>({
|
||||
// Note that items with extremely large mantissas need to be
|
||||
// calculated, because otherwise they overflow uint64. Items
|
||||
// from C with larger mantissa
|
||||
{Number{7}, Number{8}, Number{56}},
|
||||
{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999862, -18}},
|
||||
Number{1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999862, -18}},
|
||||
Number{-1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999862, -18}},
|
||||
Number{1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{false, 9'999'999'999'999'999'579ULL, -18, Number::Normalized{}}},
|
||||
Number{false, 9'999'999'999'999'999'579ULL, -18, Number::Normalized{}},
|
||||
__LINE__},
|
||||
{Number{1000000000000000000, -32768},
|
||||
Number{1000000000000000000, -32768},
|
||||
Number{0}},
|
||||
Number{0},
|
||||
__LINE__},
|
||||
// Items from cSmall expanded for the larger mantissa,
|
||||
// except duplicates. Sadly, it looks like sqrt(2)^2 != 2
|
||||
// with higher precision
|
||||
{Number{1414213562373095049, -18},
|
||||
Number{1414213562373095049, -18},
|
||||
Number{2000000000000000001, -18}},
|
||||
Number{2000000000000000001, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{1414213562373095048, -18},
|
||||
Number{-1999999999999999998, -18}},
|
||||
Number{-1999999999999999998, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{-1414213562373095049, -18},
|
||||
Number{1999999999999999999, -18}},
|
||||
{Number{3214285714285714278, -18}, Number{3111111111111111119, -18}, Number{10, 0}},
|
||||
// Maximum mantissa range - rounds up to 1e19
|
||||
Number{1999999999999999999, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285714278, -18},
|
||||
Number{3111111111111111119, -18},
|
||||
Number{10, 0},
|
||||
__LINE__},
|
||||
// Maximum internal mantissa range - rounds up to 1e19
|
||||
{Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{1, 38},
|
||||
__LINE__},
|
||||
// Maximum actual mantissa range - same as int64 range
|
||||
{Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{1, 38}},
|
||||
Number{85'070'591'730'234'615'85, 19},
|
||||
__LINE__},
|
||||
// Maximum int64 range
|
||||
{Number{Number::kMaxRep, 0},
|
||||
Number{Number::kMaxRep, 0},
|
||||
Number{85'070'591'730'234'615'85, 19}},
|
||||
{Number{Number::kLargestMantissa, 0},
|
||||
Number{Number::kLargestMantissa, 0},
|
||||
Number{85'070'591'730'234'615'85, 19},
|
||||
__LINE__},
|
||||
});
|
||||
tests(cSmall, cLarge);
|
||||
}
|
||||
@@ -483,66 +538,90 @@ public:
|
||||
testcase << "test_mul " << to_string(Number::getMantissaScale()) << " towards_zero";
|
||||
{
|
||||
auto const cSmall = std::to_array<Case>(
|
||||
{{Number{7}, Number{8}, Number{56}},
|
||||
{{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999, -15}},
|
||||
Number{1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999, -15}},
|
||||
Number{-1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999, -15}},
|
||||
Number{1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{9999999999999999, -15}},
|
||||
{Number{1000000000000000, -32768}, Number{1000000000000000, -32768}, Number{0}}});
|
||||
Number{9999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{1000000000000000, -32768},
|
||||
Number{1000000000000000, -32768},
|
||||
Number{0},
|
||||
__LINE__}});
|
||||
auto const cLarge = std::to_array<Case>(
|
||||
// Note that items with extremely large mantissas need to be
|
||||
// calculated, because otherwise they overflow uint64. Items
|
||||
// from C with larger mantissa
|
||||
{
|
||||
{Number{7}, Number{8}, Number{56}},
|
||||
{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999861, -18}},
|
||||
Number{1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999861, -18}},
|
||||
Number{-1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999861, -18}},
|
||||
Number{1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{false, 9999999999999999579ULL, -18, Number::Normalized{}}},
|
||||
Number{false, 9999999999999999579ULL, -18, Number::Normalized{}},
|
||||
__LINE__},
|
||||
{Number{1000000000000000000, -32768},
|
||||
Number{1000000000000000000, -32768},
|
||||
Number{0}},
|
||||
Number{0},
|
||||
__LINE__},
|
||||
// Items from cSmall expanded for the larger mantissa,
|
||||
// except duplicates. Sadly, it looks like sqrt(2)^2 != 2
|
||||
// with higher precision
|
||||
{Number{1414213562373095049, -18},
|
||||
Number{1414213562373095049, -18},
|
||||
Number{2, 0}},
|
||||
Number{2, 0},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{1414213562373095048, -18},
|
||||
Number{-1999999999999999997, -18}},
|
||||
Number{-1999999999999999997, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{-1414213562373095049, -18},
|
||||
Number{1999999999999999999, -18}},
|
||||
Number{1999999999999999999, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285714278, -18},
|
||||
Number{3111111111111111119, -18},
|
||||
Number{10, 0}},
|
||||
// Maximum mantissa range - rounds down to maxMantissa/10e1
|
||||
Number{10, 0},
|
||||
__LINE__},
|
||||
// Maximum internal mantissa range - rounds down to
|
||||
// maxMantissa/10e1
|
||||
// 99'999'999'999'999'999'800'000'000'000'000'000'100
|
||||
{Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, (maxInternalMantissa / 10) - 1, 20, Number::Normalized{}},
|
||||
__LINE__},
|
||||
// Maximum actual mantissa range - same as int64
|
||||
{Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, (maxMantissa / 10) - 1, 20, Number::Normalized{}}},
|
||||
Number{85'070'591'730'234'615'84, 19},
|
||||
__LINE__},
|
||||
// Maximum int64 range
|
||||
// 85'070'591'730'234'615'847'396'907'784'232'501'249
|
||||
{Number{Number::kMaxRep, 0},
|
||||
Number{Number::kMaxRep, 0},
|
||||
Number{85'070'591'730'234'615'84, 19}},
|
||||
{Number{Number::kLargestMantissa, 0},
|
||||
Number{Number::kLargestMantissa, 0},
|
||||
Number{85'070'591'730'234'615'84, 19},
|
||||
__LINE__},
|
||||
});
|
||||
tests(cSmall, cLarge);
|
||||
}
|
||||
@@ -550,66 +629,90 @@ public:
|
||||
testcase << "test_mul " << to_string(Number::getMantissaScale()) << " downward";
|
||||
{
|
||||
auto const cSmall = std::to_array<Case>(
|
||||
{{Number{7}, Number{8}, Number{56}},
|
||||
{{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999, -15}},
|
||||
Number{1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-2000000000000000, -15}},
|
||||
Number{-2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999, -15}},
|
||||
Number{1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{9999999999999999, -15}},
|
||||
{Number{1000000000000000, -32768}, Number{1000000000000000, -32768}, Number{0}}});
|
||||
Number{9999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{1000000000000000, -32768},
|
||||
Number{1000000000000000, -32768},
|
||||
Number{0},
|
||||
__LINE__}});
|
||||
auto const cLarge = std::to_array<Case>(
|
||||
// Note that items with extremely large mantissas need to be
|
||||
// calculated, because otherwise they overflow uint64. Items
|
||||
// from C with larger mantissa
|
||||
{
|
||||
{Number{7}, Number{8}, Number{56}},
|
||||
{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999861, -18}},
|
||||
Number{1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999862, -18}},
|
||||
Number{-1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999861, -18}},
|
||||
Number{1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{false, 9'999'999'999'999'999'579ULL, -18, Number::Normalized{}}},
|
||||
Number{false, 9'999'999'999'999'999'579ULL, -18, Number::Normalized{}},
|
||||
__LINE__},
|
||||
{Number{1000000000000000000, -32768},
|
||||
Number{1000000000000000000, -32768},
|
||||
Number{0}},
|
||||
Number{0},
|
||||
__LINE__},
|
||||
// Items from cSmall expanded for the larger mantissa,
|
||||
// except duplicates. Sadly, it looks like sqrt(2)^2 != 2
|
||||
// with higher precision
|
||||
{Number{1414213562373095049, -18},
|
||||
Number{1414213562373095049, -18},
|
||||
Number{2, 0}},
|
||||
Number{2, 0},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{1414213562373095048, -18},
|
||||
Number{-1999999999999999998, -18}},
|
||||
Number{-1999999999999999998, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{-1414213562373095049, -18},
|
||||
Number{1999999999999999999, -18}},
|
||||
Number{1999999999999999999, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285714278, -18},
|
||||
Number{3111111111111111119, -18},
|
||||
Number{10, 0}},
|
||||
// Maximum mantissa range - rounds down to maxMantissa/10e1
|
||||
Number{10, 0},
|
||||
__LINE__},
|
||||
// Maximum internal mantissa range - rounds down to
|
||||
// maxInternalMantissa/10-1
|
||||
// 99'999'999'999'999'999'800'000'000'000'000'000'100
|
||||
{Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, (maxInternalMantissa / 10) - 1, 20, Number::Normalized{}},
|
||||
__LINE__},
|
||||
// Maximum external mantissa range - same as INT64_MAX (2^63-1)
|
||||
{Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, (maxMantissa / 10) - 1, 20, Number::Normalized{}}},
|
||||
Number{85'070'591'730'234'615'84, 19},
|
||||
__LINE__},
|
||||
// Maximum int64 range
|
||||
// 85'070'591'730'234'615'847'396'907'784'232'501'249
|
||||
{Number{Number::kMaxRep, 0},
|
||||
Number{Number::kMaxRep, 0},
|
||||
Number{85'070'591'730'234'615'84, 19}},
|
||||
{Number{Number::kLargestMantissa, 0},
|
||||
Number{Number::kLargestMantissa, 0},
|
||||
Number{85'070'591'730'234'615'84, 19},
|
||||
__LINE__},
|
||||
});
|
||||
tests(cSmall, cLarge);
|
||||
}
|
||||
@@ -617,66 +720,89 @@ public:
|
||||
testcase << "test_mul " << to_string(Number::getMantissaScale()) << " upward";
|
||||
{
|
||||
auto const cSmall = std::to_array<Case>(
|
||||
{{Number{7}, Number{8}, Number{56}},
|
||||
{{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{2000000000000000, -15}},
|
||||
Number{2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999, -15}},
|
||||
Number{-1999999999999999, -15},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{2000000000000000, -15}},
|
||||
Number{2000000000000000, -15},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{1000000000000000, -14}},
|
||||
{Number{1000000000000000, -32768}, Number{1000000000000000, -32768}, Number{0}}});
|
||||
Number{1000000000000000, -14},
|
||||
__LINE__},
|
||||
{Number{1000000000000000, -32768},
|
||||
Number{1000000000000000, -32768},
|
||||
Number{0},
|
||||
__LINE__}});
|
||||
auto const cLarge = std::to_array<Case>(
|
||||
// Note that items with extremely large mantissas need to be
|
||||
// calculated, because otherwise they overflow uint64. Items
|
||||
// from C with larger mantissa
|
||||
{
|
||||
{Number{7}, Number{8}, Number{56}},
|
||||
{Number{7}, Number{8}, Number{56}, __LINE__},
|
||||
{Number{1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{1999999999999999862, -18}},
|
||||
Number{1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{1414213562373095, -15},
|
||||
Number{-1999999999999999861, -18}},
|
||||
Number{-1999999999999999861, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095, -15},
|
||||
Number{-1414213562373095, -15},
|
||||
Number{1999999999999999862, -18}},
|
||||
Number{1999999999999999862, -18},
|
||||
__LINE__},
|
||||
{Number{3214285714285706, -15},
|
||||
Number{3111111111111119, -15},
|
||||
Number{999999999999999958, -17}},
|
||||
Number{999999999999999958, -17},
|
||||
__LINE__},
|
||||
{Number{1000000000000000000, -32768},
|
||||
Number{1000000000000000000, -32768},
|
||||
Number{0}},
|
||||
Number{0},
|
||||
__LINE__},
|
||||
// Items from cSmall expanded for the larger mantissa,
|
||||
// except duplicates. Sadly, it looks like sqrt(2)^2 != 2
|
||||
// with higher precision
|
||||
{Number{1414213562373095049, -18},
|
||||
Number{1414213562373095049, -18},
|
||||
Number{2000000000000000001, -18}},
|
||||
Number{2000000000000000001, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{1414213562373095048, -18},
|
||||
Number{-1999999999999999997, -18}},
|
||||
Number{-1999999999999999997, -18},
|
||||
__LINE__},
|
||||
{Number{-1414213562373095048, -18},
|
||||
Number{-1414213562373095049, -18},
|
||||
Number{2, 0}},
|
||||
Number{2, 0},
|
||||
__LINE__},
|
||||
{Number{3214285714285714278, -18},
|
||||
Number{3111111111111111119, -18},
|
||||
Number{1000000000000000001, -17}},
|
||||
// Maximum mantissa range - rounds up to minMantissa*10
|
||||
// 1e19*1e19=1e38
|
||||
Number{1000000000000000001, -17},
|
||||
__LINE__},
|
||||
// Maximum internal mantissa range - rounds up to
|
||||
// minMantissa*10 1e19*1e19=1e38
|
||||
{Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxInternalMantissa, 0, Number::Normalized{}},
|
||||
Number{1, 38},
|
||||
__LINE__},
|
||||
// Maximum mantissa range - same as int64
|
||||
{Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}},
|
||||
Number{1, 38}},
|
||||
Number{85'070'591'730'234'615'85, 19},
|
||||
__LINE__},
|
||||
// Maximum int64 range
|
||||
// 85'070'591'730'234'615'847'396'907'784'232'501'249
|
||||
{Number{Number::kMaxRep, 0},
|
||||
Number{Number::kMaxRep, 0},
|
||||
Number{85'070'591'730'234'615'85, 19}},
|
||||
{Number{Number::kLargestMantissa, 0},
|
||||
Number{Number::kLargestMantissa, 0},
|
||||
Number{85'070'591'730'234'615'85, 19},
|
||||
__LINE__},
|
||||
});
|
||||
tests(cSmall, cLarge);
|
||||
}
|
||||
@@ -911,6 +1037,8 @@ public:
|
||||
};
|
||||
*/
|
||||
|
||||
auto const maxInternalMantissa = getMaxInternalMantissa();
|
||||
|
||||
auto const cSmall = std::to_array<Case>(
|
||||
{{Number{2}, 2, Number{1414213562373095049, -18}},
|
||||
{Number{2'000'000}, 2, Number{1414213562373095049, -15}},
|
||||
@@ -922,16 +1050,16 @@ public:
|
||||
{Number{0}, 5, Number{0}},
|
||||
{Number{5625, -4}, 2, Number{75, -2}}});
|
||||
auto const cLarge = std::to_array<Case>({
|
||||
{Number{false, Number::maxMantissa() - 9, -1, Number::Normalized{}},
|
||||
{Number{false, maxInternalMantissa - 9, -1, Number::Normalized{}},
|
||||
2,
|
||||
Number{false, 999'999'999'999'999'999, -9, Number::Normalized{}}},
|
||||
{Number{false, Number::maxMantissa() - 9, 0, Number::Normalized{}},
|
||||
{Number{false, maxInternalMantissa - 9, 0, Number::Normalized{}},
|
||||
2,
|
||||
Number{false, 3'162'277'660'168'379'330, -9, Number::Normalized{}}},
|
||||
{Number{Number::kMaxRep},
|
||||
{Number{Number::kLargestMantissa},
|
||||
2,
|
||||
Number{false, 3'037'000'499'976049692, -9, Number::Normalized{}}},
|
||||
{Number{Number::kMaxRep},
|
||||
{Number{Number::kLargestMantissa},
|
||||
4,
|
||||
Number{false, 55'108'98747006743627, -14, Number::Normalized{}}},
|
||||
});
|
||||
@@ -980,6 +1108,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
Number const maxInternalMantissa{getMaxInternalMantissa(), 0, Number::Normalized{}};
|
||||
|
||||
auto const cSmall = std::to_array<Number>({
|
||||
Number{2},
|
||||
Number{2'000'000},
|
||||
@@ -989,7 +1119,10 @@ public:
|
||||
Number{5, -1},
|
||||
Number{0},
|
||||
Number{5625, -4},
|
||||
Number{Number::kMaxRep},
|
||||
Number{Number::kLargestMantissa},
|
||||
maxInternalMantissa,
|
||||
Number{Number::minMantissa(), 0, Number::Unchecked{}},
|
||||
Number{Number::maxMantissa(), 0, Number::Unchecked{}},
|
||||
});
|
||||
test(cSmall);
|
||||
bool caught = false;
|
||||
@@ -1341,18 +1474,18 @@ public:
|
||||
case MantissaRange::MantissaScale::Large:
|
||||
// Test the edges
|
||||
// ((exponent < -(28)) || (exponent > -(8)))))
|
||||
test(Number::min(), "1e-32750");
|
||||
test(Number::min(), "922337203685477581e-32768");
|
||||
test(Number::max(), "9223372036854775807e32768");
|
||||
test(Number::lowest(), "-9223372036854775807e32768");
|
||||
{
|
||||
NumberRoundModeGuard const mg(Number::RoundingMode::TowardsZero);
|
||||
|
||||
auto const maxMantissa = Number::maxMantissa();
|
||||
BEAST_EXPECT(maxMantissa == 9'999'999'999'999'999'999ULL);
|
||||
BEAST_EXPECT(maxMantissa == 9'223'372'036'854'775'807ULL);
|
||||
test(
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}}, "9999999999999999990");
|
||||
Number{false, maxMantissa, 0, Number::Normalized{}}, "9223372036854775807");
|
||||
test(
|
||||
Number{true, maxMantissa, 0, Number::Normalized{}}, "-9999999999999999990");
|
||||
Number{true, maxMantissa, 0, Number::Normalized{}}, "-9223372036854775807");
|
||||
|
||||
test(
|
||||
Number{std::numeric_limits<std::int64_t>::max(), 0}, "9223372036854775807");
|
||||
@@ -1588,7 +1721,7 @@ public:
|
||||
Number const initalXrp{kInitialXrp};
|
||||
BEAST_EXPECT(initalXrp.exponent() > 0);
|
||||
|
||||
Number const maxInt64{Number::kMaxRep};
|
||||
Number const maxInt64{Number::kLargestMantissa};
|
||||
BEAST_EXPECT(maxInt64.exponent() > 0);
|
||||
// 85'070'591'730'234'615'865'843'651'857'942'052'864 - 38 digits
|
||||
BEAST_EXPECT((power(maxInt64, 2) == Number{85'070'591'730'234'62, 22}));
|
||||
@@ -1605,21 +1738,242 @@ public:
|
||||
Number const initalXrp{kInitialXrp};
|
||||
BEAST_EXPECT(initalXrp.exponent() <= 0);
|
||||
|
||||
Number const maxInt64{Number::kMaxRep};
|
||||
Number const maxInt64{Number::kLargestMantissa};
|
||||
BEAST_EXPECT(maxInt64.exponent() <= 0);
|
||||
// 85'070'591'730'234'615'847'396'907'784'232'501'249 - 38 digits
|
||||
BEAST_EXPECT((power(maxInt64, 2) == Number{85'070'591'730'234'615'85, 19}));
|
||||
|
||||
NumberRoundModeGuard const mg(Number::RoundingMode::TowardsZero);
|
||||
|
||||
auto const maxMantissa = Number::maxMantissa();
|
||||
Number const max = Number{false, maxMantissa, 0, Number::Normalized{}};
|
||||
BEAST_EXPECT(max.mantissa() == maxMantissa / 10);
|
||||
BEAST_EXPECT(max.exponent() == 1);
|
||||
// 99'999'999'999'999'999'800'000'000'000'000'000'100 - also 38
|
||||
// digits
|
||||
BEAST_EXPECT(
|
||||
(power(max, 2) == Number{false, (maxMantissa / 10) - 1, 20, Number::Normalized{}}));
|
||||
{
|
||||
auto const maxInternalMantissa = getMaxInternalMantissa();
|
||||
|
||||
// Rounds down to fit under 2^63
|
||||
Number const max = Number{false, maxInternalMantissa, 0, Number::Normalized{}};
|
||||
// No alterations by the accessors
|
||||
BEAST_EXPECT(max.mantissa() == maxInternalMantissa / 10);
|
||||
BEAST_EXPECT(max.exponent() == 1);
|
||||
// 99'999'999'999'999'999'800'000'000'000'000'000'100 - also 38
|
||||
// digits
|
||||
BEAST_EXPECT(
|
||||
(power(max, 2) ==
|
||||
Number{false, (maxInternalMantissa / 10) - 1, 20, Number::Normalized{}}));
|
||||
}
|
||||
|
||||
{
|
||||
auto const maxMantissa = Number::maxMantissa();
|
||||
Number const max = Number{false, maxMantissa, 0, Number::Normalized{}};
|
||||
// No alterations by the accessors
|
||||
BEAST_EXPECT(max.mantissa() == maxMantissa);
|
||||
BEAST_EXPECT(max.exponent() == 0);
|
||||
// 85'070'591'730'234'615'847'396'907'784'232'501'249 - also 38
|
||||
// digits
|
||||
BEAST_EXPECT(
|
||||
(power(max, 2) ==
|
||||
Number{false, 85'070'591'730'234'615'84, 19, Number::Normalized{}}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testNormalizeToRange()
|
||||
{
|
||||
// Test edge-cases of normalizeToRange
|
||||
auto const scale = Number::getMantissaScale();
|
||||
testcase << "normalizeToRange " << to_string(scale);
|
||||
|
||||
auto test = [this](
|
||||
Number const& n,
|
||||
auto const rangeMin,
|
||||
auto const rangeMax,
|
||||
auto const expectedMantissa,
|
||||
auto const expectedExponent,
|
||||
auto const line) {
|
||||
auto const normalized =
|
||||
n.normalizeToRangeImpl(rangeMin, rangeMax, MantissaRange::CuspRoundingFix::Enabled);
|
||||
BEAST_EXPECTS(
|
||||
normalized.first == expectedMantissa,
|
||||
"Number " + to_string(n) + " scaled to " + std::to_string(rangeMax) +
|
||||
". Expected mantissa:" + std::to_string(expectedMantissa) +
|
||||
", got: " + std::to_string(normalized.first) + " @ " + std::to_string(line));
|
||||
BEAST_EXPECTS(
|
||||
normalized.second == expectedExponent,
|
||||
"Number " + to_string(n) + " scaled to " + std::to_string(rangeMax) +
|
||||
". Expected exponent:" + std::to_string(expectedExponent) +
|
||||
", got: " + std::to_string(normalized.second) + " @ " + std::to_string(line));
|
||||
};
|
||||
|
||||
std::int64_t constexpr kIRangeMin = 100;
|
||||
std::int64_t constexpr kIRangeMax = 999;
|
||||
|
||||
std::uint64_t constexpr kURangeMin = 100;
|
||||
std::uint64_t constexpr kURangeMax = 999;
|
||||
|
||||
constexpr static MantissaRange kLargeRange{MantissaRange::MantissaScale::Large};
|
||||
|
||||
std::int64_t constexpr kIBigMin = kLargeRange.min;
|
||||
std::int64_t constexpr kIBigMax = kLargeRange.max;
|
||||
|
||||
auto const testSuite = [&](Number const& n,
|
||||
auto const expectedSmallMantissa,
|
||||
auto const expectedSmallExponent,
|
||||
auto const expectedLargeMantissa,
|
||||
auto const expectedLargeExponent,
|
||||
auto const line) {
|
||||
test(n, kIRangeMin, kIRangeMax, expectedSmallMantissa, expectedSmallExponent, line);
|
||||
test(n, kIBigMin, kIBigMax, expectedLargeMantissa, expectedLargeExponent, line);
|
||||
|
||||
// Only test non-negative. testing a negative number with an
|
||||
// unsigned range will assert, and asserts can't be tested.
|
||||
if (n.signum() >= 0)
|
||||
{
|
||||
test(n, kURangeMin, kURangeMax, expectedSmallMantissa, expectedSmallExponent, line);
|
||||
test(
|
||||
n,
|
||||
kLargeRange.min,
|
||||
kLargeRange.max,
|
||||
expectedLargeMantissa,
|
||||
expectedLargeExponent,
|
||||
line);
|
||||
}
|
||||
};
|
||||
|
||||
{
|
||||
// zero
|
||||
Number const n{0};
|
||||
|
||||
testSuite(
|
||||
n,
|
||||
0,
|
||||
std::numeric_limits<int>::lowest(),
|
||||
0,
|
||||
std::numeric_limits<int>::lowest(),
|
||||
__LINE__);
|
||||
}
|
||||
{
|
||||
// Small positive number
|
||||
Number const n{2};
|
||||
|
||||
testSuite(n, 200, -2, 2'000'000'000'000'000'000, -18, __LINE__);
|
||||
}
|
||||
{
|
||||
// Negative number
|
||||
Number const n{-2};
|
||||
|
||||
testSuite(n, -200, -2, -2'000'000'000'000'000'000, -18, __LINE__);
|
||||
}
|
||||
{
|
||||
// Biggest valid mantissa
|
||||
Number const n{Number::kLargestMantissa, 0, Number::Normalized{}};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
// With the small mantissa range, the value rounds up. Because
|
||||
// it rounds up, when scaling up to the full int64 range, it
|
||||
// can't go over the max, so it is one digit smaller than the
|
||||
// full value.
|
||||
testSuite(n, 922, 16, 922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, 922, 16, Number::kLargestMantissa, 0, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// Biggest valid mantissa + 1
|
||||
Number const n{Number::kLargestMantissa + 1, 0, Number::Normalized{}};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
// With the small mantissa range, the value rounds up. Because
|
||||
// it rounds up, when scaling up to the full int64 range, it
|
||||
// can't go over the max, so it is one digit smaller than the
|
||||
// full value.
|
||||
testSuite(n, 922, 16, 922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, 922, 16, (Number::kLargestMantissa / 10) + 1, 1, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// Biggest valid mantissa + 2
|
||||
Number const n{Number::kLargestMantissa + 2, 0, Number::Normalized{}};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
// With the small mantissa range, the value rounds up. Because
|
||||
// it rounds up, when scaling up to the full int64 range, it
|
||||
// can't go over the max, so it is one digit smaller than the
|
||||
// full value.
|
||||
testSuite(n, 922, 16, 922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, 922, 16, (Number::kLargestMantissa / 10) + 1, 1, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// Biggest valid mantissa + 3
|
||||
Number const n{Number::kLargestMantissa + 3, 0, Number::Normalized{}};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
// With the small mantissa range, the value rounds up. Because
|
||||
// it rounds up, when scaling up to the full int64 range, it
|
||||
// can't go over the max, so it is one digit smaller than the
|
||||
// full value.
|
||||
testSuite(n, 922, 16, 922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, 922, 16, (Number::kLargestMantissa / 10) + 1, 1, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// int64 min
|
||||
Number const n{std::numeric_limits<std::int64_t>::min(), 0};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
testSuite(n, -922, 16, -922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, -922, 16, -((Number::kLargestMantissa / 10) + 1), 1, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// int64 min + 1
|
||||
Number const n{std::numeric_limits<std::int64_t>::min() + 1, 0};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
testSuite(n, -922, 16, -922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, -922, 16, -Number::kLargestMantissa, 0, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
// int64 min - 1
|
||||
// Need to cast to uint, even though we're dealing with a negative
|
||||
// number to avoid overflow and UB
|
||||
Number const n{
|
||||
true,
|
||||
-static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::min()) + 1,
|
||||
0,
|
||||
Number::Normalized{}};
|
||||
|
||||
if (scale == MantissaRange::MantissaScale::Small)
|
||||
{
|
||||
testSuite(n, -922, 16, -922'337'203'685'477'600, 1, __LINE__);
|
||||
}
|
||||
else
|
||||
{
|
||||
testSuite(n, -922, 16, -((Number::kLargestMantissa / 10) + 1), 1, __LINE__);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1628,7 +1982,7 @@ public:
|
||||
{
|
||||
auto const scale = Number::getMantissaScale();
|
||||
{
|
||||
testcase << "upward rounding produces a value below exact at kMaxRep cusp "
|
||||
testcase << "upward rounding produces a value below exact at kLargestMantissa cusp"
|
||||
<< to_string(scale);
|
||||
|
||||
NumberRoundModeGuard const rg{Number::RoundingMode::Upward};
|
||||
@@ -1898,12 +2252,37 @@ public:
|
||||
testTruncate();
|
||||
testRounding();
|
||||
testInt64();
|
||||
testNormalizeToRange();
|
||||
|
||||
testUpwardRoundsDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class NumberPerf_test : public Number_test
|
||||
{
|
||||
void
|
||||
run() override
|
||||
{
|
||||
// This suite will give the most accurate results when run
|
||||
// single threaded, suppressing non-log output.
|
||||
// "--unittest=NumberPerf --quiet --unittest-log"
|
||||
using clock_type = std::chrono::steady_clock;
|
||||
|
||||
int const limit = 100000;
|
||||
auto const start = clock_type::now();
|
||||
for (int i = 0; i < limit; ++i)
|
||||
{
|
||||
Number_test::run();
|
||||
}
|
||||
auto const duration =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(clock_type::now() - start);
|
||||
|
||||
log << "Number test repeated " << limit << " times took " << duration << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(Number, basics, xrpl);
|
||||
BEAST_DEFINE_TESTSUITE_MANUAL(NumberPerf, tx, xrpl);
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -35,10 +35,6 @@ xrpl_add_test(json)
|
||||
target_link_libraries(xrpl.test.json PRIVATE xrpl.imports.test)
|
||||
add_dependencies(xrpl.tests xrpl.test.json)
|
||||
|
||||
xrpl_add_test(ledger)
|
||||
target_link_libraries(xrpl.test.ledger PRIVATE xrpl.imports.test)
|
||||
add_dependencies(xrpl.tests xrpl.test.ledger)
|
||||
|
||||
xrpl_add_test(tx)
|
||||
target_link_libraries(xrpl.test.tx PRIVATE xrpl.imports.test)
|
||||
add_dependencies(xrpl.tests xrpl.test.tx)
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <xrpl/basics/Log.h>
|
||||
#include <xrpl/basics/Mutex.hpp>
|
||||
#include <xrpl/basics/chrono.h>
|
||||
#include <xrpl/core/HashRouter.h>
|
||||
#include <xrpl/core/NetworkIDService.h>
|
||||
#include <xrpl/core/ServiceRegistry.h>
|
||||
#include <xrpl/core/StartUpType.h>
|
||||
#include <xrpl/ledger/PendingSaves.h>
|
||||
#include <xrpl/rdb/DatabaseCon.h>
|
||||
#include <xrpl/server/LoadFeeTrack.h>
|
||||
#include <xrpl/server/Wallet.h>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
|
||||
#include <helpers/TestFamily.h>
|
||||
#include <helpers/TestSink.h>
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -87,7 +81,6 @@ class TestServiceRegistry : public ServiceRegistry
|
||||
logs_.journal("TaggedCache")};
|
||||
PendingSaves pendingSaves_;
|
||||
std::optional<uint256> trapTxID_;
|
||||
Mutex<std::unique_ptr<DatabaseCon>> walletDB_;
|
||||
|
||||
public:
|
||||
TestServiceRegistry() = default;
|
||||
@@ -365,20 +358,10 @@ public:
|
||||
return trapTxID_;
|
||||
}
|
||||
|
||||
/** Returns a lazily-created in-memory wallet DB suitable for tests. */
|
||||
DatabaseCon&
|
||||
getWalletDB() override
|
||||
{
|
||||
auto lock = walletDB_.lock();
|
||||
auto& walletDB = *lock;
|
||||
if (!walletDB)
|
||||
{
|
||||
DatabaseCon::Setup setup;
|
||||
setup.standAlone = true;
|
||||
setup.startUp = StartUpType::Normal;
|
||||
walletDB = makeWalletDB(setup, logs_.journal("WalletDB"));
|
||||
}
|
||||
return *walletDB;
|
||||
throw std::logic_error("TestServiceRegistry::getWalletDB() not implemented");
|
||||
}
|
||||
|
||||
// Temporary: Get the underlying Application
|
||||
|
||||
@@ -1,951 +0,0 @@
|
||||
#include <xrpl/ledger/AmendmentTable.h>
|
||||
|
||||
#include <xrpl/basics/BasicConfig.h>
|
||||
#include <xrpl/basics/UnorderedContainers.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/basics/chrono.h>
|
||||
#include <xrpl/basics/contract.h>
|
||||
#include <xrpl/beast/hash/uhash.h>
|
||||
#include <xrpl/beast/utility/Journal.h>
|
||||
#include <xrpl/json/json_value.h>
|
||||
#include <xrpl/ledger/View.h>
|
||||
#include <xrpl/protocol/Feature.h>
|
||||
#include <xrpl/protocol/KeyType.h>
|
||||
#include <xrpl/protocol/PublicKey.h>
|
||||
#include <xrpl/protocol/Rules.h>
|
||||
#include <xrpl/protocol/SField.h>
|
||||
#include <xrpl/protocol/STValidation.h>
|
||||
#include <xrpl/protocol/SecretKey.h>
|
||||
#include <xrpl/protocol/TxFlags.h>
|
||||
#include <xrpl/protocol/digest.h>
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <helpers/TestServiceRegistry.h>
|
||||
#include <helpers/TxTest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace xrpl::test {
|
||||
|
||||
using ::testing::ThrowsMessage;
|
||||
|
||||
/**
|
||||
* @brief Test fixture for the AmendmentTable.
|
||||
*
|
||||
* Provides a TestServiceRegistry (with an in-memory wallet DB) and a collection
|
||||
* of helpers for building amendment sections, feature lists and validators.
|
||||
*/
|
||||
struct AmendmentTableTest : ::testing::Test
|
||||
{
|
||||
static uint256
|
||||
amendmentId(std::string in)
|
||||
{
|
||||
sha256_hasher h;
|
||||
using beast::hash_append;
|
||||
hash_append(h, in);
|
||||
auto const d = static_cast<sha256_hasher::result_type>(h);
|
||||
uint256 result;
|
||||
std::memcpy(result.data(), d.data(), d.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
static Section
|
||||
makeSection(std::string const& name, std::vector<std::string> const& amendments)
|
||||
{
|
||||
Section section(name);
|
||||
for (auto const& a : amendments)
|
||||
section.append(to_string(amendmentId(a)) + " " + a);
|
||||
return section;
|
||||
}
|
||||
|
||||
static Section
|
||||
makeSection(std::vector<std::string> const& amendments)
|
||||
{
|
||||
return makeSection("Test", amendments);
|
||||
}
|
||||
|
||||
static Section
|
||||
makeSection(uint256 const& amendment)
|
||||
{
|
||||
Section section("Test");
|
||||
section.append(to_string(amendment) + " " + to_string(amendment));
|
||||
return section;
|
||||
}
|
||||
|
||||
static std::vector<AmendmentTable::FeatureInfo>
|
||||
makeFeatureInfo(std::vector<std::string> const& amendments, VoteBehavior voteBehavior)
|
||||
{
|
||||
std::vector<AmendmentTable::FeatureInfo> result;
|
||||
result.reserve(amendments.size());
|
||||
for (auto const& a : amendments)
|
||||
{
|
||||
result.emplace_back(a, amendmentId(a), voteBehavior);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<AmendmentTable::FeatureInfo>
|
||||
makeDefaultYes(std::vector<std::string> const& amendments)
|
||||
{
|
||||
return makeFeatureInfo(amendments, VoteBehavior::DefaultYes);
|
||||
}
|
||||
|
||||
static std::vector<AmendmentTable::FeatureInfo>
|
||||
makeDefaultYes(uint256 const amendment)
|
||||
{
|
||||
std::vector<AmendmentTable::FeatureInfo> result{
|
||||
{to_string(amendment), amendment, VoteBehavior::DefaultYes}};
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<AmendmentTable::FeatureInfo>
|
||||
makeDefaultNo(std::vector<std::string> const& amendments)
|
||||
{
|
||||
return makeFeatureInfo(amendments, VoteBehavior::DefaultNo);
|
||||
}
|
||||
|
||||
static std::vector<AmendmentTable::FeatureInfo>
|
||||
makeObsolete(std::vector<std::string> const& amendments)
|
||||
{
|
||||
return makeFeatureInfo(amendments, VoteBehavior::Obsolete);
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::size_t
|
||||
totalSize(std::vector<Arg> const& src, Args const&... args)
|
||||
{
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
return src.size() + totalSize(args...);
|
||||
return src.size();
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static void
|
||||
combineArg(std::vector<Arg>& dest, std::vector<Arg> const& src, Args const&... args)
|
||||
{
|
||||
std::copy(src.begin(), src.end(), std::back_inserter(dest));
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
combineArg(dest, args...);
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::vector<Arg>
|
||||
combine(std::vector<Arg> left, std::vector<Arg> const& right, Args const&... args)
|
||||
{
|
||||
left.reserve(totalSize(left, right, args...));
|
||||
combineArg(left, right, args...);
|
||||
return left;
|
||||
}
|
||||
|
||||
// All useful amendments are supported amendments.
|
||||
// Enabled amendments are typically a subset of supported amendments.
|
||||
// Vetoed amendments should be supported but not enabled.
|
||||
// Unsupported amendments may be added to the AmendmentTable.
|
||||
std::vector<std::string> const yes{"g", "i", "k", "m", "o", "q", "r", "s", "t", "u"};
|
||||
std::vector<std::string> const enabled{"b", "d", "f", "h", "j", "l", "n", "p"};
|
||||
std::vector<std::string> const vetoed{"a", "c", "e"};
|
||||
std::vector<std::string> const obsolete{"0", "1", "2"};
|
||||
std::vector<std::string> const allSupported{combine(yes, enabled, vetoed, obsolete)};
|
||||
std::vector<std::string> const unsupported{"v", "w", "x"};
|
||||
std::vector<std::string> const unsupportedMajority{"y", "z"};
|
||||
|
||||
Section const emptySection;
|
||||
std::vector<AmendmentTable::FeatureInfo> const emptyYes;
|
||||
|
||||
TestServiceRegistry registry;
|
||||
beast::Journal journal{registry.getJournal("AmendmentTableTest")};
|
||||
|
||||
std::unique_ptr<AmendmentTable>
|
||||
makeTable(
|
||||
std::chrono::seconds majorityTime,
|
||||
std::vector<AmendmentTable::FeatureInfo> const& supported,
|
||||
Section const& enabled,
|
||||
Section const& vetoed)
|
||||
{
|
||||
return makeAmendmentTable(registry, majorityTime, supported, enabled, vetoed, journal);
|
||||
}
|
||||
|
||||
std::unique_ptr<AmendmentTable>
|
||||
makeTable(std::chrono::seconds majorityTime)
|
||||
{
|
||||
static std::vector<AmendmentTable::FeatureInfo> const kSupported = combine(
|
||||
makeDefaultYes(yes),
|
||||
// Use non-intuitive default votes for "enabled_" and "vetoed_"
|
||||
// so that when the tests later explicitly enable or veto them,
|
||||
// we can be certain that they are not simply going by their
|
||||
// default vote setting.
|
||||
makeDefaultNo(enabled),
|
||||
makeDefaultYes(vetoed),
|
||||
makeObsolete(obsolete));
|
||||
return makeTable(majorityTime, kSupported, makeSection(enabled), makeSection(vetoed));
|
||||
}
|
||||
|
||||
// Build a Rules object that has all testable amendments enabled.
|
||||
static Rules const&
|
||||
allRules()
|
||||
{
|
||||
static Rules const kRules = [] {
|
||||
std::unordered_set<uint256, beast::Uhash<>> featureSet;
|
||||
foreachFeature(allFeatures(), [&](uint256 const& f) { featureSet.insert(f); });
|
||||
return Rules{featureSet};
|
||||
}();
|
||||
return kRules;
|
||||
}
|
||||
|
||||
// Make a list of trusted validators.
|
||||
// Register the validators with AmendmentTable and return the list.
|
||||
static std::vector<std::pair<PublicKey, SecretKey>>
|
||||
makeValidators(int num, std::unique_ptr<AmendmentTable> const& table)
|
||||
{
|
||||
std::vector<std::pair<PublicKey, SecretKey>> ret;
|
||||
ret.reserve(num);
|
||||
hash_set<PublicKey> trustedValidators;
|
||||
trustedValidators.reserve(num);
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
auto const& back = ret.emplace_back(randomKeyPair(KeyType::Secp256k1));
|
||||
trustedValidators.insert(back.first);
|
||||
}
|
||||
table->trustChanged(trustedValidators);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static NetClock::time_point
|
||||
hourTime(std::chrono::hours h)
|
||||
{
|
||||
return NetClock::time_point{h};
|
||||
}
|
||||
|
||||
// State threaded through successive consensus rounds.
|
||||
// votes: Amendments and the number of validators who vote for them
|
||||
// enabled: In/out enabled amendments
|
||||
// majority: In/out majority amendments (and when they got a majority)
|
||||
struct VotingState
|
||||
{
|
||||
std::vector<std::pair<uint256, int>> votes;
|
||||
std::set<uint256> enabled;
|
||||
majorityAmendments_t majority;
|
||||
};
|
||||
|
||||
// Execute a pretend consensus round for a flag ledger, returning the
|
||||
// amendments we voted for.
|
||||
static std::vector<uint256>
|
||||
doRound(
|
||||
Rules const& rules,
|
||||
AmendmentTable& table,
|
||||
std::chrono::hours hour,
|
||||
std::vector<std::pair<PublicKey, SecretKey>> const& validators,
|
||||
VotingState& votingState)
|
||||
{
|
||||
// Do a round at the specified time
|
||||
|
||||
// Parameters:
|
||||
// table: Our table of known and vetoed amendments
|
||||
// validators: The addresses of validators we trust
|
||||
// votingState: The voting state carried across rounds (see VotingState)
|
||||
|
||||
auto const roundTime = hourTime(hour);
|
||||
|
||||
// Build validations
|
||||
std::vector<std::shared_ptr<STValidation>> validations;
|
||||
validations.reserve(validators.size());
|
||||
|
||||
int i = 0;
|
||||
for (auto const& [pub, sec] : validators)
|
||||
{
|
||||
++i;
|
||||
std::vector<uint256> field;
|
||||
|
||||
for (auto const& [hash, nVotes] : votingState.votes)
|
||||
{
|
||||
if (nVotes >= i)
|
||||
{
|
||||
// We vote yes on this amendment
|
||||
field.push_back(hash);
|
||||
}
|
||||
}
|
||||
|
||||
auto v = std::make_shared<STValidation>(
|
||||
xrpl::NetClock::time_point{}, pub, sec, calcNodeID(pub), [&field](STValidation& v) {
|
||||
if (!field.empty())
|
||||
v.setFieldV256(sfAmendments, STVector256(sfAmendments, field));
|
||||
v.setFieldU32(sfLedgerSequence, 6180339);
|
||||
});
|
||||
|
||||
validations.emplace_back(v);
|
||||
}
|
||||
|
||||
std::vector<uint256> ourVotes = table.doValidation(votingState.enabled);
|
||||
|
||||
auto actions = table.doVoting(
|
||||
rules, roundTime, votingState.enabled, votingState.majority, validations);
|
||||
for (auto const& [hash, action] : actions)
|
||||
{
|
||||
// This code assumes other validators do as we do
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case 0:
|
||||
// amendment goes from majority to enabled
|
||||
if (votingState.enabled.contains(hash))
|
||||
Throw<std::runtime_error>("enabling already enabled");
|
||||
if (!votingState.majority.contains(hash))
|
||||
Throw<std::runtime_error>("enabling without majority");
|
||||
votingState.enabled.insert(hash);
|
||||
votingState.majority.erase(hash);
|
||||
break;
|
||||
|
||||
case tfGotMajority:
|
||||
if (votingState.majority.contains(hash))
|
||||
Throw<std::runtime_error>("got majority while having majority");
|
||||
votingState.majority[hash] = roundTime;
|
||||
break;
|
||||
|
||||
case tfLostMajority:
|
||||
if (!votingState.majority.contains(hash))
|
||||
Throw<std::runtime_error>("lost majority without majority");
|
||||
votingState.majority.erase(hash);
|
||||
break;
|
||||
|
||||
default:
|
||||
Throw<std::runtime_error>("unknown action");
|
||||
}
|
||||
}
|
||||
|
||||
return ourVotes;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(AmendmentTableTest, construction)
|
||||
{
|
||||
auto table = makeTable(weeks(1));
|
||||
|
||||
for (auto const& a : allSupported)
|
||||
EXPECT_TRUE(table->isSupported(amendmentId(a)));
|
||||
|
||||
for (auto const& a : yes)
|
||||
EXPECT_TRUE(table->isSupported(amendmentId(a)));
|
||||
|
||||
for (auto const& a : enabled)
|
||||
EXPECT_TRUE(table->isSupported(amendmentId(a)));
|
||||
|
||||
for (auto const& a : vetoed)
|
||||
{
|
||||
EXPECT_TRUE(table->isSupported(amendmentId(a)));
|
||||
EXPECT_FALSE(table->isEnabled(amendmentId(a)));
|
||||
}
|
||||
|
||||
for (auto const& a : obsolete)
|
||||
{
|
||||
EXPECT_TRUE(table->isSupported(amendmentId(a)));
|
||||
EXPECT_FALSE(table->isEnabled(amendmentId(a)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(AmendmentTableTest, name_to_id_mapping)
|
||||
{
|
||||
auto table = makeTable(weeks(1));
|
||||
|
||||
for (auto const& a : yes)
|
||||
EXPECT_EQ(table->find(a), amendmentId(a));
|
||||
for (auto const& a : enabled)
|
||||
EXPECT_EQ(table->find(a), amendmentId(a));
|
||||
for (auto const& a : vetoed)
|
||||
EXPECT_EQ(table->find(a), amendmentId(a));
|
||||
for (auto const& a : obsolete)
|
||||
EXPECT_EQ(table->find(a), amendmentId(a));
|
||||
for (auto const& a : unsupported)
|
||||
EXPECT_FALSE(table->find(a));
|
||||
for (auto const& a : unsupportedMajority)
|
||||
EXPECT_FALSE(table->find(a));
|
||||
}
|
||||
|
||||
// Vetoing an unsupported amendment adds it to the table. The veto is only
|
||||
// visible through getJson when admin information is requested.
|
||||
TEST_F(AmendmentTableTest, getjson_veto_unsupported)
|
||||
{
|
||||
auto table = makeTable(weeks(1));
|
||||
|
||||
uint256 const unsupportedID = amendmentId(unsupported[0]);
|
||||
|
||||
// Before vetoing, the amendment is not in the table.
|
||||
{
|
||||
json::Value const unsupp = table->getJson(unsupportedID, true)[to_string(unsupportedID)];
|
||||
EXPECT_EQ(unsupp.size(), 0u);
|
||||
}
|
||||
{
|
||||
json::Value const unsupp = table->getJson(unsupportedID, false)[to_string(unsupportedID)];
|
||||
EXPECT_EQ(unsupp.size(), 0u);
|
||||
}
|
||||
|
||||
table->veto(unsupportedID);
|
||||
|
||||
// After vetoing, the veto is reported with admin, but not without.
|
||||
{
|
||||
json::Value const unsupp = table->getJson(unsupportedID, true)[to_string(unsupportedID)];
|
||||
EXPECT_TRUE(unsupp[jss::vetoed].asBool());
|
||||
}
|
||||
{
|
||||
json::Value const unsupp = table->getJson(unsupportedID, false)[to_string(unsupportedID)];
|
||||
EXPECT_FALSE(unsupp[jss::vetoed].asBool());
|
||||
}
|
||||
}
|
||||
|
||||
// Each case provides a function that builds a malformed amendment entry from a
|
||||
// valid amendment id. Every malformed entry must be rejected with the same
|
||||
// "Invalid entry" error.
|
||||
struct BadConfigParam
|
||||
{
|
||||
std::string name;
|
||||
std::string (*makeEntry)(std::string const& id);
|
||||
};
|
||||
|
||||
struct AmendmentTableBadConfigTest : AmendmentTableTest,
|
||||
::testing::WithParamInterface<BadConfigParam>
|
||||
{
|
||||
};
|
||||
|
||||
TEST_P(AmendmentTableBadConfigTest, rejects_invalid_entry)
|
||||
{
|
||||
auto const yesVotes = makeDefaultYes(yes);
|
||||
auto const section = makeSection(vetoed);
|
||||
auto const id = to_string(amendmentId(enabled[0]));
|
||||
|
||||
std::string const entry = GetParam().makeEntry(id);
|
||||
|
||||
Section test = section;
|
||||
test.append(entry);
|
||||
|
||||
EXPECT_THAT(
|
||||
[&] { makeTable(weeks(2), yesVotes, test, emptySection); },
|
||||
ThrowsMessage<std::runtime_error>("Invalid entry '" + entry + "' in [Test]"));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
AmendmentTableBadConfig,
|
||||
AmendmentTableBadConfigTest,
|
||||
::testing::Values(
|
||||
// Two arguments are required - we pass one
|
||||
BadConfigParam{
|
||||
"too_few_arguments",
|
||||
[](std::string const& id) -> std::string { return id; }},
|
||||
// Two arguments are required - we pass three
|
||||
BadConfigParam{
|
||||
"too_many_arguments",
|
||||
[](std::string const& id) -> std::string { return id + " Test Name"; }},
|
||||
BadConfigParam{
|
||||
"short_id",
|
||||
[](std::string const& id) -> std::string {
|
||||
std::string sid = id;
|
||||
sid.resize(sid.length() - 1);
|
||||
return sid + " Name";
|
||||
}},
|
||||
BadConfigParam{
|
||||
"long_id",
|
||||
[](std::string const& id) -> std::string {
|
||||
std::string sid = id;
|
||||
sid.resize(sid.length() + 1, '0');
|
||||
return sid + " Name";
|
||||
}},
|
||||
BadConfigParam{
|
||||
"non_hex_id",
|
||||
[](std::string const& id) -> std::string {
|
||||
std::string sid = id;
|
||||
sid.resize(sid.length() - 1);
|
||||
sid.push_back('Q');
|
||||
return sid + " Name";
|
||||
}}),
|
||||
[](::testing::TestParamInfo<BadConfigParam> const& info) { return info.param.name; });
|
||||
|
||||
TEST_F(AmendmentTableTest, enable)
|
||||
{
|
||||
std::unique_ptr<AmendmentTable> table = makeTable(weeks(2));
|
||||
|
||||
// Note which entries are enabled (convert the amendment names to IDs)
|
||||
std::set<uint256> allEnabled;
|
||||
for (auto const& a : enabled)
|
||||
allEnabled.insert(amendmentId(a));
|
||||
|
||||
for (uint256 const& a : allEnabled)
|
||||
EXPECT_TRUE(table->enable(a));
|
||||
|
||||
// So far all enabled amendments are supported.
|
||||
EXPECT_FALSE(table->hasUnsupportedEnabled());
|
||||
|
||||
// Verify all enables are enabled and nothing else.
|
||||
for (std::string const& a : yes)
|
||||
{
|
||||
uint256 const supportedID = amendmentId(a);
|
||||
bool const enabled = table->isEnabled(supportedID);
|
||||
bool const found = allEnabled.contains(supportedID);
|
||||
EXPECT_EQ(enabled, found) << a << (enabled ? " enabled " : " disabled ")
|
||||
<< (found ? " found" : " not found");
|
||||
}
|
||||
}
|
||||
|
||||
// All supported and unVetoed amendments should be returned as desired.
|
||||
TEST_F(AmendmentTableTest, desired_excludes_vetoed)
|
||||
{
|
||||
std::unique_ptr<AmendmentTable> table = makeTable(weeks(2));
|
||||
|
||||
for (std::string const& a : enabled)
|
||||
table->enable(amendmentId(a));
|
||||
|
||||
std::set<uint256> vetoed;
|
||||
for (std::string const& a : this->vetoed)
|
||||
vetoed.insert(amendmentId(a));
|
||||
|
||||
std::vector<uint256> const desired = table->getDesired();
|
||||
for (uint256 const& a : desired)
|
||||
EXPECT_TRUE(!vetoed.contains(a));
|
||||
|
||||
// Unveto an amendment that is already not vetoed. Shouldn't
|
||||
// hurt anything, but the values returned by getDesired()
|
||||
// shouldn't change.
|
||||
EXPECT_FALSE(table->unVeto(amendmentId(yes[1])));
|
||||
EXPECT_EQ(desired, table->getDesired());
|
||||
}
|
||||
|
||||
// UnVeto one of the vetoed amendments. It should now be desired.
|
||||
TEST_F(AmendmentTableTest, unveto_vetoed)
|
||||
{
|
||||
std::unique_ptr<AmendmentTable> table = makeTable(weeks(2));
|
||||
|
||||
for (std::string const& a : enabled)
|
||||
table->enable(amendmentId(a));
|
||||
|
||||
uint256 const unvetoedID = amendmentId(vetoed[0]);
|
||||
EXPECT_TRUE(table->unVeto(unvetoedID));
|
||||
|
||||
std::vector<uint256> const desired = table->getDesired();
|
||||
EXPECT_TRUE(std::ranges::find(desired, unvetoedID) != desired.end());
|
||||
}
|
||||
|
||||
// Veto all supported amendments. Now desired should be empty.
|
||||
TEST_F(AmendmentTableTest, veto_all)
|
||||
{
|
||||
std::unique_ptr<AmendmentTable> table = makeTable(weeks(2));
|
||||
|
||||
for (std::string const& a : enabled)
|
||||
table->enable(amendmentId(a));
|
||||
|
||||
for (std::string const& a : allSupported)
|
||||
table->veto(amendmentId(a));
|
||||
|
||||
EXPECT_TRUE(table->getDesired().empty());
|
||||
}
|
||||
|
||||
// Enable an unsupported amendment.
|
||||
TEST_F(AmendmentTableTest, enable_unsupported)
|
||||
{
|
||||
std::unique_ptr<AmendmentTable> table = makeTable(weeks(2));
|
||||
|
||||
EXPECT_FALSE(table->hasUnsupportedEnabled());
|
||||
table->enable(amendmentId(unsupported[0]));
|
||||
EXPECT_TRUE(table->hasUnsupportedEnabled());
|
||||
}
|
||||
|
||||
TEST_F(AmendmentTableTest, has_unsupported_enabled)
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
constexpr weeks kW(1);
|
||||
auto table = makeTable(kW);
|
||||
EXPECT_FALSE(table->hasUnsupportedEnabled());
|
||||
EXPECT_FALSE(table->firstUnsupportedExpected());
|
||||
EXPECT_TRUE(table->needValidatedLedger(1));
|
||||
|
||||
std::set<uint256> enabled;
|
||||
std::ranges::for_each(
|
||||
unsupported, [&enabled](auto const& s) { enabled.insert(amendmentId(s)); });
|
||||
|
||||
majorityAmendments_t majority;
|
||||
table->doValidatedLedger(1, enabled, majority);
|
||||
EXPECT_TRUE(table->hasUnsupportedEnabled());
|
||||
EXPECT_FALSE(table->firstUnsupportedExpected());
|
||||
|
||||
NetClock::duration t{1000s};
|
||||
std::ranges::for_each(unsupportedMajority, [&majority, &t](auto const& s) {
|
||||
majority[amendmentId(s)] = NetClock::time_point{--t};
|
||||
});
|
||||
|
||||
table->doValidatedLedger(1, enabled, majority);
|
||||
EXPECT_TRUE(table->hasUnsupportedEnabled());
|
||||
ASSERT_TRUE(table->firstUnsupportedExpected());
|
||||
// NOLINTNEXTLINE(bugprone-unchecked-optional-access) checked in ASSERT_TRUE above
|
||||
EXPECT_EQ(*table->firstUnsupportedExpected(), NetClock::time_point{t} + kW);
|
||||
|
||||
// Make sure the table knows when it needs an update.
|
||||
EXPECT_FALSE(table->needValidatedLedger(256));
|
||||
EXPECT_TRUE(table->needValidatedLedger(257));
|
||||
}
|
||||
|
||||
// No vote on unknown amendment
|
||||
TEST_F(AmendmentTableTest, no_on_unknown)
|
||||
{
|
||||
auto const testAmendment = amendmentId("TestAmendment");
|
||||
|
||||
auto table = makeTable(weeks(2), emptyYes, emptySection, emptySection);
|
||||
|
||||
auto const validators = makeValidators(10, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
std::vector<uint256> ourVotes = doRound(allRules(), *table, weeks{1}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
|
||||
votingState.votes.emplace_back(testAmendment, validators.size());
|
||||
|
||||
ourVotes = doRound(allRules(), *table, weeks{2}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
|
||||
votingState.majority[testAmendment] = hourTime(weeks{1});
|
||||
|
||||
// Note that the simulation code assumes others behave as we do,
|
||||
// so the amendment won't get enabled
|
||||
ourVotes = doRound(allRules(), *table, weeks{5}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
}
|
||||
|
||||
// No vote on vetoed amendment
|
||||
TEST_F(AmendmentTableTest, no_on_vetoed)
|
||||
{
|
||||
auto const testAmendment = amendmentId("vetoedAmendment");
|
||||
|
||||
auto table = makeTable(weeks(2), emptyYes, emptySection, makeSection(testAmendment));
|
||||
|
||||
auto const validators = makeValidators(10, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
std::vector<uint256> ourVotes = doRound(allRules(), *table, weeks{1}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
|
||||
votingState.votes.emplace_back(testAmendment, validators.size());
|
||||
|
||||
ourVotes = doRound(allRules(), *table, weeks{2}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
|
||||
votingState.majority[testAmendment] = hourTime(weeks{1});
|
||||
|
||||
ourVotes = doRound(allRules(), *table, weeks{5}, validators, votingState);
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
}
|
||||
|
||||
// Vote on and enable known, not-enabled amendment
|
||||
TEST_F(AmendmentTableTest, vote_enable)
|
||||
{
|
||||
auto table = makeTable(weeks(2), makeDefaultYes(yes), emptySection, emptySection);
|
||||
|
||||
auto const validators = makeValidators(10, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
// Week 1: We should vote for all known amendments not enabled
|
||||
std::vector<uint256> ourVotes = doRound(allRules(), *table, weeks{1}, validators, votingState);
|
||||
EXPECT_EQ(ourVotes.size(), yes.size());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
for (auto const& i : yes)
|
||||
EXPECT_TRUE(!votingState.majority.contains(amendmentId(i)));
|
||||
|
||||
// Now, everyone votes for this feature
|
||||
for (auto const& i : yes)
|
||||
votingState.votes.emplace_back(amendmentId(i), validators.size());
|
||||
|
||||
// Week 2: We should recognize a majority
|
||||
ourVotes = doRound(allRules(), *table, weeks{2}, validators, votingState);
|
||||
EXPECT_EQ(ourVotes.size(), yes.size());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
|
||||
for (auto const& i : yes)
|
||||
EXPECT_EQ(votingState.majority[amendmentId(i)], hourTime(weeks{2}));
|
||||
|
||||
// Week 5: We should enable the amendment
|
||||
ourVotes = doRound(allRules(), *table, weeks{5}, validators, votingState);
|
||||
EXPECT_EQ(votingState.enabled.size(), yes.size());
|
||||
|
||||
// Week 6: We should remove it from our votes and from having a majority
|
||||
ourVotes = doRound(allRules(), *table, weeks{6}, validators, votingState);
|
||||
EXPECT_EQ(votingState.enabled.size(), yes.size());
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
for (auto const& i : yes)
|
||||
EXPECT_TRUE(!votingState.majority.contains(amendmentId(i)));
|
||||
}
|
||||
|
||||
// Detect majority at 80%, enable later
|
||||
TEST_F(AmendmentTableTest, detect_majority)
|
||||
{
|
||||
auto const testAmendment = amendmentId("detectMajority");
|
||||
auto table = makeTable(weeks(2), makeDefaultYes(testAmendment), emptySection, emptySection);
|
||||
|
||||
auto const validators = makeValidators(16, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
for (int i = 0; i <= 17; ++i)
|
||||
{
|
||||
votingState.votes.clear();
|
||||
|
||||
if ((i > 0) && (i < 17))
|
||||
votingState.votes.emplace_back(testAmendment, i);
|
||||
|
||||
std::vector<uint256> const ourVotes =
|
||||
doRound(allRules(), *table, weeks{i}, validators, votingState);
|
||||
|
||||
if (i < 13) // 13 => 13/16 = 0.8125 => > 80%
|
||||
{
|
||||
// We are voting yes, not enabled, no majority
|
||||
EXPECT_FALSE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
}
|
||||
else if (i < 15)
|
||||
{
|
||||
// We have a majority, not enabled, keep voting
|
||||
EXPECT_FALSE(ourVotes.empty());
|
||||
EXPECT_FALSE(votingState.majority.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
}
|
||||
else if (i == 15)
|
||||
{
|
||||
// enable, keep voting, remove from majority
|
||||
EXPECT_FALSE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
EXPECT_FALSE(votingState.enabled.empty());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Done, we should be enabled and not voting
|
||||
EXPECT_TRUE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
EXPECT_FALSE(votingState.enabled.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Detect loss of majority
|
||||
TEST_F(AmendmentTableTest, lost_majority)
|
||||
{
|
||||
auto const testAmendment = amendmentId("lostMajority");
|
||||
|
||||
auto table = makeTable(weeks(8), makeDefaultYes(testAmendment), emptySection, emptySection);
|
||||
|
||||
auto const validators = makeValidators(16, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
{
|
||||
// establish majority
|
||||
votingState.votes.emplace_back(testAmendment, validators.size());
|
||||
|
||||
doRound(allRules(), *table, weeks{1}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_FALSE(votingState.majority.empty());
|
||||
}
|
||||
|
||||
for (int i = 1; i < 8; ++i)
|
||||
{
|
||||
// Gradually reduce support
|
||||
votingState.votes.clear();
|
||||
votingState.votes.emplace_back(testAmendment, validators.size() - i);
|
||||
|
||||
std::vector<uint256> const ourVotes =
|
||||
doRound(allRules(), *table, weeks{i + 1}, validators, votingState);
|
||||
|
||||
if (i < 4) // 16 - 3 = 13 => 13/16 = 0.8125 => > 80%
|
||||
{ // 16 - 4 = 12 => 12/16 = 0.75 => < 80%
|
||||
// We are voting yes, not enabled, majority
|
||||
EXPECT_FALSE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_FALSE(votingState.majority.empty());
|
||||
}
|
||||
else
|
||||
{
|
||||
// No majority, not enabled, keep voting
|
||||
EXPECT_FALSE(ourVotes.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise the UNL changing while voting is in progress.
|
||||
TEST_F(AmendmentTableTest, changed_unl)
|
||||
{
|
||||
auto const testAmendment = amendmentId("changedUNL");
|
||||
auto table = makeTable(weeks(8), makeDefaultYes(testAmendment), emptySection, emptySection);
|
||||
|
||||
std::vector<std::pair<PublicKey, SecretKey>> validators = makeValidators(10, table);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
{
|
||||
// 10 validators with 2 voting against won't get majority.
|
||||
votingState.votes.emplace_back(testAmendment, validators.size() - 2);
|
||||
|
||||
doRound(allRules(), *table, weeks{1}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
}
|
||||
|
||||
// Add one new validator to the UNL.
|
||||
validators.emplace_back(randomKeyPair(KeyType::Secp256k1));
|
||||
|
||||
// A lambda that updates the AmendmentTable with the latest
|
||||
// trusted validators.
|
||||
auto callTrustChanged = [](std::vector<std::pair<PublicKey, SecretKey>> const& validators,
|
||||
std::unique_ptr<AmendmentTable> const& table) {
|
||||
// We need a hash_set to pass to trustChanged.
|
||||
hash_set<PublicKey> trustedValidators;
|
||||
trustedValidators.reserve(validators.size());
|
||||
std::ranges::for_each(validators, [&trustedValidators](auto const& val) {
|
||||
trustedValidators.insert(val.first);
|
||||
});
|
||||
|
||||
// Tell the AmendmentTable that the UNL changed.
|
||||
table->trustChanged(trustedValidators);
|
||||
};
|
||||
|
||||
// Tell the table that there's been a change in trusted validators.
|
||||
callTrustChanged(validators, table);
|
||||
|
||||
{
|
||||
// 11 validators with 2 voting against gains majority.
|
||||
votingState.votes.clear();
|
||||
votingState.votes.emplace_back(testAmendment, validators.size() - 2);
|
||||
|
||||
doRound(allRules(), *table, weeks{2}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_FALSE(votingState.majority.empty());
|
||||
}
|
||||
{
|
||||
// One of the validators goes flaky and doesn't send validations
|
||||
// (without the UNL changing) so the amendment loses majority.
|
||||
std::pair<PublicKey, SecretKey> const savedValidator = validators.front();
|
||||
validators.erase(validators.begin());
|
||||
|
||||
votingState.votes.clear();
|
||||
votingState.votes.emplace_back(testAmendment, validators.size() - 2);
|
||||
|
||||
doRound(allRules(), *table, weeks{3}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
|
||||
// Simulate the validator re-syncing to the network by adding it
|
||||
// back to the validators vector
|
||||
validators.insert(validators.begin(), savedValidator);
|
||||
|
||||
votingState.votes.front().second = validators.size() - 2;
|
||||
|
||||
doRound(allRules(), *table, weeks{4}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_FALSE(votingState.majority.empty());
|
||||
|
||||
// Finally, remove one validator from the UNL and see that majority
|
||||
// is lost.
|
||||
validators.erase(validators.begin());
|
||||
|
||||
// Tell the table that there's been a change in trusted validators.
|
||||
callTrustChanged(validators, table);
|
||||
|
||||
votingState.votes.front().second = validators.size() - 2;
|
||||
|
||||
doRound(allRules(), *table, weeks{5}, validators, votingState);
|
||||
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise a validator losing connectivity and then regaining it after
|
||||
// extended delays. Depending on how long that delay is an amendment
|
||||
// either will or will not go live.
|
||||
TEST_F(AmendmentTableTest, validator_flapping)
|
||||
{
|
||||
// We run a test where a validator flaps on and off every 23 hours
|
||||
// and another one one where it flaps on and off every 25 hours.
|
||||
//
|
||||
// Since the local validator vote record expires after 24 hours,
|
||||
// with 23 hour flapping the amendment will go live. But with 25
|
||||
// hour flapping the amendment will not go live.
|
||||
for (int const flapRateHours : {23, 25})
|
||||
{
|
||||
auto const testAmendment = amendmentId("validatorFlapping");
|
||||
auto table = makeTable(weeks(1), makeDefaultYes(testAmendment), emptySection, emptySection);
|
||||
|
||||
// Make two lists of validators, one with a missing validator, to
|
||||
// make it easy to simulate validator flapping.
|
||||
auto const allValidators = makeValidators(11, table);
|
||||
decltype(allValidators)
|
||||
const mostValidators(allValidators.begin() + 1, allValidators.end());
|
||||
EXPECT_EQ(allValidators.size(), mostValidators.size() + 1);
|
||||
|
||||
VotingState votingState;
|
||||
|
||||
votingState.votes.emplace_back(testAmendment, allValidators.size() - 2);
|
||||
|
||||
int delay = flapRateHours;
|
||||
// Loop for 1 week plus a day.
|
||||
for (int hour = 1; hour < (24 * 8); ++hour)
|
||||
{
|
||||
decltype(allValidators) const& thisHoursValidators =
|
||||
(delay < flapRateHours) ? mostValidators : allValidators;
|
||||
delay = delay == flapRateHours ? 0 : delay + 1;
|
||||
|
||||
votingState.votes.front().second = thisHoursValidators.size() - 2;
|
||||
|
||||
using namespace std::chrono;
|
||||
doRound(allRules(), *table, hours(hour), thisHoursValidators, votingState);
|
||||
|
||||
if (hour <= (24 * 7) || flapRateHours > 24)
|
||||
{
|
||||
// The amendment should not be enabled under any
|
||||
// circumstance until one week has elapsed.
|
||||
EXPECT_TRUE(votingState.enabled.empty());
|
||||
|
||||
// If flapping is less than 24 hours, there should be
|
||||
// no flapping. Otherwise we should only have majority
|
||||
// if allValidators vote -- which means there are no
|
||||
// missing validators.
|
||||
bool const expectMajority =
|
||||
(delay <= 24) ? true : &thisHoursValidators == &allValidators;
|
||||
EXPECT_NE(votingState.majority.empty(), expectMajority);
|
||||
}
|
||||
else
|
||||
{
|
||||
// We're...
|
||||
// o Past one week, and
|
||||
// o AmendmentFlapping was less than 24 hours.
|
||||
// The amendment should be enabled.
|
||||
EXPECT_FALSE(votingState.enabled.empty());
|
||||
EXPECT_TRUE(votingState.majority.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xrpl::test
|
||||
@@ -1,8 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
18
src/xrpld/app/misc/AmendmentTableImpl.h
Normal file
18
src/xrpld/app/misc/AmendmentTableImpl.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <xrpl/ledger/AmendmentTable.h>
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
std::unique_ptr<AmendmentTable>
|
||||
make_AmendmentTable(
|
||||
ServiceRegistry& registry,
|
||||
std::chrono::seconds majorityTime,
|
||||
std::vector<AmendmentTable::FeatureInfo> const& supported,
|
||||
Section const& enabled,
|
||||
Section const& vetoed,
|
||||
beast::Journal journal);
|
||||
|
||||
} // namespace xrpl
|
||||
Reference in New Issue
Block a user