rippled
XRPEndpointStep.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/impl/AmountSpec.h>
22 #include <ripple/app/paths/impl/StepChecks.h>
23 #include <ripple/app/paths/impl/Steps.h>
24 #include <ripple/basics/IOUAmount.h>
25 #include <ripple/basics/Log.h>
26 #include <ripple/basics/XRPAmount.h>
27 #include <ripple/ledger/PaymentSandbox.h>
28 #include <ripple/protocol/Feature.h>
29 #include <ripple/protocol/Quality.h>
30 
31 #include <boost/container/flat_set.hpp>
32 
33 #include <numeric>
34 #include <sstream>
35 
36 namespace ripple {
37 
38 template <class TDerived>
40  : public StepImp<XRPAmount, XRPAmount, XRPEndpointStep<TDerived>>
41 {
42 private:
44  bool const isLast_;
46 
47  // Since this step will always be an endpoint in a strand
48  // (either the first or last step) the same cache is used
49  // for cachedIn and cachedOut and only one will ever be used
51 
53  cached() const
54  {
55  if (!cache_)
56  return std::nullopt;
57  return EitherAmount(*cache_);
58  }
59 
60 public:
62  : acc_(acc), isLast_(ctx.isLast), j_(ctx.j)
63  {
64  }
65 
66  AccountID const&
67  acc() const
68  {
69  return acc_;
70  }
71 
73  directStepAccts() const override
74  {
75  if (isLast_)
76  return std::make_pair(xrpAccount(), acc_);
77  return std::make_pair(acc_, xrpAccount());
78  }
79 
81  cachedIn() const override
82  {
83  return cached();
84  }
85 
87  cachedOut() const override
88  {
89  return cached();
90  }
91 
93  debtDirection(ReadView const& sb, StrandDirection dir) const override
94  {
95  return DebtDirection::issues;
96  }
97 
99  qualityUpperBound(ReadView const& v, DebtDirection prevStepDir)
100  const override;
101 
103  revImp(
104  PaymentSandbox& sb,
105  ApplyView& afView,
106  boost::container::flat_set<uint256>& ofrsToRm,
107  XRPAmount const& out);
108 
110  fwdImp(
111  PaymentSandbox& sb,
112  ApplyView& afView,
113  boost::container::flat_set<uint256>& ofrsToRm,
114  XRPAmount const& in);
115 
117  validFwd(PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in)
118  override;
119 
120  // Check for errors and violations of frozen constraints.
121  TER
122  check(StrandContext const& ctx) const;
123 
124 protected:
125  XRPAmount
126  xrpLiquidImpl(ReadView& sb, std::int32_t reserveReduction) const
127  {
128  return ripple::xrpLiquid(sb, acc_, reserveReduction, j_);
129  }
130 
132  logStringImpl(char const* name) const
133  {
134  std::ostringstream ostr;
135  ostr << name << ": "
136  << "\nAcc: " << acc_;
137  return ostr.str();
138  }
139 
140 private:
141  template <class P>
142  friend bool
143  operator==(XRPEndpointStep<P> const& lhs, XRPEndpointStep<P> const& rhs);
144 
145  friend bool
147  {
148  return !(lhs == rhs);
149  }
150 
151  bool
152  equal(Step const& rhs) const override
153  {
154  if (auto ds = dynamic_cast<XRPEndpointStep const*>(&rhs))
155  {
156  return *this == *ds;
157  }
158  return false;
159  }
160 };
161 
162 //------------------------------------------------------------------------------
163 
164 // Flow is used in two different circumstances for transferring funds:
165 // o Payments, and
166 // o Offer crossing.
167 // The rules for handling funds in these two cases are almost, but not
168 // quite, the same.
169 
170 // Payment XRPEndpointStep class (not offer crossing).
171 class XRPEndpointPaymentStep : public XRPEndpointStep<XRPEndpointPaymentStep>
172 {
173 public:
175 
176  XRPAmount
177  xrpLiquid(ReadView& sb) const
178  {
179  return xrpLiquidImpl(sb, 0);
180  ;
181  }
182 
184  logString() const override
185  {
186  return logStringImpl("XRPEndpointPaymentStep");
187  }
188 };
189 
190 // Offer crossing XRPEndpointStep class (not a payment).
192  : public XRPEndpointStep<XRPEndpointOfferCrossingStep>
193 {
194 private:
195  // For historical reasons, offer crossing is allowed to dig further
196  // into the XRP reserve than an ordinary payment. (I believe it's
197  // because the trust line was created after the XRP was removed.)
198  // Return how much the reserve should be reduced.
199  //
200  // Note that reduced reserve only happens if the trust line does not
201  // currently exist.
202  static std::int32_t
204  {
205  if (ctx.isFirst && !ctx.view.read(keylet::line(acc, ctx.strandDeliver)))
206  return -1;
207  return 0;
208  }
209 
210 public:
214  {
215  }
216 
217  XRPAmount
218  xrpLiquid(ReadView& sb) const
219  {
220  return xrpLiquidImpl(sb, reserveReduction_);
221  }
222 
224  logString() const override
225  {
226  return logStringImpl("XRPEndpointOfferCrossingStep");
227  }
228 
229 private:
231 };
232 
233 //------------------------------------------------------------------------------
234 
235 template <class TDerived>
236 inline bool
238  XRPEndpointStep<TDerived> const& lhs,
239  XRPEndpointStep<TDerived> const& rhs)
240 {
241  return lhs.acc_ == rhs.acc_ && lhs.isLast_ == rhs.isLast_;
242 }
243 
244 template <class TDerived>
247  ReadView const& v,
248  DebtDirection prevStepDir) const
249 {
250  return {
251  Quality{STAmount::uRateOne},
252  this->debtDirection(v, StrandDirection::forward)};
253 }
254 
255 template <class TDerived>
258  PaymentSandbox& sb,
259  ApplyView& afView,
260  boost::container::flat_set<uint256>& ofrsToRm,
261  XRPAmount const& out)
262 {
263  auto const balance = static_cast<TDerived const*>(this)->xrpLiquid(sb);
264 
265  auto const result = isLast_ ? out : std::min(balance, out);
266 
267  auto& sender = isLast_ ? xrpAccount() : acc_;
268  auto& receiver = isLast_ ? acc_ : xrpAccount();
269  auto ter = accountSend(sb, sender, receiver, toSTAmount(result), j_);
270  if (ter != tesSUCCESS)
271  return {XRPAmount{beast::zero}, XRPAmount{beast::zero}};
272 
273  cache_.emplace(result);
274  return {result, result};
275 }
276 
277 template <class TDerived>
280  PaymentSandbox& sb,
281  ApplyView& afView,
282  boost::container::flat_set<uint256>& ofrsToRm,
283  XRPAmount const& in)
284 {
285  assert(cache_);
286  auto const balance = static_cast<TDerived const*>(this)->xrpLiquid(sb);
287 
288  auto const result = isLast_ ? in : std::min(balance, in);
289 
290  auto& sender = isLast_ ? xrpAccount() : acc_;
291  auto& receiver = isLast_ ? acc_ : xrpAccount();
292  auto ter = accountSend(sb, sender, receiver, toSTAmount(result), j_);
293  if (ter != tesSUCCESS)
294  return {XRPAmount{beast::zero}, XRPAmount{beast::zero}};
295 
296  cache_.emplace(result);
297  return {result, result};
298 }
299 
300 template <class TDerived>
303  PaymentSandbox& sb,
304  ApplyView& afView,
305  EitherAmount const& in)
306 {
307  if (!cache_)
308  {
309  JLOG(j_.error()) << "Expected valid cache in validFwd";
310  return {false, EitherAmount(XRPAmount(beast::zero))};
311  }
312 
313  assert(in.native);
314 
315  auto const& xrpIn = in.xrp;
316  auto const balance = static_cast<TDerived const*>(this)->xrpLiquid(sb);
317 
318  if (!isLast_ && balance < xrpIn)
319  {
320  JLOG(j_.warn()) << "XRPEndpointStep: Strand re-execute check failed."
321  << " Insufficient balance: " << to_string(balance)
322  << " Requested: " << to_string(xrpIn);
323  return {false, EitherAmount(balance)};
324  }
325 
326  if (xrpIn != *cache_)
327  {
328  JLOG(j_.warn()) << "XRPEndpointStep: Strand re-execute check failed."
329  << " ExpectedIn: " << to_string(*cache_)
330  << " CachedIn: " << to_string(xrpIn);
331  }
332  return {true, in};
333 }
334 
335 template <class TDerived>
336 TER
338 {
339  if (!acc_)
340  {
341  JLOG(j_.debug()) << "XRPEndpointStep: specified bad account.";
342  return temBAD_PATH;
343  }
344 
345  auto sleAcc = ctx.view.read(keylet::account(acc_));
346  if (!sleAcc)
347  {
348  JLOG(j_.warn()) << "XRPEndpointStep: can't send or receive XRP from "
349  "non-existent account: "
350  << acc_;
351  return terNO_ACCOUNT;
352  }
353 
354  if (!ctx.isFirst && !ctx.isLast)
355  {
356  return temBAD_PATH;
357  }
358 
359  auto& src = isLast_ ? xrpAccount() : acc_;
360  auto& dst = isLast_ ? acc_ : xrpAccount();
361  auto ter = checkFreeze(ctx.view, src, dst, xrpCurrency());
362  if (ter != tesSUCCESS)
363  return ter;
364 
365  if (ctx.view.rules().enabled(fix1781))
366  {
367  auto const issuesIndex = isLast_ ? 0 : 1;
368  if (!ctx.seenDirectIssues[issuesIndex].insert(xrpIssue()).second)
369  {
370  JLOG(j_.debug())
371  << "XRPEndpointStep: loop detected: Index: " << ctx.strandSize
372  << ' ' << *this;
373  return temBAD_PATH_LOOP;
374  }
375  }
376 
377  return tesSUCCESS;
378 }
379 
380 //------------------------------------------------------------------------------
381 
382 namespace test {
383 // Needed for testing
384 bool
385 xrpEndpointStepEqual(Step const& step, AccountID const& acc)
386 {
387  if (auto xs =
388  dynamic_cast<XRPEndpointStep<XRPEndpointPaymentStep> const*>(&step))
389  {
390  return xs->acc() == acc;
391  }
392  return false;
393 }
394 } // namespace test
395 
396 //------------------------------------------------------------------------------
397 
400 {
401  TER ter = tefINTERNAL;
403  if (ctx.offerCrossing)
404  {
405  auto offerCrossingStep =
406  std::make_unique<XRPEndpointOfferCrossingStep>(ctx, acc);
407  ter = offerCrossingStep->check(ctx);
408  r = std::move(offerCrossingStep);
409  }
410  else // payment
411  {
412  auto paymentStep = std::make_unique<XRPEndpointPaymentStep>(ctx, acc);
413  ter = paymentStep->check(ctx);
414  r = std::move(paymentStep);
415  }
416  if (ter != tesSUCCESS)
417  return {ter, nullptr};
418 
419  return {tesSUCCESS, std::move(r)};
420 }
421 
422 } // namespace ripple
ripple::StrandContext
Context needed to build Strand Steps and for error checking.
Definition: Steps.h:497
ripple::StrandContext::strandSize
const size_t strandSize
Length of Strand.
Definition: Steps.h:509
ripple::XRPEndpointOfferCrossingStep::computeReserveReduction
static std::int32_t computeReserveReduction(StrandContext const &ctx, AccountID const &acc)
Definition: XRPEndpointStep.cpp:203
sstream
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:155
std::string
STL class.
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
ripple::XRPEndpointOfferCrossingStep::XRPEndpointOfferCrossingStep
XRPEndpointOfferCrossingStep(StrandContext const &ctx, AccountID const &acc)
Definition: XRPEndpointStep.cpp:211
ripple::XRPEndpointStep::fwdImp
std::pair< XRPAmount, XRPAmount > fwdImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, XRPAmount const &in)
Definition: XRPEndpointStep.cpp:279
ripple::StrandContext::strandDeliver
const Issue strandDeliver
Issue strand delivers.
Definition: Steps.h:502
ripple::DebtDirection
DebtDirection
Definition: Steps.h:37
ripple::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition: PaymentSandbox.h:112
std::pair
ripple::XRPEndpointStep::acc
AccountID const & acc() const
Definition: XRPEndpointStep.cpp:67
ripple::fix1781
const uint256 fix1781
ripple::XRPEndpointStep::acc_
AccountID acc_
Definition: XRPEndpointStep.cpp:43
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::XRPEndpointStep::operator!=
friend bool operator!=(XRPEndpointStep const &lhs, XRPEndpointStep const &rhs)
Definition: XRPEndpointStep.cpp:146
ripple::QualityDirection::in
@ in
ripple::XRPEndpointStep::cached
std::optional< EitherAmount > cached() const
Definition: XRPEndpointStep.cpp:53
ripple::temBAD_PATH
@ temBAD_PATH
Definition: TER.h:94
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::XRPEndpointOfferCrossingStep::reserveReduction_
const std::int32_t reserveReduction_
Definition: XRPEndpointStep.cpp:230
ripple::StrandDirection
StrandDirection
Definition: Steps.h:39
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::XRPEndpointStep::cache_
std::optional< XRPAmount > cache_
Definition: XRPEndpointStep.cpp:50
ripple::base_uint< 160, detail::AccountIDTag >
ripple::XRPEndpointOfferCrossingStep
Definition: XRPEndpointStep.cpp:191
ripple::StrandContext::view
ReadView const & view
Current ReadView.
Definition: Steps.h:499
ripple::XRPEndpointStep::cachedOut
std::optional< EitherAmount > cachedOut() const override
Definition: XRPEndpointStep.cpp:87
ripple::QualityDirection::out
@ out
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:133
ripple::StrandContext::offerCrossing
const bool offerCrossing
true if offer crossing, not payment
Definition: Steps.h:507
ripple::XRPEndpointStep::equal
bool equal(Step const &rhs) const override
Definition: XRPEndpointStep.cpp:152
ripple::XRPEndpointStep::operator==
friend bool operator==(XRPEndpointStep< P > const &lhs, XRPEndpointStep< P > const &rhs)
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:30
ripple::TERSubset< CanCvtToTER >
ripple::make_XRPEndpointStep
std::pair< TER, std::unique_ptr< Step > > make_XRPEndpointStep(StrandContext const &ctx, AccountID const &acc)
Definition: XRPEndpointStep.cpp:399
ripple::XRPEndpointStep::logStringImpl
std::string logStringImpl(char const *name) const
Definition: XRPEndpointStep.cpp:132
ripple::Step
A step in a payment path.
Definition: Steps.h:79
ripple::XRPEndpointPaymentStep::logString
std::string logString() const override
Definition: XRPEndpointStep.cpp:184
ripple::XRPEndpointOfferCrossingStep::xrpLiquid
XRPAmount xrpLiquid(ReadView &sb) const
Definition: XRPEndpointStep.cpp:218
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
Definition: View.cpp:1122
ripple::XRPEndpointStep::XRPEndpointStep
XRPEndpointStep(StrandContext const &ctx, AccountID const &acc)
Definition: XRPEndpointStep.cpp:61
ripple::XRPEndpointStep::isLast_
const bool isLast_
Definition: XRPEndpointStep.cpp:44
ripple::xrpAccount
AccountID const & xrpAccount()
Compute AccountID from public key.
Definition: AccountID.cpp:168
beast::Journal::error
Stream error() const
Definition: Journal.h:333
ripple::xrpLiquid
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition: View.cpp:331
ripple::XRPEndpointPaymentStep::xrpLiquid
XRPAmount xrpLiquid(ReadView &sb) const
Definition: XRPEndpointStep.cpp:177
ripple::XRPEndpointStep::xrpLiquidImpl
XRPAmount xrpLiquidImpl(ReadView &sb, std::int32_t reserveReduction) const
Definition: XRPEndpointStep.cpp:126
ripple::StrandContext::isLast
const bool isLast
true if Step is last in Strand
Definition: Steps.h:505
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
ripple::keylet::line
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition: Indexes.cpp:193
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::XRPEndpointStep::cachedIn
std::optional< EitherAmount > cachedIn() const override
Definition: XRPEndpointStep.cpp:81
ripple::terNO_ACCOUNT
@ terNO_ACCOUNT
Definition: TER.h:198
std::min
T min(T... args)
ripple::DebtDirection::issues
@ issues
std::ostringstream
STL class.
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::XRPEndpointStep::check
TER check(StrandContext const &ctx) const
Definition: XRPEndpointStep.cpp:337
ripple::EitherAmount
Definition: AmountSpec.h:59
ripple::StrandContext::isFirst
const bool isFirst
true if Step is first in Strand
Definition: Steps.h:504
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:95
std::optional
ripple::checkFreeze
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const &currency)
Definition: StepChecks.h:32
std::ostringstream::str
T str(T... args)
ripple::XRPEndpointStep
Definition: XRPEndpointStep.cpp:39
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::XRPEndpointStep::validFwd
std::pair< bool, EitherAmount > validFwd(PaymentSandbox &sb, ApplyView &afView, EitherAmount const &in) override
Definition: XRPEndpointStep.cpp:302
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::XRPEndpointStep::j_
const beast::Journal j_
Definition: XRPEndpointStep.cpp:45
std::make_pair
T make_pair(T... args)
ripple::test::xrpEndpointStepEqual
bool xrpEndpointStepEqual(Step const &step, AccountID const &acc)
Definition: XRPEndpointStep.cpp:385
ripple::temBAD_PATH_LOOP
@ temBAD_PATH_LOOP
Definition: TER.h:95
ripple::XRPEndpointOfferCrossingStep::logString
std::string logString() const override
Definition: XRPEndpointStep.cpp:224
ripple::XRPEndpointStep::directStepAccts
std::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
Definition: XRPEndpointStep.cpp:73
numeric
ripple::XRPEndpointStep::revImp
std::pair< XRPAmount, XRPAmount > revImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, XRPAmount const &out)
Definition: XRPEndpointStep.cpp:257
ripple::StrandContext::seenDirectIssues
std::array< boost::container::flat_set< Issue >, 2 > & seenDirectIssues
A strand may not include the same account node more than once in the same currency.
Definition: Steps.h:519
std::unique_ptr
STL class.
ripple::XRPEndpointStep::qualityUpperBound
std::pair< std::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection prevStepDir) const override
Definition: XRPEndpointStep.cpp:246
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
ripple::XRPEndpointStep::debtDirection
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
Definition: XRPEndpointStep.cpp:93
ripple::XRPEndpointPaymentStep
Definition: XRPEndpointStep.cpp:171
ripple::xrpCurrency
Currency const & xrpCurrency()
XRP currency.
Definition: UintTypes.cpp:121
ripple::StrandDirection::forward
@ forward
ripple::STAmount::uRateOne
static const std::uint64_t uRateOne
Definition: STAmount.h:75
ripple::XRPAmount
Definition: XRPAmount.h:46