20#include <xrpld/app/paths/detail/AmountSpec.h>
21#include <xrpld/ledger/PaymentSandbox.h>
22#include <xrpld/ledger/View.h>
23#include <xrpl/beast/utility/instrumentation.h>
24#include <xrpl/protocol/SField.h>
47 STAmount const& preCreditSenderBalance)
51 "ripple::detail::DeferredCredits::credit : sender is not receiver");
54 "ripple::detail::DeferredCredits::credit : positive amount");
62 if (sender < receiver)
81 if (sender < receiver)
84 v.lowAcctCredits += amount;
98 auto& mapVal = r.first->second;
121 Key const k = makeKey(main, other, currency);
122 auto i = credits_.find(k);
123 if (i == credits_.end())
126 auto const& v = i->second;
131 v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance);
137 v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance);
150 auto& toVal = r.first->second;
151 auto const& fromVal = i.second;
152 toVal.lowAcctCredits += fromVal.lowAcctCredits;
153 toVal.highAcctCredits += fromVal.highAcctCredits;
163 auto& toVal = r.first->second;
164 auto const& fromVal = i.second;
191 auto delta = amount.
zeroed();
192 auto lastBal = amount;
193 auto minBal = amount;
194 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
196 if (
auto adj = curSB->tab_.adjustments(account, issuer, currency))
198 delta += adj->debits;
199 lastBal = adj->origBalance;
200 if (lastBal < minBal)
209 auto adjustedAmt =
std::min({amount, lastBal - delta, minBal});
210 adjustedAmt.setIssuer(amount.
getIssuer());
212 if (
isXRP(issuer) && adjustedAmt < beast::zero)
227 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
229 if (
auto adj = curSB->tab_.ownerCount(account))
242 tab_.
credit(from, to, amount, preCreditBalance);
257 XRPL_ASSERT(!
ps_,
"ripple::PaymentSandbox::apply : non-null sandbox");
264 XRPL_ASSERT(
ps_ == &to,
"ripple::PaymentSandbox::apply : matching sandbox");
281 auto each = [&result](
297 auto const bt = before->getType();
302 highID = (*before)[sfAccount];
303 oldBalance = (*before)[sfBalance];
304 newBalance = oldBalance.
zeroed();
307 lowID = (*before)[sfLowLimit].getIssuer();
308 highID = (*before)[sfHighLimit].getIssuer();
309 oldBalance = (*before)[sfBalance];
310 newBalance = oldBalance.
zeroed();
322 auto const at =
after->getType();
327 highID = (*after)[sfAccount];
328 newBalance = (*after)[sfBalance];
329 oldBalance = newBalance.
zeroed();
332 lowID = (*after)[sfLowLimit].getIssuer();
333 highID = (*after)[sfHighLimit].getIssuer();
334 newBalance = (*after)[sfBalance];
335 oldBalance = newBalance.
zeroed();
347 auto const at =
after->getType();
349 at == before->getType(),
350 "ripple::PaymentSandbox::balanceChanges : after and before "
356 highID = (*after)[sfAccount];
357 oldBalance = (*before)[sfBalance];
358 newBalance = (*after)[sfBalance];
361 lowID = (*after)[sfLowLimit].getIssuer();
362 highID = (*after)[sfHighLimit].getIssuer();
363 oldBalance = (*before)[sfBalance];
364 newBalance = (*after)[sfBalance];
374 auto delta = newBalance - oldBalance;
380 r.first->second += delta;
387 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.
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.
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
STAmount lowAcctOrigBalance