xrpld
Loading...
Searching...
No Matches
AccountTx_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/acctdelete.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h> // IWYU pragma: keep
7#include <test/jtx/check.h>
8#include <test/jtx/delegate.h>
9#include <test/jtx/deposit.h>
10#include <test/jtx/envconfig.h>
11#include <test/jtx/fee.h>
12#include <test/jtx/mpt.h>
13#include <test/jtx/multisign.h>
14#include <test/jtx/noop.h>
15#include <test/jtx/offer.h>
16#include <test/jtx/pay.h>
17#include <test/jtx/regkey.h>
18#include <test/jtx/sig.h>
19#include <test/jtx/sponsor.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/ticket.h>
22#include <test/jtx/trust.h>
23
24#include <xrpld/core/Config.h>
25
26#include <xrpl/basics/base_uint.h>
27#include <xrpl/basics/chrono.h>
28#include <xrpl/basics/strHex.h>
29#include <xrpl/beast/unit_test/suite.h>
30#include <xrpl/json/json_value.h>
31#include <xrpl/json/to_string.h>
32#include <xrpl/protocol/AccountID.h>
33#include <xrpl/protocol/ApiVersion.h>
34#include <xrpl/protocol/ErrorCodes.h>
35#include <xrpl/protocol/Indexes.h>
36#include <xrpl/protocol/SField.h>
37#include <xrpl/protocol/STAmount.h>
38#include <xrpl/protocol/SeqProxy.h>
39#include <xrpl/protocol/TER.h>
40#include <xrpl/protocol/TxFlags.h>
41#include <xrpl/protocol/jss.h>
42
43#include <boost/container/flat_set.hpp>
44
45#include <chrono>
46#include <cstddef>
47#include <cstdint>
48#include <initializer_list>
49#include <iterator>
50#include <memory>
51#include <optional>
52#include <string>
53#include <utility>
54
55namespace xrpl::test {
56
58{
59 // A data structure used to describe the basic structure of a
60 // transactions array node as returned by the account_tx RPC command.
62 {
63 int const index;
65 boost::container::flat_set<std::string> created;
66 boost::container::flat_set<std::string> deleted;
67 boost::container::flat_set<std::string> modified;
68
70 int idx,
71 json::StaticString const& t,
75 : index(idx), txType(t)
76 {
77 auto const buildSet = [](auto&& init) {
78 boost::container::flat_set<std::string> r;
79 r.reserve(init.size());
80 for (auto&& s : init)
81 r.insert(s);
82 return r;
83 };
84
85 created = buildSet(c);
86 deleted = buildSet(d);
87 modified = buildSet(m);
88 }
89 };
90
91 // A helper method tests can use to validate returned JSON vs NodeSanity.
92 void
93 checkSanity(json::Value const& txNode, NodeSanity const& sane)
94 {
95 BEAST_EXPECT(txNode[jss::validated].asBool() == true);
96 BEAST_EXPECT(txNode[jss::tx][sfTransactionType.jsonName].asString() == sane.txType);
97
98 // Make sure all of the expected node types are present.
99 boost::container::flat_set<std::string> createdNodes;
100 boost::container::flat_set<std::string> deletedNodes;
101 boost::container::flat_set<std::string> modifiedNodes;
102
103 for (json::Value const& metaNode : txNode[jss::meta][sfAffectedNodes.jsonName])
104 {
105 if (metaNode.isMember(sfCreatedNode.jsonName))
106 {
107 createdNodes.insert(
108 metaNode[sfCreatedNode.jsonName][sfLedgerEntryType.jsonName].asString());
109 }
110 else if (metaNode.isMember(sfDeletedNode.jsonName))
111 {
112 deletedNodes.insert(
113 metaNode[sfDeletedNode.jsonName][sfLedgerEntryType.jsonName].asString());
114 }
115 else if (metaNode.isMember(sfModifiedNode.jsonName))
116 {
117 modifiedNodes.insert(
118 metaNode[sfModifiedNode.jsonName][sfLedgerEntryType.jsonName].asString());
119 }
120 else
121 {
122 fail("Unexpected or unlabeled node type in metadata.", __FILE__, __LINE__);
123 }
124 }
125
126 BEAST_EXPECT(createdNodes == sane.created);
127 BEAST_EXPECT(deletedNodes == sane.deleted);
128 BEAST_EXPECT(modifiedNodes == sane.modified);
129 };
130
131 void
132 testParameters(unsigned int apiVersion)
133 {
134 testcase("Parameters APIv" + std::to_string(apiVersion));
135 using namespace test::jtx;
136
137 Env env(*this, envconfig([](std::unique_ptr<Config> cfg) {
138 cfg->fees.referenceFee = 10;
139 return cfg;
140 }));
141 Account const a1{"A1"};
142 env.fund(XRP(10000), a1);
143 env.close();
144
145 // Ledger 3 has the two txs associated with funding the account
146 // All other ledgers have no txs
147
148 auto const hasTxs = [apiVersion](json::Value const& j) {
149 switch (apiVersion)
150 {
151 case 1:
152 return j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
153 (j[jss::result][jss::transactions].size() == 2) &&
154 (j[jss::result][jss::transactions][0u][jss::tx][jss::TransactionType] ==
155 jss::AccountSet) &&
156 (j[jss::result][jss::transactions][1u][jss::tx][jss::TransactionType] ==
157 jss::Payment) &&
158 (j[jss::result][jss::transactions][1u][jss::tx][jss::DeliverMax] ==
159 "10000000010") &&
160 (j[jss::result][jss::transactions][1u][jss::tx][jss::Amount] ==
161 j[jss::result][jss::transactions][1u][jss::tx][jss::DeliverMax]);
162 case 2:
163 case 3:
164 if (j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
165 (j[jss::result][jss::transactions].size() == 2) &&
166 (j[jss::result][jss::transactions][0u][jss::tx_json]
167 [jss::TransactionType] == jss::AccountSet))
168 {
169 auto const& payment = j[jss::result][jss::transactions][1u];
170 return payment.isMember(jss::tx_json) &&
171 (payment[jss::tx_json][jss::TransactionType] == jss::Payment) &&
172 (payment[jss::tx_json][jss::DeliverMax] == "10000000010") &&
173 (!payment[jss::tx_json].isMember(jss::Amount)) &&
174 (!payment[jss::tx_json].isMember(jss::hash)) &&
175 (payment[jss::hash] ==
176 "9F3085D85F472D1CC29627F260DF68EDE59D42D1D0C33E345"
177 "ECF0D4CE981D0A8") &&
178 (payment[jss::validated] == true) &&
179 (payment[jss::ledger_index] == 3) &&
180 (payment[jss::ledger_hash] ==
181 "F1B386FB6C239EE5EF63109372E38C4E85328316371496794617D1FAC4B68F31") &&
182 (payment[jss::close_time_iso] == "2000-01-01T00:00:10Z");
183 }
184 return false;
185
186 default:
187 return false;
188 }
189 };
190
191 auto const noTxs = [](json::Value const& j) {
192 return j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
193 (j[jss::result][jss::transactions].size() == 0);
194 };
195
196 auto const isErr = [](json::Value const& j, ErrorCodeI code, std::string const& message) {
197 return j.isMember(jss::result) &&
198 j[jss::result][jss::error] == rpc::getErrorInfo(code).token &&
199 j[jss::result][jss::error_code] == code &&
200 j[jss::result][jss::error_message] == message;
201 };
202
203 json::Value jParams;
204 jParams[jss::api_version] = apiVersion;
205
206 BEAST_EXPECT(isErr(
207 env.rpc("json", "account_tx", to_string(jParams)),
209 "Missing field 'account'."));
210
211 jParams[jss::account] = "0xDEADBEEF";
212
213 BEAST_EXPECT(isErr(
214 env.rpc("json", "account_tx", to_string(jParams)),
216 "Account malformed."));
217
218 jParams[jss::account] = a1.human();
219 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(jParams))));
220
221 // Ledger min/max index
222 {
223 json::Value p{jParams};
224 p[jss::ledger_index_min] = -1;
225 p[jss::ledger_index_max] = -1;
226 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
227
228 p[jss::ledger_index_min] = 0;
229 p[jss::ledger_index_max] = 100;
230 if (apiVersion < 2u)
231 {
232 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
233 }
234 else
235 {
236 BEAST_EXPECT(isErr(
237 env.rpc("json", "account_tx", to_string(p)),
239 "Ledger index malformed."));
240 }
241
242 p[jss::ledger_index_min] = 1;
243 p[jss::ledger_index_max] = 2;
244 if (apiVersion < 2u)
245 {
246 BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
247 }
248 else
249 {
250 BEAST_EXPECT(isErr(
251 env.rpc("json", "account_tx", to_string(p)),
253 "Ledger index malformed."));
254 }
255
256 p[jss::ledger_index_min] = 2;
257 p[jss::ledger_index_max] = 1;
258 BEAST_EXPECT(isErr(
259 env.rpc("json", "account_tx", to_string(p)),
260 (apiVersion == 1 ? RpcLgrIdxsInvalid : RpcInvalidLgrRange),
261 (apiVersion == 1 ? "Ledger indexes invalid." : "Ledger range is invalid.")));
262 }
263 // Ledger index min only
264 {
265 json::Value p{jParams};
266 p[jss::ledger_index_min] = -1;
267 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
268
269 p[jss::ledger_index_min] = 1;
270 if (apiVersion < 2u)
271 {
272 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
273 }
274 else
275 {
276 BEAST_EXPECT(isErr(
277 env.rpc("json", "account_tx", to_string(p)),
279 "Ledger index malformed."));
280 }
281
282 p[jss::ledger_index_min] = env.current()->header().seq;
283 BEAST_EXPECT(isErr(
284 env.rpc("json", "account_tx", to_string(p)),
285 (apiVersion == 1 ? RpcLgrIdxsInvalid : RpcInvalidLgrRange),
286 (apiVersion == 1 ? "Ledger indexes invalid." : "Ledger range is invalid.")));
287 }
288
289 // Ledger index max only
290 {
291 json::Value p{jParams};
292 p[jss::ledger_index_max] = -1;
293 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
294
295 p[jss::ledger_index_max] = env.current()->header().seq;
296 if (apiVersion < 2u)
297 {
298 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
299 }
300 else
301 {
302 BEAST_EXPECT(isErr(
303 env.rpc("json", "account_tx", to_string(p)),
305 "Ledger index malformed."));
306 }
307
308 p[jss::ledger_index_max] = 3;
309 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
310
311 p[jss::ledger_index_max] = env.closed()->header().seq;
312 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
313
314 p[jss::ledger_index_max] = env.closed()->header().seq - 1;
315 BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
316 }
317
318 // Ledger Sequence
319 {
320 json::Value p{jParams};
321
322 p[jss::ledger_index] = env.closed()->header().seq;
323 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
324
325 p[jss::ledger_index] = env.closed()->header().seq - 1;
326 BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
327
328 p[jss::ledger_index] = env.current()->header().seq;
329 BEAST_EXPECT(isErr(
330 env.rpc("json", "account_tx", to_string(p)),
332 "Ledger not validated."));
333
334 p[jss::ledger_index] = env.current()->header().seq + 1;
335 BEAST_EXPECT(isErr(
336 env.rpc("json", "account_tx", to_string(p)), RpcLgrNotFound, "ledgerNotFound"));
337 }
338
339 {
340 json::Value p{jParams};
341
342 p[jss::ledger_index] = "validated";
343 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
344
345 p[jss::ledger_index] = "closed";
346 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
347
348 p[jss::ledger_index] = "current";
349 BEAST_EXPECT(isErr(
350 env.rpc("json", "account_tx", to_string(p)),
352 "Ledger not validated."));
353
354 p[jss::ledger_index] = "";
355 BEAST_EXPECT(isErr(
356 env.rpc("json", "account_tx", to_string(p)),
358 "Ledger not validated."));
359 }
360
361 // Ledger Hash
362 {
363 json::Value p{jParams};
364
365 p[jss::ledger_hash] = to_string(env.closed()->header().hash);
366 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
367
368 p[jss::ledger_hash] = to_string(env.closed()->header().parentHash);
369 BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
370 }
371
372 // Ledger index max/min/index all specified
373 // ERRORS out with invalid Parenthesis
374 {
375 jParams[jss::account] = "0xDEADBEEF";
376 jParams[jss::account] = a1.human();
377 json::Value p{jParams};
378
379 p[jss::ledger_index_max] = -1;
380 p[jss::ledger_index_min] = -1;
381 p[jss::ledger_index] = -1;
382
383 if (apiVersion < 2u)
384 {
385 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
386 }
387 else
388 {
389 BEAST_EXPECT(isErr(
390 env.rpc("json", "account_tx", to_string(p)),
392 "invalidParams"));
393 }
394 }
395
396 // Ledger index max only
397 {
398 json::Value p{jParams};
399 p[jss::ledger_index_max] = env.current()->header().seq;
400 if (apiVersion < 2u)
401 {
402 BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
403 }
404 else
405 {
406 BEAST_EXPECT(isErr(
407 env.rpc("json", "account_tx", to_string(p)),
409 "Ledger index malformed."));
410 }
411 }
412 // test account non-string
413 {
414 auto const testInvalidAccountParam = [&](auto const& param) {
415 json::Value params;
416 params[jss::account] = param;
417 auto jrr = env.rpc("json", "account_tx", to_string(params))[jss::result];
418 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
419 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
420 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'account'.");
421 };
422
423 testInvalidAccountParam(1);
424 testInvalidAccountParam(1.1);
425 testInvalidAccountParam(true);
426 testInvalidAccountParam(json::Value(json::ValueType::Null));
427 testInvalidAccountParam(json::Value(json::ValueType::Object));
428 testInvalidAccountParam(json::Value(json::ValueType::Array));
429 }
430 // test binary and forward for bool/non bool values
431 {
432 json::Value p{jParams};
433 p[jss::binary] = "asdf";
434 if (apiVersion < 2u)
435 {
436 json::Value result{env.rpc("json", "account_tx", to_string(p))};
437 BEAST_EXPECT(result[jss::result][jss::status] == "success");
438 }
439 else
440 {
441 BEAST_EXPECT(isErr(
442 env.rpc("json", "account_tx", to_string(p)),
444 "Invalid field 'binary'."));
445 }
446
447 p[jss::binary] = true;
448 json::Value result{env.rpc("json", "account_tx", to_string(p))};
449 BEAST_EXPECT(result[jss::result][jss::status] == "success");
450
451 p[jss::forward] = "true";
452 if (apiVersion < 2u)
453 {
454 BEAST_EXPECT(result[jss::result][jss::status] == "success");
455 }
456 else
457 {
458 BEAST_EXPECT(isErr(
459 env.rpc("json", "account_tx", to_string(p)),
461 "Invalid field 'forward'."));
462 }
463
464 p[jss::forward] = false;
465 result = env.rpc("json", "account_tx", to_string(p));
466 BEAST_EXPECT(result[jss::result][jss::status] == "success");
467 }
468 // test limit with malformed values
469 {
470 json::Value p{jParams};
471
472 // Test case: limit = 0 should fail (below minimum)
473 p[jss::limit] = 0;
474 BEAST_EXPECT(isErr(
475 env.rpc("json", "account_tx", to_string(p)),
477 "Invalid field 'limit'."));
478
479 // Test case: limit = 1.2 should fail (not an integer)
480 p[jss::limit] = 1.2;
481 BEAST_EXPECT(isErr(
482 env.rpc("json", "account_tx", to_string(p)),
484 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
485
486 // Test case: limit = "10" should fail (string instead of integer)
487 p[jss::limit] = "10";
488 BEAST_EXPECT(isErr(
489 env.rpc("json", "account_tx", to_string(p)),
491 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
492
493 // Test case: limit = true should fail (boolean instead of integer)
494 p[jss::limit] = true;
495 BEAST_EXPECT(isErr(
496 env.rpc("json", "account_tx", to_string(p)),
498 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
499
500 // Test case: limit = false should fail (boolean instead of integer)
501 p[jss::limit] = false;
502 BEAST_EXPECT(isErr(
503 env.rpc("json", "account_tx", to_string(p)),
505 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
506
507 // Test case: limit = -1 should fail (negative number)
508 p[jss::limit] = -1;
509 BEAST_EXPECT(isErr(
510 env.rpc("json", "account_tx", to_string(p)),
512 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
513
514 // Test case: limit = [] should fail (array instead of integer)
515 p[jss::limit] = json::Value(json::ValueType::Array);
516 BEAST_EXPECT(isErr(
517 env.rpc("json", "account_tx", to_string(p)),
519 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
520
521 // Test case: limit = {} should fail (object instead of integer)
522 p[jss::limit] = json::Value(json::ValueType::Object);
523 BEAST_EXPECT(isErr(
524 env.rpc("json", "account_tx", to_string(p)),
526 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
527
528 // Test case: limit = "malformed" should fail (malformed string)
529 p[jss::limit] = "malformed";
530 BEAST_EXPECT(isErr(
531 env.rpc("json", "account_tx", to_string(p)),
533 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
534
535 // Test case: limit = ["limit"] should fail (array with string)
536 p[jss::limit] = json::Value(json::ValueType::Array);
537 p[jss::limit].append("limit");
538 BEAST_EXPECT(isErr(
539 env.rpc("json", "account_tx", to_string(p)),
541 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
542
543 // Test case: limit = {"limit": 10} should fail (object with
544 // property)
545 p[jss::limit] = json::Value(json::ValueType::Object);
546 p[jss::limit][jss::limit] = 10;
547 BEAST_EXPECT(isErr(
548 env.rpc("json", "account_tx", to_string(p)),
550 rpc::expectedFieldMessage(jss::limit, "unsigned integer")));
551
552 // Test case: limit = 10 should succeed (valid integer)
553 p[jss::limit] = 10;
554 BEAST_EXPECT(
555 env.rpc("json", "account_tx", to_string(p))[jss::result][jss::status] == "success");
556 }
557 }
558
559 void
561 {
562 testcase("Contents");
563
564 // Get results for all transaction types that can be associated
565 // with an account. Start by generating all transaction types.
566 using namespace test::jtx;
567 using namespace std::chrono_literals;
568
569 Env env(*this);
570 Account const alice{"alice"};
571 Account const alie{"alie"};
572 Account const gw{"gw"};
573 auto const usd{gw["USD"]};
574
575 env.fund(XRP(1000000), alice, gw);
576 env.close();
577
578 // AccountSet
579 env(noop(alice));
580
581 // Payment
582 env(pay(alice, gw, XRP(100)));
583
584 // Regular key set
585 env(regkey(alice, alie));
586 env.close();
587
588 // Trust and Offers
589 env(trust(alice, usd(200)), Sig(alie));
590 std::uint32_t const offerSeq{env.seq(alice)};
591 env(offer(alice, usd(50), XRP(150)), Sig(alie));
592 env.close();
593
594 env(offerCancel(alice, offerSeq), Sig(alie));
595 env.close();
596
597 // SignerListSet
598 env(signers(alice, 1, {{"bogie", 1}, {"demon", 1}}), Sig(alie));
599
600 // Escrow
601 {
602 // Create an escrow. Requires either a CancelAfter or FinishAfter.
603 auto const escrow =
604 [](Account const& account, Account const& to, STAmount const& amount) {
606 escrow[jss::TransactionType] = jss::EscrowCreate;
607 escrow[jss::Account] = account.human();
608 escrow[jss::Destination] = to.human();
609 escrow[jss::Amount] = amount.getJson(JsonOptions::Values::None);
610 return escrow;
611 };
612
613 NetClock::time_point const nextTime{env.now() + 2s};
614
615 json::Value escrowWithFinish{escrow(alice, alice, XRP(500))};
616 escrowWithFinish[sfFinishAfter.jsonName] = nextTime.time_since_epoch().count();
617
618 std::uint32_t const escrowFinishSeq{env.seq(alice)};
619 env(escrowWithFinish, Sig(alie));
620
621 json::Value escrowWithCancel{escrow(alice, alice, XRP(500))};
622 escrowWithCancel[sfFinishAfter.jsonName] = nextTime.time_since_epoch().count();
623 escrowWithCancel[sfCancelAfter.jsonName] = nextTime.time_since_epoch().count() + 1;
624
625 std::uint32_t const escrowCancelSeq{env.seq(alice)};
626 env(escrowWithCancel, Sig(alie));
627 env.close();
628
629 {
630 json::Value escrowFinish;
631 escrowFinish[jss::TransactionType] = jss::EscrowFinish;
632 escrowFinish[jss::Account] = alice.human();
633 escrowFinish[sfOwner.jsonName] = alice.human();
634 escrowFinish[sfOfferSequence.jsonName] = escrowFinishSeq;
635 env(escrowFinish, Sig(alie));
636 }
637 {
638 json::Value escrowCancel;
639 escrowCancel[jss::TransactionType] = jss::EscrowCancel;
640 escrowCancel[jss::Account] = alice.human();
641 escrowCancel[sfOwner.jsonName] = alice.human();
642 escrowCancel[sfOfferSequence.jsonName] = escrowCancelSeq;
643 env(escrowCancel, Sig(alie));
644 }
645 env.close();
646 }
647
648 // PayChan
649 {
650 std::uint32_t const payChanSeq{env.seq(alice)};
651 json::Value payChanCreate;
652 payChanCreate[jss::TransactionType] = jss::PaymentChannelCreate;
653 payChanCreate[jss::Account] = alice.human();
654 payChanCreate[jss::Destination] = gw.human();
655 payChanCreate[jss::Amount] = XRP(500).value().getJson(JsonOptions::Values::None);
656 payChanCreate[sfSettleDelay.jsonName] = NetClock::duration{100s}.count();
657 payChanCreate[sfPublicKey.jsonName] = strHex(alice.pk().slice());
658 env(payChanCreate, Sig(alie));
659 env.close();
660
661 std::string const payChanIndex{
662 strHex(keylet::payChannel(alice, gw, SeqProxy::rawSequence(payChanSeq)).key),
663 };
664
665 {
666 json::Value payChanFund;
667 payChanFund[jss::TransactionType] = jss::PaymentChannelFund;
668 payChanFund[jss::Account] = alice.human();
669 payChanFund[sfChannel.jsonName] = payChanIndex;
670 payChanFund[jss::Amount] = XRP(200).value().getJson(JsonOptions::Values::None);
671 env(payChanFund, Sig(alie));
672 env.close();
673 }
674 {
675 json::Value payChanClaim;
676 payChanClaim[jss::TransactionType] = jss::PaymentChannelClaim;
677 payChanClaim[jss::Flags] = tfClose;
678 payChanClaim[jss::Account] = gw.human();
679 payChanClaim[sfChannel.jsonName] = payChanIndex;
680 payChanClaim[sfPublicKey.jsonName] = strHex(alice.pk().slice());
681 env(payChanClaim);
682 env.close();
683 }
684 }
685
686 // Check
687 {
688 auto const aliceCheckId =
689 keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key;
690 env(check::create(alice, gw, XRP(300)), Sig(alie));
691
692 auto const gwCheckId = keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key;
693 env(check::create(gw, alice, XRP(200)));
694 env.close();
695
696 env(check::cash(alice, gwCheckId, XRP(200)), Sig(alie));
697 env(check::cancel(alice, aliceCheckId), Sig(alie));
698 env.close();
699 }
700 {
701 // Deposit pre-authorization with a Ticket.
702 std::uint32_t const tktSeq{env.seq(alice) + 1};
703 env(ticket::create(alice, 1), Sig(alie));
704 env.close();
705
706 env(deposit::auth(alice, gw), ticket::Use(tktSeq), Sig(alie));
707 env.close();
708 }
709
710 // Setup is done. Look at the transactions returned by account_tx.
711 json::Value params;
712 params[jss::account] = alice.human();
713 params[jss::ledger_index_min] = -1;
714 params[jss::ledger_index_max] = -1;
715
716 json::Value const result{env.rpc("json", "account_tx", to_string(params))};
717
718 BEAST_EXPECT(result[jss::result][jss::status] == "success");
719 BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
720
721 json::Value const& txs{result[jss::result][jss::transactions]};
722
723 // clang-format off
724 // Do a sanity check on each returned transaction. They should
725 // be returned in the reverse order of application to the ledger.
726 static const NodeSanity kSanity[]{
727 // txType, created, deleted, modified
728 {0, jss::DepositPreauth, {jss::DepositPreauth}, {jss::Ticket}, {jss::AccountRoot, jss::DirectoryNode}},
729 {1, jss::TicketCreate, {jss::Ticket}, {}, {jss::AccountRoot, jss::DirectoryNode}},
730 {2, jss::CheckCancel, {}, {jss::Check}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
731 {3, jss::CheckCash, {}, {jss::Check}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
732 {4, jss::CheckCreate, {jss::Check}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
733 {5, jss::CheckCreate, {jss::Check}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
734 {6, jss::PaymentChannelClaim, {}, {jss::PayChannel}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
735 {7, jss::PaymentChannelFund, {}, {}, {jss::AccountRoot, jss::PayChannel}},
736 {8, jss::PaymentChannelCreate, {jss::PayChannel}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
737 {9, jss::EscrowCancel, {}, {jss::Escrow}, {jss::AccountRoot, jss::DirectoryNode}},
738 {10, jss::EscrowFinish, {}, {jss::Escrow}, {jss::AccountRoot, jss::DirectoryNode}},
739 {11, jss::EscrowCreate, {jss::Escrow}, {}, {jss::AccountRoot, jss::DirectoryNode}},
740 {12, jss::EscrowCreate, {jss::Escrow}, {}, {jss::AccountRoot, jss::DirectoryNode}},
741 {13, jss::SignerListSet, {jss::SignerList}, {}, {jss::AccountRoot, jss::DirectoryNode}},
742 {14, jss::OfferCancel, {}, {jss::Offer, jss::DirectoryNode}, {jss::AccountRoot, jss::DirectoryNode}},
743 {15, jss::OfferCreate, {jss::Offer, jss::DirectoryNode}, {}, {jss::AccountRoot, jss::DirectoryNode}},
744 {16, jss::TrustSet, {jss::RippleState, jss::DirectoryNode, jss::DirectoryNode}, {}, {jss::AccountRoot, jss::AccountRoot}},
745 {17, jss::SetRegularKey, {}, {}, {jss::AccountRoot}},
746 {18, jss::Payment, {}, {}, {jss::AccountRoot, jss::AccountRoot}},
747 {19, jss::AccountSet, {}, {}, {jss::AccountRoot}},
748 {20, jss::AccountSet, {}, {}, {jss::AccountRoot}},
749 {21, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}},
750 };
751 // clang-format on
752
753 BEAST_EXPECT(std::size(kSanity) == result[jss::result][jss::transactions].size());
754
755 for (unsigned int index{0}; index < std::size(kSanity); ++index)
756 {
757 checkSanity(txs[index], kSanity[index]);
758 }
759 }
760
761 void
763 {
764 testcase("AccountDelete");
765
766 // Verify that if an account is resurrected then the account_tx RPC
767 // command still recovers all transactions on that account before
768 // and after resurrection.
769 using namespace test::jtx;
770 using namespace std::chrono_literals;
771
772 Env env(*this);
773 Account const alice{"alice"};
774 Account const becky{"becky"};
775
776 env.fund(XRP(10000), alice, becky);
777 env.close();
778
779 // Verify that becky's account root is present.
780 Keylet const beckyAcctKey{keylet::account(becky.id())};
781 BEAST_EXPECT(env.closed()->exists(beckyAcctKey));
782
783 // becky does an AccountSet .
784 env(noop(becky));
785
786 // Close enough ledgers to be able to delete becky's account.
787 std::uint32_t const ledgerCount{env.current()->seq() + 257 - env.seq(becky)};
788
789 for (std::uint32_t i = 0; i < ledgerCount; ++i)
790 env.close();
791
792 auto const beckyPreDelBalance{env.balance(becky)};
793
794 auto const acctDelFee{drops(env.current()->fees().increment)};
795 env(acctdelete(becky, alice), Fee(acctDelFee));
796 env.close();
797
798 // Verify that becky's account root is gone.
799 BEAST_EXPECT(!env.closed()->exists(beckyAcctKey));
800 env.close();
801
802 // clang-format off
803 // Do a sanity check on each returned transaction. They should
804 // be returned in the reverse order of application to the ledger.
805 //
806 // Note that the first two transactions in sanity have not occurred
807 // yet. We'll see those after becky's account is resurrected.
808 static const NodeSanity kSanity[]
809 {
810 // txType, created, deleted, modified
811/* becky pays alice */ { 0, jss::Payment, {}, {}, {jss::AccountRoot, jss::AccountRoot}},
812/* alice resurrects becky's acct */ { 1, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}},
813/* becky deletes her account */ { 2, jss::AccountDelete, {}, {jss::AccountRoot}, {jss::AccountRoot}},
814/* becky's noop */ { 3, jss::AccountSet, {}, {}, {jss::AccountRoot}},
815/* "fund" sets flags */ { 4, jss::AccountSet, {}, {}, {jss::AccountRoot}},
816/* "fund" creates becky's acct */ { 5, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}},
817 };
818 // clang-format on
819
820 // Verify that we can recover becky's account_tx information even
821 // after the account is deleted.
822 {
823 json::Value params;
824 params[jss::account] = becky.human();
825 params[jss::ledger_index_min] = -1;
826 params[jss::ledger_index_max] = -1;
827
828 json::Value const result{env.rpc("json", "account_tx", to_string(params))};
829
830 BEAST_EXPECT(result[jss::result][jss::status] == "success");
831 BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
832
833 // The first two transactions listed in sanity haven't happened yet.
834 static constexpr unsigned int kBeckyDeletedOffset = 2;
835 BEAST_EXPECT(
836 std::size(kSanity) ==
837 result[jss::result][jss::transactions].size() + kBeckyDeletedOffset);
838
839 json::Value const& txs{result[jss::result][jss::transactions]};
840
841 for (unsigned int index = kBeckyDeletedOffset; index < std::size(kSanity); ++index)
842 {
843 checkSanity(txs[index - kBeckyDeletedOffset], kSanity[index]);
844 }
845 }
846
847 // All it takes is a large enough XRP payment to resurrect
848 // becky's account. Try too small a payment.
849 env(pay(alice, becky, drops(env.current()->fees().accountReserve(0, 1)) - drops(1)),
851 env.close();
852
853 // Actually resurrect becky's account.
854 env(pay(alice, becky, XRP(45)));
855 env.close();
856
857 // becky's account root should be back.
858 BEAST_EXPECT(env.closed()->exists(beckyAcctKey));
859 BEAST_EXPECT(env.balance(becky) == XRP(45));
860
861 // becky pays alice.
862 env(pay(becky, alice, XRP(20)));
863 env.close();
864
865 // Setup is done. Look at the transactions returned by account_tx.
866 // Verify that account_tx locates all of becky's transactions.
867 json::Value params;
868 params[jss::account] = becky.human();
869 params[jss::ledger_index_min] = -1;
870 params[jss::ledger_index_max] = -1;
871
872 json::Value const result{env.rpc("json", "account_tx", to_string(params))};
873
874 BEAST_EXPECT(result[jss::result][jss::status] == "success");
875 BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
876
877 BEAST_EXPECT(std::size(kSanity) == result[jss::result][jss::transactions].size());
878
879 json::Value const& txs{result[jss::result][jss::transactions]};
880
881 for (unsigned int index = 0; index < std::size(kSanity); ++index)
882 {
883 checkSanity(txs[index], kSanity[index]);
884 }
885 }
886
887 void
889 {
890 testcase("MPT");
891
892 using namespace test::jtx;
893 using namespace std::chrono_literals;
894
895 auto cfg = makeConfig();
896 cfg->fees.referenceFee = 10;
897 Env env(*this, std::move(cfg));
898
899 Account const alice{"alice"};
900 Account const bob{"bob"};
901 Account const carol{"carol"};
902
903 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
904
905 // check the latest mpt-related txn is in alice's account history
906 auto const checkAliceAcctTx = [&](size_t size, json::StaticString txType) {
907 json::Value params;
908 params[jss::account] = alice.human();
909 params[jss::limit] = 100;
910 auto const jv = env.rpc("json", "account_tx", to_string(params))[jss::result];
911
912 BEAST_EXPECT(jv[jss::transactions].size() == size);
913 auto const& tx0(jv[jss::transactions][0u][jss::tx]);
914 BEAST_EXPECT(tx0[jss::TransactionType] == txType);
915
916 std::string const txHash{
917 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
918 };
919 BEAST_EXPECT(tx0[jss::hash] == txHash);
920 };
921
922 // alice creates issuance
923 mptAlice.create({
924 .ownerCount = 1,
925 .holderCount = 0,
926 .flags = tfMPTCanClawback | tfMPTRequireAuth | tfMPTCanTransfer,
927 });
928
929 checkAliceAcctTx(3, jss::MPTokenIssuanceCreate);
930
931 // bob creates a MPToken;
932 mptAlice.authorize({.account = bob});
933 checkAliceAcctTx(4, jss::MPTokenAuthorize);
934 env.close();
935
936 // TODO: windows pipeline fails validation for the hardcoded ledger hash
937 // due to having different test config, it can be uncommented after
938 // figuring out what happened
939 //
940 // ledger hash should be fixed regardless any change to account history
941 // BEAST_EXPECT(
942 // to_string(env.closed()->header().hash) ==
943 // "0BD507BB87D3C0E73B462485E6E381798A8C82FC49BF17FE39C60E08A1AF035D");
944
945 // alice authorizes bob
946 mptAlice.authorize({.account = alice, .holder = bob});
947 checkAliceAcctTx(5, jss::MPTokenAuthorize);
948
949 // carol creates a MPToken;
950 mptAlice.authorize({.account = carol});
951 checkAliceAcctTx(6, jss::MPTokenAuthorize);
952
953 // alice authorizes carol
954 mptAlice.authorize({.account = alice, .holder = carol});
955 checkAliceAcctTx(7, jss::MPTokenAuthorize);
956
957 // alice pays bob 100 tokens
958 mptAlice.pay(alice, bob, 100);
959 checkAliceAcctTx(8, jss::Payment);
960
961 // bob pays carol 10 tokens
962 mptAlice.pay(bob, carol, 10);
963 checkAliceAcctTx(9, jss::Payment);
964 }
965
966 void
968 {
969 testcase("Delegation Filtering");
970
971 using namespace test::jtx;
972
973 Env env(*this);
974 Account const alice{"alice"};
975 Account const bob{"bob"};
976 Account const carol{"carol"};
977
978 env.fund(XRP(10000), alice, bob, carol);
979 env.close();
980
981 // Normal TX: Alice pays Carol (Signed by Alice's Master Key)
982 env(pay(alice, carol, XRP(10)));
983 env.close();
984
985 // Setup Delegation: Alice allows Bob to sign Payments for her
986 env(delegate::set(alice, bob, {"Payment"}));
987 env.close();
988
989 // Delegated TX: Alice pays Bob (Signed by Bob using Delegation)
990 env(pay(alice, bob, XRP(20)), delegate::As(bob));
991 env.close();
992
993 // Normal TX: Bob pays Carol (Signed by Bob for himself)
994 env(pay(bob, carol, XRP(30)));
995 env.close();
996
997 auto const countTxs = [&](AccountID const& account,
998 json::Value const& delegateParams,
999 std::optional<std::uint32_t> const limit = std::nullopt) -> int {
1000 int count = 0;
1001 json::Value marker;
1002 bool haveMarker = false;
1003 int pages = 0;
1004
1005 while (true)
1006 {
1007 json::Value params;
1008 params[jss::account] = toBase58(account);
1009 params[jss::ledger_index_min] = -1;
1010 params[jss::ledger_index_max] = -1;
1011
1012 if (!delegateParams.isNull())
1013 params[jss::delegate] = delegateParams;
1014 if (limit)
1015 params[jss::limit] = *limit;
1016 if (haveMarker)
1017 params[jss::marker] = marker;
1018
1019 auto const res = env.rpc("json", "account_tx", to_string(params));
1020 auto const& result = res[jss::result];
1021
1022 if (result.isMember(jss::transactions))
1023 count += result[jss::transactions].size();
1024
1025 if (!limit || !result.isMember(jss::marker))
1026 break;
1027
1028 marker = result[jss::marker];
1029 haveMarker = true;
1030 ++pages;
1031 if (!BEAST_EXPECT(pages < 20))
1032 break;
1033 }
1034
1035 return count;
1036 };
1037
1038 auto const checkError = [&](json::Value const& delegateParams,
1039 std::string const& errToken,
1040 ErrorCodeI code,
1041 std::string const& errMessage) {
1042 json::Value params;
1043 params[jss::account] = alice.human();
1044 params[jss::delegate] = delegateParams;
1045 auto res = env.rpc("json", "account_tx", to_string(params));
1046 BEAST_EXPECT(res[jss::result][jss::error] == errToken);
1047 BEAST_EXPECT(res[jss::result][jss::error_code] == code);
1048 BEAST_EXPECT(res[jss::result][jss::error_message] == errMessage);
1049 };
1050
1051 // Filter: Delegatee. Expects TX #2 (Signed by Bob)
1052 {
1053 json::Value p;
1054 p[jss::delegate_filter] = "actor";
1055 BEAST_EXPECT(countTxs(alice.id(), p) == 1);
1056 }
1057
1058 // Filter: Delegatee + Counterparty Bob. Expects TX #2.
1059 {
1060 json::Value p;
1061 p[jss::delegate_filter] = "actor";
1062 p[jss::counter_party] = bob.human();
1063 BEAST_EXPECT(countTxs(alice.id(), p) == 1);
1064 }
1065
1066 // Filter: Delegatee + Counterparty Carol. Expects 0.
1067 {
1068 json::Value p;
1069 p[jss::delegate_filter] = "actor";
1070 p[jss::counter_party] = carol.human();
1071 BEAST_EXPECT(countTxs(alice.id(), p) == 0);
1072 }
1073
1074 // Filter: Delegator. Expects TX #2.
1075 // (Bob signed it, but Alice is the owner).
1076 {
1077 json::Value p;
1078 p[jss::delegate_filter] = "authorizer";
1079 BEAST_EXPECT(countTxs(bob.id(), p) == 1);
1080 }
1081
1082 // Filter: Delegator + Counterparty Alice. Expects TX #2.
1083 {
1084 json::Value p;
1085 p[jss::delegate_filter] = "authorizer";
1086 p[jss::counter_party] = alice.human();
1087 BEAST_EXPECT(countTxs(bob.id(), p) == 1);
1088 }
1089
1090 // Filter: Authorizer. Expect: None.
1091 // TX #2 has sfDelegate present, but Alice is the delegator/owner, not
1092 // the delegate signer
1093 {
1094 json::Value p;
1095 p[jss::delegate_filter] = "authorizer";
1096 BEAST_EXPECT(countTxs(alice.id(), p) == 0);
1097 }
1098
1099 // Query Bob (Signer), Filter: Delegator, Counterparty: Carol
1100 // Expect: None (Alice is Owner, not Carol)
1101 {
1102 json::Value p;
1103 p[jss::delegate_filter] = "authorizer";
1104 p[jss::counter_party] = carol.human();
1105 BEAST_EXPECT(countTxs(bob.id(), p) == 0);
1106 }
1107
1108 // Query Bob (Signer), Filter: Delegatee
1109 // Expect: None. Bob did not employ a delegatee for his own TXs (TX C).
1110 {
1111 json::Value p;
1112 p[jss::delegate_filter] = "actor";
1113 BEAST_EXPECT(countTxs(bob.id(), p) == 0);
1114 }
1115
1116 // "delegate" is not an object (e.g., string)
1117 {
1118 json::Value const p = "not_an_object";
1119 checkError(p, "invalidParams", RpcInvalidParams, "Invalid field 'delegate'.");
1120 }
1121
1122 // Missing "delegate_filter" inside object
1123 {
1125 checkError(p, "invalidParams", RpcInvalidParams, "Invalid field 'delegate_filter'.");
1126 }
1127
1128 // "delegate_filter" is not a string (e.g., int)
1129 {
1130 json::Value p;
1131 p[jss::delegate_filter] = 123;
1132 checkError(p, "invalidParams", RpcInvalidParams, "Invalid field 'delegate_filter'.");
1133 }
1134
1135 // "delegate_filter" has invalid value
1136 {
1137 json::Value p;
1138 p[jss::delegate_filter] = "random_string";
1139 checkError(p, "invalidParams", RpcInvalidParams, "Invalid field 'delegate_filter'.");
1140 }
1141
1142 // "counterparty" is not a string
1143 {
1144 json::Value p;
1145 p[jss::delegate_filter] = "actor";
1146 p[jss::counter_party] = 123;
1147 checkError(p, "invalidParams", RpcInvalidParams, "Invalid field 'counter_party'.");
1148 }
1149
1150 // "counterparty" is malformed base58
1151 {
1152 json::Value p;
1153 p[jss::delegate_filter] = "actor";
1154 p[jss::counter_party] = "not_an_account";
1155 checkError(p, "actMalformed", RpcActMalformed, "Account malformed.");
1156 }
1157
1158 // Multi-signed non-delegated TX: Alice pays Carol via multi-sig.
1159 // sfDelegate is absent and sfSigningPubKey is empty — the filter
1160 // must skip it without crashing.
1161 {
1162 Account const daria{"daria"};
1163 Account const edward{"edward"};
1164 env.fund(XRP(1000), daria, edward);
1165 env.close();
1166 env(signers(alice, 2, {{daria, 1}, {edward, 1}}));
1167 env.close();
1168 env(pay(alice, carol, XRP(1)),
1169 Fee(drops(env.current()->fees().increment * 2)),
1170 Msig(daria, edward));
1171 env.close();
1172
1173 // Alice's actor filter should still see only the 1 delegated tx,
1174 // not the multi-signed one.
1175 json::Value p;
1176 p[jss::delegate_filter] = "actor";
1177 BEAST_EXPECT(countTxs(alice.id(), p) == 1);
1178 }
1179
1180 // Regular-key-signed non-delegated TX: Alice pays Bob, signed by Bob
1181 // as Alice's regular key. This must not be treated as delegation.
1182 {
1183 env(regkey(alice, bob));
1184 env.close();
1185 env(pay(alice, bob, XRP(1)));
1186 env.close();
1187
1188 json::Value actorFilter;
1189 actorFilter[jss::delegate_filter] = "actor";
1190 BEAST_EXPECT(countTxs(alice.id(), actorFilter) == 1);
1191 // limit: 1 forces pagination past newer non-delegated rows.
1192 BEAST_EXPECT(countTxs(alice.id(), actorFilter, 1) == 1);
1193
1194 actorFilter[jss::counter_party] = bob.human();
1195 BEAST_EXPECT(countTxs(alice.id(), actorFilter) == 1);
1196 BEAST_EXPECT(countTxs(alice.id(), actorFilter, 1) == 1);
1197
1198 json::Value authorizerFilter;
1199 authorizerFilter[jss::delegate_filter] = "authorizer";
1200 BEAST_EXPECT(countTxs(bob.id(), authorizerFilter) == 1);
1201 BEAST_EXPECT(countTxs(bob.id(), authorizerFilter, 1) == 1);
1202
1203 authorizerFilter[jss::counter_party] = alice.human();
1204 BEAST_EXPECT(countTxs(bob.id(), authorizerFilter) == 1);
1205 BEAST_EXPECT(countTxs(bob.id(), authorizerFilter, 1) == 1);
1206 }
1207
1208 // Pagination marker/delegate-filter consistency. A marker returned by a
1209 // delegate-filtered query carries a `delegate` flag and is only valid
1210 // for a follow-up request that repeats the filter; mixing the two
1211 // marker conventions must be rejected with invalidParams.
1212 {
1213 json::Value actorFilter;
1214 actorFilter[jss::delegate_filter] = "actor";
1215
1216 // Obtain a delegate-filtered marker (limit 1 forces pagination).
1217 json::Value dp;
1218 dp[jss::account] = alice.human();
1219 dp[jss::ledger_index_min] = -1;
1220 dp[jss::ledger_index_max] = -1;
1221 dp[jss::delegate] = actorFilter;
1222 dp[jss::limit] = 1;
1223 auto const dRes = env.rpc("json", "account_tx", to_string(dp));
1224 BEAST_EXPECT(dRes[jss::result].isMember(jss::marker));
1225 json::Value const delegateMarker = dRes[jss::result][jss::marker];
1226 BEAST_EXPECT(
1227 delegateMarker.isMember(jss::delegate) && delegateMarker[jss::delegate].asBool());
1228
1229 // Reusing a delegate marker without the delegate filter is rejected.
1230 {
1231 json::Value p;
1232 p[jss::account] = alice.human();
1233 p[jss::ledger_index_min] = -1;
1234 p[jss::ledger_index_max] = -1;
1235 p[jss::limit] = 1;
1236 p[jss::marker] = delegateMarker;
1237 auto const r = env.rpc("json", "account_tx", to_string(p));
1238 BEAST_EXPECT(r[jss::result][jss::error] == "invalidParams");
1239 BEAST_EXPECT(r[jss::result][jss::error_code] == RpcInvalidParams);
1240 BEAST_EXPECT(
1241 r[jss::result][jss::error_message] ==
1242 "Do not mix delegate and non-delegate pagination markers in account_tx; "
1243 "repeat the same `delegate` object when using a delegate marker.");
1244 }
1245
1246 // Obtain a non-delegate marker; it must not carry the flag.
1247 json::Value np;
1248 np[jss::account] = alice.human();
1249 np[jss::ledger_index_min] = -1;
1250 np[jss::ledger_index_max] = -1;
1251 np[jss::limit] = 1;
1252 auto const nRes = env.rpc("json", "account_tx", to_string(np));
1253 BEAST_EXPECT(nRes[jss::result].isMember(jss::marker));
1254 json::Value const normalMarker = nRes[jss::result][jss::marker];
1255 BEAST_EXPECT(!normalMarker.isMember(jss::delegate));
1256
1257 // Reusing a non-delegate marker with a delegate filter is rejected.
1258 {
1259 json::Value p;
1260 p[jss::account] = alice.human();
1261 p[jss::ledger_index_min] = -1;
1262 p[jss::ledger_index_max] = -1;
1263 p[jss::limit] = 1;
1264 p[jss::delegate] = actorFilter;
1265 p[jss::marker] = normalMarker;
1266 auto const r = env.rpc("json", "account_tx", to_string(p));
1267 BEAST_EXPECT(r[jss::result][jss::error] == "invalidParams");
1268 BEAST_EXPECT(r[jss::result][jss::error_code] == RpcInvalidParams);
1269 BEAST_EXPECT(
1270 r[jss::result][jss::error_message] ==
1271 "Do not mix delegate and non-delegate pagination markers in account_tx; "
1272 "repeat the same `delegate` object when using a delegate marker.");
1273 }
1274 }
1275 }
1276
1277 void
1279 {
1280 testcase("Delegation filter with multi-signed delegatee");
1281 using namespace test::jtx;
1282
1283 Env env(*this);
1284 Account const alice{"alice"};
1285 Account const bob{"bob"};
1286 Account const carol{"carol"};
1287 Account const daria{"daria"};
1288 Account const edward{"edward"};
1289
1290 env.fund(XRP(10000), alice, bob, carol, daria, edward);
1291 env.close();
1292
1293 // Bob's identity is established via multi-sig (daria + edward)
1294 env(signers(bob, 2, {{daria, 1}, {edward, 1}}));
1295 env.close();
1296
1297 env(delegate::set(alice, bob, {"Payment"}));
1298 env.close();
1299
1300 // Delegated tx: Alice pays Carol, Bob signs via multi-sig
1301 env(pay(alice, carol, XRP(10)), Fee(XRP(1)), delegate::As(bob), Msig(daria, edward));
1302 env.close();
1303
1304 auto const countTxs = [&](AccountID const& account,
1305 json::Value const& delegateParams) -> int {
1306 json::Value params;
1307 params[jss::account] = toBase58(account);
1308 params[jss::ledger_index_min] = -1;
1309 params[jss::ledger_index_max] = -1;
1310 params[jss::delegate] = delegateParams;
1311
1312 auto const res = env.rpc("json", "account_tx", to_string(params));
1313
1314 if (res[jss::result].isMember(jss::transactions))
1315 return res[jss::result][jss::transactions].size();
1316 return 0;
1317 };
1318
1319 // Alice (owner) finds the tx with actor filter
1320 {
1321 json::Value p;
1322 p[jss::delegate_filter] = "actor";
1323 BEAST_EXPECT(countTxs(alice.id(), p) == 1);
1324 }
1325
1326 // Alice (owner) + counterparty Bob finds the tx
1327 {
1328 json::Value p;
1329 p[jss::delegate_filter] = "actor";
1330 p[jss::counter_party] = bob.human();
1331 BEAST_EXPECT(countTxs(alice.id(), p) == 1);
1332 }
1333
1334 // Bob (delegatee) finds the tx with authorizer filter
1335 {
1336 json::Value p;
1337 p[jss::delegate_filter] = "authorizer";
1338 BEAST_EXPECT(countTxs(bob.id(), p) == 1);
1339 }
1340
1341 // Bob (delegatee) + counterparty Alice finds the tx
1342 {
1343 json::Value p;
1344 p[jss::delegate_filter] = "authorizer";
1345 p[jss::counter_party] = alice.human();
1346 BEAST_EXPECT(countTxs(bob.id(), p) == 1);
1347 }
1348 }
1349
1350 void
1352 {
1353 testcase("Delegation filter marker within a single query page");
1354
1355 using namespace test::jtx;
1356
1357 Env env(*this);
1358 Account const alice{"alice"};
1359 Account const bob{"bob"};
1360 Account const carol{"carol"};
1361
1362 env.fund(XRP(10000), alice, bob, carol);
1363 env.close();
1364
1365 env(delegate::set(alice, bob, {"Payment"}));
1366 env.close();
1367
1368 auto const startLedger = env.closed()->header().seq + 1;
1369
1370 env(pay(alice, carol, XRP(1)), delegate::As(bob));
1371 env.close();
1372 env(pay(alice, carol, XRP(1)), delegate::As(bob));
1373 env.close();
1374
1375 json::Value p;
1376 p[jss::delegate_filter] = "actor";
1377
1378 json::Value params;
1379 params[jss::account] = alice.human();
1380 params[jss::ledger_index_min] = startLedger;
1381 params[jss::ledger_index_max] = -1;
1382 params[jss::delegate] = p;
1383 params[jss::limit] = 1;
1384
1385 auto const res = env.rpc("json", "account_tx", to_string(params));
1386 auto const& result = res[jss::result];
1387
1388 // The first page emits only the first delegated payment. (as page limit is set to 1)
1389 BEAST_EXPECT(result[jss::transactions].size() == 1);
1390 BEAST_EXPECT(result.isMember(jss::marker));
1391
1392 // Following the marker resumes right after the first payment and
1393 // returns the second one.
1394 json::Value page2 = params;
1395 page2[jss::marker] = result[jss::marker];
1396 auto const res2 = env.rpc("json", "account_tx", to_string(page2));
1397 BEAST_EXPECT(res2[jss::result][jss::transactions].size() == 1);
1398 }
1399
1400 void
1402 {
1403 // test all sponsored transactions are in sponsor and sponsee's account
1404 // tx list
1405 testcase("Sponsorship");
1406
1407 using namespace test::jtx;
1408 Env env(*this);
1409 Account const alice("alice");
1410 Account const sponsor("sponsor");
1411 Account const sponsor2("sponsor2");
1412 env.fund(XRP(10000), alice, sponsor, sponsor2);
1413 env.close();
1414
1415 // check the latest sponsorship-related txn is in account tx list
1416 auto const checkTx = [&](Account const& account, json::StaticString txType) {
1417 json::Value params;
1418 params[jss::account] = account.human();
1419 params[jss::limit] = 100;
1420 auto const jv = env.rpc("json", "account_tx", to_string(params))[jss::result];
1421
1422 auto const& tx0(jv[jss::transactions][0u][jss::tx]);
1423 BEAST_EXPECT(tx0[jss::TransactionType] == txType);
1424
1425 std::string const txHash{
1426 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
1427 };
1428 BEAST_EXPECT(tx0[jss::hash] == txHash);
1429 };
1430
1431 // fee sponsorship
1432 env(noop(alice), sponsor::As(sponsor, spfSponsorFee), Sig(sfSponsorSignature, sponsor));
1433 env.close();
1434 checkTx(alice, jss::AccountSet);
1435 checkTx(sponsor, jss::AccountSet);
1436
1437 // set sponsor
1438 env(sponsor::set(sponsor, 0, 100, XRP(100)), sponsor::SponseeAcc(alice), Ter(tesSUCCESS));
1439 env.close();
1440 checkTx(alice, jss::SponsorshipSet);
1441 checkTx(sponsor, jss::SponsorshipSet);
1442
1443 // create an object with sponsor
1444 auto const checkId = keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1446 env.close();
1447 checkTx(alice, jss::CheckCreate);
1448 checkTx(sponsor, jss::CheckCreate);
1449
1450 // transfer object sponsorship
1451 env(sponsor::transfer(alice, tfSponsorshipReassign, checkId),
1452 sponsor::As(sponsor2, spfSponsorReserve),
1453 Sig(sfSponsorSignature, sponsor2));
1454 env.close();
1455 checkTx(alice, jss::SponsorshipTransfer);
1456 checkTx(sponsor, jss::SponsorshipTransfer);
1457 checkTx(sponsor2, jss::SponsorshipTransfer);
1458
1459 // delete the sponsored object
1460 env(check::cancel(alice, checkId),
1462 Sig(sfSponsorSignature, sponsor));
1463 env.close();
1464 checkTx(alice, jss::CheckCancel);
1465 checkTx(sponsor, jss::CheckCancel);
1466 checkTx(sponsor2, jss::CheckCancel);
1467
1468 // account sponsorship
1469 env(sponsor::transfer(alice, tfSponsorshipCreate),
1471 Sig(sfSponsorSignature, sponsor));
1472 env.close();
1473 checkTx(alice, jss::SponsorshipTransfer);
1474 checkTx(sponsor, jss::SponsorshipTransfer);
1475 }
1476
1477public:
1478 void
1479 run() override
1480 {
1481 forAllApiVersions([this](unsigned apiVersion) { testParameters(apiVersion); });
1482 testContents();
1484 testMPT();
1489 }
1490};
1492
1493} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Lightweight wrapper to tag static string.
Definition json_value.h:52
Represents a JSON value.
Definition json_value.h:121
bool asBool() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Slice slice() const noexcept
Definition PublicKey.h:115
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
void run() override
Runs the suite.
void testParameters(unsigned int apiVersion)
void checkSanity(json::Value const &txNode, NodeSanity const &sane)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
PublicKey const & pk() const
Return the public key.
Definition jtx/Account.h:84
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
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< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:560
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
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
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 a ticket sequence on a JTx.
Definition ticket.h:36
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
Keylet payChannel(AccountID const &src, AccountID const &dst, SeqProxy const &seq) noexcept
A PaymentChannel.
Definition Indexes.cpp:436
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
The api_version numbers this server serves.
Definition ApiVersion.h:26
std::string expectedFieldMessage(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
ErrorInfo const & getErrorInfo(ErrorCodeI code)
Returns an ErrorInfo that reflects the error code.
json::Value cancel(jtx::Account const &dest, UInt256 const &checkId)
Cancel a check.
Definition check.cpp:39
json::Value cash(jtx::Account const &dest, UInt256 const &checkId, STAmount const &amount)
Cash a check requiring that a specific amount be delivered.
Definition check.cpp:15
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
Definition delegate.cpp:17
json::Value auth(Account const &account, Account const &auth)
Preauthorize for deposit.
Definition deposit.cpp:16
Escrow operations.
Definition escrow.h:25
json::Value transfer(jtx::Account const &account, uint32_t flags, std::optional< UInt256 > const &index)
Definition sponsor.cpp:52
json::Value set(jtx::Account const &account, uint32_t flags, std::optional< int32_t > const reserveCountDelta, std::optional< STAmount > const feeAmountDelta, std::optional< STAmount > const maxFee)
Definition sponsor.cpp:22
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
json::Value regkey(Account const &account, DisabledT)
Disable the regular key.
Definition regkey.cpp:13
json::Value offerCancel(Account const &account, std::uint32_t offerSeq)
Cancel an offer.
Definition offer.cpp:31
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::unique_ptr< Config > makeConfig(std::map< std::string, std::string > extraTxQ={}, std::map< std::string, std::string > extraVoting={})
json::Value acctdelete(Account const &account, Account const &dest)
Delete account.
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
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)
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
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcLgrIdxsInvalid
Definition ErrorCodes.h:95
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcLgrNotValidated
Definition ErrorCodes.h:56
@ RpcInvalidLgrRange
Definition ErrorCodes.h:119
@ RpcLgrIdxMalformed
Definition ErrorCodes.h:96
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:159
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ tecNO_DST_INSUF_XRP
Definition TER.h:302
constexpr FlagValue spfSponsorReserve
Definition TxFlags.h:468
@ tesSUCCESS
Definition TER.h:253
T size(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
json::StaticString token
Definition ErrorCodes.h:194
boost::container::flat_set< std::string > modified
boost::container::flat_set< std::string > created
NodeSanity(int idx, json::StaticString const &t, std::initializer_list< char const * > c, std::initializer_list< char const * > d, std::initializer_list< char const * > m)
boost::container::flat_set< std::string > deleted
T time_since_epoch(T... args)
T to_string(T... args)