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