Skip to content

Commit

Permalink
Add cross-asset payment unit-tests plus some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Sep 29, 2024
1 parent de66bba commit 2dc0a8c
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 35 deletions.
82 changes: 58 additions & 24 deletions src/libxrpl/protocol/STParsedJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ parseLeaf(
json_name + "." + ss.str());

// each element in this path has some combination of
// account, currency, or issuer
// account, asset, or issuer

Json::Value pathEl = value[i][j];

Expand All @@ -643,20 +643,32 @@ parseLeaf(
return ret;
}

Json::Value const& account = pathEl["account"];
Json::Value const& currency = pathEl["currency"];
Json::Value const& issuer = pathEl["issuer"];
if (pathEl.isMember(jss::currency) &&
pathEl.isMember(jss::mpt_issuance_id))
{
error = RPC::make_error(
rpcINVALID_PARAMS, "Invalid Asset.");
return ret;
}

bool const isMPT =
pathEl.isMember(jss::mpt_issuance_id);
auto const assetName =
isMPT ? jss::mpt_issuance_id : jss::currency;
Json::Value const& account = pathEl[jss::account];
Json::Value const& asset = pathEl[assetName];
Json::Value const& issuer = pathEl[jss::issuer];
bool hasCurrency = false;
AccountID uAccount, uIssuer;
Currency uCurrency;
PathAsset uAsset;

if (account)
{
// human account id
if (!account.isString())
{
error =
string_expected(element_name, "account");
error = string_expected(
element_name, jss::account.c_str());
return ret;
}

Expand All @@ -668,35 +680,57 @@ parseLeaf(
parseBase58<AccountID>(account.asString());
if (!a)
{
error =
invalid_data(element_name, "account");
error = invalid_data(
element_name, jss::account.c_str());
return ret;
}
uAccount = *a;
}
}

if (currency)
if (asset)
{
// human currency
if (!currency.isString())
// human asset
if (!asset.isString())
{
error =
string_expected(element_name, "currency");
error = string_expected(
element_name, assetName.c_str());
return ret;
}

hasCurrency = true;

if (!uCurrency.parseHex(currency.asString()))
if (isMPT)
{
if (!to_currency(
uCurrency, currency.asString()))
MPTID u;
if (!u.parseHex(asset.asString()))
{
error =
invalid_data(element_name, "currency");
error = invalid_data(
element_name, assetName.c_str());
return ret;
}
uAsset = u;
if (getMPTIssuer(u) == beast::zero)
{
error = invalid_data(
element_name, jss::account.c_str());
return ret;
}
}
else
{
Currency currency;
if (!currency.parseHex(asset.asString()))
{
if (!to_currency(
currency, asset.asString()))
{
error = invalid_data(
element_name, assetName.c_str());
return ret;
}
}
uAsset = currency;
}
}

Expand All @@ -705,7 +739,8 @@ parseLeaf(
// human account id
if (!issuer.isString())
{
error = string_expected(element_name, "issuer");
error = string_expected(
element_name, jss::issuer.c_str());
return ret;
}

Expand All @@ -715,16 +750,15 @@ parseLeaf(
parseBase58<AccountID>(issuer.asString());
if (!a)
{
error =
invalid_data(element_name, "issuer");
error = invalid_data(
element_name, jss::issuer.c_str());
return ret;
}
uIssuer = *a;
}
}

p.emplace_back(
uAccount, uCurrency, uIssuer, hasCurrency);
p.emplace_back(uAccount, uAsset, uIssuer, hasCurrency);
}

tail.push_back(p);
Expand Down
Loading

0 comments on commit 2dc0a8c

Please sign in to comment.