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