rippled
RippleCalc.cpp
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 #include <ripple/app/paths/Flow.h>
21 #include <ripple/app/paths/RippleCalc.h>
22 #include <ripple/app/paths/Tuning.h>
23 #include <ripple/app/paths/impl/FlowDebugInfo.h>
24 #include <ripple/basics/Log.h>
25 #include <ripple/ledger/View.h>
26 #include <ripple/protocol/Feature.h>
27 
28 namespace ripple {
29 namespace path {
30 
31 RippleCalc::Output
33  PaymentSandbox& view,
34 
35  // Compute paths using this ledger entry set. Up to caller to actually
36  // apply to ledger.
37 
38  // Issuer:
39  // XRP: xrpAccount()
40  // non-XRP: uSrcAccountID (for any issuer) or another account with
41  // trust node.
42  STAmount const& saMaxAmountReq, // --> -1 = no limit.
43 
44  // Issuer:
45  // XRP: xrpAccount()
46  // non-XRP: uDstAccountID (for any issuer) or another account with
47  // trust node.
48  STAmount const& saDstAmountReq,
49 
50  AccountID const& uDstAccountID,
51  AccountID const& uSrcAccountID,
52 
53  // A set of paths that are included in the transaction that we'll
54  // explore for liquidity.
55  STPathSet const& spsPaths,
56  Logs& l,
57  Input const* const pInputs)
58 {
59  Output flowOut;
60  PaymentSandbox flowSB(&view);
61  auto j = l.journal("Flow");
62 
63  if (!view.rules().enabled(featureFlow))
64  {
65  // The new payment engine was enabled several years ago. New transaction
66  // should never use the old rules. Assume this is a replay
67  j.fatal()
68  << "Old payment rules are required for this transaction. Assuming "
69  "this is a replay and running with the new rules.";
70  }
71 
72  {
73  bool const defaultPaths =
74  !pInputs ? true : pInputs->defaultPathsAllowed;
75 
76  bool const partialPayment =
77  !pInputs ? false : pInputs->partialPaymentAllowed;
78 
79  auto const limitQuality = [&]() -> boost::optional<Quality> {
80  if (pInputs && pInputs->limitQuality &&
81  saMaxAmountReq > beast::zero)
82  return Quality{Amounts(saMaxAmountReq, saDstAmountReq)};
83  return boost::none;
84  }();
85 
86  auto const sendMax = [&]() -> boost::optional<STAmount> {
87  if (saMaxAmountReq >= beast::zero ||
88  saMaxAmountReq.getCurrency() != saDstAmountReq.getCurrency() ||
89  saMaxAmountReq.getIssuer() != uSrcAccountID)
90  {
91  return saMaxAmountReq;
92  }
93  return boost::none;
94  }();
95 
96  bool const ownerPaysTransferFee =
98 
99  try
100  {
101  flowOut = flow(
102  flowSB,
103  saDstAmountReq,
104  uSrcAccountID,
105  uDstAccountID,
106  spsPaths,
107  defaultPaths,
108  partialPayment,
109  ownerPaysTransferFee,
110  /* offerCrossing */ false,
111  limitQuality,
112  sendMax,
113  j,
114  nullptr);
115  }
116  catch (std::exception& e)
117  {
118  JLOG(j.error()) << "Exception from flow: " << e.what();
119 
120  // return a tec so the tx is stored
121  path::RippleCalc::Output exceptResult;
122  exceptResult.setResult(tecINTERNAL);
123  return exceptResult;
124  }
125  }
126 
127  j.debug() << "RippleCalc Result> "
128  << " actualIn: " << flowOut.actualAmountIn
129  << ", actualOut: " << flowOut.actualAmountOut
130  << ", result: " << flowOut.result()
131  << ", dstAmtReq: " << saDstAmountReq
132  << ", sendMax: " << saMaxAmountReq;
133 
134  flowSB.apply(view);
135  return flowOut;
136 }
137 
138 } // namespace path
139 } // namespace ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
std::exception
STL class.
ripple::Logs
Manages partitions for logging.
Definition: Log.h:48
ripple::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition: PaymentSandbox.h:112
ripple::path::RippleCalc::Input
Definition: RippleCalc.h:46
ripple::STPathSet
Definition: STPathSet.h:309
ripple::base_uint< 160, detail::AccountIDTag >
ripple::path::RippleCalc::Input::defaultPathsAllowed
bool defaultPathsAllowed
Definition: RippleCalc.h:51
ripple::STAmount
Definition: STAmount.h:42
ripple::tecINTERNAL
@ tecINTERNAL
Definition: TER.h:271
ripple::path::RippleCalc::Output::actualAmountOut
STAmount actualAmountOut
Definition: RippleCalc.h:63
ripple::path::RippleCalc::Output::actualAmountIn
STAmount actualAmountIn
Definition: RippleCalc.h:60
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:103
ripple::path::RippleCalc::Output
Definition: RippleCalc.h:55
ripple::STAmount::getCurrency
Currency const & getCurrency() const
Definition: STAmount.h:204
ripple::path::RippleCalc::Output::result
TER result() const
Definition: RippleCalc.h:77
ripple::STAmount::getIssuer
AccountID const & getIssuer() const
Definition: STAmount.h:209
ripple::path::RippleCalc::view
PaymentSandbox & view
Definition: RippleCalc.h:117
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Logs::journal
beast::Journal journal(std::string const &name)
Definition: Log.cpp:144
ripple::PaymentSandbox::apply
void apply(RawView &to)
Apply changes to base view.
Definition: PaymentSandbox.cpp:255
ripple::featureFlow
const uint256 featureFlow
Definition: Feature.cpp:164
ripple::path::RippleCalc::rippleCalculate
static Output rippleCalculate(PaymentSandbox &view, STAmount const &saMaxAmountReq, STAmount const &saDstAmountReq, AccountID const &uDstAccountID, AccountID const &uSrcAccountID, STPathSet const &spsPaths, Logs &l, Input const *const pInputs=nullptr)
Definition: RippleCalc.cpp:32
ripple::path::RippleCalc::Input::partialPaymentAllowed
bool partialPaymentAllowed
Definition: RippleCalc.h:50
ripple::flow
path::RippleCalc::Output flow(PaymentSandbox &view, STAmount const &deliver, AccountID const &src, AccountID const &dst, STPathSet const &paths, bool defaultPaths, bool partialPayment, bool ownerPaysTransferFee, bool offerCrossing, boost::optional< Quality > const &limitQuality, boost::optional< STAmount > const &sendMax, beast::Journal j, path::detail::FlowDebugInfo *flowDebugInfo=nullptr)
Make a payment from the src account to the dst account.
ripple::path::RippleCalc::Output::setResult
void setResult(TER const value)
Definition: RippleCalc.h:82
ripple::detail::ApplyViewBase::rules
Rules const & rules() const override
Returns the tx processing rules.
Definition: ApplyViewBase.cpp:53
ripple::featureOwnerPaysFee
const uint256 featureOwnerPaysFee
Definition: Feature.cpp:163
ripple::path::RippleCalc::Input::limitQuality
bool limitQuality
Definition: RippleCalc.h:52
std::exception::what
T what(T... args)