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