mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-18 16:16:55 +00:00
Compare commits
163 Commits
copilot/re
...
ximinez/nu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42a1106952 | ||
|
|
f75f3ca2ad | ||
|
|
df395d6851 | ||
|
|
8e618d68cd | ||
|
|
cee157485e | ||
|
|
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 |
2
.github/scripts/strategy-matrix/linux.json
vendored
2
.github/scripts/strategy-matrix/linux.json
vendored
@@ -10,7 +10,7 @@
|
||||
|
||||
{
|
||||
"compiler": ["gcc", "clang"],
|
||||
"build_type": ["Debug"],
|
||||
"build_type": ["Debug", "Release"],
|
||||
"arch": ["amd64"],
|
||||
"sanitizers": ["address", "undefinedbehavior"]
|
||||
},
|
||||
|
||||
@@ -233,8 +233,10 @@ words:
|
||||
- pyenv
|
||||
- pyparsing
|
||||
- qalloc
|
||||
- qbsprofile
|
||||
- queuable
|
||||
- Raphson
|
||||
- rcflags
|
||||
- replayer
|
||||
- rerere
|
||||
- retriable
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -12,6 +12,6 @@ namespace xrpl {
|
||||
@throws runtime_error
|
||||
*/
|
||||
void
|
||||
extractTarLz4(std::filesystem::path const& src, std::filesystem::path const& dst);
|
||||
extractTarLz4(boost::filesystem::path const& src, boost::filesystem::path const& dst);
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
std::string
|
||||
getFileContents(
|
||||
std::error_code& ec,
|
||||
std::filesystem::path const& sourcePath,
|
||||
boost::system::error_code& ec,
|
||||
boost::filesystem::path const& sourcePath,
|
||||
std::optional<std::size_t> maxSize = std::nullopt);
|
||||
|
||||
void
|
||||
writeFileContents(
|
||||
std::error_code& ec,
|
||||
std::filesystem::path const& destPath,
|
||||
boost::system::error_code& ec,
|
||||
boost::filesystem::path const& destPath,
|
||||
std::string const& contents);
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <xrpl/beast/utility/Journal.h>
|
||||
|
||||
#include <boost/beast/core/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -76,7 +76,7 @@ private:
|
||||
@return `true` if the file was opened.
|
||||
*/
|
||||
bool
|
||||
open(std::filesystem::path const& path);
|
||||
open(boost::filesystem::path const& path);
|
||||
|
||||
/** Close and re-open the system file associated with the log
|
||||
This assists in interoperating with external log management tools.
|
||||
@@ -118,7 +118,7 @@ private:
|
||||
|
||||
private:
|
||||
std::unique_ptr<std::ofstream> stream_;
|
||||
std::filesystem::path path_;
|
||||
boost::filesystem::path path_;
|
||||
};
|
||||
|
||||
std::mutex mutable mutex_;
|
||||
@@ -137,7 +137,7 @@ public:
|
||||
virtual ~Logs() = default;
|
||||
|
||||
bool
|
||||
open(std::filesystem::path const& pathToLogFile);
|
||||
open(boost::filesystem::path const& pathToLogFile);
|
||||
|
||||
beast::Journal::Sink&
|
||||
get(std::string const& name);
|
||||
|
||||
@@ -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
|
||||
@@ -410,8 +500,8 @@ public:
|
||||
friend constexpr bool
|
||||
operator<(Number const& l, Number const& r) noexcept
|
||||
{
|
||||
bool const lneg = l.negative_;
|
||||
bool const rneg = r.negative_;
|
||||
bool const lneg = l.mantissa_ < 0;
|
||||
bool const rneg = r.mantissa_ < 0;
|
||||
|
||||
// If the two amounts have different signs (zero is treated as positive)
|
||||
// then the comparison is true iff the left is negative.
|
||||
@@ -435,10 +525,13 @@ public:
|
||||
return !lneg;
|
||||
|
||||
// If equal signs and exponents, compare mantissas.
|
||||
if (lneg)
|
||||
if constexpr (std::is_unsigned_v<decltype(l.mantissa_)>)
|
||||
{
|
||||
// If negative, the operator is reversed.
|
||||
return l.mantissa_ > r.mantissa_;
|
||||
if (lneg)
|
||||
{
|
||||
// If negative, the operator is reversed.
|
||||
return l.mantissa_ < r.mantissa_;
|
||||
}
|
||||
}
|
||||
|
||||
return l.mantissa_ < r.mantissa_;
|
||||
@@ -448,9 +541,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
|
||||
@@ -489,6 +584,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 };
|
||||
|
||||
@@ -542,6 +640,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
|
||||
|
||||
@@ -550,6 +660,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);
|
||||
|
||||
@@ -580,6 +698,10 @@ private:
|
||||
MantissaRange::CuspRoundingFix cuspRoundingFixEnabled,
|
||||
bool dropped);
|
||||
|
||||
[[nodiscard]]
|
||||
bool
|
||||
isnormal(MantissaRange const& range) const noexcept;
|
||||
|
||||
[[nodiscard]] bool
|
||||
isnormal() const noexcept;
|
||||
|
||||
@@ -589,18 +711,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}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -611,12 +781,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{})
|
||||
{
|
||||
@@ -639,17 +803,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.
|
||||
@@ -660,16 +814,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
|
||||
@@ -684,7 +829,7 @@ Number::operator-() const noexcept
|
||||
if (mantissa_ == 0)
|
||||
return Number{};
|
||||
auto x = *this;
|
||||
x.negative_ = !x.negative_;
|
||||
x.mantissa_ = -x.mantissa_;
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -765,23 +910,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>
|
||||
@@ -795,12 +946,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>)
|
||||
@@ -809,14 +976,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
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include <xrpl/beast/unit_test/runner.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -25,7 +25,7 @@ makeReason(String const& reason, char const* file, int line)
|
||||
std::string s(reason);
|
||||
if (!s.empty())
|
||||
s.append(": ");
|
||||
namespace fs = std::filesystem;
|
||||
namespace fs = boost::filesystem;
|
||||
s.append(fs::path{file}.filename().string());
|
||||
s.append("(");
|
||||
s.append(boost::lexical_cast<std::string>(line));
|
||||
|
||||
@@ -1,56 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Generate a unique, non-existing path under @p base with an optional @p prefix
|
||||
and a random hex suffix.
|
||||
|
||||
Attempts up to @p maxAttempts paths. Throws `std::runtime_error` if a
|
||||
unique path cannot be found or if the filesystem returns an error while
|
||||
checking for existence.
|
||||
*/
|
||||
inline std::filesystem::path
|
||||
uniqueRandomPath(
|
||||
std::filesystem::path const& base,
|
||||
std::size_t maxAttempts = 100,
|
||||
std::string const& prefix = "")
|
||||
{
|
||||
std::random_device rd;
|
||||
for (std::size_t attempt = 0; attempt < maxAttempts; ++attempt)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << prefix << std::hex << std::setfill('0') << std::setw(8) << rd() << std::setw(8)
|
||||
<< rd();
|
||||
auto candidate = base / oss.str();
|
||||
std::error_code ec;
|
||||
bool const exists = std::filesystem::exists(candidate, ec);
|
||||
if (ec)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Unable to check path '" + candidate.string() + "': " + ec.message());
|
||||
}
|
||||
if (!exists)
|
||||
return candidate;
|
||||
}
|
||||
throw std::runtime_error("Unable to generate a unique path under '" + base.string() + "'");
|
||||
}
|
||||
|
||||
/** RAII temporary directory.
|
||||
|
||||
The directory and all its contents are deleted when
|
||||
the instance of `TempDir` is destroyed.
|
||||
the instance of `temp_dir` is destroyed.
|
||||
*/
|
||||
class TempDir
|
||||
{
|
||||
std::filesystem::path path_;
|
||||
boost::filesystem::path path_;
|
||||
|
||||
public:
|
||||
#if !GENERATING_DOCS
|
||||
@@ -62,16 +25,20 @@ public:
|
||||
/// Construct a temporary directory.
|
||||
TempDir()
|
||||
{
|
||||
path_ = uniqueRandomPath(std::filesystem::temp_directory_path());
|
||||
std::filesystem::create_directory(path_);
|
||||
auto const dir = boost::filesystem::temp_directory_path();
|
||||
do
|
||||
{
|
||||
path_ = dir / boost::filesystem::unique_path();
|
||||
} while (boost::filesystem::exists(path_));
|
||||
boost::filesystem::create_directory(path_);
|
||||
}
|
||||
|
||||
/// Destroy a temporary directory.
|
||||
~TempDir()
|
||||
{
|
||||
// use non-throwing calls in the destructor
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path_, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::remove_all(path_, ec);
|
||||
// TODO: warn/notify if ec set ?
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
#include <xrpl/core/JobTypes.h>
|
||||
#include <xrpl/json/json_value.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -42,7 +43,7 @@ public:
|
||||
*/
|
||||
struct Setup
|
||||
{
|
||||
std::filesystem::path perfLog;
|
||||
boost::filesystem::path perfLog;
|
||||
// log_interval is in milliseconds to support faster testing.
|
||||
milliseconds logInterval{seconds(1)};
|
||||
};
|
||||
@@ -147,7 +148,7 @@ public:
|
||||
};
|
||||
|
||||
PerfLog::Setup
|
||||
setupPerfLog(Section const& section, std::filesystem::path const& configDir);
|
||||
setupPerfLog(Section const& section, boost::filesystem::path const& configDir);
|
||||
|
||||
std::unique_ptr<PerfLog>
|
||||
makePerfLog(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <xrpl/rdb/DBInit.h>
|
||||
#include <xrpl/rdb/SociDB.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -71,7 +72,7 @@ public:
|
||||
|
||||
StartUpType startUp = StartUpType::Normal;
|
||||
bool standAlone = false;
|
||||
std::filesystem::path dataDir;
|
||||
boost::filesystem::path dataDir;
|
||||
// Indicates whether or not to return the `globalPragma`
|
||||
// from commonPragma()
|
||||
bool useGlobalPragma = false;
|
||||
@@ -134,7 +135,7 @@ public:
|
||||
|
||||
template <std::size_t N, std::size_t M>
|
||||
DatabaseCon(
|
||||
std::filesystem::path const& dataDir,
|
||||
boost::filesystem::path const& dataDir,
|
||||
std::string const& dbName,
|
||||
std::array<std::string, N> const& pragma,
|
||||
std::array<char const*, M> const& initSQL,
|
||||
@@ -146,7 +147,7 @@ public:
|
||||
// Use this constructor to setup checkpointing
|
||||
template <std::size_t N, std::size_t M>
|
||||
DatabaseCon(
|
||||
std::filesystem::path const& dataDir,
|
||||
boost::filesystem::path const& dataDir,
|
||||
std::string const& dbName,
|
||||
std::array<std::string, N> const& pragma,
|
||||
std::array<char const*, M> const& initSQL,
|
||||
@@ -181,7 +182,7 @@ private:
|
||||
|
||||
template <std::size_t N, std::size_t M>
|
||||
DatabaseCon(
|
||||
std::filesystem::path const& pPath,
|
||||
boost::filesystem::path const& pPath,
|
||||
std::vector<std::string> const* commonPragma,
|
||||
std::array<std::string, N> const& pragma,
|
||||
std::array<char const*, M> const& initSQL,
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <xrpl/protocol/TxSearched.h>
|
||||
#include <xrpl/rdb/DatabaseCon.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <xrpl/rdb/DatabaseCon.h>
|
||||
#include <xrpl/server/Manifest.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
struct SavedState
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
{ pkgs, ... }:
|
||||
let
|
||||
inherit (import ./packages.nix { inherit pkgs; }) commonPackages;
|
||||
# conan is in the binary cache for Linux but not for Darwin, so on Darwin
|
||||
# it is always built from source — and its bundled test suite is unreliable
|
||||
# in the sandbox: `test_qbsprofile_rcflags` needs gcc (absent on Darwin, see
|
||||
# https://github.com/NixOS/nixpkgs/pull/528995) and the patch tests are
|
||||
# flaky from source. We only use conan as a build tool, so skip its tests on
|
||||
# Darwin. Scoped to the dev shell (not the CI env, which builds conan on
|
||||
# Linux from the cache). Drop once the fix reaches nixos-unstable and the
|
||||
# lock is bumped.
|
||||
pkgs_patched =
|
||||
if pkgs.stdenv.isDarwin then
|
||||
pkgs.extend (
|
||||
final: prev: {
|
||||
conan = prev.conan.overridePythonAttrs (_: {
|
||||
doCheck = false;
|
||||
});
|
||||
}
|
||||
)
|
||||
else
|
||||
pkgs;
|
||||
|
||||
inherit (import ./packages.nix { pkgs = pkgs_patched; }) commonPackages;
|
||||
|
||||
# Supported compiler versions
|
||||
gccVersion = pkgs.lib.range 13 15;
|
||||
|
||||
@@ -2,20 +2,22 @@
|
||||
|
||||
#include <xrpl/basics/contract.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
void
|
||||
extractTarLz4(std::filesystem::path const& src, std::filesystem::path const& dst)
|
||||
extractTarLz4(boost::filesystem::path const& src, boost::filesystem::path const& dst)
|
||||
{
|
||||
if (!std::filesystem::is_regular_file(src))
|
||||
if (!is_regular_file(src))
|
||||
Throw<std::runtime_error>("Invalid source file");
|
||||
|
||||
using archive_ptr = std::unique_ptr<struct archive, void (*)(struct archive*)>;
|
||||
|
||||
@@ -1,24 +1,29 @@
|
||||
#include <xrpl/basics/FileUtilities.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
#include <boost/system/errc.hpp>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
std::string
|
||||
getFileContents(
|
||||
std::error_code& ec,
|
||||
std::filesystem::path const& sourcePath,
|
||||
boost::system::error_code& ec,
|
||||
boost::filesystem::path const& sourcePath,
|
||||
std::optional<std::size_t> maxSize)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
using namespace boost::system::errc;
|
||||
|
||||
path const fullPath{canonical(sourcePath, ec)};
|
||||
if (ec)
|
||||
@@ -27,15 +32,15 @@ getFileContents(
|
||||
if (maxSize && (file_size(fullPath, ec) > *maxSize || ec))
|
||||
{
|
||||
if (!ec)
|
||||
ec = make_error_code(std::errc::file_too_large);
|
||||
ec = make_error_code(file_too_large);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::ifstream fileStream(fullPath, std::ios::in);
|
||||
std::ifstream fileStream(fullPath.string(), std::ios::in);
|
||||
|
||||
if (!fileStream)
|
||||
{
|
||||
ec.assign(errno, std::generic_category());
|
||||
ec = make_error_code(static_cast<errc_t>(errno));
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -44,7 +49,7 @@ getFileContents(
|
||||
|
||||
if (fileStream.bad())
|
||||
{
|
||||
ec.assign(errno, std::generic_category());
|
||||
ec = make_error_code(static_cast<errc_t>(errno));
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -53,15 +58,18 @@ getFileContents(
|
||||
|
||||
void
|
||||
writeFileContents(
|
||||
std::error_code& ec,
|
||||
std::filesystem::path const& destPath,
|
||||
boost::system::error_code& ec,
|
||||
boost::filesystem::path const& destPath,
|
||||
std::string const& contents)
|
||||
{
|
||||
std::ofstream fileStream(destPath, std::ios::out | std::ios::trunc);
|
||||
using namespace boost::filesystem;
|
||||
using namespace boost::system::errc;
|
||||
|
||||
std::ofstream fileStream(destPath.string(), std::ios::out | std::ios::trunc);
|
||||
|
||||
if (!fileStream)
|
||||
{
|
||||
ec.assign(errno, std::generic_category());
|
||||
ec = make_error_code(static_cast<errc_t>(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,7 +77,7 @@ writeFileContents(
|
||||
|
||||
if (fileStream.bad())
|
||||
{
|
||||
ec.assign(errno, std::generic_category());
|
||||
ec = make_error_code(static_cast<errc_t>(errno));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
@@ -54,7 +54,7 @@ Logs::File::isOpen() const noexcept
|
||||
}
|
||||
|
||||
bool
|
||||
Logs::File::open(std::filesystem::path const& path)
|
||||
Logs::File::open(boost::filesystem::path const& path)
|
||||
{
|
||||
close();
|
||||
|
||||
@@ -114,7 +114,7 @@ Logs::Logs(beast::Severity thresh) : thresh_(thresh) // default severity
|
||||
}
|
||||
|
||||
bool
|
||||
Logs::open(std::filesystem::path const& pathToLogFile)
|
||||
Logs::open(boost::filesystem::path const& pathToLogFile)
|
||||
{
|
||||
return file_.open(pathToLogFile);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
@@ -1267,15 +1377,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;
|
||||
@@ -1284,12 +1412,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)
|
||||
@@ -1311,7 +1445,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;
|
||||
}
|
||||
@@ -1321,8 +1455,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;
|
||||
@@ -1344,7 +1480,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
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <xrpl/nodestore/detail/EncodedBlob.h>
|
||||
#include <xrpl/nodestore/detail/codec.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
|
||||
#include <nudb/context.hpp>
|
||||
@@ -34,14 +36,12 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
namespace xrpl::NodeStore {
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
void
|
||||
open(bool createIfMissing, uint64_t appType, uint64_t uid, uint64_t salt) override
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (db.is_open())
|
||||
{
|
||||
// LCOV_EXCL_START
|
||||
@@ -194,12 +194,11 @@ public:
|
||||
|
||||
if (deletePath)
|
||||
{
|
||||
std::error_code fsec;
|
||||
std::filesystem::remove_all(name, fsec);
|
||||
if (fsec)
|
||||
boost::filesystem::remove_all(name, ec);
|
||||
if (ec)
|
||||
{
|
||||
JLOG(j.fatal()) << "Filesystem remove_all of " << name
|
||||
<< " failed with: " << fsec.message();
|
||||
JLOG(j.fatal())
|
||||
<< "Filesystem remove_all of " << name << " failed with: " << ec.message();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -353,7 +352,7 @@ private:
|
||||
static std::size_t
|
||||
parseBlockSize(std::string const& name, Section const& keyValues, beast::Journal journal)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
auto const folder = path(name);
|
||||
auto const kp = (folder / "nudb.key").string();
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
#include <xrpl/nodestore/Scheduler.h>
|
||||
#include <xrpl/nodestore/Types.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <rocksdb/advanced_options.h>
|
||||
#include <rocksdb/cache.h>
|
||||
#include <rocksdb/compression_type.h>
|
||||
@@ -24,7 +27,6 @@
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -263,8 +265,8 @@ public:
|
||||
db.reset();
|
||||
if (deletePath_)
|
||||
{
|
||||
std::filesystem::path const dir = name;
|
||||
std::filesystem::remove_all(dir);
|
||||
boost::filesystem::path const dir = name;
|
||||
boost::filesystem::remove_all(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
#include <xrpl/core/JobQueue.h>
|
||||
#include <xrpl/core/ServiceRegistry.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <soci/blob.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -43,8 +45,8 @@ getSociSqliteInit(std::string const& name, std::string const& dir, std::string c
|
||||
Throw<std::runtime_error>(
|
||||
"Sqlite databases must specify a dir and a name. Name: " + name + " Dir: " + dir);
|
||||
}
|
||||
std::filesystem::path file(dir);
|
||||
if (std::filesystem::is_directory(file))
|
||||
boost::filesystem::path file(dir);
|
||||
if (is_directory(file))
|
||||
file /= name + ext;
|
||||
return file.string();
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <xrpl/rdb/DBInit.h>
|
||||
#include <xrpl/rdb/DatabaseCon.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/format.hpp> // IWYU pragma: keep
|
||||
|
||||
#include <soci/into.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
@@ -19,12 +20,12 @@ namespace xrpl {
|
||||
bool
|
||||
doVacuumDB(DatabaseCon::Setup const& setup, beast::Journal j)
|
||||
{
|
||||
std::filesystem::path const dbPath = setup.dataDir / kTxDbName;
|
||||
boost::filesystem::path const dbPath = setup.dataDir / kTxDbName;
|
||||
|
||||
uintmax_t const dbSize = std::filesystem::file_size(dbPath);
|
||||
uintmax_t const dbSize = file_size(dbPath);
|
||||
XRPL_ASSERT(dbSize != static_cast<uintmax_t>(-1), "xrpl::doVacuumDB : file_size succeeded");
|
||||
|
||||
if (auto available = std::filesystem::space(dbPath.parent_path()).available; available < dbSize)
|
||||
if (auto available = space(dbPath.parent_path()).available; available < dbSize)
|
||||
{
|
||||
std::cerr << "The database filesystem must have at least as "
|
||||
"much free space as the size of "
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
#include <test/jtx/envconfig.h>
|
||||
|
||||
#include <xrpl/beast/unit_test/suite.h>
|
||||
#include <xrpl/beast/utility/temp_dir.h>
|
||||
#include <xrpl/config/Constants.h>
|
||||
#include <xrpl/proto/org/xrpl/rpc/v1/get_ledger.pb.h>
|
||||
#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
|
||||
#include <grpcpp/client_context.h>
|
||||
#include <grpcpp/create_channel.h>
|
||||
#include <grpcpp/grpcpp.h>
|
||||
@@ -16,7 +17,6 @@
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -254,8 +254,10 @@ public:
|
||||
|
||||
TemporaryTLSCertificates()
|
||||
{
|
||||
tempDir_ = beast::uniqueRandomPath(
|
||||
std::filesystem::temp_directory_path(), 100, std::string(kCertsDirPrefix));
|
||||
auto tmpDir = std::filesystem::temp_directory_path();
|
||||
auto uniqueDirName =
|
||||
boost::filesystem::unique_path(std::string(kCertsDirPrefix) + "%%%%%%%%");
|
||||
tempDir_ = tmpDir / uniqueDirName.string();
|
||||
std::filesystem::create_directories(tempDir_);
|
||||
|
||||
writeFile(tempDir_ / kCaCertFilename, kCaCertContent);
|
||||
|
||||
@@ -18,16 +18,16 @@
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <boost/algorithm/string/erase.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -139,7 +139,7 @@ class LedgerLoad_test : public beast::unit_test::Suite
|
||||
{
|
||||
testcase("Load ledger: Bad Files");
|
||||
using namespace test::jtx;
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
// empty path
|
||||
except([&] {
|
||||
@@ -161,8 +161,8 @@ class LedgerLoad_test : public beast::unit_test::Suite
|
||||
});
|
||||
|
||||
// make a corrupted version of the ledger file (last 10 bytes removed).
|
||||
std::error_code ec;
|
||||
auto ledgerFileCorrupt = std::filesystem::path{sd.dbPath} / "ledgerdata_bad.json";
|
||||
boost::system::error_code ec;
|
||||
auto ledgerFileCorrupt = boost::filesystem::path{sd.dbPath} / "ledgerdata_bad.json";
|
||||
copy_file(sd.ledgerFile, ledgerFileCorrupt, copy_options::overwrite_existing, ec);
|
||||
if (!BEAST_EXPECTS(!ec, ec.message()))
|
||||
return;
|
||||
|
||||
@@ -22,12 +22,14 @@
|
||||
#include <xrpl/server/Manifest.h>
|
||||
#include <xrpl/server/Wallet.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -54,18 +56,18 @@ private:
|
||||
}
|
||||
|
||||
static void
|
||||
cleanupDatabaseDir(std::filesystem::path const& dbPath)
|
||||
cleanupDatabaseDir(boost::filesystem::path const& dbPath)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (!exists(dbPath) || !is_directory(dbPath) || !is_empty(dbPath))
|
||||
return;
|
||||
remove(dbPath);
|
||||
}
|
||||
|
||||
static void
|
||||
setupDatabaseDir(std::filesystem::path const& dbPath)
|
||||
setupDatabaseDir(boost::filesystem::path const& dbPath)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (!exists(dbPath))
|
||||
{
|
||||
create_directory(dbPath);
|
||||
@@ -78,10 +80,10 @@ private:
|
||||
Throw<std::runtime_error>("Cannot create directory: " + dbPath.string());
|
||||
}
|
||||
}
|
||||
static std::filesystem::path
|
||||
static boost::filesystem::path
|
||||
getDatabasePath()
|
||||
{
|
||||
return std::filesystem::current_path() / "manifest_test_databases";
|
||||
return boost::filesystem::current_path() / "manifest_test_databases";
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -350,7 +352,7 @@ public:
|
||||
BEAST_EXPECT(loaded.revoked(pk));
|
||||
}
|
||||
}
|
||||
std::filesystem::remove(getDatabasePath() / std::filesystem::path(dbName));
|
||||
boost::filesystem::remove(getDatabasePath() / boost::filesystem::path(dbName));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -22,9 +22,10 @@
|
||||
#include <xrpl/protocol/XRPAmount.h>
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -491,7 +492,7 @@ public:
|
||||
makeBackendRotating(jtx::Env& env, NodeStoreScheduler& scheduler, std::string path)
|
||||
{
|
||||
Section section{env.app().config().section(Sections::kNodeDatabase)};
|
||||
std::filesystem::path newPath;
|
||||
boost::filesystem::path newPath;
|
||||
|
||||
if (!BEAST_EXPECT(path.size()))
|
||||
return {};
|
||||
|
||||
@@ -15,12 +15,13 @@
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/filesystem/directory.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
|
||||
#include <date/date.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
@@ -703,7 +704,7 @@ public:
|
||||
.effectiveOverlap = detail::kDefaultEffectiveOverlap,
|
||||
.expectedRefreshMin = 60 * 24}}); // max of 24 hours
|
||||
}
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
for (auto const& file : directory_iterator(good.subdir()))
|
||||
{
|
||||
remove_all(file);
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
#include <xrpl/basics/FileUtilities.h>
|
||||
#include <xrpl/beast/unit_test/suite.h>
|
||||
|
||||
#include <system_error>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -15,6 +16,7 @@ public:
|
||||
testGetFileContents()
|
||||
{
|
||||
using namespace xrpl::detail;
|
||||
using namespace boost::system;
|
||||
|
||||
static constexpr char const* kExpectedContents =
|
||||
"This file is very short. That's all we need.";
|
||||
@@ -22,7 +24,7 @@ public:
|
||||
FileDirGuard const file(
|
||||
*this, "test_file", "test.txt", "This is temporary text that should get overwritten");
|
||||
|
||||
std::error_code ec;
|
||||
error_code ec;
|
||||
auto const path = file.file();
|
||||
|
||||
writeFileContents(ec, path, kExpectedContents);
|
||||
@@ -45,7 +47,7 @@ public:
|
||||
{
|
||||
// Test with small max
|
||||
auto const bad = getFileContents(ec, path, 16);
|
||||
BEAST_EXPECT(ec && ec == std::errc::file_too_large);
|
||||
BEAST_EXPECT(ec && ec.value() == boost::system::errc::file_too_large);
|
||||
BEAST_EXPECT(bad.empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
@@ -101,9 +102,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 =
|
||||
@@ -128,8 +130,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
|
||||
@@ -239,7 +242,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
|
||||
@@ -249,21 +254,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)
|
||||
@@ -360,14 +376,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)
|
||||
@@ -388,20 +406,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) {
|
||||
@@ -415,70 +443,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);
|
||||
}
|
||||
@@ -486,66 +541,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);
|
||||
}
|
||||
@@ -553,66 +632,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);
|
||||
}
|
||||
@@ -620,66 +723,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);
|
||||
}
|
||||
@@ -914,6 +1040,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}},
|
||||
@@ -925,16 +1053,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{}}},
|
||||
});
|
||||
@@ -983,6 +1111,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
Number const maxInternalMantissa{getMaxInternalMantissa(), 0, Number::Normalized{}};
|
||||
|
||||
auto const cSmall = std::to_array<Number>({
|
||||
Number{2},
|
||||
Number{2'000'000},
|
||||
@@ -992,7 +1122,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;
|
||||
@@ -1344,18 +1477,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");
|
||||
@@ -1683,7 +1816,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}));
|
||||
@@ -1700,21 +1833,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__);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1723,7 +2077,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};
|
||||
@@ -1993,12 +2347,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
|
||||
|
||||
@@ -15,10 +15,14 @@
|
||||
#include <xrpl/protocol/ErrorCodes.h>
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <boost/filesystem/file_status.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iterator>
|
||||
@@ -27,7 +31,6 @@
|
||||
#include <ostream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -40,7 +43,7 @@ class PerfLog_test : public beast::unit_test::Suite
|
||||
{
|
||||
enum class WithFile : bool { No = false, Yes = true };
|
||||
|
||||
using path = std::filesystem::path;
|
||||
using path = boost::filesystem::path;
|
||||
|
||||
// We're only using Env for its Journal. That Journal gives better
|
||||
// coverage in unit tests.
|
||||
@@ -63,14 +66,14 @@ class PerfLog_test : public beast::unit_test::Suite
|
||||
// The error code is intentionally ignored: if the path doesn't
|
||||
// exist (the common case on a clean runner) remove_all returns
|
||||
// an error, and that's fine — there's nothing to clean up.
|
||||
using namespace std::filesystem;
|
||||
std::error_code ec;
|
||||
using namespace boost::filesystem;
|
||||
boost::system::error_code ec;
|
||||
remove_all(logDir(), ec);
|
||||
}
|
||||
|
||||
~Fixture()
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
auto const dir{logDir()};
|
||||
auto const file{logFile()};
|
||||
@@ -93,7 +96,7 @@ class PerfLog_test : public beast::unit_test::Suite
|
||||
static path
|
||||
logDir()
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
return temp_directory_path() / "perf_log_test_dir";
|
||||
}
|
||||
|
||||
@@ -126,7 +129,7 @@ class PerfLog_test : public beast::unit_test::Suite
|
||||
static void
|
||||
wait()
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
auto const path = logFile();
|
||||
if (!exists(path))
|
||||
@@ -198,7 +201,7 @@ public:
|
||||
void
|
||||
testFileCreation()
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
{
|
||||
// Verify a PerfLog creates its file when constructed.
|
||||
@@ -253,23 +256,22 @@ public:
|
||||
|
||||
// Construct and write protect a file to prevent PerfLog
|
||||
// from creating its file.
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(fixture.logDir(), ec);
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::create_directories(fixture.logDir(), ec);
|
||||
if (!BEAST_EXPECT(!ec))
|
||||
return;
|
||||
|
||||
auto fileWriteable = [](std::filesystem::path const& p) -> bool {
|
||||
return std::ofstream{p, std::ios::out | std::ios::app}.is_open();
|
||||
auto fileWriteable = [](boost::filesystem::path const& p) -> bool {
|
||||
return std::ofstream{p.c_str(), std::ios::out | std::ios::app}.is_open();
|
||||
};
|
||||
|
||||
if (!BEAST_EXPECT(fileWriteable(fixture.logFile())))
|
||||
return;
|
||||
|
||||
std::filesystem::permissions(
|
||||
boost::filesystem::permissions(
|
||||
fixture.logFile(),
|
||||
std::filesystem::perms::owner_write | std::filesystem::perms::others_write |
|
||||
std::filesystem::perms::group_write,
|
||||
std::filesystem::perm_options::remove);
|
||||
perms::remove_perms | perms::owner_write | perms::others_write |
|
||||
perms::group_write);
|
||||
|
||||
// If the test is running as root, then the write protect may have
|
||||
// no effect. Make sure write protect worked before proceeding.
|
||||
@@ -293,11 +295,9 @@ public:
|
||||
perfLog->stop();
|
||||
|
||||
// Fix file permissions so the file can be cleaned up.
|
||||
std::filesystem::permissions(
|
||||
boost::filesystem::permissions(
|
||||
fixture.logFile(),
|
||||
std::filesystem::perms::owner_write | std::filesystem::perms::others_write |
|
||||
std::filesystem::perms::group_write,
|
||||
std::filesystem::perm_options::add);
|
||||
perms::add_perms | perms::owner_write | perms::others_write | perms::group_write);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -962,7 +962,7 @@ public:
|
||||
// We can't fully test rotate because unit tests must run on Windows,
|
||||
// and Windows doesn't (may not?) support rotate. But at least call
|
||||
// the interface and see that it doesn't crash.
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
Fixture fixture{env_.app(), j_};
|
||||
BEAST_EXPECT(!exists(fixture.logDir()));
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <xrpl/protocol/SystemParameters.h> // IWYU pragma: keep
|
||||
#include <xrpl/server/Port.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/format.hpp> // IWYU pragma: keep
|
||||
#include <boost/format/free_funcs.hpp>
|
||||
#include <boost/lexical_cast/bad_lexical_cast.hpp>
|
||||
@@ -19,7 +20,6 @@
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
@@ -179,7 +179,7 @@ public:
|
||||
[[nodiscard]] bool
|
||||
dataDirExists() const
|
||||
{
|
||||
return std::filesystem::is_directory(dataDir_);
|
||||
return boost::filesystem::is_directory(dataDir_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool
|
||||
@@ -192,7 +192,7 @@ public:
|
||||
{
|
||||
try
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (rmDataDir_)
|
||||
rmDir(dataDir_);
|
||||
}
|
||||
@@ -273,7 +273,7 @@ public:
|
||||
class Config_test final : public TestSuite
|
||||
{
|
||||
private:
|
||||
using path = std::filesystem::path;
|
||||
using path = boost::filesystem::path;
|
||||
|
||||
public:
|
||||
void
|
||||
@@ -309,7 +309,7 @@ port_wss_admin
|
||||
{
|
||||
testcase("config_file");
|
||||
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
auto const cwd = current_path();
|
||||
|
||||
// Test both config file names.
|
||||
@@ -425,7 +425,7 @@ port_wss_admin
|
||||
{
|
||||
testcase("database_path");
|
||||
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
{
|
||||
boost::format cc("[database_path]\n%1%\n");
|
||||
|
||||
@@ -601,7 +601,7 @@ main
|
||||
{
|
||||
testcase("validators_file");
|
||||
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
{
|
||||
// load should throw for missing specified validators file
|
||||
boost::format cc("[validators_file]\n%1%\n");
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
@@ -32,7 +31,7 @@ class SociDB_test final : public TestSuite
|
||||
{
|
||||
private:
|
||||
static void
|
||||
setupSQLiteConfig(BasicConfig& config, std::filesystem::path const& dbPath)
|
||||
setupSQLiteConfig(BasicConfig& config, boost::filesystem::path const& dbPath)
|
||||
{
|
||||
config.overwrite(Sections::kSqdb, Keys::kBackend, "sqlite");
|
||||
auto value = dbPath.string();
|
||||
@@ -41,18 +40,18 @@ private:
|
||||
}
|
||||
|
||||
static void
|
||||
cleanupDatabaseDir(std::filesystem::path const& dbPath)
|
||||
cleanupDatabaseDir(boost::filesystem::path const& dbPath)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (!exists(dbPath) || !is_directory(dbPath) || !is_empty(dbPath))
|
||||
return;
|
||||
remove(dbPath);
|
||||
}
|
||||
|
||||
static void
|
||||
setupDatabaseDir(std::filesystem::path const& dbPath)
|
||||
setupDatabaseDir(boost::filesystem::path const& dbPath)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (!exists(dbPath))
|
||||
{
|
||||
create_directory(dbPath);
|
||||
@@ -65,10 +64,10 @@ private:
|
||||
Throw<std::runtime_error>("Cannot create directory: " + dbPath.string());
|
||||
}
|
||||
}
|
||||
static std::filesystem::path
|
||||
static boost::filesystem::path
|
||||
getDatabasePath()
|
||||
{
|
||||
return std::filesystem::current_path() / "socidb_test_databases";
|
||||
return boost::filesystem::current_path() / "socidb_test_databases";
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -158,7 +157,7 @@ public:
|
||||
checkValues(s);
|
||||
}
|
||||
{
|
||||
namespace bfs = std::filesystem;
|
||||
namespace bfs = boost::filesystem;
|
||||
// Remove the database
|
||||
bfs::path const dbPath(sc.connectionString());
|
||||
if (bfs::is_regular_file(dbPath))
|
||||
@@ -288,7 +287,7 @@ public:
|
||||
#endif
|
||||
}
|
||||
{
|
||||
namespace bfs = std::filesystem;
|
||||
namespace bfs = boost::filesystem;
|
||||
// Remove the database
|
||||
bfs::path const dbPath(sc.connectionString());
|
||||
if (bfs::is_regular_file(dbPath))
|
||||
@@ -340,7 +339,7 @@ public:
|
||||
s << "SELECT LedgerSeq FROM Ledgers;", soci::into(ledgersLS);
|
||||
BEAST_EXPECT(ledgersLS.size() == numRows);
|
||||
}
|
||||
namespace bfs = std::filesystem;
|
||||
namespace bfs = boost::filesystem;
|
||||
// Remove the database
|
||||
bfs::path const dbPath(sc.connectionString());
|
||||
if (bfs::is_regular_file(dbPath))
|
||||
|
||||
@@ -3,12 +3,21 @@
|
||||
#include <test/jtx/Oracle.h>
|
||||
#include <test/jtx/amount.h>
|
||||
|
||||
#include <xrpld/app/ledger/OpenLedger.h>
|
||||
|
||||
#include <xrpl/basics/Number.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/beast/unit_test/suite.h>
|
||||
#include <xrpl/beast/utility/Journal.h>
|
||||
#include <xrpl/core/ServiceRegistry.h>
|
||||
#include <xrpl/ledger/OpenView.h>
|
||||
#include <xrpl/protocol/Feature.h>
|
||||
#include <xrpl/protocol/Indexes.h>
|
||||
#include <xrpl/protocol/SField.h>
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -312,11 +321,91 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testNullTxReadMeta()
|
||||
{
|
||||
testcase("Null txRead metadata");
|
||||
using namespace jtx;
|
||||
|
||||
// Verify that iteratePriceData handles a null txRead result
|
||||
// gracefully (returns early) rather than crashing with a
|
||||
// nullptr dereference. This simulates local data corruption
|
||||
// where a transaction referenced by sfPreviousTxnID is missing
|
||||
// from the ledger's transaction map.
|
||||
Env env(*this);
|
||||
auto const baseFee = static_cast<int>(env.current()->fees().base.drops());
|
||||
|
||||
Account const owner{"owner"};
|
||||
env.fund(XRP(1'000), owner);
|
||||
|
||||
// Create oracle with XRP/USD and XRP/EUR
|
||||
Oracle oracle(
|
||||
env,
|
||||
{.owner = owner,
|
||||
.series = {{"XRP", "USD", 740, 1}, {"XRP", "EUR", 840, 1}},
|
||||
.fee = baseFee});
|
||||
|
||||
// Update oracle to only have XRP/EUR, pushing XRP/USD into
|
||||
// history. iteratePriceData will need to read historical tx
|
||||
// metadata to find the XRP/USD price.
|
||||
oracle.set(UpdateArg{.series = {{"XRP", "EUR", 850, 1}}, .fee = baseFee});
|
||||
|
||||
OraclesData const oracles{{owner, oracle.documentID()}};
|
||||
|
||||
// Precondition: with an uncorrupted oracle, the historical
|
||||
// traversal must succeed and produce a price for XRP/USD.
|
||||
// This proves the test reaches iteratePriceData's history
|
||||
// path; without it, a future change that breaks the setup
|
||||
// could turn the post-corruption assertion into a vacuous
|
||||
// pass (objectNotFound is reachable from many unrelated
|
||||
// code paths).
|
||||
{
|
||||
auto const ret = Oracle::aggregatePrice(env, "XRP", "USD", oracles);
|
||||
BEAST_EXPECT(!ret.isMember(jss::error));
|
||||
BEAST_EXPECT(ret.isMember(jss::median));
|
||||
}
|
||||
|
||||
// Simulate data corruption: modify the oracle SLE in the open
|
||||
// ledger to have a bogus sfPreviousTxnID that doesn't exist in
|
||||
// any ledger. sfPreviousTxnLgrSeq still points to a valid closed
|
||||
// ledger, so getLedgerBySeq succeeds but txRead returns null.
|
||||
auto const oracleKeylet = keylet::oracle(owner, oracle.documentID());
|
||||
uint256 const bogusTxnID{0xABCABCAB};
|
||||
bool const modified = env.app().getOpenLedger().modify(
|
||||
[&oracleKeylet, &bogusTxnID](OpenView& view, beast::Journal) -> bool {
|
||||
auto const sle = view.read(oracleKeylet);
|
||||
if (!sle)
|
||||
return false;
|
||||
auto replacement = std::make_shared<SLE>(*sle, sle->key());
|
||||
replacement->setFieldH256(sfPreviousTxnID, bogusTxnID);
|
||||
view.rawReplace(replacement);
|
||||
return true;
|
||||
});
|
||||
|
||||
// Confirm the injection actually took effect: modify must
|
||||
// report success, and re-reading the SLE must show the
|
||||
// bogus hash. Otherwise the failure-mode assertion below
|
||||
// would not be exercising the null-txRead path at all.
|
||||
BEAST_EXPECT(modified);
|
||||
if (auto const sle = env.current()->read(oracleKeylet); BEAST_EXPECT(sle))
|
||||
BEAST_EXPECT(sle->getFieldH256(sfPreviousTxnID) == bogusTxnID);
|
||||
|
||||
// Query for XRP/USD using the "current" (open) ledger.
|
||||
// The oracle SLE now has a bogus sfPreviousTxnID. The current
|
||||
// oracle only has EUR, so iteratePriceData will try to read
|
||||
// history. txRead returns null for the bogus hash, and the
|
||||
// null check should cause a graceful early return instead of
|
||||
// a nullptr dereference.
|
||||
auto const ret = Oracle::aggregatePrice(env, "XRP", "USD", oracles);
|
||||
BEAST_EXPECT(ret[jss::error].asString() == "objectNotFound");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testErrors();
|
||||
testRpc();
|
||||
testNullTxReadMeta();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
#include <xrpl/basics/contract.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace xrpl::detail {
|
||||
@@ -15,7 +16,7 @@ namespace xrpl::detail {
|
||||
class DirGuard
|
||||
{
|
||||
protected:
|
||||
using path = std::filesystem::path;
|
||||
using path = boost::filesystem::path;
|
||||
|
||||
private:
|
||||
path subDir_;
|
||||
@@ -42,7 +43,7 @@ public:
|
||||
DirGuard(beast::unit_test::Suite& test, path subDir, bool useCounter = true)
|
||||
: subDir_(std::move(subDir)), test_(test)
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
static auto kSubDirCounter = 0;
|
||||
if (useCounter)
|
||||
@@ -68,7 +69,7 @@ public:
|
||||
{
|
||||
try
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
if (rmSubDir_)
|
||||
rmDir(subDir_);
|
||||
@@ -125,7 +126,7 @@ public:
|
||||
{
|
||||
try
|
||||
{
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
if (exists(file_))
|
||||
{
|
||||
remove(file_);
|
||||
@@ -155,7 +156,7 @@ public:
|
||||
[[nodiscard]] bool
|
||||
fileExists() const
|
||||
{
|
||||
return std::filesystem::exists(file_);
|
||||
return boost::filesystem::exists(file_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -615,7 +614,7 @@ GRPCServerImpl::createServerCredentials()
|
||||
|
||||
try
|
||||
{
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
grpc::SslServerCredentialsOptions sslOpts;
|
||||
grpc::SslServerCredentialsOptions::PemKeyCertPair keyCertPair;
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <xrpl/beast/core/CurrentThreadName.h>
|
||||
#include <xrpl/beast/utility/Journal.h>
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
#include <xrpl/beast/utility/temp_dir.h>
|
||||
#include <xrpl/config/BasicConfig.h>
|
||||
#include <xrpl/config/Constants.h>
|
||||
#include <xrpl/ledger/Ledger.h>
|
||||
@@ -25,12 +24,13 @@
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/filesystem/directory.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@@ -392,10 +392,10 @@ SHAMapStoreImp::dbPaths()
|
||||
if (boost::iequals(get(section, Keys::kType), "memory"))
|
||||
return;
|
||||
|
||||
std::filesystem::path dbPath = get(section, Keys::kPath);
|
||||
if (std::filesystem::exists(dbPath))
|
||||
boost::filesystem::path dbPath = get(section, Keys::kPath);
|
||||
if (boost::filesystem::exists(dbPath))
|
||||
{
|
||||
if (!std::filesystem::is_directory(dbPath))
|
||||
if (!boost::filesystem::is_directory(dbPath))
|
||||
{
|
||||
journal_.error() << "node db path must be a directory. " << dbPath.string();
|
||||
Throw<std::runtime_error>("node db path must be a directory.");
|
||||
@@ -403,7 +403,7 @@ SHAMapStoreImp::dbPaths()
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::create_directories(dbPath);
|
||||
boost::filesystem::create_directories(dbPath);
|
||||
}
|
||||
|
||||
SavedState state = stateDb_.getState();
|
||||
@@ -414,8 +414,8 @@ SHAMapStoreImp::dbPaths()
|
||||
return false;
|
||||
|
||||
// Check if configured "path" matches stored directory path
|
||||
using namespace std::filesystem;
|
||||
auto const stored{std::filesystem::path(sPath)};
|
||||
using namespace boost::filesystem;
|
||||
auto const stored{path(sPath)};
|
||||
if (stored.parent_path() == dbPath)
|
||||
return false;
|
||||
|
||||
@@ -433,9 +433,9 @@ SHAMapStoreImp::dbPaths()
|
||||
bool writableDbExists = false;
|
||||
bool archiveDbExists = false;
|
||||
|
||||
std::vector<std::filesystem::path> pathsToDelete;
|
||||
for (std::filesystem::directory_iterator it(dbPath);
|
||||
it != std::filesystem::directory_iterator();
|
||||
std::vector<boost::filesystem::path> pathsToDelete;
|
||||
for (boost::filesystem::directory_iterator it(dbPath);
|
||||
it != boost::filesystem::directory_iterator();
|
||||
++it)
|
||||
{
|
||||
if (state.writableDb.compare(it->path().string()) == 0)
|
||||
@@ -456,7 +456,7 @@ SHAMapStoreImp::dbPaths()
|
||||
(!archiveDbExists && !state.archiveDb.empty()) || (writableDbExists != archiveDbExists) ||
|
||||
state.writableDb.empty() != state.archiveDb.empty())
|
||||
{
|
||||
std::filesystem::path stateDbPathName = app_.config().legacy(Sections::kDatabasePath);
|
||||
boost::filesystem::path stateDbPathName = app_.config().legacy(Sections::kDatabasePath);
|
||||
stateDbPathName /= dbName_;
|
||||
stateDbPathName += "*";
|
||||
|
||||
@@ -478,15 +478,15 @@ SHAMapStoreImp::dbPaths()
|
||||
}
|
||||
|
||||
// The necessary directories exist. Now, remove any others.
|
||||
for (std::filesystem::path const& p : pathsToDelete)
|
||||
std::filesystem::remove_all(p);
|
||||
for (boost::filesystem::path const& p : pathsToDelete)
|
||||
boost::filesystem::remove_all(p);
|
||||
}
|
||||
|
||||
std::unique_ptr<NodeStore::Backend>
|
||||
SHAMapStoreImp::makeBackendRotating(std::string path)
|
||||
{
|
||||
Section section{app_.config().section(Sections::kNodeDatabase)};
|
||||
std::filesystem::path newPath;
|
||||
boost::filesystem::path newPath;
|
||||
|
||||
if (!path.empty())
|
||||
{
|
||||
@@ -494,7 +494,10 @@ SHAMapStoreImp::makeBackendRotating(std::string path)
|
||||
}
|
||||
else
|
||||
{
|
||||
newPath = beast::uniqueRandomPath(get(section, Keys::kPath), 100, dbPrefix_ + ".");
|
||||
boost::filesystem::path p = get(section, Keys::kPath);
|
||||
p /= dbPrefix_;
|
||||
p += ".%%%%";
|
||||
newPath = boost::filesystem::unique_path(p);
|
||||
}
|
||||
section.set(Keys::kPath, newPath.string());
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
|
||||
@@ -205,7 +204,7 @@ class ValidatorList
|
||||
ManifestCache& validatorManifests_;
|
||||
ManifestCache& publisherManifests_;
|
||||
TimeKeeper& timeKeeper_;
|
||||
std::filesystem::path const dataPath_;
|
||||
boost::filesystem::path const dataPath_;
|
||||
beast::Journal const j_;
|
||||
std::shared_mutex mutable mutex_;
|
||||
using scoped_lock = std::scoped_lock<decltype(mutex_)>;
|
||||
@@ -804,7 +803,7 @@ private:
|
||||
|
||||
/** Get the filename used for caching UNLs
|
||||
*/
|
||||
std::filesystem::path
|
||||
boost::filesystem::path
|
||||
getCacheFileName(scoped_lock const&, PublicKey const& pubKey) const;
|
||||
|
||||
/** Build a Json representation of the collection, suitable for
|
||||
|
||||
@@ -29,8 +29,12 @@
|
||||
#include <xrpl/server/Manifest.h>
|
||||
#include <xrpl/server/NetworkOPs.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/regex/v5/regex.hpp>
|
||||
#include <boost/regex/v5/regex_match.hpp>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
#include <boost/system/errc.hpp>
|
||||
|
||||
#include <xrpl.pb.h>
|
||||
|
||||
@@ -39,7 +43,6 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
@@ -51,7 +54,6 @@
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -286,7 +288,7 @@ ValidatorList::load(
|
||||
return true;
|
||||
}
|
||||
|
||||
std::filesystem::path
|
||||
boost::filesystem::path
|
||||
ValidatorList::getCacheFileName(ValidatorList::scoped_lock const&, PublicKey const& pubKey) const
|
||||
{
|
||||
return dataPath_ / (kFilePrefix + strHex(pubKey));
|
||||
@@ -370,9 +372,9 @@ ValidatorList::cacheValidatorFile(ValidatorList::scoped_lock const& lock, Public
|
||||
if (dataPath_.empty())
|
||||
return;
|
||||
|
||||
std::filesystem::path const filename = getCacheFileName(lock, pubKey);
|
||||
boost::filesystem::path const filename = getCacheFileName(lock, pubKey);
|
||||
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
json::Value value = buildFileData(strHex(pubKey), publisherLists_.at(pubKey), j_);
|
||||
// xrpld should be the only process writing to this file, so
|
||||
@@ -1281,7 +1283,8 @@ std::vector<std::string>
|
||||
ValidatorList::loadLists()
|
||||
{
|
||||
using namespace std::string_literals;
|
||||
using namespace std::filesystem;
|
||||
using namespace boost::filesystem;
|
||||
using namespace boost::system::errc;
|
||||
|
||||
std::scoped_lock const lock{mutex_};
|
||||
|
||||
@@ -1289,12 +1292,12 @@ ValidatorList::loadLists()
|
||||
sites.reserve(publisherLists_.size());
|
||||
for (auto const& [pubKey, publisherCollection] : publisherLists_)
|
||||
{
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
if (publisherCollection.status == PublisherStatus::Available)
|
||||
continue;
|
||||
|
||||
std::filesystem::path const filename = getCacheFileName(lock, pubKey);
|
||||
boost::filesystem::path const filename = getCacheFileName(lock, pubKey);
|
||||
|
||||
auto const fullPath{canonical(filename, ec)};
|
||||
if (ec)
|
||||
@@ -1305,7 +1308,7 @@ ValidatorList::loadLists()
|
||||
{
|
||||
// Treat an empty file as a missing file, because
|
||||
// nobody else is going to write it.
|
||||
ec = make_error_code(std::errc::no_such_file_or_directory);
|
||||
ec = make_error_code(no_such_file_or_directory);
|
||||
}
|
||||
if (ec)
|
||||
continue;
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include <xrpl/rdb/RelationalDatabase.h>
|
||||
#include <xrpl/rdb/SociDB.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/format/free_funcs.hpp>
|
||||
#include <boost/optional/optional.hpp> // IWYU pragma: keep
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
@@ -53,7 +54,6 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
@@ -62,7 +62,6 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@@ -1292,8 +1291,8 @@ getTransaction(
|
||||
bool
|
||||
dbHasSpace(soci::session& session, Config const& config, beast::Journal j)
|
||||
{
|
||||
std::filesystem::space_info const space =
|
||||
std::filesystem::space(config.legacy(Sections::kDatabasePath));
|
||||
boost::filesystem::space_info const space =
|
||||
boost::filesystem::space(config.legacy(Sections::kDatabasePath));
|
||||
|
||||
if (space.available < megabytes(512))
|
||||
{
|
||||
@@ -1304,9 +1303,9 @@ dbHasSpace(soci::session& session, Config const& config, beast::Journal j)
|
||||
if (config.useTxTables())
|
||||
{
|
||||
DatabaseCon::Setup const dbSetup = setupDatabaseCon(config);
|
||||
std::filesystem::path const dbPath = dbSetup.dataDir / kTxDbName;
|
||||
std::error_code ec;
|
||||
std::optional<std::uint64_t> dbSize = std::filesystem::file_size(dbPath, ec);
|
||||
boost::filesystem::path const dbPath = dbSetup.dataDir / kTxDbName;
|
||||
boost::system::error_code ec;
|
||||
std::optional<std::uint64_t> dbSize = boost::filesystem::file_size(dbPath, ec);
|
||||
if (ec)
|
||||
{
|
||||
JLOG(j.error()) << "Error checking transaction db file size: " << ec.message();
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
#include <xrpl/protocol/SystemParameters.h> // VFALCO Breaks levelization
|
||||
#include <xrpl/rdb/DatabaseCon.h>
|
||||
|
||||
#include <boost/filesystem.hpp> // VFALCO FIX: This include should not be here
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
@@ -81,17 +82,17 @@ public:
|
||||
static char const* const kValidatorsFileName;
|
||||
|
||||
/** Returns the full path and filename of the debug log file. */
|
||||
[[nodiscard]] std::filesystem::path
|
||||
[[nodiscard]] boost::filesystem::path
|
||||
getDebugLogFile() const;
|
||||
|
||||
private:
|
||||
std::filesystem::path configFile_;
|
||||
boost::filesystem::path configFile_;
|
||||
|
||||
public:
|
||||
std::filesystem::path configDir;
|
||||
boost::filesystem::path configDir;
|
||||
|
||||
private:
|
||||
std::filesystem::path debugLogfile_;
|
||||
boost::filesystem::path debugLogfile_;
|
||||
|
||||
void
|
||||
load();
|
||||
|
||||
@@ -21,19 +21,21 @@
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/format/free_funcs.hpp>
|
||||
#include <boost/multiprecision/detail/endian.hpp>
|
||||
#include <boost/predef.h>
|
||||
#include <boost/regex.hpp> // IWYU pragma: keep
|
||||
#include <boost/regex/v5/regex.hpp>
|
||||
#include <boost/regex/v5/regex_match.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
@@ -43,7 +45,6 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@@ -312,13 +313,13 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
// directory, use the current working directory as the
|
||||
// config directory and that with "db" as the data
|
||||
// directory.
|
||||
std::filesystem::path dataDir;
|
||||
boost::filesystem::path dataDir;
|
||||
|
||||
if (!strConf.empty())
|
||||
{
|
||||
// --conf=<path> : everything is relative that file.
|
||||
configFile_ = strConf;
|
||||
configDir = std::filesystem::absolute(configFile_);
|
||||
configDir = boost::filesystem::absolute(configFile_);
|
||||
configDir.remove_filename();
|
||||
dataDir = configDir / kDatabaseDirName;
|
||||
}
|
||||
@@ -329,13 +330,13 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
// Check if either of the config files exist in the current working
|
||||
// directory, in which case the databases will be stored in a
|
||||
// subdirectory.
|
||||
configDir = std::filesystem::current_path();
|
||||
configDir = boost::filesystem::current_path();
|
||||
dataDir = configDir / kDatabaseDirName;
|
||||
configFile_ = configDir / kConfigFileName;
|
||||
if (std::filesystem::exists(configFile_))
|
||||
if (boost::filesystem::exists(configFile_))
|
||||
break;
|
||||
configFile_ = configDir / kConfigLegacyName;
|
||||
if (std::filesystem::exists(configFile_))
|
||||
if (boost::filesystem::exists(configFile_))
|
||||
break;
|
||||
|
||||
// Check if the home directory is set, and optionally the XDG config
|
||||
@@ -362,10 +363,10 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
dataDir = strXdgDataHome + "/" + systemName();
|
||||
configDir = strXdgConfigHome + "/" + systemName();
|
||||
configFile_ = configDir / kConfigFileName;
|
||||
if (std::filesystem::exists(configFile_))
|
||||
if (boost::filesystem::exists(configFile_))
|
||||
break;
|
||||
configFile_ = configDir / kConfigLegacyName;
|
||||
if (std::filesystem::exists(configFile_))
|
||||
if (boost::filesystem::exists(configFile_))
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -373,7 +374,7 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
dataDir = "/var/lib/" + systemName();
|
||||
configDir = "/etc/" + systemName();
|
||||
configFile_ = configDir / kConfigFileName;
|
||||
if (std::filesystem::exists(configFile_))
|
||||
if (boost::filesystem::exists(configFile_))
|
||||
break;
|
||||
configFile_ = configDir / kConfigLegacyName;
|
||||
} while (false);
|
||||
@@ -386,7 +387,7 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
std::string const dbPath(legacy(Sections::kDatabasePath));
|
||||
if (!dbPath.empty())
|
||||
{
|
||||
dataDir = std::filesystem::path(dbPath);
|
||||
dataDir = boost::filesystem::path(dbPath);
|
||||
}
|
||||
else if (runStandalone_)
|
||||
{
|
||||
@@ -396,13 +397,13 @@ Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStand
|
||||
|
||||
if (!dataDir.empty())
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(dataDir, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::create_directories(dataDir, ec);
|
||||
|
||||
if (ec)
|
||||
Throw<std::runtime_error>(boost::str(boost::format("Can not create %s") % dataDir));
|
||||
|
||||
legacy(Sections::kDatabasePath, std::filesystem::absolute(dataDir).string());
|
||||
legacy(Sections::kDatabasePath, boost::filesystem::absolute(dataDir).string());
|
||||
}
|
||||
|
||||
HTTPClient::initializeSSLContext(this->sslVerifyDir, this->sslVerifyFile, this->sslVerify, j_);
|
||||
@@ -454,7 +455,7 @@ Config::load()
|
||||
if (!quiet_)
|
||||
std::cerr << "Loading: " << configFile_ << "\n";
|
||||
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
auto const fileContents = getFileContents(ec, configFile_);
|
||||
|
||||
if (ec)
|
||||
@@ -507,8 +508,8 @@ Config::loadFromString(std::string const& fileContents)
|
||||
std::string dbPath;
|
||||
if (getSingleSection(secConfig, Sections::kDatabasePath, dbPath, j_))
|
||||
{
|
||||
std::filesystem::path const p(dbPath);
|
||||
legacy(Sections::kDatabasePath, std::filesystem::absolute(p).string());
|
||||
boost::filesystem::path const p(dbPath);
|
||||
legacy(Sections::kDatabasePath, boost::filesystem::absolute(p).string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -967,7 +968,7 @@ Config::loadFromString(std::string const& fileContents)
|
||||
// If no path was specified, then look for validators.txt
|
||||
// in the same directory as the config file, but don't complain
|
||||
// if we can't find it.
|
||||
std::filesystem::path validatorsFile;
|
||||
boost::filesystem::path validatorsFile;
|
||||
|
||||
if (getSingleSection(secConfig, Sections::kValidatorsFile, strTemp, j_))
|
||||
{
|
||||
@@ -982,7 +983,7 @@ Config::loadFromString(std::string const& fileContents)
|
||||
if (!validatorsFile.is_absolute() && !configDir.empty())
|
||||
validatorsFile = configDir / validatorsFile;
|
||||
|
||||
if (!std::filesystem::exists(validatorsFile))
|
||||
if (!boost::filesystem::exists(validatorsFile))
|
||||
{
|
||||
Throw<std::runtime_error>(
|
||||
std::string("The file specified in [") + Sections::kValidatorsFile +
|
||||
@@ -991,8 +992,8 @@ Config::loadFromString(std::string const& fileContents)
|
||||
validatorsFile.string());
|
||||
}
|
||||
else if (
|
||||
!std::filesystem::is_regular_file(validatorsFile) &&
|
||||
!std::filesystem::is_symlink(validatorsFile))
|
||||
!boost::filesystem::is_regular_file(validatorsFile) &&
|
||||
!boost::filesystem::is_symlink(validatorsFile))
|
||||
{
|
||||
Throw<std::runtime_error>(
|
||||
std::string("Invalid file specified in [") + Sections::kValidatorsFile +
|
||||
@@ -1005,24 +1006,24 @@ Config::loadFromString(std::string const& fileContents)
|
||||
|
||||
if (!validatorsFile.empty())
|
||||
{
|
||||
if (!std::filesystem::exists(validatorsFile))
|
||||
if (!boost::filesystem::exists(validatorsFile))
|
||||
{
|
||||
validatorsFile.clear();
|
||||
}
|
||||
else if (
|
||||
!std::filesystem::is_regular_file(validatorsFile) &&
|
||||
!std::filesystem::is_symlink(validatorsFile))
|
||||
!boost::filesystem::is_regular_file(validatorsFile) &&
|
||||
!boost::filesystem::is_symlink(validatorsFile))
|
||||
{
|
||||
validatorsFile.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!validatorsFile.empty() && std::filesystem::exists(validatorsFile) &&
|
||||
(std::filesystem::is_regular_file(validatorsFile) ||
|
||||
std::filesystem::is_symlink(validatorsFile)))
|
||||
if (!validatorsFile.empty() && boost::filesystem::exists(validatorsFile) &&
|
||||
(boost::filesystem::is_regular_file(validatorsFile) ||
|
||||
boost::filesystem::is_symlink(validatorsFile)))
|
||||
{
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
auto const data = getFileContents(ec, validatorsFile);
|
||||
if (ec)
|
||||
{
|
||||
@@ -1155,7 +1156,7 @@ Config::loadFromString(std::string const& fileContents)
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path
|
||||
boost::filesystem::path
|
||||
Config::getDebugLogFile() const
|
||||
{
|
||||
auto logFile = debugLogfile_;
|
||||
@@ -1164,17 +1165,17 @@ Config::getDebugLogFile() const
|
||||
{
|
||||
// Unless an absolute path for the log file is specified, the
|
||||
// path is relative to the config file directory.
|
||||
logFile = std::filesystem::absolute(configDir / logFile);
|
||||
logFile = boost::filesystem::absolute(logFile, configDir);
|
||||
}
|
||||
|
||||
if (!logFile.empty())
|
||||
{
|
||||
auto logDir = logFile.parent_path();
|
||||
|
||||
if (!std::filesystem::is_directory(logDir))
|
||||
if (!boost::filesystem::is_directory(logDir))
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(logDir, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::create_directories(logDir, ec);
|
||||
|
||||
// If we fail, we warn but continue so that the calling code can
|
||||
// decide how to handle this situation.
|
||||
|
||||
@@ -14,9 +14,11 @@
|
||||
#include <xrpl/json/json_writer.h>
|
||||
#include <xrpl/protocol/jss.h>
|
||||
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
@@ -24,7 +26,6 @@
|
||||
#include <ostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -216,10 +217,10 @@ PerfLogImp::openLog()
|
||||
logFile_.close();
|
||||
|
||||
auto logDir = setup_.perfLog.parent_path();
|
||||
if (!std::filesystem::is_directory(logDir))
|
||||
if (!boost::filesystem::is_directory(logDir))
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(logDir, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::create_directories(logDir, ec);
|
||||
if (ec)
|
||||
{
|
||||
JLOG(j_.fatal()) << "Unable to create performance log "
|
||||
@@ -474,17 +475,17 @@ PerfLogImp::stop()
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PerfLog::Setup
|
||||
setupPerfLog(Section const& section, std::filesystem::path const& configDir)
|
||||
setupPerfLog(Section const& section, boost::filesystem::path const& configDir)
|
||||
{
|
||||
PerfLog::Setup setup;
|
||||
std::string perfLog;
|
||||
set(perfLog, "perf_log", section);
|
||||
if (!perfLog.empty())
|
||||
{
|
||||
setup.perfLog = std::filesystem::path(perfLog);
|
||||
setup.perfLog = boost::filesystem::path(perfLog);
|
||||
if (setup.perfLog.is_relative())
|
||||
{
|
||||
setup.perfLog = std::filesystem::absolute(configDir / setup.perfLog);
|
||||
setup.perfLog = boost::filesystem::absolute(setup.perfLog, configDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user