rippled
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 <ripple/app/paths/Credit.h>
21 #include <ripple/app/paths/Flow.h>
22 #include <ripple/app/paths/impl/AmountSpec.h>
23 #include <ripple/app/paths/impl/StrandFlow.h>
24 #include <ripple/app/paths/impl/Steps.h>
25 #include <ripple/basics/Log.h>
26 #include <ripple/basics/IOUAmount.h>
27 #include <ripple/basics/XRPAmount.h>
28 
29 #include <boost/container/flat_set.hpp>
30 
31 #include <numeric>
32 #include <sstream>
33 
34 namespace ripple {
35 
36 template<class FlowResult>
37 static
38 auto finishFlow (PaymentSandbox& sb,
39  Issue const& srcIssue, Issue const& dstIssue,
40  FlowResult&& f)
41 {
42  path::RippleCalc::Output result;
43  if (f.ter == tesSUCCESS)
44  f.sandbox->apply (sb);
45  else
46  result.removableOffers = std::move (f.removableOffers);
47 
48  result.setResult (f.ter);
49  result.actualAmountIn = toSTAmount (f.in, srcIssue);
50  result.actualAmountOut = toSTAmount (f.out, dstIssue);
51 
52  return result;
53 };
54 
55 path::RippleCalc::Output
56 flow (
57  PaymentSandbox& sb,
58  STAmount const& deliver,
59  AccountID const& src,
60  AccountID const& dst,
61  STPathSet const& paths,
62  bool defaultPaths,
63  bool partialPayment,
64  bool ownerPaysTransferFee,
65  bool offerCrossing,
66  boost::optional<Quality> const& limitQuality,
67  boost::optional<STAmount> const& sendMax,
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  boost::optional<Issue> sendMaxIssue;
82  if (sendMax)
83  sendMaxIssue = sendMax->issue ();
84 
85  // convert the paths to a collection of strands. Each strand is the collection
86  // of account->account steps and book steps that may be used in this payment.
87  auto [toStrandsTer, strands] =
88  toStrands(sb, src, dst, dstIssue, limitQuality, sendMaxIssue, paths,
89  defaultPaths, ownerPaysTransferFee, offerCrossing, j);
90 
91  if (toStrandsTer != tesSUCCESS)
92  {
93  path::RippleCalc::Output result;
94  result.setResult (toStrandsTer);
95  return result;
96  }
97 
98  if (j.trace())
99  {
100  j.trace() << "\nsrc: " << src << "\ndst: " << dst
101  << "\nsrcIssue: " << srcIssue << "\ndstIssue: " << dstIssue;
102  j.trace() << "\nNumStrands: " << strands.size ();
103  for (auto const& curStrand : strands)
104  {
105  j.trace() << "NumSteps: " << curStrand.size ();
106  for (auto const& step : curStrand)
107  {
108  j.trace() << '\n' << *step << '\n';
109  }
110  }
111  }
112 
113  const bool srcIsXRP = isXRP (srcIssue.currency);
114  const bool dstIsXRP = isXRP (dstIssue.currency);
115 
116  auto const asDeliver = toAmountSpec (deliver);
117 
118  // The src account may send either xrp or iou. The dst account may receive
119  // either xrp or iou. Since XRP and IOU amounts are represented by different
120  // types, use templates to tell `flow` about the amount types.
121  if (srcIsXRP && dstIsXRP)
122  {
123  return finishFlow (sb, srcIssue, dstIssue,
124  flow<XRPAmount, XRPAmount> (
125  sb, strands, asDeliver.xrp, partialPayment, offerCrossing,
126  limitQuality, sendMax, j, flowDebugInfo));
127  }
128 
129  if (srcIsXRP && !dstIsXRP)
130  {
131  return finishFlow (sb, srcIssue, dstIssue,
132  flow<XRPAmount, IOUAmount> (
133  sb, strands, asDeliver.iou, partialPayment, offerCrossing,
134  limitQuality, sendMax, j, flowDebugInfo));
135  }
136 
137  if (!srcIsXRP && dstIsXRP)
138  {
139  return finishFlow (sb, srcIssue, dstIssue,
140  flow<IOUAmount, XRPAmount> (
141  sb, strands, asDeliver.xrp, partialPayment, offerCrossing,
142  limitQuality, sendMax, j, flowDebugInfo));
143  }
144 
145  assert (!srcIsXRP && !dstIsXRP);
146  return finishFlow (sb, srcIssue, dstIssue,
147  flow<IOUAmount, IOUAmount> (
148  sb, strands, asDeliver.iou, partialPayment, offerCrossing,
149  limitQuality, sendMax, j, flowDebugInfo));
150 
151 }
152 
153 } // ripple
sstream
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:287
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:48
ripple::toAmountSpec
AmountSpec toAmountSpec(STAmount const &amt)
Definition: AmountSpec.h:176
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:31
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:121
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:97
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::toStrands
std::pair< TER, std::vector< Strand > > toStrands(ReadView const &view, AccountID const &src, AccountID const &dst, Issue const &deliver, boost::optional< Quality > const &limitQuality, boost::optional< Issue > const &sendMax, STPathSet const &paths, bool addDefaultPath, bool ownerPaysTransferFee, bool offerCrossing, beast::Journal j)
Create a Strand for each specified path (including the default path, if indicated)
Definition: PaySteps.cpp:443
numeric
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219