Skip to content

Commit

Permalink
Support path-finding
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Mar 19, 2024
1 parent d12105b commit 69b3245
Show file tree
Hide file tree
Showing 19 changed files with 685 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ target_sources (rippled PRIVATE
src/ripple/app/misc/impl/ValidatorList.cpp
src/ripple/app/misc/impl/ValidatorSite.cpp
src/ripple/app/paths/AccountCurrencies.cpp
src/ripple/app/paths/AssetCache.cpp
src/ripple/app/paths/Credit.cpp
src/ripple/app/paths/Flow.cpp
src/ripple/app/paths/PathRequest.cpp
src/ripple/app/paths/PathRequests.cpp
src/ripple/app/paths/Pathfinder.cpp
src/ripple/app/paths/RippleCalc.cpp
src/ripple/app/paths/RippleLineCache.cpp
src/ripple/app/paths/TrustLine.cpp
src/ripple/app/paths/impl/AMMLiquidity.cpp
src/ripple/app/paths/impl/AMMOffer.cpp
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 @@ -24,7 +24,7 @@ namespace ripple {
hash_set<Currency>
accountSourceCurrencies(
AccountID const& account,
std::shared_ptr<RippleLineCache> const& lrCache,
std::shared_ptr<AssetCache> const& lrCache,
bool includeXRP)
{
hash_set<Currency> currencies;
Expand Down Expand Up @@ -60,7 +60,7 @@ accountSourceCurrencies(
hash_set<Currency>
accountDestCurrencies(
AccountID const& account,
std::shared_ptr<RippleLineCache> const& lrCache,
std::shared_ptr<AssetCache> const& lrCache,
bool includeXRP)
{
hash_set<Currency> currencies;
Expand Down
6 changes: 3 additions & 3 deletions src/ripple/app/paths/AccountCurrencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
#ifndef RIPPLE_APP_PATHS_ACCOUNTCURRENCIES_H_INCLUDED
#define RIPPLE_APP_PATHS_ACCOUNTCURRENCIES_H_INCLUDED

#include <ripple/app/paths/RippleLineCache.h>
#include <ripple/app/paths/AssetCache.h>
#include <ripple/protocol/UintTypes.h>

namespace ripple {

hash_set<Currency>
accountDestCurrencies(
AccountID const& account,
std::shared_ptr<RippleLineCache> const& cache,
std::shared_ptr<AssetCache> const& cache,
bool includeXRP);

hash_set<Currency>
accountSourceCurrencies(
AccountID const& account,
std::shared_ptr<RippleLineCache> const& lrLedger,
std::shared_ptr<AssetCache> const& lrLedger,
bool includeXRP);

} // namespace ripple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@
*/
//==============================================================================

#include <ripple/app/paths/RippleLineCache.h>
#include <ripple/app/paths/AssetCache.h>
#include <ripple/app/paths/TrustLine.h>
#include <ripple/ledger/OpenView.h>

namespace ripple {

RippleLineCache::RippleLineCache(
AssetCache::AssetCache(
std::shared_ptr<ReadView const> const& ledger,
beast::Journal j)
: ledger_(ledger), journal_(j)
{
JLOG(journal_.debug()) << "created for ledger " << ledger_->info().seq;
}

RippleLineCache::~RippleLineCache()
AssetCache::~AssetCache()
{
JLOG(journal_.debug()) << "destroyed for ledger " << ledger_->info().seq
<< " with " << lines_.size() << " accounts and "
<< totalLineCount_ << " distinct trust lines.";
}

std::shared_ptr<std::vector<PathFindTrustLine>>
RippleLineCache::getRippleLines(
AccountID const& accountID,
LineDirection direction)
AssetCache::getRippleLines(AccountID const& accountID, LineDirection direction)
{
auto const hash = hasher_(accountID);
AccountKey key(accountID, direction, hash);
Expand Down Expand Up @@ -125,4 +123,33 @@ RippleLineCache::getRippleLines(
return it->second;
}

std::shared_ptr<std::vector<MPT>> const&
AssetCache::getMPTs(const ripple::AccountID& account)
{
std::lock_guard sl(mLock);

if (auto it = mpts_.find(account); it != mpts_.end())
return it->second;

std::vector<MPT> mpts;
// Get issued/authorized tokens
forEachItem(*ledger_, account, [&](std::shared_ptr<SLE const> const& sle) {
if (sle->getType() == ltMPTOKEN_ISSUANCE)
mpts.push_back(
std::make_pair(sle->getFieldU32(sfSequence), account));
else if (sle->getType() == ltMPTOKEN)
mpts.push_back(getMPT(sle->getFieldH192(sfMPTokenIssuanceID)));
});

totalMPTCount_ += mpts.size();

if (mpts.empty())
mpts_.emplace(account, nullptr);
else
mpts_.emplace(
account, std::make_shared<std::vector<MPT>>(std::move(mpts)));

return mpts_[account];
}

} // namespace ripple
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
namespace ripple {

// Used by Pathfinder
class RippleLineCache final : public CountedObject<RippleLineCache>
class AssetCache final : public CountedObject<AssetCache>
{
public:
explicit RippleLineCache(
explicit AssetCache(
std::shared_ptr<ReadView const> const& l,
beast::Journal j);
~RippleLineCache();
~AssetCache();

std::shared_ptr<ReadView const> const&
getLedger() const
Expand All @@ -62,6 +62,9 @@ class RippleLineCache final : public CountedObject<RippleLineCache>
std::shared_ptr<std::vector<PathFindTrustLine>>
getRippleLines(AccountID const& accountID, LineDirection direction);

std::shared_ptr<std::vector<MPT>> const&
getMPTs(AccountID const& account);

private:
std::mutex mLock;

Expand Down Expand Up @@ -125,6 +128,8 @@ class RippleLineCache final : public CountedObject<RippleLineCache>
AccountKey::Hash>
lines_;
std::size_t totalLineCount_ = 0;
hash_map<AccountID, std::shared_ptr<std::vector<MPT>>> mpts_;
std::size_t totalMPTCount_ = 0;
};

} // namespace ripple
Expand Down
42 changes: 31 additions & 11 deletions src/ripple/app/paths/PathRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ PathRequest::updateComplete()
}

bool
PathRequest::isValid(std::shared_ptr<RippleLineCache> const& crCache)
PathRequest::isValid(std::shared_ptr<AssetCache> const& crCache)
{
if (!raSrcAccount || !raDstAccount)
return false;
Expand Down Expand Up @@ -222,6 +222,13 @@ PathRequest::isValid(std::shared_ptr<RippleLineCache> const& crCache)

for (auto const& currency : usDestCurrID)
jvDestCur.append(to_string(currency));

if (auto mpts = crCache->getMPTs(*raDstAccount))
{
for (auto const& mpt : *mpts)
jvDestCur.append(to_string(mpt));
}

jvStatus[jss::destination_tag] =
(sleDest->getFlags() & lsfRequireDestTag);
}
Expand All @@ -242,7 +249,7 @@ PathRequest::isValid(std::shared_ptr<RippleLineCache> const& crCache)
*/
std::pair<bool, Json::Value>
PathRequest::doCreate(
std::shared_ptr<RippleLineCache> const& cache,
std::shared_ptr<AssetCache> const& cache,
Json::Value const& value)
{
bool valid = false;
Expand Down Expand Up @@ -497,7 +504,7 @@ PathRequest::doAborting() const

std::unique_ptr<Pathfinder> const&
PathRequest::getPathFinder(
std::shared_ptr<RippleLineCache> const& cache,
std::shared_ptr<AssetCache> const& cache,
hash_map<PathAsset, std::unique_ptr<Pathfinder>>& pathasset_map,
PathAsset const& asset,
STAmount const& dst_amount,
Expand Down Expand Up @@ -525,7 +532,7 @@ PathRequest::getPathFinder(

bool
PathRequest::findPaths(
std::shared_ptr<RippleLineCache> const& cache,
std::shared_ptr<AssetCache> const& cache,
int const level,
Json::Value& jvArray,
std::function<bool(void)> const& continueCallback)
Expand All @@ -537,7 +544,6 @@ PathRequest::findPaths(
}
if (sourceAssets.empty())
{
// TODO MPT
auto currencies = accountSourceCurrencies(*raSrcAccount, cache, true);
bool const sameAccount = *raSrcAccount == *raDstAccount;
for (auto const& c : currencies)
Expand All @@ -551,6 +557,13 @@ PathRequest::findPaths(
{c, c.isZero() ? xrpAccount() : *raSrcAccount});
}
}
if (auto mpts = cache->getMPTs(*raSrcAccount))
{
if (sourceAssets.size() >= RPC::Tuning::max_auto_src_cur)
return false;
for (auto const& mpt : *mpts)
sourceAssets.insert(mpt);
}
}

auto const dst_amount = convertAmount(saDstAmount, convert_all_);
Expand Down Expand Up @@ -659,7 +672,9 @@ PathRequest::findPaths(
if (rc.result() == tesSUCCESS)
{
Json::Value jvEntry(Json::objectValue);
rc.actualAmountIn.setIssuer(sourceAccount);
// TODO MPT
if (rc.actualAmountIn.isIssue())
rc.actualAmountIn.setIssuer(sourceAccount);
jvEntry[jss::source_amount] =
rc.actualAmountIn.getJson(JsonOptions::none);
jvEntry[jss::paths_computed] = ps.getJson(JsonOptions::none);
Expand Down Expand Up @@ -694,7 +709,7 @@ PathRequest::findPaths(

Json::Value
PathRequest::doUpdate(
std::shared_ptr<RippleLineCache> const& cache,
std::shared_ptr<AssetCache> const& cache,
bool fast,
std::function<bool(void)> const& continueCallback)
{
Expand All @@ -714,11 +729,16 @@ PathRequest::doUpdate(
if (hasCompletion())
{
// Old ripple_path_find API gives destination_currencies
auto& destCurrencies =
auto& destAssets =
(newStatus[jss::destination_currencies] = Json::arrayValue);
auto usCurrencies = accountDestCurrencies(*raDstAccount, cache, true);
for (auto const& c : usCurrencies)
destCurrencies.append(to_string(c));
auto usAssets = accountDestCurrencies(*raDstAccount, cache, true);
for (auto const& c : usAssets)
destAssets.append(to_string(c));
if (auto mpts = cache->getMPTs(*raDstAccount))
{
for (auto const& mpt : *mpts)
destAssets.append(to_string(mpt));
}
}

newStatus[jss::source_account] = toBase58(*raSrcAccount);
Expand Down
14 changes: 7 additions & 7 deletions src/ripple/app/paths/PathRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#define RIPPLE_APP_PATHS_PATHREQUEST_H_INCLUDED

#include <ripple/app/ledger/Ledger.h>
#include <ripple/app/paths/AssetCache.h>
#include <ripple/app/paths/Pathfinder.h>
#include <ripple/app/paths/RippleLineCache.h>
#include <ripple/json/json_value.h>
#include <ripple/net/InfoSub.h>
#include <ripple/protocol/Asset.h>
Expand All @@ -38,7 +38,7 @@ namespace ripple {
// A pathfinding request submitted by a client
// The request issuer must maintain a strong pointer

class RippleLineCache;
class AssetCache;
class PathRequests;

// Return values from parseJson <0 = invalid, >0 = valid
Expand Down Expand Up @@ -87,7 +87,7 @@ class PathRequest final : public InfoSubRequest,
updateComplete();

std::pair<bool, Json::Value>
doCreate(std::shared_ptr<RippleLineCache> const&, Json::Value const&);
doCreate(std::shared_ptr<AssetCache> const&, Json::Value const&);

Json::Value
doClose() override;
Expand All @@ -99,7 +99,7 @@ class PathRequest final : public InfoSubRequest,
// update jvStatus
Json::Value
doUpdate(
std::shared_ptr<RippleLineCache> const&,
std::shared_ptr<AssetCache> const&,
bool fast,
std::function<bool(void)> const& continueCallback = {});
InfoSub::pointer
Expand All @@ -109,11 +109,11 @@ class PathRequest final : public InfoSubRequest,

private:
bool
isValid(std::shared_ptr<RippleLineCache> const& crCache);
isValid(std::shared_ptr<AssetCache> const& crCache);

std::unique_ptr<Pathfinder> const&
getPathFinder(
std::shared_ptr<RippleLineCache> const&,
std::shared_ptr<AssetCache> const&,
hash_map<PathAsset, std::unique_ptr<Pathfinder>>&,
PathAsset const&,
STAmount const&,
Expand All @@ -125,7 +125,7 @@ class PathRequest final : public InfoSubRequest,
*/
bool
findPaths(
std::shared_ptr<RippleLineCache> const&,
std::shared_ptr<AssetCache> const&,
int const,
Json::Value&,
std::function<bool(void)> const&);
Expand Down
Loading

0 comments on commit 69b3245

Please sign in to comment.