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.If the total number of offers consumed by all the steps
271 // combined exceeds 1500, the payment stops.
272 {
273 Env env(*this, features);
274
275 env.fund(XRP(100000000), gw, alice, bob, carol);
276
277 env.trust(USD(4000), alice);
278 env(pay(gw, alice, USD(4000)));
279 env.trust(USD(1000), carol);
280 env(pay(gw, carol, USD(3)));
281
282 // Notice the strand with the 800 unfunded offers has the initial
283 // best quality
284 n_offers(env, 2000, alice, EUR(2), XRP(1));
285 n_offers(env, 100, alice, XRP(1), USD(4));
286 n_offers(
287 env, 801, carol, XRP(1), USD(3)); // only one offer is funded
288 n_offers(env, 1000, alice, XRP(1), USD(3));
289
290 n_offers(env, 1, alice, EUR(500), USD(500));
291
292 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000 EUR
293 // 1. The best quality is the autobridged offers that take 2 EUR
294 // and give 4 USD.
295 // Bob spends 200 EUR and receives 400 USD.
296 // 100 EUR->XRP offers consumed.
297 // 100 XRP->USD offers consumed.
298 // 200 total offers consumed.
299 //
300 // 2. The best quality is the autobridged offers that take 2 EUR
301 // and give 3 USD.
302 // a. One of Carol's offers is taken. This leaves her other
303 // offers unfunded.
304 // b. Carol's remaining 800 offers are consumed as unfunded.
305 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
306 // A book step is allowed to consume a maxium of 1000 offers
307 // at a given quality, and that limit is now reached.
308 // d. Now the strand is dry, even though there are still funded
309 // XRP(1) to USD(3) offers available.
310 // Bob has spent 400 EUR and received 600 USD in this step.
311 // 200 EUR->XRP offers consumed
312 // 800 unfunded XRP->USD offers consumed
313 // 200 funded XRP->USD offers consumed (1 carol, 199 alice)
314 // 1400 total offers consumed so far (100 left before the
315 // limit)
316 // 3. The best is the non-autobridged offers that takes 500 EUR and
317 // gives 500 USD.
318 // Bob started with 2000 EUR
319 // Bob spent 500 EUR (100+400)
320 // Bob has 1500 EUR left
321 // In this step:
322 // Bob spents 500 EUR and receives 500 USD.
323 // In total:
324 // Bob spent 1100 EUR (200 + 400 + 500)
325 // Bob has 900 EUR remaining (2000 - 1100)
326 // Bob received 1500 USD (400 + 600 + 500)
327 // Alice spent 1497 USD (100*4 + 199*3 + 500)
328 // Alice has 2503 remaining (4000 - 1497)
329 // Alice received 1100 EUR (200 + 400 + 500)
330 env.trust(EUR(10000), bob);
331 env.close();
332 env(pay(gw, bob, EUR(2000)));
333 env.close();
334 env(offer(bob, USD(4000), EUR(4000)));
335 env.close();
336
337 env.require(balance(bob, USD(1500)));
338 env.require(balance(bob, EUR(900)));
339 env.require(offers(bob, 1));
340 env.require(owners(bob, 3));
341
342 env.require(balance(alice, USD(2503)));
343 env.require(balance(alice, EUR(1100)));
344 auto const numAOffers =
345 2000 + 100 + 1000 + 1 - (2 * 100 + 2 * 199 + 1 + 1);
346 env.require(offers(alice, numAOffers));
347 env.require(owners(alice, numAOffers + 2));
348
349 env.require(offers(carol, 0));
350 }
351 {
352 Env env(*this, features);
353
354 env.fund(XRP(100000000), gw, alice, bob, carol);
355
356 env.trust(USD(4000), alice);
357 env(pay(gw, alice, USD(4000)));
358 env.trust(USD(1000), carol);
359 env(pay(gw, carol, USD(3)));
360
361 // Notice the strand with the 800 unfunded offers does not have the
362 // initial best quality
363 n_offers(env, 1, alice, EUR(1), USD(10));
364 n_offers(env, 2000, alice, EUR(2), XRP(1));
365 n_offers(env, 100, alice, XRP(1), USD(4));
366 n_offers(
367 env, 801, carol, XRP(1), USD(3)); // only one offer is funded
368 n_offers(env, 1000, alice, XRP(1), USD(3));
369
370 n_offers(env, 1, alice, EUR(499), USD(499));
371
372 // Bob offers to buy 2000 USD for 2000 EUR; He starts with 2000 EUR
373 // 1. The best quality is the offer that takes 1 EUR and gives 10
374 // USD
375 // Bob spends 1 EUR and receives 10 USD.
376 //
377 // 2. The best quality is the autobridged offers that takes 2 EUR
378 // and gives 4 USD.
379 // Bob spends 200 EUR and receives 400 USD.
380 //
381 // 3. The best quality is the autobridged offers that takes 2 EUR
382 // and gives 3 USD.
383 // a. One of Carol's offers is taken. This leaves her other
384 // offers unfunded.
385 // b. Carol's remaining 800 offers are consumed as unfunded.
386 // c. 199 of alice's XRP(1) to USD(3) offers are consumed.
387 // A book step is allowed to consume a maxium of 1000 offers
388 // at a given quality, and that limit is now reached.
389 // d. Now the strand is dry, even though there are still funded
390 // XRP(1) to USD(3) offers available. Bob has spent 400 EUR and
391 // received 600 USD in this step. (200 funded offers consumed
392 // 800 unfunded offers)
393 // 4. The best is the non-autobridged offers that takes 499 EUR and
394 // gives 499 USD.
395 // Bob has 2000 EUR, and has spent 1+200+400=601 EUR. He has
396 // 1399 left. Bob spent 499 EUR and receives 499 USD.
397 // In total: Bob spent EUR(1 + 200 + 400 + 499) = EUR(1100). He
398 // started with 2000 so has 900 remaining
399 // Bob received USD(10 + 400 + 600 + 499) = USD(1509).
400 // Alice spent 10 + 100*4 + 199*3 + 499 = 1506 USD. She
401 // started with 4000 so has 2494 USD remaining. Alice
402 // received 200 + 400 + 500 = 1100 EUR
403 env.trust(EUR(10000), bob);
404 env.close();
405 env(pay(gw, bob, EUR(2000)));
406 env.close();
407 env(offer(bob, USD(4000), EUR(4000)));
408 env.close();
409
410 env.require(balance(bob, USD(1509)));
411 env.require(balance(bob, EUR(900)));
412 env.require(offers(bob, 1));
413 env.require(owners(bob, 3));
414
415 env.require(balance(alice, USD(2494)));
416 env.require(balance(alice, EUR(1100)));
417 auto const numAOffers =
418 1 + 2000 + 100 + 1000 + 1 - (1 + 2 * 100 + 2 * 199 + 1 + 1);
419 env.require(offers(alice, numAOffers));
420 env.require(owners(alice, numAOffers + 2));
421
422 env.require(offers(carol, 0));
423 }
424 }
425
426 void
428 {
429 testcase("Offer Overflow");
430
431 using namespace jtx;
432
433 auto const gw = Account("gateway");
434 auto const alice = Account("alice");
435 auto const bob = Account("bob");
436
437 auto const USD = gw["USD"];
438
439 Env env(*this, features);
440
441 env.fund(XRP(100000000), gw, alice, bob);
442
443 env.trust(USD(8000), alice);
444 env.trust(USD(8000), bob);
445 env.close();
446
447 env(pay(gw, alice, USD(8000)));
448 env.close();
449
450 // The new flow cross handles consuming excessive offers differently
451 // than the old offer crossing code. In the old code, the total number
452 // of consumed offers is tracked, and the crossings will stop after this
453 // limit is hit. In the new code, the number of offers is tracked per
454 // offerbook and per quality. This test shows how they can differ. Set
455 // up a book with many offers. At each quality keep the number of offers
456 // below the limit. However, if all the offers are consumed it would
457 // create a tecOVERSIZE error.
458
459 // The implementation allows any single step to consume at most 1000
460 // offers. If the total number of offers consumed by all the steps
461 // combined exceeds 1500, the payment stops. Since the first set of
462 // offers consumes 998 offers, the second set will consume 998, which is
463 // not over the limit and the payment stops. So 2*998, or 1996 is the
464 // expected value.
465 n_offers(env, 998, alice, XRP(1.00), USD(1));
466 n_offers(env, 998, alice, XRP(0.99), USD(1));
467 n_offers(env, 998, alice, XRP(0.98), USD(1));
468 n_offers(env, 998, alice, XRP(0.97), USD(1));
469 n_offers(env, 998, alice, XRP(0.96), USD(1));
470 n_offers(env, 998, alice, XRP(0.95), USD(1));
471
472 env(offer(bob, USD(8000), XRP(8000)), ter(tesSUCCESS));
473 env.close();
474
475 env.require(balance(bob, USD(1996)));
476 }
477
478 void
479 run() override
480 {
481 auto testAll = [this](FeatureBitset features) {
482 testStepLimit(features);
483 testCrossingLimit(features);
484 testStepAndCrossingLimit(features);
485 testAutoBridgedLimits(features);
486 testOfferOverflow(features);
487 };
488 using namespace jtx;
489 auto const sa = testable_amendments();
490 testAll(sa);
491 testAll(sa - featurePermissionedDEX);
492 }
493};
494
495BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, ripple, 10);
496
497} // namespace test
498} // 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
@ tesSUCCESS
Definition TER.h:226