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#include <xrpl/basics/CountedObject.h>
27#include <xrpl/protocol/STAmount.h>
28#include <xrpl/protocol/STPathSet.h>
29
30namespace ripple {
31
38class Pathfinder : public CountedObject<Pathfinder>
39{
40public:
44 AccountID const& srcAccount,
45 AccountID const& dstAccount,
46 Currency const& uSrcCurrency,
47 std::optional<AccountID> const& uSrcIssuer,
48 STAmount const& dstAmount,
49 std::optional<STAmount> const& srcAmount,
50 Application& app);
51 Pathfinder(Pathfinder const&) = delete;
53 operator=(Pathfinder const&) = delete;
54 ~Pathfinder() = default;
55
56 static void
58
59 bool
61 int searchLevel,
62 std::function<bool(void)> const& continueCallback = {});
63
65 void
67 int maxPaths,
68 std::function<bool(void)> const& continueCallback = {});
69
70 /* Get the best paths, up to maxPaths in number, from mCompletePaths.
71
72 On return, if fullLiquidityPath is not empty, then it contains the best
73 additional single path which can consume all the liquidity.
74 */
75 STPathSet
77 int maxPaths,
78 STPath& fullLiquidityPath,
79 STPathSet const& extraPaths,
80 AccountID const& srcIssuer,
81 std::function<bool(void)> const& continueCallback = {});
82
83 enum NodeType {
84 nt_SOURCE, // The source account: with an issuer account, if needed.
85 nt_ACCOUNTS, // Accounts that connect from this source/currency.
86 nt_BOOKS, // Order books that connect to this currency.
87 nt_XRP_BOOK, // The order book from this currency to XRP.
88 nt_DEST_BOOK, // The order book to the destination currency/issuer.
89 nt_DESTINATION // The destination account only.
90 };
91
92 // The PathType is a list of the NodeTypes for a path.
94
95 // PaymentType represents the types of the source and destination currencies
96 // in a path request.
101 pt_nonXRP_to_same, // Destination currency is the same as source.
102 pt_nonXRP_to_nonXRP // Destination currency is NOT the same as source.
103 };
104
105 struct PathRank
106 {
110 int index;
111 };
112
113private:
114 /*
115 Call graph of Pathfinder methods.
116
117 findPaths:
118 addPathsForType:
119 addLinks:
120 addLink:
121 getPathsOut
122 issueMatchesOrigin
123 isNoRippleOut:
124 isNoRipple
125
126 computePathRanks:
127 rippleCalculate
128 getPathLiquidity:
129 rippleCalculate
130
131 getBestPaths
132 */
133
134 // Add all paths of one type to mCompletePaths.
135 STPathSet&
137 PathType const& type,
138 std::function<bool(void)> const& continueCallback);
139
140 bool
142
143 int
145 Currency const& currency,
146 AccountID const& account,
147 LineDirection direction,
148 bool isDestCurrency,
149 AccountID const& dest,
150 std::function<bool(void)> const& continueCallback);
151
152 void
153 addLink(
154 STPath const& currentPath,
155 STPathSet& incompletePaths,
156 int addFlags,
157 std::function<bool(void)> const& continueCallback);
158
159 // Call addLink() for each path in currentPaths.
160 void
161 addLinks(
162 STPathSet const& currentPaths,
163 STPathSet& incompletePaths,
164 int addFlags,
165 std::function<bool(void)> const& continueCallback);
166
167 // Compute the liquidity for a path. Return tesSUCCESS if it has has enough
168 // liquidity to be worth keeping, otherwise an error.
169 TER
171 STPath const& path, // IN: The path to check.
172 STAmount const& minDstAmount, // IN: The minimum output this path must
173 // deliver to be worth keeping.
174 STAmount& amountOut, // OUT: The actual liquidity on the path.
175 uint64_t& qualityOut) const; // OUT: The returned initial quality
176
177 // Does this path end on an account-to-account link whose last account has
178 // set the "no ripple" flag on the link?
179 bool
180 isNoRippleOut(STPath const& currentPath);
181
182 // Is the "no ripple" flag set from one account to another?
183 bool
185 AccountID const& fromAccount,
186 AccountID const& toAccount,
187 Currency const& currency);
188
189 void
190 rankPaths(
191 int maxPaths,
192 STPathSet const& paths,
193 std::vector<PathRank>& rankedPaths,
194 std::function<bool(void)> const& continueCallback);
195
198 AccountID mEffectiveDst; // The account the paths need to end at
207
211
216
218
221
222 // Add ripple paths
223 static std::uint32_t const afADD_ACCOUNTS = 0x001;
224
225 // Add order books
226 static std::uint32_t const afADD_BOOKS = 0x002;
227
228 // Add order book to XRP only
229 static std::uint32_t const afOB_XRP = 0x010;
230
231 // Must link to destination currency
232 static std::uint32_t const afOB_LAST = 0x040;
233
234 // Destination account only
235 static std::uint32_t const afAC_LAST = 0x080;
236};
237
238} // namespace ripple
239
240#endif
A generic endpoint for log messages.
Definition: Journal.h:59
Tracks the number of instances of an object.
A currency issued by an account.
Definition: Issue.h:36
Calculates payment paths.
Definition: Pathfinder.h:39
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:201
STAmount mDstAmount
Definition: Pathfinder.h:199
STPathSet mCompletePaths
Definition: Pathfinder.h:213
AccountID mSrcAccount
Definition: Pathfinder.h:196
std::unique_ptr< LoadEvent > m_loadEvent
Definition: Pathfinder.h:209
Pathfinder & operator=(Pathfinder const &)=delete
std::shared_ptr< RippleLineCache > mRLCache
Definition: Pathfinder.h:210
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
Definition: Pathfinder.cpp:346
AccountID mEffectiveDst
Definition: Pathfinder.h:198
std::map< PathType, STPathSet > mPaths
Definition: Pathfinder.h:215
Currency mSrcCurrency
Definition: Pathfinder.h:200
static std::uint32_t const afADD_ACCOUNTS
Definition: Pathfinder.h:223
Application & app_
Definition: Pathfinder.h:219
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:205
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:226
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:220
~Pathfinder()=default
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
Definition: Pathfinder.cpp:893
STPathElement mSource
Definition: Pathfinder.h:212
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:229
static void initPathTable()
hash_map< Issue, int > mPathsOutCountMap
Definition: Pathfinder.h:217
std::vector< PathRank > mPathRanks
Definition: Pathfinder.h:214
AccountID mDstAccount
Definition: Pathfinder.h:197
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:235
std::shared_ptr< ReadView const > mLedger
Definition: Pathfinder.h:208
STAmount mSrcAmount
Definition: Pathfinder.h:202
static std::uint32_t const afOB_LAST
Definition: Pathfinder.h:232
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:41