mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Merge branch 'ximinez/lending-XLS-66' into ximinez/lending-enabled
This commit is contained in:
@@ -24,42 +24,17 @@ isPowerOfTen(T value)
|
||||
|
||||
class Number
|
||||
{
|
||||
public:
|
||||
/** Describes whether and how to enforce this number as an integer.
|
||||
*
|
||||
* - none: No enforcement. The value may vary freely. This is the default.
|
||||
* - compatible: If the absolute value is greater than maxIntValue, valid()
|
||||
* will return false. Needed for backward compatibility with XRP used in
|
||||
* AMMs, and available for functions that will do their own checking. This
|
||||
* is the default for automatic conversions from XRPAmount to Number.
|
||||
* - weak: Like compatible, plus, if the value is unrepresentable (larger
|
||||
* than maxMantissa), assignment and other operations will throw.
|
||||
* - strong: Like weak, plus, if the absolute value is invalid (larger than
|
||||
* maxIntValue), assignment and other operations will throw. This is the
|
||||
* defalut for automatic conversions from MPTAmount to Number.
|
||||
*/
|
||||
enum EnforceInteger { none, compatible, weak, strong };
|
||||
|
||||
private:
|
||||
using rep = std::int64_t;
|
||||
rep mantissa_{0};
|
||||
int exponent_{std::numeric_limits<int>::lowest()};
|
||||
|
||||
// The enforcement setting is not serialized, and does not affect the
|
||||
// ledger. If not "none", the value is checked to be within the valid
|
||||
// integer range. See the enum description for more detail.
|
||||
EnforceInteger enforceInteger_ = none;
|
||||
|
||||
public:
|
||||
// The range for the mantissa when normalized
|
||||
constexpr static rep minMantissa = 1'000'000'000'000'000LL;
|
||||
constexpr static std::int64_t minMantissa = 1'000'000'000'000'000LL;
|
||||
static_assert(isPowerOfTen(minMantissa));
|
||||
constexpr static rep maxMantissa = minMantissa * 10 - 1;
|
||||
constexpr static std::int64_t maxMantissa = minMantissa * 10 - 1;
|
||||
static_assert(maxMantissa == 9'999'999'999'999'999LL);
|
||||
|
||||
constexpr static rep maxIntValue = maxMantissa / 100;
|
||||
static_assert(maxIntValue == 99'999'999'999'999LL);
|
||||
|
||||
// The range for the exponent when normalized
|
||||
constexpr static int minExponent = -32768;
|
||||
constexpr static int maxExponent = 32768;
|
||||
@@ -71,42 +46,15 @@ public:
|
||||
|
||||
explicit constexpr Number() = default;
|
||||
|
||||
Number(rep mantissa, EnforceInteger enforce = none);
|
||||
explicit Number(rep mantissa, int exponent, EnforceInteger enforce = none);
|
||||
Number(rep mantissa);
|
||||
explicit Number(rep mantissa, int exponent);
|
||||
explicit constexpr Number(rep mantissa, int exponent, unchecked) noexcept;
|
||||
constexpr Number(Number const& other) = default;
|
||||
constexpr Number(Number&& other) = default;
|
||||
|
||||
~Number() = default;
|
||||
|
||||
constexpr Number&
|
||||
operator=(Number const& other);
|
||||
constexpr Number&
|
||||
operator=(Number&& other);
|
||||
|
||||
constexpr rep
|
||||
mantissa() const noexcept;
|
||||
constexpr int
|
||||
exponent() const noexcept;
|
||||
|
||||
void
|
||||
setIntegerEnforcement(EnforceInteger enforce);
|
||||
|
||||
EnforceInteger
|
||||
integerEnforcement() const noexcept;
|
||||
|
||||
bool
|
||||
valid() const noexcept;
|
||||
bool
|
||||
representable() const noexcept;
|
||||
/// Combines setIntegerEnforcement(EnforceInteger) and valid()
|
||||
bool
|
||||
valid(EnforceInteger enforce);
|
||||
/// Because this function is const, it should only be used for one-off
|
||||
/// checks
|
||||
bool
|
||||
valid(EnforceInteger enforce) const;
|
||||
|
||||
constexpr Number
|
||||
operator+() const noexcept;
|
||||
constexpr Number
|
||||
@@ -232,9 +180,6 @@ public:
|
||||
private:
|
||||
static thread_local rounding_mode mode_;
|
||||
|
||||
void
|
||||
checkInteger(char const* what) const;
|
||||
|
||||
void
|
||||
normalize();
|
||||
constexpr bool
|
||||
@@ -250,52 +195,16 @@ inline constexpr Number::Number(rep mantissa, int exponent, unchecked) noexcept
|
||||
{
|
||||
}
|
||||
|
||||
inline Number::Number(rep mantissa, int exponent, EnforceInteger enforce)
|
||||
: mantissa_{mantissa}, exponent_{exponent}, enforceInteger_(enforce)
|
||||
inline Number::Number(rep mantissa, int exponent)
|
||||
: mantissa_{mantissa}, exponent_{exponent}
|
||||
{
|
||||
normalize();
|
||||
|
||||
checkInteger("Number::Number integer overflow");
|
||||
}
|
||||
|
||||
inline Number::Number(rep mantissa, EnforceInteger enforce)
|
||||
: Number{mantissa, 0, enforce}
|
||||
inline Number::Number(rep mantissa) : Number{mantissa, 0}
|
||||
{
|
||||
}
|
||||
|
||||
constexpr Number&
|
||||
Number::operator=(Number const& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
mantissa_ = other.mantissa_;
|
||||
exponent_ = other.exponent_;
|
||||
enforceInteger_ = std::max(enforceInteger_, other.enforceInteger_);
|
||||
|
||||
checkInteger("Number::operator= integer overflow");
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Number&
|
||||
Number::operator=(Number&& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// std::move doesn't really do anything for these types, but
|
||||
// this is future-proof in case the types ever change
|
||||
mantissa_ = std::move(other.mantissa_);
|
||||
exponent_ = std::move(other.exponent_);
|
||||
if (other.enforceInteger_ > enforceInteger_)
|
||||
enforceInteger_ = std::move(other.enforceInteger_);
|
||||
|
||||
checkInteger("Number::operator= integer overflow");
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline constexpr Number::rep
|
||||
Number::mantissa() const noexcept
|
||||
{
|
||||
@@ -308,20 +217,6 @@ Number::exponent() const noexcept
|
||||
return exponent_;
|
||||
}
|
||||
|
||||
inline void
|
||||
Number::setIntegerEnforcement(EnforceInteger enforce)
|
||||
{
|
||||
enforceInteger_ = enforce;
|
||||
|
||||
checkInteger("Number::setIntegerEnforcement integer overflow");
|
||||
}
|
||||
|
||||
inline Number::EnforceInteger
|
||||
Number::integerEnforcement() const noexcept
|
||||
{
|
||||
return enforceInteger_;
|
||||
}
|
||||
|
||||
inline constexpr Number
|
||||
Number::operator+() const noexcept
|
||||
{
|
||||
|
||||
@@ -62,15 +62,9 @@ public:
|
||||
explicit constexpr
|
||||
operator bool() const noexcept;
|
||||
|
||||
operator Number() const
|
||||
operator Number() const noexcept
|
||||
{
|
||||
return {value(), Number::strong};
|
||||
}
|
||||
|
||||
Number
|
||||
toNumber(Number::EnforceInteger enforce) const
|
||||
{
|
||||
return {value(), enforce};
|
||||
return value();
|
||||
}
|
||||
|
||||
/** Return the sign of the amount */
|
||||
|
||||
@@ -40,12 +40,6 @@ private:
|
||||
exponent_type mOffset;
|
||||
bool mIsNegative;
|
||||
|
||||
// The Enforce integer setting is not stored or serialized. If set, it is
|
||||
// used during automatic conversions to Number. If not set, the default
|
||||
// behavior is used. It can also be overridden when coverting by using
|
||||
// toNumber().
|
||||
std::optional<Number::EnforceInteger> enforceConversion_;
|
||||
|
||||
public:
|
||||
using value_type = STAmount;
|
||||
|
||||
@@ -143,28 +137,9 @@ public:
|
||||
STAmount(A const& asset, int mantissa, int exponent = 0);
|
||||
|
||||
template <AssetType A>
|
||||
STAmount(
|
||||
A const& asset,
|
||||
Number const& number,
|
||||
std::optional<Number::EnforceInteger> enforce = std::nullopt)
|
||||
STAmount(A const& asset, Number const& number)
|
||||
: STAmount(asset, number.mantissa(), number.exponent())
|
||||
{
|
||||
enforceConversion_ = enforce;
|
||||
if (!enforce)
|
||||
{
|
||||
// Use the default conversion behavior
|
||||
[[maybe_unused]]
|
||||
Number const n = *this;
|
||||
}
|
||||
else if (enforce == Number::strong)
|
||||
{
|
||||
// Throw if it's not valid
|
||||
if (!validNumber())
|
||||
{
|
||||
Throw<std::overflow_error>(
|
||||
"STAmount::STAmount integer Number lost precision");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy support for new-style amounts
|
||||
@@ -172,17 +147,6 @@ public:
|
||||
STAmount(XRPAmount const& amount);
|
||||
STAmount(MPTAmount const& amount, MPTIssue const& mptIssue);
|
||||
operator Number() const;
|
||||
Number
|
||||
toNumber(Number::EnforceInteger enforce) const;
|
||||
|
||||
void
|
||||
setIntegerEnforcement(std::optional<Number::EnforceInteger> enforce);
|
||||
|
||||
std::optional<Number::EnforceInteger>
|
||||
integerEnforcement() const noexcept;
|
||||
|
||||
bool
|
||||
validNumber() const noexcept;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
//
|
||||
@@ -557,8 +521,6 @@ inline STAmount::operator bool() const noexcept
|
||||
|
||||
inline STAmount::operator Number() const
|
||||
{
|
||||
if (enforceConversion_)
|
||||
return toNumber(*enforceConversion_);
|
||||
if (native())
|
||||
return xrp();
|
||||
if (mAsset.holds<MPTIssue>())
|
||||
@@ -566,17 +528,6 @@ inline STAmount::operator Number() const
|
||||
return iou();
|
||||
}
|
||||
|
||||
inline Number
|
||||
STAmount::toNumber(Number::EnforceInteger enforce) const
|
||||
{
|
||||
if (native())
|
||||
return xrp().toNumber(enforce);
|
||||
if (mAsset.holds<MPTIssue>())
|
||||
return mpt().toNumber(enforce);
|
||||
// It doesn't make sense to enforce limits on IOUs
|
||||
return iou();
|
||||
}
|
||||
|
||||
inline STAmount&
|
||||
STAmount::operator=(beast::Zero)
|
||||
{
|
||||
@@ -598,11 +549,6 @@ STAmount::operator=(Number const& number)
|
||||
mValue = mIsNegative ? -number.mantissa() : number.mantissa();
|
||||
mOffset = number.exponent();
|
||||
canonicalize();
|
||||
|
||||
// Convert it back to a Number to check that it's valid
|
||||
[[maybe_unused]]
|
||||
Number n = *this;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -618,7 +564,7 @@ STAmount::clear()
|
||||
{
|
||||
// The -100 is used to allow 0 to sort less than a small positive values
|
||||
// which have a negative exponent.
|
||||
mOffset = integral() ? 0 : -100;
|
||||
mOffset = native() ? 0 : -100;
|
||||
mValue = 0;
|
||||
mIsNegative = false;
|
||||
}
|
||||
|
||||
@@ -56,18 +56,6 @@ public:
|
||||
bool
|
||||
isDefault() const override;
|
||||
|
||||
/// Sets the flag on the underlying number
|
||||
void
|
||||
setIntegerEnforcement(Number::EnforceInteger enforce);
|
||||
|
||||
/// Gets the flag value on the underlying number
|
||||
Number::EnforceInteger
|
||||
integerEnforcement() const noexcept;
|
||||
|
||||
/// Checks the underlying number
|
||||
bool
|
||||
valid() const noexcept;
|
||||
|
||||
operator Number() const
|
||||
{
|
||||
return value_;
|
||||
|
||||
@@ -23,7 +23,6 @@ systemName()
|
||||
|
||||
/** Number of drops in the genesis account. */
|
||||
constexpr XRPAmount INITIAL_XRP{100'000'000'000 * DROPS_PER_XRP};
|
||||
static_assert(INITIAL_XRP.drops() == 100'000'000'000'000'000);
|
||||
|
||||
/** Returns true if the amount does not exceed the initial XRP in existence. */
|
||||
inline bool
|
||||
|
||||
@@ -143,13 +143,7 @@ public:
|
||||
|
||||
operator Number() const noexcept
|
||||
{
|
||||
return {drops(), Number::compatible};
|
||||
}
|
||||
|
||||
Number
|
||||
toNumber(Number::EnforceInteger enforce) const
|
||||
{
|
||||
return {value(), enforce};
|
||||
return drops();
|
||||
}
|
||||
|
||||
/** Return the sign of the amount */
|
||||
|
||||
@@ -480,10 +480,10 @@ LEDGER_ENTRY(ltVAULT, 0x0084, Vault, vault, ({
|
||||
{sfAccount, soeREQUIRED},
|
||||
{sfData, soeOPTIONAL},
|
||||
{sfAsset, soeREQUIRED},
|
||||
{sfAssetsTotal, soeDEFAULT},
|
||||
{sfAssetsAvailable, soeDEFAULT},
|
||||
{sfAssetsTotal, soeREQUIRED},
|
||||
{sfAssetsAvailable, soeREQUIRED},
|
||||
{sfAssetsMaximum, soeDEFAULT},
|
||||
{sfLossUnrealized, soeDEFAULT},
|
||||
{sfLossUnrealized, soeREQUIRED},
|
||||
{sfShareMPTID, soeREQUIRED},
|
||||
{sfWithdrawalPolicy, soeREQUIRED},
|
||||
{sfScale, soeDEFAULT},
|
||||
|
||||
Reference in New Issue
Block a user