Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixUniversalNumber: Number merge #4192

Merged
merged 13 commits into from
Feb 7, 2023
Merged
3 changes: 3 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ target_sources (xrpl_core PRIVATE
src/ripple/basics/impl/FileUtilities.cpp
src/ripple/basics/impl/IOUAmount.cpp
src/ripple/basics/impl/Log.cpp
src/ripple/basics/impl/Number.cpp
src/ripple/basics/impl/StringUtilities.cpp
#[===============================[
main sources:
Expand Down Expand Up @@ -153,6 +154,7 @@ install (
src/ripple/basics/LocalValue.h
src/ripple/basics/Log.h
src/ripple/basics/MathUtilities.h
src/ripple/basics/Number.h
src/ripple/basics/safe_cast.h
src/ripple/basics/Slice.h
src/ripple/basics/spinlock.h
Expand Down Expand Up @@ -737,6 +739,7 @@ if (tests)
src/test/basics/FileUtilities_test.cpp
src/test/basics/IOUAmount_test.cpp
src/test/basics/KeyCache_test.cpp
src/test/basics/Number_test.cpp
src/test/basics/PerfLog_test.cpp
src/test/basics/RangeSet_test.cpp
src/test/basics/scope_test.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/app/misc/impl/TxQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ TxQ::MaybeTx::apply(Application& app, OpenView& view, beast::Journal j)
// If the rules or flags change, preflight again
assert(pfresult);
STAmountSO stAmountSO{view.rules().enabled(fixSTAmountCanonicalize)};
NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};

if (pfresult->rules != view.rules() || pfresult->flags != flags)
{
Expand Down Expand Up @@ -717,6 +718,7 @@ TxQ::apply(
beast::Journal j)
{
STAmountSO stAmountSO{view.rules().enabled(fixSTAmountCanonicalize)};
NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};

// See if the transaction paid a high enough fee that it can go straight
// into the ledger.
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/paths/impl/AmountSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct AmountSpec
union
{
XRPAmount xrp;
IOUAmount iou;
IOUAmount iou = {};
};
std::optional<AccountID> issuer;
std::optional<Currency> currency;
Expand Down Expand Up @@ -64,7 +64,7 @@ struct EitherAmount

union
{
IOUAmount iou;
IOUAmount iou = {};
XRPAmount xrp;
};

Expand Down
1 change: 1 addition & 0 deletions src/ripple/app/tx/impl/Transactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ Transactor::operator()()
JLOG(j_.trace()) << "apply: " << ctx_.tx.getTransactionID();

STAmountSO stAmountSO{view().rules().enabled(fixSTAmountCanonicalize)};
NumberSO stNumberSO{view().rules().enabled(fixUniversalNumber)};

#ifdef DEBUG
{
Expand Down
1 change: 1 addition & 0 deletions src/ripple/app/tx/impl/apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ apply(
beast::Journal j)
{
STAmountSO stAmountSO{view.rules().enabled(fixSTAmountCanonicalize)};
NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};

auto pfresult = preflight(app, view.rules(), tx, flags, j);
auto pcresult = preclaim(pfresult, app, view);
Expand Down
162 changes: 112 additions & 50 deletions src/ripple/basics/IOUAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef RIPPLE_BASICS_IOUAMOUNT_H_INCLUDED
#define RIPPLE_BASICS_IOUAMOUNT_H_INCLUDED

#include <ripple/basics/LocalValue.h>
#include <ripple/basics/Number.h>
#include <ripple/beast/utility/Zero.h>
#include <boost/operators.hpp>
#include <cstdint>
Expand Down Expand Up @@ -56,84 +58,119 @@ class IOUAmount : private boost::totally_ordered<IOUAmount>,

public:
IOUAmount() = default;
IOUAmount(IOUAmount const& other) = default;
IOUAmount&
operator=(IOUAmount const& other) = default;

IOUAmount(beast::Zero)
{
*this = beast::zero;
}
explicit IOUAmount(Number const& other);
IOUAmount(beast::Zero);
IOUAmount(std::int64_t mantissa, int exponent);

IOUAmount(std::int64_t mantissa, int exponent)
: mantissa_(mantissa), exponent_(exponent)
{
normalize();
}
IOUAmount& operator=(beast::Zero);

IOUAmount& operator=(beast::Zero)
{
// The -100 is used to allow 0 to sort less than small positive values
// which will have a large negative exponent.
mantissa_ = 0;
exponent_ = -100;
return *this;
}
operator Number() const;

IOUAmount&
operator+=(IOUAmount const& other);

IOUAmount&
operator-=(IOUAmount const& other)
{
*this += -other;
return *this;
}
operator-=(IOUAmount const& other);

IOUAmount
operator-() const
{
return {-mantissa_, exponent_};
}
operator-() const;

bool
operator==(IOUAmount const& other) const
{
return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
}
operator==(IOUAmount const& other) const;

bool
operator<(IOUAmount const& other) const;

/** Returns true if the amount is not zero */
explicit operator bool() const noexcept
{
return mantissa_ != 0;
}
explicit operator bool() const noexcept;

/** Return the sign of the amount */
int
signum() const noexcept
{
return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
}
signum() const noexcept;

int
exponent() const noexcept
{
return exponent_;
}
exponent() const noexcept;

std::int64_t
mantissa() const noexcept
{
return mantissa_;
}
mantissa() const noexcept;

static IOUAmount
minPositiveAmount();
};

inline IOUAmount::IOUAmount(beast::Zero)
{
*this = beast::zero;
}

inline IOUAmount::IOUAmount(std::int64_t mantissa, int exponent)
: mantissa_(mantissa), exponent_(exponent)
{
normalize();
}

inline IOUAmount& IOUAmount::operator=(beast::Zero)
{
// The -100 is used to allow 0 to sort less than small positive values
// which will have a large negative exponent.
mantissa_ = 0;
exponent_ = -100;
return *this;
}

inline IOUAmount::operator Number() const
{
return Number{mantissa_, exponent_};
}

inline IOUAmount&
IOUAmount::operator-=(IOUAmount const& other)
{
*this += -other;
return *this;
}

inline IOUAmount
IOUAmount::operator-() const
{
return {-mantissa_, exponent_};
}

inline bool
IOUAmount::operator==(IOUAmount const& other) const
{
return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
}

inline bool
IOUAmount::operator<(IOUAmount const& other) const
{
return Number{*this} < Number{other};
}

inline IOUAmount::operator bool() const noexcept
{
return mantissa_ != 0;
}

inline int
IOUAmount::signum() const noexcept
{
return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
}

inline int
IOUAmount::exponent() const noexcept
{
return exponent_;
}

inline std::int64_t
IOUAmount::mantissa() const noexcept
{
return mantissa_;
}

std::string
to_string(IOUAmount const& amount);

Expand All @@ -149,6 +186,31 @@ mulRatio(
std::uint32_t den,
bool roundUp);

extern LocalValue<bool> stNumberSwitchover;

/** RAII class to set and restore the Number switchover.
*/

class NumberSO
{
bool saved_;

public:
~NumberSO()
{
*stNumberSwitchover = saved_;
}

NumberSO(NumberSO const&) = delete;
NumberSO&
operator=(NumberSO const&) = delete;

explicit NumberSO(bool v) : saved_(*stNumberSwitchover)
{
*stNumberSwitchover = v;
}
};

} // namespace ripple

#endif
Loading