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>
26 STAmount const& preCreditSenderBalance)
29 sender != receiver,
"xrpl::detail::DeferredCredits::credit : sender is not receiver");
30 XRPL_ASSERT(!amount.negative(),
"xrpl::detail::DeferredCredits::credit : positive amount");
32 auto const k =
makeKey(sender, receiver, amount.getCurrency());
38 if (sender < receiver)
57 if (sender < receiver)
63 v.lowAcctCredits += amount;
75 auto& mapVal = r.first->second;
98 Key const k = makeKey(main, other, currency);
99 auto i = credits_.find(k);
100 if (i == credits_.end())
103 auto const& v = i->second;
107 result.
emplace(v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance);
111 result.
emplace(v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance);
123 auto& toVal = r.first->second;
124 auto const& fromVal = i.second;
125 toVal.lowAcctCredits += fromVal.lowAcctCredits;
126 toVal.highAcctCredits += fromVal.highAcctCredits;
136 auto& toVal = r.first->second;
137 auto const& fromVal = i.second;
162 auto const currency = amount.getCurrency();
164 auto delta = amount.zeroed();
165 auto lastBal = amount;
166 auto minBal = amount;
167 for (
auto curSB =
this; curSB !=
nullptr; curSB = curSB->ps_)
169 if (
auto adj = curSB->tab_.adjustments(account, issuer, currency))
171 delta += adj->debits;
172 lastBal = adj->origBalance;
173 if (lastBal < minBal)
182 auto adjustedAmt =
std::min({amount, lastBal - delta, minBal});
183 adjustedAmt.setIssuer(amount.getIssuer());
185 if (
isXRP(issuer) && adjustedAmt < beast::zero)
201 for (
auto curSB =
this; curSB !=
nullptr; curSB = curSB->ps_)
203 if (
auto adj = curSB->tab_.ownerCount(account))
216 tab_.
credit(from, to, amount, preCreditBalance);
231 XRPL_ASSERT(!
ps_,
"xrpl::PaymentSandbox::apply : non-null sandbox");
238 XRPL_ASSERT(
ps_ == &to,
"xrpl::PaymentSandbox::apply : matching sandbox");
255 auto each = [&result](
271 auto const bt = before->getType();
276 highID = (*before)[sfAccount];
277 oldBalance = (*before)[sfBalance];
278 newBalance = oldBalance.
zeroed();
281 lowID = (*before)[sfLowLimit].getIssuer();
282 highID = (*before)[sfHighLimit].getIssuer();
283 oldBalance = (*before)[sfBalance];
284 newBalance = oldBalance.
zeroed();
296 auto const at =
after->getType();
301 highID = (*after)[sfAccount];
302 newBalance = (*after)[sfBalance];
303 oldBalance = newBalance.
zeroed();
306 lowID = (*after)[sfLowLimit].getIssuer();
307 highID = (*after)[sfHighLimit].getIssuer();
308 newBalance = (*after)[sfBalance];
309 oldBalance = newBalance.
zeroed();
321 auto const at =
after->getType();
323 at == before->getType(),
324 "xrpl::PaymentSandbox::balanceChanges : after and before "
330 highID = (*after)[sfAccount];
331 oldBalance = (*before)[sfBalance];
332 newBalance = (*after)[sfBalance];
335 lowID = (*after)[sfLowLimit].getIssuer();
336 highID = (*after)[sfHighLimit].getIssuer();
337 oldBalance = (*before)[sfBalance];
338 newBalance = (*after)[sfBalance];
348 auto delta = newBalance - oldBalance;
354 r.first->second += delta;
361 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