github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/tools/deploy/deploy_contract_on_multiple.sh (about) 1 #!/bin/bash 2 3 #本脚本在节点不启动CA的情况下使用 4 #本脚本部署合约并设置管理员节点和完成联盟的初始化 5 #本脚本不具备添加普通节点的功能 6 7 start=$(date +%s) 8 9 if [ ! -e ./param_config.sh ];then 10 echo "Make sure the file ./host_config.sh exists" 11 exit 1 12 fi 13 source ./param_config.sh 14 15 cmd="./sipe" 16 17 if [ ! -e $cmd ];then 18 echo "Make sure the file $cmd exists" 19 exit 1 20 fi 21 22 chmod +x $cmd 23 24 if [ ! -e ./Permission.bin ];then 25 echo "Make sure the file ./Permission.bin exists" 26 exit 1 27 fi 28 #等待时间 29 waitPeriod=3 30 31 # shellcheck disable=SC2154 32 allNodeCount=${#allNodeHosts[*]} 33 34 accountCount=${#accountPasswords[*]} 35 36 # shellcheck disable=SC2086 37 # shellcheck disable=SC2154 38 if [ $initNodeCount -gt $allNodeCount ];then 39 echo "The number of initial nodes is illegal. " 40 echo "The number of initial nodes must be less than or equal to the total number of nodes" 41 exit 1 42 fi 43 44 #一个节点一个账户,所以allNodeHosts的个数和accountPasswords个数必须一致 45 # shellcheck disable=SC2086 46 if [ $allNodeCount != $accountCount ];then 47 echo "all node hosts must equal account password" 48 exit 1 49 fi 50 51 #先确认,各个节点已经连接完成 52 expected=$((allNodeCount-1)) 53 54 for((i=0;i<${#allNodeHosts[*]};i++)) 55 do 56 # shellcheck disable=SC2027 57 # shellcheck disable=SC2154 58 httpUrl="http://"${allNodeHosts[$i]}":$httpPort" 59 60 peerCount=$(${cmd} attach "${httpUrl}" --exec "admin.peers.length") 61 62 echo "节点 ${allNodeHosts[$i]} 连接数为:$peerCount" 63 64 # shellcheck disable=SC2086 65 if [ $peerCount != $expected ];then 66 i=0 67 sleep $waitPeriod 68 fi 69 done 70 71 #读取合约 72 code="0x$(<./Permission.bin)" 73 74 #取第一个节点 75 # shellcheck disable=SC2027 76 httpUrl="http://"${allNodeHosts[0]}":$httpPort" 77 78 #部署合约 79 hash=$($cmd attach "${httpUrl}" --exec "eth.sendTransaction({from:eth.accounts[0],data:\"${code}\"})") 80 81 echo "deploy contract hash:$hash" 82 83 sleep $waitPeriod 84 85 #根据哈希获取合约地址 86 for ((i=1; i<=3; i++)) 87 do 88 contractAddress="" 89 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 90 echo "status:$status" 91 if [ "$status" = '"0x1"' ] ;then 92 echo "contract deploy success" 93 contractAddress=$($cmd attach "${httpUrl}" --exec "eth.getTransactionReceipt($hash).contractAddress") 94 break 95 else 96 sleep $waitPeriod 97 fi 98 done 99 100 if [[ $contractAddress == "" ]];then 101 echo "try 3 times,fail" 102 exit 1 103 fi 104 105 manageHashes=() 106 107 #初始的initNodeCount个节点都是管理员节点 108 # shellcheck disable=SC2004 109 for((i=0;i<${initNodeCount};i++)) 110 do 111 # shellcheck disable=SC2027 112 httpUrl="http://"${allNodeHosts[$i]}":$httpPort" 113 result=$($cmd attach "${httpUrl}" --exec "permission.setPermissionContractAddress($contractAddress)") 114 if [ "$result" != "true" ];then 115 echo "$result" 116 exit 1 117 fi 118 # shellcheck disable=SC2154 119 hash=$($cmd attach "${httpUrl}" --exec "permission.setAdminNode(admin.nodeInfo.enode,\"${nodeNames[$i]}\",eth.accounts[0],eth.accounts[0])") 120 if [[ $hash == \"0x* ]];then 121 echo "hash:$hash;" 122 manageHashes[$i]=$hash 123 else 124 echo "result:$hash" 125 echo "httpUrl:${httpUrl}" 126 fi 127 done 128 # shellcheck disable=SC2053 129 if [[ ${#manageHashes[*]} != $initNodeCount ]];then 130 echo "Not all the setAdminNode success,want $initNodeCount ,got ${#manageHashes[*]}" 131 exit 1 132 fi 133 134 sleep $waitPeriod 135 136 #检测设置管理员节点是否全部成功,必须保证全部成功再进行下一步 137 for((i=0;i<${#manageHashes[*]};i++)) 138 do 139 # shellcheck disable=SC2027 140 httpUrl="http://"${allNodeHosts[$i]}":$httpPort" 141 hash=${manageHashes[$i]} 142 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 143 if [ "$status" = '"0x1"' ] ;then 144 echo "hash:$hash:success" 145 else 146 echo "hash:$hash:$status" 147 echo "try getTransactionReceipt again" 148 sleep $waitPeriod 149 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 150 if [ "$status" = '"0x1"' ] ;then 151 echo "hash:$hash:success" 152 else 153 echo "hash:$hash:$status" 154 exit 1 155 fi 156 fi 157 done 158 159 # shellcheck disable=SC2027 160 httpUrl="http://"${allNodeHosts[0]}":$httpPort" 161 162 #结束网络的初始化 163 hash=$($cmd attach "${httpUrl}" --exec "permission.initFinish(eth.accounts[0])") 164 165 sleep $waitPeriod 166 167 for((i=0;i<3;i++)) 168 do 169 status=$($cmd attach "${httpUrl}" --exec "var receipt=eth.getTransactionReceipt($hash);if(receipt!=null){receipt.status}") 170 if [ "$status" = '"0x1"' ] ;then 171 echo "hash:$hash:success" 172 echo "chain network init success" 173 break 174 else 175 echo "hash:$hash:$status" 176 echo "try again" 177 sleep $waitPeriod 178 fi 179 done 180 181 result=$($cmd attach "${httpUrl}" --exec "permission.isNetworkInitFinished()") 182 183 echo "IsNetworkInitFinished:$result" 184 185 end=$(date +%s) 186 187 delta=$((end-start)) 188 189 echo "costs $delta seconds." 190