This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
forked from CryptAxe/DriveChainIntegration
-
Notifications
You must be signed in to change notification settings - Fork 2
/
drivechainintegration.sh
executable file
·1157 lines (939 loc) · 31.9 KB
/
drivechainintegration.sh
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
#!/bin/bash
# Drivechain integration testing
# This script will download and build the mainchain, testchain, trainchain and
# thunder sidechains and then run a series of tests.
VERSION=2
REINDEX=0
BMM_BID=0.0001
MIN_WORK_SCORE=131
SIDECHAIN_ACTIVATION_SCORE=20
# Read arguments
SKIP_CLONE=0 # Skip cloning the repositories from github
SKIP_BUILD=0 # Skip pulling and building repositories
SKIP_CHECK=0 # Skip make check on repositories
SKIP_REPLACE_TIP=0 # Skip tests where we replace the chainActive.Tip()
SKIP_RESTART=0 # Skip tests where we restart and verify state after restart
SKIP_SHUTDOWN=0 # Don't shutdown the main & side clients when finished testing
INCOMPATIBLE_BDB=0 # Compile --with-incompatible-bdb
for arg in "$@"
do
if [ "$arg" == "--help" ]; then
echo "The following command line options are available:"
echo "--skip_clone"
echo "--skip_build"
echo "--skip_check"
echo "--skip_replace_tip"
echo "--skip_restart"
echo "--skip_shutdown"
echo "--with-incompatible-bdb"
exit
elif [ "$arg" == "--skip_clone" ]; then
SKIP_CLONE=1
elif [ "$arg" == "--skip_build" ]; then
SKIP_BUILD=1
elif [ "$arg" == "--skip_check" ]; then
SKIP_CHECK=1
elif [ "$arg" == "--skip_replace_tip" ]; then
SKIP_REPLACE_TIP=1
elif [ "$arg" == "--skip_restart" ]; then
SKIP_RESTART=1
elif [ "$arg" == "--skip_shutdown" ]; then
SKIP_SHUTDOWN=1
elif [ "$arg" == "--with-incompatible-bdb" ]; then
INCOMPATIBLE_BDB=1
fi
done
clear
echo -e "\e[36m██████╗ ██████╗ ██╗██╗ ██╗███████╗███╗ ██╗███████╗████████╗\e[0m"
echo -e "\e[36m██╔══██╗██╔══██╗██║██║ ██║██╔════╝████╗ ██║██╔════╝╚══██╔══╝\e[0m"
echo -e "\e[36m██║ ██║██████╔╝██║██║ ██║█████╗ ██╔██╗ ██║█████╗ ██║\e[0m"
echo -e "\e[36m██║ ██║██╔══██╗██║╚██╗ ██╔╝██╔══╝ ██║╚██╗██║██╔══╝ ██║\e[0m"
echo -e "\e[36m██████╔╝██║ ██║██║ ╚████╔╝ ███████╗██║ ╚████║███████╗ ██║\e[0m"
echo -e "\e[36m╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═╝\e[0m"
echo -e "\e[1mAutomated integration testing script (v$VERSION)\e[0m"
echo
echo "This script will clone, build, configure & run drivechain and sidechains"
echo "The functional unit tests will be run for drivechain and sidechains."
echo "If those tests pass, the integration test script will try to go through"
echo "the process of BMM mining, deposit to and withdraw from the sidechains."
echo
echo "We will also restart the software many times to check for issues with"
echo "shutdown and initialization."
echo
echo -e "\e[1mREAD: YOUR DATA DIRECTORIES WILL BE DELETED\e[0m"
echo
echo "Your data directories ex: ~/.drivechain & ~/.testchain and any other"
echo "sidechain data directories will be deleted!"
echo
echo -e "\e[31mWARNING: THIS WILL DELETE YOUR DRIVECHAIN & SIDECHAIN DATA!\e[0m"
echo
echo -e "\e[32mYou should probably run this in a VM\e[0m"
echo
read -p "Are you sure you want to run this? (yes/no): " WARNING_ANSWER
if [ "$WARNING_ANSWER" != "yes" ]; then
exit
fi
#
# Functions to help the script
#
function startdrivechain {
if [ $REINDEX -eq 1 ]; then
echo
echo "drivechain will be reindexed"
echo
./mainchain/src/qt/drivechain-qt \
--reindex \
--regtest &
else
./mainchain/src/qt/drivechain-qt \
--regtest &
fi
sleep 15s
}
function starttestchain {
./testchain/src/qt/testchain-qt \
--regtest \
--verifybmmacceptheader \
--verifybmmacceptblock \
--verifybmmreadblock \
--verifybmmcheckblock \
--verifywithdrawalbundleacceptblock \
--minwithdrawal=1 &
sleep 15s
}
function starttrainchain {
./trainchain/src/qt/trainchain-qt \
--regtest \
--verifybmmacceptheader \
--verifybmmacceptblock \
--verifybmmreadblock \
--verifybmmcheckblock \
--verifywithdrawalbundleacceptblock \
--minwithdrawal=1 &
sleep 8s
}
function startthunder {
./thunder/src/qt/thunder-qt \
--regtest \
--verifybmmacceptheader \
--verifybmmacceptblock \
--verifybmmreadblock \
--verifybmmcheckblock \
--verifywithdrawalbundleacceptblock \
--minwithdrawal=1 &
sleep 8s
}
function restartdrivechain {
if [ $SKIP_RESTART -eq 1 ]; then
return 0
fi
#
# Shutdown drivechain mainchain, restart it, and make sure nothing broke.
# Exits the script if anything did break.
#
# TODO check return value of python json parsing and exit if it failed
# TODO use jq instead of python
echo
echo "We will now restart drivechain & verify its state after restarting!"
# Record the state before restart
HASHSCDB=`./mainchain/src/drivechain-cli --regtest gettotalscdbhash`
HASHSCDB=`echo $HASHSCDB | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["hashscdbtotal"]'`
# Count doesn't return a json array like the above commands - so no parsing
COUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
# getbestblockhash also doesn't return an array
BESTBLOCK=`./mainchain/src/drivechain-cli --regtest getbestblockhash`
# Restart
./mainchain/src/drivechain-cli --regtest stop
sleep 20s # Wait a little bit incase shutdown takes a while
echo "Waiting for drivechain to start"
startdrivechain
# Verify the state after restart
HASHSCDBRESTART=`./mainchain/src/drivechain-cli --regtest gettotalscdbhash`
HASHSCDBRESTART=`echo $HASHSCDBRESTART | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["hashscdbtotal"]'`
COUNTRESTART=`./mainchain/src/drivechain-cli --regtest getblockcount`
BESTBLOCKRESTART=`./mainchain/src/drivechain-cli --regtest getbestblockhash`
if [ "$COUNT" != "$COUNTRESTART" ]; then
echo "Error after restarting drivechain!"
echo "COUNT != COUNTRESTART"
echo "$COUNT != $COUNTRESTART"
exit
fi
if [ "$BESTBLOCK" != "$BESTBLOCKRESTART" ]; then
echo "Error after restarting drivechain!"
echo "BESTBLOCK != BESTBLOCKRESTART"
echo "$BESTBLOCK != $BESTBLOCKRESTART"
exit
fi
if [ "$HASHSCDB" != "$HASHSCDBRESTART" ]; then
echo "Error after restarting drivechain!"
echo "HASHSCDB != HASHSCDBRESTART"
echo "$HASHSCDB != $HASHSCDBRESTART"
exit
fi
echo
echo "drivechain restart and state check check successful!"
sleep 3s
}
function replacetip {
if [ $SKIP_REPLACE_TIP -eq 1 ]; then
return 0
fi
# Disconnect chainActive.Tip() and replace it with a new tip
echo
echo "We will now disconnect the chain tip and replace it with a new one!"
sleep 3s
OLDCOUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
OLDTIP=`./mainchain/src/drivechain-cli --regtest getbestblockhash`
./mainchain/src/drivechain-cli --regtest invalidateblock $OLDTIP
sleep 3s # Give some time for the block to be invalidated
DISCONNECTCOUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
if [ "$DISCONNECTCOUNT" == "$OLDCOUNT" ]; then
echo "Failed to disconnect tip!"
exit
fi
./mainchain/src/drivechain-cli --regtest generate 1
NEWTIP=`./mainchain/src/drivechain-cli --regtest getbestblockhash`
NEWCOUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
if [ "$OLDTIP" == "$NEWTIP" ] || [ "$OLDCOUNT" != "$NEWCOUNT" ]; then
echo "Failed to replace tip!"
exit
else
echo "Tip replaced!"
echo "Old tip: $OLDTIP"
echo "New tip: $NEWTIP"
fi
}
function minemainchain {
for ((x = 0; x < $1; x++)); do
OLDCOUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
./mainchain/src/drivechain-cli --regtest generate 1
NEWCOUNT=`./mainchain/src/drivechain-cli --regtest getblockcount`
if [ "$OLDCOUNT" -eq "$NEWCOUNT" ]; then
echo
echo "Failed to mine mainchain block!"
exit
fi
done
}
function bmm {
sleep 0.5s
OLD_TESTCHAIN=0
OLD_TRAINCHAIN=0
OLD_THUNDER=0
NEW_TESTCHAIN=0
NEW_TRAINCHAIN=0
NEW_THUNDER=0
# Call refreshbmm RPC on any sidechains we want to BMM
# Make new bmm request if required and connect new bmm blocks if found
for arg in "$@"
do
if [ "$arg" == "testchain" ]; then
OLD_TESTCHAIN=`./testchain/src/testchain-cli --regtest getblockcount`
./testchain/src/testchain-cli --regtest refreshbmm $BMM_BID
elif [ "$arg" == "trainchain" ]; then
OLD_TRAINCHAIN=`./trainchain/src/trainchain-cli --regtest getblockcount`
./trainchain/src/trainchain-cli --regtest refreshbmm $BMM_BID
elif [ "$arg" == "thunder" ]; then
OLD_THUNDER=`./thunder/src/thunder-cli --regtest getblockcount`
./thunder/src/thunder-cli --regtest refreshbmm $BMM_BID
sleep 1s
fi
done
# Up to 3 tries to BMM a block for every sidechain requested
for ((y = 0; y < 3; y++)); do
# Mine a mainchain block to include BMM requests
sleep 1s
minemainchain 1
sleep 1s
# Refresh BMM again for selected sidechains and check if block connected
for arg in "$@"
do
if [ "$arg" == "testchain" ]; then
./testchain/src/testchain-cli --regtest refreshbmm $BMM_BID false
NEW_TESTCHAIN=`./testchain/src/testchain-cli --regtest getblockcount`
elif [ "$arg" == "trainchain" ]; then
./trainchain/src/trainchain-cli --regtest refreshbmm $BMM_BID false
NEW_TRAINCHAIN=`./trainchain/src/trainchain-cli --regtest getblockcount`
elif [ "$arg" == "thunder" ]; then
./thunder/src/thunder-cli --regtest refreshbmm $BMM_BID false
NEW_THUNDER=`./thunder/src/thunder-cli --regtest getblockcount`
fi
done
# Check completion
if [ "$OLD_TESTCHAIN" -ne "$NEW_TESTCHAIN" ] && \
[ "$OLD_TRAINCHAIN" -ne "$NEW_TRAINCHAIN" ] && \
[ "$OLD_THUNDER" -ne "$NEW_THUNDER" ]; then
break
fi
done
}
function buildchain {
git pull
./autogen.sh
if [ $INCOMPATIBLE_BDB -ne 1 ]; then
./configure
else
./configure --with-incompatible-bdb
fi
if [ $? -ne 0 ]; then
echo "Configure failed!"
exit
fi
make -j "$(nproc)"
if [ $? -ne 0 ]; then
echo "Make failed!"
exit
fi
if [ $SKIP_CHECK -ne 1 ]; then
make check
if [ $? -ne 0 ]; then
echo "Make check failed!"
exit
fi
fi
}
#
# The testing starts here
#
#
# Remove old data directories
#
rm -rf ~/.drivechain
rm -rf ~/.testchain
rm -rf ~/.trainchain
rm -rf ~/.thunder
#
# Clone repositories & copy sidechains
#
if [ $SKIP_CLONE -ne 1 ]; then
echo
echo "Cloning repositories"
git clone https://github.com/drivechain-project/mainchain
git clone https://github.com/drivechain-project/sidechains testchain
if [ ! -d thunder/ ]; then
cp -rf testchain thunder
fi
if [ ! -d trainchain/ ]; then
cp -rf testchain trainchain
fi
fi
#
# Build repositories & run their unit tests
#
if [ $SKIP_BUILD -ne 1 ]; then
echo
echo "Building repositories"
cd mainchain
git checkout master
buildchain
cd ..
cd testchain
git checkout testchain
buildchain
cd ..
cd trainchain
git checkout trainchain
buildchain
cd ..
cd thunder
git checkout thunder
buildchain
cd ..
fi
#
# Create configuration files
#
echo
echo "Create drivechain configuration file"
mkdir ~/.drivechain/
touch ~/.drivechain/drivechain.conf
echo "rpcuser=drivechain" > ~/.drivechain/drivechain.conf
echo "rpcpassword=integrationtesting" >> ~/.drivechain/drivechain.conf
echo "server=1" >> ~/.drivechain/drivechain.conf
echo
echo "Creating testchain configuration file"
mkdir ~/.testchain/
touch ~/.testchain/testchain.conf
echo "rpcuser=drivechain" > ~/.testchain/testchain.conf
echo "rpcpassword=integrationtesting" >> ~/.testchain/testchain.conf
echo "server=1" >> ~/.testchain/testchain.conf
echo
echo "Creating trainchain configuration file"
mkdir ~/.trainchain/
touch ~/.trainchain/trainchain.conf
echo "rpcuser=drivechain" > ~/.trainchain/trainchain.conf
echo "rpcpassword=integrationtesting" >> ~/.trainchain/trainchain.conf
echo "server=1" >> ~/.trainchain/trainchain.conf
echo
echo "Creating thunder configuration file"
mkdir ~/.thunder/
touch ~/.thunder/thunder.conf
echo "rpcuser=drivechain" > ~/.thunder/thunder.conf
echo "rpcpassword=integrationtesting" >> ~/.thunder/thunder.conf
echo "server=1" >> ~/.thunder/thunder.conf
#
# Get mainchain running and mine first 100 mainchain blocks
#
# Start drivechain-qt
echo
echo "Waiting for mainchain to start"
startdrivechain
echo
echo "Checking if the mainchain has started"
# Test that mainchain can receive commands and has 0 blocks
GETINFO=`./mainchain/src/drivechain-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 0"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Drivechain up and running!"
else
echo
echo "ERROR failed to send commands to Drivechain or block count non-zero"
exit
fi
echo
echo "Drivechain will now generate first 100 blocks"
sleep 3s
# Generate 100 mainchain blocks
minemainchain 100
# Check that 100 blocks were mined
GETINFO=`./mainchain/src/drivechain-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 100"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Drivechain has mined first 100 blocks"
else
echo
echo "ERROR failed to mine first 100 blocks!"
exit
fi
# Disconnect chain tip, replace with a new one
replacetip
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
#
# Activate testchain
#
# Create a sidechain proposal
./mainchain/src/drivechain-cli --regtest createsidechainproposal 0 "testchain" "testchain for integration test"
# Check that proposal was cached (not in chain yet)
LISTPROPOSALS=`./mainchain/src/drivechain-cli --regtest listsidechainproposals`
COUNT=`echo $LISTPROPOSALS | grep -c "\"title\": \"testchain\""`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain proposal for sidechain testchain has been created!"
else
echo
echo "ERROR failed to create testchain sidechain proposal!"
exit
fi
echo
echo "Will now mine a block so that sidechain proposal is added to the chain"
# Mine one mainchain block, proposal should be in chain after that
minemainchain 1
# Check that we have 101 blocks now
GETINFO=`./mainchain/src/drivechain-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 101"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "mainchain has 101 blocks now"
else
echo
echo "ERROR failed to mine block including testchain proposal!"
exit
fi
# Disconnect chain tip, replace with a new one
replacetip
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
# Check that proposal has been added to the chain and ready for voting
LISTACTIVATION=`./mainchain/src/drivechain-cli --regtest listsidechainactivationstatus`
COUNT=`echo $LISTACTIVATION | grep -c "\"title\": \"testchain\""`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain proposal made it into the chain!"
else
echo
echo "ERROR sidechain proposal not in chain!"
exit
fi
# Check age
COUNT=`echo $LISTACTIVATION | grep -c "\"nage\": 1"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain proposal age correct!"
else
echo
echo "ERROR sidechain proposal age invalid!"
exit
fi
# Check fail count
COUNT=`echo $LISTACTIVATION | grep -c "\"nfail\": 0"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain proposal has no failures!"
else
echo
echo "ERROR sidechain proposal has failures but should not!"
exit
fi
# Check that there are currently no active sidechains
LISTACTIVESIDECHAINS=`./mainchain/src/drivechain-cli --regtest listactivesidechains`
if [ "$LISTACTIVESIDECHAINS" == $'[\n]' ]; then
echo
echo "Good: no sidechains are active yet"
else
echo
echo "ERROR sidechain is already active but should not be!"
exit
fi
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
echo
echo "Will now mine enough blocks to activate the sidechain"
sleep 5s
# Mine enough blocks to activate the sidechain
minemainchain $SIDECHAIN_ACTIVATION_SCORE
# Check that the sidechain has been activated
LISTACTIVESIDECHAINS=`./mainchain/src/drivechain-cli --regtest listactivesidechains`
COUNT=`echo $LISTACTIVESIDECHAINS | grep -c "\"title\": \"testchain\""`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain has activated!"
else
echo
echo "ERROR sidechain failed to activate!"
exit
fi
echo
echo "listactivesidechains:"
echo
echo "$LISTACTIVESIDECHAINS"
# Disconnect chain tip, replace with a new one
replacetip
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
#
# Get testchain running
#
echo
echo "The sidechain testchain will now be started"
sleep 5s
# Start the sidechain and test that it can receive commands and has 0 blocks
starttestchain
echo
echo "Checking if the sidechain has started"
# Test that sidechain can receive commands and has 0 blocks
GETINFO=`./testchain/src/testchain-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 0"`
if [ "$COUNT" -eq 1 ]; then
echo "Sidechain up and running!"
else
echo "ERROR failed to send commands to sidechain"
exit
fi
#
# Test BMM mining testchain
#
echo
echo "Mining first Testchain BMM block!"
bmm testchain
# Check that BMM block was added to the sidechain
COUNT_TESTCHAIN=`./testchain/src/testchain-cli --regtest getblockcount`
if [ "$COUNT_TESTCHAIN" -eq 1 ]; then
echo "Sidechain connected BMM block!"
else
echo "ERROR sidechain has no BMM block connected!"
exit
fi
# Mine some more BMM blocks
echo
echo "Now we will test mining more BMM blocks"
for ((i = 0; i < 10; i++)); do
echo
echo "Mining more BMM blocks!"
bmm testchain
done
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=1
restartdrivechain
#
# Deposit to the sidechain
#
echo "We will now deposit to the sidechain"
sleep 3s
# Create sidechain deposit
ADDRESS=`./testchain/src/testchain-cli --regtest getnewaddress sidechain legacy`
DEPOSITADDRESS=`./testchain/src/testchain-cli --regtest formatdepositaddress $ADDRESS`
./mainchain/src/drivechain-cli --regtest createsidechaindeposit 0 $DEPOSITADDRESS 1 0.01
# Verify that there are currently no deposits in the db
DEPOSITCOUNT=`./mainchain/src/drivechain-cli --regtest countsidechaindeposits 0`
if [ $DEPOSITCOUNT -ne 0 ]; then
echo "Error: There is already a deposit in the db when there should be 0!"
exit
else
echo "Good: No deposits in db yet"
fi
# Generate a block to add the deposit to the mainchain
minemainchain 1
# Verify that a deposit was added to the db
DEPOSITCOUNT=`./mainchain/src/drivechain-cli --regtest countsidechaindeposits 0`
if [ $DEPOSITCOUNT -ne 1 ]; then
echo "Error: No deposit was added to the db!"
exit
else
echo "Good: Deposit added to db"
fi
# Replace the chain tip and restart
replacetip
REINDEX=0
restartdrivechain
# Verify that a deposit is still in the db after replacing tip & restarting
DEPOSITCOUNT=`./mainchain/src/drivechain-cli --regtest countsidechaindeposits 0`
if [ $DEPOSITCOUNT -ne 1 ]; then
echo "Error: Deposit vanished after replacing tip & restarting!"
exit
else
echo "Good: Deposit still in db after replacing tip & restarting"
fi
# Mine some BMM blocks so the sidechain can process the deposit
for ((i = 0; i < 10; i++)); do
echo
echo "Mining BMM to process deposit!"
sleep 0.5s
bmm testchain
done
# Check if the deposit address has any transactions on the sidechain
LIST_TRANSACTIONS=`./testchain/src/testchain-cli --regtest listtransactions "sidechain"`
COUNT=`echo $LIST_TRANSACTIONS | grep -c "\"address\": \"$ADDRESS\""`
if [ "$COUNT" -ge 1 ]; then
echo
echo "Sidechain deposit address has transactions!"
else
echo
echo "ERROR sidechain did not receive deposit!"
exit
fi
# Check for the deposit amount
COUNT=`echo $LIST_TRANSACTIONS | grep -c "\"amount\": 0.99999000"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain received correct deposit amount!"
else
echo
echo "ERROR sidechain did not receive deposit!"
exit
fi
# Check that the deposit has been added to our sidechain balance
BALANCE=`./testchain/src/testchain-cli --regtest getbalance`
BC=`echo "$BALANCE>0.9" | bc`
if [ $BC -eq 1 ]; then
echo
echo "Sidechain balance updated, deposit matured!"
echo "Sidechain balance: $BALANCE"
else
echo
echo "ERROR sidechain balance not what it should be... Balance: $BALANCE!"
exit
fi
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
#
# Withdraw from the sidechain
#
# Get a mainchain address and testchain refund address
MAINCHAIN_ADDRESS=`./mainchain/src/drivechain-cli --regtest getnewaddress mainchain legacy`
REFUND_ADDRESS=`./testchain/src/testchain-cli --regtest getnewaddress refund legacy`
# Call the CreateWithdrawal RPC
echo
echo "We will now create a withdrawal on the sidechain"
./testchain/src/testchain-cli --regtest createwithdrawal $MAINCHAIN_ADDRESS $REFUND_ADDRESS 0.5 0.1 0.1
sleep 3s
# Mine enough BMM blocks for a withdrawal bundle to be created and sent to the
# mainchain. We will mine up to 10 blocks before giving up.
echo
echo "Now we will mine enough BMM blocks for the sidechain to create a bundle"
for ((i = 0; i < 3; i++)); do
echo
echo "Mining BMM to process withdrawal!"
sleep 0.5s
bmm testchain
done
# Check if bundle was created
HASHBUNDLE=`./mainchain/src/drivechain-cli --regtest listwithdrawalstatus 0`
HASHBUNDLE=`echo $HASHBUNDLE | python -c 'import json, sys; obj=json.load(sys.stdin); print obj[0]["hash"]'`
if [ -z "$HASHBUNDLE" ]; then
echo "Error: No withdrawal bundle found"
exit
else
echo "Good: bundle found: $HASHBUNDLE"
fi
# Check that bundle has work score
WORKSCORE=`./mainchain/src/drivechain-cli --regtest getworkscore 0 $HASHBUNDLE`
if [ $WORKSCORE -lt 1 ]; then
echo "Error: No Workscore!"
exit
else
echo "Good: workscore: $WORKSCORE"
fi
# Check that if we replace the tip the workscore does not change
replacetip
NEWWORKSCORE=`./mainchain/src/drivechain-cli --regtest getworkscore 0 $HASHBUNDLE`
if [ $NEWWORKSCORE -ne $WORKSCORE ]; then
echo "Error: Workscore invalid after replacing tip!"
echo "$NEWWORKSCORE != $WORKSCORE"
exit
else
echo "Good - Workscore: $NEWWORKSCORE unchanged"
fi
# Set our node to upvote the withdrawal
echo "Setting vote for withdrawal to upvote!"
sleep 5s
./mainchain/src/drivechain-cli --regtest setwithdrawalvote upvote 0 $HASHBUNDLE
# Mine blocks until payout should happen
BLOCKSREMAINING=`./mainchain/src/drivechain-cli --regtest listwithdrawalstatus 0`
BLOCKSREMAINING=`echo $BLOCKSREMAINING | python -c 'import json, sys; obj=json.load(sys.stdin); print obj[0]["nblocksleft"]'`
WORKSCORE=`./mainchain/src/drivechain-cli --regtest getworkscore 0 $HASHBUNDLE`
echo
echo "Blocks remaining in verification period: $BLOCKSREMAINING"
echo "Workscore: $WORKSCORE / $MIN_WORK_SCORE"
sleep 10s
echo "Will now mine $MIN_WORK_SCORE blocks"
minemainchain $MIN_WORK_SCORE
# Check if payout was received
WITHDRAW_BALANCE=`./mainchain/src/drivechain-cli --regtest getbalance mainchain`
BC=`echo "$WITHDRAW_BALANCE>0.4" | bc`
if [ $BC -eq 1 ]; then
echo
echo
echo -e "\e[32m==========================\e[0m"
echo
echo -e "\e[1mpayout received!\e[0m"
echo "amount: $WITHDRAW_BALANCE"
echo
echo -e "\e[32m==========================\e[0m"
else
echo
echo -e "\e[31mError: payout not received!\e[0m"
exit
fi
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
# Restart again but with reindex
REINDEX=1
restartdrivechain
# Mine 100 more mainchain blocks
minemainchain 100
#
# Propose the trainchain and thunder sidechains
#
./mainchain/src/drivechain-cli --regtest createsidechainproposal 2 "thunder" "and lightning"
minemainchain 1
./mainchain/src/drivechain-cli --regtest createsidechainproposal 3 "trainchain" "all aboard"
minemainchain 1
# Mine enough blocks to activate the sidechains
minemainchain $SIDECHAIN_ACTIVATION_SCORE
# Check that the sidechains have been activated
LISTACTIVESIDECHAINS=`./mainchain/src/drivechain-cli --regtest listactivesidechains`
COUNTTHUNDER=`echo $LISTACTIVESIDECHAINS | grep -c "\"title\": \"thunder\""`
if [ "$COUNTTHUNDER" -eq 1 ]; then
echo
echo "Thunder has activated!"
else
echo
echo "ERROR Thunder failed to activate!"
exit
fi
COUNTTRAINCHAIN=`echo $LISTACTIVESIDECHAINS | grep -c "\"title\": \"trainchain\""`
if [ "$COUNTTRAINCHAIN" -eq 1 ]; then
echo
echo "Trainchain has activated!"
else
echo
echo "ERROR Trainchain failed to activate!"
exit
fi
echo
echo "listactivesidechains:"
echo
echo "$LISTACTIVESIDECHAINS"
# Shutdown drivechain, restart it, and make sure nothing broke
REINDEX=0
restartdrivechain
#
# Start Trainchain & Thunder and then test BMM mining them
#
echo
echo "Starting Trainchain and Thunder sidechains"
starttrainchain
startthunder
# BMM mine the three active sidechains
echo
echo "Now we will BMM mine all three active sidechains"
for ((i = 0; i < 10; i++)); do
echo
echo "BMM mining all three active sidechains!"
bmm testchain trainchain thunder
done
#
# Create deposits to all three sidechains
#
TESTCHAIN_ADDRESS=`./testchain/src/testchain-cli --regtest getnewaddress sidechain legacy`
TESTCHAIN_DEPOSIT_ADDRESS=`./testchain/src/testchain-cli --regtest formatdepositaddress $TESTCHAIN_ADDRESS`
./mainchain/src/drivechain-cli --regtest createsidechaindeposit 0 $TESTCHAIN_DEPOSIT_ADDRESS 1000 0.01
THUNDER_ADDRESS=`./thunder/src/thunder-cli --regtest getnewaddress sidechain legacy`
THUNDER_DEPOSIT_ADDRESS=`./thunder/src/thunder-cli --regtest formatdepositaddress $THUNDER_ADDRESS`
./mainchain/src/drivechain-cli --regtest createsidechaindeposit 2 $THUNDER_DEPOSIT_ADDRESS 1000 0.01
TRAINCHAIN_ADDRESS=`./trainchain/src/trainchain-cli --regtest getnewaddress sidechain legacy`
TRAINCHAIN_DEPOSIT_ADDRESS=`./trainchain/src/trainchain-cli --regtest formatdepositaddress $TRAINCHAIN_ADDRESS`
./mainchain/src/drivechain-cli --regtest createsidechaindeposit 3 $TRAINCHAIN_DEPOSIT_ADDRESS 1000 0.01
# Process deposits
echo
echo "Now we will BMM mine to process deposits"
for ((i = 0; i < 6; i++)); do
echo
echo "BMM mining to process deposits!"
bmm testchain trainchain thunder
done
# Check that the deposits have been added to our sidechain balance
BALANCE_TESTCHAIN=`./testchain/src/testchain-cli --regtest getbalance`
BC=`echo "$BALANCE_TESTCHAIN>=1000" | bc`
if [ $BC -eq 1 ]; then
echo
echo "Sidechain balance updated, deposit processed!"
echo "testchain balance: $BALANCE_TESTCHAIN"
else