1#include <xrpld/app/paths/detail/StepChecks.h>
2#include <xrpld/app/paths/detail/Steps.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/ledger/Credit.h>
6#include <xrpl/ledger/PaymentSandbox.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/IOUAmount.h>
9#include <xrpl/protocol/Quality.h>
11#include <boost/container/flat_set.hpp>
18template <
class TDerived>
19class DirectStepI :
public StepImp<IOUAmount, IOUAmount, DirectStepI<TDerived>>
154 return !(lhs == rhs);
163 <<
"\nSrc: " <<
src_ <<
"\nDst: " <<
dst_;
171 if (
auto ds =
dynamic_cast<DirectStepI const*
>(&rhs))
247 return issues(prevStepDir);
255 return dstQIn == QUALITY_ONE;
293 auto const& field = [&,
this]() ->
SF_UINT32 const& {
297 if (this->dst_ < this->
src_)
298 return sfLowQualityIn;
300 return sfHighQualityIn;
305 if (this->src_ < this->
dst_)
306 return sfLowQualityOut;
308 return sfHighQualityOut;
312 if (!sle->isFieldPresent(field))
315 auto const q = (*sle)[field];
365 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
371 if (((*sleSrc)[sfFlags] &
lsfRequireAuth) && !((*sleLine)[sfFlags] & authField) &&
372 (*sleLine)[sfBalance] == beast::zero)
374 JLOG(
j_.
debug()) <<
"DirectStepI: can't receive IOUs from issuer without auth."
383 auto const noRippleSrcToDst =
385 if (noRippleSrcToDst)
393 if (owed <= beast::zero)
398 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed <<
" limit: " << limit;
417template <
class TDerived>
423 if (srcOwed.signum() > 0)
430template <
class TDerived>
435 return cache_->srcDebtDir;
441template <
class TDerived>
446 boost::container::flat_set<uint256>& ,
451 auto const [maxSrcToDst, srcDebtDir] =
static_cast<TDerived const*
>(
this)->maxFlow(sb,
out);
455 static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn),
456 "xrpl::DirectStepI : valid destination quality");
458 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
460 JLOG(j_.trace()) <<
"DirectStepI::rev"
462 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst) <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
464 if (maxSrcToDst.signum() <= 0)
466 JLOG(j_.trace()) <<
"DirectStepI::rev: dry";
468 return {beast::zero, beast::zero};
473 if (srcToDst <= maxSrcToDst)
476 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
484 JLOG(j_.trace()) <<
"DirectStepI::rev: Non-limiting"
493 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
501 JLOG(j_.trace()) <<
"DirectStepI::rev: Limiting"
504 return {
in, actualOut};
511template <
class TDerived>
519 if (cache_->in < fwdIn)
522 auto const diff = fwdIn - cache_->in;
523 if (diff > smallDiff)
525 if (fwdIn.
exponent() != cache_->in.exponent() || !cache_->in.mantissa() ||
526 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) > 1.01)
530 JLOG(j_.warn()) <<
"DirectStepI::fwd: setCacheLimiting"
532 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
533 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst) <<
" fwdOut: " <<
to_string(fwdOut)
534 <<
" cacheOut: " <<
to_string(cache_->out);
535 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
541 if (fwdSrcToDst < cache_->srcToDst)
542 cache_->srcToDst = fwdSrcToDst;
543 if (fwdOut < cache_->
out)
544 cache_->out = fwdOut;
545 cache_->srcDebtDir = srcDebtDir;
548template <
class TDerived>
553 boost::container::flat_set<uint256>& ,
556 XRPL_ASSERT(cache_,
"xrpl::DirectStepI::fwdImp : cache is set");
558 auto const [maxSrcToDst, srcDebtDir] =
static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
562 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
564 JLOG(j_.trace()) <<
"DirectStepI::fwd"
566 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst) <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
568 if (maxSrcToDst.signum() <= 0)
570 JLOG(j_.trace()) <<
"DirectStepI::fwd: dry";
572 return {beast::zero, beast::zero};
577 if (srcToDst <= maxSrcToDst)
580 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
588 JLOG(j_.trace()) <<
"DirectStepI::fwd: Non-limiting"
597 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
605 JLOG(j_.trace()) <<
"DirectStepI::rev: Limiting"
609 return {cache_->in, cache_->out};
612template <
class TDerived>
618 JLOG(j_.trace()) <<
"Expected valid cache in validFwd";
622 auto const savCache = *cache_;
624 XRPL_ASSERT(!
in.native,
"xrpl::DirectStepI::validFwd : input is not XRP");
626 auto const [maxSrcToDst, srcDebtDir] =
static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
631 boost::container::flat_set<uint256> dummy;
632 fwdImp(sb, afView, dummy,
in.iou);
634 catch (FlowException
const&)
639 if (maxSrcToDst < cache_->srcToDst)
641 JLOG(j_.warn()) <<
"DirectStepI: Strand re-execute check failed."
642 <<
" Exceeded max src->dst limit"
643 <<
" max src->dst: " <<
to_string(maxSrcToDst)
644 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
650 JLOG(j_.warn()) <<
"DirectStepI: Strand re-execute check failed."
651 <<
" ExpectedIn: " <<
to_string(savCache.in) <<
" CachedIn: " <<
to_string(cache_->in)
652 <<
" ExpectedOut: " <<
to_string(savCache.out) <<
" CachedOut: " <<
to_string(cache_->out);
659template <
class TDerived>
664 return {QUALITY_ONE, QUALITY_ONE};
666 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
669 if (prevStepQIn > srcQOut)
670 srcQOut = prevStepQIn;
671 return {srcQOut, QUALITY_ONE};
675template <
class TDerived>
682 static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(prevStepDebtDirection),
683 "xrpl::DirectStepI::qualitiesSrcIssues : will prevStepDebtDirection "
689 if (isLast_ && dstQIn > QUALITY_ONE)
690 dstQIn = QUALITY_ONE;
691 return {srcQOut, dstQIn};
695template <
class TDerived>
701 return qualitiesSrcRedeems(sb);
705 auto const prevStepDebtDirection = [&] {
707 return prevStep_->debtDirection(sb, strandDir);
710 return qualitiesSrcIssues(sb, prevStepDebtDirection);
714template <
class TDerived>
722template <
class TDerived>
728 auto const [srcQOut, dstQIn] =
redeems(dir) ? qualitiesSrcRedeems(v) : qualitiesSrcIssues(v, prevStepDir);
730 Issue const iss{currency_, src_};
740template <
class TDerived>
747 JLOG(j_.debug()) <<
"DirectStepI: specified bad account.";
753 JLOG(j_.debug()) <<
"DirectStepI: same src and dst.";
760 JLOG(j_.warn()) <<
"DirectStepI: can't receive IOUs from non-existent issuer: " << src_;
784 Issue const srcIssue{currency_, src_};
785 Issue const dstIssue{currency_, dst_};
793 "xrpl::DirectStepI::check : prev seen book without a "
803 if (book->out != srcIssue)
810 JLOG(j_.debug()) <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize <<
' ' << *
this;
815 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
827 return ds->src() == src && ds->dst() == dst && ds->currency() == currency;
843 ter = offerCrossingStep->check(ctx);
844 r = std::move(offerCrossingStep);
849 ter = paymentStep->check(ctx);
850 r = std::move(paymentStep);
853 return {ter,
nullptr};
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
bool verifyDstQualityIn(std::uint32_t dstQIn) const
bool verifyPrevStepDebtDirection(DebtDirection prevStepDir) const
std::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
std::string logString() const override
TER check(StrandContext const &ctx, std::shared_ptr< const SLE > const &sleSrc) const
bool verifyDstQualityIn(std::uint32_t dstQIn) const
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
std::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
bool verifyPrevStepDebtDirection(DebtDirection) const
TER check(StrandContext const &ctx, std::shared_ptr< const SLE > const &sleSrc) const
std::string logString() const override
Step const *const prevStep_
std::pair< IOUAmount, IOUAmount > revImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &out)
Currency const & currency() const
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcIssues(ReadView const &sb, DebtDirection prevStepDebtDirection) const
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcRedeems(ReadView const &sb) const
bool equal(Step const &rhs) const override
DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
std::pair< IOUAmount, IOUAmount > fwdImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &in)
void setCacheLimiting(IOUAmount const &fwdIn, IOUAmount const &fwdSrcToDst, IOUAmount const &fwdOut, DebtDirection srcDebtDir)
AccountID const & src() const
std::optional< EitherAmount > cachedOut() const override
std::optional< AccountID > directStepSrcAcct() const override
AccountID const & dst() const
friend bool operator==(DirectStepI const &lhs, DirectStepI const &rhs)
std::pair< std::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection dir) const override
std::string logStringImpl(char const *name) const
std::uint32_t lineQualityIn(ReadView const &v) const override
std::optional< Cache > cache_
std::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
std::pair< bool, EitherAmount > validFwd(PaymentSandbox &sb, ApplyView &afView, EitherAmount const &in) override
std::pair< std::uint32_t, std::uint32_t > qualities(ReadView const &sb, DebtDirection srcDebtDir, StrandDirection strandDir) const
std::pair< IOUAmount, DebtDirection > maxPaymentFlow(ReadView const &sb) const
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
TER check(StrandContext const &ctx) const
friend bool operator!=(DirectStepI const &lhs, DirectStepI const &rhs)
std::optional< EitherAmount > cachedIn() const override
Floating point representation of amounts with high dynamic range.
mantissa_type mantissa() const noexcept
exponent_type exponent() const noexcept
A currency issued by an account.
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.
A step in a payment path.
virtual std::optional< AccountID > directStepSrcAcct() const
If this step is DirectStepI (IOU->IOU direct step), return the src account.
virtual std::optional< Book > bookStepBook() const
If this step is a BookStep, return the book.
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 directStepEqual(Step const &step, AccountID const &src, AccountID const &dst, Currency const ¤cy)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
STAmount creditLimit(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const ¤cy)
Calculate the maximum amount of IOUs that an account can hold.
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const ¤cy)
STAmount creditBalance(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const ¤cy)
Returns the amount of IOUs issued by issuer that are held by an account.
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
std::pair< TER, std::unique_ptr< Step > > make_DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
bool checkNear(IOUAmount const &expected, IOUAmount const &actual)
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
TER checkNoRipple(ReadView const &view, AccountID const &prev, AccountID const &cur, AccountID const &next, Currency const ¤cy, beast::Journal j)
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static rippleCreditIOU if saAmount represents Issue.
Cache(IOUAmount const &in_, IOUAmount const &srcToDst_, IOUAmount const &out_, DebtDirection srcDebtDir_)
Context needed to build Strand Steps and for error checking.
boost::container::flat_set< Issue > & seenBookOuts
A strand may not include an offer that output the same issue more than once.
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.
Step const *const prevStep
The previous step in the strand.
OfferCrossing const offerCrossing
Yes/Sell if offer crossing, not payment.
A field with a type known at compile time.