Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Introduce MPT support (XLS-33d): #5143

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Builds/levelization/results/loops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Loop: test.jtx test.unit_test
test.unit_test == test.jtx

Loop: xrpl.basics xrpl.json
xrpl.json ~= xrpl.basics
xrpl.json == xrpl.basics

Loop: xrpld.app xrpld.core
xrpld.app > xrpld.core
Expand Down
164 changes: 164 additions & 0 deletions include/xrpl/basics/MPTAmount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2024 Ripple Labs Inc.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
#define RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED

#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/json/json_value.h>

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>

#include <cstdint>
#include <optional>
#include <string>
#include <type_traits>

namespace ripple {

class MPTAmount : private boost::totally_ordered<MPTAmount>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing how similar MPTAmount is to XRPAmount it's a pity that they aren't implemented with common code. I.e. through a base class, or a template or something. Rather than diving down the potential rabbit hole of implementing it, I'm just going to leave this note here for someone else or for future reference.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My opinion:

  • They should be called MPTNumber and XRPNumber because they represent quantities, while STAmount represents a quantity plus an asset / issue / unit (however you want to think of it). These are more like Number, and convert directly to and from it.
  • All of the arithmetic is moving to Number after the switchover anyway. In due time, when we retire that amendment, effectively locking it in permanently, then I think we can remove MPTAmount and XRPAmount.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should IOUAmount change too? I think that Amount better communicates what the value is. I don't think you'd say number when talking about tokens or currencies even if the values don't have associate unit. And XRPAmount doesn't need an issue, it's implicit. Also doesn't seem like this refactoring has to be done in MPT PR. But this is just my opinion. I'll change if everyone thinks Number is better. There are over 300 instances of XRPAmount, MPTAmount, IOUAmount.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a ticket to refactor MPTAmount and XRPAmount to use a common code + renaming.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not asking it to be done here, or at all even. Was just stating my position. My opinion is that IOUAmount should be renamed too (in an ideal world), and my only reason for these renames is that STAmount has quantity + issue, while these {XRP,IOU,MPT}Amounts only have quantity, like Number, which is their common representation. The alternative fix is to rename STAmount to a different suffix, but I think that would be even more disruptive, especially to external projects.

private boost::additive<MPTAmount>,
private boost::equality_comparable<MPTAmount, std::int64_t>,
private boost::additive<MPTAmount, std::int64_t>
{
public:
using value_type = std::int64_t;

protected:
value_type value_;

public:
MPTAmount() = default;
constexpr MPTAmount(MPTAmount const& other) = default;
constexpr MPTAmount&
operator=(MPTAmount const& other) = default;

constexpr explicit MPTAmount(value_type value);

constexpr MPTAmount& operator=(beast::Zero);

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

MPTAmount&
operator-=(MPTAmount const& other);

MPTAmount
operator-() const;

bool
operator==(MPTAmount const& other) const;

bool
operator==(value_type other) const;

bool
operator<(MPTAmount const& other) const;

/** Returns true if the amount is not zero */
explicit constexpr operator bool() const noexcept;

/** Return the sign of the amount */
constexpr int
signum() const noexcept;

/** Returns the underlying value. Code SHOULD NOT call this
function unless the type has been abstracted away,
e.g. in a templated function.
*/
constexpr value_type
value() const;

static MPTAmount
minPositiveAmount();
};

constexpr MPTAmount::MPTAmount(value_type value) : value_(value)
{
}

constexpr MPTAmount& MPTAmount::operator=(beast::Zero)
{
value_ = 0;
return *this;
}

/** Returns true if the amount is not zero */
constexpr MPTAmount::operator bool() const noexcept
{
return value_ != 0;
}

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

/** Returns the underlying value. Code SHOULD NOT call this
function unless the type has been abstracted away,
e.g. in a templated function.
*/
constexpr MPTAmount::value_type
MPTAmount::value() const
{
return value_;
}

inline std::string
to_string(MPTAmount const& amount)
{
return std::to_string(amount.value());
}

inline MPTAmount
mulRatio(
MPTAmount const& amt,
std::uint32_t num,
std::uint32_t den,
bool roundUp)
{
using namespace boost::multiprecision;

if (!den)
Throw<std::runtime_error>("division by zero");

int128_t const amt128(amt.value());
auto const neg = amt.value() < 0;
auto const m = amt128 * num;
auto r = m / den;
if (m % den)
{
if (!neg && roundUp)
r += 1;
if (neg && !roundUp)
r -= 1;
}
if (r > std::numeric_limits<MPTAmount::value_type>::max())
Throw<std::overflow_error>("MPT mulRatio overflow");
return MPTAmount(r.convert_to<MPTAmount::value_type>());
}

} // namespace ripple

#endif // RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
7 changes: 7 additions & 0 deletions include/xrpl/basics/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef RIPPLE_BASICS_NUMBER_H_INCLUDED
#define RIPPLE_BASICS_NUMBER_H_INCLUDED

#include <xrpl/basics/MPTAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <cstdint>
#include <limits>
Expand Down Expand Up @@ -52,6 +53,7 @@ class Number
explicit constexpr Number(rep mantissa, int exponent, unchecked) noexcept;

Number(XRPAmount const& x);
Number(MPTAmount const& x);
Comment on lines 55 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These both should probably be explicit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This follows the original implementation by @HowardHinnant

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design is that conversions to Number are implicit and conversions away from Number are explicit. This design encourages and facilitates the use of Number as the preferred type for floating point arithmetic as it makes "mixed mode" more convenient, e.g. MPTAmount + Number.

As Number holds a superset of values of all the other types, these implicit conversions are guaranteed to be loss-less. However the reverse conversion does not carry this guarantee.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that clarification, @HowardHinnant ! I left the feedback originally because I assumed that we didn't want any accidental conversions to Number from those other types. Now I understand those are desirable.

@gregtatcam Could you add a comment on these functions so that we have this info preserved for the future. It's probably fine to just copy Howard's comment here.


constexpr rep
mantissa() const noexcept;
Expand Down Expand Up @@ -89,6 +91,7 @@ class Number
lowest() noexcept;

explicit operator XRPAmount() const; // round to nearest, even on tie
explicit operator MPTAmount() const; // round to nearest, even on tie
explicit operator rep() const; // round to nearest, even on tie

friend constexpr bool
Expand Down Expand Up @@ -210,6 +213,10 @@ inline Number::Number(XRPAmount const& x) : Number{x.drops()}
{
}

inline Number::Number(MPTAmount const& x) : Number{x.value()}
{
}

inline constexpr Number::rep
Number::mantissa() const noexcept
{
Expand Down
4 changes: 4 additions & 0 deletions include/xrpl/basics/XRPAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ class XRPAmount : private boost::totally_ordered<XRPAmount>,
return dropsAs<Dest>().value_or(defaultValue.drops());
}

/* Clips a 64-bit value to a 32-bit JSON number. It is only used
* in contexts that don't expect the value to ever approach
* the 32-bit limits (i.e. fees and reserves).
*/
Json::Value
jsonClipped() const
{
Expand Down
2 changes: 2 additions & 0 deletions include/xrpl/basics/base_uint.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class base_uint
using uint128 = base_uint<128>;
using uint160 = base_uint<160>;
using uint256 = base_uint<256>;
using uint192 = base_uint<192>;

template <std::size_t Bits, class Tag>
[[nodiscard]] inline constexpr std::strong_ordering
Expand Down Expand Up @@ -633,6 +634,7 @@ operator<<(std::ostream& out, base_uint<Bits, Tag> const& u)
#ifndef __INTELLISENSE__
static_assert(sizeof(uint128) == 128 / 8, "There should be no padding bytes");
static_assert(sizeof(uint160) == 160 / 8, "There should be no padding bytes");
static_assert(sizeof(uint192) == 192 / 8, "There should be no padding bytes");
static_assert(sizeof(uint256) == 256 / 8, "There should be no padding bytes");
#endif

Expand Down
8 changes: 1 addition & 7 deletions include/xrpl/protocol/AmountConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ toSTAmount(IOUAmount const& iou, Issue const& iss)
{
bool const isNeg = iou.signum() < 0;
std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
return STAmount(
iss,
umant,
iou.exponent(),
/*native*/ false,
isNeg,
STAmount::unchecked());
return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
}

inline STAmount
Expand Down
Loading
Loading