1#include <test/jtx/AMM.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/delegate.h>
7#include <test/jtx/deposit.h>
8#include <test/jtx/fee.h>
9#include <test/jtx/flags.h>
10#include <test/jtx/multisign.h>
11#include <test/jtx/noop.h>
12#include <test/jtx/offer.h>
13#include <test/jtx/pay.h>
14#include <test/jtx/proposal.h>
15#include <test/jtx/sig.h>
16#include <test/jtx/sponsor.h>
17#include <test/jtx/ter.h>
18#include <test/jtx/ticket.h>
19#include <test/jtx/token.h>
20#include <test/jtx/trust.h>
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/basics/strHex.h>
24#include <xrpl/beast/unit_test/suite.h>
25#include <xrpl/beast/utility/Journal.h>
26#include <xrpl/json/json_value.h>
27#include <xrpl/ledger/OpenView.h>
28#include <xrpl/ledger/helpers/ProposalHelpers.h>
29#include <xrpl/protocol/AccountID.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/Indexes.h>
32#include <xrpl/protocol/Keylet.h>
33#include <xrpl/protocol/SField.h>
34#include <xrpl/protocol/STAmount.h>
35#include <xrpl/protocol/STArray.h>
36#include <xrpl/protocol/STLedgerEntry.h>
37#include <xrpl/protocol/STObject.h>
38#include <xrpl/protocol/SeqProxy.h>
39#include <xrpl/protocol/TER.h>
40#include <xrpl/protocol/TxFlags.h>
41#include <xrpl/protocol/TxFormats.h>
42#include <xrpl/protocol/jss.h>
76 using namespace std::chrono_literals;
78 Env env{*
this, features - featureCosign};
82 env.
fund(
XRP(10000), target, bob);
107 testcase(
"reject payload that must not be stored");
110 using namespace std::chrono_literals;
112 Env env{*
this, features};
114 Account const target{
"target"};
116 env.
fund(
XRP(10000), target, bob);
124 auto const payload = [&] {
130 auto const reject = [&](
json::Value const& proposedTx,
TER expected) {
143 tx[jss::TransactionType] = 65535;
153 tx[jss::TransactionType] = jss::EnableAmendment;
173 tx[jss::TransactionType] =
"TransactionProposalCreate";
202 missingType[jss::Account] = target.
human();
204 missingType[jss::Sequence] = env.
seq(target);
205 missingType[jss::Fee] =
"10";
206 missingType[jss::SigningPubKey] =
"";
229 STArray rawTxns(sfRawTransactions);
233 batch.setFieldU16(sfTransactionType, ttBATCH);
234 batch.setFieldArray(sfRawTransactions, rawTxns);
248 tx[jss::Sequence] = 1;
282 tx[jss::Amount] =
"0";
292 tx[sfMemos.jsonName][0u][sfMemo.jsonName][sfMemoData.jsonName] =
329 testcase(
"reject payload carrying a signature");
332 using namespace std::chrono_literals;
334 Env env{*
this, features};
336 Account const target{
"target"};
338 env.
fund(
XRP(10000), target, bob);
354 auto const payment = [&] {
357 auto const sponsoredPayment = [&] {
362 tx[sfSponsor.getJsonName()] = bob.
human();
366 auto const loanSet = [&] {
368 tx[sfCounterparty.getJsonName()] = bob.
human();
371 auto const batchTx = [&] {
385 auto const reject = [&](
json::Value const& proposedTx,
TER expected) {
405 {.apply = [&](
json::Value& o) { o[jss::SigningPubKey] = key; }, .signs =
false},
407 .apply = [&](
json::Value& o) { o[sfTxnSignature.getJsonName()] = sig; },
413 o[jss::SigningPubKey] =
"";
414 o[sfTxnSignature.getJsonName()] = sig;
423 o[jss::SigningPubKey] = key;
424 o[sfTxnSignature.getJsonName()] = sig;
435 o[jss::SigningPubKey] =
"";
436 auto& signer = o[sfSigners.getJsonName()][0u][sfSigner.getJsonName()];
437 signer[jss::Account] = bob.
human();
438 signer[jss::SigningPubKey] = key;
439 signer[sfTxnSignature.getJsonName()] = sig;
446 o[jss::SigningPubKey] =
"";
447 auto& signer = o[sfSigners.getJsonName()][0u][sfSigner.getJsonName()];
448 signer[jss::Account] = bob.
human();
449 signer[jss::SigningPubKey] =
"";
450 signer[sfTxnSignature.getJsonName()] = sig;
469 bool checksSignatureContent;
476 .checksSignatureContent =
true,
481 auto& o = tx[sfCounterpartySignature.getJsonName()];
485 .checksSignatureContent =
true,
488 .payload = sponsoredPayment,
490 auto& o = tx[sfSponsorSignature.getJsonName()];
494 .checksSignatureContent =
false,
502 auto& o = tx[sfBatchSigners.getJsonName()][0u][sfBatchSigner.getJsonName()];
503 o[jss::Account] = bob.
human();
506 .checksSignatureContent =
false,
510 for (
auto const& place : places)
512 for (
auto const& fill : fills)
515 fill.apply(place.at(tx));
547 tx[sfTxnSignature.getJsonName()] = sig;
557 testcase(
"reject proposal that cannot be completed");
560 using namespace std::chrono_literals;
562 Env env{*
this, features};
564 Account const target{
"target"};
567 env.
fund(
XRP(10000), target, bob);
595 tx[sfLastLedgerSequence.getJsonName()] = env.
current()->seq() - 1;
609 tx[sfLastLedgerSequence.getJsonName()] = env.
current()->seq();
674 testcase(
"reject proposal from an unauthorized proposer");
677 using namespace std::chrono_literals;
679 Env env{*
this, features};
681 Account const target{
"target"};
682 Account const signer{
"signer"};
683 Account const stranger{
"stranger"};
685 env.
fund(
XRP(10000), target, signer, stranger, bob);
688 env(
signers(target, 1, {{signer, 1}}));
751 env.
fund(
XRP(10000), s1, s2, s3, s4, s5);
754 env(
signers(target, 3, {{s1, 1}, {s2, 1}, {s3, 1}, {s4, 1}, {s5, 1}}));
757 for (
Account const& s : {s1, s2, s3, s4, s5})
803 testcase(
"unparseable on-ledger SignerList is tefBAD_LEDGER");
806 using namespace std::chrono_literals;
809 env.
fund(
XRP(10000), target, signer);
811 env(
signers(target, 1, {{signer, 1}}));
818 auto const proposeAsSigner =
830 Env env{*
this, features};
831 Account const target{
"targetMissing"};
832 Account const signer{
"signerMissing"};
837 auto const sle = view.read(signerListKeylet);
840 auto const replacement = std::make_shared<SLE>(*sle);
841 if (!replacement->delField(sfSignerEntries))
843 view.rawReplace(replacement);
846 BEAST_EXPECT(env.
le(signerListKeylet));
848 proposeAsSigner(env, target, signer, ticketSeq);
852 Env env{*
this, features};
853 Account const target{
"targetBadEntry"};
854 Account const signer{
"signerBadEntry"};
859 auto const sle = view.read(signerListKeylet);
862 auto const replacement = std::make_shared<SLE>(*sle);
864 badEntries.pushBack(STObject{sfSigner});
865 replacement->setFieldArray(sfSignerEntries, badEntries);
869 BEAST_EXPECT(env.
le(signerListKeylet));
871 proposeAsSigner(env, target, signer, ticketSeq);
875 Env env{*
this, features};
876 Account const target{
"targetMissingAccount"};
877 Account const signer{
"signerMissingAccount"};
878 std::uint32_t
const ticketSeq = setup(env, target, signer);
882 auto const sle = view.read(signerListKeylet);
885 auto const replacement = std::make_shared<SLE>(*sle);
890 badEntries.pushBack(STObject{sfSignerEntry});
891 replacement->setFieldArray(sfSignerEntries, badEntries);
892 view.rawReplace(replacement);
895 BEAST_EXPECT(env.
le(signerListKeylet));
897 proposeAsSigner(env, target, signer, ticketSeq);
911 testcase(
"proposer authorized through a delegated proposed txn");
914 using namespace std::chrono_literals;
916 Env env{*
this, features};
918 Account const target{
"target"};
919 Account const delegateAcct{
"delegateAcct"};
922 Account const stranger{
"stranger"};
924 env.
fund(
XRP(10000), target, delegateAcct, ds1, ds2, stranger, bob);
929 tx[sfDelegate.jsonName] = delegateAcct.
human();
961 env(
signers(delegateAcct, 1, {{ds1, 1}, {ds2, 1}}));
991 testcase(
"proposer authorized through granular delegate permission");
994 using namespace std::chrono_literals;
996 Env env{*
this, features};
1001 env.
fund(
XRP(10000), gw, alice, bob);
1002 env(
fset(gw, asfRequireAuth));
1005 env(
trust(alice, gw[
"USD"](50)));
1012 tx[sfDelegate.jsonName] = bob.
human();
1033 delegatedTrustSet(ticketSeq, tfSetFreeze),
1046 tx[sfDelegate.jsonName] = bob.
human();
1047 tx[sfQualityOut.jsonName] = 100;
1068 testcase(
"unparseable delegate SignerList is tefBAD_LEDGER");
1070 using namespace jtx;
1071 using namespace std::chrono_literals;
1073 Env env{*
this, features};
1075 Account const target{
"target"};
1076 Account const delegateAcct{
"delegateAcct"};
1079 env.
fund(
XRP(10000), target, delegateAcct, ds1, bob);
1088 env(
signers(delegateAcct, 1, {{ds1, 1}}));
1099 auto const sle = view.read(delegateSignerListKeylet);
1102 auto const replacement = std::make_shared<SLE>(*sle);
1107 badEntries.pushBack(STObject{sfSignerEntry});
1108 replacement->setFieldArray(sfSignerEntries, badEntries);
1112 BEAST_EXPECT(env.le(delegateSignerListKeylet));
1115 tx[sfDelegate.jsonName] = delegateAcct.human();
1131 testcase(
"reject proposal targeting a pseudo-account");
1133 using namespace jtx;
1134 using namespace std::chrono_literals;
1136 Env env{*
this, features};
1138 Account const proposer{
"proposer"};
1143 auto const USD = gw[
"USD"];
1144 env.
fund(
XRP(10000), proposer, alice, gw, bob);
1146 env.
trust(USD(1'000'000), alice);
1148 env(
pay(gw, alice, USD(10'000)));
1168 testcase(
"create proposal object");
1170 using namespace jtx;
1171 using namespace std::chrono_literals;
1173 Env env{*
this, features};
1175 Account const target{
"target"};
1177 env.
fund(
XRP(10000), target, bob);
1194 if (!BEAST_EXPECT(sle))
1197 BEAST_EXPECT(sle->getAccountID(sfOwner) == target.
id());
1198 BEAST_EXPECT(sle->getFieldU32(sfExpiration) == expiration);
1200 auto const stored = sle->getFieldObject(sfProposedTransaction);
1201 BEAST_EXPECT(stored.getAccountID(sfAccount) == target.
id());
1202 BEAST_EXPECT(stored.getFieldU32(sfSequence) == 0);
1203 BEAST_EXPECT(stored.getFieldU32(sfTicketSequence) == targetTicketSeq);
1204 BEAST_EXPECT(stored.getFieldVL(sfSigningPubKey).empty());
1220 testcase(
"proposals for other transaction types");
1222 using namespace jtx;
1223 using namespace std::chrono_literals;
1225 Env env{*
this, features};
1227 Account const target{
"target"};
1231 auto const USD = gw[
"USD"];
1232 env.
fund(
XRP(10000), target, bob, gw);
1240 trust(target, USD(1000)),
1241 signers(target, 1, {{bob, 1}}),
1275 testcase(
"proposal for a transaction type with an auxiliary co-signature");
1277 using namespace jtx;
1278 using namespace std::chrono_literals;
1280 Env env{*
this, features};
1282 Account const borrower{
"borrower"};
1285 env.
fund(
XRP(10000), borrower, bob);
1308 tx[sfSponsor.getJsonName()] = bob.
human();
1324 using namespace jtx;
1325 using namespace std::chrono_literals;
1327 Env env{*
this, features};
1330 Account const target{
"target"};
1332 env.
fund(
XRP(10000), target, bob);
1353 env(
pay(bob, alice,
XRP(10)));
1366 testcase(
"create paid for with the proposer's own ticket");
1368 using namespace jtx;
1369 using namespace std::chrono_literals;
1371 Env env{*
this, features};
1374 Account const target{
"target"};
1376 env.
fund(
XRP(10000), alice, target, bob);
1398 BEAST_EXPECT(env.
seq(alice) == aliceSeq);
1402 BEAST_EXPECT(
ownerCount(env, target) == targetOwnerCount);
1412 testcase(
"proposer reserve sponsored");
1414 using namespace jtx;
1415 using namespace std::chrono_literals;
1422 Env env{*
this, features - featureSponsor};
1425 Account const target{
"target"};
1427 Account const backer{
"backer"};
1428 env.
fund(
XRP(10000), alice, target, bob, backer);
1438 Sig(sfSponsorSignature, backer),
1445 Env env{*
this, features};
1448 Account const target{
"target"};
1450 Account const backer{
"backer"};
1452 env.
fund(
XRP(10000), alice, target, bob, backer);
1462 Sig(sfSponsorSignature, backer),
1467 if (!BEAST_EXPECT(sle))
1470 BEAST_EXPECT(sle->isFieldPresent(sfSponsor));
1471 BEAST_EXPECT(sle->getAccountID(sfSponsor) == backer.
id());
1489 testcase(
"proposer reserve sponsorship transferred");
1491 using namespace jtx;
1492 using namespace std::chrono_literals;
1494 Env env{*
this, features};
1497 Account const target{
"target"};
1499 Account const backer1{
"backer1"};
1500 Account const backer2{
"backer2"};
1502 env.
fund(
XRP(10000), alice, target, bob, backer1, backer2);
1512 Sig(sfSponsorSignature, backer1),
1523 Sig(sfSponsorSignature, backer2));
1527 if (!BEAST_EXPECT(sle))
1530 BEAST_EXPECT(sle->isFieldPresent(sfSponsor));
1531 BEAST_EXPECT(sle->getAccountID(sfSponsor) == backer2.
id());
1548 testcase(
"proposal creation fee sponsored");
1550 using namespace jtx;
1551 using namespace std::chrono_literals;
1553 Env env{*
this, features};
1555 Account const target{
"target"};
1557 Account const backer{
"backer"};
1559 env.
fund(
XRP(10000), target, bob, backer);
1566 auto const targetBalance = env.
balance(target);
1567 auto const backerBalance = env.
balance(backer);
1577 Sig(sfSponsorSignature, backer),
1582 BEAST_EXPECT(env.
balance(target) == targetBalance);
1583 BEAST_EXPECT(env.
balance(backer) == backerBalance - feeAmt);
1591 testcase(
"proposed batch reserve");
1593 using namespace jtx;
1594 using namespace std::chrono_literals;
1596 Env env{*
this, features};
1598 Account const target{
"target"};
1600 env.
fund(
XRP(10000), target, bob);
1633 testcase(
"proposed multi-account batch");
1635 using namespace jtx;
1636 using namespace std::chrono_literals;
1638 Env env{*
this, features};
1640 Account const target{
"target"};
1642 env.
fund(
XRP(10000), target, bob);
1664 if (!BEAST_EXPECT(sle))
1669 auto const stored = sle->getFieldObject(sfProposedTransaction);
1670 BEAST_EXPECT(!stored.isFieldPresent(sfBatchSigners));
1683 testcase(
"proposed batch inner account-reserve SponsorshipTransfer");
1685 using namespace jtx;
1686 using namespace std::chrono_literals;
1688 Env env{*
this, features};
1690 Account const target{
"target"};
1692 env.
fund(
XRP(10000), target, bob);
1695 auto const unsignedInnerSponsorship = [&](
Account const& account) {
1697 tx[sfSponsor.getJsonName()] = bob.
human();
1707 auto const seq = env.
seq(target);
1729 auto const seq = env.
seq(target);
1751 using namespace jtx;
A generic endpoint for log messages.
TestcaseT testcase
Memberspace for declaring test cases.
Value removeMember(char const *key)
Remove and return the named member.
bool modify(ModifyType const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Slice slice() const noexcept
void push_back(STObject const &object)
void setAccountID(SField const &field, AccountID const &)
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
virtual OpenLedger & getOpenLedger()=0
Convenience class to test AMM functionality.
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
PublicKey const & pk() const
Return the 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.
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.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
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.
Set a ticket sequence on a JTx.
@ Object
object value (collection of name/value pairs).
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.
bool isValidProposalTxnType(STObject const &proposedTx)
Whether the proposed transaction is independently submittable through the ordinary multi-sign path: n...
Helpers for constructing Batch test transactions.
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
json::Value auth(Account const &account, Account const &auth)
Preauthorize for deposit.
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 mint(jtx::Account const &account, std::uint32_t nfTokenTaxon)
Mint an NFToken.
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()
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
std::uint32_t sponsoredOwnerCount(Env const &env, Account const &account)
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr FlagValue spfSponsorFee
constexpr FlagValue tfInnerBatchTxn
ApplyResult apply(ServiceRegistry ®istry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
std::string strHex(FwdIt begin, FwdIt end)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
TERSubset< CanCvtToTER > TER
@ tecINSUFFICIENT_RESERVE
constexpr FlagValue spfSponsorReserve
A pair of SHAMap key and LedgerEntryType.
void testProposerAuthorization(FeatureBitset features)
void testPseudoTarget(FeatureBitset features)
void testCorruptDelegateSignerList(FeatureBitset features)
void testProposedBatchInnerSponsorship(FeatureBitset features)
void testRejectedSignatureFields(FeatureBitset features)
void testSponsorshipTransfer(FeatureBitset features)
void testReserve(FeatureBitset features)
void testCorruptSignerList(FeatureBitset features)
void testMultiAccountBatch(FeatureBitset features)
void testPreclaim(FeatureBitset features)
void testRejectedPayload(FeatureBitset features)
void testFeeSponsored(FeatureBitset features)
void testSponsoredReserve(FeatureBitset features)
void testDelegatedProposedTx(FeatureBitset features)
void testDelegatedGranularProposedTx(FeatureBitset features)
void testAuxiliaryCoSignatureTypes(FeatureBitset features)
void testCreate(FeatureBitset features)
void testDisabled(FeatureBitset features)
void run() override
Runs the suite.
void testTicketedCreate(FeatureBitset features)
void testOtherTransactionTypes(FeatureBitset features)
void testBatchReserve(FeatureBitset features)