Skip to content

Commit

Permalink
Introduce new type variant<Currency, CFTokenIssuanceID> to replace Cu…
Browse files Browse the repository at this point in the history
…rrency, where CFTokenIssuanceID is uint256. Update unit-tests.
  • Loading branch information
gregtatcam committed Nov 21, 2023
1 parent 5489d61 commit 501024c
Show file tree
Hide file tree
Showing 86 changed files with 1,258 additions and 1,136 deletions.
18 changes: 10 additions & 8 deletions src/ripple/app/ledger/OrderBookDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,18 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)
{
Book book;

book.in.currency = sle->getFieldH160(sfTakerPaysCurrency);
if (sle->isFieldPresent(sfTakerPaysCurrency))
book.in.asset = static_cast<Currency>(
sle->getFieldH160(sfTakerPaysCurrency));
else
book.in.asset = sle->getFieldH256(sfTakerPaysCFTID);
book.in.account = sle->getFieldH160(sfTakerPaysIssuer);
book.in.isCFT = sle->isFieldPresent(sfTakerPaysCFT)
? sle->getFieldU8(sfTakerPaysCFT)
: false;
book.out.currency = sle->getFieldH160(sfTakerGetsCurrency);
if (sle->isFieldPresent(sfTakerGetsCurrency))
book.out.asset = static_cast<Currency>(
sle->getFieldH160(sfTakerGetsCurrency));
else
book.out.asset = sle->getFieldH256(sfTakerGetsCFTID);
book.out.account = sle->getFieldH160(sfTakerGetsIssuer);
book.out.isCFT = sle->isFieldPresent(sfTakerGetsCFT)
? sle->getFieldU8(sfTakerGetsCFT)
: false;

allBooks[book.in].insert(book.out);

Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/misc/AMMUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ ammHolds(
STAmount
ammLPHolds(
ReadView const& view,
Currency const& cur1,
Currency const& cur2,
Asset const& cur1,
Asset const& cur2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j);
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4343,7 +4343,7 @@ NetworkOPsImp::getBookPage(
saOwnerFunds = accountHolds(
view,
uOfferOwnerID,
book.out.currency,
book.out.asset,
book.out.account,
fhZERO_IF_FROZEN,
viewJ);
Expand Down Expand Up @@ -4494,7 +4494,7 @@ NetworkOPsImp::getBookPage(

saOwnerFunds = lesActive.accountHolds(
uOfferOwnerID,
book.out.currency,
book.out.asset,
book.out.account,
fhZERO_IF_FROZEN);

Expand Down
21 changes: 10 additions & 11 deletions src/ripple/app/misc/impl/AMMUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ ammHolds(
STAmount
ammLPHolds(
ReadView const& view,
Currency const& cur1,
Currency const& cur2,
Asset const& cur1,
Asset const& cur2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j)
Expand All @@ -125,10 +125,11 @@ ammLPHolds(
AccountID const& lpAccount,
beast::Journal const j)
{
// TODO CFT CFT CFT
return ammLPHolds(
view,
ammSle[sfAsset].currency,
ammSle[sfAsset2].currency,
ammSle[sfAsset].asset,
ammSle[sfAsset2].asset,
ammSle[sfAccount],
lpAccount,
j);
Expand Down Expand Up @@ -173,21 +174,19 @@ ammAccountHolds(
if (auto const sle = view.read(keylet::account(ammAccountID)))
return (*sle)[sfBalance];
}
else if (issue.isCFT)
else if (issue.isCFT())
{
auto const cftokenID = keylet::cftoken(
ammAccountID,
keylet::cftIssuance(issue.account, issue.currency).key);
ammAccountID, keylet::cftIssuance((uint256)issue.asset).key);
if (auto const sle = view.read(cftokenID))
return STAmount{
issue,
sle->getFieldU64(sfCFTAmount) -
sle->getFieldU64(sfCFTLockedAmount)};
}
else if (auto const sle = view.read(
keylet::line(ammAccountID, issue.account, issue.currency));
sle &&
!isFrozen(view, ammAccountID, issue.currency, issue.account))
else if (auto const sle = view.read(keylet::line(
ammAccountID, issue.account, (Currency)issue.asset));
sle && !isFrozen(view, ammAccountID, issue.asset, issue.account))
{
auto amount = (*sle)[sfBalance];
if (ammAccountID > issue.account)
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/paths/AccountCurrencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ accountSourceCurrencies(
// Peer extends credit.
&& ((-saBalance) < rspEntry.getLimitPeer()))) // Credit left.
{
currencies.insert(saBalance.getCurrency());
currencies.insert((Currency)saBalance.getAsset());
}
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ accountDestCurrencies(
auto& saBalance = rspEntry.getBalance();

if (saBalance < rspEntry.getLimit()) // Can take more
currencies.insert(saBalance.getCurrency());
currencies.insert((Currency)saBalance.getAsset());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/paths/Credit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ creditLimit(
}

assert(result.getIssuer() == account);
assert(result.getCurrency() == currency);
assert(result.getAsset() == currency);
return result;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ creditBalance(
}

assert(result.getIssuer() == account);
assert(result.getCurrency() == currency);
assert(result.getAsset() == currency);
return result;
}

Expand Down
23 changes: 12 additions & 11 deletions src/ripple/app/paths/Flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ finishFlow(
return result;
};

static std::variant<XRPAmount, CFTAmount, IOUAmount>
static std::variant<XRPAmount*, CFTAmount*, IOUAmount*>
getTypedAmt(Issue const& iss)
{
static XRPAmount xrp{};
static CFTAmount cft{};
static IOUAmount iou{};
static auto xrp = XRPAmount{};
static auto cft = CFTAmount{};
static auto iou = IOUAmount{};
if (isXRP(iss))
return xrp;
if (iss.isCFT)
return cft;
return iou;
return &xrp;
if (iss.isCFT())
return &cft;
return &iou;
}

path::RippleCalc::Output
Expand All @@ -88,8 +88,8 @@ flow(
Issue const srcIssue = [&] {
if (sendMax)
return sendMax->issue();
if (!isXRP(deliver.issue().currency))
return Issue(deliver.issue().currency, src);
if (!isXRP(deliver.issue().asset))
return Issue(deliver.issue().asset, src);
return xrpIssue();
}();

Expand Down Expand Up @@ -147,7 +147,8 @@ flow(
// different types, use templates to tell `flow` about the amount types.
path::RippleCalc::Output result;
std::visit(
[&, &strands_ = strands]<typename TIn, typename TOut>(TIn&&, TOut&&) {
[&, &strands_ = strands]<typename TIn, typename TOut>(
TIn const*&&, TOut const*&&) {
result = finishFlow(
sb,
srcIssue,
Expand Down
18 changes: 9 additions & 9 deletions src/ripple/app/paths/PathRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ PathRequest::parseJson(Json::Value const& jvParams)

convert_all_ = saDstAmount == STAmount(saDstAmount.issue(), 1u, 0, true);

if ((saDstAmount.getCurrency().isZero() &&
if ((isXRP(saDstAmount.getAsset()) &&
saDstAmount.getIssuer().isNonZero()) ||
(saDstAmount.getCurrency() == badCurrency()) ||
(saDstAmount.getAsset() == badCurrency()) ||
(!convert_all_ && saDstAmount <= beast::zero))
{
jvStatus = rpcError(rpcDST_AMT_MALFORMED);
Expand All @@ -335,9 +335,9 @@ PathRequest::parseJson(Json::Value const& jvParams)

saSendMax.emplace();
if (!amountFromJsonNoThrow(*saSendMax, jvParams[jss::send_max]) ||
(saSendMax->getCurrency().isZero() &&
(isXRP(saSendMax->getAsset()) &&
saSendMax->getIssuer().isNonZero()) ||
(saSendMax->getCurrency() == badCurrency()) ||
(saSendMax->getAsset() == badCurrency()) ||
(*saSendMax <= beast::zero &&
*saSendMax != STAmount(saSendMax->issue(), 1u, 0, true)))
{
Expand Down Expand Up @@ -396,7 +396,7 @@ PathRequest::parseJson(Json::Value const& jvParams)
if (saSendMax)
{
// If the currencies don't match, ignore the source currency.
if (srcCurrencyID == saSendMax->getCurrency())
if (srcCurrencyID == saSendMax->getAsset())
{
// If neither is the source and they are not equal, then the
// source issuer is illegal.
Expand Down Expand Up @@ -509,7 +509,7 @@ PathRequest::findPaths(
bool const sameAccount = *raSrcAccount == *raDstAccount;
for (auto const& c : currencies)
{
if (!sameAccount || c != saDstAmount.getCurrency())
if (!sameAccount || c != saDstAmount.getAsset())
{
if (sourceCurrencies.size() >= RPC::Tuning::max_auto_src_cur)
return false;
Expand All @@ -532,7 +532,7 @@ PathRequest::findPaths(
auto& pathfinder = getPathFinder(
cache,
currency_map,
issue.currency,
(Currency)issue.asset, // TODO CFT
dst_amount,
level,
continueCallback);
Expand All @@ -556,14 +556,14 @@ PathRequest::findPaths(
if (!isXRP(issue.account))
return issue.account;

if (isXRP(issue.currency))
if (isXRP(issue.asset))
return xrpAccount();

return *raSrcAccount;
}();

STAmount saMaxAmount = saSendMax.value_or(
STAmount({issue.currency, sourceAccount}, 1u, 0, true));
STAmount({issue.asset, sourceAccount}, 1u, 0, true));

JLOG(m_journal.debug())
<< iIdentifier << " Paths found, calling rippleCalc";
Expand Down
Loading

0 comments on commit 501024c

Please sign in to comment.