Skip to content

Commit

Permalink
Resolve MPT/Currency inconsistencies, use accountSend() for direct MP…
Browse files Browse the repository at this point in the history
…T payment.
  • Loading branch information
gregtatcam committed Jan 8, 2024
1 parent 5f5182f commit 9807aae
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 43 deletions.
7 changes: 2 additions & 5 deletions src/ripple/app/ledger/OrderBookDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,12 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)
sle->getFieldH256(sfRootIndex) == sle->key())
{
Book book;
// TODO update for MPT once supported in the offers
Currency currency;
AccountID account;
currency = static_cast<Currency>(
sle->getFieldH160(sfTakerPaysCurrency));
currency = sle->getFieldH160(sfTakerPaysCurrency);
account = sle->getFieldH160(sfTakerPaysIssuer);
book.in = std::make_pair(currency, account);
currency = static_cast<Currency>(
sle->getFieldH160(sfTakerGetsCurrency));
currency = sle->getFieldH160(sfTakerGetsCurrency);
account = sle->getFieldH160(sfTakerGetsIssuer);
book.out = std::make_pair(currency, account);

Expand Down
10 changes: 5 additions & 5 deletions src/ripple/app/paths/impl/AmountSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct AmountSpec
IOUAmount iou = {};
};
std::optional<AccountID> issuer;
std::optional<Asset> currency;
std::optional<Asset> asset;

friend std::ostream&
operator<<(std::ostream& stream, AmountSpec const& amt)
Expand All @@ -53,8 +53,8 @@ struct AmountSpec
stream << to_string(amt.xrp);
else
stream << to_string(amt.iou);
if (amt.currency)
stream << "/(" << *amt.currency << ")";
if (amt.asset)
stream << "/(" << *amt.asset << ")";
if (amt.issuer)
stream << "/" << *amt.issuer << "";
return stream;
Expand Down Expand Up @@ -217,7 +217,7 @@ toAmountSpec(STAmount const& amt)
else
result.iou = IOUAmount(sMant, amt.exponent());
result.issuer = amt.issue().account();
result.currency = amt.issue().asset();
result.asset = amt.issue().asset();
}

return result;
Expand All @@ -238,7 +238,7 @@ toAmountSpec(EitherAmount const& ea, std::optional<Asset> const& a)
{
AmountSpec r;
r.native = (!a || isXRP(*a));
r.currency = a;
r.asset = a;
assert(ea.native == r.native);
if (r.is_mpt)
{
Expand Down
25 changes: 12 additions & 13 deletions src/ripple/app/paths/impl/PaySteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ toStrand(
}

Issue curIssue = [&] {
auto const& asset =
sendMaxIssue ? sendMaxIssue->asset() : deliver.asset();
if (isXRP(asset))
auto const& currency = static_cast<Currency>(
sendMaxIssue ? sendMaxIssue->asset() : deliver.asset());
if (isXRP(currency))
return xrpIssue();
return Issue{asset, src};
return Issue{currency, src};
}();

auto hasCurrency = [](STPathElement const pe) {
Expand Down Expand Up @@ -304,11 +304,10 @@ toStrand(

if (cur->hasCurrency())
{
Currency currency;
currency = cur->getCurrency();
if (isXRP(currency))
curIssue.setIssuer(xrpAccount());
curIssue = std::make_pair(currency, curIssue.account());
Currency const currency = cur->getCurrency();
AccountID const account =
isXRP(currency) ? xrpAccount() : curIssue.account();
curIssue = std::make_pair(currency, account);
}

if (cur->isAccount() && next->isAccount())
Expand Down Expand Up @@ -422,11 +421,11 @@ toStrand(

auto curAcc = src;
auto curIss = [&] {
auto& asset =
sendMaxIssue ? sendMaxIssue->asset() : deliver.asset();
if (isXRP(asset))
auto const& currency = static_cast<Currency>(
sendMaxIssue ? sendMaxIssue->asset() : deliver.asset());
if (isXRP(currency))
return xrpIssue();
return Issue{asset, src};
return Issue{currency, src};
}();

for (auto const& s : result)
Expand Down
1 change: 0 additions & 1 deletion src/ripple/app/tx/impl/CreateOffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,6 @@ CreateOffer::applyGuts(Sandbox& sb, Sandbox& sbCancel)
bool const bookExisted = static_cast<bool>(sb.peek(dir));

auto const bookNode = sb.dirAppend(dir, offer_index, [&](SLE::ref sle) {
// TODO add MPT once offers are supported for MPT
sle->setFieldH160(
sfTakerPaysCurrency,
static_cast<Currency>(saTakerPays.issue().asset()));
Expand Down
3 changes: 1 addition & 2 deletions src/ripple/app/tx/impl/Payment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ Payment::preflight(PreflightContext const& ctx)
auto const& uSrcCurrency = maxSourceAmount.getAsset();
auto const& uDstCurrency = saDstAmount.getAsset();

// isZero() is XRP. FIX!
bool const bXRPDirect = uSrcCurrency.isXRP() && uDstCurrency.isXRP();
bool const bMPTDirect = uSrcCurrency.isMPT() && uDstCurrency.isMPT();
bool const bDirect = bXRPDirect || bMPTDirect;
Expand Down Expand Up @@ -448,7 +447,7 @@ Payment::doApply()

PaymentSandbox pv(&view());
auto const res =
rippleMPTCredit(pv, account_, uDstAccountID, saDstAmount, j_);
accountSend(pv, account_, uDstAccountID, saDstAmount, j_);
pv.apply(ctx_.rawView());
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/tx/impl/SetTrust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ SetTrust::preclaim(PreclaimContext const& ctx)

auto const saLimitAmount = ctx.tx[sfLimitAmount];

auto const currency = saLimitAmount.getAsset();
auto const currency = static_cast<Currency>(saLimitAmount.getAsset());
auto const uDstAccountID = saLimitAmount.getIssuer();

if (ctx.view.rules().enabled(fixTrustLinesToSelf))
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/protocol/Book.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ reversed(Book const& book);

/** Equality comparison. */
/** @{ */
[[nodiscard]] inline bool
[[nodiscard]] inline constexpr bool
operator==(Book const& lhs, Book const& rhs)
{
return (lhs.in == rhs.in) && (lhs.out == rhs.out);
Expand Down
11 changes: 5 additions & 6 deletions src/ripple/protocol/Issue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ namespace ripple {
class Issue
{
private:
// using IOU = std::pair<Currency, AccountID>;
// std::variant<MPT, IOU> issue_;
Asset asset_{};
std::optional<AccountID> account_{std::nullopt};

Expand Down Expand Up @@ -157,14 +155,15 @@ void
hash_append(Hasher& h, Issue const& r)
{
using beast::hash_append;
std::visit(
[&](auto&& arg) { hash_append(h, arg, r.account()); },
r.asset().asset());
if (r.isMPT())
hash_append(h, std::get<MPT>(r.asset().asset()));
else
hash_append(h, std::get<Currency>(r.asset().asset()), r.account());
}

/** Equality comparison. */
/** @{ */
[[nodiscard]] inline bool
[[nodiscard]] inline constexpr bool
operator==(Issue const& lhs, Issue const& rhs)
{
return (lhs.asset() == rhs.asset()) &&
Expand Down
20 changes: 12 additions & 8 deletions src/ripple/protocol/impl/Issue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,26 @@ issueFromJson(Json::Value const& v)

bool const isMPT = v.isMember(jss::mpt_issuance_id);

Json::Value const assetStr =
isMPT ? v[jss::mpt_issuance_id] : v[jss::currency];
assert(!isMPT);
if (isMPT)
Throw<std::runtime_error>("issueFromJson MPT is not supported");

Json::Value const curStr = v[jss::currency];
Json::Value const issStr = v[jss::issuer];

if (!assetStr.isString())
if (!curStr.isString())
{
Throw<Json::error>("issueFromJson asset must be a string Json value");
Throw<Json::error>(
"issueFromJson currency must be a string Json value");
}

Asset asset = to_currency(assetStr.asString());
if (asset == badCurrency() || asset == noCurrency())
auto const currency = to_currency(curStr.asString());
if (currency == badCurrency() || currency == noCurrency())
{
Throw<Json::error>("issueFromJson currency must be a valid currency");
}

if (asset.isXRP())
if (isXRP(currency))
{
if (!issStr.isNull())
{
Expand All @@ -129,7 +133,7 @@ issueFromJson(Json::Value const& v)
Throw<Json::error>("issueFromJson issuer must be a valid account");
}

return Issue{asset, *issuer};
return Issue{currency, *issuer};
}

std::ostream&
Expand Down
1 change: 0 additions & 1 deletion src/ripple/rpc/handlers/Subscribe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ doSubscribe(RPC::JsonContext& context)
Json::Value taker_pays = j[jss::taker_pays];
Json::Value taker_gets = j[jss::taker_gets];

// TODO Add MPT once supported in OFFERS
// Parse mandatory currency.
Currency inCurrency;
if (!taker_pays.isMember(jss::currency) ||
Expand Down

0 comments on commit 9807aae

Please sign in to comment.