Skip to content

Commit

Permalink
Address reviewer feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Oct 3, 2024
1 parent fab0c78 commit ea887bf
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 90 deletions.
3 changes: 0 additions & 3 deletions include/xrpl/basics/MPTAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ class MPTAmount : private boost::totally_ordered<MPTAmount>,
constexpr value_type
value() const;

friend std::istream&
operator>>(std::istream& s, MPTAmount& val);

static MPTAmount
minPositiveAmount();
};
Expand Down
39 changes: 28 additions & 11 deletions include/xrpl/protocol/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ template <typename TIss>
concept ValidIssueType =
std::is_same_v<TIss, Issue> || std::is_same_v<TIss, MPTIssue>;

/* Asset is a variant of Issue (XRP and IOU) and MPTIssue (MPT).
* It enables handling of different issues when either one is expected.
* For instance, it extends STAmount class to support either issue
* in a general way. It handles specifics such arithmetics and serialization
* depending on specific issue type held by Asset.
/* 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
* type. Asset replaces Issue where any issue type is expected. For instance,
* STAmount replaces Issue with Asset to represent any issue amount.
*/
class Asset
{
Expand All @@ -45,15 +44,27 @@ class Asset
public:
Asset() = default;

Asset(Issue const& issue);
Asset(Issue const& issue) : issue_(issue)
{
}

Asset(MPTIssue const& mptIssue);
Asset(MPTIssue const& mptIssue) : issue_(mptIssue)
{
}

Asset(MPTID const& issuanceID);
Asset(MPTID const& issuanceID) : issue_(MPTIssue{issuanceID})
{
}

explicit operator Issue() const;
explicit operator Issue() const
{
return get<Issue>();
}

explicit operator MPTIssue() const;
explicit operator MPTIssue() const
{
return get<MPTIssue>();
}

AccountID const&
getIssuer() const;
Expand All @@ -79,6 +90,12 @@ class Asset
void
setJson(Json::Value& jv) const;

bool
native() const
{
return holds<Issue>() && get<Issue>().native();
}

friend constexpr bool
operator==(Asset const& lhs, Asset const& rhs);

Expand Down Expand Up @@ -141,7 +158,7 @@ operator!=(Asset const& lhs, Asset const& rhs)
inline bool
isXRP(Asset const& asset)
{
return asset.holds<Issue>() && isXRP(asset.get<Issue>());
return asset.native();
}

std::string
Expand Down
8 changes: 4 additions & 4 deletions include/xrpl/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Keylet
oracle(AccountID const& account, std::uint32_t const& documentID) noexcept;

Keylet
mptIssuance(AccountID const& issuer, std::uint32_t seq) noexcept;
mptIssuance(std::uint32_t seq, AccountID const& issuer) noexcept;

Keylet
mptIssuance(MPTID const& issuanceID) noexcept;
Expand All @@ -303,9 +303,9 @@ Keylet
mptoken(MPTID const& issuanceID, AccountID const& holder) noexcept;

inline Keylet
mptoken(uint256 const& issuanceKey)
mptoken(uint256 const& mptokenKey)
{
return {ltMPTOKEN, issuanceKey};
return {ltMPTOKEN, mptokenKey};
}

Keylet
Expand Down Expand Up @@ -352,7 +352,7 @@ std::array<keyletDesc<AccountID const&>, 6> const directAccountKeylets{
{&keylet::did, jss::DID, true}}};

MPTID
makeMptID(AccountID const& account, std::uint32_t sequence);
makeMptID(std::uint32_t sequence, AccountID const& account);

} // namespace ripple

Expand Down
5 changes: 4 additions & 1 deletion include/xrpl/protocol/Issue.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Issue

void
setJson(Json::Value& jv) const;

bool
native() const;
};

bool
Expand Down Expand Up @@ -126,7 +129,7 @@ noIssue()
inline bool
isXRP(Issue const& issue)
{
return issue == xrpIssue();
return issue.native();
}

} // namespace ripple
Expand Down
14 changes: 9 additions & 5 deletions include/xrpl/protocol/MPTIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

namespace ripple {

/* MPTIssue represents a Multi Purpose Token (MPT) and enables handling of
* either Issue or MPTIssue tokens by Asset and STAmount. MPT is identified
* by MPTID, which is a 192-bit concatenation of a 32-bit account sequence
* number at the time of MPT creation and a 160-bit account id.
* The sequence number is stored in big endian order.
/* Adapt MPTID to provide the same interface as Issue. Enables using static
* polymorphism by Asset and other classes. MPTID is a 192-bit concatenation
* of a 32-bit account sequence and a 160-bit account id.
*/
class MPTIssue
{
Expand Down Expand Up @@ -58,6 +56,12 @@ class MPTIssue

friend constexpr bool
operator!=(MPTIssue const& lhs, MPTIssue const& rhs);

bool
native() const
{
return false;
}
};

constexpr bool
Expand Down
4 changes: 2 additions & 2 deletions include/xrpl/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ STAmount::exponent() const noexcept
inline bool
STAmount::native() const noexcept
{
return isXRP(mAsset);
return mAsset.native();
}

template <ValidIssueType TIss>
Expand Down Expand Up @@ -679,7 +679,7 @@ getRate(STAmount const& offerOut, STAmount const& offerIn);
inline bool
isXRP(STAmount const& amount)
{
return isXRP(amount.asset());
return amount.native();
}

// Since `canonicalize` does not have access to a ledger, this is needed to put
Expand Down
42 changes: 7 additions & 35 deletions src/libxrpl/protocol/Asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,31 @@

namespace ripple {

Asset::Asset(Issue const& issue) : issue_(issue)
{
}

Asset::Asset(MPTIssue const& mptIssue) : issue_(mptIssue)
{
}

Asset::Asset(MPTID const& issuanceID) : issue_(MPTIssue{issuanceID})
{
}

Asset::operator Issue() const
{
return get<Issue>();
}

Asset::operator MPTIssue() const
{
return get<MPTIssue>();
}

AccountID const&
Asset::getIssuer() const
{
if (holds<Issue>())
return get<Issue>().getIssuer();
return get<MPTIssue>().getIssuer();
return std::visit(
[&](auto&& issue) -> AccountID const& { return issue.getIssuer(); },
issue_);
}

std::string
Asset::getText() const
{
if (holds<Issue>())
return get<Issue>().getText();
return get<MPTIssue>().getText();
return std::visit([&](auto&& issue) { return issue.getText(); }, issue_);
}

void
Asset::setJson(Json::Value& jv) const
{
if (holds<Issue>())
get<Issue>().setJson(jv);
else
get<MPTIssue>().setJson(jv);
std::visit([&](auto&& issue) { issue.setJson(jv); }, issue_);
}

std::string
to_string(Asset const& asset)

Check warning on line 47 in src/libxrpl/protocol/Asset.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/Asset.cpp#L47

Added line #L47 was not covered by tests
{
if (asset.holds<Issue>())
return to_string(asset.get<Issue>());
return to_string(asset.get<MPTIssue>());
return std::visit(
[&](auto const& issue) { return to_string(issue); }, asset.value());
}

bool
Expand Down
6 changes: 3 additions & 3 deletions src/libxrpl/protocol/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ getTicketIndex(AccountID const& account, SeqProxy ticketSeq)
}

MPTID
makeMptID(AccountID const& account, std::uint32_t sequence)
makeMptID(std::uint32_t sequence, AccountID const& account)
{
MPTID u;
sequence = boost::endian::native_to_big(sequence);
Expand Down Expand Up @@ -464,9 +464,9 @@ oracle(AccountID const& account, std::uint32_t const& documentID) noexcept
}

Keylet
mptIssuance(AccountID const& issuer, std::uint32_t seq) noexcept
mptIssuance(std::uint32_t seq, AccountID const& issuer) noexcept
{
return mptIssuance(makeMptID(issuer, seq));
return mptIssuance(makeMptID(seq, issuer));
}

Keylet
Expand Down
6 changes: 6 additions & 0 deletions src/libxrpl/protocol/Issue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Issue::setJson(Json::Value& jv) const
jv[jss::issuer] = toBase58(account);
}

bool
Issue::native() const
{
return *this == xrpIssue();
}

bool
isConsistent(Issue const& ac)
{
Expand Down
12 changes: 6 additions & 6 deletions src/libxrpl/protocol/STAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ amountFromString(Asset const& asset, std::string const& amount)
bool negative = (match[1].matched && (match[1] == "-"));

// Can't specify XRP or MPT using fractional representation
if ((isXRP(asset) || asset.holds<MPTIssue>()) && match[3].matched)
if ((asset.native() || asset.holds<MPTIssue>()) && match[3].matched)
Throw<std::runtime_error>(
"XRP and MPT must be specified as integral amount.");

Expand Down Expand Up @@ -962,7 +962,7 @@ amountFromJson(SField const& name, Json::Value const& v)
if (!issuer.isString() ||
!to_issuer(issue.account, issuer.asString()))
Throw<std::runtime_error>("invalid issuer");

Check warning on line 964 in src/libxrpl/protocol/STAmount.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STAmount.cpp#L964

Added line #L964 was not covered by tests
if (isXRP(issue))
if (issue.native())
Throw<std::runtime_error>("invalid issuer");

Check warning on line 966 in src/libxrpl/protocol/STAmount.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STAmount.cpp#L966

Added line #L966 was not covered by tests
asset = issue;
}
Expand Down Expand Up @@ -1182,7 +1182,7 @@ multiply(STAmount const& v1, STAmount const& v2, Asset const& asset)
if (v1 == beast::zero || v2 == beast::zero)
return STAmount(asset);

if (v1.native() && v2.native() && isXRP(asset))
if (v1.native() && v2.native() && asset.native())
{
std::uint64_t const minV = std::min(getSNValue(v1), getSNValue(v2));
std::uint64_t const maxV = std::max(getSNValue(v1), getSNValue(v2));
Expand Down Expand Up @@ -1386,7 +1386,7 @@ mulRoundImpl(
if (v1 == beast::zero || v2 == beast::zero)
return {asset};

bool const xrp = isXRP(asset);
bool const xrp = asset.native();

if (v1.native() && v2.native() && xrp)
{
Expand Down Expand Up @@ -1557,7 +1557,7 @@ divRoundImpl(

if (resultNegative != roundUp)
canonicalizeRound(
isXRP(asset) || asset.holds<MPTIssue>(), amount, offset, roundUp);
asset.native() || asset.holds<MPTIssue>(), amount, offset, roundUp);

STAmount result = [&]() {
// If appropriate, tell Number the rounding mode we are using.
Expand All @@ -1571,7 +1571,7 @@ divRoundImpl(

if (roundUp && !resultNegative && !result)
{
if (isXRP(asset) || asset.holds<MPTIssue>())
if (asset.native() || asset.holds<MPTIssue>())
{
// return the smallest value above zero
amount = 1;
Expand Down
Loading

0 comments on commit ea887bf

Please sign in to comment.