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