xrpld
Loading...
Searching...
No Matches
CrossingLimitsMPT_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/mpt.h>
8#include <test/jtx/offer.h>
9#include <test/jtx/owners.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/tags.h>
12#include <test/jtx/ter.h>
13
14#include <xrpl/beast/unit_test/suite.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/TER.h>
18
19namespace xrpl::test {
20
22{
23public:
24 void
26 {
27 testcase("Step Limit");
28
29 using namespace jtx;
30 Env env(*this, features);
31
32 auto const gw = Account("gateway");
33
34 env.fund(XRP(100'000'000), gw, "alice", "bob", "carol", "dan");
35 MPT const usd =
36 MPTTester({.env = env, .issuer = gw, .holders = {"bob", "dan"}, .maxAmt = 2});
37 env(pay(gw, "bob", usd(1)));
38 env(pay(gw, "dan", usd(1)));
39 nOffers(env, 2'000, "bob", XRP(1), usd(1));
40 nOffers(env, 1, "dan", XRP(1), usd(1));
41
42 // Alice offers to buy 1000 XRP for 1000 USD. She takes Bob's first
43 // offer, removes 999 more as unfunded, then hits the step limit.
44 env(offer("alice", usd(1'000), XRP(1'000)));
45 env.require(Balance("alice", usd(1)));
46 env.require(Owners("alice", 2));
47 env.require(Balance("bob", usd(0)));
48 env.require(Owners("bob", 1'001));
49 env.require(Balance("dan", usd(1)));
50 env.require(Owners("dan", 2));
51
52 // Carol offers to buy 1000 XRP for 1000 USD. She removes Bob's next
53 // 1000 offers as unfunded and hits the step limit.
54 env(offer("carol", usd(1'000), XRP(1'000)));
55 env.require(Balance("carol", usd(kNone)));
56 env.require(Owners("carol", 1));
57 env.require(Balance("bob", usd(0)));
58 env.require(Owners("bob", 1));
59 env.require(Balance("dan", usd(1)));
60 env.require(Owners("dan", 2));
61 }
62
63 void
65 {
66 testcase("Crossing Limit");
67
68 using namespace jtx;
69 Env env(*this, features);
70
71 auto const gw = Account("gateway");
72
73 int const maxConsumed = 1'000;
74
75 env.fund(XRP(100'000'000), gw, "alice", "bob", "carol");
76 int const bobsOfferCount = maxConsumed + 150;
77 MPT const usd =
78 MPTTester({.env = env, .issuer = gw, .holders = {"bob"}, .maxAmt = bobsOfferCount});
79 env(pay(gw, "bob", usd(bobsOfferCount)));
80 env.close();
81 nOffers(env, bobsOfferCount, "bob", XRP(1), usd(1));
82
83 // Alice offers to buy Bob's offers. However, she hits the offer
84 // crossing limit, so she can't buy them all at once.
85 env(offer("alice", usd(bobsOfferCount), XRP(bobsOfferCount)));
86 env.close();
87 env.require(Balance("alice", usd(maxConsumed)));
88 env.require(Balance("bob", usd(150)));
89 env.require(Owners("bob", 150 + 1));
90
91 // Carol offers to buy 1000 XRP for 1000 USD. She takes Bob's
92 // remaining 150 offers without hitting a limit.
93 env(offer("carol", usd(1'000), XRP(1'000)));
94 env.close();
95 env.require(Balance("carol", usd(150)));
96 env.require(Balance("bob", usd(0)));
97 env.require(Owners("bob", 1));
98 }
99
100 void
102 {
103 testcase("Step And Crossing Limit");
104
105 using namespace jtx;
106 Env env(*this, features);
107
108 auto const gw = Account("gateway");
109
110 env.fund(XRP(100'000'000), gw, "alice", "bob", "carol", "dan", "evita");
111
112 int const maxConsumed = 1'000;
113 int const evitaOfferCount{maxConsumed + 49};
114
115 MPT const usd = MPTTester({
116 .env = env,
117 .issuer = gw,
118 .holders = {"bob", "alice", "carol", "evita"},
119 .maxAmt = 2'000 + evitaOfferCount + 1,
120 });
121
122 env(pay(gw, "alice", usd(1000)));
123 env(pay(gw, "carol", usd(1)));
124 env(pay(gw, "evita", usd(evitaOfferCount + 1)));
125
126 // Give carol an extra 150 (unfunded) offers when we're using Taker
127 // to accommodate that difference.
128 int const carolOfferCount{700};
129 nOffers(env, 400, "alice", XRP(1), usd(1));
130 nOffers(env, carolOfferCount, "carol", XRP(1), usd(1));
131 nOffers(env, evitaOfferCount, "evita", XRP(1), usd(1));
132
133 // Bob offers to buy 1000 XRP for 1000 USD. He takes all 400 USD from
134 // Alice's offers, 1 USD from Carol's and then removes 599 of Carol's
135 // offers as unfunded, before hitting the step limit.
136 env(offer("bob", usd(1000), XRP(1000)));
137 env.require(Balance("bob", usd(401)));
138 env.require(Balance("alice", usd(600)));
139 env.require(Owners("alice", 1));
140 env.require(Balance("carol", usd(0)));
141 env.require(Owners("carol", carolOfferCount - 599));
142 env.require(Balance("evita", usd(evitaOfferCount + 1)));
143 env.require(Owners("evita", evitaOfferCount + 1));
144
145 // Dan offers to buy maxConsumed + 50 XRP USD. He removes all of
146 // Carol's remaining offers as unfunded, then takes
147 // (maxConsumed - 100) USD from Evita's, hitting the crossing limit.
148 env(offer("dan", usd(maxConsumed + 50), XRP(maxConsumed + 50)));
149 env.require(Balance("dan", usd(maxConsumed - 100)));
150 env.require(Owners("dan", 2));
151 env.require(Balance("alice", usd(600)));
152 env.require(Owners("alice", 1));
153 env.require(Balance("carol", usd(0)));
154 env.require(Owners("carol", 1));
155 env.require(Balance("evita", usd(150)));
156 env.require(Owners("evita", 150));
157 }
158
159 void
161 {
162 testcase("Auto Bridged Limits");
163
164 // Extracts as much as possible in one book at one Quality
165 // before proceeding to the other book. This reduces the number of
166 // times we change books.
167
168 // If any book step in a payment strand consumes 1000 offers, the
169 // liquidity from the offers is used, but that strand will be marked as
170 // dry for the remainder of the transaction.
171
172 using namespace jtx;
173
174 auto const gw = Account("gateway");
175 auto const alice = Account("alice");
176 auto const bob = Account("bob");
177 auto const carol = Account("carol");
178
179 // There are two almost identical tests. There is a strand with a large
180 // number of unfunded offers that will cause the strand to be marked dry
181 // even though there will still be liquidity available on that strand.
182 // In the first test, the strand has the best initial quality. In the
183 // second test the strand does not have the best quality (the
184 // implementation has to handle this case correct and not mark the
185 // strand dry until the liquidity is actually used)
186
187 // The implementation allows any single step to consume at most 1000
188 // offers. With the `FlowSortStrands` feature enabled, if the total
189 // number of offers consumed by all the steps combined exceeds 1500, the
190 // payment stops.
191 {
192 auto const test = [&](auto&& issue1, auto&& issue2) {
193 Env env(*this, features);
194
195 env.fund(XRP(100'000'000), gw, alice, bob, carol);
196
197 auto const usd = issue1({
198 .env = env,
199 .token = "USD",
200 .issuer = gw,
201 .holders = {alice, carol},
203 });
204 auto const eur = issue2({
205 .env = env,
206 .token = "EUR",
207 .issuer = gw,
208 .holders = {bob},
210 });
211
212 env(pay(gw, alice, usd(4'000)));
213 env(pay(gw, carol, usd(3)));
214
215 // Notice the strand with the 800 unfunded offers has the
216 // initial best quality
217 nOffers(env, 2'000, alice, eur(2), XRP(1));
218 nOffers(env, 100, alice, XRP(1), usd(4));
219 nOffers(env, 801, carol, XRP(1),
220 usd(3)); // only one offer is funded
221 nOffers(env, 1'000, alice, XRP(1), usd(3));
222
223 nOffers(env, 1, alice, eur(500), usd(500));
224
225 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000
226 // EUR
227 // 1. The best quality is the autobridged offers that take 2
228 // EUR and give 4 USD.
229 // Bob spends 200 EUR and receives 400 USD.
230 // 100 EUR->XRP offers consumed.
231 // 100 XRP->USD offers consumed.
232 // 200 total offers consumed.
233 //
234 // 2. The best quality is the autobridged offers that take 2
235 // EUR and give 3 USD.
236 // a. One of Carol's offers is taken. This leaves her other
237 // offers unfunded.
238 // b. Carol's remaining 800 offers are consumed as unfunded.
239 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
240 // A book step is allowed to consume a maximum of 1000
241 // offers at a given quality, and that limit is now
242 // reached.
243 // d. Now the strand is dry, even though there are still
244 // funded XRP(1) to USD(3) offers available.
245 // Bob has spent 400 EUR and received 600 USD in this
246 // step. 200 EUR->XRP offers consumed 800 unfunded
247 // XRP->USD offers consumed 200 funded XRP->USD offers
248 // consumed (1 carol, 199 alice) 1400 total offers
249 // consumed so far (100 left before the limit)
250 // 3. The best is the non-autobridged offers that takes 500 EUR
251 // and gives 500 USD.
252 // Bob started with 2000 EUR
253 // Bob spent 500 EUR (100+400)
254 // Bob has 1500 EUR left
255 // In this step:
256 // Bob spends 500 EUR and receives 500 USD.
257 // In total:
258 // Bob spent 1100 EUR (200 + 400 + 500)
259 // Bob has 900 EUR remaining (2000 - 1100)
260 // Bob received 1500 USD (400 + 600 + 500)
261 // Alice spent 1497 USD (100*4 + 199*3 + 500)
262 // Alice has 2503 remaining (4000 - 1497)
263 // Alice received 1100 EUR (200 + 400 + 500)
264 env(pay(gw, bob, eur(2'000)));
265 env.close();
266 env(offer(bob, usd(4'000), eur(4'000)));
267 env.close();
268
269 env.require(Balance(bob, usd(1'500)));
270 env.require(Balance(bob, eur(900)));
271 env.require(Offers(bob, 1));
272 env.require(Owners(bob, 3));
273
274 env.require(Balance(alice, usd(2'503)));
275 env.require(Balance(alice, eur(1'100)));
276 auto const numAOffers = 2'000 + 100 + 1'000 + 1 - ((2 * 100) + (2 * 199) + 1 + 1);
277 env.require(Offers(alice, numAOffers));
278 env.require(Owners(alice, numAOffers + 2));
279
280 env.require(Offers(carol, 0));
281 };
283 }
284 {
285 auto const test = [&](auto&& issue1, auto&& issue2) {
286 Env env(*this, features);
287
288 env.fund(XRP(100'000'000), gw, alice, bob, carol);
289
290 auto const usd = issue1({
291 .env = env,
292 .token = "USD",
293 .issuer = gw,
294 .holders = {alice, carol},
296 });
297 auto const eur = issue2({
298 .env = env,
299 .token = "EUR",
300 .issuer = gw,
301 .holders = {bob},
303 });
304
305 env(pay(gw, alice, usd(4'000)));
306 env(pay(gw, carol, usd(3)));
307
308 // Notice the strand with the 800 unfunded offers does not have
309 // the initial best quality
310 nOffers(env, 1, alice, eur(1), usd(10));
311 nOffers(env, 2'000, alice, eur(2), XRP(1));
312 nOffers(env, 100, alice, XRP(1), usd(4));
313 nOffers(env, 801, carol, XRP(1),
314 usd(3)); // only one offer is funded
315 nOffers(env, 1'000, alice, XRP(1), usd(3));
316
317 nOffers(env, 1, alice, eur(499), usd(499));
318
319 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000
320 // EUR
321 // 1. The best quality is the offer that takes 1 EUR and gives
322 // 10 USD
323 // Bob spends 1 EUR and receives 10 USD.
324 //
325 // 2. The best quality is the autobridged offers that takes 2
326 // EUR and gives 4 USD.
327 // Bob spends 200 EUR and receives 400 USD.
328 //
329 // 3. The best quality is the autobridged offers that takes 2
330 // EUR and gives 3 USD.
331 // a. One of Carol's offers is taken. This leaves her other
332 // offers unfunded.
333 // b. Carol's remaining 800 offers are consumed as unfunded.
334 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
335 // A book step is allowed to consume a maximum of 1000
336 // offers at a given quality, and that limit is now
337 // reached.
338 // d. Now the strand is dry, even though there are still
339 // funded XRP(1) to USD(3) offers available. Bob has spent
340 // 400 EUR and received 600 USD in this step. (200 funded
341 // offers consumed 800 unfunded offers)
342 // 4. The best is the non-autobridged offers that takes 499 EUR
343 // and gives 499 USD.
344 // Bob has 2000 EUR, and has spent 1+200+400=601 EUR. He has
345 // 1399 left. Bob spent 499 EUR and receives 499 USD.
346 // In total: Bob spent EUR(1 + 200 + 400 + 499) = EUR(1100). He
347 // started with 2000 so has 900 remaining
348 // Bob received USD(10 + 400 + 600 + 499) = USD(1509).
349 // Alice spent 10 + 100*4 + 199*3 + 499 = 1506 USD.
350 // She started with 4000 so has 2494 USD remaining.
351 // Alice received 200 + 400 + 500 = 1100 EUR
352 env.close();
353 env(pay(gw, bob, eur(2'000)));
354 env.close();
355 env(offer(bob, usd(4'000), eur(4'000)));
356 env.close();
357
358 env.require(Balance(bob, usd(1'509)));
359 env.require(Balance(bob, eur(900)));
360 env.require(Offers(bob, 1));
361 env.require(Owners(bob, 3));
362
363 env.require(Balance(alice, usd(2'494)));
364 env.require(Balance(alice, eur(1'100)));
365 auto const numAOffers =
366 1 + 2'000 + 100 + 1'000 + 1 - (1 + (2 * 100) + (2 * 199) + 1 + 1);
367 env.require(Offers(alice, numAOffers));
368 env.require(Owners(alice, numAOffers + 2));
369
370 env.require(Offers(carol, 0));
371 };
373 }
374 }
375
376 void
378 {
379 testcase("Offer Overflow");
380
381 using namespace jtx;
382
383 auto const gw = Account("gateway");
384 auto const alice = Account("alice");
385 auto const bob = Account("bob");
386
387 Env env(*this, features);
388
389 env.fund(XRP(100'000'000), gw, alice, bob);
390
391 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}});
392
393 env(pay(gw, alice, usd(8'000)));
394 env.close();
395
396 // The new flow cross handles consuming excessive offers differently
397 // than the old offer crossing code. In the old code, the total number
398 // of consumed offers is tracked, and the crossings will stop after this
399 // limit is hit. In the new code, the number of offers is tracked per
400 // offerbook and per quality. This test shows how they can differ. Set
401 // up a book with many offers. At each quality keep the number of offers
402 // below the limit. However, if all the offers are consumed it would
403 // create a tecOVERSIZE error.
404
405 // The featureFlowSortStrands introduces a way of tracking the total
406 // number of consumed offers; with this feature the transaction no
407 // longer fails with a tecOVERSIZE error.
408 // The implementation allows any single step to consume at most 1000
409 // offers. With the `FlowSortStrands` feature enabled, if the total
410 // number of offers consumed by all the steps combined exceeds 1500, the
411 // payment stops. Since the first set of offers consumes 998 offers, the
412 // second set will consume 998, which is not over the limit and the
413 // payment stops. So 2*998, or 1996 is the expected value when
414 // `FlowSortStrands` is enabled.
415 nOffers(env, 998, alice, XRP(1.00), usd(1));
416 nOffers(env, 998, alice, XRP(0.99), usd(1));
417 nOffers(env, 998, alice, XRP(0.98), usd(1));
418 nOffers(env, 998, alice, XRP(0.97), usd(1));
419 nOffers(env, 998, alice, XRP(0.96), usd(1));
420 nOffers(env, 998, alice, XRP(0.95), usd(1));
421
422 auto const expectedTER = tesSUCCESS;
423
424 env(offer(bob, usd(8'000), XRP(8'000)), Ter(expectedTER));
425 env.close();
426
427 auto const expectedUSD = usd(1'996);
428
429 env.require(Balance(bob, expectedUSD));
430 }
431
432 void
433 run() override
434 {
435 using namespace jtx;
436 auto const features = testableAmendments();
437 testStepLimit(features);
438 testCrossingLimit(features);
439 testStepAndCrossingLimit(features);
440 testAutoBridgedLimits(features);
441 testOfferOverflow(features);
442 }
443};
444
445BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimitsMPT, tx, xrpl, 10);
446
447} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
void testAutoBridgedLimits(FeatureBitset features)
void testStepLimit(FeatureBitset features)
void testStepAndCrossingLimit(FeatureBitset features)
void testCrossingLimit(FeatureBitset features)
void testOfferOverflow(FeatureBitset features)
A balance matches.
Definition balance.h:24
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
Definition Env.cpp:254
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
Converts to MPT Issue or STAmount.
Match the number of items in the account's owner directory.
Definition owners.h:55
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
void nOffers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
static NoneT const kNone
Definition tags.h:9
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
void testHelper2TokensMix(TTester &&tester)
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:92
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
Definition owners.h:137
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, xrpl, 10)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:297
@ tesSUCCESS
Definition TER.h:253