forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-network.sh
executable file
·101 lines (72 loc) · 3.21 KB
/
start-network.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
#!/usr/bin/env bash
num_of_nodes=3
docker_image_version=v1.5.3
### first we check if docker and docker-compose are installed
has_docker=$(docker version | echo "$?")
if [ ! has_docker ]; then
echo [-] You must have docker installed to run the network
exit 1
fi
has_docker_compose=$(docker compose --version | echo "$?")
if [ ! has_docker_compose ]; then
echo [-] You must have docker compose installed to run the network
exit 1
fi
echo [+] Pulling the official drand docker image $docker_image_version
docker pull drandorg/go-drand:$docker_image_version 1>/dev/null
### then we create a volume for each of those nodes
echo [+] Creating docker volumes for $num_of_nodes nodes
for i in $(seq 1 $num_of_nodes);
do
docker volume create drand_docker_demo$i
done
### next we're going to generate a keypair on each of those volumes
echo [+] Generating a default network keypair for each node
for i in $(seq 1 $num_of_nodes);
do
# these will end up on drand1:8010, drand2:8020, drand3:8030, etc
# note they map to the container's mapped ports, but the internal ports; internally the services still listen on 8080
path=drand_docker_demo$i:80${i}0
docker run --rm --volume drand_docker_demo$i:/data/drand drandorg/go-drand:$docker_image_version generate-keypair --folder /data/drand/.drand --tls-disable --id default $path 1>/dev/null
done
### now we start them all using docker-compose as it'll be easy to spin up and down
echo [+] Starting all the nodes using docker-compose
docker compose -f docker-compose-network.yml up -d
### sleep to let the nodes start up
sleep 5
### now we run the initial distributed key generation
### we're going to use the first node as the leader node
echo [+] Starting distributed key generation for the leader
# we start the DKG and send it to the background;
docker exec --env DRAND_SHARE_SECRET=deadbeefdeadbeefdeadbeefdeadbeef --detach drand_docker_demo1 sh -c "drand share --id default --leader --nodes 3 --threshold 2 --period 15s --tls-disable"
# and sleep a second so the other nodes don't try and join before the leader has set up all its bits and bobs!
sleep 1
echo [+] Started distributed key generation for the leader
echo [+] Joining distributed key generation for the followers
for i in $(seq 2 $num_of_nodes);
do
# we start the DKG and send it to the background
docker exec --env DRAND_SHARE_SECRET=deadbeefdeadbeefdeadbeefdeadbeef --detach drand_docker_demo$i sh -c "drand share --id default --connect drand_docker_demo1:8010 --tls-disable"
done
### now we wait for the distributed key generation to be completed and the first round to be created
echo [+] Waiting for the DKG to finish - could take up to a minute!
attempts=60
while :
do
### if it isn't working after a bunch of attempts, it probably failed
if [ "$attempts" -eq 0 ]; then
echo [-] the DKG didn\'t finish successfully - check the container logs with '`docker logs -f drand_docker_demo1`'
exit 1
fi
### once the first round has been created, we know that the DKG happened succesfully
response=$(curl --silent localhost:9010/public/1)
code=$?
if [ $code -eq 0 ]; then
if [[ $response =~ "round" ]]; then
break
fi
fi
attempts=$(($attempts - 1))
sleep 1
done
echo [+] Network running successfully!