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>
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>
55 using namespace std::chrono_literals;
83 Env env{*
this, features - featureCosign};
102 Env env{*
this, features};
123 testcase(
"proposal does not exist");
126 using namespace std::chrono_literals;
128 Env env{*
this, features};
131 Account const target{
"target"};
132 env.
fund(
XRP(10000), alice, target);
169 testcase(
"owner cancels live proposal");
172 using namespace std::chrono_literals;
174 Env env{*
this, features};
177 Account const target{
"target"};
179 env.
fund(
XRP(10000), alice, target, bob);
225 testcase(
"target cancels live proposal");
229 Env env{*
this, features};
232 Account const target{
"target"};
234 env.
fund(
XRP(10000), alice, target, bob);
261 testcase(
"target cancels a proposal it made for itself");
264 using namespace std::chrono_literals;
266 Env env{*
this, features};
268 Account const target{
"target"};
270 env.
fund(
XRP(10000), target, bob);
280 if (!BEAST_EXPECT(sle))
282 BEAST_EXPECT(sle->getAccountID(sfOwner) == target.
id());
284 sle->getFieldObject(sfProposedTransaction).getAccountID(sfAccount) == target.
id());
305 testcase(
"proposed transaction's delegate cancels");
309 Env env{*
this, features};
312 Account const target{
"target"};
315 env.
fund(
XRP(10000), alice, target, carol, bob);
321 payload[sfDelegate.getJsonName()] = carol.
human();
335 makeProposal(env, alice, target, bob, delegatedPayload(ticketSeq));
349 makeProposal(env, alice, target, bob, delegatedPayload(ticketSeq));
365 testcase(
"third party cannot cancel live proposal");
369 Env env{*
this, features};
372 Account const target{
"target"};
374 env.
fund(
XRP(10000), alice, target, bob);
392 testcase(
"anyone cancels expired proposal");
395 using namespace std::chrono_literals;
397 Env env{*
this, features};
400 Account const target{
"target"};
402 env.
fund(
XRP(10000), alice, target, bob);
411 payload[sfLastLedgerSequence.getJsonName()] = env.
current()->seq() + 1000;
444 testcase(
"anyone cancels proposal past its LastLedgerSequence");
447 using namespace std::chrono_literals;
449 Env env{*
this, features};
452 Account const target{
"target"};
454 env.
fund(
XRP(10000), alice, target, bob);
462 payload[sfLastLedgerSequence.getJsonName()] = lastLedgerSeq;
471 while (env.
current()->seq() < lastLedgerSeq)
473 BEAST_EXPECT(env.
current()->seq() == lastLedgerSeq);
479 BEAST_EXPECT(env.
current()->seq() == lastLedgerSeq + 1);
493 testcase(
"expiration boundary is inclusive");
496 using namespace std::chrono_literals;
498 Env env{*
this, features};
501 Account const target{
"target"};
503 env.
fund(
XRP(10000), alice, target, bob);
514 auto const resolution = env.
closed()->header().closeTimeResolution;
530 env.
close(expiryTime - 2 * resolution + 1s);
531 BEAST_EXPECT(env.
current()->parentCloseTime() == expiryTime - resolution);
536 env.
close(expiryTime - resolution + 1s);
537 BEAST_EXPECT(env.
current()->parentCloseTime() == expiryTime);
550 testcase(
"owner cancels expired proposal");
553 using namespace std::chrono_literals;
555 Env env{*
this, features};
558 Account const target{
"target"};
560 env.
fund(
XRP(10000), alice, target, bob);
586 testcase(
"cancel batch proposal releases its larger reserve");
589 using namespace std::chrono_literals;
591 Env env{*
this, features};
594 Account const target{
"target"};
596 env.
fund(
XRP(10000), alice, target, bob);
602 auto const seq = env.
seq(target);
632 testcase(
"ticketed and multisigned cancels");
636 Env env{*
this, features};
639 Account const target{
"target"};
643 env.
fund(
XRP(10000), alice, target, bob, carol, daria);
648 env(
signers(target, 2, {{alice, 1}, {carol, 1}, {daria, 1}}));
695 auto const aliceOwnerCount =
ownerCount(env, alice);
704 BEAST_EXPECT(
ownerCount(env, alice) == aliceOwnerCount);
714 testcase(
"cancel as an inner batch transaction");
718 Env env{*
this, features};
721 Account const target{
"target"};
723 env.
fund(
XRP(10000), alice, target, bob);
732 auto const seq = env.
seq(bob);
750 auto const seq = env.
seq(target);
752 env(
batch::outer(target, seq, batchFee, tfAllOrNothing),
762 auto const seq2 = env.
seq(target);
763 env(
batch::outer(target, seq2, batchFee, tfIndependent),
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));
782 for (
auto const& txn : jrr[jss::result][jss::ledger][jss::transactions])
784 if (txn[jss::TransactionType] == jss::TransactionProposalCancel)
787 txn[jss::metaData][sfTransactionResult.getJsonName()].asString();
788 success += (result ==
"tesSUCCESS") ? 1 : 0;
789 noEntry += (result ==
"tecNO_ENTRY") ? 1 : 0;
792 BEAST_EXPECT(success == 1);
793 BEAST_EXPECT(noEntry == 1);
806 Env env{*
this, features};
809 Account const target{
"target"};
812 env.
fund(
XRP(10000), alice, target, bob, payer);
822 Sig(sfSponsorSignature, payer),
828 auto const before = env.
balance(alice);
829 auto const payerBefore = env.
balance(payer);
833 Sig(sfSponsorSignature, payer));
838 BEAST_EXPECT(env.
balance(alice) == before);
839 BEAST_EXPECT(env.
balance(payer) == payerBefore -
XRP(1));
849 testcase(
"cancel releases a sponsored reserve to its sponsor");
852 using namespace std::chrono_literals;
854 Env env{*
this, features};
857 Account const target{
"target"};
859 Account const backer{
"backer"};
860 env.
fund(
XRP(10000), alice, target, bob, backer);
873 Sig(sfSponsorSignature, backer),
900 auto const seq = env.
seq(target);
913 Sig(sfSponsorSignature, backer),
936 testcase(
"cancel a proposal listed on a later directory page");
940 Env env{*
this, features};
943 Account const target{
"target"};
945 env.
fund(
XRP(100000), alice, target, bob);
955 BEAST_EXPECT(
ownerCount(env, alice) == pageEntries);
962 if (!BEAST_EXPECT(sle && sle->getFieldU64(sfOwnerNode) != 0))
971 BEAST_EXPECT(
ownerCount(env, alice) == pageEntries);
981 testcase(
"RPC visibility of a proposal");
985 Env env{*
this, features};
988 Account const target{
"target"};
990 env.
fund(
XRP(10000), alice, target, bob);
996 auto const ledgerEntry = [&env](
json::Value const& proposalParams) {
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];
1004 byPair[jss::account] = target.
human();
1005 byPair[jss::ticket_seq] = ticketSeq;
1007 auto const accountObjects = [&env](
Account const& account) {
1009 params[jss::account] = account.human();
1010 params[jss::type] = jss::transaction_proposal;
1012 "json",
"account_objects",
to_string(params))[jss::result][jss::account_objects];
1017 auto const jrr = ledgerEntry(byPair);
1018 BEAST_EXPECT(jrr[jss::index] ==
to_string(proposalID));
1020 jrr[jss::node][sfLedgerEntryType.getJsonName()] == jss::TransactionProposal);
1021 BEAST_EXPECT(jrr[jss::node][sfOwner.getJsonName()] == alice.
human());
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]);
1035 noTicket[jss::account] = target.
human();
1036 auto const noTicketResult = ledgerEntry(noTicket);
1037 BEAST_EXPECT(noTicketResult[jss::error] ==
"malformedRequest");
1039 BEAST_EXPECT(noTicketResult[jss::error_message] ==
"Missing field 'ticket_seq'.");
1043 auto const noAccountResult = ledgerEntry(
noAccount);
1044 BEAST_EXPECT(noAccountResult[jss::error] ==
"malformedRequest");
1046 BEAST_EXPECT(noAccountResult[jss::error_message] ==
"Missing field 'account'.");
1049 badAccount[jss::account] =
"not an account";
1050 auto const badAccountResult = ledgerEntry(badAccount);
1051 BEAST_EXPECT(badAccountResult[jss::error] ==
"malformedAddress");
1054 badAccountResult[jss::error_message] ==
"Invalid field 'account', not AccountID.");
1058 BEAST_EXPECT(accountObjects(alice).size() == 1);
1060 accountObjects(alice)[0u][sfLedgerEntryType.getJsonName()] == jss::TransactionProposal);
1061 BEAST_EXPECT(accountObjects(target).size() == 0);
1069 auto const jrr = ledgerEntry(byPair);
1070 BEAST_EXPECT(jrr[jss::error] ==
"entryNotFound");
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));
1076 BEAST_EXPECT(accountObjects(alice).size() == 0);
1084 testcase(
"cancel cannot be delegated");
1086 using namespace jtx;
1088 Env env{*
this, features};
1091 Account const target{
"target"};
1093 env.
fund(
XRP(10000), alice, target, carol);
1114 testcase(
"proposed transaction's counterparty cannot cancel");
1116 using namespace jtx;
1117 using namespace std::chrono_literals;
1119 Env env{*
this, features};
1122 Account const borrower{
"borrower"};
1124 env.
fund(
XRP(10000), alice, borrower, carol);
1132 payload[sfCounterparty.getJsonName()] = carol.
human();
1159 testcase(
"proposed transaction's sponsor cannot cancel");
1161 using namespace jtx;
1162 using namespace std::chrono_literals;
1164 Env env{*
this, features};
1167 Account const target{
"target"};
1169 Account const backer{
"backer"};
1170 env.
fund(
XRP(10000), alice, target, bob, backer);
1177 tx[sfSponsor.getJsonName()] = backer.
human();
1183 payload[sfLastLedgerSequence.getJsonName()] = env.
current()->seq() + 1000;
1213 testcase(
"cancel cannot consume the reserved ticket");
1215 using namespace jtx;
1217 Env env{*
this, features};
1220 Account const target{
"target"};
1222 env.
fund(
XRP(10000), alice, target, bob);
1249 auto const seq = env.
seq(target);
1250 auto const bobBefore = env.
balance(bob);
1252 env(
batch::outer(target, seq, batchFee, tfAllOrNothing),
1259 BEAST_EXPECT(env.
balance(bob) == bobBefore);
1271 testcase(
"a cancel cannot itself be proposed");
1273 using namespace jtx;
1274 using namespace std::chrono_literals;
1276 Env env{*
this, features};
1279 Account const target{
"target"};
1280 env.
fund(
XRP(10000), alice, target);
1302 auto const rejectWrapped = [&](
json::Value const& wrapped,
TER expected) {
1334 nested[jss::TransactionType] = jss::Batch;
1335 nested[jss::Account] = target.
human();
1336 nested[jss::Flags] = tfAllOrNothing;
1337 nested[jss::RawTransactions][0u][jss::RawTransaction] =
1339 nested[jss::RawTransactions][1u][jss::RawTransaction] =
1349 using namespace jtx;
TestcaseT testcase
Memberspace for declaring test cases.
std::chrono::time_point< NetClock > time_point
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
AccountID id() const
Returns the Account ID.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
SLE::const_pointer le(Account const &account) const
Return an account root.
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
NetClock::time_point now()
Returns the current network time.
Set a multisignature on a JTx.
Set the regular signature on a JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Adds an inner Batch transaction to a JTx and autofills it.
Sets the Batch transaction signers on a JTx.
Set a ticket sequence on a JTx.
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Keylet txProposal(AccountID const &target, std::uint32_t ticketSequence) noexcept
A TransactionProposal.
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Build an outer Batch transaction JSON object.
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected outer Batch transaction fee.
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
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...
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 ...
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.
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,...
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.
json::Value create(Account const &proposer, json::Value const &proposedTx, std::uint32_t expiration)
Build a TransactionProposalCreate carrying an unsigned proposed transaction.
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.
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.
std::uint32_t expiration(Env &env, NetClock::duration delta)
An absolute expiration delta past the environment's current time.
json::Value cancel(Account const &account, uint256 const &proposalID)
Build a TransactionProposalCancel naming the proposal to delete.
json::Value create(Account const &account, std::uint32_t count)
Create one of more tickets.
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
XrpT const XRP
Converts to XRP Issue or STAmount.
json::Value noop(Account const &account)
The null transaction.
std::uint32_t ownerCount(Env const &env, Account const &account)
std::uint32_t sponsoringOwnerCount(Env const &env, Account const &account)
FeatureBitset testableAmendments()
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)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr FlagValue spfSponsorFee
constexpr std::size_t kDirNodeMaxEntries
The maximum number of entries per directory page.
std::string to_string(BaseUInt< Bits, Tag > const &a)
AccountID const & noAccount()
A placeholder for empty accounts.
TERSubset< CanCvtToTER > TER
constexpr FlagValue spfSponsorReserve
void testTargetCancel(FeatureBitset features)
void run() override
Runs the suite.
void testSponsoredReserveCancel(FeatureBitset features)
void testCounterpartyCannotCancel(FeatureBitset features)
void testDelegatedCancelRejected(FeatureBitset features)
void testCannotProposeCancel(FeatureBitset features)
void testExpiredPermissionlessCancel(FeatureBitset features)
void testOwnerCancelTerminal(FeatureBitset features)
void testExpirationBoundary(FeatureBitset features)
void testCancelFromLaterDirectoryPage(FeatureBitset features)
void testOwnerCancel(FeatureBitset features)
void testMalformed(FeatureBitset features)
void testRpcVisibility(FeatureBitset features)
void testBatchProposalCancel(FeatureBitset features)
void testSponsoredCancel(FeatureBitset features)
void testCancelCannotSpendReservedTicket(FeatureBitset features)
void testLastLedgerSequenceTerminal(FeatureBitset features)
void testCancelSubmissionForms(FeatureBitset features)
uint256 makeProposal(jtx::Env &env, jtx::Account const &proposer, jtx::Account const &target, jtx::Account const &dest, std::optional< json::Value > payloadOverride=std::nullopt)
void testCancelInsideBatch(FeatureBitset features)
void testSponsorCannotCancel(FeatureBitset features)
void testDelegateCancel(FeatureBitset features)
void testNoEntry(FeatureBitset features)
void testSelfProposalCancel(FeatureBitset features)
void testDisabled(FeatureBitset features)
void testThirdPartyNoPermission(FeatureBitset features)
T time_since_epoch(T... args)