xrpld
Loading...
Searching...
No Matches
Check_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/check.h>
8#include <test/jtx/fee.h>
9#include <test/jtx/flags.h>
10#include <test/jtx/invoice_id.h>
11#include <test/jtx/multisign.h>
12#include <test/jtx/offer.h>
13#include <test/jtx/owners.h>
14#include <test/jtx/pay.h>
15#include <test/jtx/quality.h>
16#include <test/jtx/rate.h>
17#include <test/jtx/regkey.h>
18#include <test/jtx/sendmax.h>
19#include <test/jtx/sig.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/ticket.h>
22#include <test/jtx/trust.h>
23#include <test/jtx/txflags.h>
24
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/core/ServiceRegistry.h>
29#include <xrpl/ledger/helpers/DirectoryHelpers.h>
30#include <xrpl/protocol/AccountID.h>
31#include <xrpl/protocol/Feature.h>
32#include <xrpl/protocol/Indexes.h>
33#include <xrpl/protocol/Issue.h>
34#include <xrpl/protocol/KeyType.h>
35#include <xrpl/protocol/LedgerFormats.h>
36#include <xrpl/protocol/SField.h>
37#include <xrpl/protocol/STAmount.h>
38#include <xrpl/protocol/SeqProxy.h>
39#include <xrpl/protocol/TER.h>
40#include <xrpl/protocol/TxFlags.h>
41#include <xrpl/protocol/UintTypes.h>
42#include <xrpl/protocol/XRPAmount.h>
43#include <xrpl/protocol/jss.h>
44
45#include <cstddef>
46#include <cstdint>
47#include <string>
48#include <vector>
49
50namespace xrpl {
51
53{
54 static UInt256
55 getCheckIndex(AccountID const& account, std::uint32_t uSequence)
56 {
57 return keylet::check(account, SeqProxy::rawSequence(uSequence)).key;
58 }
59
60 // Helper function that returns the Checks on an account.
63 {
65 forEachItem(*env.current(), account, [&result](SLE::ConstRef sle) {
66 if (sle && sle->getType() == ltCHECK)
67 result.push_back(sle);
68 });
69 return result;
70 }
71
72 // Helper function that verifies the expected DeliveredAmount is present.
73 //
74 // NOTE: the function _infers_ the transaction to operate on by calling
75 // env.tx(), which returns the result from the most recent transaction.
76 void
78 {
79 // Get the hash for the most recent transaction.
80 std::string const txHash{
81 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
82 };
83
84 // Verify DeliveredAmount and delivered_amount metadata are correct.
85 env.close();
86 json::Value const meta = env.rpc("tx", txHash)[jss::result][jss::meta];
87
88 // Expect there to be a DeliveredAmount field.
89 if (!BEAST_EXPECT(meta.isMember(sfDeliveredAmount.jsonName)))
90 return;
91
92 // DeliveredAmount and delivered_amount should both be present and
93 // equal amount.
94 BEAST_EXPECT(meta[sfDeliveredAmount.jsonName] == amount.getJson(JsonOptions::Values::None));
95 BEAST_EXPECT(meta[jss::delivered_amount] == amount.getJson(JsonOptions::Values::None));
96 }
97
98 void
100 {
101 testcase("Enabled");
102
103 using namespace test::jtx;
104 Account const alice{"alice"};
105 {
106 // If the Checks amendment is enabled all check-related
107 // facilities should be available.
108 Env env{*this, features};
109
110 env.fund(XRP(1000), alice);
111 env.close();
112
113 UInt256 const checkId1{getCheckIndex(env.master, env.seq(env.master))};
114 env(check::create(env.master, alice, XRP(100)));
115 env.close();
116
117 env(check::cash(alice, checkId1, XRP(100)));
118 env.close();
119
120 UInt256 const checkId2{getCheckIndex(env.master, env.seq(env.master))};
121 env(check::create(env.master, alice, XRP(100)));
122 env.close();
123
124 env(check::cancel(alice, checkId2));
125 env.close();
126 }
127 }
128
129 void
131 {
132 // Explore many of the valid ways to create a check.
133 testcase("Create valid");
134
135 using namespace test::jtx;
136
137 Account const gw{"gateway"};
138 Account const alice{"alice"};
139 Account const bob{"bob"};
140 IOU const usd{gw["USD"]};
141
142 Env env{*this, features};
143
144 STAmount const startBalance{XRP(1000).value()};
145 env.fund(startBalance, gw, alice, bob);
146 env.close();
147
148 // Note that no trust line has been set up for alice, but alice can
149 // still write a check for USD. You don't have to have the funds
150 // necessary to cover a check in order to write a check.
151 auto const writeTwoChecks = [&env, &usd, this](Account const& from, Account const& to) {
152 std::uint32_t const fromOwnerCount{ownerCount(env, from)};
153 std::uint32_t const toOwnerCount{ownerCount(env, to)};
154
155 std::size_t const fromCkCount{checksOnAccount(env, from).size()};
156 std::size_t const toCkCount{checksOnAccount(env, to).size()};
157
158 env(check::create(from, to, XRP(2000)));
159 env.close();
160
161 env(check::create(from, to, usd(50)));
162 env.close();
163
164 BEAST_EXPECT(checksOnAccount(env, from).size() == fromCkCount + 2);
165 BEAST_EXPECT(checksOnAccount(env, to).size() == toCkCount + 2);
166
167 env.require(Owners(from, fromOwnerCount + 2));
168 env.require(Owners(to, to == from ? fromOwnerCount + 2 : toOwnerCount));
169 };
170 // from to
171 writeTwoChecks(alice, bob);
172 writeTwoChecks(gw, alice);
173 writeTwoChecks(alice, gw);
174
175 // Now try adding the various optional fields. There's no
176 // expected interaction between these optional fields; other than
177 // the expiration, they are just plopped into the ledger. So I'm
178 // not looking at interactions.
179 using namespace std::chrono_literals;
180 std::size_t const aliceCount{checksOnAccount(env, alice).size()};
181 std::size_t const bobCount{checksOnAccount(env, bob).size()};
182 env(check::create(alice, bob, usd(50)), Expiration(env.now() + 1s));
183 env.close();
184
185 env(check::create(alice, bob, usd(50)), SourceTag(2));
186 env.close();
187 env(check::create(alice, bob, usd(50)), DestTag(3));
188 env.close();
189 env(check::create(alice, bob, usd(50)), InvoiceId(UInt256{4}));
190 env.close();
191 env(check::create(alice, bob, usd(50)),
192 Expiration(env.now() + 1s),
193 SourceTag(12),
194 DestTag(13),
195 InvoiceId(UInt256{4}));
196 env.close();
197
198 BEAST_EXPECT(checksOnAccount(env, alice).size() == aliceCount + 5);
199 BEAST_EXPECT(checksOnAccount(env, bob).size() == bobCount + 5);
200
201 // Use a regular key and also multisign to create a check.
202 Account const alie{"alie", KeyType::Ed25519};
203 env(regkey(alice, alie));
204 env.close();
205
206 Account const bogie{"bogie", KeyType::Secp256k1};
207 Account const demon{"demon", KeyType::Ed25519};
208 env(signers(alice, 2, {{bogie, 1}, {demon, 1}}), Sig(alie));
209 env.close();
210
211 // alice uses her regular key to create a check.
212 env(check::create(alice, bob, usd(50)), Sig(alie));
213 env.close();
214 BEAST_EXPECT(checksOnAccount(env, alice).size() == aliceCount + 6);
215 BEAST_EXPECT(checksOnAccount(env, bob).size() == bobCount + 6);
216
217 // alice uses multisigning to create a check.
218 XRPAmount const baseFeeDrops{env.current()->fees().base};
219 env(check::create(alice, bob, usd(50)), Msig(bogie, demon), Fee(3 * baseFeeDrops));
220 env.close();
221 BEAST_EXPECT(checksOnAccount(env, alice).size() == aliceCount + 7);
222 BEAST_EXPECT(checksOnAccount(env, bob).size() == bobCount + 7);
223 }
224
225 void
227 {
228 testcase("Create valid with disallow incoming");
229
230 using namespace test::jtx;
231
232 Account const gw{"gateway"};
233 Account const alice{"alice"};
234 Account const bob{"bob"};
235 IOU const usd{gw["USD"]};
236
237 Env env{*this, features};
238
239 STAmount const startBalance{XRP(1000).value()};
240 env.fund(startBalance, gw, alice, bob);
241 env.close();
242
243 /*
244 * Attempt to create two checks from `from` to `to` and
245 * require they both result in error/success code `expected`
246 */
247 auto const writeTwoChecksDI = [&env, &usd, this](
248 Account const& from, Account const& to, TER expected) {
249 std::uint32_t const fromOwnerCount{ownerCount(env, from)};
250 std::uint32_t const toOwnerCount{ownerCount(env, to)};
251
252 std::size_t const fromCkCount{checksOnAccount(env, from).size()};
253 std::size_t const toCkCount{checksOnAccount(env, to).size()};
254
255 env(check::create(from, to, XRP(2000)), Ter(expected));
256 env.close();
257
258 env(check::create(from, to, usd(50)), Ter(expected));
259 env.close();
260
261 if (isTesSuccess(expected))
262 {
263 BEAST_EXPECT(checksOnAccount(env, from).size() == fromCkCount + 2);
264 BEAST_EXPECT(checksOnAccount(env, to).size() == toCkCount + 2);
265
266 env.require(Owners(from, fromOwnerCount + 2));
267 env.require(Owners(to, to == from ? fromOwnerCount + 2 : toOwnerCount));
268 return;
269 }
270
271 BEAST_EXPECT(checksOnAccount(env, from).size() == fromCkCount);
272 BEAST_EXPECT(checksOnAccount(env, to).size() == toCkCount);
273
274 env.require(Owners(from, fromOwnerCount));
275 env.require(Owners(to, to == from ? fromOwnerCount : toOwnerCount));
276 };
277
278 // enable the DisallowIncoming flag on both bob and alice
279 env(fset(bob, asfDisallowIncomingCheck));
280 env(fset(alice, asfDisallowIncomingCheck));
281 env.close();
282
283 // both alice and bob can't receive checks
284 writeTwoChecksDI(alice, bob, tecNO_PERMISSION);
285 writeTwoChecksDI(gw, alice, tecNO_PERMISSION);
286
287 // remove the flag from alice but not from bob
288 env(fclear(alice, asfDisallowIncomingCheck));
289 env.close();
290
291 // now bob can send alice a cheque but not visa-versa
292 writeTwoChecksDI(bob, alice, tesSUCCESS);
293 writeTwoChecksDI(alice, bob, tecNO_PERMISSION);
294
295 // remove bob's flag too
296 env(fclear(bob, asfDisallowIncomingCheck));
297 env.close();
298
299 // now they can send checks freely
300 writeTwoChecksDI(bob, alice, tesSUCCESS);
301 writeTwoChecksDI(alice, bob, tesSUCCESS);
302 }
303
304 void
306 {
307 // Explore many of the invalid ways to create a check.
308 testcase("Create invalid");
309
310 using namespace test::jtx;
311
312 Account const gw1{"gateway1"};
313 Account const gwF{"gatewayFrozen"};
314 Account const alice{"alice"};
315 Account const bob{"bob"};
316 IOU const usd{gw1["USD"]};
317
318 Env env{*this, features};
319
320 STAmount const startBalance{XRP(1000).value()};
321 env.fund(startBalance, gw1, gwF, alice, bob);
322 env.close();
323
324 // Bad fee.
325 env(check::create(alice, bob, usd(50)), Fee(drops(-10)), Ter(temBAD_FEE));
326 env.close();
327
328 // Bad flags.
329 env(check::create(alice, bob, usd(50)), Txflags(tfImmediateOrCancel), Ter(temINVALID_FLAG));
330 env.close();
331
332 // Check to self.
333 env(check::create(alice, alice, XRP(10)), Ter(temREDUNDANT));
334 env.close();
335
336 // Bad amount.
337 env(check::create(alice, bob, drops(-1)), Ter(temBAD_AMOUNT));
338 env.close();
339
340 env(check::create(alice, bob, drops(0)), Ter(temBAD_AMOUNT));
341 env.close();
342
343 env(check::create(alice, bob, drops(1)));
344 env.close();
345
346 env(check::create(alice, bob, usd(-1)), Ter(temBAD_AMOUNT));
347 env.close();
348
349 env(check::create(alice, bob, usd(0)), Ter(temBAD_AMOUNT));
350 env.close();
351
352 env(check::create(alice, bob, usd(1)));
353 env.close();
354 {
355 IOU const bad{gw1, badCurrency()};
356 env(check::create(alice, bob, bad(2)), Ter(temBAD_CURRENCY));
357 env.close();
358 }
359
360 // Bad expiration.
361 env(check::create(alice, bob, usd(50)),
362 Expiration(NetClock::time_point{}),
363 Ter(temBAD_EXPIRATION));
364 env.close();
365
366 // Destination does not exist.
367 Account const bogie{"bogie"};
368 env(check::create(alice, bogie, usd(50)), Ter(tecNO_DST));
369 env.close();
370
371 // Require destination tag.
372 env(fset(bob, asfRequireDest));
373 env.close();
374
375 env(check::create(alice, bob, usd(50)), Ter(tecDST_TAG_NEEDED));
376 env.close();
377
378 env(check::create(alice, bob, usd(50)), DestTag(11));
379 env.close();
380
381 env(fclear(bob, asfRequireDest));
382 env.close();
383 {
384 // Globally frozen asset.
385 IOU const usf{gwF["USF"]};
386 env(fset(gwF, asfGlobalFreeze));
387 env.close();
388
389 env(check::create(alice, bob, usf(50)), Ter(tecFROZEN));
390 env.close();
391
392 env(check::create(gwF, bob, usf(50)), Ter(tecFROZEN));
393 env.close();
394
395 env(fclear(gwF, asfGlobalFreeze));
396 env.close();
397
398 env(check::create(alice, bob, usf(50)));
399 env.close();
400
401 env(check::create(gwF, bob, usf(50)));
402 env.close();
403 }
404 {
405 // Frozen trust line. Check creation should be similar to payment
406 // behavior in the face of frozen trust lines.
407 env.trust(usd(1000), alice);
408 env.trust(usd(1000), bob);
409 env.close();
410 env(pay(gw1, alice, usd(25)));
411 env(pay(gw1, bob, usd(25)));
412 env.close();
413
414 // Setting trustline freeze in one direction prevents alice from
415 // creating a check for USD. But bob and gw1 should still be able
416 // to create a check for USD to alice.
417 env(trust(gw1, alice["USD"](0), tfSetFreeze));
418 env.close();
419 env(check::create(alice, bob, usd(50)), Ter(tecFROZEN));
420 env.close();
421 env(pay(alice, bob, usd(1)), Ter(tecPATH_DRY));
422 env.close();
423 env(check::create(bob, alice, usd(50)));
424 env.close();
425 env(pay(bob, alice, usd(1)));
426 env.close();
427 env(check::create(gw1, alice, usd(50)));
428 env.close();
429 env(pay(gw1, alice, usd(1)));
430 env.close();
431
432 // Clear that freeze. Now check creation works.
433 env(trust(gw1, alice["USD"](0), tfClearFreeze));
434 env.close();
435 env(check::create(alice, bob, usd(50)));
436 env.close();
437 env(check::create(bob, alice, usd(50)));
438 env.close();
439 env(check::create(gw1, alice, usd(50)));
440 env.close();
441
442 // Freezing in the other direction does not effect alice's USD
443 // check creation, but prevents bob and gw1 from writing a check
444 // for USD to alice.
445 env(trust(alice, usd(0), tfSetFreeze));
446 env.close();
447 env(check::create(alice, bob, usd(50)));
448 env.close();
449 env(pay(alice, bob, usd(1)));
450 env.close();
451 env(check::create(bob, alice, usd(50)), Ter(tecFROZEN));
452 env.close();
453 env(pay(bob, alice, usd(1)), Ter(tecPATH_DRY));
454 env.close();
455 env(check::create(gw1, alice, usd(50)), Ter(tecFROZEN));
456 env.close();
457 env(pay(gw1, alice, usd(1)), Ter(tecPATH_DRY));
458 env.close();
459
460 // Clear that freeze.
461 env(trust(alice, usd(0), tfClearFreeze));
462 env.close();
463 }
464
465 // Expired expiration.
466 env(check::create(alice, bob, usd(50)), Expiration(env.now()), Ter(tecEXPIRED));
467 env.close();
468
469 using namespace std::chrono_literals;
470 env(check::create(alice, bob, usd(50)), Expiration(env.now() + 1s));
471 env.close();
472
473 // Insufficient reserve.
474 Account const cheri{"cheri"};
475 env.fund(env.current()->fees().accountReserve(1, 1) - drops(1), cheri);
476 env.close();
477
478 env(check::create(cheri, bob, usd(50)),
479 Fee(drops(env.current()->fees().base)),
481 env.close();
482
483 env(pay(bob, cheri, drops(env.current()->fees().base + 1)));
484 env.close();
485
486 env(check::create(cheri, bob, usd(50)));
487 env.close();
488 }
489
490 void
492 {
493 // Explore many of the valid ways to cash a check for XRP.
494 testcase("Cash XRP");
495
496 using namespace test::jtx;
497
498 Account const alice{"alice"};
499 Account const bob{"bob"};
500
501 Env env{*this, features};
502
503 XRPAmount const baseFeeDrops{env.current()->fees().base};
504 STAmount const startBalance{XRP(300).value()};
505 env.fund(startBalance, alice, bob);
506 env.close();
507 {
508 // Basic XRP check.
509 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
510 env(check::create(alice, bob, XRP(10)));
511 env.close();
512 env.require(Balance(alice, startBalance - drops(baseFeeDrops)));
513 env.require(Balance(bob, startBalance));
514 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
515 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
516 BEAST_EXPECT(ownerCount(env, alice) == 1);
517 BEAST_EXPECT(ownerCount(env, bob) == 0);
518
519 env(check::cash(bob, chkId, XRP(10)));
520 env.close();
521 env.require(Balance(alice, startBalance - XRP(10) - drops(baseFeeDrops)));
522 env.require(Balance(bob, startBalance + XRP(10) - drops(baseFeeDrops)));
523 BEAST_EXPECT(checksOnAccount(env, alice).empty());
524 BEAST_EXPECT(checksOnAccount(env, bob).empty());
525 BEAST_EXPECT(ownerCount(env, alice) == 0);
526 BEAST_EXPECT(ownerCount(env, bob) == 0);
527
528 // Make alice's and bob's balances easy to think about.
529 env(pay(env.master, alice, XRP(10) + drops(baseFeeDrops)));
530 env(pay(bob, env.master, XRP(10) - drops(baseFeeDrops * 2)));
531 env.close();
532 env.require(Balance(alice, startBalance));
533 env.require(Balance(bob, startBalance));
534 }
535 {
536 // Write a check that chews into alice's reserve.
537 STAmount const reserve{env.current()->fees().reserve};
538 STAmount const checkAmount{startBalance - reserve - drops(baseFeeDrops)};
539 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
540 env(check::create(alice, bob, checkAmount));
541 env.close();
542
543 // bob tries to cash for more than the check amount.
544 env(check::cash(bob, chkId, checkAmount + drops(1)), Ter(tecPATH_PARTIAL));
545 env.close();
546 env(check::cash(bob, chkId, check::DeliverMin(checkAmount + drops(1))),
547 Ter(tecPATH_PARTIAL));
548 env.close();
549
550 // bob cashes exactly the check amount. This is successful
551 // because one unit of alice's reserve is released when the
552 // check is consumed.
553 env(check::cash(bob, chkId, check::DeliverMin(checkAmount)));
554 verifyDeliveredAmount(env, drops(checkAmount.mantissa()));
555 env.require(Balance(alice, reserve));
556 env.require(Balance(bob, startBalance + checkAmount - drops(baseFeeDrops * 3)));
557 BEAST_EXPECT(checksOnAccount(env, alice).empty());
558 BEAST_EXPECT(checksOnAccount(env, bob).empty());
559 BEAST_EXPECT(ownerCount(env, alice) == 0);
560 BEAST_EXPECT(ownerCount(env, bob) == 0);
561
562 // Make alice's and bob's balances easy to think about.
563 env(pay(env.master, alice, checkAmount + drops(baseFeeDrops)));
564 env(pay(bob, env.master, checkAmount - drops(baseFeeDrops * 4)));
565 env.close();
566 env.require(Balance(alice, startBalance));
567 env.require(Balance(bob, startBalance));
568 }
569 {
570 // Write a check that goes one drop past what alice can pay.
571 STAmount const reserve{env.current()->fees().reserve};
572 STAmount const checkAmount{startBalance - reserve - drops(baseFeeDrops - 1)};
573 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
574 env(check::create(alice, bob, checkAmount));
575 env.close();
576
577 // bob tries to cash for exactly the check amount. Fails because
578 // alice is one drop shy of funding the check.
579 env(check::cash(bob, chkId, checkAmount), Ter(tecPATH_PARTIAL));
580 env.close();
581
582 // bob decides to get what he can from the bounced check.
583 env(check::cash(bob, chkId, check::DeliverMin(drops(1))));
584 verifyDeliveredAmount(env, drops(checkAmount.mantissa() - 1));
585 env.require(Balance(alice, reserve));
586 env.require(Balance(bob, startBalance + checkAmount - drops(baseFeeDrops * 2 + 1)));
587 BEAST_EXPECT(checksOnAccount(env, alice).empty());
588 BEAST_EXPECT(checksOnAccount(env, bob).empty());
589 BEAST_EXPECT(ownerCount(env, alice) == 0);
590 BEAST_EXPECT(ownerCount(env, bob) == 0);
591
592 // Make alice's and bob's balances easy to think about.
593 env(pay(env.master, alice, checkAmount + drops(baseFeeDrops - 1)));
594 env(pay(bob, env.master, checkAmount - drops(baseFeeDrops * 3 + 1)));
595 env.close();
596 env.require(Balance(alice, startBalance));
597 env.require(Balance(bob, startBalance));
598 }
599 }
600
601 void
603 {
604 // Explore many of the valid ways to cash a check for an IOU.
605 testcase("Cash IOU");
606
607 using namespace test::jtx;
608
609 Account const gw{"gateway"};
610 Account const alice{"alice"};
611 Account const bob{"bob"};
612 IOU const usd{gw["USD"]};
613 {
614 // Simple IOU check cashed with Amount (with failures).
615 Env env{*this, features};
616
617 env.fund(XRP(1000), gw, alice, bob);
618 env.close();
619
620 // alice writes the check before she gets the funds.
621 UInt256 const chkId1{getCheckIndex(alice, env.seq(alice))};
622 env(check::create(alice, bob, usd(10)));
623 env.close();
624
625 // bob attempts to cash the check. Should fail.
626 env(check::cash(bob, chkId1, usd(10)), Ter(tecPATH_PARTIAL));
627 env.close();
628
629 // alice gets almost enough funds. bob tries and fails again.
630 env(trust(alice, usd(20)));
631 env.close();
632 env(pay(gw, alice, usd(9.5)));
633 env.close();
634 env(check::cash(bob, chkId1, usd(10)), Ter(tecPATH_PARTIAL));
635 env.close();
636
637 // alice gets the last of the necessary funds. bob tries again
638 // and fails because he hasn't got a trust line for USD.
639 env(pay(gw, alice, usd(0.5)));
640 env.close();
641
642 // bob sets up the trust line, but not at a high enough limit.
643 env(trust(bob, usd(9.5)));
644 env.close();
645
646 // bob sets the trust line limit high enough but asks for more
647 // than the check's SendMax.
648 env(trust(bob, usd(10.5)));
649 env.close();
650 env(check::cash(bob, chkId1, usd(10.5)), Ter(tecPATH_PARTIAL));
651 env.close();
652
653 // bob asks for exactly the check amount and the check clears.
654 env(check::cash(bob, chkId1, usd(10)));
655 env.close();
656 env.require(Balance(alice, usd(0)));
657 env.require(Balance(bob, usd(10)));
658
659 BEAST_EXPECT(checksOnAccount(env, alice).empty());
660 BEAST_EXPECT(checksOnAccount(env, bob).empty());
661 BEAST_EXPECT(ownerCount(env, alice) == 1);
662 BEAST_EXPECT(ownerCount(env, bob) == 1);
663
664 // bob tries to cash the same check again, which fails.
665 env(check::cash(bob, chkId1, usd(10)), Ter(tecNO_ENTRY));
666 env.close();
667
668 // bob pays alice USD(7) so he can try another case.
669 env(pay(bob, alice, usd(7)));
670 env.close();
671
672 UInt256 const chkId2{getCheckIndex(alice, env.seq(alice))};
673 env(check::create(alice, bob, usd(7)));
674 env.close();
675 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
676 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
677
678 // bob cashes the check for less than the face amount. That works,
679 // consumes the check, and bob receives as much as he asked for.
680 env(check::cash(bob, chkId2, usd(5)));
681 env.close();
682 env.require(Balance(alice, usd(2)));
683 env.require(Balance(bob, usd(8)));
684 BEAST_EXPECT(checksOnAccount(env, alice).empty());
685 BEAST_EXPECT(checksOnAccount(env, bob).empty());
686 BEAST_EXPECT(ownerCount(env, alice) == 1);
687 BEAST_EXPECT(ownerCount(env, bob) == 1);
688
689 // alice writes two checks for USD(2), although she only has USD(2).
690 UInt256 const chkId3{getCheckIndex(alice, env.seq(alice))};
691 env(check::create(alice, bob, usd(2)));
692 env.close();
693 UInt256 const chkId4{getCheckIndex(alice, env.seq(alice))};
694 env(check::create(alice, bob, usd(2)));
695 env.close();
696 BEAST_EXPECT(checksOnAccount(env, alice).size() == 2);
697 BEAST_EXPECT(checksOnAccount(env, bob).size() == 2);
698
699 // bob cashes the second check for the face amount.
700 env(check::cash(bob, chkId4, usd(2)));
701 env.close();
702 env.require(Balance(alice, usd(0)));
703 env.require(Balance(bob, usd(10)));
704 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
705 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
706 BEAST_EXPECT(ownerCount(env, alice) == 2);
707 BEAST_EXPECT(ownerCount(env, bob) == 1);
708
709 // bob is not allowed to cash the last check for USD(0), he must
710 // use check::cancel instead.
711 env(check::cash(bob, chkId3, usd(0)), Ter(temBAD_AMOUNT));
712 env.close();
713 env.require(Balance(alice, usd(0)));
714 env.require(Balance(bob, usd(10)));
715 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
716 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
717 BEAST_EXPECT(ownerCount(env, alice) == 2);
718 BEAST_EXPECT(ownerCount(env, bob) == 1);
719
720 // Automatic trust lines are enabled. But one aspect of
721 // automatic trust lines is that they allow the account
722 // cashing a check to exceed their trust line limit. Show
723 // that at work.
724 //
725 // bob's trust line limit is currently USD(10.5). Show that
726 // a payment to bob cannot exceed that trust line, but cashing
727 // a check can.
728
729 // Payment of 20 USD fails.
730 env(pay(gw, bob, usd(20)), Ter(tecPATH_PARTIAL));
731 env.close();
732
733 UInt256 const chkId20{getCheckIndex(gw, env.seq(gw))};
734 env(check::create(gw, bob, usd(20)));
735 env.close();
736
737 // However cashing a check for 20 USD succeeds.
738 env(check::cash(bob, chkId20, usd(20)));
739 env.close();
740 env.require(Balance(bob, usd(30)));
741
742 // Clean up this most recent experiment so the rest of the
743 // tests work.
744 env(pay(bob, gw, usd(20)));
745
746 // ... so bob cancels alice's remaining check.
747 env(check::cancel(bob, chkId3));
748 env.close();
749 env.require(Balance(alice, usd(0)));
750 env.require(Balance(bob, usd(10)));
751 BEAST_EXPECT(checksOnAccount(env, alice).empty());
752 BEAST_EXPECT(checksOnAccount(env, bob).empty());
753 BEAST_EXPECT(ownerCount(env, alice) == 1);
754 BEAST_EXPECT(ownerCount(env, bob) == 1);
755 }
756 {
757 // Simple IOU check cashed with DeliverMin (with failures).
758 Env env{*this, features};
759
760 env.fund(XRP(1000), gw, alice, bob);
761 env.close();
762
763 env(trust(alice, usd(20)));
764 env(trust(bob, usd(20)));
765 env.close();
766 env(pay(gw, alice, usd(8)));
767 env.close();
768
769 // alice creates several checks ahead of time.
770 UInt256 const chkId9{getCheckIndex(alice, env.seq(alice))};
771 env(check::create(alice, bob, usd(9)));
772 env.close();
773 UInt256 const chkId8{getCheckIndex(alice, env.seq(alice))};
774 env(check::create(alice, bob, usd(8)));
775 env.close();
776 UInt256 const chkId7{getCheckIndex(alice, env.seq(alice))};
777 env(check::create(alice, bob, usd(7)));
778 env.close();
779 UInt256 const chkId6{getCheckIndex(alice, env.seq(alice))};
780 env(check::create(alice, bob, usd(6)));
781 env.close();
782
783 // bob attempts to cash a check for the amount on the check.
784 // Should fail, since alice doesn't have the funds.
785 env(check::cash(bob, chkId9, check::DeliverMin(usd(9))), Ter(tecPATH_PARTIAL));
786 env.close();
787
788 // bob sets a DeliverMin of 7 and gets all that alice has.
789 env(check::cash(bob, chkId9, check::DeliverMin(usd(7))));
790 verifyDeliveredAmount(env, usd(8));
791 env.require(Balance(alice, usd(0)));
792 env.require(Balance(bob, usd(8)));
793 BEAST_EXPECT(checksOnAccount(env, alice).size() == 3);
794 BEAST_EXPECT(checksOnAccount(env, bob).size() == 3);
795 BEAST_EXPECT(ownerCount(env, alice) == 4);
796 BEAST_EXPECT(ownerCount(env, bob) == 1);
797
798 // bob pays alice USD(7) so he can use another check.
799 env(pay(bob, alice, usd(7)));
800 env.close();
801
802 // Using DeliverMin for the SendMax value of the check (and no
803 // transfer fees) should work just like setting Amount.
804 env(check::cash(bob, chkId7, check::DeliverMin(usd(7))));
805 verifyDeliveredAmount(env, usd(7));
806 env.require(Balance(alice, usd(0)));
807 env.require(Balance(bob, usd(8)));
808 BEAST_EXPECT(checksOnAccount(env, alice).size() == 2);
809 BEAST_EXPECT(checksOnAccount(env, bob).size() == 2);
810 BEAST_EXPECT(ownerCount(env, alice) == 3);
811 BEAST_EXPECT(ownerCount(env, bob) == 1);
812
813 // bob pays alice USD(8) so he can use the last two checks.
814 env(pay(bob, alice, usd(8)));
815 env.close();
816
817 // alice has USD(8). If bob uses the check for USD(6) and uses a
818 // DeliverMin of 4, he should get the SendMax value of the check.
819 env(check::cash(bob, chkId6, check::DeliverMin(usd(4))));
820 verifyDeliveredAmount(env, usd(6));
821 env.require(Balance(alice, usd(2)));
822 env.require(Balance(bob, usd(6)));
823 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
824 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
825 BEAST_EXPECT(ownerCount(env, alice) == 2);
826 BEAST_EXPECT(ownerCount(env, bob) == 1);
827
828 // bob cashes the last remaining check setting a DeliverMin.
829 // of exactly alice's remaining USD.
830 env(check::cash(bob, chkId8, check::DeliverMin(usd(2))));
831 verifyDeliveredAmount(env, usd(2));
832 env.require(Balance(alice, usd(0)));
833 env.require(Balance(bob, usd(8)));
834 BEAST_EXPECT(checksOnAccount(env, alice).empty());
835 BEAST_EXPECT(checksOnAccount(env, bob).empty());
836 BEAST_EXPECT(ownerCount(env, alice) == 1);
837 BEAST_EXPECT(ownerCount(env, bob) == 1);
838 }
839 {
840 // Examine the effects of the asfRequireAuth flag.
841 Env env(*this, features);
842
843 env.fund(XRP(1000), gw, alice, bob);
844 env(fset(gw, asfRequireAuth));
845 env.close();
846 env(trust(gw, alice["USD"](100)), Txflags(tfSetfAuth));
847 env(trust(alice, usd(20)));
848 env.close();
849 env(pay(gw, alice, usd(8)));
850 env.close();
851
852 // alice writes a check to bob for USD. bob can't cash it
853 // because he is not authorized to hold gw["USD"].
854 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
855 env(check::create(alice, bob, usd(7)));
856 env.close();
857
858 env(check::cash(bob, chkId, usd(7)), Ter(tecNO_AUTH));
859 env.close();
860
861 // Now give bob a trustline for USD. bob still can't cash the
862 // check because he is not authorized.
863 env(trust(bob, usd(5)));
864 env.close();
865
866 env(check::cash(bob, chkId, usd(7)), Ter(tecNO_AUTH));
867 env.close();
868
869 // bob gets authorization to hold gw["USD"].
870 env(trust(gw, bob["USD"](1)), Txflags(tfSetfAuth));
871 env.close();
872
873 // Two possible outcomes here depending on whether cashing a
874 // check can build a trust line:
875 // o If it can't build a trust line, then since bob set his
876 // limit low, he cashes the check with a DeliverMin and hits
877 // his trust limit.
878 // o If it can build a trust line, then the check is allowed to
879 // exceed the trust limit and bob gets the full transfer.
880 env(check::cash(bob, chkId, check::DeliverMin(usd(4))));
881 STAmount const bobGot = usd(7);
882 verifyDeliveredAmount(env, bobGot);
883 env.require(Balance(alice, usd(8) - bobGot));
884 env.require(Balance(bob, bobGot));
885
886 BEAST_EXPECT(checksOnAccount(env, alice).empty());
887 BEAST_EXPECT(checksOnAccount(env, bob).empty());
888 BEAST_EXPECT(ownerCount(env, alice) == 1);
889 BEAST_EXPECT(ownerCount(env, bob) == 1);
890 }
891
892 // Use a regular key and also multisign to cash a check.
893 {
894 Env env{*this, features};
895 env.fund(XRP(1000), gw, alice, bob);
896 env.close();
897
898 // alice creates her checks ahead of time.
899 UInt256 const chkId1{getCheckIndex(alice, env.seq(alice))};
900 env(check::create(alice, bob, usd(1)));
901 env.close();
902
903 UInt256 const chkId2{getCheckIndex(alice, env.seq(alice))};
904 env(check::create(alice, bob, usd(2)));
905 env.close();
906
907 env(trust(alice, usd(20)));
908 env(trust(bob, usd(20)));
909 env.close();
910 env(pay(gw, alice, usd(8)));
911 env.close();
912
913 // Give bob a regular key and signers
914 Account const bobby{"bobby", KeyType::Secp256k1};
915 env(regkey(bob, bobby));
916 env.close();
917
918 Account const bogie{"bogie", KeyType::Secp256k1};
919 Account const demon{"demon", KeyType::Ed25519};
920 env(signers(bob, 2, {{bogie, 1}, {demon, 1}}), Sig(bobby));
921 env.close();
922
923 BEAST_EXPECT(ownerCount(env, bob) == 2);
924
925 // bob uses his regular key to cash a check.
926 env(check::cash(bob, chkId1, (usd(1))), Sig(bobby));
927 env.close();
928 env.require(Balance(alice, usd(7)));
929 env.require(Balance(bob, usd(1)));
930 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
931 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
932 BEAST_EXPECT(ownerCount(env, alice) == 2);
933 BEAST_EXPECT(ownerCount(env, bob) == 2);
934
935 // bob uses multisigning to cash a check.
936 XRPAmount const baseFeeDrops{env.current()->fees().base};
937 env(check::cash(bob, chkId2, (usd(2))), Msig(bogie, demon), Fee(3 * baseFeeDrops));
938 env.close();
939 env.require(Balance(alice, usd(5)));
940 env.require(Balance(bob, usd(3)));
941 BEAST_EXPECT(checksOnAccount(env, alice).empty());
942 BEAST_EXPECT(checksOnAccount(env, bob).empty());
943 BEAST_EXPECT(ownerCount(env, alice) == 1);
944 BEAST_EXPECT(ownerCount(env, bob) == 2);
945 }
946 }
947
948 void
950 {
951 // Look at behavior when the issuer charges a transfer fee.
952 testcase("Cash with transfer fee");
953
954 using namespace test::jtx;
955
956 Account const gw{"gateway"};
957 Account const alice{"alice"};
958 Account const bob{"bob"};
959 IOU const usd{gw["USD"]};
960
961 Env env{*this, features};
962
963 env.fund(XRP(1000), gw, alice, bob);
964 env.close();
965
966 env(trust(alice, usd(1000)));
967 env(trust(bob, usd(1000)));
968 env.close();
969 env(pay(gw, alice, usd(1000)));
970 env.close();
971
972 // Set gw's transfer rate and see the consequences when cashing a check.
973 env(rate(gw, 1.25));
974 env.close();
975
976 // alice writes a check with a SendMax of USD(125). The most bob
977 // can get is USD(100) because of the transfer rate.
978 UInt256 const chkId125{getCheckIndex(alice, env.seq(alice))};
979 env(check::create(alice, bob, usd(125)));
980 env.close();
981
982 // alice writes another check that won't get cashed until the transfer
983 // rate changes so we can see the rate applies when the check is
984 // cashed, not when it is created.
985 UInt256 const chkId120{getCheckIndex(alice, env.seq(alice))};
986 env(check::create(alice, bob, usd(120)));
987 env.close();
988
989 // bob attempts to cash the check for face value. Should fail.
990 env(check::cash(bob, chkId125, usd(125)), Ter(tecPATH_PARTIAL));
991 env.close();
992 env(check::cash(bob, chkId125, check::DeliverMin(usd(101))), Ter(tecPATH_PARTIAL));
993 env.close();
994
995 // bob decides that he'll accept anything USD(75) or up.
996 // He gets USD(100).
997 env(check::cash(bob, chkId125, check::DeliverMin(usd(75))));
998 verifyDeliveredAmount(env, usd(100));
999 env.require(Balance(alice, usd(1000 - 125)));
1000 env.require(Balance(bob, usd(0 + 100)));
1001 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
1002 BEAST_EXPECT(checksOnAccount(env, bob).size() == 1);
1003
1004 // Adjust gw's rate...
1005 env(rate(gw, 1.2));
1006 env.close();
1007
1008 // bob cashes the second check for less than the face value. The new
1009 // rate applies to the actual value transferred.
1010 env(check::cash(bob, chkId120, usd(50)));
1011 env.close();
1012 env.require(Balance(alice, usd(1000 - 125 - 60)));
1013 env.require(Balance(bob, usd(0 + 100 + 50)));
1014 BEAST_EXPECT(checksOnAccount(env, alice).empty());
1015 BEAST_EXPECT(checksOnAccount(env, bob).empty());
1016 }
1017
1018 void
1020 {
1021 // Look at the eight possible cases for Quality In/Out.
1022 testcase("Cash quality");
1023
1024 using namespace test::jtx;
1025
1026 Account const gw{"gateway"};
1027 Account const alice{"alice"};
1028 Account const bob{"bob"};
1029 IOU const usd{gw["USD"]};
1030
1031 Env env{*this, features};
1032
1033 env.fund(XRP(1000), gw, alice, bob);
1034 env.close();
1035
1036 env(trust(alice, usd(1000)));
1037 env(trust(bob, usd(1000)));
1038 env.close();
1039 env(pay(gw, alice, usd(1000)));
1040 env.close();
1041
1042 //
1043 // Quality effects on transfers between two non-issuers.
1044 //
1045
1046 // Provide lambdas that return a qualityInPercent and qualityOutPercent.
1047 auto const qIn = [](double percent) { return QualityInPercent(percent); };
1048 auto const qOut = [](double percent) { return QualityOutPercent(percent); };
1049
1050 // There are two test lambdas: one for a Payment and one for a Check.
1051 // This shows whether a Payment and a Check behave the same.
1052 auto const testNonIssuerQPay = [&env, &alice, &bob, &usd](
1053 Account const& truster,
1054 IOU const& iou,
1055 auto const& inOrOut,
1056 double pct,
1057 double amount) {
1058 // Capture bob's and alice's balances so we can test at the end.
1059 STAmount const aliceStart{env.balance(alice, usd).value()};
1060 STAmount const bobStart{env.balance(bob, usd).value()};
1061
1062 // Set the modified quality.
1063 env(trust(truster, iou(1000)), inOrOut(pct));
1064 env.close();
1065
1066 env(pay(alice, bob, usd(amount)), Sendmax(usd(10)));
1067 env.close();
1068 env.require(Balance(alice, aliceStart - usd(10)));
1069 env.require(Balance(bob, bobStart + usd(10)));
1070
1071 // Return the quality to the unmodified state so it doesn't
1072 // interfere with upcoming tests.
1073 env(trust(truster, iou(1000)), inOrOut(0));
1074 env.close();
1075 };
1076
1077 auto const testNonIssuerQCheck = [&env, &alice, &bob, &usd](
1078 Account const& truster,
1079 IOU const& iou,
1080 auto const& inOrOut,
1081 double pct,
1082 double amount) {
1083 // Capture bob's and alice's balances so we can test at the end.
1084 STAmount const aliceStart{env.balance(alice, usd).value()};
1085 STAmount const bobStart{env.balance(bob, usd).value()};
1086
1087 // Set the modified quality.
1088 env(trust(truster, iou(1000)), inOrOut(pct));
1089 env.close();
1090
1091 UInt256 const chkId = getCheckIndex(alice, env.seq(alice));
1092 env(check::create(alice, bob, usd(10)));
1093 env.close();
1094
1095 env(check::cash(bob, chkId, usd(amount)));
1096 env.close();
1097 env.require(Balance(alice, aliceStart - usd(10)));
1098 env.require(Balance(bob, bobStart + usd(10)));
1099
1100 // Return the quality to the unmodified state so it doesn't
1101 // interfere with upcoming tests.
1102 env(trust(truster, iou(1000)), inOrOut(0));
1103 env.close();
1104 };
1105
1106 // pct amount
1107 testNonIssuerQPay(alice, gw["USD"], qIn, 50, 10);
1108 testNonIssuerQCheck(alice, gw["USD"], qIn, 50, 10);
1109
1110 // This is the only case where the Quality affects the outcome.
1111 testNonIssuerQPay(bob, gw["USD"], qIn, 50, 5);
1112 testNonIssuerQCheck(bob, gw["USD"], qIn, 50, 5);
1113
1114 testNonIssuerQPay(gw, alice["USD"], qIn, 50, 10);
1115 testNonIssuerQCheck(gw, alice["USD"], qIn, 50, 10);
1116
1117 testNonIssuerQPay(gw, bob["USD"], qIn, 50, 10);
1118 testNonIssuerQCheck(gw, bob["USD"], qIn, 50, 10);
1119
1120 testNonIssuerQPay(alice, gw["USD"], qOut, 200, 10);
1121 testNonIssuerQCheck(alice, gw["USD"], qOut, 200, 10);
1122
1123 testNonIssuerQPay(bob, gw["USD"], qOut, 200, 10);
1124 testNonIssuerQCheck(bob, gw["USD"], qOut, 200, 10);
1125
1126 testNonIssuerQPay(gw, alice["USD"], qOut, 200, 10);
1127 testNonIssuerQCheck(gw, alice["USD"], qOut, 200, 10);
1128
1129 testNonIssuerQPay(gw, bob["USD"], qOut, 200, 10);
1130 testNonIssuerQCheck(gw, bob["USD"], qOut, 200, 10);
1131
1132 //
1133 // Quality effects on transfers between an issuer and a non-issuer.
1134 //
1135
1136 // There are two test lambdas for the same reason as before.
1137 auto const testIssuerQPay = [&env, &gw, &alice, &usd](
1138 Account const& truster,
1139 IOU const& iou,
1140 auto const& inOrOut,
1141 double pct,
1142 double amt1,
1143 double max1,
1144 double amt2,
1145 double max2) {
1146 // Capture alice's balance so we can test at the end. It doesn't
1147 // make any sense to look at the balance of a gateway.
1148 STAmount const aliceStart{env.balance(alice, usd).value()};
1149
1150 // Set the modified quality.
1151 env(trust(truster, iou(1000)), inOrOut(pct));
1152 env.close();
1153
1154 // alice pays gw.
1155 env(pay(alice, gw, usd(amt1)), Sendmax(usd(max1)));
1156 env.close();
1157 env.require(Balance(alice, aliceStart - usd(10)));
1158
1159 // gw pays alice.
1160 env(pay(gw, alice, usd(amt2)), Sendmax(usd(max2)));
1161 env.close();
1162 env.require(Balance(alice, aliceStart));
1163
1164 // Return the quality to the unmodified state so it doesn't
1165 // interfere with upcoming tests.
1166 env(trust(truster, iou(1000)), inOrOut(0));
1167 env.close();
1168 };
1169
1170 auto const testIssuerQCheck = [&env, &gw, &alice, &usd](
1171 Account const& truster,
1172 IOU const& iou,
1173 auto const& inOrOut,
1174 double pct,
1175 double amt1,
1176 double max1,
1177 double amt2,
1178 double max2) {
1179 // Capture alice's balance so we can test at the end. It doesn't
1180 // make any sense to look at the balance of the issuer.
1181 STAmount const aliceStart{env.balance(alice, usd).value()};
1182
1183 // Set the modified quality.
1184 env(trust(truster, iou(1000)), inOrOut(pct));
1185 env.close();
1186
1187 // alice writes check to gw. gw cashes.
1188 UInt256 const chkAliceId{getCheckIndex(alice, env.seq(alice))};
1189 env(check::create(alice, gw, usd(max1)));
1190 env.close();
1191
1192 env(check::cash(gw, chkAliceId, usd(amt1)));
1193 env.close();
1194 env.require(Balance(alice, aliceStart - usd(10)));
1195
1196 // gw writes check to alice. alice cashes.
1197 UInt256 const chkGwId{getCheckIndex(gw, env.seq(gw))};
1198 env(check::create(gw, alice, usd(max2)));
1199 env.close();
1200
1201 env(check::cash(alice, chkGwId, usd(amt2)));
1202 env.close();
1203 env.require(Balance(alice, aliceStart));
1204
1205 // Return the quality to the unmodified state so it doesn't
1206 // interfere with upcoming tests.
1207 env(trust(truster, iou(1000)), inOrOut(0));
1208 env.close();
1209 };
1210
1211 // The first case is the only one where the quality affects the outcome.
1212 // pct amt1 max1 amt2 max2
1213 testIssuerQPay(alice, gw["USD"], qIn, 50, 10, 10, 5, 10);
1214 testIssuerQCheck(alice, gw["USD"], qIn, 50, 10, 10, 5, 10);
1215
1216 testIssuerQPay(gw, alice["USD"], qIn, 50, 10, 10, 10, 10);
1217 testIssuerQCheck(gw, alice["USD"], qIn, 50, 10, 10, 10, 10);
1218
1219 testIssuerQPay(alice, gw["USD"], qOut, 200, 10, 10, 10, 10);
1220 testIssuerQCheck(alice, gw["USD"], qOut, 200, 10, 10, 10, 10);
1221
1222 testIssuerQPay(gw, alice["USD"], qOut, 200, 10, 10, 10, 10);
1223 testIssuerQCheck(gw, alice["USD"], qOut, 200, 10, 10, 10, 10);
1224 }
1225
1226 void
1228 {
1229 // Explore many of the ways to fail at cashing a check.
1230 testcase("Cash invalid");
1231
1232 using namespace test::jtx;
1233
1234 Account const gw{"gateway"};
1235 Account const alice{"alice"};
1236 Account const bob{"bob"};
1237 Account const zoe{"zoe"};
1238 IOU const usd{gw["USD"]};
1239
1240 Env env(*this, features);
1241
1242 env.fund(XRP(1000), gw, alice, bob, zoe);
1243 env.close();
1244
1245 // Now set up alice's trustline.
1246 env(trust(alice, usd(20)));
1247 env.close();
1248 env(pay(gw, alice, usd(20)));
1249 env.close();
1250
1251 // Now set up bob's trustline.
1252 env(trust(bob, usd(20)));
1253 env.close();
1254
1255 // bob tries to cash a non-existent check from alice.
1256 {
1257 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
1258 env(check::cash(bob, chkId, usd(20)), Ter(tecNO_ENTRY));
1259 env.close();
1260 }
1261
1262 // Can't run pre-amendment behavior due to assertion failure.
1263 if (features[fixCleanup3_3_0])
1264 {
1265 env(check::cash(bob, UInt256{}, usd(20)), Ter(temMALFORMED));
1266 env.close();
1267 }
1268
1269 // alice creates her checks ahead of time.
1270 UInt256 const chkIdU{getCheckIndex(alice, env.seq(alice))};
1271 env(check::create(alice, bob, usd(20)));
1272 env.close();
1273
1274 UInt256 const chkIdX{getCheckIndex(alice, env.seq(alice))};
1275 env(check::create(alice, bob, XRP(10)));
1276 env.close();
1277
1278 using namespace std::chrono_literals;
1279 UInt256 const chkIdExp{getCheckIndex(alice, env.seq(alice))};
1280 env(check::create(alice, bob, XRP(10)), Expiration(env.now() + 1s));
1281 env.close();
1282
1283 UInt256 const chkIdFroz1{getCheckIndex(alice, env.seq(alice))};
1284 env(check::create(alice, bob, usd(1)));
1285 env.close();
1286
1287 UInt256 const chkIdFroz2{getCheckIndex(alice, env.seq(alice))};
1288 env(check::create(alice, bob, usd(2)));
1289 env.close();
1290
1291 UInt256 const chkIdFroz3{getCheckIndex(alice, env.seq(alice))};
1292 env(check::create(alice, bob, usd(3)));
1293 env.close();
1294
1295 UInt256 const chkIdFroz4{getCheckIndex(alice, env.seq(alice))};
1296 env(check::create(alice, bob, usd(4)));
1297 env.close();
1298
1299 UInt256 const chkIdFroz4ToIssuer{getCheckIndex(alice, env.seq(alice))};
1300 env(check::create(alice, gw, usd(4)));
1301 env.close();
1302
1303 UInt256 const chkIdFroz4Issuer{getCheckIndex(gw, env.seq(gw))};
1304 env(check::create(gw, alice, usd(4)));
1305 env.close();
1306
1307 UInt256 const chkIdNoDest1{getCheckIndex(alice, env.seq(alice))};
1308 env(check::create(alice, bob, usd(1)));
1309 env.close();
1310
1311 UInt256 const chkIdHasDest2{getCheckIndex(alice, env.seq(alice))};
1312 env(check::create(alice, bob, usd(2)), DestTag(7));
1313 env.close();
1314
1315 // Same set of failing cases for both IOU and XRP check cashing.
1316 auto const failingCases = [&env, &gw, &alice, &bob](
1317 UInt256 const& chkId, STAmount const& amount) {
1318 // Bad fee.
1319 env(check::cash(bob, chkId, amount), Fee(drops(-10)), Ter(temBAD_FEE));
1320 env.close();
1321
1322 // Bad flags.
1323 env(check::cash(bob, chkId, amount),
1324 Txflags(tfImmediateOrCancel),
1325 Ter(temINVALID_FLAG));
1326 env.close();
1327
1328 // Missing both Amount and DeliverMin.
1329 {
1330 json::Value tx{check::cash(bob, chkId, amount)};
1331 tx.removeMember(sfAmount.jsonName);
1332 env(tx, Ter(temMALFORMED));
1333 env.close();
1334 }
1335 // Both Amount and DeliverMin present.
1336 {
1337 json::Value tx{check::cash(bob, chkId, amount)};
1338 tx[sfDeliverMin.jsonName] = amount.getJson(JsonOptions::Values::None);
1339 env(tx, Ter(temMALFORMED));
1340 env.close();
1341 }
1342
1343 // Negative or zero amount.
1344 {
1345 STAmount neg{amount};
1346 neg.negate();
1347 env(check::cash(bob, chkId, neg), Ter(temBAD_AMOUNT));
1348 env.close();
1349 env(check::cash(bob, chkId, amount.zeroed()), Ter(temBAD_AMOUNT));
1350 env.close();
1351 }
1352
1353 // Bad currency.
1354 if (!amount.native())
1355 {
1356 Issue const badIssue{badCurrency(), amount.getIssuer()};
1357 STAmount badAmount{amount};
1358 badAmount.setIssue(Issue{badCurrency(), amount.getIssuer()});
1359 env(check::cash(bob, chkId, badAmount), Ter(temBAD_CURRENCY));
1360 env.close();
1361 }
1362
1363 // Not destination cashing check.
1364 env(check::cash(alice, chkId, amount), Ter(tecNO_PERMISSION));
1365 env.close();
1366 env(check::cash(gw, chkId, amount), Ter(tecNO_PERMISSION));
1367 env.close();
1368
1369 // Currency mismatch.
1370 {
1371 IOU const wrongCurrency{gw["EUR"]};
1372 STAmount badAmount{amount};
1373 badAmount.setIssue(wrongCurrency);
1374 env(check::cash(bob, chkId, badAmount), Ter(temMALFORMED));
1375 env.close();
1376 }
1377
1378 // Issuer mismatch.
1379 {
1380 IOU const wrongIssuer{alice["USD"]};
1381 STAmount badAmount{amount};
1382 badAmount.setIssue(wrongIssuer);
1383 env(check::cash(bob, chkId, badAmount), Ter(temMALFORMED));
1384 env.close();
1385 }
1386
1387 // Amount bigger than SendMax.
1388 env(check::cash(bob, chkId, amount + amount), Ter(tecPATH_PARTIAL));
1389 env.close();
1390
1391 // DeliverMin bigger than SendMax.
1392 env(check::cash(bob, chkId, check::DeliverMin(amount + amount)), Ter(tecPATH_PARTIAL));
1393 env.close();
1394 };
1395
1396 failingCases(chkIdX, XRP(10));
1397 failingCases(chkIdU, usd(20));
1398
1399 // Verify that those two checks really were cashable.
1400 env(check::cash(bob, chkIdU, usd(20)));
1401 env.close();
1402 env(check::cash(bob, chkIdX, check::DeliverMin(XRP(10))));
1403 verifyDeliveredAmount(env, XRP(10));
1404
1405 // Try to cash an expired check.
1406 env(check::cash(bob, chkIdExp, XRP(10)), Ter(tecEXPIRED));
1407 env.close();
1408
1409 // Cancel the expired check. Anyone can cancel an expired check.
1410 env(check::cancel(zoe, chkIdExp));
1411 env.close();
1412
1413 // Can we cash a check with frozen currency?
1414 {
1415 env(pay(bob, alice, usd(20)));
1416 env.close();
1417 env.require(Balance(alice, usd(20)));
1418 env.require(Balance(bob, usd(0)));
1419
1420 // Global freeze
1421 env(fset(gw, asfGlobalFreeze));
1422 env.close();
1423
1424 env(check::cash(bob, chkIdFroz1, usd(1)), Ter(tecPATH_PARTIAL));
1425 env.close();
1426 env(check::cash(bob, chkIdFroz1, check::DeliverMin(usd(0.5))), Ter(tecPATH_PARTIAL));
1427 env.close();
1428
1429 env(check::cash(gw, chkIdFroz4ToIssuer, usd(1)), Ter(tecPATH_PARTIAL));
1430 env.close();
1431 env(check::cash(gw, chkIdFroz4ToIssuer, check::DeliverMin(usd(0.5))),
1432 Ter(tecPATH_PARTIAL));
1433 env.close();
1434
1435 env(check::cash(alice, chkIdFroz4Issuer, usd(1)), Ter(tecFROZEN));
1436 env.close();
1437 env(check::cash(alice, chkIdFroz4Issuer, check::DeliverMin(usd(0.5))), Ter(tecFROZEN));
1438 env.close();
1439
1440 env(fclear(gw, asfGlobalFreeze));
1441 env.close();
1442
1443 // No longer frozen. Success.
1444 env(check::cash(bob, chkIdFroz1, usd(1)));
1445 env.close();
1446 env.require(Balance(alice, usd(19)));
1447 env.require(Balance(bob, usd(1)));
1448
1449 env(check::cash(gw, chkIdFroz4ToIssuer, usd(1)));
1450 env.close();
1451
1452 // Freeze individual trustlines.
1453 env(trust(gw, alice["USD"](0), tfSetFreeze));
1454 env.close();
1455 env(check::cash(bob, chkIdFroz2, usd(2)), Ter(tecPATH_PARTIAL));
1456 env.close();
1457 env(check::cash(bob, chkIdFroz2, check::DeliverMin(usd(1))), Ter(tecPATH_PARTIAL));
1458 env.close();
1459
1460 // Clear that freeze. Now check cashing works.
1461 env(trust(gw, alice["USD"](0), tfClearFreeze));
1462 env.close();
1463 env(check::cash(bob, chkIdFroz2, usd(2)));
1464 env.close();
1465 env.require(Balance(alice, usd(16)));
1466 env.require(Balance(bob, usd(3)));
1467
1468 // Freeze bob's trustline. bob can't cash the check.
1469 env(trust(gw, bob["USD"](0), tfSetFreeze));
1470 env.close();
1471 env(check::cash(bob, chkIdFroz3, usd(3)), Ter(tecFROZEN));
1472 env.close();
1473 env(check::cash(bob, chkIdFroz3, check::DeliverMin(usd(1))), Ter(tecFROZEN));
1474 env.close();
1475
1476 // Clear that freeze. Now check cashing works again.
1477 env(trust(gw, bob["USD"](0), tfClearFreeze));
1478 env.close();
1479 env(check::cash(bob, chkIdFroz3, check::DeliverMin(usd(1))));
1480 verifyDeliveredAmount(env, usd(3));
1481 env.require(Balance(alice, usd(13)));
1482 env.require(Balance(bob, usd(6)));
1483
1484 // Set bob's freeze bit in the other direction. Check
1485 // cashing fails.
1486 env(trust(bob, usd(20), tfSetFreeze));
1487 env.close();
1488 env(check::cash(bob, chkIdFroz4, usd(4)), Ter(terNO_LINE));
1489 env.close();
1490 env(check::cash(bob, chkIdFroz4, check::DeliverMin(usd(1))), Ter(terNO_LINE));
1491 env.close();
1492
1493 // Clear bob's freeze bit and the check should be cashable.
1494 env(trust(bob, usd(20), tfClearFreeze));
1495 env.close();
1496 env(check::cash(bob, chkIdFroz4, usd(4)));
1497 env.close();
1498 env.require(Balance(alice, usd(9)));
1499 env.require(Balance(bob, usd(10)));
1500 }
1501 {
1502 // Set the RequireDest flag on bob's account (after the check
1503 // was created) then cash a check without a destination tag.
1504 env(fset(bob, asfRequireDest));
1505 env.close();
1506 env(check::cash(bob, chkIdNoDest1, usd(1)), Ter(tecDST_TAG_NEEDED));
1507 env.close();
1508 env(check::cash(bob, chkIdNoDest1, check::DeliverMin(usd(0.5))),
1509 Ter(tecDST_TAG_NEEDED));
1510 env.close();
1511
1512 // bob can cash a check with a destination tag.
1513 env(check::cash(bob, chkIdHasDest2, usd(2)));
1514 env.close();
1515 env.require(Balance(alice, usd(7)));
1516 env.require(Balance(bob, usd(12)));
1517
1518 // Clear the RequireDest flag on bob's account so he can
1519 // cash the check with no DestinationTag.
1520 env(fclear(bob, asfRequireDest));
1521 env.close();
1522 env(check::cash(bob, chkIdNoDest1, usd(1)));
1523 env.close();
1524 env.require(Balance(alice, usd(6)));
1525 env.require(Balance(bob, usd(13)));
1526 }
1527 }
1528
1529 void
1531 {
1532 // Explore many of the ways to cancel a check.
1533 testcase("Cancel valid");
1534
1535 using namespace test::jtx;
1536
1537 Account const gw{"gateway"};
1538 Account const alice{"alice"};
1539 Account const bob{"bob"};
1540 Account const zoe{"zoe"};
1541 IOU const usd{gw["USD"]};
1542
1543 {
1544 Env env{*this, features};
1545
1546 env.fund(XRP(1000), gw, alice, bob, zoe);
1547 env.close();
1548
1549 // alice creates her checks ahead of time.
1550 // Three ordinary checks with no expiration.
1551 UInt256 const chkId1{getCheckIndex(alice, env.seq(alice))};
1552 env(check::create(alice, bob, usd(10)));
1553 env.close();
1554
1555 UInt256 const chkId2{getCheckIndex(alice, env.seq(alice))};
1556 env(check::create(alice, bob, XRP(10)));
1557 env.close();
1558
1559 UInt256 const chkId3{getCheckIndex(alice, env.seq(alice))};
1560 env(check::create(alice, bob, usd(10)));
1561 env.close();
1562
1563 // Three checks that expire in 10 minutes.
1564 using namespace std::chrono_literals;
1565 UInt256 const chkIdNotExp1{getCheckIndex(alice, env.seq(alice))};
1566 env(check::create(alice, bob, XRP(10)), Expiration(env.now() + 600s));
1567 env.close();
1568
1569 UInt256 const chkIdNotExp2{getCheckIndex(alice, env.seq(alice))};
1570 env(check::create(alice, bob, usd(10)), Expiration(env.now() + 600s));
1571 env.close();
1572
1573 UInt256 const chkIdNotExp3{getCheckIndex(alice, env.seq(alice))};
1574 env(check::create(alice, bob, XRP(10)), Expiration(env.now() + 600s));
1575 env.close();
1576
1577 // Three checks that expire in one second.
1578 UInt256 const chkIdExp1{getCheckIndex(alice, env.seq(alice))};
1579 env(check::create(alice, bob, usd(10)), Expiration(env.now() + 1s));
1580 env.close();
1581
1582 UInt256 const chkIdExp2{getCheckIndex(alice, env.seq(alice))};
1583 env(check::create(alice, bob, XRP(10)), Expiration(env.now() + 1s));
1584 env.close();
1585
1586 UInt256 const chkIdExp3{getCheckIndex(alice, env.seq(alice))};
1587 env(check::create(alice, bob, usd(10)), Expiration(env.now() + 1s));
1588 env.close();
1589
1590 // Two checks to cancel using a regular key and using multisigning.
1591 UInt256 const chkIdReg{getCheckIndex(alice, env.seq(alice))};
1592 env(check::create(alice, bob, usd(10)));
1593 env.close();
1594
1595 UInt256 const chkIdMSig{getCheckIndex(alice, env.seq(alice))};
1596 env(check::create(alice, bob, XRP(10)));
1597 env.close();
1598 BEAST_EXPECT(checksOnAccount(env, alice).size() == 11);
1599 BEAST_EXPECT(ownerCount(env, alice) == 11);
1600
1601 // Creator, destination, and an outsider cancel the checks.
1602 env(check::cancel(alice, chkId1));
1603 env.close();
1604 BEAST_EXPECT(checksOnAccount(env, alice).size() == 10);
1605 BEAST_EXPECT(ownerCount(env, alice) == 10);
1606
1607 env(check::cancel(bob, chkId2));
1608 env.close();
1609 BEAST_EXPECT(checksOnAccount(env, alice).size() == 9);
1610 BEAST_EXPECT(ownerCount(env, alice) == 9);
1611
1612 env(check::cancel(zoe, chkId3), Ter(tecNO_PERMISSION));
1613 env.close();
1614 BEAST_EXPECT(checksOnAccount(env, alice).size() == 9);
1615 BEAST_EXPECT(ownerCount(env, alice) == 9);
1616
1617 // Creator, destination, and an outsider cancel unexpired checks.
1618 env(check::cancel(alice, chkIdNotExp1));
1619 env.close();
1620 BEAST_EXPECT(checksOnAccount(env, alice).size() == 8);
1621 BEAST_EXPECT(ownerCount(env, alice) == 8);
1622
1623 env(check::cancel(bob, chkIdNotExp2));
1624 env.close();
1625 BEAST_EXPECT(checksOnAccount(env, alice).size() == 7);
1626 BEAST_EXPECT(ownerCount(env, alice) == 7);
1627
1628 env(check::cancel(zoe, chkIdNotExp3), Ter(tecNO_PERMISSION));
1629 env.close();
1630 BEAST_EXPECT(checksOnAccount(env, alice).size() == 7);
1631 BEAST_EXPECT(ownerCount(env, alice) == 7);
1632
1633 // Creator, destination, and an outsider cancel expired checks.
1634 env(check::cancel(alice, chkIdExp1));
1635 env.close();
1636 BEAST_EXPECT(checksOnAccount(env, alice).size() == 6);
1637 BEAST_EXPECT(ownerCount(env, alice) == 6);
1638
1639 env(check::cancel(bob, chkIdExp2));
1640 env.close();
1641 BEAST_EXPECT(checksOnAccount(env, alice).size() == 5);
1642 BEAST_EXPECT(ownerCount(env, alice) == 5);
1643
1644 env(check::cancel(zoe, chkIdExp3));
1645 env.close();
1646 BEAST_EXPECT(checksOnAccount(env, alice).size() == 4);
1647 BEAST_EXPECT(ownerCount(env, alice) == 4);
1648
1649 // Use a regular key and also multisign to cancel checks.
1650 Account const alie{"alie", KeyType::Ed25519};
1651 env(regkey(alice, alie));
1652 env.close();
1653
1654 Account const bogie{"bogie", KeyType::Secp256k1};
1655 Account const demon{"demon", KeyType::Ed25519};
1656 env(signers(alice, 2, {{bogie, 1}, {demon, 1}}), Sig(alie));
1657 env.close();
1658
1659 // alice uses her regular key to cancel a check.
1660 env(check::cancel(alice, chkIdReg), Sig(alie));
1661 env.close();
1662 BEAST_EXPECT(checksOnAccount(env, alice).size() == 3);
1663 BEAST_EXPECT(ownerCount(env, alice) == 4);
1664
1665 // alice uses multisigning to cancel a check.
1666 XRPAmount const baseFeeDrops{env.current()->fees().base};
1667 env(check::cancel(alice, chkIdMSig), Msig(bogie, demon), Fee(3 * baseFeeDrops));
1668 env.close();
1669 BEAST_EXPECT(checksOnAccount(env, alice).size() == 2);
1670 BEAST_EXPECT(ownerCount(env, alice) == 3);
1671
1672 // Creator and destination cancel the remaining unexpired checks.
1673 env(check::cancel(alice, chkId3), Sig(alice));
1674 env.close();
1675 BEAST_EXPECT(checksOnAccount(env, alice).size() == 1);
1676 BEAST_EXPECT(ownerCount(env, alice) == 2);
1677
1678 env(check::cancel(bob, chkIdNotExp3));
1679 env.close();
1680 BEAST_EXPECT(checksOnAccount(env, alice).empty());
1681 BEAST_EXPECT(ownerCount(env, alice) == 1);
1682 }
1683 }
1684
1685 void
1687 {
1688 // Explore many of the ways to fail at canceling a check.
1689 testcase("Cancel invalid");
1690
1691 using namespace test::jtx;
1692
1693 Account const alice{"alice"};
1694 Account const bob{"bob"};
1695
1696 Env env{*this, features};
1697
1698 env.fund(XRP(1000), alice, bob);
1699 env.close();
1700
1701 // Bad fee.
1702 env(check::cancel(bob, getCheckIndex(alice, env.seq(alice))),
1703 Fee(drops(-10)),
1704 Ter(temBAD_FEE));
1705 env.close();
1706
1707 // Bad flags.
1708 env(check::cancel(bob, getCheckIndex(alice, env.seq(alice))),
1709 Txflags(tfImmediateOrCancel),
1710 Ter(temINVALID_FLAG));
1711 env.close();
1712
1713 // Non-existent check.
1714 env(check::cancel(bob, getCheckIndex(alice, env.seq(alice))), Ter(tecNO_ENTRY));
1715 env.close();
1716
1717 // Can't run pre-amendment behavior due to assertion failure.
1718 if (features[fixCleanup3_3_0])
1719 {
1720 env(check::cancel(bob, UInt256{}), Ter(temMALFORMED));
1721 env.close();
1722 }
1723 }
1724
1725 void
1727 {
1728 testcase("DeliveredAmount For CheckCash Txn");
1729
1730 using namespace test::jtx;
1731 Account const alice{"alice"};
1732 Account const bob{"bob"};
1733
1734 Env env{*this, features};
1735
1736 env.fund(XRP(1000), alice, bob);
1737 env.close();
1738
1739 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
1740 env(check::create(alice, bob, XRP(200)));
1741 env.close();
1742
1743 env(check::cash(bob, chkId, check::DeliverMin(XRP(100))));
1744
1745 // Get the hash for the most recent transaction.
1746 std::string const txHash{
1747 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
1748 };
1749
1750 env.close();
1751 json::Value const meta = env.rpc("tx", txHash)[jss::result][jss::meta];
1752
1753 // DeliveredAmount and delivered_amount are present.
1754 BEAST_EXPECT(meta.isMember(sfDeliveredAmount.jsonName));
1755 BEAST_EXPECT(meta.isMember(jss::delivered_amount));
1756 }
1757
1758 void
1760 {
1761 testcase("With Tickets");
1762
1763 using namespace test::jtx;
1764
1765 Account const gw{"gw"};
1766 Account const alice{"alice"};
1767 Account const bob{"bob"};
1768 IOU const usd{gw["USD"]};
1769
1770 Env env{*this, features};
1771 env.fund(XRP(1000), gw, alice, bob);
1772 env.close();
1773
1774 // alice and bob grab enough tickets for all of the following
1775 // transactions. Note that once the tickets are acquired alice's
1776 // and bob's account sequence numbers should not advance.
1777 std::uint32_t aliceTicketSeq{env.seq(alice) + 1};
1778 env(ticket::create(alice, 10));
1779 std::uint32_t const aliceSeq{env.seq(alice)};
1780
1781 std::uint32_t bobTicketSeq{env.seq(bob) + 1};
1782 env(ticket::create(bob, 10));
1783 std::uint32_t const bobSeq{env.seq(bob)};
1784
1785 env.close();
1786 env.require(Owners(alice, 10));
1787 env.require(Owners(bob, 10));
1788
1789 // alice gets enough USD to write a few checks.
1790 env(trust(alice, usd(1000)), ticket::Use(aliceTicketSeq++));
1791 env(trust(bob, usd(1000)), ticket::Use(bobTicketSeq++));
1792 env.close();
1793 env.require(Owners(alice, 10));
1794 env.require(Owners(bob, 10));
1795
1796 env.require(Tickets(alice, env.seq(alice) - aliceTicketSeq));
1797 BEAST_EXPECT(env.seq(alice) == aliceSeq);
1798
1799 env.require(Tickets(bob, env.seq(bob) - bobTicketSeq));
1800 BEAST_EXPECT(env.seq(bob) == bobSeq);
1801
1802 env(pay(gw, alice, usd(900)));
1803 env.close();
1804
1805 // alice creates four checks; two XRP, two IOU. Bob will cash
1806 // one of each and cancel one of each.
1807 UInt256 const chkIdXrp1{getCheckIndex(alice, aliceTicketSeq)};
1808 env(check::create(alice, bob, XRP(200)), ticket::Use(aliceTicketSeq++));
1809
1810 UInt256 const chkIdXrp2{getCheckIndex(alice, aliceTicketSeq)};
1811 env(check::create(alice, bob, XRP(300)), ticket::Use(aliceTicketSeq++));
1812
1813 UInt256 const chkIdUsd1{getCheckIndex(alice, aliceTicketSeq)};
1814 env(check::create(alice, bob, usd(200)), ticket::Use(aliceTicketSeq++));
1815
1816 UInt256 const chkIdUsd2{getCheckIndex(alice, aliceTicketSeq)};
1817 env(check::create(alice, bob, usd(300)), ticket::Use(aliceTicketSeq++));
1818
1819 env.close();
1820 // Alice used four tickets but created four checks.
1821 env.require(Owners(alice, 10));
1822 env.require(Tickets(alice, env.seq(alice) - aliceTicketSeq));
1823 BEAST_EXPECT(checksOnAccount(env, alice).size() == 4);
1824 BEAST_EXPECT(env.seq(alice) == aliceSeq);
1825
1826 env.require(Owners(bob, 10));
1827 BEAST_EXPECT(env.seq(bob) == bobSeq);
1828
1829 // Bob cancels two of alice's checks.
1830 env(check::cancel(bob, chkIdXrp1), ticket::Use(bobTicketSeq++));
1831 env(check::cancel(bob, chkIdUsd2), ticket::Use(bobTicketSeq++));
1832 env.close();
1833
1834 env.require(Owners(alice, 8));
1835 env.require(Tickets(alice, env.seq(alice) - aliceTicketSeq));
1836 BEAST_EXPECT(checksOnAccount(env, alice).size() == 2);
1837 BEAST_EXPECT(env.seq(alice) == aliceSeq);
1838
1839 env.require(Owners(bob, 8));
1840 BEAST_EXPECT(env.seq(bob) == bobSeq);
1841
1842 // Bob cashes alice's two remaining checks.
1843 env(check::cash(bob, chkIdXrp2, XRP(300)), ticket::Use(bobTicketSeq++));
1844 env(check::cash(bob, chkIdUsd1, usd(200)), ticket::Use(bobTicketSeq++));
1845 env.close();
1846
1847 env.require(Owners(alice, 6));
1848 env.require(Tickets(alice, env.seq(alice) - aliceTicketSeq));
1849 BEAST_EXPECT(checksOnAccount(env, alice).empty());
1850 BEAST_EXPECT(env.seq(alice) == aliceSeq);
1851 env.require(Balance(alice, usd(700)));
1852
1853 env.require(Owners(bob, 6));
1854 BEAST_EXPECT(env.seq(bob) == bobSeq);
1855 env.require(Balance(bob, usd(200)));
1856 }
1857
1858 void
1860 {
1861 // Explore automatic trust line creation when a check is cashed.
1862 //
1863
1864 testcase("Trust Line Creation");
1865
1866 using namespace test::jtx;
1867
1868 Env env{*this, features};
1869
1870 // An account that independently tracks its owner count.
1871 struct AccountOwns
1872 {
1874 Env const& env;
1875 Account const acct;
1876 std::size_t owners;
1877
1878 void
1879 verifyOwners(std::uint32_t line) const
1880 {
1881 suite.expect(
1882 ownerCount(env, acct) == owners, "Owner count mismatch", __FILE__, line);
1883 }
1884
1885 // Operators to make using the class more convenient.
1886 operator Account() const
1887 {
1888 return acct;
1889 }
1890
1891 operator xrpl::AccountID() const
1892 {
1893 return acct.id();
1894 }
1895
1896 IOU
1897 operator[](std::string const& s) const
1898 {
1899 return acct[s];
1900 }
1901 };
1902
1903 AccountOwns alice{.suite = *this, .env = env, .acct = "alice", .owners = 0};
1904 AccountOwns bob{.suite = *this, .env = env, .acct = "bob", .owners = 0};
1905
1906 // Fund with noripple so the accounts do not have any flags set.
1907 env.fund(XRP(5000), noripple(alice, bob));
1908 env.close();
1909
1910 // Automatic trust line creation should fail if the check destination
1911 // can't afford the reserve for the trust line.
1912 {
1913 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
1914
1915 // Fund gw1 with noripple (even though that's atypical for a
1916 // gateway) so it does not have any flags set. We'll set flags
1917 // on gw1 later.
1918 env.fund(XRP(5000), noripple(gw1));
1919 env.close();
1920
1921 IOU const cK8 = gw1["CK8"];
1922 gw1.verifyOwners(__LINE__);
1923
1924 Account const yui{"yui"};
1925
1926 // Note the reserve in unit tests is 200 XRP, not 20. So here
1927 // we're just barely giving yui enough XRP to meet the
1928 // account reserve.
1929 env.fund(XRP(200), yui);
1930 env.close();
1931
1932 UInt256 const chkId{getCheckIndex(gw1, env.seq(gw1))};
1933 env(check::create(gw1, yui, cK8(99)));
1934 env.close();
1935
1936 env(check::cash(yui, chkId, cK8(99)), Ter(tecNO_LINE_INSUF_RESERVE));
1937 env.close();
1938 alice.verifyOwners(__LINE__);
1939
1940 // Give yui enough XRP to meet the trust line's reserve. Cashing
1941 // the check succeeds and creates the trust line.
1942 env(pay(env.master, yui, XRP(51)));
1943 env.close();
1944 env(check::cash(yui, chkId, cK8(99)));
1945 verifyDeliveredAmount(env, cK8(99));
1946 env.close();
1947 BEAST_EXPECT(ownerCount(env, yui) == 1);
1948
1949 // The automatic trust line does not take a reserve from gw1.
1950 // Since gw1's check was consumed it has no owners.
1951 gw1.verifyOwners(__LINE__);
1952 }
1953
1954 // We'll be looking at the effects of various account root flags.
1955
1956 // Automatically create trust lines using
1957 // o Offers and
1958 // o Check cashing
1959 // Compare the resulting trust lines and expect them to be very similar.
1960
1961 // Lambda that compares two trust lines created by
1962 // o Offer crossing and
1963 // o Check cashing
1964 // between the same two accounts but with two different currencies.
1965 // The lambda expects the two trust lines to be largely similar.
1966 auto const cmpTrustLines = [this, &env](
1967 Account const& acct1,
1968 Account const& acct2,
1969 IOU const& offerIou,
1970 IOU const& checkIou) {
1971 auto const offerLine = env.le(keylet::trustLine(acct1, acct2, offerIou.currency));
1972 auto const checkLine = env.le(keylet::trustLine(acct1, acct2, checkIou.currency));
1973 if (offerLine == nullptr || checkLine == nullptr)
1974 {
1975 BEAST_EXPECT(offerLine == nullptr && checkLine == nullptr);
1976 return;
1977 }
1978
1979 {
1980 // Compare the contents of required fields.
1981 BEAST_EXPECT(offerLine->at(sfFlags) == checkLine->at(sfFlags));
1982
1983 // Lambda that compares the contents of required STAmounts
1984 // without comparing the currency.
1985 auto const cmpReqAmount = [this, offerLine, checkLine](SF_AMOUNT const& sfield) {
1986 STAmount const offerAmount = offerLine->at(sfield);
1987 STAmount const checkAmount = checkLine->at(sfield);
1988
1989 // Neither STAmount should be native.
1990 if (!BEAST_EXPECT(!offerAmount.native() && !checkAmount.native()))
1991 return;
1992
1993 BEAST_EXPECT(offerAmount.getIssuer() == checkAmount.getIssuer());
1994 BEAST_EXPECT(offerAmount.negative() == checkAmount.negative());
1995 BEAST_EXPECT(offerAmount.mantissa() == checkAmount.mantissa());
1996 BEAST_EXPECT(offerAmount.exponent() == checkAmount.exponent());
1997 };
1998 cmpReqAmount(sfBalance);
1999 cmpReqAmount(sfLowLimit);
2000 cmpReqAmount(sfHighLimit);
2001 }
2002 {
2003 // Lambda that compares the contents of optional fields.
2004 auto const cmpOptField = [this, offerLine, checkLine](auto const& sfield) {
2005 // Expect both fields to either be present or absent.
2006 if (!BEAST_EXPECT(
2007 offerLine->isFieldPresent(sfield) == checkLine->isFieldPresent(sfield)))
2008 return;
2009
2010 // If both fields are absent then there's nothing
2011 // further to check.
2012 if (!offerLine->isFieldPresent(sfield))
2013 return;
2014
2015 // Both optional fields are present so we can compare
2016 // them.
2017 BEAST_EXPECT(offerLine->at(sfield) == checkLine->at(sfield));
2018 };
2019 cmpOptField(sfLowNode);
2020 cmpOptField(sfLowQualityIn);
2021 cmpOptField(sfLowQualityOut);
2022
2023 cmpOptField(sfHighNode);
2024 cmpOptField(sfHighQualityIn);
2025 cmpOptField(sfHighQualityOut);
2026 }
2027 };
2028
2029 //----------- No account root flags, check written by issuer -----------
2030 {
2031 // No account root flags on any participant.
2032 // Automatic trust line from issuer to destination.
2033 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2034
2035 BEAST_EXPECT((*env.le(gw1))[sfFlags] == 0);
2036 BEAST_EXPECT((*env.le(alice))[sfFlags] == 0);
2037 BEAST_EXPECT((*env.le(bob))[sfFlags] == 0);
2038
2039 // Use offers to automatically create the trust line.
2040 IOU const oF1 = gw1["OF1"];
2041 env(offer(gw1, XRP(98), oF1(98)));
2042 env.close();
2043 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, oF1.currency)) == nullptr);
2044 env(offer(alice, oF1(98), XRP(98)));
2045 ++alice.owners;
2046 env.close();
2047
2048 // Both offers should be consumed.
2049 // Since gw1's offer was consumed and the trust line was not
2050 // created by gw1, gw1's owner count should be 0.
2051 gw1.verifyOwners(__LINE__);
2052
2053 // alice's automatically created trust line bumps her owner count.
2054 alice.verifyOwners(__LINE__);
2055
2056 // Use check cashing to automatically create the trust line.
2057 IOU const cK1 = gw1["CK1"];
2058 UInt256 const chkId{getCheckIndex(gw1, env.seq(gw1))};
2059 env(check::create(gw1, alice, cK1(98)));
2060 env.close();
2061 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, cK1.currency)) == nullptr);
2062 env(check::cash(alice, chkId, cK1(98)));
2063 ++alice.owners;
2064 verifyDeliveredAmount(env, cK1(98));
2065 env.close();
2066
2067 // gw1's check should be consumed.
2068 // Since gw1's check was consumed and the trust line was not
2069 // created by gw1, gw1's owner count should be 0.
2070 gw1.verifyOwners(__LINE__);
2071
2072 // alice's automatically created trust line bumps her owner count.
2073 alice.verifyOwners(__LINE__);
2074
2075 cmpTrustLines(gw1, alice, oF1, cK1);
2076 }
2077 //--------- No account root flags, check written by non-issuer ---------
2078 {
2079 // No account root flags on any participant.
2080 // Automatic trust line from non-issuer to non-issuer.
2081
2082 // Use offers to automatically create the trust line.
2083 // Transfer of assets using offers does not require rippling.
2084 // So bob's offer is successfully crossed which creates the
2085 // trust line.
2086 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2087 IOU const oF1 = gw1["OF1"];
2088 env(offer(alice, XRP(97), oF1(97)));
2089 env.close();
2090 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, oF1.currency)) == nullptr);
2091 env(offer(bob, oF1(97), XRP(97)));
2092 ++bob.owners;
2093 env.close();
2094
2095 // Both offers should be consumed.
2096 env.require(Balance(alice, oF1(1)));
2097 env.require(Balance(bob, oF1(97)));
2098
2099 // bob now has an owner count of 1 due to the new trust line.
2100 gw1.verifyOwners(__LINE__);
2101 alice.verifyOwners(__LINE__);
2102 bob.verifyOwners(__LINE__);
2103
2104 // Use check cashing to automatically create the trust line.
2105 //
2106 // However cashing a check (unlike crossing offers) requires
2107 // rippling through the currency's issuer. Since gw1 does not
2108 // have rippling enabled the check cash fails and bob does not
2109 // have a trust line created.
2110 IOU const cK1 = gw1["CK1"];
2111 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
2112 env(check::create(alice, bob, cK1(97)));
2113 env.close();
2114 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, cK1.currency)) == nullptr);
2115 env(check::cash(bob, chkId, cK1(97)), Ter(terNO_RIPPLE));
2116 env.close();
2117
2118 BEAST_EXPECT(env.le(keylet::trustLine(gw1, bob, oF1.currency)) != nullptr);
2119 BEAST_EXPECT(env.le(keylet::trustLine(gw1, bob, cK1.currency)) == nullptr);
2120
2121 // Delete alice's check since it is no longer needed.
2122 env(check::cancel(alice, chkId));
2123 env.close();
2124
2125 // No one's owner count should have changed.
2126 gw1.verifyOwners(__LINE__);
2127 alice.verifyOwners(__LINE__);
2128 bob.verifyOwners(__LINE__);
2129 }
2130
2131 //------------- lsfDefaultRipple, check written by issuer --------------
2132 {
2133 // gw1 enables rippling.
2134 // Automatic trust line from issuer to non-issuer should still work.
2135 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2136 env(fset(gw1, asfDefaultRipple));
2137 env.close();
2138
2139 // Use offers to automatically create the trust line.
2140 IOU const oF2 = gw1["OF2"];
2141 env(offer(gw1, XRP(96), oF2(96)));
2142 env.close();
2143 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, oF2.currency)) == nullptr);
2144 env(offer(alice, oF2(96), XRP(96)));
2145 ++alice.owners;
2146 env.close();
2147
2148 // Both offers should be consumed.
2149 // Since gw1's offer was consumed and the trust line was not
2150 // created by gw1, gw1's owner count should still be 0.
2151 gw1.verifyOwners(__LINE__);
2152
2153 // alice's automatically created trust line bumps her owner count.
2154 alice.verifyOwners(__LINE__);
2155
2156 // Use check cashing to automatically create the trust line.
2157 IOU const cK2 = gw1["CK2"];
2158 UInt256 const chkId{getCheckIndex(gw1, env.seq(gw1))};
2159 env(check::create(gw1, alice, cK2(96)));
2160 env.close();
2161 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, cK2.currency)) == nullptr);
2162 env(check::cash(alice, chkId, cK2(96)));
2163 ++alice.owners;
2164 verifyDeliveredAmount(env, cK2(96));
2165 env.close();
2166
2167 // gw1's check should be consumed.
2168 // Since gw1's check was consumed and the trust line was not
2169 // created by gw1, gw1's owner count should still be 0.
2170 gw1.verifyOwners(__LINE__);
2171
2172 // alice's automatically created trust line bumps her owner count.
2173 alice.verifyOwners(__LINE__);
2174
2175 cmpTrustLines(gw1, alice, oF2, cK2);
2176 }
2177 //----------- lsfDefaultRipple, check written by non-issuer ------------
2178 {
2179 // gw1 enabled rippling, so automatic trust line from non-issuer
2180 // to non-issuer should work.
2181
2182 // Use offers to automatically create the trust line.
2183 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2184 IOU const oF2 = gw1["OF2"];
2185 env(offer(alice, XRP(95), oF2(95)));
2186 env.close();
2187 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, oF2.currency)) == nullptr);
2188 env(offer(bob, oF2(95), XRP(95)));
2189 ++bob.owners;
2190 env.close();
2191
2192 // bob's owner count should increase due to the new trust line.
2193 gw1.verifyOwners(__LINE__);
2194 alice.verifyOwners(__LINE__);
2195 bob.verifyOwners(__LINE__);
2196
2197 // Use check cashing to automatically create the trust line.
2198 IOU const cK2 = gw1["CK2"];
2199 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
2200 env(check::create(alice, bob, cK2(95)));
2201 env.close();
2202 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, cK2.currency)) == nullptr);
2203 env(check::cash(bob, chkId, cK2(95)));
2204 ++bob.owners;
2205 verifyDeliveredAmount(env, cK2(95));
2206 env.close();
2207
2208 // bob's owner count should increase due to the new trust line.
2209 gw1.verifyOwners(__LINE__);
2210 alice.verifyOwners(__LINE__);
2211 bob.verifyOwners(__LINE__);
2212
2213 cmpTrustLines(alice, bob, oF2, cK2);
2214 }
2215
2216 //-------------- lsfDepositAuth, check written by issuer ---------------
2217 {
2218 // Both offers and checks ignore the lsfDepositAuth flag, since
2219 // the destination signs the transaction that delivers their funds.
2220 // So setting lsfDepositAuth on all the participants should not
2221 // change any outcomes.
2222 //
2223 // Automatic trust line from issuer to non-issuer should still work.
2224 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2225 env(fset(gw1, asfDepositAuth));
2226 env(fset(alice, asfDepositAuth));
2227 env(fset(bob, asfDepositAuth));
2228 env.close();
2229
2230 // Use offers to automatically create the trust line.
2231 IOU const oF3 = gw1["OF3"];
2232 env(offer(gw1, XRP(94), oF3(94)));
2233 env.close();
2234 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, oF3.currency)) == nullptr);
2235 env(offer(alice, oF3(94), XRP(94)));
2236 ++alice.owners;
2237 env.close();
2238
2239 // Both offers should be consumed.
2240 // Since gw1's offer was consumed and the trust line was not
2241 // created by gw1, gw1's owner count should still be 0.
2242 gw1.verifyOwners(__LINE__);
2243
2244 // alice's automatically created trust line bumps her owner count.
2245 alice.verifyOwners(__LINE__);
2246
2247 // Use check cashing to automatically create the trust line.
2248 IOU const cK3 = gw1["CK3"];
2249 UInt256 const chkId{getCheckIndex(gw1, env.seq(gw1))};
2250 env(check::create(gw1, alice, cK3(94)));
2251 env.close();
2252 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, cK3.currency)) == nullptr);
2253 env(check::cash(alice, chkId, cK3(94)));
2254 ++alice.owners;
2255 verifyDeliveredAmount(env, cK3(94));
2256 env.close();
2257
2258 // gw1's check should be consumed.
2259 // Since gw1's check was consumed and the trust line was not
2260 // created by gw1, gw1's owner count should still be 0.
2261 gw1.verifyOwners(__LINE__);
2262
2263 // alice's automatically created trust line bumps her owner count.
2264 alice.verifyOwners(__LINE__);
2265
2266 cmpTrustLines(gw1, alice, oF3, cK3);
2267 }
2268 //------------ lsfDepositAuth, check written by non-issuer -------------
2269 {
2270 // The presence of the lsfDepositAuth flag should not affect
2271 // automatic trust line creation.
2272
2273 // Use offers to automatically create the trust line.
2274 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2275 IOU const oF3 = gw1["OF3"];
2276 env(offer(alice, XRP(93), oF3(93)));
2277 env.close();
2278 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, oF3.currency)) == nullptr);
2279 env(offer(bob, oF3(93), XRP(93)));
2280 ++bob.owners;
2281 env.close();
2282
2283 // bob's owner count should increase due to the new trust line.
2284 gw1.verifyOwners(__LINE__);
2285 alice.verifyOwners(__LINE__);
2286 bob.verifyOwners(__LINE__);
2287
2288 // Use check cashing to automatically create the trust line.
2289 IOU const cK3 = gw1["CK3"];
2290 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
2291 env(check::create(alice, bob, cK3(93)));
2292 env.close();
2293 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, cK3.currency)) == nullptr);
2294 env(check::cash(bob, chkId, cK3(93)));
2295 ++bob.owners;
2296 verifyDeliveredAmount(env, cK3(93));
2297 env.close();
2298
2299 // bob's owner count should increase due to the new trust line.
2300 gw1.verifyOwners(__LINE__);
2301 alice.verifyOwners(__LINE__);
2302 bob.verifyOwners(__LINE__);
2303
2304 cmpTrustLines(alice, bob, oF3, cK3);
2305 }
2306
2307 //-------------- lsfGlobalFreeze, check written by issuer --------------
2308 {
2309 // Set lsfGlobalFreeze on gw1. That should stop any automatic
2310 // trust lines from being created.
2311 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2312 env(fset(gw1, asfGlobalFreeze));
2313 env.close();
2314
2315 // Use offers to automatically create the trust line.
2316 IOU const oF4 = gw1["OF4"];
2317 env(offer(gw1, XRP(92), oF4(92)), Ter(tecFROZEN));
2318 env.close();
2319 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, oF4.currency)) == nullptr);
2320 env(offer(alice, oF4(92), XRP(92)), Ter(tecFROZEN));
2321 env.close();
2322
2323 // No one's owner count should have changed.
2324 gw1.verifyOwners(__LINE__);
2325 alice.verifyOwners(__LINE__);
2326 bob.verifyOwners(__LINE__);
2327
2328 // Use check cashing to automatically create the trust line.
2329 IOU const cK4 = gw1["CK4"];
2330 UInt256 const chkId{getCheckIndex(gw1, env.seq(gw1))};
2331 env(check::create(gw1, alice, cK4(92)), Ter(tecFROZEN));
2332 env.close();
2333 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, cK4.currency)) == nullptr);
2334 env(check::cash(alice, chkId, cK4(92)), Ter(tecNO_ENTRY));
2335 env.close();
2336
2337 // No one's owner count should have changed.
2338 gw1.verifyOwners(__LINE__);
2339 alice.verifyOwners(__LINE__);
2340 bob.verifyOwners(__LINE__);
2341
2342 // Because gw1 has set lsfGlobalFreeze, neither trust line
2343 // is created.
2344 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, oF4.currency)) == nullptr);
2345 BEAST_EXPECT(env.le(keylet::trustLine(gw1, alice, cK4.currency)) == nullptr);
2346 }
2347 //------------ lsfGlobalFreeze, check written by non-issuer ------------
2348 {
2349 // Since gw1 has the lsfGlobalFreeze flag set, there should be
2350 // no automatic trust line creation between non-issuers.
2351
2352 // Use offers to automatically create the trust line.
2353 AccountOwns const gw1{.suite = *this, .env = env, .acct = "gw1", .owners = 0};
2354 IOU const oF4 = gw1["OF4"];
2355 env(offer(alice, XRP(91), oF4(91)), Ter(tecFROZEN));
2356 env.close();
2357 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, oF4.currency)) == nullptr);
2358 env(offer(bob, oF4(91), XRP(91)), Ter(tecFROZEN));
2359 env.close();
2360
2361 // No one's owner count should have changed.
2362 gw1.verifyOwners(__LINE__);
2363 alice.verifyOwners(__LINE__);
2364 bob.verifyOwners(__LINE__);
2365
2366 // Use check cashing to automatically create the trust line.
2367 IOU const cK4 = gw1["CK4"];
2368 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
2369 env(check::create(alice, bob, cK4(91)), Ter(tecFROZEN));
2370 env.close();
2371 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, cK4.currency)) == nullptr);
2372 env(check::cash(bob, chkId, cK4(91)), Ter(tecNO_ENTRY));
2373 env.close();
2374
2375 // No one's owner count should have changed.
2376 gw1.verifyOwners(__LINE__);
2377 alice.verifyOwners(__LINE__);
2378 bob.verifyOwners(__LINE__);
2379
2380 // Because gw1 has set lsfGlobalFreeze, neither trust line
2381 // is created.
2382 BEAST_EXPECT(env.le(keylet::trustLine(gw1, bob, oF4.currency)) == nullptr);
2383 BEAST_EXPECT(env.le(keylet::trustLine(gw1, bob, cK4.currency)) == nullptr);
2384 }
2385
2386 //-------------- lsfRequireAuth, check written by issuer ---------------
2387
2388 // We want to test the lsfRequireAuth flag, but we can't set that
2389 // flag on an account that already has trust lines. So we'll fund
2390 // a new gateway and use that.
2391 {
2392 AccountOwns gw2{.suite = *this, .env = env, .acct = "gw2", .owners = 0};
2393 env.fund(XRP(5000), gw2);
2394 env.close();
2395
2396 // Set lsfRequireAuth on gw2. That should stop any automatic
2397 // trust lines from being created.
2398 env(fset(gw2, asfRequireAuth));
2399 env.close();
2400
2401 // Use offers to automatically create the trust line.
2402 IOU const oF5 = gw2["OF5"];
2403 std::uint32_t const gw2OfferSeq = {env.seq(gw2)};
2404 env(offer(gw2, XRP(92), oF5(92)));
2405 ++gw2.owners;
2406 env.close();
2407 BEAST_EXPECT(env.le(keylet::trustLine(gw2, alice, oF5.currency)) == nullptr);
2408 env(offer(alice, oF5(92), XRP(92)), Ter(tecNO_LINE));
2409 env.close();
2410
2411 // gw2 should still own the offer, but no one else's owner
2412 // count should have changed.
2413 gw2.verifyOwners(__LINE__);
2414 alice.verifyOwners(__LINE__);
2415 bob.verifyOwners(__LINE__);
2416
2417 // Since we don't need it any more, remove gw2's offer.
2418 env(offerCancel(gw2, gw2OfferSeq));
2419 --gw2.owners;
2420 env.close();
2421 gw2.verifyOwners(__LINE__);
2422
2423 // Use check cashing to automatically create the trust line.
2424 IOU const cK5 = gw2["CK5"];
2425 UInt256 const chkId{getCheckIndex(gw2, env.seq(gw2))};
2426 env(check::create(gw2, alice, cK5(92)));
2427 ++gw2.owners;
2428 env.close();
2429 BEAST_EXPECT(env.le(keylet::trustLine(gw2, alice, cK5.currency)) == nullptr);
2430 env(check::cash(alice, chkId, cK5(92)), Ter(tecNO_AUTH));
2431 env.close();
2432
2433 // gw2 should still own the check, but no one else's owner
2434 // count should have changed.
2435 gw2.verifyOwners(__LINE__);
2436 alice.verifyOwners(__LINE__);
2437 bob.verifyOwners(__LINE__);
2438
2439 // Because gw2 has set lsfRequireAuth, neither trust line
2440 // is created.
2441 BEAST_EXPECT(env.le(keylet::trustLine(gw2, alice, oF5.currency)) == nullptr);
2442 BEAST_EXPECT(env.le(keylet::trustLine(gw2, alice, cK5.currency)) == nullptr);
2443
2444 // Since we don't need it any more, remove gw2's check.
2445 env(check::cancel(gw2, chkId));
2446 --gw2.owners;
2447 env.close();
2448 gw2.verifyOwners(__LINE__);
2449 }
2450 //------------ lsfRequireAuth, check written by non-issuer -------------
2451 {
2452 // Since gw2 has the lsfRequireAuth flag set, there should be
2453 // no automatic trust line creation between non-issuers.
2454
2455 // Use offers to automatically create the trust line.
2456 AccountOwns const gw2{.suite = *this, .env = env, .acct = "gw2", .owners = 0};
2457 IOU const oF5 = gw2["OF5"];
2458 env(offer(alice, XRP(91), oF5(91)), Ter(tecUNFUNDED_OFFER));
2459 env.close();
2460 env(offer(bob, oF5(91), XRP(91)), Ter(tecNO_LINE));
2461 BEAST_EXPECT(env.le(keylet::trustLine(gw2, bob, oF5.currency)) == nullptr);
2462 env.close();
2463
2464 gw2.verifyOwners(__LINE__);
2465 alice.verifyOwners(__LINE__);
2466 bob.verifyOwners(__LINE__);
2467
2468 // Use check cashing to automatically create the trust line.
2469 IOU const cK5 = gw2["CK5"];
2470 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
2471 env(check::create(alice, bob, cK5(91)));
2472 env.close();
2473 BEAST_EXPECT(env.le(keylet::trustLine(alice, bob, cK5.currency)) == nullptr);
2474 env(check::cash(bob, chkId, cK5(91)), Ter(tecPATH_PARTIAL));
2475 env.close();
2476
2477 // Delete alice's check since it is no longer needed.
2478 env(check::cancel(alice, chkId));
2479 env.close();
2480
2481 // No one's owner count should have changed.
2482 gw2.verifyOwners(__LINE__);
2483 alice.verifyOwners(__LINE__);
2484 bob.verifyOwners(__LINE__);
2485
2486 // Because gw2 has set lsfRequireAuth, neither trust line
2487 // is created.
2488 BEAST_EXPECT(env.le(keylet::trustLine(gw2, bob, oF5.currency)) == nullptr);
2489 BEAST_EXPECT(env.le(keylet::trustLine(gw2, bob, cK5.currency)) == nullptr);
2490 }
2491 }
2492
2493 void
2495 {
2496 testEnabled(features);
2497 testCreateValid(features);
2499 testCreateInvalid(features);
2500 testCashXRP(features);
2501 testCashIOU(features);
2502 testCashXferFee(features);
2503 testCashQuality(features);
2504 testCashInvalid(features);
2505 testCancelValid(features);
2506 testCancelInvalid(features);
2508 testWithTickets(features);
2509 }
2510
2511public:
2512 void
2513 run() override
2514 {
2515 using namespace test::jtx;
2516 auto const sa = testableAmendments();
2517 testWithFeats(sa);
2519 }
2520};
2521
2523
2524} // namespace xrpl
A testsuite class.
Definition suite.h:52
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Value removeMember(char const *key)
Remove and return the named member.
bool isMember(char const *key) const
Return true if the object has a member named key.
void testCreateInvalid(FeatureBitset features)
void testCreateDisallowIncoming(FeatureBitset features)
void testCashQuality(FeatureBitset features)
static std::vector< SLE::const_pointer > checksOnAccount(test::jtx::Env &env, test::jtx::Account account)
void verifyDeliveredAmount(test::jtx::Env &env, STAmount const &amount)
void testCreateValid(FeatureBitset features)
void testTrustLineCreation(FeatureBitset features)
void testCashInvalid(FeatureBitset features)
void testCancelInvalid(FeatureBitset features)
void testEnabled(FeatureBitset features)
void testCashIOU(FeatureBitset features)
void testWithTickets(FeatureBitset features)
static UInt256 getCheckIndex(AccountID const &account, std::uint32_t uSequence)
void testCancelValid(FeatureBitset features)
void testWithFeats(FeatureBitset features)
void testCashXferFee(FeatureBitset features)
void run() override
Runs the suite.
void testDeliveredAmountForCheckCashTxn(FeatureBitset features)
void testCashXRP(FeatureBitset features)
A currency issued by an account.
Definition Issue.h:18
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
void setIssue(Asset const &asset)
Set the Issue for this amount.
void negate()
Definition STAmount.h:586
std::uint64_t mantissa() const noexcept
Definition STAmount.h:490
bool negative() const noexcept
Definition STAmount.h:484
bool native() const noexcept
Definition STAmount.h:471
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
int exponent() const noexcept
Definition STAmount.h:459
AccountID const & getIssuer() const
Definition STAmount.h:516
std::shared_ptr< STLedgerEntry const > const & ConstRef
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:560
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
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
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(SLE::ConstRef)> const &f)
Iterate all items in the given directory.
@ terNO_LINE
Definition TER.h:221
@ terNO_RIPPLE
Definition TER.h:226
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").
TypedField< STAmount > SF_AMOUNT
Definition SField.h:357
BaseUInt< 256 > UInt256
Definition base_uint.h:581
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ temBAD_CURRENCY
Definition TER.h:78
@ temBAD_EXPIRATION
Definition TER.h:79
@ temBAD_FEE
Definition TER.h:80
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temBAD_AMOUNT
Definition TER.h:77
@ temREDUNDANT
Definition TER.h:100
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_LINE_INSUF_RESERVE
Definition TER.h:303
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecNO_ENTRY
Definition TER.h:317
@ tecPATH_DRY
Definition TER.h:305
@ tecNO_AUTH
Definition TER.h:311
@ tecFROZEN
Definition TER.h:314
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecEXPIRED
Definition TER.h:325
@ tecNO_LINE
Definition TER.h:312
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tecNO_PERMISSION
Definition TER.h:316
@ tecDST_TAG_NEEDED
Definition TER.h:320
@ tecNO_DST
Definition TER.h:301
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
@ tesSUCCESS
Definition TER.h:253
UInt256 key
Definition Keylet.h:21