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