-
Notifications
You must be signed in to change notification settings - Fork 4
/
drivechainintegration.sh
executable file
·1055 lines (835 loc) · 28.7 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 and all known sidechain
# projects. Then a series of integration tests will run.
#
# Warn user, delete old data, clone repositories
#
VERSION=0
# 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
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"
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
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 DriveNet and sidechain(s)"
echo "The functional unit tests will be run for DriveNet and sidechain(s)."
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 sidechain(s)."
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: ~/.drivenet & ~/.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
REINDEX=0
#
# Functions to help the script
#
function startdrivenet {
if [ $REINDEX -eq 1 ]; then
echo
echo "DriveNet will be reindexed"
echo
./mainchain/src/qt/drivenet-qt \
--reindex \
--connect=0 \
--regtest \
--defaultwtprimevote=upvote &
# Also set REINDEX back to 0
REINDEX=0
else
./mainchain/src/qt/drivenet-qt \
--connect=0 \
--regtest \
--defaultwtprimevote=upvote &
fi
}
function starttestchain {
./sidechains/src/qt/testchain-qt \
--connect=0 \
--mainchainregtest \
--verifybmmacceptheader \
--verifybmmacceptblock \
--verifybmmreadblock \
--verifybmmcheckblock \
--verifywtprimeacceptblock \
--mainchainrpcport=18443 &
}
function restartdrivenet {
if [ $SKIP_RESTART -eq 1 ]; then
return 0
fi
#
# Shutdown DriveNet, 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 DriveNet & verify its state after restarting!"
sleep 3s
# Record the state before restart
HASHSCDB=`./mainchain/src/drivenet-cli --regtest getscdbhash`
HASHSCDB=`echo $HASHSCDB | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["hashscdb"]'`
HASHSCDBTOTAL=`./mainchain/src/drivenet-cli --regtest gettotalscdbhash`
HASHSCDBTOTAL=`echo $HASHSCDBTOTAL | 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/drivenet-cli --regtest getblockcount`
# getbestblockhash also doesn't return an array
BESTBLOCK=`./mainchain/src/drivenet-cli --regtest getbestblockhash`
# Restart
./mainchain/src/drivenet-cli --regtest stop
sleep 5s # Wait a little bit incase shutdown takes a while
startdrivenet
echo
echo "Waiting for mainchain to start"
sleep 10s
# Verify the state after restart
HASHSCDBRESTART=`./mainchain/src/drivenet-cli --regtest getscdbhash`
HASHSCDBRESTART=`echo $HASHSCDBRESTART | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["hashscdb"]'`
HASHSCDBTOTALRESTART=`./mainchain/src/drivenet-cli --regtest gettotalscdbhash`
HASHSCDBTOTALRESTART=`echo $HASHSCDBTOTALRESTART | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["hashscdbtotal"]'`
COUNTRESTART=`./mainchain/src/drivenet-cli --regtest getblockcount`
BESTBLOCKRESTART=`./mainchain/src/drivenet-cli --regtest getbestblockhash`
if [ "$COUNT" != "$COUNTRESTART" ]; then
echo "Error after restarting DriveNet!"
echo "COUNT != COUNTRESTART"
echo "$COUNT != $COUNTRESTART"
exit
fi
if [ "$BESTBLOCK" != "$BESTBLOCKRESTART" ]; then
echo "Error after restarting DriveNet!"
echo "BESTBLOCK != BESTBLOCKRESTART"
echo "$BESTBLOCK != $BESTBLOCKRESTART"
exit
fi
if [ "$HASHSCDB" != "$HASHSCDBRESTART" ]; then
echo "Error after restarting DriveNet!"
echo "HASHSCDB != HASHSCDBRESTART"
echo "$HASHSCDB != $HASHSCDBRESTART"
exit
fi
if [ "$HASHSCDBTOTAL" != "$HASHSCDBTOTALRESTART" ]; then
echo "Error after restarting DriveNet!"
echo "HASHSCDBTOTAL != HASHSCDBTOTALRESTART"
echo "$HASHSCDBTOTAL != $HASHSCDBTOTALRESTART"
exit
fi
echo
echo "DriveNet 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/drivenet-cli --regtest getblockcount`
OLDTIP=`./mainchain/src/drivenet-cli --regtest getbestblockhash`
./mainchain/src/drivenet-cli --regtest invalidateblock $OLDTIP
sleep 3s # Give some time for the block to be invalidated
DISCONNECTCOUNT=`./mainchain/src/drivenet-cli --regtest getblockcount`
if [ "$DISCONNECTCOUNT" == "$OLDCOUNT" ]; then
echo "Failed to disconnect tip!"
exit
fi
./mainchain/src/drivenet-cli --regtest generate 1
NEWTIP=`./mainchain/src/drivenet-cli --regtest getbestblockhash`
NEWCOUNT=`./mainchain/src/drivenet-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
}
# Remove old data directories
rm -rf ~/.drivenet
rm -rf ~/.testchain
# These can fail, meaning that the repository is already downloaded
if [ $SKIP_CLONE -ne 1 ]; then
echo
echo "Cloning repositories"
echo "If you see \"Fatal error\" here that means the repository is already cloned - no problem"
git clone https://github.com/drivechain-project/mainchain
git clone https://github.com/drivechain-project/sidechains
fi
#
# Build repositories & run their unit tests
#
echo
echo "Building repositories"
cd sidechains
if [ $SKIP_BUILD -ne 1 ]; then
git checkout testchain &&
git pull &&
./autogen.sh &&
./configure &&
make -j 9
fi
if [ $SKIP_CHECK -ne 1 ]; then
make check
if [ $? -ne 0 ]; then
echo "Make check failed!"
exit
fi
fi
cd ../mainchain
if [ $SKIP_BUILD -ne 1 ]; then
git checkout master &&
git pull &&
./autogen.sh &&
./configure &&
make -j 9
fi
if [ $SKIP_CHECK -ne 1 ]; then
make check
if [ $? -ne 0 ]; then
echo "Make check failed!"
exit
fi
fi
cd ../
#
# Get mainchain configured and running. Mine first 100 mainchain blocks.
#
# Create configuration file for mainchain
echo
echo "Create mainchain configuration file"
mkdir ~/.drivenet/
touch ~/.drivenet/drivenet.conf
echo "rpcuser=patrick" > ~/.drivenet/drivenet.conf
echo "rpcpassword=integrationtesting" >> ~/.drivenet/drivenet.conf
echo "server=1" >> ~/.drivenet/drivenet.conf
# Start DriveNet-qt
startdrivenet
echo
echo "Waiting for mainchain to start"
sleep 5s
echo
echo "Checking if the mainchain has started"
# Test that mainchain can receive commands and has 0 blocks
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 0"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain up and running!"
else
echo
echo "ERROR failed to send commands to mainchain or block count non-zero"
exit
fi
echo
echo "Mainchain will now generate first 100 blocks"
sleep 3s
./mainchain/src/drivenet-cli --regtest generate 100
# Check that 100 blocks were mined
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 100"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain 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 DriveNet, restart it, and make sure nothing broke
REINDEX=0
restartdrivenet
#
# Activate a sidechain
#
# Create a sidechain proposal
./mainchain/src/drivenet-cli --regtest createsidechainproposal "testchain" "testchain for integration test" "0186ff51f527ffdcf2413d50bdf8fab1feb20e5f82815dad48c73cf462b8b313"
# Check that proposal was cached (not in chain yet)
LISTPROPOSALS=`./mainchain/src/drivenet-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 block, proposal should be in chain after that
./mainchain/src/drivenet-cli --regtest generate 1
# Check that we have 101 blocks now
GETINFO=`./mainchain/src/drivenet-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!"
exit
fi
# Disconnect chain tip, replace with a new one
replacetip
# Shutdown DriveNet, restart it, and make sure nothing broke
# This time we will also reindex
REINDEX=0
restartdrivenet
# Check that proposal has been added to the chain and ready for voting
LISTACTIVATION=`./mainchain/src/drivenet-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/drivenet-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 DriveNet, restart it, and make sure nothing broke
REINDEX=0
restartdrivenet
echo
echo "Will now mine enough blocks to activate the sidechain"
sleep 5s
# Mine enough blocks to activate the sidechain
./mainchain/src/drivenet-cli --regtest generate 255
# Check that 255 blocks were mined
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 356"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has 356 blocks"
else
echo
echo "ERROR failed to mine blocks!"
exit
fi
# Check that the sidechain has been activated
LISTACTIVESIDECHAINS=`./mainchain/src/drivenet-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 DriveNet, restart it, and make sure nothing broke
restartdrivenet
#
# Get sidechain configured and running
#
# Create configuration file for sidechain testchain
echo
echo "Creating sidechain configuration file"
mkdir ~/.testchain/
touch ~/.testchain/testchain.conf
echo "rpcuser=patrick" > ~/.testchain/testchain.conf
echo "rpcpassword=integrationtesting" >> ~/.testchain/testchain.conf
echo "server=1" >> ~/.testchain/testchain.conf
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 "Waiting for testchain to start"
sleep 5s
echo
echo "Checking if the sidechain has started"
# Test that sidechain can receive commands and has 0 blocks
GETINFO=`./sidechains/src/testchain-cli 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 or block count non-zero"
exit
fi
# Check if the sidechain can communicate with the mainchain
#
# Start BMM mining the sidechain
#
# The first time that we call this it should create the first BMM request and
# send it to the mainchain node, which will add it to the mempool
echo
echo "Going to refresh BMM on the sidechain and send BMM request to mainchain"
./sidechains/src/testchain-cli refreshbmm
# TODO check that mainchain has BMM request in mempool
echo
echo "Giving mainchain some time to receive BMM request from sidechain..."
sleep 3s
echo
echo "Mining block on the mainchain, should include BMM commit"
# Mine a mainchain block, which should include the BMM request we just made
./mainchain/src/drivenet-cli --regtest generate 1
# Check that the block was mined
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 357"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has 357 blocks"
else
echo
echo "ERROR failed to mine blocks!"
exit
fi
# Shutdown DriveNet, restart it, and make sure nothing broke
REINDEX=1
restartdrivenet
# TODO verifiy that bmm request was added to chain and removed from mempool
# Refresh BMM again, this time the block we created the first BMM request for
# should be added to the side chain, and a new BMM request created for the next
# block
echo
echo "Will now refresh BMM on the sidechain again and look for our BMM commit"
echo "BMM block will be connected to the sidechain if BMM commit was made."
./sidechains/src/testchain-cli refreshbmm
# Check that BMM block was added to the sidechain
GETINFO=`./sidechains/src/testchain-cli getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": 1"`
if [ "$COUNT" -eq 1 ]; then
echo "Sidechain connected BMM block!"
else
echo "ERROR sidechain has no BMM block connected!"
exit
fi
# Mine some more BMM blocks and make sure that they all make it to the sidechain
echo
echo "Now we will test mining more BMM blocks"
CURRENT_BLOCKS=357
CURRENT_SIDE_BLOCKS=1
COUNTER=1
while [ $COUNTER -le 10 ]
do
# Wait a little bit
echo
echo "Waiting for new BMM request to make it to the mainchain..."
sleep 0.26s
echo "Mining mainchain block"
# Generate mainchain block
./mainchain/src/drivenet-cli --regtest generate 1
CURRENT_BLOCKS=$(( CURRENT_BLOCKS + 1 ))
# Check that mainchain block was connected
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
echo $GETINFO
echo $CURRENT_BLOCKS
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has $CURRENT_BLOCKS blocks"
else
echo
echo "ERROR failed to mine block!"
exit
fi
# Refresh BMM on the sidechain
echo
echo "Refreshing BMM on the sidechain..."
./sidechains/src/testchain-cli refreshbmm
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS + 1 ))
# Check that BMM block was added to the side chain
GETINFO=`./sidechains/src/testchain-cli getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_SIDE_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain connected BMM block!"
else
echo
echo "ERROR sidechain did not connect BMM block!"
# TODO In the testing environment we shouldn't have any failures at all.
# It would however be normal in real use to have some failures...
#
# For now, if we have a failure during testing which is probably due
# to a bug on main or side and not the testing environment which has
# perfect conditions, move on and try again just like a real node would.
# TODO renable exit here?
# Subtract 1 before moving on, since we
# failed to actually add it.
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS - 1 ))
fi
((COUNTER++))
done
# Shutdown DriveNet, restart it, and make sure nothing broke
restartdrivenet
#
# Deposit to the sidechain
#
echo "We will now deposit to the sidechain"
sleep 3s
# Create sidechain deposit
ADDRESS=`./sidechains/src/testchain-cli getnewaddress sidechain legacy`
./mainchain/src/drivenet-cli --regtest createsidechaindeposit 0 $ADDRESS 1 0.01
# Verify that there are currently no deposits in the db
DEPOSITCOUNT=`./mainchain/src/drivenet-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
./mainchain/src/drivenet-cli --regtest generate 1
CURRENT_BLOCKS=$(( CURRENT_BLOCKS + 1 )) # TODO stop using CURRENT_BLOCKS
# Verify that a deposit was added to the db
DEPOSITCOUNT=`./mainchain/src/drivenet-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
restartdrivenet
# Verify that a deposit is still in the db after replacing tip & restarting
DEPOSITCOUNT=`./mainchain/src/drivenet-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 blocks and BMM the sidechain so it can process the deposit
COUNTER=1
while [ $COUNTER -le 200 ]
do
# Wait a little bit
echo
echo "Waiting for new BMM request to make it to the mainchain..."
sleep 0.26s
echo "Mining mainchain block"
# Generate mainchain block
./mainchain/src/drivenet-cli --regtest generate 1
CURRENT_BLOCKS=$(( CURRENT_BLOCKS + 1 ))
# Check that mainchain block was connected
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has $CURRENT_BLOCKS blocks"
else
echo
echo "ERROR failed to mine block!"
exit
fi
# Refresh BMM on the sidechain
echo
echo "Refreshing BMM on the sidechain..."
./sidechains/src/testchain-cli refreshbmm
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS + 1 ))
# Check that BMM block was added to the side chain
GETINFO=`./sidechains/src/testchain-cli getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_SIDE_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain connected BMM block!"
else
echo
echo "ERROR sidechain did not connect BMM block!"
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS - 1 ))
fi
((COUNTER++))
done
# Check if the deposit made it to the sidechain
LIST_TRANSACTIONS=`./sidechains/src/testchain-cli listtransactions`
COUNT=`echo $LIST_TRANSACTIONS | grep -c "\"address\": \"$ADDRESS\""`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain received deposit!"
echo "Deposit: "
echo "$LIST_TRANSACTIONS"
else
echo
echo "ERROR sidechain did not receive deposit!"
exit
fi
# Shutdown DriveNet, restart it, and make sure nothing broke
restartdrivenet
echo
echo "Now we will BMM the sidechain until the deposit has matured!"
# Sleep here so user can read the deposit debug output
sleep 5s
# Mature the deposit on the sidechain, so that it can be withdrawn
COUNTER=1
while [ $COUNTER -le 121 ]
do
# Wait a little bit
echo
echo "Waiting for new BMM request to make it to the mainchain..."
sleep 0.26s
echo "Mining mainchain block"
# Generate mainchain block
./mainchain/src/drivenet-cli --regtest generate 1
CURRENT_BLOCKS=$(( CURRENT_BLOCKS + 1 ))
# Check that mainchain block was connected
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has $CURRENT_BLOCKS blocks"
else
echo
echo "ERROR failed to mine block!"
exit
fi
# Refresh BMM on the sidechain
echo
echo "Refreshing BMM on the sidechain..."
./sidechains/src/testchain-cli refreshbmm
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS + 1 ))
# Check that BMM block was added to the side chain
GETINFO=`./sidechains/src/testchain-cli getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_SIDE_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain connected BMM block!"
else
echo
echo "ERROR sidechain did not connect BMM block!"
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS - 1 ))
fi
((COUNTER++))
done
# Check that the deposit has been added to our sidechain balance
BALANCE=`./sidechains/src/testchain-cli 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
# Test sending the deposit around to other addresses on the sidechain
# TODO
# Shutdown DriveNet, restart it, and make sure nothing broke
REINDEX=1
restartdrivenet
#
# Withdraw from the sidechain
#
# Get a mainchain address
MAINCHAIN_ADDRESS=`./mainchain/src/drivenet-cli --regtest getnewaddress mainchain legacy`
# Call the CreateWT RPC
echo
echo "We will now create a wt on the sidechain"
./sidechains/src/testchain-cli createwt $MAINCHAIN_ADDRESS 0.5
sleep 3s
# Mine enough BMM blocks for a WT^ to be created and sent to the mainchain
# We will mine up to 300 blocks before giving up
echo
echo "Now we will mine enough BMM blocks for the sidechain to create a WT^"
COUNTER=1
while [ $COUNTER -le 300 ]
do
# Wait a little bit
echo
echo "Waiting for new BMM request to make it to the mainchain..."
sleep 0.26s
echo "Mining mainchain block"
# Generate mainchain block
./mainchain/src/drivenet-cli --regtest generate 1
CURRENT_BLOCKS=$(( CURRENT_BLOCKS + 1 ))
# Check that mainchain block was connected
GETINFO=`./mainchain/src/drivenet-cli --regtest getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Mainchain has $CURRENT_BLOCKS blocks"
else
echo
echo "ERROR failed to mine block!"
exit
fi
# Refresh BMM on the sidechain
echo
echo "Refreshing BMM on the sidechain..."
./sidechains/src/testchain-cli refreshbmm
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS + 1 ))
# Check that BMM block was added to the side chain
GETINFO=`./sidechains/src/testchain-cli getmininginfo`
COUNT=`echo $GETINFO | grep -c "\"blocks\": $CURRENT_SIDE_BLOCKS"`
if [ "$COUNT" -eq 1 ]; then
echo
echo "Sidechain connected BMM block!"
else
echo
echo "ERROR sidechain did not connect BMM block!"
CURRENT_SIDE_BLOCKS=$(( CURRENT_SIDE_BLOCKS - 1 ))
fi
# Check for WT^
WTPRIMECHECK=`./mainchain/src/drivenet-cli --regtest listwtprimestatus 0`
if [ "-$WTPRIMECHECK-" != "--" ]; then
echo "WT^ has been found!"
break
fi
((COUNTER++))
done
# Check if WT^ was created
HASHWTPRIME=`./mainchain/src/drivenet-cli --regtest listwtprimestatus 0`
HASHWTPRIME=`echo $HASHWTPRIME | python -c 'import json, sys; obj=json.load(sys.stdin); print obj[0]["hashwtprime"]'`
if [ -z "$HASHWTPRIME" ]; then
echo "Error: No WT^ found"
exit
else
echo "Good: WT^ found: $HASHWTPRIME"
fi
# Check that WT^ has work score
WORKSCORE=`./mainchain/src/drivenet-cli --regtest getworkscore 0 $HASHWTPRIME`
if [ $WORKSCORE -lt 1 ]; then
echo "Error: No Workscore for WT^!"
exit
else
echo "Good: WT^ workscore: $WORKSCORE"
fi
# Check that if we replace the tip the workscore does not change
replacetip
NEWWORKSCORE=`./mainchain/src/drivenet-cli --regtest getworkscore 0 $HASHWTPRIME`
if [ $NEWWORKSCORE -ne $WORKSCORE ]; then
echo "Error: Workscore invalid after replacing tip!"
echo "$NEWWORKSCORE != $WORKSCORE"
exit
else
echo "Good - Workscore: $NEWWORKSCORE unchanged"
fi
# Mine blocks until WT^ payout should happen