-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAccountTx_test.cpp
More file actions
859 lines (715 loc) · 38.1 KB
/
AccountTx_test.cpp
File metadata and controls
859 lines (715 loc) · 38.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
#include <test/jtx.h>
#include <test/jtx/envconfig.h>
#include <xrpl/beast/unit_test.h>
#include <xrpl/beast/unit_test/suite.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/TxFlags.h>
#include <xrpl/protocol/jss.h>
#include <boost/container/flat_set.hpp>
namespace xrpl {
namespace test {
class AccountTx_test : public beast::unit_test::suite
{
// A data structure used to describe the basic structure of a
// transactions array node as returned by the account_tx RPC command.
struct NodeSanity
{
int const index;
Json::StaticString const& txType;
boost::container::flat_set<std::string> created;
boost::container::flat_set<std::string> deleted;
boost::container::flat_set<std::string> modified;
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)
: index(idx), txType(t)
{
auto buildSet = [](auto&& init) {
boost::container::flat_set<std::string> r;
r.reserve(init.size());
for (auto&& s : init)
r.insert(s);
return r;
};
created = buildSet(c);
deleted = buildSet(d);
modified = buildSet(m);
}
};
// A helper method tests can use to validate returned JSON vs NodeSanity.
void
checkSanity(Json::Value const& txNode, NodeSanity const& sane)
{
BEAST_EXPECT(txNode[jss::validated].asBool() == true);
BEAST_EXPECT(txNode[jss::tx][sfTransactionType.jsonName].asString() == sane.txType);
// Make sure all of the expected node types are present.
boost::container::flat_set<std::string> createdNodes;
boost::container::flat_set<std::string> deletedNodes;
boost::container::flat_set<std::string> modifiedNodes;
for (Json::Value const& metaNode : txNode[jss::meta][sfAffectedNodes.jsonName])
{
if (metaNode.isMember(sfCreatedNode.jsonName))
createdNodes.insert(
metaNode[sfCreatedNode.jsonName][sfLedgerEntryType.jsonName].asString());
else if (metaNode.isMember(sfDeletedNode.jsonName))
deletedNodes.insert(
metaNode[sfDeletedNode.jsonName][sfLedgerEntryType.jsonName].asString());
else if (metaNode.isMember(sfModifiedNode.jsonName))
modifiedNodes.insert(
metaNode[sfModifiedNode.jsonName][sfLedgerEntryType.jsonName].asString());
else
fail("Unexpected or unlabeled node type in metadata.", __FILE__, __LINE__);
}
BEAST_EXPECT(createdNodes == sane.created);
BEAST_EXPECT(deletedNodes == sane.deleted);
BEAST_EXPECT(modifiedNodes == sane.modified);
};
void
testParameters(unsigned int apiVersion)
{
testcase("Parameters APIv" + std::to_string(apiVersion));
using namespace test::jtx;
Env env(*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->FEES.reference_fee = 10;
return cfg;
}));
Account A1{"A1"};
env.fund(XRP(10000), A1);
env.close();
// Ledger 3 has the two txs associated with funding the account
// All other ledgers have no txs
auto hasTxs = [apiVersion](Json::Value const& j) {
switch (apiVersion)
{
case 1:
return j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
(j[jss::result][jss::transactions].size() == 2) &&
(j[jss::result][jss::transactions][0u][jss::tx][jss::TransactionType] ==
jss::AccountSet) &&
(j[jss::result][jss::transactions][1u][jss::tx][jss::TransactionType] ==
jss::Payment) &&
(j[jss::result][jss::transactions][1u][jss::tx][jss::DeliverMax] ==
"10000000010") &&
(j[jss::result][jss::transactions][1u][jss::tx][jss::Amount] ==
j[jss::result][jss::transactions][1u][jss::tx][jss::DeliverMax]);
case 2:
case 3:
if (j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
(j[jss::result][jss::transactions].size() == 2) &&
(j[jss::result][jss::transactions][0u][jss::tx_json]
[jss::TransactionType] == jss::AccountSet))
{
auto const& payment = j[jss::result][jss::transactions][1u];
if (apiVersion >= 3)
{
// In API v3, server-added lower-case fields must
// not be in tx_json
return (payment.isMember(jss::tx_json)) &&
(payment[jss::tx_json][jss::TransactionType] == jss::Payment) &&
(payment[jss::tx_json][jss::DeliverMax] == "10000000010") &&
(!payment[jss::tx_json].isMember(jss::Amount)) &&
(!payment[jss::tx_json].isMember(jss::hash)) &&
(!payment[jss::tx_json].isMember(jss::date)) &&
(!payment[jss::tx_json].isMember(jss::ledger_index)) &&
(!payment[jss::tx_json].isMember(jss::ctid)) &&
(payment[jss::hash] ==
"9F3085D85F472D1CC29627F260DF68EDE59D42D1D0C33E345"
"ECF0D4CE981D0A8") &&
(payment[jss::validated] == true) &&
(payment[jss::ledger_index] == 3) &&
(payment[jss::ledger_hash] ==
"5476DCD816EA04CBBA57D47BBF1FC58A5217CC93A5ADD79CB"
"580A5AFDD727E33") &&
(payment[jss::close_time_iso] == "2000-01-01T00:00:10Z");
}
else
{
// In API v2, date and ledger_index are still in
// tx_json for backwards compatibility
return (payment.isMember(jss::tx_json)) &&
(payment[jss::tx_json][jss::TransactionType] == jss::Payment) &&
(payment[jss::tx_json][jss::DeliverMax] == "10000000010") &&
(!payment[jss::tx_json].isMember(jss::Amount)) &&
(!payment[jss::tx_json].isMember(jss::hash)) &&
(payment[jss::tx_json].isMember(jss::date)) &&
(payment[jss::tx_json].isMember(jss::ledger_index)) &&
(payment[jss::hash] ==
"9F3085D85F472D1CC29627F260DF68EDE59D42D1D0C33E345"
"ECF0D4CE981D0A8") &&
(payment[jss::validated] == true) &&
(payment[jss::ledger_index] == 3) &&
(payment[jss::ledger_hash] ==
"5476DCD816EA04CBBA57D47BBF1FC58A5217CC93A5ADD79CB"
"580A5AFDD727E33") &&
(payment[jss::close_time_iso] == "2000-01-01T00:00:10Z");
}
}
else
return false;
default:
return false;
}
};
auto noTxs = [](Json::Value const& j) {
return j.isMember(jss::result) && (j[jss::result][jss::status] == "success") &&
(j[jss::result][jss::transactions].size() == 0);
};
auto isErr = [](Json::Value const& j, error_code_i code) {
return j.isMember(jss::result) && j[jss::result].isMember(jss::error) &&
j[jss::result][jss::error] == RPC::get_error_info(code).token;
};
Json::Value jParams;
jParams[jss::api_version] = apiVersion;
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(jParams)), rpcINVALID_PARAMS));
jParams[jss::account] = "0xDEADBEEF";
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(jParams)), rpcACT_MALFORMED));
jParams[jss::account] = A1.human();
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(jParams))));
// Ledger min/max index
{
Json::Value p{jParams};
p[jss::ledger_index_min] = -1;
p[jss::ledger_index_max] = -1;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index_min] = 0;
p[jss::ledger_index_max] = 100;
if (apiVersion < 2u)
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
else
BEAST_EXPECT(
isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_IDX_MALFORMED));
p[jss::ledger_index_min] = 1;
p[jss::ledger_index_max] = 2;
if (apiVersion < 2u)
BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
else
BEAST_EXPECT(
isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_IDX_MALFORMED));
p[jss::ledger_index_min] = 2;
p[jss::ledger_index_max] = 1;
BEAST_EXPECT(isErr(
env.rpc("json", "account_tx", to_string(p)),
(apiVersion == 1 ? rpcLGR_IDXS_INVALID : rpcINVALID_LGR_RANGE)));
}
// Ledger index min only
{
Json::Value p{jParams};
p[jss::ledger_index_min] = -1;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index_min] = 1;
if (apiVersion < 2u)
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
else
BEAST_EXPECT(
isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_IDX_MALFORMED));
p[jss::ledger_index_min] = env.current()->header().seq;
BEAST_EXPECT(isErr(
env.rpc("json", "account_tx", to_string(p)),
(apiVersion == 1 ? rpcLGR_IDXS_INVALID : rpcINVALID_LGR_RANGE)));
}
// Ledger index max only
{
Json::Value p{jParams};
p[jss::ledger_index_max] = -1;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index_max] = env.current()->header().seq;
if (apiVersion < 2u)
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
else
BEAST_EXPECT(
isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_IDX_MALFORMED));
p[jss::ledger_index_max] = 3;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index_max] = env.closed()->header().seq;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index_max] = env.closed()->header().seq - 1;
BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
}
// Ledger Sequence
{
Json::Value p{jParams};
p[jss::ledger_index] = env.closed()->header().seq;
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_index] = env.closed()->header().seq - 1;
BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
p[jss::ledger_index] = env.current()->header().seq;
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_NOT_VALIDATED));
p[jss::ledger_index] = env.current()->header().seq + 1;
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_NOT_FOUND));
}
// Ledger Hash
{
Json::Value p{jParams};
p[jss::ledger_hash] = to_string(env.closed()->header().hash);
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
p[jss::ledger_hash] = to_string(env.closed()->header().parentHash);
BEAST_EXPECT(noTxs(env.rpc("json", "account_tx", to_string(p))));
}
// Ledger index max/min/index all specified
// ERRORS out with invalid Parenthesis
{
jParams[jss::account] = "0xDEADBEEF";
jParams[jss::account] = A1.human();
Json::Value p{jParams};
p[jss::ledger_index_max] = -1;
p[jss::ledger_index_min] = -1;
p[jss::ledger_index] = -1;
if (apiVersion < 2u)
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
else
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcINVALID_PARAMS));
}
// Ledger index max only
{
Json::Value p{jParams};
p[jss::ledger_index_max] = env.current()->header().seq;
if (apiVersion < 2u)
BEAST_EXPECT(hasTxs(env.rpc(apiVersion, "json", "account_tx", to_string(p))));
else
BEAST_EXPECT(
isErr(env.rpc("json", "account_tx", to_string(p)), rpcLGR_IDX_MALFORMED));
}
// test account non-string
{
auto testInvalidAccountParam = [&](auto const& param) {
Json::Value params;
params[jss::account] = param;
auto jrr = env.rpc("json", "account_tx", to_string(params))[jss::result];
BEAST_EXPECT(jrr[jss::error] == "invalidParams");
BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'account'.");
};
testInvalidAccountParam(1);
testInvalidAccountParam(1.1);
testInvalidAccountParam(true);
testInvalidAccountParam(Json::Value(Json::nullValue));
testInvalidAccountParam(Json::Value(Json::objectValue));
testInvalidAccountParam(Json::Value(Json::arrayValue));
}
// test binary and forward for bool/non bool values
{
Json::Value p{jParams};
p[jss::binary] = "asdf";
if (apiVersion < 2u)
{
Json::Value result{env.rpc("json", "account_tx", to_string(p))};
BEAST_EXPECT(result[jss::result][jss::status] == "success");
}
else
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcINVALID_PARAMS));
p[jss::binary] = true;
Json::Value result{env.rpc("json", "account_tx", to_string(p))};
BEAST_EXPECT(result[jss::result][jss::status] == "success");
p[jss::forward] = "true";
if (apiVersion < 2u)
BEAST_EXPECT(result[jss::result][jss::status] == "success");
else
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcINVALID_PARAMS));
p[jss::forward] = false;
result = env.rpc("json", "account_tx", to_string(p));
BEAST_EXPECT(result[jss::result][jss::status] == "success");
}
// test limit with malformed values
{
Json::Value p{jParams};
// Test case: limit = 0 should fail (below minimum)
p[jss::limit] = 0;
BEAST_EXPECT(isErr(env.rpc("json", "account_tx", to_string(p)), rpcINVALID_PARAMS));
// Test case: limit = 1.2 should fail (not an integer)
p[jss::limit] = 1.2;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = "10" should fail (string instead of integer)
p[jss::limit] = "10";
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = true should fail (boolean instead of integer)
p[jss::limit] = true;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = false should fail (boolean instead of integer)
p[jss::limit] = false;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = -1 should fail (negative number)
p[jss::limit] = -1;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = [] should fail (array instead of integer)
p[jss::limit] = Json::Value(Json::arrayValue);
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = {} should fail (object instead of integer)
p[jss::limit] = Json::Value(Json::objectValue);
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = "malformed" should fail (malformed string)
p[jss::limit] = "malformed";
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = ["limit"] should fail (array with string)
p[jss::limit] = Json::Value(Json::arrayValue);
p[jss::limit].append("limit");
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = {"limit": 10} should fail (object with
// property)
p[jss::limit] = Json::Value(Json::objectValue);
p[jss::limit][jss::limit] = 10;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] ==
RPC::expected_field_message(jss::limit, "unsigned integer"));
// Test case: limit = 10 should succeed (valid integer)
p[jss::limit] = 10;
BEAST_EXPECT(
env.rpc("json", "account_tx", to_string(p))[jss::result][jss::status] == "success");
}
}
void
testContents()
{
testcase("Contents");
// Get results for all transaction types that can be associated
// with an account. Start by generating all transaction types.
using namespace test::jtx;
using namespace std::chrono_literals;
Env env(*this);
Account const alice{"alice"};
Account const alie{"alie"};
Account const gw{"gw"};
auto const USD{gw["USD"]};
env.fund(XRP(1000000), alice, gw);
env.close();
// AccountSet
env(noop(alice));
// Payment
env(pay(alice, gw, XRP(100)));
// Regular key set
env(regkey(alice, alie));
env.close();
// Trust and Offers
env(trust(alice, USD(200)), sig(alie));
std::uint32_t const offerSeq{env.seq(alice)};
env(offer(alice, USD(50), XRP(150)), sig(alie));
env.close();
env(offer_cancel(alice, offerSeq), sig(alie));
env.close();
// SignerListSet
env(signers(alice, 1, {{"bogie", 1}, {"demon", 1}}), sig(alie));
// Escrow
{
// Create an escrow. Requires either a CancelAfter or FinishAfter.
auto escrow = [](Account const& account, Account const& to, STAmount const& amount) {
Json::Value escrow;
escrow[jss::TransactionType] = jss::EscrowCreate;
escrow[jss::Account] = account.human();
escrow[jss::Destination] = to.human();
escrow[jss::Amount] = amount.getJson(JsonOptions::none);
return escrow;
};
NetClock::time_point const nextTime{env.now() + 2s};
Json::Value escrowWithFinish{escrow(alice, alice, XRP(500))};
escrowWithFinish[sfFinishAfter.jsonName] = nextTime.time_since_epoch().count();
std::uint32_t const escrowFinishSeq{env.seq(alice)};
env(escrowWithFinish, sig(alie));
Json::Value escrowWithCancel{escrow(alice, alice, XRP(500))};
escrowWithCancel[sfFinishAfter.jsonName] = nextTime.time_since_epoch().count();
escrowWithCancel[sfCancelAfter.jsonName] = nextTime.time_since_epoch().count() + 1;
std::uint32_t const escrowCancelSeq{env.seq(alice)};
env(escrowWithCancel, sig(alie));
env.close();
{
Json::Value escrowFinish;
escrowFinish[jss::TransactionType] = jss::EscrowFinish;
escrowFinish[jss::Account] = alice.human();
escrowFinish[sfOwner.jsonName] = alice.human();
escrowFinish[sfOfferSequence.jsonName] = escrowFinishSeq;
env(escrowFinish, sig(alie));
}
{
Json::Value escrowCancel;
escrowCancel[jss::TransactionType] = jss::EscrowCancel;
escrowCancel[jss::Account] = alice.human();
escrowCancel[sfOwner.jsonName] = alice.human();
escrowCancel[sfOfferSequence.jsonName] = escrowCancelSeq;
env(escrowCancel, sig(alie));
}
env.close();
}
// PayChan
{
std::uint32_t payChanSeq{env.seq(alice)};
Json::Value payChanCreate;
payChanCreate[jss::TransactionType] = jss::PaymentChannelCreate;
payChanCreate[jss::Account] = alice.human();
payChanCreate[jss::Destination] = gw.human();
payChanCreate[jss::Amount] = XRP(500).value().getJson(JsonOptions::none);
payChanCreate[sfSettleDelay.jsonName] = NetClock::duration{100s}.count();
payChanCreate[sfPublicKey.jsonName] = strHex(alice.pk().slice());
env(payChanCreate, sig(alie));
env.close();
std::string const payChanIndex{strHex(keylet::payChan(alice, gw, payChanSeq).key)};
{
Json::Value payChanFund;
payChanFund[jss::TransactionType] = jss::PaymentChannelFund;
payChanFund[jss::Account] = alice.human();
payChanFund[sfChannel.jsonName] = payChanIndex;
payChanFund[jss::Amount] = XRP(200).value().getJson(JsonOptions::none);
env(payChanFund, sig(alie));
env.close();
}
{
Json::Value payChanClaim;
payChanClaim[jss::TransactionType] = jss::PaymentChannelClaim;
payChanClaim[jss::Flags] = tfClose;
payChanClaim[jss::Account] = gw.human();
payChanClaim[sfChannel.jsonName] = payChanIndex;
payChanClaim[sfPublicKey.jsonName] = strHex(alice.pk().slice());
env(payChanClaim);
env.close();
}
}
// Check
{
auto const aliceCheckId = keylet::check(alice, env.seq(alice)).key;
env(check::create(alice, gw, XRP(300)), sig(alie));
auto const gwCheckId = keylet::check(gw, env.seq(gw)).key;
env(check::create(gw, alice, XRP(200)));
env.close();
env(check::cash(alice, gwCheckId, XRP(200)), sig(alie));
env(check::cancel(alice, aliceCheckId), sig(alie));
env.close();
}
{
// Deposit pre-authorization with a Ticket.
std::uint32_t const tktSeq{env.seq(alice) + 1};
env(ticket::create(alice, 1), sig(alie));
env.close();
env(deposit::auth(alice, gw), ticket::use(tktSeq), sig(alie));
env.close();
}
// Setup is done. Look at the transactions returned by account_tx.
Json::Value params;
params[jss::account] = alice.human();
params[jss::ledger_index_min] = -1;
params[jss::ledger_index_max] = -1;
Json::Value const result{env.rpc("json", "account_tx", to_string(params))};
BEAST_EXPECT(result[jss::result][jss::status] == "success");
BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
Json::Value const& txs{result[jss::result][jss::transactions]};
// clang-format off
// Do a sanity check on each returned transaction. They should
// be returned in the reverse order of application to the ledger.
static const NodeSanity sanity[]{
// txType, created, deleted, modified
{0, jss::DepositPreauth, {jss::DepositPreauth}, {jss::Ticket}, {jss::AccountRoot, jss::DirectoryNode}},
{1, jss::TicketCreate, {jss::Ticket}, {}, {jss::AccountRoot, jss::DirectoryNode}},
{2, jss::CheckCancel, {}, {jss::Check}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{3, jss::CheckCash, {}, {jss::Check}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{4, jss::CheckCreate, {jss::Check}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{5, jss::CheckCreate, {jss::Check}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{6, jss::PaymentChannelClaim, {}, {jss::PayChannel}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{7, jss::PaymentChannelFund, {}, {}, {jss::AccountRoot, jss::PayChannel}},
{8, jss::PaymentChannelCreate, {jss::PayChannel}, {}, {jss::AccountRoot, jss::AccountRoot, jss::DirectoryNode, jss::DirectoryNode}},
{9, jss::EscrowCancel, {}, {jss::Escrow}, {jss::AccountRoot, jss::DirectoryNode}},
{10, jss::EscrowFinish, {}, {jss::Escrow}, {jss::AccountRoot, jss::DirectoryNode}},
{11, jss::EscrowCreate, {jss::Escrow}, {}, {jss::AccountRoot, jss::DirectoryNode}},
{12, jss::EscrowCreate, {jss::Escrow}, {}, {jss::AccountRoot, jss::DirectoryNode}},
{13, jss::SignerListSet, {jss::SignerList}, {}, {jss::AccountRoot, jss::DirectoryNode}},
{14, jss::OfferCancel, {}, {jss::Offer, jss::DirectoryNode}, {jss::AccountRoot, jss::DirectoryNode}},
{15, jss::OfferCreate, {jss::Offer, jss::DirectoryNode}, {}, {jss::AccountRoot, jss::DirectoryNode}},
{16, jss::TrustSet, {jss::RippleState, jss::DirectoryNode, jss::DirectoryNode}, {}, {jss::AccountRoot, jss::AccountRoot}},
{17, jss::SetRegularKey, {}, {}, {jss::AccountRoot}},
{18, jss::Payment, {}, {}, {jss::AccountRoot, jss::AccountRoot}},
{19, jss::AccountSet, {}, {}, {jss::AccountRoot}},
{20, jss::AccountSet, {}, {}, {jss::AccountRoot}},
{21, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}},
};
// clang-format on
BEAST_EXPECT(std::size(sanity) == result[jss::result][jss::transactions].size());
for (unsigned int index{0}; index < std::size(sanity); ++index)
{
checkSanity(txs[index], sanity[index]);
}
}
void
testAccountDelete()
{
testcase("AccountDelete");
// Verify that if an account is resurrected then the account_tx RPC
// command still recovers all transactions on that account before
// and after resurrection.
using namespace test::jtx;
using namespace std::chrono_literals;
Env env(*this);
Account const alice{"alice"};
Account const becky{"becky"};
env.fund(XRP(10000), alice, becky);
env.close();
// Verify that becky's account root is present.
Keylet const beckyAcctKey{keylet::account(becky.id())};
BEAST_EXPECT(env.closed()->exists(beckyAcctKey));
// becky does an AccountSet .
env(noop(becky));
// Close enough ledgers to be able to delete becky's account.
std::uint32_t const ledgerCount{env.current()->seq() + 257 - env.seq(becky)};
for (std::uint32_t i = 0; i < ledgerCount; ++i)
env.close();
auto const beckyPreDelBalance{env.balance(becky)};
auto const acctDelFee{drops(env.current()->fees().increment)};
env(acctdelete(becky, alice), fee(acctDelFee));
env.close();
// Verify that becky's account root is gone.
BEAST_EXPECT(!env.closed()->exists(beckyAcctKey));
env.close();
// clang-format off
// Do a sanity check on each returned transaction. They should
// be returned in the reverse order of application to the ledger.
//
// Note that the first two transactions in sanity have not occurred
// yet. We'll see those after becky's account is resurrected.
static const NodeSanity sanity[]
{
// txType, created, deleted, modified
/* becky pays alice */ { 0, jss::Payment, {}, {}, {jss::AccountRoot, jss::AccountRoot}},
/* alice resurrects becky's acct */ { 1, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}},
/* becky deletes her account */ { 2, jss::AccountDelete, {}, {jss::AccountRoot}, {jss::AccountRoot}},
/* becky's noop */ { 3, jss::AccountSet, {}, {}, {jss::AccountRoot}},
/* "fund" sets flags */ { 4, jss::AccountSet, {}, {}, {jss::AccountRoot}},
/* "fund" creates becky's acct */ { 5, jss::Payment, {jss::AccountRoot}, {}, {jss::AccountRoot}}
};
// clang-format on
// Verify that we can recover becky's account_tx information even
// after the account is deleted.
{
Json::Value params;
params[jss::account] = becky.human();
params[jss::ledger_index_min] = -1;
params[jss::ledger_index_max] = -1;
Json::Value const result{env.rpc("json", "account_tx", to_string(params))};
BEAST_EXPECT(result[jss::result][jss::status] == "success");
BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
// The first two transactions listed in sanity haven't happened yet.
constexpr unsigned int beckyDeletedOffset = 2;
BEAST_EXPECT(
std::size(sanity) ==
result[jss::result][jss::transactions].size() + beckyDeletedOffset);
Json::Value const& txs{result[jss::result][jss::transactions]};
for (unsigned int index = beckyDeletedOffset; index < std::size(sanity); ++index)
{
checkSanity(txs[index - beckyDeletedOffset], sanity[index]);
}
}
// All it takes is a large enough XRP payment to resurrect
// becky's account. Try too small a payment.
env(pay(alice, becky, drops(env.current()->fees().accountReserve(0)) - XRP(1)),
ter(tecNO_DST_INSUF_XRP));
env.close();
// Actually resurrect becky's account.
env(pay(alice, becky, XRP(45)));
env.close();
// becky's account root should be back.
BEAST_EXPECT(env.closed()->exists(beckyAcctKey));
BEAST_EXPECT(env.balance(becky) == XRP(45));
// becky pays alice.
env(pay(becky, alice, XRP(20)));
env.close();
// Setup is done. Look at the transactions returned by account_tx.
// Verify that account_tx locates all of becky's transactions.
Json::Value params;
params[jss::account] = becky.human();
params[jss::ledger_index_min] = -1;
params[jss::ledger_index_max] = -1;
Json::Value const result{env.rpc("json", "account_tx", to_string(params))};
BEAST_EXPECT(result[jss::result][jss::status] == "success");
BEAST_EXPECT(result[jss::result][jss::transactions].isArray());
BEAST_EXPECT(std::size(sanity) == result[jss::result][jss::transactions].size());
Json::Value const& txs{result[jss::result][jss::transactions]};
for (unsigned int index = 0; index < std::size(sanity); ++index)
{
checkSanity(txs[index], sanity[index]);
}
}
void
testMPT()
{
testcase("MPT");
using namespace test::jtx;
using namespace std::chrono_literals;
auto cfg = makeConfig();
cfg->FEES.reference_fee = 10;
Env env(*this, std::move(cfg));
Account const alice{"alice"};
Account const bob{"bob"};
Account const carol{"carol"};
MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
// check the latest mpt-related txn is in alice's account history
auto const checkAliceAcctTx = [&](size_t size, Json::StaticString txType) {
Json::Value params;
params[jss::account] = alice.human();
params[jss::limit] = 100;
auto const jv = env.rpc("json", "account_tx", to_string(params))[jss::result];
BEAST_EXPECT(jv[jss::transactions].size() == size);
auto const& tx0(jv[jss::transactions][0u][jss::tx]);
BEAST_EXPECT(tx0[jss::TransactionType] == txType);
std::string const txHash{env.tx()->getJson(JsonOptions::none)[jss::hash].asString()};
BEAST_EXPECT(tx0[jss::hash] == txHash);
};
// alice creates issuance
mptAlice.create(
{.ownerCount = 1,
.holderCount = 0,
.flags = tfMPTCanClawback | tfMPTRequireAuth | tfMPTCanTransfer});
checkAliceAcctTx(3, jss::MPTokenIssuanceCreate);
// bob creates a MPToken;
mptAlice.authorize({.account = bob});
checkAliceAcctTx(4, jss::MPTokenAuthorize);
env.close();
// TODO: windows pipeline fails validation for the hardcoded ledger hash
// due to having different test config, it can be uncommented after
// figuring out what happened
//
// ledger hash should be fixed regardless any change to account history
// BEAST_EXPECT(
// to_string(env.closed()->header().hash) ==
// "0BD507BB87D3C0E73B462485E6E381798A8C82FC49BF17FE39C60E08A1AF035D");
// alice authorizes bob
mptAlice.authorize({.account = alice, .holder = bob});
checkAliceAcctTx(5, jss::MPTokenAuthorize);
// carol creates a MPToken;
mptAlice.authorize({.account = carol});
checkAliceAcctTx(6, jss::MPTokenAuthorize);
// alice authorizes carol
mptAlice.authorize({.account = alice, .holder = carol});
checkAliceAcctTx(7, jss::MPTokenAuthorize);
// alice pays bob 100 tokens
mptAlice.pay(alice, bob, 100);
checkAliceAcctTx(8, jss::Payment);
// bob pays carol 10 tokens
mptAlice.pay(bob, carol, 10);
checkAliceAcctTx(9, jss::Payment);
}
public:
void
run() override
{
forAllApiVersions(std::bind_front(&AccountTx_test::testParameters, this));
testContents();
testAccountDelete();
testMPT();
}
};
BEAST_DEFINE_TESTSUITE(AccountTx, rpc, xrpl);
} // namespace test
} // namespace xrpl