rippled
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 <ripple/app/ledger/Ledger.h>
24 #include <ripple/app/paths/RippleLineCache.h>
25 #include <ripple/basics/CountedObject.h>
26 #include <ripple/core/LoadEvent.h>
27 #include <ripple/protocol/STAmount.h>
28 #include <ripple/protocol/STPathSet.h>
29 
30 namespace ripple {
31 
38 class Pathfinder : public CountedObject<Pathfinder>
39 {
40 public:
42  Pathfinder(
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;
52  Pathfinder&
53  operator=(Pathfinder const&) = delete;
54  ~Pathfinder() = default;
55 
56  static void
57  initPathTable();
58 
59  bool
60  findPaths(
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.
97  enum PaymentType {
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 
113 private:
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
141  issueMatchesOrigin(Issue const&);
142 
143  int
144  getPathsOut(
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
184  isNoRipple(
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
ripple::Pathfinder::addPathsForType
STPathSet & addPathsForType(PathType const &type, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:795
ripple::Pathfinder::pt_nonXRP_to_nonXRP
@ pt_nonXRP_to_nonXRP
Definition: Pathfinder.h:102
ripple::Pathfinder::mSrcCurrency
Currency mSrcCurrency
Definition: Pathfinder.h:200
ripple::Application
Definition: Application.h:115
ripple::Pathfinder::mSrcAccount
AccountID mSrcAccount
Definition: Pathfinder.h:196
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::Pathfinder::addLink
void addLink(STPath const &currentPath, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:937
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
std::shared_ptr
STL class.
ripple::Pathfinder::afADD_ACCOUNTS
static const std::uint32_t afADD_ACCOUNTS
Definition: Pathfinder.h:223
ripple::LineDirection
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition: TrustLine.h:41
ripple::Pathfinder
Calculates payment paths.
Definition: Pathfinder.h:38
ripple::Pathfinder::addLinks
void addLinks(STPathSet const &currentPaths, STPathSet &incompletePaths, int addFlags, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:778
ripple::Pathfinder::computePathRanks
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
Definition: Pathfinder.cpp:411
std::vector
STL class.
ripple::Pathfinder::afOB_LAST
static const std::uint32_t afOB_LAST
Definition: Pathfinder.h:232
ripple::Pathfinder::nt_XRP_BOOK
@ nt_XRP_BOOK
Definition: Pathfinder.h:87
ripple::Pathfinder::mPaths
std::map< PathType, STPathSet > mPaths
Definition: Pathfinder.h:215
ripple::Pathfinder::PathRank::index
int index
Definition: Pathfinder.h:110
ripple::Pathfinder::mDstAmount
STAmount mDstAmount
Definition: Pathfinder.h:199
ripple::Pathfinder::app_
Application & app_
Definition: Pathfinder.h:219
ripple::Pathfinder::initPathTable
static void initPathTable()
Definition: Pathfinder.cpp:1300
ripple::Pathfinder::findPaths
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:195
std::function
ripple::Pathfinder::PathRank::length
std::uint64_t length
Definition: Pathfinder.h:108
ripple::Pathfinder::getPathsOut
int getPathsOut(Currency const &currency, AccountID const &account, LineDirection direction, bool isDestCurrency, AccountID const &dest, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:708
ripple::STPathSet
Definition: STPathSet.h:176
ripple::Pathfinder::pt_XRP_to_XRP
@ pt_XRP_to_XRP
Definition: Pathfinder.h:98
ripple::Pathfinder::isNoRipple
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
Definition: Pathfinder.cpp:885
ripple::Pathfinder::getPathLiquidity
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
Definition: Pathfinder.cpp:344
ripple::Pathfinder::NodeType
NodeType
Definition: Pathfinder.h:83
ripple::Pathfinder::afAC_LAST
static const std::uint32_t afAC_LAST
Definition: Pathfinder.h:235
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::Pathfinder::operator=
Pathfinder & operator=(Pathfinder const &)=delete
ripple::Pathfinder::mSrcAmount
STAmount mSrcAmount
Definition: Pathfinder.h:202
ripple::Pathfinder::nt_BOOKS
@ nt_BOOKS
Definition: Pathfinder.h:86
ripple::Pathfinder::PathRank::quality
std::uint64_t quality
Definition: Pathfinder.h:107
ripple::Pathfinder::mSrcIssuer
std::optional< AccountID > mSrcIssuer
Definition: Pathfinder.h:201
ripple::TERSubset< CanCvtToTER >
ripple::Pathfinder::mLedger
std::shared_ptr< ReadView const > mLedger
Definition: Pathfinder.h:208
ripple::STAmount
Definition: STAmount.h:45
ripple::Pathfinder::mRLCache
std::shared_ptr< RippleLineCache > mRLCache
Definition: Pathfinder.h:210
ripple::Pathfinder::Pathfinder
Pathfinder(std::shared_ptr< RippleLineCache > const &cache, AccountID const &srcAccount, AccountID const &dstAccount, Currency const &uSrcCurrency, std::optional< AccountID > const &uSrcIssuer, STAmount const &dstAmount, std::optional< STAmount > const &srcAmount, Application &app)
Construct a pathfinder without an issuer.
Definition: Pathfinder.cpp:161
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint64_t
ripple::Pathfinder::m_loadEvent
std::unique_ptr< LoadEvent > m_loadEvent
Definition: Pathfinder.h:209
ripple::Pathfinder::mRemainingAmount
STAmount mRemainingAmount
The amount remaining from mSrcAccount after the default liquidity has been removed.
Definition: Pathfinder.h:205
std::map
STL class.
ripple::Pathfinder::nt_DESTINATION
@ nt_DESTINATION
Definition: Pathfinder.h:89
ripple::Pathfinder::mEffectiveDst
AccountID mEffectiveDst
Definition: Pathfinder.h:198
ripple::Pathfinder::PathRank::liquidity
STAmount liquidity
Definition: Pathfinder.h:109
ripple::Pathfinder::mDstAccount
AccountID mDstAccount
Definition: Pathfinder.h:197
ripple::Pathfinder::mSource
STPathElement mSource
Definition: Pathfinder.h:212
ripple::Pathfinder::mCompletePaths
STPathSet mCompletePaths
Definition: Pathfinder.h:213
ripple::Pathfinder::getBestPaths
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:567
ripple::Pathfinder::pt_XRP_to_nonXRP
@ pt_XRP_to_nonXRP
Definition: Pathfinder.h:99
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Pathfinder::afOB_XRP
static const std::uint32_t afOB_XRP
Definition: Pathfinder.h:229
ripple::Pathfinder::issueMatchesOrigin
bool issueMatchesOrigin(Issue const &)
Definition: Pathfinder.cpp:697
ripple::STPathElement
Definition: STPathSet.h:34
ripple::Pathfinder::j_
const beast::Journal j_
Definition: Pathfinder.h:220
ripple::Pathfinder::~Pathfinder
~Pathfinder()=default
ripple::Pathfinder::pt_nonXRP_to_same
@ pt_nonXRP_to_same
Definition: Pathfinder.h:101
ripple::Pathfinder::afADD_BOOKS
static const std::uint32_t afADD_BOOKS
Definition: Pathfinder.h:226
ripple::Pathfinder::pt_nonXRP_to_XRP
@ pt_nonXRP_to_XRP
Definition: Pathfinder.h:100
ripple::Pathfinder::isNoRippleOut
bool isNoRippleOut(STPath const &currentPath)
Definition: Pathfinder.cpp:902
std::optional
ripple::Pathfinder::mPathsOutCountMap
hash_map< Issue, int > mPathsOutCountMap
Definition: Pathfinder.h:217
ripple::Pathfinder::nt_ACCOUNTS
@ nt_ACCOUNTS
Definition: Pathfinder.h:85
ripple::Pathfinder::nt_DEST_BOOK
@ nt_DEST_BOOK
Definition: Pathfinder.h:88
ripple::Pathfinder::PathRank
Definition: Pathfinder.h:105
ripple::Pathfinder::mPathRanks
std::vector< PathRank > mPathRanks
Definition: Pathfinder.h:214
ripple::Pathfinder::PaymentType
PaymentType
Definition: Pathfinder.h:97
ripple::Pathfinder::rankPaths
void rankPaths(int maxPaths, STPathSet const &paths, std::vector< PathRank > &rankedPaths, std::function< bool(void)> const &continueCallback)
Definition: Pathfinder.cpp:489
std::unique_ptr
STL class.
ripple::STPath
Definition: STPathSet.h:118
std::unordered_map
STL class.
ripple::Pathfinder::nt_SOURCE
@ nt_SOURCE
Definition: Pathfinder.h:84
ripple::Pathfinder::convert_all_
bool convert_all_
Definition: Pathfinder.h:206