xrpld
Loading...
Searching...
No Matches
Pathfinder.h
1#pragma once
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/rpc/detail/AssetCache.h>
5
6#include <xrpl/basics/CountedObject.h>
7#include <xrpl/core/LoadEvent.h>
8#include <xrpl/ledger/Ledger.h>
9#include <xrpl/protocol/PathAsset.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STPathSet.h>
12
13namespace xrpl {
14
21class Pathfinder : public CountedObject<Pathfinder>
22{
23public:
26 std::shared_ptr<AssetCache> const& cache,
27 AccountID const& srcAccount,
28 AccountID const& dstAccount,
29 PathAsset const& uSrcPathAsset,
30 std::optional<AccountID> const& uSrcIssuer,
31 STAmount const& dstAmount,
32 std::optional<STAmount> const& srcAmount,
33 std::optional<uint256> const& domain,
34 Application& app);
35 Pathfinder(Pathfinder const&) = delete;
37 operator=(Pathfinder const&) = delete;
38 ~Pathfinder() = default;
39
40 static void
42
43 bool
44 findPaths(int searchLevel, std::function<bool(void)> const& continueCallback = {});
45
47 void
48 computePathRanks(int maxPaths, std::function<bool(void)> const& continueCallback = {});
49
50 /* Get the best paths, up to maxPaths in number, from completePaths_.
51
52 On return, if fullLiquidityPath is not empty, then it contains the best
53 additional single path which can consume all the liquidity.
54 */
55 STPathSet
57 int maxPaths,
58 STPath& fullLiquidityPath,
59 STPathSet const& extraPaths,
60 AccountID const& srcIssuer,
61 std::function<bool(void)> const& continueCallback = {});
62
63 enum class NodeType {
64 Source, // The source account: with an issuer account, if needed.
65 Accounts, // Accounts that connect from this source/currency.
66 Books, // Order books that connect to this currency.
67 XrpBook, // The order book from this currency to XRP.
68 DestBook, // The order book to the destination currency/issuer.
69 Destination // The destination account only.
70 };
71
72 // The PathType is a list of the NodeTypes for a path.
74
75 // PaymentType represents the types of the source and destination currencies
76 // in a path request.
77 enum class PaymentType {
81 NonXrpToSame, // Destination currency is the same as source.
82 NonXrpToNonXrp // Destination currency is NOT the same as source.
83 };
84
92
93private:
94 /*
95 Call graph of Pathfinder methods.
96
97 findPaths:
98 addPathsForType:
99 addLinks:
100 addLink:
101 getPathsOut
102 issueMatchesOrigin
103 isNoRippleOut:
104 isNoRipple
105
106 computePathRanks:
107 rippleCalculate
108 getPathLiquidity:
109 rippleCalculate
110
111 getBestPaths
112 */
113
114 // Add all paths of one type to completePaths_.
115 STPathSet&
116 addPathsForType(PathType const& type, std::function<bool(void)> const& continueCallback);
117
118 bool
120
121 int
123 PathAsset const& pathAsset,
124 AccountID const& account,
125 LineDirection direction,
126 bool isDestPathAsset,
127 AccountID const& dest,
128 std::function<bool(void)> const& continueCallback);
129
130 void
131 addLink(
132 STPath const& currentPath,
133 STPathSet& incompletePaths,
134 int addFlags,
135 std::function<bool(void)> const& continueCallback);
136
137 // Call addLink() for each path in currentPaths.
138 void
139 addLinks(
140 STPathSet const& currentPaths,
141 STPathSet& incompletePaths,
142 int addFlags,
143 std::function<bool(void)> const& continueCallback);
144
145 // Compute the liquidity for a path. Return tesSUCCESS if it has enough
146 // liquidity to be worth keeping, otherwise an error.
147 TER
149 STPath const& path, // IN: The path to check.
150 STAmount const& minDstAmount, // IN: The minimum output this path must
151 // deliver to be worth keeping.
152 STAmount& amountOut, // OUT: The actual liquidity on the path.
153 uint64_t& qualityOut) const; // OUT: The returned initial quality
154
155 // Does this path end on an account-to-account link whose last account has
156 // set the "no ripple" flag on the link?
157 bool
158 isNoRippleOut(STPath const& currentPath);
159
160 // Is the "no ripple" flag set from one account to another?
161 bool
162 isNoRipple(AccountID const& fromAccount, AccountID const& toAccount, Currency const& currency);
163
164 void
165 rankPaths(
166 int maxPaths,
167 STPathSet const& paths,
168 std::vector<PathRank>& rankedPaths,
169 std::function<bool(void)> const& continueCallback);
170
173 AccountID effectiveDst_; // The account the paths need to end at
183
187
192
194
197
198 // Add ripple paths
199 static std::uint32_t const kAfAddAccounts = 0x001;
200
201 // Add order books
202 static std::uint32_t const kAfAddBooks = 0x002;
203
204 // Add order book to XRP only
205 static std::uint32_t const kAfObXrp = 0x010;
206
207 // Must link to destination currency
208 static std::uint32_t const kAfObLast = 0x040;
209
210 // Destination account only
211 static std::uint32_t const kAfAcLast = 0x080;
212};
213
214} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
STAmount dstAmount_
Definition Pathfinder.h:174
static std::uint32_t const kAfObLast
Definition Pathfinder.h:208
void addLinks(STPathSet const &currentPaths, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
void rankPaths(int maxPaths, STPathSet const &paths, std::vector< PathRank > &rankedPaths, std::function< bool(void)> const &continueCallback)
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
~Pathfinder()=default
Pathfinder(std::shared_ptr< AssetCache > const &cache, AccountID const &srcAccount, AccountID const &dstAccount, PathAsset const &uSrcPathAsset, std::optional< AccountID > const &uSrcIssuer, STAmount const &dstAmount, std::optional< STAmount > const &srcAmount, std::optional< uint256 > const &domain, Application &app)
Construct a pathfinder without an issuer.
hash_map< Asset, int > pathsOutCountMap_
Definition Pathfinder.h:193
bool issueMatchesOrigin(Asset const &)
STAmount srcAmount_
Definition Pathfinder.h:177
static std::uint32_t const kAfAddAccounts
Definition Pathfinder.h:199
AccountID dstAccount_
Definition Pathfinder.h:172
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
void addLink(STPath const &currentPath, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
PathAsset srcPathAsset_
Definition Pathfinder.h:175
std::vector< NodeType > PathType
Definition Pathfinder.h:73
std::unique_ptr< LoadEvent > loadEvent_
Definition Pathfinder.h:185
Pathfinder(Pathfinder const &)=delete
AccountID srcAccount_
Definition Pathfinder.h:171
static std::uint32_t const kAfObXrp
Definition Pathfinder.h:205
std::optional< AccountID > srcIssuer_
Definition Pathfinder.h:176
std::map< PathType, STPathSet > paths_
Definition Pathfinder.h:191
std::optional< uint256 > domain_
Definition Pathfinder.h:182
static std::uint32_t const kAfAcLast
Definition Pathfinder.h:211
std::shared_ptr< AssetCache > rLCache_
Definition Pathfinder.h:186
STPathSet completePaths_
Definition Pathfinder.h:189
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
STPathElement source_
Definition Pathfinder.h:188
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Application & app_
Definition Pathfinder.h:195
bool isNoRippleOut(STPath const &currentPath)
beast::Journal const j_
Definition Pathfinder.h:196
static void initPathTable()
STAmount remainingAmount_
The amount remaining from srcAccount_ after the default liquidity has been removed.
Definition Pathfinder.h:180
static std::uint32_t const kAfAddBooks
Definition Pathfinder.h:202
AccountID effectiveDst_
Definition Pathfinder.h:173
Pathfinder & operator=(Pathfinder const &)=delete
int getPathsOut(PathAsset const &pathAsset, AccountID const &account, LineDirection direction, bool isDestPathAsset, AccountID const &dest, std::function< bool(void)> const &continueCallback)
std::vector< PathRank > pathRanks_
Definition Pathfinder.h:190
std::shared_ptr< ReadView const > ledger_
Definition Pathfinder.h:184
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition TrustLine.h:21
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634