-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTransaction_test.cpp
More file actions
888 lines (734 loc) · 33.2 KB
/
Transaction_test.cpp
File metadata and controls
888 lines (734 loc) · 33.2 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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
#include <test/jtx.h>
#include <test/jtx/Env.h>
#include <test/jtx/envconfig.h>
#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
#include <xrpld/rpc/CTID.h>
#include <xrpl/core/NetworkIDService.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/STBase.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/protocol/serialize.h>
#include <cctype>
#include <optional>
#include <tuple>
namespace xrpl {
class Transaction_test : public beast::unit_test::suite
{
std::unique_ptr<Config>
makeNetworkConfig(uint32_t networkID)
{
using namespace test::jtx;
return envconfig([&](std::unique_ptr<Config> cfg) {
cfg->NETWORK_ID = networkID;
return cfg;
});
}
void
testRangeRequest(FeatureBitset features)
{
testcase("Test Range Request");
using namespace test::jtx;
using std::to_string;
char const* COMMAND = jss::tx.c_str();
char const* BINARY = jss::binary.c_str();
char const* NOT_FOUND = RPC::get_error_info(rpcTXN_NOT_FOUND).token;
char const* INVALID = RPC::get_error_info(rpcINVALID_LGR_RANGE).token;
char const* EXCESSIVE = RPC::get_error_info(rpcEXCESSIVE_LGR_RANGE).token;
Env env{*this, features};
auto const alice = Account("alice");
env.fund(XRP(1000), alice);
env.close();
std::vector<std::shared_ptr<STTx const>> txns;
std::vector<std::shared_ptr<STObject const>> metas;
auto const startLegSeq = env.current()->header().seq;
for (int i = 0; i < 750; ++i)
{
env(noop(alice));
txns.emplace_back(env.tx());
env.close();
metas.emplace_back(env.closed()->txRead(env.tx()->getTransactionID()).second);
}
auto const endLegSeq = env.closed()->header().seq;
// Find the existing transactions
for (size_t i = 0; i < txns.size(); ++i)
{
auto const& tx = txns[i];
auto const& meta = metas[i];
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(startLegSeq),
to_string(endLegSeq));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(result[jss::result][jss::tx] == strHex(tx->getSerializer().getData()));
BEAST_EXPECT(result[jss::result][jss::meta] == strHex(meta->getSerializer().getData()));
}
auto const tx = env.jt(noop(alice), seq(env.seq(alice))).stx;
for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(startLegSeq),
to_string(endLegSeq + deltaEndSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
if (deltaEndSeq)
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
else
BEAST_EXPECT(result[jss::result][jss::searched_all].asBool());
}
// Find transactions outside of provided range.
for (auto&& tx : txns)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(endLegSeq + 1),
to_string(endLegSeq + 100));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
auto const deletedLedger = (startLegSeq + endLegSeq) / 2;
{
// Remove one of the ledgers from the database directly
env.app().getRelationalDatabase().deleteTransactionByLedgerSeq(deletedLedger);
}
for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(startLegSeq),
to_string(endLegSeq + deltaEndSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
to_string(startLegSeq),
to_string(endLegSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
to_string(startLegSeq),
to_string(deletedLedger - 1));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const result = env.rpc(
COMMAND,
to_string(txns[0]->getTransactionID()),
to_string(startLegSeq),
to_string(deletedLedger - 1));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min > max)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(deletedLedger - 1),
to_string(startLegSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min < 0)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(-1),
to_string(deletedLedger - 1));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min < 0, max < 0)
{
auto const result = env.rpc(
COMMAND, to_string(tx->getTransactionID()), BINARY, to_string(-20), to_string(-10));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (only one value)
{
auto const result =
env.rpc(COMMAND, to_string(tx->getTransactionID()), BINARY, to_string(20));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (only one value)
{
auto const result = env.rpc(COMMAND, to_string(tx->getTransactionID()), to_string(20));
// Since we only provided one value for the range,
// the interface parses it as a false binary flag,
// as single-value ranges are not accepted. Since
// the error this causes differs depending on the platform
// we don't call out a specific error here.
BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (max - min > 1000)
{
auto const result = env.rpc(
COMMAND,
to_string(tx->getTransactionID()),
BINARY,
to_string(startLegSeq),
to_string(startLegSeq + 1001));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == EXCESSIVE);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
}
void
testRangeCTIDRequest(FeatureBitset features)
{
testcase("CTID Range Request");
using namespace test::jtx;
using std::to_string;
char const* COMMAND = jss::tx.c_str();
char const* BINARY = jss::binary.c_str();
char const* NOT_FOUND = RPC::get_error_info(rpcTXN_NOT_FOUND).token;
char const* INVALID = RPC::get_error_info(rpcINVALID_LGR_RANGE).token;
char const* EXCESSIVE = RPC::get_error_info(rpcEXCESSIVE_LGR_RANGE).token;
Env env{*this, makeNetworkConfig(11111)};
uint32_t netID = env.app().getNetworkIDService().getNetworkID();
auto const alice = Account("alice");
env.fund(XRP(1000), alice);
env.close();
std::vector<std::shared_ptr<STTx const>> txns;
std::vector<std::shared_ptr<STObject const>> metas;
auto const startLegSeq = env.current()->header().seq;
for (int i = 0; i < 750; ++i)
{
env(noop(alice));
txns.emplace_back(env.tx());
env.close();
metas.emplace_back(env.closed()->txRead(env.tx()->getTransactionID()).second);
}
auto const endLegSeq = env.closed()->header().seq;
// Find the existing transactions
for (size_t i = 0; i < txns.size(); ++i)
{
auto const& tx = txns[i];
auto const& meta = metas[i];
uint32_t txnIdx = meta->getFieldU32(sfTransactionIndex);
auto const result = env.rpc(
COMMAND,
*RPC::encodeCTID(startLegSeq + i, txnIdx, netID),
BINARY,
to_string(startLegSeq),
to_string(endLegSeq));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(result[jss::result][jss::tx] == strHex(tx->getSerializer().getData()));
BEAST_EXPECT(result[jss::result][jss::meta] == strHex(meta->getSerializer().getData()));
}
auto const tx = env.jt(noop(alice), seq(env.seq(alice))).stx;
auto const ctid = *RPC::encodeCTID(endLegSeq, tx->getSeqValue(), netID);
for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
{
auto const result = env.rpc(
COMMAND, ctid, BINARY, to_string(startLegSeq), to_string(endLegSeq + deltaEndSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
if (deltaEndSeq)
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
else
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Find transactions outside of provided range.
for (size_t i = 0; i < txns.size(); ++i)
{
// auto const& tx = txns[i];
auto const& meta = metas[i];
uint32_t txnIdx = meta->getFieldU32(sfTransactionIndex);
auto const result = env.rpc(
COMMAND,
*RPC::encodeCTID(startLegSeq + i, txnIdx, netID),
BINARY,
to_string(endLegSeq + 1),
to_string(endLegSeq + 100));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
auto const deletedLedger = (startLegSeq + endLegSeq) / 2;
{
// Remove one of the ledgers from the database directly
env.app().getRelationalDatabase().deleteTransactionByLedgerSeq(deletedLedger);
}
for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
{
auto const result = env.rpc(
COMMAND, ctid, BINARY, to_string(startLegSeq), to_string(endLegSeq + deltaEndSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const result =
env.rpc(COMMAND, ctid, to_string(startLegSeq), to_string(endLegSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const result =
env.rpc(COMMAND, ctid, to_string(startLegSeq), to_string(deletedLedger - 1));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == NOT_FOUND);
BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
}
// Provide range without providing the `binary`
// field. (Tests parameter parsing)
{
auto const& meta = metas[0];
uint32_t txnIdx = meta->getFieldU32(sfTransactionIndex);
auto const result = env.rpc(
COMMAND,
*RPC::encodeCTID(endLegSeq, txnIdx, netID),
to_string(startLegSeq),
to_string(deletedLedger - 1));
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min > max)
{
auto const result = env.rpc(
COMMAND, ctid, BINARY, to_string(deletedLedger - 1), to_string(startLegSeq));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min < 0)
{
auto const result =
env.rpc(COMMAND, ctid, BINARY, to_string(-1), to_string(deletedLedger - 1));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (min < 0, max < 0)
{
auto const result = env.rpc(COMMAND, ctid, BINARY, to_string(-20), to_string(-10));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (only one value)
{
auto const result = env.rpc(COMMAND, ctid, BINARY, to_string(20));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == INVALID);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (only one value)
{
auto const result = env.rpc(COMMAND, ctid, to_string(20));
// Since we only provided one value for the range,
// the interface parses it as a false binary flag,
// as single-value ranges are not accepted. Since
// the error this causes differs depending on the platform
// we don't call out a specific error here.
BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
// Provide an invalid range: (max - min > 1000)
{
auto const result = env.rpc(
COMMAND, ctid, BINARY, to_string(startLegSeq), to_string(startLegSeq + 1001));
BEAST_EXPECT(
result[jss::result][jss::status] == jss::error &&
result[jss::result][jss::error] == EXCESSIVE);
BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
}
}
void
testCTIDValidation(FeatureBitset features)
{
testcase("CTID Validation");
using namespace test::jtx;
using std::to_string;
Env env{*this, makeNetworkConfig(11111)};
// Test case 1: Valid input values
auto const expected11 = std::optional<std::string>("CFFFFFFFFFFFFFFF");
BEAST_EXPECT(RPC::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU) == expected11);
auto const expected12 = std::optional<std::string>("C000000000000000");
BEAST_EXPECT(RPC::encodeCTID(0, 0, 0) == expected12);
auto const expected13 = std::optional<std::string>("C000000100020003");
BEAST_EXPECT(RPC::encodeCTID(1U, 2U, 3U) == expected13);
auto const expected14 = std::optional<std::string>("C0CA2AA7326FFFFF");
BEAST_EXPECT(RPC::encodeCTID(13249191UL, 12911U, 65535U) == expected14);
// Test case 2: ledger_seq greater than 0xFFFFFFF
BEAST_EXPECT(!RPC::encodeCTID(0x1000'0000UL, 0xFFFFU, 0xFFFFU));
// Test case 3: txn_index greater than 0xFFFF
BEAST_EXPECT(!RPC::encodeCTID(0x0FFF'FFFF, 0x1'0000, 0xFFFF));
// Test case 4: network_id greater than 0xFFFF
BEAST_EXPECT(!RPC::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0x1'0000U));
// Test case 5: Valid input values
auto const expected51 =
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(std::make_tuple(0, 0, 0));
BEAST_EXPECT(RPC::decodeCTID("C000000000000000") == expected51);
auto const expected52 =
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(std::make_tuple(1U, 2U, 3U));
BEAST_EXPECT(RPC::decodeCTID("C000000100020003") == expected52);
auto const expected53 = std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(
std::make_tuple(13249191UL, 12911U, 49221U));
BEAST_EXPECT(RPC::decodeCTID("C0CA2AA7326FC045") == expected53);
// Test case 6: ctid not a string or big int
BEAST_EXPECT(!RPC::decodeCTID(0xCFF));
// Test case 7: ctid not a hexadecimal string
BEAST_EXPECT(!RPC::decodeCTID("C003FFFFFFFFFFFG"));
// Test case 8: ctid not exactly 16 nibbles
BEAST_EXPECT(!RPC::decodeCTID("C003FFFFFFFFFFF"));
// Test case 9: ctid too large to be a valid CTID value
BEAST_EXPECT(!RPC::decodeCTID("CFFFFFFFFFFFFFFFF"));
// Test case 10: ctid doesn't start with a C nibble
BEAST_EXPECT(!RPC::decodeCTID("FFFFFFFFFFFFFFFF"));
// Test case 11: Valid input values
BEAST_EXPECT(
(RPC::decodeCTID(0xCFFF'FFFF'FFFF'FFFFULL) ==
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(
std::make_tuple(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU))));
BEAST_EXPECT(
(RPC::decodeCTID(0xC000'0000'0000'0000ULL) ==
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(std::make_tuple(0, 0, 0))));
BEAST_EXPECT(
(RPC::decodeCTID(0xC000'0001'0002'0003ULL) ==
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(std::make_tuple(1U, 2U, 3U))));
BEAST_EXPECT(
(RPC::decodeCTID(0xC0CA'2AA7'326F'C045ULL) ==
std::optional<std::tuple<int32_t, uint16_t, uint16_t>>(
std::make_tuple(1324'9191UL, 12911U, 49221U))));
// Test case 12: ctid not exactly 16 nibbles
BEAST_EXPECT(!RPC::decodeCTID(0xC003'FFFF'FFFF'FFF));
// Test case 13: ctid too large to be a valid CTID value
// this test case is not possible in c++ because it would overflow the
// type, left in for completeness
// BEAST_EXPECT(!RPC::decodeCTID(0xCFFFFFFFFFFFFFFFFULL));
// Test case 14: ctid doesn't start with a C nibble
BEAST_EXPECT(!RPC::decodeCTID(0xFFFF'FFFF'FFFF'FFFFULL));
}
void
testRPCsForCTID(FeatureBitset features)
{
testcase("CTID RPC");
using namespace test::jtx;
// Use a Concise Transaction Identifier to request a transaction.
for (uint32_t netID : {11111, 65535, 65536})
{
Env env{*this, makeNetworkConfig(netID)};
BEAST_EXPECT(netID == env.app().getNetworkIDService().getNetworkID());
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const startLegSeq = env.current()->header().seq;
env.fund(XRP(10000), alice, bob);
env(pay(alice, bob, XRP(10)));
env.close();
auto const ctid = RPC::encodeCTID(startLegSeq, 0, netID);
if (netID > 0xFFFF)
{
// Concise transaction IDs do not support a network ID > 0xFFFF.
BEAST_EXPECT(ctid == std::nullopt);
continue;
}
Json::Value jsonTx;
jsonTx[jss::binary] = false;
jsonTx[jss::ctid] = *ctid;
jsonTx[jss::id] = 1;
auto const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
BEAST_EXPECT(jrr[jss::ctid] == ctid);
BEAST_EXPECT(jrr.isMember(jss::hash));
}
// test querying with mixed case ctid
{
Env env{*this, makeNetworkConfig(11111)};
std::uint32_t const netID = env.app().getNetworkIDService().getNetworkID();
Account const alice = Account("alice");
Account const bob = Account("bob");
std::uint32_t const startLegSeq = env.current()->header().seq;
env.fund(XRP(10000), alice, bob);
env(pay(alice, bob, XRP(10)));
env.close();
std::string const ctid = *RPC::encodeCTID(startLegSeq, 0, netID);
auto isUpper = [](char c) { return std::isupper(c) != 0; };
// Verify that there are at least two upper case letters in ctid and
// test a mixed case
if (BEAST_EXPECT(std::count_if(ctid.begin(), ctid.end(), isUpper) > 1))
{
// Change the first upper case letter to lower case.
std::string mixedCase = ctid;
{
auto const iter = std::find_if(mixedCase.begin(), mixedCase.end(), isUpper);
*iter = std::tolower(*iter);
}
BEAST_EXPECT(ctid != mixedCase);
Json::Value jsonTx;
jsonTx[jss::binary] = false;
jsonTx[jss::ctid] = mixedCase;
jsonTx[jss::id] = 1;
Json::Value const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
BEAST_EXPECT(jrr[jss::ctid] == ctid);
BEAST_EXPECT(jrr[jss::hash]);
}
}
// test that if the network is 65535 the ctid is not in the response
// Using a hash to request the transaction, test the network ID
// boundary where the CTID is (not) in the response.
for (uint32_t netID : {2, 1024, 65535, 65536})
{
Env env{*this, makeNetworkConfig(netID)};
BEAST_EXPECT(netID == env.app().getNetworkIDService().getNetworkID());
auto const alice = Account("alice");
auto const bob = Account("bob");
env.fund(XRP(10000), alice, bob);
env(pay(alice, bob, XRP(10)));
env.close();
auto const ledgerSeq = env.current()->header().seq;
env(noop(alice), ter(tesSUCCESS));
env.close();
Json::Value params;
params[jss::id] = 1;
auto const hash = env.tx()->getJson(JsonOptions::none)[jss::hash];
params[jss::transaction] = hash;
auto const jrr = env.rpc("json", "tx", to_string(params))[jss::result];
BEAST_EXPECT(jrr[jss::hash] == hash);
BEAST_EXPECT(jrr.isMember(jss::ctid) == (netID <= 0xFFFF));
if (jrr.isMember(jss::ctid))
{
auto const ctid = RPC::encodeCTID(ledgerSeq, 0, netID);
BEAST_EXPECT(jrr[jss::ctid] == *ctid);
}
}
// test the wrong network ID was submitted
{
Env env{*this, makeNetworkConfig(21337)};
uint32_t netID = env.app().getNetworkIDService().getNetworkID();
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const startLegSeq = env.current()->header().seq;
env.fund(XRP(10000), alice, bob);
env(pay(alice, bob, XRP(10)));
env.close();
auto const ctid = *RPC::encodeCTID(startLegSeq, 0, netID + 1);
Json::Value jsonTx;
jsonTx[jss::binary] = false;
jsonTx[jss::ctid] = ctid;
jsonTx[jss::id] = 1;
auto const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
BEAST_EXPECT(jrr[jss::error] == "wrongNetwork");
BEAST_EXPECT(jrr[jss::error_code] == rpcWRONG_NETWORK);
BEAST_EXPECT(
jrr[jss::error_message] ==
"Wrong network. You should submit this request to a node "
"running on NetworkID: 21338");
}
}
void
testRequest(FeatureBitset features, unsigned apiVersion)
{
testcase("Test Request API version " + std::to_string(apiVersion));
using namespace test::jtx;
using std::to_string;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->FEES.reference_fee = 10;
return cfg;
})};
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)));
std::shared_ptr<STTx const> txn = env.tx();
env.close();
std::shared_ptr<STObject const> meta =
env.closed()->txRead(env.tx()->getTransactionID()).second;
Json::Value expected = txn->getJson(JsonOptions::none);
expected[jss::DeliverMax] = expected[jss::Amount];
if (apiVersion > 1)
{
expected.removeMember(jss::hash);
expected.removeMember(jss::Amount);
}
Json::Value const result = {[&env, txn, apiVersion]() {
Json::Value params{Json::objectValue};
params[jss::transaction] = to_string(txn->getTransactionID());
params[jss::binary] = false;
params[jss::api_version] = apiVersion;
return env.client().invoke("tx", params);
}()};
BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
if (apiVersion > 1)
{
BEAST_EXPECT(result[jss::result][jss::close_time_iso] == "2000-01-01T00:00:20Z");
BEAST_EXPECT(result[jss::result][jss::hash] == to_string(txn->getTransactionID()));
BEAST_EXPECT(result[jss::result][jss::validated] == true);
BEAST_EXPECT(result[jss::result][jss::ledger_index] == 4);
BEAST_EXPECT(
result[jss::result][jss::ledger_hash] ==
"B41882E20F0EC6228417D28B9AE0F33833645D35F6799DFB782AC97FC4BB51"
"D2");
auto const& tx_json = result[jss::result][jss::tx_json];
if (apiVersion >= 3)
{
// In API v3, server-added lower-case fields must not appear
// inside tx_json; they are at the result level.
BEAST_EXPECT(!tx_json.isMember(jss::date));
BEAST_EXPECT(!tx_json.isMember(jss::ledger_index));
BEAST_EXPECT(!tx_json.isMember(jss::ctid));
// date must be at result level in API v3
BEAST_EXPECT(result[jss::result].isMember(jss::date));
}
else
{
// In API v2, date and ledger_index are still included in
// tx_json for backwards compatibility.
BEAST_EXPECT(tx_json.isMember(jss::date));
BEAST_EXPECT(tx_json.isMember(jss::ledger_index));
}
}
for (auto memberIt = expected.begin(); memberIt != expected.end(); memberIt++)
{
std::string const name = memberIt.memberName();
auto const& result_transaction =
(apiVersion > 1 ? result[jss::result][jss::tx_json] : result[jss::result]);
if (BEAST_EXPECT(result_transaction.isMember(name)))
{
auto const received = result_transaction[name];
BEAST_EXPECTS(
received == *memberIt,
"Transaction contains \n\"" + name + "\": " //
+ to_string(received) //
+ " but expected " //
+ to_string(expected));
}
}
}
void
testBinaryRequest(unsigned apiVersion)
{
testcase("Test binary request API version " + std::to_string(apiVersion));
using namespace test::jtx;
using std::to_string;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->FEES.reference_fee = 10;
return cfg;
})};
Account const alice{"alice"};
Account const gw{"gw"};
auto const USD{gw["USD"]};
env.fund(XRP(1000000), alice, gw);
std::shared_ptr<STTx const> const txn = env.tx();
BEAST_EXPECT(
to_string(txn->getTransactionID()) ==
"3F8BDE5A5F82C4F4708E5E9255B713E303E6E1A371FD5C7A704AFD1387C23981");
env.close();
std::shared_ptr<STObject const> meta = env.closed()->txRead(txn->getTransactionID()).second;
std::string const expected_tx_blob = serializeHex(*txn);
std::string const expected_meta_blob = serializeHex(*meta);
Json::Value const result = [&env, txn, apiVersion]() {
Json::Value params{Json::objectValue};
params[jss::transaction] = to_string(txn->getTransactionID());
params[jss::binary] = true;
params[jss::api_version] = apiVersion;
return env.client().invoke("tx", params);
}();
if (BEAST_EXPECT(result[jss::status] == "success"))
{
BEAST_EXPECT(result[jss::result][jss::status] == "success");
BEAST_EXPECT(result[jss::result][jss::validated] == true);
BEAST_EXPECT(result[jss::result][jss::hash] == to_string(txn->getTransactionID()));
BEAST_EXPECT(result[jss::result][jss::ledger_index] == 3);
BEAST_EXPECT(result[jss::result][jss::ctid] == "C000000300030000");
if (apiVersion > 1)
{
BEAST_EXPECT(result[jss::result][jss::tx_blob] == expected_tx_blob);
BEAST_EXPECT(result[jss::result][jss::meta_blob] == expected_meta_blob);
BEAST_EXPECT(
result[jss::result][jss::ledger_hash] ==
"2D5150E5A5AA436736A732291E437ABF01BC9E206C2DF3C77C4F856915"
"7905AA");
BEAST_EXPECT(result[jss::result][jss::close_time_iso] == "2000-01-01T00:00:10Z");
}
else
{
BEAST_EXPECT(result[jss::result][jss::tx] == expected_tx_blob);
BEAST_EXPECT(result[jss::result][jss::meta] == expected_meta_blob);
BEAST_EXPECT(result[jss::result][jss::date] == 10);
}
}
}
public:
void
run() override
{
using namespace test::jtx;
forAllApiVersions(std::bind_front(&Transaction_test::testBinaryRequest, this));
FeatureBitset const all{testable_amendments()};
testWithFeats(all);
}
void
testWithFeats(FeatureBitset features)
{
testRangeRequest(features);
testRangeCTIDRequest(features);
testCTIDValidation(features);
testRPCsForCTID(features);
forAllApiVersions(std::bind_front(&Transaction_test::testRequest, this, features));
}
};
BEAST_DEFINE_TESTSUITE(Transaction, rpc, xrpl);
} // namespace xrpl