Skip to content

Commit

Permalink
Add MPTIssue
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Feb 20, 2024
1 parent 958dc36 commit 987065e
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 44 deletions.
1 change: 1 addition & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ target_sources (xrpl_core PRIVATE
src/ripple/protocol/impl/Keylet.cpp
src/ripple/protocol/impl/LedgerFormats.cpp
src/ripple/protocol/impl/LedgerHeader.cpp
src/ripple/protocol/impl/MPTIssue.cpp
src/ripple/protocol/impl/PublicKey.cpp
src/ripple/protocol/impl/Quality.cpp
src/ripple/protocol/impl/QualityFunction.cpp
Expand Down
8 changes: 4 additions & 4 deletions src/ripple/app/tx/impl/Payment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,17 @@ Payment::doApply()
else if (saDstAmount.isMPT())
{
if (auto const ter =
requireAuth(view(), saDstAmount.asset().mpt(), account_);
requireAuth(view(), saDstAmount.mptIssue(), account_);
ter != tesSUCCESS)
return ter;

if (auto const ter =
requireAuth(view(), saDstAmount.asset().mpt(), uDstAccountID);
requireAuth(view(), saDstAmount.mptIssue(), uDstAccountID);
ter != tesSUCCESS)
return ter;

auto const& mpt = saDstAmount.asset().mpt();
auto const& issuer = mpt.second;
auto const& mpt = saDstAmount.mptIssue();
auto const& issuer = mpt.account();
// If globally/individually locked then
// can't send between holders
// holder can send back to issuer
Expand Down
11 changes: 7 additions & 4 deletions src/ripple/ledger/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ enum FreezeHandling { fhIGNORE_FREEZE, fhZERO_IF_FROZEN };
isGlobalFrozen(ReadView const& view, AccountID const& issuer);

[[nodiscard]] bool
isGlobalFrozen(ReadView const& view, MPT const& mpt);
isGlobalFrozen(ReadView const& view, MPTIssue const& mpt);

[[nodiscard]] bool
isIndividualFrozen(
Expand All @@ -104,7 +104,7 @@ isIndividualFrozen(
isIndividualFrozen(
ReadView const& view,
AccountID const& account,
MPT const& mpt);
MPTIssue const& mpt);

[[nodiscard]] bool
isFrozen(
Expand All @@ -120,7 +120,7 @@ isFrozen(ReadView const& view, AccountID const& account, Issue const& issue)
}

[[nodiscard]] bool
isFrozen(ReadView const& view, AccountID const& account, MPT const& mpt);
isFrozen(ReadView const& view, AccountID const& account, MPTIssue const& mpt);

// Returns the amount an account can spend without going into debt.
//
Expand Down Expand Up @@ -491,7 +491,10 @@ transferXRP(
[[nodiscard]] TER
requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
[[nodiscard]] TER
requireAuth(ReadView const& view, MPT const& mpt, AccountID const& account);
requireAuth(
ReadView const& view,
MPTIssue const& mpt,
AccountID const& account);

/** Deleter function prototype. Returns the status of the entry deletion
* (if should not be skipped) and if the entry should be skipped. The status
Expand Down
25 changes: 13 additions & 12 deletions src/ripple/ledger/impl/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ isGlobalFrozen(ReadView const& view, AccountID const& issuer)
}

bool
isGlobalFrozen(ReadView const& view, MPT const& mpt)
isGlobalFrozen(ReadView const& view, MPTIssue const& mpt)
{
if (auto const sle = view.read(keylet::mptIssuance(mpt)))
if (auto const sle = view.read(keylet::mptIssuance(mpt.mpt())))
return sle->getFlags() & lsfMPTLocked;
return false;
}
Expand Down Expand Up @@ -209,9 +209,9 @@ bool
isIndividualFrozen(
ReadView const& view,
AccountID const& account,
MPT const& mpt)
MPTIssue const& mpt)
{
if (auto const sle = view.read(keylet::mptoken(mpt, account)))
if (auto const sle = view.read(keylet::mptoken(mpt.mpt(), account)))
return sle->getFlags() & lsfMPTLocked;
return false;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ isFrozen(
}

bool
isFrozen(ReadView const& view, AccountID const& account, MPT const& mpt)
isFrozen(ReadView const& view, AccountID const& account, MPTIssue const& mpt)
{
if (isGlobalFrozen(view, mpt))
return true;
Expand Down Expand Up @@ -1297,7 +1297,7 @@ rippleSendMPT(
assert(uSenderID != uReceiverID);

// Safe to get MPT since rippleSendMPT is only called by accountSendMPT
auto const issuer = saAmount.asset().mpt().second;
auto const issuer = saAmount.mptIssue().account();

if (uSenderID == issuer || uReceiverID == issuer || issuer == noAccount())
{
Expand All @@ -1311,14 +1311,15 @@ rippleSendMPT(
}

// Sending 3rd party MPTs: transit.
if (auto const sle = view.read(keylet::mptIssuance(saAmount.asset().mpt())))
if (auto const sle =
view.read(keylet::mptIssuance(saAmount.mptIssue().mpt())))
{
saActual = (waiveFee == WaiveTransferFee::Yes)
? saAmount
: multiply(
saAmount,
transferRateMPT(
view, static_cast<MPT>(saAmount.asset().mpt())));
view, static_cast<MPT>(saAmount.mptIssue().mpt())));

JLOG(j.debug()) << "rippleSend> " << to_string(uSenderID) << " - > "
<< to_string(uReceiverID)
Expand Down Expand Up @@ -1641,9 +1642,9 @@ requireAuth(ReadView const& view, Issue const& issue, AccountID const& account)
}

TER
requireAuth(ReadView const& view, MPT const& mpt, AccountID const& account)
requireAuth(ReadView const& view, MPTIssue const& mpt, AccountID const& account)
{
auto const mptID = keylet::mptIssuance(mpt);
auto const mptID = keylet::mptIssuance(mpt.mpt());
if (auto const sle = view.read(mptID);
sle && sle->getFieldU32(sfFlags) & lsfMPTRequireAuth)
{
Expand Down Expand Up @@ -1789,8 +1790,8 @@ rippleMPTCredit(
STAmount saAmount,
beast::Journal j)
{
auto const mptID = keylet::mptIssuance(saAmount.asset().mpt());
auto const issuer = saAmount.asset().mpt().second;
auto const mptID = keylet::mptIssuance(saAmount.mptIssue().mpt());
auto const issuer = saAmount.mptIssue().account();
if (uSenderID == issuer)
{
if (auto sle = view.peek(mptID))
Expand Down
32 changes: 18 additions & 14 deletions src/ripple/protocol/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@

#include <ripple/basics/base_uint.h>
#include <ripple/protocol/Issue.h>
#include <ripple/protocol/MPTIssue.h>

namespace ripple {

class Asset
{
private:
std::variant<Issue, MPT> asset_;
std::variant<Issue, MPTIssue> asset_;

public:
Asset(Issue const& issue) : asset_(issue)
{
}
Asset(MPT const& mpt) : asset_(mpt)
Asset(MPTIssue const& mpt) : asset_(mpt)
{
}
Asset(MPT const& mpt) : asset_(MPTIssue{mpt})
{
}
Asset(uint192 const& mptID);
Expand All @@ -45,9 +49,9 @@ class Asset
return issue();
}

operator MPT() const
operator MPTIssue() const
{
return mpt();
return mptIssue();
}

Issue const&
Expand All @@ -65,25 +69,25 @@ class Asset
return std::get<Issue>(asset_);
}

MPT const&
mpt() const
MPTIssue const&
mptIssue() const
{
if (!std::holds_alternative<MPT>(asset_))
if (!std::holds_alternative<MPTIssue>(asset_))
Throw<std::logic_error>("Asset is not MPT");
return std::get<MPT>(asset_);
return std::get<MPTIssue>(asset_);
}
MPT&
mpt()
MPTIssue&
mptIssue()
{
if (!std::holds_alternative<MPT>(asset_))
if (!std::holds_alternative<MPTIssue>(asset_))
Throw<std::logic_error>("Asset is not MPT");
return std::get<MPT>(asset_);
return std::get<MPTIssue>(asset_);
}

constexpr bool
isMPT() const
{
return std::holds_alternative<MPT>(asset_);
return std::holds_alternative<MPTIssue>(asset_);
}

constexpr bool
Expand All @@ -102,7 +106,7 @@ class Asset
Throw<std::logic_error>("Assets are not comparable");
if (lhs.isIssue())
return lhs.issue() == rhs.issue();
return lhs.mpt() == lhs.mpt();
return lhs.mptIssue() == lhs.mptIssue();
}

friend constexpr bool
Expand Down
23 changes: 22 additions & 1 deletion src/ripple/protocol/MPTIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,32 @@ class MPTIssue
return mpt_.second;
}

std::uint32_t
sequence() const
{
return mpt_.first;
}

MPT const&
MPTID() const
mpt() const
{
return mpt_;
}

MPT&
mpt()
{
return mpt_;
}

uint192
getMptID() const;

friend constexpr bool
operator==(MPTIssue const& lhs, MPTIssue const& rhs)
{
return lhs.mpt_ == rhs.mpt_;
}
};

} // namespace ripple
Expand Down
13 changes: 11 additions & 2 deletions src/ripple/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ class STAmount final : public STBase, public CountedObject<STAmount>
Issue const&
issue() const;

MPTIssue const&
mptIssue() const;

// These three are deprecated
Currency const&
getCurrency() const;
Expand Down Expand Up @@ -561,6 +564,12 @@ STAmount::issue() const
return mAsset.issue();
}

inline MPTIssue const&
STAmount::mptIssue() const
{
return mAsset.mptIssue();
}

inline Currency const&
STAmount::getCurrency() const
{
Expand All @@ -584,7 +593,7 @@ STAmount::zeroed() const
{
if (mAsset.isIssue())
return STAmount(mAsset.issue());
return STAmount(mAsset.mpt());
return STAmount(mAsset.mptIssue());
}

inline STAmount::operator bool() const noexcept
Expand Down Expand Up @@ -636,7 +645,7 @@ inline void
STAmount::clear(STAmount const& saTmpl)
{
if (saTmpl.isMPT())
clear(saTmpl.mAsset.mpt());
clear(saTmpl.mAsset.mptIssue());
else
clear(saTmpl.issue());
}
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/protocol/impl/Asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Asset::getText() const
{
if (isIssue())
return issue().getText();
return to_string(getMptID(mpt().second, mpt().first));
return to_string(mptIssue().getMptID());
}

std::string
to_string(Asset const& asset)
{
if (asset.isIssue())
return to_string(asset.issue());
return to_string(getMptID(asset.mpt().second, asset.mpt().first));
return to_string(asset.mptIssue().getMptID());
}

} // namespace ripple
31 changes: 31 additions & 0 deletions src/ripple/protocol/impl/MPTIssue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================

#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/MPTIssue.h>

namespace ripple {

uint192
MPTIssue::getMptID() const
{
return ripple::getMptID(account(), sequence());
}

} // namespace ripple
11 changes: 6 additions & 5 deletions src/ripple/protocol/impl/STAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ getMPTValue(STAmount const& amount)
static bool
areComparable(STAmount const& v1, STAmount const& v2)
{
return (v1.isMPT() && v2.isMPT() && v1.asset().mpt() == v2.asset().mpt()) ||
return (v1.isMPT() && v2.isMPT() &&
v1.asset().mptIssue() == v2.asset().mptIssue()) ||
(v1.isIssue() && v1.native() == v2.native() &&
v1.asset().issue().currency == v2.asset().issue().currency);
v1.issue().currency == v2.issue().currency);
}

STAmount::STAmount(SerialIter& sit, SField const& name) : STBase(name)
Expand Down Expand Up @@ -607,7 +608,7 @@ STAmount::setJson(Json::Value& elem) const
// json.
elem[jss::value] = getText();
if (mAsset.isMPT())
elem[jss::mpt_issuance_id] = to_string(mAsset.mpt());
elem[jss::mpt_issuance_id] = to_string(mAsset.mptIssue());
else
{
elem[jss::currency] = to_string(mAsset.issue().currency);
Expand Down Expand Up @@ -755,8 +756,8 @@ STAmount::add(Serializer& s) const
s.add64(mValue | cMPToken);
else
s.add64(mValue | cMPToken | cPositive);
s.add32(mAsset.mpt().first);
s.addBitString(mAsset.mpt().second);
s.add32(mAsset.mptIssue().sequence());
s.addBitString(mAsset.mptIssue().account());
}
else
{
Expand Down

0 comments on commit 987065e

Please sign in to comment.