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
25#include <xrpl/basics/Log.h>
26#include <xrpl/protocol/Feature.h>
27
28namespace ripple {
29namespace path {
30
31RippleCalc::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
57 std::optional<uint256> const& domainID,
58 Logs& l,
59 Input const* const pInputs)
60{
61 Output flowOut;
62 PaymentSandbox flowSB(&view);
63 auto j = l.journal("Flow");
64
65 if (!view.rules().enabled(featureFlow))
66 {
67 // The new payment engine was enabled several years ago. New transaction
68 // should never use the old rules. Assume this is a replay
69 j.fatal()
70 << "Old payment rules are required for this transaction. Assuming "
71 "this is a replay and running with the new rules.";
72 }
73
74 {
75 bool const defaultPaths =
76 !pInputs ? true : pInputs->defaultPathsAllowed;
77
78 bool const partialPayment =
79 !pInputs ? false : pInputs->partialPaymentAllowed;
80
81 auto const limitQuality = [&]() -> std::optional<Quality> {
82 if (pInputs && pInputs->limitQuality &&
83 saMaxAmountReq > beast::zero)
84 return Quality{Amounts(saMaxAmountReq, saDstAmountReq)};
85 return std::nullopt;
86 }();
87
88 auto const sendMax = [&]() -> std::optional<STAmount> {
89 if (saMaxAmountReq >= beast::zero ||
90 saMaxAmountReq.getCurrency() != saDstAmountReq.getCurrency() ||
91 saMaxAmountReq.getIssuer() != uSrcAccountID)
92 {
93 return saMaxAmountReq;
94 }
95 return std::nullopt;
96 }();
97
98 bool const ownerPaysTransferFee =
99 view.rules().enabled(featureOwnerPaysFee);
100
101 try
102 {
103 flowOut = flow(
104 flowSB,
105 saDstAmountReq,
106 uSrcAccountID,
107 uDstAccountID,
108 spsPaths,
109 defaultPaths,
110 partialPayment,
111 ownerPaysTransferFee,
113 limitQuality,
114 sendMax,
115 domainID,
116 j,
117 nullptr);
118 }
119 catch (std::exception& e)
120 {
121 JLOG(j.error()) << "Exception from flow: " << e.what();
122
123 // return a tec so the tx is stored
124 path::RippleCalc::Output exceptResult;
125 exceptResult.setResult(tecINTERNAL);
126 return exceptResult;
127 }
128 }
129
130 j.debug() << "RippleCalc Result> "
131 << " actualIn: " << flowOut.actualAmountIn
132 << ", actualOut: " << flowOut.actualAmountOut
133 << ", result: " << flowOut.result()
134 << ", dstAmtReq: " << saDstAmountReq
135 << ", sendMax: " << saMaxAmountReq;
136
137 flowSB.apply(view);
138 return flowOut;
139}
140
141} // namespace path
142} // namespace ripple
Stream fatal() const
Definition: Journal.h:352
Manages partitions for logging.
Definition: Log.h:51
beast::Journal journal(std::string const &name)
Definition: Log.cpp:160
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:130
Currency const & getCurrency() const
Definition: STAmount.h:502
AccountID const & getIssuer() const
Definition: STAmount.h:508
Rules const & rules() const override
Returns the tx processing rules.
PaymentSandbox & view
Definition: RippleCalc.h:120
static Output rippleCalculate(PaymentSandbox &view, STAmount const &saMaxAmountReq, STAmount const &saDstAmountReq, AccountID const &uDstAccountID, AccountID const &uSrcAccountID, STPathSet const &spsPaths, std::optional< uint256 > const &domainID, Logs &l, Input const *const pInputs=nullptr)
Definition: RippleCalc.cpp:32
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:105
@ no
Definition: Steps.h:45
@ tecINTERNAL
Definition: TER.h:310
void setResult(TER const value)
Definition: RippleCalc.h:83
T what(T... args)