xrpld
Loading...
Searching...
No Matches
Ticket_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h> // IWYU pragma: keep
6#include <test/jtx/deposit.h>
7#include <test/jtx/fee.h>
8#include <test/jtx/jtx_json.h>
9#include <test/jtx/noop.h>
10#include <test/jtx/owners.h>
11#include <test/jtx/pay.h>
12#include <test/jtx/seq.h>
13#include <test/jtx/ter.h>
14#include <test/jtx/ticket.h>
15#include <test/jtx/trust.h>
16#include <test/jtx/txflags.h>
17
18#include <xrpld/app/misc/Transaction.h>
19
20#include <xrpl/basics/base_uint.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/beast/unit_test/suite.h>
23#include <xrpl/json/json_value.h>
24#include <xrpl/json/to_string.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STTx.h>
28#include <xrpl/protocol/Seed.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/TxFlags.h>
31#include <xrpl/protocol/TxFormats.h>
32#include <xrpl/protocol/TxMeta.h>
33#include <xrpl/protocol/TxSearched.h>
34#include <xrpl/protocol/jss.h>
35
36#include <algorithm>
37#include <cstdint>
38#include <memory>
39#include <optional>
40#include <stdexcept>
41#include <string>
42#include <utility>
43#include <variant>
44#include <vector>
45
46namespace xrpl {
47
49{
55 void
57 {
58 using namespace std::string_literals;
59
60 json::Value const& tx{env.tx()->getJson(JsonOptions::Values::None)};
61 {
62 std::string const txType = tx[sfTransactionType.jsonName].asString();
63
64 if (!BEAST_EXPECTS(
65 txType == jss::TicketCreate, "Unexpected TransactionType: "s + txType))
66 return;
67 }
68
69 std::uint32_t const count = {tx[sfTicketCount.jsonName].asUInt()};
70 if (!BEAST_EXPECTS(count >= 1, "Unexpected ticket count: "s + std::to_string(count)))
71 return;
72
73 std::uint32_t const txSeq = {tx[sfSequence.jsonName].asUInt()};
74 std::string const account = tx[sfAccount.jsonName].asString();
75
76 json::Value const& metadata = env.meta()->getJson(JsonOptions::Values::None);
77 if (!BEAST_EXPECTS(
78 metadata.isMember(sfTransactionResult.jsonName) &&
79 metadata[sfTransactionResult.jsonName].asString() == "tesSUCCESS",
80 "Not metadata for successful TicketCreate."))
81 return;
82
83 BEAST_EXPECT(metadata.isMember(sfAffectedNodes.jsonName));
84 BEAST_EXPECT(metadata[sfAffectedNodes.jsonName].isArray());
85
86 bool directoryChanged = false;
87 std::uint32_t acctRootFinalSeq = {0};
89 ticketSeqs.reserve(count);
90 for (json::Value const& node : metadata[sfAffectedNodes.jsonName])
91 {
92 if (node.isMember(sfModifiedNode.jsonName))
93 {
94 json::Value const& modified = node[sfModifiedNode.jsonName];
95 std::string const entryType = modified[sfLedgerEntryType.jsonName].asString();
96 if (entryType == jss::AccountRoot)
97 {
98 auto const& previousFields = modified[sfPreviousFields.jsonName];
99 auto const& finalFields = modified[sfFinalFields.jsonName];
100 {
101 // Verify the account root Sequence did the right thing.
102 std::uint32_t const prevSeq = previousFields[sfSequence.jsonName].asUInt();
103
104 acctRootFinalSeq = finalFields[sfSequence.jsonName].asUInt();
105
106 if (txSeq == 0)
107 {
108 // Transaction used a TicketSequence.
109 BEAST_EXPECT(acctRootFinalSeq == prevSeq + count);
110 }
111 else
112 {
113 // Transaction used a (plain) Sequence.
114 BEAST_EXPECT(prevSeq == txSeq);
115 BEAST_EXPECT(acctRootFinalSeq == prevSeq + count + 1);
116 }
117 }
118
119 std::uint32_t const consumedTickets = {txSeq == 0u ? 1u : 0u};
120
121 // If...
122 // 1. The TicketCount is 1 and
123 // 2. A ticket was consumed by the ticket create, then
124 // 3. The final TicketCount did not change, so the
125 // previous TicketCount is not reported.
126 // But, since the count did not change, we know it equals
127 // the final Ticket count.
128 bool const unreportedPrevTicketCount = {count == 1 && txSeq == 0};
129
130 // Verify the OwnerCount did the right thing
131 if (unreportedPrevTicketCount)
132 {
133 // The number of Tickets should not have changed, so
134 // the previous OwnerCount should not be reported.
135 BEAST_EXPECT(!previousFields.isMember(sfOwnerCount.jsonName));
136 }
137 else
138 {
139 // Verify the OwnerCount did the right thing.
140 std::uint32_t const prevCount = {
141 previousFields[sfOwnerCount.jsonName].asUInt(),
142 };
143
144 std::uint32_t const finalCount = {
145 finalFields[sfOwnerCount.jsonName].asUInt(),
146 };
147
148 BEAST_EXPECT(prevCount + count - consumedTickets == finalCount);
149 }
150
151 // Verify TicketCount metadata.
152 BEAST_EXPECT(finalFields.isMember(sfTicketCount.jsonName));
153
154 if (unreportedPrevTicketCount)
155 {
156 // The number of Tickets should not have changed, so
157 // the previous TicketCount should not be reported.
158 BEAST_EXPECT(!previousFields.isMember(sfTicketCount.jsonName));
159 }
160 else
161 {
162 // If the TicketCount was previously present it
163 // should have been greater than zero.
164 std::uint32_t const startCount = {
165 previousFields.isMember(sfTicketCount.jsonName)
166 ? previousFields[sfTicketCount.jsonName].asUInt()
167 : 0u,
168 };
169
170 BEAST_EXPECT(
171 (startCount == 0u) ^ previousFields.isMember(sfTicketCount.jsonName));
172
173 BEAST_EXPECT(
174 startCount + count - consumedTickets ==
175 finalFields[sfTicketCount.jsonName]);
176 }
177 }
178 else if (entryType == jss::DirectoryNode)
179 {
180 directoryChanged = true;
181 }
182 else
183 {
184 fail("Unexpected modified node: "s + entryType, __FILE__, __LINE__);
185 }
186 }
187 else if (node.isMember(sfCreatedNode.jsonName))
188 {
189 json::Value const& created = node[sfCreatedNode.jsonName];
190 std::string const entryType = created[sfLedgerEntryType.jsonName].asString();
191 if (entryType == jss::Ticket)
192 {
193 auto const& newFields = created[sfNewFields.jsonName];
194
195 BEAST_EXPECT(newFields[sfAccount.jsonName].asString() == account);
196 ticketSeqs.push_back(newFields[sfTicketSequence.jsonName].asUInt());
197 }
198 else if (entryType == jss::DirectoryNode)
199 {
200 directoryChanged = true;
201 }
202 else
203 {
204 fail("Unexpected created node: "s + entryType, __FILE__, __LINE__);
205 }
206 }
207 else if (node.isMember(sfDeletedNode.jsonName))
208 {
209 json::Value const& deleted = node[sfDeletedNode.jsonName];
210 std::string const entryType = deleted[sfLedgerEntryType.jsonName].asString();
211
212 if (entryType == jss::Ticket)
213 {
214 // Verify the transaction's Sequence == 0.
215 BEAST_EXPECT(txSeq == 0);
216
217 // Verify the account of the deleted ticket.
218 auto const& finalFields = deleted[sfFinalFields.jsonName];
219 BEAST_EXPECT(finalFields[sfAccount.jsonName].asString() == account);
220
221 // Verify the deleted ticket has the right TicketSequence.
222 BEAST_EXPECT(
223 finalFields[sfTicketSequence.jsonName].asUInt() ==
224 tx[sfTicketSequence.jsonName].asUInt());
225 }
226 }
227 else
228 {
229 fail("Unexpected node type in TicketCreate metadata.", __FILE__, __LINE__);
230 }
231 }
232 BEAST_EXPECT(directoryChanged);
233
234 // Verify that all the expected Tickets were created.
235 BEAST_EXPECT(ticketSeqs.size() == count);
236 std::ranges::sort(ticketSeqs);
237 BEAST_EXPECT(std::ranges::adjacent_find(ticketSeqs) == ticketSeqs.end());
238 BEAST_EXPECT(*ticketSeqs.rbegin() == acctRootFinalSeq - 1);
239 }
240
248 void
250 {
251 json::Value const& tx{env.tx()->getJson(JsonOptions::Values::None)};
252
253 // Verify that the transaction includes a TicketSequence.
254
255 // Capture that TicketSequence.
256 // Capture the Account from the transaction
257
258 // Verify that metadata indicates a tec or a tesSUCCESS.
259
260 // Walk affected nodes:
261 //
262 // For each deleted node, see if it is a Ticket node. If it is
263 // a Ticket Node being deleted, then assert that the...
264 //
265 // Account == the transaction Account &&
266 // TicketSequence == the transaction TicketSequence
267 //
268 // If a modified node is an AccountRoot, see if it is the transaction
269 // Account. If it is then verify the TicketCount decreased by one.
270 // If the old TicketCount was 1, then the TicketCount field should be
271 // removed from the final fields of the AccountRoot.
272 //
273 // After looking at all nodes verify that exactly one Ticket node
274 // was deleted.
275 BEAST_EXPECT(tx[sfSequence.jsonName].asUInt() == 0);
276 std::string const account{tx[sfAccount.jsonName].asString()};
277 if (!BEAST_EXPECTS(
278 tx.isMember(sfTicketSequence.jsonName),
279 "Not metadata for a ticket consuming transaction."))
280 return;
281
282 std::uint32_t const ticketSeq{tx[sfTicketSequence.jsonName].asUInt()};
283
284 json::Value const& metadata{env.meta()->getJson(JsonOptions::Values::None)};
285 if (!BEAST_EXPECTS(
286 metadata.isMember(sfTransactionResult.jsonName),
287 "Metadata is missing TransactionResult."))
288 return;
289
290 {
291 std::string const transactionResult{metadata[sfTransactionResult.jsonName].asString()};
292 if (!BEAST_EXPECTS(
293 transactionResult == "tesSUCCESS" ||
294 transactionResult.compare(0, 3, "tec") == 0,
295 transactionResult + " neither tesSUCCESS nor tec"))
296 return;
297 }
298
299 BEAST_EXPECT(metadata.isMember(sfAffectedNodes.jsonName));
300 BEAST_EXPECT(metadata[sfAffectedNodes.jsonName].isArray());
301
302 bool acctRootFound{false};
303 std::uint32_t acctRootSeq{0};
304 int ticketsRemoved{0};
305 for (json::Value const& node : metadata[sfAffectedNodes.jsonName])
306 {
307 if (node.isMember(sfModifiedNode.jsonName))
308 {
309 json::Value const& modified{node[sfModifiedNode.jsonName]};
310 std::string const entryType = modified[sfLedgerEntryType.jsonName].asString();
311 if (entryType == "AccountRoot" &&
312 modified[sfFinalFields.jsonName][sfAccount.jsonName].asString() == account)
313 {
314 acctRootFound = true;
315
316 auto const& previousFields = modified[sfPreviousFields.jsonName];
317 auto const& finalFields = modified[sfFinalFields.jsonName];
318
319 acctRootSeq = finalFields[sfSequence.jsonName].asUInt();
320
321 // Check that the TicketCount was present and decremented
322 // by 1. If it decremented to zero, then the field should
323 // be gone.
324 if (!BEAST_EXPECTS(
325 previousFields.isMember(sfTicketCount.jsonName),
326 "AccountRoot previous is missing TicketCount"))
327 return;
328
329 std::uint32_t const prevTicketCount =
330 previousFields[sfTicketCount.jsonName].asUInt();
331
332 BEAST_EXPECT(prevTicketCount > 0);
333 if (prevTicketCount == 1)
334 {
335 BEAST_EXPECT(!finalFields.isMember(sfTicketCount.jsonName));
336 }
337 else
338 {
339 BEAST_EXPECT(
340 finalFields.isMember(sfTicketCount.jsonName) &&
341 finalFields[sfTicketCount.jsonName].asUInt() == prevTicketCount - 1);
342 }
343 }
344 }
345 else if (node.isMember(sfDeletedNode.jsonName))
346 {
347 json::Value const& deleted{node[sfDeletedNode.jsonName]};
348 std::string const entryType{deleted[sfLedgerEntryType.jsonName].asString()};
349
350 if (entryType == jss::Ticket)
351 {
352 // Verify the account of the deleted ticket.
353 BEAST_EXPECT(
354 deleted[sfFinalFields.jsonName][sfAccount.jsonName].asString() == account);
355
356 // Verify the deleted ticket has the right TicketSequence.
357 BEAST_EXPECT(
358 deleted[sfFinalFields.jsonName][sfTicketSequence.jsonName].asUInt() ==
359 ticketSeq);
360
361 ++ticketsRemoved;
362 }
363 }
364 }
365 BEAST_EXPECT(acctRootFound);
366 BEAST_EXPECT(ticketsRemoved == 1);
367 BEAST_EXPECT(ticketSeq < acctRootSeq);
368 }
369
370 void
372 {
373 testcase("Create Tickets that fail Preflight");
374
375 using namespace test::jtx;
376 Env env{*this};
377
378 Account const master{env.master};
379
380 // Exercise boundaries on count.
381 env(ticket::create(master, 0), Ter(temINVALID_COUNT));
382 env(ticket::create(master, 251), Ter(temINVALID_COUNT));
383
384 // Exercise fees.
385 std::uint32_t const ticketSeqA{env.seq(master) + 1};
386 env(ticket::create(master, 1), Fee(XRP(10)));
388 env.close();
389 env.require(Owners(master, 1), Tickets(master, 1));
390
391 env(ticket::create(master, 1), Fee(XRP(-1)), Ter(temBAD_FEE));
392
393 // Exercise flags.
394 std::uint32_t const ticketSeqB{env.seq(master) + 1};
395 env(ticket::create(master, 1), Txflags(tfFullyCanonicalSig));
397 env.close();
398 env.require(Owners(master, 2), Tickets(master, 2));
399
400 env(ticket::create(master, 1), Txflags(tfSell), Ter(temINVALID_FLAG));
401 env.close();
402 env.require(Owners(master, 2), Tickets(master, 2));
403
404 // We successfully created 1 ticket earlier. Verify that we can
405 // create 250 tickets in one shot. We must consume one ticket first.
406 env(noop(master), ticket::Use(ticketSeqA));
408 env.close();
409 env.require(Owners(master, 1), Tickets(master, 1));
410
411 env(ticket::create(master, 250), ticket::Use(ticketSeqB));
413 env.close();
414 env.require(Owners(master, 250), Tickets(master, 250));
415 }
416
417 void
419 {
420 testcase("Create Tickets that fail Preclaim");
421
422 using namespace test::jtx;
423 {
424 // Create tickets on a non-existent account.
425 Env env{*this};
426 Account const alice{"alice"};
427 env.memoize(alice);
428
429 env(ticket::create(alice, 1), Json(jss::Sequence, 1), Ter(terNO_ACCOUNT));
430 }
431 {
432 // Exceed the threshold where tickets can no longer be
433 // added to an account.
434 Env env{*this};
435 Account const alice{"alice"};
436
437 env.fund(XRP(100000), alice);
438
439 std::uint32_t const ticketSeq{env.seq(alice) + 1};
440 env(ticket::create(alice, 250));
442 env.close();
443 env.require(Owners(alice, 250), Tickets(alice, 250));
444
445 // Note that we can add one more ticket while consuming a ticket
446 // because the final result is still 250 tickets.
447 env(ticket::create(alice, 1), ticket::Use(ticketSeq + 0));
449 env.close();
450 env.require(Owners(alice, 250), Tickets(alice, 250));
451
452 // Adding one more ticket will exceed the threshold.
453 env(ticket::create(alice, 2), ticket::Use(ticketSeq + 1), Ter(tecDIR_FULL));
454 env.close();
455 env.require(Owners(alice, 249), Tickets(alice, 249));
456
457 // Now we can successfully add one more ticket.
458 env(ticket::create(alice, 2), ticket::Use(ticketSeq + 2));
460 env.close();
461 env.require(Owners(alice, 250), Tickets(alice, 250));
462
463 // Since we're at 250, we can't add another ticket using a
464 // sequence.
465 env(ticket::create(alice, 1), Ter(tecDIR_FULL));
466 env.close();
467 env.require(Owners(alice, 250), Tickets(alice, 250));
468 }
469 {
470 // Explore exceeding the ticket threshold from another angle.
471 Env env{*this};
472 Account const alice{"alice"};
473
474 env.fund(XRP(100000), alice);
475 env.close();
476
477 std::uint32_t const ticketSeqAb{env.seq(alice) + 1};
478 env(ticket::create(alice, 2));
480 env.close();
481 env.require(Owners(alice, 2), Tickets(alice, 2));
482
483 // Adding 250 tickets (while consuming one) will exceed the
484 // threshold.
485 env(ticket::create(alice, 250), ticket::Use(ticketSeqAb + 0), Ter(tecDIR_FULL));
486 env.close();
487 env.require(Owners(alice, 1), Tickets(alice, 1));
488
489 // Adding 250 tickets (without consuming one) will exceed the
490 // threshold.
491 env(ticket::create(alice, 250), Ter(tecDIR_FULL));
492 env.close();
493 env.require(Owners(alice, 1), Tickets(alice, 1));
494
495 // Alice can now add 250 tickets while consuming one.
496 env(ticket::create(alice, 250), ticket::Use(ticketSeqAb + 1));
498 env.close();
499 env.require(Owners(alice, 250), Tickets(alice, 250));
500 }
501 }
502
503 void
505 {
506 testcase("Create Ticket Insufficient Reserve");
507
508 using namespace test::jtx;
509 Env env{*this};
510 Account const alice{"alice"};
511
512 // Fund alice not quite enough to make the reserve for a Ticket.
513 env.fund(env.current()->fees().accountReserve(1, 1) - drops(1), alice);
514 env.close();
515
516 env(ticket::create(alice, 1), Ter(tecINSUFFICIENT_RESERVE));
517 env.close();
518 env.require(Owners(alice, 0), Tickets(alice, 0));
519
520 // Give alice enough to exactly meet the reserve for one Ticket.
521 env(pay(
522 env.master, alice, env.current()->fees().accountReserve(1, 1) - env.balance(alice)));
523 env.close();
524
525 env(ticket::create(alice, 1));
527 env.close();
528 env.require(Owners(alice, 1), Tickets(alice, 1));
529
530 // Give alice not quite enough to make the reserve for a total of
531 // 250 Tickets.
532 env(
533 pay(env.master,
534 alice,
535 env.current()->fees().accountReserve(250, 1) - drops(1) - env.balance(alice)));
536 env.close();
537
538 // alice doesn't quite have the reserve for a total of 250
539 // Tickets, so the transaction fails.
540 env(ticket::create(alice, 249), Ter(tecINSUFFICIENT_RESERVE));
541 env.close();
542 env.require(Owners(alice, 1), Tickets(alice, 1));
543
544 // Give alice enough so she can make the reserve for all 250
545 // Tickets.
546 env(pay(
547 env.master, alice, env.current()->fees().accountReserve(250, 1) - env.balance(alice)));
548 env.close();
549
550 std::uint32_t const ticketSeq{env.seq(alice) + 1};
551 env(ticket::create(alice, 249));
553 env.close();
554 env.require(Owners(alice, 250), Tickets(alice, 250));
555 BEAST_EXPECT(ticketSeq + 249 == env.seq(alice));
556 }
557
558 void
560 {
561 testcase("Using Tickets");
562
563 using namespace test::jtx;
564 Env env{*this};
565 Account const alice{"alice"};
566
567 env.fund(XRP(10000), alice);
568 env.close();
569
570 // Successfully create tickets (using a sequence)
571 std::uint32_t const ticketSeqAb{env.seq(alice) + 1};
572 env(ticket::create(alice, 2));
574 env.close();
575 env.require(Owners(alice, 2), Tickets(alice, 2));
576 BEAST_EXPECT(ticketSeqAb + 2 == env.seq(alice));
577
578 // You can use a ticket to create one ticket ...
579 std::uint32_t const ticketSeqC{env.seq(alice)};
580 env(ticket::create(alice, 1), ticket::Use(ticketSeqAb + 0));
582 env.close();
583 env.require(Owners(alice, 2), Tickets(alice, 2));
584 BEAST_EXPECT(ticketSeqC + 1 == env.seq(alice));
585
586 // ... you can use a ticket to create multiple tickets ...
587 std::uint32_t const ticketSeqDe{env.seq(alice)};
588 env(ticket::create(alice, 2), ticket::Use(ticketSeqAb + 1));
590 env.close();
591 env.require(Owners(alice, 3), Tickets(alice, 3));
592 BEAST_EXPECT(ticketSeqDe + 2 == env.seq(alice));
593
594 // ... and you can use a ticket for other things.
595 env(noop(alice), ticket::Use(ticketSeqDe + 0));
597 env.close();
598 env.require(Owners(alice, 2), Tickets(alice, 2));
599 BEAST_EXPECT(ticketSeqDe + 2 == env.seq(alice));
600
601 env(pay(alice, env.master, XRP(20)), ticket::Use(ticketSeqDe + 1));
603 env.close();
604 env.require(Owners(alice, 1), Tickets(alice, 1));
605 BEAST_EXPECT(ticketSeqDe + 2 == env.seq(alice));
606
607 env(trust(alice, env.master["USD"](20)), ticket::Use(ticketSeqC));
609 env.close();
610 env.require(Owners(alice, 1), Tickets(alice, 0));
611 BEAST_EXPECT(ticketSeqDe + 2 == env.seq(alice));
612
613 // Attempt to use a ticket that has already been used.
614 env(noop(alice), ticket::Use(ticketSeqC), Ter(tefNO_TICKET));
615 env.close();
616
617 // Attempt to use a ticket from the future.
618 std::uint32_t const ticketSeqF{env.seq(alice) + 1};
619 env(noop(alice), ticket::Use(ticketSeqF), Ter(terPRE_TICKET));
620 env.close();
621
622 // Now create the ticket. The retry will consume the new ticket.
623 env(ticket::create(alice, 1));
625 env.close();
626 env.require(Owners(alice, 1), Tickets(alice, 0));
627 BEAST_EXPECT(ticketSeqF + 1 == env.seq(alice));
628
629 // Try a transaction that combines consuming a ticket with
630 // AccountTxnID.
631 std::uint32_t const ticketSeqG{env.seq(alice) + 1};
632 env(ticket::create(alice, 1));
634 env.close();
635
636 env(noop(alice),
637 ticket::Use(ticketSeqG),
638 Json(R"({"AccountTxnID": "0"})"),
639 Ter(temINVALID));
640 env.close();
641 env.require(Owners(alice, 2), Tickets(alice, 1));
642 }
643
644 void
646 {
647 // The Transaction database keeps each transaction's sequence number
648 // in an entry (called "FromSeq"). Until the introduction of tickets
649 // each sequence stored for a given account would always be unique.
650 // With the advent of tickets there could be lots of entries
651 // with zero.
652 //
653 // We really don't expect those zeros to cause any problems since
654 // there are no indexes that use "FromSeq". But it still seems
655 // prudent to exercise this a bit to see if tickets cause any obvious
656 // harm.
657 testcase("Transaction Database With Tickets");
658
659 using namespace test::jtx;
660 Env env{*this};
661 Account const alice{"alice"};
662
663 env.fund(XRP(10000), alice);
664 env.close();
665
666 // Lambda that returns the hash of the most recent transaction.
667 auto const getTxID = [&env, this] -> UInt256 {
668 std::shared_ptr<STTx const> const tx{env.tx()};
669 if (!BEAST_EXPECTS(tx, "Transaction not found"))
670 Throw<std::invalid_argument>("Invalid transaction ID");
671
672 return tx->getTransactionID();
673 };
674
675 // A note about the metadata created by these transactions.
676 //
677 // We _could_ check the metadata on these transactions. However
678 // checking the metadata has the side effect of advancing the ledger.
679 // So if we check the metadata we don't get to look at several
680 // transactions in the same ledger. Therefore a specific choice was
681 // made to not check the metadata on these transactions.
682
683 // Successfully create several tickets (using a sequence).
684 std::uint32_t ticketSeq{env.seq(alice)};
685 static constexpr std::uint32_t kTicketCount{10};
686 env(ticket::create(alice, kTicketCount));
687 UInt256 const txHash1{getTxID()};
688
689 // Just for grins use the tickets in reverse from largest to smallest.
690 ticketSeq += kTicketCount;
691 env(noop(alice), ticket::Use(--ticketSeq));
692 UInt256 const txHash2{getTxID()};
693
694 env(pay(alice, env.master, XRP(200)), ticket::Use(--ticketSeq));
695 UInt256 const txHash3{getTxID()};
696
697 env(deposit::auth(alice, env.master), ticket::Use(--ticketSeq));
698 UInt256 const txHash4{getTxID()};
699
700 // Close the ledger so we look at transactions from a couple of
701 // different ledgers.
702 env.close();
703
704 env(pay(alice, env.master, XRP(300)), ticket::Use(--ticketSeq));
705 UInt256 const txHash5{getTxID()};
706
707 env(pay(alice, env.master, XRP(400)), ticket::Use(--ticketSeq));
708 UInt256 const txHash6{getTxID()};
709
710 env(deposit::unauth(alice, env.master), ticket::Use(--ticketSeq));
711 UInt256 const txHash7{getTxID()};
712
713 env(noop(alice), ticket::Use(--ticketSeq));
714 UInt256 const txHash8{getTxID()};
715
716 env.close();
717
718 // Checkout what's in the Transaction database. We go straight
719 // to the database. Most of our interfaces cache transactions
720 // in memory. So if we use normal interfaces we would get the
721 // transactions from memory rather than from the database.
722
723 // Lambda to verify a transaction pulled from the Transaction database.
724 auto const checkTxFromDB = [&env, this](
725 UInt256 const& txID,
726 std::uint32_t ledgerSeq,
727 std::uint32_t txSeq,
729 TxType txType) {
730 ErrorCodeI txErrCode{RpcSuccess};
731
734 Transaction::load(txID, env.app(), txErrCode);
735
736 BEAST_EXPECT(txErrCode == RpcSuccess);
737 if (auto txPtr = std::get_if<TxPair>(&maybeTx))
738 {
739 std::shared_ptr<Transaction> const& tx = txPtr->first;
740 BEAST_EXPECT(tx->getLedger() == ledgerSeq);
741 std::shared_ptr<STTx const> const& sttx = tx->getSTransaction();
742 BEAST_EXPECT((*sttx)[sfSequence] == txSeq);
743 if (ticketSeq)
744 BEAST_EXPECT((*sttx)[sfTicketSequence] == *ticketSeq);
745 BEAST_EXPECT((*sttx)[sfTransactionType] == txType);
746 }
747 else
748 {
749 fail("Expected transaction was not found");
750 }
751 };
752
753 // txID ledgerSeq txSeq ticketSeq txType
754 checkTxFromDB(txHash1, 4, 4, {}, ttTICKET_CREATE);
755 checkTxFromDB(txHash2, 4, 0, 13, ttACCOUNT_SET);
756 checkTxFromDB(txHash3, 4, 0, 12, ttPAYMENT);
757 checkTxFromDB(txHash4, 4, 0, 11, ttDEPOSIT_PREAUTH);
758
759 checkTxFromDB(txHash5, 5, 0, 10, ttPAYMENT);
760 checkTxFromDB(txHash6, 5, 0, 9, ttPAYMENT);
761 checkTxFromDB(txHash7, 5, 0, 8, ttDEPOSIT_PREAUTH);
762 checkTxFromDB(txHash8, 5, 0, 7, ttACCOUNT_SET);
763 }
764
765 void
767 {
768 // The sign and the submit RPC commands automatically fill in the
769 // Sequence field of a transaction if kNone is provided. If a
770 // TicketSequence is provided in the transaction, then the
771 // auto-filled Sequence should be zero.
772 testcase("Sign with TicketSequence");
773
774 using namespace test::jtx;
775 Env env{*this};
776 Account const alice{"alice"};
777
778 env.fund(XRP(10000), alice);
779 env.close();
780
781 // Successfully create tickets (using a sequence)
782 std::uint32_t const ticketSeq = env.seq(alice) + 1;
783 env(ticket::create(alice, 2));
785 env.close();
786 env.require(Owners(alice, 2), Tickets(alice, 2));
787 BEAST_EXPECT(ticketSeq + 2 == env.seq(alice));
788
789 {
790 // Test that the "sign" RPC command fills in a "Sequence": 0 field
791 // if kNone is provided.
792
793 // Create a noop transaction using a TicketSequence but don't fill
794 // in the Sequence field.
796 tx[jss::tx_json] = noop(alice);
797 tx[jss::tx_json][sfTicketSequence.jsonName] = ticketSeq;
798 tx[jss::secret] = toBase58(generateSeed("alice"));
799
800 // Verify that there is no "Sequence" field.
801 BEAST_EXPECT(!tx[jss::tx_json].isMember(sfSequence.jsonName));
802
803 // Call the "sign" RPC command and see the "Sequence": 0 field
804 // filled in.
805 json::Value jr = env.rpc("json", "sign", to_string(tx));
806
807 // Verify that "sign" inserted a "Sequence": 0 field.
808 if (BEAST_EXPECT(jr[jss::result][jss::tx_json].isMember(sfSequence.jsonName)))
809 {
810 BEAST_EXPECT(jr[jss::result][jss::tx_json][sfSequence.jsonName] == 0);
811 }
812
813 // "sign" should not have consumed any of alice's tickets.
814 env.close();
815 env.require(Owners(alice, 2), Tickets(alice, 2));
816
817 // "submit" the signed blob and see one of alice's tickets consumed.
818 env.rpc("submit", jr[jss::result][jss::tx_blob].asString());
819 env.close();
820 env.require(Owners(alice, 1), Tickets(alice, 1));
821 }
822 {
823 // Test that the "submit" RPC command fills in a "Sequence": 0
824 // field if kNone is provided.
825
826 // Create a noop transaction using a TicketSequence but don't fill
827 // in the Sequence field.
829 tx[jss::tx_json] = noop(alice);
830 tx[jss::tx_json][sfTicketSequence.jsonName] = ticketSeq + 1;
831 tx[jss::secret] = toBase58(generateSeed("alice"));
832
833 // Verify that there is no "Sequence" field.
834 BEAST_EXPECT(!tx[jss::tx_json].isMember(sfSequence.jsonName));
835
836 // Call the "submit" RPC command and see the "Sequence": 0 field
837 // filled in.
838 json::Value jr = env.rpc("json", "submit", to_string(tx));
839
840 // Verify that "submit" inserted a "Sequence": 0 field.
841 if (BEAST_EXPECT(jr[jss::result][jss::tx_json].isMember(sfSequence.jsonName)))
842 {
843 BEAST_EXPECT(jr[jss::result][jss::tx_json][sfSequence.jsonName] == 0);
844 }
845
846 // "submit" should have consumed the last of alice's tickets.
847 env.close();
848 env.require(Owners(alice, 0), Tickets(alice, 0));
849 }
850 }
851
852 void
854 {
855 using namespace test::jtx;
856
857 // It is an error if a transaction contains a non-zero Sequence field
858 // and a TicketSequence field. Verify that the error is detected.
859 testcase("Fix both Seq and Ticket");
860
861 Env env{*this, testableAmendments()};
862 Account const alice{"alice"};
863
864 env.fund(XRP(10000), alice);
865 env.close();
866
867 // Create a ticket.
868 std::uint32_t const ticketSeq = env.seq(alice) + 1;
869 env(ticket::create(alice, 1));
870 env.close();
871 env.require(Owners(alice, 1), Tickets(alice, 1));
872 BEAST_EXPECT(ticketSeq + 1 == env.seq(alice));
873
874 // Create a transaction that includes both a ticket and a non-zero
875 // sequence number. The transaction fails with temSEQ_AND_TICKET.
876 env(noop(alice), ticket::Use(ticketSeq), Seq(env.seq(alice)), Ter(temSEQ_AND_TICKET));
877 env.close();
878
879 // Verify that the transaction failed by looking at alice's
880 // sequence number and tickets.
881 env.require(Owners(alice, 1), Tickets(alice, 1));
882 BEAST_EXPECT(ticketSeq + 1 == env.seq(alice));
883 }
884
885public:
886 void
897};
898
900
901} // namespace xrpl
T adjacent_find(T... args)
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
bool isArray() const
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
void testTicketCreatePreflightFail()
void testTicketInsufficientReserve()
void run() override
Runs the suite.
void checkTicketConsumeMeta(test::jtx::Env &env)
Validate metadata for a ticket using transaction.
void checkTicketCreateMeta(test::jtx::Env &env)
Validate metadata for a successful TicketCreate transaction.
void testTransactionDatabaseWithTickets()
void testFixBothSeqAndTicket()
void testSignWithTicketSequence()
void testTicketCreatePreclaimFail()
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(UInt256 const &id, Application &app, ErrorCodeI &ec)
A transaction testing environment.
Definition Env.h:161
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:560
T compare(T... args)
T end(T... args)
T get_if(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_ACCOUNT
Definition TER.h:219
@ terPRE_TICKET
Definition TER.h:228
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcSuccess
Definition ErrorCodes.h:27
TxType
Transaction type identifiers.
Definition TxFormats.h:45
@ tefNO_TICKET
Definition TER.h:179
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ temBAD_FEE
Definition TER.h:80
@ temINVALID
Definition TER.h:98
@ temINVALID_FLAG
Definition TER.h:99
@ temINVALID_COUNT
Definition TER.h:109
@ temSEQ_AND_TICKET
Definition TER.h:114
@ tecDIR_FULL
Definition TER.h:298
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
constexpr FlagValue tfFullyCanonicalSig
Definition TxFlags.h:43
T push_back(T... args)
T rbegin(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
T to_string(T... args)