Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Dec 9, 2024
1 parent 5771e20 commit 9f58512
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 145 deletions.
45 changes: 18 additions & 27 deletions include/xrpl/protocol/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@

namespace ripple {

struct XRPAmountType
{
using amount_type = XRPAmount;
};

struct IOUAmountType
{
using amount_type = IOUAmount;
};

struct MPTAmountType
{
using amount_type = MPTAmount;
};

/* 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. Many functions and classes there were first written for Issue
Expand Down Expand Up @@ -92,14 +107,11 @@ class Asset
return holds<Issue>() && get<Issue>().native();
}

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

friend constexpr bool
operator!=(Asset const& lhs, Asset const& rhs);
std::variant<XRPAmount*, IOUAmount*, MPTAmount*>
getAmountType() const;

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

friend constexpr std::weak_ordering
operator<=>(Asset const& lhs, Asset const& rhs);
Expand Down Expand Up @@ -160,27 +172,6 @@ operator==(Asset const& lhs, Asset const& rhs)
rhs.issue_);
}

constexpr bool
operator!=(Asset const& lhs, Asset const& rhs)
{
return !(lhs == rhs);
}

constexpr bool
operator<(Asset const& lhs, Asset const& rhs)
{
return std::visit(
[&]<typename TLhs, typename TRhs>(
TLhs const& issLhs, TRhs const& issRhs) {
if constexpr (std::is_same_v<TLhs, TRhs>)
return issLhs < issRhs;
else
return false;
},
lhs.issue_,
rhs.issue_);
}

constexpr bool
operator==(Currency const& lhs, Asset const& rhs)
{
Expand Down
25 changes: 1 addition & 24 deletions include/xrpl/protocol/MPTIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,16 @@ class MPTIssue
void
setJson(Json::Value& jv) const;

auto
constexpr std::weak_ordering
operator<=>(MPTIssue const&) const = default;

bool
native() const
{
return false;
}

friend constexpr std::weak_ordering
operator<=>(MPTIssue const& lhs, MPTIssue const& rhs);
};

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

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

constexpr std::weak_ordering
operator<=>(MPTIssue const& lhs, MPTIssue const& rhs)
{
if (auto const c{lhs.mptID_ <=> rhs.mptID_}; c != 0)
return c;
return lhs.mptID_ <=> rhs.mptID_;
}

/** MPT is a non-native token.
*/
inline bool
Expand Down
24 changes: 12 additions & 12 deletions include/xrpl/protocol/detail/transactions.macro
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ TRANSACTION(ttCLAWBACK, 30, Clawback, ({
/** This transaction claws back tokens from an AMM pool. */
TRANSACTION(ttAMM_CLAWBACK, 31, AMMClawback, ({
{sfHolder, soeREQUIRED},
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
}))

Expand All @@ -250,8 +250,8 @@ TRANSACTION(ttAMM_CREATE, 35, AMMCreate, ({

/** This transaction type deposits into an AMM instance */
TRANSACTION(ttAMM_DEPOSIT, 36, AMMDeposit, ({
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfAmount2, soeOPTIONAL, soeMPTSupported},
{sfEPrice, soeOPTIONAL},
Expand All @@ -261,8 +261,8 @@ TRANSACTION(ttAMM_DEPOSIT, 36, AMMDeposit, ({

/** This transaction type withdraws from an AMM instance */
TRANSACTION(ttAMM_WITHDRAW, 37, AMMWithdraw, ({
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfAmount, soeOPTIONAL, soeMPTSupported},
{sfAmount2, soeOPTIONAL, soeMPTSupported},
{sfEPrice, soeOPTIONAL},
Expand All @@ -271,24 +271,24 @@ TRANSACTION(ttAMM_WITHDRAW, 37, AMMWithdraw, ({

/** This transaction type votes for the trading fee */
TRANSACTION(ttAMM_VOTE, 38, AMMVote, ({
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfTradingFee, soeREQUIRED},
}))

/** This transaction type bids for the auction slot */
TRANSACTION(ttAMM_BID, 39, AMMBid, ({
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
{sfBidMin, soeOPTIONAL},
{sfBidMax, soeOPTIONAL},
{sfAuthAccounts, soeOPTIONAL},
}))

/** This transaction type deletes AMM in the empty state */
TRANSACTION(ttAMM_DELETE, 40, AMMDelete, ({
{sfAsset, soeREQUIRED},
{sfAsset2, soeREQUIRED},
{sfAsset, soeREQUIRED, soeMPTSupported},
{sfAsset2, soeREQUIRED, soeMPTSupported},
}))

/** This transactions creates a crosschain sequence number */
Expand Down
13 changes: 13 additions & 0 deletions src/libxrpl/protocol/Asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ Asset::setJson(Json::Value& jv) const
std::visit([&](auto&& issue) { issue.setJson(jv); }, issue_);
}

std::variant<XRPAmount*, IOUAmount*, MPTAmount*>
Asset::getAmountType() const
{
static XRPAmount* xrp = nullptr;
static IOUAmount* iou = nullptr;
static MPTAmount* mpt = nullptr;
if (holds<MPTIssue>())
return mpt;
if (native())
return xrp;
return iou;
}

std::string
to_string(Asset const& asset)
{
Expand Down
1 change: 1 addition & 0 deletions src/libxrpl/protocol/MPTIssue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <xrpl/json/json_errors.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/jss.h>

Expand Down
27 changes: 18 additions & 9 deletions src/test/app/MPToken_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,6 @@ class MPToken_test : public beast::unit_test::suite
jrr = env.rpc("json", "sign", to_string(jv1));
BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
};
auto toSFieldRef = [](SField const& field) {
return std::ref(field);
};
auto setMPTFields = [&](SField const& field,
Json::Value& jv,
bool withAmount = true) {
Expand Down Expand Up @@ -3130,16 +3127,28 @@ class MPToken_test : public beast::unit_test::suite
auto const USD = gw["USD"];
Env env(*this, features);
fund(env, gw, {alice}, XRP(1'000), {USD(1'000)});
MPTTester mpt(env, gw, {.holders = {alice}, .fund = false});
mpt.create(
{.ownerCount = 1,
.holderCount = 0,
.flags = tfMPTCanTransfer | tfMPTCanTrade});
MPTTester mpt(env, gw, {.fund = false});
mpt.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
auto const MPT = mpt["MPT"];
AMM amm(env, gw, MPT(100), XRP(100));
amm.deposit(DepositArg{.account = alice, .asset1In = XRP(10)});
amm::ammClawback(
gw, alice, MPTIssue(mpt.issuanceID()), xrpIssue(), MPT(10));
}

{
Account const gw{"gw"};
Account const alice{"alice"};
auto const USD = gw["USD"];
Env env(*this, features);
fund(env, gw, {alice}, XRP(1'000), {USD(1'000)});
MPTTester mpt(env, gw, {.fund = false});
mpt.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
mpt.authorize({.account = alice});
mpt.pay(gw, alice, 1'000);
auto const MPT = mpt["MPT"];
AMM amm(env, gw, MPT(100), XRP(100));
amm.deposit(DepositArg{.account = alice, .tokens = 100});
amm.deposit(DepositArg{.account = alice, .tokens = 10'000});
amm::ammClawback(
gw, alice, MPTIssue(mpt.issuanceID()), xrpIssue(), MPT(10));
}
Expand Down
17 changes: 2 additions & 15 deletions src/xrpld/app/paths/Flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,6 @@ flow(
}
}

using Var =
std::variant<XRPAmount const*, MPTAmount const*, IOUAmount const*>;
auto getTypedAmt = [&](Asset const& iss) -> Var {
static auto xrp = XRPAmount{};
static auto mpt = MPTAmount{};
static auto iou = IOUAmount{};
if (isXRP(iss))
return &xrp;
if (iss.holds<MPTIssue>())
return &mpt;
return &iou;
};

// The src account may send either xrp,iou,mpt. The dst account may receive
// either xrp,iou,mpt. Since XRP, IOU, and MPT amounts are represented by
// different types, use templates to tell `flow` about the amount types.
Expand All @@ -165,8 +152,8 @@ flow(
ammContext,
flowDebugInfo));
},
getTypedAmt(srcAsset),
getTypedAmt(dstAsset));
srcAsset.getAmountType(),
dstAsset.getAmountType());
}

} // namespace ripple
27 changes: 5 additions & 22 deletions src/xrpld/app/paths/detail/BookStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1419,21 +1419,6 @@ equalHelper(Step const& step, ripple::Book const& book)
return false;
}

static std::variant<XRPAmount*, MPTAmount*, IOUAmount*>
getTypedAmt(Asset const& asset)
{
static auto xrp = XRPAmount{};
static auto mpt = MPTAmount{};
static auto iou = IOUAmount{};
if (asset.holds<Issue>())
{
if (isXRP(asset.get<Issue>().currency))
return &xrp;
return &iou;
}
return &mpt;
}

bool
bookStepEqual(Step const& step, ripple::Book const& book)
{
Expand All @@ -1442,15 +1427,13 @@ bookStepEqual(Step const& step, ripple::Book const& book)
UNREACHABLE("ripple::test::bookStepEqual : no XRP to XRP book step");
return false; // no such thing as xrp/xrp book step
}
bool ret = false;
std::visit(
return std::visit(
[&]<typename TIn, typename TOut>(TIn*&&, TOut*&&) {
ret =
equalHelper<TIn, TOut, BookPaymentStep<TIn, TOut>>(step, book);
return equalHelper<TIn, TOut, BookPaymentStep<TIn, TOut>>(
step, book);
},
getTypedAmt(book.in),
getTypedAmt(book.out));
return ret;
book.in.getAmountType(),
book.out.getAmountType());
}
} // namespace test

Expand Down
2 changes: 1 addition & 1 deletion src/xrpld/app/paths/detail/MPTEndpointStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <xrpld/app/paths/detail/Steps.h>
#include <xrpld/ledger/PaymentSandbox.h>
#include <xrpl/basics/Log.h>
#include <xrpl/basics/MPTAmount.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/MPTAmount.h>
#include <xrpl/protocol/Quality.h>

#include <boost/container/flat_set.hpp>
Expand Down
3 changes: 1 addition & 2 deletions src/xrpld/app/tx/detail/AMMBid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ AMMBid::preflight(PreflightContext const& ctx)
return temINVALID_FLAG;
}

if (auto const res = invalidAMMAssetPair(
ctx.tx[sfAsset].get<Issue>(), ctx.tx[sfAsset2].get<Issue>()))
if (auto const res = invalidAMMAssetPair(ctx.tx[sfAsset], ctx.tx[sfAsset2]))
{
JLOG(ctx.j.debug()) << "AMM Bid: Invalid asset pair.";
return res;
Expand Down
10 changes: 5 additions & 5 deletions src/xrpld/app/tx/detail/AMMClawback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ AMMClawback::preflight(PreflightContext const& ctx)
return temMALFORMED;
}

auto const asset = ctx.tx[sfAsset].get<Issue>();
auto const asset2 = ctx.tx[sfAsset2].get<Issue>();
auto const asset = ctx.tx[sfAsset];
auto const asset2 = ctx.tx[sfAsset2];

if (isXRP(asset))
return temMALFORMED;
Expand All @@ -82,7 +82,7 @@ AMMClawback::preflight(PreflightContext const& ctx)
return temMALFORMED;
}

if (clawAmount && clawAmount->get<Issue>() != asset)
if (clawAmount && clawAmount->issue() != asset)
{
JLOG(ctx.j.trace()) << "AMMClawback: Amount's issuer/currency subfield "
"does not match Asset field";
Expand All @@ -98,8 +98,8 @@ AMMClawback::preflight(PreflightContext const& ctx)
TER
AMMClawback::preclaim(PreclaimContext const& ctx)
{
auto const asset = ctx.tx[sfAsset].get<Issue>();
auto const asset2 = ctx.tx[sfAsset2].get<Issue>();
auto const asset = ctx.tx[sfAsset];
auto const asset2 = ctx.tx[sfAsset2];
auto const sleIssuer = ctx.view.read(keylet::account(ctx.tx[sfAccount]));
if (!sleIssuer)
return terNO_ACCOUNT; // LCOV_EXCL_LINE
Expand Down
12 changes: 9 additions & 3 deletions src/xrpld/app/tx/detail/AMMCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,14 @@ applyCreate(
// Authorize MPT
if (amount.holds<MPTIssue>())
{
auto const mptokenKey =
keylet::mptoken(amount.get<MPTIssue>().getMptID(), *ammAccount);
auto const& mptIssue = amount.get<MPTIssue>();
if (auto const err = requireAuth(
ctx_.view(), mptIssue, account_, MPTAuthType::WeakAuth);
err != tesSUCCESS)
return err;

auto const& mptID = mptIssue.getMptID();
auto const mptokenKey = keylet::mptoken(mptID, *ammAccount);

auto const ownerNode = sb.dirInsert(
keylet::ownerDir(*ammAccount),
Expand All @@ -358,7 +364,7 @@ applyCreate(

auto mptoken = std::make_shared<SLE>(mptokenKey);
(*mptoken)[sfAccount] = *ammAccount;
(*mptoken)[sfMPTokenIssuanceID] = amount.get<MPTIssue>().getMptID();
(*mptoken)[sfMPTokenIssuanceID] = mptID;
(*mptoken)[sfFlags] = 0;
(*mptoken)[sfOwnerNode] = *ownerNode;
sb.insert(mptoken);
Expand Down
Loading

0 comments on commit 9f58512

Please sign in to comment.