20#include <xrpld/app/tx/detail/OfferStream.h>
21#include <xrpl/basics/Log.h>
22#include <xrpl/protocol/Feature.h>
28checkIssuers(ReadView
const& view, Book
const& book)
30 auto issuerExists = [](ReadView
const& view, Issue
const& iss) ->
bool {
33 return issuerExists(view, book.in) && issuerExists(view, book.out);
37template <
class TIn,
class TOut>
47 , cancelView_(cancelView)
49 , validBook_(checkIssuers(view, book))
55 validBook_,
"ripple::TOfferStreamBase::TOfferStreamBase : valid book");
60template <
class TIn,
class TOut>
72 JLOG(j_.
error()) <<
"Missing directory " << tip_.dir() <<
" for offer "
77 auto v(p->getFieldV256(sfIndexes));
78 auto it(
std::find(v.begin(), v.end(), tip_.index()));
82 JLOG(j_.
error()) <<
"Missing offer " << tip_.index()
83 <<
" for directory " << tip_.dir();
88 p->setFieldV256(sfIndexes, v);
91 JLOG(j_.
trace()) <<
"Missing offer " << tip_.index()
92 <<
" removed from directory " << tip_.dir();
104 return accountFunds(view,
id, saDefault, freezeHandling, j);
137template <
class TIn,
class TOut>
138template <
class TTakerPays,
class TTakerGets>
143 std::is_same_v<TTakerPays, IOUAmount> ||
144 std::is_same_v<TTakerPays, XRPAmount>,
145 "STAmount is not supported");
148 std::is_same_v<TTakerGets, IOUAmount> ||
149 std::is_same_v<TTakerGets, XRPAmount>,
150 "STAmount is not supported");
153 !std::is_same_v<TTakerPays, XRPAmount> ||
154 !std::is_same_v<TTakerGets, XRPAmount>,
155 "Cannot have XRP/XRP offers");
157 if (!view_.rules().enabled(fixRmSmallIncreasedQOffers))
163 constexpr bool const inIsXRP = std::is_same_v<TTakerPays, XRPAmount>;
164 constexpr bool const outIsXRP = std::is_same_v<TTakerGets, XRPAmount>;
166 if constexpr (outIsXRP)
175 TAmounts<TTakerPays, TTakerGets>
const ofrAmts{
176 toAmount<TTakerPays>(offer_.amount().in),
177 toAmount<TTakerGets>(offer_.amount().out)};
179 if constexpr (!inIsXRP && !outIsXRP)
181 if (ofrAmts.in >= ofrAmts.out)
185 TTakerGets
const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
186 bool const fixReduced = view_.rules().enabled(fixReducedOffersV1);
188 auto const effectiveAmounts = [&] {
189 if (offer_.owner() != offer_.issueOut().account &&
190 ownerFunds < ofrAmts.out)
198 return offer_.quality().ceil_out_strict(
199 ofrAmts, ownerFunds,
false);
201 return offer_.quality().ceil_out(ofrAmts, ownerFunds);
209 (effectiveAmounts.in.signum() <= 0 ||
210 effectiveAmounts.out.signum() <= 0))
213 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
216 Quality
const effectiveQuality{effectiveAmounts};
217 return effectiveQuality < offer_.quality();
220template <
class TIn,
class TOut>
232 ownerFunds_ = std::nullopt;
241 if (!counter_.step())
255 if (entry->isFieldPresent(sfExpiration) &&
256 tp{d{(*entry)[sfExpiration]}} <= expire_)
258 JLOG(j_.
trace()) <<
"Removing expired offer " << entry->key();
259 permRmOffer(entry->key());
265 auto const amount(offer_.amount());
270 JLOG(j_.
warn()) <<
"Removing bad offer " << entry->key();
271 permRmOffer(entry->key());
279 offer_.issueIn().currency,
280 offer_.issueIn().account);
284 <<
"Removing deep frozen unfunded offer " << entry->key();
285 permRmOffer(entry->key());
300 if (*ownerFunds_ <= beast::zero)
313 if (original_funds == *ownerFunds_)
315 permRmOffer(entry->key());
316 JLOG(j_.
trace()) <<
"Removing unfunded offer " << entry->key();
321 <<
"Removing became unfunded offer " << entry->key();
328 bool const rmSmallIncreasedQOffer = [&] {
329 bool const inIsXRP =
isXRP(offer_.issueIn());
330 bool const outIsXRP =
isXRP(offer_.issueOut());
331 if (inIsXRP && !outIsXRP)
339 if constexpr (!(std::is_same_v<TIn, IOUAmount> ||
340 std::is_same_v<TOut, XRPAmount>))
341 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
343 if (!inIsXRP && outIsXRP)
346 if constexpr (!(std::is_same_v<TIn, XRPAmount> ||
347 std::is_same_v<TOut, IOUAmount>))
348 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
350 if (!inIsXRP && !outIsXRP)
353 if constexpr (!(std::is_same_v<TIn, XRPAmount> ||
354 std::is_same_v<TOut, XRPAmount>))
355 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
358 "rippls::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
363 if (rmSmallIncreasedQOffer)
373 if (original_funds == *ownerFunds_)
375 permRmOffer(entry->key());
377 <<
"Removing tiny offer due to reduced quality "
382 JLOG(j_.
trace()) <<
"Removing tiny offer that became tiny due "
383 "to reduced quality "
398OfferStream::permRmOffer(
uint256 const& offerIndex)
400 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
403template <
class TIn,
class TOut>
407 permToRemove_.insert(offerIndex);
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
Presents and consumes the offers in an order book.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
void erase(ApplyView &view)
bool step()
Advance to the next valid offer.
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
bool shouldRmSmallIncreasedQOffer() const
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
FreezeHandling
Controls the treatment of frozen account balances.
bool isXRP(AccountID const &c)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
static STAmount accountFundsHelper(ReadView const &view, AccountID const &id, STAmount const &saDefault, Issue const &, FreezeHandling freezeHandling, beast::Journal j)
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
XRPAmount toAmount< XRPAmount >(STAmount const &amt)