xrpld
Loading...
Searching...
No Matches
Clawback_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/CaptureLogs.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/flags.h>
8#include <test/jtx/pay.h>
9#include <test/jtx/ter.h>
10#include <test/jtx/ticket.h>
11#include <test/jtx/trust.h>
12#include <test/jtx/txflags.h>
13
14#include <xrpl/basics/contract.h>
15#include <xrpl/beast/unit_test/suite.h>
16#include <xrpl/protocol/Feature.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/Issue.h>
19#include <xrpl/protocol/LedgerFormats.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/TxFlags.h>
24#include <xrpl/protocol/UintTypes.h>
25
26#include <array>
27#include <cstdint>
28#include <memory>
29#include <stdexcept>
30#include <string>
31
32namespace xrpl {
33
35{
36 template <class T>
37 static std::string
38 toString(T const& t)
39 {
40 return boost::lexical_cast<std::string>(t);
41 }
42
43 // Helper function that returns the number of tickets held by an account.
44 static std::uint32_t
46 {
47 std::uint32_t ret{0};
48 if (auto const sleAcct = env.le(acct))
49 ret = sleAcct->at(~sfTicketCount).value_or(0);
50 return ret;
51 }
52
53 // Helper function that returns the freeze status of a trustline
54 static bool
56 test::jtx::Env const& env,
57 test::jtx::Account const& src,
58 test::jtx::Account const& dst,
59 Currency const& cur)
60 {
61 if (auto const sle = env.le(keylet::trustLine(src, dst, cur)))
62 {
63 auto const useHigh = src.id() > dst.id();
64 return sle->isFlag(useHigh ? lsfHighFreeze : lsfLowFreeze);
65 }
66 Throw<std::runtime_error>("No line in getLineFreezeFlag");
67 return false; // silence warning
68 }
69
70 void
72 {
73 testcase("Enable AllowTrustLineClawback flag");
74 using namespace test::jtx;
75
76 // Test that one can successfully set asfAllowTrustLineClawback flag.
77 // If successful, asfNoFreeze can no longer be set.
78 // Also, asfAllowTrustLineClawback cannot be cleared.
79 {
80 Env env(*this, features);
81 Account const alice{"alice"};
82
83 env.fund(XRP(1000), alice);
84 env.close();
85
86 // set asfAllowTrustLineClawback
87 env(fset(alice, asfAllowTrustLineClawback));
88 env.close();
89 env.require(Flags(alice, asfAllowTrustLineClawback));
90
91 // clear asfAllowTrustLineClawback does nothing
92 env(fclear(alice, asfAllowTrustLineClawback));
93 env.close();
94 env.require(Flags(alice, asfAllowTrustLineClawback));
95
96 // asfNoFreeze cannot be set when asfAllowTrustLineClawback is set
97 env.require(Nflags(alice, asfNoFreeze));
98 env(fset(alice, asfNoFreeze), Ter(tecNO_PERMISSION));
99 env.close();
100 }
101
102 // Test that asfAllowTrustLineClawback cannot be set when
103 // asfNoFreeze has been set
104 {
105 Env env(*this, features);
106 Account const alice{"alice"};
107
108 env.fund(XRP(1000), alice);
109 env.close();
110
111 env.require(Nflags(alice, asfNoFreeze));
112
113 // set asfNoFreeze
114 env(fset(alice, asfNoFreeze));
115 env.close();
116
117 // NoFreeze is set
118 env.require(Flags(alice, asfNoFreeze));
119
120 // asfAllowTrustLineClawback cannot be set if asfNoFreeze is set
121 env(fset(alice, asfAllowTrustLineClawback), Ter(tecNO_PERMISSION));
122 env.close();
123
124 env.require(Nflags(alice, asfAllowTrustLineClawback));
125 }
126
127 // Test that asfAllowTrustLineClawback is not allowed when owner dir is
128 // non-empty
129 {
130 Env env(*this, features);
131
132 Account const alice{"alice"};
133 Account const bob{"bob"};
134
135 env.fund(XRP(1000), alice, bob);
136 env.close();
137
138 auto const usd = alice["USD"];
139 env.require(Nflags(alice, asfAllowTrustLineClawback));
140
141 // alice issues 10 USD to bob
142 env.trust(usd(1000), bob);
143 env(pay(alice, bob, usd(10)));
144 env.close();
145
146 BEAST_EXPECT(ownerCount(env, alice) == 0);
147 BEAST_EXPECT(ownerCount(env, bob) == 1);
148
149 // alice fails to enable clawback because she has trustline with bob
150 env(fset(alice, asfAllowTrustLineClawback), Ter(tecOWNERS));
151 env.close();
152
153 // bob sets trustline to default limit and pays alice back to delete
154 // the trustline
155 env(trust(bob, usd(0), 0));
156 env(pay(bob, alice, usd(10)));
157
158 BEAST_EXPECT(ownerCount(env, alice) == 0);
159 BEAST_EXPECT(ownerCount(env, bob) == 0);
160
161 // alice now is able to set asfAllowTrustLineClawback
162 env(fset(alice, asfAllowTrustLineClawback));
163 env.close();
164 env.require(Flags(alice, asfAllowTrustLineClawback));
165
166 BEAST_EXPECT(ownerCount(env, alice) == 0);
167 BEAST_EXPECT(ownerCount(env, bob) == 0);
168 }
169 }
170
171 void
173 {
174 testcase("Validation");
175 using namespace test::jtx;
176
177 // Test that Clawback tx fails for the following:
178 // 1. invalid flag
179 // 2. negative STAmount
180 // 3. zero STAmount
181 // 4. XRP amount
182 // 5. `account` and `issuer` fields are same account
183 // 6. trustline has a balance of 0
184 // 7. trustline does not exist
185 {
186 Env env(*this, features);
187
188 Account const alice{"alice"};
189 Account const bob{"bob"};
190
191 env.fund(XRP(1000), alice, bob);
192 env.close();
193
194 // alice sets asfAllowTrustLineClawback
195 env(fset(alice, asfAllowTrustLineClawback));
196 env.close();
197 env.require(Flags(alice, asfAllowTrustLineClawback));
198
199 auto const usd = alice["USD"];
200
201 // alice issues 10 USD to bob
202 env.trust(usd(1000), bob);
203 env(pay(alice, bob, usd(10)));
204 env.close();
205
206 env.require(Balance(bob, alice["USD"](10)));
207 env.require(Balance(alice, bob["USD"](-10)));
208
209 // fails due to invalid flag
210 env(claw(alice, bob["USD"](5)), Txflags(0x00008000), Ter(temINVALID_FLAG));
211 env.close();
212
213 // fails due to negative amount
214 env(claw(alice, bob["USD"](-5)), Ter(temBAD_AMOUNT));
215 env.close();
216
217 // fails due to zero amount
218 env(claw(alice, bob["USD"](0)), Ter(temBAD_AMOUNT));
219 env.close();
220
221 // fails because amount is in XRP
222 env(claw(alice, XRP(10)), Ter(temBAD_AMOUNT));
223 env.close();
224
225 // fails when `issuer` field in `amount` is not token holder
226 // NOTE: we are using the `issuer` field for the token holder
227 env(claw(alice, alice["USD"](5)), Ter(temBAD_AMOUNT));
228 env.close();
229
230 // bob pays alice back, trustline has a balance of 0
231 env(pay(bob, alice, usd(10)));
232 env.close();
233
234 // bob still owns the trustline that has 0 balance
235 BEAST_EXPECT(ownerCount(env, alice) == 0);
236 BEAST_EXPECT(ownerCount(env, bob) == 1);
237 env.require(Balance(bob, alice["USD"](0)));
238 env.require(Balance(alice, bob["USD"](0)));
239
240 // clawback fails because because balance is 0
241 env(claw(alice, bob["USD"](5)), Ter(tecINSUFFICIENT_FUNDS));
242 env.close();
243
244 // set the limit to default, which should delete the trustline
245 env(trust(bob, usd(0), 0));
246 env.close();
247
248 // bob no longer owns the trustline
249 BEAST_EXPECT(ownerCount(env, alice) == 0);
250 BEAST_EXPECT(ownerCount(env, bob) == 0);
251
252 // clawback fails because trustline does not exist
253 env(claw(alice, bob["USD"](5)), Ter(tecNO_LINE));
254 env.close();
255 }
256 }
257
258 void
260 {
261 // Checks the tx submitter has the permission to clawback.
262 // Exercises preclaim code
263 testcase("Permission");
264 using namespace test::jtx;
265
266 // Clawing back from an non-existent account returns error
267 {
268 Env env(*this, features);
269
270 Account const alice{"alice"};
271 Account const bob{"bob"};
272
273 // bob's account is not funded and does not exist
274 env.fund(XRP(1000), alice);
275 env.close();
276
277 // alice sets asfAllowTrustLineClawback
278 env(fset(alice, asfAllowTrustLineClawback));
279 env.close();
280 env.require(Flags(alice, asfAllowTrustLineClawback));
281
282 // bob, the token holder, does not exist
283 env(claw(alice, bob["USD"](5)), Ter(terNO_ACCOUNT));
284 env.close();
285 }
286
287 // Test that trustline cannot be clawed by someone who is
288 // not the issuer of the currency
289 {
290 Env env(*this, features);
291
292 Account const alice{"alice"};
293 Account const bob{"bob"};
294 Account const cindy{"cindy"};
295
296 env.fund(XRP(1000), alice, bob, cindy);
297 env.close();
298
299 auto const usd = alice["USD"];
300
301 // alice sets asfAllowTrustLineClawback
302 env(fset(alice, asfAllowTrustLineClawback));
303 env.close();
304 env.require(Flags(alice, asfAllowTrustLineClawback));
305
306 // cindy sets asfAllowTrustLineClawback
307 env(fset(cindy, asfAllowTrustLineClawback));
308 env.close();
309 env.require(Flags(cindy, asfAllowTrustLineClawback));
310
311 // alice issues 1000 USD to bob
312 env.trust(usd(1000), bob);
313 env(pay(alice, bob, usd(1000)));
314 env.close();
315
316 env.require(Balance(bob, alice["USD"](1000)));
317 env.require(Balance(alice, bob["USD"](-1000)));
318
319 // cindy tries to claw from bob, and fails because trustline does
320 // not exist
321 env(claw(cindy, bob["USD"](200)), Ter(tecNO_LINE));
322 env.close();
323 }
324
325 // When a trustline is created between issuer and holder,
326 // we must make sure the holder is unable to claw back from
327 // the issuer by impersonating the issuer account.
328 //
329 // This must be tested bidirectionally for both accounts because the
330 // issuer could be either the low or high account in the trustline
331 // object
332 {
333 Env env(*this, features);
334
335 Account const alice{"alice"};
336 Account const bob{"bob"};
337
338 env.fund(XRP(1000), alice, bob);
339 env.close();
340
341 auto const usd = alice["USD"];
342 auto const cad = bob["CAD"];
343
344 // alice sets asfAllowTrustLineClawback
345 env(fset(alice, asfAllowTrustLineClawback));
346 env.close();
347 env.require(Flags(alice, asfAllowTrustLineClawback));
348
349 // bob sets asfAllowTrustLineClawback
350 env(fset(bob, asfAllowTrustLineClawback));
351 env.close();
352 env.require(Flags(bob, asfAllowTrustLineClawback));
353
354 // alice issues 10 USD to bob.
355 // bob then attempts to submit a clawback tx to claw USD from alice.
356 // this must FAIL, because bob is not the issuer for this
357 // trustline!!!
358 {
359 // bob creates a trustline with alice, and alice sends 10 USD to
360 // bob
361 env.trust(usd(1000), bob);
362 env(pay(alice, bob, usd(10)));
363 env.close();
364
365 env.require(Balance(bob, alice["USD"](10)));
366 env.require(Balance(alice, bob["USD"](-10)));
367
368 // bob cannot claw back USD from alice because he's not the
369 // issuer
370 env(claw(bob, alice["USD"](5)), Ter(tecNO_PERMISSION));
371 env.close();
372 }
373
374 // bob issues 10 CAD to alice.
375 // alice then attempts to submit a clawback tx to claw CAD from bob.
376 // this must FAIL, because alice is not the issuer for this
377 // trustline!!!
378 {
379 // alice creates a trustline with bob, and bob sends 10 CAD to
380 // alice
381 env.trust(cad(1000), alice);
382 env(pay(bob, alice, cad(10)));
383 env.close();
384
385 env.require(Balance(bob, alice["CAD"](-10)));
386 env.require(Balance(alice, bob["CAD"](10)));
387
388 // alice cannot claw back CAD from bob because she's not the
389 // issuer
390 env(claw(alice, bob["CAD"](5)), Ter(tecNO_PERMISSION));
391 env.close();
392 }
393 }
394 }
395
396 void
398 {
399 testcase("Enable clawback");
400 using namespace test::jtx;
401
402 // Test that alice is able to successfully clawback tokens from bob
403 Env env(*this, features);
404
405 Account const alice{"alice"};
406 Account const bob{"bob"};
407
408 env.fund(XRP(1000), alice, bob);
409 env.close();
410
411 auto const usd = alice["USD"];
412
413 // alice sets asfAllowTrustLineClawback
414 env(fset(alice, asfAllowTrustLineClawback));
415 env.close();
416 env.require(Flags(alice, asfAllowTrustLineClawback));
417
418 // alice issues 1000 USD to bob
419 env.trust(usd(1000), bob);
420 env(pay(alice, bob, usd(1000)));
421 env.close();
422
423 env.require(Balance(bob, alice["USD"](1000)));
424 env.require(Balance(alice, bob["USD"](-1000)));
425
426 // alice claws back 200 USD from bob
427 env(claw(alice, bob["USD"](200)));
428 env.close();
429
430 // bob should have 800 USD left
431 env.require(Balance(bob, alice["USD"](800)));
432 env.require(Balance(alice, bob["USD"](-800)));
433
434 // alice claws back 800 USD from bob again
435 env(claw(alice, bob["USD"](800)));
436 env.close();
437
438 // trustline has a balance of 0
439 env.require(Balance(bob, alice["USD"](0)));
440 env.require(Balance(alice, bob["USD"](0)));
441 }
442
443 void
445 {
446 // Test scenarios where multiple trustlines are involved
447 testcase("Multi line");
448 using namespace test::jtx;
449
450 // Both alice and bob issues their own "USD" to cindy.
451 // When alice and bob tries to claw back, they will only
452 // claw back from their respective trustline.
453 {
454 Env env(*this, features);
455
456 Account const alice{"alice"};
457 Account const bob{"bob"};
458 Account const cindy{"cindy"};
459
460 env.fund(XRP(1000), alice, bob, cindy);
461 env.close();
462
463 // alice sets asfAllowTrustLineClawback
464 env(fset(alice, asfAllowTrustLineClawback));
465 env.close();
466 env.require(Flags(alice, asfAllowTrustLineClawback));
467
468 // bob sets asfAllowTrustLineClawback
469 env(fset(bob, asfAllowTrustLineClawback));
470 env.close();
471 env.require(Flags(bob, asfAllowTrustLineClawback));
472
473 // alice sends 1000 USD to cindy
474 env.trust(alice["USD"](1000), cindy);
475 env(pay(alice, cindy, alice["USD"](1000)));
476 env.close();
477
478 // bob sends 1000 USD to cindy
479 env.trust(bob["USD"](1000), cindy);
480 env(pay(bob, cindy, bob["USD"](1000)));
481 env.close();
482
483 // alice claws back 200 USD from cindy
484 env(claw(alice, cindy["USD"](200)));
485 env.close();
486
487 // cindy has 800 USD left in alice's trustline after clawed by alice
488 env.require(Balance(cindy, alice["USD"](800)));
489 env.require(Balance(alice, cindy["USD"](-800)));
490
491 // cindy still has 1000 USD in bob's trustline
492 env.require(Balance(cindy, bob["USD"](1000)));
493 env.require(Balance(bob, cindy["USD"](-1000)));
494
495 // bob claws back 600 USD from cindy
496 env(claw(bob, cindy["USD"](600)));
497 env.close();
498
499 // cindy has 400 USD left in bob's trustline after clawed by bob
500 env.require(Balance(cindy, bob["USD"](400)));
501 env.require(Balance(bob, cindy["USD"](-400)));
502
503 // cindy still has 800 USD in alice's trustline
504 env.require(Balance(cindy, alice["USD"](800)));
505 env.require(Balance(alice, cindy["USD"](-800)));
506 }
507
508 // alice issues USD to both bob and cindy.
509 // when alice claws back from bob, only bob's USD balance is
510 // affected, and cindy's balance remains unchanged, and vice versa.
511 {
512 Env env(*this, features);
513
514 Account const alice{"alice"};
515 Account const bob{"bob"};
516 Account const cindy{"cindy"};
517
518 env.fund(XRP(1000), alice, bob, cindy);
519 env.close();
520
521 auto const usd = alice["USD"];
522
523 // alice sets asfAllowTrustLineClawback
524 env(fset(alice, asfAllowTrustLineClawback));
525 env.close();
526 env.require(Flags(alice, asfAllowTrustLineClawback));
527
528 // alice sends 600 USD to bob
529 env.trust(usd(1000), bob);
530 env(pay(alice, bob, usd(600)));
531 env.close();
532
533 env.require(Balance(alice, bob["USD"](-600)));
534 env.require(Balance(bob, alice["USD"](600)));
535
536 // alice sends 1000 USD to cindy
537 env.trust(usd(1000), cindy);
538 env(pay(alice, cindy, usd(1000)));
539 env.close();
540
541 env.require(Balance(alice, cindy["USD"](-1000)));
542 env.require(Balance(cindy, alice["USD"](1000)));
543
544 // alice claws back 500 USD from bob
545 env(claw(alice, bob["USD"](500)));
546 env.close();
547
548 // bob's balance is reduced
549 env.require(Balance(alice, bob["USD"](-100)));
550 env.require(Balance(bob, alice["USD"](100)));
551
552 // cindy's balance is unchanged
553 env.require(Balance(alice, cindy["USD"](-1000)));
554 env.require(Balance(cindy, alice["USD"](1000)));
555
556 // alice claws back 300 USD from cindy
557 env(claw(alice, cindy["USD"](300)));
558 env.close();
559
560 // bob's balance is unchanged
561 env.require(Balance(alice, bob["USD"](-100)));
562 env.require(Balance(bob, alice["USD"](100)));
563
564 // cindy's balance is reduced
565 env.require(Balance(alice, cindy["USD"](-700)));
566 env.require(Balance(cindy, alice["USD"](700)));
567 }
568 }
569
570 void
572 {
573 testcase("Bidirectional line");
574 using namespace test::jtx;
575
576 // Test when both alice and bob issues USD to each other.
577 // This scenario creates only one trustline.
578 // In this case, both alice and bob can be seen as the "issuer"
579 // and they can send however many USDs to each other.
580 // We test that only the person who has a negative balance from their
581 // perspective is allowed to clawback
582 Env env(*this, features);
583
584 Account const alice{"alice"};
585 Account const bob{"bob"};
586
587 env.fund(XRP(1000), alice, bob);
588 env.close();
589
590 // alice sets asfAllowTrustLineClawback
591 env(fset(alice, asfAllowTrustLineClawback));
592 env.close();
593 env.require(Flags(alice, asfAllowTrustLineClawback));
594
595 // bob sets asfAllowTrustLineClawback
596 env(fset(bob, asfAllowTrustLineClawback));
597 env.close();
598 env.require(Flags(bob, asfAllowTrustLineClawback));
599
600 // alice issues 1000 USD to bob
601 env.trust(alice["USD"](1000), bob);
602 env(pay(alice, bob, alice["USD"](1000)));
603 env.close();
604
605 BEAST_EXPECT(ownerCount(env, alice) == 0);
606 BEAST_EXPECT(ownerCount(env, bob) == 1);
607
608 // bob is the holder, and alice is the issuer
609 env.require(Balance(bob, alice["USD"](1000)));
610 env.require(Balance(alice, bob["USD"](-1000)));
611
612 // bob issues 1500 USD to alice
613 env.trust(bob["USD"](1500), alice);
614 env(pay(bob, alice, bob["USD"](1500)));
615 env.close();
616
617 BEAST_EXPECT(ownerCount(env, alice) == 1);
618 BEAST_EXPECT(ownerCount(env, bob) == 1);
619
620 // bob has negative 500 USD because bob issued 500 USD more than alice
621 // bob can now been seen as the issuer, while alice is the holder
622 env.require(Balance(bob, alice["USD"](-500)));
623 env.require(Balance(alice, bob["USD"](500)));
624
625 // At this point, both alice and bob are the issuers of USD
626 // and can send USD to each other through one trustline
627
628 // alice fails to clawback. Even though she is also an issuer,
629 // the trustline balance is positive from her perspective
630 env(claw(alice, bob["USD"](200)), Ter(tecNO_PERMISSION));
631 env.close();
632
633 // bob is able to successfully clawback from alice because
634 // the trustline balance is negative from his perspective
635 env(claw(bob, alice["USD"](200)));
636 env.close();
637
638 env.require(Balance(bob, alice["USD"](-300)));
639 env.require(Balance(alice, bob["USD"](300)));
640
641 // alice pays bob 1000 USD
642 env(pay(alice, bob, alice["USD"](1000)));
643 env.close();
644
645 // bob's balance becomes positive from his perspective because
646 // alice issued more USD than the balance
647 env.require(Balance(bob, alice["USD"](700)));
648 env.require(Balance(alice, bob["USD"](-700)));
649
650 // bob is now the holder and fails to clawback
651 env(claw(bob, alice["USD"](200)), Ter(tecNO_PERMISSION));
652 env.close();
653
654 // alice successfully claws back
655 env(claw(alice, bob["USD"](200)));
656 env.close();
657
658 env.require(Balance(bob, alice["USD"](500)));
659 env.require(Balance(alice, bob["USD"](-500)));
660 }
661
662 void
664 {
665 testcase("Delete default trustline");
666 using namespace test::jtx;
667
668 // If clawback results the trustline to be default,
669 // trustline should be automatically deleted
670 Env env(*this, features);
671 Account const alice{"alice"};
672 Account const bob{"bob"};
673
674 env.fund(XRP(1000), alice, bob);
675 env.close();
676
677 auto const usd = alice["USD"];
678
679 // alice sets asfAllowTrustLineClawback
680 env(fset(alice, asfAllowTrustLineClawback));
681 env.close();
682 env.require(Flags(alice, asfAllowTrustLineClawback));
683
684 // alice issues 1000 USD to bob
685 env.trust(usd(1000), bob);
686 env(pay(alice, bob, usd(1000)));
687 env.close();
688
689 BEAST_EXPECT(ownerCount(env, alice) == 0);
690 BEAST_EXPECT(ownerCount(env, bob) == 1);
691
692 env.require(Balance(bob, alice["USD"](1000)));
693 env.require(Balance(alice, bob["USD"](-1000)));
694
695 // set limit to default,
696 env(trust(bob, usd(0), 0));
697 env.close();
698
699 BEAST_EXPECT(ownerCount(env, alice) == 0);
700 BEAST_EXPECT(ownerCount(env, bob) == 1);
701
702 // alice claws back full amount from bob, and should also delete
703 // trustline
704 env(claw(alice, bob["USD"](1000)));
705 env.close();
706
707 // bob no longer owns the trustline because it was deleted
708 BEAST_EXPECT(ownerCount(env, alice) == 0);
709 BEAST_EXPECT(ownerCount(env, bob) == 0);
710 }
711
712 void
714 {
715 testcase("Frozen trustline");
716 using namespace test::jtx;
717
718 // Claws back from frozen trustline
719 // and the trustline should remain frozen
720 Env env(*this, features);
721 Account const alice{"alice"};
722 Account const bob{"bob"};
723
724 env.fund(XRP(1000), alice, bob);
725 env.close();
726
727 auto const usd = alice["USD"];
728
729 // alice sets asfAllowTrustLineClawback
730 env(fset(alice, asfAllowTrustLineClawback));
731 env.close();
732 env.require(Flags(alice, asfAllowTrustLineClawback));
733
734 // alice issues 1000 USD to bob
735 env.trust(usd(1000), bob);
736 env(pay(alice, bob, usd(1000)));
737 env.close();
738
739 env.require(Balance(bob, alice["USD"](1000)));
740 env.require(Balance(alice, bob["USD"](-1000)));
741
742 // freeze trustline
743 env(trust(alice, bob["USD"](0), tfSetFreeze));
744 env.close();
745
746 // alice claws back 200 USD from bob
747 env(claw(alice, bob["USD"](200)));
748 env.close();
749
750 // bob should have 800 USD left
751 env.require(Balance(bob, alice["USD"](800)));
752 env.require(Balance(alice, bob["USD"](-800)));
753
754 // trustline remains frozen
755 BEAST_EXPECT(getLineFreezeFlag(env, alice, bob, usd.currency));
756 }
757
758 void
760 {
761 testcase("Amount exceeds available");
762 using namespace test::jtx;
763
764 // When alice tries to claw back an amount that is greater
765 // than what bob holds, only the max available balance is clawed
766 Env env(*this, features);
767 Account const alice{"alice"};
768 Account const bob{"bob"};
769
770 env.fund(XRP(1000), alice, bob);
771 env.close();
772
773 auto const usd = alice["USD"];
774
775 // alice sets asfAllowTrustLineClawback
776 env(fset(alice, asfAllowTrustLineClawback));
777 env.close();
778 env.require(Flags(alice, asfAllowTrustLineClawback));
779
780 // alice issues 1000 USD to bob
781 env.trust(usd(1000), bob);
782 env(pay(alice, bob, usd(1000)));
783 env.close();
784
785 env.require(Balance(bob, alice["USD"](1000)));
786 env.require(Balance(alice, bob["USD"](-1000)));
787
788 // alice tries to claw back 2000 USD
789 env(claw(alice, bob["USD"](2000)));
790 env.close();
791
792 // check alice and bob's balance.
793 // alice was only able to claw back 1000 USD at maximum
794 env.require(Balance(bob, alice["USD"](0)));
795 env.require(Balance(alice, bob["USD"](0)));
796
797 // bob still owns the trustline because trustline is not in default
798 // state
799 BEAST_EXPECT(ownerCount(env, alice) == 0);
800 BEAST_EXPECT(ownerCount(env, bob) == 1);
801
802 // set limit to default,
803 env(trust(bob, usd(0), 0));
804 env.close();
805
806 // verify that bob's trustline was deleted
807 BEAST_EXPECT(ownerCount(env, alice) == 0);
808 BEAST_EXPECT(ownerCount(env, bob) == 0);
809 }
810
811 void
813 {
814 testcase("Sub-resolution amount");
815 using namespace test::jtx;
816
817 // IOU balances keep 16 significant digits. When alice claws back an
818 // amount much smaller than bob's balance, the balance changes by the
819 // rounded amount, which can be zero, less, or more than requested. The
820 // clawback invariant must accept that instead of failing the
821 // transaction under MPTokensV2 or logging a failure without it.
822 Account const alice{"alice"};
823 Account const bob{"bob"};
824
825 struct Case
826 {
827 std::uint64_t balanceMantissa;
828 int balanceExponent;
829 std::uint64_t clawMantissa;
830 int clawExponent;
831 std::uint64_t afterMantissa;
832 int afterExponent;
833 };
834 std::array const cases{
835 // 1e7 - 1e-10 rounds back to 1e7
836 Case{
837 .balanceMantissa = 10'000'000,
838 .balanceExponent = 0,
839 .clawMantissa = 1,
840 .clawExponent = -10,
841 .afterMantissa = 10'000'000,
842 .afterExponent = 0,
843 },
844 // 1234567890.123456 - 0.0000005 rounds back to the balance
845 Case{
846 .balanceMantissa = 1'234'567'890'123'456,
847 .balanceExponent = -6,
848 .clawMantissa = 5,
849 .clawExponent = -7,
850 .afterMantissa = 1'234'567'890'123'456,
851 .afterExponent = -6,
852 },
853 // 0.0000014 removes 0.000001
854 Case{
855 .balanceMantissa = 1'234'567'890'123'456,
856 .balanceExponent = -6,
857 .clawMantissa = 14,
858 .clawExponent = -7,
859 .afterMantissa = 1'234'567'890'123'455,
860 .afterExponent = -6,
861 },
862 // 0.0000016 removes 0.000002
863 Case{
864 .balanceMantissa = 1'234'567'890'123'456,
865 .balanceExponent = -6,
866 .clawMantissa = 16,
867 .clawExponent = -7,
868 .afterMantissa = 1'234'567'890'123'454,
869 .afterExponent = -6,
870 },
871 };
872
873 for (auto const feat : {features, features - featureMPTokensV2})
874 {
875 for (auto const& c : cases)
876 {
877 std::string logs;
878 {
879 Env env(*this, feat, std::make_unique<test::CaptureLogs>(&logs));
880
881 env.fund(XRP(1000), alice, bob);
882 env.close();
883
884 auto const usd = alice["USD"];
885
886 env(fset(alice, asfAllowTrustLineClawback));
887 env.close();
888
889 env.trust(usd(10'000'000'000), bob);
890 env(pay(
891 alice, bob, STAmount{usd.issue(), c.balanceMantissa, c.balanceExponent}));
892 env.close();
893
894 env(claw(
895 alice,
896 STAmount{
897 Issue{usd.currency, bob.id()}, c.clawMantissa, c.clawExponent}),
898 Ter(tesSUCCESS));
899 env.close();
900
901 BEAST_EXPECT(
902 env.balance(bob, usd).value() ==
903 STAmount(usd.issue(), c.afterMantissa, c.afterExponent));
904 }
905 // Without MPTokensV2 a failed invariant only logs and the
906 // transaction still succeeds, so check the logs instead.
907 if (!feat[featureMPTokensV2])
908 BEAST_EXPECT(!logs.contains("Invariant failed"));
909 }
910 }
911 }
912
913 void
915 {
916 testcase("Tickets");
917 using namespace test::jtx;
918
919 // Tests clawback with tickets
920 Env env(*this, features);
921 Account const alice{"alice"};
922 Account const bob{"bob"};
923
924 env.fund(XRP(1000), alice, bob);
925 env.close();
926
927 auto const usd = alice["USD"];
928
929 // alice sets asfAllowTrustLineClawback
930 env(fset(alice, asfAllowTrustLineClawback));
931 env.close();
932 env.require(Flags(alice, asfAllowTrustLineClawback));
933
934 // alice issues 100 USD to bob
935 env.trust(usd(1000), bob);
936 env(pay(alice, bob, usd(100)));
937 env.close();
938
939 env.require(Balance(bob, alice["USD"](100)));
940 env.require(Balance(alice, bob["USD"](-100)));
941
942 // alice creates 10 tickets
943 std::uint32_t ticketCnt = 10;
944 std::uint32_t aliceTicketSeq{env.seq(alice) + 1};
945 env(ticket::create(alice, ticketCnt));
946 env.close();
947 std::uint32_t const aliceSeq{env.seq(alice)};
948 BEAST_EXPECT(ticketCount(env, alice) == ticketCnt);
949 BEAST_EXPECT(ownerCount(env, alice) == ticketCnt);
950
951 while (ticketCnt > 0)
952 {
953 // alice claws back 5 USD using a ticket
954 env(claw(alice, bob["USD"](5)), ticket::Use(aliceTicketSeq++));
955 env.close();
956
957 ticketCnt--;
958 BEAST_EXPECT(ticketCount(env, alice) == ticketCnt);
959 BEAST_EXPECT(ownerCount(env, alice) == ticketCnt);
960 }
961
962 // alice clawed back 50 USD total, trustline has 50 USD remaining
963 env.require(Balance(bob, alice["USD"](50)));
964 env.require(Balance(alice, bob["USD"](-50)));
965
966 // Verify that the account sequence numbers did not advance.
967 BEAST_EXPECT(env.seq(alice) == aliceSeq);
968 }
969
970 void
972 {
974 testValidation(features);
975 testPermission(features);
976 testEnabled(features);
977 testMultiLine(features);
978 testBidirectionalLine(features);
979 testDeleteDefaultLine(features);
980 testFrozenLine(features);
982 testSubResolutionAmount(features);
983 testTickets(features);
984 }
985
986public:
987 void
988 run() override
989 {
990 using namespace test::jtx;
991 FeatureBitset const all{testableAmendments()};
992
993 testWithFeats(all - featureMPTokensV1);
994 testWithFeats(all);
995 }
996};
997
999} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
static std::string toString(T const &t)
void testDeleteDefaultLine(FeatureBitset features)
void testTickets(FeatureBitset features)
static std::uint32_t ticketCount(test::jtx::Env const &env, test::jtx::Account const &acct)
void testSubResolutionAmount(FeatureBitset features)
void testBidirectionalLine(FeatureBitset features)
void run() override
Runs the suite.
void testAmountExceedsAvailable(FeatureBitset features)
void testEnabled(FeatureBitset features)
void testFrozenLine(FeatureBitset features)
void testWithFeats(FeatureBitset features)
void testPermission(FeatureBitset features)
void testMultiLine(FeatureBitset features)
void testValidation(FeatureBitset features)
static bool getLineFreezeFlag(test::jtx::Env const &env, test::jtx::Account const &src, test::jtx::Account const &dst, Currency const &cur)
void testAllowTrustLineClawbackFlag(FeatureBitset features)
A currency issued by an account.
Definition Issue.h:18
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
A transaction testing environment.
Definition Env.h:161
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
T make_unique(T... args)
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:287
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_ACCOUNT
Definition TER.h:219
std::uint32_t ownerCount(SLE::ConstRef sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
@ temINVALID_FLAG
Definition TER.h:99
@ temBAD_AMOUNT
Definition TER.h:77
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecNO_LINE
Definition TER.h:312
@ tecOWNERS
Definition TER.h:309
@ tecNO_PERMISSION
Definition TER.h:316
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
@ tesSUCCESS
Definition TER.h:253
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52