rippled
Loading...
Searching...
No Matches
Flow.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/AMMContext.h>
21#include <xrpld/app/paths/Credit.h>
22#include <xrpld/app/paths/Flow.h>
23#include <xrpld/app/paths/detail/AmountSpec.h>
24#include <xrpld/app/paths/detail/Steps.h>
25#include <xrpld/app/paths/detail/StrandFlow.h>
26
27#include <xrpl/basics/Log.h>
28#include <xrpl/protocol/IOUAmount.h>
29#include <xrpl/protocol/XRPAmount.h>
30
31namespace ripple {
32
33template <class FlowResult>
34static auto
37 Issue const& srcIssue,
38 Issue const& dstIssue,
39 FlowResult&& f)
40{
42 if (f.ter == tesSUCCESS)
43 f.sandbox->apply(sb);
44 else
45 result.removableOffers = std::move(f.removableOffers);
46
47 result.setResult(f.ter);
48 result.actualAmountIn = toSTAmount(f.in, srcIssue);
49 result.actualAmountOut = toSTAmount(f.out, dstIssue);
50
51 return result;
52};
53
54path::RippleCalc::Output
57 STAmount const& deliver,
58 AccountID const& src,
59 AccountID const& dst,
60 STPathSet const& paths,
61 bool defaultPaths,
62 bool partialPayment,
63 bool ownerPaysTransferFee,
64 OfferCrossing offerCrossing,
65 std::optional<Quality> const& limitQuality,
66 std::optional<STAmount> const& sendMax,
67 std::optional<uint256> const& domainID,
69 path::detail::FlowDebugInfo* flowDebugInfo)
70{
71 Issue const srcIssue = [&] {
72 if (sendMax)
73 return sendMax->issue();
74 if (!isXRP(deliver.issue().currency))
75 return Issue(deliver.issue().currency, src);
76 return xrpIssue();
77 }();
78
79 Issue const dstIssue = deliver.issue();
80
81 std::optional<Issue> sendMaxIssue;
82 if (sendMax)
83 sendMaxIssue = sendMax->issue();
84
85 AMMContext ammContext(src, false);
86
87 // convert the paths to a collection of strands. Each strand is the
88 // collection of account->account steps and book steps that may be used in
89 // this payment.
90 auto [toStrandsTer, strands] = toStrands(
91 sb,
92 src,
93 dst,
94 dstIssue,
95 limitQuality,
96 sendMaxIssue,
97 paths,
98 defaultPaths,
99 ownerPaysTransferFee,
100 offerCrossing,
101 ammContext,
102 domainID,
103 j);
104
105 if (toStrandsTer != tesSUCCESS)
106 {
108 result.setResult(toStrandsTer);
109 return result;
110 }
111
112 ammContext.setMultiPath(strands.size() > 1);
113
114 if (j.trace())
115 {
116 j.trace() << "\nsrc: " << src << "\ndst: " << dst
117 << "\nsrcIssue: " << srcIssue << "\ndstIssue: " << dstIssue;
118 j.trace() << "\nNumStrands: " << strands.size();
119 for (auto const& curStrand : strands)
120 {
121 j.trace() << "NumSteps: " << curStrand.size();
122 for (auto const& step : curStrand)
123 {
124 j.trace() << '\n' << *step << '\n';
125 }
126 }
127 }
128
129 bool const srcIsXRP = isXRP(srcIssue.currency);
130 bool const dstIsXRP = isXRP(dstIssue.currency);
131
132 auto const asDeliver = toAmountSpec(deliver);
133
134 // The src account may send either xrp or iou. The dst account may receive
135 // either xrp or iou. Since XRP and IOU amounts are represented by different
136 // types, use templates to tell `flow` about the amount types.
137 if (srcIsXRP && dstIsXRP)
138 {
139 return finishFlow(
140 sb,
141 srcIssue,
142 dstIssue,
143 flow<XRPAmount, XRPAmount>(
144 sb,
145 strands,
146 asDeliver.xrp,
147 partialPayment,
148 offerCrossing,
149 limitQuality,
150 sendMax,
151 j,
152 ammContext,
153 flowDebugInfo));
154 }
155
156 if (srcIsXRP && !dstIsXRP)
157 {
158 return finishFlow(
159 sb,
160 srcIssue,
161 dstIssue,
162 flow<XRPAmount, IOUAmount>(
163 sb,
164 strands,
165 asDeliver.iou,
166 partialPayment,
167 offerCrossing,
168 limitQuality,
169 sendMax,
170 j,
171 ammContext,
172 flowDebugInfo));
173 }
174
175 if (!srcIsXRP && dstIsXRP)
176 {
177 return finishFlow(
178 sb,
179 srcIssue,
180 dstIssue,
181 flow<IOUAmount, XRPAmount>(
182 sb,
183 strands,
184 asDeliver.xrp,
185 partialPayment,
186 offerCrossing,
187 limitQuality,
188 sendMax,
189 j,
190 ammContext,
191 flowDebugInfo));
192 }
193
194 XRPL_ASSERT(!srcIsXRP && !dstIsXRP, "ripple::flow : neither is XRP");
195 return finishFlow(
196 sb,
197 srcIssue,
198 dstIssue,
199 flow<IOUAmount, IOUAmount>(
200 sb,
201 strands,
202 asDeliver.iou,
203 partialPayment,
204 offerCrossing,
205 limitQuality,
206 sendMax,
207 j,
208 ammContext,
209 flowDebugInfo));
210}
211
212} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Stream trace() const
Severity stream access functions.
Definition Journal.h:322
Maintains AMM info per overall payment engine execution and individual iteration.
Definition AMMContext.h:36
void setMultiPath(bool fs)
Definition AMMContext.h:70
A currency issued by an account.
Definition Issue.h:33
Currency currency
Definition Issue.h:35
A wrapper which makes credits unavailable to balances.
Issue const & issue() const
Definition STAmount.h:496
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:115
bool isXRP(AccountID const &c)
Definition AccountID.h:90
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
static auto finishFlow(PaymentSandbox &sb, Issue const &srcIssue, Issue const &dstIssue, FlowResult &&f)
Definition Flow.cpp:35
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
AmountSpec toAmountSpec(STAmount const &amt)
Definition AmountSpec.h:169
OfferCrossing
Definition Steps.h:45
@ tesSUCCESS
Definition TER.h:245
std::pair< TER, std::vector< Strand > > toStrands(ReadView const &view, AccountID const &src, AccountID const &dst, Issue const &deliver, std::optional< Quality > const &limitQuality, std::optional< Issue > const &sendMax, STPathSet const &paths, bool addDefaultPath, bool ownerPaysTransferFee, OfferCrossing offerCrossing, AMMContext &ammContext, std::optional< uint256 > const &domainID, beast::Journal j)
Create a Strand for each specified path (including the default path, if indicated)
Definition PaySteps.cpp:475
boost::container::flat_set< uint256 > removableOffers
Definition RippleCalc.h:70
void setResult(TER const value)
Definition RippleCalc.h:82