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