20#include <xrpld/app/paths/Credit.h>
21#include <xrpld/app/paths/detail/StepChecks.h>
22#include <xrpld/app/paths/detail/Steps.h>
23#include <xrpld/ledger/PaymentSandbox.h>
24#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/IOUAmount.h>
27#include <xrpl/protocol/Quality.h>
29#include <boost/container/flat_set.hpp>
36template <
class TDerived>
37class DirectStepI :
public StepImp<IOUAmount, IOUAmount, DirectStepI<TDerived>>
163 boost::container::flat_set<uint256>& ofrsToRm,
170 boost::container::flat_set<uint256>& ofrsToRm,
199 return !(lhs == rhs);
208 <<
"\nSrc: " <<
src_ <<
"\nDst: " <<
dst_;
216 if (
auto ds =
dynamic_cast<DirectStepI const*
>(&rhs))
293 return issues(prevStepDir);
301 return dstQIn == QUALITY_ONE;
340 auto const& field = [&,
this]() ->
SF_UINT32 const& {
344 if (this->dst_ < this->
src_)
345 return sfLowQualityIn;
347 return sfHighQualityIn;
352 if (this->src_ < this->
dst_)
353 return sfLowQualityOut;
355 return sfHighQualityOut;
359 if (!sle->isFieldPresent(field))
362 auto const q = (*sle)[field];
415 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
422 !((*sleLine)[sfFlags] & authField) &&
423 (*sleLine)[sfBalance] == beast::zero)
426 <<
"DirectStepI: can't receive IOUs from issuer without auth."
435 auto const noRippleSrcToDst =
436 ((*sleLine)[sfFlags] &
438 if (noRippleSrcToDst)
446 if (owed <= beast::zero)
451 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed
452 <<
" limit: " << limit;
473template <
class TDerived>
480 if (srcOwed.signum() > 0)
489template <
class TDerived>
495 return cache_->srcDebtDir;
503template <
class TDerived>
508 boost::container::flat_set<uint256>& ,
513 auto const [maxSrcToDst, srcDebtDir] =
514 static_cast<TDerived const*
>(
this)->maxFlow(sb,
out);
516 auto const [srcQOut, dstQIn] =
519 static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn),
520 "ripple::DirectStepI : valid destination quality");
522 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
524 JLOG(j_.
trace()) <<
"DirectStepI::rev"
525 <<
" srcRedeems: " <<
redeems(srcDebtDir)
527 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
528 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
530 if (maxSrcToDst.signum() <= 0)
532 JLOG(j_.
trace()) <<
"DirectStepI::rev: dry";
538 return {beast::zero, beast::zero};
544 if (srcToDst <= maxSrcToDst)
547 mulRatio(srcToDst, srcQOut, QUALITY_ONE,
true);
548 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
556 JLOG(j_.
trace()) <<
"DirectStepI::rev: Non-limiting"
557 <<
" srcRedeems: " <<
redeems(srcDebtDir)
566 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
568 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
569 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
577 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
578 <<
" srcRedeems: " <<
redeems(srcDebtDir)
580 <<
" srcToDst: " <<
to_string(maxSrcToDst)
582 return {
in, actualOut};
589template <
class TDerived>
597 if (cache_->in < fwdIn)
600 auto const diff = fwdIn - cache_->in;
601 if (diff > smallDiff)
603 if (fwdIn.
exponent() != cache_->in.exponent() ||
604 !cache_->in.mantissa() ||
605 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) >
611 <<
"DirectStepI::fwd: setCacheLimiting"
614 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
615 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst)
617 <<
" cacheOut: " <<
to_string(cache_->out);
618 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
624 if (fwdSrcToDst < cache_->srcToDst)
625 cache_->srcToDst = fwdSrcToDst;
626 if (fwdOut < cache_->
out)
627 cache_->out = fwdOut;
628 cache_->srcDebtDir = srcDebtDir;
631template <
class TDerived>
636 boost::container::flat_set<uint256>& ,
639 XRPL_ASSERT(cache_,
"ripple::DirectStepI::fwdImp : cache is set");
641 auto const [maxSrcToDst, srcDebtDir] =
642 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
644 auto const [srcQOut, dstQIn] =
647 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
649 JLOG(j_.
trace()) <<
"DirectStepI::fwd"
650 <<
" srcRedeems: " <<
redeems(srcDebtDir)
652 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
653 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
655 if (maxSrcToDst.signum() <= 0)
657 JLOG(j_.
trace()) <<
"DirectStepI::fwd: dry";
663 return {beast::zero, beast::zero};
669 if (srcToDst <= maxSrcToDst)
672 mulRatio(srcToDst, dstQIn, QUALITY_ONE,
false);
673 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
681 JLOG(j_.
trace()) <<
"DirectStepI::fwd: Non-limiting"
682 <<
" srcRedeems: " <<
redeems(srcDebtDir)
691 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
693 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
694 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
702 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
703 <<
" srcRedeems: " <<
redeems(srcDebtDir)
708 return {cache_->in, cache_->out};
711template <
class TDerived>
720 JLOG(j_.
trace()) <<
"Expected valid cache in validFwd";
724 auto const savCache = *cache_;
726 XRPL_ASSERT(!
in.native,
"ripple::DirectStepI::validFwd : input is not XRP");
728 auto const [maxSrcToDst, srcDebtDir] =
729 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
734 boost::container::flat_set<uint256> dummy;
735 fwdImp(sb, afView, dummy,
in.iou);
737 catch (FlowException
const&)
742 if (maxSrcToDst < cache_->srcToDst)
744 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
745 <<
" Exceeded max src->dst limit"
746 <<
" max src->dst: " <<
to_string(maxSrcToDst)
747 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
751 if (!(
checkNear(savCache.in, cache_->in) &&
754 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
755 <<
" ExpectedIn: " <<
to_string(savCache.in)
756 <<
" CachedIn: " <<
to_string(cache_->in)
757 <<
" ExpectedOut: " <<
to_string(savCache.out)
758 <<
" CachedOut: " <<
to_string(cache_->out);
765template <
class TDerived>
770 return {QUALITY_ONE, QUALITY_ONE};
772 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
776 if (prevStepQIn > srcQOut)
777 srcQOut = prevStepQIn;
778 return {srcQOut, QUALITY_ONE};
782template <
class TDerived>
791 static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(
792 prevStepDebtDirection),
793 "ripple::DirectStepI::qualitiesSrcIssues : will prevStepDebtDirection "
802 if (isLast_ && dstQIn > QUALITY_ONE)
803 dstQIn = QUALITY_ONE;
804 return {srcQOut, dstQIn};
808template <
class TDerived>
817 return qualitiesSrcRedeems(sb);
821 auto const prevStepDebtDirection = [&] {
823 return prevStep_->debtDirection(sb, strandDir);
826 return qualitiesSrcIssues(sb, prevStepDebtDirection);
830template <
class TDerived>
838template <
class TDerived>
853 auto dstQIn =
static_cast<TDerived const*
>(
this)->quality(
856 if (isLast_ && dstQIn > QUALITY_ONE)
857 dstQIn = QUALITY_ONE;
858 Issue const iss{currency_, src_};
864 auto const [srcQOut, dstQIn] =
redeems(dir)
865 ? qualitiesSrcRedeems(v)
866 : qualitiesSrcIssues(v, prevStepDir);
868 Issue const iss{currency_, src_};
879template <
class TDerived>
886 JLOG(j_.
debug()) <<
"DirectStepI: specified bad account.";
892 JLOG(j_.
debug()) <<
"DirectStepI: same src and dst.";
900 <<
"DirectStepI: can't receive IOUs from non-existent issuer: "
926 Issue const srcIssue{currency_, src_};
927 Issue const dstIssue{currency_, dst_};
934 "ripple::DirectStepI::check : prev seen book without a "
943 if (book->out != srcIssue)
952 <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize
958 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
974 return ds->src() == src && ds->dst() == dst &&
975 ds->currency() == currency;
994 auto offerCrossingStep =
995 std::make_unique<DirectIOfferCrossingStep>(ctx, src, dst, c);
996 ter = offerCrossingStep->check(ctx);
997 r = std::move(offerCrossingStep);
1002 std::make_unique<DirectIPaymentStep>(ctx, src, dst, c);
1003 ter = paymentStep->check(ctx);
1004 r = std::move(paymentStep);
1007 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::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
std::string logString() const override
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
bool verifyPrevStepDebtDirection(DebtDirection prevStepDir) const
bool verifyDstQualityIn(std::uint32_t dstQIn) const
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::string logString() const override
std::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
TER check(StrandContext const &ctx, std::shared_ptr< const SLE > const &sleSrc) const
bool verifyPrevStepDebtDirection(DebtDirection) const
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcRedeems(ReadView const &sb) const
std::pair< IOUAmount, DebtDirection > maxPaymentFlow(ReadView const &sb) const
std::optional< EitherAmount > cachedOut() const override
std::pair< IOUAmount, IOUAmount > revImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &out)
std::string logStringImpl(char const *name) const
std::pair< IOUAmount, IOUAmount > fwdImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &in)
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcIssues(ReadView const &sb, DebtDirection prevStepDebtDirection) const
void setCacheLimiting(IOUAmount const &fwdIn, IOUAmount const &fwdSrcToDst, IOUAmount const &fwdOut, DebtDirection srcDebtDir)
std::uint32_t lineQualityIn(ReadView const &v) const override
TER check(StrandContext const &ctx) const
Step const *const prevStep_
DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
AccountID const & src() const
friend bool operator==(DirectStepI const &lhs, DirectStepI const &rhs)
std::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
AccountID const & dst() const
Currency const & currency() const
bool equal(Step const &rhs) const override
std::optional< EitherAmount > cachedIn() const override
std::pair< std::uint32_t, std::uint32_t > qualities(ReadView const &sb, DebtDirection srcDebtDir, StrandDirection strandDir) const
std::optional< Cache > cache_
friend bool operator!=(DirectStepI const &lhs, DirectStepI const &rhs)
std::optional< AccountID > directStepSrcAcct() const override
std::pair< std::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection dir) const override
std::pair< bool, EitherAmount > validFwd(PaymentSandbox &sb, ApplyView &afView, EitherAmount const &in) override
Floating point representation of amounts with high dynamic range.
int exponent() const noexcept
std::int64_t mantissa() 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.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
A step in a payment path.
virtual std::optional< Book > bookStepBook() const
If this step is a BookStep, return the book.
virtual std::optional< AccountID > directStepSrcAcct() const
If this step is DirectStepI (IOU->IOU direct step), return the src account.
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.
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const ¤cy)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
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 toAmount< IOUAmount >(STAmount const &amt)
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.
TER checkNoRipple(ReadView const &view, AccountID const &prev, AccountID const &cur, AccountID const &next, Currency const ¤cy, beast::Journal j)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
bool checkNear(IOUAmount const &expected, IOUAmount const &actual)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
std::pair< TER, std::unique_ptr< Step > > make_DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
std::string to_string(base_uint< Bits, Tag > const &a)
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
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.
size_t const strandSize
Length of Strand.
boost::container::flat_set< Issue > & seenBookOuts
A strand may not include an offer that output the same issue more than once.
ReadView const & view
Current ReadView.
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.
bool const isFirst
true if Step is first in Strand
bool const isLast
true if Step is last in Strand
A field with a type known at compile time.