20#include <xrpld/app/paths/detail/AmountSpec.h>
21#include <xrpld/ledger/PaymentSandbox.h>
22#include <xrpld/ledger/View.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/protocol/SField.h>
48 STAmount const& preCreditSenderBalance)
52 "ripple::detail::DeferredCredits::credit : sender is not receiver");
55 "ripple::detail::DeferredCredits::credit : positive amount");
63 if (sender < receiver)
82 if (sender < receiver)
85 v.lowAcctCredits += amount;
99 auto& mapVal = r.first->second;
122 Key const k = makeKey(main, other, currency);
123 auto i = credits_.find(k);
124 if (i == credits_.end())
127 auto const& v = i->second;
132 v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance);
138 v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance);
151 auto& toVal = r.first->second;
152 auto const& fromVal = i.second;
153 toVal.lowAcctCredits += fromVal.lowAcctCredits;
154 toVal.highAcctCredits += fromVal.highAcctCredits;
164 auto& toVal = r.first->second;
165 auto const& fromVal = i.second;
192 auto delta = amount.
zeroed();
193 auto lastBal = amount;
194 auto minBal = amount;
195 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
197 if (
auto adj = curSB->tab_.adjustments(account, issuer, currency))
199 delta += adj->debits;
200 lastBal = adj->origBalance;
201 if (lastBal < minBal)
210 auto adjustedAmt =
std::min({amount, lastBal - delta, minBal});
213 if (
isXRP(issuer) && adjustedAmt < beast::zero)
228 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
230 if (
auto adj = curSB->tab_.ownerCount(account))
243 tab_.
credit(from, to, amount, preCreditBalance);
258 XRPL_ASSERT(!
ps_,
"ripple::PaymentSandbox::apply : non-null sandbox");
265 XRPL_ASSERT(
ps_ == &to,
"ripple::PaymentSandbox::apply : matching sandbox");
282 auto each = [&result](
298 auto const bt = before->getType();
303 highID = (*before)[sfAccount];
304 oldBalance = (*before)[sfBalance];
305 newBalance = oldBalance.
zeroed();
308 lowID = (*before)[sfLowLimit].getIssuer();
309 highID = (*before)[sfHighLimit].getIssuer();
310 oldBalance = (*before)[sfBalance];
311 newBalance = oldBalance.
zeroed();
323 auto const at =
after->getType();
328 highID = (*after)[sfAccount];
329 newBalance = (*after)[sfBalance];
330 oldBalance = newBalance.
zeroed();
333 lowID = (*after)[sfLowLimit].getIssuer();
334 highID = (*after)[sfHighLimit].getIssuer();
335 newBalance = (*after)[sfBalance];
336 oldBalance = newBalance.
zeroed();
348 auto const at =
after->getType();
350 at == before->getType(),
351 "ripple::PaymentSandbox::balanceChanges : after and before "
357 highID = (*after)[sfAccount];
358 oldBalance = (*before)[sfBalance];
359 newBalance = (*after)[sfBalance];
362 lowID = (*after)[sfLowLimit].getIssuer();
363 highID = (*after)[sfHighLimit].getIssuer();
364 oldBalance = (*before)[sfBalance];
365 newBalance = (*after)[sfBalance];
375 auto delta = newBalance - oldBalance;
381 r.first->second += delta;
388 r.first->second += delta;
A wrapper which makes credits unavailable to balances.
XRPAmount xrpDestroyed() const
std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const override
std::map< std::tuple< AccountID, AccountID, Currency >, STAmount > balanceChanges(ReadView const &view) const
void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
void apply(RawView &to)
Apply changes to base view.
detail::DeferredCredits tab_
STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const override
void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next) override
PaymentSandbox const * ps_
Interface for ledger entry changes.
void setIssuer(AccountID const &uIssuer)
Currency const & getCurrency() const
bool negative() const noexcept
AccountID const & getIssuer() const
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
XRPAmount const & dropsDestroyed() const
void visit(ReadView const &base, std::function< void(uint256 const &key, bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)> const &func) const
void apply(RawView &to) const
detail::ApplyStateTable items_
std::optional< Adjustment > adjustments(AccountID const &main, AccountID const &other, Currency const ¤cy) const
void credit(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
void apply(DeferredCredits &to)
std::map< AccountID, std::uint32_t > ownerCounts_
void ownerCount(AccountID const &id, std::uint32_t cur, std::uint32_t next)
std::map< Key, Value > credits_
static Key makeKey(AccountID const &a1, AccountID const &a2, Currency const &c)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isXRP(AccountID const &c)
AccountID const & xrpAccount()
Compute AccountID from public key.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
STAmount lowAcctOrigBalance