rippled
Loading...
Searching...
No Matches
OfferStream.cpp
1#include <xrpld/app/misc/PermissionedDEXHelpers.h>
2#include <xrpld/app/tx/detail/OfferStream.h>
3
4#include <xrpl/basics/Log.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/LedgerFormats.h>
8
9namespace ripple {
10
11namespace {
12bool
13checkIssuers(ReadView const& view, Book const& book)
14{
15 auto issuerExists = [](ReadView const& view, Issue const& iss) -> bool {
16 return isXRP(iss.account) || view.read(keylet::account(iss.account));
17 };
18 return issuerExists(view, book.in) && issuerExists(view, book.out);
19}
20} // namespace
21
22template <class TIn, class TOut>
24 ApplyView& view,
25 ApplyView& cancelView,
26 Book const& book,
28 StepCounter& counter,
29 beast::Journal journal)
30 : j_(journal)
31 , view_(view)
32 , cancelView_(cancelView)
33 , book_(book)
34 , validBook_(checkIssuers(view, book))
35 , expire_(when)
36 , tip_(view, book_)
37 , counter_(counter)
38{
39 XRPL_ASSERT(
40 validBook_, "ripple::TOfferStreamBase::TOfferStreamBase : valid book");
41}
42
43// Handle the case where a directory item with no corresponding ledger entry
44// is found. This shouldn't happen but if it does we clean it up.
45template <class TIn, class TOut>
46void
48{
49 // NIKB NOTE This should be using ApplyView::dirRemove, which would
50 // correctly remove the directory if its the last entry.
51 // Unfortunately this is a protocol breaking change.
52
53 auto p = view.peek(keylet::page(tip_.dir()));
54
55 if (p == nullptr)
56 {
57 JLOG(j_.error()) << "Missing directory " << tip_.dir() << " for offer "
58 << tip_.index();
59 return;
60 }
61
62 auto v(p->getFieldV256(sfIndexes));
63 auto it(std::find(v.begin(), v.end(), tip_.index()));
65 if (it == v.end())
66 {
67 JLOG(j_.error()) << "Missing offer " << tip_.index()
68 << " for directory " << tip_.dir();
69 return;
70 }
72 v.erase(it);
73 p->setFieldV256(sfIndexes, v);
74 view.update(p);
75
76 JLOG(j_.trace()) << "Missing offer " << tip_.index()
77 << " removed from directory " << tip_.dir();
78}
79
80static STAmount
82 ReadView const& view,
83 AccountID const& id,
84 STAmount const& saDefault,
85 Issue const&,
86 FreezeHandling freezeHandling,
88{
89 return accountFunds(view, id, saDefault, freezeHandling, j);
90}
91
92static IOUAmount
94 ReadView const& view,
95 AccountID const& id,
96 IOUAmount const& amtDefault,
97 Issue const& issue,
98 FreezeHandling freezeHandling,
100{
101 if (issue.account == id)
102 // self funded
103 return amtDefault;
104
106 view, id, issue.currency, issue.account, freezeHandling, j));
107}
108
109static XRPAmount
111 ReadView const& view,
112 AccountID const& id,
113 XRPAmount const& amtDefault,
114 Issue const& issue,
115 FreezeHandling freezeHandling,
117{
119 view, id, issue.currency, issue.account, freezeHandling, j));
120}
121
122template <class TIn, class TOut>
123template <class TTakerPays, class TTakerGets>
124bool
126{
127 static_assert(
130 "STAmount is not supported");
131
132 static_assert(
135 "STAmount is not supported");
136
137 static_assert(
140 "Cannot have XRP/XRP offers");
141
142 // Consider removing the offer if:
143 // o `TakerPays` is XRP (because of XRP drops granularity) or
144 // o `TakerPays` and `TakerGets` are both IOU and `TakerPays`<`TakerGets`
145 constexpr bool const inIsXRP = std::is_same_v<TTakerPays, XRPAmount>;
146 constexpr bool const outIsXRP = std::is_same_v<TTakerGets, XRPAmount>;
147
148 if constexpr (outIsXRP)
149 {
150 // If `TakerGets` is XRP, the worst this offer's quality can change is
151 // to about 10^-81 `TakerPays` and 1 drop `TakerGets`. This will be
152 // remarkably good quality for any realistic asset, so these offers
153 // don't need this extra check.
154 return false;
155 }
156
157 TAmounts<TTakerPays, TTakerGets> const ofrAmts{
158 toAmount<TTakerPays>(offer_.amount().in),
159 toAmount<TTakerGets>(offer_.amount().out)};
160
161 if constexpr (!inIsXRP && !outIsXRP)
162 {
163 if (ofrAmts.in >= ofrAmts.out)
164 return false;
165 }
166
167 TTakerGets const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
168
169 auto const effectiveAmounts = [&] {
170 if (offer_.owner() != offer_.issueOut().account &&
171 ownerFunds < ofrAmts.out)
172 {
173 // adjust the amounts by owner funds.
174 //
175 // It turns out we can prevent order book blocking by rounding down
176 // the ceil_out() result.
177 return offer_.quality().ceil_out_strict(
178 ofrAmts, ownerFunds, /* roundUp */ false);
179 }
180 return ofrAmts;
181 }();
182
183 // If either the effective in or out are zero then remove the offer.
184 if (effectiveAmounts.in.signum() <= 0 || effectiveAmounts.out.signum() <= 0)
185 return true;
186
187 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
188 return false;
189
190 Quality const effectiveQuality{effectiveAmounts};
191 return effectiveQuality < offer_.quality();
192}
193
194template <class TIn, class TOut>
195bool
197{
198 // Modifying the order or logic of these
199 // operations causes a protocol breaking change.
200
201 if (!validBook_)
202 return false;
203
204 for (;;)
205 {
206 ownerFunds_ = std::nullopt;
207 // BookTip::step deletes the current offer from the view before
208 // advancing to the next (unless the ledger entry is missing).
209 if (!tip_.step(j_))
210 return false;
211
212 std::shared_ptr<SLE> entry = tip_.entry();
213
214 // If we exceed the maximum number of allowed steps, we're done.
215 if (!counter_.step())
216 return false;
217
218 // Remove if missing
219 if (!entry)
220 {
221 erase(view_);
222 erase(cancelView_);
223 continue;
224 }
225
226 // Remove if expired
227 using d = NetClock::duration;
228 using tp = NetClock::time_point;
229 if (entry->isFieldPresent(sfExpiration) &&
230 tp{d{(*entry)[sfExpiration]}} <= expire_)
231 {
232 JLOG(j_.trace()) << "Removing expired offer " << entry->key();
233 permRmOffer(entry->key());
234 continue;
235 }
236
237 offer_ = TOffer<TIn, TOut>(entry, tip_.quality());
238
239 auto const amount(offer_.amount());
240
241 // Remove if either amount is zero
242 if (amount.empty())
243 {
244 JLOG(j_.warn()) << "Removing bad offer " << entry->key();
245 permRmOffer(entry->key());
246 offer_ = TOffer<TIn, TOut>{};
247 continue;
248 }
249
250 bool const deepFrozen = isDeepFrozen(
251 view_,
252 offer_.owner(),
253 offer_.issueIn().currency,
254 offer_.issueIn().account);
255 if (deepFrozen)
256 {
257 JLOG(j_.trace())
258 << "Removing deep frozen unfunded offer " << entry->key();
259 permRmOffer(entry->key());
260 offer_ = TOffer<TIn, TOut>{};
261 continue;
262 }
263
264 if (entry->isFieldPresent(sfDomainID) &&
266 view_, entry->key(), entry->getFieldH256(sfDomainID), j_))
267 {
268 JLOG(j_.trace())
269 << "Removing offer no longer in domain " << entry->key();
270 permRmOffer(entry->key());
271 offer_ = TOffer<TIn, TOut>{};
272 continue;
273 }
274
275 // Calculate owner funds
276 ownerFunds_ = accountFundsHelper(
277 view_,
278 offer_.owner(),
279 amount.out,
280 offer_.issueOut(),
282 j_);
283
284 // Check for unfunded offer
285 if (*ownerFunds_ <= beast::zero)
286 {
287 // If the owner's balance in the pristine view is the same,
288 // we haven't modified the balance and therefore the
289 // offer is "found unfunded" versus "became unfunded"
290 auto const original_funds = accountFundsHelper(
291 cancelView_,
292 offer_.owner(),
293 amount.out,
294 offer_.issueOut(),
296 j_);
297
298 if (original_funds == *ownerFunds_)
299 {
300 permRmOffer(entry->key());
301 JLOG(j_.trace()) << "Removing unfunded offer " << entry->key();
302 }
303 else
304 {
305 JLOG(j_.trace())
306 << "Removing became unfunded offer " << entry->key();
307 }
308 offer_ = TOffer<TIn, TOut>{};
309 // See comment at top of loop for how the offer is removed
310 continue;
311 }
312
313 bool const rmSmallIncreasedQOffer = [&] {
314 bool const inIsXRP = isXRP(offer_.issueIn());
315 bool const outIsXRP = isXRP(offer_.issueOut());
316 if (inIsXRP && !outIsXRP)
317 {
318 // Without the `if constexpr`, the
319 // `shouldRmSmallIncreasedQOffer` template will be instantiated
320 // even if it is never used. This can cause compiler errors in
321 // some cases, hence the `if constexpr` guard.
322 // Note that TIn can be XRPAmount or STAmount, and TOut can be
323 // IOUAmount or STAmount.
324 if constexpr (!(std::is_same_v<TIn, IOUAmount> ||
326 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
327 }
328 if (!inIsXRP && outIsXRP)
329 {
330 // See comment above for `if constexpr` rationale
331 if constexpr (!(std::is_same_v<TIn, XRPAmount> ||
333 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
334 }
335 if (!inIsXRP && !outIsXRP)
336 {
337 // See comment above for `if constexpr` rationale
338 if constexpr (!(std::is_same_v<TIn, XRPAmount> ||
340 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
341 }
342 // LCOV_EXCL_START
343 UNREACHABLE(
344 "rippls::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
345 "vs XRP offer");
346 return false;
347 // LCOV_EXCL_STOP
348 }();
349
350 if (rmSmallIncreasedQOffer)
351 {
352 auto const original_funds = accountFundsHelper(
353 cancelView_,
354 offer_.owner(),
355 amount.out,
356 offer_.issueOut(),
358 j_);
359
360 if (original_funds == *ownerFunds_)
361 {
362 permRmOffer(entry->key());
363 JLOG(j_.trace())
364 << "Removing tiny offer due to reduced quality "
365 << entry->key();
366 }
367 else
368 {
369 JLOG(j_.trace()) << "Removing tiny offer that became tiny due "
370 "to reduced quality "
371 << entry->key();
372 }
373 offer_ = TOffer<TIn, TOut>{};
374 // See comment at top of loop for how the offer is removed
375 continue;
376 }
377
378 break;
379 }
380
381 return true;
382}
383
384void
385OfferStream::permRmOffer(uint256 const& offerIndex)
386{
387 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
388}
389
390template <class TIn, class TOut>
391void
393{
394 permToRemove_.insert(offerIndex);
395}
396
401
406} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream error() const
Definition Journal.h:327
Stream trace() const
Severity stream access functions.
Definition Journal.h:303
Stream warn() const
Definition Journal.h:321
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:124
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.
Specifies an order book.
Definition Book.h:17
Presents and consumes the offers in an order book.
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:27
A currency issued by an account.
Definition Issue.h:14
AccountID account
Definition Issue.h:17
Currency currency
Definition Issue.h:16
std::chrono::time_point< NetClock > time_point
Definition chrono.h:50
std::chrono::duration< rep, period > duration
Definition chrono.h:49
A view into a ledger.
Definition ReadView.h:32
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
T find(T... args)
T is_same_v
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:361
bool offerInDomain(ReadView const &view, uint256 const &offerID, Domain const &domainID, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition View.cpp:535
FreezeHandling
Controls the treatment of frozen account balances.
Definition View.h:58
@ fhZERO_IF_FROZEN
Definition View.h:58
bool isXRP(AccountID const &c)
Definition AccountID.h:71
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition View.cpp:331
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)
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition STExchange.h:153
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition View.cpp:368
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition View.cpp:1628