-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-nodes.sh
executable file
·131 lines (123 loc) · 4.48 KB
/
init-nodes.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
#!/usr/bin/env bash
echo $BASH_VERSION
#title : start.sh
#description : Build private containerized ethereum blockchain.
#author : ET-TAOUSY Zouhair
#date : 20111101
#version : 0.4
#usage : sh start.sh
#bash_version : 4.1.5(1)-release
#modified by : raresifrim@github
#==========================================================================
number_node=${1:-4}
number_chain=${2:-1}
nodes_path=./seed_data
account_file=seed_data/accounts.json
config_path=seed_data
extra_allocation_file=seed_data/extra_allocation.json
# Generate the cryptographic material for all nodes
createNodes(){
if [ $number_chain -eq 1 ]
then
for i in $(seq 1 $number_node); do
if [ -d "$nodes_path/node_$i" ]; then rm -Rf "$nodes_path/node_$i"; fi
mkdir -p "$nodes_path/node_$i"
mkdir -p "$nodes_path/node_$i/data"
done
else
for i in $(seq 1 $number_node); do
if [ -d "$nodes_path/node_$i" ]; then rm -Rf "$nodes_path/node_$i"; fi
mkdir -p "$nodes_path/node_$i"
for j in $(seq 1 $number_chain); do
mkdir -p "$nodes_path/node_$i/data$j"
done
done
fi
for i in $(seq 1 $number_node); do
mkdir -p "$nodes_path/node_$i/keys"
keys_path="$nodes_path/node_$i/keys"
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > $keys_path/key
echo "node_$i" > $keys_path/password
cat $keys_path/key | grep pub -A 5 | tail -n +2 | tr -d "\n[:space:]:" | sed "s/^04//" > $keys_path/pub.key
cat $keys_path/key | grep priv -A 3 | tail -n +2 | tr -d "\n[:space:]:" | sed "s/^00//" > $keys_path/priv.key
done
# We must generate the genesis for all blockchains -> only difference is the config_file
if [ $number_chain -eq 1 ]
then
config_file="${config_path}/blockchain_config.json"
python3 poa-genesis-gen.py "$nodes_path" "$number_node" "$config_file" "$account_file" "$extra_allocation_file"
else
for i in $(seq 1 $number_chain); do
config_file="${config_path}/blockchain${i}_config.json"
python3 poa-genesis-gen.py "$nodes_path" "$number_node" "$config_file" "$account_file" "$extra_allocation_file" "$nodes_path/genesis$i.json"
done
fi
}
# Import the generated nodes cryptographics materials to the blockchain network
# Skipped, import in docker entry command
# Initialise the nodes data folder with genesis block configuration
# Skipped, init in docker entry command
# Generate the enodes keys to sync nodes each others
getEnodesByIndex(){
enodes=()
for i in $(seq 1 $number_node); do
if [ "$i" != "$1" ]
then
if [ $number_chain -eq 1 ]
then
port=$((30312+$i))
else
port=$((30312 + $i + $((128 * $(($2-1))))))
fi
path="${nodes_path}/node_${i}/keys/pub.key"
line=$(head -n 1 "$path")
lf="$(($number_node-1))"
if [ "$i" -eq "$number_node" ]
then
enodes+='"enode://'$line'@'${addresses[${i}]}':'$port'"'$'\r'
elif [ "$number_node" -eq "$1" ] && [ "$i" -eq "$lf" ]
then
if [ $number_chain -eq 1 ]
then
enodes+='"enode://'$line'@node'${i}':'$port'"'$'\r'
else
enodes+='"enode://'$line'@node'${i}'_chain'$2':'$port'"'$'\r'
fi
else
if [ $number_chain -eq 1 ]
then
enodes+='"enode://'$line'@node'${i}':'$port'",'$'\r'
else
enodes+='"enode://'$line'@node'${i}'_chain'$2':'$port'",'$'\r'
fi
fi
fi
done
echo "${enodes[@]}"
}
# Save enodes in every node's data folder & export docker-compose file
saveEnodes(){
if [ $number_chain -eq 1 ]
then
for i in $(seq 1 $number_node); do
data_path="$nodes_path/node_$i/data"
echo "[" > "$data_path/static-nodes.json"
enodes=$(getEnodesByIndex $i)
printf "%s\n" "${enodes[@]}" >> "$data_path/static-nodes.json"
echo "]" >> "$data_path/static-nodes.json"
done
else
for k in $(seq 1 $number_chain); do
for i in $(seq 1 $number_node); do
data_path="$nodes_path/node_$i/data$k"
echo "[" > "$data_path/static-nodes.json"
enodes=$(getEnodesByIndex $i $k)
printf "%s\n" "${enodes[@]}" >> "$data_path/static-nodes.json"
echo "]" >> "$data_path/static-nodes.json"
done
done
fi
python3 poa-generate-yaml.py "$number_node" "$number_chain"
}
createNodes
saveEnodes