2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/credentials.h>
8#include <test/jtx/deposit.h>
9#include <test/jtx/escrow.h>
10#include <test/jtx/fee.h>
11#include <test/jtx/flags.h>
12#include <test/jtx/noop.h>
13#include <test/jtx/offer.h>
14#include <test/jtx/owners.h>
15#include <test/jtx/paths.h>
16#include <test/jtx/pay.h>
17#include <test/jtx/require.h>
18#include <test/jtx/sendmax.h>
19#include <test/jtx/seq.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/ticket.h>
22#include <test/jtx/trust.h>
23#include <test/jtx/txflags.h>
24#include <test/jtx/vault.h>
26#include <xrpl/basics/strHex.h>
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/json/json_value.h>
29#include <xrpl/json/to_string.h>
30#include <xrpl/protocol/AccountID.h>
31#include <xrpl/protocol/ErrorCodes.h>
32#include <xrpl/protocol/Feature.h>
33#include <xrpl/protocol/Issue.h>
34#include <xrpl/protocol/LedgerFormats.h>
35#include <xrpl/protocol/SField.h>
36#include <xrpl/protocol/STAmount.h>
37#include <xrpl/protocol/TER.h>
38#include <xrpl/protocol/TxFlags.h>
39#include <xrpl/protocol/XRPAmount.h>
40#include <xrpl/protocol/jss.h>
57 return env.
current()->fees().accountReserve(count, 1);
64 return env.
le(acct)->isFlag(lsfDepositAuth);
81 env(
fset(alice, asfDepositAuth));
85 env(
fclear(alice, asfDepositAuth));
103 IOU const usd = gw[
"USD"];
107 env.
fund(
XRP(10000), alice, bob, carol, gw);
109 env.
trust(usd(1000), alice, bob);
112 env(
pay(gw, alice, usd(150)));
113 env(
offer(carol, usd(100),
XRP(100)));
117 env(
pay(alice, bob, usd(50)));
125 auto const failedIouPayments = [
this, &env, &alice, &bob, &usd] {
140 BEAST_EXPECT(bobXrpBalance == env.
balance(bob,
XRP));
141 BEAST_EXPECT(bobUsdBalance == env.
balance(bob, usd));
149 env(
pay(bob, alice, usd(25)));
155 env(
pay(bob, alice, bobPaysXRP),
Fee(bobPaysFee));
161 BEAST_EXPECT(env.
balance(bob, usd) == usd(25));
175 env(
fclear(bob, asfDepositAuth));
178 env(
pay(alice, bob, usd(50)));
197 auto const baseFee = env.
current()->fees().base;
199 env.
fund(
XRP(10000), alice, bob);
216 env(
pay(bob, alice, bobPaysXRP),
Fee(bobPaysFee));
270 env(
pay(alice, bob,
drops(baseFee - 1)));
300 IOU const usD1(gw1[
"USD"]);
301 IOU const usD2(gw2[
"USD"]);
306 bool withDepositAuth) {
307 Env env(*
this, features);
309 env.
fund(
XRP(10000), gw1, alice, bob);
311 env(
trust(gw1, alice[
"USD"](10), noRipplePrev ? tfSetNoRipple : 0));
312 env(
trust(gw1, bob[
"USD"](10), noRippleNext ? tfSetNoRipple : 0));
313 env.
trust(usD1(10), alice, bob);
315 env(
pay(gw1, alice, usD1(10)));
318 env(
fset(gw1, asfDepositAuth));
321 env(
pay(alice, bob, usD1(10)),
Path(gw1),
Ter(result));
324 auto const testNonIssuer = [&](
FeatureBitset const& features,
327 bool withDepositAuth) {
328 Env env(*
this, features);
330 env.
fund(
XRP(10000), gw1, gw2, alice);
332 env(
trust(alice, usD1(10), noRipplePrev ? tfSetNoRipple : 0));
333 env(
trust(alice, usD2(10), noRippleNext ? tfSetNoRipple : 0));
334 env(
pay(gw2, alice, usD2(10)));
337 env(
fset(alice, asfDepositAuth));
344 for (
int i = 0; i < 8; ++i)
346 auto const noRipplePrev = i & 0x1;
347 auto const noRippleNext = i & 0x2;
348 auto const withDepositAuth = i & 0x4;
374 jvParams[jss::ledger_index] = jss::validated;
375 jvParams[jss::deposit_preauth][jss::owner] = acc.
human();
377 auto& arr(jvParams[jss::deposit_preauth][jss::authorized_credentials]);
378 for (
auto const& o : auth)
380 arr.append(o.toLEJson());
382 return env.
rpc(
"json",
"ledger_entry",
to_string(jvParams));
400 env.
fund(
XRP(10000), alice, becky);
419 env.
fund(
XRP(10000), alice, becky);
436 BEAST_EXPECT(env.
seq(alice) == aliceSeq);
444 BEAST_EXPECT(env.
seq(alice) == aliceSeq);
459 Env env(*
this, features);
469 env.
fund(
XRP(10000), alice, becky);
490 tx[sfUnauthorize.jsonName] = becky.
human();
568 Vault const vault{env};
569 auto [tx,
keylet] = vault.create({.owner = becky, .asset =
xrpIssue()});
573 auto const sleVault = env.
le(
keylet);
574 if (!BEAST_EXPECT(sleVault))
576 Account const vaultPseudo{
"vault", sleVault->at(sfAccount)};
578 auto const expectedResult =
582 env.
require(
Owners(alice, features[fixCleanup3_3_0] ? 0 : 1));
595 IOU const usd(gw[
"USD"]);
601 Env env(*
this, features);
602 env.
fund(
XRP(5000), alice, becky, gw);
605 env.
trust(usd(1000), alice);
606 env.
trust(usd(1000), becky);
609 env(
pay(gw, alice, usd(500)));
621 env(
fset(becky, asfDepositAuth));
630 char const credType[] =
"abcde";
635 bool const supportsCredentials = features[featureCredentials];
651 ? jv[jss::result][jss::index].asString()
652 :
"48004829F915654A81B11C4AB8218D96FED67F209B58328A72314FB6"
664 Env env(*
this, features);
665 env.
fund(
XRP(5000), alice, becky, carol, gw);
668 env.
trust(usd(1000), alice);
669 env.
trust(usd(1000), becky);
670 env.
trust(usd(1000), carol);
673 env(
pay(gw, alice, usd(1000)));
677 env(
pay(alice, becky,
XRP(100)));
678 env(
pay(alice, becky, usd(100)));
682 env(
fset(becky, asfDepositAuth));
705 env(
pay(alice, becky,
XRP(100)));
706 env(
pay(alice, becky, usd(100)));
710 env(
fset(alice, asfDepositAuth));
723 env(
pay(alice, becky,
XRP(100)));
724 env(
pay(alice, becky, usd(100)));
737 env(
fclear(becky, asfDepositAuth));
740 env(
pay(alice, becky,
XRP(100)));
741 env(
pay(alice, becky, usd(100)));
750 char const credType[] =
"abcde";
751 Account const issuer{
"issuer"};
758 testcase(
"Payment failure with disabled credentials rule.");
762 env.
fund(
XRP(5000), issuer, bob, alice);
766 env(
fset(bob, asfDepositAuth));
781 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
788 testcase(
"Payment with credentials.");
792 env.
fund(
XRP(5000), issuer, alice, bob, john);
801 std::string const credIdx = jv[jss::result][jss::index].asString();
804 env(
fset(bob, asfDepositAuth));
815 jDP.isObject() && jDP.isMember(jss::result) &&
816 !jDP[jss::result].isMember(jss::error) && jDP[jss::result].isMember(jss::node) &&
817 jDP[jss::result][jss::node].isMember(
"LedgerEntryType") &&
818 jDP[jss::result][jss::node][
"LedgerEntryType"] == jss::DepositPreauth);
822 auto jv =
pay(alice, bob,
XRP(100));
846 env(
fset(john, asfDepositAuth));
853 testcase(
"Payment failure with invalid credentials.");
857 env.
fund(
XRP(10000), issuer, alice, bob, maria);
868 std::string const credIdx = jv[jss::result][jss::index].asString();
877 env(
fset(bob, asfDepositAuth));
889 {.issuer = issuer, .credType = credType},
890 {.issuer = issuer, .credType = credType},
900 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
903 env(
pay(alice, bob,
XRP(100)),
910 env(
pay(maria, bob,
XRP(100)),
917 char const credType2[] =
"fghij";
923 std::string const credIdx2 = jv[jss::result][jss::index].asString();
926 env(
pay(alice, bob,
XRP(100)),
947 char const credType[] =
"abcde";
948 Account const issuer{
"issuer"};
952 Env env(*
this, features);
954 env.
fund(
XRP(5000), issuer, alice, bob);
963 std::string const credIdx = jv[jss::result][jss::index].asString();
985 char const credType[] =
"abcde";
986 Account const issuer{
"issuer"};
992 testcase(
"Creating / deleting with credentials.");
996 env.
fund(
XRP(5000), issuer, alice, bob);
1009 jv[sfUnauthorize.jsonName] = issuer.
human();
1016 jv[sfAuthorize.jsonName] = issuer.
human();
1024 jv[sfUnauthorize.jsonName] = issuer.
human();
1032 jv[sfAuthorize.jsonName] = issuer.
human();
1045 auto& arr(jv[sfAuthorizeCredentials.jsonName]);
1050 credParent[jss::Credential] = cred;
1051 arr.
append(std::move(credParent));
1064 Account const a(
"a"), b(
"b"), c(
"c"), d(
"d"), e(
"e"), f(
"f"), g(
"g"), h(
"h"),
1066 auto const& z = credType;
1070 {.issuer = a, .credType = z},
1071 {.issuer = b, .credType = z},
1072 {.issuer = c, .credType = z},
1073 {.issuer = d, .credType = z},
1074 {.issuer = e, .credType = z},
1075 {.issuer = f, .credType = z},
1076 {.issuer = g, .credType = z},
1077 {.issuer = h, .credType = z},
1078 {.issuer = i, .credType = z},
1095 env.
fund(env.
current()->fees().accountReserve(0, 1), john);
1116 jDP.isObject() && jDP.isMember(jss::result) &&
1117 !jDP[jss::result].isMember(jss::error) &&
1118 jDP[jss::result].isMember(jss::node) &&
1119 jDP[jss::result][jss::node].isMember(
"LedgerEntryType") &&
1120 jDP[jss::result][jss::node][
"LedgerEntryType"] == jss::DepositPreauth);
1123 BEAST_EXPECT(jDP[jss::result][jss::node][jss::Account] == bob.
human());
1124 auto const&
credentials(jDP[jss::result][jss::node][
"AuthorizeCredentials"]);
1128 auto const& c(o[jss::Credential]);
1129 BEAST_EXPECT(c[jss::Issuer].asString() == issuer.
human());
1146 jDP.isObject() && jDP.isMember(jss::result) &&
1147 jDP[jss::result][jss::error] ==
"entryNotFound" &&
1149 jDP[jss::result][jss::error_message] ==
"Entry not found.");
1157 using namespace jtx;
1158 char const credType[] =
"abcde";
1159 char const credType2[] =
"fghijkl";
1160 Account const issuer{
"issuer"};
1164 IOU const usd = gw[
"USD"];
1168 testcase(
"Payment failure with expired credentials.");
1172 env.
fund(
XRP(10000), issuer, alice, bob, gw);
1180 env.
current()->header().parentCloseTime.time_since_epoch().count() + 60;
1181 jv[sfExpiration.jsonName] = t;
1192 env.
current()->header().parentCloseTime.time_since_epoch().count() + 1000;
1193 jv[sfExpiration.jsonName] = t2;
1204 std::string const credIdx = jv[jss::result][jss::index].asString();
1206 std::string const credIdx2 = jv[jss::result][jss::index].asString();
1209 env(
fset(bob, asfDepositAuth));
1215 {.issuer = issuer, .credType = credType},
1216 {.issuer = issuer, .credType = credType2},
1227 env(
pay(alice, bob,
XRP(100)),
1236 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1237 jDelCred[jss::result][jss::error] ==
"entryNotFound" &&
1239 jDelCred[jss::result][jss::error_message] ==
"Entry not found.");
1246 jle.isObject() && jle.isMember(jss::result) &&
1247 !jle[jss::result].isMember(jss::error) &&
1248 jle[jss::result].isMember(jss::node) &&
1249 jle[jss::result][jss::node].isMember(
"LedgerEntryType") &&
1250 jle[jss::result][jss::node][
"LedgerEntryType"] == jss::Credential &&
1251 jle[jss::result][jss::node][jss::Issuer] == issuer.
human() &&
1252 jle[jss::result][jss::node][jss::Subject] == alice.
human() &&
1253 jle[jss::result][jss::node][
"CredentialType"] ==
1264 env.
current()->header().parentCloseTime.time_since_epoch().count() + 40;
1265 jv[sfExpiration.jsonName] = t;
1272 std::string const credIdx = jv[jss::result][jss::index].asString();
1288 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1289 jDelCred[jss::result][jss::error] ==
"entryNotFound" &&
1291 jDelCred[jss::result][jss::error_message] ==
"Entry not found.");
1301 testcase(
"Escrow failure with expired credentials.");
1305 env.
fund(
XRP(5000), issuer, alice, bob, zelda);
1311 env.
current()->header().parentCloseTime.time_since_epoch().count() + 50;
1312 jv[sfExpiration.jsonName] = t;
1322 std::string const credIdx = jv[jss::result][jss::index].asString();
1325 env(
fset(bob, asfDepositAuth));
1331 auto const seq = env.
seq(alice);
1344 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
1364 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1365 jDelCred[jss::result][jss::error] ==
"entryNotFound" &&
1367 jDelCred[jss::result][jss::error_message] ==
"Entry not found.");
1374 using namespace jtx;
1384 env.
fund(
XRP(5000), stock, alice, bob);
1387 {.issuer =
"a", .credType =
"a"},
1388 {.issuer =
"b", .credType =
"b"},
1389 {.issuer =
"c", .credType =
"c"},
1390 {.issuer =
"d", .credType =
"d"},
1391 {.issuer =
"e", .credType =
"e"},
1392 {.issuer =
"f", .credType =
"f"},
1393 {.issuer =
"g", .credType =
"g"},
1394 {.issuer =
"h", .credType =
"h"},
1398 env.
fund(
XRP(5000), c.issuer);
1407 pubKey2Acc.
emplace(c.issuer.human(), c.issuer);
1410 for (
int i = 0; i < 10; ++i)
1417 auto const& authCred(dp[jss::result][jss::node][
"AuthorizeCredentials"]);
1418 BEAST_EXPECT(authCred.isArray() && authCred.size() ==
credentials.size());
1420 for (
auto const& o : authCred)
1422 auto const& c(o[jss::Credential]);
1423 auto const issuer = c[jss::Issuer].asString();
1425 if (BEAST_EXPECT(pubKey2Acc.
contains(issuer)))
1428 pubKey2Acc.
at(issuer), c[
"CredentialType"].asString());
1445 for (
int i = 0; i < 10; ++i)
1452 testcase(
"Check duplicate credentials.");
1459 for (
auto const& c : copyCredentials)
1461 auto credentials2 = copyCredentials;
1477 env, alice, c.issuer, c.credType)[jss::result][jss::index]
1482 for (
auto const& h : credentialIDs)
1484 auto credentialIDs2 = credentialIDs;
TestcaseT testcase
Memberspace for declaring test cases.
Value removeMember(char const *key)
Remove and return the named member.
Value & append(Value const &value)
Append value to array at the end.
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
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.
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.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void memoize(Account const &account)
Associate AccountID with account.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
NetClock::time_point now()
Returns the current network time.
Converts to IOU Issue or STAmount.
Match clear account flags.
Match the number of items in the account's owner directory.
Check a set of conditions.
Sets the SendMax 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.
T emplace_back(T... args)
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
Keylet computation functions.
json::Value accept(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
json::Value create(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
json::Value ledgerEntry(jtx::Env &env, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
json::Value unauthCredentials(jtx::Account const &account, std::vector< AuthorizeCredentials > const &auth)
json::Value authCredentials(jtx::Account const &account, std::vector< AuthorizeCredentials > const &auth)
json::Value auth(Account const &account, Account const &auth)
Preauthorize for deposit.
json::Value unauth(Account const &account, Account const &unauth)
Remove pre-authorization for deposit.
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
json::Value finish(AccountID const &account, AccountID const &from, std::uint32_t seq)
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)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
FeatureBitset testableAmendments()
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
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.
OwnerCount< ltTICKET > Tickets
Match the number of tickets on the account.
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
static bool hasDepositAuth(jtx::Env const &env, jtx::Account const &acct)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
static XRPAmount reserve(jtx::Env const &env, std::uint32_t count)
static json::Value ledgerEntryDepositPreauth(jtx::Env &env, jtx::Account const &acc, std::vector< jtx::deposit::AuthorizeCredentials > const &auth)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
std::string strHex(FwdIt begin, FwdIt end)
std::string to_string(BaseUInt< Bits, Tag > const &a)
TERSubset< CanCvtToTER > TER
AccountID const & xrpAccount()
Compute AccountID from public key.
@ tecINSUFFICIENT_RESERVE
void run() override
Runs the suite.
void testCredentialsPayment()
void run() override
Runs the suite.
void testInvalid(FeatureBitset features)
void testSortingCredentials()
void testCredentialsCreation()
void testPayment(FeatureBitset features)
void testZeroCredentialID(FeatureBitset features)
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
Set the sequence number on a JTx.