rippled
Loading...
Searching...
No Matches
Pathfinder.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_PATHS_PATHFINDER_H_INCLUDED
21#define RIPPLE_APP_PATHS_PATHFINDER_H_INCLUDED
22
23#include <xrpld/app/ledger/Ledger.h>
24#include <xrpld/app/paths/RippleLineCache.h>
25#include <xrpld/core/LoadEvent.h>
26
27#include <xrpl/basics/CountedObject.h>
28#include <xrpl/protocol/STAmount.h>
29#include <xrpl/protocol/STPathSet.h>
30
31namespace ripple {
32
39class Pathfinder : public CountedObject<Pathfinder>
40{
41public:
45 AccountID const& srcAccount,
46 AccountID const& dstAccount,
47 Currency const& uSrcCurrency,
48 std::optional<AccountID> const& uSrcIssuer,
49 STAmount const& dstAmount,
50 std::optional<STAmount> const& srcAmount,
51 Application& app);
52 Pathfinder(Pathfinder const&) = delete;
54 operator=(Pathfinder const&) = delete;
55 ~Pathfinder() = default;
56
57 static void
59
60 bool
62 int searchLevel,
63 std::function<bool(void)> const& continueCallback = {});
64
66 void
68 int maxPaths,
69 std::function<bool(void)> const& continueCallback = {});
70
71 /* Get the best paths, up to maxPaths in number, from mCompletePaths.
72
73 On return, if fullLiquidityPath is not empty, then it contains the best
74 additional single path which can consume all the liquidity.
75 */
76 STPathSet
78 int maxPaths,
79 STPath& fullLiquidityPath,
80 STPathSet const& extraPaths,
81 AccountID const& srcIssuer,
82 std::function<bool(void)> const& continueCallback = {});
83
84 enum NodeType {
85 nt_SOURCE, // The source account: with an issuer account, if needed.
86 nt_ACCOUNTS, // Accounts that connect from this source/currency.
87 nt_BOOKS, // Order books that connect to this currency.
88 nt_XRP_BOOK, // The order book from this currency to XRP.
89 nt_DEST_BOOK, // The order book to the destination currency/issuer.
90 nt_DESTINATION // The destination account only.
91 };
92
93 // The PathType is a list of the NodeTypes for a path.
95
96 // PaymentType represents the types of the source and destination currencies
97 // in a path request.
102 pt_nonXRP_to_same, // Destination currency is the same as source.
103 pt_nonXRP_to_nonXRP // Destination currency is NOT the same as source.
104 };
105
106 struct PathRank
107 {
111 int index;
112 };
113
114private:
115 /*
116 Call graph of Pathfinder methods.
117
118 findPaths:
119 addPathsForType:
120 addLinks:
121 addLink:
122 getPathsOut
123 issueMatchesOrigin
124 isNoRippleOut:
125 isNoRipple
126
127 computePathRanks:
128 rippleCalculate
129 getPathLiquidity:
130 rippleCalculate
131
132 getBestPaths
133 */
134
135 // Add all paths of one type to mCompletePaths.
136 STPathSet&
138 PathType const& type,
139 std::function<bool(void)> const& continueCallback);
140
141 bool
143
144 int
146 Currency const& currency,
147 AccountID const& account,
148 LineDirection direction,
149 bool isDestCurrency,
150 AccountID const& dest,
151 std::function<bool(void)> const& continueCallback);
152
153 void
154 addLink(
155 STPath const& currentPath,
156 STPathSet& incompletePaths,
157 int addFlags,
158 std::function<bool(void)> const& continueCallback);
159
160 // Call addLink() for each path in currentPaths.
161 void
162 addLinks(
163 STPathSet const& currentPaths,
164 STPathSet& incompletePaths,
165 int addFlags,
166 std::function<bool(void)> const& continueCallback);
167
168 // Compute the liquidity for a path. Return tesSUCCESS if it has has enough
169 // liquidity to be worth keeping, otherwise an error.
170 TER
172 STPath const& path, // IN: The path to check.
173 STAmount const& minDstAmount, // IN: The minimum output this path must
174 // deliver to be worth keeping.
175 STAmount& amountOut, // OUT: The actual liquidity on the path.
176 uint64_t& qualityOut) const; // OUT: The returned initial quality
177
178 // Does this path end on an account-to-account link whose last account has
179 // set the "no ripple" flag on the link?
180 bool
181 isNoRippleOut(STPath const& currentPath);
182
183 // Is the "no ripple" flag set from one account to another?
184 bool
186 AccountID const& fromAccount,
187 AccountID const& toAccount,
188 Currency const& currency);
189
190 void
191 rankPaths(
192 int maxPaths,
193 STPathSet const& paths,
194 std::vector<PathRank>& rankedPaths,
195 std::function<bool(void)> const& continueCallback);
196
199 AccountID mEffectiveDst; // The account the paths need to end at
208
212
217
219
222
223 // Add ripple paths
224 static std::uint32_t const afADD_ACCOUNTS = 0x001;
225
226 // Add order books
227 static std::uint32_t const afADD_BOOKS = 0x002;
228
229 // Add order book to XRP only
230 static std::uint32_t const afOB_XRP = 0x010;
231
232 // Must link to destination currency
233 static std::uint32_t const afOB_LAST = 0x040;
234
235 // Destination account only
236 static std::uint32_t const afAC_LAST = 0x080;
237};
238
239} // namespace ripple
240
241#endif
A generic endpoint for log messages.
Definition: Journal.h:60
Tracks the number of instances of an object.
A currency issued by an account.
Definition: Issue.h:36
Calculates payment paths.
Definition: Pathfinder.h:40
bool issueMatchesOrigin(Issue const &)
Definition: Pathfinder.cpp:703
void rankPaths(int maxPaths, STPathSet const &paths, std::vector< PathRank > &rankedPaths, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:491
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:198
std::optional< AccountID > mSrcIssuer
Definition: Pathfinder.h:202
STAmount mDstAmount
Definition: Pathfinder.h:200
STPathSet mCompletePaths
Definition: Pathfinder.h:214
AccountID mSrcAccount
Definition: Pathfinder.h:197
std::unique_ptr< LoadEvent > m_loadEvent
Definition: Pathfinder.h:210
Pathfinder & operator=(Pathfinder const &)=delete
std::shared_ptr< RippleLineCache > mRLCache
Definition: Pathfinder.h:211
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
Definition: Pathfinder.cpp:346
AccountID mEffectiveDst
Definition: Pathfinder.h:199
std::map< PathType, STPathSet > mPaths
Definition: Pathfinder.h:216
Currency mSrcCurrency
Definition: Pathfinder.h:201
static std::uint32_t const afADD_ACCOUNTS
Definition: Pathfinder.h:224
Application & app_
Definition: Pathfinder.h:220
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
Definition: Pathfinder.cpp:413
STAmount mRemainingAmount
The amount remaining from mSrcAccount after the default liquidity has been removed.
Definition: Pathfinder.h:206
bool isNoRippleOut(STPath const &currentPath)
Definition: Pathfinder.cpp:910
void addLinks(STPathSet const &currentPaths, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:784
static std::uint32_t const afADD_BOOKS
Definition: Pathfinder.h:227
int getPathsOut(Currency const &currency, AccountID const &account, LineDirection direction, bool isDestCurrency, AccountID const &dest, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:714
Pathfinder(Pathfinder const &)=delete
beast::Journal const j_
Definition: Pathfinder.h:221
~Pathfinder()=default
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
Definition: Pathfinder.cpp:893
STPathElement mSource
Definition: Pathfinder.h:213
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:801
static std::uint32_t const afOB_XRP
Definition: Pathfinder.h:230
static void initPathTable()
hash_map< Issue, int > mPathsOutCountMap
Definition: Pathfinder.h:218
std::vector< PathRank > mPathRanks
Definition: Pathfinder.h:215
AccountID mDstAccount
Definition: Pathfinder.h:198
void addLink(STPath const &currentPath, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:945
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:569
static std::uint32_t const afAC_LAST
Definition: Pathfinder.h:236
std::shared_ptr< ReadView const > mLedger
Definition: Pathfinder.h:209
STAmount mSrcAmount
Definition: Pathfinder.h:203
static std::uint32_t const afOB_LAST
Definition: Pathfinder.h:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition: TrustLine.h:42