From c9c54c97994b3729604a42c5b96bcf6c25d8ba8d Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Wed, 20 Apr 2022 17:10:04 -0400 Subject: [PATCH] Add implicit conversion from STAmount to Number --- src/ripple/basics/impl/Number.cpp | 9 +++++---- src/ripple/protocol/STAmount.h | 9 +++++++++ src/ripple/rpc/impl/ShardArchiveHandler.cpp | 9 +++++---- src/test/basics/Number_test.cpp | 5 +++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ripple/basics/impl/Number.cpp b/src/ripple/basics/impl/Number.cpp index c9e9c51ce..e59ca5fb6 100644 --- a/src/ripple/basics/impl/Number.cpp +++ b/src/ripple/basics/impl/Number.cpp @@ -325,7 +325,7 @@ Number::operator*=(Number const& y) std::to_string(xe)); mantissa_ = xm * zn; exponent_ = xe; - assert(isnormal()); + assert(isnormal() || *this == Number{}); return *this; } @@ -362,8 +362,9 @@ Number::operator/=(Number const& y) static_assert(a2.isnormal()); Number rm2{}; Number rm1{}; - Number r = a2; - r = (a2 * d + a1) * d + a0; + Number r = (a2 * d + a1) * d + a0; + // Newton–Raphson iteration of 1/x - d with initial guess r + // halt when r stops changing, checking for bouncing on the last iteration do { rm2 = rm1; @@ -376,7 +377,7 @@ Number::operator/=(Number const& y) Number::operator rep() const { - std::int64_t drops = mantissa_; + rep drops = mantissa_; int offset = exponent_; guard g; if (drops != 0) diff --git a/src/ripple/protocol/STAmount.h b/src/ripple/protocol/STAmount.h index d0add30db..b6e0e3046 100644 --- a/src/ripple/protocol/STAmount.h +++ b/src/ripple/protocol/STAmount.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -144,6 +145,7 @@ public: // Legacy support for new-style amounts STAmount(IOUAmount const& amount, Issue const& issue); STAmount(XRPAmount const& amount); + operator Number() const; //-------------------------------------------------------------------------- // @@ -370,6 +372,13 @@ inline STAmount::operator bool() const noexcept return *this != beast::zero; } +inline STAmount::operator Number() const +{ + if (mIsNative) + return xrp(); + return iou(); +} + inline STAmount& STAmount::operator=(beast::Zero) { clear(); diff --git a/src/ripple/rpc/impl/ShardArchiveHandler.cpp b/src/ripple/rpc/impl/ShardArchiveHandler.cpp index 2284780c2..d05744f48 100644 --- a/src/ripple/rpc/impl/ShardArchiveHandler.cpp +++ b/src/ripple/rpc/impl/ShardArchiveHandler.cpp @@ -37,11 +37,12 @@ using namespace std::chrono_literals; boost::filesystem::path ShardArchiveHandler::getDownloadDirectory(Config const& config) { - return get(config.section(ConfigSection::shardDatabase()), - "download_path", + return boost::filesystem::path{ get(config.section(ConfigSection::shardDatabase()), - "path", - "")) / + "download_path", + get(config.section(ConfigSection::shardDatabase()), + "path", + ""))} / "download"; } diff --git a/src/test/basics/Number_test.cpp b/src/test/basics/Number_test.cpp index 43a8884c5..b605ed434 100644 --- a/src/test/basics/Number_test.cpp +++ b/src/test/basics/Number_test.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace ripple { @@ -124,6 +125,10 @@ public: BEAST_EXPECT((y == Number{5, 6})); IOUAmount z{y}; BEAST_EXPECT(x == z); + XRPAmount xrp{500}; + STAmount st = xrp; + Number n = st; + BEAST_EXPECT(XRPAmount{n} == xrp); } void