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>
30 #include <boost/optional.hpp>
37 template <
class TDerived>
38 class DirectStepI :
public StepImp<IOUAmount, IOUAmount, DirectStepI<TDerived>>
123 boost::optional<EitherAmount>
131 boost::optional<EitherAmount>
139 boost::optional<AccountID>
145 boost::optional<std::pair<AccountID, AccountID>>
164 boost::container::flat_set<uint256>& ofrsToRm,
171 boost::container::flat_set<uint256>& ofrsToRm,
200 return !(lhs == rhs);
209 <<
"\nSrc: " <<
src_ <<
"\nDst: " <<
dst_;
217 if (
auto ds =
dynamic_cast<DirectStepI const*
>(&rhs))
293 return issues(prevStepDir);
301 return dstQIn == QUALITY_ONE;
340 auto const& field = [
this, qDir]() ->
SF_UINT32 const& {
344 if (this->dst_ < this->
src_)
352 if (this->src_ < this->
dst_)
359 if (!sle->isFieldPresent(field))
362 auto const q = (*sle)[field];
415 JLOG(
j_.
trace()) <<
"DirectStepI: No credit line. " << *
this;
422 !((*sleLine)[
sfFlags] & authField) &&
426 <<
"DirectStepI: can't receive IOUs from issuer without auth."
435 auto const noRippleSrcToDst =
438 if (noRippleSrcToDst)
446 if (owed <= beast::zero)
451 JLOG(
j_.
debug()) <<
"DirectStepI: dry: owed: " << owed
452 <<
" limit: " << limit;
473 template <
class TDerived>
480 if (srcOwed.signum() > 0)
489 template <
class TDerived>
495 return cache_->srcDebtDir;
503 template <
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] =
518 assert(
static_cast<TDerived const*
>(
this)->verifyDstQualityIn(dstQIn));
520 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
522 JLOG(j_.
trace()) <<
"DirectStepI::rev"
523 <<
" srcRedeems: " <<
redeems(srcDebtDir)
525 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
526 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
528 if (maxSrcToDst.signum() <= 0)
530 JLOG(j_.
trace()) <<
"DirectStepI::rev: dry";
536 return {beast::zero, beast::zero};
542 if (srcToDst <= maxSrcToDst)
545 mulRatio(srcToDst, srcQOut, QUALITY_ONE,
true);
546 cache_.emplace(
in, srcToDst,
out, srcDebtDir);
554 JLOG(j_.
trace()) <<
"DirectStepI::rev: Non-limiting"
555 <<
" srcRedeems: " <<
redeems(srcDebtDir)
564 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
566 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
567 cache_.emplace(
in, maxSrcToDst, actualOut, srcDebtDir);
575 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
576 <<
" srcRedeems: " <<
redeems(srcDebtDir)
578 <<
" srcToDst: " <<
to_string(maxSrcToDst)
580 return {
in, actualOut};
587 template <
class TDerived>
595 if (cache_->in < fwdIn)
598 auto const diff = fwdIn - cache_->in;
599 if (
diff > smallDiff)
601 if (fwdIn.
exponent() != cache_->in.exponent() ||
602 !cache_->in.mantissa() ||
603 (
double(fwdIn.
mantissa()) /
double(cache_->in.mantissa())) >
609 <<
"DirectStepI::fwd: setCacheLimiting"
612 <<
" fwdSrcToDst: " <<
to_string(fwdSrcToDst)
613 <<
" cacheSrcToDst: " <<
to_string(cache_->srcToDst)
615 <<
" cacheOut: " <<
to_string(cache_->out);
616 cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir);
622 if (fwdSrcToDst < cache_->srcToDst)
623 cache_->srcToDst = fwdSrcToDst;
624 if (fwdOut < cache_->
out)
625 cache_->out = fwdOut;
626 cache_->srcDebtDir = srcDebtDir;
629 template <
class TDerived>
634 boost::container::flat_set<uint256>& ,
639 auto const [maxSrcToDst, srcDebtDir] =
640 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
642 auto const [srcQOut, dstQIn] =
645 Issue const srcToDstIss(currency_,
redeems(srcDebtDir) ? dst_ : src_);
647 JLOG(j_.
trace()) <<
"DirectStepI::fwd"
648 <<
" srcRedeems: " <<
redeems(srcDebtDir)
650 <<
" maxSrcToDst: " <<
to_string(maxSrcToDst)
651 <<
" srcQOut: " << srcQOut <<
" dstQIn: " << dstQIn;
653 if (maxSrcToDst.signum() <= 0)
655 JLOG(j_.
trace()) <<
"DirectStepI::fwd: dry";
661 return {beast::zero, beast::zero};
667 if (srcToDst <= maxSrcToDst)
670 mulRatio(srcToDst, dstQIn, QUALITY_ONE,
false);
671 setCacheLimiting(
in, srcToDst,
out, srcDebtDir);
679 JLOG(j_.
trace()) <<
"DirectStepI::fwd: Non-limiting"
680 <<
" srcRedeems: " <<
redeems(srcDebtDir)
689 mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE,
true);
691 mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE,
false);
692 setCacheLimiting(actualIn, maxSrcToDst,
out, srcDebtDir);
700 JLOG(j_.
trace()) <<
"DirectStepI::rev: Limiting"
701 <<
" srcRedeems: " <<
redeems(srcDebtDir)
706 return {cache_->in, cache_->out};
709 template <
class TDerived>
718 JLOG(j_.
trace()) <<
"Expected valid cache in validFwd";
722 auto const savCache = *cache_;
726 auto const [maxSrcToDst, srcDebtDir] =
727 static_cast<TDerived const*
>(
this)->maxFlow(sb, cache_->srcToDst);
732 boost::container::flat_set<uint256> dummy;
733 fwdImp(sb, afView, dummy,
in.iou);
735 catch (FlowException
const&)
740 if (maxSrcToDst < cache_->srcToDst)
742 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
743 <<
" Exceeded max src->dst limit"
744 <<
" max src->dst: " <<
to_string(maxSrcToDst)
745 <<
" actual src->dst: " <<
to_string(cache_->srcToDst);
749 if (!(
checkNear(savCache.in, cache_->in) &&
752 JLOG(j_.
warn()) <<
"DirectStepI: Strand re-execute check failed."
753 <<
" ExpectedIn: " <<
to_string(savCache.in)
754 <<
" CachedIn: " <<
to_string(cache_->in)
755 <<
" ExpectedOut: " <<
to_string(savCache.out)
756 <<
" CachedOut: " <<
to_string(cache_->out);
763 template <
class TDerived>
768 return {QUALITY_ONE, QUALITY_ONE};
770 auto const prevStepQIn = prevStep_->lineQualityIn(sb);
774 if (prevStepQIn > srcQOut)
775 srcQOut = prevStepQIn;
776 return {srcQOut, QUALITY_ONE};
780 template <
class TDerived>
788 assert(
static_cast<TDerived const*
>(
this)->verifyPrevStepDebtDirection(
789 prevStepDebtDirection));
797 if (isLast_ && dstQIn > QUALITY_ONE)
798 dstQIn = QUALITY_ONE;
799 return {srcQOut, dstQIn};
803 template <
class TDerived>
812 return qualitiesSrcRedeems(sb);
816 auto const prevStepDebtDirection = [&] {
818 return prevStep_->debtDirection(sb, strandDir);
821 return qualitiesSrcIssues(sb, prevStepDebtDirection);
825 template <
class TDerived>
833 template <
class TDerived>
848 auto dstQIn =
static_cast<TDerived const*
>(
this)->quality(
851 if (isLast_ && dstQIn > QUALITY_ONE)
852 dstQIn = QUALITY_ONE;
853 Issue const iss{currency_, src_};
859 auto const [srcQOut, dstQIn] =
redeems(dir)
860 ? qualitiesSrcRedeems(v)
861 : qualitiesSrcIssues(v, prevStepDir);
863 Issue const iss{currency_, src_};
874 template <
class TDerived>
881 JLOG(j_.
debug()) <<
"DirectStepI: specified bad account.";
887 JLOG(j_.
debug()) <<
"DirectStepI: same src and dst.";
895 <<
"DirectStepI: can't receive IOUs from non-existent issuer: "
921 Issue const srcIssue{currency_, src_};
922 Issue const dstIssue{currency_, dst_};
936 if (book->out != srcIssue)
945 <<
"DirectStepI: loop detected: Index: " << ctx.
strandSize
951 return static_cast<TDerived const*
>(
this)->check(ctx, sleSrc);
967 return ds->src() == src && ds->dst() == dst &&
968 ds->currency() == currency;
987 auto offerCrossingStep =
988 std::make_unique<DirectIOfferCrossingStep>(ctx, src, dst, c);
989 ter = offerCrossingStep->check(ctx);
990 r = std::move(offerCrossingStep);
995 std::make_unique<DirectIPaymentStep>(ctx, src, dst, c);
996 ter = paymentStep->check(ctx);
997 r = std::move(paymentStep);
1000 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.
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.
std::pair< std::uint32_t, std::uint32_t > qualities(ReadView const &sb, DebtDirection srcDebtDir, StrandDirection strandDir) const
bool verifyPrevStepDebtDirection(DebtDirection prevStepDir) const
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
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)
boost::optional< std::pair< AccountID, AccountID > > directStepAccts() const override
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::string to_string(ListDisposition disposition)
std::uint32_t quality(ReadView const &sb, QualityDirection qDir) const
virtual boost::optional< Book > bookStepBook() const
If this step is a BookStep, return the book.
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
ReadView const & view
Current ReadView.
boost::optional< EitherAmount > cachedOut() const override
boost::optional< EitherAmount > cachedIn() const override
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.
boost::container::flat_set< Issue > & seenBookOuts
A strand may not include an offer that output the same issue more than once.
boost::optional< AccountID > directStepSrcAcct() const override
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
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
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
DebtDirection debtDirection(ReadView const &sb, StrandDirection dir) const override
const SF_AMOUNT sfBalance
boost::optional< Cache > cache_
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
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::pair< IOUAmount, DebtDirection > maxFlow(ReadView const &sb, IOUAmount const &desired) const
std::pair< boost::optional< Quality >, DebtDirection > qualityUpperBound(ReadView const &v, DebtDirection dir) const override
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
virtual boost::optional< AccountID > directStepSrcAcct() const
If this step is DirectStepI (IOU->IOU direct step), return the src account.
std::string logString() const override