1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/PaymentSandbox.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/RippleStateHelpers.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/IOUAmount.h>
7#include <xrpl/protocol/Quality.h>
8#include <xrpl/protocol/XRPAmount.h>
9#include <xrpl/tx/paths/detail/AmountSpec.h>
10#include <xrpl/tx/paths/detail/StepChecks.h>
11#include <xrpl/tx/paths/detail/Steps.h>
13#include <boost/container/flat_set.hpp>
19template <
class TDerived>
20class XRPEndpointStep :
public StepImp<XRPAmount, XRPAmount, XRPEndpointStep<TDerived>>
85 boost::container::flat_set<uint256>& ofrsToRm,
92 boost::container::flat_set<uint256>& ofrsToRm,
114 <<
"\nAcc: " <<
acc_;
126 return !(lhs == rhs);
217template <
class TDerived>
224template <
class TDerived>
231template <
class TDerived>
236 boost::container::flat_set<uint256>& ofrsToRm,
239 auto const balance =
static_cast<TDerived const*
>(
this)->
xrpLiquid(sb);
244 auto& receiver = isLast_ ? acc_ :
xrpAccount();
249 cache_.emplace(result);
250 return {result, result};
253template <
class TDerived>
258 boost::container::flat_set<uint256>& ofrsToRm,
261 XRPL_ASSERT(cache_,
"xrpl::XRPEndpointStep::fwdImp : cache is set");
262 auto const balance =
static_cast<TDerived const*
>(
this)->
xrpLiquid(sb);
264 auto const result = isLast_ ?
in :
std::min(balance,
in);
267 auto& receiver = isLast_ ? acc_ :
xrpAccount();
272 cache_.emplace(result);
273 return {result, result};
276template <
class TDerived>
282 JLOG(j_.
error()) <<
"Expected valid cache in validFwd";
286 XRPL_ASSERT(
in.native,
"xrpl::XRPEndpointStep::validFwd : input is XRP");
288 auto const& xrpIn =
in.xrp;
289 auto const balance =
static_cast<TDerived const*
>(
this)->
xrpLiquid(sb);
291 if (!isLast_ && balance < xrpIn)
293 JLOG(j_.
warn()) <<
"XRPEndpointStep: Strand re-execute check failed."
294 <<
" Insufficient balance: " <<
to_string(balance)
299 if (xrpIn != *cache_)
301 JLOG(j_.
warn()) <<
"XRPEndpointStep: Strand re-execute check failed."
308template <
class TDerived>
314 JLOG(j_.
debug()) <<
"XRPEndpointStep: specified bad account.";
321 JLOG(j_.
warn()) <<
"XRPEndpointStep: can't send or receive XRP from "
322 "non-existent account: "
338 auto const issuesIndex = isLast_ ? 0 : 1;
341 JLOG(j_.
debug()) <<
"XRPEndpointStep: loop detected: Index: " << ctx.
strandSize <<
' '
358 return xs->acc() == acc;
374 ter = offerCrossingStep->check(ctx);
375 r = std::move(offerCrossingStep);
380 ter = paymentStep->check(ctx);
381 r = std::move(paymentStep);
384 return {ter,
nullptr};
A generic endpoint for log messages.
Writeable view to a ledger, for applying a transaction.
A wrapper which makes credits unavailable to balances.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
static std::uint64_t const uRateOne
A step in a payment path.
static std::int32_t computeReserveReduction(StrandContext const &ctx, AccountID const &acc)
XRPAmount xrpLiquid(ReadView &sb) const
std::int32_t const reserveReduction_
std::string logString() const override
XRPEndpointOfferCrossingStep(StrandContext const &ctx, AccountID const &acc)
XRPAmount xrpLiquid(ReadView &sb) const
XRPEndpointPaymentStep(StrandContext const &ctx, AccountID const &acc)
std::string logString() const override
std::optional< EitherAmount > cachedIn() const override
std::pair< bool, EitherAmount > validFwd(PaymentSandbox &sb, ApplyView &afView, EitherAmount const &in) override
std::optional< EitherAmount > cached() const
XRPEndpointStep(StrandContext const &ctx, AccountID const &acc)
friend bool operator==(XRPEndpointStep< P > const &lhs, XRPEndpointStep< P > const &rhs)
std::optional< EitherAmount > cachedOut() const override
std::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
friend bool operator!=(XRPEndpointStep const &lhs, XRPEndpointStep const &rhs)
std::optional< XRPAmount > cache_
AccountID const & acc() const
XRPAmount xrpLiquidImpl(ReadView &sb, std::int32_t reserveReduction) const
bool equal(Step const &rhs) const override
TER check(StrandContext const &ctx) const
std::pair< XRPAmount, XRPAmount > revImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, XRPAmount const &out)
std::string logStringImpl(char const *name) const
std::pair< std::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection prevStepDir) const override
std::pair< XRPAmount, XRPAmount > fwdImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, XRPAmount const &in)
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet account(AccountID const &id) noexcept
AccountID root.
bool xrpEndpointStepEqual(Step const &step, AccountID const &acc)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
std::pair< TER, std::unique_ptr< Step > > make_XRPEndpointStep(StrandContext const &ctx, AccountID const &acc)
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Currency const & xrpCurrency()
XRP currency.
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const ¤cy)
bool isTesSuccess(TER x) noexcept
AccountID const & xrpAccount()
Compute AccountID from public key.
Context needed to build Strand Steps and for error checking.
size_t const strandSize
Length of Strand.
ReadView const & view
Current ReadView.
bool const isFirst
true if Step is first in Strand
bool const isLast
true if Step is last in Strand
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.
Issue const strandDeliver
Issue strand delivers.
OfferCrossing const offerCrossing
Yes/Sell if offer crossing, not payment.