github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/tools/deploy/network_on_single.sh (about) 1 #!/bin/bash 2 3 #################################本脚本适合于在一台服务器上进行区块链的体验#################################### 4 #使用方法 5 # bash network-on-single.sh 6 #set -x 7 8 start=$(date +%s) 9 10 #注意!注意!注意!修改为本机的IP地址 11 host="192.168.4.157" 12 13 #一般不用修改 14 localIP="127.0.0.1" 15 16 rm -rf node* 17 18 #链的Id,可以自定义,只要是一个大于0的整数即可。 19 chainId=$(date '+%Y%m%d') 20 21 #等待时间 22 waitPeriod=3 23 24 #出块时间,以秒为单位 25 period=3 26 27 gasLimit=0x1cf1ab00 28 29 balance=0x84595161401484a000000 30 31 #三个账户,三个节点 32 allNodeCount=7 33 34 #一个节点一个账户 35 accountPasswords=("123456" "123456" "123456" "123456" "123456" "123456" "123456") 36 37 httpPort=6545 38 39 websocketPort=6546 40 41 p2pPort=30312 42 43 graphqlPort=6547 44 45 accounts=() 46 47 encodeNodes=() 48 49 cmd="./sipe" 50 51 if [ ! -e $cmd ];then 52 echo "Make sure the file $cmd exists" 53 exit 1 54 fi 55 56 chmod +x $cmd 57 58 if [ ! -e ./Permission.bin ];then 59 echo "Make sure the file ./Permission.bin exists" 60 exit 1 61 fi 62 63 chmod +x $cmd 64 65 # shellcheck disable=SC2004 66 for((i=0;i<${allNodeCount};i++)) 67 do 68 mkdir -p "node-$i/data" 69 echo "${accountPasswords[$i]}">>./node-$i/password.txt 70 content=$($cmd --data.dir=./node-$i/data --password=./node-$i/password.txt account new) 71 echo "$content">>./node-$i/data/content.txt 72 # shellcheck disable=SC2002 73 account=$(cat ./node-$i/data/content.txt| grep "Public address of the key"|awk -F":" '{print $2}'| awk '{print $1}') 74 accounts[$i]=$account 75 echo " " 76 echo "############ create account $i:$account ############ " 77 echo " " 78 rm -rf ./node-$i/data/content.txt 79 done 80 81 genesis="{\"config\":{\"chainId\":" 82 83 genesis="$genesis$chainId," 84 85 genesis=$genesis"\"singularityBlock\": 0,\"hotstuff\": {\"view\": 0,\"council\": " 86 87 hotstuffpeer='[' 88 89 for((i=0;i<${allNodeCount};i++)) 90 do 91 peerInfo=$($cmd --data.dir=./node-$i/data genbls12sec) 92 # shellcheck disable=SC2086 93 if [ $i -eq $((allNodeCount-1)) ] ;then 94 hotstuffpeer=$hotstuffpeer"{\"id\":$((i+1)),\"publicKey\":\"0x$peerInfo\"}" 95 else 96 hotstuffpeer=$hotstuffpeer"{\"id\":$((i+1)),\"publicKey\":\"0x$peerInfo\"}," 97 fi 98 done 99 100 101 genesis=$genesis$hotstuffpeer"]}},\"nonce\": \"0x0000000000000001\",\"timestamp\": \"0x611dd0cd\"," 102 103 extraData="\"0x6800c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"" 104 105 genesis=$genesis"\"extraData\":" 106 107 genesis=$genesis$extraData"," 108 109 genesis=$genesis"\"gasLimit\": \"$gasLimit\"," 110 111 end="\"difficulty\": \"0x1\",\"mixHash\": \"0x0000000000000000000000000000000000000000000000000000000000000000\",\"coinbase\": \"0x0000000000000000000000000000000000000000\"," 112 113 end=$end"\"alloc\": {\"0000000000000000000000000000000000000000\": {\"balance\": \"0x1\"}," 114 115 #根据初始化,给管理员也是出块账户分配余额 116 # shellcheck disable=SC2004 117 for((i=0;i<$allNodeCount;i++)) 118 do 119 if [ $i -eq $((allNodeCount-1)) ] ;then 120 end=$end"\"${accounts[$i]:2}\": {\"balance\": \"$balance\"}" 121 else 122 end=$end"\"${accounts[$i]:2}\": {\"balance\": \"$balance\"}," 123 fi 124 done 125 126 end=$end"}," 127 128 end=$end"\"number\": \"0x0\",\"gasUsed\": \"0x0\",\"parentHash\": \"0x0000000000000000000000000000000000000000000000000000000000000000\"}" 129 130 genesis=$genesis$end 131 132 echo "$genesis" 133 134 #每个目录下的都拷贝一个genesis.json文件 135 # shellcheck disable=SC2004 136 for((i=0;i<$allNodeCount;i++)) 137 do 138 echo "$genesis">>"./node-$i/genesis.json" 139 done 140 141 echo "############ genesis file generate success #################" 142 143 echo " " 144 145 sleep $waitPeriod 146 147 # shellcheck disable=SC2046 148 # shellcheck disable=SC2164 149 workdir="$(cd $(dirname "$0"); pwd)" 150 151 152 153 #初始化好每个节点目录 154 # shellcheck disable=SC2004 155 for((i=0;i<$allNodeCount;i++)) 156 do 157 158 httpPort=$((httpPort + 1000)) 159 160 websocketPort=$((websocketPort + 1000)) 161 162 p2pPort=$((p2pPort + 1000)) 163 164 graphqlPort=$((graphqlPort + 1000)) 165 166 pkill sipe 167 168 #拷贝链的可执行程序 169 cp -r "$workdir/sipe" "$workdir/node-$i" 170 171 cp -r "$workdir/stop.sh" "$workdir/node-$i" 172 173 #进入节点对应的目录 174 # shellcheck disable=SC2164 175 cd "$workdir/node-$i" 176 177 178 #初始化链的数据目录 179 $cmd --data.dir=./data --no.usb init genesis.json>>app.log 2>&1 180 181 #将命令和启动参数都写入start.sh文件中 182 # shellcheck disable=SC2129 183 echo "#!/bin/bash">>./start.sh 184 echo "export dataDir=./data">>./start.sh 185 echo "export httpPort=$httpPort">>./start.sh 186 echo "export p2pPort=$p2pPort">>./start.sh 187 echo "export unlockAccount=${accounts[$i]}">>./start.sh 188 echo "export password=./password.txt">>./start.sh 189 echo "export websocketPort=$websocketPort">>./start.sh 190 191 echo "nohup ./sipe \\">>./start.sh 192 echo "--no.discover \\">>./start.sh 193 echo "--no.usb \\">>./start.sh 194 echo "--data.dir \$dataDir \\">>./start.sh 195 echo "--gc.mode archive \\">>./start.sh 196 echo "--allow-insecure-unlock \\">>./start.sh 197 echo "--http \\">>./start.sh 198 echo "--http.addr '0.0.0.0' \\">>./start.sh 199 echo "--http.port \$httpPort \\">>./start.sh 200 echo "--http.api 'admin,miner,db,eth,net,web3,personal,debug,txpool,permission,raft,clique' \\">>./start.sh 201 echo "--http.cors.domain '*' \\">>./start.sh 202 echo "--port \$p2pPort \\">>./start.sh 203 echo "--miner.gas.price 0 \\">>./start.sh 204 echo "--unlock \$unlockAccount \\">>./start.sh 205 echo "--password \$password \\">>./start.sh 206 echo "--mine \\">>./start.sh 207 echo "--miner.no.empty \\">>./start.sh 208 echo "--miner.ether.base \$unlockAccount \\">>./start.sh 209 echo "--sync.mode full \\">>./start.sh 210 echo "--verbosity 4 \\">>./start.sh 211 echo "--ws \\">>./start.sh 212 echo "--ws.addr '0.0.0.0' \\">>./start.sh 213 echo "--ws.port \$websocketPort \\">>./start.sh 214 echo "--ws.api 'admin,miner,db,eth,net,web3,personal,debug,txpool,permission,raft,clique' \\">>./start.sh 215 echo "--ws.origins '*' \\">>./start.sh 216 echo "--tx.pool.account.slots 10000 \\">>./start.sh 217 echo "--tx.pool.global.slots 20000 \\">>./start.sh 218 echo "--tx.pool.account.queue 10000 \\">>./start.sh 219 echo "--tx.pool.global.queue 10240 \\">>./start.sh 220 echo "--graphql \\">>./start.sh 221 echo "--graphql.addr '0.0.0.0' \\">>./start.sh 222 echo "--graphql.port $graphqlPort \\">>./start.sh 223 echo "--graphql.cors.domain '*' \\">>./start.sh 224 echo "--graphql.vhosts '*' \\">>./start.sh 225 echo "--miner.gas.limit 238000000 \\">>./start.sh 226 echo "--hotstuff \\">>./start.sh 227 echo "--hotstuff.id $((i+1)) \\">>./start.sh 228 echo " >> app.log 2>&1 &">>./start.sh 229 230 231 #启动链服务 232 bash ./start.sh 233 sleep $waitPeriod 234 if [ ! -e "./data/sipe.ipc" ];then 235 ls ./data 236 sleep $waitPeriod 237 fi 238 #获取节点的enode 239 encodeNode=$($cmd attach data/sipe.ipc --exec "admin.nodeInfo.enode") 240 241 # shellcheck disable=SC2154 242 ip=${host} 243 244 #将127.0.0.1替换为具体的ip地址 245 remote=${encodeNode//$localIP/$ip} 246 247 encodeNodes[(($i))]=$remote 248 249 done 250 251 echo " " 252 253 echo "############# node init success #############" 254 255 echo " " 256 257 pkill sipe 258 259 len=${#encodeNodes[*]} 260 261 staticNodes='[' 262 263 for((i=0;i<len;i++)) 264 do 265 # shellcheck disable=SC2086 266 if [ $i -eq $((len-1)) ] ;then 267 staticNodes=$staticNodes${encodeNodes[$i]} 268 else 269 staticNodes=$staticNodes${encodeNodes[$i]}',' 270 fi 271 done 272 273 staticNodes=$staticNodes"]" 274 275 echo "############# static-nodes init success #############" 276 277 echo " " 278 279 # shellcheck disable=SC2164 280 cd "$workdir" 281 282 #每个目录下都有一个static-nodes.json文件 283 # shellcheck disable=SC2004 284 for((i=0;i<$allNodeCount;i++)) 285 do 286 echo "$staticNodes">>./node-$i/data/static-nodes.json 287 done 288 289 # shellcheck disable=SC2004 290 for((i=0;i<$allNodeCount;i++)) 291 do 292 # shellcheck disable=SC2164 293 cd "$workdir" 294 cd node-$i&&bash start.sh 295 done 296 297 echo "################ node start success ###################" 298 299 echo " " 300 301 # shellcheck disable=SC2164 302 cd "$workdir" 303 304 sleep $waitPeriod 305 306 #读取合约 307 code="0x$(<./Permission.bin)" 308 309 #取第一个节点 310 httpUrl="$workdir/node-0/data/sipe.ipc" 311 312 #部署合约 313 hash=$($cmd attach "${httpUrl}" --exec "eth.sendTransaction({from:eth.accounts[0],data:\"${code}\"})") 314 315 echo "############# deploy contract hash:$hash #############" 316 317 echo " " 318 319 sleep $waitPeriod 320 321 #根据哈希获取合约地址 322 323 contractAddress="" 324 325 for ((i=1; i<=6; i++)) 326 do 327 328 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 329 330 if [ "$status" = '"0x1"' ] ;then 331 echo " " 332 echo "############# contract deploy success #############" 333 echo " " 334 contractAddress=$($cmd attach "${httpUrl}" --exec "eth.getTransactionReceipt($hash).contractAddress") 335 break 336 else 337 sleep $waitPeriod 338 echo "################ wait $waitPeriod seconds,please wait ################" 339 echo " " 340 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 341 echo "################ now blockchain number is:$blockNumber ################" 342 echo " " 343 fi 344 done 345 346 if [[ $contractAddress == "" ]];then 347 echo "################ try 6 times,fail ################" 348 exit 1 349 fi 350 351 # shellcheck disable=SC2164 352 cd "$workdir" 353 354 manageHashes=() 355 356 #初始的allNodeCount个节点都是管理员节点 357 # shellcheck disable=SC2004 358 for((i=0;i<$allNodeCount;i++)) 359 do 360 httpUrl="$workdir/node-$i/data/sipe.ipc" 361 #必须给节点设置好合约地址,否则调用permission的接口各种method handler crashed 362 result=$($cmd attach "${httpUrl}" --exec "permission.setPermissionContractAddress($contractAddress)") 363 if [ "$result" != "true" ];then 364 echo "$result" 365 exit 1 366 else 367 echo "node-$i setPermissionContractAddress $result" 368 echo " " 369 fi 370 hash=$($cmd attach "${httpUrl}" --exec "permission.setAdminNode(${encodeNodes[$i]},\"node-$i\",\"${accounts[$i]}\",eth.accounts[0])") 371 if [[ $hash == \"0x* ]];then 372 manageHashes[$i]=$hash 373 else 374 echo "setAdminNode result:$hash" 375 echo " " 376 fi 377 done 378 # shellcheck disable=SC2053 379 if [[ ${#manageHashes[*]} != $allNodeCount ]];then 380 echo "################ Not all the setAdminNode success ################" 381 echo " " 382 exit 1 383 fi 384 385 386 sleep $waitPeriod 387 388 #检测设置管理员节点是否全部成功,必须保证全部成功再进行下一步 389 for((i=0;i<${#manageHashes[*]};i++)) 390 do 391 httpUrl="$workdir/node-$i/data/sipe.ipc" 392 hash=${manageHashes[$i]} 393 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 394 if [ "$status" = '"0x1"' ] ;then 395 echo "################ hash:$hash:success ################" 396 echo " " 397 else 398 #try again 399 echo "################ setAdminNode check again ################" 400 echo " " 401 sleep $waitPeriod 402 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 403 404 echo " " 405 406 echo "################ now blockchain number is:$blockNumber ################" 407 408 echo " " 409 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 410 if [ "$status" = '"0x1"' ] ;then 411 echo "################ hash:$hash:success ################" 412 echo " " 413 break 414 else 415 echo "################ hash:$hash:$status ################" 416 echo " " 417 exit 1 418 fi 419 fi 420 done 421 422 httpUrl="$workdir/node-0/data/sipe.ipc" 423 424 #结束网络的初始化 425 hash=$($cmd attach "${httpUrl}" --exec "permission.initFinish(eth.accounts[0])") 426 427 sleep $waitPeriod 428 429 for ((i=1; i<=3; i++)) 430 do 431 432 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 433 if [ "$status" = '"0x1"' ] ;then 434 435 echo "################ hash:$hash ################" 436 437 echo " " 438 439 echo "################ chain network init success ################" 440 441 echo " " 442 443 break 444 445 else 446 echo "################ initFinish check again ################" 447 echo " " 448 sleep $waitPeriod 449 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 450 echo " " 451 echo "################ now blockchain number is:$blockNumber ################" 452 echo " " 453 fi 454 done 455 456 # shellcheck disable=SC2009 457 ps aux|grep sipe 458 459 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 460 461 echo " " 462 463 echo "################ now blockchain number is:$blockNumber ################" 464 465 echo " " 466 467 end=$(date +%s) 468 469 delta=$((end-start)) 470 471 echo " " 472 473 echo "################ costs $delta seconds ################" 474 475 echo " " 476 477 #set +x