1#include <xrpld/app/paths/Credit.h>
2#include <xrpld/app/paths/detail/StepChecks.h>
3#include <xrpld/app/paths/detail/Steps.h>
5#include <xrpl/basics/Log.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>>
145 boost::container::flat_set<uint256>& ofrsToRm,
152 boost::container::flat_set<uint256>& ofrsToRm,
181 return !(lhs == rhs);
190 <<
"\nSrc: " <<
src_ <<
"\nDst: " <<
dst_;
198 if (
auto ds =
dynamic_cast<DirectStepI const*
>(&rhs))
275 return issues(prevStepDir);
283 return dstQIn == QUALITY_ONE;
322 auto const& field = [&,
this]() ->
SF_UINT32 const& {
326 if (this->dst_ < this->
src_)
327 return sfLowQualityIn;
329 return sfHighQualityIn;
334 if (this->src_ < this->
dst_)
335 return sfLowQualityOut;
337 return sfHighQualityOut;
341 if (!sle->isFieldPresent(field))
344 auto const q = (*sle)[field];
397 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
404 !((*sleLine)[sfFlags] & authField) &&
405 (*sleLine)[sfBalance] == beast::zero)
408 <<
"DirectStepI: can't receive IOUs from issuer without auth."
417 auto const noRippleSrcToDst =
418 ((*sleLine)[sfFlags] &
420 if (noRippleSrcToDst)
428 if (owed <= beast::zero)
433 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed
434 <<
" limit: " << limit;
455template <
class TDerived>
462 if (srcOwed.signum() > 0)
471template <
class TDerived>
477 return cache_->srcDebtDir;
485template <
class TDerived>
490 boost::container::flat_set<uint256>& ,
495 auto const [maxSrcToDst, srcDebtDir] =
496 static_cast<TDerived const*
>(
this)->maxFlow(sb,
out);
498 auto const [srcQOut, dstQIn] =
501 static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn),
502 "ripple::DirectStepI : valid destination quality");
504 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
506 JLOG(j_.
trace()) <<
"DirectStepI::rev"
507 <<
" srcRedeems: " <<
redeems(srcDebtDir)
509 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
510 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
512 if (maxSrcToDst.signum() <= 0)
514 JLOG(j_.
trace()) <<
"DirectStepI::rev: dry";
520 return {beast::zero, beast::zero};
526 if (srcToDst <= maxSrcToDst)
529 mulRatio(srcToDst, srcQOut, QUALITY_ONE,
true);
530 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
538 JLOG(j_.
trace()) <<
"DirectStepI::rev: Non-limiting"
539 <<
" srcRedeems: " <<
redeems(srcDebtDir)
548 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
550 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
551 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
559 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
560 <<
" srcRedeems: " <<
redeems(srcDebtDir)
562 <<
" srcToDst: " <<
to_string(maxSrcToDst)
564 return {
in, actualOut};
571template <
class TDerived>
579 if (cache_->in < fwdIn)
582 auto const diff = fwdIn - cache_->in;
583 if (diff > smallDiff)
585 if (fwdIn.
exponent() != cache_->in.exponent() ||
586 !cache_->in.mantissa() ||
587 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) >
593 <<
"DirectStepI::fwd: setCacheLimiting"
596 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
597 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst)
599 <<
" cacheOut: " <<
to_string(cache_->out);
600 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
606 if (fwdSrcToDst < cache_->srcToDst)
607 cache_->srcToDst = fwdSrcToDst;
608 if (fwdOut < cache_->
out)
609 cache_->out = fwdOut;
610 cache_->srcDebtDir = srcDebtDir;
613template <
class TDerived>
618 boost::container::flat_set<uint256>& ,
621 XRPL_ASSERT(cache_,
"ripple::DirectStepI::fwdImp : cache is set");
623 auto const [maxSrcToDst, srcDebtDir] =
624 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
626 auto const [srcQOut, dstQIn] =
629 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
631 JLOG(j_.
trace()) <<
"DirectStepI::fwd"
632 <<
" srcRedeems: " <<
redeems(srcDebtDir)
634 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
635 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
637 if (maxSrcToDst.signum() <= 0)
639 JLOG(j_.
trace()) <<
"DirectStepI::fwd: dry";
645 return {beast::zero, beast::zero};
651 if (srcToDst <= maxSrcToDst)
654 mulRatio(srcToDst, dstQIn, QUALITY_ONE,
false);
655 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
663 JLOG(j_.
trace()) <<
"DirectStepI::fwd: Non-limiting"
664 <<
" srcRedeems: " <<
redeems(srcDebtDir)
673 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
675 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
676 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
684 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
685 <<
" srcRedeems: " <<
redeems(srcDebtDir)
690 return {cache_->in, cache_->out};
693template <
class TDerived>
702 JLOG(j_.
trace()) <<
"Expected valid cache in validFwd";
706 auto const savCache = *cache_;
708 XRPL_ASSERT(!
in.native,
"ripple::DirectStepI::validFwd : input is not XRP");
710 auto const [maxSrcToDst, srcDebtDir] =
711 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
716 boost::container::flat_set<uint256> dummy;
717 fwdImp(sb, afView, dummy,
in.iou);
719 catch (FlowException
const&)
724 if (maxSrcToDst < cache_->srcToDst)
726 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
727 <<
" Exceeded max src->dst limit"
728 <<
" max src->dst: " <<
to_string(maxSrcToDst)
729 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
733 if (!(
checkNear(savCache.in, cache_->in) &&
736 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
737 <<
" ExpectedIn: " <<
to_string(savCache.in)
738 <<
" CachedIn: " <<
to_string(cache_->in)
739 <<
" ExpectedOut: " <<
to_string(savCache.out)
740 <<
" CachedOut: " <<
to_string(cache_->out);
747template <
class TDerived>
752 return {QUALITY_ONE, QUALITY_ONE};
754 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
758 if (prevStepQIn > srcQOut)
759 srcQOut = prevStepQIn;
760 return {srcQOut, QUALITY_ONE};
764template <
class TDerived>
773 static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(
774 prevStepDebtDirection),
775 "ripple::DirectStepI::qualitiesSrcIssues : will prevStepDebtDirection "
784 if (isLast_ && dstQIn > QUALITY_ONE)
785 dstQIn = QUALITY_ONE;
786 return {srcQOut, dstQIn};
790template <
class TDerived>
799 return qualitiesSrcRedeems(sb);
803 auto const prevStepDebtDirection = [&] {
805 return prevStep_->debtDirection(sb, strandDir);
808 return qualitiesSrcIssues(sb, prevStepDebtDirection);
812template <
class TDerived>
820template <
class TDerived>
828 auto const [srcQOut, dstQIn] =
redeems(dir)
829 ? qualitiesSrcRedeems(v)
830 : qualitiesSrcIssues(v, prevStepDir);
832 Issue const iss{currency_, src_};
843template <
class TDerived>
850 JLOG(j_.
debug()) <<
"DirectStepI: specified bad account.";
856 JLOG(j_.
debug()) <<
"DirectStepI: same src and dst.";
864 <<
"DirectStepI: can't receive IOUs from non-existent issuer: "
890 Issue const srcIssue{currency_, src_};
891 Issue const dstIssue{currency_, dst_};
899 "ripple::DirectStepI::check : prev seen book without a "
909 if (book->out != srcIssue)
918 <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize
924 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
940 return ds->src() == src && ds->dst() == dst &&
941 ds->currency() == currency;
960 auto offerCrossingStep =
962 ter = offerCrossingStep->check(ctx);
963 r = std::move(offerCrossingStep);
969 ter = paymentStep->check(ctx);
970 r = std::move(paymentStep);
973 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.
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)
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)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
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.
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)
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.