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 std::optional<uint256> const& domain,
52 Application& app);
53 Pathfinder(Pathfinder const&) = delete;
55 operator=(Pathfinder const&) = delete;
56 ~Pathfinder() = default;
57
58 static void
60
61 bool
63 int searchLevel,
64 std::function<bool(void)> const& continueCallback = {});
65
67 void
69 int maxPaths,
70 std::function<bool(void)> const& continueCallback = {});
71
72 /* Get the best paths, up to maxPaths in number, from mCompletePaths.
73
74 On return, if fullLiquidityPath is not empty, then it contains the best
75 additional single path which can consume all the liquidity.
76 */
77 STPathSet
79 int maxPaths,
80 STPath& fullLiquidityPath,
81 STPathSet const& extraPaths,
82 AccountID const& srcIssuer,
83 std::function<bool(void)> const& continueCallback = {});
84
85 enum NodeType {
86 nt_SOURCE, // The source account: with an issuer account, if needed.
87 nt_ACCOUNTS, // Accounts that connect from this source/currency.
88 nt_BOOKS, // Order books that connect to this currency.
89 nt_XRP_BOOK, // The order book from this currency to XRP.
90 nt_DEST_BOOK, // The order book to the destination currency/issuer.
91 nt_DESTINATION // The destination account only.
92 };
93
94 // The PathType is a list of the NodeTypes for a path.
96
97 // PaymentType represents the types of the source and destination currencies
98 // in a path request.
103 pt_nonXRP_to_same, // Destination currency is the same as source.
104 pt_nonXRP_to_nonXRP // Destination currency is NOT the same as source.
105 };
106
107 struct PathRank
108 {
112 int index;
113 };
114
115private:
116 /*
117 Call graph of Pathfinder methods.
118
119 findPaths:
120 addPathsForType:
121 addLinks:
122 addLink:
123 getPathsOut
124 issueMatchesOrigin
125 isNoRippleOut:
126 isNoRipple
127
128 computePathRanks:
129 rippleCalculate
130 getPathLiquidity:
131 rippleCalculate
132
133 getBestPaths
134 */
135
136 // Add all paths of one type to mCompletePaths.
137 STPathSet&
139 PathType const& type,
140 std::function<bool(void)> const& continueCallback);
141
142 bool
144
145 int
147 Currency const& currency,
148 AccountID const& account,
149 LineDirection direction,
150 bool isDestCurrency,
151 AccountID const& dest,
152 std::function<bool(void)> const& continueCallback);
153
154 void
155 addLink(
156 STPath const& currentPath,
157 STPathSet& incompletePaths,
158 int addFlags,
159 std::function<bool(void)> const& continueCallback);
160
161 // Call addLink() for each path in currentPaths.
162 void
163 addLinks(
164 STPathSet const& currentPaths,
165 STPathSet& incompletePaths,
166 int addFlags,
167 std::function<bool(void)> const& continueCallback);
168
169 // Compute the liquidity for a path. Return tesSUCCESS if it has has enough
170 // liquidity to be worth keeping, otherwise an error.
171 TER
173 STPath const& path, // IN: The path to check.
174 STAmount const& minDstAmount, // IN: The minimum output this path must
175 // deliver to be worth keeping.
176 STAmount& amountOut, // OUT: The actual liquidity on the path.
177 uint64_t& qualityOut) const; // OUT: The returned initial quality
178
179 // Does this path end on an account-to-account link whose last account has
180 // set the "no ripple" flag on the link?
181 bool
182 isNoRippleOut(STPath const& currentPath);
183
184 // Is the "no ripple" flag set from one account to another?
185 bool
187 AccountID const& fromAccount,
188 AccountID const& toAccount,
189 Currency const& currency);
190
191 void
192 rankPaths(
193 int maxPaths,
194 STPathSet const& paths,
195 std::vector<PathRank>& rankedPaths,
196 std::function<bool(void)> const& continueCallback);
197
200 AccountID mEffectiveDst; // The account the paths need to end at
210
214
219
221
224
225 // Add ripple paths
226 static std::uint32_t const afADD_ACCOUNTS = 0x001;
227
228 // Add order books
229 static std::uint32_t const afADD_BOOKS = 0x002;
230
231 // Add order book to XRP only
232 static std::uint32_t const afOB_XRP = 0x010;
233
234 // Must link to destination currency
235 static std::uint32_t const afOB_LAST = 0x040;
236
237 // Destination account only
238 static std::uint32_t const afAC_LAST = 0x080;
239};
240
241} // namespace ripple
242
243#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:33
Calculates payment paths.
Definition: Pathfinder.h:40
bool issueMatchesOrigin(Issue const &)
Definition: Pathfinder.cpp:709
void rankPaths(int maxPaths, STPathSet const &paths, std::vector< PathRank > &rankedPaths, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:497
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:200
std::optional< AccountID > mSrcIssuer
Definition: Pathfinder.h:203
STAmount mDstAmount
Definition: Pathfinder.h:201
STPathSet mCompletePaths
Definition: Pathfinder.h:216
AccountID mSrcAccount
Definition: Pathfinder.h:198
std::unique_ptr< LoadEvent > m_loadEvent
Definition: Pathfinder.h:212
Pathfinder & operator=(Pathfinder const &)=delete
std::shared_ptr< RippleLineCache > mRLCache
Definition: Pathfinder.h:213
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
Definition: Pathfinder.cpp:349
AccountID mEffectiveDst
Definition: Pathfinder.h:200
std::map< PathType, STPathSet > mPaths
Definition: Pathfinder.h:218
Currency mSrcCurrency
Definition: Pathfinder.h:202
static std::uint32_t const afADD_ACCOUNTS
Definition: Pathfinder.h:226
Application & app_
Definition: Pathfinder.h:222
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
Definition: Pathfinder.cpp:418
STAmount mRemainingAmount
The amount remaining from mSrcAccount after the default liquidity has been removed.
Definition: Pathfinder.h:207
bool isNoRippleOut(STPath const &currentPath)
Definition: Pathfinder.cpp:916
void addLinks(STPathSet const &currentPaths, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:790
static std::uint32_t const afADD_BOOKS
Definition: Pathfinder.h:229
int getPathsOut(Currency const &currency, AccountID const &account, LineDirection direction, bool isDestCurrency, AccountID const &dest, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:720
Pathfinder(Pathfinder const &)=delete
beast::Journal const j_
Definition: Pathfinder.h:223
~Pathfinder()=default
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
Definition: Pathfinder.cpp:899
STPathElement mSource
Definition: Pathfinder.h:215
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:807
static std::uint32_t const afOB_XRP
Definition: Pathfinder.h:232
static void initPathTable()
hash_map< Issue, int > mPathsOutCountMap
Definition: Pathfinder.h:220
std::vector< PathRank > mPathRanks
Definition: Pathfinder.h:217
AccountID mDstAccount
Definition: Pathfinder.h:199
std::optional< uint256 > mDomain
Definition: Pathfinder.h:209
void addLink(STPath const &currentPath, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:951
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:575
static std::uint32_t const afAC_LAST
Definition: Pathfinder.h:238
std::shared_ptr< ReadView const > mLedger
Definition: Pathfinder.h:211
STAmount mSrcAmount
Definition: Pathfinder.h:204
static std::uint32_t const afOB_LAST
Definition: Pathfinder.h:235
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:48
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition: TrustLine.h:42