rippled
Loading...
Searching...
No Matches
CrossingLimits_test.cpp
1#include <test/jtx.h>
2
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/protocol/Feature.h>
5
6namespace ripple {
7namespace test {
8
10{
11public:
12 void
14 {
15 testcase("Step Limit");
16
17 using namespace jtx;
18 Env env(*this, features);
19
20 auto const gw = Account("gateway");
21 auto const USD = gw["USD"];
22
23 env.fund(XRP(100000000), gw, "alice", "bob", "carol", "dan");
24 env.trust(USD(1), "bob");
25 env(pay(gw, "bob", USD(1)));
26 env.trust(USD(1), "dan");
27 env(pay(gw, "dan", USD(1)));
28 n_offers(env, 2000, "bob", XRP(1), USD(1));
29 n_offers(env, 1, "dan", XRP(1), USD(1));
30
31 // Alice offers to buy 1000 XRP for 1000 USD. She takes Bob's first
32 // offer, removes 999 more as unfunded, then hits the step limit.
33 env(offer("alice", USD(1000), XRP(1000)));
34 env.require(balance("alice", USD(1)));
35 env.require(owners("alice", 2));
36 env.require(balance("bob", USD(0)));
37 env.require(owners("bob", 1001));
38 env.require(balance("dan", USD(1)));
39 env.require(owners("dan", 2));
40
41 // Carol offers to buy 1000 XRP for 1000 USD. She removes Bob's next
42 // 1000 offers as unfunded and hits the step limit.
43 env(offer("carol", USD(1000), XRP(1000)));
44 env.require(balance("carol", USD(none)));
45 env.require(owners("carol", 1));
46 env.require(balance("bob", USD(0)));
47 env.require(owners("bob", 1));
48 env.require(balance("dan", USD(1)));
49 env.require(owners("dan", 2));
50 }
51
52 void
54 {
55 testcase("Crossing Limit");
56
57 using namespace jtx;
58 Env env(*this, features);
59
60 auto const gw = Account("gateway");
61 auto const USD = gw["USD"];
62
63 // The payment engine allows 1000 offers to cross.
64 int const maxConsumed = 1000;
65
66 env.fund(XRP(100000000), gw, "alice", "bob", "carol");
67 int const bobsOfferCount = maxConsumed + 150;
68 env.trust(USD(bobsOfferCount), "bob");
69 env(pay(gw, "bob", USD(bobsOfferCount)));
70 env.close();
71 n_offers(env, bobsOfferCount, "bob", XRP(1), USD(1));
72
73 // Alice offers to buy Bob's offers. However she hits the offer
74 // crossing limit, so she can't buy them all at once.
75 env(offer("alice", USD(bobsOfferCount), XRP(bobsOfferCount)));
76 env.close();
77 env.require(balance("alice", USD(maxConsumed)));
78 env.require(balance("bob", USD(150)));
79 env.require(owners("bob", 150 + 1));
80
81 // Carol offers to buy 1000 XRP for 1000 USD. She takes Bob's
82 // remaining 150 offers without hitting a limit.
83 env(offer("carol", USD(1000), XRP(1000)));
84 env.close();
85 env.require(balance("carol", USD(150)));
86 env.require(balance("bob", USD(0)));
87 env.require(owners("bob", 1));
88 }
89
90 void
92 {
93 testcase("Step And Crossing Limit");
94
95 using namespace jtx;
96 Env env(*this, features);
97
98 auto const gw = Account("gateway");
99 auto const USD = gw["USD"];
100
101 env.fund(XRP(100000000), gw, "alice", "bob", "carol", "dan", "evita");
102
103 // The payment engine allows 1000 offers to cross.
104 int const maxConsumed = 1000;
105
106 int const evitasOfferCount{maxConsumed + 49};
107 env.trust(USD(1000), "alice");
108 env(pay(gw, "alice", USD(1000)));
109 env.trust(USD(1000), "carol");
110 env(pay(gw, "carol", USD(1)));
111 env.trust(USD(evitasOfferCount + 1), "evita");
112 env(pay(gw, "evita", USD(evitasOfferCount + 1)));
113
114 // The payment engine has a limit of 1000 funded or unfunded offers.
115 int const carolsOfferCount{700};
116 n_offers(env, 400, "alice", XRP(1), USD(1));
117 n_offers(env, carolsOfferCount, "carol", XRP(1), USD(1));
118 n_offers(env, evitasOfferCount, "evita", XRP(1), USD(1));
119
120 // Bob offers to buy 1000 XRP for 1000 USD. He takes all 400 USD from
121 // Alice's offers, 1 USD from Carol's and then removes 599 of Carol's
122 // offers as unfunded, before hitting the step limit.
123 env(offer("bob", USD(1000), XRP(1000)));
124 env.require(balance("bob", USD(401)));
125 env.require(balance("alice", USD(600)));
126 env.require(owners("alice", 1));
127 env.require(balance("carol", USD(0)));
128 env.require(owners("carol", carolsOfferCount - 599));
129 env.require(balance("evita", USD(evitasOfferCount + 1)));
130 env.require(owners("evita", evitasOfferCount + 1));
131
132 // Dan offers to buy maxConsumed + 50 XRP USD. He removes all of
133 // Carol's remaining offers as unfunded, then takes
134 // (maxConsumed - 100) USD from Evita's, hitting the crossing limit.
135 env(offer("dan", USD(maxConsumed + 50), XRP(maxConsumed + 50)));
136 env.require(balance("dan", USD(maxConsumed - 100)));
137 env.require(owners("dan", 2));
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", 1));
142 env.require(balance("evita", USD(150)));
143 env.require(owners("evita", 150));
144 }
145
146 void
148 {
149 testcase("Auto Bridged Limits Taker");
150
151 using namespace jtx;
152 Env env(*this, features);
153
154 auto const gw = Account("gateway");
155 auto const USD = gw["USD"];
156 auto const EUR = gw["EUR"];
157
158 env.fund(XRP(100000000), gw, "alice", "bob", "carol", "dan", "evita");
159
160 env.trust(USD(2000), "alice");
161 env(pay(gw, "alice", USD(2000)));
162 env.trust(USD(1000), "carol");
163 env(pay(gw, "carol", USD(3)));
164 env.trust(USD(1000), "evita");
165 env(pay(gw, "evita", USD(1000)));
166
167 n_offers(env, 302, "alice", EUR(2), XRP(1));
168 n_offers(env, 300, "alice", XRP(1), USD(4));
169 n_offers(env, 497, "carol", XRP(1), USD(3));
170 n_offers(env, 1001, "evita", EUR(1), USD(1));
171
172 // Bob offers to buy 2000 USD for 2000 EUR, even though he only has
173 // 1000 EUR.
174 // 1. He spends 600 EUR taking Alice's auto-bridged offers and
175 // gets 1200 USD for that.
176 // 2. He spends another 2 EUR taking one of Alice's EUR->XRP and
177 // one of Carol's XRP-USD offers. He gets 3 USD for that.
178 // 3. The remainder of Carol's offers are now unfunded. We've
179 // consumed 602 offers so far. We now chew through 398 more
180 // of Carol's unfunded offers until we hit the 1000 offer limit.
181 // This sets have_bridge to false -- we will handle no more
182 // bridged offers.
183 // 4. However, have_direct is still true. So we go around one more
184 // time and take one of Evita's offers.
185 // 5. After taking one of Evita's offers we notice (again) that our
186 // offer count was exceeded. So we completely stop after taking
187 // one of Evita's offers.
188 env.trust(EUR(10000), "bob");
189 env.close();
190 env(pay(gw, "bob", EUR(1000)));
191 env.close();
192 env(offer("bob", USD(2000), EUR(2000)));
193 env.require(balance("bob", USD(1204)));
194 env.require(balance("bob", EUR(397)));
195
196 env.require(balance("alice", USD(800)));
197 env.require(balance("alice", EUR(602)));
198 env.require(offers("alice", 1));
199 env.require(owners("alice", 3));
200
201 env.require(balance("carol", USD(0)));
202 env.require(balance("carol", EUR(none)));
203 env.require(offers("carol", 100));
204 env.require(owners("carol", 101));
205
206 env.require(balance("evita", USD(999)));
207 env.require(balance("evita", EUR(1)));
208 env.require(offers("evita", 1000));
209 env.require(owners("evita", 1002));
210
211 // Dan offers to buy 900 EUR for 900 USD.
212 // 1. He removes all 100 of Carol's remaining unfunded offers.
213 // 2. Then takes 850 USD from Evita's offers.
214 // 3. Consuming 850 of Evita's funded offers hits the crossing
215 // limit. So Dan's offer crossing stops even though he would
216 // be willing to take another 50 of Evita's offers.
217 env.trust(EUR(10000), "dan");
218 env.close();
219 env(pay(gw, "dan", EUR(1000)));
220 env.close();
221
222 env(offer("dan", USD(900), EUR(900)));
223 env.require(balance("dan", USD(850)));
224 env.require(balance("dan", EUR(150)));
225
226 env.require(balance("alice", USD(800)));
227 env.require(balance("alice", EUR(602)));
228 env.require(offers("alice", 1));
229 env.require(owners("alice", 3));
230
231 env.require(balance("carol", USD(0)));
232 env.require(balance("carol", EUR(none)));
233 env.require(offers("carol", 0));
234 env.require(owners("carol", 1));
235
236 env.require(balance("evita", USD(149)));
237 env.require(balance("evita", EUR(851)));
238 env.require(offers("evita", 150));
239 env.require(owners("evita", 152));
240 }
241
242 void
244 {
245 testcase("Auto Bridged Limits");
246
247 // If any book step in a payment strand consumes 1000 offers, the
248 // liquidity from the offers is used, but that strand will be marked as
249 // dry for the remainder of the transaction.
250
251 using namespace jtx;
252
253 auto const gw = Account("gateway");
254 auto const alice = Account("alice");
255 auto const bob = Account("bob");
256 auto const carol = Account("carol");
257
258 auto const USD = gw["USD"];
259 auto const EUR = gw["EUR"];
260
261 // There are two almost identical tests. There is a strand with a large
262 // number of unfunded offers that will cause the strand to be marked dry
263 // even though there will still be liquidity available on that strand.
264 // In the first test, the strand has the best initial quality. In the
265 // second test the strand does not have the best quality (the
266 // implementation has to handle this case correct and not mark the
267 // strand dry until the liquidity is actually used)
268
269 // The implementation allows any single step to consume at most 1000
270 // offers. With the `FlowSortStrands` feature enabled, if the total
271 // number of offers consumed by all the steps combined exceeds 1500, the
272 // payment stops.
273 {
274 Env env(*this, features);
275
276 env.fund(XRP(100000000), gw, alice, bob, carol);
277
278 env.trust(USD(4000), alice);
279 env(pay(gw, alice, USD(4000)));
280 env.trust(USD(1000), carol);
281 env(pay(gw, carol, USD(3)));
282
283 // Notice the strand with the 800 unfunded offers has the initial
284 // best quality
285 n_offers(env, 2000, alice, EUR(2), XRP(1));
286 n_offers(env, 100, alice, XRP(1), USD(4));
287 n_offers(
288 env, 801, carol, XRP(1), USD(3)); // only one offer is funded
289 n_offers(env, 1000, alice, XRP(1), USD(3));
290
291 n_offers(env, 1, alice, EUR(500), USD(500));
292
293 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000 EUR
294 // 1. The best quality is the autobridged offers that take 2 EUR
295 // and give 4 USD.
296 // Bob spends 200 EUR and receives 400 USD.
297 // 100 EUR->XRP offers consumed.
298 // 100 XRP->USD offers consumed.
299 // 200 total offers consumed.
300 //
301 // 2. The best quality is the autobridged offers that take 2 EUR
302 // and give 3 USD.
303 // a. One of Carol's offers is taken. This leaves her other
304 // offers unfunded.
305 // b. Carol's remaining 800 offers are consumed as unfunded.
306 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
307 // A book step is allowed to consume a maxium of 1000 offers
308 // at a given quality, and that limit is now reached.
309 // d. Now the strand is dry, even though there are still funded
310 // XRP(1) to USD(3) offers available.
311 // Bob has spent 400 EUR and received 600 USD in this step.
312 // 200 EUR->XRP offers consumed
313 // 800 unfunded XRP->USD offers consumed
314 // 200 funded XRP->USD offers consumed (1 carol, 199 alice)
315 // 1400 total offers consumed so far (100 left before the
316 // limit)
317 // 3. The best is the non-autobridged offers that takes 500 EUR and
318 // gives 500 USD.
319 // Bob started with 2000 EUR
320 // Bob spent 500 EUR (100+400)
321 // Bob has 1500 EUR left
322 // In this step:
323 // Bob spents 500 EUR and receives 500 USD.
324 // In total:
325 // Bob spent 1100 EUR (200 + 400 + 500)
326 // Bob has 900 EUR remaining (2000 - 1100)
327 // Bob received 1500 USD (400 + 600 + 500)
328 // Alice spent 1497 USD (100*4 + 199*3 + 500)
329 // Alice has 2503 remaining (4000 - 1497)
330 // Alice received 1100 EUR (200 + 400 + 500)
331 env.trust(EUR(10000), bob);
332 env.close();
333 env(pay(gw, bob, EUR(2000)));
334 env.close();
335 env(offer(bob, USD(4000), EUR(4000)));
336 env.close();
337
338 env.require(balance(bob, USD(1500)));
339 env.require(balance(bob, EUR(900)));
340 env.require(offers(bob, 1));
341 env.require(owners(bob, 3));
342
343 env.require(balance(alice, USD(2503)));
344 env.require(balance(alice, EUR(1100)));
345 auto const numAOffers =
346 2000 + 100 + 1000 + 1 - (2 * 100 + 2 * 199 + 1 + 1);
347 env.require(offers(alice, numAOffers));
348 env.require(owners(alice, numAOffers + 2));
349
350 env.require(offers(carol, 0));
351 }
352 {
353 Env env(*this, features);
354
355 env.fund(XRP(100000000), gw, alice, bob, carol);
356
357 env.trust(USD(4000), alice);
358 env(pay(gw, alice, USD(4000)));
359 env.trust(USD(1000), carol);
360 env(pay(gw, carol, USD(3)));
361
362 // Notice the strand with the 800 unfunded offers does not have the
363 // initial best quality
364 n_offers(env, 1, alice, EUR(1), USD(10));
365 n_offers(env, 2000, alice, EUR(2), XRP(1));
366 n_offers(env, 100, alice, XRP(1), USD(4));
367 n_offers(
368 env, 801, carol, XRP(1), USD(3)); // only one offer is funded
369 n_offers(env, 1000, alice, XRP(1), USD(3));
370
371 n_offers(env, 1, alice, EUR(499), USD(499));
372
373 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000 EUR
374 // 1. The best quality is the offer that takes 1 EUR and gives 10
375 // USD
376 // Bob spends 1 EUR and receives 10 USD.
377 //
378 // 2. The best quality is the autobridged offers that takes 2 EUR
379 // and gives 4 USD.
380 // Bob spends 200 EUR and receives 400 USD.
381 //
382 // 3. The best quality is the autobridged offers that takes 2 EUR
383 // and gives 3 USD.
384 // a. One of Carol's offers is taken. This leaves her other
385 // offers unfunded.
386 // b. Carol's remaining 800 offers are consumed as unfunded.
387 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
388 // A book step is allowed to consume a maxium of 1000 offers
389 // at a given quality, and that limit is now reached.
390 // d. Now the strand is dry, even though there are still funded
391 // XRP(1) to USD(3) offers available. Bob has spent 400 EUR and
392 // received 600 USD in this step. (200 funded offers consumed
393 // 800 unfunded offers)
394 // 4. The best is the non-autobridged offers that takes 499 EUR and
395 // gives 499 USD.
396 // Bob has 2000 EUR, and has spent 1+200+400=601 EUR. He has
397 // 1399 left. Bob spent 499 EUR and receives 499 USD.
398 // In total: Bob spent EUR(1 + 200 + 400 + 499) = EUR(1100). He
399 // started with 2000 so has 900 remaining
400 // Bob received USD(10 + 400 + 600 + 499) = USD(1509).
401 // Alice spent 10 + 100*4 + 199*3 + 499 = 1506 USD. She
402 // started with 4000 so has 2494 USD remaining. Alice
403 // received 200 + 400 + 500 = 1100 EUR
404 env.trust(EUR(10000), bob);
405 env.close();
406 env(pay(gw, bob, EUR(2000)));
407 env.close();
408 env(offer(bob, USD(4000), EUR(4000)));
409 env.close();
410
411 env.require(balance(bob, USD(1509)));
412 env.require(balance(bob, EUR(900)));
413 env.require(offers(bob, 1));
414 env.require(owners(bob, 3));
415
416 env.require(balance(alice, USD(2494)));
417 env.require(balance(alice, EUR(1100)));
418 auto const numAOffers =
419 1 + 2000 + 100 + 1000 + 1 - (1 + 2 * 100 + 2 * 199 + 1 + 1);
420 env.require(offers(alice, numAOffers));
421 env.require(owners(alice, numAOffers + 2));
422
423 env.require(offers(carol, 0));
424 }
425 }
426
427 void
429 {
430 testcase("Offer Overflow");
431
432 using namespace jtx;
433
434 auto const gw = Account("gateway");
435 auto const alice = Account("alice");
436 auto const bob = Account("bob");
437
438 auto const USD = gw["USD"];
439
440 Env env(*this, features);
441
442 env.fund(XRP(100000000), gw, alice, bob);
443
444 env.trust(USD(8000), alice);
445 env.trust(USD(8000), bob);
446 env.close();
447
448 env(pay(gw, alice, USD(8000)));
449 env.close();
450
451 // The new flow cross handles consuming excessive offers differently
452 // than the old offer crossing code. In the old code, the total number
453 // of consumed offers is tracked, and the crossings will stop after this
454 // limit is hit. In the new code, the number of offers is tracked per
455 // offerbook and per quality. This test shows how they can differ. Set
456 // up a book with many offers. At each quality keep the number of offers
457 // below the limit. However, if all the offers are consumed it would
458 // create a tecOVERSIZE error.
459
460 // The featureFlowSortStrands introduces a way of tracking the total
461 // number of consumed offers; with this feature the transaction no
462 // longer fails with a tecOVERSIZE error.
463 // The implementation allows any single step to consume at most 1000
464 // offers. With the `FlowSortStrands` feature enabled, if the total
465 // number of offers consumed by all the steps combined exceeds 1500, the
466 // payment stops. Since the first set of offers consumes 998 offers, the
467 // second set will consume 998, which is not over the limit and the
468 // payment stops. So 2*998, or 1996 is the expected value when
469 // `FlowSortStrands` is enabled.
470 n_offers(env, 998, alice, XRP(1.00), USD(1));
471 n_offers(env, 998, alice, XRP(0.99), USD(1));
472 n_offers(env, 998, alice, XRP(0.98), USD(1));
473 n_offers(env, 998, alice, XRP(0.97), USD(1));
474 n_offers(env, 998, alice, XRP(0.96), USD(1));
475 n_offers(env, 998, alice, XRP(0.95), USD(1));
476
477 bool const withSortStrands = features[featureFlowSortStrands];
478
479 auto const expectedTER = [&]() -> TER {
480 if (!withSortStrands)
481 return TER{tecOVERSIZE};
482 return tesSUCCESS;
483 }();
484
485 env(offer(bob, USD(8000), XRP(8000)), ter(expectedTER));
486 env.close();
487
488 auto const expectedUSD = [&] {
489 if (!withSortStrands)
490 return USD(0);
491 return USD(1996);
492 }();
493
494 env.require(balance(bob, expectedUSD));
495 }
496
497 void
498 run() override
499 {
500 auto testAll = [this](FeatureBitset features) {
501 testStepLimit(features);
502 testCrossingLimit(features);
503 testStepAndCrossingLimit(features);
504 testAutoBridgedLimits(features);
505 testOfferOverflow(features);
506 };
507 using namespace jtx;
508 auto const sa = testable_amendments();
509 testAll(sa);
510 testAll(sa - featureFlowSortStrands);
511 testAll(sa - featurePermissionedDEX);
512 testAll(sa - featureFlowSortStrands - featurePermissionedDEX);
513 }
514};
515
516BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, ripple, 10);
517
518} // namespace test
519} // namespace ripple
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
void testStepAndCrossingLimit(FeatureBitset features)
void testAutoBridgedLimitsTaker(FeatureBitset features)
void testAutoBridgedLimits(FeatureBitset features)
void testStepLimit(FeatureBitset features)
void testCrossingLimit(FeatureBitset features)
void run() override
Runs the suite.
void testOfferOverflow(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition Account.h:20
A transaction testing environment.
Definition Env.h:102
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:528
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:103
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:302
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:271
A balance matches.
Definition balance.h:20
Match the number of items in the account's owner directory.
Definition owners.h:54
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:16
static none_t const none
Definition tags.h:15
owner_count< ltOFFER > offers
Match the number of offers in the account's owner directory.
Definition owners.h:73
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:11
void n_offers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
FeatureBitset testable_amendments()
Definition Env.h:55
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:10
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ tecOVERSIZE
Definition TER.h:293
@ tesSUCCESS
Definition TER.h:226