forked from mshuaic/Blockchain_med
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_node.sh
139 lines (115 loc) · 4.27 KB
/
create_node.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
#!/bin/bash
#bash script.sh [dir_prefix] [num_of_node] [port] [chain_name]
dir_prefix=node
num_of_node=4
rpcport=8570
chain=chain1
if (( $# == 4 )); then
echo "format: script.sh dir_prefix num_of_node port chain_name"
dir_prefix=$1
num_of_node=$2
rpcport=$3
chain=$4
fi
# check if jason parser exist
if ! [ -x "$(command -v jq)" ]; then
echo "Cannot find jq. Please install jq."
echo "https://stedolan.github.io/jq/"
exit
fi
TIMEOUT=10
echo rpcuser=multichainrpc >> multichain.conf
echo rpcpassword=emory >> multichain.conf
echo rpcallowip=0.0.0.0/0 >> multichain.conf
echo rpcport=$rpcport >> multichain.conf
# create folder for each node
for ((i=0; i < $num_of_node; i++))
do
newdir=$(echo $dir_prefix$i)/multichain
mkdir -p $newdir
cp multichain.conf $newdir/
done
rm multichain.conf
# run master node
echo "building master node"
dir=$(pwd)/${dir_prefix}0
docker run -it -d --name ${dir_prefix}0 -v $dir/multichain:/root/.multichain -p 2750:2750 -p $rpcport:$rpcport mshuaic/blockchainnode 1>/dev/null
# create blockchain
docker exec -d ${dir_prefix}0 multichain-util create $chain -default-rpc-port=$rpcport -default-network-port=$[$rpcport+1] -mine-empty-rounds=1 -anyone-can-connect=true -anyone-can-create=true -anyone-can-send=true -anyone-can-receive=true -setup-first-blocks=1 -anyone-can-mine=false -mining-turnover=0 -target-block-time=2
# copy config file
docker exec -d ${dir_prefix}0 cp -f /root/.multichain/multichain.conf /root/.multichain/$chain/multichain.conf
# run blockchain
docker exec -d ${dir_prefix}0 multichaind $chain -autosubscribe=streams -daemon
# run multichain-explorer
# ATTENTION:
# if chain name is not chain1, configure multichain accordingly.
docker exec -d ${dir_prefix}0 python -m Mce.abe --config /root/multichain-explorer/chain1.conf
# docker network ip
ip=$(docker exec ${dir_prefix}0 hostname -I)
# blockchain connection port
conport=$[$rpcport + 1]
# run other nodes
echo "building slave nodes"
for ((i=1; i < $num_of_node; i++))
do
nodename=$dir_prefix$i
docker run -it -d --name $nodename -v $(pwd)/$nodename/multichain:/root/.multichain -p $[$rpcport + $i]:$rpcport mshuaic/blockchainnode 1>/dev/null
docker exec -d $nodename multichaind $chain@$ip:$conport -rpcpassword=emory -autosubscribe=streams -daemon
# this setup is not good for multichain-explorer on slave nodes
# docker exec -d $nodename cp -f /root/.multichain/multichain.conf /root/.multichain/$chain/multichain.conf
done
# pause for 1 s, let node start to run blockchain
echo "setting up nodes"
sleep 2
# a workaround of this issue
# https://www.multichain.com/qa/3601/anyone-can-issue-dont-seem-to-work?show=3604#a3604
# get slaves' address and send 0 asset to them
confirmation=0
until ((confirmation >= 1))
do
echo "send 0 asset to slave"
for ((i=0; i< $num_of_node; i++))
do
address[$i]=$(curl -d '{"method":"getaddresses","params":[]}' http://multichainrpc:[email protected]:$[$rpcport + $i]/ 2>/dev/null | jq '.["result"][0]')
# echo ${address[$i]}
done
for ((i=1; i< $num_of_node; i++))
do
txid[$i]=$(curl -d '{"method":"send","params":['"${address[$i]}"',0]}' http://multichainrpc:[email protected]:$rpcport/ 2>/dev/null | jq '.["result"]')
# echo ${txid[$i]}
done
for ((i=1; i< $num_of_node; i++))
do
timer=0
confirmation=0
until (( $confirmation >= 1 ))
do
confirmation=$(curl -d '{"method":"gettxout","params":['"${txid[$i]}"',0]}' http://multichainrpc:[email protected]:$rpcport/ 2>/dev/null | jq '.["result"]["confirmations"]')
sleep 1
((timer++))
if ((timer == TIMEOUT)); then
echo "TIME OUT"
break
fi
done
if ((timer == TIMEOUT)); then
break
fi
done
done
# for ((i=1; i< $num_of_node; i++))
# do
# timer=0
# confirmation=0
# until (( $confirmation >= 1 ))
# do
# confirmation=$(curl -d '{"method":"gettxout","params":['"${txid[$i]}"',0]}' http://multichainrpc:[email protected]:$rpcport/ 2>/dev/null | jq '.["result"]["confirmations"]')
# sleep 1
# ((timer++))
# if ((timer == TIMEOUT)); then
# echo "TIME OUT"
# exit
# fi
# done
# done
echo "nodes and blockchain are successfully created"