xrpld
Loading...
Searching...
No Matches
OfferStream.cpp
1#include <xrpl/tx/paths/OfferStream.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/ledger/ApplyView.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/ledger/helpers/MPTokenHelpers.h>
13#include <xrpl/ledger/helpers/PermissionedDEXHelpers.h>
14#include <xrpl/ledger/helpers/RippleStateHelpers.h>
15#include <xrpl/ledger/helpers/TokenHelpers.h>
16#include <xrpl/protocol/AccountID.h>
17#include <xrpl/protocol/Asset.h>
18#include <xrpl/protocol/Book.h>
19#include <xrpl/protocol/Concepts.h>
20#include <xrpl/protocol/Feature.h>
21#include <xrpl/protocol/IOUAmount.h>
22#include <xrpl/protocol/Indexes.h>
23#include <xrpl/protocol/MPTAmount.h>
24#include <xrpl/protocol/MPTIssue.h>
25#include <xrpl/protocol/Quality.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STLedgerEntry.h>
28#include <xrpl/protocol/XRPAmount.h>
29
30#include <algorithm>
31#include <optional>
32
33namespace xrpl {
34
35namespace {
36bool
37checkIssuers(ReadView const& view, Book const& book)
38{
39 auto issuerExists = [](ReadView const& view, Asset const& asset) -> bool {
40 auto const& issuer = asset.getIssuer();
41 return isXRP(issuer) || view.exists(keylet::account(issuer));
42 };
43 return issuerExists(view, book.in) && issuerExists(view, book.out);
44}
45} // namespace
46
47template <StepAmount TIn, StepAmount TOut>
49 ApplyView& view,
50 ApplyView& cancelView,
51 Book const& book,
53 StepCounter& counter,
54 beast::Journal journal)
55 : j_(journal)
56 , view_(view)
57 , cancelView_(cancelView)
58 , book_(book)
59 , validBook_(checkIssuers(view, book))
60 , expire_(when)
61 , tip_(view, book_)
62 , counter_(counter)
63{
64 XRPL_ASSERT(validBook_, "xrpl::TOfferStreamBase::TOfferStreamBase : valid book");
65}
66
67// Handle the case where a directory item with no corresponding ledger entry
68// is found. This shouldn't happen but if it does we clean it up.
69template <StepAmount TIn, StepAmount TOut>
70void
72{
73 // NIKB NOTE This should be using ApplyView::dirRemove, which would
74 // correctly remove the directory if its the last entry.
75 // Unfortunately this is a protocol breaking change.
76
77 auto p = view.peek(keylet::page(tip_.dir()));
78
79 if (p == nullptr)
80 {
81 JLOG(j_.error()) << "Missing directory " << tip_.dir() << " for offer " << tip_.index();
82 return;
83 }
84
85 auto v(p->getFieldV256(sfIndexes));
86 auto it(std::ranges::find(v, tip_.index()));
87
88 if (it == v.end())
89 {
90 JLOG(j_.error()) << "Missing offer " << tip_.index() << " for directory " << tip_.dir();
91 return;
92 }
93
94 v.erase(it);
95 p->setFieldV256(sfIndexes, v);
96 view.update(p);
97
98 JLOG(j_.trace()) << "Missing offer " << tip_.index() << " removed from directory "
99 << tip_.dir();
100}
101
102template <StepAmount T>
103static T
105 ReadView const& view,
106 AccountID const& id,
107 T const& amtDefault,
108 Asset const& asset,
109 FreezeHandling freezeHandling,
110 AuthHandling authHandling,
112{
113 if constexpr (std::is_same_v<T, IOUAmount>)
114 {
115 if (id == asset.getIssuer())
116 {
117 // self funded
118 return amtDefault;
119 }
120 }
121 else if constexpr (std::is_same_v<T, MPTAmount>)
122 {
123 if (id == asset.getIssuer())
124 {
125 return toAmount<T>(issuerFundsToSelfIssue(view, asset.get<MPTIssue>()));
126 }
127 }
128
129 return toAmount<T>(accountHolds(view, id, asset, freezeHandling, authHandling, j));
130}
131
132template <StepAmount TIn, StepAmount TOut>
133template <class TTakerPays, class TTakerGets>
134 requires ValidTaker<TTakerPays, TTakerGets>
135[[nodiscard]] bool
137{
138 // Consider removing the offer if:
139 // o `TakerPays` is XRP (because of XRP drops granularity) or
140 // o `TakerPays` and `TakerGets` are both IOU and `TakerPays`<`TakerGets`
141 static constexpr bool kInIsXrp = std::is_same_v<TTakerPays, XRPAmount>;
142 static constexpr bool kOutIsXrp = std::is_same_v<TTakerGets, XRPAmount>;
143
144 if constexpr (kOutIsXrp)
145 {
146 // If `TakerGets` is XRP, the worst this offer's quality can change is
147 // to about 10^-81 `TakerPays` and 1 drop `TakerGets`. This will be
148 // remarkably good quality for any realistic asset, so these offers
149 // don't need this extra check.
150 return false;
151 }
152
153 if (!ownerFunds_)
154 return false;
155
157 toAmount<TTakerPays>(offer_.amount().in), toAmount<TTakerGets>(offer_.amount().out)};
158
159 if constexpr (!kInIsXrp && !kOutIsXrp)
160 {
161 if (Number(ofrAmts.in) >= Number(ofrAmts.out))
162 return false;
163 }
164
165 TTakerGets const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
166
167 auto const effectiveAmounts = [&] {
168 if (offer_.owner() != offer_.assetOut().getIssuer() && ownerFunds < ofrAmts.out)
169 {
170 // adjust the amounts by owner funds.
171 //
172 // It turns out we can prevent order book blocking by rounding down
173 // the ceil_out() result.
174 return offer_.quality().ceilOutStrict(ofrAmts, ownerFunds, /* roundUp */ false);
175 }
176 return ofrAmts;
177 }();
178
179 // If either the effective in or out are zero then remove the offer.
180 if (effectiveAmounts.in.signum() <= 0 || effectiveAmounts.out.signum() <= 0)
181 return true;
182
183 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
184 return false;
185
186 Quality const effectiveQuality{effectiveAmounts};
187 return effectiveQuality < offer_.quality();
188}
189
190template <StepAmount TIn, StepAmount TOut>
191bool
193{
194 // Modifying the order or logic of these
195 // operations causes a protocol breaking change.
196
197 if (!validBook_)
198 return false;
199
200 for (;;)
201 {
202 ownerFunds_ = std::nullopt;
203 // BookTip::step deletes the current offer from the view before
204 // advancing to the next (unless the ledger entry is missing).
205 if (!tip_.step(j_))
206 return false;
207
208 SLE::pointer const entry = tip_.entry();
209
210 // If we exceed the maximum number of allowed steps, we're done.
211 if (!counter_.step())
212 return false;
213
214 // Remove if missing
215 if (!entry)
216 {
217 erase(view_);
219 continue;
220 }
221
222 // Remove if expired
223 using d = NetClock::duration;
224 using tp = NetClock::time_point;
225 if (entry->isFieldPresent(sfExpiration) && tp{d{(*entry)[sfExpiration]}} <= expire_)
226 {
227 JLOG(j_.trace()) << "Removing expired offer " << entry->key();
228 permRmOffer(entry->key());
229 continue;
230 }
231
232 offer_ = TOffer<TIn, TOut>(entry, tip_.quality());
233
234 auto const amount(offer_.amount());
235
236 // Remove if either amount is zero
237 if (amount.empty())
238 {
239 JLOG(j_.warn()) << "Removing bad offer " << entry->key();
240 permRmOffer(entry->key());
242 continue;
243 }
244
245 if (isDeepFrozen(view_, offer_.owner(), offer_.assetIn()))
246 {
247 JLOG(j_.trace()) << "Removing deep frozen unfunded offer " << entry->key();
248 permRmOffer(entry->key());
250 continue;
251 }
252
253 // Pre-fixCleanup3_3_0: validate domain membership for any book.
254 // Post-fixCleanup3_3_0: only validate when walking a domain book.
255 // Hybrid offers carry sfDomainID but also participate in the open
256 // book; expiry of the owner's domain credential should not evict
257 // the offer from the open book.
258 if ((!view_.rules().enabled(fixCleanup3_3_0) || book_.domain.has_value()) &&
259 entry->isFieldPresent(sfDomainID) &&
261 view_, entry->key(), entry->getFieldH256(sfDomainID), j_))
262 {
263 JLOG(j_.trace()) << "Removing offer no longer in domain " << entry->key();
264 permRmOffer(entry->key());
266 continue;
267 }
268
269 // Calculate owner funds
271 view_,
272 offer_.owner(),
273 amount.out,
274 offer_.assetOut(),
277 j_);
278
279 // Check for unfunded offer
280 if (*ownerFunds_ <= beast::kZero)
281 {
282 // If the owner's balance in the pristine view is the same,
283 // we haven't modified the balance and therefore the
284 // offer is "found unfunded" versus "became unfunded"
285 auto const originalFunds = accountFundsHelper(
287 offer_.owner(),
288 amount.out,
289 offer_.assetOut(),
292 j_);
293
294 if (originalFunds == *ownerFunds_)
295 {
296 permRmOffer(entry->key());
297 JLOG(j_.trace()) << "Removing unfunded offer " << entry->key();
298 }
299 else
300 {
301 JLOG(j_.trace()) << "Removing became unfunded offer " << entry->key();
302 }
304 // See comment at top of loop for how the offer is removed
305 continue;
306 }
307
309 {
310 auto const originalFunds = accountFundsHelper(
312 offer_.owner(),
313 amount.out,
314 offer_.assetOut(),
317 j_);
318
319 if (originalFunds == *ownerFunds_)
320 {
321 permRmOffer(entry->key());
322 JLOG(j_.trace()) << "Removing tiny offer due to reduced quality " << entry->key();
323 }
324 else
325 {
326 JLOG(j_.trace()) << "Removing tiny offer that became tiny due "
327 "to reduced quality "
328 << entry->key();
329 }
331 // See comment at top of loop for how the offer is removed
332 continue;
333 }
334
335 break;
336 }
337
338 return true;
339}
340
341template <StepAmount TIn, StepAmount TOut>
342void
344{
345 permToRemove_.insert(offerIndex);
346}
347
356
365} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
constexpr TIss const & get() const
AccountID const & getIssuer() const
Definition Asset.cpp:21
Specifies an order book.
Definition Book.h:16
Presents and consumes the offers in an order book.
boost::container::flat_set< uint256 > permToRemove_
void permRmOffer(uint256 const &offerIndex) override
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
std::chrono::duration< rep, period > duration
Definition chrono.h:45
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
Represents the logical ratio of output currency to input currency.
Definition Quality.h:91
A view into a ledger.
Definition ReadView.h:31
std::shared_ptr< STLedgerEntry > pointer
StepCounter & counter_
Definition OfferStream.h:59
std::optional< TOut > ownerFunds_
Definition OfferStream.h:58
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
bool shouldRmSmallIncreasedQOffer() const
ApplyView & cancelView_
Definition OfferStream.h:52
TOut ownerFunds() const
NetClock::time_point const expire_
Definition OfferStream.h:55
TOffer< TIn, TOut > offer_
Definition OfferStream.h:57
virtual void permRmOffer(uint256 const &offerIndex)=0
beast::Journal const j_
Definition OfferStream.h:50
bool step()
Advance to the next valid offer.
void erase(ApplyView &view)
T find(T... args)
T is_same_v
Keylet book(Book const &b)
The beginning of an order book.
Definition Indexes.cpp:235
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:363
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
static T accountFundsHelper(ReadView const &view, AccountID const &id, T const &amtDefault, Asset const &asset, FreezeHandling freezeHandling, AuthHandling authHandling, beast::Journal j)
FreezeHandling
Controls the treatment of frozen account balances.
bool isXRP(AccountID const &c)
Definition AccountID.h:70
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
T toAmount(STAmount const &amt)=delete
AuthHandling
Controls the treatment of unauthorized MPT balances.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition STExchange.h:148
STAmount issuerFundsToSelfIssue(ReadView const &view, MPTIssue const &issue)
Determine funds available for an issuer to sell in an issuer owned offer.
BaseUInt< 256 > uint256
Definition base_uint.h:562
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
Represents a pair of input and output currencies.
Definition Quality.h:26