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/core/LoadEvent.h>
26 #include <ripple/protocol/STAmount.h>
27 #include <ripple/protocol/STPathSet.h>
28 
29 namespace ripple {
30 
38 {
39 public:
41  Pathfinder(
43  AccountID const& srcAccount,
44  AccountID const& dstAccount,
45  Currency const& uSrcCurrency,
46  boost::optional<AccountID> const& uSrcIssuer,
47  STAmount const& dstAmount,
48  boost::optional<STAmount> const& srcAmount,
49  Application& app);
50  Pathfinder(Pathfinder const&) = delete;
51  Pathfinder&
52  operator=(Pathfinder const&) = delete;
53  ~Pathfinder() = default;
54 
55  static void
56  initPathTable();
57 
58  bool
59  findPaths(int searchLevel);
60 
62  void
63  computePathRanks(int maxPaths);
64 
65  /* Get the best paths, up to maxPaths in number, from mCompletePaths.
66 
67  On return, if fullLiquidityPath is not empty, then it contains the best
68  additional single path which can consume all the liquidity.
69  */
70  STPathSet
72  int maxPaths,
73  STPath& fullLiquidityPath,
74  STPathSet const& extraPaths,
75  AccountID const& srcIssuer);
76 
77  enum NodeType {
78  nt_SOURCE, // The source account: with an issuer account, if needed.
79  nt_ACCOUNTS, // Accounts that connect from this source/currency.
80  nt_BOOKS, // Order books that connect to this currency.
81  nt_XRP_BOOK, // The order book from this currency to XRP.
82  nt_DEST_BOOK, // The order book to the destination currency/issuer.
83  nt_DESTINATION // The destination account only.
84  };
85 
86  // The PathType is a list of the NodeTypes for a path.
88 
89  // PaymentType represents the types of the source and destination currencies
90  // in a path request.
91  enum PaymentType {
95  pt_nonXRP_to_same, // Destination currency is the same as source.
96  pt_nonXRP_to_nonXRP // Destination currency is NOT the same as source.
97  };
98 
99  struct PathRank
100  {
104  int index;
105  };
106 
107 private:
108  /*
109  Call graph of Pathfinder methods.
110 
111  findPaths:
112  addPathsForType:
113  addLinks:
114  addLink:
115  getPathsOut
116  issueMatchesOrigin
117  isNoRippleOut:
118  isNoRipple
119 
120  computePathRanks:
121  rippleCalculate
122  getPathLiquidity:
123  rippleCalculate
124 
125  getBestPaths
126  */
127 
128  // Add all paths of one type to mCompletePaths.
129  STPathSet&
130  addPathsForType(PathType const& type);
131 
132  bool
133  issueMatchesOrigin(Issue const&);
134 
135  int
136  getPathsOut(
137  Currency const& currency,
138  AccountID const& account,
139  bool isDestCurrency,
140  AccountID const& dest);
141 
142  void
143  addLink(
144  STPath const& currentPath,
145  STPathSet& incompletePaths,
146  int addFlags);
147 
148  // Call addLink() for each path in currentPaths.
149  void
150  addLinks(
151  STPathSet const& currentPaths,
152  STPathSet& incompletePaths,
153  int addFlags);
154 
155  // Compute the liquidity for a path. Return tesSUCCESS if it has has enough
156  // liquidity to be worth keeping, otherwise an error.
157  TER
159  STPath const& path, // IN: The path to check.
160  STAmount const& minDstAmount, // IN: The minimum output this path must
161  // deliver to be worth keeping.
162  STAmount& amountOut, // OUT: The actual liquidity on the path.
163  uint64_t& qualityOut) const; // OUT: The returned initial quality
164 
165  // Does this path end on an account-to-account link whose last account has
166  // set the "no ripple" flag on the link?
167  bool
168  isNoRippleOut(STPath const& currentPath);
169 
170  // Is the "no ripple" flag set from one account to another?
171  bool
172  isNoRipple(
173  AccountID const& fromAccount,
174  AccountID const& toAccount,
175  Currency const& currency);
176 
177  void
178  rankPaths(
179  int maxPaths,
180  STPathSet const& paths,
181  std::vector<PathRank>& rankedPaths);
182 
185  AccountID mEffectiveDst; // The account the paths need to end at
188  boost::optional<AccountID> mSrcIssuer;
194 
198 
203 
205 
208 
209  // Add ripple paths
210  static std::uint32_t const afADD_ACCOUNTS = 0x001;
211 
212  // Add order books
213  static std::uint32_t const afADD_BOOKS = 0x002;
214 
215  // Add order book to XRP only
216  static std::uint32_t const afOB_XRP = 0x010;
217 
218  // Must link to destination currency
219  static std::uint32_t const afOB_LAST = 0x040;
220 
221  // Destination account only
222  static std::uint32_t const afAC_LAST = 0x080;
223 };
224 
225 } // namespace ripple
226 
227 #endif
ripple::Pathfinder::pt_nonXRP_to_nonXRP
@ pt_nonXRP_to_nonXRP
Definition: Pathfinder.h:96
ripple::Pathfinder::mSrcCurrency
Currency mSrcCurrency
Definition: Pathfinder.h:187
ripple::Application
Definition: Application.h:97
ripple::Pathfinder::mSrcAccount
AccountID mSrcAccount
Definition: Pathfinder.h:183
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:210
ripple::Pathfinder
Calculates payment paths.
Definition: Pathfinder.h:37
std::vector
STL class.
ripple::Pathfinder::afOB_LAST
static const std::uint32_t afOB_LAST
Definition: Pathfinder.h:219
ripple::Pathfinder::nt_XRP_BOOK
@ nt_XRP_BOOK
Definition: Pathfinder.h:81
ripple::Pathfinder::mPaths
std::map< PathType, STPathSet > mPaths
Definition: Pathfinder.h:202
ripple::Pathfinder::PathRank::index
int index
Definition: Pathfinder.h:104
ripple::Pathfinder::mDstAmount
STAmount mDstAmount
Definition: Pathfinder.h:186
ripple::Pathfinder::app_
Application & app_
Definition: Pathfinder.h:206
ripple::Pathfinder::findPaths
bool findPaths(int searchLevel)
Definition: Pathfinder.cpp:187
ripple::Pathfinder::initPathTable
static void initPathTable()
Definition: Pathfinder.cpp:1257
ripple::Pathfinder::PathRank::length
std::uint64_t length
Definition: Pathfinder.h:102
ripple::Pathfinder::Pathfinder
Pathfinder(std::shared_ptr< RippleLineCache > const &cache, AccountID const &srcAccount, AccountID const &dstAccount, Currency const &uSrcCurrency, boost::optional< AccountID > const &uSrcIssuer, STAmount const &dstAmount, boost::optional< STAmount > const &srcAmount, Application &app)
Construct a pathfinder without an issuer.
Definition: Pathfinder.cpp:148
ripple::STPathSet
Definition: STPathSet.h:309
ripple::Pathfinder::pt_XRP_to_XRP
@ pt_XRP_to_XRP
Definition: Pathfinder.h:92
ripple::Pathfinder::isNoRipple
bool isNoRipple(AccountID const &fromAccount, AccountID const &toAccount, Currency const &currency)
Definition: Pathfinder.cpp:854
ripple::Pathfinder::getPathLiquidity
TER getPathLiquidity(STPath const &path, STAmount const &minDstAmount, STAmount &amountOut, uint64_t &qualityOut) const
Definition: Pathfinder.cpp:332
ripple::Pathfinder::NodeType
NodeType
Definition: Pathfinder.h:77
ripple::Pathfinder::afAC_LAST
static const std::uint32_t afAC_LAST
Definition: Pathfinder.h:222
ripple::Pathfinder::getBestPaths
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer)
Definition: Pathfinder.cpp:566
ripple::base_uint< 160, detail::AccountIDTag >
ripple::Pathfinder::operator=
Pathfinder & operator=(Pathfinder const &)=delete
ripple::Pathfinder::mSrcAmount
STAmount mSrcAmount
Definition: Pathfinder.h:189
ripple::Pathfinder::nt_BOOKS
@ nt_BOOKS
Definition: Pathfinder.h:80
ripple::Pathfinder::PathRank::quality
std::uint64_t quality
Definition: Pathfinder.h:101
ripple::Pathfinder::computePathRanks
void computePathRanks(int maxPaths)
Compute the rankings of the paths.
Definition: Pathfinder.cpp:411
ripple::Pathfinder::addPathsForType
STPathSet & addPathsForType(PathType const &type)
Definition: Pathfinder.cpp:783
ripple::TERSubset< CanCvtToTER >
ripple::Pathfinder::addLinks
void addLinks(STPathSet const &currentPaths, STPathSet &incompletePaths, int addFlags)
Definition: Pathfinder.cpp:771
ripple::Pathfinder::mLedger
std::shared_ptr< ReadView const > mLedger
Definition: Pathfinder.h:195
ripple::Pathfinder::rankPaths
void rankPaths(int maxPaths, STPathSet const &paths, std::vector< PathRank > &rankedPaths)
Definition: Pathfinder.cpp:491
ripple::STAmount
Definition: STAmount.h:42
ripple::Pathfinder::mRLCache
std::shared_ptr< RippleLineCache > mRLCache
Definition: Pathfinder.h:197
ripple::Pathfinder::addLink
void addLink(STPath const &currentPath, STPathSet &incompletePaths, int addFlags)
Definition: Pathfinder.cpp:906
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:196
ripple::Pathfinder::mRemainingAmount
STAmount mRemainingAmount
The amount remaining from mSrcAccount after the default liquidity has been removed.
Definition: Pathfinder.h:192
ripple::Pathfinder::getPathsOut
int getPathsOut(Currency const &currency, AccountID const &account, bool isDestCurrency, AccountID const &dest)
Definition: Pathfinder.cpp:704
std::map
STL class.
ripple::Pathfinder::nt_DESTINATION
@ nt_DESTINATION
Definition: Pathfinder.h:83
ripple::Pathfinder::mEffectiveDst
AccountID mEffectiveDst
Definition: Pathfinder.h:185
ripple::Pathfinder::PathRank::liquidity
STAmount liquidity
Definition: Pathfinder.h:103
ripple::Pathfinder::mSrcIssuer
boost::optional< AccountID > mSrcIssuer
Definition: Pathfinder.h:188
ripple::Pathfinder::mDstAccount
AccountID mDstAccount
Definition: Pathfinder.h:184
ripple::Pathfinder::mSource
STPathElement mSource
Definition: Pathfinder.h:199
ripple::Pathfinder::mCompletePaths
STPathSet mCompletePaths
Definition: Pathfinder.h:200
ripple::Pathfinder::pt_XRP_to_nonXRP
@ pt_XRP_to_nonXRP
Definition: Pathfinder.h:93
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:216
ripple::Pathfinder::issueMatchesOrigin
bool issueMatchesOrigin(Issue const &)
Definition: Pathfinder.cpp:693
ripple::STPathElement
Definition: STPathSet.h:33
ripple::Pathfinder::j_
const beast::Journal j_
Definition: Pathfinder.h:207
ripple::Pathfinder::~Pathfinder
~Pathfinder()=default
ripple::Pathfinder::pt_nonXRP_to_same
@ pt_nonXRP_to_same
Definition: Pathfinder.h:95
ripple::Pathfinder::afADD_BOOKS
static const std::uint32_t afADD_BOOKS
Definition: Pathfinder.h:213
ripple::Pathfinder::pt_nonXRP_to_XRP
@ pt_nonXRP_to_XRP
Definition: Pathfinder.h:94
ripple::Pathfinder::isNoRippleOut
bool isNoRippleOut(STPath const &currentPath)
Definition: Pathfinder.cpp:871
ripple::Pathfinder::mPathsOutCountMap
hash_map< Issue, int > mPathsOutCountMap
Definition: Pathfinder.h:204
ripple::Pathfinder::nt_ACCOUNTS
@ nt_ACCOUNTS
Definition: Pathfinder.h:79
ripple::Pathfinder::nt_DEST_BOOK
@ nt_DEST_BOOK
Definition: Pathfinder.h:82
ripple::Pathfinder::PathRank
Definition: Pathfinder.h:99
ripple::Pathfinder::mPathRanks
std::vector< PathRank > mPathRanks
Definition: Pathfinder.h:201
ripple::Pathfinder::PaymentType
PaymentType
Definition: Pathfinder.h:91
std::unique_ptr
STL class.
ripple::STPath
Definition: STPathSet.h:212
std::unordered_map
STL class.
ripple::Pathfinder::nt_SOURCE
@ nt_SOURCE
Definition: Pathfinder.h:78
ripple::Pathfinder::convert_all_
bool convert_all_
Definition: Pathfinder.h:193