github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/tools/deploy/deploy_contract_on_single.sh (about) 1 #!/bin/bash 2 3 #################################本脚本适合于在一台服务器上进行区块链的体验#################################### 4 #使用方法 5 # bash network-on-single.sh 6 7 #注意!注意!注意!本机的IP地址 8 9 set -x 10 11 start=$(date +%s) 12 13 #服务器ip 14 host=192.168.124.5 15 16 #各个节点的http端口 17 ports=(7545 8545 9545) 18 19 #等待时间 20 waitPeriod=3 21 22 # shellcheck disable=SC2046 23 # shellcheck disable=SC2164 24 workdir=$(cd $(dirname "$0"); pwd) 25 26 cmd="./sipe" 27 28 if [ ! -e $cmd ];then 29 echo "Make sure the file $cmd exists" 30 exit 1 31 fi 32 33 if [ ! -e ./Permission.bin ];then 34 echo "Make sure the file ./Permission.bin exists" 35 exit 1 36 fi 37 38 chmod +x $cmd 39 40 #读取合约 41 code="0x$(<./Permission.bin)" 42 43 #节点连接 44 httpUrl="http://${host}:${ports[0]}" 45 46 #部署合约 47 hash=$($cmd attach "${httpUrl}" --exec "eth.sendTransaction({from:eth.accounts[0],data:\"${code}\"})") 48 49 echo "############# deploy contract hash:$hash #############" 50 51 echo " " 52 53 sleep $waitPeriod 54 55 #根据哈希获取合约地址 56 57 contractAddress="" 58 59 for ((i=1; i<=6; i++)) 60 do 61 62 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 63 64 if [ "$status" = '"0x1"' ] ;then 65 echo " " 66 echo "############# contract deploy success #############" 67 echo " " 68 contractAddress=$($cmd attach "${httpUrl}" --exec "eth.getTransactionReceipt($hash).contractAddress") 69 break 70 else 71 sleep $waitPeriod 72 echo "################ wait $waitPeriod seconds,please wait ################" 73 echo " " 74 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 75 echo "################ now blockchain number is:$blockNumber ################" 76 echo " " 77 fi 78 done 79 80 if [[ $contractAddress == "" ]];then 81 echo "################ try 6 times,fail ################" 82 exit 1 83 fi 84 85 #合约部署完毕 86 87 # shellcheck disable=SC2164 88 cd "$workdir" 89 90 manageHashes=() 91 92 #初始的ALL_NODE_COUNT个节点都是管理员节点 93 for((i=0;i<${#ports[*]};i++)) 94 do 95 httpUrl="http://${host}:${ports[$i]}" 96 #必须给节点设置好合约地址,否则调用permission的接口各种method handler crashed 97 result=$($cmd attach "${httpUrl}" --exec "permission.setPermissionContractAddress($contractAddress)") 98 if [ "$result" != "true" ];then 99 echo "$result" 100 exit 1 101 else 102 echo "node-$i setPermissionContractAddress $result" 103 echo " " 104 fi 105 hash=$($cmd attach "${httpUrl}" --exec "permission.setAdminNode(admin.nodeInfo.enode,\"node-$i\",eth.accounts[0],eth.accounts[0])") 106 if [[ $hash == \"0x* ]];then 107 manageHashes[$i]=$hash 108 else 109 echo "setAdminNode result:$hash" 110 echo " " 111 fi 112 done 113 114 # shellcheck disable=SC2053 115 if [[ ${#manageHashes[*]} != ${#ports[*]} ]];then 116 echo "################ Not all the setAdminNode success ################" 117 echo " " 118 exit 1 119 fi 120 121 122 sleep $waitPeriod 123 124 #检测设置管理员节点是否全部成功,必须保证全部成功再进行下一步 125 for((i=0;i<${#manageHashes[*]};i++)) 126 do 127 httpUrl="http://${host}:${ports[0]}" 128 hash=${manageHashes[$i]} 129 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 130 if [ "$status" = '"0x1"' ] ;then 131 echo "################ hash:$hash:success ################" 132 echo " " 133 else 134 #try again 135 echo "################ setAdminNode check again ################" 136 echo " " 137 sleep $waitPeriod 138 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 139 140 echo " " 141 142 echo "################ now blockchain number is:$blockNumber ################" 143 144 echo " " 145 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 146 if [ "$status" = '"0x1"' ] ;then 147 echo "################ hash:$hash:success ################" 148 echo " " 149 break 150 else 151 echo "################ hash:$hash:$status ################" 152 echo " " 153 exit 1 154 fi 155 fi 156 done 157 158 httpUrl="http://${host}:${ports[0]}" 159 160 #结束网络的初始化 161 hash=$($cmd attach "${httpUrl}" --exec "permission.initFinish(eth.accounts[0])") 162 163 sleep $waitPeriod 164 165 for ((i=1; i<=3; i++)) 166 do 167 168 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 169 if [ "$status" = '"0x1"' ] ;then 170 171 echo "################ hash:$hash ################" 172 173 echo " " 174 175 echo "################ chain network init success ################" 176 177 echo " " 178 179 break 180 181 else 182 echo "################ initFinish check again ################" 183 echo " " 184 sleep $waitPeriod 185 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 186 echo " " 187 echo "################ now blockchain number is:$blockNumber ################" 188 echo " " 189 fi 190 done 191 192 ps aux|grep sipe 193 194 blockNumber=$($cmd attach "${httpUrl}" --exec "eth.blockNumber") 195 196 echo " " 197 198 echo "################ now blockchain number is:$blockNumber ################" 199 200 echo " " 201 202 end=$(date +%s) 203 204 delta=$((end-start)) 205 206 echo " " 207 208 echo "################ costs $delta seconds ################" 209 210 echo " " 211 212 set +x