forked from drivechain-project/mainchain-old
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sidechainactivation_tests.cpp
747 lines (583 loc) · 27.2 KB
/
sidechainactivation_tests.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
// Copyright (c) 2021-2022 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 <base58.h>
#include <chainparams.h>
#include <sidechain.h>
#include <sidechaindb.h>
#include <validation.h>
#include <test/test_skydoge.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(sidechainactivation_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(proposal_single)
{
// Test adding one proposal to scdbTest
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
// Updated scdbTest to add the proposal
uint256 hash = GetRandHash();
scdbTest.Update(0, hash, uint256(), std::vector<CTxOut>{out});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
// Verify scdbTest is tracking the proposal
BOOST_CHECK((vActivation.size() == 1) && (vActivation.front().proposal.GetProposalScript() == proposal.GetProposalScript()));
}
BOOST_AUTO_TEST_CASE(proposal_multiple)
{
// Test adding multiple proposal to scdbTest
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal1;
proposal1.nSidechain = 0;
proposal1.title = "test1";
proposal1.description = "description";
proposal1.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal1.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal1.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
// Update scdbTest to add the proposal
uint256 hash1 = GetRandHash();
scdbTest.Update(0, hash1, uint256(), std::vector<CTxOut>{out});
// Create sidechain proposal
Sidechain proposal2;
proposal2.nSidechain = 1;
proposal2.title = "test2";
proposal2.description = "description";
proposal2.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out2;
out2.scriptPubKey = proposal2.GetProposalScript();
out2.nValue = 50 * CENT;
BOOST_CHECK(out2.scriptPubKey.IsSidechainProposalCommit());
// Update scdbTest to add the proposal
scdbTest.Update(1, GetRandHash(), hash1, std::vector<CTxOut>{out2});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
// Verify scdbTest is tracking the proposals
BOOST_CHECK((vActivation.size() == 2) &&
(vActivation.front().proposal.GetProposalScript() == proposal1.GetProposalScript()) &&
(vActivation.back().proposal.GetProposalScript() == proposal2.GetProposalScript()));
}
BOOST_AUTO_TEST_CASE(proposal_perblock_limit)
{
// Make sure multiple sidechain proposals in one block will be rejected.
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal1;
proposal1.nSidechain = 0;
proposal1.title = "test1";
proposal1.description = "description";
proposal1.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal1.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal1.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
// Create sidechain proposal
Sidechain proposal2;
proposal2.nSidechain = 1;
proposal2.title = "test2";
proposal2.description = "description";
proposal2.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out2;
out2.scriptPubKey = proposal2.GetProposalScript();
out2.nValue = 50 * CENT;
BOOST_CHECK(out2.scriptPubKey.IsSidechainProposalCommit());
// Update scdbTest to add the proposal
scdbTest.Update(0, GetRandHash(), uint256(), std::vector<CTxOut>{out, out2});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
// Nothing should have been added
BOOST_CHECK(vActivation.empty());
}
BOOST_AUTO_TEST_CASE(activate_single)
{
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.nVersion = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
}
BOOST_AUTO_TEST_CASE(activate_multiple)
{
SidechainDB scdbTest;
Sidechain proposal1;
proposal1.nSidechain = 0;
proposal1.nVersion = 0;
proposal1.title = "sidechain1";
proposal1.description = "description";
proposal1.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal1.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal1, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Proposal for a second sidechain
Sidechain proposal2;
proposal2.nSidechain = 1;
proposal2.nVersion = 0;
proposal2.title = "sidechain2";
proposal2.description = "test";
proposal2.hashID1 = GetRandHash();
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(ActivateSidechain(scdbTest, proposal2, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 2);
// Copy sidechain 2
Sidechain proposal3 = proposal2;
proposal3.nSidechain = 2;
proposal3.title = "abc";
BOOST_CHECK(ActivateSidechain(scdbTest, proposal3, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 3);
}
BOOST_AUTO_TEST_CASE(activate_max)
{
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
Sidechain proposal;
proposal.nVersion = 0;
proposal.title = "sidechain";
proposal.description = "test";
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
unsigned int nSidechains = 0;
for (int i = 0; i < SIDECHAIN_ACTIVATION_MAX_ACTIVE; i++) {
proposal.nSidechain = 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);
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain.size() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
// Check sidechain numbers and active status
for (size_t i = 0; i < vSidechain.size(); i++) {
BOOST_CHECK(vSidechain[i].fActive == true);
BOOST_CHECK(vSidechain[i].nSidechain == i);
}
}
BOOST_AUTO_TEST_CASE(activate_fail)
{
// Test adding one proposal to scdbTest and fail to activate it
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.nVersion = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
uint256 hash1 = GetRandHash();
scdbTest.Update(0, hash1, uint256(), std::vector<CTxOut>{out});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK((vActivation.size() == 1) && (vActivation.front().proposal.GetProposalScript() == proposal.GetProposalScript()));
// 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.GetSerHash());
// Add votes until the sidechain is half way activated
for (int i = 1; i <= SIDECHAIN_ACTIVATION_PERIOD / 2; i++) {
uint256 hash2 = GetRandHash();
scdbTest.Update(i, hash2, hash1, block.vtx.front()->vout);
hash1 = hash2;
}
// Check activation status
// Sidechain should still be in activation cache
// Sidechain should not be in ValidSidechains
std::vector<Sidechain> vSidechain = scdbTest.GetActiveSidechains();
BOOST_CHECK(vSidechain.empty());
}
BOOST_AUTO_TEST_CASE(activate_remove_failed)
{
// Test that sidechains which have no chance of success (based on their
// rejection count) are pruned from the activation cache.
SidechainDB scdbTest;
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.nVersion = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
uint256 hash1 = GetRandHash();
scdbTest.Update(0, hash1, uint256(), std::vector<CTxOut>{out});
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK((vActivation.size() == 1) && (vActivation.front().proposal.GetProposalScript() == proposal.GetProposalScript()));
// Pass coinbase without sidechain activation commit into scdbTest enough times
// that the proposal is rejected and pruned.
out.scriptPubKey = OP_TRUE;
out.nValue = 50 * CENT;
for (int i = 1; i <= SIDECHAIN_ACTIVATION_MAX_FAILURES + 1; i++) {
vActivation = scdbTest.GetSidechainActivationStatus();
uint256 hash2 = GetRandHash();
scdbTest.Update(i, hash2, hash1, std::vector<CTxOut>{out});
hash1 = hash2;
}
// Check activation status
// Sidechain should have been pruned from activation cache
// Sidechain should not be in ValidSidechains
std::vector<SidechainActivationStatus> vActivationFinal;
vActivationFinal = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivationFinal.empty());
std::vector<Sidechain> vSidechain = scdbTest.GetActiveSidechains();
BOOST_CHECK(vSidechain.empty());
}
BOOST_AUTO_TEST_CASE(none_active)
{
// Test that when no sidechains have been activated, the sidechain list
// lists all of them with inactive status and the correct sidechain number.
SidechainDB scdbTest;
// No sidechains should be active
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
std::vector<Sidechain> vActive = scdbTest.GetActiveSidechains();
BOOST_CHECK(vActive.empty());
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain.size() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
// Check sidechain numbers and active status
for (size_t i = 0; i < vSidechain.size(); i++) {
BOOST_CHECK(vSidechain[i].fActive == false);
BOOST_CHECK(vSidechain[i].nSidechain == i);
}
}
BOOST_AUTO_TEST_CASE(max_active_reverse)
{
// Test activating the maximum number of sidechains but in reverse order
// from sidechain #255 to #0.
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
Sidechain proposal;
proposal.nVersion = 0;
proposal.description = "test";
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
unsigned int nSidechains = 0;
for (int i = SIDECHAIN_ACTIVATION_MAX_ACTIVE - 1; i >= 0; i--) {
proposal.nSidechain = 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);
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain.size() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
// Check sidechain numbers and active status
for (size_t i = 0; i < vSidechain.size(); i++) {
BOOST_CHECK(vSidechain[i].fActive == true);
BOOST_CHECK(vSidechain[i].nSidechain == i);
}
}
BOOST_AUTO_TEST_CASE(every_other_active)
{
// Test activating half of the maximum number of sidechains skipping one
// sidechain slot between each activated sidechain.
SidechainDB scdbTest;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
// TODO sidechains with the same key and IDs should be rejected
Sidechain proposal;
proposal.nVersion = 0;
proposal.description = "test";
proposal.hashID1 = GetRandHash();
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
unsigned int nSidechains = 0;
for (int i = 0; i < SIDECHAIN_ACTIVATION_MAX_ACTIVE; i++) {
if (i % 2 == 0)
continue;
proposal.nSidechain = i;
proposal.title = "sidechain" + std::to_string(i);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
nSidechains++;
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == nSidechains);
}
// Check that half of the maximum number have been activated
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == SIDECHAIN_ACTIVATION_MAX_ACTIVE / 2);
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain.size() == SIDECHAIN_ACTIVATION_MAX_ACTIVE);
// Check sidechain numbers and active status
for (size_t i = 0; i < vSidechain.size(); i++) {
if (i % 2 == 0)
BOOST_CHECK(vSidechain[i].fActive == false);
else
BOOST_CHECK(vSidechain[i].fActive == true);
BOOST_CHECK(vSidechain[i].nSidechain == i);
}
}
BOOST_AUTO_TEST_CASE(replace_sidechain)
{
SidechainDB scdbTest;
// Activate first sidechain
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.nVersion = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Create replacement sidechain proposal
// Create sidechain proposal
Sidechain proposal2;
proposal2.nSidechain = 0;
proposal2.nVersion = 0;
proposal2.title = "replacement";
proposal2.description = "description";
proposal2.hashID1 = uint256S("ff5d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal2.hashID2 = uint160S("ffd98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal2.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
uint256 hash1 = GetRandHash();
BOOST_CHECK(scdbTest.Update(0, hash1, scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out}));
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK((vActivation.size() == 1) && (vActivation.front().proposal.GetProposalScript() == proposal2.GetProposalScript()));
// Generate a sidechain activation commit for the replacement
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, vActivation.front().proposal.GetSerHash());
// Add the requirement to replace
for (int i = 1; i <= SIDECHAIN_REPLACEMENT_PERIOD - 1; i++)
BOOST_CHECK(scdbTest.Update(i, GetRandHash(), scdbTest.GetHashBlockLastSeen(), block.vtx.front()->vout));
// Check that the proposal has half of the replacement requirement
vActivation = scdbTest.GetSidechainActivationStatus();
// Check activation status
// replacement should have been pruned from activation cache
std::vector<SidechainActivationStatus> vActivationFinal;
vActivationFinal = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivationFinal.empty());
// Sidechain 0 should now be "replacement"
// Check that "replacement" sidechain was activated
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain[0].title == "replacement");
}
BOOST_AUTO_TEST_CASE(replace_sidechain_fail)
{
SidechainDB scdbTest;
// Activate first sidechain
// Create sidechain proposal
Sidechain proposal;
proposal.nSidechain = 0;
proposal.nVersion = 0;
proposal.title = "test";
proposal.description = "description";
proposal.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 0);
BOOST_CHECK(ActivateSidechain(scdbTest, proposal, 0));
BOOST_CHECK(scdbTest.GetActiveSidechainCount() == 1);
// Create replacement sidechain proposal
// Create sidechain proposal
Sidechain proposal2;
proposal2.nSidechain = 0;
proposal2.nVersion = 0;
proposal2.title = "replacement";
proposal2.description = "description";
proposal2.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction output with sidechain proposal
CTxOut out;
out.scriptPubKey = proposal2.GetProposalScript();
out.nValue = 50 * CENT;
BOOST_CHECK(out.scriptPubKey.IsSidechainProposalCommit());
BOOST_CHECK(scdbTest.Update(0, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out}));
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK((vActivation.size() == 1) && (vActivation.front().proposal.GetProposalScript() == proposal2.GetProposalScript()));
// Generate a sidechain activation commit for the replacement
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, vActivation.front().proposal.GetSerHash());
// Add half of the requirement to replace
for (int i = 1; i <= SIDECHAIN_REPLACEMENT_PERIOD / 2; i++)
BOOST_CHECK(scdbTest.Update(i, GetRandHash(), scdbTest.GetHashBlockLastSeen(), block.vtx.front()->vout));
// Generate blocks without activation commitments to make proposal fail
out.scriptPubKey = OP_TRUE;
out.nValue = 50 * CENT;
for (int i = 1; i <= SIDECHAIN_ACTIVATION_MAX_FAILURES + 1; i++)
BOOST_CHECK(scdbTest.Update(i, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out}));
// Check activation status
// replacement should have been pruned from activation cache
// replacement should not be active
std::vector<SidechainActivationStatus> vActivationFinal;
vActivationFinal = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivationFinal.empty());
// Check that "replacement" sidechain was not activated
// Sidechain 0 should still be "test"
std::vector<Sidechain> vSidechain = scdbTest.GetSidechains();
BOOST_CHECK(vSidechain[0].title == "test");
}
BOOST_AUTO_TEST_CASE(per_block_activation_limit_pass)
{
// Test that only one sidechain activation commit is allowed for each
// sidechain number per block. In this test we will ACK two sidechains that
// have different sidechain numbers, which should be allowed.
SidechainDB scdbTest;
Sidechain proposal1;
proposal1.nSidechain = 0;
proposal1.nVersion = 0;
proposal1.title = "sidechain1";
proposal1.description = "description";
proposal1.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal1.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Proposal for a second sidechain
Sidechain proposal2;
proposal2.nSidechain = 1;
proposal2.nVersion = 0;
proposal2.title = "sidechain2";
proposal2.description = "test";
proposal2.hashID1 = GetRandHash();
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction outputs with sidechain proposals
CTxOut out1;
out1.scriptPubKey = proposal1.GetProposalScript();
out1.nValue = 50 * CENT;
BOOST_CHECK(out1.scriptPubKey.IsSidechainProposalCommit());
CTxOut out2;
out2.scriptPubKey = proposal2.GetProposalScript();
out2.nValue = 50 * CENT;
BOOST_CHECK(out2.scriptPubKey.IsSidechainProposalCommit());
// Add both proposals to blocks and get them into SCDB
BOOST_CHECK(scdbTest.Update(0, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out1}));
BOOST_CHECK(scdbTest.Update(1, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out2}));
// Check that the proposals were added
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivation.size() == 2);
BOOST_CHECK(vActivation.size() && vActivation.front().proposal.GetProposalScript() == proposal1.GetProposalScript());
BOOST_CHECK(vActivation.size() && vActivation.back().proposal.GetProposalScript() == proposal2.GetProposalScript());
// Start ACKing the proposals
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, vActivation.front().proposal.GetSerHash());
GenerateSidechainActivationCommitment(block, vActivation.back().proposal.GetSerHash());
// Add votes until the sidechains are activated
int nHeight = 2;
for (int i = 0; i < SIDECHAIN_ACTIVATION_PERIOD - 1; i++) {
if (i == SIDECHAIN_ACTIVATION_PERIOD - 2) {
// For the last block we only want to vote on the second proposal
// as the first has already activated in the previous block
CMutableTransaction mtxFinal = CMutableTransaction(*block.vtx[0]);
mtxFinal.vout.clear();
block.vtx[0] = MakeTransactionRef(std::move(mtx));
GenerateSidechainActivationCommitment(block, vActivation.back().proposal.GetSerHash());
}
BOOST_CHECK(scdbTest.Update(nHeight, GetRandHash(), scdbTest.GetHashBlockLastSeen(), block.vtx.front()->vout));
nHeight++;
}
// Proposals should be removed now
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivation.empty());
// Both sidechains should be active now
std::vector<Sidechain> vSidechain = scdbTest.GetActiveSidechains();
BOOST_CHECK(vSidechain.size() == 2);
}
BOOST_AUTO_TEST_CASE(per_block_activation_limit_fail)
{
// Test that only one sidechain activation commit is allowed for each
// sidechain number per block. In this test we will ACK two sidechains that
// have the same sidechain numbers, which should be rejected.
SidechainDB scdbTest;
Sidechain proposal1;
proposal1.nSidechain = 0;
proposal1.nVersion = 0;
proposal1.title = "sidechain1";
proposal1.description = "description";
proposal1.hashID1 = uint256S("b55d224f1fda033d930c92b1b40871f209387355557dd5e0d2b5dd9bb813c33f");
proposal1.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Proposal for a second sidechain
Sidechain proposal2;
proposal2.nSidechain = 0;
proposal2.nVersion = 0;
proposal2.title = "sidechain2";
proposal2.description = "test";
proposal2.hashID1 = GetRandHash();
proposal2.hashID2 = uint160S("31d98584f3c570961359c308619f5cf2e9178482");
// Create transaction outputs with sidechain proposals
CTxOut out1;
out1.scriptPubKey = proposal1.GetProposalScript();
out1.nValue = 50 * CENT;
BOOST_CHECK(out1.scriptPubKey.IsSidechainProposalCommit());
CTxOut out2;
out2.scriptPubKey = proposal2.GetProposalScript();
out2.nValue = 50 * CENT;
BOOST_CHECK(out2.scriptPubKey.IsSidechainProposalCommit());
// Add both proposals to blocks and get them into SCDB
BOOST_CHECK(scdbTest.Update(0, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out1}));
BOOST_CHECK(scdbTest.Update(1, GetRandHash(), scdbTest.GetHashBlockLastSeen(), std::vector<CTxOut>{out2}));
// Check that the proposals were added
std::vector<SidechainActivationStatus> vActivation;
vActivation = scdbTest.GetSidechainActivationStatus();
BOOST_CHECK(vActivation.size() == 2);
BOOST_CHECK(vActivation.size() && vActivation.front().proposal.GetProposalScript() == proposal1.GetProposalScript());
BOOST_CHECK(vActivation.size() && vActivation.back().proposal.GetProposalScript() == proposal2.GetProposalScript());
// Start ACKing the proposals
CBlock block;
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vin[0].prevout.SetNull();
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
GenerateSidechainActivationCommitment(block, proposal1.GetSerHash());
GenerateSidechainActivationCommitment(block, proposal2.GetSerHash());
// Acking two sidechains in one block with the same sidechain number should
// fail
BOOST_CHECK(!scdbTest.Update(2, GetRandHash(), scdbTest.GetHashBlockLastSeen(), block.vtx.front()->vout));
}
BOOST_AUTO_TEST_SUITE_END()