Compare commits

..

3 Commits

Author SHA1 Message Date
Bart
303205c117 Add stdexcept include header 2026-04-07 13:24:27 -04:00
Bart
ca1a387c2d Rename namespace in comment 2026-04-07 11:53:58 -04:00
Bart
b275f47a25 refactor: Improve exception handling (#6540) 2026-04-07 11:47:43 -04:00
242 changed files with 5222 additions and 36514 deletions

View File

@@ -44,10 +44,6 @@ suggestWords:
words:
- abempty
- AMMID
- AMMMPT
- AMMMPToken
- AMMMPTokens
- AMMXRP
- amt
- amts
- asnode
@@ -100,7 +96,6 @@ words:
- distro
- doxyfile
- dxrpl
- enabled
- endmacro
- exceptioned
- Falco
@@ -153,8 +148,6 @@ words:
- ltype
- mcmodel
- MEMORYSTATUSEX
- MPTAMM
- MPTDEX
- Merkle
- Metafuncton
- misprediction
@@ -164,7 +157,6 @@ words:
- mptid
- mptissuance
- mptissuanceid
- mptissue
- mptoken
- mptokenid
- mptokenissuance

View File

@@ -2,19 +2,13 @@
#include <xrpl/beast/utility/instrumentation.h>
#include <concepts>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <ostream>
#include <stdexcept>
#include <string>
#ifdef _MSC_VER
#include <boost/multiprecision/cpp_int.hpp>
#endif // !defined(_MSC_VER)
namespace xrpl {
class Number;
@@ -22,39 +16,18 @@ 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)
{
auto const est = logTenEstimate(value);
if (est.second == 1)
return est.first;
int log = 0;
while (value >= 10 && value % 10 == 0)
{
value /= 10;
++log;
}
if (value == 1)
return log;
return std::nullopt;
}
@@ -68,10 +41,12 @@ isPowerOfTen(T value)
/** 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 mantissa_scale enum indicates whether the range is "small" or
* "large". This intentionally prevents the creation of any
* MantissaRanges representing other values.
* The mantissa_scale enum indicates whether the range is "small" or "large".
* This intentionally restricts the number of MantissaRanges that can be
* instantiated to two: one for each scale.
*
* 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
@@ -85,8 +60,8 @@ isPowerOfTen(T value)
* "large" scale.
*
* The "large" scale is intended to represent all values that can be represented
* by an STAmount - IOUs, XRP, and MPTs. It has a min value of 2^63/10+1
* (truncated), and a max value of 2^63-1.
* by an STAmount - IOUs, XRP, and MPTs. It has a min value of 10^18, and a max
* value of 10^19-1.
*
* 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
@@ -98,52 +73,25 @@ struct MantissaRange
enum mantissa_scale { small, large };
explicit constexpr MantissaRange(mantissa_scale scale_)
: max(getMax(scale_)), internalMin(getInternalMin(scale_, min)), scale(scale_)
: min(getMin(scale_)), log(logTen(min).value_or(-1)), scale(scale_)
{
// 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 (computeLog(internalMin) != log)
throw std::out_of_range("Invalid mantissa range: computeLog(internalMin) != log");
}
// Explicitly delete copy and move operations
MantissaRange(MantissaRange const&) = delete;
MantissaRange(MantissaRange&&) = delete;
MantissaRange&
operator=(MantissaRange const&) = delete;
MantissaRange&
operator=(MantissaRange&&) = delete;
rep max;
rep 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 internalMin;
int log{computeLog(min)};
rep min;
rep max{min * 10 - 1};
int log;
mantissa_scale scale;
private:
static constexpr rep
getMax(mantissa_scale scale)
getMin(mantissa_scale scale_)
{
switch (scale)
switch (scale_)
{
case small:
return 9'999'999'999'999'999ULL;
return 1'000'000'000'000'000ULL;
case large:
return std::numeric_limits<std::int64_t>::max();
return 1'000'000'000'000'000'000ULL;
default:
// Since this can never be called outside a non-constexpr
// context, this throw assures that the build fails if an
@@ -151,59 +99,19 @@ private:
throw std::runtime_error("Unknown mantissa scale");
}
}
static constexpr rep
computeMin(rep max)
{
return max / 10 + 1;
}
static constexpr rep
getInternalMin(mantissa_scale scale, rep min)
{
switch (scale)
{
case large:
return 1'000'000'000'000'000'000ULL;
default:
if (isPowerOfTen(min))
return min;
throw std::runtime_error("Unknown/bad mantissa scale");
}
}
static constexpr rep
computeLog(rep min)
{
auto const estimate = logTenEstimate(min);
return estimate.first + (estimate.second == 1 ? 0 : 1);
}
};
// Like std::integral, but only 64-bit integral types.
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 Operational Representation ----
* ---- Internal Representation ----
*
* Internally, Number is represented with three values:
* 1. a bool sign flag,
@@ -218,21 +126,15 @@ concept UnsignedMantissa = std::is_unsigned_v<T> || std::is_same_v<T, uint128_t>
*
* 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.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.
* [MantissaRange.min, MantissaRange.max].
*
* 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. 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.
* 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.
*
* ---- External Interface ----
*
@@ -245,12 +147,13 @@ concept UnsignedMantissa = std::is_unsigned_v<T> || std::is_same_v<T, uint128_t>
* represent the full range of valid XRP and MPT integer values accurately.
*
* Note:
* 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.
* 1. 2^63-1 is between 10^18 and 10^19-1, which are the limits of the "large"
* mantissa range.
* 2. The functions mantissa() and exponent() return the external view of the
* Number value, specifically using a signed 63-bit mantissa.
* 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.
* 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
@@ -305,7 +208,8 @@ class Number
using rep = std::int64_t;
using internalrep = MantissaRange::rep;
rep mantissa_{0};
bool negative_{false};
internalrep mantissa_{0};
int exponent_{std::numeric_limits<int>::lowest()};
public:
@@ -313,6 +217,10 @@ public:
constexpr static int minExponent = -32768;
constexpr static int maxExponent = 32768;
constexpr static internalrep maxRep = std::numeric_limits<rep>::max();
static_assert(maxRep == 9'223'372'036'854'775'807);
static_assert(-maxRep == std::numeric_limits<rep>::min() + 1);
// May need to make unchecked private
struct unchecked
{
@@ -390,7 +298,8 @@ public:
friend constexpr bool
operator==(Number const& x, Number const& y) noexcept
{
return x.mantissa_ == y.mantissa_ && x.exponent_ == y.exponent_;
return x.negative_ == y.negative_ && x.mantissa_ == y.mantissa_ &&
x.exponent_ == y.exponent_;
}
friend constexpr bool
@@ -404,8 +313,8 @@ public:
{
// If the two amounts have different signs (zero is treated as positive)
// then the comparison is true iff the left is negative.
bool const lneg = x.mantissa_ < 0;
bool const rneg = y.mantissa_ < 0;
bool const lneg = x.negative_;
bool const rneg = y.negative_;
if (lneg != rneg)
return lneg;
@@ -433,7 +342,7 @@ public:
constexpr int
signum() const noexcept
{
return mantissa_ < 0 ? -1 : (mantissa_ ? 1 : 0);
return negative_ ? -1 : (mantissa_ ? 1 : 0);
}
Number
@@ -472,9 +381,6 @@ 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 rounding_mode { to_nearest, towards_zero, downward, upward };
static rounding_mode
@@ -539,39 +445,22 @@ private:
static_assert(isPowerOfTen(smallRange.min));
static_assert(smallRange.min == 1'000'000'000'000'000LL);
static_assert(smallRange.max == 9'999'999'999'999'999LL);
static_assert(smallRange.internalMin == smallRange.min);
static_assert(smallRange.log == 15);
static_assert(smallRange.min < maxRep);
static_assert(smallRange.max < maxRep);
constexpr static MantissaRange largeRange{MantissaRange::large};
static_assert(!isPowerOfTen(largeRange.min));
static_assert(largeRange.min == 922'337'203'685'477'581ULL);
static_assert(largeRange.max == internalrep(9'223'372'036'854'775'807ULL));
static_assert(largeRange.max == std::numeric_limits<rep>::max());
static_assert(largeRange.internalMin == 1'000'000'000'000'000'000ULL);
static_assert(isPowerOfTen(largeRange.min));
static_assert(largeRange.min == 1'000'000'000'000'000'000ULL);
static_assert(largeRange.max == internalrep(9'999'999'999'999'999'999ULL));
static_assert(largeRange.log == 18);
// There are 2 values that will not fit in largeRange without some extra
// work
// * 9223372036854775808
// * 9223372036854775809
// They both end up < min, but with a leftover. If they round up, everything
// will be fine. If they don't, we'll need to bring them up into range.
// Guard::bringIntoRange handles this situation.
static_assert(largeRange.min < maxRep);
static_assert(largeRange.max > maxRep);
// The range for the mantissa when normalized.
// Use reference_wrapper to avoid making copies, and prevent accidentally
// changing the values inside the range.
static thread_local std::reference_wrapper<MantissaRange const> range_;
// 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);
void
normalize();
@@ -594,14 +483,11 @@ private:
friend void
doNormalize(
bool& negative,
T& mantissa,
int& exponent,
T& mantissa_,
int& exponent_,
MantissaRange::rep const& minMantissa,
MantissaRange::rep const& maxMantissa);
bool
isnormal(MantissaRange const& range) const noexcept;
bool
isnormal() const noexcept;
@@ -611,60 +497,14 @@ private:
Number
shiftExponent(int exponentDelta) const;
// Safely return the absolute value of a rep (int64) mantissa as an internalrep (uint64).
// 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.
static internalrep
externalToInternal(rep mantissa);
/** Breaks down the number into components, potentially de-normalizing it.
*
* Ensures that the mantissa always has range_.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 range_.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 largestMantissa = largeRange.max;
};
inline constexpr Number::Number(
@@ -672,8 +512,7 @@ inline constexpr Number::Number(
internalrep mantissa,
int exponent,
unchecked) noexcept
: mantissa_{negative ? -static_cast<rep>(mantissa) : static_cast<rep>(mantissa)}
, exponent_{exponent}
: negative_(negative), mantissa_{mantissa}, exponent_{exponent}
{
}
@@ -684,6 +523,12 @@ inline constexpr Number::Number(internalrep mantissa, int exponent, unchecked) n
constexpr static Number numZero{};
inline Number::Number(bool negative, internalrep mantissa, int exponent, normalized)
: Number(negative, mantissa, exponent, unchecked{})
{
normalize();
}
inline Number::Number(internalrep mantissa, int exponent, normalized)
: Number(false, mantissa, exponent, normalized{})
{
@@ -706,7 +551,17 @@ inline Number::Number(rep mantissa) : Number{mantissa, 0}
inline constexpr Number::rep
Number::mantissa() const noexcept
{
return mantissa_;
auto m = mantissa_;
if (m > maxRep)
{
XRPL_ASSERT_PARTS(
!isnormal() || (m % 10 == 0 && m / 10 <= maxRep),
"xrpl::Number::mantissa",
"large normalized mantissa has no remainder");
m /= 10;
}
auto const sign = negative_ ? -1 : 1;
return sign * static_cast<Number::rep>(m);
}
/** Returns the exponent of the external view of the Number.
@@ -717,7 +572,16 @@ Number::mantissa() const noexcept
inline constexpr int
Number::exponent() const noexcept
{
return exponent_;
auto e = exponent_;
if (mantissa_ > maxRep)
{
XRPL_ASSERT_PARTS(
!isnormal() || (mantissa_ % 10 == 0 && mantissa_ / 10 <= maxRep),
"xrpl::Number::exponent",
"large normalized mantissa has no remainder");
++e;
}
return e;
}
inline constexpr Number
@@ -732,7 +596,7 @@ Number::operator-() const noexcept
if (mantissa_ == 0)
return Number{};
auto x = *this;
x.mantissa_ = -x.mantissa_;
x.negative_ = !x.negative_;
return x;
}
@@ -813,58 +677,42 @@ Number::min() noexcept
inline Number
Number::max() noexcept
{
return Number{false, range_.get().max, maxExponent, unchecked{}};
return Number{false, std::min(range_.get().max, maxRep), maxExponent, unchecked{}};
}
inline Number
Number::lowest() noexcept
{
return Number{true, range_.get().max, maxExponent, unchecked{}};
}
inline bool
Number::isnormal(MantissaRange const& range) const noexcept
{
auto const abs_m = externalToInternal(mantissa_);
return *this == Number{} ||
(range.min <= abs_m && abs_m <= range.max && //
minExponent <= exponent_ && exponent_ <= maxExponent);
return Number{true, std::min(range_.get().max, maxRep), maxExponent, unchecked{}};
}
inline bool
Number::isnormal() const noexcept
{
return isnormal(range_);
MantissaRange const& range = range_;
auto const abs_m = mantissa_;
return *this == Number{} ||
(range.min <= abs_m && abs_m <= range.max && (abs_m <= maxRep || abs_m % 10 == 0) &&
minExponent <= exponent_ && exponent_ <= maxExponent);
}
template <Integral64 T>
std::pair<T, int>
Number::normalizeToRange(T minMantissa, T maxMantissa) const
{
bool negative = mantissa_ < 0;
internalrep mantissa = externalToInternal(mantissa_);
bool negative = negative_;
internalrep mantissa = mantissa_;
int exponent = exponent_;
if constexpr (std::is_unsigned_v<T>)
{
XRPL_ASSERT_PARTS(
!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.");
}
Number::normalize(negative, mantissa, exponent, minMantissa, maxMantissa);
// 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);
auto const sign = negative ? -1 : 1;
return std::make_pair(static_cast<T>(sign * mantissa), exponent);
}
inline constexpr Number

View File

@@ -213,60 +213,11 @@ public:
// Called when a credit is made to an account
// This is required to support PaymentSandbox
virtual void
creditHookIOU(
creditHook(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
STAmount const& preCreditBalance)
{
XRPL_ASSERT(amount.holds<Issue>(), "creditHookIOU: amount is for Issue");
}
virtual void
creditHookMPT(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
std::uint64_t preCreditBalanceHolder,
std::int64_t preCreditBalanceIssuer)
{
XRPL_ASSERT(amount.holds<MPTIssue>(), "creditHookMPT: amount is for MPTIssue");
}
/** Facilitate tracking of MPT sold by an issuer owning MPT sell offer.
* Unlike IOU, MPT doesn't have bi-directional relationship with an issuer,
* where a trustline limits an amount that can be issued to a holder.
* Consequently, the credit step (last MPTEndpointStep or
* BookStep buying MPT) might temporarily overflow OutstandingAmount.
* Limiting of a step's output amount in this case is delegated to
* the next step (in rev order). The next step always redeems when a holder
* account sells MPT (first MPTEndpointStep or BookStep selling MPT).
* In this case the holder account is only limited by the step's output
* and it's available funds since it's transferring the funds from one
* account to another account and doesn't change OutstandingAmount.
* This doesn't apply to an offer owned by an issuer.
* In this case the issuer sells or self debits and is increasing
* OutstandingAmount. Ability to issue is limited by the issuer
* originally available funds less already self sold MPT amounts (MPT sell
* offer).
* Consider an example:
* - GW creates MPT(USD) with 1,000USD MaximumAmount.
* - GW pays 950USD to A1.
* - A1 creates an offer 100XRP(buy)/100USD(sell).
* - GW creates an offer 100XRP(buy)/100USD(sell).
* - A2 pays 200USD to A3 with sendMax of 200XRP.
* Since the payment engine executes payments in reverse,
* OutstandingAmount overflows in MPTEndpointStep: 950 + 200 = 1,150USD.
* BookStep first consumes A1 offer. This reduces OutstandingAmount
* by 100USD: 1,150 - 100 = 1,050USD. GW offer can only be partially
* consumed because the initial available amount is 50USD = 1,000 - 950.
* BookStep limits it's output to 150USD. This in turn limits A3's send
* amount to 150XRP: A1 buys 100XRP and sells 100USD to A3. This doesn't
* change OutstandingAmount. GW buys 50XRP and sells 50USD to A3. This
* changes OutstandingAmount to 1,000USD.
*/
virtual void
issuerSelfDebitHookMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance)
{
}

View File

@@ -3,8 +3,8 @@
#include <xrpl/ledger/AcceptedLedgerTx.h>
#include <xrpl/ledger/BookListeners.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Book.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/MultiApiJson.h>
#include <xrpl/protocol/UintTypes.h>
@@ -53,30 +53,30 @@ public:
issue. This is useful for pathfinding to find all possible next hops
from a given currency.
@param asset The asset to search for
@param issue The issue to search for
@param domain Optional domain restriction for the order book
@return Vector of books that want this issue
*/
virtual std::vector<Book>
getBooksByTakerPays(Asset const& asset, std::optional<Domain> const& domain = std::nullopt) = 0;
getBooksByTakerPays(Issue const& issue, std::optional<Domain> const& domain = std::nullopt) = 0;
/** Get the count of order books that want a specific issue.
@param asset The asset to search for
@param issue The issue to search for
@param domain Optional domain restriction for the order book
@return Number of books that want this issue
*/
virtual int
getBookSize(Asset const& asset, std::optional<Domain> const& domain = std::nullopt) = 0;
getBookSize(Issue const& issue, std::optional<Domain> const& domain = std::nullopt) = 0;
/** Check if an order book to XRP exists for the given issue.
@param asset The asset to check
@param issue The issue to check
@param domain Optional domain restriction for the order book
@return true if a book from this issue to XRP exists
*/
virtual bool
isBookToXRP(Asset const& asset, std::optional<Domain> const& domain = std::nullopt) = 0;
isBookToXRP(Issue const& issue, std::optional<Domain> const& domain = std::nullopt) = 0;
/**
* Process a transaction for order book tracking.

View File

@@ -15,55 +15,10 @@ namespace detail {
// into the PaymentSandbox class itself
class DeferredCredits
{
private:
using KeyIOU = std::tuple<AccountID, AccountID, Currency>;
struct ValueIOU
{
explicit ValueIOU() = default;
STAmount lowAcctCredits;
STAmount highAcctCredits;
STAmount lowAcctOrigBalance;
};
struct HolderValueMPT
{
HolderValueMPT() = default;
// Debit to issuer
std::uint64_t debit = 0;
std::uint64_t origBalance = 0;
};
struct IssuerValueMPT
{
IssuerValueMPT() = default;
std::map<AccountID, HolderValueMPT> holders;
// Credit to holder
std::uint64_t credit = 0;
// OutstandingAmount might overflow when MPTs are credited to a holder.
// Consider A1 paying 100MPT to A2 and A1 already having maximum MPTs.
// Since the payment engine executes a payment in revers, A2 is
// credited first and OutstandingAmount is going to be equal
// to MaximumAmount + 100MPT. In the next step A1 redeems 100MPT
// to the issuer and OutstandingAmount balances out.
std::int64_t origBalance = 0;
// Self debit on offer selling MPT. Since the payment engine executes
// a payment in reverse, a crediting/buying step may overflow
// OutstandingAmount. A sell MPT offer owned by a holder can redeem any
// amount up to the offer's amount and holder's available funds,
// balancing out OutstandingAmount. But if the offer's owner is issuer
// then it issues more MPT. In this case the available amount to issue
// is the initial issuer's available amount less all offer sell amounts
// by the issuer. This is self-debit, where the offer's owner,
// issuer in this case, debits to self.
std::uint64_t selfDebit = 0;
};
using AdjustmentMPT = IssuerValueMPT;
public:
struct AdjustmentIOU
struct Adjustment
{
AdjustmentIOU(STAmount const& d, STAmount const& c, STAmount const& b)
Adjustment(STAmount const& d, STAmount const& c, STAmount const& b)
: debits(d), credits(c), origBalance(b)
{
}
@@ -74,30 +29,16 @@ public:
// Get the adjustments for the balance between main and other.
// Returns the debits, credits and the original balance
std::optional<AdjustmentIOU>
adjustmentsIOU(AccountID const& main, AccountID const& other, Currency const& currency) const;
std::optional<AdjustmentMPT>
adjustmentsMPT(MPTID const& mptID) const;
std::optional<Adjustment>
adjustments(AccountID const& main, AccountID const& other, Currency const& currency) const;
void
creditIOU(
credit(
AccountID const& sender,
AccountID const& receiver,
STAmount const& amount,
STAmount const& preCreditSenderBalance);
void
creditMPT(
AccountID const& sender,
AccountID const& receiver,
STAmount const& amount,
std::uint64_t preCreditBalanceHolder,
std::int64_t preCreditBalanceIssuer);
void
issuerSelfDebitMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance);
void
ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next);
@@ -111,11 +52,21 @@ public:
apply(DeferredCredits& to);
private:
static KeyIOU
makeKeyIOU(AccountID const& a1, AccountID const& a2, Currency const& currency);
// lowAccount, highAccount
using Key = std::tuple<AccountID, AccountID, Currency>;
struct Value
{
explicit Value() = default;
std::map<KeyIOU, ValueIOU> creditsIOU_;
std::map<MPTID, IssuerValueMPT> creditsMPT_;
STAmount lowAcctCredits;
STAmount highAcctCredits;
STAmount lowAcctOrigBalance;
};
static Key
makeKey(AccountID const& a1, AccountID const& a2, Currency const& c);
std::map<Key, Value> credits_;
std::map<AccountID, std::uint32_t> ownerCounts_;
};
@@ -180,35 +131,16 @@ public:
/** @} */
STAmount
balanceHookIOU(AccountID const& account, AccountID const& issuer, STAmount const& amount)
balanceHook(AccountID const& account, AccountID const& issuer, STAmount const& amount)
const override;
STAmount
balanceHookMPT(AccountID const& account, MPTIssue const& issue, std::int64_t amount)
const override;
STAmount
balanceHookSelfIssueMPT(MPTIssue const& issue, std::int64_t amount) const override;
void
creditHookIOU(
creditHook(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
STAmount const& preCreditBalance) override;
void
creditHookMPT(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
std::uint64_t preCreditBalanceHolder,
std::int64_t preCreditBalanceIssuer) override;
void
issuerSelfDebitHookMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance)
override;
void
adjustOwnerCountHook(AccountID const& account, std::uint32_t cur, std::uint32_t next) override;

View File

@@ -148,35 +148,15 @@ public:
// Accounts in a payment are not allowed to use assets acquired during that
// payment. The PaymentSandbox tracks the debits, credits, and owner count
// changes that accounts make during a payment. `balanceHookIOU` adjusts
// changes that accounts make during a payment. `balanceHook` adjusts
// balances so newly acquired assets are not counted toward the balance.
// This is required to support PaymentSandbox.
virtual STAmount
balanceHookIOU(AccountID const& account, AccountID const& issuer, STAmount const& amount) const
balanceHook(AccountID const& account, AccountID const& issuer, STAmount const& amount) const
{
XRPL_ASSERT(amount.holds<Issue>(), "balanceHookIOU: amount is for Issue");
return amount;
}
// balanceHookMPT adjusts balances so newly acquired assets are not counted
// toward the balance.
virtual STAmount
balanceHookMPT(AccountID const& account, MPTIssue const& issue, std::int64_t amount) const
{
return STAmount{issue, amount};
}
// An offer owned by an issuer and selling MPT is limited by the issuer's
// funds available to issue, which are originally available funds less
// already self sold MPT amounts (MPT sell offer). This hook is used
// by issuerFundsToSelfIssue() function.
virtual STAmount
balanceHookSelfIssueMPT(MPTIssue const& issue, std::int64_t amount) const
{
return STAmount{issue, amount};
}
// Accounts in a payment are not allowed to use assets acquired during that
// payment. The PaymentSandbox tracks the debits, credits, and owner count
// changes that accounts make during a payment. `ownerCountHook` adjusts the

View File

@@ -63,8 +63,8 @@ isVaultPseudoAccountFrozen(
isLPTokenFrozen(
ReadView const& view,
AccountID const& account,
Asset const& asset,
Asset const& asset2);
Issue const& asset,
Issue const& asset2);
// Return the list of enabled amendments
[[nodiscard]] std::set<uint256>

View File

@@ -1,13 +1,8 @@
#pragma once
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Log.h>
#include <xrpl/basics/Number.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/Sandbox.h>
#include <xrpl/ledger/helpers/RippleStateHelpers.h>
#include <xrpl/ledger/helpers/TokenHelpers.h>
#include <xrpl/protocol/AMMCore.h>
#include <xrpl/protocol/AmountConversions.h>
#include <xrpl/protocol/Feature.h>
@@ -16,7 +11,6 @@
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/Rules.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/STLedgerEntry.h>
namespace xrpl {
@@ -42,7 +36,7 @@ enum class IsDeposit : bool { No = false, Yes = true };
* @return LP Tokens as IOU
*/
STAmount
ammLPTokens(STAmount const& asset1, STAmount const& asset2, Asset const& lptIssue);
ammLPTokens(STAmount const& asset1, STAmount const& asset2, Issue const& lptIssue);
/** Calculate LP Tokens given asset's deposit amount.
* @param asset1Balance current AMM asset1 balance
@@ -130,8 +124,7 @@ withinRelativeDistance(Quality const& calcQuality, Quality const& reqQuality, Nu
template <typename Amt>
requires(
std::is_same_v<Amt, STAmount> || std::is_same_v<Amt, IOUAmount> ||
std::is_same_v<Amt, XRPAmount> || std::is_same_v<Amt, MPTAmount> ||
std::is_same_v<Amt, Number>)
std::is_same_v<Amt, XRPAmount> || std::is_same_v<Amt, Number>)
bool
withinRelativeDistance(Amt const& calc, Amt const& req, Number const& dist)
{
@@ -202,7 +195,7 @@ getAMMOfferStartWithTakerGets(
// Round downward to minimize the offer and to maximize the quality.
// This has the most impact when takerGets is XRP.
auto const takerGets =
toAmount<TOut>(getAsset(pool.out), nTakerGetsProposed, Number::downward);
toAmount<TOut>(getIssue(pool.out), nTakerGetsProposed, Number::downward);
return TAmounts<TIn, TOut>{swapAssetOut(pool, takerGets, tfee), takerGets};
};
@@ -269,7 +262,7 @@ getAMMOfferStartWithTakerPays(
// Round downward to minimize the offer and to maximize the quality.
// This has the most impact when takerPays is XRP.
auto const takerPays =
toAmount<TIn>(getAsset(pool.in), nTakerPaysProposed, Number::downward);
toAmount<TIn>(getIssue(pool.in), nTakerPaysProposed, Number::downward);
return TAmounts<TIn, TOut>{takerPays, swapAssetIn(pool, takerPays, tfee)};
};
@@ -338,7 +331,7 @@ changeSpotPriceQuality(
<< " " << to_string(pool.out) << " " << quality << " " << tfee;
return std::nullopt;
}
auto const takerPays = toAmount<TIn>(getAsset(pool.in), nTakerPays, Number::upward);
auto const takerPays = toAmount<TIn>(getIssue(pool.in), nTakerPays, Number::upward);
// should not fail
if (auto amounts = TAmounts<TIn, TOut>{takerPays, swapAssetIn(pool, takerPays, tfee)};
Quality{amounts} < quality &&
@@ -367,7 +360,7 @@ changeSpotPriceQuality(
// Generate the offer starting with XRP side. Return seated offer amounts
// if the offer can be generated, otherwise nullopt.
auto amounts = [&]() {
if (isXRP(getAsset(pool.out)))
if (isXRP(getIssue(pool.out)))
return getAMMOfferStartWithTakerGets(pool, quality, tfee);
return getAMMOfferStartWithTakerPays(pool, quality, tfee);
}();
@@ -452,7 +445,7 @@ swapAssetIn(TAmounts<TIn, TOut> const& pool, TIn const& assetIn, std::uint16_t t
auto const denom = pool.in + assetIn * (1 - fee);
if (denom.signum() <= 0)
return toAmount<TOut>(getAsset(pool.out), 0);
return toAmount<TOut>(getIssue(pool.out), 0);
Number::setround(Number::upward);
auto const ratio = numerator / denom;
@@ -461,14 +454,14 @@ swapAssetIn(TAmounts<TIn, TOut> const& pool, TIn const& assetIn, std::uint16_t t
auto const swapOut = pool.out - ratio;
if (swapOut.signum() < 0)
return toAmount<TOut>(getAsset(pool.out), 0);
return toAmount<TOut>(getIssue(pool.out), 0);
return toAmount<TOut>(getAsset(pool.out), swapOut, Number::downward);
return toAmount<TOut>(getIssue(pool.out), swapOut, Number::downward);
}
else
{
return toAmount<TOut>(
getAsset(pool.out),
getIssue(pool.out),
pool.out - (pool.in * pool.out) / (pool.in + assetIn * feeMult(tfee)),
Number::downward);
}
@@ -515,7 +508,7 @@ swapAssetOut(TAmounts<TIn, TOut> const& pool, TOut const& assetOut, std::uint16_
auto const denom = pool.out - assetOut;
if (denom.signum() <= 0)
{
return toMaxAmount<TIn>(getAsset(pool.in));
return toMaxAmount<TIn>(getIssue(pool.in));
}
Number::setround(Number::upward);
@@ -529,14 +522,14 @@ swapAssetOut(TAmounts<TIn, TOut> const& pool, TOut const& assetOut, std::uint16_
Number::setround(Number::upward);
auto const swapIn = numerator2 / feeMult;
if (swapIn.signum() < 0)
return toAmount<TIn>(getAsset(pool.in), 0);
return toAmount<TIn>(getIssue(pool.in), 0);
return toAmount<TIn>(getAsset(pool.in), swapIn, Number::upward);
return toAmount<TIn>(getIssue(pool.in), swapIn, Number::upward);
}
else
{
return toAmount<TIn>(
getAsset(pool.in),
getIssue(pool.in),
((pool.in * pool.out) / (pool.out - assetOut) - pool.in) / feeMult(tfee),
Number::upward);
}
@@ -623,9 +616,9 @@ getRoundedAsset(Rules const& rules, STAmount const& balance, A const& frac, IsDe
if (!rules.enabled(fixAMMv1_3))
{
if constexpr (std::is_same_v<A, STAmount>)
return multiply(balance, frac, balance.asset());
return multiply(balance, frac, balance.issue());
else
return toSTAmount(balance.asset(), balance * frac);
return toSTAmount(balance.issue(), balance * frac);
}
auto const rm = detail::getAssetRounding(isDeposit);
return multiply(balance, frac, rm);
@@ -719,94 +712,4 @@ adjustFracByTokens(
STAmount const& tokens,
Number const& frac);
/** Get AMM pool balances.
*/
std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Asset const& asset1,
Asset const& asset2,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal const j);
/** Get AMM pool and LP token balances. If both optIssue are
* provided then they are used as the AMM token pair issues.
* Otherwise the missing issues are fetched from ammSle.
*/
Expected<std::tuple<STAmount, STAmount, STAmount>, TER>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Asset> const& optAsset1,
std::optional<Asset> const& optAsset2,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal const j);
/** Get the balance of LP tokens.
*/
STAmount
ammLPHolds(
ReadView const& view,
Asset const& asset1,
Asset const& asset2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j);
STAmount
ammLPHolds(
ReadView const& view,
SLE const& ammSle,
AccountID const& lpAccount,
beast::Journal const j);
/** Get AMM trading fee for the given account. The fee is discounted
* if the account is the auction slot owner or one of the slot's authorized
* accounts.
*/
std::uint16_t
getTradingFee(ReadView const& view, SLE const& ammSle, AccountID const& account);
/** Returns total amount held by AMM for the given token.
*/
STAmount
ammAccountHolds(ReadView const& view, AccountID const& ammAccountID, Asset const& asset);
/** Delete trustlines to AMM. If all trustlines are deleted then
* AMM object and account are deleted. Otherwise tecINCOMPLETE is returned.
*/
TER
deleteAMMAccount(Sandbox& view, Asset const& asset, Asset const& asset2, beast::Journal j);
/** Initialize Auction and Voting slots and set the trading/discounted fee.
*/
void
initializeFeeAuctionVote(
ApplyView& view,
std::shared_ptr<SLE>& ammSle,
AccountID const& account,
Asset const& lptAsset,
std::uint16_t tfee);
/** Return true if the Liquidity Provider is the only AMM provider, false
* otherwise. Return tecINTERNAL if encountered an unexpected condition,
* for instance Liquidity Provider has more than one LPToken trustline.
*/
Expected<bool, TER>
isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount);
/** Due to rounding, the LPTokenBalance of the last LP might
* not match the LP's trustline balance. If it's within the tolerance,
* update LPTokenBalance to match the LP's trustline balance.
*/
Expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox& sb,
STAmount const& lpTokens,
std::shared_ptr<SLE>& ammSle,
AccountID const& account);
} // namespace xrpl

View File

@@ -0,0 +1,106 @@
#pragma once
#include <xrpl/basics/Expected.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/ledger/View.h>
#include <xrpl/ledger/helpers/RippleStateHelpers.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <xrpl/protocol/TER.h>
namespace xrpl {
class ReadView;
class ApplyView;
class Sandbox;
class NetClock;
/** Get AMM pool balances.
*/
std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Issue const& issue1,
Issue const& issue2,
FreezeHandling freezeHandling,
beast::Journal const j);
/** Get AMM pool and LP token balances. If both optIssue are
* provided then they are used as the AMM token pair issues.
* Otherwise the missing issues are fetched from ammSle.
*/
Expected<std::tuple<STAmount, STAmount, STAmount>, TER>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Issue> const& optIssue1,
std::optional<Issue> const& optIssue2,
FreezeHandling freezeHandling,
beast::Journal const j);
/** Get the balance of LP tokens.
*/
STAmount
ammLPHolds(
ReadView const& view,
Currency const& cur1,
Currency const& cur2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j);
STAmount
ammLPHolds(
ReadView const& view,
SLE const& ammSle,
AccountID const& lpAccount,
beast::Journal const j);
/** Get AMM trading fee for the given account. The fee is discounted
* if the account is the auction slot owner or one of the slot's authorized
* accounts.
*/
std::uint16_t
getTradingFee(ReadView const& view, SLE const& ammSle, AccountID const& account);
/** Returns total amount held by AMM for the given token.
*/
STAmount
ammAccountHolds(ReadView const& view, AccountID const& ammAccountID, Issue const& issue);
/** Delete trustlines to AMM. If all trustlines are deleted then
* AMM object and account are deleted. Otherwise tecIMPCOMPLETE is returned.
*/
TER
deleteAMMAccount(Sandbox& view, Issue const& asset, Issue const& asset2, beast::Journal j);
/** Initialize Auction and Voting slots and set the trading/discounted fee.
*/
void
initializeFeeAuctionVote(
ApplyView& view,
std::shared_ptr<SLE>& ammSle,
AccountID const& account,
Issue const& lptIssue,
std::uint16_t tfee);
/** Return true if the Liquidity Provider is the only AMM provider, false
* otherwise. Return tecINTERNAL if encountered an unexpected condition,
* for instance Liquidity Provider has more than one LPToken trustline.
*/
Expected<bool, TER>
isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount);
/** Due to rounding, the LPTokenBalance of the last LP might
* not match the LP's trustline balance. If it's within the tolerance,
* update LPTokenBalance to match the LP's trustline balance.
*/
Expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox& sb,
STAmount const& lpTokens,
std::shared_ptr<SLE>& ammSle,
AccountID const& account);
} // namespace xrpl

View File

@@ -41,8 +41,7 @@ escrowUnlockApplyHelper<Issue>(
bool createAsset,
beast::Journal journal)
{
Issue const& issue = amount.get<Issue>();
Keylet const trustLineKey = keylet::line(receiver, issue);
Keylet const trustLineKey = keylet::line(receiver, amount.issue());
bool const recvLow = issuer > receiver;
bool const senderIssuer = issuer == sender;
bool const receiverIssuer = issuer == receiver;
@@ -65,9 +64,9 @@ escrowUnlockApplyHelper<Issue>(
return tecNO_LINE_INSUF_RESERVE;
}
Currency const currency = issue.currency;
STAmount initialBalance(issue);
initialBalance.get<Issue>().account = noAccount();
Currency const currency = amount.getCurrency();
STAmount initialBalance(amount.issue());
initialBalance.setIssuer(noAccount());
if (TER const ter = trustCreate(
view, // payment sandbox
@@ -114,8 +113,7 @@ escrowUnlockApplyHelper<Issue>(
if ((!senderIssuer && !receiverIssuer) && lockedRate != parityRate)
{
// compute transfer fee, if any
auto const xferFee =
amount.value() - divideRound(amount, lockedRate, amount.get<Issue>(), true);
auto const xferFee = amount.value() - divideRound(amount, lockedRate, amount.issue(), true);
// compute balance to transfer
finalAmt = amount.value() - xferFee;
}

View File

@@ -80,7 +80,6 @@ authorizeMPToken(
* requireAuth check is recursive for MPT shares in a vault, descending to
* assets in the vault, up to maxAssetCheckDepth recursion depth. This is
* purely defensive, as we currently do not allow such vaults to be created.
* WeakAuth intentionally allows missing MPTokens under MPToken V2.
*/
[[nodiscard]] TER
requireAuth(
@@ -115,12 +114,6 @@ canTransfer(
AccountID const& from,
AccountID const& to);
/** Check if Asset can be traded on DEX. return tecNO_PERMISSION
* if it doesn't and tesSUCCESS otherwise.
*/
[[nodiscard]] TER
canTrade(ReadView const& view, Asset const& asset);
//------------------------------------------------------------------------------
//
// Empty holding operations (MPT-specific)
@@ -171,73 +164,4 @@ createMPToken(
AccountID const& account,
std::uint32_t const flags);
TER
checkCreateMPT(
xrpl::ApplyView& view,
xrpl::MPTIssue const& mptIssue,
xrpl::AccountID const& holder,
beast::Journal j);
//------------------------------------------------------------------------------
//
// MPT Overflow related
//
//------------------------------------------------------------------------------
// MaximumAmount doesn't exceed 2**63-1
std::int64_t
maxMPTAmount(SLE const& sleIssuance);
// OutstandingAmount may overflow and available amount might be negative.
// But available amount is always <= |MaximumAmount - OutstandingAmount|.
std::int64_t
availableMPTAmount(SLE const& sleIssuance);
std::int64_t
availableMPTAmount(ReadView const& view, MPTID const& mptID);
/** Checks for two types of OutstandingAmount overflow during a send operation.
* 1. **Direct directSendNoFee (Overflow: No):** A true overflow check when
* `OutstandingAmount > MaximumAmount`. This threshold is used for direct
* directSendNoFee transactions that bypass the payment engine.
* 2. **accountSend & Payment Engine (Overflow: Yes):** A temporary overflow
* check when `OutstandingAmount > UINT64_MAX`. This higher threshold is used
* for `accountSend` and payments processed via the payment engine.
*/
bool
isMPTOverflow(
std::int64_t sendAmount,
std::uint64_t outstandingAmount,
std::int64_t maximumAmount,
AllowMPTOverflow allowOverflow);
/**
* Determine funds available for an issuer to sell in an issuer owned offer.
* Issuing step, which could be either MPTEndPointStep last step or BookStep's
* TakerPays may overflow OutstandingAmount. Redeeming step, in BookStep's
* TakerGets redeems the offer's owner funds, essentially balancing out
* the overflow, unless the offer's owner is the issuer.
*/
[[nodiscard]] STAmount
issuerFundsToSelfIssue(ReadView const& view, MPTIssue const& issue);
/** Facilitate tracking of MPT sold by an issuer owning MPT sell offer.
* See ApplyView::issuerSelfDebitHookMPT().
*/
void
issuerSelfDebitHookMPT(ApplyView& view, MPTIssue const& issue, std::uint64_t amount);
//------------------------------------------------------------------------------
//
// MPT DEX
//
//------------------------------------------------------------------------------
/* Return true if a transaction is allowed for the specified MPT/account. The
* function checks MPTokenIssuance and MPToken objects flags to determine if the
* transaction is allowed.
*/
TER
checkMPTTxAllowed(ReadView const& v, TxType tx, Asset const& asset, AccountID const& accountID);
} // namespace xrpl

View File

@@ -252,14 +252,4 @@ deleteAMMTrustLine(
std::optional<AccountID> const& ammAccountID,
beast::Journal j);
/** Delete AMMs MPToken. The passed `sle` must be obtained from a prior
* call to view.peek().
*/
[[nodiscard]] TER
deleteAMMMPToken(
ApplyView& view,
std::shared_ptr<SLE> sleMPT,
AccountID const& ammAccountID,
beast::Journal j);
} // namespace xrpl

View File

@@ -31,9 +31,6 @@ enum SpendableHandling { shSIMPLE_BALANCE, shFULL_BALANCE };
enum class WaiveTransferFee : bool { No = false, Yes };
/** Controls whether accountSend is allowed to overflow OutstandingAmount **/
enum class AllowMPTOverflow : bool { No = false, Yes };
/* Check if MPToken (for MPT) or trust line (for IOU) exists:
* - StrongAuth - before checking if authorization is required
* - WeakAuth
@@ -179,16 +176,6 @@ accountFunds(
FreezeHandling freezeHandling,
beast::Journal j);
// Overload with AuthHandling to support IOU and MPT.
[[nodiscard]] STAmount
accountFunds(
ReadView const& view,
AccountID const& id,
STAmount const& saDefault,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal j);
/** Returns the transfer fee as Rate based on the type of token
* @param view The ledger view
* @param amount The amount to transfer
@@ -270,8 +257,7 @@ accountSend(
AccountID const& to,
STAmount const& saAmount,
beast::Journal j,
WaiveTransferFee waiveFee = WaiveTransferFee::No,
AllowMPTOverflow allowOverflow = AllowMPTOverflow::No);
WaiveTransferFee waiveFee = WaiveTransferFee::No);
using MultiplePaymentDestinations = std::vector<std::pair<AccountID, Number>>;
/** Like accountSend, except one account is sending multiple payments (with the

View File

@@ -134,7 +134,7 @@ public:
{
lowest_layer().shutdown(plain_socket::shutdown_both);
}
catch (boost::system::system_error& e)
catch (boost::system::system_error const& e)
{
ec = e.code();
}

View File

@@ -2,7 +2,7 @@
#include <xrpl/basics/Number.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/TER.h>
#include <xrpl/protocol/UintTypes.h>
@@ -31,12 +31,12 @@ class Rules;
/** Calculate Liquidity Provider Token (LPT) Currency.
*/
Currency
ammLPTCurrency(Asset const& asset1, Asset const& asset2);
ammLPTCurrency(Currency const& cur1, Currency const& cur2);
/** Calculate LPT Issue from AMM asset pair.
*/
Issue
ammLPTIssue(Asset const& asset1, Asset const& asset2, AccountID const& ammAccountID);
ammLPTIssue(Currency const& cur1, Currency const& cur2, AccountID const& ammAccountID);
/** Validate the amount.
* If validZero is false and amount is beast::zero then invalid amount.
@@ -46,19 +46,19 @@ ammLPTIssue(Asset const& asset1, Asset const& asset2, AccountID const& ammAccoun
NotTEC
invalidAMMAmount(
STAmount const& amount,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt,
bool validZero = false);
NotTEC
invalidAMMAsset(
Asset const& asset,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt);
Issue const& issue,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt);
NotTEC
invalidAMMAssetPair(
Asset const& asset1,
Asset const& asset2,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt);
Issue const& issue1,
Issue const& issue2,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt);
/** Get time slot of the auction slot.
*/

View File

@@ -1,7 +1,6 @@
#pragma once
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/XRPAmount.h>
@@ -10,12 +9,11 @@
namespace xrpl {
inline STAmount
toSTAmount(IOUAmount const& iou, Asset const& asset)
toSTAmount(IOUAmount const& iou, Issue const& iss)
{
XRPL_ASSERT(asset.holds<Issue>(), "xrpl::toSTAmount : is Issue");
bool const isNeg = iou.signum() < 0;
std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
return STAmount(asset, umant, iou.exponent(), isNeg, STAmount::unchecked());
return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
}
inline STAmount
@@ -33,25 +31,12 @@ toSTAmount(XRPAmount const& xrp)
}
inline STAmount
toSTAmount(XRPAmount const& xrp, Asset const& asset)
toSTAmount(XRPAmount const& xrp, Issue const& iss)
{
XRPL_ASSERT(isXRP(asset), "xrpl::toSTAmount : is XRP");
XRPL_ASSERT(isXRP(iss.account) && isXRP(iss.currency), "xrpl::toSTAmount : is XRP");
return toSTAmount(xrp);
}
inline STAmount
toSTAmount(MPTAmount const& mpt)
{
return STAmount(mpt, noMPT());
}
inline STAmount
toSTAmount(MPTAmount const& mpt, Asset const& asset)
{
XRPL_ASSERT(asset.holds<MPTIssue>(), "xrpl::toSTAmount : is MPT");
return STAmount(mpt, asset.get<MPTIssue>());
}
template <class T>
T
toAmount(STAmount const& amt) = delete;
@@ -91,21 +76,6 @@ toAmount<XRPAmount>(STAmount const& amt)
return XRPAmount(sMant);
}
template <>
inline MPTAmount
toAmount<MPTAmount>(STAmount const& amt)
{
XRPL_ASSERT(
amt.holds<MPTIssue>() && amt.mantissa() <= maxMPTokenAmount && amt.exponent() == 0,
"xrpl::toAmount<MPTAmount> : maximum mantissa");
if (amt.mantissa() > maxMPTokenAmount || amt.exponent() != 0)
Throw<std::runtime_error>("toAmount<MPTAmount>: invalid mantissa or exponent");
bool const isNeg = amt.negative();
std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
return MPTAmount(sMant);
}
template <class T>
T
toAmount(IOUAmount const& amt) = delete;
@@ -128,36 +98,23 @@ toAmount<XRPAmount>(XRPAmount const& amt)
return amt;
}
template <class T>
T
toAmount(MPTAmount const& amt) = delete;
template <>
inline MPTAmount
toAmount<MPTAmount>(MPTAmount const& amt)
{
return amt;
}
template <typename T>
T
toAmount(Asset const& asset, Number const& n, Number::rounding_mode mode = Number::getround())
toAmount(Issue const& issue, Number const& n, Number::rounding_mode mode = Number::getround())
{
saveNumberRoundMode const rm(Number::getround());
if (isXRP(asset))
if (isXRP(issue))
Number::setround(mode);
if constexpr (std::is_same_v<IOUAmount, T>)
return IOUAmount(n);
else if constexpr (std::is_same_v<XRPAmount, T>)
return XRPAmount(static_cast<std::int64_t>(n));
else if constexpr (std::is_same_v<MPTAmount, T>)
return MPTAmount(static_cast<std::int64_t>(n));
else if constexpr (std::is_same_v<STAmount, T>)
{
if (isXRP(asset))
return STAmount(asset, static_cast<std::int64_t>(n));
return STAmount(asset, n);
if (isXRP(issue))
return STAmount(issue, static_cast<std::int64_t>(n));
return STAmount(issue, n);
}
else
{
@@ -168,23 +125,17 @@ toAmount(Asset const& asset, Number const& n, Number::rounding_mode mode = Numbe
template <typename T>
T
toMaxAmount(Asset const& asset)
toMaxAmount(Issue const& issue)
{
if constexpr (std::is_same_v<IOUAmount, T>)
return IOUAmount(STAmount::cMaxValue, STAmount::cMaxOffset);
else if constexpr (std::is_same_v<XRPAmount, T>)
return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
else if constexpr (std::is_same_v<MPTAmount, T>)
return MPTAmount(maxMPTokenAmount);
else if constexpr (std::is_same_v<STAmount, T>)
{
return asset.visit(
[](Issue const& issue) {
if (isXRP(issue))
return STAmount(issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
return STAmount(issue, STAmount::cMaxValue, STAmount::cMaxOffset);
},
[](MPTIssue const& issue) { return STAmount(issue, maxMPTokenAmount); });
if (isXRP(issue))
return STAmount(issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
return STAmount(issue, STAmount::cMaxValue, STAmount::cMaxOffset);
}
else
{
@@ -194,23 +145,21 @@ toMaxAmount(Asset const& asset)
}
inline STAmount
toSTAmount(Asset const& asset, Number const& n, Number::rounding_mode mode = Number::getround())
toSTAmount(Issue const& issue, Number const& n, Number::rounding_mode mode = Number::getround())
{
return toAmount<STAmount>(asset, n, mode);
return toAmount<STAmount>(issue, n, mode);
}
template <typename T>
Asset
getAsset(T const& amt)
Issue
getIssue(T const& amt)
{
if constexpr (std::is_same_v<IOUAmount, T>)
return noIssue();
else if constexpr (std::is_same_v<XRPAmount, T>)
return xrpIssue();
else if constexpr (std::is_same_v<MPTAmount, T>)
return noMPT();
else if constexpr (std::is_same_v<STAmount, T>)
return amt.asset();
return amt.issue();
else
{
constexpr bool alwaysFalse = !std::is_same_v<T, T>;
@@ -226,8 +175,6 @@ get(STAmount const& a)
return a.iou();
else if constexpr (std::is_same_v<XRPAmount, T>)
return a.xrp();
else if constexpr (std::is_same_v<MPTAmount, T>)
return a.mpt();
else if constexpr (std::is_same_v<STAmount, T>)
return a;
else

View File

@@ -2,37 +2,20 @@
#include <xrpl/basics/Number.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/Rules.h>
namespace xrpl {
class Asset;
class STAmount;
template <typename T>
requires(
std::is_same_v<T, XRPAmount> || std::is_same_v<T, IOUAmount> ||
std::is_same_v<T, MPTAmount>)
struct AmountType
{
using amount_type = T;
};
template <typename TIss>
concept ValidIssueType = std::is_same_v<TIss, Issue> || std::is_same_v<TIss, MPTIssue>;
/* Used to check for an asset with either badCurrency()
* or MPT with 0 account.
*/
struct BadAsset
{
};
inline BadAsset const&
badAsset()
{
static BadAsset const a;
return a;
}
template <typename A>
concept AssetType = std::is_convertible_v<A, Asset> || std::is_convertible_v<A, Issue> ||
std::is_convertible_v<A, MPTIssue> || std::is_convertible_v<A, MPTID>;
/* Asset is an abstraction of three different issue types: XRP, IOU, MPT.
* For historical reasons, two issue types XRP and IOU are wrapped in Issue
@@ -43,9 +26,6 @@ class Asset
{
public:
using value_type = std::variant<Issue, MPTIssue>;
using token_type = std::variant<Currency, MPTID>;
using AmtType =
std::variant<AmountType<XRPAmount>, AmountType<IOUAmount>, AmountType<MPTAmount>>;
private:
value_type issue_;
@@ -89,42 +69,36 @@ public:
constexpr value_type const&
value() const;
constexpr token_type
token() const;
void
setJson(Json::Value& jv) const;
STAmount
operator()(Number const&) const;
constexpr AmtType
getAmountType() const;
// Custom, generic visit implementation
template <typename... Visitors>
constexpr auto
visit(Visitors&&... visitors) const -> decltype(auto)
{
// Simple delegation to the reusable utility, passing the internal
// variant data.
return detail::visit(issue_, std::forward<Visitors>(visitors)...);
}
constexpr bool
bool
native() const
{
return visit(
[&](Issue const& issue) { return issue.native(); },
[&](MPTIssue const&) { return false; });
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
return issue.native();
if constexpr (std::is_same_v<TIss, MPTIssue>)
return false;
},
issue_);
}
bool
integral() const
{
return visit(
[&](Issue const& issue) { return issue.native(); },
[&](MPTIssue const&) { return true; });
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
return issue.native();
if constexpr (std::is_same_v<TIss, MPTIssue>)
return true;
},
issue_);
}
friend constexpr bool
@@ -136,10 +110,6 @@ public:
friend constexpr bool
operator==(Currency const& lhs, Asset const& rhs);
// rhs is either badCurrency() or MPT issuer is 0
friend constexpr bool
operator==(BadAsset const& lhs, Asset const& rhs);
/** Return true if both assets refer to the same currency (regardless of
* issuer) or MPT issuance. Otherwise return false.
*/
@@ -147,12 +117,6 @@ public:
equalTokens(Asset const& lhs, Asset const& rhs);
};
template <ValidIssueType TIss>
constexpr bool is_issue_v = std::is_same_v<TIss, Issue>;
template <ValidIssueType TIss>
constexpr bool is_mptissue_v = std::is_same_v<TIss, MPTIssue>;
inline Json::Value
to_json(Asset const& asset)
{
@@ -192,29 +156,6 @@ Asset::value() const
return issue_;
}
constexpr Asset::token_type
Asset::token() const
{
return visit(
[&](Issue const& issue) -> Asset::token_type { return issue.currency; },
[&](MPTIssue const& issue) -> Asset::token_type { return issue.getMptID(); });
}
constexpr Asset::AmtType
Asset::getAmountType() const
{
return visit(
[&](Issue const& issue) -> Asset::AmtType {
constexpr AmountType<XRPAmount> xrp;
constexpr AmountType<IOUAmount> iou;
return native() ? AmtType(xrp) : AmtType(iou);
},
[&](MPTIssue const& issue) -> Asset::AmtType {
constexpr AmountType<MPTAmount> mpt;
return AmtType(mpt);
});
}
constexpr bool
operator==(Asset const& lhs, Asset const& rhs)
{
@@ -236,7 +177,7 @@ operator<=>(Asset const& lhs, Asset const& rhs)
[]<ValidIssueType TLhs, ValidIssueType TRhs>(TLhs const& lhs_, TRhs const& rhs_) {
if constexpr (std::is_same_v<TLhs, TRhs>)
return std::weak_ordering(lhs_ <=> rhs_);
else if constexpr (is_issue_v<TLhs> && is_mptissue_v<TRhs>)
else if constexpr (std::is_same_v<TLhs, Issue> && std::is_same_v<TRhs, MPTIssue>)
return std::weak_ordering::greater;
else
return std::weak_ordering::less;
@@ -248,17 +189,7 @@ operator<=>(Asset const& lhs, Asset const& rhs)
constexpr bool
operator==(Currency const& lhs, Asset const& rhs)
{
return rhs.visit(
[&](Issue const& issue) { return issue.currency == lhs; },
[](MPTIssue const& issue) { return false; });
}
constexpr bool
operator==(BadAsset const&, Asset const& rhs)
{
return rhs.visit(
[](Issue const& issue) -> bool { return badCurrency() == issue.currency; },
[](MPTIssue const& issue) -> bool { return issue.getIssuer() == xrpAccount(); });
return rhs.holds<Issue>() && rhs.get<Issue>().currency == lhs;
}
constexpr bool
@@ -292,36 +223,4 @@ validJSONAsset(Json::Value const& jv);
Asset
assetFromJson(Json::Value const& jv);
Json::Value
to_json(Asset const& asset);
inline bool
isConsistent(Asset const& asset)
{
return asset.visit(
[](Issue const& issue) { return isConsistent(issue); },
[](MPTIssue const&) { return true; });
}
inline bool
validAsset(Asset const& asset)
{
return asset.visit(
[](Issue const& issue) { return isConsistent(issue) && issue.currency != badCurrency(); },
[](MPTIssue const& issue) { return issue.getIssuer() != xrpAccount(); });
}
template <class Hasher>
void
hash_append(Hasher& h, Asset const& r)
{
using beast::hash_append;
r.visit(
[&](Issue const& issue) { hash_append(h, issue); },
[&](MPTIssue const& issue) { hash_append(h, issue); });
}
std::ostream&
operator<<(std::ostream& os, Asset const& x);
} // namespace xrpl

View File

@@ -2,7 +2,7 @@
#include <xrpl/basics/CountedObject.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Issue.h>
#include <boost/utility/base_from_member.hpp>
@@ -15,15 +15,15 @@ namespace xrpl {
class Book final : public CountedObject<Book>
{
public:
Asset in;
Asset out;
Issue in;
Issue out;
std::optional<uint256> domain;
Book()
{
}
Book(Asset const& in_, Asset const& out_, std::optional<uint256> const& domain_)
Book(Issue const& in_, Issue const& out_, std::optional<uint256> const& domain_)
: in(in_), out(out_), domain(domain_)
{
}
@@ -112,67 +112,16 @@ public:
}
};
template <>
struct hash<xrpl::MPTIssue> : private boost::base_from_member<std::hash<xrpl::MPTID>, 0>
{
private:
using id_hash_type = boost::base_from_member<std::hash<xrpl::MPTID>, 0>;
public:
explicit hash() = default;
using value_type = std::size_t;
using argument_type = xrpl::MPTIssue;
value_type
operator()(argument_type const& value) const
{
value_type const result(id_hash_type::member(value.getMptID()));
return result;
}
};
template <>
struct hash<xrpl::Asset>
{
private:
using value_type = std::size_t;
using argument_type = xrpl::Asset;
using issue_hasher = std::hash<xrpl::Issue>;
using mptissue_hasher = std::hash<xrpl::MPTIssue>;
issue_hasher m_issue_hasher;
mptissue_hasher m_mptissue_hasher;
public:
explicit hash() = default;
value_type
operator()(argument_type const& asset) const
{
return asset.visit(
[&](xrpl::Issue const& issue) {
value_type const result(m_issue_hasher(issue));
return result;
},
[&](xrpl::MPTIssue const& issue) {
value_type const result(m_mptissue_hasher(issue));
return result;
});
}
};
//------------------------------------------------------------------------------
template <>
struct hash<xrpl::Book>
{
private:
using asset_hasher = std::hash<xrpl::Asset>;
using issue_hasher = std::hash<xrpl::Issue>;
using uint256_hasher = xrpl::uint256::hasher;
asset_hasher m_asset_hasher;
issue_hasher m_issue_hasher;
uint256_hasher m_uint256_hasher;
public:
@@ -184,8 +133,8 @@ public:
value_type
operator()(argument_type const& value) const
{
value_type result(m_asset_hasher(value.in));
boost::hash_combine(result, m_asset_hasher(value.out));
value_type result(m_issue_hasher(value.in));
boost::hash_combine(result, m_issue_hasher(value.out));
if (value.domain)
boost::hash_combine(result, m_uint256_hasher(*value.domain));
@@ -210,22 +159,6 @@ struct hash<xrpl::Issue> : std::hash<xrpl::Issue>
// using Base::Base; // inherit ctors
};
template <>
struct hash<xrpl::MPTIssue> : std::hash<xrpl::MPTIssue>
{
explicit hash() = default;
using Base = std::hash<xrpl::MPTIssue>;
};
template <>
struct hash<xrpl::Asset> : std::hash<xrpl::Asset>
{
explicit hash() = default;
using Base = std::hash<xrpl::Asset>;
};
template <>
struct hash<xrpl::Book> : std::hash<xrpl::Book>
{

View File

@@ -1,86 +0,0 @@
#pragma once
#include <xrpl/protocol/UintTypes.h>
#include <type_traits>
namespace xrpl {
class STAmount;
class Asset;
class Issue;
class MPTIssue;
class IOUAmount;
class XRPAmount;
class MPTAmount;
template <typename A>
concept StepAmount =
std::is_same_v<A, XRPAmount> || std::is_same_v<A, IOUAmount> || std::is_same_v<A, MPTAmount>;
template <typename TIss>
concept ValidIssueType = std::is_same_v<TIss, Issue> || std::is_same_v<TIss, MPTIssue>;
template <typename A>
concept AssetType = std::is_convertible_v<A, Asset> || std::is_convertible_v<A, Issue> ||
std::is_convertible_v<A, MPTIssue> || std::is_convertible_v<A, MPTID>;
template <typename T>
concept ValidPathAsset = (std::is_same_v<T, Currency> || std::is_same_v<T, MPTID>);
template <class TTakerPays, class TTakerGets>
concept ValidTaker =
((std::is_same_v<TTakerPays, IOUAmount> || std::is_same_v<TTakerPays, XRPAmount> ||
std::is_same_v<TTakerPays, MPTAmount>) &&
(std::is_same_v<TTakerGets, IOUAmount> || std::is_same_v<TTakerGets, XRPAmount> ||
std::is_same_v<TTakerGets, MPTAmount>) &&
(!std::is_same_v<TTakerPays, XRPAmount> || !std::is_same_v<TTakerGets, XRPAmount>));
namespace detail {
// This template combines multiple callable objects (lambdas) into a single
// object that std::visit can use for overload resolution.
template <typename... Ts>
struct CombineVisitors : Ts...
{
// Bring all operator() overloads from base classes into this scope.
// It's the mechanism that makes the CombineVisitors struct function
// as a single callable object with multiple overloads.
using Ts::operator()...;
// Perfect forwarding constructor to correctly initialize the base class
// lambdas
constexpr CombineVisitors(Ts&&... ts) : Ts(std::forward<Ts>(ts))...
{
}
};
// This function forces function template argument deduction, which is more
// robust than class template argument deduction (CTAD) via the deduction guide.
template <typename... Ts>
constexpr CombineVisitors<std::decay_t<Ts>...>
make_combine_visitors(Ts&&... ts)
{
// std::decay_t<Ts> is used to remove references/constness from the lambda
// types before they are passed as template arguments to the CombineVisitors
// struct.
return CombineVisitors<std::decay_t<Ts>...>{std::forward<Ts>(ts)...};
}
// This function takes ANY variant and ANY number of visitors, and performs the
// visit. It is the reusable core logic.
template <typename Variant, typename... Visitors>
constexpr auto
visit(Variant&& v, Visitors&&... visitors) -> decltype(auto)
{
// Use the function template helper instead of raw CTAD.
auto visitor_set = make_combine_visitors(std::forward<Visitors>(visitors)...);
// Delegate to std::visit, perfectly forwarding the variant and the visitor
// set.
return std::visit(visitor_set, std::forward<Variant>(v));
}
} // namespace detail
} // namespace xrpl

View File

@@ -187,8 +187,7 @@ enum LedgerEntryType : std::uint16_t {
\
LEDGER_OBJECT(MPToken, \
LSF_FLAG2(lsfMPTLocked, 0x00000001) \
LSF_FLAG(lsfMPTAuthorized, 0x00000002) \
LSF_FLAG(lsfMPTAMM, 0x00000004)) \
LSF_FLAG(lsfMPTAuthorized, 0x00000002)) \
\
LEDGER_OBJECT(Credential, \
LSF_FLAG(lsfAccepted, 0x00010000)) \

View File

@@ -22,12 +22,11 @@ public:
using value_type = std::int64_t;
protected:
value_type value_{};
value_type value_;
public:
MPTAmount() = default;
constexpr MPTAmount(MPTAmount const& other) = default;
constexpr MPTAmount(beast::Zero);
constexpr MPTAmount&
operator=(MPTAmount const& other) = default;
@@ -86,11 +85,6 @@ constexpr MPTAmount::MPTAmount(value_type value) : value_(value)
{
}
constexpr MPTAmount::MPTAmount(beast::Zero)
{
*this = beast::zero;
}
constexpr MPTAmount&
MPTAmount::operator=(beast::Zero)
{
@@ -122,14 +116,6 @@ MPTAmount::value() const
return value_;
}
// Output MPTAmount as just the value.
template <class Char, class Traits>
std::basic_ostream<Char, Traits>&
operator<<(std::basic_ostream<Char, Traits>& os, MPTAmount const& q)
{
return os << q.value();
}
inline std::string
to_string(MPTAmount const& amount)
{

View File

@@ -17,14 +17,7 @@ private:
public:
MPTIssue() = default;
MPTIssue(MPTID const& issuanceID);
MPTIssue(std::uint32_t sequence, AccountID const& account);
operator MPTID const&() const
{
return mptID_;
}
explicit MPTIssue(MPTID const& issuanceID);
AccountID const&
getIssuer() const;
@@ -80,47 +73,6 @@ isXRP(MPTID const&)
return false;
}
inline AccountID
getMPTIssuer(MPTID const& mptid)
{
static_assert(sizeof(MPTID) == (sizeof(std::uint32_t) + sizeof(AccountID)));
// Extract the 20 bytes for the AccountID
std::array<std::uint8_t, sizeof(AccountID)> bytes{};
std::copy_n(mptid.data() + sizeof(std::uint32_t), sizeof(AccountID), bytes.begin());
// bit_cast is a "magic" compiler intrinsic that is
// usually optimized away to nothing in the final assembly.
return std::bit_cast<AccountID>(bytes);
}
// Disallow temporary
inline AccountID const&
getMPTIssuer(MPTID const&&) = delete;
inline AccountID const&
getMPTIssuer(MPTID&&) = delete;
inline MPTID
noMPT()
{
static MPTIssue const mpt{0, noAccount()};
return mpt.getMptID();
}
inline MPTID
badMPT()
{
static MPTIssue const mpt{0, xrpAccount()};
return mpt.getMptID();
}
template <class Hasher>
void
hash_append(Hasher& h, MPTIssue const& r)
{
using beast::hash_append;
hash_append(h, r.getMptID());
}
Json::Value
to_json(MPTIssue const& mptIssue);
@@ -130,17 +82,4 @@ to_string(MPTIssue const& mptIssue);
MPTIssue
mptIssueFromJson(Json::Value const& jv);
std::ostream&
operator<<(std::ostream& os, MPTIssue const& x);
} // namespace xrpl
namespace std {
template <>
struct hash<xrpl::MPTID> : xrpl::MPTID::hasher
{
explicit hash() = default;
};
} // namespace std

View File

@@ -1,130 +0,0 @@
#pragma once
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Concepts.h>
namespace xrpl {
/* Represent STPathElement's asset, which can be Currency or MPTID.
*/
class PathAsset
{
private:
std::variant<Currency, MPTID> easset_;
public:
PathAsset() = default;
// Enables comparing Asset and PathAsset
PathAsset(Asset const& asset);
PathAsset(Currency const& currency) : easset_(currency)
{
}
PathAsset(MPTID const& mpt) : easset_(mpt)
{
}
template <ValidPathAsset T>
constexpr bool
holds() const;
constexpr bool
isXRP() const;
template <ValidPathAsset T>
T const&
get() const;
constexpr std::variant<Currency, MPTID> const&
value() const;
// Custom, generic visit implementation
template <typename... Visitors>
constexpr auto
visit(Visitors&&... visitors) const -> decltype(auto)
{
// Simple delegation to the reusable utility, passing the internal
// variant data.
return detail::visit(easset_, std::forward<Visitors>(visitors)...);
}
friend constexpr bool
operator==(PathAsset const& lhs, PathAsset const& rhs);
};
template <ValidPathAsset PA>
constexpr bool is_currency_v = std::is_same_v<PA, Currency>;
template <ValidPathAsset PA>
constexpr bool is_mptid_v = std::is_same_v<PA, MPTID>;
inline PathAsset::PathAsset(Asset const& asset)
{
asset.visit(
[&](Issue const& issue) { easset_ = issue.currency; },
[&](MPTIssue const& issue) { easset_ = issue.getMptID(); });
}
template <ValidPathAsset T>
constexpr bool
PathAsset::holds() const
{
return std::holds_alternative<T>(easset_);
}
template <ValidPathAsset T>
T const&
PathAsset::get() const
{
if (!holds<T>())
Throw<std::runtime_error>("PathAsset doesn't hold requested asset.");
return std::get<T>(easset_);
}
constexpr std::variant<Currency, MPTID> const&
PathAsset::value() const
{
return easset_;
}
constexpr bool
PathAsset::isXRP() const
{
return visit(
[&](Currency const& currency) { return xrpl::isXRP(currency); },
[](MPTID const&) { return false; });
}
constexpr bool
operator==(PathAsset const& lhs, PathAsset const& rhs)
{
return std::visit(
[]<ValidPathAsset TLhs, ValidPathAsset TRhs>(TLhs const& lhs_, TRhs const& rhs_) {
if constexpr (std::is_same_v<TLhs, TRhs>)
return lhs_ == rhs_;
else
return false;
},
lhs.value(),
rhs.value());
}
template <typename Hasher>
void
hash_append(Hasher& h, PathAsset const& pathAsset)
{
std::visit([&]<ValidPathAsset T>(T const& e) { hash_append(h, e); }, pathAsset.value());
}
inline bool
isXRP(PathAsset const& asset)
{
return asset.isXRP();
}
std::string
to_string(PathAsset const& asset);
std::ostream&
operator<<(std::ostream& os, PathAsset const& x);
} // namespace xrpl

View File

@@ -232,7 +232,7 @@ std::size_t constexpr maxMPTokenMetadataLength = 1024;
/** The maximum amount of MPTokenIssuance */
std::uint64_t constexpr maxMPTokenAmount = 0x7FFF'FFFF'FFFF'FFFFull;
static_assert(Number::largestMantissa >= maxMPTokenAmount);
static_assert(Number::maxRep >= maxMPTokenAmount);
/** The maximum length of Data payload */
std::size_t constexpr maxDataPayloadLength = 256;

View File

@@ -164,9 +164,12 @@ public:
constexpr TIss const&
get() const;
template <ValidIssueType TIss>
TIss&
get();
Issue const&
issue() const;
// These three are deprecated
Currency const&
getCurrency() const;
AccountID const&
getIssuer() const;
@@ -222,6 +225,9 @@ public:
void
clear(Asset const& asset);
void
setIssuer(AccountID const& uIssuer);
/** Set the Issue for this amount. */
void
setIssue(Asset const& asset);
@@ -460,11 +466,16 @@ STAmount::get() const
return mAsset.get<TIss>();
}
template <ValidIssueType TIss>
TIss&
STAmount::get()
inline Issue const&
STAmount::issue() const
{
return mAsset.get<TIss>();
return get<Issue>();
}
inline Currency const&
STAmount::getCurrency() const
{
return mAsset.get<Issue>().currency;
}
inline AccountID const&
@@ -494,13 +505,11 @@ operator bool() const noexcept
inline STAmount::
operator Number() const
{
return asset().visit(
[&](Issue const& issue) -> Number {
if (issue.native())
return xrp();
return iou();
},
[&](MPTIssue const&) -> Number { return mpt(); });
if (native())
return xrp();
if (mAsset.holds<MPTIssue>())
return mpt();
return iou();
}
inline STAmount&
@@ -530,8 +539,6 @@ 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(cMinValue, cMaxValue);
return STAmount{asset, mantissa, exponent, negative};
@@ -561,6 +568,12 @@ STAmount::clear(Asset const& asset)
clear();
}
inline void
STAmount::setIssuer(AccountID const& uIssuer)
{
mAsset.get<Issue>().account = uIssuer;
}
inline STAmount const&
STAmount::value() const noexcept
{

View File

@@ -349,8 +349,6 @@ public:
void
setFieldH128(SField const& field, uint128 const&);
void
setFieldH192(SField const& field, uint192 const&);
void
setFieldH256(SField const& field, uint256 const&);
void
setFieldI32(SField const& field, std::int32_t);

View File

@@ -3,8 +3,6 @@
#include <xrpl/basics/CountedObject.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/PathAsset.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STBase.h>
#include <xrpl/protocol/UintTypes.h>
@@ -18,7 +16,7 @@ class STPathElement final : public CountedObject<STPathElement>
{
unsigned int mType;
AccountID mAccountID;
PathAsset mAssetID;
Currency mCurrencyID;
AccountID mIssuerID;
bool is_offer_;
@@ -30,10 +28,8 @@ public:
typeAccount = 0x01, // Rippling through an account (vs taking an offer).
typeCurrency = 0x10, // Currency follows.
typeIssuer = 0x20, // Issuer follows.
typeMPT = 0x40, // MPT follows.
typeBoundary = 0xFF, // Boundary between alternate paths.
typeAsset = typeCurrency | typeMPT,
typeAll = typeAccount | typeCurrency | typeIssuer | typeMPT,
typeAll = typeAccount | typeCurrency | typeIssuer,
// Combination of all types.
};
@@ -44,19 +40,19 @@ public:
STPathElement(
std::optional<AccountID> const& account,
std::optional<PathAsset> const& asset,
std::optional<Currency> const& currency,
std::optional<AccountID> const& issuer);
STPathElement(
AccountID const& account,
PathAsset const& asset,
Currency const& currency,
AccountID const& issuer,
bool forceAsset = false);
bool forceCurrency = false);
STPathElement(
unsigned int uType,
AccountID const& account,
PathAsset const& asset,
Currency const& currency,
AccountID const& issuer);
auto
@@ -74,12 +70,6 @@ public:
bool
hasCurrency() const;
bool
hasMPT() const;
bool
hasAsset() const;
bool
isNone() const;
@@ -88,21 +78,12 @@ public:
AccountID const&
getAccountID() const;
PathAsset const&
getPathAsset() const;
Currency const&
getCurrency() const;
MPTID const&
getMPTID() const;
AccountID const&
getIssuerID() const;
bool
isType(Type const& pe) const;
bool
operator==(STPathElement const& t) const;
@@ -137,7 +118,7 @@ public:
emplace_back(Args&&... args);
bool
hasSeen(AccountID const& account, PathAsset const& asset, AccountID const& issuer) const;
hasSeen(AccountID const& account, Currency const& currency, AccountID const& issuer) const;
Json::Value getJson(JsonOptions) const;
@@ -240,7 +221,7 @@ inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
inline STPathElement::STPathElement(
std::optional<AccountID> const& account,
std::optional<PathAsset> const& asset,
std::optional<Currency> const& currency,
std::optional<AccountID> const& issuer)
: mType(typeNone)
{
@@ -257,10 +238,10 @@ inline STPathElement::STPathElement(
mAccountID != noAccount(), "xrpl::STPathElement::STPathElement : account is set");
}
if (asset)
if (currency)
{
mAssetID = *asset;
mType |= mAssetID.holds<Currency>() ? typeCurrency : typeMPT;
mCurrencyID = *currency;
mType |= typeCurrency;
}
if (issuer)
@@ -275,20 +256,20 @@ inline STPathElement::STPathElement(
inline STPathElement::STPathElement(
AccountID const& account,
PathAsset const& asset,
Currency const& currency,
AccountID const& issuer,
bool forceAsset)
bool forceCurrency)
: mType(typeNone)
, mAccountID(account)
, mAssetID(asset)
, mCurrencyID(currency)
, mIssuerID(issuer)
, is_offer_(isXRP(mAccountID))
{
if (!is_offer_)
mType |= typeAccount;
if (forceAsset || !isXRP(mAssetID))
mType |= asset.holds<Currency>() ? typeCurrency : typeMPT;
if (forceCurrency || !isXRP(currency))
mType |= typeCurrency;
if (!isXRP(issuer))
mType |= typeIssuer;
@@ -299,19 +280,14 @@ inline STPathElement::STPathElement(
inline STPathElement::STPathElement(
unsigned int uType,
AccountID const& account,
PathAsset const& asset,
Currency const& currency,
AccountID const& issuer)
: mType(uType)
, mAccountID(account)
, mAssetID(asset)
, mCurrencyID(currency)
, mIssuerID(issuer)
, is_offer_(isXRP(mAccountID))
{
// uType could be assetType; i.e. either Currency or MPTID.
// Get the actual type.
mAssetID.visit(
[&](Currency const&) { mType = mType & (~Type::typeMPT); },
[&](MPTID const&) { mType = mType & (~Type::typeCurrency); });
hash_value_ = get_hash(*this);
}
@@ -333,34 +309,16 @@ STPathElement::isAccount() const
return !isOffer();
}
inline bool
STPathElement::isType(Type const& pe) const
{
return (mType & pe) != 0u;
}
inline bool
STPathElement::hasIssuer() const
{
return isType(STPathElement::typeIssuer);
return getNodeType() & STPathElement::typeIssuer;
}
inline bool
STPathElement::hasCurrency() const
{
return isType(STPathElement::typeCurrency);
}
inline bool
STPathElement::hasMPT() const
{
return isType(STPathElement::typeMPT);
}
inline bool
STPathElement::hasAsset() const
{
return isType(STPathElement::typeAsset);
return getNodeType() & STPathElement::typeCurrency;
}
inline bool
@@ -377,22 +335,10 @@ STPathElement::getAccountID() const
return mAccountID;
}
inline PathAsset const&
STPathElement::getPathAsset() const
{
return mAssetID;
}
inline Currency const&
STPathElement::getCurrency() const
{
return mAssetID.get<Currency>();
}
inline MPTID const&
STPathElement::getMPTID() const
{
return mAssetID.get<MPTID>();
return mCurrencyID;
}
inline AccountID const&
@@ -405,7 +351,7 @@ inline bool
STPathElement::operator==(STPathElement const& t) const
{
return (mType & typeAccount) == (t.mType & typeAccount) && hash_value_ == t.hash_value_ &&
mAccountID == t.mAccountID && mAssetID == t.mAssetID && mIssuerID == t.mIssuerID;
mAccountID == t.mAccountID && mCurrencyID == t.mCurrencyID && mIssuerID == t.mIssuerID;
}
inline bool

View File

@@ -23,7 +23,7 @@ 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);
static_assert(Number::largestMantissa >= INITIAL_XRP.drops());
static_assert(Number::maxRep >= INITIAL_XRP.drops());
/** Returns true if the amount does not exceed the initial XRP in existence. */
inline bool

View File

@@ -121,7 +121,6 @@ enum TEMcodes : TERUnderlyingType {
temARRAY_TOO_LARGE,
temBAD_TRANSFER_FEE,
temINVALID_INNER_BATCH,
temBAD_MPT,
};
//------------------------------------------------------------------------------
@@ -209,7 +208,6 @@ enum TERcodes : TERUnderlyingType {
terADDRESS_COLLISION, // Failed to allocate AccountID when trying to
// create a pseudo-account
terNO_DELEGATE_PERMISSION, // Delegate does not have permission
terLOCKED, // MPT is locked
};
//------------------------------------------------------------------------------
@@ -344,6 +342,10 @@ enum TECcodes : TERUnderlyingType {
tecLIMIT_EXCEEDED = 195,
tecPSEUDO_ACCOUNT = 196,
tecPRECISION_LOSS = 197,
// DEPRECATED: This error code tecNO_DELEGATE_PERMISSION is reserved for
// backward compatibility with historical data on non-prod networks, can be
// reclaimed after those networks reset.
tecNO_DELEGATE_PERMISSION = 198,
};
//------------------------------------------------------------------------------

View File

@@ -15,7 +15,6 @@
// Add new amendments to the top of this list.
// Keep it sorted in reverse chronological order.
XRPL_FEATURE(MPTokensV2, Supported::no, VoteBehavior::DefaultNo)
XRPL_FIX (Security3_1_3, Supported::no, VoteBehavior::DefaultNo)
XRPL_FIX (PermissionedDomainInvariant, Supported::yes, VoteBehavior::DefaultNo)
XRPL_FIX (ExpiredNFTokenOfferRemoval, Supported::yes, VoteBehavior::DefaultNo)

View File

@@ -161,10 +161,8 @@ LEDGER_ENTRY(ltDIR_NODE, 0x0064, DirectoryNode, directory, ({
{sfOwner, soeOPTIONAL}, // for owner directories
{sfTakerPaysCurrency, soeOPTIONAL}, // order book directories
{sfTakerPaysIssuer, soeOPTIONAL}, // order book directories
{sfTakerPaysMPT, soeOPTIONAL}, // order book directories
{sfTakerGetsCurrency, soeOPTIONAL}, // order book directories
{sfTakerGetsIssuer, soeOPTIONAL}, // order book directories
{sfTakerGetsMPT, soeOPTIONAL}, // order book directories
{sfExchangeRate, soeOPTIONAL}, // order book directories
{sfIndexes, soeREQUIRED},
{sfRootIndex, soeREQUIRED},

View File

@@ -159,8 +159,6 @@ TYPED_SFIELD(sfTakerGetsIssuer, UINT160, 4)
// 192-bit (common)
TYPED_SFIELD(sfMPTokenIssuanceID, UINT192, 1)
TYPED_SFIELD(sfShareMPTID, UINT192, 2)
TYPED_SFIELD(sfTakerPaysMPT, UINT192, 3)
TYPED_SFIELD(sfTakerGetsMPT, UINT192, 4)
// 256-bit (common)
TYPED_SFIELD(sfLedgerHash, UINT256, 1)

View File

@@ -27,7 +27,7 @@
TRANSACTION(ttPAYMENT, 0, Payment,
Delegation::delegable,
uint256{},
createAcct | mayCreateMPT,
createAcct,
({
{sfDestination, soeREQUIRED},
{sfAmount, soeREQUIRED, soeMPTSupported},
@@ -129,10 +129,10 @@ TRANSACTION(ttREGULAR_KEY_SET, 5, SetRegularKey,
TRANSACTION(ttOFFER_CREATE, 7, OfferCreate,
Delegation::delegable,
uint256{},
mayCreateMPT,
noPriv,
({
{sfTakerPays, soeREQUIRED, soeMPTSupported},
{sfTakerGets, soeREQUIRED, soeMPTSupported},
{sfTakerPays, soeREQUIRED},
{sfTakerGets, soeREQUIRED},
{sfExpiration, soeOPTIONAL},
{sfOfferSequence, soeOPTIONAL},
{sfDomainID, soeOPTIONAL},
@@ -239,7 +239,7 @@ TRANSACTION(ttCHECK_CREATE, 16, CheckCreate,
noPriv,
({
{sfDestination, soeREQUIRED},
{sfSendMax, soeREQUIRED, soeMPTSupported},
{sfSendMax, soeREQUIRED},
{sfExpiration, soeOPTIONAL},
{sfDestinationTag, soeOPTIONAL},
{sfInvoiceID, soeOPTIONAL},
@@ -252,11 +252,11 @@ TRANSACTION(ttCHECK_CREATE, 16, CheckCreate,
TRANSACTION(ttCHECK_CASH, 17, CheckCash,
Delegation::delegable,
uint256{},
mayCreateMPT,
noPriv,
({
{sfCheckID, soeREQUIRED},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfDeliverMin, soeOPTIONAL, soeMPTSupported},
{sfAmount, soeOPTIONAL},
{sfDeliverMin, soeOPTIONAL},
}))
/** This transaction type cancels an existing check. */
@@ -409,12 +409,12 @@ TRANSACTION(ttCLAWBACK, 30, Clawback,
TRANSACTION(ttAMM_CLAWBACK, 31, AMMClawback,
Delegation::delegable,
featureAMMClawback,
mayDeleteAcct | overrideFreeze | mayAuthorizeMPT,
mayDeleteAcct | overrideFreeze,
({
{sfHolder, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAmount, soeOPTIONAL},
}))
/** This transaction type creates an AMM instance */
@@ -424,10 +424,10 @@ TRANSACTION(ttAMM_CLAWBACK, 31, AMMClawback,
TRANSACTION(ttAMM_CREATE, 35, AMMCreate,
Delegation::delegable,
featureAMM,
createPseudoAcct | mayCreateMPT,
createPseudoAcct,
({
{sfAmount, soeREQUIRED, soeMPTSupported},
{sfAmount2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeREQUIRED},
{sfAmount2, soeREQUIRED},
{sfTradingFee, soeREQUIRED},
}))
@@ -440,10 +440,10 @@ TRANSACTION(ttAMM_DEPOSIT, 36, AMMDeposit,
featureAMM,
noPriv,
({
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfAmount2, soeOPTIONAL, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAmount, soeOPTIONAL},
{sfAmount2, soeOPTIONAL},
{sfEPrice, soeOPTIONAL},
{sfLPTokenOut, soeOPTIONAL},
{sfTradingFee, soeOPTIONAL},
@@ -456,12 +456,12 @@ TRANSACTION(ttAMM_DEPOSIT, 36, AMMDeposit,
TRANSACTION(ttAMM_WITHDRAW, 37, AMMWithdraw,
Delegation::delegable,
featureAMM,
mayDeleteAcct | mayAuthorizeMPT,
mayDeleteAcct,
({
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfAmount2, soeOPTIONAL, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAmount, soeOPTIONAL},
{sfAmount2, soeOPTIONAL},
{sfEPrice, soeOPTIONAL},
{sfLPTokenIn, soeOPTIONAL},
}))
@@ -475,8 +475,8 @@ TRANSACTION(ttAMM_VOTE, 38, AMMVote,
featureAMM,
noPriv,
({
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfTradingFee, soeREQUIRED},
}))
@@ -489,8 +489,8 @@ TRANSACTION(ttAMM_BID, 39, AMMBid,
featureAMM,
noPriv,
({
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfBidMin, soeOPTIONAL},
{sfBidMax, soeOPTIONAL},
{sfAuthAccounts, soeOPTIONAL},
@@ -503,10 +503,10 @@ TRANSACTION(ttAMM_BID, 39, AMMBid,
TRANSACTION(ttAMM_DELETE, 40, AMMDelete,
Delegation::delegable,
featureAMM,
mustDeleteAcct | mayDeleteMPT,
mustDeleteAcct,
({
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
}))
/** This transactions creates a crosschain sequence number */

View File

@@ -401,8 +401,6 @@ JSS(min_ledger); // in: LedgerCleaner
JSS(minimum_fee); // out: TxQ
JSS(minimum_level); // out: TxQ
JSS(missingCommand); // error
JSS(mpt_issuance_id_a); // out: BookChanges
JSS(mpt_issuance_id_b); // out: BookChanges
JSS(name); // out: AmendmentTableImpl, PeerImp
JSS(needed_state_hashes); // out: InboundLedger
JSS(needed_transaction_hashes); // out: InboundLedger

View File

@@ -117,30 +117,6 @@ public:
return this->sle_->isFieldPresent(sfTakerPaysIssuer);
}
/**
* @brief Get sfTakerPaysMPT (soeOPTIONAL)
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
protocol_autogen::Optional<SF_UINT192::type::value_type>
getTakerPaysMPT() const
{
if (hasTakerPaysMPT())
return this->sle_->at(sfTakerPaysMPT);
return std::nullopt;
}
/**
* @brief Check if sfTakerPaysMPT is present.
* @return True if the field is present, false otherwise.
*/
[[nodiscard]]
bool
hasTakerPaysMPT() const
{
return this->sle_->isFieldPresent(sfTakerPaysMPT);
}
/**
* @brief Get sfTakerGetsCurrency (soeOPTIONAL)
* @return The field value, or std::nullopt if not present.
@@ -189,30 +165,6 @@ public:
return this->sle_->isFieldPresent(sfTakerGetsIssuer);
}
/**
* @brief Get sfTakerGetsMPT (soeOPTIONAL)
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
protocol_autogen::Optional<SF_UINT192::type::value_type>
getTakerGetsMPT() const
{
if (hasTakerGetsMPT())
return this->sle_->at(sfTakerGetsMPT);
return std::nullopt;
}
/**
* @brief Check if sfTakerGetsMPT is present.
* @return True if the field is present, false otherwise.
*/
[[nodiscard]]
bool
hasTakerGetsMPT() const
{
return this->sle_->isFieldPresent(sfTakerGetsMPT);
}
/**
* @brief Get sfExchangeRate (soeOPTIONAL)
* @return The field value, or std::nullopt if not present.
@@ -475,17 +427,6 @@ public:
return *this;
}
/**
* @brief Set sfTakerPaysMPT (soeOPTIONAL)
* @return Reference to this builder for method chaining.
*/
DirectoryNodeBuilder&
setTakerPaysMPT(std::decay_t<typename SF_UINT192::type::value_type> const& value)
{
object_[sfTakerPaysMPT] = value;
return *this;
}
/**
* @brief Set sfTakerGetsCurrency (soeOPTIONAL)
* @return Reference to this builder for method chaining.
@@ -508,17 +449,6 @@ public:
return *this;
}
/**
* @brief Set sfTakerGetsMPT (soeOPTIONAL)
* @return Reference to this builder for method chaining.
*/
DirectoryNodeBuilder&
setTakerGetsMPT(std::decay_t<typename SF_UINT192::type::value_type> const& value)
{
object_[sfTakerGetsMPT] = value;
return *this;
}
/**
* @brief Set sfExchangeRate (soeOPTIONAL)
* @return Reference to this builder for method chaining.

View File

@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -194,7 +192,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMBidBuilder&
@@ -206,7 +203,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMBidBuilder&

View File

@@ -21,7 +21,7 @@ class AMMClawbackBuilder;
* Type: ttAMM_CLAWBACK (31)
* Delegable: Delegation::delegable
* Amendment: featureAMMClawback
* Privileges: mayDeleteAcct | overrideFreeze | mayAuthorizeMPT
* Privileges: mayDeleteAcct | overrideFreeze
*
* Immutable wrapper around STTx providing type-safe field access.
* Use AMMClawbackBuilder to construct new transactions.
@@ -60,7 +60,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -72,7 +71,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -84,7 +82,6 @@ public:
/**
* @brief Get sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -169,7 +166,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMClawbackBuilder&
@@ -181,7 +177,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMClawbackBuilder&
@@ -193,7 +188,6 @@ public:
/**
* @brief Set sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMClawbackBuilder&

View File

@@ -21,7 +21,7 @@ class AMMCreateBuilder;
* Type: ttAMM_CREATE (35)
* Delegable: Delegation::delegable
* Amendment: featureAMM
* Privileges: createPseudoAcct | mayCreateMPT
* Privileges: createPseudoAcct
*
* Immutable wrapper around STTx providing type-safe field access.
* Use AMMCreateBuilder to construct new transactions.
@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAmount (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAmount2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -131,7 +129,6 @@ public:
/**
* @brief Set sfAmount (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMCreateBuilder&
@@ -143,7 +140,6 @@ public:
/**
* @brief Set sfAmount2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMCreateBuilder&

View File

@@ -21,7 +21,7 @@ class AMMDeleteBuilder;
* Type: ttAMM_DELETE (40)
* Delegable: Delegation::delegable
* Amendment: featureAMM
* Privileges: mustDeleteAcct | mayDeleteMPT
* Privileges: mustDeleteAcct
*
* Immutable wrapper around STTx providing type-safe field access.
* Use AMMDeleteBuilder to construct new transactions.
@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -118,7 +116,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDeleteBuilder&
@@ -130,7 +127,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDeleteBuilder&

View File

@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -73,7 +71,6 @@ public:
/**
* @brief Get sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -100,7 +97,6 @@ public:
/**
* @brief Get sfAmount2 (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -250,7 +246,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDepositBuilder&
@@ -262,7 +257,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDepositBuilder&
@@ -274,7 +268,6 @@ public:
/**
* @brief Set sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDepositBuilder&
@@ -286,7 +279,6 @@ public:
/**
* @brief Set sfAmount2 (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMDepositBuilder&

View File

@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -131,7 +129,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMVoteBuilder&
@@ -143,7 +140,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMVoteBuilder&

View File

@@ -21,7 +21,7 @@ class AMMWithdrawBuilder;
* Type: ttAMM_WITHDRAW (37)
* Delegable: Delegation::delegable
* Amendment: featureAMM
* Privileges: mayDeleteAcct | mayAuthorizeMPT
* Privileges: mayDeleteAcct
*
* Immutable wrapper around STTx providing type-safe field access.
* Use AMMWithdrawBuilder to construct new transactions.
@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -73,7 +71,6 @@ public:
/**
* @brief Get sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -100,7 +97,6 @@ public:
/**
* @brief Get sfAmount2 (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -224,7 +220,6 @@ public:
/**
* @brief Set sfAsset (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMWithdrawBuilder&
@@ -236,7 +231,6 @@ public:
/**
* @brief Set sfAsset2 (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMWithdrawBuilder&
@@ -248,7 +242,6 @@ public:
/**
* @brief Set sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMWithdrawBuilder&
@@ -260,7 +253,6 @@ public:
/**
* @brief Set sfAmount2 (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
AMMWithdrawBuilder&

View File

@@ -21,7 +21,7 @@ class CheckCashBuilder;
* Type: ttCHECK_CASH (17)
* Delegable: Delegation::delegable
* Amendment: uint256{}
* Privileges: mayCreateMPT
* Privileges: noPriv
*
* Immutable wrapper around STTx providing type-safe field access.
* Use CheckCashBuilder to construct new transactions.
@@ -60,7 +60,6 @@ public:
/**
* @brief Get sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -87,7 +86,6 @@ public:
/**
* @brief Get sfDeliverMin (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value, or std::nullopt if not present.
*/
[[nodiscard]]
@@ -168,7 +166,6 @@ public:
/**
* @brief Set sfAmount (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
CheckCashBuilder&
@@ -180,7 +177,6 @@ public:
/**
* @brief Set sfDeliverMin (soeOPTIONAL)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
CheckCashBuilder&

View File

@@ -60,7 +60,6 @@ public:
/**
* @brief Get sfSendMax (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -206,7 +205,6 @@ public:
/**
* @brief Set sfSendMax (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
CheckCreateBuilder&

View File

@@ -21,7 +21,7 @@ class OfferCreateBuilder;
* Type: ttOFFER_CREATE (7)
* Delegable: Delegation::delegable
* Amendment: uint256{}
* Privileges: mayCreateMPT
* Privileges: noPriv
*
* Immutable wrapper around STTx providing type-safe field access.
* Use OfferCreateBuilder to construct new transactions.
@@ -49,7 +49,6 @@ public:
/**
* @brief Get sfTakerPays (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -61,7 +60,6 @@ public:
/**
* @brief Get sfTakerGets (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return The field value.
*/
[[nodiscard]]
@@ -196,7 +194,6 @@ public:
/**
* @brief Set sfTakerPays (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
OfferCreateBuilder&
@@ -208,7 +205,6 @@ public:
/**
* @brief Set sfTakerGets (soeREQUIRED)
* @note This field supports MPT (Multi-Purpose Token) amounts.
* @return Reference to this builder for method chaining.
*/
OfferCreateBuilder&

View File

@@ -21,7 +21,7 @@ class PaymentBuilder;
* Type: ttPAYMENT (0)
* Delegable: Delegation::delegable
* Amendment: uint256{}
* Privileges: createAcct | mayCreateMPT
* Privileges: createAcct
*
* Immutable wrapper around STTx providing type-safe field access.
* Use PaymentBuilder to construct new transactions.

View File

@@ -398,8 +398,7 @@ using InvariantChecks = std::tuple<
ValidPseudoAccounts,
ValidLoanBroker,
ValidLoan,
ValidVault,
ValidMPTPayment>;
ValidVault>;
/**
* @brief get a tuple of all invariant checks

View File

@@ -44,7 +44,6 @@ enum Privilege {
mayDeleteMPT = 0x0400, // The transaction MAY delete an MPT object. May not create.
mustModifyVault = 0x0800, // The transaction must modify, delete or create, a vault
mayModifyVault = 0x1000, // The transaction MAY modify, delete or create, a vault
mayCreateMPT = 0x2000, // The transaction MAY create an MPT object, except for issuer.
};
constexpr Privilege

View File

@@ -28,32 +28,4 @@ public:
finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&) const;
};
/** Verify:
* - OutstandingAmount <= MaximumAmount for any MPT
* - OutstandingAmount after = OutstandingAmount before +
* sum (MPT after - MPT before) - this is total MPT credit/debit
*/
class ValidMPTPayment
{
enum Order { Before = 0, After = 1 };
struct MPTData
{
std::array<std::int64_t, After + 1> outstanding{};
// sum (MPT after - MPT before)
std::int64_t mptAmount{0};
};
// true if OutstandingAmount > MaximumAmount in after for any MPT
bool overflow_{false};
// mptid:MPTData
hash_map<uint192, MPTData> data_;
public:
void
visitEntry(bool, std::shared_ptr<SLE const> const&, std::shared_ptr<SLE const> const&);
bool
finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
};
} // namespace xrpl

View File

@@ -4,13 +4,13 @@
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/View.h>
#include <xrpl/ledger/helpers/AMMHelpers.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/ledger/helpers/AMMUtils.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/tx/transactors/dex/AMMContext.h>
namespace xrpl {
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
class AMMOffer;
/** AMMLiquidity class provides AMM offers to BookStep class.
@@ -35,8 +35,8 @@ private:
AMMContext& ammContext_;
AccountID const ammAccountID_;
std::uint32_t const tradingFee_;
Asset const assetIn_;
Asset const assetOut_;
Issue const issueIn_;
Issue const issueOut_;
// Initial AMM pool balances
TAmounts<TIn, TOut> const initialBalances_;
beast::Journal const j_;
@@ -46,8 +46,8 @@ public:
ReadView const& view,
AccountID const& ammAccountID,
std::uint32_t tradingFee,
Asset const& in,
Asset const& out,
Issue const& in,
Issue const& out,
AMMContext& ammContext,
beast::Journal j);
~AMMLiquidity() = default;
@@ -87,16 +87,16 @@ public:
return ammContext_;
}
Asset const&
assetIn() const
Issue const&
issueIn() const
{
return assetIn_;
return issueIn_;
}
Asset const&
assetOut() const
Issue const&
issueOut() const
{
return assetOut_;
return issueOut_;
}
private:

View File

@@ -3,7 +3,6 @@
#include <xrpl/ledger/ApplyView.h>
#include <xrpl/ledger/View.h>
#include <xrpl/ledger/helpers/TokenHelpers.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/TER.h>
@@ -17,7 +16,7 @@ class QualityFunction;
* methods for use in generic BookStep methods. AMMOffer amounts
* are changed indirectly in BookStep limiting steps.
*/
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
class AMMOffer
{
private:
@@ -53,11 +52,8 @@ public:
return quality_;
}
Asset const&
assetIn() const;
Asset const&
assetOut() const;
Issue const&
issueIn() const;
AccountID const&
owner() const;
@@ -103,8 +99,7 @@ public:
static TER
send(Args&&... args)
{
return accountSend(
std::forward<Args>(args)..., WaiveTransferFee::Yes, AllowMPTOverflow::Yes);
return accountSend(std::forward<Args>(args)..., WaiveTransferFee::Yes);
}
bool

View File

@@ -3,8 +3,6 @@
#include <xrpl/basics/Log.h>
#include <xrpl/basics/contract.h>
#include <xrpl/ledger/View.h>
#include <xrpl/ledger/helpers/TokenHelpers.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/Rules.h>
#include <xrpl/protocol/SField.h>
@@ -14,15 +12,28 @@
namespace xrpl {
template <StepAmount TIn, StepAmount TOut>
class TOffer
template <class TIn, class TOut>
class TOfferBase
{
protected:
Issue issIn_;
Issue issOut_;
};
template <>
class TOfferBase<STAmount, STAmount>
{
public:
explicit TOfferBase() = default;
};
template <class TIn = STAmount, class TOut = STAmount>
class TOffer : private TOfferBase<TIn, TOut>
{
private:
SLE::pointer m_entry;
Quality m_quality{};
AccountID m_account;
Asset assetIn_;
Asset assetOut_;
TAmounts<TIn, TOut> m_amounts{};
void
@@ -102,10 +113,10 @@ public:
return m_entry->key();
}
Asset const&
assetIn() const;
Asset const&
assetOut() const;
Issue const&
issueIn() const;
Issue const&
issueOut() const;
TAmounts<TIn, TOut>
limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp) const;
@@ -120,8 +131,8 @@ public:
bool
isFunded() const
{
// Offer owner is issuer; they have unlimited funds if IOU
return m_account == assetOut_.getIssuer() && assetOut_.holds<Issue>();
// Offer owner is issuer; they have unlimited funds
return m_account == issueOut().account;
}
static std::pair<std::uint32_t, std::uint32_t>
@@ -156,7 +167,9 @@ public:
}
};
template <StepAmount TIn, StepAmount TOut>
using Offer = TOffer<>;
template <class TIn, class TOut>
TOffer<TIn, TOut>::TOffer(SLE::pointer const& entry, Quality quality)
: m_entry(entry), m_quality(quality), m_account(m_entry->getAccountID(sfAccount))
{
@@ -164,26 +177,33 @@ TOffer<TIn, TOut>::TOffer(SLE::pointer const& entry, Quality quality)
auto const tg = m_entry->getFieldAmount(sfTakerGets);
m_amounts.in = toAmount<TIn>(tp);
m_amounts.out = toAmount<TOut>(tg);
assetIn_ = tp.asset();
assetOut_ = tg.asset();
this->issIn_ = tp.issue();
this->issOut_ = tg.issue();
}
template <StepAmount TIn, StepAmount TOut>
template <>
inline TOffer<STAmount, STAmount>::TOffer(SLE::pointer const& entry, Quality quality)
: m_entry(entry)
, m_quality(quality)
, m_account(m_entry->getAccountID(sfAccount))
, m_amounts(m_entry->getFieldAmount(sfTakerPays), m_entry->getFieldAmount(sfTakerGets))
{
}
template <class TIn, class TOut>
void
TOffer<TIn, TOut>::setFieldAmounts()
{
if constexpr (std::is_same_v<TIn, XRPAmount>)
m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in));
else
m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, assetIn_));
if constexpr (std::is_same_v<TOut, XRPAmount>)
m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out));
else
m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, assetOut_));
// LCOV_EXCL_START
#ifdef _MSC_VER
UNREACHABLE("xrpl::TOffer::setFieldAmounts : must be specialized");
#else
static_assert(sizeof(TOut) == -1, "Must be specialized");
#endif
// LCOV_EXCL_STOP
}
template <StepAmount TIn, StepAmount TOut>
template <class TIn, class TOut>
TAmounts<TIn, TOut>
TOffer<TIn, TOut>::limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp)
const
@@ -193,7 +213,7 @@ TOffer<TIn, TOut>::limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const&
return quality().ceil_out_strict(offerAmount, limit, roundUp);
}
template <StepAmount TIn, StepAmount TOut>
template <class TIn, class TOut>
TAmounts<TIn, TOut>
TOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp)
const
@@ -208,29 +228,75 @@ TOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& li
return m_quality.ceil_in(offerAmount, limit);
}
template <StepAmount TIn, StepAmount TOut>
template <class TIn, class TOut>
template <typename... Args>
TER
TOffer<TIn, TOut>::send(Args&&... args)
{
return accountSend(std::forward<Args>(args)..., WaiveTransferFee::No, AllowMPTOverflow::Yes);
return accountSend(std::forward<Args>(args)...);
}
template <StepAmount TIn, StepAmount TOut>
Asset const&
TOffer<TIn, TOut>::assetIn() const
template <>
inline void
TOffer<STAmount, STAmount>::setFieldAmounts()
{
return assetIn_;
m_entry->setFieldAmount(sfTakerPays, m_amounts.in);
m_entry->setFieldAmount(sfTakerGets, m_amounts.out);
}
template <StepAmount TIn, StepAmount TOut>
Asset const&
TOffer<TIn, TOut>::assetOut() const
template <>
inline void
TOffer<IOUAmount, IOUAmount>::setFieldAmounts()
{
return assetOut_;
m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_));
m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_));
}
template <StepAmount TIn, StepAmount TOut>
template <>
inline void
TOffer<IOUAmount, XRPAmount>::setFieldAmounts()
{
m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_));
m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out));
}
template <>
inline void
TOffer<XRPAmount, IOUAmount>::setFieldAmounts()
{
m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in));
m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_));
}
template <class TIn, class TOut>
Issue const&
TOffer<TIn, TOut>::issueIn() const
{
return this->issIn_;
}
template <>
inline Issue const&
TOffer<STAmount, STAmount>::issueIn() const
{
return m_amounts.in.issue();
}
template <class TIn, class TOut>
Issue const&
TOffer<TIn, TOut>::issueOut() const
{
return this->issOut_;
}
template <>
inline Issue const&
TOffer<STAmount, STAmount>::issueOut() const
{
return m_amounts.out.issue();
}
template <class TIn, class TOut>
inline std::ostream&
operator<<(std::ostream& os, TOffer<TIn, TOut> const& offer)
{

View File

@@ -4,7 +4,6 @@
#include <xrpl/basics/chrono.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/ledger/View.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/tx/paths/BookTip.h>
#include <xrpl/tx/paths/Offer.h>
@@ -12,7 +11,7 @@
namespace xrpl {
template <StepAmount TIn, StepAmount TOut>
template <class TIn, class TOut>
class TOfferStreamBase
{
public:
@@ -65,7 +64,6 @@ protected:
permRmOffer(uint256 const& offerIndex) = 0;
template <class TTakerPays, class TTakerGets>
requires ValidTaker<TTakerPays, TTakerGets>
bool
shouldRmSmallIncreasedQOffer() const;
@@ -107,6 +105,33 @@ public:
}
};
/** Presents and consumes the offers in an order book.
Two `ApplyView` objects accumulate changes to the ledger. `view`
is applied when the calling transaction succeeds. If the calling
transaction fails, then `view_cancel` is applied.
Certain invalid offers are automatically removed:
- Offers with missing ledger entries
- Offers that expired
- Offers found unfunded:
An offer is found unfunded when the corresponding balance is zero
and the caller has not modified the balance. This is accomplished
by also looking up the balance in the cancel view.
When an offer is removed, it is removed from both views. This grooms the
order book regardless of whether or not the transaction is successful.
*/
class OfferStream : public TOfferStreamBase<STAmount, STAmount>
{
protected:
void
permRmOffer(uint256 const& offerIndex) override;
public:
using TOfferStreamBase<STAmount, STAmount>::TOfferStreamBase;
};
/** Presents and consumes the offers in an order book.
The `view_' ` `ApplyView` accumulates changes to the ledger.
@@ -124,7 +149,7 @@ public:
and the caller has not modified the balance. This is accomplished
by also looking up the balance in the cancel view.
*/
template <StepAmount TIn, StepAmount TOut>
template <class TIn, class TOut>
class FlowOfferStream : public TOfferStreamBase<TIn, TOut>
{
private:

View File

@@ -0,0 +1,198 @@
#pragma once
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/XRPAmount.h>
#include <optional>
namespace xrpl {
struct AmountSpec
{
explicit AmountSpec() = default;
bool native{};
union
{
XRPAmount xrp;
IOUAmount iou = {};
};
std::optional<AccountID> issuer;
std::optional<Currency> currency;
friend std::ostream&
operator<<(std::ostream& stream, AmountSpec const& amt)
{
if (amt.native)
stream << to_string(amt.xrp);
else
stream << to_string(amt.iou);
if (amt.currency)
stream << "/(" << *amt.currency << ")";
if (amt.issuer)
stream << "/" << *amt.issuer << "";
return stream;
}
};
struct EitherAmount
{
#ifndef NDEBUG
bool native = false;
#endif
union
{
IOUAmount iou = {};
XRPAmount xrp;
};
EitherAmount() = default;
explicit EitherAmount(IOUAmount const& a) : iou(a)
{
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
// ignore warning about half of iou amount being uninitialized
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
explicit EitherAmount(XRPAmount const& a) : xrp(a)
{
#ifndef NDEBUG
native = true;
#endif
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
explicit EitherAmount(AmountSpec const& a)
{
#ifndef NDEBUG
native = a.native;
#endif
if (a.native)
xrp = a.xrp;
else
iou = a.iou;
}
#ifndef NDEBUG
friend std::ostream&
operator<<(std::ostream& stream, EitherAmount const& amt)
{
if (amt.native)
stream << to_string(amt.xrp);
else
stream << to_string(amt.iou);
return stream;
}
#endif
};
template <class T>
T&
get(EitherAmount& amt)
{
static_assert(sizeof(T) == -1, "Must used specialized function");
return T(0);
}
template <>
inline IOUAmount&
get<IOUAmount>(EitherAmount& amt)
{
XRPL_ASSERT(!amt.native, "xrpl::get<IOUAmount>(EitherAmount&) : is not XRP");
return amt.iou;
}
template <>
inline XRPAmount&
get<XRPAmount>(EitherAmount& amt)
{
XRPL_ASSERT(amt.native, "xrpl::get<XRPAmount>(EitherAmount&) : is XRP");
return amt.xrp;
}
template <class T>
T const&
get(EitherAmount const& amt)
{
static_assert(sizeof(T) == -1, "Must used specialized function");
return T(0);
}
template <>
inline IOUAmount const&
get<IOUAmount>(EitherAmount const& amt)
{
XRPL_ASSERT(!amt.native, "xrpl::get<IOUAmount>(EitherAmount const&) : is not XRP");
return amt.iou;
}
template <>
inline XRPAmount const&
get<XRPAmount>(EitherAmount const& amt)
{
XRPL_ASSERT(amt.native, "xrpl::get<XRPAmount>(EitherAmount const&) : is XRP");
return amt.xrp;
}
inline AmountSpec
toAmountSpec(STAmount const& amt)
{
XRPL_ASSERT(
amt.mantissa() < std::numeric_limits<std::int64_t>::max(),
"xrpl::toAmountSpec(STAmount const&) : maximum mantissa");
bool const isNeg = amt.negative();
std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
AmountSpec result;
result.native = isXRP(amt);
if (result.native)
{
result.xrp = XRPAmount(sMant);
}
else
{
result.iou = IOUAmount(sMant, amt.exponent());
result.issuer = amt.issue().account;
result.currency = amt.issue().currency;
}
return result;
}
inline EitherAmount
toEitherAmount(STAmount const& amt)
{
if (isXRP(amt))
return EitherAmount{amt.xrp()};
return EitherAmount{amt.iou()};
}
inline AmountSpec
toAmountSpec(EitherAmount const& ea, std::optional<Currency> const& c)
{
AmountSpec r;
r.native = (!c || isXRP(*c));
r.currency = c;
XRPL_ASSERT(
ea.native == r.native,
"xrpl::toAmountSpec(EitherAmount const&&, std::optional<Currency>) : "
"matching native");
if (r.native)
{
r.xrp = ea.xrp;
}
else
{
r.iou = ea.iou;
}
return r;
}
} // namespace xrpl

View File

@@ -1,54 +0,0 @@
#pragma once
#include <xrpl/protocol/Concepts.h>
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/XRPAmount.h>
namespace xrpl {
struct EitherAmount
{
std::variant<XRPAmount, IOUAmount, MPTAmount> amount;
explicit EitherAmount() = default;
template <StepAmount T>
explicit EitherAmount(T const& a) : amount(a)
{
}
template <StepAmount T>
[[nodiscard]] bool
holds() const
{
return std::holds_alternative<T>(amount);
}
template <StepAmount T>
[[nodiscard]] T const&
get() const
{
if (!holds<T>())
Throw<std::logic_error>("EitherAmount doesn't hold requested amount");
return std::get<T>(amount);
}
#ifndef NDEBUG
friend std::ostream&
operator<<(std::ostream& stream, EitherAmount const& amt)
{
std::visit([&]<StepAmount T>(T const& a) { stream << to_string(a); }, amt.amount);
return stream;
}
#endif
};
template <StepAmount T>
T const&
get(EitherAmount const& amt)
{
return amt.get<T>();
}
} // namespace xrpl

View File

@@ -208,14 +208,14 @@ struct FlowDebugInfo
auto writeXrpAmtList = [&write_list](
std::vector<EitherAmount> const& amts, char delim = ';') {
auto get_val = [](EitherAmount const& a) -> std::string {
return xrpl::to_string(a.get<XRPAmount>());
return xrpl::to_string(a.xrp);
};
write_list(amts, get_val, delim);
};
auto writeIouAmtList = [&write_list](
std::vector<EitherAmount> const& amts, char delim = ';') {
auto get_val = [](EitherAmount const& a) -> std::string {
return xrpl::to_string(a.get<IOUAmount>());
return xrpl::to_string(a.iou);
};
write_list(amts, get_val, delim);
};

View File

@@ -50,7 +50,8 @@ checkFreeze(
if (!sleAmm)
return tecINTERNAL; // LCOV_EXCL_LINE
if (isLPTokenFrozen(view, src, (*sleAmm)[sfAsset], (*sleAmm)[sfAsset2]))
if (isLPTokenFrozen(
view, src, (*sleAmm)[sfAsset].get<Issue>(), (*sleAmm)[sfAsset2].get<Issue>()))
{
return terNO_LINE;
}

View File

@@ -2,11 +2,11 @@
#include <xrpl/basics/Log.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/Concepts.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/QualityFunction.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <xrpl/protocol/TER.h>
#include <xrpl/tx/paths/detail/EitherAmount.h>
#include <xrpl/tx/paths/detail/AmountSpec.h>
#include <boost/container/flat_set.hpp>
@@ -44,7 +44,6 @@ issues(DebtDirection dir)
BookStepIX is an IOU/XRP offer book
BookStepXI is an XRP/IOU offer book
XRPEndpointStep is the source or destination account for XRP
MPTEndpointStep is the source or destination account for MPT
Amounts may be transformed through a step in either the forward or the
reverse direction. In the forward direction, the function `fwd` is used to
@@ -340,8 +339,8 @@ std::pair<TER, STPath>
normalizePath(
AccountID const& src,
AccountID const& dst,
Asset const& deliver,
std::optional<Asset> const& sendMaxAsset,
Issue const& deliver,
std::optional<Issue> const& sendMaxIssue,
STPath const& path);
/**
@@ -356,7 +355,7 @@ normalizePath(
optimization. If, during direct offer crossing, the
quality of the tip of the book drops below this value,
then evaluating the strand can stop.
@param sendMaxAsset Optional asset to send.
@param sendMaxIssue Optional asset to send.
@param path Liquidity sources to use for this strand of the payment. The path
contains an ordered collection of the offer books to use and
accounts to ripple through.
@@ -373,9 +372,9 @@ toStrand(
ReadView const& sb,
AccountID const& src,
AccountID const& dst,
Asset const& deliver,
Issue const& deliver,
std::optional<Quality> const& limitQuality,
std::optional<Asset> const& sendMaxAsset,
std::optional<Issue> const& sendMaxIssue,
STPath const& path,
bool ownerPaysTransferFee,
OfferCrossing offerCrossing,
@@ -414,9 +413,9 @@ toStrands(
ReadView const& sb,
AccountID const& src,
AccountID const& dst,
Asset const& deliver,
Issue const& deliver,
std::optional<Quality> const& limitQuality,
std::optional<Asset> const& sendMax,
std::optional<Issue> const& sendMax,
STPathSet const& paths,
bool addDefaultPath,
bool ownerPaysTransferFee,
@@ -426,7 +425,7 @@ toStrands(
beast::Journal j);
/// @cond INTERNAL
template <StepAmount TIn, StepAmount TOut, class TDerived>
template <class TIn, class TOut, class TDerived>
struct StepImp : public Step
{
explicit StepImp() = default;
@@ -494,16 +493,8 @@ public:
// Check equal with tolerance
bool
checkNear(IOUAmount const& expected, IOUAmount const& actual);
inline bool
checkNear(MPTAmount const& expected, MPTAmount const& actual)
{
return expected == actual;
}
inline bool
checkNear(XRPAmount const& expected, XRPAmount const& actual)
{
return expected == actual;
}
bool
checkNear(XRPAmount const& expected, XRPAmount const& actual);
/// @endcond
/**
@@ -514,7 +505,7 @@ struct StrandContext
ReadView const& view; ///< Current ReadView
AccountID const strandSrc; ///< Strand source account
AccountID const strandDst; ///< Strand destination account
Asset const strandDeliver; ///< Asset strand delivers
Issue const strandDeliver; ///< Issue strand delivers
std::optional<Quality> const limitQuality; ///< Worst accepted quality
bool const isFirst; ///< true if Step is first in Strand
bool const isLast = false; ///< true if Step is last in Strand
@@ -531,11 +522,11 @@ struct StrandContext
at most twice: once as a src and once as a dst (hence the two element
array). The strandSrc and strandDst will only show up once each.
*/
std::array<boost::container::flat_set<Asset>, 2>& seenDirectAssets;
std::array<boost::container::flat_set<Issue>, 2>& seenDirectIssues;
/** A strand may not include an offer that output the same issue more
than once
*/
boost::container::flat_set<Asset>& seenBookOuts;
boost::container::flat_set<Issue>& seenBookOuts;
AMMContext& ammContext;
std::optional<uint256> domainID; // the domain the order book will use
beast::Journal const j;
@@ -548,15 +539,15 @@ struct StrandContext
// replicates the source or destination.
AccountID const& strandSrc_,
AccountID const& strandDst_,
Asset const& strandDeliver_,
Issue const& strandDeliver_,
std::optional<Quality> const& limitQuality_,
bool isLast_,
bool ownerPaysTransferFee_,
OfferCrossing offerCrossing_,
bool isDefaultPath_,
std::array<boost::container::flat_set<Asset>, 2>&
seenDirectAssets_, ///< For detecting currency loops
boost::container::flat_set<Asset>& seenBookOuts_, ///< For detecting book loops
std::array<boost::container::flat_set<Issue>, 2>&
seenDirectIssues_, ///< For detecting currency loops
boost::container::flat_set<Issue>& seenBookOuts_, ///< For detecting book loops
AMMContext& ammContext_,
std::optional<uint256> const& domainID,
beast::Journal j_); ///< Journal for logging
@@ -572,13 +563,6 @@ directStepEqual(
AccountID const& dst,
Currency const& currency);
bool
mptEndpointStepEqual(
Step const& step,
AccountID const& src,
AccountID const& dst,
MPTID const& mptid);
bool
xrpEndpointStepEqual(Step const& step, AccountID const& acc);
@@ -593,13 +577,6 @@ make_DirectStepI(
AccountID const& dst,
Currency const& c);
std::pair<TER, std::unique_ptr<Step>>
make_MPTEndpointStep(
StrandContext const& ctx,
AccountID const& src,
AccountID const& dst,
MPTID const& a);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepII(StrandContext const& ctx, Issue const& in, Issue const& out);
@@ -612,30 +589,9 @@ make_BookStepXI(StrandContext const& ctx, Issue const& out);
std::pair<TER, std::unique_ptr<Step>>
make_XRPEndpointStep(StrandContext const& ctx, AccountID const& acc);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepMM(StrandContext const& ctx, MPTIssue const& in, MPTIssue const& out);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepMX(StrandContext const& ctx, MPTIssue const& in);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepXM(StrandContext const& ctx, MPTIssue const& out);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepMI(StrandContext const& ctx, MPTIssue const& in, Issue const& out);
std::pair<TER, std::unique_ptr<Step>>
make_BookStepIM(StrandContext const& ctx, Issue const& in, MPTIssue const& out);
template <StepAmount InAmt, StepAmount OutAmt>
template <class InAmt, class OutAmt>
bool
isDirectXrpToXrp(Strand const& strand)
{
if constexpr (std::is_same_v<InAmt, XRPAmount> && std::is_same_v<OutAmt, XRPAmount>)
return strand.size() == 2;
else
return false;
}
isDirectXrpToXrp(Strand const& strand);
/// @endcond
} // namespace xrpl

View File

@@ -379,10 +379,8 @@ limitOut(
return XRPAmount{*out};
else if constexpr (std::is_same_v<TOutAmt, IOUAmount>)
return IOUAmount{*out};
else if constexpr (std::is_same_v<TOutAmt, MPTAmount>)
return MPTAmount{*out};
else
return STAmount{remainingOut.asset(), out->mantissa(), out->exponent()};
return STAmount{remainingOut.issue(), out->mantissa(), out->exponent()};
}();
// A tiny difference could be due to the round off
if (withinRelativeDistance(out, remainingOut, Number(1, -9)))
@@ -536,7 +534,7 @@ public:
@return Actual amount in and out from the strands, errors, and payment
sandbox
*/
template <StepAmount TInAmt, StepAmount TOutAmt>
template <class TInAmt, class TOutAmt>
FlowResult<TInAmt, TOutAmt>
flow(
PaymentSandbox const& baseView,

View File

@@ -13,9 +13,6 @@ public:
{
}
static bool
checkExtraFeatures(PreflightContext const& ctx);
static NotTEC
preflight(PreflightContext const& ctx);

View File

@@ -13,9 +13,6 @@ public:
{
}
static bool
checkExtraFeatures(xrpl::PreflightContext const& ctx);
static NotTEC
preflight(PreflightContext const& ctx);

View File

@@ -13,9 +13,6 @@ public:
{
}
static bool
checkExtraFeatures(PreflightContext const& ctx);
static std::uint32_t
getFlagsMask(PreflightContext const& ctx);

View File

@@ -224,7 +224,7 @@ private:
AccountID const& ammAccount,
STAmount const& amount,
STAmount const& amount2,
Asset const& lptIssue,
Issue const& lptIssue,
std::uint16_t tfee);
};

View File

@@ -98,8 +98,7 @@ public:
STAmount const& lpTokens,
STAmount const& lpTokensWithdraw,
std::uint16_t tfee,
FreezeHandling freezeHandling,
AuthHandling authHandling,
FreezeHandling freezeHanding,
WithdrawAll withdrawAll,
XRPAmount const& priorBalance,
beast::Journal const& journal);
@@ -133,7 +132,6 @@ public:
STAmount const& lpTokensWithdraw,
std::uint16_t tfee,
FreezeHandling freezeHandling,
AuthHandling authHandling,
WithdrawAll withdrawAll,
XRPAmount const& priorBalance,
beast::Journal const& journal);
@@ -143,8 +141,8 @@ public:
Sandbox& sb,
std::shared_ptr<SLE> const ammSle,
STAmount const& lpTokenBalance,
Asset const& asset1,
Asset const& asset2,
Issue const& issue1,
Issue const& issue2,
beast::Journal const& journal);
private:

View File

@@ -51,7 +51,7 @@ private:
ApplyFlags const flags,
AccountID const id,
beast::Journal const j,
Asset const& asset);
Issue const& issue);
// Use the payment flow code to perform offer crossing.
std::pair<TER, Amounts>

View File

@@ -9,17 +9,20 @@
#include <iterator>
#include <limits>
#include <numeric>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#ifdef _MSC_VER
#pragma message("Using boost::multiprecision::uint128_t and int128_t")
#endif
using uint128_t = xrpl::detail::uint128_t;
using int128_t = xrpl::detail::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)
namespace xrpl {
@@ -58,6 +61,9 @@ Number::setMantissaScale(MantissaRange::mantissa_scale scale)
// 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
@@ -91,7 +97,7 @@ public:
round() const noexcept;
// Modify the result to the correctly rounded value
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
doRoundUp(
bool& negative,
@@ -99,22 +105,22 @@ public:
int& exponent,
internalrep const& minMantissa,
internalrep const& maxMantissa,
std::string_view location);
std::string location);
// Modify the result to the correctly rounded value
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
doRoundDown(bool& negative, T& mantissa, int& exponent, internalrep const& minMantissa);
// Modify the result to the correctly rounded value
void
doRound(internalrep& drops, std::string_view location) const;
doRound(rep& drops, std::string location) const;
private:
void
doPush(unsigned d) noexcept;
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
bringIntoRange(bool& negative, T& mantissa, int& exponent, internalrep const& minMantissa);
};
@@ -201,7 +207,7 @@ Number::Guard::round() const noexcept
return 0;
}
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
Number::Guard::bringIntoRange(
bool& negative,
@@ -220,11 +226,13 @@ Number::Guard::bringIntoRange(
{
constexpr Number zero = Number{};
std::tie(negative, mantissa, exponent) = zero.toInternal();
negative = zero.negative_;
mantissa = zero.mantissa_;
exponent = zero.exponent_;
}
}
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
Number::Guard::doRoundUp(
bool& negative,
@@ -232,7 +240,7 @@ Number::Guard::doRoundUp(
int& exponent,
internalrep const& minMantissa,
internalrep const& maxMantissa,
std::string_view location)
std::string location)
{
auto r = round();
if (r == 1 || (r == 0 && (mantissa & 1) == 1))
@@ -240,7 +248,7 @@ Number::Guard::doRoundUp(
++mantissa;
// Ensure mantissa after incrementing fits within both the
// min/maxMantissa range and is a valid "rep".
if (mantissa > maxMantissa)
if (mantissa > maxMantissa || mantissa > maxRep)
{
mantissa /= 10;
++exponent;
@@ -251,7 +259,7 @@ Number::Guard::doRoundUp(
Throw<std::overflow_error>(std::string(location));
}
template <detail::UnsignedMantissa T>
template <UnsignedMantissa T>
void
Number::Guard::doRoundDown(
bool& negative,
@@ -274,25 +282,26 @@ Number::Guard::doRoundDown(
// Modify the result to the correctly rounded value
void
Number::Guard::doRound(internalrep& drops, std::string_view location) const
Number::Guard::doRound(rep& drops, std::string location) const
{
auto r = round();
if (r == 1 || (r == 0 && (drops & 1) == 1))
{
auto const& range = range_.get();
if (drops >= range.max)
if (drops >= maxRep)
{
static_assert(sizeof(internalrep) == sizeof(rep));
// This should be impossible, because it's impossible to represent
// "largestMantissa + 0.6" in Number, regardless of the scale. There aren't
// enough digits available. You'd either get a mantissa of "largestMantissa"
// or "largestMantissa / 10 + 1", neither of which will round up when
// "maxRep + 0.6" in Number, regardless of the scale. There aren't
// enough digits available. You'd either get a mantissa of "maxRep"
// or "(maxRep + 1) / 10", 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 (is_negative())
drops = -drops;
}
// Number
@@ -307,6 +316,10 @@ 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
@@ -316,127 +329,10 @@ 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 range_.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 range_.log + 1 digits.
*
*/
template <detail::UnsignedMantissa Rep>
std::tuple<bool, Rep, int>
Number::toInternal() const
{
return toInternal(range_);
}
/** 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::range_.get();
}
fromInternal(negative, mantissa, exponent, pRange);
}
constexpr Number
Number::oneSmall()
{
return Number{
false, Number::smallRange.internalMin, -Number::smallRange.log, Number::unchecked{}};
return Number{false, Number::smallRange.min, -Number::smallRange.log, Number::unchecked{}};
};
constexpr Number oneSml = Number::oneSmall();
@@ -444,86 +340,103 @@ constexpr Number oneSml = Number::oneSmall();
constexpr Number
Number::oneLarge()
{
return Number{
false, Number::largeRange.internalMin, -Number::largeRange.log, Number::unchecked{}};
return Number{false, Number::largeRange.min, -Number::largeRange.log, Number::unchecked{}};
};
constexpr Number oneLrg = Number::oneLarge();
Number
Number::one(MantissaRange const& range)
Number::one()
{
if (&range == &smallRange)
if (&range_.get() == &smallRange)
return oneSml;
XRPL_ASSERT(&range == &largeRange, "Number::one() : valid range");
XRPL_ASSERT(&range_.get() == &largeRange, "Number::one() : valid range_");
return oneLrg;
}
Number
Number::one()
{
return one(range_);
}
// Use the member names in this static function for now so the diff is cleaner
// TODO: Rename the function parameters to get rid of the "_" suffix
template <class T>
void
doNormalize(
bool& negative,
T& mantissa,
int& exponent,
T& mantissa_,
int& exponent_,
MantissaRange::rep const& minMantissa,
MantissaRange::rep const& maxMantissa)
{
auto constexpr minExponent = Number::minExponent;
auto constexpr maxExponent = Number::maxExponent;
auto constexpr maxRep = Number::maxRep;
using Guard = Number::Guard;
constexpr Number zero = Number{};
auto const& range = Number::range_.get();
if (mantissa == 0 || (mantissa < minMantissa && exponent <= minExponent))
if (mantissa_ == 0)
{
std::tie(negative, mantissa, exponent) = zero.toInternal(range);
mantissa_ = zero.mantissa_;
exponent_ = zero.exponent_;
negative = zero.negative_;
return;
}
auto m = mantissa;
while ((m < minMantissa) && (exponent > minExponent))
auto m = mantissa_;
while ((m < minMantissa) && (exponent_ > minExponent))
{
m *= 10;
--exponent;
--exponent_;
}
Guard g;
if (negative)
g.set_negative();
while (m > maxMantissa)
{
if (exponent >= maxExponent)
if (exponent_ >= maxExponent)
throw std::overflow_error("Number::normalize 1");
g.push(m % 10);
m /= 10;
++exponent;
++exponent_;
}
if ((exponent < minExponent) || (m == 0))
if ((exponent_ < minExponent) || (m < minMantissa))
{
std::tie(negative, mantissa, exponent) = zero.toInternal(range);
mantissa_ = zero.mantissa_;
exponent_ = zero.exponent_;
negative = zero.negative_;
return;
}
XRPL_ASSERT_PARTS(m <= maxMantissa, "xrpl::doNormalize", "intermediate mantissa fits in int64");
mantissa = m;
g.doRoundUp(negative, mantissa, exponent, minMantissa, maxMantissa, "Number::normalize 2");
// 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 > maxRep)
{
if (exponent_ >= maxExponent)
throw std::overflow_error("Number::normalize 1.5");
g.push(m % 10);
m /= 10;
++exponent_;
}
// Before modification, m should be within the min/max range. After
// modification, it must be less than maxRep. In other words, the original
// value should have been no more than maxRep * 10.
// (maxRep * 10 > maxMantissa)
XRPL_ASSERT_PARTS(m <= maxRep, "xrpl::doNormalize", "intermediate mantissa fits in int64");
mantissa_ = m;
g.doRoundUp(negative, mantissa_, exponent_, minMantissa, maxMantissa, "Number::normalize 2");
XRPL_ASSERT_PARTS(
mantissa >= minMantissa && mantissa <= maxMantissa,
mantissa_ >= minMantissa && mantissa_ <= maxMantissa,
"xrpl::doNormalize",
"final mantissa fits in range");
XRPL_ASSERT_PARTS(
exponent >= minExponent && exponent <= maxExponent,
"xrpl::doNormalize",
"final exponent fits in range");
}
template <>
@@ -562,20 +475,11 @@ Number::normalize<unsigned long>(
doNormalize(negative, mantissa, exponent, minMantissa, maxMantissa);
}
void
Number::normalize(MantissaRange const& range)
{
auto [negative, mantissa, exponent] = toInternal(range);
normalize(negative, mantissa, exponent, range.min, range.max);
fromInternal(negative, mantissa, exponent, &range);
}
void
Number::normalize()
{
normalize(range_);
auto const& range = range_.get();
normalize(negative_, mantissa_, exponent_, range.min, range.max);
}
// Copy the number, but set a new exponent. Because the mantissa doesn't change,
@@ -585,33 +489,21 @@ Number
Number::shiftExponent(int exponentDelta) const
{
XRPL_ASSERT_PARTS(isnormal(), "xrpl::Number::shiftExponent", "normalized");
Number result = *this;
result.exponent_ += exponentDelta;
if (result.exponent_ >= maxExponent)
auto const newExponent = exponent_ + exponentDelta;
if (newExponent >= maxExponent)
throw std::overflow_error("Number::shiftExponent");
if (result.exponent_ < minExponent)
if (newExponent < minExponent)
{
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 = range_.get();
normalize(negative, mantissa, exponent, range.min, range.max);
fromInternal(negative, mantissa, exponent, &range);
}
Number&
Number::operator+=(Number const& y)
{
auto const& range = range_.get();
constexpr Number zero = Number{};
if (y == zero)
return *this;
@@ -626,8 +518,7 @@ Number::operator+=(Number const& y)
return *this;
}
XRPL_ASSERT(
isnormal(range) && y.isnormal(range), "xrpl::Number::operator+=(Number) : is normal");
XRPL_ASSERT(isnormal() && y.isnormal(), "xrpl::Number::operator+=(Number) : is normal");
// *n = negative
// *s = sign
// *m = mantissa
@@ -635,10 +526,13 @@ Number::operator+=(Number const& y)
// Need to use uint128_t, because large mantissas can overflow when added
// together.
auto [xn, xm, xe] = toInternal<uint128_t>(range);
auto [yn, ym, ye] = y.toInternal<uint128_t>(range);
bool xn = negative_;
uint128_t xm = mantissa_;
auto xe = exponent_;
bool const yn = y.negative_;
uint128_t ym = y.mantissa_;
auto ye = y.exponent_;
Guard g;
if (xe < ye)
{
@@ -663,13 +557,14 @@ Number::operator+=(Number const& y)
} while (xe > ye);
}
auto const& range = range_.get();
auto const& minMantissa = range.min;
auto const& maxMantissa = range.max;
if (xn == yn)
{
xm += ym;
if (xm > maxMantissa)
if (xm > maxMantissa || xm > maxRep)
{
g.push(xm % 10);
xm /= 10;
@@ -689,7 +584,7 @@ Number::operator+=(Number const& y)
xe = ye;
xn = yn;
}
while (xm < minMantissa)
while (xm < minMantissa && xm * 10 <= maxRep)
{
xm *= 10;
xm -= g.pop();
@@ -698,8 +593,10 @@ Number::operator+=(Number const& y)
g.doRoundDown(xn, xm, xe, minMantissa);
}
normalize(xn, xm, xe, minMantissa, maxMantissa);
fromInternal(xn, xm, xe, &range);
negative_ = xn;
mantissa_ = static_cast<internalrep>(xm);
exponent_ = xe;
normalize();
return *this;
}
@@ -734,8 +631,6 @@ divu10(uint128_t& u)
Number&
Number::operator*=(Number const& y)
{
auto const& range = range_.get();
constexpr Number zero = Number{};
if (*this == zero)
return *this;
@@ -749,11 +644,15 @@ Number::operator*=(Number const& y)
// *m = mantissa
// *e = exponent
auto [xn, xm, xe] = toInternal(range);
bool const xn = negative_;
int const xs = xn ? -1 : 1;
internalrep xm = mantissa_;
auto xe = exponent_;
auto [yn, ym, ye] = y.toInternal(range);
bool const yn = y.negative_;
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;
@@ -763,10 +662,11 @@ Number::operator*=(Number const& y)
if (zn)
g.set_negative();
auto const& range = range_.get();
auto const& minMantissa = range.min;
auto const& maxMantissa = range.max;
while (zm > maxMantissa)
while (zm > maxMantissa || zm > maxRep)
{
// The following is optimization for:
// g.push(static_cast<unsigned>(zm % 10));
@@ -783,17 +683,17 @@ Number::operator*=(Number const& y)
minMantissa,
maxMantissa,
"Number::multiplication overflow : exponent is " + std::to_string(xe));
negative_ = zn;
mantissa_ = xm;
exponent_ = xe;
normalize(zn, xm, xe, minMantissa, maxMantissa);
fromInternal(zn, xm, xe, &range);
normalize();
return *this;
}
Number&
Number::operator/=(Number const& y)
{
auto const& range = range_.get();
constexpr Number zero = Number{};
if (y == zero)
throw std::overflow_error("Number: divide by 0");
@@ -806,12 +706,17 @@ Number::operator/=(Number const& y)
// *m = mantissa
// *e = exponent
auto [np, nm, ne] = toInternal(range);
bool const np = negative_;
int const ns = (np ? -1 : 1);
auto nm = mantissa_;
auto ne = exponent_;
auto [dp, dm, de] = y.toInternal(range);
bool const dp = y.negative_;
int const ds = (dp ? -1 : 1);
auto dm = y.mantissa_;
auto de = y.exponent_;
auto const& range = range_.get();
auto const& minMantissa = range.min;
auto const& maxMantissa = range.max;
@@ -823,7 +728,7 @@ Number::operator/=(Number const& y)
// f can be up to 10^(38-19) = 10^19 safely
static_assert(smallRange.log == 15);
static_assert(largeRange.log == 18);
bool const small = range.scale == MantissaRange::small;
bool const small = Number::getMantissaScale() == MantissaRange::small;
uint128_t const f = small ? 100'000'000'000'000'000 : 10'000'000'000'000'000'000ULL;
XRPL_ASSERT_PARTS(f >= minMantissa * 10, "Number::operator/=", "factor expected size");
@@ -873,8 +778,10 @@ Number::operator/=(Number const& y)
}
}
normalize(zn, zm, ze, minMantissa, maxMantissa);
fromInternal(zn, zm, ze, &range);
XRPL_ASSERT_PARTS(isnormal(range), "xrpl::Number::operator/=", "result is normalized");
negative_ = zn;
mantissa_ = static_cast<internalrep>(zm);
exponent_ = ze;
XRPL_ASSERT_PARTS(isnormal(), "xrpl::Number::operator/=", "result is normalized");
return *this;
}
@@ -882,36 +789,30 @@ Number::operator/=(Number const& y)
Number::
operator rep() const
{
auto const m = mantissa();
internalrep drops = externalToInternal(m);
if (drops == 0)
return drops;
rep drops = mantissa();
int offset = exponent();
Guard g;
if (m < 0)
if (drops != 0)
{
g.set_negative();
if (negative_)
{
g.set_negative();
drops = -drops;
}
for (; offset < 0; ++offset)
{
g.push(drops % 10);
drops /= 10;
}
for (; offset > 0; --offset)
{
if (drops > maxRep / 10)
throw std::overflow_error("Number::operator rep() overflow");
drops *= 10;
}
g.doRound(drops, "Number::operator rep() rounding overflow");
}
for (; offset < 0; ++offset)
{
g.push(drops % 10);
drops /= 10;
}
for (; offset > 0; --offset)
{
if (drops >= largeRange.min)
throw std::overflow_error("Number::operator rep() overflow");
drops *= 10;
}
g.doRound(drops, "Number::operator rep() rounding overflow");
if (g.is_negative())
return -drops;
else
return drops;
return drops;
}
Number
@@ -935,22 +836,19 @@ Number::truncate() const noexcept
std::string
to_string(Number const& amount)
{
auto const& range = Number::range_.get();
// keep full internal accuracy, but make more human friendly if possible
constexpr Number zero = Number{};
if (amount == zero)
return "0";
// The mantissa must have a set number of decimal places for this to work
auto [negative, mantissa, exponent] = amount.toInternal(range);
auto exponent = amount.exponent_;
auto mantissa = amount.mantissa_;
bool const negative = amount.negative_;
// Use scientific notation for exponents that are too small or too large
auto const rangeLog = range.log;
if (((exponent != 0 && amount.exponent() != 0) &&
((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10)))))
auto const rangeLog = Number::mantissaLog();
if (((exponent != 0) && ((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10)))))
{
// Remove trailing zeroes from the mantissa.
while (mantissa != 0 && mantissa % 10 == 0 && exponent < Number::maxExponent)
{
mantissa /= 10;
@@ -958,11 +856,8 @@ to_string(Number const& amount)
}
std::string ret = negative ? "-" : "";
ret.append(std::to_string(mantissa));
if (exponent != 0)
{
ret.append(1, 'e');
ret.append(std::to_string(exponent));
}
ret.append(1, 'e');
ret.append(std::to_string(exponent));
return ret;
}
@@ -1050,11 +945,20 @@ power(Number const& f, unsigned n)
return r;
}
// Returns f^(1/d)
// Uses NewtonRaphson 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
Number::root(MantissaRange const& range, Number f, unsigned d)
root(Number f, unsigned d)
{
constexpr Number zero = Number{};
auto const one = Number::one(range);
auto const one = Number::one();
if (f == one || d == 1)
return f;
@@ -1071,28 +975,21 @@ Number::root(MantissaRange const& range, Number f, unsigned d)
if (f == zero)
return f;
auto const [e, di] = [&]() {
auto const exponent = std::get<2>(f.toInternal(range));
// 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);
// 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;
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");
XRPL_ASSERT_PARTS(f.isnormal(), "xrpl::root(Number, unsigned)", "f is normalized");
bool neg = false;
if (f < zero)
{
@@ -1125,33 +1022,15 @@ Number::root(MantissaRange const& range, Number f, unsigned d)
// return r * 10^(e/d) to reverse scaling
auto const result = r.shiftExponent(e / di);
XRPL_ASSERT_PARTS(
result.isnormal(range), "xrpl::root(Number, unsigned)", "result is normalized");
XRPL_ASSERT_PARTS(result.isnormal(), "xrpl::root(Number, unsigned)", "result is normalized");
return result;
}
// Returns f^(1/d)
// Uses NewtonRaphson 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::range_.get();
return Number::root(range, f, d);
}
Number
root2(Number f)
{
auto const& range = Number::range_.get();
constexpr Number zero = Number{};
auto const one = Number::one(range);
auto const one = Number::one();
if (f == one)
return f;
@@ -1160,18 +1039,12 @@ root2(Number f)
if (f == zero)
return f;
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");
// 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");
// Quadratic least squares curve fit of f^(1/d) in the range [0, 1]
auto const D = 105;
@@ -1193,7 +1066,7 @@ root2(Number f)
// return r * 10^(e/2) to reverse scaling
auto const result = r.shiftExponent(e / 2);
XRPL_ASSERT_PARTS(result.isnormal(range), "xrpl::root2(Number)", "result is normalized");
XRPL_ASSERT_PARTS(result.isnormal(), "xrpl::root2(Number)", "result is normalized");
return result;
}
@@ -1203,10 +1076,8 @@ root2(Number f)
Number
power(Number const& f, unsigned n, unsigned d)
{
auto const& range = Number::range_.get();
constexpr Number zero = Number{};
auto const one = Number::one(range);
auto const one = Number::one();
if (f == one)
return f;
@@ -1228,7 +1099,7 @@ power(Number const& f, unsigned n, unsigned d)
d /= g;
if ((n % 2) == 1 && (d % 2) == 0 && f < zero)
throw std::overflow_error("Number::power nan");
return Number::root(range, power(f, n), d);
return root(power(f, n), d);
}
} // namespace xrpl

View File

@@ -43,14 +43,13 @@ AcceptedLedgerTx::AcceptedLedgerTx(
auto const amount = mTxn->getFieldAmount(sfTakerGets);
// If the offer create is not self funded then add the owner balance
if (account != amount.getIssuer())
if (account != amount.issue().account)
{
auto const ownerFunds = accountFunds(
*ledger,
account,
amount,
fhIGNORE_FREEZE,
ahIGNORE_AUTH,
beast::Journal{beast::Journal::getNullSink()});
mJson[jss::transaction][jss::owner_funds] = ownerFunds.getText();
}

View File

@@ -5,6 +5,8 @@
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/st.h>
#include <stdexcept>
namespace xrpl {
namespace detail {
@@ -374,14 +376,14 @@ ApplyStateTable::erase(ReadView const& base, std::shared_ptr<SLE> const& sle)
{
auto const iter = items_.find(sle->key());
if (iter == items_.end())
LogicError("ApplyStateTable::erase: missing key");
Throw<std::logic_error>("ApplyStateTable::erase: missing key");
auto& item = iter->second;
if (item.second != sle)
LogicError("ApplyStateTable::erase: unknown SLE");
Throw<std::logic_error>("ApplyStateTable::erase: unknown SLE");
switch (item.first)
{
case Action::erase:
LogicError("ApplyStateTable::erase: double erase");
Throw<std::logic_error>("ApplyStateTable::erase: double erase");
break;
case Action::insert:
items_.erase(iter);
@@ -405,7 +407,7 @@ ApplyStateTable::rawErase(ReadView const& base, std::shared_ptr<SLE> const& sle)
switch (item.first)
{
case Action::erase:
LogicError("ApplyStateTable::rawErase: double erase");
Throw<std::logic_error>("ApplyStateTable::rawErase: double erase");
break;
case Action::insert:
items_.erase(result.first);
@@ -436,11 +438,11 @@ ApplyStateTable::insert(ReadView const& base, std::shared_ptr<SLE> const& sle)
switch (item.first)
{
case Action::cache:
LogicError("ApplyStateTable::insert: already cached");
Throw<std::logic_error>("ApplyStateTable::insert: already cached");
case Action::insert:
LogicError("ApplyStateTable::insert: already inserted");
Throw<std::logic_error>("ApplyStateTable::insert: already inserted");
case Action::modify:
LogicError("ApplyStateTable::insert: already modified");
Throw<std::logic_error>("ApplyStateTable::insert: already modified");
case Action::erase:
break;
}
@@ -466,7 +468,7 @@ ApplyStateTable::replace(ReadView const& base, std::shared_ptr<SLE> const& sle)
switch (item.first)
{
case Action::erase:
LogicError("ApplyStateTable::replace: already erased");
Throw<std::logic_error>("ApplyStateTable::replace: already erased");
case Action::cache:
item.first = Action::modify;
break;
@@ -482,14 +484,14 @@ ApplyStateTable::update(ReadView const& base, std::shared_ptr<SLE> const& sle)
{
auto const iter = items_.find(sle->key());
if (iter == items_.end())
LogicError("ApplyStateTable::update: missing key");
Throw<std::logic_error>("ApplyStateTable::update: missing key");
auto& item = iter->second;
if (item.second != sle)
LogicError("ApplyStateTable::update: unknown SLE");
Throw<std::logic_error>("ApplyStateTable::update: unknown SLE");
switch (item.first)
{
case Action::erase:
LogicError("ApplyStateTable::update: erased");
Throw<std::logic_error>("ApplyStateTable::update: erased");
break;
case Action::cache:
item.first = Action::modify;

View File

@@ -4,6 +4,7 @@
#include <xrpl/protocol/Protocol.h>
#include <limits>
#include <stdexcept>
#include <type_traits>
namespace xrpl {
@@ -40,10 +41,8 @@ findPreviousPage(ApplyView& view, Keylet const& directory, SLE::ref start)
{
node = view.peek(keylet::page(directory, page));
if (!node)
{ // LCOV_EXCL_START
LogicError("Directory chain: root back-pointer broken.");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>(
"Directory chain: root back-pointer broken."); // LCOV_EXCL_LINE
}
auto indexes = node->getFieldV256(sfIndexes);
@@ -62,21 +61,20 @@ insertKey(
if (preserveOrder)
{
if (std::find(indexes.begin(), indexes.end(), key) != indexes.end())
LogicError("dirInsert: double insertion"); // LCOV_EXCL_LINE
Throw<std::logic_error>("dirInsert: double insertion"); // LCOV_EXCL_LINE
indexes.push_back(key);
}
else
{
// We can't be sure if this page is already sorted because
// it may be a legacy page we haven't yet touched. Take
// the time to sort it.
// We can't be sure if this page is already sorted because it may be a
// legacy page we haven't yet touched. Take the time to sort it.
std::sort(indexes.begin(), indexes.end());
auto pos = std::lower_bound(indexes.begin(), indexes.end(), key);
if (pos != indexes.end() && key == *pos)
LogicError("dirInsert: double insertion"); // LCOV_EXCL_LINE
Throw<std::logic_error>("dirInsert: double insertion"); // LCOV_EXCL_LINE
indexes.insert(pos, key);
}
@@ -129,8 +127,7 @@ insertPage(
node->setFieldH256(sfRootIndex, directory.key);
node->setFieldV256(sfIndexes, indexes);
// Save some space by not specifying the value 0 since
// it's the default.
// Save some space by not specifying the value 0 since it's the default.
if (page != 1)
node->setFieldU64(sfIndexPrevious, page - 1);
XRPL_ASSERT_PARTS(!nextPage, "xrpl::directory::insertPage", "nextPage has default value");
@@ -199,28 +196,24 @@ ApplyView::emptyDirDelete(Keylet const& directory)
auto nextPage = node->getFieldU64(sfIndexNext);
if (nextPage == rootPage && prevPage != rootPage)
LogicError("Directory chain: fwd link broken"); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: fwd link broken"); // LCOV_EXCL_LINE
if (prevPage == rootPage && nextPage != rootPage)
LogicError("Directory chain: rev link broken"); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: rev link broken"); // LCOV_EXCL_LINE
// Older versions of the code would, in some cases, allow the last
// page to be empty. Remove such pages:
// Older versions of the code would, in some cases, allow the last page to
// be empty. Remove such pages:
if (nextPage == prevPage && nextPage != rootPage)
{
auto last = peek(keylet::page(directory, nextPage));
if (!last)
{ // LCOV_EXCL_START
LogicError("Directory chain: fwd link broken.");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>("Directory chain: fwd link broken."); // LCOV_EXCL_LINE
if (!last->getFieldV256(sfIndexes).empty())
return false;
// Update the first page's linked list and
// mark it as updated.
// Update the first page's linked list and mark it as updated.
node->setFieldU64(sfIndexNext, rootPage);
node->setFieldU64(sfIndexPrevious, rootPage);
update(node);
@@ -228,8 +221,7 @@ ApplyView::emptyDirDelete(Keylet const& directory)
// And erase the empty last page:
erase(last);
// Make sure our local values reflect the
// updated information:
// Make sure our local values reflect the updated information:
nextPage = rootPage;
prevPage = rootPage;
}
@@ -269,46 +261,33 @@ ApplyView::dirRemove(Keylet const& directory, std::uint64_t page, uint256 const&
return true;
}
// The current page is now empty; check if it can be
// deleted, and, if so, whether the entire directory
// can now be removed.
// The current page is now empty; check if it can be deleted, and, if so,
// whether the entire directory can now be removed.
auto prevPage = node->getFieldU64(sfIndexPrevious);
auto nextPage = node->getFieldU64(sfIndexNext);
// The first page is the directory's root node and is
// treated specially: it can never be deleted even if
// it is empty, unless we plan on removing the entire
// directory.
// The first page is the directory's root node and is treated specially: it
// can never be deleted even if it is empty, unless we plan on removing the
// entire directory.
if (page == rootPage)
{
if (nextPage == page && prevPage != page)
{ // LCOV_EXCL_START
LogicError("Directory chain: fwd link broken");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>("Directory chain: fwd link broken"); // LCOV_EXCL_LINE
if (prevPage == page && nextPage != page)
{ // LCOV_EXCL_START
LogicError("Directory chain: rev link broken");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>("Directory chain: rev link broken"); // LCOV_EXCL_LINE
// Older versions of the code would, in some cases,
// allow the last page to be empty. Remove such
// pages if we stumble on them:
// Older versions of the code would, in some cases, allow the last page
// to be empty. Remove such pages if we stumble on them:
if (nextPage == prevPage && nextPage != page)
{
auto last = peek(keylet::page(directory, nextPage));
if (!last)
{ // LCOV_EXCL_START
LogicError("Directory chain: fwd link broken.");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>("Directory chain: fwd link broken."); // LCOV_EXCL_LINE
if (last->getFieldV256(sfIndexes).empty())
{
// Update the first page's linked list and
// mark it as updated.
// Update the first page's linked list and mark it as updated.
node->setFieldU64(sfIndexNext, page);
node->setFieldU64(sfIndexPrevious, page);
update(node);
@@ -316,8 +295,7 @@ ApplyView::dirRemove(Keylet const& directory, std::uint64_t page, uint256 const&
// And erase the empty last page:
erase(last);
// Make sure our local values reflect the
// updated information:
// Make sure our local values reflect the updated information:
nextPage = page;
prevPage = page;
}
@@ -335,25 +313,24 @@ ApplyView::dirRemove(Keylet const& directory, std::uint64_t page, uint256 const&
// This can never happen for nodes other than the root:
if (nextPage == page)
LogicError("Directory chain: fwd link broken"); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: fwd link broken"); // LCOV_EXCL_LINE
if (prevPage == page)
LogicError("Directory chain: rev link broken"); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: rev link broken"); // LCOV_EXCL_LINE
// This node isn't the root, so it can either be in the
// middle of the list, or at the end. Unlink it first
// and then check if that leaves the list with only a
// root:
// This node isn't the root, so it can either be in the middle of the list,
// or at the end. Unlink it first and then check if that leaves the list
// with only a root:
auto prev = peek(keylet::page(directory, prevPage));
if (!prev)
LogicError("Directory chain: fwd link broken."); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: fwd link broken."); // LCOV_EXCL_LINE
// Fix previous to point to its new next.
prev->setFieldU64(sfIndexNext, nextPage);
update(prev);
auto next = peek(keylet::page(directory, nextPage));
if (!next)
LogicError("Directory chain: rev link broken."); // LCOV_EXCL_LINE
Throw<std::logic_error>("Directory chain: rev link broken."); // LCOV_EXCL_LINE
// Fix next to point to its new previous.
next->setFieldU64(sfIndexPrevious, prevPage);
update(next);
@@ -361,13 +338,12 @@ ApplyView::dirRemove(Keylet const& directory, std::uint64_t page, uint256 const&
// The page is no longer linked. Delete it.
erase(node);
// Check whether the next page is the last page and, if
// so, whether it's empty. If it is, delete it.
// Check whether the next page is the last page and, if so, whether it's
// empty. If it is, delete it.
if (nextPage != rootPage && next->getFieldU64(sfIndexNext) == rootPage &&
next->getFieldV256(sfIndexes).empty())
{
// Since next doesn't point to the root, it
// can't be pointing to prev.
// Since next doesn't point to the root, it can't be pointing to prev.
erase(next);
// The previous page is now the last page:
@@ -377,18 +353,16 @@ ApplyView::dirRemove(Keylet const& directory, std::uint64_t page, uint256 const&
// And the root points to the last page:
auto root = peek(keylet::page(directory, rootPage));
if (!root)
{ // LCOV_EXCL_START
LogicError("Directory chain: root link broken.");
// LCOV_EXCL_STOP
}
Throw<std::logic_error>("Directory chain: root link broken."); // LCOV_EXCL_LINE
root->setFieldU64(sfIndexPrevious, prevPage);
update(root);
nextPage = rootPage;
}
// If we're not keeping the root, then check to see if
// it's left empty. If so, delete it as well.
// If we're not keeping the root, then check to see if it's left empty.
// If so, delete it as well.
if (!keepRoot && nextPage == rootPage && prevPage == rootPage)
{
if (prev->getFieldV256(sfIndexes).empty())

View File

@@ -12,6 +12,7 @@
#include <xrpl/protocol/digest.h>
#include <xrpl/protocol/jss.h>
#include <stdexcept>
#include <utility>
#include <vector>
@@ -461,14 +462,14 @@ void
Ledger::rawErase(std::shared_ptr<SLE> const& sle)
{
if (!stateMap_.delItem(sle->key()))
LogicError("Ledger::rawErase: key not found");
Throw<std::logic_error>("Ledger::rawErase: key not found");
}
void
Ledger::rawErase(uint256 const& key)
{
if (!stateMap_.delItem(key))
LogicError("Ledger::rawErase: key not found");
Throw<std::logic_error>("Ledger::rawErase: key not found");
}
void
@@ -478,7 +479,7 @@ Ledger::rawInsert(std::shared_ptr<SLE> const& sle)
sle->add(ss);
if (!stateMap_.addGiveItem(
SHAMapNodeType::tnACCOUNT_STATE, make_shamapitem(sle->key(), ss.slice())))
LogicError("Ledger::rawInsert: key already exists");
Throw<std::logic_error>("Ledger::rawInsert: key already exists");
}
void
@@ -488,7 +489,7 @@ Ledger::rawReplace(std::shared_ptr<SLE> const& sle)
sle->add(ss);
if (!stateMap_.updateGiveItem(
SHAMapNodeType::tnACCOUNT_STATE, make_shamapitem(sle->key(), ss.slice())))
LogicError("Ledger::rawReplace: key not found");
Throw<std::logic_error>("Ledger::rawReplace: key not found");
}
void
@@ -504,7 +505,7 @@ Ledger::rawTxInsert(
s.addVL(txn->peekData());
s.addVL(metaData->peekData());
if (!txMap_.addGiveItem(SHAMapNodeType::tnTRANSACTION_MD, make_shamapitem(key, s.slice())))
LogicError("duplicate_tx: " + to_string(key));
Throw<std::logic_error>("duplicate_tx: " + to_string(key));
}
uint256
@@ -522,7 +523,7 @@ Ledger::rawTxInsertWithHash(
auto item = make_shamapitem(key, s.slice());
auto hash = sha512Half(HashPrefix::txNode, item->slice(), item->key());
if (!txMap_.addGiveItem(SHAMapNodeType::tnTRANSACTION_MD, std::move(item)))
LogicError("duplicate_tx: " + to_string(key));
Throw<std::logic_error>("duplicate_tx: " + to_string(key));
return hash;
}

View File

@@ -1,6 +1,8 @@
#include <xrpl/basics/contract.h>
#include <xrpl/ledger/OpenView.h>
#include <stdexcept>
namespace xrpl {
class OpenView::txs_iter_impl : public txs_type::iter_base
@@ -247,7 +249,7 @@ OpenView::rawTxInsert(
auto const result = txs_.emplace(
std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(txn, metaData));
if (!result.second)
LogicError("rawTxInsert: duplicate TX id: " + to_string(key));
Throw<std::logic_error>("rawTxInsert: duplicate TX id: " + to_string(key));
}
} // namespace xrpl

View File

@@ -3,14 +3,12 @@
#include <xrpl/ledger/View.h>
#include <xrpl/protocol/SField.h>
#include <algorithm>
namespace xrpl {
namespace detail {
auto
DeferredCredits::makeKeyIOU(AccountID const& a1, AccountID const& a2, Currency const& c) -> KeyIOU
DeferredCredits::makeKey(AccountID const& a1, AccountID const& a2, Currency const& c) -> Key
{
if (a1 < a2)
{
@@ -21,23 +19,21 @@ DeferredCredits::makeKeyIOU(AccountID const& a1, AccountID const& a2, Currency c
}
void
DeferredCredits::creditIOU(
DeferredCredits::credit(
AccountID const& sender,
AccountID const& receiver,
STAmount const& amount,
STAmount const& preCreditSenderBalance)
{
XRPL_ASSERT(
sender != receiver, "xrpl::detail::DeferredCredits::creditIOU : sender is not receiver");
XRPL_ASSERT(!amount.negative(), "xrpl::detail::DeferredCredits::creditIOU : positive amount");
XRPL_ASSERT(
amount.holds<Issue>(), "xrpl::detail::DeferredCredits::creditIOU : amount is for Issue");
sender != receiver, "xrpl::detail::DeferredCredits::credit : sender is not receiver");
XRPL_ASSERT(!amount.negative(), "xrpl::detail::DeferredCredits::credit : positive amount");
auto const k = makeKeyIOU(sender, receiver, amount.get<Issue>().currency);
auto i = creditsIOU_.find(k);
if (i == creditsIOU_.end())
auto const k = makeKey(sender, receiver, amount.getCurrency());
auto i = credits_.find(k);
if (i == credits_.end())
{
ValueIOU v;
Value v;
if (sender < receiver)
{
@@ -52,7 +48,7 @@ DeferredCredits::creditIOU(
v.lowAcctOrigBalance = -preCreditSenderBalance;
}
creditsIOU_[k] = v;
credits_[k] = v;
}
else
{
@@ -69,93 +65,6 @@ DeferredCredits::creditIOU(
}
}
void
DeferredCredits::creditMPT(
AccountID const& sender,
AccountID const& receiver,
STAmount const& amount,
std::uint64_t preCreditBalanceHolder,
std::int64_t preCreditBalanceIssuer)
{
XRPL_ASSERT(
amount.holds<MPTIssue>(),
"xrpl::detail::DeferredCredits::creditMPT : amount is for MPTIssue");
XRPL_ASSERT(!amount.negative(), "xrpl::detail::DeferredCredits::creditMPT : positive amount");
XRPL_ASSERT(
sender != receiver, "xrpl::detail::DeferredCredits::creditMPT : sender is not receiver");
auto const mptAmtVal = amount.mpt().value();
auto const& issuer = amount.getIssuer();
auto const& mptIssue = amount.get<MPTIssue>();
auto const& mptID = mptIssue.getMptID();
bool const isSenderIssuer = sender == issuer;
auto i = creditsMPT_.find(mptID);
if (i == creditsMPT_.end())
{
IssuerValueMPT v;
if (isSenderIssuer)
{
v.credit = mptAmtVal;
v.holders[receiver].origBalance = preCreditBalanceHolder;
}
else
{
v.holders[sender].debit = mptAmtVal;
v.holders[sender].origBalance = preCreditBalanceHolder;
}
v.origBalance = preCreditBalanceIssuer;
creditsMPT_.emplace(mptID, std::move(v));
}
else
{
// only record the balance the first time, do not record it here
auto& v = i->second;
if (isSenderIssuer)
{
v.credit += mptAmtVal;
if (!v.holders.contains(receiver))
{
v.holders[receiver].origBalance = preCreditBalanceHolder;
}
}
else
{
if (!v.holders.contains(sender))
{
v.holders[sender].debit = mptAmtVal;
v.holders[sender].origBalance = preCreditBalanceHolder;
}
else
{
v.holders[sender].debit += mptAmtVal;
}
}
}
}
void
DeferredCredits::issuerSelfDebitMPT(
MPTIssue const& issue,
std::uint64_t amount,
std::int64_t origBalance)
{
auto const& mptID = issue.getMptID();
auto i = creditsMPT_.find(mptID);
if (i == creditsMPT_.end())
{
IssuerValueMPT v;
v.origBalance = origBalance;
v.selfDebit = amount;
creditsMPT_.emplace(mptID, std::move(v));
}
else
{
i->second.selfDebit += amount;
}
}
void
DeferredCredits::ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next)
{
@@ -179,16 +88,16 @@ DeferredCredits::ownerCount(AccountID const& id) const
// Get the adjustments for the balance between main and other.
auto
DeferredCredits::adjustmentsIOU(
DeferredCredits::adjustments(
AccountID const& main,
AccountID const& other,
Currency const& currency) const -> std::optional<AdjustmentIOU>
Currency const& currency) const -> std::optional<Adjustment>
{
std::optional<AdjustmentIOU> result;
std::optional<Adjustment> result;
KeyIOU const k = makeKeyIOU(main, other, currency);
auto i = creditsIOU_.find(k);
if (i == creditsIOU_.end())
Key const k = makeKey(main, other, currency);
auto i = credits_.find(k);
if (i == credits_.end())
return result;
auto const& v = i->second;
@@ -203,21 +112,12 @@ DeferredCredits::adjustmentsIOU(
return result;
}
auto
DeferredCredits::adjustmentsMPT(xrpl::MPTID const& mptID) const -> std::optional<AdjustmentMPT>
{
auto i = creditsMPT_.find(mptID);
if (i == creditsMPT_.end())
return std::nullopt;
return i->second;
}
void
DeferredCredits::apply(DeferredCredits& to)
{
for (auto const& i : creditsIOU_)
for (auto const& i : credits_)
{
auto r = to.creditsIOU_.emplace(i);
auto r = to.credits_.emplace(i);
if (!r.second)
{
auto& toVal = r.first->second;
@@ -228,30 +128,6 @@ DeferredCredits::apply(DeferredCredits& to)
}
}
for (auto const& i : creditsMPT_)
{
auto r = to.creditsMPT_.emplace(i);
if (!r.second)
{
auto& toVal = r.first->second;
auto const& fromVal = i.second;
toVal.credit += fromVal.credit;
toVal.selfDebit += fromVal.selfDebit;
for (auto& [k, v] : fromVal.holders)
{
if (toVal.holders.find(k) == toVal.holders.end())
{
toVal.holders[k] = v;
}
else
{
toVal.holders[k].debit += v.debit;
}
}
// Do not update the orig balance, it's already correct
}
}
for (auto const& i : ownerCounts_)
{
auto r = to.ownerCounts_.emplace(i);
@@ -267,13 +143,11 @@ DeferredCredits::apply(DeferredCredits& to)
} // namespace detail
STAmount
PaymentSandbox::balanceHookIOU(
PaymentSandbox::balanceHook(
AccountID const& account,
AccountID const& issuer,
STAmount const& amount) const
{
XRPL_ASSERT(amount.holds<Issue>(), "balanceHookIOU: amount is for Issue");
/*
There are two algorithms here. The pre-switchover algorithm takes the
current amount and subtracts the recorded credits. The post-switchover
@@ -285,14 +159,14 @@ PaymentSandbox::balanceHookIOU(
magnitudes, (B+C)-C may not equal B.
*/
auto const& currency = amount.get<Issue>().currency;
auto const currency = amount.getCurrency();
auto delta = amount.zeroed();
auto lastBal = amount;
auto minBal = amount;
for (auto curSB = this; curSB != nullptr; curSB = curSB->ps_)
{
if (auto adj = curSB->tab_.adjustmentsIOU(account, issuer, currency))
if (auto adj = curSB->tab_.adjustments(account, issuer, currency))
{
delta += adj->debits;
lastBal = adj->origBalance;
@@ -306,13 +180,13 @@ PaymentSandbox::balanceHookIOU(
// to compute usable balance just slightly above what the ledger
// calculates (but always less than the actual balance).
auto adjustedAmt = std::min({amount, lastBal - delta, minBal});
adjustedAmt.get<Issue>().account = amount.getIssuer();
adjustedAmt.setIssuer(amount.getIssuer());
if (isXRP(issuer) && adjustedAmt < beast::zero)
{
// A calculated negative XRP balance is not an error case. Consider a
// payment snippet that credits a large XRP amount and then debits the
// same amount. The credit can't be used, but we subtract the debit and
// same amount. The credit can't be used but we subtract the debit and
// calculate a negative value. It's not an error case.
adjustedAmt.clear();
}
@@ -320,64 +194,6 @@ PaymentSandbox::balanceHookIOU(
return adjustedAmt;
}
STAmount
PaymentSandbox::balanceHookMPT(AccountID const& account, MPTIssue const& issue, std::int64_t amount)
const
{
auto const& issuer = issue.getIssuer();
bool const accountIsHolder = account != issuer;
std::int64_t delta = 0;
std::int64_t lastBal = amount;
std::int64_t minBal = amount;
for (auto curSB = this; curSB != nullptr; curSB = curSB->ps_)
{
if (auto adj = curSB->tab_.adjustmentsMPT(issue))
{
if (accountIsHolder)
{
if (auto const i = adj->holders.find(account); i != adj->holders.end())
{
delta += i->second.debit;
lastBal = i->second.origBalance;
}
}
else
{
delta += adj->credit;
lastBal = adj->origBalance;
}
minBal = std::min(lastBal, minBal);
}
}
// The adjusted amount should never be larger than the balance.
auto const adjustedAmt = std::min({amount, lastBal - delta, minBal});
return adjustedAmt > 0 ? STAmount{issue, adjustedAmt} : STAmount{issue};
}
STAmount
PaymentSandbox::balanceHookSelfIssueMPT(xrpl::MPTIssue const& issue, std::int64_t amount) const
{
std::int64_t selfDebited = 0;
std::int64_t lastBal = amount;
for (auto curSB = this; curSB != nullptr; curSB = curSB->ps_)
{
if (auto adj = curSB->tab_.adjustmentsMPT(issue))
{
selfDebited += adj->selfDebit;
lastBal = adj->origBalance;
}
}
if (lastBal > selfDebited)
return STAmount{issue, lastBal - selfDebited};
return STAmount{issue};
}
std::uint32_t
PaymentSandbox::ownerCountHook(AccountID const& account, std::uint32_t count) const
{
@@ -391,39 +207,13 @@ PaymentSandbox::ownerCountHook(AccountID const& account, std::uint32_t count) co
}
void
PaymentSandbox::creditHookIOU(
PaymentSandbox::creditHook(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
STAmount const& preCreditBalance)
{
XRPL_ASSERT(amount.holds<Issue>(), "creditHookIOU: amount is for Issue");
tab_.creditIOU(from, to, amount, preCreditBalance);
}
void
PaymentSandbox::creditHookMPT(
AccountID const& from,
AccountID const& to,
STAmount const& amount,
std::uint64_t preCreditBalanceHolder,
std::int64_t preCreditBalanceIssuer)
{
XRPL_ASSERT(amount.holds<MPTIssue>(), "creditHookMPT: amount is for MPTIssue");
tab_.creditMPT(from, to, amount, preCreditBalanceHolder, preCreditBalanceIssuer);
}
void
PaymentSandbox::issuerSelfDebitHookMPT(
MPTIssue const& issue,
std::uint64_t amount,
std::int64_t origBalance)
{
XRPL_ASSERT(amount > 0, "PaymentSandbox::issuerSelfDebitHookMPT: amount must be > 0");
tab_.issuerSelfDebitMPT(issue, amount, origBalance);
tab_.credit(from, to, amount, preCreditBalance);
}
void
@@ -556,7 +346,7 @@ PaymentSandbox::balanceChanges(ReadView const& view) const
}
// The following are now set, put them in the map
auto delta = newBalance - oldBalance;
auto const cur = newBalance.get<Issue>().currency;
auto const cur = newBalance.getCurrency();
result[std::make_tuple(lowID, highID, cur)] = delta;
auto r = result.emplace(std::make_tuple(lowID, lowID, cur), delta);
if (r.second)

View File

@@ -1,6 +1,8 @@
#include <xrpl/basics/contract.h>
#include <xrpl/ledger/detail/RawStateTable.h>
#include <stdexcept>
namespace xrpl {
namespace detail {
@@ -241,7 +243,7 @@ RawStateTable::erase(std::shared_ptr<SLE> const& sle)
switch (item.action)
{
case Action::erase:
LogicError("RawStateTable::erase: already erased");
Throw<std::logic_error>("RawStateTable::erase: already erased");
break;
case Action::insert:
items_.erase(result.first);
@@ -270,10 +272,10 @@ RawStateTable::insert(std::shared_ptr<SLE> const& sle)
item.sle = sle;
break;
case Action::insert:
LogicError("RawStateTable::insert: already inserted");
Throw<std::logic_error>("RawStateTable::insert: already inserted");
break;
case Action::replace:
LogicError("RawStateTable::insert: already exists");
Throw<std::logic_error>("RawStateTable::insert: already exists");
break;
}
}
@@ -291,7 +293,7 @@ RawStateTable::replace(std::shared_ptr<SLE> const& sle)
switch (item.action)
{
case Action::erase:
LogicError("RawStateTable::replace: was erased");
Throw<std::logic_error>("RawStateTable::replace: was erased");
break;
case Action::insert:
case Action::replace:

View File

@@ -84,10 +84,11 @@ bool
isLPTokenFrozen(
ReadView const& view,
AccountID const& account,
Asset const& asset,
Asset const& asset2)
Issue const& asset,
Issue const& asset2)
{
return isFrozen(view, account, asset) || isFrozen(view, account, asset2);
return isFrozen(view, account, asset.currency, asset.account) ||
isFrozen(view, account, asset2.currency, asset2.account);
}
bool
@@ -333,19 +334,22 @@ withdrawToDestExceedsLimit(
if (from == to || to == issuer || isXRP(issuer))
return tesSUCCESS;
return amount.asset().visit(
[&](Issue const& issue) -> TER {
auto const& currency = issue.currency;
auto const owed = creditBalance(view, to, issuer, currency);
if (owed <= beast::zero)
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) -> TER {
if constexpr (std::is_same_v<TIss, Issue>)
{
auto const limit = creditLimit(view, to, issuer, currency);
if (-owed >= limit || amount > (limit + owed))
return tecNO_LINE;
auto const& currency = issue.currency;
auto const owed = creditBalance(view, to, issuer, currency);
if (owed <= beast::zero)
{
auto const limit = creditLimit(view, to, issuer, currency);
if (-owed >= limit || amount > (limit + owed))
return tecNO_LINE;
}
}
return tesSUCCESS;
},
[](MPTIssue const&) -> TER { return tesSUCCESS; });
amount.asset().value());
}
[[nodiscard]] TER

View File

@@ -1,11 +1,9 @@
#include <xrpl/ledger/helpers/AMMHelpers.h>
//
#include <xrpl/ledger/View.h>
namespace xrpl {
STAmount
ammLPTokens(STAmount const& asset1, STAmount const& asset2, Asset const& lptIssue)
ammLPTokens(STAmount const& asset1, STAmount const& asset2, Issue const& lptIssue)
{
// AMM invariant: sqrt(asset1 * asset2) >= LPTokensBalance
auto const rounding = isFeatureEnabled(fixAMMv1_3) ? Number::downward : Number::getround();
@@ -34,7 +32,7 @@ lpTokensOut(
if (!isFeatureEnabled(fixAMMv1_3))
{
auto const t = lptAMMBalance * (r - c) / (1 + c);
return toSTAmount(lptAMMBalance.asset(), t);
return toSTAmount(lptAMMBalance.issue(), t);
}
// minimize tokens out
@@ -70,7 +68,7 @@ ammAssetIn(
auto const c = d * d - f2 * f2;
if (!isFeatureEnabled(fixAMMv1_3))
{
return toSTAmount(asset1Balance.asset(), asset1Balance * solveQuadraticEq(a, b, c));
return toSTAmount(asset1Balance.issue(), asset1Balance * solveQuadraticEq(a, b, c));
}
// maximize deposit
@@ -95,7 +93,7 @@ lpTokensIn(
if (!isFeatureEnabled(fixAMMv1_3))
{
auto const t = lptAMMBalance * (c - root2(c * c - 4 * fr)) / 2;
return toSTAmount(lptAMMBalance.asset(), t);
return toSTAmount(lptAMMBalance.issue(), t);
}
// maximize tokens in
@@ -125,7 +123,7 @@ ammAssetOut(
if (!isFeatureEnabled(fixAMMv1_3))
{
auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
return toSTAmount(assetBalance.asset(), b);
return toSTAmount(assetBalance.issue(), b);
}
// minimize withdraw
@@ -185,8 +183,8 @@ adjustAmountsByLPTokens(
if (amount2)
{
Number const fr = lpTokensActual / lpTokens;
auto const amountActual = toSTAmount(amount.asset(), fr * amount);
auto const amount2Actual = toSTAmount(amount2->asset(), fr * *amount2);
auto const amountActual = toSTAmount(amount.issue(), fr * amount);
auto const amount2Actual = toSTAmount(amount2->issue(), fr * *amount2);
if (!ammRoundingEnabled)
{
return std::make_tuple(
@@ -255,7 +253,7 @@ multiply(STAmount const& amount, Number const& frac, Number::rounding_mode rm)
{
NumberRoundModeGuard const g(rm);
auto const t = amount * frac;
return toSTAmount(amount.asset(), t, rm);
return toSTAmount(amount.issue(), t, rm);
}
STAmount
@@ -267,13 +265,13 @@ getRoundedAsset(
IsDeposit isDeposit)
{
if (!rules.enabled(fixAMMv1_3))
return toSTAmount(balance.asset(), noRoundCb());
return toSTAmount(balance.issue(), noRoundCb());
auto const rm = detail::getAssetRounding(isDeposit);
if (isDeposit == IsDeposit::Yes)
return multiply(balance, productCb(), rm);
NumberRoundModeGuard const g(rm);
return toSTAmount(balance.asset(), productCb(), rm);
return toSTAmount(balance.issue(), productCb(), rm);
}
STAmount
@@ -284,7 +282,7 @@ getRoundedLPTokens(
IsDeposit isDeposit)
{
if (!rules.enabled(fixAMMv1_3))
return toSTAmount(balance.asset(), balance * frac);
return toSTAmount(balance.issue(), balance * frac);
auto const rm = detail::getLPTokenRounding(isDeposit);
auto const tokens = multiply(balance, frac, rm);
@@ -300,14 +298,14 @@ getRoundedLPTokens(
IsDeposit isDeposit)
{
if (!rules.enabled(fixAMMv1_3))
return toSTAmount(lptAMMBalance.asset(), noRoundCb());
return toSTAmount(lptAMMBalance.issue(), noRoundCb());
auto const tokens = [&] {
auto const rm = detail::getLPTokenRounding(isDeposit);
if (isDeposit == IsDeposit::Yes)
{
NumberRoundModeGuard const g(rm);
return toSTAmount(lptAMMBalance.asset(), productCb(), rm);
return toSTAmount(lptAMMBalance.issue(), productCb(), rm);
}
return multiply(lptAMMBalance, productCb(), rm);
}();
@@ -378,535 +376,4 @@ adjustFracByTokens(
return tokens / lptAMMBalance;
}
std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Asset const& asset1,
Asset const& asset2,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal const j)
{
auto const assetInBalance =
accountHolds(view, ammAccountID, asset1, freezeHandling, authHandling, j);
auto const assetOutBalance =
accountHolds(view, ammAccountID, asset2, freezeHandling, authHandling, j);
return std::make_pair(assetInBalance, assetOutBalance);
}
Expected<std::tuple<STAmount, STAmount, STAmount>, TER>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Asset> const& optAsset1,
std::optional<Asset> const& optAsset2,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal const j)
{
auto const assets = [&]() -> std::optional<std::pair<Asset, Asset>> {
auto const asset1 = ammSle[sfAsset];
auto const asset2 = ammSle[sfAsset2];
if (optAsset1 && optAsset2)
{
if (invalidAMMAssetPair(
*optAsset1, *optAsset2, std::make_optional(std::make_pair(asset1, asset2))))
{
// This error can only be hit if the AMM is corrupted
// LCOV_EXCL_START
JLOG(j.debug()) << "ammHolds: Invalid optAsset1 or optAsset2 " << *optAsset1 << " "
<< *optAsset2;
return std::nullopt;
// LCOV_EXCL_STOP
}
return std::make_optional(std::make_pair(*optAsset1, *optAsset2));
}
auto const singleAsset = [&asset1, &asset2, &j](
Asset checkIssue,
char const* label) -> std::optional<std::pair<Asset, Asset>> {
if (checkIssue == asset1)
{
return std::make_optional(std::make_pair(asset1, asset2));
}
if (checkIssue == asset2)
{
return std::make_optional(std::make_pair(asset2, asset1));
}
// Unreachable unless AMM corrupted.
// LCOV_EXCL_START
JLOG(j.debug()) << "ammHolds: Invalid " << label << " " << checkIssue;
return std::nullopt;
// LCOV_EXCL_STOP
};
if (optAsset1)
{
return singleAsset(*optAsset1, "optAsset1");
}
if (optAsset2)
{
// Cannot have Amount2 without Amount.
return singleAsset(*optAsset2, "optAsset2"); // LCOV_EXCL_LINE
}
return std::make_optional(std::make_pair(asset1, asset2));
}();
if (!assets)
return Unexpected(tecAMM_INVALID_TOKENS);
auto const [amount1, amount2] = ammPoolHolds(
view,
ammSle.getAccountID(sfAccount),
assets->first,
assets->second,
freezeHandling,
authHandling,
j);
return std::make_tuple(amount1, amount2, ammSle[sfLPTokenBalance]);
}
STAmount
ammLPHolds(
ReadView const& view,
Asset const& asset1,
Asset const& asset2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j)
{
// This function looks similar to `accountHolds`. However, it only checks if
// a LPToken holder has enough balance. On the other hand, `accountHolds`
// checks if the underlying assets of LPToken are frozen with the
// fixFrozenLPTokenTransfer amendment
auto const currency = ammLPTCurrency(asset1, asset2);
STAmount amount;
auto const sle = view.read(keylet::line(lpAccount, ammAccount, currency));
if (!sle)
{
amount.clear(Issue{currency, ammAccount});
JLOG(j.trace()) << "ammLPHolds: no SLE "
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
else if (isFrozen(view, lpAccount, currency, ammAccount))
{
amount.clear(Issue{currency, ammAccount});
JLOG(j.trace()) << "ammLPHolds: frozen currency "
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
else
{
amount = sle->getFieldAmount(sfBalance);
if (lpAccount > ammAccount)
{
// Put balance in account terms.
amount.negate();
}
amount.get<Issue>().account = ammAccount;
JLOG(j.trace()) << "ammLPHolds:"
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
return view.balanceHookIOU(lpAccount, ammAccount, amount);
}
STAmount
ammLPHolds(
ReadView const& view,
SLE const& ammSle,
AccountID const& lpAccount,
beast::Journal const j)
{
return ammLPHolds(view, ammSle[sfAsset], ammSle[sfAsset2], ammSle[sfAccount], lpAccount, j);
}
std::uint16_t
getTradingFee(ReadView const& view, SLE const& ammSle, AccountID const& account)
{
using namespace std::chrono;
XRPL_ASSERT(
!view.rules().enabled(fixInnerObjTemplate) || ammSle.isFieldPresent(sfAuctionSlot),
"xrpl::getTradingFee : auction present");
if (ammSle.isFieldPresent(sfAuctionSlot))
{
auto const& auctionSlot = safe_downcast<STObject const&>(ammSle.peekAtField(sfAuctionSlot));
// Not expired
if (auto const expiration = auctionSlot[~sfExpiration];
duration_cast<seconds>(view.header().parentCloseTime.time_since_epoch()).count() <
expiration)
{
if (auctionSlot[~sfAccount] == account)
return auctionSlot[sfDiscountedFee];
if (auctionSlot.isFieldPresent(sfAuthAccounts))
{
for (auto const& acct : auctionSlot.getFieldArray(sfAuthAccounts))
{
if (acct[~sfAccount] == account)
return auctionSlot[sfDiscountedFee];
}
}
}
}
return ammSle[sfTradingFee];
}
STAmount
ammAccountHolds(ReadView const& view, AccountID const& ammAccountID, Asset const& asset)
{
// Get the actual AMM balance without factoring in the balance hook
return asset.visit(
[&](MPTIssue const& issue) {
if (auto const sle = view.read(keylet::mptoken(issue, ammAccountID));
sle && !isFrozen(view, ammAccountID, issue))
return STAmount{issue, (*sle)[sfMPTAmount]};
return STAmount{asset};
},
[&](Issue const& issue) {
if (isXRP(issue))
{
if (auto const sle = view.read(keylet::account(ammAccountID)))
return (*sle)[sfBalance];
}
else if (
auto const sle =
view.read(keylet::line(ammAccountID, issue.account, issue.currency));
sle && !isFrozen(view, ammAccountID, issue.currency, issue.account))
{
STAmount amount = (*sle)[sfBalance];
if (ammAccountID > issue.account)
amount.negate();
amount.get<Issue>().account = issue.account;
return amount;
}
return STAmount{asset};
});
}
static TER
deleteAMMTrustLines(
Sandbox& sb,
AccountID const& ammAccountID,
std::uint16_t maxTrustlinesToDelete,
beast::Journal j)
{
return cleanupOnAccountDelete(
sb,
keylet::ownerDir(ammAccountID),
[&](LedgerEntryType nodeType,
uint256 const&,
std::shared_ptr<SLE>& sleItem) -> std::pair<TER, SkipEntry> {
// Skip AMM and MPToken
if (nodeType == ltAMM || nodeType == ltMPTOKEN)
return {tesSUCCESS, SkipEntry::Yes};
if (nodeType == ltRIPPLE_STATE)
{
// Trustlines must have zero balance
if (sleItem->getFieldAmount(sfBalance) != beast::zero)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMObjects: deleting trustline with "
"non-zero balance.";
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
}
return {deleteAMMTrustLine(sb, sleItem, ammAccountID, j), SkipEntry::No};
}
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMObjects: deleting non-trustline or non-MPT " << nodeType;
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
},
j,
maxTrustlinesToDelete);
}
static TER
deleteAMMMPTokens(Sandbox& sb, AccountID const& ammAccountID, beast::Journal j)
{
return cleanupOnAccountDelete(
sb,
keylet::ownerDir(ammAccountID),
[&](LedgerEntryType nodeType,
uint256 const&,
std::shared_ptr<SLE>& sleItem) -> std::pair<TER, SkipEntry> {
// Skip AMM
if (nodeType == ltAMM)
return {tesSUCCESS, SkipEntry::Yes};
if (nodeType == ltMPTOKEN)
{
// MPT must have zero balance
if (sleItem->getFieldU64(sfMPTAmount) != 0 ||
(*sleItem)[~sfLockedAmount].value_or(0) != 0)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMObjects: deleting MPT with "
"non-zero balance.";
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
}
return {deleteAMMMPToken(sb, sleItem, ammAccountID, j), SkipEntry::No};
}
if (nodeType == ltRIPPLE_STATE)
{
// Trustlines should have been deleted
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMObjects: trustlines should have been deleted";
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
}
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMObjects: deleting non-trustline or non-MPT " << nodeType;
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
},
j,
3); // At most two MPToken plus AMM object
}
TER
deleteAMMAccount(Sandbox& sb, Asset const& asset, Asset const& asset2, beast::Journal j)
{
auto ammSle = sb.peek(keylet::amm(asset, asset2));
if (!ammSle)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: AMM object does not exist " << asset << " " << asset2;
return tecINTERNAL;
// LCOV_EXCL_STOP
}
auto const ammAccountID = (*ammSle)[sfAccount];
auto sleAMMRoot = sb.peek(keylet::account(ammAccountID));
if (!sleAMMRoot)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: AMM account does not exist "
<< to_string(ammAccountID);
return tecINTERNAL;
// LCOV_EXCL_STOP
}
if (auto const ter = deleteAMMTrustLines(sb, ammAccountID, maxDeletableAMMTrustLines, j);
!isTesSuccess(ter))
return ter;
// Delete AMM's MPTokens only if all trustlines are deleted. If trustlines
// are not deleted then AMM can be re-created with Deposit and
// AMM's MPToken(s) must exist.
if (auto const ter = deleteAMMMPTokens(sb, ammAccountID, j); !isTesSuccess(ter))
return ter;
auto const ownerDirKeylet = keylet::ownerDir(ammAccountID);
if (!sb.dirRemove(ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(), false))
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: failed to remove dir link";
return tecINTERNAL;
// LCOV_EXCL_STOP
}
if (sb.exists(ownerDirKeylet) && !sb.emptyDirDelete(ownerDirKeylet))
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: cannot delete root dir node of "
<< toBase58(ammAccountID);
return tecINTERNAL;
// LCOV_EXCL_STOP
}
sb.erase(ammSle);
sb.erase(sleAMMRoot);
return tesSUCCESS;
}
void
initializeFeeAuctionVote(
ApplyView& view,
std::shared_ptr<SLE>& ammSle,
AccountID const& account,
Asset const& lptAsset,
std::uint16_t tfee)
{
auto const& rules = view.rules();
// AMM creator gets the voting slot.
STArray voteSlots;
STObject voteEntry = STObject::makeInnerObject(sfVoteEntry);
if (tfee != 0)
voteEntry.setFieldU16(sfTradingFee, tfee);
voteEntry.setFieldU32(sfVoteWeight, VOTE_WEIGHT_SCALE_FACTOR);
voteEntry.setAccountID(sfAccount, account);
voteSlots.push_back(voteEntry);
ammSle->setFieldArray(sfVoteSlots, voteSlots);
// AMM creator gets the auction slot for free.
// AuctionSlot is created on AMMCreate and updated on AMMDeposit
// when AMM is in an empty state
if (rules.enabled(fixInnerObjTemplate) && !ammSle->isFieldPresent(sfAuctionSlot))
{
STObject auctionSlot = STObject::makeInnerObject(sfAuctionSlot);
ammSle->set(std::move(auctionSlot));
}
STObject& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
auctionSlot.setAccountID(sfAccount, account);
// current + sec in 24h
auto const expiration = std::chrono::duration_cast<std::chrono::seconds>(
view.header().parentCloseTime.time_since_epoch())
.count() +
TOTAL_TIME_SLOT_SECS;
auctionSlot.setFieldU32(sfExpiration, expiration);
auctionSlot.setFieldAmount(sfPrice, STAmount{lptAsset, 0});
// Set the fee
if (tfee != 0)
{
ammSle->setFieldU16(sfTradingFee, tfee);
}
else if (ammSle->isFieldPresent(sfTradingFee))
{
ammSle->makeFieldAbsent(sfTradingFee); // LCOV_EXCL_LINE
}
if (auto const dfee = tfee / AUCTION_SLOT_DISCOUNTED_FEE_FRACTION)
{
auctionSlot.setFieldU16(sfDiscountedFee, dfee);
}
else if (auctionSlot.isFieldPresent(sfDiscountedFee))
{
auctionSlot.makeFieldAbsent(sfDiscountedFee); // LCOV_EXCL_LINE
}
}
Expected<bool, TER>
isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount)
{
// Liquidity Provider (LP) must have one LPToken trustline
std::uint8_t nLPTokenTrustLines = 0;
// AMM account has at most two IOU (pool tokens, not LPToken) trustlines.
// One or both trustlines could be to the LP if LP is the issuer,
// or a different account if LP is not an issuer. For instance,
// if AMM has two tokens USD and EUR and LP is not the issuer of the tokens
// then the trustlines are between AMM account and the issuer.
// There is one LPToken trustline for each LP. Only remaining LP has
// exactly one LPToken trustlines and at most two IOU trustline for each
// pool token. One or both tokens could be MPT.
std::uint8_t nIOUTrustLines = 0;
// There are at most two MPT objects, one for each side of the pool.
std::uint8_t nMPT = 0;
// There is only one AMM object
bool hasAMM = false;
// AMM LP has at most three trustlines, at most two MPTs, and only one
// AMM object must exist. If there are more than four objects then
// it's either an error or there are more than one LP. Ten pages should
// be sufficient to include four objects.
std::uint8_t limit = 10;
auto const root = keylet::ownerDir(ammIssue.account);
auto currentIndex = root;
// Iterate over AMM owner directory objects.
while (limit-- >= 1)
{
auto const ownerDir = view.read(currentIndex);
if (!ownerDir)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
for (auto const& key : ownerDir->getFieldV256(sfIndexes))
{
auto const sle = view.read(keylet::child(key));
if (!sle)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
auto const entryType = sle->getFieldU16(sfLedgerEntryType);
// Only one AMM object
if (entryType == ltAMM)
{
if (hasAMM)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
hasAMM = true;
continue;
}
if (entryType == ltMPTOKEN)
{
++nMPT;
continue;
}
if (entryType != ltRIPPLE_STATE)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
auto const lowLimit = sle->getFieldAmount(sfLowLimit);
auto const highLimit = sle->getFieldAmount(sfHighLimit);
auto const isLPTrustline =
lowLimit.getIssuer() == lpAccount || highLimit.getIssuer() == lpAccount;
auto const isLPTokenTrustline =
lowLimit.asset() == ammIssue || highLimit.asset() == ammIssue;
// Liquidity Provider trustline
if (isLPTrustline)
{
// LPToken trustline
if (isLPTokenTrustline)
{
// LP has exactly one LPToken trustline
if (++nLPTokenTrustLines > 1)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
// AMM account has at most two IOU trustlines
else if (++nIOUTrustLines > 2)
{
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
}
// Another Liquidity Provider LPToken trustline
else if (isLPTokenTrustline)
{
return false;
}
// AMM account has at most two IOU trustlines
else if (++nIOUTrustLines > 2)
{
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
}
auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
if (uNodeNext == 0)
{
if (nLPTokenTrustLines != 1 || (nIOUTrustLines == 0 && nMPT == 0) ||
(nIOUTrustLines > 2 || nMPT > 2) || (nIOUTrustLines + nMPT) > 2)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
return true;
}
currentIndex = keylet::page(root, uNodeNext);
}
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
Expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox& sb,
STAmount const& lpTokens,
std::shared_ptr<SLE>& ammSle,
AccountID const& account)
{
auto const res = isOnlyLiquidityProvider(sb, lpTokens.get<Issue>(), account);
if (!res.has_value())
{
return Unexpected<TER>(res.error());
}
if (res.value())
{
if (withinRelativeDistance(
lpTokens, ammSle->getFieldAmount(sfLPTokenBalance), Number{1, -3}))
{
ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
sb.update(ammSle);
}
else
{
return Unexpected<TER>(tecAMM_INVALID_TOKENS);
}
}
return true;
}
} // namespace xrpl

View File

@@ -0,0 +1,462 @@
#include <xrpl/basics/Log.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/ledger/Sandbox.h>
#include <xrpl/ledger/helpers/AMMHelpers.h>
#include <xrpl/ledger/helpers/AMMUtils.h>
#include <xrpl/protocol/AMMCore.h>
#include <xrpl/protocol/STObject.h>
namespace xrpl {
std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Issue const& issue1,
Issue const& issue2,
FreezeHandling freezeHandling,
beast::Journal const j)
{
auto const assetInBalance = accountHolds(view, ammAccountID, issue1, freezeHandling, j);
auto const assetOutBalance = accountHolds(view, ammAccountID, issue2, freezeHandling, j);
return std::make_pair(assetInBalance, assetOutBalance);
}
Expected<std::tuple<STAmount, STAmount, STAmount>, TER>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Issue> const& optIssue1,
std::optional<Issue> const& optIssue2,
FreezeHandling freezeHandling,
beast::Journal const j)
{
auto const issues = [&]() -> std::optional<std::pair<Issue, Issue>> {
auto const issue1 = ammSle[sfAsset].get<Issue>();
auto const issue2 = ammSle[sfAsset2].get<Issue>();
if (optIssue1 && optIssue2)
{
if (invalidAMMAssetPair(
*optIssue1, *optIssue2, std::make_optional(std::make_pair(issue1, issue2))))
{
// This error can only be hit if the AMM is corrupted
// LCOV_EXCL_START
JLOG(j.debug()) << "ammHolds: Invalid optIssue1 or optIssue2 " << *optIssue1 << " "
<< *optIssue2;
return std::nullopt;
// LCOV_EXCL_STOP
}
return std::make_optional(std::make_pair(*optIssue1, *optIssue2));
}
auto const singleIssue = [&issue1, &issue2, &j](
Issue checkIssue,
char const* label) -> std::optional<std::pair<Issue, Issue>> {
if (checkIssue == issue1)
{
return std::make_optional(std::make_pair(issue1, issue2));
}
if (checkIssue == issue2)
{
return std::make_optional(std::make_pair(issue2, issue1));
}
// Unreachable unless AMM corrupted.
// LCOV_EXCL_START
JLOG(j.debug()) << "ammHolds: Invalid " << label << " " << checkIssue;
return std::nullopt;
// LCOV_EXCL_STOP
};
if (optIssue1)
{
return singleIssue(*optIssue1, "optIssue1");
}
if (optIssue2)
{
// Cannot have Amount2 without Amount.
return singleIssue(*optIssue2, "optIssue2"); // LCOV_EXCL_LINE
}
return std::make_optional(std::make_pair(issue1, issue2));
}();
if (!issues)
return Unexpected(tecAMM_INVALID_TOKENS);
auto const [asset1, asset2] = ammPoolHolds(
view, ammSle.getAccountID(sfAccount), issues->first, issues->second, freezeHandling, j);
return std::make_tuple(asset1, asset2, ammSle[sfLPTokenBalance]);
}
STAmount
ammLPHolds(
ReadView const& view,
Currency const& cur1,
Currency const& cur2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j)
{
// This function looks similar to `accountHolds`. However, it only checks if
// a LPToken holder has enough balance. On the other hand, `accountHolds`
// checks if the underlying assets of LPToken are frozen with the
// fixFrozenLPTokenTransfer amendment
auto const currency = ammLPTCurrency(cur1, cur2);
STAmount amount;
auto const sle = view.read(keylet::line(lpAccount, ammAccount, currency));
if (!sle)
{
amount.clear(Issue{currency, ammAccount});
JLOG(j.trace()) << "ammLPHolds: no SLE "
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
else if (isFrozen(view, lpAccount, currency, ammAccount))
{
amount.clear(Issue{currency, ammAccount});
JLOG(j.trace()) << "ammLPHolds: frozen currency "
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
else
{
amount = sle->getFieldAmount(sfBalance);
if (lpAccount > ammAccount)
{
// Put balance in account terms.
amount.negate();
}
amount.setIssuer(ammAccount);
JLOG(j.trace()) << "ammLPHolds:"
<< " lpAccount=" << to_string(lpAccount)
<< " amount=" << amount.getFullText();
}
return view.balanceHook(lpAccount, ammAccount, amount);
}
STAmount
ammLPHolds(
ReadView const& view,
SLE const& ammSle,
AccountID const& lpAccount,
beast::Journal const j)
{
return ammLPHolds(
view,
ammSle[sfAsset].get<Issue>().currency,
ammSle[sfAsset2].get<Issue>().currency,
ammSle[sfAccount],
lpAccount,
j);
}
std::uint16_t
getTradingFee(ReadView const& view, SLE const& ammSle, AccountID const& account)
{
using namespace std::chrono;
XRPL_ASSERT(
!view.rules().enabled(fixInnerObjTemplate) || ammSle.isFieldPresent(sfAuctionSlot),
"xrpl::getTradingFee : auction present");
if (ammSle.isFieldPresent(sfAuctionSlot))
{
auto const& auctionSlot = safe_downcast<STObject const&>(ammSle.peekAtField(sfAuctionSlot));
// Not expired
if (auto const expiration = auctionSlot[~sfExpiration];
duration_cast<seconds>(view.header().parentCloseTime.time_since_epoch()).count() <
expiration)
{
if (auctionSlot[~sfAccount] == account)
return auctionSlot[sfDiscountedFee];
if (auctionSlot.isFieldPresent(sfAuthAccounts))
{
for (auto const& acct : auctionSlot.getFieldArray(sfAuthAccounts))
{
if (acct[~sfAccount] == account)
return auctionSlot[sfDiscountedFee];
}
}
}
}
return ammSle[sfTradingFee];
}
STAmount
ammAccountHolds(ReadView const& view, AccountID const& ammAccountID, Issue const& issue)
{
if (isXRP(issue))
{
if (auto const sle = view.read(keylet::account(ammAccountID)))
return (*sle)[sfBalance];
}
else if (
auto const sle = view.read(keylet::line(ammAccountID, issue.account, issue.currency));
sle && !isFrozen(view, ammAccountID, issue.currency, issue.account))
{
auto amount = (*sle)[sfBalance];
if (ammAccountID > issue.account)
amount.negate();
amount.setIssuer(issue.account);
return amount;
}
return STAmount{issue};
}
static TER
deleteAMMTrustLines(
Sandbox& sb,
AccountID const& ammAccountID,
std::uint16_t maxTrustlinesToDelete,
beast::Journal j)
{
return cleanupOnAccountDelete(
sb,
keylet::ownerDir(ammAccountID),
[&](LedgerEntryType nodeType,
uint256 const&,
std::shared_ptr<SLE>& sleItem) -> std::pair<TER, SkipEntry> {
// Skip AMM
if (nodeType == LedgerEntryType::ltAMM)
return {tesSUCCESS, SkipEntry::Yes};
// Should only have the trustlines
if (nodeType != LedgerEntryType::ltRIPPLE_STATE)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMTrustLines: deleting non-trustline " << nodeType;
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
}
// Trustlines must have zero balance
if (sleItem->getFieldAmount(sfBalance) != beast::zero)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMTrustLines: deleting trustline with "
"non-zero balance.";
return {tecINTERNAL, SkipEntry::No};
// LCOV_EXCL_STOP
}
return {deleteAMMTrustLine(sb, sleItem, ammAccountID, j), SkipEntry::No};
},
j,
maxTrustlinesToDelete);
}
TER
deleteAMMAccount(Sandbox& sb, Issue const& asset, Issue const& asset2, beast::Journal j)
{
auto ammSle = sb.peek(keylet::amm(asset, asset2));
if (!ammSle)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: AMM object does not exist " << asset << " " << asset2;
return tecINTERNAL;
// LCOV_EXCL_STOP
}
auto const ammAccountID = (*ammSle)[sfAccount];
auto sleAMMRoot = sb.peek(keylet::account(ammAccountID));
if (!sleAMMRoot)
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: AMM account does not exist "
<< to_string(ammAccountID);
return tecINTERNAL;
// LCOV_EXCL_STOP
}
if (auto const ter = deleteAMMTrustLines(sb, ammAccountID, maxDeletableAMMTrustLines, j);
!isTesSuccess(ter))
return ter;
auto const ownerDirKeylet = keylet::ownerDir(ammAccountID);
if (!sb.dirRemove(ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(), false))
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: failed to remove dir link";
return tecINTERNAL;
// LCOV_EXCL_STOP
}
if (sb.exists(ownerDirKeylet) && !sb.emptyDirDelete(ownerDirKeylet))
{
// LCOV_EXCL_START
JLOG(j.error()) << "deleteAMMAccount: cannot delete root dir node of "
<< toBase58(ammAccountID);
return tecINTERNAL;
// LCOV_EXCL_STOP
}
sb.erase(ammSle);
sb.erase(sleAMMRoot);
return tesSUCCESS;
}
void
initializeFeeAuctionVote(
ApplyView& view,
std::shared_ptr<SLE>& ammSle,
AccountID const& account,
Issue const& lptIssue,
std::uint16_t tfee)
{
auto const& rules = view.rules();
// AMM creator gets the voting slot.
STArray voteSlots;
STObject voteEntry = STObject::makeInnerObject(sfVoteEntry);
if (tfee != 0)
voteEntry.setFieldU16(sfTradingFee, tfee);
voteEntry.setFieldU32(sfVoteWeight, VOTE_WEIGHT_SCALE_FACTOR);
voteEntry.setAccountID(sfAccount, account);
voteSlots.push_back(voteEntry);
ammSle->setFieldArray(sfVoteSlots, voteSlots);
// AMM creator gets the auction slot for free.
// AuctionSlot is created on AMMCreate and updated on AMMDeposit
// when AMM is in an empty state
if (rules.enabled(fixInnerObjTemplate) && !ammSle->isFieldPresent(sfAuctionSlot))
{
STObject auctionSlot = STObject::makeInnerObject(sfAuctionSlot);
ammSle->set(std::move(auctionSlot));
}
STObject& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
auctionSlot.setAccountID(sfAccount, account);
// current + sec in 24h
auto const expiration = std::chrono::duration_cast<std::chrono::seconds>(
view.header().parentCloseTime.time_since_epoch())
.count() +
TOTAL_TIME_SLOT_SECS;
auctionSlot.setFieldU32(sfExpiration, expiration);
auctionSlot.setFieldAmount(sfPrice, STAmount{lptIssue, 0});
// Set the fee
if (tfee != 0)
{
ammSle->setFieldU16(sfTradingFee, tfee);
}
else if (ammSle->isFieldPresent(sfTradingFee))
{
ammSle->makeFieldAbsent(sfTradingFee); // LCOV_EXCL_LINE
}
if (auto const dfee = tfee / AUCTION_SLOT_DISCOUNTED_FEE_FRACTION)
{
auctionSlot.setFieldU16(sfDiscountedFee, dfee);
}
else if (auctionSlot.isFieldPresent(sfDiscountedFee))
{
auctionSlot.makeFieldAbsent(sfDiscountedFee); // LCOV_EXCL_LINE
}
}
Expected<bool, TER>
isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount)
{
// Liquidity Provider (LP) must have one LPToken trustline
std::uint8_t nLPTokenTrustLines = 0;
// There are at most two IOU trustlines. One or both could be to the LP
// if LP is the issuer, or a different account if LP is not an issuer.
// For instance, if AMM has two tokens USD and EUR and LP is not the issuer
// of the tokens then the trustlines are between AMM account and the issuer.
std::uint8_t nIOUTrustLines = 0;
// There is only one AMM object
bool hasAMM = false;
// AMM LP has at most three trustlines and only one AMM object must exist.
// If there are more than five objects then it's either an error or
// there are more than one LP. Ten pages should be sufficient to include
// five objects.
std::uint8_t limit = 10;
auto const root = keylet::ownerDir(ammIssue.account);
auto currentIndex = root;
// Iterate over AMM owner directory objects.
while (limit-- >= 1)
{
auto const ownerDir = view.read(currentIndex);
if (!ownerDir)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
for (auto const& key : ownerDir->getFieldV256(sfIndexes))
{
auto const sle = view.read(keylet::child(key));
if (!sle)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
// Only one AMM object
if (sle->getFieldU16(sfLedgerEntryType) == ltAMM)
{
if (hasAMM)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
hasAMM = true;
continue;
}
if (sle->getFieldU16(sfLedgerEntryType) != ltRIPPLE_STATE)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
auto const lowLimit = sle->getFieldAmount(sfLowLimit);
auto const highLimit = sle->getFieldAmount(sfHighLimit);
auto const isLPTrustline =
lowLimit.getIssuer() == lpAccount || highLimit.getIssuer() == lpAccount;
auto const isLPTokenTrustline =
lowLimit.issue() == ammIssue || highLimit.issue() == ammIssue;
// Liquidity Provider trustline
if (isLPTrustline)
{
// LPToken trustline
if (isLPTokenTrustline)
{
if (++nLPTokenTrustLines > 1)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
else if (++nIOUTrustLines > 2)
{
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
}
// Another Liquidity Provider LPToken trustline
else if (isLPTokenTrustline)
{
return false;
}
else if (++nIOUTrustLines > 2)
{
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
}
auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
if (uNodeNext == 0)
{
if (nLPTokenTrustLines != 1 || nIOUTrustLines == 0 || nIOUTrustLines > 2)
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
return true;
}
currentIndex = keylet::page(root, uNodeNext);
}
return Unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}
Expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox& sb,
STAmount const& lpTokens,
std::shared_ptr<SLE>& ammSle,
AccountID const& account)
{
auto const res = isOnlyLiquidityProvider(sb, lpTokens.issue(), account);
if (!res.has_value())
{
return Unexpected<TER>(res.error());
}
if (res.value())
{
if (withinRelativeDistance(
lpTokens, ammSle->getFieldAmount(sfLPTokenBalance), Number{1, -3}))
{
ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
sb.update(ammSle);
}
else
{
return Unexpected<TER>(tecAMM_INVALID_TOKENS);
}
}
return true;
}
} // namespace xrpl

View File

@@ -8,6 +8,7 @@
#include <algorithm>
#include <limits>
#include <stdexcept>
namespace xrpl {
@@ -80,7 +81,7 @@ xrpLiquid(ReadView const& view, AccountID const& id, std::int32_t ownerCountAdj,
auto const fullBalance = sle->getFieldAmount(sfBalance);
auto const balance = view.balanceHookIOU(id, xrpAccount(), fullBalance);
auto const balance = view.balanceHook(id, xrpAccount(), fullBalance);
STAmount const amount = (balance < reserve) ? STAmount{0} : balance - reserve;
@@ -153,7 +154,7 @@ getPseudoAccountFields()
if (!ar)
{
// LCOV_EXCL_START
LogicError(
Throw<std::logic_error>(
"xrpl::getPseudoAccountFields : unable to find account root "
"ledger format");
// LCOV_EXCL_STOP

View File

@@ -306,11 +306,18 @@ requireAuth(
return tefINTERNAL; // LCOV_EXCL_LINE
auto const asset = sleVault->at(sfAsset);
if (auto const err = asset.visit(
[&](Issue const& issue) { return requireAuth(view, issue, account, authType); },
[&](MPTIssue const& issue) {
return requireAuth(view, issue, account, authType, depth + 1);
});
if (auto const err = std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return requireAuth(view, issue, account, authType);
}
else
{
return requireAuth(view, issue, account, authType, depth + 1);
}
},
asset.value());
!isTesSuccess(err))
return err;
}
@@ -480,21 +487,6 @@ canTransfer(
return tesSUCCESS;
}
TER
canTrade(ReadView const& view, Asset const& asset)
{
return asset.visit(
[&](Issue const&) -> TER { return tesSUCCESS; },
[&](MPTIssue const& mptIssue) -> TER {
auto const sleIssuance = view.read(keylet::mptIssuance(mptIssue.getMptID()));
if (!sleIssuance)
return tecOBJECT_NOT_FOUND;
if (!sleIssuance->isFlag(lsfMPTCanTrade))
return tecNO_PERMISSION;
return tesSUCCESS;
});
}
TER
lockEscrowMPT(ApplyView& view, AccountID const& sender, STAmount const& amount, beast::Journal j)
{
@@ -778,149 +770,4 @@ createMPToken(
return tesSUCCESS;
}
TER
checkCreateMPT(
xrpl::ApplyView& view,
xrpl::MPTIssue const& mptIssue,
xrpl::AccountID const& holder,
beast::Journal j)
{
if (mptIssue.getIssuer() == holder)
return tesSUCCESS;
auto const mptIssuanceID = keylet::mptIssuance(mptIssue.getMptID());
auto const mptokenID = keylet::mptoken(mptIssuanceID.key, holder);
if (!view.exists(mptokenID))
{
if (auto const err = createMPToken(view, mptIssue.getMptID(), holder, 0);
!isTesSuccess(err))
{
return err;
}
auto const sleAcct = view.peek(keylet::account(holder));
if (!sleAcct)
{
return tecINTERNAL;
}
adjustOwnerCount(view, sleAcct, 1, j);
}
return tesSUCCESS;
}
std::int64_t
maxMPTAmount(SLE const& sleIssuance)
{
return sleIssuance[~sfMaximumAmount].value_or(maxMPTokenAmount);
}
std::int64_t
availableMPTAmount(SLE const& sleIssuance)
{
auto const max = maxMPTAmount(sleIssuance);
auto const outstanding = sleIssuance[sfOutstandingAmount];
return max - outstanding;
}
std::int64_t
availableMPTAmount(ReadView const& view, MPTID const& mptID)
{
auto const sle = view.read(keylet::mptIssuance(mptID));
if (!sle)
Throw<std::runtime_error>(transHuman(tecINTERNAL));
return availableMPTAmount(*sle);
}
bool
isMPTOverflow(
std::int64_t sendAmount,
std::uint64_t outstandingAmount,
std::int64_t maximumAmount,
AllowMPTOverflow allowOverflow)
{
std::uint64_t const limit = (allowOverflow == AllowMPTOverflow::Yes)
? std::numeric_limits<std::uint64_t>::max()
: maximumAmount;
return (sendAmount > maximumAmount || outstandingAmount > (limit - sendAmount));
}
STAmount
issuerFundsToSelfIssue(ReadView const& view, MPTIssue const& issue)
{
STAmount amount{issue};
auto const sle = view.read(keylet::mptIssuance(issue));
if (!sle)
return amount;
auto const available = availableMPTAmount(*sle);
return view.balanceHookSelfIssueMPT(issue, available);
}
void
issuerSelfDebitHookMPT(ApplyView& view, MPTIssue const& issue, std::uint64_t amount)
{
auto const available = availableMPTAmount(view, issue);
view.issuerSelfDebitHookMPT(issue, amount, available);
}
static TER
checkMPTAllowed(ReadView const& view, TxType txType, Asset const& asset, AccountID const& accountID)
{
if (!asset.holds<MPTIssue>())
return tesSUCCESS;
auto const& issuanceID = asset.get<MPTIssue>().getMptID();
auto const validTx = txType == ttAMM_CREATE || txType == ttAMM_DEPOSIT ||
txType == ttAMM_WITHDRAW || txType == ttOFFER_CREATE || txType == ttCHECK_CREATE ||
txType == ttCHECK_CASH || txType == ttPAYMENT;
XRPL_ASSERT(validTx, "xrpl::checkMPTAllowed : all MPT tx or DEX");
if (!validTx)
return tefINTERNAL; // LCOV_EXCL_LINE
auto const& issuer = asset.getIssuer();
if (!view.exists(keylet::account(issuer)))
return tecNO_ISSUER; // LCOV_EXCL_LINE
auto const issuanceKey = keylet::mptIssuance(issuanceID);
auto const issuanceSle = view.read(issuanceKey);
if (!issuanceSle)
return tecOBJECT_NOT_FOUND; // LCOV_EXCL_LINE
auto const flags = issuanceSle->getFlags();
if ((flags & lsfMPTLocked) != 0u)
return tecLOCKED; // LCOV_EXCL_LINE
// Offer crossing and Payment
if ((flags & lsfMPTCanTrade) == 0)
return tecNO_PERMISSION;
if (accountID != issuer)
{
if ((flags & lsfMPTCanTransfer) == 0)
return tecNO_PERMISSION;
auto const mptSle = view.read(keylet::mptoken(issuanceKey.key, accountID));
// Allow to succeed since some tx create MPToken if it doesn't exist.
// Tx's have their own check for missing MPToken.
if (!mptSle)
return tesSUCCESS;
if (mptSle->isFlag(lsfMPTLocked))
return tecLOCKED;
}
return tesSUCCESS;
}
TER
checkMPTTxAllowed(
ReadView const& view,
TxType txType,
Asset const& asset,
AccountID const& accountID)
{
// use isDEXAllowed for payment/offer crossing
XRPL_ASSERT(txType != ttPAYMENT, "xrpl::checkMPTTxAllowed : not payment");
return checkMPTAllowed(view, txType, asset, accountID);
}
} // namespace xrpl

View File

@@ -855,15 +855,15 @@ tokenOfferCreatePreclaim(
if (view.rules().enabled(featureNFTokenMintOffer))
{
if (nftIssuer != amount.getIssuer() &&
!view.read(keylet::line(nftIssuer, amount.get<Issue>())))
!view.read(keylet::line(nftIssuer, amount.issue())))
return tecNO_LINE;
}
else if (!view.exists(keylet::line(nftIssuer, amount.get<Issue>())))
else if (!view.exists(keylet::line(nftIssuer, amount.issue())))
{
return tecNO_LINE;
}
if (isFrozen(view, nftIssuer, amount.get<Issue>().currency, amount.getIssuer()))
if (isFrozen(view, nftIssuer, amount.getCurrency(), amount.getIssuer()))
return tecFROZEN;
}
@@ -876,7 +876,7 @@ tokenOfferCreatePreclaim(
return tefNFTOKEN_IS_NOT_TRANSFERABLE;
}
if (isFrozen(view, acctID, amount.get<Issue>().currency, amount.getIssuer()))
if (isFrozen(view, acctID, amount.getCurrency(), amount.getIssuer()))
return tecFROZEN;
// If this is an offer to buy the token, the account must have the

View File

@@ -33,14 +33,11 @@ creditLimit(
if (sleRippleState)
{
result = sleRippleState->getFieldAmount(account < issuer ? sfLowLimit : sfHighLimit);
result.get<Issue>().account = account;
result.setIssuer(account);
}
XRPL_ASSERT(result.getIssuer() == account, "xrpl::creditLimit : result issuer match");
XRPL_ASSERT(
result.get<Issue>().currency == currency,
"xrpl::creditLimit : result currency "
"match");
XRPL_ASSERT(result.getCurrency() == currency, "xrpl::creditLimit : result currency match");
return result;
}
@@ -66,14 +63,11 @@ creditBalance(
result = sleRippleState->getFieldAmount(sfBalance);
if (account < issuer)
result.negate();
result.get<Issue>().account = account;
result.setIssuer(account);
}
XRPL_ASSERT(result.getIssuer() == account, "xrpl::creditBalance : result issuer match");
XRPL_ASSERT(
result.get<Issue>().currency == currency,
"xrpl::creditBalance : result currency "
"match");
XRPL_ASSERT(result.getCurrency() == currency, "xrpl::creditBalance : result currency match");
return result;
}
@@ -228,7 +222,7 @@ trustCreate(
sleRippleState->setFieldAmount(bSetHigh ? sfHighLimit : sfLowLimit, saLimit);
sleRippleState->setFieldAmount(
bSetHigh ? sfLowLimit : sfHighLimit,
STAmount(Issue{saBalance.get<Issue>().currency, bSetDst ? uSrcAccountID : uDstAccountID}));
STAmount(Issue{saBalance.getCurrency(), bSetDst ? uSrcAccountID : uDstAccountID}));
if (uQualityIn != 0u)
sleRippleState->setFieldU32(bSetHigh ? sfHighQualityIn : sfLowQualityIn, uQualityIn);
@@ -267,7 +261,7 @@ trustCreate(
// ONLY: Create ripple balance.
sleRippleState->setFieldAmount(sfBalance, bSetHigh ? -saBalance : saBalance);
view.creditHookIOU(uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
view.creditHook(uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
return tesSUCCESS;
}
@@ -373,7 +367,7 @@ issueIOU(
"xrpl::issueIOU : neither account nor issuer is XRP");
// Consistency check
XRPL_ASSERT(issue == amount.get<Issue>(), "xrpl::issueIOU : matching issue");
XRPL_ASSERT(issue == amount.issue(), "xrpl::issueIOU : matching issue");
// Can't send to self!
XRPL_ASSERT(issue.account != account, "xrpl::issueIOU : not issuer account");
@@ -398,7 +392,7 @@ issueIOU(
auto const must_delete = updateTrustLine(
view, state, bSenderHigh, issue.account, start_balance, final_balance, j);
view.creditHookIOU(issue.account, account, amount, start_balance);
view.creditHook(issue.account, account, amount, start_balance);
if (bSenderHigh)
final_balance.negate();
@@ -428,7 +422,7 @@ issueIOU(
STAmount const limit(Issue{issue.currency, account});
STAmount final_balance = amount;
final_balance.get<Issue>().account = noAccount();
final_balance.setIssuer(noAccount());
auto const receiverAccount = view.peek(keylet::account(account));
if (!receiverAccount)
@@ -467,7 +461,7 @@ redeemIOU(
"xrpl::redeemIOU : neither account nor issuer is XRP");
// Consistency check
XRPL_ASSERT(issue == amount.get<Issue>(), "xrpl::redeemIOU : matching issue");
XRPL_ASSERT(issue == amount.issue(), "xrpl::redeemIOU : matching issue");
// Can't send to self!
XRPL_ASSERT(issue.account != account, "xrpl::redeemIOU : not issuer account");
@@ -490,7 +484,7 @@ redeemIOU(
auto const must_delete =
updateTrustLine(view, state, bSenderHigh, account, start_balance, final_balance, j);
view.creditHookIOU(account, issue.account, amount, start_balance);
view.creditHook(account, issue.account, amount, start_balance);
if (bSenderHigh)
final_balance.negate();
@@ -763,20 +757,4 @@ deleteAMMTrustLine(
return tesSUCCESS;
}
TER
deleteAMMMPToken(
ApplyView& view,
std::shared_ptr<SLE> sleMpt,
AccountID const& ammAccountID,
beast::Journal j)
{
if (!view.dirRemove(
keylet::ownerDir(ammAccountID), (*sleMpt)[sfOwnerNode], sleMpt->key(), false))
return tefBAD_LEDGER; // LCOV_EXCL_LINE
view.erase(sleMpt);
return tesSUCCESS;
}
} // namespace xrpl

View File

@@ -23,8 +23,8 @@ bool
isLPTokenFrozen(
ReadView const& view,
AccountID const& account,
Asset const& asset,
Asset const& asset2);
Issue const& asset,
Issue const& asset2);
//------------------------------------------------------------------------------
//
@@ -35,9 +35,18 @@ isLPTokenFrozen(
bool
isGlobalFrozen(ReadView const& view, Asset const& asset)
{
return asset.visit(
[&](Issue const& issue) { return isGlobalFrozen(view, issue.getIssuer()); },
[&](MPTIssue const& issue) { return isGlobalFrozen(view, issue); });
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return isGlobalFrozen(view, issue.getIssuer());
}
else
{
return isGlobalFrozen(view, issue);
}
},
asset.value());
}
bool
@@ -94,9 +103,18 @@ isAnyFrozen(
Asset const& asset,
int depth)
{
return asset.visit(
[&](Issue const& issue) { return isAnyFrozen(view, accounts, issue); },
[&](MPTIssue const& issue) { return isAnyFrozen(view, accounts, issue, depth); });
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return isAnyFrozen(view, accounts, issue);
}
else
{
return isAnyFrozen(view, accounts, issue, depth);
}
},
asset.value());
}
bool
@@ -172,7 +190,11 @@ getLineIfUsable(
auto const sleAmm = view.read(keylet::amm((*sleIssuer)[sfAMMID]));
if (!sleAmm ||
isLPTokenFrozen(view, account, (*sleAmm)[sfAsset], (*sleAmm)[sfAsset2]))
isLPTokenFrozen(
view,
account,
(*sleAmm)[sfAsset].get<Issue>(),
(*sleAmm)[sfAsset2].get<Issue>()))
{
return nullptr;
}
@@ -208,7 +230,7 @@ getTrustLineBalance(
{
amount += sle->getFieldAmount(oppositeField);
}
amount.get<Issue>().account = issuer;
amount.setIssuer(issuer);
}
else
{
@@ -218,7 +240,7 @@ getTrustLineBalance(
JLOG(j.trace()) << "getTrustLineBalance:" << " account=" << to_string(account)
<< " amount=" << amount.getFullText();
return view.balanceHookIOU(account, issuer, amount);
return view.balanceHook(account, issuer, amount);
}
STAmount
@@ -276,9 +298,6 @@ accountHolds(
SpendableHandling includeFullBalance)
{
bool const returnSpendable = (includeFullBalance == shFULL_BALANCE);
STAmount amount{mptIssue};
auto const& issuer = mptIssue.getIssuer();
bool const mptokensV2 = view.rules().enabled(featureMPTokensV2);
if (returnSpendable && account == mptIssue.getIssuer())
{
@@ -288,14 +307,16 @@ accountHolds(
if (!issuance)
{
return amount;
return STAmount{mptIssue};
}
auto const available = availableMPTAmount(*issuance);
if (!mptokensV2)
return STAmount{mptIssue, available};
return view.balanceHookMPT(issuer, mptIssue, available);
return STAmount{
mptIssue,
issuance->at(~sfMaximumAmount).value_or(maxMPTokenAmount) -
issuance->at(sfOutstandingAmount)};
}
STAmount amount;
auto const sleMpt = view.read(keylet::mptoken(mptIssue.getMptID(), account));
if (!sleMpt)
@@ -331,8 +352,6 @@ accountHolds(
}
}
if (view.rules().enabled(featureMPTokensV2))
return view.balanceHookMPT(account, mptIssue, amount.mpt().value());
return amount;
}
@@ -346,14 +365,19 @@ accountHolds(
beast::Journal j,
SpendableHandling includeFullBalance)
{
return asset.visit(
[&](Issue const& issue) {
return accountHolds(view, account, issue, zeroIfFrozen, j, includeFullBalance);
return std::visit(
[&]<ValidIssueType TIss>(TIss const& value) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return accountHolds(view, account, value, zeroIfFrozen, j, includeFullBalance);
}
else if constexpr (std::is_same_v<TIss, MPTIssue>)
{
return accountHolds(
view, account, value, zeroIfFrozen, zeroIfUnauthorized, j, includeFullBalance);
}
},
[&](MPTIssue const& issue) {
return accountHolds(
view, account, issue, zeroIfFrozen, zeroIfUnauthorized, j, includeFullBalance);
});
asset.value());
}
STAmount
@@ -364,38 +388,28 @@ accountFunds(
FreezeHandling freezeHandling,
beast::Journal j)
{
XRPL_ASSERT(saDefault.holds<Issue>(), "xrpl::accountFunds: saDefault holds Issue");
if (!saDefault.native() && saDefault.getIssuer() == id)
return saDefault;
return accountHolds(
view, id, saDefault.get<Issue>().currency, saDefault.getIssuer(), freezeHandling, j);
}
STAmount
accountFunds(
ReadView const& view,
AccountID const& id,
STAmount const& saDefault,
FreezeHandling freezeHandling,
AuthHandling authHandling,
beast::Journal j)
{
return saDefault.asset().visit(
[&](Issue const&) { return accountFunds(view, id, saDefault, freezeHandling, j); },
[&](MPTIssue const&) {
return accountHolds(
view, id, saDefault.asset(), freezeHandling, authHandling, j, shFULL_BALANCE);
});
view, id, saDefault.getCurrency(), saDefault.getIssuer(), freezeHandling, j);
}
Rate
transferRate(ReadView const& view, STAmount const& amount)
{
return amount.asset().visit(
[&](Issue const& issue) { return transferRate(view, issue.getIssuer()); },
[&](MPTIssue const& issue) { return transferRate(view, issue.getMptID()); });
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return transferRate(view, issue.getIssuer());
}
else
{
return transferRate(view, issue.getMptID());
}
},
amount.asset().value());
}
//------------------------------------------------------------------------------
@@ -508,7 +522,7 @@ directSendNoFeeIOU(
beast::Journal j)
{
AccountID const& issuer = saAmount.getIssuer();
Currency const& currency = saAmount.get<Issue>().currency;
Currency const& currency = saAmount.getCurrency();
// Make sure issuer is involved.
XRPL_ASSERT(
@@ -537,7 +551,7 @@ directSendNoFeeIOU(
if (bSenderHigh)
saBalance.negate(); // Put balance in sender terms.
view.creditHookIOU(uSenderID, uReceiverID, saAmount, saBalance);
view.creditHook(uSenderID, uReceiverID, saAmount, saBalance);
STAmount const saBefore = saBalance;
@@ -608,7 +622,7 @@ directSendNoFeeIOU(
STAmount const saReceiverLimit(Issue{currency, uReceiverID});
STAmount saBalance{saAmount};
saBalance.get<Issue>().account = noAccount();
saBalance.setIssuer(noAccount());
JLOG(j.debug()) << "directSendNoFeeIOU: "
"create line: "
@@ -845,7 +859,7 @@ accountSendIOU(
else
{
auto const sndBal = sender->getFieldAmount(sfBalance);
view.creditHookIOU(uSenderID, xrpAccount(), saAmount, sndBal);
view.creditHook(uSenderID, xrpAccount(), saAmount, sndBal);
// Decrement XRP balance.
sender->setFieldAmount(sfBalance, sndBal - saAmount);
@@ -858,7 +872,7 @@ accountSendIOU(
// Increment XRP balance.
auto const rcvBal = receiver->getFieldAmount(sfBalance);
receiver->setFieldAmount(sfBalance, rcvBal + saAmount);
view.creditHookIOU(xrpAccount(), uReceiverID, saAmount, -rcvBal);
view.creditHook(xrpAccount(), uReceiverID, saAmount, -rcvBal);
view.update(receiver);
}
@@ -961,7 +975,7 @@ accountSendMultiIOU(
// Increment XRP balance.
auto const rcvBal = receiver->getFieldAmount(sfBalance);
receiver->setFieldAmount(sfBalance, rcvBal + amount);
view.creditHookIOU(xrpAccount(), receiverID, amount, -rcvBal);
view.creditHook(xrpAccount(), receiverID, amount, -rcvBal);
view.update(receiver);
@@ -989,7 +1003,7 @@ accountSendMultiIOU(
return TER{tecFAILED_PROCESSING};
}
auto const sndBal = sender->getFieldAmount(sfBalance);
view.creditHookIOU(senderID, xrpAccount(), takeFromSender, sndBal);
view.creditHook(senderID, xrpAccount(), takeFromSender, sndBal);
// Decrement XRP balance.
sender->setFieldAmount(sfBalance, sndBal - takeFromSender);
@@ -1023,20 +1037,9 @@ directSendNoFeeMPT(
auto sleIssuance = view.peek(mptID);
if (!sleIssuance)
return tecOBJECT_NOT_FOUND;
auto const maxAmount = maxMPTAmount(*sleIssuance);
auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
auto const available = availableMPTAmount(*sleIssuance);
auto const amt = saAmount.mpt().value();
if (uSenderID == issuer)
{
if (view.rules().enabled(featureMPTokensV2))
{
if (isMPTOverflow(amt, outstanding, maxAmount, AllowMPTOverflow::Yes))
return tecPATH_DRY;
}
(*sleIssuance)[sfOutstandingAmount] += amt;
(*sleIssuance)[sfOutstandingAmount] += saAmount.mpt().value();
view.update(sleIssuance);
}
else
@@ -1044,11 +1047,11 @@ directSendNoFeeMPT(
auto const mptokenID = keylet::mptoken(mptID.key, uSenderID);
if (auto sle = view.peek(mptokenID))
{
auto const senderBalance = sle->getFieldU64(sfMPTAmount);
if (senderBalance < amt)
auto const amt = sle->getFieldU64(sfMPTAmount);
auto const pay = saAmount.mpt().value();
if (amt < pay)
return tecINSUFFICIENT_FUNDS;
view.creditHookMPT(uSenderID, uReceiverID, saAmount, (*sle)[sfMPTAmount], available);
(*sle)[sfMPTAmount] = senderBalance - amt;
(*sle)[sfMPTAmount] = amt - pay;
view.update(sle);
}
else
@@ -1059,9 +1062,11 @@ directSendNoFeeMPT(
if (uReceiverID == issuer)
{
if (outstanding >= amt)
auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
auto const redeem = saAmount.mpt().value();
if (outstanding >= redeem)
{
sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - amt);
sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - redeem);
view.update(sleIssuance);
}
else
@@ -1074,8 +1079,7 @@ directSendNoFeeMPT(
auto const mptokenID = keylet::mptoken(mptID.key, uReceiverID);
if (auto sle = view.peek(mptokenID))
{
view.creditHookMPT(uSenderID, uReceiverID, saAmount, (*sle)[sfMPTAmount], available);
(*sle)[sfMPTAmount] += amt;
(*sle)[sfMPTAmount] += saAmount.mpt().value();
view.update(sle);
}
else
@@ -1095,8 +1099,7 @@ directSendNoLimitMPT(
STAmount const& saAmount,
STAmount& saActual,
beast::Journal j,
WaiveTransferFee waiveFee,
AllowMPTOverflow allowOverflow)
WaiveTransferFee waiveFee)
{
XRPL_ASSERT(uSenderID != uReceiverID, "xrpl::directSendNoLimitMPT : sender is not receiver");
@@ -1114,13 +1117,9 @@ directSendNoLimitMPT(
if (uSenderID == issuer)
{
auto const sendAmount = saAmount.mpt().value();
auto const maxAmount = maxMPTAmount(*sle);
auto const outstanding = sle->getFieldU64(sfOutstandingAmount);
auto const mptokensV2 = view.rules().enabled(featureMPTokensV2);
allowOverflow = (allowOverflow == AllowMPTOverflow::Yes && mptokensV2)
? AllowMPTOverflow::Yes
: AllowMPTOverflow::No;
if (isMPTOverflow(sendAmount, outstanding, maxAmount, allowOverflow))
auto const maximumAmount = sle->at(~sfMaximumAmount).value_or(maxMPTokenAmount);
if (sendAmount > maximumAmount ||
sle->getFieldU64(sfOutstandingAmount) > maximumAmount - sendAmount)
return tecPATH_DRY;
}
@@ -1234,8 +1233,7 @@ directSendNoLimitMultiMPT(
}
// Direct send: redeeming MPTs and/or sending own MPTs.
if (auto const ter = directSendNoFeeMPT(view, senderID, receiverID, amount, j);
!isTesSuccess(ter))
if (auto const ter = directSendNoFeeMPT(view, senderID, receiverID, amount, j))
return ter;
actual += amount;
// Do not add amount to takeFromSender, because directSendNoFeeMPT took it.
@@ -1254,15 +1252,13 @@ directSendNoLimitMultiMPT(
<< to_string(receiverID) << " : deliver=" << amount.getFullText()
<< " cost=" << actualSend.getFullText();
if (auto const ter = directSendNoFeeMPT(view, issuer, receiverID, amount, j);
!isTesSuccess(ter))
return ter;
if (auto const terResult = directSendNoFeeMPT(view, issuer, receiverID, amount, j))
return terResult;
}
if (senderID != issuer && takeFromSender)
{
if (auto const ter = directSendNoFeeMPT(view, senderID, issuer, takeFromSender, j);
!isTesSuccess(ter))
return ter;
if (TER const terResult = directSendNoFeeMPT(view, senderID, issuer, takeFromSender, j))
return terResult;
}
return tesSUCCESS;
@@ -1275,8 +1271,7 @@ accountSendMPT(
AccountID const& uReceiverID,
STAmount const& saAmount,
beast::Journal j,
WaiveTransferFee waiveFee,
AllowMPTOverflow allowOverflow)
WaiveTransferFee waiveFee)
{
XRPL_ASSERT(
saAmount >= beast::zero && saAmount.holds<MPTIssue>(),
@@ -1290,8 +1285,7 @@ accountSendMPT(
STAmount saActual{saAmount.asset()};
return directSendNoLimitMPT(
view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee, allowOverflow);
return directSendNoLimitMPT(view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
}
static TER
@@ -1323,14 +1317,19 @@ directSendNoFee(
bool bCheckIssuer,
beast::Journal j)
{
return saAmount.asset().visit(
[&](Issue const&) {
return directSendNoFeeIOU(view, uSenderID, uReceiverID, saAmount, bCheckIssuer, j);
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return directSendNoFeeIOU(view, uSenderID, uReceiverID, saAmount, bCheckIssuer, j);
}
else
{
XRPL_ASSERT(!bCheckIssuer, "xrpl::directSendNoFee : not checking issuer");
return directSendNoFeeMPT(view, uSenderID, uReceiverID, saAmount, j);
}
},
[&](MPTIssue const&) {
XRPL_ASSERT(!bCheckIssuer, "xrpl::directSendNoFee : not checking issuer");
return directSendNoFeeMPT(view, uSenderID, uReceiverID, saAmount, j);
});
saAmount.asset().value());
}
TER
@@ -1340,17 +1339,20 @@ accountSend(
AccountID const& uReceiverID,
STAmount const& saAmount,
beast::Journal j,
WaiveTransferFee waiveFee,
AllowMPTOverflow allowOverflow)
WaiveTransferFee waiveFee)
{
return saAmount.asset().visit(
[&](Issue const&) {
return accountSendIOU(view, uSenderID, uReceiverID, saAmount, j, waiveFee);
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return accountSendIOU(view, uSenderID, uReceiverID, saAmount, j, waiveFee);
}
else
{
return accountSendMPT(view, uSenderID, uReceiverID, saAmount, j, waiveFee);
}
},
[&](MPTIssue const&) {
return accountSendMPT(
view, uSenderID, uReceiverID, saAmount, j, waiveFee, allowOverflow);
});
saAmount.asset().value());
}
TER
@@ -1364,13 +1366,18 @@ accountSendMulti(
{
XRPL_ASSERT_PARTS(
receivers.size() > 1, "xrpl::accountSendMulti", "multiple recipients provided");
return asset.visit(
[&](Issue const& issue) {
return accountSendMultiIOU(view, senderID, issue, receivers, j, waiveFee);
return std::visit(
[&]<ValidIssueType TIss>(TIss const& issue) {
if constexpr (std::is_same_v<TIss, Issue>)
{
return accountSendMultiIOU(view, senderID, issue, receivers, j, waiveFee);
}
else
{
return accountSendMultiMPT(view, senderID, issue, receivers, j, waiveFee);
}
},
[&](MPTIssue const& issue) {
return accountSendMultiMPT(view, senderID, issue, receivers, j, waiveFee);
});
asset.value());
}
TER

View File

@@ -21,23 +21,12 @@
namespace xrpl {
Currency
ammLPTCurrency(Asset const& asset1, Asset const& asset2)
ammLPTCurrency(Currency const& cur1, Currency const& cur2)
{
// AMM LPToken is 0x03 plus 19 bytes of the hash
std::int32_t constexpr AMMCurrencyCode = 0x03;
auto const& [minA, maxA] = std::minmax(asset1, asset2);
uint256 const hash = std::visit(
[](auto&& issue1, auto&& issue2) {
auto fromIss = []<ValidIssueType T>(T const& issue) {
if constexpr (std::is_same_v<T, Issue>)
return issue.currency;
if constexpr (std::is_same_v<T, MPTIssue>)
return issue.getMptID();
};
return sha512Half(fromIss(issue1), fromIss(issue2));
},
minA.value(),
maxA.value());
auto const [minC, maxC] = std::minmax(cur1, cur2);
auto const hash = sha512Half(minC, maxC);
Currency currency;
*currency.begin() = AMMCurrencyCode;
std::copy(hash.begin(), hash.begin() + currency.size() - 1, currency.begin() + 1);
@@ -45,45 +34,34 @@ ammLPTCurrency(Asset const& asset1, Asset const& asset2)
}
Issue
ammLPTIssue(Asset const& asset1, Asset const& asset2, AccountID const& ammAccountID)
ammLPTIssue(Currency const& cur1, Currency const& cur2, AccountID const& ammAccountID)
{
return Issue(ammLPTCurrency(asset1, asset2), ammAccountID);
return Issue(ammLPTCurrency(cur1, cur2), ammAccountID);
}
NotTEC
invalidAMMAsset(Asset const& asset, std::optional<std::pair<Asset, Asset>> const& pair)
invalidAMMAsset(Issue const& issue, std::optional<std::pair<Issue, Issue>> const& pair)
{
auto const err = asset.visit(
[](MPTIssue const& issue) -> std::optional<NotTEC> {
if (issue.getIssuer() == beast::zero)
return temBAD_MPT;
return std::nullopt;
},
[](Issue const& issue) -> std::optional<NotTEC> {
if (badCurrency() == issue.currency)
return temBAD_CURRENCY;
if (isXRP(issue) && issue.getIssuer().isNonZero())
return temBAD_ISSUER;
return std::nullopt;
});
if (err)
return *err;
if (pair && asset != pair->first && asset != pair->second)
if (badCurrency() == issue.currency)
return temBAD_CURRENCY;
if (isXRP(issue) && issue.account.isNonZero())
return temBAD_ISSUER;
if (pair && issue != pair->first && issue != pair->second)
return temBAD_AMM_TOKENS;
return tesSUCCESS;
}
NotTEC
invalidAMMAssetPair(
Asset const& asset1,
Asset const& asset2,
std::optional<std::pair<Asset, Asset>> const& pair)
Issue const& issue1,
Issue const& issue2,
std::optional<std::pair<Issue, Issue>> const& pair)
{
if (asset1 == asset2)
if (issue1 == issue2)
return temBAD_AMM_TOKENS;
if (auto const res = invalidAMMAsset(asset1, pair))
if (auto const res = invalidAMMAsset(issue1, pair))
return res;
if (auto const res = invalidAMMAsset(asset2, pair))
if (auto const res = invalidAMMAsset(issue2, pair))
return res;
return tesSUCCESS;
}
@@ -91,10 +69,10 @@ invalidAMMAssetPair(
NotTEC
invalidAMMAmount(
STAmount const& amount,
std::optional<std::pair<Asset, Asset>> const& pair,
std::optional<std::pair<Issue, Issue>> const& pair,
bool validZero)
{
if (auto const res = invalidAMMAsset(amount.asset(), pair))
if (auto const res = invalidAMMAsset(amount.issue(), pair))
return res;
if (amount < beast::zero || (!validZero && amount == beast::zero))
return temBAD_AMOUNT;

View File

@@ -62,11 +62,4 @@ assetFromJson(Json::Value const& v)
return mptIssueFromJson(v);
}
std::ostream&
operator<<(std::ostream& os, Asset const& x)
{
std::visit([&]<ValidIssueType TIss>(TIss const& issue) { os << issue; }, x.value());
return os;
}
} // namespace xrpl

View File

@@ -99,36 +99,19 @@ getBookBase(Book const& book)
{
XRPL_ASSERT(isConsistent(book), "xrpl::getBookBase : input is consistent");
auto getIndexHash = [&book]<typename... Args>(Args... args) {
if (book.domain)
return indexHash(std::forward<Args>(args)..., *book.domain);
return indexHash(std::forward<Args>(args)...);
};
auto const index = std::visit(
[&]<ValidIssueType TIn, ValidIssueType TOut>(TIn const& in, TOut const& out) {
if constexpr (std::is_same_v<TIn, Issue> && std::is_same_v<TOut, Issue>)
{
return getIndexHash(
LedgerNameSpace::BOOK_DIR, in.currency, out.currency, in.account, out.account);
}
else if constexpr (std::is_same_v<TIn, Issue> && std::is_same_v<TOut, MPTIssue>)
{
return getIndexHash(
LedgerNameSpace::BOOK_DIR, in.currency, out.getMptID(), in.account);
}
else if constexpr (std::is_same_v<TIn, MPTIssue> && std::is_same_v<TOut, Issue>)
{
return getIndexHash(
LedgerNameSpace::BOOK_DIR, in.getMptID(), out.currency, out.account);
}
else
{
return getIndexHash(LedgerNameSpace::BOOK_DIR, in.getMptID(), out.getMptID());
}
},
book.in.value(),
book.out.value());
auto const index = book.domain ? indexHash(
LedgerNameSpace::BOOK_DIR,
book.in.currency,
book.out.currency,
book.in.account,
book.out.account,
*(book.domain))
: indexHash(
LedgerNameSpace::BOOK_DIR,
book.in.currency,
book.out.currency,
book.in.account,
book.out.account);
// Return with quality 0.
auto k = keylet::quality({ltDIR_NODE, index}, 0);
@@ -418,37 +401,11 @@ nft_sells(uint256 const& id) noexcept
}
Keylet
amm(Asset const& asset1, Asset const& asset2) noexcept
amm(Asset const& issue1, Asset const& issue2) noexcept
{
auto const& [minA, maxA] = std::minmax(asset1, asset2);
return std::visit(
[]<ValidIssueType TIss1, ValidIssueType TIss2>(TIss1 const& issue1, TIss2 const& issue2) {
if constexpr (std::is_same_v<TIss1, Issue> && std::is_same_v<TIss2, Issue>)
{
return amm(indexHash(
LedgerNameSpace::AMM,
issue1.account,
issue1.currency,
issue2.account,
issue2.currency));
}
else if constexpr (std::is_same_v<TIss1, Issue> && std::is_same_v<TIss2, MPTIssue>)
{
return amm(indexHash(
LedgerNameSpace::AMM, issue1.account, issue1.currency, issue2.getMptID()));
}
else if constexpr (std::is_same_v<TIss1, MPTIssue> && std::is_same_v<TIss2, Issue>)
{
return amm(indexHash(
LedgerNameSpace::AMM, issue1.getMptID(), issue2.account, issue2.currency));
}
else if constexpr (std::is_same_v<TIss1, MPTIssue> && std::is_same_v<TIss2, MPTIssue>)
{
return amm(indexHash(LedgerNameSpace::AMM, issue1.getMptID(), issue2.getMptID()));
}
},
minA.value(),
maxA.value());
auto const& [minI, maxI] = std::minmax(issue1.get<Issue>(), issue2.get<Issue>());
return amm(
indexHash(LedgerNameSpace::AMM, minI.account, minI.currency, maxI.account, maxI.currency));
}
Keylet

View File

@@ -2,8 +2,9 @@
#include <xrpl/basics/contract.h>
#include <xrpl/json/json_errors.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/UintTypes.h>
#include <xrpl/protocol/jss.h>
#include <cstdint>
@@ -16,11 +17,6 @@ MPTIssue::MPTIssue(MPTID const& issuanceID) : mptID_(issuanceID)
{
}
MPTIssue::MPTIssue(std::uint32_t sequence, AccountID const& account)
: MPTIssue(xrpl::makeMptID(sequence, account))
{
}
AccountID const&
MPTIssue::getIssuer() const
{
@@ -90,11 +86,4 @@ mptIssueFromJson(Json::Value const& v)
return MPTIssue{id};
}
std::ostream&
operator<<(std::ostream& os, MPTIssue const& x)
{
os << to_string(x);
return os;
}
} // namespace xrpl

View File

@@ -1,19 +0,0 @@
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/PathAsset.h>
namespace xrpl {
std::string
to_string(PathAsset const& asset)
{
return std::visit([&](auto const& issue) { return to_string(issue); }, asset.value());
}
std::ostream&
operator<<(std::ostream& os, PathAsset const& x)
{
os << to_string(x);
return os;
}
} // namespace xrpl

View File

@@ -67,11 +67,6 @@ Permission::Permission()
#pragma pop_macro("PERMISSION")
};
XRPL_ASSERT(
txFeatureMap_.size() == delegableTx_.size(),
"xrpl::Permission : txFeatureMap_ and delegableTx_ must have same "
"size");
for ([[maybe_unused]] auto const& permission : granularPermissionMap_)
{
XRPL_ASSERT(

View File

@@ -90,23 +90,11 @@ getMPTValue(STAmount const& amount)
static bool
areComparable(STAmount const& v1, STAmount const& v2)
{
return std::visit(
[&]<ValidIssueType TIss1, ValidIssueType TIss2>(TIss1 const& issue1, TIss2 const& issue2) {
if constexpr (is_issue_v<TIss1> && is_issue_v<TIss2>)
{
return v1.native() == v2.native() && issue1.currency == issue2.currency;
}
else if constexpr (is_mptissue_v<TIss1> && is_mptissue_v<TIss2>)
{
return issue1 == issue2;
}
else
{
return false;
}
},
v1.asset().value(),
v2.asset().value());
if (v1.holds<Issue>() && v2.holds<Issue>())
return v1.native() == v2.native() && v1.get<Issue>().currency == v2.get<Issue>().currency;
if (v1.holds<MPTIssue>() && v2.holds<MPTIssue>())
return v1.get<MPTIssue>() == v2.get<MPTIssue>();
return false;
}
static_assert(INITIAL_XRP.drops() == STAmount::cMaxNativeN);
@@ -525,35 +513,26 @@ canAdd(STAmount const& a, STAmount const& b)
}
// IOU case (precision check)
auto const ret = std::visit(
[&]<ValidIssueType TIss1, ValidIssueType TIss2>(
TIss1 const&, TIss2 const&) -> std::optional<bool> {
if constexpr (is_issue_v<TIss1> && is_issue_v<TIss2>)
{
static STAmount const one{IOUAmount{1, 0}, noIssue()};
static STAmount const maxLoss{IOUAmount{1, -4}, noIssue()};
STAmount const lhs = divide((a - b) + b, a, noIssue()) - one;
STAmount const rhs = divide((b - a) + a, b, noIssue()) - one;
return ((rhs.negative() ? -rhs : rhs) + (lhs.negative() ? -lhs : lhs)) <= maxLoss;
}
if (a.holds<Issue>() && b.holds<Issue>())
{
static STAmount const one{IOUAmount{1, 0}, noIssue()};
static STAmount const maxLoss{IOUAmount{1, -4}, noIssue()};
STAmount const lhs = divide((a - b) + b, a, noIssue()) - one;
STAmount const rhs = divide((b - a) + a, b, noIssue()) - one;
return ((rhs.negative() ? -rhs : rhs) + (lhs.negative() ? -lhs : lhs)) <= maxLoss;
}
// MPT (overflow & underflow check)
if constexpr (is_mptissue_v<TIss1> && is_mptissue_v<TIss2>)
{
MPTAmount const A = a.mpt();
MPTAmount const B = b.mpt();
return !(
(B > MPTAmount{0} &&
A > MPTAmount{std::numeric_limits<MPTAmount::value_type>::max()} - B) ||
(B < MPTAmount{0} &&
A < MPTAmount{std::numeric_limits<MPTAmount::value_type>::min()} - B));
}
return std::nullopt;
},
a.asset().value(),
b.asset().value());
if (ret)
return *ret;
// MPT (overflow & underflow check)
if (a.holds<MPTIssue>() && b.holds<MPTIssue>())
{
MPTAmount const A = a.mpt();
MPTAmount const B = b.mpt();
return !(
(B > MPTAmount{0} &&
A > MPTAmount{std::numeric_limits<MPTAmount::value_type>::max()} - B) ||
(B < MPTAmount{0} &&
A < MPTAmount{std::numeric_limits<MPTAmount::value_type>::min()} - B));
}
// LCOV_EXCL_START
UNREACHABLE("STAmount::canAdd : unexpected STAmount type");
return false;
@@ -606,36 +585,27 @@ canSubtract(STAmount const& a, STAmount const& b)
}
// IOU case (no underflow)
auto const ret = std::visit(
[&]<ValidIssueType TIss1, ValidIssueType TIss2>(
TIss1 const&, TIss2 const&) -> std::optional<bool> {
if constexpr (is_issue_v<TIss1> && is_issue_v<TIss2>)
{
return true;
}
if (a.holds<Issue>() && b.holds<Issue>())
{
return true;
}
// MPT case (underflow & overflow check)
if constexpr (is_mptissue_v<TIss1> && is_mptissue_v<TIss2>)
{
MPTAmount const A = a.mpt();
MPTAmount const B = b.mpt();
// MPT case (underflow & overflow check)
if (a.holds<MPTIssue>() && b.holds<MPTIssue>())
{
MPTAmount const A = a.mpt();
MPTAmount const B = b.mpt();
// Underflow check
if (B > MPTAmount{0} && A < B)
return false;
// Underflow check
if (B > MPTAmount{0} && A < B)
return false;
// Overflow check
if (B < MPTAmount{0} &&
A > MPTAmount{std::numeric_limits<MPTAmount::value_type>::max()} + B)
return false;
return true;
}
return std::nullopt;
},
a.asset().value(),
b.asset().value());
if (ret)
return *ret;
// Overflow check
if (B < MPTAmount{0} &&
A > MPTAmount{std::numeric_limits<MPTAmount::value_type>::max()} + B)
return false;
return true;
}
// LCOV_EXCL_START
UNREACHABLE("STAmount::canSubtract : unexpected STAmount type");
return false;
@@ -781,49 +751,45 @@ STAmount::getJson(JsonOptions) const
void
STAmount::add(Serializer& s) const
{
mAsset.visit(
[&](MPTIssue const& issue) {
auto u8 = static_cast<unsigned char>(cMPToken >> 56);
if (!mIsNegative)
u8 |= static_cast<unsigned char>(cPositive >> 56);
s.add8(u8);
s.add64(mValue);
s.addBitString(issue.getMptID());
},
[&](Issue const& issue) {
if (native())
{
XRPL_ASSERT(mOffset == 0, "xrpl::STAmount::add : zero offset");
if (native())
{
XRPL_ASSERT(mOffset == 0, "xrpl::STAmount::add : zero offset");
if (!mIsNegative)
{
s.add64(mValue | cPositive);
}
else
{
s.add64(mValue);
}
}
else
{
if (*this == beast::zero)
{
s.add64(cIssuedCurrency);
}
else if (mIsNegative) // 512 = not native
{
s.add64(mValue | (static_cast<std::uint64_t>(mOffset + 512 + 97) << (64 - 10)));
}
else // 256 = positive
{
s.add64(
mValue |
(static_cast<std::uint64_t>(mOffset + 512 + 256 + 97) << (64 - 10)));
}
s.addBitString(issue.currency);
s.addBitString(issue.account);
}
});
if (!mIsNegative)
{
s.add64(mValue | cPositive);
}
else
{
s.add64(mValue);
}
}
else if (mAsset.holds<MPTIssue>())
{
auto u8 = static_cast<unsigned char>(cMPToken >> 56);
if (!mIsNegative)
u8 |= static_cast<unsigned char>(cPositive >> 56);
s.add8(u8);
s.add64(mValue);
s.addBitString(mAsset.get<MPTIssue>().getMptID());
}
else
{
if (*this == beast::zero)
{
s.add64(cIssuedCurrency);
}
else if (mIsNegative)
{ // 512 = not native
s.add64(mValue | (static_cast<std::uint64_t>(mOffset + 512 + 97) << (64 - 10)));
}
else
{ // 256 = positive
s.add64(mValue | (static_cast<std::uint64_t>(mOffset + 512 + 256 + 97) << (64 - 10)));
}
s.addBitString(mAsset.get<Issue>().currency);
s.addBitString(mAsset.get<Issue>().account);
}
}
bool
@@ -1099,7 +1065,7 @@ amountFromJson(SField const& name, Json::Value const& v)
if (isMPT)
{
// sequence (32 bits) + account (160 bits)
MPTID u;
uint192 u;
if (!u.parseHex(currencyOrMPTID.asString()))
Throw<std::runtime_error>("invalid MPTokenIssuanceID");
asset = u;
@@ -1289,7 +1255,7 @@ divide(STAmount const& num, STAmount const& den, Asset const& asset)
int numOffset = num.exponent();
int denOffset = den.exponent();
if (num.integral())
if (num.native() || num.holds<MPTIssue>())
{
while (numVal < STAmount::cMinValue)
{
@@ -1299,7 +1265,7 @@ divide(STAmount const& num, STAmount const& den, Asset const& asset)
}
}
if (den.integral())
if (den.native() || den.holds<MPTIssue>())
{
while (denVal < STAmount::cMinValue)
{
@@ -1364,7 +1330,7 @@ multiply(STAmount const& v1, STAmount const& v2, Asset const& asset)
int offset1 = v1.exponent();
int offset2 = v2.exponent();
if (v1.integral())
if (v1.native() || v1.holds<MPTIssue>())
{
while (value1 < STAmount::cMinValue)
{
@@ -1373,7 +1339,7 @@ multiply(STAmount const& v1, STAmount const& v2, Asset const& asset)
}
}
if (v2.integral())
if (v2.native() || v2.holds<MPTIssue>())
{
while (value2 < STAmount::cMinValue)
{
@@ -1397,7 +1363,7 @@ multiply(STAmount const& v1, STAmount const& v2, Asset const& asset)
// for years, so it is deeply embedded in the behavior of cross-currency
// transactions.
//
// However, in 2022 it was noticed that the rounding characteristics were
// However in 2022 it was noticed that the rounding characteristics were
// surprising. When the code converts from IOU-like to XRP-like there may
// be a fraction of the IOU-like representation that is too small to be
// represented in drops. `canonicalizeRound()` currently does some unusual
@@ -1414,9 +1380,9 @@ multiply(STAmount const& v1, STAmount const& v2, Asset const& asset)
// So an alternative rounding approach was introduced. You'll see that
// alternative below.
static void
canonicalizeRound(bool integral, std::uint64_t& value, int& offset, bool)
canonicalizeRound(bool native, std::uint64_t& value, int& offset, bool)
{
if (integral)
if (native)
{
if (offset < 0)
{
@@ -1453,9 +1419,9 @@ canonicalizeRound(bool integral, std::uint64_t& value, int& offset, bool)
// rounding decisions. canonicalizeRoundStrict() tracks all of the bits in
// the value being rounded.
static void
canonicalizeRoundStrict(bool integral, std::uint64_t& value, int& offset, bool roundUp)
canonicalizeRoundStrict(bool native, std::uint64_t& value, int& offset, bool roundUp)
{
if (integral)
if (native)
{
if (offset < 0)
{
@@ -1546,7 +1512,9 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
if (v1 == beast::zero || v2 == beast::zero)
return {asset};
if (v1.native() && v2.native() && asset.native())
bool const xrp = asset.native();
if (v1.native() && v2.native() && xrp)
{
std::uint64_t const minV = std::min(getSNValue(v1), getSNValue(v2));
std::uint64_t const maxV = std::max(getSNValue(v1), getSNValue(v2));
@@ -1577,7 +1545,7 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
std::uint64_t value1 = v1.mantissa(), value2 = v2.mantissa();
int offset1 = v1.exponent(), offset2 = v2.exponent();
if (v1.integral())
if (v1.native() || v1.holds<MPTIssue>())
{
while (value1 < STAmount::cMinValue)
{
@@ -1586,7 +1554,7 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
}
}
if (v2.integral())
if (v2.native() || v2.holds<MPTIssue>())
{
while (value2 < STAmount::cMinValue)
{
@@ -1602,7 +1570,7 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
// range. Dividing their product by 10^14 maintains the
// precision, by scaling the result to 10^16 to 10^18.
//
// If we're rounding up, we want to round up away
// If the we're rounding up, we want to round up away
// from zero, and if we're rounding down, truncation
// is implicit.
std::uint64_t amount =
@@ -1611,7 +1579,7 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
int offset = offset1 + offset2 + 14;
if (resultNegative != roundUp)
{
CanonicalizeFunc(asset.integral(), amount, offset, roundUp);
CanonicalizeFunc(xrp, amount, offset, roundUp);
}
STAmount result = [&]() {
// If appropriate, tell Number to round down. This gives the desired
@@ -1622,7 +1590,7 @@ mulRoundImpl(STAmount const& v1, STAmount const& v2, Asset const& asset, bool ro
if (roundUp && !resultNegative && !result)
{
if (asset.integral())
if (xrp)
{
// return the smallest value above zero
amount = 1;
@@ -1666,7 +1634,7 @@ divRoundImpl(STAmount const& num, STAmount const& den, Asset const& asset, bool
std::uint64_t numVal = num.mantissa(), denVal = den.mantissa();
int numOffset = num.exponent(), denOffset = den.exponent();
if (num.integral())
if (num.native() || num.holds<MPTIssue>())
{
while (numVal < STAmount::cMinValue)
{
@@ -1675,7 +1643,7 @@ divRoundImpl(STAmount const& num, STAmount const& den, Asset const& asset, bool
}
}
if (den.integral())
if (den.native() || den.holds<MPTIssue>())
{
while (denVal < STAmount::cMinValue)
{
@@ -1700,12 +1668,12 @@ divRoundImpl(STAmount const& num, STAmount const& den, Asset const& asset, bool
int offset = numOffset - denOffset - 17;
if (resultNegative != roundUp)
canonicalizeRound(asset.integral(), amount, offset, roundUp);
canonicalizeRound(asset.native() || asset.holds<MPTIssue>(), amount, offset, roundUp);
STAmount result = [&]() {
// If appropriate, tell Number the rounding mode we are using.
// Note that "roundUp == true" actually means "round away from zero".
// Otherwise, round toward zero.
// Otherwise round toward zero.
using enum Number::rounding_mode;
MightSaveRound const savedRound(roundUp ^ resultNegative ? upward : downward);
return STAmount(asset, amount, offset, resultNegative);
@@ -1713,7 +1681,7 @@ divRoundImpl(STAmount const& num, STAmount const& den, Asset const& asset, bool
if (roundUp && !resultNegative && !result)
{
if (asset.integral())
if (asset.native() || asset.holds<MPTIssue>())
{
// return the smallest value above zero
amount = 1;

View File

@@ -88,19 +88,22 @@ STIssue::getJson(JsonOptions) const
void
STIssue::add(Serializer& s) const
{
asset_.visit(
[&](Issue const& issue) {
s.addBitString(issue.currency);
if (!isXRP(issue.currency))
s.addBitString(issue.account);
},
[&](MPTIssue const& issue) {
s.addBitString(issue.getIssuer());
s.addBitString(noAccount());
std::uint32_t sequence = 0;
memcpy(&sequence, issue.getMptID().data(), sizeof(sequence));
s.add32(sequence);
});
if (holds<Issue>())
{
auto const& issue = asset_.get<Issue>();
s.addBitString(issue.currency);
if (!isXRP(issue.currency))
s.addBitString(issue.account);
}
else
{
auto const& issue = asset_.get<MPTIssue>();
s.addBitString(issue.getIssuer());
s.addBitString(noAccount());
std::uint32_t sequence = 0;
memcpy(&sequence, issue.getMptID().data(), sizeof(sequence));
s.add32(sequence);
}
}
bool
@@ -113,9 +116,7 @@ STIssue::isEquivalent(STBase const& t) const
bool
STIssue::isDefault() const
{
return asset_.visit(
[](Issue const& issue) { return issue == xrpIssue(); },
[](MPTIssue const&) { return false; });
return holds<Issue>() && asset_.get<Issue>() == xrpIssue();
}
STBase*

View File

@@ -749,12 +749,6 @@ STObject::setFieldH128(SField const& field, uint128 const& v)
setFieldUsingSetValue<STUInt128>(field, v);
}
void
STObject::setFieldH192(SField const& field, uint192 const& v)
{
setFieldUsingSetValue<STUInt192>(field, v);
}
void
STObject::setFieldH256(SField const& field, uint256 const& v)
{

View File

@@ -736,7 +736,7 @@ parseLeaf(
std::string const element_name(json_name + "." + ss.str());
// each element in this path has some combination of
// account, asset, or issuer
// account, currency, or issuer
Json::Value pathEl = value[i][j];
@@ -746,22 +746,14 @@ parseLeaf(
return ret;
}
if (pathEl.isMember(jss::currency) && pathEl.isMember(jss::mpt_issuance_id))
{
error = RPC::make_error(rpcINVALID_PARAMS, "Invalid Asset.");
return ret;
}
bool const isMPT = pathEl.isMember(jss::mpt_issuance_id);
auto const assetName = isMPT ? jss::mpt_issuance_id : jss::currency;
Json::Value const& account = pathEl[jss::account];
Json::Value const& asset = pathEl[assetName];
Json::Value const& issuer = pathEl[jss::issuer];
bool hasAsset = false;
Json::Value const& account = pathEl["account"];
Json::Value const& currency = pathEl["currency"];
Json::Value const& issuer = pathEl["issuer"];
bool hasCurrency = false;
AccountID uAccount, uIssuer;
PathAsset uAsset;
Currency uCurrency;
if (!account && !asset && !issuer)
if (!account && !currency && !issuer)
{
error = invalid_data(element_name);
return ret;
@@ -772,7 +764,7 @@ parseLeaf(
// human account id
if (!account.isString())
{
error = string_expected(element_name, jss::account.c_str());
error = string_expected(element_name, "account");
return ret;
}
@@ -783,51 +775,31 @@ parseLeaf(
auto const a = parseBase58<AccountID>(account.asString());
if (!a)
{
error = invalid_data(element_name, jss::account.c_str());
error = invalid_data(element_name, "account");
return ret;
}
uAccount = *a;
}
}
if (asset)
if (currency)
{
// human asset
if (!asset.isString())
// human currency
if (!currency.isString())
{
error = string_expected(element_name, assetName.c_str());
error = string_expected(element_name, "currency");
return ret;
}
hasAsset = true;
hasCurrency = true;
if (isMPT)
if (!uCurrency.parseHex(currency.asString()))
{
MPTID u;
if (!u.parseHex(asset.asString()))
if (!to_currency(uCurrency, currency.asString()))
{
error = invalid_data(element_name, assetName.c_str());
error = invalid_data(element_name, "currency");
return ret;
}
if (getMPTIssuer(u) == beast::zero)
{
error = invalid_data(element_name, jss::account.c_str());
return ret;
}
uAsset = u;
}
else
{
Currency currency;
if (!currency.parseHex(asset.asString()))
{
if (!to_currency(currency, asset.asString()))
{
error = invalid_data(element_name, assetName.c_str());
return ret;
}
}
uAsset = currency;
}
}
@@ -836,7 +808,7 @@ parseLeaf(
// human account id
if (!issuer.isString())
{
error = string_expected(element_name, jss::issuer.c_str());
error = string_expected(element_name, "issuer");
return ret;
}
@@ -845,20 +817,14 @@ parseLeaf(
auto const a = parseBase58<AccountID>(issuer.asString());
if (!a)
{
error = invalid_data(element_name, jss::issuer.c_str());
error = invalid_data(element_name, "issuer");
return ret;
}
uIssuer = *a;
}
if (isMPT && uIssuer != getMPTIssuer(uAsset.get<MPTID>()))
{
error = invalid_data(element_name, jss::issuer.c_str());
return ret;
}
}
p.emplace_back(uAccount, uAsset, uIssuer, hasAsset);
p.emplace_back(uAccount, uCurrency, uIssuer, hasCurrency);
}
tail.push_back(p);

View File

@@ -31,15 +31,8 @@ STPathElement::get_hash(STPathElement const& element)
for (auto const x : element.getAccountID())
hash_account += (hash_account * 257) ^ x;
// Check pathAsset type instead of element's mType
// In some cases mType might be account but the asset
// is still set to either MPT or currency (see Pathfinder::addLink())
element.getPathAsset().visit(
[&](MPTID const& mpt) { hash_currency += beast::uhash<>{}(mpt); },
[&](Currency const& currency) {
for (auto const x : currency)
hash_currency += (hash_currency * 509) ^ x;
});
for (auto const x : element.getCurrency())
hash_currency += (hash_currency * 509) ^ x;
for (auto const x : element.getIssuerID())
hash_issuer += (hash_issuer * 911) ^ x;
@@ -75,30 +68,24 @@ STPathSet::STPathSet(SerialIter& sit, SField const& name) : STBase(name)
}
else
{
auto const hasAccount = (iType & STPathElement::typeAccount) != 0u;
auto const hasCurrency = (iType & STPathElement::typeCurrency) != 0u;
auto const hasIssuer = (iType & STPathElement::typeIssuer) != 0u;
auto const hasMPT = (iType & STPathElement::typeMPT) != 0u;
auto hasAccount = iType & STPathElement::typeAccount;
auto hasCurrency = iType & STPathElement::typeCurrency;
auto hasIssuer = iType & STPathElement::typeIssuer;
AccountID account;
PathAsset asset;
Currency currency;
AccountID issuer;
if (hasAccount)
if (hasAccount != 0)
account = sit.get160();
XRPL_ASSERT(
!(hasCurrency && hasMPT), "xrpl::STPathSet::STPathSet : not has Currency and MPT");
if (hasCurrency)
asset = static_cast<Currency>(sit.get160());
if (hasCurrency != 0)
currency = sit.get160();
if (hasMPT)
asset = sit.get192();
if (hasIssuer)
if (hasIssuer != 0)
issuer = sit.get160();
path.emplace_back(account, asset, issuer, hasCurrency);
path.emplace_back(account, currency, issuer, hasCurrency);
}
}
}
@@ -150,11 +137,11 @@ STPathSet::isDefault() const
}
bool
STPath::hasSeen(AccountID const& account, PathAsset const& asset, AccountID const& issuer) const
STPath::hasSeen(AccountID const& account, Currency const& currency, AccountID const& issuer) const
{
for (auto& p : mPath)
{
if (p.getAccountID() == account && p.getPathAsset() == asset && p.getIssuerID() == issuer)
if (p.getAccountID() == account && p.getCurrency() == currency && p.getIssuerID() == issuer)
return true;
}
@@ -176,16 +163,9 @@ STPath::getJson(JsonOptions) const
if ((iType & STPathElement::typeAccount) != 0u)
elem[jss::account] = to_string(it.getAccountID());
XRPL_ASSERT(
((iType & STPathElement::typeCurrency) == 0u) ||
((iType & STPathElement::typeMPT) == 0u),
"xrpl::STPath::getJson : not type Currency and MPT");
if ((iType & STPathElement::typeCurrency) != 0u)
elem[jss::currency] = to_string(it.getCurrency());
if ((iType & STPathElement::typeMPT) != 0u)
elem[jss::mpt_issuance_id] = to_string(it.getMPTID());
if ((iType & STPathElement::typeIssuer) != 0u)
elem[jss::issuer] = to_string(it.getIssuerID());
@@ -229,16 +209,13 @@ STPathSet::add(Serializer& s) const
s.add8(iType);
if ((iType & STPathElement::typeAccount) != 0u)
if ((iType & STPathElement::typeAccount) != 0)
s.addBitString(speElement.getAccountID());
if ((iType & STPathElement::typeMPT) != 0u)
s.addBitString(speElement.getMPTID());
if ((iType & STPathElement::typeCurrency) != 0u)
if ((iType & STPathElement::typeCurrency) != 0)
s.addBitString(speElement.getCurrency());
if ((iType & STPathElement::typeIssuer) != 0u)
if ((iType & STPathElement::typeIssuer) != 0)
s.addBitString(speElement.getIssuerID());
}

View File

@@ -156,7 +156,6 @@ transResults()
MAKE_ERROR(temBAD_FEE, "Invalid fee, negative or not XRP."),
MAKE_ERROR(temBAD_ISSUER, "Malformed: Bad issuer."),
MAKE_ERROR(temBAD_LIMIT, "Limits must be non-negative."),
MAKE_ERROR(temBAD_MPT, "Malformed: Bad MPT."),
MAKE_ERROR(temBAD_OFFER, "Malformed: Bad offer."),
MAKE_ERROR(temBAD_PATH, "Malformed: Bad path."),
MAKE_ERROR(temBAD_PATH_LOOP, "Malformed: Loop in path."),

View File

@@ -1006,26 +1006,6 @@ removeDeletedTrustLines(
}
}
static void
removeDeletedMPTs(ApplyView& view, std::vector<uint256> const& mpts, beast::Journal viewJ)
{
// There could be at most two MPTs - one for each side of AMM pool
if (mpts.size() > 2)
{
JLOG(viewJ.error()) << "removeDeletedMPTs: deleted mpts exceed 2 " << mpts.size();
return;
}
for (auto const& index : mpts)
{
if (auto const sleState = view.peek({ltMPTOKEN, index}); sleState &&
deleteAMMMPToken(view, sleState, (*sleState)[sfIssuer], viewJ) != tesSUCCESS)
{
JLOG(viewJ.error()) << "removeDeletedMPTs: failed to delete AMM MPT";
}
}
}
/** Reset the context, discarding any changes made and adjust the fee.
@param fee The transaction fee to be charged.
@@ -1164,21 +1144,19 @@ Transactor::operator()()
// when transactions fail with a `tec` code.
std::vector<uint256> removedOffers;
std::vector<uint256> removedTrustLines;
std::vector<uint256> removedMPTs;
std::vector<uint256> expiredNFTokenOffers;
std::vector<uint256> expiredCredentials;
bool const doOffers = ((result == tecOVERSIZE) || (result == tecKILLED));
bool const doLinesOrMPTs = (result == tecINCOMPLETE);
bool const doLines = (result == tecINCOMPLETE);
bool const doNFTokenOffers = (result == tecEXPIRED);
bool const doCredentials = (result == tecEXPIRED);
if (doOffers || doLinesOrMPTs || doNFTokenOffers || doCredentials)
if (doOffers || doLines || doNFTokenOffers || doCredentials)
{
ctx_.visit([doOffers,
&removedOffers,
doLinesOrMPTs,
doLines,
&removedTrustLines,
&removedMPTs,
doNFTokenOffers,
&expiredNFTokenOffers,
doCredentials,
@@ -1200,17 +1178,10 @@ Transactor::operator()()
removedOffers.push_back(index);
}
if (doLinesOrMPTs && before && after)
if (doLines && before && after && (before->getType() == ltRIPPLE_STATE))
{
// Removal of obsolete AMM trust line
if (before->getType() == ltRIPPLE_STATE)
{
removedTrustLines.push_back(index);
}
else if (before->getType() == ltMPTOKEN)
{
removedMPTs.push_back(index);
}
removedTrustLines.push_back(index);
}
if (doNFTokenOffers && before && after &&
@@ -1248,7 +1219,6 @@ Transactor::operator()()
{
removeDeletedTrustLines(
view(), removedTrustLines, ctx_.registry.get().getJournal("View"));
removeDeletedMPTs(view(), removedMPTs, ctx_.registry.get().getJournal("View"));
}
if (result == tecEXPIRED)

View File

@@ -3,6 +3,7 @@
#include <xrpl/basics/Log.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/ledger/helpers/AMMHelpers.h>
#include <xrpl/ledger/helpers/AMMUtils.h>
#include <xrpl/protocol/TxFormats.h>
namespace xrpl {
@@ -127,16 +128,15 @@ ValidAMM::finalizeCreate(
auto const [amount, amount2] = ammPoolHolds(
view,
*ammAccount_,
tx[sfAmount].asset(),
tx[sfAmount2].asset(),
tx[sfAmount].get<Issue>(),
tx[sfAmount2].get<Issue>(),
fhIGNORE_FREEZE,
ahIGNORE_AUTH,
j);
// Create invariant:
// sqrt(amount * amount2) == LPTokens
// all balances are greater than zero
if (!validBalances(amount, amount2, *lptAMMBalanceAfter_, ZeroAllowed::No) ||
ammLPTokens(amount, amount2, lptAMMBalanceAfter_->get<Issue>()) != *lptAMMBalanceAfter_)
ammLPTokens(amount, amount2, lptAMMBalanceAfter_->issue()) != *lptAMMBalanceAfter_)
{
JLOG(j.error()) << "AMMCreate invariant failed: " << amount << " " << amount2 << " "
<< *lptAMMBalanceAfter_;
@@ -188,7 +188,12 @@ ValidAMM::generalInvariant(
beast::Journal const& j) const
{
auto const [amount, amount2] = ammPoolHolds(
view, *ammAccount_, tx[sfAsset], tx[sfAsset2], fhIGNORE_FREEZE, ahIGNORE_AUTH, j);
view,
*ammAccount_,
tx[sfAsset].get<Issue>(),
tx[sfAsset2].get<Issue>(),
fhIGNORE_FREEZE,
j);
// Deposit and Withdrawal invariant:
// sqrt(amount * amount2) >= LPTokens
// all balances are greater than zero

View File

@@ -172,7 +172,7 @@ TransfersNotFrozen::recordBalanceChanges(
STAmount const& balanceChange)
{
auto const balanceChangeSign = balanceChange.signum();
auto const currency = after->at(sfBalance).get<Issue>().currency;
auto const currency = after->at(sfBalance).getCurrency();
// Change from low account's perspective, which is trust line default
recordBalance({currency, after->at(sfHighLimit).getIssuer()}, {after, balanceChangeSign});

View File

@@ -284,29 +284,25 @@ NoZeroEscrow::visitEntry(
}
else
{
return amount.asset().visit(
[&](Issue const& issue) {
// IOU case
if (amount <= beast::zero)
return true;
// IOU case
if (amount.holds<Issue>())
{
if (amount <= beast::zero)
return true;
if (badCurrency() == issue.currency)
return true;
if (badCurrency() == amount.getCurrency())
return true;
}
return false;
}
// MPT case
if (amount.holds<MPTIssue>())
{
if (amount <= beast::zero)
return true;
// MPT case
,
[&](MPTIssue const&) {
if (amount <= beast::zero)
return true;
if (amount.mpt() > MPTAmount{maxMPTokenAmount})
return true; // LCOV_EXCL_LINE
return false;
});
if (amount.mpt() > MPTAmount{maxMPTokenAmount})
return true; // LCOV_EXCL_LINE
}
}
return false;
};
@@ -604,8 +600,8 @@ NoXRPTrustLines::visitEntry(
// checking the issue directly here instead of
// relying on .native() just in case native somehow
// were systematically incorrect
xrpTrustLine_ = after->getFieldAmount(sfLowLimit).asset() == xrpIssue() ||
after->getFieldAmount(sfHighLimit).asset() == xrpIssue();
xrpTrustLine_ = after->getFieldAmount(sfLowLimit).issue() == xrpIssue() ||
after->getFieldAmount(sfHighLimit).issue() == xrpIssue();
}
}
@@ -778,23 +774,17 @@ ValidClawback::finalize(
return false;
}
bool const mptV2Enabled = view.rules().enabled(featureMPTokensV2);
if (trustlinesChanged == 1 || (mptV2Enabled && mptokensChanged == 1))
if (trustlinesChanged == 1)
{
AccountID const issuer = tx.getAccountID(sfAccount);
STAmount const& amount = tx.getFieldAmount(sfAmount);
AccountID const& holder = amount.getIssuer();
STAmount const holderBalance = amount.asset().visit(
[&](Issue const& issue) {
return accountHolds(view, holder, issue.currency, issuer, fhIGNORE_FREEZE, j);
},
[&](MPTIssue const& issue) {
return accountHolds(view, issuer, issue, fhIGNORE_FREEZE, ahIGNORE_AUTH, j);
});
STAmount const holderBalance =
accountHolds(view, holder, amount.getCurrency(), issuer, fhIGNORE_FREEZE, j);
if (holderBalance.signum() < 0)
{
JLOG(j.fatal()) << "Invariant failed: trustline or MPT balance is negative";
JLOG(j.fatal()) << "Invariant failed: trustline balance is negative";
return false;
}
}

View File

@@ -2,7 +2,6 @@
//
#include <xrpl/basics/Log.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/ledger/helpers/MPTokenHelpers.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/MPTIssue.h>
@@ -53,10 +52,9 @@ ValidMPTIssuance::finalize(
ReadView const& view,
beast::Journal const& j) const
{
auto const& rules = view.rules();
bool const mptV2Enabled = rules.enabled(featureMPTokensV2);
if (isTesSuccess(result) || (mptV2Enabled && result == tecINCOMPLETE))
if (isTesSuccess(result))
{
auto const& rules = view.rules();
[[maybe_unused]]
bool const enforceCreatedByIssuer =
rules.enabled(featureSingleAssetVault) || rules.enabled(featureLendingProtocol);
@@ -114,12 +112,12 @@ ValidMPTIssuance::finalize(
return mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 1;
}
bool const lendingProtocolEnabled = rules.enabled(featureLendingProtocol);
bool const lendingProtocolEnabled = view.rules().enabled(featureLendingProtocol);
// ttESCROW_FINISH may authorize an MPT, but it can't have the
// mayAuthorizeMPT privilege, because that may cause
// non-amendment-gated side effects.
bool const enforceEscrowFinish = (txnType == ttESCROW_FINISH) &&
(rules.enabled(featureSingleAssetVault) || lendingProtocolEnabled);
(view.rules().enabled(featureSingleAssetVault) || lendingProtocolEnabled);
if (hasPrivilege(tx, mustAuthorizeMPT | mayAuthorizeMPT) || enforceEscrowFinish)
{
bool const submittedByIssuer = tx.isFieldPresent(sfHolder);
@@ -136,42 +134,19 @@ ValidMPTIssuance::finalize(
"succeeded but deleted issuances";
return false;
}
if (mptV2Enabled && hasPrivilege(tx, mayAuthorizeMPT) &&
(txnType == ttAMM_WITHDRAW || txnType == ttAMM_CLAWBACK))
{
if (submittedByIssuer && txnType == ttAMM_WITHDRAW && mptokensCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"submitted by issuer succeeded "
"but created bad number of mptokens";
return false;
}
// At most one MPToken may be created on withdraw/clawback since:
// - Liquidity Provider must have at least one token in order
// participate in AMM pool liquidity.
// - At most two MPTokens may be deleted if AMM pool, which has exactly
// two tokens, is empty after withdraw/clawback.
if (mptokensCreated_ > 1 || mptokensDeleted_ > 2)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize succeeded "
"but created/deleted bad number of mptokens";
return false;
}
}
else if (lendingProtocolEnabled && (mptokensCreated_ + mptokensDeleted_) > 1)
if (lendingProtocolEnabled && mptokensCreated_ + mptokensDeleted_ > 1)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize succeeded "
"but created/deleted bad number mptokens";
return false;
}
else if (submittedByIssuer && (mptokensCreated_ > 0 || mptokensDeleted_ > 0))
if (submittedByIssuer && (mptokensCreated_ > 0 || mptokensDeleted_ > 0))
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize submitted by issuer "
"succeeded but created/deleted mptokens";
return false;
}
else if (
!submittedByIssuer && hasPrivilege(tx, mustAuthorizeMPT) &&
if (!submittedByIssuer && hasPrivilege(tx, mustAuthorizeMPT) &&
(mptokensCreated_ + mptokensDeleted_ != 1))
{
// if the holder submitted this tx, then a mptoken must be
@@ -183,52 +158,6 @@ ValidMPTIssuance::finalize(
return true;
}
if (hasPrivilege(tx, mayCreateMPT))
{
bool const submittedByIssuer = tx.isFieldPresent(sfHolder);
if (mptIssuancesCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but created MPT issuances";
return false;
}
if (mptIssuancesDeleted_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but deleted issuances";
return false;
}
if (mptokensDeleted_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but deleted MPTokens";
return false;
}
// AMMCreate may auto-create up to two MPT objects:
// - one per asset side in an MPT/MPT AMM, or one in an IOU/MPT AMM.
// CheckCash may auto-create at most one MPT object for the receiver.
if ((txnType == ttAMM_CREATE && mptokensCreated_ > 2) ||
(txnType == ttCHECK_CASH && mptokensCreated_ > 1))
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but created bad number of mptokens";
return false;
}
if (submittedByIssuer)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize submitted by issuer "
"succeeded but created mptokens";
return false;
}
// Offer crossing or payment may consume multiple offers
// where takerPays is MPT amount. If the offer owner doesn't
// own MPT then MPT is created automatically.
return true;
}
if (txnType == ttESCROW_FINISH)
{
// ttESCROW_FINISH may authorize an MPT, but it can't have the
@@ -239,9 +168,8 @@ ValidMPTIssuance::finalize(
return true;
}
if (hasPrivilege(tx, mayDeleteMPT) &&
((txnType == ttAMM_DELETE && mptokensDeleted_ <= 2) || mptokensDeleted_ == 1) &&
mptokensCreated_ == 0 && mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 0)
if (hasPrivilege(tx, mayDeleteMPT) && mptokensDeleted_ == 1 && mptokensCreated_ == 0 &&
mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 0)
return true;
}
@@ -266,107 +194,4 @@ ValidMPTIssuance::finalize(
mptokensDeleted_ == 0;
}
void
ValidMPTPayment::visitEntry(
bool,
std::shared_ptr<SLE const> const& before,
std::shared_ptr<SLE const> const& after)
{
if (overflow_)
return;
auto makeKey = [](SLE const& sle) {
if (sle.getType() == ltMPTOKEN_ISSUANCE)
return makeMptID(sle[sfSequence], sle[sfIssuer]);
return sle[sfMPTokenIssuanceID];
};
auto update = [&](SLE const& sle, Order order) -> bool {
auto const type = sle.getType();
if (type == ltMPTOKEN_ISSUANCE)
{
auto const outstanding = sle[sfOutstandingAmount];
if (outstanding > maxMPTokenAmount)
{
overflow_ = true;
return false;
}
data_[makeKey(sle)].outstanding[order] = outstanding;
}
else if (type == ltMPTOKEN)
{
auto const mptAmt = sle[sfMPTAmount];
auto const lockedAmt = sle[~sfLockedAmount].value_or(0);
if (mptAmt > maxMPTokenAmount || lockedAmt > maxMPTokenAmount ||
lockedAmt > (maxMPTokenAmount - mptAmt))
{
overflow_ = true;
return false;
}
auto const res = static_cast<std::int64_t>(mptAmt + lockedAmt);
// subtract before from after
if (order == Before)
{
data_[makeKey(sle)].mptAmount -= res;
}
else
{
data_[makeKey(sle)].mptAmount += res;
}
}
return true;
};
if (before && !update(*before, Before))
return;
if (after)
{
if (after->getType() == ltMPTOKEN_ISSUANCE)
{
overflow_ = (*after)[sfOutstandingAmount] > maxMPTAmount(*after);
}
if (!update(*after, After))
return;
}
}
bool
ValidMPTPayment::finalize(
STTx const& tx,
TER const result,
XRPAmount const,
ReadView const& view,
beast::Journal const& j)
{
if (isTesSuccess(result))
{
bool const enforce = view.rules().enabled(featureMPTokensV2);
if (overflow_)
{
JLOG(j.fatal()) << "Invariant failed: OutstandingAmount overflow";
return !enforce;
}
auto const signedMax = static_cast<std::int64_t>(maxMPTokenAmount);
for (auto const& [id, data] : data_)
{
(void)id;
bool const addOverflows =
(data.mptAmount > 0 && data.outstanding[Before] > (signedMax - data.mptAmount)) ||
(data.mptAmount < 0 && data.outstanding[Before] < (-signedMax - data.mptAmount));
if (addOverflows ||
data.outstanding[After] != (data.outstanding[Before] + data.mptAmount))
{
JLOG(j.fatal()) << "Invariant failed: invalid OutstandingAmount balance "
<< data.outstanding[Before] << " " << data.outstanding[After] << " "
<< data.mptAmount;
return !enforce;
}
}
}
return true;
}
} // namespace xrpl

View File

@@ -8,15 +8,15 @@ AMMLiquidity<TIn, TOut>::AMMLiquidity(
ReadView const& view,
AccountID const& ammAccountID,
std::uint32_t tradingFee,
Asset const& in,
Asset const& out,
Issue const& in,
Issue const& out,
AMMContext& ammContext,
beast::Journal j)
: ammContext_(ammContext)
, ammAccountID_(ammAccountID)
, tradingFee_(tradingFee)
, assetIn_(in)
, assetOut_(out)
, issueIn_(in)
, issueOut_(out)
, initialBalances_{fetchBalances(view)}
, j_(j)
{
@@ -26,13 +26,13 @@ template <typename TIn, typename TOut>
TAmounts<TIn, TOut>
AMMLiquidity<TIn, TOut>::fetchBalances(ReadView const& view) const
{
auto const amountIn = ammAccountHolds(view, ammAccountID_, assetIn_);
auto const amountOut = ammAccountHolds(view, ammAccountID_, assetOut_);
auto const assetIn = ammAccountHolds(view, ammAccountID_, issueIn_);
auto const assetOut = ammAccountHolds(view, ammAccountID_, issueOut_);
// This should not happen.
if (amountIn < beast::zero || amountOut < beast::zero)
if (assetIn < beast::zero || assetOut < beast::zero)
Throw<std::runtime_error>("AMMLiquidity: invalid balances");
return TAmounts{get<TIn>(amountIn), get<TOut>(amountOut)};
return TAmounts{get<TIn>(assetIn), get<TOut>(assetOut)};
}
template <typename TIn, typename TOut>
@@ -42,7 +42,7 @@ AMMLiquidity<TIn, TOut>::generateFibSeqOffer(TAmounts<TIn, TOut> const& balances
TAmounts<TIn, TOut> cur{};
cur.in = toAmount<TIn>(
getAsset(balances.in),
getIssue(balances.in),
InitialFibSeqPct * initialBalances_.in,
Number::rounding_mode::upward);
cur.out = swapAssetIn(initialBalances_, cur.in, tradingFee_);
@@ -60,7 +60,7 @@ AMMLiquidity<TIn, TOut>::generateFibSeqOffer(TAmounts<TIn, TOut> const& balances
"xrpl::AMMLiquidity::generateFibSeqOffer : maximum iterations");
cur.out = toAmount<TOut>(
getAsset(balances.out),
getIssue(balances.out),
cur.out * fib[ammContext_.curIters() - 1],
Number::rounding_mode::downward);
// swapAssetOut() returns negative in this case
@@ -89,18 +89,14 @@ maxAmount()
{
return STAmount(STAmount::cMaxValue / 2, STAmount::cMaxOffset);
}
else if constexpr (std::is_same_v<T, MPTAmount>)
{
return MPTAmount(maxMPTokenAmount);
}
}
template <typename T>
T
maxOut(T const& out, Asset const& asset)
maxOut(T const& out, Issue const& iss)
{
Number const res = out * Number{99, -2};
return toAmount<T>(asset, res, Number::rounding_mode::downward);
return toAmount<T>(iss, res, Number::rounding_mode::downward);
}
} // namespace
@@ -117,7 +113,7 @@ AMMLiquidity<TIn, TOut>::maxOffer(TAmounts<TIn, TOut> const& balances, Rules con
Quality{balances});
}
auto const out = maxOut<TOut>(balances.out, assetOut());
auto const out = maxOut<TOut>(balances.out, issueOut());
if (out <= TOut{0} || out >= balances.out)
return std::nullopt;
return AMMOffer<TIn, TOut>(
@@ -215,8 +211,8 @@ AMMLiquidity<TIn, TOut>::getOffer(ReadView const& view, std::optional<Quality> c
if (offer->amount().in > beast::zero && offer->amount().out > beast::zero)
{
JLOG(j_.trace()) << "AMMLiquidity::getOffer, created " << to_string(offer->amount().in)
<< "/" << assetIn_ << " " << to_string(offer->amount().out) << "/"
<< assetOut_;
<< "/" << issueIn_ << " " << to_string(offer->amount().out) << "/"
<< issueOut_;
return offer;
}
@@ -229,13 +225,9 @@ AMMLiquidity<TIn, TOut>::getOffer(ReadView const& view, std::optional<Quality> c
return std::nullopt;
}
template class AMMLiquidity<STAmount, STAmount>;
template class AMMLiquidity<IOUAmount, IOUAmount>;
template class AMMLiquidity<XRPAmount, IOUAmount>;
template class AMMLiquidity<IOUAmount, XRPAmount>;
template class AMMLiquidity<MPTAmount, MPTAmount>;
template class AMMLiquidity<XRPAmount, MPTAmount>;
template class AMMLiquidity<MPTAmount, XRPAmount>;
template class AMMLiquidity<MPTAmount, IOUAmount>;
template class AMMLiquidity<IOUAmount, MPTAmount>;
} // namespace xrpl

View File

@@ -4,7 +4,7 @@
namespace xrpl {
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
AMMOffer<TIn, TOut>::AMMOffer(
AMMLiquidity<TIn, TOut> const& ammLiquidity,
TAmounts<TIn, TOut> const& amounts,
@@ -15,35 +15,28 @@ AMMOffer<TIn, TOut>::AMMOffer(
{
}
template <StepAmount TIn, StepAmount TOut>
Asset const&
AMMOffer<TIn, TOut>::assetIn() const
template <typename TIn, typename TOut>
Issue const&
AMMOffer<TIn, TOut>::issueIn() const
{
return ammLiquidity_.assetIn();
return ammLiquidity_.issueIn();
}
template <StepAmount TIn, StepAmount TOut>
Asset const&
AMMOffer<TIn, TOut>::assetOut() const
{
return ammLiquidity_.assetOut();
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
AccountID const&
AMMOffer<TIn, TOut>::owner() const
{
return ammLiquidity_.ammAccount();
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
TAmounts<TIn, TOut> const&
AMMOffer<TIn, TOut>::amount() const
{
return amounts_;
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
void
AMMOffer<TIn, TOut>::consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed)
{
@@ -59,7 +52,7 @@ AMMOffer<TIn, TOut>::consume(ApplyView& view, TAmounts<TIn, TOut> const& consume
ammLiquidity_.context().setAMMUsed();
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
TAmounts<TIn, TOut>
AMMOffer<TIn, TOut>::limitOut(
TAmounts<TIn, TOut> const& offerAmount,
@@ -84,7 +77,7 @@ AMMOffer<TIn, TOut>::limitOut(
return {swapAssetOut(balances_, limit, ammLiquidity_.tradingFee()), limit};
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
TAmounts<TIn, TOut>
AMMOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp)
const
@@ -101,7 +94,7 @@ AMMOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const&
return {limit, swapAssetIn(balances_, limit, ammLiquidity_.tradingFee())};
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
QualityFunction
AMMOffer<TIn, TOut>::getQualityFunc() const
{
@@ -110,7 +103,7 @@ AMMOffer<TIn, TOut>::getQualityFunc() const
return QualityFunction{balances_, ammLiquidity_.tradingFee(), QualityFunction::AMMTag{}};
}
template <StepAmount TIn, StepAmount TOut>
template <typename TIn, typename TOut>
bool
AMMOffer<TIn, TOut>::checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const
{
@@ -140,13 +133,9 @@ AMMOffer<TIn, TOut>::checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::
return false;
}
template class AMMOffer<STAmount, STAmount>;
template class AMMOffer<IOUAmount, IOUAmount>;
template class AMMOffer<XRPAmount, IOUAmount>;
template class AMMOffer<IOUAmount, XRPAmount>;
template class AMMOffer<MPTAmount, MPTAmount>;
template class AMMOffer<XRPAmount, MPTAmount>;
template class AMMOffer<MPTAmount, XRPAmount>;
template class AMMOffer<IOUAmount, MPTAmount>;
template class AMMOffer<MPTAmount, IOUAmount>;
} // namespace xrpl

Some files were not shown because too many files have changed in this diff Show More