rippled
Loading...
Searching...
No Matches
Pathfinder.h
1#pragma once
2
3#include <xrpld/app/ledger/Ledger.h>
4#include <xrpld/app/paths/RippleLineCache.h>
5
6#include <xrpl/basics/CountedObject.h>
7#include <xrpl/core/LoadEvent.h>
8#include <xrpl/protocol/STAmount.h>
9#include <xrpl/protocol/STPathSet.h>
10
11namespace xrpl {
12
19class Pathfinder : public CountedObject<Pathfinder>
20{
21public:
25 AccountID const& srcAccount,
26 AccountID const& dstAccount,
27 Currency const& uSrcCurrency,
28 std::optional<AccountID> const& uSrcIssuer,
29 STAmount const& dstAmount,
30 std::optional<STAmount> const& srcAmount,
31 std::optional<uint256> const& domain,
32 Application& app);
33 Pathfinder(Pathfinder const&) = delete;
35 operator=(Pathfinder const&) = delete;
36 ~Pathfinder() = default;
37
38 static void
40
41 bool
42 findPaths(int searchLevel, std::function<bool(void)> const& continueCallback = {});
43
45 void
46 computePathRanks(int maxPaths, std::function<bool(void)> const& continueCallback = {});
47
48 /* Get the best paths, up to maxPaths in number, from mCompletePaths.
49
50 On return, if fullLiquidityPath is not empty, then it contains the best
51 additional single path which can consume all the liquidity.
52 */
53 STPathSet
55 int maxPaths,
56 STPath& fullLiquidityPath,
57 STPathSet const& extraPaths,
58 AccountID const& srcIssuer,
59 std::function<bool(void)> const& continueCallback = {});
60
61 enum NodeType {
62 nt_SOURCE, // The source account: with an issuer account, if needed.
63 nt_ACCOUNTS, // Accounts that connect from this source/currency.
64 nt_BOOKS, // Order books that connect to this currency.
65 nt_XRP_BOOK, // The order book from this currency to XRP.
66 nt_DEST_BOOK, // The order book to the destination currency/issuer.
67 nt_DESTINATION // The destination account only.
68 };
69
70 // The PathType is a list of the NodeTypes for a path.
72
73 // PaymentType represents the types of the source and destination currencies
74 // in a path request.
79 pt_nonXRP_to_same, // Destination currency is the same as source.
80 pt_nonXRP_to_nonXRP // Destination currency is NOT the same as source.
81 };
82
90
91private:
92 /*
93 Call graph of Pathfinder methods.
94
95 findPaths:
96 addPathsForType:
97 addLinks:
98 addLink:
99 getPathsOut
100 issueMatchesOrigin
101 isNoRippleOut:
102 isNoRipple
103
104 computePathRanks:
105 rippleCalculate
106 getPathLiquidity:
107 rippleCalculate
108
109 getBestPaths
110 */
111
112 // Add all paths of one type to mCompletePaths.
113 STPathSet&
114 addPathsForType(PathType const& type, std::function<bool(void)> const& continueCallback);
115
116 bool
118
119 int
121 Currency const& currency,
122 AccountID const& account,
123 LineDirection direction,
124 bool isDestCurrency,
125 AccountID const& dest,
126 std::function<bool(void)> const& continueCallback);
127
128 void
129 addLink(
130 STPath const& currentPath,
131 STPathSet& incompletePaths,
132 int addFlags,
133 std::function<bool(void)> const& continueCallback);
134
135 // Call addLink() for each path in currentPaths.
136 void
137 addLinks(
138 STPathSet const& currentPaths,
139 STPathSet& incompletePaths,
140 int addFlags,
141 std::function<bool(void)> const& continueCallback);
142
143 // Compute the liquidity for a path. Return tesSUCCESS if it has enough
144 // liquidity to be worth keeping, otherwise an error.
145 TER
147 STPath const& path, // IN: The path to check.
148 STAmount const& minDstAmount, // IN: The minimum output this path must
149 // deliver to be worth keeping.
150 STAmount& amountOut, // OUT: The actual liquidity on the path.
151 uint64_t& qualityOut) const; // OUT: The returned initial quality
152
153 // Does this path end on an account-to-account link whose last account has
154 // set the "no ripple" flag on the link?
155 bool
156 isNoRippleOut(STPath const& currentPath);
157
158 // Is the "no ripple" flag set from one account to another?
159 bool
160 isNoRipple(AccountID const& fromAccount, AccountID const& toAccount, Currency const& currency);
161
162 void
163 rankPaths(
164 int maxPaths,
165 STPathSet const& paths,
166 std::vector<PathRank>& rankedPaths,
167 std::function<bool(void)> const& continueCallback);
168
171 AccountID mEffectiveDst; // The account the paths need to end at
181
185
190
192
195
196 // Add ripple paths
197 static std::uint32_t const afADD_ACCOUNTS = 0x001;
198
199 // Add order books
200 static std::uint32_t const afADD_BOOKS = 0x002;
201
202 // Add order book to XRP only
203 static std::uint32_t const afOB_XRP = 0x010;
204
205 // Must link to destination currency
206 static std::uint32_t const afOB_LAST = 0x040;
207
208 // Destination account only
209 static std::uint32_t const afAC_LAST = 0x080;
210};
211
212} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Tracks the number of instances of an object.
A currency issued by an account.
Definition Issue.h:13
Calculates payment paths.
Definition Pathfinder.h:20
Currency mSrcCurrency
Definition Pathfinder.h:173
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
AccountID mSrcAccount
Definition Pathfinder.h:169
std::shared_ptr< RippleLineCache > mRLCache
Definition Pathfinder.h:184
static std::uint32_t const afADD_ACCOUNTS
Definition Pathfinder.h:197
~Pathfinder()=default
std::unique_ptr< LoadEvent > m_loadEvent
Definition Pathfinder.h:183
STAmount mDstAmount
Definition Pathfinder.h:172
bool issueMatchesOrigin(Issue const &)
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)
static std::uint32_t const afAC_LAST
Definition Pathfinder.h:209
std::optional< AccountID > mSrcIssuer
Definition Pathfinder.h:174
STPathElement mSource
Definition Pathfinder.h:186
static std::uint32_t const afOB_XRP
Definition Pathfinder.h:203
Pathfinder(Pathfinder const &)=delete
std::map< PathType, STPathSet > mPaths
Definition Pathfinder.h:189
hash_map< Issue, int > mPathsOutCountMap
Definition Pathfinder.h:191
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
std::optional< uint256 > mDomain
Definition Pathfinder.h:180
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
AccountID mEffectiveDst
Definition Pathfinder.h:171
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Application & app_
Definition Pathfinder.h:193
int getPathsOut(Currency const &currency, AccountID const &account, LineDirection direction, bool isDestCurrency, AccountID const &dest, std::function< bool(void)> const &continueCallback)
static std::uint32_t const afOB_LAST
Definition Pathfinder.h:206
bool isNoRippleOut(STPath const &currentPath)
beast::Journal const j_
Definition Pathfinder.h:194
STAmount mSrcAmount
Definition Pathfinder.h:175
static void initPathTable()
STAmount mRemainingAmount
The amount remaining from mSrcAccount after the default liquidity has been removed.
Definition Pathfinder.h:178
Pathfinder & operator=(Pathfinder const &)=delete
std::shared_ptr< ReadView const > mLedger
Definition Pathfinder.h:182
static std::uint32_t const afADD_BOOKS
Definition Pathfinder.h:200
std::vector< PathRank > mPathRanks
Definition Pathfinder.h:188
STPathSet mCompletePaths
Definition Pathfinder.h:187
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
AccountID mDstAccount
Definition Pathfinder.h:170
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition TrustLine.h:21