xrpld
Loading...
Searching...
No Matches
TransactionProposalCancel_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/batch.h>
6#include <test/jtx/delegate.h>
7#include <test/jtx/fee.h>
8#include <test/jtx/multisign.h>
9#include <test/jtx/noop.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/proposal.h>
12#include <test/jtx/sig.h>
13#include <test/jtx/sponsor.h>
14#include <test/jtx/ter.h>
15#include <test/jtx/ticket.h>
16#include <test/jtx/txflags.h>
17
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/basics/chrono.h>
20#include <xrpl/beast/unit_test/suite.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/json/to_string.h>
23#include <xrpl/protocol/ErrorCodes.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/Indexes.h>
26#include <xrpl/protocol/Protocol.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/SeqProxy.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/TxFlags.h>
31#include <xrpl/protocol/jss.h>
32
33#include <chrono>
34#include <cstdint>
35#include <optional>
36#include <string>
37#include <utility>
38
39namespace xrpl::test {
40
42{
43 // Create one ticket for the target and a live proposal keyed to it,
44 // returning the proposal's ID. The proposal is the proposer's and its
45 // proposed transaction pays dest.
48 jtx::Env& env,
49 jtx::Account const& proposer,
50 jtx::Account const& target,
51 jtx::Account const& dest,
52 std::optional<json::Value> payloadOverride = std::nullopt)
53 {
54 using namespace jtx;
55 using namespace std::chrono_literals;
56
57 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
58
59 json::Value const payload = payloadOverride
60 ? *payloadOverride
61 : proposal::unsignedPayload(env, pay(target, dest, XRP(1)), ticketSeq);
62
63 if (proposer.id() != target.id() && !env.le(keylet::signerList(target.id())))
64 proposal::authorizeProposer(env, target, proposer);
65
66 env(proposal::create(proposer, payload, proposal::expiration(env, 1000s)));
67 env.close();
68
69 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
70 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
71 return proposalID;
72 }
73
74 // Nothing about the transaction is available before the amendment is
75 // active.
76 void
78 {
79 testcase("amendment disabled");
80
81 using namespace jtx;
82
83 Env env{*this, features - featureCosign};
84
85 Account const alice{"alice"};
86 env.fund(XRP(10000), alice);
87 env.close();
88
89 env(proposal::cancel(alice, uint256{1}), Ter(temDISABLED));
90 env.close();
91 }
92
93 // Static rejections: a zero ProposalID identifies nothing, and the
94 // transaction defines no flags of its own.
95 void
97 {
98 testcase("malformed transaction");
99
100 using namespace jtx;
101
102 Env env{*this, features};
103
104 Account const alice{"alice"};
105 env.fund(XRP(10000), alice);
106 env.close();
107
108 env(proposal::cancel(alice, uint256{}), Ter(temMALFORMED));
109 env.close();
110
111 env(proposal::cancel(alice, uint256{1}), Txflags(0x00010000), Ter(temINVALID_FLAG));
112 env.close();
113 }
114
115 // The named proposal must exist as a TransactionProposal entry:
116 // (1) an ID that matches no object
117 // (2) an entry of a different type
118 // (3) A previously cancelled ProposalID
119 // All the above cases will result in tecNO_ENTRY
120 void
122 {
123 testcase("proposal does not exist");
124
125 using namespace jtx;
126 using namespace std::chrono_literals;
127
128 Env env{*this, features};
129
130 Account const alice{"alice"};
131 Account const target{"target"};
132 env.fund(XRP(10000), alice, target);
133 env.close();
134 proposal::authorizeProposer(env, target, alice);
135
136 // No such entry at all.
137 env(proposal::cancel(alice, uint256{1}), Ter(tecNO_ENTRY));
138 env.close();
139
140 // The ID of a live ledger entry of a different type (the ticket the
141 // proposal below will consume) does not name a proposal.
142 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
144 alice, keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq)).key),
146 env.close();
147
148 // A proposal that was already cancelled is gone: a second cancel —
149 // even by its Owner — finds nothing.
151 alice,
152 proposal::unsignedPayload(env, pay(target, alice, XRP(1)), ticketSeq),
153 proposal::expiration(env, 1000s)));
154 env.close();
155 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
156
157 env(proposal::cancel(alice, proposalID));
158 env.close();
159 env(proposal::cancel(alice, proposalID), Ter(tecNO_ENTRY));
160 env.close();
161 }
162
163 // The Owner may cancel a live proposal. Cancellation deletes the entry,
164 // releases the Owner's reserve, and frees the (target, ticket) slot — but
165 // leaves the target's Ticket itself intact and usable.
166 void
168 {
169 testcase("owner cancels live proposal");
170
171 using namespace jtx;
172 using namespace std::chrono_literals;
173
174 Env env{*this, features};
175
176 Account const alice{"alice"};
177 Account const target{"target"};
178 Account const bob{"bob"};
179 env.fund(XRP(10000), alice, target, bob);
180 env.close();
181
182 std::uint32_t const ticketSeq = env.seq(target) + 1;
183 uint256 const proposalID = makeProposal(env, alice, target, bob);
184 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
185
186 env(proposal::cancel(alice, proposalID));
187 env.close();
188
189 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
190 BEAST_EXPECT(ownerCount(env, alice) == 0);
191
192 // The ticket the proposed transaction would have consumed still
193 // belongs to the target.
194 BEAST_EXPECT(env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
195 BEAST_EXPECT(ownerCount(env, target) == 2);
196
197 // The (target, ticket) slot is free again: a new proposal for the
198 // same pair is not a duplicate.
200 alice,
201 proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq),
202 proposal::expiration(env, 1000s)));
203 env.close();
204 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
205 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
206
207 // And once cancelled again, the target may spend the ticket on
208 // something else entirely (which is also what positively revokes a
209 // proposal: cancellation alone does not invalidate signatures an
210 // observer may have copied, On-Chain Cosigner spec §13.4).
211 env(proposal::cancel(alice, proposalID));
212 env.close();
213 env(noop(target), ticket::Use(ticketSeq));
214 env.close();
215 BEAST_EXPECT(!env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
216 }
217
218 // The target account may refuse any proposal made for it, at any point in
219 // its lifecycle, without owning the object: any of its signers, an
220 // authorized delegate, or that delegate's signers can create one and tie
221 // up one of its tickets (XLS-0103 §7.2).
222 void
224 {
225 testcase("target cancels live proposal");
226
227 using namespace jtx;
228
229 Env env{*this, features};
230
231 Account const alice{"alice"};
232 Account const target{"target"};
233 Account const bob{"bob"};
234 env.fund(XRP(10000), alice, target, bob);
235 env.close();
236
237 uint256 const proposalID = makeProposal(env, alice, target, bob);
238
239 env(proposal::cancel(target, proposalID));
240 env.close();
241
242 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
243 // The reserve was the proposer's, and cancellation returns it to the
244 // proposer even when someone else cancels.
245 BEAST_EXPECT(ownerCount(env, alice) == 0);
246
247 // The target account still holds the SignerList plus one unused ticket.
248 BEAST_EXPECT(ownerCount(env, target) == 2);
249 }
250
251 // An account may propose against itself, collapsing the Owner and the
252 // target into one account. Two things follow that no other case in this
253 // suite reaches. Create skips its authorization check entirely for a
254 // self-proposal, so no SignerList is involved anywhere; and the account
255 // holding the reserve is also the account being granted permission, so the
256 // release has to happen once rather than once per satisfied arm of that
257 // test — the owner count is the only thing that would show the difference.
258 void
260 {
261 testcase("target cancels a proposal it made for itself");
262
263 using namespace jtx;
264 using namespace std::chrono_literals;
265
266 Env env{*this, features};
267
268 Account const target{"target"}; // both the proposer and the target
269 Account const bob{"bob"};
270 env.fund(XRP(10000), target, bob);
271 env.close();
272
273 std::uint32_t const ticketSeq = env.seq(target) + 1;
274 uint256 const proposalID = makeProposal(env, target, target, bob);
275
276 // Nothing authorized this proposer but its own identity.
277 BEAST_EXPECT(!env.le(keylet::signerList(target.id())));
278
279 auto const sle = env.le(keylet::txProposal(proposalID));
280 if (!BEAST_EXPECT(sle))
281 return;
282 BEAST_EXPECT(sle->getAccountID(sfOwner) == target.id());
283 BEAST_EXPECT(
284 sle->getFieldObject(sfProposedTransaction).getAccountID(sfAccount) == target.id());
285
286 // The ticket and the proposal's increments are on the same account.
287 BEAST_EXPECT(ownerCount(env, target) == 1 + proposal::kProposalOwnerCount);
288
289 env(proposal::cancel(target, proposalID));
290 env.close();
291
292 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
293 // Released exactly once: the unused ticket is all that is left, and it
294 // is still the target's to spend.
295 BEAST_EXPECT(ownerCount(env, target) == 1);
296 BEAST_EXPECT(env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
297 }
298
299 // When the proposed transaction is initiated by a Delegate, that delegate
300 // is a target account too (On-Chain Cosigner spec §7.2): the proposal seeks its
301 // authorization, so it may refuse.
302 void
304 {
305 testcase("proposed transaction's delegate cancels");
306
307 using namespace jtx;
308
309 Env env{*this, features};
310
311 Account const alice{"alice"};
312 Account const target{"target"};
313 Account const carol{"carol"}; // the proposed transaction's Delegate
314 Account const bob{"bob"};
315 env.fund(XRP(10000), alice, target, carol, bob);
316 env.close();
317
318 auto const delegatedPayload = [&](std::uint32_t ticketSeq) {
319 json::Value payload =
320 proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq);
321 payload[sfDelegate.getJsonName()] = carol.human();
322 return payload;
323 };
324
325 // Naming carol in the payload is the whole of her claim: preclaim
326 // reads the Delegate as-is and never asks whether the delegation
327 // exists. Granting one here would make the cases below pass for a
328 // second reason and stop covering that, so pin its absence.
329 BEAST_EXPECT(!env.le(keylet::delegate(target.id(), carol.id())));
330
331 // The Delegate named in the payload may cancel.
332 {
333 std::uint32_t const ticketSeq = env.seq(target) + 1;
334 uint256 const proposalID =
335 makeProposal(env, alice, target, bob, delegatedPayload(ticketSeq));
336
337 env(proposal::cancel(carol, proposalID));
338 env.close();
339
340 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
341 BEAST_EXPECT(ownerCount(env, alice) == 0);
342 }
343
344 // A Delegate widens, not replaces, the target: the proposed
345 // transaction's Account may still cancel.
346 {
347 std::uint32_t const ticketSeq = env.seq(target) + 1;
348 uint256 const proposalID =
349 makeProposal(env, alice, target, bob, delegatedPayload(ticketSeq));
350
351 env(proposal::cancel(target, proposalID));
352 env.close();
353
354 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
355 BEAST_EXPECT(ownerCount(env, alice) == 0);
356 }
357 }
358
359 // While the proposal is live, nobody else may cancel it — not even the
360 // destination of the proposed payment (unlike a Check, whose destination
361 // may cancel it).
362 void
364 {
365 testcase("third party cannot cancel live proposal");
366
367 using namespace jtx;
368
369 Env env{*this, features};
370
371 Account const alice{"alice"};
372 Account const target{"target"};
373 Account const bob{"bob"}; // destination of the proposed payment
374 env.fund(XRP(10000), alice, target, bob);
375 env.close();
376
377 uint256 const proposalID = makeProposal(env, alice, target, bob);
378
379 env(proposal::cancel(bob, proposalID), Ter(tecNO_PERMISSION));
380 env.close();
381
382 // The proposal survives and the Owner's reserve is still held.
383 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
384 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
385 }
386
387 // Once the proposal's Expiration has passed it is terminal: closed to new
388 // signatures, so anyone may delete it and release the Owner's reserve.
389 void
391 {
392 testcase("anyone cancels expired proposal");
393
394 using namespace jtx;
395 using namespace std::chrono_literals;
396
397 Env env{*this, features};
398
399 Account const alice{"alice"};
400 Account const target{"target"};
401 Account const bob{"bob"};
402 env.fund(XRP(10000), alice, target, bob);
403 env.close();
404 proposal::authorizeProposer(env, target, alice);
405
406 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
407
408 // A LastLedgerSequence far beyond this test keeps that arm of the
409 // terminal rule inert: only the Expiration arm may fire here.
410 json::Value payload = proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq);
411 payload[sfLastLedgerSequence.getJsonName()] = env.current()->seq() + 1000;
412
413 env(proposal::create(alice, payload, proposal::expiration(env, 100s)));
414 env.close();
415 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
416
417 // Still live: a third party may not touch it.
418 env(proposal::cancel(bob, proposalID), Ter(tecNO_PERMISSION));
419 env.close();
420
421 // Jump past the expiration. Whether the proposal is terminal is
422 // judged against the parent ledger's close time, the same rule
423 // Expiration follows everywhere.
424 env.close(env.now() + 200s);
425
426 env(proposal::cancel(bob, proposalID));
427 env.close();
428
429 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
430 BEAST_EXPECT(ownerCount(env, alice) == 0);
431 // The target's unspent ticket survives its proposal.
432 BEAST_EXPECT(env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
433 }
434
435 // A proposal whose proposed transaction has run out of ledgers — the
436 // ledger has moved past its LastLedgerSequence — is just as terminal as
437 // an expired one. The boundary mirrors the ordinary transactor's
438 // tefMAX_LEDGER rule: at view.seq() == LastLedgerSequence the payload can
439 // still be submitted, so the proposal is still live; only at view.seq() >
440 // LastLedgerSequence does it become terminal.
441 void
443 {
444 testcase("anyone cancels proposal past its LastLedgerSequence");
445
446 using namespace jtx;
447 using namespace std::chrono_literals;
448
449 Env env{*this, features};
450
451 Account const alice{"alice"};
452 Account const target{"target"};
453 Account const bob{"bob"};
454 env.fund(XRP(10000), alice, target, bob);
455 env.close();
456 proposal::authorizeProposer(env, target, alice);
457
458 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
459
460 std::uint32_t const lastLedgerSeq = env.current()->seq() + 3;
461 json::Value payload = proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq);
462 payload[sfLastLedgerSequence.getJsonName()] = lastLedgerSeq;
463
464 env(proposal::create(alice, payload, proposal::expiration(env, 1000s)));
465 env.close();
466 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
467
468 // Close until the open ledger sits exactly at the bound. The payload
469 // could still be submitted in this ledger, so the proposal is not
470 // yet terminal and a third party still cannot cancel it.
471 while (env.current()->seq() < lastLedgerSeq)
472 env.close();
473 BEAST_EXPECT(env.current()->seq() == lastLedgerSeq);
474 env(proposal::cancel(bob, proposalID), Ter(tecNO_PERMISSION));
475 env.close();
476
477 // One ledger past the bound the payload can never be submitted, so
478 // the proposal is terminal and anyone may cancel it.
479 BEAST_EXPECT(env.current()->seq() == lastLedgerSeq + 1);
480 env(proposal::cancel(bob, proposalID));
481 env.close();
482
483 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
484 BEAST_EXPECT(ownerCount(env, alice) == 0);
485 }
486
487 // The Expiration boundary: a proposal is terminal already in the first
488 // ledger whose parent close time equals its Expiration (the >= rule
489 // Expiration follows everywhere), not merely after it.
490 void
492 {
493 testcase("expiration boundary is inclusive");
494
495 using namespace jtx;
496 using namespace std::chrono_literals;
497
498 Env env{*this, features};
499
500 Account const alice{"alice"};
501 Account const target{"target"};
502 Account const bob{"bob"};
503 env.fund(XRP(10000), alice, target, bob);
504 env.close();
505 proposal::authorizeProposer(env, target, alice);
506
507 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
508
509 // Recorded close times land on multiples of the close time
510 // resolution, so pick the expiration on that grid and later aim a
511 // close at it exactly. Env::close(tp) sets the clock to
512 // tp + resolution - 1s and the accept path rounds to the nearest
513 // multiple, so a target of E - resolution + 1s closes at exactly E.
514 auto const resolution = env.closed()->header().closeTimeResolution;
515 NetClock::time_point const lastClose = env.current()->parentCloseTime();
516 BEAST_EXPECT((lastClose.time_since_epoch() % resolution).count() == 0);
517
518 NetClock::time_point const expiryTime = lastClose + 20 * resolution;
519 std::uint32_t const expiration = expiryTime.time_since_epoch().count();
520
522 alice,
523 proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq),
524 expiration));
525 env.close();
526 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
527
528 // One grid step before the expiration the proposal is still live,
529 // even at the adjacent tick.
530 env.close(expiryTime - 2 * resolution + 1s);
531 BEAST_EXPECT(env.current()->parentCloseTime() == expiryTime - resolution);
532 env(proposal::cancel(bob, proposalID), Ter(tecNO_PERMISSION));
533
534 // The first ledger whose parent close time equals the Expiration is
535 // terminal.
536 env.close(expiryTime - resolution + 1s);
537 BEAST_EXPECT(env.current()->parentCloseTime() == expiryTime);
538
539 env(proposal::cancel(bob, proposalID));
540 env.close();
541 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
542 BEAST_EXPECT(ownerCount(env, alice) == 0);
543 }
544
545 // A proposal turning terminal only widens who may cancel: the Owner and
546 // the target can always do it, before and after.
547 void
549 {
550 testcase("owner cancels expired proposal");
551
552 using namespace jtx;
553 using namespace std::chrono_literals;
554
555 Env env{*this, features};
556
557 Account const alice{"alice"};
558 Account const target{"target"};
559 Account const bob{"bob"};
560 env.fund(XRP(10000), alice, target, bob);
561 env.close();
562 proposal::authorizeProposer(env, target, alice);
563
564 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
565
567 alice,
568 proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq),
569 proposal::expiration(env, 100s)));
570 env.close();
571 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
572
573 env.close(env.now() + 200s);
574
575 env(proposal::cancel(alice, proposalID));
576 env.close();
577 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
578 BEAST_EXPECT(ownerCount(env, alice) == 0);
579 }
580
581 // A proposed Batch reserves more increments than an ordinary proposal;
582 // cancellation must release exactly that larger amount.
583 void
585 {
586 testcase("cancel batch proposal releases its larger reserve");
587
588 using namespace jtx;
589 using namespace std::chrono_literals;
590
591 Env env{*this, features};
592
593 Account const alice{"alice"};
594 Account const target{"target"};
595 Account const bob{"bob"};
596 env.fund(XRP(10000), alice, target, bob);
597 env.close();
598 proposal::authorizeProposer(env, target, alice);
599
600 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
601
602 auto const seq = env.seq(target);
603 json::Value const payload = proposal::unsignedBatch(
604 env,
605 target,
606 ticketSeq,
607 tfAllOrNothing,
608 {
609 proposal::innerTx(pay(target, bob, XRP(1)), seq),
610 proposal::innerTx(pay(target, bob, XRP(1)), seq + 1),
611 });
612
613 env(proposal::create(alice, payload, proposal::expiration(env, 1000s)));
614 env.close();
615
616 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
617 BEAST_EXPECT(ownerCount(env, alice) == proposal::kBatchProposalOwnerCount);
618
619 env(proposal::cancel(target, proposalID));
620 env.close();
621
622 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
623 BEAST_EXPECT(ownerCount(env, alice) == 0);
624 }
625
626 // The Cancel transaction itself goes through the ordinary transaction
627 // machinery: it may spend a ticket of its submitter, and a target whose
628 // authority lives in a SignerList may cancel by multi-signing.
629 void
631 {
632 testcase("ticketed and multisigned cancels");
633
634 using namespace jtx;
635
636 Env env{*this, features};
637
638 Account const alice{"alice"};
639 Account const target{"target"};
640 Account const bob{"bob"};
641 Account const carol{"carol"};
642 Account const daria{"daria"};
643 env.fund(XRP(10000), alice, target, bob, carol, daria);
644 env.close();
645
646 // The target's authority comes from a SignerList. Alice is on it so
647 // it may create the proposal this test then cancels.
648 env(signers(target, 2, {{alice, 1}, {carol, 1}, {daria, 1}}));
649 env.close();
650
651 // Cancel spending a ticket of the canceller.
652 {
653 uint256 const proposalID = makeProposal(env, alice, target, bob);
654
655 std::uint32_t const cancelTicket = proposal::createTicket(env, alice);
656
657 env(proposal::cancel(alice, proposalID), ticket::Use(cancelTicket));
658 env.close();
659 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
660 }
661
662 // Cancel multi-signed for the target by its signer quorum.
663 {
664 uint256 const proposalID = makeProposal(env, alice, target, bob);
665
666 env(proposal::cancel(target, proposalID),
667 Msig(carol, daria),
668 Fee(env.current()->fees().base * 3));
669 env.close();
670 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
671 }
672
673 // Multi-signing for the target is not the same as acting for oneself.
674 // Carol is on the target's SignerList, which is enough to create a
675 // proposal against the target, yet §7.2 grants cancellation only to
676 // the Owner and to the target itself. So an account may propose but
677 // not un-propose — cancelling still has to come from the target, by
678 // the quorum route the block above takes.
679 {
680 uint256 const proposalID = makeProposal(env, alice, target, bob);
681
682 env(proposal::cancel(carol, proposalID), Ter(tecNO_PERMISSION));
683 env.close();
684
685 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
686 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
687 }
688
689 // Paying with a ticket changes none of that. The failed cancel still
690 // consumes carol's ticket, and nothing else moves: the proposal and
691 // its reserve stay as they were.
692 {
693 uint256 const proposalID = makeProposal(env, alice, target, bob);
694 std::uint32_t const cancelTicket = proposal::createTicket(env, carol);
695 auto const aliceOwnerCount = ownerCount(env, alice);
696
697 env(proposal::cancel(carol, proposalID),
698 ticket::Use(cancelTicket),
700 env.close();
701
702 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
703 BEAST_EXPECT(!env.le(keylet::ticket(carol.id(), SeqProxy::rawTicket(cancelTicket))));
704 BEAST_EXPECT(ownerCount(env, alice) == aliceOwnerCount);
705 BEAST_EXPECT(ownerCount(env, carol) == 0);
706 }
707 }
708
709 // A Cancel works as an inner Batch transaction, where each inner runs its
710 // own preclaim at inner-apply time against the accumulated batch view.
711 void
713 {
714 testcase("cancel as an inner batch transaction");
715
716 using namespace jtx;
717
718 Env env{*this, features};
719
720 Account const alice{"alice"};
721 Account const target{"target"};
722 Account const bob{"bob"};
723 env.fund(XRP(10000), alice, target, bob);
724 env.close();
725 proposal::authorizeProposer(env, target, alice);
726
727 // A multi-account batch: bob submits the outer Batch and the target
728 // authorizes its inner Cancel through a BatchSigners entry.
729 {
730 uint256 const proposalID = makeProposal(env, alice, target, bob);
731
732 auto const seq = env.seq(bob);
733 auto const batchFee = batch::calcBatchFee(env, 1, 2);
734 env(batch::outer(bob, seq, batchFee, tfAllOrNothing),
735 batch::Inner(pay(bob, target, XRP(1)), seq + 1),
736 batch::Inner(proposal::cancel(target, proposalID), env.seq(target)),
737 batch::Sig(target));
738 env.close();
739
740 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
741 BEAST_EXPECT(ownerCount(env, alice) == 0);
742 }
743
744 // Two inner Cancels of the same proposal: the second finds nothing at
745 // its own preclaim. Under tfAllOrNothing that failure discards the
746 // whole batch, so the proposal survives with the reserve still held.
747 {
748 uint256 const proposalID = makeProposal(env, alice, target, bob);
749
750 auto const seq = env.seq(target);
751 auto const batchFee = batch::calcBatchFee(env, 0, 2);
752 env(batch::outer(target, seq, batchFee, tfAllOrNothing),
753 batch::Inner(proposal::cancel(target, proposalID), seq + 1),
754 batch::Inner(proposal::cancel(target, proposalID), seq + 2));
755 env.close();
756
757 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
758 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
759
760 // Under tfIndependent the first inner's deletion stands and only
761 // the second fails.
762 auto const seq2 = env.seq(target);
763 env(batch::outer(target, seq2, batchFee, tfIndependent),
764 batch::Inner(proposal::cancel(target, proposalID), seq2 + 1),
765 batch::Inner(proposal::cancel(target, proposalID), seq2 + 2));
766 env.close();
767
768 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
769 BEAST_EXPECT(ownerCount(env, alice) == 0);
770
771 // Pin the two inner results from the closed ledger: the second
772 // Cancel failed at its own preclaim with tecNO_ENTRY. A tef from
773 // deeper in the apply pipeline would have kept it out of the
774 // ledger entirely.
775 json::Value params;
776 params[jss::ledger_index] = env.closed()->seq();
777 params[jss::transactions] = true;
778 params[jss::expand] = true;
779 auto const jrr = env.rpc("json", "ledger", to_string(params));
780 int success = 0;
781 int noEntry = 0;
782 for (auto const& txn : jrr[jss::result][jss::ledger][jss::transactions])
783 {
784 if (txn[jss::TransactionType] == jss::TransactionProposalCancel)
785 {
786 auto const result =
787 txn[jss::metaData][sfTransactionResult.getJsonName()].asString();
788 success += (result == "tesSUCCESS") ? 1 : 0;
789 noEntry += (result == "tecNO_ENTRY") ? 1 : 0;
790 }
791 }
792 BEAST_EXPECT(success == 1);
793 BEAST_EXPECT(noEntry == 1);
794 }
795 }
796
797 // The Cancel transaction itself may have its fee sponsored, but it is not
798 // on the reserve-sponsorship allow-list (it creates nothing to reserve).
799 void
801 {
802 testcase("sponsored cancels");
803
804 using namespace jtx;
805
806 Env env{*this, features};
807
808 Account const alice{"alice"};
809 Account const target{"target"};
810 Account const bob{"bob"};
811 Account const payer{"payer"};
812 env.fund(XRP(10000), alice, target, bob, payer);
813 env.close();
814 proposal::authorizeProposer(env, target, alice);
815
816 uint256 const proposalID = makeProposal(env, alice, target, bob);
817
818 // Reserve sponsorship is rejected for this transaction type.
819 env(proposal::cancel(alice, proposalID),
820 Fee(XRP(1)),
822 Sig(sfSponsorSignature, payer),
824 env.close();
825 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
826
827 // Fee sponsorship works: the sponsor pays, the canceller does not.
828 auto const before = env.balance(alice);
829 auto const payerBefore = env.balance(payer);
830 env(proposal::cancel(alice, proposalID),
831 Fee(XRP(1)),
833 Sig(sfSponsorSignature, payer));
834 env.close();
835
836 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
837 BEAST_EXPECT(ownerCount(env, alice) == 0);
838 BEAST_EXPECT(env.balance(alice) == before);
839 BEAST_EXPECT(env.balance(payer) == payerBefore - XRP(1));
840 }
841
842 // A proposal's reserve may be sponsored, in which case deleteProposal has
843 // to release it against the account recorded in the entry's Sponsor rather
844 // than against the Owner. Creation and sponsorship transfer are covered by
845 // the Create suite; this is the release side of the same bookkeeping.
846 void
848 {
849 testcase("cancel releases a sponsored reserve to its sponsor");
850
851 using namespace jtx;
852 using namespace std::chrono_literals;
853
854 Env env{*this, features};
855
856 Account const alice{"alice"}; // the proposer, and the proposal's Owner
857 Account const target{"target"};
858 Account const bob{"bob"};
859 Account const backer{"backer"}; // sponsors alice's proposal reserve
860 env.fund(XRP(10000), alice, target, bob, backer);
861 env.close();
862 proposal::authorizeProposer(env, target, alice);
863
864 // An ordinary proposal, cancelled by its Owner.
865 {
866 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
867
869 alice,
870 proposal::unsignedPayload(env, pay(target, bob, XRP(1)), ticketSeq),
871 proposal::expiration(env, 1000s)),
873 Sig(sfSponsorSignature, backer),
875 env.close();
876
877 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
878 BEAST_EXPECT(sponsoringOwnerCount(env, backer) == proposal::kProposalOwnerCount);
879
880 env(proposal::cancel(alice, proposalID));
881 env.close();
882
883 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
884 // The sponsorship unwinds from both ends: alice stops owning the
885 // increments and backer stops covering them. Neither is left
886 // holding a reserve for an object that no longer exists.
887 BEAST_EXPECT(ownerCount(env, alice) == 0);
888 BEAST_EXPECT(sponsoredOwnerCount(env, alice) == 0);
889 BEAST_EXPECT(sponsoringOwnerCount(env, backer) == 0);
890 BEAST_EXPECT(ownerCount(env, backer) == 0);
891 }
892
893 // A proposed Batch holds the larger reserve, and here the target
894 // cancels rather than the Owner — so the release is shown to follow
895 // the Sponsor recorded on the entry, not anything about who submitted
896 // the Cancel.
897 {
898 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
899
900 auto const seq = env.seq(target);
901 json::Value const payload = proposal::unsignedBatch(
902 env,
903 target,
904 ticketSeq,
905 tfAllOrNothing,
906 {
907 proposal::innerTx(pay(target, bob, XRP(1)), seq),
908 proposal::innerTx(pay(target, bob, XRP(1)), seq + 1),
909 });
910
911 env(proposal::create(alice, payload, proposal::expiration(env, 1000s)),
913 Sig(sfSponsorSignature, backer),
915 env.close();
916
917 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
918 BEAST_EXPECT(sponsoringOwnerCount(env, backer) == proposal::kBatchProposalOwnerCount);
919
920 env(proposal::cancel(target, proposalID));
921 env.close();
922
923 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
924 BEAST_EXPECT(ownerCount(env, alice) == 0);
925 BEAST_EXPECT(sponsoredOwnerCount(env, alice) == 0);
926 BEAST_EXPECT(sponsoringOwnerCount(env, backer) == 0);
927 }
928 }
929
930 // deleteProposal removes the entry from its Owner's directory using the
931 // page hint stored in the entry's OwnerNode, so a proposal that is not on
932 // the first page has to be just as removable.
933 void
935 {
936 testcase("cancel a proposal listed on a later directory page");
937
938 using namespace jtx;
939
940 Env env{*this, features};
941
942 Account const alice{"alice"};
943 Account const target{"target"};
944 Account const bob{"bob"};
945 env.fund(XRP(100000), alice, target, bob);
946 env.close();
947
948 // Fill alice's first directory page exactly, so the proposal she
949 // creates next is listed on the second one. Tickets are the cheapest
950 // filler: one directory entry and one owner-count increment each,
951 // whereas a proposal costs one entry but five increments.
952 auto const pageEntries = static_cast<std::uint32_t>(kDirNodeMaxEntries);
953 env(ticket::create(alice, pageEntries));
954 env.close();
955 BEAST_EXPECT(ownerCount(env, alice) == pageEntries);
956
957 uint256 const proposalID = makeProposal(env, alice, target, bob);
958
959 // Without this the setup could stop spanning pages — if the page size
960 // changed, say — and the test would quietly become testOwnerCancel.
961 auto const sle = env.le(keylet::txProposal(proposalID));
962 if (!BEAST_EXPECT(sle && sle->getFieldU64(sfOwnerNode) != 0))
963 return;
964
965 env(proposal::cancel(alice, proposalID));
966 env.close();
967
968 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
969 // Only the proposal left; the tickets that filled the page are still
970 // alice's, so the release was scoped to the one entry.
971 BEAST_EXPECT(ownerCount(env, alice) == pageEntries);
972 }
973
974 // What the ledger holds has to be visible to clients, and reported gone
975 // once cancelled: ledger_entry resolves the proposal by the (target,
976 // ticket) pair it is keyed on and by raw index, and account_objects lists
977 // it for the Owner — never for the target, whose directory it was never in.
978 void
980 {
981 testcase("RPC visibility of a proposal");
982
983 using namespace jtx;
984
985 Env env{*this, features};
986
987 Account const alice{"alice"};
988 Account const target{"target"};
989 Account const bob{"bob"};
990 env.fund(XRP(10000), alice, target, bob);
991 env.close();
992
993 std::uint32_t const ticketSeq = env.seq(target) + 1;
994 uint256 const proposalID = makeProposal(env, alice, target, bob);
995
996 auto const ledgerEntry = [&env](json::Value const& proposalParams) {
997 json::Value params;
998 params[jss::ledger_index] = jss::validated;
999 params[jss::transaction_proposal] = proposalParams;
1000 return env.rpc("json", "ledger_entry", to_string(params))[jss::result];
1001 };
1002
1003 json::Value byPair;
1004 byPair[jss::account] = target.human();
1005 byPair[jss::ticket_seq] = ticketSeq;
1006
1007 auto const accountObjects = [&env](Account const& account) {
1008 json::Value params;
1009 params[jss::account] = account.human();
1010 params[jss::type] = jss::transaction_proposal;
1011 return env.rpc(
1012 "json", "account_objects", to_string(params))[jss::result][jss::account_objects];
1013 };
1014
1015 // Found by the pair the key is derived from, and by that key directly.
1016 {
1017 auto const jrr = ledgerEntry(byPair);
1018 BEAST_EXPECT(jrr[jss::index] == to_string(proposalID));
1019 BEAST_EXPECT(
1020 jrr[jss::node][sfLedgerEntryType.getJsonName()] == jss::TransactionProposal);
1021 BEAST_EXPECT(jrr[jss::node][sfOwner.getJsonName()] == alice.human());
1022
1023 json::Value params;
1024 params[jss::ledger_index] = jss::validated;
1025 params[jss::index] = to_string(proposalID);
1026 auto const byIndex = env.rpc("json", "ledger_entry", to_string(params))[jss::result];
1027 BEAST_EXPECT(byIndex[jss::node] == jrr[jss::node]);
1028 }
1029
1030 // Each required field, in turn, left out — an absent field is a
1031 // malformed request whichever one it is. Only a field that is present
1032 // and unusable gets the more specific complaint.
1033 {
1034 json::Value noTicket;
1035 noTicket[jss::account] = target.human();
1036 auto const noTicketResult = ledgerEntry(noTicket);
1037 BEAST_EXPECT(noTicketResult[jss::error] == "malformedRequest");
1038 BEAST_EXPECT(noTicketResult[jss::error_code] == RpcInvalidParams);
1039 BEAST_EXPECT(noTicketResult[jss::error_message] == "Missing field 'ticket_seq'.");
1040
1042 noAccount[jss::ticket_seq] = ticketSeq;
1043 auto const noAccountResult = ledgerEntry(noAccount);
1044 BEAST_EXPECT(noAccountResult[jss::error] == "malformedRequest");
1045 BEAST_EXPECT(noAccountResult[jss::error_code] == RpcInvalidParams);
1046 BEAST_EXPECT(noAccountResult[jss::error_message] == "Missing field 'account'.");
1047
1048 json::Value badAccount = byPair;
1049 badAccount[jss::account] = "not an account";
1050 auto const badAccountResult = ledgerEntry(badAccount);
1051 BEAST_EXPECT(badAccountResult[jss::error] == "malformedAddress");
1052 BEAST_EXPECT(badAccountResult[jss::error_code] == RpcInvalidParams);
1053 BEAST_EXPECT(
1054 badAccountResult[jss::error_message] == "Invalid field 'account', not AccountID.");
1055 }
1056
1057 // Listed for the proposer, who owns it, and not for the target.
1058 BEAST_EXPECT(accountObjects(alice).size() == 1);
1059 BEAST_EXPECT(
1060 accountObjects(alice)[0u][sfLedgerEntryType.getJsonName()] == jss::TransactionProposal);
1061 BEAST_EXPECT(accountObjects(target).size() == 0);
1062
1063 env(proposal::cancel(alice, proposalID));
1064 env.close();
1065
1066 // Gone. The handler reports the index it computed either way, so a
1067 // client can tell "nothing at this key" from "malformed request".
1068 {
1069 auto const jrr = ledgerEntry(byPair);
1070 BEAST_EXPECT(jrr[jss::error] == "entryNotFound");
1071 BEAST_EXPECT(jrr[jss::error_code] == RpcEntryNotFound);
1072 BEAST_EXPECT(jrr[jss::error_message] == "Entry not found.");
1073 BEAST_EXPECT(!jrr.isMember(jss::node));
1074 BEAST_EXPECT(jrr[jss::index] == to_string(proposalID));
1075 }
1076 BEAST_EXPECT(accountObjects(alice).size() == 0);
1077 }
1078
1079 // The Cancel transaction is not delegable: a Cancel carrying sfDelegate is
1080 // rejected at preflight, and the permission cannot even be granted.
1081 void
1083 {
1084 testcase("cancel cannot be delegated");
1085
1086 using namespace jtx;
1087
1088 Env env{*this, features};
1089
1090 Account const alice{"alice"};
1091 Account const target{"target"};
1092 Account const carol{"carol"};
1093 env.fund(XRP(10000), alice, target, carol);
1094 env.close();
1095 proposal::authorizeProposer(env, target, alice);
1096
1097 uint256 const proposalID = makeProposal(env, alice, target, alice);
1098
1099 env(delegate::set(target, carol, {"TransactionProposalCancel"}), Ter(temMALFORMED));
1100 env.close();
1101
1102 env(proposal::cancel(target, proposalID), delegate::As(carol), Ter(temINVALID));
1103 env.close();
1104 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
1105 }
1106
1107 // A required auxiliary co-signer of the proposed transaction — here a
1108 // LoanSet's explicit Counterparty — has its authorization sought by the
1109 // proposal, yet §7.2 grants it no cancellation rights: it is neither the
1110 // Owner nor the target.
1111 void
1113 {
1114 testcase("proposed transaction's counterparty cannot cancel");
1115
1116 using namespace jtx;
1117 using namespace std::chrono_literals;
1118
1119 Env env{*this, features};
1120
1121 Account const alice{"alice"};
1122 Account const borrower{"borrower"};
1123 Account const carol{"carol"}; // the LoanSet's Counterparty
1124 env.fund(XRP(10000), alice, borrower, carol);
1125 env.close();
1126 proposal::authorizeProposer(env, borrower, alice);
1127
1128 std::uint32_t const ticketSeq = proposal::createTicket(env, borrower);
1129
1130 json::Value payload =
1131 proposal::unsignedPayload(env, loan::set(borrower, uint256{1}, 1'000), ticketSeq);
1132 payload[sfCounterparty.getJsonName()] = carol.human();
1133
1134 env(proposal::create(alice, payload, proposal::expiration(env, 1000s)));
1135 env.close();
1136 uint256 const proposalID = keylet::txProposal(borrower.id(), ticketSeq).key;
1137
1138 env(proposal::cancel(carol, proposalID), Ter(tecNO_PERMISSION));
1139 env.close();
1140 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
1141
1142 // The target itself may, of course initiate the Cancel transaction.
1143 env(proposal::cancel(borrower, proposalID));
1144 env.close();
1145 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
1146 }
1147
1148 // Being named in the payload is never itself a claim to cancel: preclaim's
1149 // permitted set is a closed comparison against the Owner, the payload's
1150 // Account and its Delegate, so a Sponsor is a third party like any other
1151 // (§7.2). It is the most interested third party there is — it has agreed to
1152 // pay the fee when the payload is submitted — which is what makes it worth
1153 // stating. What the sponsorship does not change is either half of the rule:
1154 // no permission while the proposal is live, and the same permission as
1155 // everyone else once it is terminal.
1156 void
1158 {
1159 testcase("proposed transaction's sponsor cannot cancel");
1160
1161 using namespace jtx;
1162 using namespace std::chrono_literals;
1163
1164 Env env{*this, features};
1165
1166 Account const alice{"alice"};
1167 Account const target{"target"};
1168 Account const bob{"bob"};
1169 Account const backer{"backer"}; // pays the payload's fee
1170 env.fund(XRP(10000), alice, target, bob, backer);
1171 env.close();
1172 proposal::authorizeProposer(env, target, alice);
1173
1174 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
1175
1176 json::Value tx = pay(target, bob, XRP(1));
1177 tx[sfSponsor.getJsonName()] = backer.human();
1178 tx[sfSponsorFlags.getJsonName()] = spfSponsorFee;
1179 json::Value payload = proposal::unsignedPayload(env, tx, ticketSeq);
1180
1181 // A LastLedgerSequence far beyond this test keeps that arm of the
1182 // terminal rule inert: only the Expiration arm may fire below.
1183 payload[sfLastLedgerSequence.getJsonName()] = env.current()->seq() + 1000;
1184
1185 env(proposal::create(alice, payload, proposal::expiration(env, 100s)));
1186 env.close();
1187 uint256 const proposalID = keylet::txProposal(target.id(), ticketSeq).key;
1188
1189 // Live: the sponsor is refused, and refusing costs the proposal nothing.
1190 env(proposal::cancel(backer, proposalID), Ter(tecNO_PERMISSION));
1191 env.close();
1192 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
1193 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
1194
1195 // Terminal: the permission question stops being asked, so the account
1196 // refused a moment ago may now delete it — and the reserve it releases
1197 // is still the Owner's, not the sponsor's.
1198 env.close(env.now() + 200s);
1199
1200 env(proposal::cancel(backer, proposalID));
1201 env.close();
1202 BEAST_EXPECT(!env.le(keylet::txProposal(proposalID)));
1203 BEAST_EXPECT(ownerCount(env, alice) == 0);
1204 }
1205
1206 // Cancel may not spend the ticket the proposal is keyed to. The
1207 // reservation is only for the proposed transaction (On-Chain Cosigner spec §4.2.1);
1208 // a target that wants to revoke both the proposal and the ticket must
1209 // cancel first, then spend the ticket separately.
1210 void
1212 {
1213 testcase("cancel cannot consume the reserved ticket");
1214
1215 using namespace jtx;
1216
1217 Env env{*this, features};
1218
1219 Account const alice{"alice"};
1220 Account const target{"target"};
1221 Account const bob{"bob"};
1222 env.fund(XRP(10000), alice, target, bob);
1223 env.close();
1224 proposal::authorizeProposer(env, target, alice);
1225
1226 // Standalone: the Cancel is held as a retry and neither object is
1227 // touched.
1228 {
1229 std::uint32_t const ticketSeq = env.seq(target) + 1;
1230 uint256 const proposalID = makeProposal(env, alice, target, bob);
1231
1232 env(proposal::cancel(target, proposalID),
1233 ticket::Use(ticketSeq),
1235 env.close();
1236
1237 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
1238 BEAST_EXPECT(env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
1239 BEAST_EXPECT(ownerCount(env, alice) == proposal::kProposalOwnerCount);
1240 BEAST_EXPECT(ownerCount(env, target) == 2);
1241 }
1242
1243 // The same attempt as an inner Batch transaction: tfAllOrNothing
1244 // discards the whole batch, so the sibling payment does not land.
1245 {
1246 std::uint32_t const ticketSeq = env.seq(target) + 1;
1247 uint256 const proposalID = makeProposal(env, alice, target, bob);
1248
1249 auto const seq = env.seq(target);
1250 auto const bobBefore = env.balance(bob);
1251 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1252 env(batch::outer(target, seq, batchFee, tfAllOrNothing),
1253 batch::Inner(proposal::cancel(target, proposalID), 0, ticketSeq),
1254 batch::Inner(pay(target, bob, XRP(1)), seq + 1));
1255 env.close();
1256
1257 BEAST_EXPECT(env.le(keylet::txProposal(proposalID)));
1258 BEAST_EXPECT(env.le(keylet::ticket(target.id(), SeqProxy::rawTicket(ticketSeq))));
1259 BEAST_EXPECT(env.balance(bob) == bobBefore);
1260 BEAST_EXPECT(ownerCount(env, alice) == 2 * proposal::kProposalOwnerCount);
1261 BEAST_EXPECT(ownerCount(env, target) == 3);
1262 }
1263 }
1264
1265 // A proposal must not nest proposal machinery: proposing a
1266 // TransactionProposalCancel is rejected outright. A signer-quorum target
1267 // that wants to refuse a proposal multi-signs a Cancel directly instead.
1268 void
1270 {
1271 testcase("a cancel cannot itself be proposed");
1272
1273 using namespace jtx;
1274 using namespace std::chrono_literals;
1275
1276 Env env{*this, features};
1277
1278 Account const alice{"alice"};
1279 Account const target{"target"};
1280 env.fund(XRP(10000), alice, target);
1281 env.close();
1282
1283 std::uint32_t const ticketSeq = proposal::createTicket(env, target);
1284 std::uint32_t const expiration = proposal::expiration(env, 1000s);
1285
1286 // Directly proposed.
1287 {
1288 json::Value const payload =
1289 proposal::unsignedPayload(env, proposal::cancel(target, uint256{1}), ticketSeq);
1290
1291 env(proposal::create(alice, payload, expiration), Ter(temINVALID));
1292 env.close();
1293 BEAST_EXPECT(!proposal::entry(env, target, ticketSeq));
1294 }
1295
1296 // Wrapped inside a proposed Batch: nesting must not be evadable by
1297 // one level of indirection (On-Chain Cosigner spec §5.3.1). Each
1298 // wrapped transaction is well-formed for its type, so the rejection
1299 // can only come from the nesting rule itself. The sibling inner uses
1300 // a sequence distinct from the wrapped transaction's, so the Batch is
1301 // not rejected as redundant either.
1302 auto const rejectWrapped = [&](json::Value const& wrapped, TER expected) {
1303 json::Value const payload = proposal::unsignedBatch(
1304 env,
1305 target,
1306 ticketSeq,
1307 tfAllOrNothing,
1308 {proposal::innerTx(pay(target, alice, XRP(1)), env.seq(target) + 1), wrapped});
1309
1310 env(proposal::create(alice, payload, expiration), Ter(expected));
1311 env.close();
1312 BEAST_EXPECT(!proposal::entry(env, target, ticketSeq));
1313 };
1314
1315 // A well-formed inner TransactionProposalCancel.
1316 rejectWrapped(
1317 proposal::innerTx(proposal::cancel(target, uint256{1}), env.seq(target)), temINVALID);
1318
1319 // A well-formed inner TransactionProposalCreate.
1320 rejectWrapped(
1323 target,
1324 proposal::unsignedPayload(env, pay(target, alice, XRP(1)), 1),
1325 expiration),
1326 env.seq(target)),
1327 temINVALID);
1328
1329 // An inner Batch is rejected at STTx construction (a Batch cannot
1330 // contain a Batch), so Create surfaces temMALFORMED rather than the
1331 // nesting rule's temINVALID.
1332 {
1333 json::Value nested;
1334 nested[jss::TransactionType] = jss::Batch;
1335 nested[jss::Account] = target.human();
1336 nested[jss::Flags] = tfAllOrNothing;
1337 nested[jss::RawTransactions][0u][jss::RawTransaction] =
1338 proposal::innerTx(pay(target, alice, XRP(1)), env.seq(target));
1339 nested[jss::RawTransactions][1u][jss::RawTransaction] =
1340 proposal::innerTx(proposal::cancel(target, uint256{1}), env.seq(target));
1341
1342 rejectWrapped(proposal::innerTx(std::move(nested), env.seq(target)), temMALFORMED);
1343 }
1344 }
1345
1346 void
1347 run() override
1348 {
1349 using namespace jtx;
1350
1351 FeatureBitset const all{testableAmendments()};
1352
1353 // Preflight
1354 testDisabled(all);
1355 testMalformed(all);
1358
1359 // Preclaim
1360 testNoEntry(all);
1368
1369 // Apply
1370 testOwnerCancel(all);
1371 testTargetCancel(all);
1373 testDelegateCancel(all);
1381
1382 // RPC
1383 testRpcVisibility(all);
1384 }
1385};
1386
1388
1389} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
AccountID id() const
Returns the Account ID.
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
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
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
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Set the fee on a JTx.
Definition fee.h:20
Set a multisignature on a JTx.
Definition multisign.h:53
Set the regular signature on a JTx.
Definition sig.h:19
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
Adds an inner Batch transaction to a JTx and autofills it.
Definition batch.h:91
Sets the Batch transaction signers on a JTx.
Definition batch.h:142
Set a ticket sequence on a JTx.
Definition ticket.h:36
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:360
Keylet txProposal(AccountID const &target, std::uint32_t ticketSequence) noexcept
A TransactionProposal.
Definition Indexes.cpp:378
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:344
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:523
json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Build an outer Batch transaction JSON object.
Definition batch.cpp:55
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected outer Batch transaction fee.
Definition batch.cpp:37
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
Definition delegate.cpp:17
json::Value set(AccountID const &account, UInt256 const &loanBrokerID, Number principalRequested, std::uint32_t flags)
Create create()
Check the ledger effects a TransactionProposalCreate must have, whatever its outcome: on tesSUCCESS a...
Definition proposal.h:89
json::Value innerTx(json::Value tx, std::uint32_t seq)
Put a transaction into the form an inner transaction of a proposed Batch takes, as batch::Inner does ...
Definition proposal.cpp:80
std::uint32_t createTicket(Env &env, Account const &account, std::uint32_t count)
Create tickets for a proposal to be built against, and close the ledger.
Definition proposal.cpp:128
json::Value unsignedPayload(Env const &env, json::Value tx, std::uint32_t ticketSeq)
Put a transaction of any type into the form a proposal stores it in: unsigned and ticket-based,...
Definition proposal.cpp:61
constexpr std::uint32_t kBatchProposalOwnerCount
Owner-reserve increments held by a proposal of a Batch transaction.
constexpr std::uint32_t kProposalOwnerCount
Owner-reserve increments held by a proposal of an ordinary transaction.
SLE::const_pointer entry(Env const &env, AccountID const &target, std::uint32_t ticketSeq)
The proposal stored against a target account's ticket.
Definition proposal.cpp:145
json::Value create(Account const &proposer, json::Value const &proposedTx, std::uint32_t expiration)
Build a TransactionProposalCreate carrying an unsigned proposed transaction.
Definition proposal.cpp:40
void authorizeProposer(Env &env, Account const &target, Account const &proposer)
Give proposer a place on target's SignerList, so it may create proposals against target.
Definition proposal.cpp:121
json::Value unsignedBatch(Env const &env, Account const &target, std::uint32_t ticketSeq, std::uint32_t flags, std::vector< json::Value > const &inners, std::optional< std::uint32_t > numSigners)
An unsigned outer Batch payload holding inners.
Definition proposal.cpp:86
std::uint32_t expiration(Env &env, NetClock::duration delta)
An absolute expiration delta past the environment's current time.
Definition proposal.cpp:139
json::Value cancel(Account const &account, uint256 const &proposalID)
Build a TransactionProposalCancel naming the proposal to delete.
Definition proposal.cpp:51
json::Value create(Account const &account, std::uint32_t count)
Create one of more tickets.
Definition ticket.cpp:16
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
std::uint32_t ownerCount(Env const &env, Account const &account)
std::uint32_t sponsoringOwnerCount(Env const &env, Account const &account)
FeatureBitset testableAmendments()
Definition Env.h:92
std::uint32_t sponsoredOwnerCount(Env const &env, Account const &account)
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
Definition multisign.cpp:31
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr FlagValue spfSponsorFee
Definition TxFlags.h:467
@ terTICKET_RESERVED
Definition TER.h:235
@ RpcEntryNotFound
Definition ErrorCodes.h:144
@ RpcInvalidParams
Definition ErrorCodes.h:67
constexpr std::size_t kDirNodeMaxEntries
The maximum number of entries per directory page.
Definition Protocol.h:58
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
AccountID const & noAccount()
A placeholder for empty accounts.
@ temINVALID
Definition TER.h:98
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_PERMISSION
Definition TER.h:316
constexpr FlagValue spfSponsorReserve
Definition TxFlags.h:468
UInt256 uint256
Definition base_uint.h:587
UInt256 key
Definition Keylet.h:21
uint256 makeProposal(jtx::Env &env, jtx::Account const &proposer, jtx::Account const &target, jtx::Account const &dest, std::optional< json::Value > payloadOverride=std::nullopt)
T time_since_epoch(T... args)