-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathBatch_test.cpp
More file actions
4370 lines (3728 loc) · 167 KB
/
Batch_test.cpp
File metadata and controls
4370 lines (3728 loc) · 167 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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <test/jtx.h>
#include <test/jtx/TestHelpers.h>
#include <test/jtx/utility.h>
#include <xrpld/app/misc/Transaction.h>
#include <xrpl/core/HashRouter.h>
#include <xrpl/protocol/Batch.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/STParsedJSON.h>
#include <xrpl/protocol/Sign.h>
#include <xrpl/protocol/TxFlags.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/server/NetworkOPs.h>
#include <xrpl/tx/apply.h>
#include <xrpl/tx/transactors/system/Batch.h>
namespace xrpl {
namespace test {
class Batch_test : public beast::unit_test::suite
{
struct TestLedgerData
{
int index;
std::string txType;
std::string result;
std::string txHash;
std::optional<std::string> batchID;
};
struct TestBatchData
{
std::string result;
std::string txHash;
};
static Json::Value
getTxByIndex(Json::Value const& jrr, int const index)
{
for (auto const& txn : jrr[jss::result][jss::ledger][jss::transactions])
{
if (txn[jss::metaData][sfTransactionIndex.jsonName] == index)
return txn;
}
return {};
}
static Json::Value
getLastLedger(jtx::Env& env)
{
Json::Value params;
params[jss::ledger_index] = env.closed()->seq();
params[jss::transactions] = true;
params[jss::expand] = true;
return env.rpc("json", "ledger", to_string(params));
}
void
validateInnerTxn(jtx::Env& env, std::string const& batchID, TestLedgerData const& ledgerResult)
{
Json::Value const jrr = env.rpc("tx", ledgerResult.txHash)[jss::result];
BEAST_EXPECT(jrr[sfTransactionType.jsonName] == ledgerResult.txType);
BEAST_EXPECT(jrr[jss::meta][sfTransactionResult.jsonName] == ledgerResult.result);
BEAST_EXPECT(jrr[jss::meta][sfParentBatchID.jsonName] == batchID);
}
void
validateClosedLedger(jtx::Env& env, std::vector<TestLedgerData> const& ledgerResults)
{
auto const jrr = getLastLedger(env);
auto const transactions = jrr[jss::result][jss::ledger][jss::transactions];
BEAST_EXPECT(transactions.size() == ledgerResults.size());
for (TestLedgerData const& ledgerResult : ledgerResults)
{
auto const txn = getTxByIndex(jrr, ledgerResult.index);
BEAST_EXPECT(txn[jss::hash].asString() == ledgerResult.txHash);
BEAST_EXPECT(txn.isMember(jss::metaData));
Json::Value const meta = txn[jss::metaData];
BEAST_EXPECT(txn[sfTransactionType.jsonName] == ledgerResult.txType);
BEAST_EXPECT(meta[sfTransactionResult.jsonName] == ledgerResult.result);
if (ledgerResult.batchID)
validateInnerTxn(env, *ledgerResult.batchID, ledgerResult);
}
}
template <typename... Args>
std::pair<std::vector<std::string>, std::string>
submitBatch(jtx::Env& env, TER const& result, Args&&... args)
{
auto batchTxn = env.jt(std::forward<Args>(args)...);
env(batchTxn, jtx::ter(result));
auto const ids = batchTxn.stx->getBatchTransactionIDs();
std::vector<std::string> txIDs;
for (auto const& id : ids)
txIDs.push_back(strHex(id));
TxID const batchID = batchTxn.stx->getTransactionID();
return std::make_pair(txIDs, strHex(batchID));
}
static uint256
getCheckIndex(AccountID const& account, std::uint32_t uSequence)
{
return keylet::check(account, uSequence).key;
}
static std::unique_ptr<Config>
makeSmallQueueConfig(
std::map<std::string, std::string> extraTxQ = {},
std::map<std::string, std::string> extraVoting = {})
{
auto p = test::jtx::envconfig();
auto& section = p->section("transaction_queue");
section.set("ledgers_in_queue", "2");
section.set("minimum_queue_size", "2");
section.set("min_ledgers_to_compute_size_limit", "3");
section.set("max_ledger_counts_to_store", "100");
section.set("retry_sequence_percent", "25");
section.set("normal_consensus_increase_percent", "0");
for (auto const& [k, v] : extraTxQ)
section.set(k, v);
return p;
}
static auto
openLedgerFee(jtx::Env& env, XRPAmount const& batchFee)
{
using namespace jtx;
auto const& view = *env.current();
auto metrics = env.app().getTxQ().getMetrics(view);
return toDrops(metrics.openLedgerFeeLevel, batchFee) + 1;
}
void
testEnable(FeatureBitset features)
{
using namespace test::jtx;
using namespace std::literals;
bool const withInnerSigFix = features[fixBatchInnerSigs];
for (bool const withBatch : {true, false})
{
testcase << "enabled: Batch " << (withBatch ? "enabled" : "disabled")
<< ", Inner Sig Fix: " << (withInnerSigFix ? "enabled" : "disabled");
auto const amend = withBatch ? features : features - featureBatch;
test::jtx::Env env{*this, amend};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
env.fund(XRP(10000), alice, bob, carol);
env.close();
// ttBatch
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto const txResult = withBatch ? ter(tesSUCCESS) : ter(temDISABLED);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(alice, bob, XRP(1)), seq + 2),
txResult);
env.close();
}
// tfInnerBatchTxn
// If the feature is disabled, the transaction fails with
// temINVALID_FLAG. If the feature is enabled, the transaction fails
// early in checkValidity()
{
auto const txResult = withBatch ? ter(telENV_RPC_FAILED) : ter(temINVALID_FLAG);
env(pay(alice, bob, XRP(1)), txflags(tfInnerBatchTxn), txResult);
env.close();
}
env.close();
}
}
void
testPreflight(FeatureBitset features)
{
testcase("preflight");
using namespace test::jtx;
using namespace std::literals;
//----------------------------------------------------------------------
// preflight
test::jtx::Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
env.fund(XRP(10000), alice, bob, carol);
env.close();
// temBAD_FEE: preflight1
{
env(batch::outer(alice, env.seq(alice), XRP(-1), tfAllOrNothing), ter(temBAD_FEE));
env.close();
}
// DEFENSIVE: temINVALID_FLAG: Batch: inner batch flag.
// ACTUAL: telENV_RPC_FAILED: checkValidity()
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 0);
env(batch::outer(alice, seq, batchFee, tfInnerBatchTxn), ter(telENV_RPC_FAILED));
env.close();
}
// temINVALID_FLAG: Batch: invalid flags.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 0);
env(batch::outer(alice, seq, batchFee, tfDisallowXRP), ter(temINVALID_FLAG));
env.close();
}
// temINVALID_FLAG: Batch: too many flags.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 0);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
txflags(tfAllOrNothing | tfOnlyOne),
ter(temINVALID_FLAG));
env.close();
}
// temARRAY_EMPTY: Batch: txns array must have at least 2 entries.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 0);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing), ter(temARRAY_EMPTY));
env.close();
}
// temARRAY_EMPTY: Batch: txns array must have at least 2 entries.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 0);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
ter(temARRAY_EMPTY));
env.close();
}
// DEFENSIVE: temARRAY_TOO_LARGE: Batch: txns array exceeds 8 entries.
// ACTUAL: telENV_RPC_FAILED: isRawTransactionOkay()
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 9);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(alice, bob, XRP(1)), seq + 2),
batch::inner(pay(alice, bob, XRP(1)), seq + 3),
batch::inner(pay(alice, bob, XRP(1)), seq + 4),
batch::inner(pay(alice, bob, XRP(1)), seq + 5),
batch::inner(pay(alice, bob, XRP(1)), seq + 6),
batch::inner(pay(alice, bob, XRP(1)), seq + 7),
batch::inner(pay(alice, bob, XRP(1)), seq + 8),
batch::inner(pay(alice, bob, XRP(1)), seq + 9),
ter(telENV_RPC_FAILED));
env.close();
}
// temREDUNDANT: Batch: duplicate Txn found.
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto jt = env.jtnofill(
batch::outer(alice, env.seq(alice), batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(alice, bob, XRP(10)), seq + 1));
env(jt.jv, batch::sig(bob), ter(temREDUNDANT));
env.close();
}
// DEFENSIVE: temINVALID: Batch: batch cannot have inner batch txn.
// ACTUAL: telENV_RPC_FAILED: isRawTransactionOkay()
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(batch::outer(alice, seq, batchFee, tfAllOrNothing), seq),
batch::inner(pay(alice, bob, XRP(1)), seq + 2),
ter(telENV_RPC_FAILED));
env.close();
}
// temINVALID_FLAG: Batch: inner txn must have the
// tfInnerBatchTxn flag.
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1[jss::Flags] = 0;
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(temINVALID_FLAG));
env.close();
}
// temBAD_SIGNATURE: Batch: inner txn cannot include TxnSignature.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto jt = env.jt(pay(alice, bob, XRP(1)));
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(jt.jv, seq + 1),
batch::inner(pay(alice, bob, XRP(1)), seq + 2),
ter(temBAD_SIGNATURE));
env.close();
}
// temBAD_SIGNER: Batch: inner txn cannot include Signers.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = pay(alice, bob, XRP(1));
tx1[sfSigners.jsonName] = Json::arrayValue;
tx1[sfSigners.jsonName][0U][sfSigner.jsonName] = Json::objectValue;
tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfAccount.jsonName] = alice.human();
tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfSigningPubKey.jsonName] =
strHex(alice.pk());
tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfTxnSignature.jsonName] = "DEADBEEF";
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(tx1, seq + 1),
batch::inner(pay(alice, bob, XRP(1)), seq + 2),
ter(temBAD_SIGNER));
env.close();
}
// temBAD_REGKEY: Batch: inner txn must include empty
// SigningPubKey.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = batch::inner(pay(alice, bob, XRP(1)), seq + 1);
tx1[jss::SigningPubKey] = strHex(alice.pk());
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(1)), seq + 2));
env(jt.jv, ter(temBAD_REGKEY));
env.close();
}
// temINVALID_INNER_BATCH: Batch: inner txn preflight failed.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
// amount can't be negative
batch::inner(pay(alice, bob, XRP(-1)), seq + 2),
ter(temINVALID_INNER_BATCH));
env.close();
}
// temBAD_FEE: Batch: inner txn must have a fee of 0.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = batch::inner(pay(alice, bob, XRP(1)), seq + 1);
tx1[jss::Fee] = to_string(env.current()->fees().base);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(2)), seq + 2),
ter(temBAD_FEE));
env.close();
}
// temBAD_FEE: Inner txn with negative fee
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = batch::inner(pay(alice, bob, XRP(1)), seq + 1);
tx1[jss::Fee] = "-1";
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(2)), seq + 2),
ter(temBAD_FEE));
env.close();
}
// temBAD_FEE: Inner txn with non-integer fee
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = batch::inner(pay(alice, bob, XRP(1)), seq + 1);
tx1[jss::Fee] = "1.5";
env.set_parse_failure_expected(true);
try
{
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(2)), seq + 2));
fail("Expected parse_error for fractional fee");
}
catch (jtx::parse_error const&)
{
BEAST_EXPECT(true);
}
env.set_parse_failure_expected(false);
}
// temSEQ_AND_TICKET: Batch: inner txn cannot have both Sequence
// and TicketSequence.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
auto tx1 = batch::inner(pay(alice, bob, XRP(1)), 0, 1);
tx1[jss::Sequence] = seq + 1;
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(2)), seq + 2),
ter(temSEQ_AND_TICKET));
env.close();
}
// temSEQ_AND_TICKET: Batch: inner txn must have either Sequence or
// TicketSequence.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), 0),
batch::inner(pay(alice, bob, XRP(2)), seq + 2),
ter(temSEQ_AND_TICKET));
env.close();
}
// temREDUNDANT: Batch: duplicate sequence found:
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(alice, bob, XRP(2)), seq + 1),
ter(temREDUNDANT));
env.close();
}
// temREDUNDANT: Batch: duplicate ticket found:
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), 0, seq + 1),
batch::inner(pay(alice, bob, XRP(2)), 0, seq + 1),
ter(temREDUNDANT));
env.close();
}
// temREDUNDANT: Batch: duplicate ticket & sequence found:
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 0, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), 0, seq + 1),
batch::inner(pay(alice, bob, XRP(2)), seq + 1),
ter(temREDUNDANT));
env.close();
}
// DEFENSIVE: temARRAY_TOO_LARGE: Batch: signers array exceeds 8
// entries.
// ACTUAL: telENV_RPC_FAILED: isRawTransactionOkay()
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 9, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(alice, bob, XRP(5)), seq + 2),
batch::sig(bob, carol, alice, bob, carol, alice, bob, carol, alice, alice),
ter(telENV_RPC_FAILED));
env.close();
}
// temBAD_SIGNER: Batch: signer cannot be the outer account
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 2, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::sig(alice, bob),
ter(temBAD_SIGNER));
env.close();
}
// temREDUNDANT: Batch: duplicate signer found
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 2, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::sig(bob, bob),
ter(temREDUNDANT));
env.close();
}
// temBAD_SIGNER: Batch: no account signature for inner txn.
// Note: Extra signature by bob
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(alice, bob, XRP(5)), seq + 2),
batch::sig(bob),
ter(temBAD_SIGNER));
env.close();
}
// temBAD_SIGNER: Batch: no account signature for inner txn.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::sig(carol),
ter(temBAD_SIGNER));
env.close();
}
// temBAD_SIGNATURE: Batch: invalid batch txn signature.
{
auto const seq = env.seq(alice);
auto const bobSeq = env.seq(bob);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto jt = env.jtnofill(
batch::outer(alice, env.seq(alice), batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), bobSeq));
Serializer msg;
serializeBatch(msg, tfAllOrNothing, jt.stx->getBatchTransactionIDs());
auto const sig = xrpl::sign(bob.pk(), bob.sk(), msg.slice());
jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfAccount.jsonName] =
bob.human();
jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfSigningPubKey.jsonName] =
strHex(alice.pk());
jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfTxnSignature.jsonName] =
strHex(Slice{sig.data(), sig.size()});
env(jt.jv, ter(temBAD_SIGNATURE));
env.close();
}
// temBAD_SIGNER: Batch: invalid batch signers.
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 2, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::inner(pay(carol, alice, XRP(5)), env.seq(carol)),
batch::sig(bob),
ter(temBAD_SIGNER));
env.close();
}
}
void
testPreclaim(FeatureBitset features)
{
testcase("preclaim");
using namespace test::jtx;
using namespace std::literals;
//----------------------------------------------------------------------
// preclaim
test::jtx::Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
auto const dave = Account("dave");
auto const elsa = Account("elsa");
auto const frank = Account("frank");
auto const phantom = Account("phantom");
env.memoize(phantom);
env.fund(XRP(10000), alice, bob, carol, dave, elsa, frank);
env.close();
//----------------------------------------------------------------------
// checkSign.checkSingleSign
// tefBAD_AUTH: Bob is not authorized to sign for Alice
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(alice, bob, XRP(20)), seq + 2),
sig(bob),
ter(tefBAD_AUTH));
env.close();
}
//----------------------------------------------------------------------
// checkBatchSign.checkMultiSign
// tefNOT_MULTI_SIGNING: SignersList not enabled
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {dave, carol}),
ter(tefNOT_MULTI_SIGNING));
env.close();
}
env(signers(alice, 2, {{bob, 1}, {carol, 1}}));
env.close();
env(signers(bob, 2, {{carol, 1}, {dave, 1}, {elsa, 1}}));
env.close();
// tefBAD_SIGNATURE: Account not in SignersList
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, frank}),
ter(tefBAD_SIGNATURE));
env.close();
}
// tefBAD_SIGNATURE: Wrong publicKey type
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, Account("dave", KeyType::ed25519)}),
ter(tefBAD_SIGNATURE));
env.close();
}
// tefMASTER_DISABLED: Master key disabled
{
env(regkey(elsa, frank));
env(fset(elsa, asfDisableMaster), sig(elsa));
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, elsa}),
ter(tefMASTER_DISABLED));
env.close();
}
// tefBAD_SIGNATURE: Signer does not exist
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, phantom}),
ter(tefBAD_SIGNATURE));
env.close();
}
// tefBAD_SIGNATURE: Signer has not enabled RegularKey
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
Account const davo{"davo", KeyType::ed25519};
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, Reg{dave, davo}}),
ter(tefBAD_SIGNATURE));
env.close();
}
// tefBAD_SIGNATURE: Wrong RegularKey Set
{
env(regkey(dave, frank));
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
Account const davo{"davo", KeyType::ed25519};
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, Reg{dave, davo}}),
ter(tefBAD_SIGNATURE));
env.close();
}
// tefBAD_QUORUM
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 2, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol}),
ter(tefBAD_QUORUM));
env.close();
}
// tesSUCCESS: BatchSigners.Signers
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 3, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, dave}),
ter(tesSUCCESS));
env.close();
}
// tesSUCCESS: Multisign + BatchSigners.Signers
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 4, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), seq + 1),
batch::inner(pay(bob, alice, XRP(5)), env.seq(bob)),
batch::msig(bob, {carol, dave}),
msig(bob, carol),
ter(tesSUCCESS));
env.close();
}
//----------------------------------------------------------------------
// checkBatchSign.checkSingleSign
// tefBAD_AUTH: Inner Account is not signer
{
auto const ledSeq = env.current()->seq();
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, phantom, XRP(1000)), seq + 1),
batch::inner(noop(phantom), ledSeq),
batch::sig(Reg{phantom, carol}),
ter(tefBAD_AUTH));
env.close();
}
// tefBAD_AUTH: Account is not signer
{
auto const ledSeq = env.current()->seq();
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1000)), seq + 1),
batch::inner(noop(bob), ledSeq),
batch::sig(Reg{bob, carol}),
ter(tefBAD_AUTH));
env.close();
}
// tesSUCCESS: Signed With Regular Key
{
env(regkey(bob, carol));
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(bob, alice, XRP(2)), env.seq(bob)),
batch::sig(Reg{bob, carol}),
ter(tesSUCCESS));
env.close();
}
// tesSUCCESS: Signed With Master Key
{
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(bob, alice, XRP(2)), env.seq(bob)),
batch::sig(bob),
ter(tesSUCCESS));
env.close();
}
// tefMASTER_DISABLED: Signed With Master Key Disabled
{
env(regkey(bob, carol));
env(fset(bob, asfDisableMaster), sig(bob));
auto const seq = env.seq(alice);
auto const batchFee = batch::calcBatchFee(env, 1, 2);
env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(1)), seq + 1),
batch::inner(pay(bob, alice, XRP(2)), env.seq(bob)),
batch::sig(bob),
ter(tefMASTER_DISABLED));
env.close();
}
}
void
testBadRawTxn(FeatureBitset features)
{
testcase("bad raw txn");
using namespace test::jtx;
using namespace std::literals;
test::jtx::Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
env.fund(XRP(10000), alice, bob);
// Invalid: sfTransactionType
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1.removeMember(jss::TransactionType);
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(telENV_RPC_FAILED));
env.close();
}
// Invalid: sfAccount
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1.removeMember(jss::Account);
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(telENV_RPC_FAILED));
env.close();
}
// Invalid: sfSequence
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1.removeMember(jss::Sequence);
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(telENV_RPC_FAILED));
env.close();
}
// Invalid: sfFee
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1.removeMember(jss::Fee);
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(telENV_RPC_FAILED));
env.close();
}
// Invalid: sfSigningPubKey
{
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const seq = env.seq(alice);
auto tx1 = batch::inner(pay(alice, bob, XRP(10)), seq + 1);
tx1.removeMember(jss::SigningPubKey);
auto jt = env.jtnofill(
batch::outer(alice, seq, batchFee, tfAllOrNothing),
tx1,
batch::inner(pay(alice, bob, XRP(10)), seq + 2));
env(jt.jv, batch::sig(bob), ter(telENV_RPC_FAILED));
env.close();
}
}
void
testBadSequence(FeatureBitset features)
{
testcase("bad sequence");
using namespace test::jtx;
using namespace std::literals;
test::jtx::Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account("gw");
auto const USD = gw["USD"];
env.fund(XRP(10000), alice, bob, gw);
env.close();
env.trust(USD(1000), alice, bob);
env(pay(gw, alice, USD(100)));
env(pay(gw, bob, USD(100)));
env.close();
env(noop(bob), ter(tesSUCCESS));
env.close();
// Invalid: Alice Sequence is a past sequence
{
auto const preAliceSeq = env.seq(alice);
auto const preAlice = env.balance(alice);
auto const preAliceUSD = env.balance(alice, USD.issue());
auto const preBobSeq = env.seq(bob);
auto const preBob = env.balance(bob);
auto const preBobUSD = env.balance(bob, USD.issue());
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const [txIDs, batchID] = submitBatch(
env,
tesSUCCESS,
batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), preAliceSeq - 10),
batch::inner(pay(bob, alice, XRP(5)), preBobSeq),
batch::sig(bob));
env.close();
{
std::vector<TestLedgerData> testCases = {
{0, "Batch", "tesSUCCESS", batchID, std::nullopt},
};
validateClosedLedger(env, testCases);
}
env.close();
{
// next ledger is empty
std::vector<TestLedgerData> testCases = {};
validateClosedLedger(env, testCases);
}
// Alice pays fee & Bob should not be affected.
BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
BEAST_EXPECT(env.balance(alice, USD.issue()) == preAliceUSD);
BEAST_EXPECT(env.seq(bob) == preBobSeq);
BEAST_EXPECT(env.balance(bob) == preBob);
BEAST_EXPECT(env.balance(bob, USD.issue()) == preBobUSD);
}
// Invalid: Alice Sequence is a future sequence
{
auto const preAliceSeq = env.seq(alice);
auto const preAlice = env.balance(alice);
auto const preAliceUSD = env.balance(alice, USD.issue());
auto const preBobSeq = env.seq(bob);
auto const preBob = env.balance(bob);
auto const preBobUSD = env.balance(bob, USD.issue());
auto const batchFee = batch::calcBatchFee(env, 1, 2);
auto const [txIDs, batchID] = submitBatch(
env,
tesSUCCESS,
batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
batch::inner(pay(alice, bob, XRP(10)), preAliceSeq + 10),
batch::inner(pay(bob, alice, XRP(5)), preBobSeq),
batch::sig(bob));