-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsidechaindb_tests.cpp
More file actions
1404 lines (1114 loc) · 46.5 KB
/
Copy pathsidechaindb_tests.cpp
File metadata and controls
1404 lines (1114 loc) · 46.5 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
// Copyright (c) 2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "chainparams.h"
#include "consensus/validation.h"
#include "core_io.h"
#include "miner.h"
#include "random.h"
#include "script/script.h"
#include "script/sigcache.h"
#include "sidechain.h"
#include "sidechaindb.h"
#include "uint256.h"
#include "utilstrencodings.h"
#include "validation.h"
#include "test/test_drivenet.h"
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(sidechaindb_tests, TestingSetup)
bool ActivateSidechain(SidechainDB& scdbTest, const SidechainProposal& proposal, int nHeight)
{
/* Activate a sidechain for testing purposes */
unsigned int nActive = scdbTest.GetActiveSidechainCount();
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal.GetScript();
out.nValue = 50 * CENT;
if (!out.scriptPubKey.IsSidechainProposalCommit()) {
return false;
}
uint256 hashBlock1 = GetRandHash();
scdbTest.Update(nHeight, hashBlock1, scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
if (vActivation.size() != 1) {
return false;
}
if (vActivation.front().proposal.GetHash() != proposal.GetHash())
{
return false;
}
// Use the function from validation to generate the commit, and then
// copy it from the block.
// TODO do this for all of the other unit tests that test commitments
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, proposal.GetHash(), Params().GetConsensus());
// Add votes until the sidechain is activated
int nHeightUpdate = nHeight + 1;
uint256 hashPrev = hashBlock1;
for (int i = 0; i <= SIDECHAIN_ACTIVATION_MAX_AGE; i++) {
uint256 hashNew = GetRandHash();
if (!scdbTest.Update(nHeightUpdate, hashNew, hashPrev, block.vtx.front()->vout)) {
return false;
}
hashPrev = hashNew;
nHeightUpdate++;
}
// Check activation status
// Sidechain should have been removed from activation cache
// Sidechain should be in ValidSidechains
vActivation = scdbTest.GetSidechainActivationStatus();
if (!vActivation.empty()) {
return false;
}
std::vector<Sidechain> vSidechain = scdbTest.GetActiveSidechains();
return(vSidechain.size() == nActive + 1 && vSidechain.back() == proposal);
}
bool ActivateSidechain(SidechainDB& scdbTest, int nHeight = 0)
{
// Create sidechain proposal
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "Test";
proposal.description = "Description";
proposal.sidechainKeyID = "80dca759b4ff2c9e9b65ec790703ad09fba844cd";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac";
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
return ActivateSidechain(scdbTest, proposal, nHeight);
}
BOOST_AUTO_TEST_CASE(activate_single_sidechain)
{
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
}
BOOST_AUTO_TEST_CASE(activate_multiple_sidechains)
{
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Proposal for a second sidechain
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "sidechain2";
proposal.description = "test";
proposal.sidechainKeyID = "c37afd89181060fa69deb3b26a0b95c02986ec78";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac"; // TODO
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r"; // TODO
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 2);
// Modify the proposal to create a third sidechain
proposal.title = "sidechain3";
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 3);
}
BOOST_AUTO_TEST_CASE(activate_max_sidechains)
{
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "sidechain";
proposal.description = "test";
proposal.sidechainKeyID = "c37afd89181060fa69deb3b26a0b95c02986ec78";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac"; // TODO
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r"; // TODO
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
unsigned int nSidechains = 0;
for (int i = 0; i < SIDECHAIN_ACTIVATION_MAX_ACTIVE; i++) {
proposal.title = "sidechain" + std::to_string(i);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
nSidechains++;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == nSidechains);
}
// Check that the maximum number have been activated
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
// Now try to activate one more than the max, it should be rejected.
proposal.title = "one too many...";
BOOST_CHECK(!ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wtprime)
{
// Test creating a WT^ and approving it with enough workscore
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
uint256 hashWTTest = GetRandHash();
SidechainWTPrimeState wtTest;
wtTest.hashWTPrime = hashWTTest;
wtTest.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wtTest.nSidechain = 0;
int nHeight = 0;
for (int i = 1; i <= SIDECHAIN_MIN_WORKSCORE; i++) {
wtTest.nWorkScore = i;
wtTest.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD - nHeight;
scdbTest.UpdateSCDBIndex(std::vector<SidechainWTPrimeState>{wtTest}, nHeight);
nHeight++;
}
// WT^ 0 should pass with valid workscore (100/100)
BOOST_CHECK(scdbTest.CheckWorkScore(0, hashWTTest));
}
BOOST_AUTO_TEST_CASE(sidechaindb_MultipleVerificationPeriods)
{
// Test multiple verification periods, approve multiple WT^s on the
// same sidechain
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// WT^ hash for first period
uint256 hashWTTest1 = GetRandHash();
// Verify first transaction, check work score
SidechainWTPrimeState wt1;
wt1.hashWTPrime = hashWTTest1;
wt1.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt1.nSidechain = 0;
int nHeight = 0;
for (int i = 1; i <= SIDECHAIN_MIN_WORKSCORE; i++) {
std::vector<SidechainWTPrimeState> vWT;
wt1.nWorkScore = i;
wt1.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD - nHeight;
vWT.push_back(wt1);
scdbTest.UpdateSCDBIndex(vWT, nHeight);
nHeight++;
}
BOOST_CHECK(scdbTest.CheckWorkScore(0, hashWTTest1));
// Create dummy coinbase tx
CMutableTransaction mtx;
mtx.nVersion = 1;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].scriptSig = CScript() << 486604799;
mtx.vout.push_back(CTxOut(50 * CENT, CScript() << OP_RETURN));
uint256 hashBlock = GetRandHash();
// Update scdbTest (will clear out old data from first period)
scdbTest.Update(SIDECHAIN_VERIFICATION_PERIOD, hashBlock, scdbTest.GetHashBlockLastSeen(), mtx.vout);
// WT^ hash for second period
uint256 hashWTTest2 = GetRandHash();
// Add new WT^
std::vector<SidechainWTPrimeState> vWT;
SidechainWTPrimeState wt2;
wt2.hashWTPrime = hashWTTest2;
wt2.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt2.nSidechain = 0;
wt2.nWorkScore = 1;
vWT.push_back(wt2);
scdbTest.UpdateSCDBIndex(vWT, 0);
BOOST_CHECK(!scdbTest.CheckWorkScore(0, hashWTTest2));
// Verify that scdbTest has updated to correct WT^
const std::vector<SidechainWTPrimeState> vState = scdbTest.GetState(0);
BOOST_CHECK(vState.size() == 1 && vState[0].hashWTPrime == hashWTTest2);
// Give second transaction sufficient workscore and check work score
nHeight = 0;
for (int i = 1; i <= SIDECHAIN_MIN_WORKSCORE; i++) {
std::vector<SidechainWTPrimeState> vWT;
wt2.nWorkScore = i;
wt2.nBlocksLeft--;
vWT.push_back(wt2);
scdbTest.UpdateSCDBIndex(vWT, nHeight);
nHeight++;
}
BOOST_CHECK(scdbTest.CheckWorkScore(0, hashWTTest2));
}
BOOST_AUTO_TEST_CASE(sidechaindb_MT_single)
{
// Merkle tree based scdbTest update test with only scdbTest data (no LD)
// in the tree, and a single WT^ to be updated.
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Create scdbTest with initial WT^
std::vector<SidechainWTPrimeState> vWT;
SidechainWTPrimeState wt;
wt.hashWTPrime = GetRandHash();
wt.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt.nWorkScore = 1;
wt.nSidechain = 0;
vWT.push_back(wt);
scdbTest.UpdateSCDBIndex(vWT, 0);
// Create a copy of the scdbTest to manipulate
SidechainDB scdbTestCopy = scdbTest;
// Update the scdbTest copy to get a new MT hash
vWT.clear();
wt.nWorkScore++;
wt.nBlocksLeft--;
vWT.push_back(wt);
scdbTestCopy.UpdateSCDBIndex(vWT, 0);
BOOST_CHECK(scdbTest.UpdateSCDBMatchMT(2, scdbTestCopy.GetSCDBHash()));
}
BOOST_AUTO_TEST_CASE(sidechaindb_MT_multipleSC)
{
// TODO fix, does not actually have multiple sidechains
// Merkle tree based scdbTest update test with multiple sidechains that each
// have one WT^ to update. Only one WT^ out of the three will be updated.
// This test ensures that nBlocksLeft is properly decremented even when a
// WT^'s score is unchanged.
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Add initial WT^s to scdbTest
SidechainWTPrimeState wtTest;
wtTest.hashWTPrime = GetRandHash();
wtTest.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wtTest.nSidechain = 0;
wtTest.nWorkScore = 1;
std::vector<SidechainWTPrimeState> vWT;
vWT.push_back(wtTest);
scdbTest.UpdateSCDBIndex(vWT, 0);
// Create a copy of the scdbTest to manipulate
SidechainDB scdbTestCopy = scdbTest;
// Update the scdbTest copy to get a new MT hash
wtTest.nBlocksLeft--;
wtTest.nWorkScore++;
vWT.clear();
vWT.push_back(wtTest);
scdbTestCopy.UpdateSCDBIndex(vWT, 1);
// Use MT hash prediction to update the original scdbTest
BOOST_CHECK(scdbTest.UpdateSCDBMatchMT(2, scdbTestCopy.GetSCDBHash()));
}
BOOST_AUTO_TEST_CASE(sidechaindb_MT_multipleWT)
{
// TODO fix, does not actually have multiple sidechains
// Merkle tree based scdbTest update test with multiple sidechains and multiple
// WT^(s) being updated. This tests that MT based scdbTest update will work if
// work scores are updated for more than one sidechain per block.
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Add initial WT^s to scdbTest
SidechainWTPrimeState wtTest;
wtTest.hashWTPrime = GetRandHash();
wtTest.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wtTest.nSidechain = 0;
wtTest.nWorkScore = 1;
std::vector<SidechainWTPrimeState> vWT;
vWT.push_back(wtTest);
scdbTest.UpdateSCDBIndex(vWT, 0);
// Create a copy of the scdbTest to manipulate
SidechainDB scdbTestCopy = scdbTest;
// Update the scdbTest copy to get a new MT hash
wtTest.nWorkScore++;
wtTest.nBlocksLeft--;
vWT.clear();
vWT.push_back(wtTest);
scdbTestCopy.UpdateSCDBIndex(vWT, 1);
// Use MT hash prediction to update the original scdbTest
BOOST_CHECK(scdbTest.UpdateSCDBMatchMT(2, scdbTestCopy.GetSCDBHash()));
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_create)
{
// Create a deposit (and CTIP) for a single sidechain
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// TODO use the wallet function
// Create deposit
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
CKey key;
CPubKey pubkey;
key.MakeNewKey(true);
pubkey = key.GetPubKey();
// User deposit data script
CScript dataScript = CScript() << OP_RETURN << ToByteVector(pubkey.GetID());
mtx.vout.push_back(CTxOut(CAmount(0), dataScript));
Sidechain sidechain;
BOOST_CHECK(scdbTest.GetSidechain(0, sidechain));
CScript sidechainScript;
BOOST_CHECK(scdbTest.GetSidechainScript(0, sidechainScript));
// Add deposit output
mtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx}, GetRandHash());
// Check if we cached it
std::vector<SidechainDeposit> vDeposit = scdbTest.GetDeposits(0);
BOOST_CHECK(vDeposit.size() == 1 && vDeposit.front().tx == mtx);
// Compare with scdbTest CTIP
SidechainCTIP ctip;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip));
BOOST_CHECK(ctip.out.hash == mtx.GetHash());
BOOST_CHECK(ctip.out.n == 1);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_multi_sidechain)
{
// Create a deposit (and CTIP) for multiple sidechains
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_multi_deposits)
{
// Create many deposits and make sure that single valid CTIP results
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// TODO use the wallet function
// Create deposit
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
CKey key;
CPubKey pubkey;
key.MakeNewKey(true);
pubkey = key.GetPubKey();
// User deposit data script
CScript dataScript = CScript() << OP_RETURN << ToByteVector(pubkey.GetID());
mtx.vout.push_back(CTxOut(CAmount(0), dataScript));
Sidechain sidechain;
BOOST_CHECK(scdbTest.GetSidechain(0, sidechain));
CScript sidechainScript;
BOOST_CHECK(scdbTest.GetSidechainScript(0, sidechainScript));
// Add deposit output
mtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx}, GetRandHash());
// Check if we cached it
std::vector<SidechainDeposit> vDeposit = scdbTest.GetDeposits(0);
BOOST_CHECK(vDeposit.size() == 1 && vDeposit.front().tx == mtx);
// Compare with scdbTest CTIP
SidechainCTIP ctip;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip));
BOOST_CHECK(ctip.out.hash == mtx.GetHash());
BOOST_CHECK(ctip.out.n == 1);
// Create another deposit
CMutableTransaction mtx2;
mtx2.vin.resize(1);
mtx2.vin[0].prevout.SetNull();
CKey key2;
CPubKey pubkey2;
key2.MakeNewKey(true);
pubkey2 = key2.GetPubKey();
// User deposit data script
CScript dataScript2 = CScript() << OP_RETURN << ToByteVector(pubkey2.GetID());
mtx2.vout.push_back(CTxOut(CAmount(0), dataScript2));
// Add deposit output
mtx2.vout.push_back(CTxOut(25 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx2}, GetRandHash());
// Check if we cached it
vDeposit.clear();
vDeposit = scdbTest.GetDeposits(0);
BOOST_CHECK(vDeposit.size() == 2 && vDeposit.back().tx == mtx2);
// Compare with scdbTest CTIP
SidechainCTIP ctip2;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip2));
BOOST_CHECK(ctip2.out.hash == mtx2.GetHash());
BOOST_CHECK(ctip2.out.n == 1);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_multi_deposits_multi_sidechain)
{
// Create many deposits and make sure that single valid CTIP results
// for multiple sidechains.
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_spend_wtprime)
{
// Create a deposit (and CTIP) for a single sidechain,
// and then spend it with a WT^
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// TODO use the wallet function
// Create deposit
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
CKey key;
CPubKey pubkey;
key.MakeNewKey(true);
pubkey = key.GetPubKey();
// User deposit data script
CScript dataScript = CScript() << OP_RETURN << ToByteVector(pubkey.GetID());
mtx.vout.push_back(CTxOut(CAmount(0), dataScript));
Sidechain sidechain;
BOOST_CHECK(scdbTest.GetSidechain(0, sidechain));
CScript sidechainScript;
BOOST_CHECK(scdbTest.GetSidechainScript(0, sidechainScript));
// Add deposit output
mtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx}, GetRandHash());
// Check if we cached it
std::vector<SidechainDeposit> vDeposit = scdbTest.GetDeposits(0);
BOOST_CHECK(vDeposit.size() == 1 && vDeposit.front().tx == mtx);
// Compare with scdbTest CTIP
SidechainCTIP ctip;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip));
BOOST_CHECK(ctip.out.hash == mtx.GetHash());
BOOST_CHECK(ctip.out.n == 1);
// Create a WT^ that spends the CTIP
CMutableTransaction wmtx;
wmtx.nVersion = 2;
wmtx.vin.push_back(CTxIn(ctip.out.hash, ctip.out.n));
wmtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
// Give it sufficient work score
SidechainWTPrimeState wt;
uint256 hashBlind;
BOOST_CHECK(CTransaction(wmtx).GetBWTHash(hashBlind));
wt.hashWTPrime = hashBlind;
wt.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt.nSidechain = 0;
int nHeight = 0;
for (int i = 1; i <= SIDECHAIN_MIN_WORKSCORE; i++) {
wt.nWorkScore = i;
wt.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD - nHeight;
scdbTest.UpdateSCDBIndex(std::vector<SidechainWTPrimeState>{wt}, nHeight);
nHeight++;
}
// WT^ 0 should pass with valid workscore (100/100)
BOOST_CHECK(scdbTest.CheckWorkScore(0, hashBlind));
// Spend the WT^
BOOST_CHECK(scdbTest.SpendWTPrime(0, GetRandHash(), wmtx));
// Check that the CTIP has been updated to the return amount from the WT^
SidechainCTIP ctipFinal;
BOOST_CHECK(scdbTest.GetCTIP(0, ctipFinal));
BOOST_CHECK(ctipFinal.out.hash == wmtx.GetHash());
BOOST_CHECK(ctipFinal.out.n == 0);
}
BOOST_AUTO_TEST_CASE(sidechaindb_wallet_ctip_spend_wtprime_then_deposit)
{
// Create a deposit (and CTIP) for a single sidechain, and then spend it
// with a WT^. After doing that, create another deposit.
SidechainDB scdbTest;
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// TODO use the wallet function
// Create deposit
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
CKey key;
CPubKey pubkey;
key.MakeNewKey(true);
pubkey = key.GetPubKey();
// User deposit data script
CScript dataScript = CScript() << OP_RETURN << ToByteVector(pubkey.GetID());
mtx.vout.push_back(CTxOut(CAmount(0), dataScript));
Sidechain sidechain;
BOOST_CHECK(scdbTest.GetSidechain(0, sidechain));
CScript sidechainScript;
BOOST_CHECK(scdbTest.GetSidechainScript(0, sidechainScript));
// Add deposit output
mtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx}, GetRandHash());
// Check if we cached it
std::vector<SidechainDeposit> vDeposit = scdbTest.GetDeposits(0);
BOOST_CHECK(vDeposit.size() == 1 && vDeposit.front().tx == mtx);
// Compare with scdbTest CTIP
SidechainCTIP ctip;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip));
BOOST_CHECK(ctip.out.hash == mtx.GetHash());
BOOST_CHECK(ctip.out.n == 1);
// Create a WT^ that spends the CTIP
CMutableTransaction wmtx;
wmtx.nVersion = 2;
wmtx.vin.push_back(CTxIn(ctip.out.hash, ctip.out.n));
wmtx.vout.push_back(CTxOut(50 * CENT, sidechainScript));
// Give it sufficient work score
SidechainWTPrimeState wt;
uint256 hashBlind;
BOOST_CHECK(CTransaction(wmtx).GetBWTHash(hashBlind));
wt.hashWTPrime = hashBlind;
wt.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt.nSidechain = 0;
int nHeight = 0;
for (int i = 1; i <= SIDECHAIN_MIN_WORKSCORE; i++) {
wt.nWorkScore = i;
wt.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD - nHeight;
scdbTest.UpdateSCDBIndex(std::vector<SidechainWTPrimeState>{wt}, nHeight);
nHeight++;
}
// WT^ 0 should pass with valid workscore (100/100)
BOOST_CHECK(scdbTest.CheckWorkScore(0, hashBlind));
// Spend the WT^
BOOST_CHECK(scdbTest.SpendWTPrime(0, GetRandHash(), wmtx));
// Check that the CTIP has been updated to the return amount from the WT^
SidechainCTIP ctipFinal;
BOOST_CHECK(scdbTest.GetCTIP(0, ctipFinal));
BOOST_CHECK(ctipFinal.out.hash == wmtx.GetHash());
BOOST_CHECK(ctipFinal.out.n == 0);
// Create another deposit
CMutableTransaction mtx2;
mtx2.vin.resize(1);
mtx2.vin[0].prevout.SetNull();
CKey key2;
CPubKey pubkey2;
key2.MakeNewKey(true);
pubkey2 = key2.GetPubKey();
// User deposit data script
CScript dataScript2 = CScript() << OP_RETURN << ToByteVector(pubkey2.GetID());
mtx2.vout.push_back(CTxOut(CAmount(0), dataScript2));
// Add deposit output
mtx2.vout.push_back(CTxOut(25 * CENT, sidechainScript));
scdbTest.AddDeposits(std::vector<CTransaction>{mtx2}, GetRandHash());
// Check if we cached it
vDeposit.clear();
vDeposit = scdbTest.GetDeposits(0);
// Should now have 3 deposits cached (first deposit, WT^, this deposit)
BOOST_CHECK(vDeposit.size() == 3 && vDeposit.back().tx == mtx2);
// Compare with scdbTest CTIP
SidechainCTIP ctip2;
BOOST_CHECK(scdbTest.GetCTIP(0, ctip2));
BOOST_CHECK(ctip2.out.hash == mtx2.GetHash());
BOOST_CHECK(ctip2.out.n == 1);
}
BOOST_AUTO_TEST_CASE(IsCriticalHashCommit)
{
// TODO
}
BOOST_AUTO_TEST_CASE(IsSCDBHashMerkleRootCommit)
{
// TODO
}
BOOST_AUTO_TEST_CASE(IsWTPrimeHashCommit)
{
// TODO test invalid
// Test WT^ hash commitments for nSidechain 0-255 with random WT^ hashes
for (unsigned int i = 0; i < 256; i++) {
uint256 hashWTPrime = GetRandHash();
uint8_t nSidechain = i;
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateWTPrimeHashCommitment(block, hashWTPrime, nSidechain, Params().GetConsensus());
uint256 hashWTPrimeFromCommit;
uint8_t nSidechainFromCommit;
BOOST_CHECK(block.vtx[0]->vout[0].scriptPubKey.IsWTPrimeHashCommit(hashWTPrimeFromCommit, nSidechainFromCommit));
BOOST_CHECK(hashWTPrime == hashWTPrimeFromCommit);
BOOST_CHECK(nSidechain == nSidechainFromCommit);
}
}
BOOST_AUTO_TEST_CASE(IsSidechainProposalCommit)
{
// TODO test more proposals with different data, versions etc
// TODO test invalid
// Create sidechain proposal
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "Test";
proposal.description = "Description";
proposal.sidechainKeyID = "80dca759b4ff2c9e9b65ec790703ad09fba844cd";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac";
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal.GetScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
}
BOOST_AUTO_TEST_CASE(IsSidechainActivationCommit)
{
// TODO test more proposals with different data, versions etc
// TODO test invalid
// Create sidechain proposal
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "Test";
proposal.description = "Description";
proposal.sidechainKeyID = "80dca759b4ff2c9e9b65ec790703ad09fba844cd";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac";
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Use the function from validation to generate the commit, and then
// copy it from the block.
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, proposal.GetHash(), Params().GetConsensus());
uint256 hashSidechain;
BOOST_CHECK(block.vtx[0]->vout[0].scriptPubKey.IsSidechainActivationCommit(hashSidechain));
BOOST_CHECK(hashSidechain == proposal.GetHash());
}
BOOST_AUTO_TEST_CASE(IsSidechainUpdateBytes)
{
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSCDBUpdateScript(block, std::vector<std::vector<SidechainWTPrimeState>>{}, std::vector<SidechainCustomVote>{}, Params().GetConsensus());
BOOST_CHECK(block.vtx[0]->vout[0].scriptPubKey.IsSCDBUpdate());
}
BOOST_AUTO_TEST_CASE(update_helper_basic)
{
// A test of the minimal functionality of generating and parsing an SCDB
// update script. Two sidechains with one WT^ each. Abstain WT^ of sidechain
// 0 and downvote WT^ of sidechain 1.
SidechainDB scdbTest;
// Activate first sidechain (default test sidechain)
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// A second sidechain proposal
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "sidechain2";
proposal.description = "test";
proposal.sidechainKeyID = "c37afd89181060fa69deb3b26a0b95c02986ec78";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac"; // TODO
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r"; // TODO
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Activate second sidechain
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 2);
// Add initial WT^s to scdbTest
SidechainWTPrimeState wt1;
wt1.hashWTPrime = GetRandHash();
wt1.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt1.nSidechain = 0; // For first sidechain
wt1.nWorkScore = 1;
SidechainWTPrimeState wt2;
wt2.hashWTPrime = GetRandHash();
wt2.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt2.nSidechain = 1; // For second sidechain
wt2.nWorkScore = 1;
std::vector<SidechainWTPrimeState> vWT;
vWT.push_back(wt1);
vWT.push_back(wt2);
BOOST_CHECK(scdbTest.UpdateSCDBIndex(vWT, 0));
BOOST_CHECK(scdbTest.GetState(0).size() == 1);
BOOST_CHECK(scdbTest.GetState(1).size() == 1);
// Create a copy of the scdbTest to manipulate
SidechainDB scdbTestCopy = scdbTest;
// Update the scdbTest copy to get a new MT hash
wt1.nBlocksLeft--;
// No change to WT^ 1 means it will have a default abstain vote
wt2.nBlocksLeft--;
wt2.nWorkScore--;
vWT.clear();
vWT.push_back(wt1);
vWT.push_back(wt2);
BOOST_CHECK(scdbTestCopy.UpdateSCDBIndex(vWT, 1));
// MT hash prediction should fail here without update script
BOOST_CHECK(!scdbTest.UpdateSCDBMatchMT(2, scdbTestCopy.GetSCDBHash()));
// Create custom vote for WT^ 2
SidechainCustomVote vote;
vote.nSidechain = 1;
vote.hashWTPrime = wt2.hashWTPrime;
vote.vote = SCDB_DOWNVOTE;
// Generate an update script
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
std::vector<std::vector<SidechainWTPrimeState>> vOldScores;
for (const Sidechain& s : scdbTest.GetActiveSidechains()) {
vOldScores.push_back(scdbTest.GetState(s.nSidechain));
}
GenerateSCDBUpdateScript(block, vOldScores, std::vector<SidechainCustomVote>{vote}, Params().GetConsensus());
const CScript script = block.vtx[0]->vout[0].scriptPubKey;
BOOST_CHECK(script.IsSCDBUpdate());
// Use ParseUpdateScript from validation to read it
std::vector<SidechainWTPrimeState> vNew;
std::vector<std::vector<SidechainWTPrimeState>> vOld;
for (const Sidechain& s : scdbTest.GetActiveSidechains()) {
vOld.push_back(scdbTest.GetState(s.nSidechain));
}
BOOST_CHECK(ParseSCDBUpdateScript(script, vOld, vNew));
BOOST_CHECK(scdbTest.UpdateSCDBMatchMT(2, scdbTestCopy.GetSCDBHash(), vNew));
}
BOOST_AUTO_TEST_CASE(update_helper_multi_custom)
{
// SCDB update script test with custom votes for more than one WT^ and
// three active sidechains but still only one WT^ per sidechain.
SidechainDB scdbTest;
// Activate first sidechain (default test sidechain)
BOOST_CHECK(ActivateSidechain(scdbTest));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// A second sidechain proposal
SidechainProposal proposal;
proposal.nVersion = 0;
proposal.title = "sidechain2";
proposal.description = "test 2";
proposal.sidechainKeyID = "c37afd89181060fa69deb3b26a0b95c02986ec78";
proposal.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac"; // TODO
proposal.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r"; // TODO
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Activate second sidechain
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 2);
// A third sidechain proposal
SidechainProposal proposal2;
proposal2.nVersion = 0;
proposal2.title = "sidechain3";
proposal2.description = "test 3";
proposal2.sidechainKeyID = "c37afd89181060fa69deb3b26a0b95c02986ec78";
proposal2.sidechainHex = "76a91480dca759b4ff2c9e9b65ec790703ad09fba844cd88ac"; // TODO
proposal2.sidechainPriv = "5Jf2vbdzdCccKApCrjmwL5EFc4f1cUm5Ah4L4LGimEuFyqYpa9r"; // TODO
proposal2.hashID1 = GetRandHash();
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Activate second sidechain
BOOST_CHECK(ActivateSidechain(scdbTest, proposal2, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 3);
// Add initial WT^s to scdbTest
SidechainWTPrimeState wt1;
wt1.hashWTPrime = GetRandHash();
wt1.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt1.nSidechain = 0; // For first sidechain
wt1.nWorkScore = 1;
SidechainWTPrimeState wt2;
wt2.hashWTPrime = GetRandHash();
wt2.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt2.nSidechain = 1; // For second sidechain
wt2.nWorkScore = 1;
SidechainWTPrimeState wt3;
wt3.hashWTPrime = GetRandHash();
wt3.nBlocksLeft = SIDECHAIN_VERIFICATION_PERIOD;
wt3.nSidechain = 2; // For third sidechain
wt3.nWorkScore = 1;
std::vector<SidechainWTPrimeState> vWT;
vWT.push_back(wt1);
vWT.push_back(wt2);
vWT.push_back(wt3);
BOOST_CHECK(scdbTest.UpdateSCDBIndex(vWT, 0));
BOOST_CHECK(scdbTest.GetState(0).size() == 1);
BOOST_CHECK(scdbTest.GetState(1).size() == 1);
BOOST_CHECK(scdbTest.GetState(2).size() == 1);
// Create a copy of the scdbTest to manipulate
SidechainDB scdbTestCopy = scdbTest;
// Update the scdbTest copy to get a new MT hash
wt1.nBlocksLeft--;
// No change to WT^ 1 means it will have a default abstain vote