1#include <xrpl/beast/utility/instrumentation.h>
2#include <xrpl/ledger/PaymentSandbox.h>
3#include <xrpl/ledger/View.h>
4#include <xrpl/protocol/SField.h>
24 STAmount const& preCreditSenderBalance)
26 XRPL_ASSERT(sender != receiver,
"xrpl::detail::DeferredCredits::credit : sender is not receiver");
27 XRPL_ASSERT(!amount.negative(),
"xrpl::detail::DeferredCredits::credit : positive amount");
29 auto const k =
makeKey(sender, receiver, amount.getCurrency());
35 if (sender < receiver)
54 if (sender < receiver)
57 v.lowAcctCredits += amount;
68 auto& mapVal = r.first->second;
89 Key const k = makeKey(main, other, currency);
90 auto i = credits_.find(k);
91 if (i == credits_.end())
94 auto const& v = i->second;
98 result.
emplace(v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance);
103 result.
emplace(v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance);
116 auto& toVal = r.first->second;
117 auto const& fromVal = i.second;
118 toVal.lowAcctCredits += fromVal.lowAcctCredits;
119 toVal.highAcctCredits += fromVal.highAcctCredits;
129 auto& toVal = r.first->second;
130 auto const& fromVal = i.second;
152 auto const currency = amount.getCurrency();
154 auto delta = amount.zeroed();
155 auto lastBal = amount;
156 auto minBal = amount;
157 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
159 if (
auto adj = curSB->tab_.adjustments(account, issuer, currency))
161 delta += adj->debits;
162 lastBal = adj->origBalance;
163 if (lastBal < minBal)
172 auto adjustedAmt =
std::min({amount, lastBal - delta, minBal});
173 adjustedAmt.setIssuer(amount.getIssuer());
175 if (
isXRP(issuer) && adjustedAmt < beast::zero)
189 for (
auto curSB =
this; curSB; curSB = curSB->ps_)
191 if (
auto adj = curSB->tab_.ownerCount(account))
204 tab_.
credit(from, to, amount, preCreditBalance);
216 XRPL_ASSERT(!
ps_,
"xrpl::PaymentSandbox::apply : non-null sandbox");
223 XRPL_ASSERT(
ps_ == &to,
"xrpl::PaymentSandbox::apply : matching sandbox");
240 auto each = [&result](
256 auto const bt = before->getType();
261 highID = (*before)[sfAccount];
262 oldBalance = (*before)[sfBalance];
263 newBalance = oldBalance.
zeroed();
266 lowID = (*before)[sfLowLimit].getIssuer();
267 highID = (*before)[sfHighLimit].getIssuer();
268 oldBalance = (*before)[sfBalance];
269 newBalance = oldBalance.
zeroed();
281 auto const at =
after->getType();
286 highID = (*after)[sfAccount];
287 newBalance = (*after)[sfBalance];
288 oldBalance = newBalance.
zeroed();
291 lowID = (*after)[sfLowLimit].getIssuer();
292 highID = (*after)[sfHighLimit].getIssuer();
293 newBalance = (*after)[sfBalance];
294 oldBalance = newBalance.
zeroed();
306 auto const at =
after->getType();
308 at == before->getType(),
309 "xrpl::PaymentSandbox::balanceChanges : after and before "
315 highID = (*after)[sfAccount];
316 oldBalance = (*before)[sfBalance];
317 newBalance = (*after)[sfBalance];
320 lowID = (*after)[sfLowLimit].getIssuer();
321 highID = (*after)[sfHighLimit].getIssuer();
322 oldBalance = (*before)[sfBalance];
323 newBalance = (*after)[sfBalance];
333 auto delta = newBalance - oldBalance;
339 r.first->second += delta;
346 r.first->second += delta;
A wrapper which makes credits unavailable to balances.
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_
void apply(RawView &to)
Apply changes to base view.
detail::DeferredCredits tab_
std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const override
void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
XRPAmount xrpDestroyed() const
std::map< std::tuple< AccountID, AccountID, Currency >, STAmount > balanceChanges(ReadView const &view) const
Interface for ledger entry changes.
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
Currency const & getCurrency() 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
XRPAmount const & dropsDestroyed() const
void apply(RawView &to) const
detail::ApplyStateTable items_
void apply(DeferredCredits &to)
std::optional< Adjustment > adjustments(AccountID const &main, AccountID const &other, Currency const ¤cy) const
std::map< AccountID, std::uint32_t > ownerCounts_
void credit(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
std::map< Key, Value > credits_
void ownerCount(AccountID const &id, std::uint32_t cur, std::uint32_t next)
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)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
AccountID const & xrpAccount()
Compute AccountID from public key.
STAmount lowAcctOrigBalance