20 #include <ripple/app/paths/Credit.h>
21 #include <ripple/app/paths/impl/StepChecks.h>
22 #include <ripple/app/paths/impl/Steps.h>
23 #include <ripple/basics/IOUAmount.h>
24 #include <ripple/basics/Log.h>
25 #include <ripple/ledger/PaymentSandbox.h>
26 #include <ripple/protocol/Feature.h>
27 #include <ripple/protocol/Quality.h>
29 #include <boost/container/flat_set.hpp>
36 template <
class TDerived>
37 class 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))
292 return issues(prevStepDir);
300 return dstQIn == QUALITY_ONE;
339 auto const& field = [
this, qDir]() ->
SF_UINT32 const& {
343 if (this->dst_ < this->
src_)
351 if (this->src_ < this->
dst_)
358 if (!sle->isFieldPresent(field))
361 auto const q = (*sle)[field];
414 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
421 !((*sleLine)[
sfFlags] & authField) &&
425 <<
"DirectStepI: can't receive IOUs from issuer without auth."
434 auto const noRippleSrcToDst =
437 if (noRippleSrcToDst)
445 if (owed <= beast::zero)
450 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed
451 <<
" limit: " << limit;
472 template <
class TDerived>
479 if (srcOwed.signum() > 0)
488 template <
class TDerived>
494 return cache_->srcDebtDir;
502 template <
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] =
517 assert(
static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn));
519 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
521 JLOG(j_.
trace()) <<
"DirectStepI::rev"
522 <<
" srcRedeems: " <<
redeems(srcDebtDir)
524 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
525 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
527 if (maxSrcToDst.signum() <= 0)
529 JLOG(j_.
trace()) <<
"DirectStepI::rev: dry";
535 return {beast::zero, beast::zero};
541 if (srcToDst <= maxSrcToDst)
544 mulRatio(srcToDst, srcQOut, QUALITY_ONE,
true);
545 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
553 JLOG(j_.
trace()) <<
"DirectStepI::rev: Non-limiting"
554 <<
" srcRedeems: " <<
redeems(srcDebtDir)
563 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
565 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
566 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
574 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
575 <<
" srcRedeems: " <<
redeems(srcDebtDir)
577 <<
" srcToDst: " <<
to_string(maxSrcToDst)
579 return {
in, actualOut};
586 template <
class TDerived>
594 if (cache_->in < fwdIn)
597 auto const diff = fwdIn - cache_->in;
598 if (diff > smallDiff)
600 if (fwdIn.
exponent() != cache_->in.exponent() ||
601 !cache_->in.mantissa() ||
602 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) >
608 <<
"DirectStepI::fwd: setCacheLimiting"
611 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
612 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst)
614 <<
" cacheOut: " <<
to_string(cache_->out);
615 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
621 if (fwdSrcToDst < cache_->srcToDst)
622 cache_->srcToDst = fwdSrcToDst;
623 if (fwdOut < cache_->
out)
624 cache_->out = fwdOut;
625 cache_->srcDebtDir = srcDebtDir;
628 template <
class TDerived>
633 boost::container::flat_set<uint256>& ,
638 auto const [maxSrcToDst, srcDebtDir] =
639 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
641 auto const [srcQOut, dstQIn] =
644 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
646 JLOG(j_.
trace()) <<
"DirectStepI::fwd"
647 <<
" srcRedeems: " <<
redeems(srcDebtDir)
649 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
650 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
652 if (maxSrcToDst.signum() <= 0)
654 JLOG(j_.
trace()) <<
"DirectStepI::fwd: dry";
660 return {beast::zero, beast::zero};
666 if (srcToDst <= maxSrcToDst)
669 mulRatio(srcToDst, dstQIn, QUALITY_ONE,
false);
670 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
678 JLOG(j_.
trace()) <<
"DirectStepI::fwd: Non-limiting"
679 <<
" srcRedeems: " <<
redeems(srcDebtDir)
688 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
690 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
691 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
699 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
700 <<
" srcRedeems: " <<
redeems(srcDebtDir)
705 return {cache_->in, cache_->out};
708 template <
class TDerived>
717 JLOG(j_.
trace()) <<
"Expected valid cache in validFwd";
721 auto const savCache = *cache_;
725 auto const [maxSrcToDst, srcDebtDir] =
726 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
731 boost::container::flat_set<uint256> dummy;
732 fwdImp(sb, afView, dummy,
in.iou);
734 catch (FlowException
const&)
739 if (maxSrcToDst < cache_->srcToDst)
741 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
742 <<
" Exceeded max src->dst limit"
743 <<
" max src->dst: " <<
to_string(maxSrcToDst)
744 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
748 if (!(
checkNear(savCache.in, cache_->in) &&
751 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
752 <<
" ExpectedIn: " <<
to_string(savCache.in)
753 <<
" CachedIn: " <<
to_string(cache_->in)
754 <<
" ExpectedOut: " <<
to_string(savCache.out)
755 <<
" CachedOut: " <<
to_string(cache_->out);
762 template <
class TDerived>
767 return {QUALITY_ONE, QUALITY_ONE};
769 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
773 if (prevStepQIn > srcQOut)
774 srcQOut = prevStepQIn;
775 return {srcQOut, QUALITY_ONE};
779 template <
class TDerived>
787 assert(
static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(
788 prevStepDebtDirection));
796 if (isLast_ && dstQIn > QUALITY_ONE)
797 dstQIn = QUALITY_ONE;
798 return {srcQOut, dstQIn};
802 template <
class TDerived>
811 return qualitiesSrcRedeems(sb);
815 auto const prevStepDebtDirection = [&] {
817 return prevStep_->debtDirection(sb, strandDir);
820 return qualitiesSrcIssues(sb, prevStepDebtDirection);
824 template <
class TDerived>
832 template <
class TDerived>
847 auto dstQIn =
static_cast<TDerived const*
>(
this)->quality(
850 if (isLast_ && dstQIn > QUALITY_ONE)
851 dstQIn = QUALITY_ONE;
852 Issue const iss{currency_, src_};
858 auto const [srcQOut, dstQIn] =
redeems(dir)
859 ? qualitiesSrcRedeems(v)
860 : qualitiesSrcIssues(v, prevStepDir);
862 Issue const iss{currency_, src_};
873 template <
class TDerived>
880 JLOG(j_.
debug()) <<
"DirectStepI: specified bad account.";
886 JLOG(j_.
debug()) <<
"DirectStepI: same src and dst.";
894 <<
"DirectStepI: can't receive IOUs from non-existent issuer: "
920 Issue const srcIssue{currency_, src_};
921 Issue const dstIssue{currency_, dst_};
935 if (book->out != srcIssue)
944 <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize
950 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
966 return ds->src() == src && ds->dst() == dst &&
967 ds->currency() == currency;
986 auto offerCrossingStep =
987 std::make_unique<DirectIOfferCrossingStep>(ctx, src, dst, c);
988 ter = offerCrossingStep->check(ctx);
989 r = std::move(offerCrossingStep);
994 std::make_unique<DirectIPaymentStep>(ctx, src, dst, c);
995 ter = paymentStep->check(ctx);
996 r = std::move(paymentStep);
999 return {ter,
nullptr};
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Context needed to build Strand Steps and for error checking.
const SF_UINT32 sfHighQualityIn
const size_t strandSize
Length of Strand.
Rate transferRate(ReadView const &view, AccountID const &issuer)
bool issues(DebtDirection dir)
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
std::pair< IOUAmount, DebtDirection > maxPaymentFlow(ReadView const &sb) const
const uint256 fixQualityUpperBound
int exponent() const noexcept
A currency issued by an account.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
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.
A field with a type known at compile time.
bool verifyDstQualityIn(std::uint32_t dstQIn) const
Stream trace() const
Severity stream access functions.
A wrapper which makes credits unavailable to balances.
virtual std::optional< AccountID > directStepSrcAcct() const
If this step is DirectStepI (IOU->IOU direct step), return the src account.
std::pair< std::uint32_t, std::uint32_t > qualities(ReadView const &sb, DebtDirection srcDebtDir, StrandDirection strandDir) const
std::optional< Cache > cache_
bool verifyPrevStepDebtDirection(DebtDirection prevStepDir) const
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
std::pair< std::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection dir) const override
TER check(StrandContext const &ctx) const
AccountID const & src() const
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcIssues(ReadView const &sb, DebtDirection prevStepDebtDirection) const
bool checkNear(IOUAmount const &expected, IOUAmount const &actual)
Cache(IOUAmount const &in_, IOUAmount const &srcToDst_, IOUAmount const &out_, DebtDirection srcDebtDir_)
Currency const & currency() const
friend bool operator==(DirectStepI const &lhs, DirectStepI const &rhs)
std::pair< TER, std::unique_ptr< Step > > make_DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
DirectStepI(StrandContext const &ctx, AccountID const &src, AccountID const &dst, Currency const &c)
Floating point representation of amounts with high dynamic range.
bool verifyDstQualityIn(std::uint32_t dstQIn) const
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Writeable view to a ledger, for applying a transaction.
std::string logString() const override
TER check(StrandContext const &ctx, std::shared_ptr< const SLE > const &sleSrc) const
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
TER checkNoRipple(ReadView const &view, AccountID const &prev, AccountID const &cur, AccountID const &next, Currency const ¤cy, beast::Journal j)
void setCacheLimiting(IOUAmount const &fwdIn, IOUAmount const &fwdSrcToDst, IOUAmount const &fwdOut, DebtDirection srcDebtDir)
friend bool operator!=(DirectStepI const &lhs, DirectStepI const &rhs)
const SF_UINT32 sfLowQualityOut
virtual std::optional< Book > bookStepBook() const
If this step is a BookStep, return the book.
ReadView const & view
Current ReadView.
std::pair< IOUAmount, IOUAmount > fwdImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &in)
std::string logStringImpl(char const *name) const
Keylet account(AccountID const &id) noexcept
AccountID root.
bool equal(Step const &rhs) const override
const bool offerCrossing
true if offer crossing, not payment
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
std::pair< IOUAmount, IOUAmount > revImp(PaymentSandbox &sb, ApplyView &afView, boost::container::flat_set< uint256 > &ofrsToRm, IOUAmount const &out)
Step const *const prevStep_
const SF_UINT32 sfLowQualityIn
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.
A step in a payment path.
std::optional< EitherAmount > cachedOut() const override
boost::container::flat_set< Issue > & seenBookOuts
A strand may not include an offer that output the same issue more than once.
const bool isLast
true if Step is last in Strand
A generic endpoint for log messages.
std::uint32_t lineQualityIn(ReadView const &v) const override
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool directStepEqual(Step const &step, AccountID const &src, AccountID const &dst, Currency const ¤cy)
bool redeems(DebtDirection dir)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::int64_t mantissa() const noexcept
AccountID const & dst() const
virtual Rules const & rules() const =0
Returns the tx processing rules.
std::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
std::optional< EitherAmount > cachedIn() const override
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
const SF_AMOUNT sfBalance
bool verifyPrevStepDebtDirection(DebtDirection) const
const bool isFirst
true if Step is first in Strand
Step const *const prevStep
The previous step in the strand.
std::pair< bool, EitherAmount > validFwd(PaymentSandbox &sb, ApplyView &afView, EitherAmount const &in) override
const SF_UINT32 sfHighQualityOut
std::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const ¤cy)
TER check(StrandContext const &ctx, std::shared_ptr< const SLE > const &sleSrc) const
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
std::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
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.
std::pair< std::uint32_t, std::uint32_t > qualitiesSrcRedeems(ReadView const &sb) const
std::optional< AccountID > directStepSrcAcct() const override
std::string logString() const override