/
XahauGenesis_test.cpp
5023 lines (4342 loc) · 170 KB
/
XahauGenesis_test.cpp
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
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/misc/HashRouter.h>
#include <ripple/app/tx/apply.h>
#include <ripple/app/tx/impl/XahauGenesis.h>
#include <ripple/json/json_reader.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/jss.h>
#include <string>
#include <test/jtx.h>
#include <vector>
#define BEAST_REQUIRE(x) \
{ \
BEAST_EXPECT(!!(x)); \
if (!(x)) \
return; \
}
// Function to handle integer types
template <typename T>
std::string
maybe_to_string(T val, std::enable_if_t<std::is_integral_v<T>, int> = 0)
{
return std::to_string(val);
}
// Overload to handle non-integer types
template <typename T>
std::string
maybe_to_string(T val, std::enable_if_t<!std::is_integral_v<T>, int> = 0)
{
return val;
}
#define M(m) memo(maybe_to_string(m), "", "")
#define DEBUG_XGTEST 0
using namespace XahauGenesis;
namespace ripple {
namespace test {
/*
Accounts used in this test suite:
alice: AE123A8556F3CF91154711376AFB0F894F832B3D,
rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn bob:
F51DFC2A09D62CBBA1DFBDD4691DAC96AD98B90F, rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK
carol: B389FBCED0AF9DCDFF62900BFAEFA3EB872D8A96,
rH4KEcG9dEwGwpn6AyoWK9cZPLL4RLSmWW david:
0F4BFC99EC975E3F753927A69713889359C7100E, rpPtwXbmeXxznrUvnMuGhUqTu4Vvk4V98i
edward: 98D3AAD96D5D3F32C3723B6550A49DEE4DD9D4AC,
rNAnHhommqNJvV3mLieHADNJU6mfdRqXKp m1:
1EF5C53AC2B0E1CDEAF960833A9B7B6814879152, rsF66BeCcW5dxrDhPbCqm4ReU5MjkmshQo
m2: 0D20E8D13A89AA84F2334F57331903D21CCC866C,
rpURDnkgizBF1ksJr9DmDTNLhds2NBYNbC m6:
F41810565A673D2C45C63B7A51FEA139221DFB1B, rPEe68FWPYpfEYeL3TtdVGXR8WDET24h6e
m7: 52C947B84E2412B5BD2211639E2B9DEDF3ECB5F4,
r3YjXvPbu1srxx22tQefDwhXJS8qjCAgxg m8:
3392382F09E5EA935D5624D52919C683B661238A, rn6gdHFFtieGK748TioK8cZ8SPrHsWAfe3
m9: 62F73BE6E7718B3328DEB497F8298B1C42FFD3D8,
rwpHPvGwiJAFStN3KqVTeTLBTczFyK6Z6D m10:
971B25036F1EAAEEC7A0C299E15F60AB4C465B37, rNmyZrJEtbg5p84PSKGurNs1efdDNLhb8D
m11: B79B324A56425919D0BF64FA4560C7B616F08509,
rHjF2LuJKSRerNHtQMtyDUVXpQKmT18XuC m12:
8C3AA72EBB3172726BE9FDE2A91C9CE2C9F766E5, rD8T19Nz2gkAtKKEeLpXLkTb57AS7nJ45b
m13: 3B8292604D9CF9C679E70CCD0ED5C9DC1DF84607,
raRCHchPDpP8qAysxsmehC279GH613iGiM m14:
E77F5DFE960C4DA9524389F946BCD13AC26AB0D0, r4fsCASeoGzhD2ZeFgcJj1c2tmDsm1re3r
m15: D300D94BA59F55426889E144B423CFEE8F685450,
rLNgbTE2HVk5CJG7SHrpSD8gsFcwu5cG1a m16:
D9C6AC46D87BCFE374BE4F59C280CEC377E67788, rLiVdCesA2rV2NjC6HZPVK7k1VpvGwCbjx
m17: A620E154D56E38B12AD76A094A214A25AD8B87A1,
rG9QZQDR8XqCU1x2VARPuZwYxqcb3J9bYa m18:
7A5A72F059F6DCDD6E938DAFBCFFB0541E0A7481, rU9A8u622H6fxGQjux8uDgomks5D8QySfS
m19: F6C06C3D86A9D39FF813AEF6B839AD041651BE7D,
rPV6jx8QoQBCR1AcmVLDnu98QBu4QakwhU m20:
1B4D3113C2AB370293A0ACEA4D68C1B29A01A013, rsVM5ZaK9QMCgrW9UKyXLguESpDpsJnRbu
m21: 748B256D2BAD918A967F40F465692C7B9A01F836,
rBdNnJ4q9G3riJFcXjkwTgBu1MBR5pG4gD
*/
struct XahauGenesis_test : public beast::unit_test::suite
{
uint256 const acceptHookHash = ripple::sha512Half_s(ripple::Slice(
jtx::genesis::AcceptHook.data(),
jtx::genesis::AcceptHook.size()));
uint256 const governHookHash = ripple::sha512Half_s(ripple::Slice(
XahauGenesis::GovernanceHook.data(),
XahauGenesis::GovernanceHook.size()));
uint256 const rewardHookHash = ripple::sha512Half_s(ripple::Slice(
XahauGenesis::RewardHook.data(),
XahauGenesis::RewardHook.size()));
AccountID const genesisAccID = calcAccountID(
generateKeyPair(KeyType::secp256k1, generateSeed("masterpassphrase"))
.first);
// the test cases in this test suite are based on changing the state of the
// ledger before xahaugenesis is activated, to do this they call this
// templated function with an "execute-first" lambda
void
activate(
uint64_t lineno,
jtx::Env& env,
bool burnedViaTest =
false, // means the calling test already burned some of the genesis
bool skipTests = false,
bool const testFlag = false)
{
using namespace jtx;
auto isEnabled = [&](void) -> bool {
auto const obj = env.le(keylet::amendments());
if (!obj)
return false;
STVector256 amendments = obj->getFieldV256(sfAmendments);
return std::find(
amendments.begin(),
amendments.end(),
featureXahauGenesis) != amendments.end();
};
BEAST_EXPECT(!isEnabled());
uint32_t const startLgr =
env.app().getLedgerMaster().getClosedLedger()->info().seq + 1;
if (DEBUG_XGTEST)
{
std::cout << "activate called. " << lineno << "L"
<< " burnedViaTest: "
<< (burnedViaTest ? "true" : "false")
<< " skipTests: " << (skipTests ? "true" : "false")
<< " testFlags: " << (testFlag ? "true" : "false")
<< " startLgr: " << startLgr << "\n";
}
// insert a ttAMENDMENT pseudo into the open ledger
env.app().openLedger().modify(
[&](OpenView& view, beast::Journal j) -> bool {
STTx tx(ttAMENDMENT, [&](auto& obj) {
obj.setAccountID(sfAccount, AccountID());
obj.setFieldH256(sfAmendment, featureXahauGenesis);
obj.setFieldU32(sfLedgerSequence, startLgr);
if (testFlag)
obj.setFieldU32(sfFlags, tfTestSuite);
});
uint256 txID = tx.getTransactionID();
auto s = std::make_shared<ripple::Serializer>();
tx.add(*s);
env.app().getHashRouter().setFlags(txID, SF_PRIVATE2);
view.rawTxInsert(txID, std::move(s), nullptr);
return true;
});
// close the ledger
env.close();
BEAST_EXPECT(isEnabled());
if (skipTests)
return;
// sum the initial distribution balances, these should equal total coins
// in the closed ledger
std::vector<std::pair<std::string, XRPAmount>> const& l1membership =
testFlag ? XahauGenesis::TestL1Membership
: XahauGenesis::L1Membership;
XRPAmount total{GenesisAmount};
for (auto const& [node, amt] : l1membership)
total += amt;
// and the non gov distributions
std::vector<std::pair<std::string, XRPAmount>> const& ngdist = testFlag
? XahauGenesis::TestNonGovernanceDistribution
: XahauGenesis::NonGovernanceDistribution;
for (auto const& [node, amt] : ngdist)
total += amt;
BEAST_EXPECT(
burnedViaTest ||
env.app().getLedgerMaster().getClosedLedger()->info().drops ==
total);
// is the hook array present
auto genesisHooksLE = env.le(keylet::hook(genesisAccID));
BEAST_REQUIRE(!!genesisHooksLE);
auto genesisHookArray = genesisHooksLE->getFieldArray(sfHooks);
BEAST_EXPECT(genesisHookArray.size() == 2);
// make sure the account root exists and has the correct balance and
// ownercount
auto genesisAccRoot = env.le(keylet::account(genesisAccID));
BEAST_REQUIRE(!!genesisAccRoot);
BEAST_EXPECT(
genesisAccRoot->getFieldAmount(sfBalance) ==
XahauGenesis::GenesisAmount);
BEAST_EXPECT(genesisAccRoot->getFieldU32(sfOwnerCount) == 2);
// ensure the definitions are correctly set
{
auto const govHash = ripple::sha512Half_s(
ripple::Slice(GovernanceHook.data(), GovernanceHook.size()));
auto const govKL = keylet::hookDefinition(govHash);
auto govSLE = env.le(govKL);
BEAST_EXPECT(!!govSLE);
BEAST_EXPECT(govSLE->getFieldH256(sfHookHash) == govHash);
auto const govVL = govSLE->getFieldVL(sfCreateCode);
BEAST_EXPECT(
govHash ==
ripple::sha512Half_s(
ripple::Slice(govVL.data(), govVL.size())));
BEAST_EXPECT(
govSLE->getFieldU64(sfReferenceCount) == 1 + testFlag
? XahauGenesis::TestL2Membership.size()
: XahauGenesis::L2Membership.size());
BEAST_EXPECT(
govSLE->getFieldH256(sfHookOn) ==
ripple::uint256("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFF"
"FFFFFFFFFFFFBFFFFF"));
BEAST_EXPECT(
govSLE->getFieldH256(sfHookNamespace) ==
ripple::uint256("0000000000000000000000000000000000000000000000"
"000000000000000000"));
BEAST_EXPECT(govSLE->getFieldU16(sfHookApiVersion) == 0);
auto const govFee = govSLE->getFieldAmount(sfFee);
BEAST_EXPECT(isXRP(govFee) && govFee > beast::zero);
BEAST_EXPECT(!govSLE->isFieldPresent(sfHookCallbackFee));
BEAST_EXPECT(govSLE->getFieldH256(sfHookSetTxnID) != beast::zero);
BEAST_EXPECT(
genesisHookArray[0].getFieldH256(sfHookHash) == govHash);
auto const rwdHash = ripple::sha512Half_s(
ripple::Slice(RewardHook.data(), RewardHook.size()));
auto const rwdKL = keylet::hookDefinition(rwdHash);
auto rwdSLE = env.le(rwdKL);
BEAST_EXPECT(!!rwdSLE);
BEAST_EXPECT(rwdSLE->getFieldH256(sfHookHash) == rwdHash);
auto const rwdVL = rwdSLE->getFieldVL(sfCreateCode);
BEAST_EXPECT(
rwdHash ==
ripple::sha512Half_s(
ripple::Slice(rwdVL.data(), rwdVL.size())));
BEAST_EXPECT(rwdSLE->getFieldU64(sfReferenceCount) == 1);
BEAST_EXPECT(
rwdSLE->getFieldH256(sfHookOn) ==
ripple::uint256("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFF"
"FFFFFFFFFFFFBFFFFF"));
BEAST_EXPECT(
rwdSLE->getFieldH256(sfHookNamespace) ==
ripple::uint256("0000000000000000000000000000000000000000000000"
"000000000000000000"));
BEAST_EXPECT(rwdSLE->getFieldU16(sfHookApiVersion) == 0);
auto const rwdFee = rwdSLE->getFieldAmount(sfFee);
BEAST_EXPECT(isXRP(rwdFee) && rwdFee > beast::zero);
BEAST_EXPECT(!rwdSLE->isFieldPresent(sfHookCallbackFee));
BEAST_EXPECT(rwdSLE->getFieldH256(sfHookSetTxnID) != beast::zero);
BEAST_EXPECT(
genesisHookArray[1].getFieldH256(sfHookHash) == rwdHash);
}
// check non-governance distributions
{
for (auto const& [rn, x] : ngdist)
{
const char first = rn.c_str()[0];
BEAST_EXPECT(
(first == 'r' && !!parseBase58<AccountID>(rn)) ||
first == 'n' &&
!!parseBase58<PublicKey>(TokenType::NodePublic, rn));
if (first == 'r')
{
AccountID id = *parseBase58<AccountID>(rn);
auto acc = env.le(keylet::account(id));
BEAST_EXPECT(!!acc);
auto bal = acc->getFieldAmount(sfBalance);
BEAST_EXPECT(bal == STAmount(x));
continue;
}
// node based addresses
auto const pk =
parseBase58<PublicKey>(TokenType::NodePublic, rn);
BEAST_EXPECT(!!pk);
AccountID id = calcAccountID(*pk);
auto acc = env.le(keylet::account(id));
BEAST_EXPECT(!!acc);
auto bal = acc->getFieldAmount(sfBalance);
BEAST_EXPECT(bal == STAmount(x));
}
}
// check distribution amounts and hook parameters
{
uint8_t member_count = 0;
std::vector<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>>
params = XahauGenesis::GovernanceParameters;
for (auto const& [rn, x] : l1membership)
{
const char first = rn.c_str()[0];
BEAST_EXPECT(
(first == 'r' && !!parseBase58<AccountID>(rn)) ||
first == 'n' &&
!!parseBase58<PublicKey>(TokenType::NodePublic, rn));
if (first == 'r')
{
AccountID id = *parseBase58<AccountID>(rn);
auto acc = env.le(keylet::account(id));
BEAST_EXPECT(!!acc);
auto bal = acc->getFieldAmount(sfBalance);
BEAST_EXPECT(bal == STAmount(x));
params.emplace_back(
std::vector<uint8_t>{'I', 'S', member_count++},
std::vector<uint8_t>(id.data(), id.data() + 20));
continue;
}
// node based addresses
auto const pk =
parseBase58<PublicKey>(TokenType::NodePublic, rn);
BEAST_EXPECT(!!pk);
AccountID id = calcAccountID(*pk);
auto acc = env.le(keylet::account(id));
BEAST_EXPECT(!!acc);
auto bal = acc->getFieldAmount(sfBalance);
BEAST_EXPECT(bal == STAmount(x));
// initial member enumeration
params.emplace_back(
std::vector<uint8_t>{'I', 'S', member_count++},
std::vector<uint8_t>(id.data(), id.data() + 20));
}
// initial member count
params.emplace_back(
std::vector<uint8_t>{'I', 'M', 'C'},
std::vector<uint8_t>{member_count});
auto leParams = genesisHookArray[0].getFieldArray(sfHookParameters);
if (DEBUG_XGTEST)
{
std::cout << "leParams.size() = " << leParams.size()
<< ", params.size() == " << params.size() << "\n";
std::cout << "leParams:\n";
for (auto const& p : leParams)
{
std::cout
<< "\t" << strHex(p.getFieldVL(sfHookParameterName))
<< " : " << strHex(p.getFieldVL(sfHookParameterValue))
<< "\n";
}
std::cout << "params:\n";
for (auto const& [k, v] : params)
std::cout << "\t" << strHex(k) << " : " << strHex(v)
<< "\n";
}
// check parameters
BEAST_EXPECT(leParams.size() == params.size());
// these should be recorded in the same order
std::set<std::vector<uint8_t>> keys_used;
int counter = 0;
for (auto& param : leParams)
{
auto key = param.getFieldVL(sfHookParameterName);
auto val = param.getFieldVL(sfHookParameterValue);
// no duplicates allowed
BEAST_EXPECT(keys_used.find(key) == keys_used.end());
BEAST_EXPECT(counter < leParams.size());
BEAST_EXPECT(params[counter].first == key);
BEAST_EXPECT(params[counter].second == val);
// add key to used set
keys_used.emplace(key);
counter++;
}
}
// check fees object correctly recordsed activation seq
auto fees = env.le(keylet::fees());
BEAST_REQUIRE(!!fees);
BEAST_EXPECT(
fees->isFieldPresent(sfXahauActivationLgrSeq) &&
fees->getFieldU32(sfXahauActivationLgrSeq) == startLgr);
// ensure no signerlist
BEAST_EXPECT(!env.le(keylet::signers(genesisAccID)));
// ensure correctly blackholed
BEAST_EXPECT(
genesisAccRoot->isFieldPresent(sfRegularKey) &&
genesisAccRoot->getAccountID(sfRegularKey) == noAccount() &&
genesisAccRoot->getFieldU32(sfFlags) & lsfDisableMaster);
}
void
testPlainActivation(FeatureBitset features)
{
testcase("Test activation");
using namespace jtx;
Env env{*this, envconfig(), features - featureXahauGenesis};
activate(__LINE__, env, false, false, false);
}
void
testWithSignerList(FeatureBitset features)
{
using namespace jtx;
testcase("Test signerlist");
Env env{*this, envconfig(), features - featureXahauGenesis};
Account const alice{"alice", KeyType::ed25519};
env.fund(XRP(1000), alice);
env.memoize(env.master);
env(signers(env.master, 1, {{alice, 1}}));
env.close();
activate(__LINE__, env, true, false, false);
}
void
testWithRegularKey(FeatureBitset features)
{
using namespace jtx;
testcase("Test regkey");
Env env{*this, envconfig(), features - featureXahauGenesis};
env.memoize(env.master);
Account const alice("alice");
env.fund(XRP(10000), alice);
env(regkey(env.master, alice));
env.close();
activate(__LINE__, env, true, false, false);
}
void
setupGov(
jtx::Env& env,
std::vector<AccountID> const members,
std::map<AccountID, std::vector<AccountID>> const tables = {})
{
using namespace jtx;
auto const invoker = Account("invoker");
env.fund(XRP(10000), invoker);
env.close();
XahauGenesis::TestNonGovernanceDistribution.clear();
XahauGenesis::TestL1Membership.clear();
XahauGenesis::TestL2Membership.clear();
for (auto& m : members)
{
std::string acc = toBase58(m);
XahauGenesis::TestL1Membership.emplace_back(
acc, XRPAmount(10000000000));
}
for (auto& [t, members] : tables)
{
std::vector<std::string> membersStr;
for (auto& m : members)
membersStr.emplace_back(toBase58(m));
XahauGenesis::TestL2Membership.emplace_back(
toBase58(t), membersStr);
}
activate(__LINE__, env, true, false, true);
env.close();
env.close();
XahauGenesis::TestNonGovernanceDistribution.clear();
XahauGenesis::TestL1Membership.clear();
XahauGenesis::TestL2Membership.clear();
Json::Value invoke;
invoke[jss::TransactionType] = "Invoke";
invoke[jss::Account] = invoker.human();
invoke[jss::Destination] = env.master.human();
env(invoke, fee(XRP(10)));
env.close();
env.close();
for (auto& [t, members] : tables)
{
// setup each L2 table
Json::Value invoke;
invoke[jss::TransactionType] = "Invoke";
invoke[jss::Account] = invoker.human();
invoke[jss::Destination] = toBase58(t);
env(invoke, fee(XRP(1)));
env.close();
if (DEBUG_XGTEST)
std::cout << "invoke: " << invoke << "\n";
}
}
inline static std::string
charToHex(uint8_t inp)
{
std::string ret("00");
ret.data()[0] = "0123456789ABCDEF"[inp >> 4];
ret.data()[1] = "0123456789ABCDEF"[(inp >> 0) & 0xFU];
return ret;
}
inline static std::vector<uint8_t>
vecFromAcc(jtx::Account const& acc)
{
// this was changed to a less efficient version
// to accomodate older compilers
std::vector<uint8_t> vec;
vec.reserve(20);
for (int i = 0; i < 20; ++i)
vec.push_back(acc.id().data()[i]);
return vec;
};
inline static Json::Value
vote(
uint16_t lineno,
jtx::Account const& acc,
jtx::Account const& table,
char topic1,
std::optional<char> topic2,
std::vector<uint8_t> data,
std::optional<uint8_t> layer = std::nullopt)
{
Json::Value txn(Json::objectValue);
txn[jss::HookParameters] = Json::arrayValue;
txn[jss::HookParameters][0u] = Json::objectValue;
txn[jss::HookParameters][0u][jss::HookParameter] = Json::objectValue;
txn[jss::HookParameters][0u][jss::HookParameter]
[jss::HookParameterName] = "54"; // 'T'
std::string val =
charToHex(topic1) + (topic2 ? charToHex(*topic2) : "");
if (DEBUG_XGTEST)
std::cout << "val: `" << val << "`\n";
txn[jss::HookParameters][0u][jss::HookParameter]
[jss::HookParameterValue] = val;
txn[jss::HookParameters][1u] = Json::objectValue;
txn[jss::HookParameters][1u][jss::HookParameter]
[jss::HookParameterName] = "56"; // 'V'
{
std::string strData;
strData.reserve(data.size() << 1U);
for (uint8_t c : data)
strData += charToHex(c);
txn[jss::HookParameters][1u][jss::HookParameter]
[jss::HookParameterValue] = strData;
}
uint8_t upto = 2u;
if (layer)
{
txn[jss::HookParameters][upto] = Json::objectValue;
txn[jss::HookParameters][upto][jss::HookParameter]
[jss::HookParameterName] = "4C";
txn[jss::HookParameters][upto][jss::HookParameter]
[jss::HookParameterValue] = charToHex(*layer);
upto++;
}
// add a line number for debugging
txn[jss::HookParameters][upto] = Json::objectValue;
txn[jss::HookParameters][upto][jss::HookParameter]
[jss::HookParameterName] = "44"; // D
{
std::string strData;
strData += charToHex((uint8_t)(lineno >> 8U));
strData += charToHex((uint8_t)(lineno & 0xFFU));
txn[jss::HookParameters][upto][jss::HookParameter]
[jss::HookParameterValue] = strData;
}
txn[jss::Account] = acc.human();
txn[jss::TransactionType] = "Invoke";
txn[jss::Destination] = table.human();
return txn;
};
std::vector<uint8_t> const null_acc_id{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
AccountID const null_acc;
inline static uint256
makeStateKey(
char voteType,
char topic1,
char topic2,
uint8_t layer,
AccountID const& id)
{
uint8_t data[32];
memset(data, 0, 32);
data[0] = voteType;
data[1] = topic1;
data[2] = topic2;
data[3] = layer;
for (int i = 0; i < 20; ++i)
data[12 + i] = id.data()[i];
return uint256::fromVoid(data);
}
uint8_t const member_count_key[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 'M', 'C'};
void
testGovernanceL1(FeatureBitset features)
{
using namespace jtx;
testcase("Test governance membership voting L1");
Env env{*this, envconfig(), features - featureXahauGenesis, nullptr};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
auto const david = Account("david");
auto const edward = Account("edward");
auto const m1 = Account("m1");
auto const m2 = Account("m2");
auto const m6 = Account("m6");
auto const m7 = Account("m7");
auto const m8 = Account("m8");
auto const m9 = Account("m9");
auto const m10 = Account("m10");
auto const m11 = Account("m11");
auto const m12 = Account("m12");
auto const m13 = Account("m13");
auto const m14 = Account("m14");
auto const m15 = Account("m15");
auto const m16 = Account("m16");
auto const m17 = Account("m17");
auto const m18 = Account("m18");
auto const m19 = Account("m19");
auto const m20 = Account("m20");
auto const m21 = Account("m21");
env.fund(
XRP(10000),
alice,
bob,
carol,
david,
edward,
m1,
m2,
m6,
m7,
m8,
m9,
m10,
m11,
m12,
m13,
m14,
m15,
m16,
m17,
m18,
m19,
m20,
m21);
env.close();
std::vector<Account> initial_members{alice, bob, carol, david, edward};
std::vector<AccountID> initial_members_ids{
alice.id(), bob.id(), carol.id(), david.id(), edward.id()};
setupGov(env, initial_members_ids);
auto doL1Vote = [&](uint64_t lineno,
Account const& acc,
char topic1,
char topic2,
std::vector<uint8_t> const& vote_data,
std::vector<uint8_t> const& old_data,
bool actioned = true,
bool const shouldFail = false) {
if (shouldFail)
actioned = false;
bool isZero = true;
for (auto& x : vote_data)
if (x != 0)
{
isZero = false;
break;
}
bool isOldDataZero = true;
for (auto& x : old_data)
if (x != 0)
{
isOldDataZero = false;
break;
}
uint8_t const key[32] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
static_cast<uint8_t>(topic1 == 'S' ? 0 : topic1),
static_cast<uint8_t>(topic2)};
// check actioning prior to vote
{
auto entry = env.le(keylet::hookState(
env.master.id(), uint256::fromVoid(key), beast::zero));
if (DEBUG_XGTEST)
{
std::cout
<< "topic vote precheck: " << lineno << "L\n"
<< "\tacc: " << acc.human()
<< " shouldAction: " << (actioned ? "true" : "false")
<< "\tshouldFail: " << (shouldFail ? "true" : "false")
<< "\n"
<< "\ttopic: " << topic1 << ","
<< (topic2 < 48 ? topic2 + '1' : topic2) << "\n"
<< "\tisOldDataZero: "
<< (isOldDataZero ? "true" : "false") << "\n"
<< "\tisVoteDataZero: " << (isZero ? "true" : "false")
<< "\n"
<< "\told_data: " << strHex(old_data) << "\n"
<< "\tlgr_data: "
<< (!entry ? "doesn't exist"
: strHex(entry->getFieldVL(sfHookStateData)))
<< "\n"
<< "\tnew_data: " << strHex(vote_data) << "\n"
<< "\t(isOldDataZero && !entry): "
<< (isOldDataZero && !entry ? "true" : "false") << "\n"
<< "\t(entry && entry->getFieldVL(sfHookStateData) == "
"old_data): "
<< (entry &&
entry->getFieldVL(sfHookStateData) ==
old_data
? "true"
: "false")
<< "\n"
<< ((isOldDataZero && !entry) ||
(entry &&
entry->getFieldVL(sfHookStateData) ==
old_data)
? ""
: "\tfailed: ^^^\n");
}
BEAST_EXPECT(
(isOldDataZero && !entry) ||
(entry && entry->getFieldVL(sfHookStateData) == old_data));
}
// perform and check vote
{
env(vote(lineno, acc, env.master, topic1, topic2, vote_data),
M(lineno),
fee(XRP(1)),
shouldFail ? ter(tecHOOK_REJECTED) : ter(tesSUCCESS));
env.close();
auto entry = env.le(keylet::hookState(
env.master.id(),
makeStateKey('V', topic1, topic2, 1, acc.id()),
beast::zero));
if (!shouldFail)
{
BEAST_REQUIRE(!!entry);
auto lgr_data = entry->getFieldVL(sfHookStateData);
BEAST_EXPECT(lgr_data.size() == vote_data.size());
BEAST_EXPECT(lgr_data == vote_data);
}
}
// check actioning
if (!shouldFail)
{
// if the vote count isn't high enough it will be hte old value
// if it's high enough it will be the new value
auto entry = env.le(keylet::hookState(
env.master.id(), uint256::fromVoid(key), beast::zero));
if (!actioned && isOldDataZero)
{
BEAST_EXPECT(
!entry ||
entry->getFieldVL(sfHookStateData) == old_data);
return;
}
if (actioned)
{
if (isZero)
BEAST_EXPECT(!entry);
else
{
if (DEBUG_XGTEST)
{
std::cout << "new data: " << strHex(vote_data)
<< "\n";
std::cout << "lgr data: "
<< (!entry ? "<doesn't exist>"
: strHex(entry->getFieldVL(
sfHookStateData)))
<< "\n";
}
BEAST_EXPECT(
!!entry &&
entry->getFieldVL(sfHookStateData) == vote_data);
}
}
else
{
if (!!entry && DEBUG_XGTEST)
std::cout << "old data: "
<< strHex(entry->getFieldVL(sfHookStateData))
<< "\n";
BEAST_EXPECT(
!!entry &&
entry->getFieldVL(sfHookStateData) == old_data);
}
}
};
// 100% vote for a different reward rate
{
// this will be the new reward rate
std::vector<uint8_t> vote_data{
0x00U, 0x81U, 0xC6U, 0xA4U, 0x7EU, 0x8DU, 0x43U, 0x54U};
// this is the default reward rate
std::vector<uint8_t> const original_data{
0x55U, 0x55U, 0x40U, 0x25U, 0xA6U, 0xD7U, 0xCBU, 0x53U};
doL1Vote(
__LINE__, alice, 'R', 'R', vote_data, original_data, false);
doL1Vote(__LINE__, bob, 'R', 'R', vote_data, original_data, false);
doL1Vote(
__LINE__, carol, 'R', 'R', vote_data, original_data, false);
doL1Vote(
__LINE__, david, 'R', 'R', vote_data, original_data, false);
doL1Vote(
__LINE__, edward, 'R', 'R', vote_data, original_data, true);
// reverting a vote should not undo the action
doL1Vote(
__LINE__, carol, 'R', 'R', original_data, vote_data, false);
// submitting a null vote should delete the vote, and should not
// undo the action
std::vector<uint8_t> const null_data{0, 0, 0, 0, 0, 0, 0, 0};
doL1Vote(__LINE__, david, 'R', 'R', null_data, vote_data, false);
}
// 100% vote for a different reward delay
{
// this will be the new reward delay
std::vector<uint8_t> vote_data{
0x00U, 0x80U, 0xC6U, 0xA4U, 0x7EU, 0x8DU, 0x03U, 0x55U};
// this is the default reward delay
std::vector<uint8_t> const original_data{
0x00U, 0x80U, 0x6AU, 0xACU, 0xAFU, 0x3CU, 0x09U, 0x56U};
doL1Vote(
__LINE__, edward, 'R', 'D', vote_data, original_data, false);
doL1Vote(
__LINE__, david, 'R', 'D', vote_data, original_data, false);
doL1Vote(
__LINE__, carol, 'R', 'D', vote_data, original_data, false);
doL1Vote(__LINE__, bob, 'R', 'D', vote_data, original_data, false);
doL1Vote(__LINE__, alice, 'R', 'D', vote_data, original_data, true);
// reverting a vote should not undo the action
doL1Vote(
__LINE__, david, 'R', 'D', original_data, vote_data, false);
// submitting a null vote should delete the vote, and should not
// undo the action
std::vector<uint8_t> const null_data{0, 0, 0, 0, 0, 0, 0, 0};
doL1Vote(__LINE__, alice, 'R', 'D', null_data, vote_data, false);
}
// 100% vote to install the accept hook at hook position 7
// create a definition for accept hook first
{
Json::Value tx(Json::objectValue);
tx[jss::Account] = m21.human();
tx[jss::TransactionType] = "SetHook";
tx[jss::Hooks] = Json::arrayValue;
tx[jss::Hooks][0u] = Json::objectValue;
tx[jss::Hooks][0u][jss::Hook] = Json::objectValue;
tx[jss::Hooks][0u][jss::Hook][jss::CreateCode] =
strHex(jtx::genesis::AcceptHook);
tx[jss::Hooks][0u][jss::Hook][jss::HookApiVersion] = "0";
tx[jss::Hooks][0u][jss::Hook][jss::HookNamespace] =
"00000000000000000000000000000000000000000000000000000000000000"
"00";
tx[jss::Hooks][0u][jss::Hook][jss::HookOn] =
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFFFFFFFFFBFFF"
"FF";
// we'll also make a reference anchor for reward and governance
// hooks here, so they aren't deleted from the ledger in subsequent
// tests
tx[jss::Hooks][1u] = Json::objectValue;
tx[jss::Hooks][1u][jss::Hook] = Json::objectValue;
tx[jss::Hooks][1u][jss::Hook][jss::HookHash] =
strHex(governHookHash);
tx[jss::Hooks][2u] = Json::objectValue;
tx[jss::Hooks][2u][jss::Hook] = Json::objectValue;
tx[jss::Hooks][2u][jss::Hook][jss::HookHash] =