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/basics/UnorderedContainers.h>
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/beast/utility/Journal.h>
10#include <xrpl/core/LoadEvent.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Asset.h>
14#include <xrpl/protocol/PathAsset.h>
15#include <xrpl/protocol/STAmount.h>
16#include <xrpl/protocol/STPathSet.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/UintTypes.h>
19
20#include <cstdint>
21#include <functional>
22#include <map>
23#include <memory>
24#include <optional>
25#include <vector>
26
27namespace xrpl {
28
36class Pathfinder : public CountedObject<Pathfinder>
37{
38public:
43 std::shared_ptr<AssetCache> const& cache,
44 AccountID const& srcAccount,
45 AccountID const& dstAccount,
46 PathAsset const& uSrcPathAsset,
47 std::optional<AccountID> const& uSrcIssuer,
48 STAmount const& dstAmount,
49 std::optional<STAmount> const& srcAmount,
50 std::optional<uint256> const& domain,
51 Application& app);
52 Pathfinder(Pathfinder const&) = delete;
54 operator=(Pathfinder const&) = delete;
55 ~Pathfinder() = default;
56
57 static void
59
60 bool
61 findPaths(int searchLevel, std::function<bool(void)> const& continueCallback = {});
62
66 void
67 computePathRanks(int maxPaths, std::function<bool(void)> const& continueCallback = {});
68
69 /* Get the best paths, up to maxPaths in number, from completePaths_.
70
71 On return, if fullLiquidityPath is not empty, then it contains the best
72 additional single path which can consume all the liquidity.
73 */
74 STPathSet
76 int maxPaths,
77 STPath& fullLiquidityPath,
78 STPathSet const& extraPaths,
79 AccountID const& srcIssuer,
80 std::function<bool(void)> const& continueCallback = {});
81
82 enum class NodeType {
83 Source, // The source account: with an issuer account, if needed.
84 Accounts, // Accounts that connect from this source/currency.
85 Books, // Order books that connect to this currency.
86 XrpBook, // The order book from this currency to XRP.
87 DestBook, // The order book to the destination currency/issuer.
88 Destination // The destination account only.
89 };
90
91 // The PathType is a list of the NodeTypes for a path.
93
94 // PaymentType represents the types of the source and destination currencies
95 // in a path request.
96 enum class PaymentType {
100 NonXrpToSame, // Destination currency is the same as source.
101 NonXrpToNonXrp // Destination currency is NOT the same as source.
102 };
103
111
112private:
113 /*
114 Call graph of Pathfinder methods.
115
116 findPaths:
117 addPathsForType:
118 addLinks:
119 addLink:
120 getPathsOut
121 issueMatchesOrigin
122 isNoRippleOut:
123 isNoRipple
124
125 computePathRanks:
126 rippleCalculate
127 getPathLiquidity:
128 rippleCalculate
129
130 getBestPaths
131 */
132
133 // Add all paths of one type to completePaths_.
134 STPathSet&
135 addPathsForType(PathType const& type, std::function<bool(void)> const& continueCallback);
136
137 bool
139
140 int
142 PathAsset const& pathAsset,
143 AccountID const& account,
144 LineDirection direction,
145 bool isDestPathAsset,
146 AccountID const& dest,
147 std::function<bool(void)> const& continueCallback);
148
149 void
150 addLink(
151 STPath const& currentPath,
152 STPathSet& incompletePaths,
153 int addFlags,
154 std::function<bool(void)> const& continueCallback);
155
156 // Call addLink() for each path in currentPaths.
157 void
158 addLinks(
159 STPathSet const& currentPaths,
160 STPathSet& incompletePaths,
161 int addFlags,
162 std::function<bool(void)> const& continueCallback);
163
164 // Compute the liquidity for a path. Return tesSUCCESS if it has enough
165 // liquidity to be worth keeping, otherwise an error.
166 TER
168 STPath const& path, // IN: The path to check.
169 STAmount const& minDstAmount, // IN: The minimum output this path must
170 // deliver to be worth keeping.
171 STAmount& amountOut, // OUT: The actual liquidity on the path.
172 uint64_t& qualityOut) const; // OUT: The returned initial quality
173
174 // Does this path end on an account-to-account link whose last account has
175 // set the "no ripple" flag on the link?
176 bool
177 isNoRippleOut(STPath const& currentPath);
178
179 // Is the "no ripple" flag set from one account to another?
180 bool
181 isNoRipple(AccountID const& fromAccount, AccountID const& toAccount, Currency const& currency);
182
183 void
184 rankPaths(
185 int maxPaths,
186 STPathSet const& paths,
187 std::vector<PathRank>& rankedPaths,
188 std::function<bool(void)> const& continueCallback);
189
192 AccountID effectiveDst_; // The account the paths need to end at
204
208
213
215
218
219 // Add ripple paths
220 static std::uint32_t const kAfAddAccounts = 0x001;
221
222 // Add order books
223 static std::uint32_t const kAfAddBooks = 0x002;
224
225 // Add order book to XRP only
226 static std::uint32_t const kAfObXrp = 0x010;
227
228 // Must link to destination currency
229 static std::uint32_t const kAfObLast = 0x040;
230
231 // Destination account only
232 static std::uint32_t const kAfAcLast = 0x080;
233};
234
235} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
STAmount dstAmount_
Definition Pathfinder.h:193
static std::uint32_t const kAfObLast
Definition Pathfinder.h:229
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:214
bool issueMatchesOrigin(Asset const &)
STAmount srcAmount_
Definition Pathfinder.h:196
static std::uint32_t const kAfAddAccounts
Definition Pathfinder.h:220
AccountID dstAccount_
Definition Pathfinder.h:191
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:194
std::vector< NodeType > PathType
Definition Pathfinder.h:92
std::unique_ptr< LoadEvent > loadEvent_
Definition Pathfinder.h:206
Pathfinder(Pathfinder const &)=delete
AccountID srcAccount_
Definition Pathfinder.h:190
static std::uint32_t const kAfObXrp
Definition Pathfinder.h:226
std::optional< AccountID > srcIssuer_
Definition Pathfinder.h:195
std::map< PathType, STPathSet > paths_
Definition Pathfinder.h:212
std::optional< uint256 > domain_
Definition Pathfinder.h:203
static std::uint32_t const kAfAcLast
Definition Pathfinder.h:232
std::shared_ptr< AssetCache > rLCache_
Definition Pathfinder.h:207
STPathSet completePaths_
Definition Pathfinder.h:210
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
STPathElement source_
Definition Pathfinder.h:209
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:216
bool isNoRippleOut(STPath const &currentPath)
beast::Journal const j_
Definition Pathfinder.h:217
static void initPathTable()
STAmount remainingAmount_
The amount remaining from srcAccount_ after the default liquidity has been removed.
Definition Pathfinder.h:201
static std::uint32_t const kAfAddBooks
Definition Pathfinder.h:223
AccountID effectiveDst_
Definition Pathfinder.h:192
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:211
std::shared_ptr< ReadView const > ledger_
Definition Pathfinder.h:205
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:42
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition TrustLine.h:27
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:34
TERSubset< CanCvtToTER > TER
Definition TER.h:647