Skip to content

Commit

Permalink
Track total trustlines, avoid duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
ximinez committed Mar 15, 2022
1 parent 9246cfd commit 4bff92f
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 156 deletions.
35 changes: 21 additions & 14 deletions src/ripple/app/paths/AccountCurrencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ accountSourceCurrencies(
if (includeXRP)
currencies.insert(xrpCurrency());

for (auto const& rspEntry : lrCache->getRippleLines(account, true))
if (auto const lines = lrCache->getRippleLines(account, true))
{
auto& saBalance = rspEntry.getBalance();

// Filter out non
if (saBalance > beast::zero
// Have IOUs to send.
|| (rspEntry.getLimitPeer()
// Peer extends credit.
&& ((-saBalance) < rspEntry.getLimitPeer()))) // Credit left.
for (auto const& rspEntry : *lines)
{
currencies.insert(saBalance.getCurrency());
auto& saBalance = rspEntry.getBalance();

// Filter out non
if (saBalance > beast::zero
// Have IOUs to send.
||
(rspEntry.getLimitPeer()
// Peer extends credit.
&& ((-saBalance) < rspEntry.getLimitPeer()))) // Credit left.
{
currencies.insert(saBalance.getCurrency());
}
}
}

Expand All @@ -64,12 +68,15 @@ accountDestCurrencies(
currencies.insert(xrpCurrency());
// Even if account doesn't exist

for (auto const& rspEntry : lrCache->getRippleLines(account, true))
if (auto const lines = lrCache->getRippleLines(account, true))
{
auto& saBalance = rspEntry.getBalance();
for (auto const& rspEntry : *lines)
{
auto& saBalance = rspEntry.getBalance();

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

currencies.erase(badCurrency());
Expand Down
248 changes: 129 additions & 119 deletions src/ripple/app/paths/Pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,33 +736,37 @@ Pathfinder::getPathsOut(
{
count = app_.getOrderBookDB().getBookSize(issue);

for (auto const& rspEntry : mRLCache->getRippleLines(account, outgoing))
if (auto const lines = mRLCache->getRippleLines(account, outgoing))
{
if (currency != rspEntry.getLimit().getCurrency())
for (auto const& rspEntry : *lines)
{
}
else if (
rspEntry.getBalance() <= beast::zero &&
(!rspEntry.getLimitPeer() ||
-rspEntry.getBalance() >= rspEntry.getLimitPeer() ||
(bAuthRequired && !rspEntry.getAuth())))
{
}
else if (isDstCurrency && dstAccount == rspEntry.getAccountIDPeer())
{
count += 10000; // count a path to the destination extra
}
else if (rspEntry.getNoRipplePeer())
{
// This probably isn't a useful path out
}
else if (rspEntry.getFreezePeer())
{
// Not a useful path out
}
else
{
++count;
if (currency != rspEntry.getLimit().getCurrency())
{
}
else if (
rspEntry.getBalance() <= beast::zero &&
(!rspEntry.getLimitPeer() ||
-rspEntry.getBalance() >= rspEntry.getLimitPeer() ||
(bAuthRequired && !rspEntry.getAuth())))
{
}
else if (
isDstCurrency && dstAccount == rspEntry.getAccountIDPeer())
{
count += 10000; // count a path to the destination extra
}
else if (rspEntry.getNoRipplePeer())
{
// This probably isn't a useful path out
}
else if (rspEntry.getFreezePeer())
{
// Not a useful path out
}
else
{
++count;
}
}
}
}
Expand Down Expand Up @@ -977,120 +981,126 @@ Pathfinder::addLink(
bool const bIsNoRippleOut(isNoRippleOut(currentPath));
bool const bDestOnly(addFlags & afAC_LAST);

auto& rippleLines(
mRLCache->getRippleLines(uEndAccount, !bIsNoRippleOut));

AccountCandidates candidates;
candidates.reserve(rippleLines.size());

for (auto const& rs : rippleLines)
if (auto const lines =
mRLCache->getRippleLines(uEndAccount, !bIsNoRippleOut))
{
if (continueCallback && !continueCallback())
return;
auto const& acct = rs.getAccountIDPeer();
bool const outgoing = !rs.getNoRipplePeer();
auto& rippleLines = *lines;

if (hasEffectiveDestination && (acct == mDstAccount))
{
// We skipped the gateway
continue;
}

bool bToDestination = acct == mEffectiveDst;
AccountCandidates candidates;
candidates.reserve(rippleLines.size());

if (bDestOnly && !bToDestination)
for (auto const& rs : rippleLines)
{
continue;
}
if (continueCallback && !continueCallback())
return;
auto const& acct = rs.getAccountIDPeer();
bool const outgoing = !rs.getNoRipplePeer();

if ((uEndCurrency == rs.getLimit().getCurrency()) &&
!currentPath.hasSeen(acct, uEndCurrency, acct))
{
// path is for correct currency and has not been seen
if (rs.getBalance() <= beast::zero &&
(!rs.getLimitPeer() ||
-rs.getBalance() >= rs.getLimitPeer() ||
(bRequireAuth && !rs.getAuth())))
if (hasEffectiveDestination && (acct == mDstAccount))
{
// path has no credit
// We skipped the gateway
continue;
}
else if (bIsNoRippleOut && rs.getNoRipple())

bool bToDestination = acct == mEffectiveDst;

if (bDestOnly && !bToDestination)
{
// Can't leave on this path
continue;
}
else if (bToDestination)

if ((uEndCurrency == rs.getLimit().getCurrency()) &&
!currentPath.hasSeen(acct, uEndCurrency, acct))
{
// destination is always worth trying
if (uEndCurrency == mDstAmount.getCurrency())
// path is for correct currency and has not been
// seen
if (rs.getBalance() <= beast::zero &&
(!rs.getLimitPeer() ||
-rs.getBalance() >= rs.getLimitPeer() ||
(bRequireAuth && !rs.getAuth())))
{
// path has no credit
}
else if (bIsNoRippleOut && rs.getNoRipple())
{
// Can't leave on this path
}
else if (bToDestination)
{
// this is a complete path
if (!currentPath.empty())
// destination is always worth trying
if (uEndCurrency == mDstAmount.getCurrency())
{
JLOG(j_.trace())
<< "complete path found ae: "
<< currentPath.getJson(
JsonOptions::none);
addUniquePath(mCompletePaths, currentPath);
// this is a complete path
if (!currentPath.empty())
{
JLOG(j_.trace())
<< "complete path found ae: "
<< currentPath.getJson(
JsonOptions::none);
addUniquePath(
mCompletePaths, currentPath);
}
}
else if (!bDestOnly)
{
// this is a high-priority candidate
candidates.push_back(
{AccountCandidate::highPriority, acct});
}
}
else if (!bDestOnly)
else if (acct == mSrcAccount)
{
// this is a high-priority candidate
candidates.push_back(
{AccountCandidate::highPriority, acct});
// going back to the source is bad
}
else
{
// save this candidate
int out = getPathsOut(
uEndCurrency,
acct,
outgoing,
bIsEndCurrency,
mEffectiveDst,
continueCallback);
if (out)
candidates.push_back({out, acct});
}
}
else if (acct == mSrcAccount)
{
// going back to the source is bad
}
else
{
// save this candidate
int out = getPathsOut(
uEndCurrency,
acct,
outgoing,
bIsEndCurrency,
mEffectiveDst,
continueCallback);
if (out)
candidates.push_back({out, acct});
}
}
}

if (!candidates.empty())
{
std::sort(
candidates.begin(),
candidates.end(),
std::bind(
compareAccountCandidate,
mLedger->seq(),
std::placeholders::_1,
std::placeholders::_2));

int count = candidates.size();
// allow more paths from source
if ((count > 10) && (uEndAccount != mSrcAccount))
count = 10;
else if (count > 50)
count = 50;

auto it = candidates.begin();
while (count-- != 0)
if (!candidates.empty())
{
if (continueCallback && !continueCallback())
return;
// Add accounts to incompletePaths
STPathElement pathElement(
STPathElement::typeAccount,
it->account,
uEndCurrency,
it->account);
incompletePaths.assembleAdd(currentPath, pathElement);
++it;
std::sort(
candidates.begin(),
candidates.end(),
std::bind(
compareAccountCandidate,
mLedger->seq(),
std::placeholders::_1,
std::placeholders::_2));

int count = candidates.size();
// allow more paths from source
if ((count > 10) && (uEndAccount != mSrcAccount))
count = 10;
else if (count > 50)
count = 50;

auto it = candidates.begin();
while (count-- != 0)
{
if (continueCallback && !continueCallback())
return;
// Add accounts to incompletePaths
STPathElement pathElement(
STPathElement::typeAccount,
it->account,
uEndCurrency,
it->account);
incompletePaths.assembleAdd(
currentPath, pathElement);
++it;
}
}
}
}
Expand Down
Loading

0 comments on commit 4bff92f

Please sign in to comment.