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);
207 ostr << name <<
": " <<
"\nSrc: " <<
src_ <<
"\nDst: " <<
dst_;
215 if (
auto ds =
dynamic_cast<DirectStepI const*
>(&rhs))
292 return issues(prevStepDir);
300 return dstQIn == QUALITY_ONE;
339 auto const& field = [&,
this]() ->
SF_UINT32 const& {
343 if (this->dst_ < this->
src_)
344 return sfLowQualityIn;
346 return sfHighQualityIn;
351 if (this->src_ < this->
dst_)
352 return sfLowQualityOut;
354 return sfHighQualityOut;
358 if (!sle->isFieldPresent(field))
361 auto const q = (*sle)[field];
414 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
421 !((*sleLine)[sfFlags] & authField) &&
422 (*sleLine)[sfBalance] == beast::zero)
425 <<
"DirectStepI: can't receive IOUs from issuer without auth."
434 auto const noRippleSrcToDst =
435 ((*sleLine)[sfFlags] &
437 if (noRippleSrcToDst)
445 if (owed <= beast::zero)
450 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed
451 <<
" limit: " << limit;
472template <
class TDerived>
479 if (srcOwed.signum() > 0)
488template <
class TDerived>
494 return cache_->srcDebtDir;
502template <
class TDerived>
507 boost::container::flat_set<uint256>& ,
512 auto const [maxSrcToDst, srcDebtDir] =
513 static_cast<TDerived const*
>(
this)->maxFlow(sb,
out);
515 auto const [srcQOut, dstQIn] =
518 static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn),
519 "ripple::DirectStepI : valid destination quality");
521 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
523 JLOG(j_.
trace()) <<
"DirectStepI::rev"
524 <<
" srcRedeems: " <<
redeems(srcDebtDir)
526 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
527 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
529 if (maxSrcToDst.signum() <= 0)
531 JLOG(j_.
trace()) <<
"DirectStepI::rev: dry";
537 return {beast::zero, beast::zero};
543 if (srcToDst <= maxSrcToDst)
546 mulRatio(srcToDst, srcQOut, QUALITY_ONE,
true);
547 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
555 JLOG(j_.
trace()) <<
"DirectStepI::rev: Non-limiting"
556 <<
" srcRedeems: " <<
redeems(srcDebtDir)
565 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
567 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
568 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
576 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
577 <<
" srcRedeems: " <<
redeems(srcDebtDir)
579 <<
" srcToDst: " <<
to_string(maxSrcToDst)
581 return {
in, actualOut};
588template <
class TDerived>
596 if (cache_->in < fwdIn)
599 auto const diff = fwdIn - cache_->in;
600 if (diff > smallDiff)
602 if (fwdIn.
exponent() != cache_->in.exponent() ||
603 !cache_->in.mantissa() ||
604 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) >
610 <<
"DirectStepI::fwd: setCacheLimiting"
613 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
614 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst)
616 <<
" cacheOut: " <<
to_string(cache_->out);
617 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
623 if (fwdSrcToDst < cache_->srcToDst)
624 cache_->srcToDst = fwdSrcToDst;
625 if (fwdOut < cache_->
out)
626 cache_->out = fwdOut;
627 cache_->srcDebtDir = srcDebtDir;
630template <
class TDerived>
635 boost::container::flat_set<uint256>& ,
638 XRPL_ASSERT(cache_,
"ripple::DirectStepI::fwdImp : cache is set");
640 auto const [maxSrcToDst, srcDebtDir] =
641 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
643 auto const [srcQOut, dstQIn] =
646 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
648 JLOG(j_.
trace()) <<
"DirectStepI::fwd"
649 <<
" srcRedeems: " <<
redeems(srcDebtDir)
651 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
652 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
654 if (maxSrcToDst.signum() <= 0)
656 JLOG(j_.
trace()) <<
"DirectStepI::fwd: dry";
662 return {beast::zero, beast::zero};
668 if (srcToDst <= maxSrcToDst)
671 mulRatio(srcToDst, dstQIn, QUALITY_ONE,
false);
672 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
680 JLOG(j_.
trace()) <<
"DirectStepI::fwd: Non-limiting"
681 <<
" srcRedeems: " <<
redeems(srcDebtDir)
690 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
692 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
693 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
701 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
702 <<
" srcRedeems: " <<
redeems(srcDebtDir)
707 return {cache_->in, cache_->out};
710template <
class TDerived>
719 JLOG(j_.
trace()) <<
"Expected valid cache in validFwd";
723 auto const savCache = *cache_;
725 XRPL_ASSERT(!
in.native,
"ripple::DirectStepI::validFwd : input is not XRP");
727 auto const [maxSrcToDst, srcDebtDir] =
728 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
733 boost::container::flat_set<uint256> dummy;
734 fwdImp(sb, afView, dummy,
in.iou);
736 catch (FlowException
const&)
741 if (maxSrcToDst < cache_->srcToDst)
743 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
744 <<
" Exceeded max src->dst limit"
745 <<
" max src->dst: " <<
to_string(maxSrcToDst)
746 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
750 if (!(
checkNear(savCache.in, cache_->in) &&
753 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
754 <<
" ExpectedIn: " <<
to_string(savCache.in)
755 <<
" CachedIn: " <<
to_string(cache_->in)
756 <<
" ExpectedOut: " <<
to_string(savCache.out)
757 <<
" CachedOut: " <<
to_string(cache_->out);
764template <
class TDerived>
769 return {QUALITY_ONE, QUALITY_ONE};
771 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
775 if (prevStepQIn > srcQOut)
776 srcQOut = prevStepQIn;
777 return {srcQOut, QUALITY_ONE};
781template <
class TDerived>
790 static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(
791 prevStepDebtDirection),
792 "ripple::DirectStepI::qualitiesSrcIssues : will prevStepDebtDirection "
801 if (isLast_ && dstQIn > QUALITY_ONE)
802 dstQIn = QUALITY_ONE;
803 return {srcQOut, dstQIn};
807template <
class TDerived>
816 return qualitiesSrcRedeems(sb);
820 auto const prevStepDebtDirection = [&] {
822 return prevStep_->debtDirection(sb, strandDir);
825 return qualitiesSrcIssues(sb, prevStepDebtDirection);
829template <
class TDerived>
837template <
class TDerived>
852 auto dstQIn =
static_cast<TDerived const*
>(
this)->quality(
855 if (isLast_ && dstQIn > QUALITY_ONE)
856 dstQIn = QUALITY_ONE;
857 Issue const iss{currency_, src_};
863 auto const [srcQOut, dstQIn] =
redeems(dir)
864 ? qualitiesSrcRedeems(v)
865 : qualitiesSrcIssues(v, prevStepDir);
867 Issue const iss{currency_, src_};
878template <
class TDerived>
885 JLOG(j_.
debug()) <<
"DirectStepI: specified bad account.";
891 JLOG(j_.
debug()) <<
"DirectStepI: same src and dst.";
899 <<
"DirectStepI: can't receive IOUs from non-existent issuer: "
925 Issue const srcIssue{currency_, src_};
926 Issue const dstIssue{currency_, dst_};
933 "ripple::DirectStepI::check : prev seen book without a "
942 if (book->out != srcIssue)
951 <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize
957 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
973 return ds->src() == src && ds->dst() == dst &&
974 ds->currency() == currency;
993 auto offerCrossingStep =
994 std::make_unique<DirectIOfferCrossingStep>(ctx, src, dst, c);
995 ter = offerCrossingStep->check(ctx);
996 r = std::move(offerCrossingStep);
1001 std::make_unique<DirectIPaymentStep>(ctx, src, dst, c);
1002 ter = paymentStep->check(ctx);
1003 r = std::move(paymentStep);
1006 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.