github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/dev/testnet/upgrade.sh (about) 1 #!/usr/bin/env bash 2 3 NUM_NODE=4 4 5 set -e 6 set -o errexit 7 set -a 8 set -m 9 10 # set -x # activate debugging 11 12 source fbc.profile 13 PRERUN=false 14 15 REST_PORT_MAP='{"val0":8545,"val1":8645,"val2":8745,"val3":8845,"rpc4":8945,"rpc5":9045}' 16 RPC_PORT_MAP='{"val0":26657,"val1":26757,"val2":26857,"val3":26957,"rpc4":27057,"rpc5":27157}' 17 18 function killbyname_gracefully() { 19 NAME=$1 20 ps -ef|grep "$NAME"|grep -v grep |awk '{print "kill "$2", "$8}' 21 ps -ef|grep "$NAME"|grep -v grep |awk '{print "kill "$2}' | sh 22 echo "All <$NAME> killed gracefully!" 23 } 24 25 function build_fbchain() { 26 version=$1 27 (cd ../.. && git checkout dev && git pull && git checkout $version && make install) 28 echo "fbchaind version ////" 29 fbchaind version 30 } 31 32 function get_latest_height() { 33 node=$1 34 port=`echo $RPC_PORT_MAP | jq .$node` 35 height=`fbchaincli status --node http://${IP}:${port} | jq .sync_info.latest_block_height | awk '{ gsub(/"/,""); print $0 }'` 36 echo $height 37 } 38 39 function get_tx_count_of_height() { 40 node=$1 41 height=$2 42 port=`echo $REST_PORT_MAP | jq .$node` 43 hex_height=`printf "0x%x" $height` 44 data_json='{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["'${hex_height}'"],"id":1}' 45 tx_count=`curl -X POST --data $data_json -H "Content-Type: application/json" http://${IP}:$port -s | jq .result | awk '{ gsub(/"/,""); print $0 }' ` 46 echo $tx_count 47 } 48 49 function check_block() { 50 # node_name should like val1 rpc2 .... 51 node_name=$1 52 # extra_check is an optional parameter 53 # This function also checks that new block contains tx when it be set to "tx" 54 extra_check=$2 55 56 echo "Check block: $node_name" 57 i=0 58 is_valid=0 59 base_height=`get_latest_height $node_name` 60 61 # To record the count of block that contains tx 62 block_contains_tx=0 63 while [ $i -le 600 ] 64 do 65 latest_height=`get_latest_height $node_name` 66 echo "latest_height,"$latest_height 67 latest_tx_count=`get_tx_count_of_height $node_name $latest_height` 68 echo "tx count,"$latest_tx_count 69 if [[ $latest_tx_count != "0x0" ]] ; then 70 let block_contains_tx+=1 71 fi 72 73 if [[ $extra_check = "tx" ]] ; then 74 # Need to get new blocks over 25 and make sure those blocks contains tx 75 if [ `expr $latest_height - $base_height` -gt 25 -a $block_contains_tx -gt 25 ] ;then 76 echo "block_contains_tx ,"$block_contains_tx 77 is_valid=1 78 break 79 fi 80 else 81 # Only check that the node generates blocks over 10 82 if [ `expr $latest_height - $base_height` -gt 10 ] ;then 83 is_valid=1 84 break 85 fi 86 fi 87 88 let i+=1 89 sleep 1 90 echo "Checking... $latest_height" 91 done 92 93 if [ $is_valid -eq 0 ] ;then 94 echo "Check valid $node_name: Failed, not pass." 95 exit 99 96 else 97 echo "Check valid $node_name: Successful, pass." 98 fi 99 } 100 101 function check_block_all() { 102 echo "Check all nodes generate block" 103 check_block val0 104 check_block val1 105 check_block val2 106 check_block val3 107 check_block rpc4 108 check_block rpc5 109 } 110 111 function send_tx() { 112 echo "Start sending tx ..." 113 (cd ../client/ && bash run.sh > /dev/null 2>&1 &) 114 } 115 116 function start_node() { 117 index=$1 118 node_name=$2 119 fbchaind_opts=${@:3} 120 121 if [[ $index == "0" ]] ; then 122 p2pport=${seedp2pport} 123 rpcport=${seedrpcport} 124 else 125 ((p2pport = BASE_PORT_PREFIX + index * 100 + P2P_PORT_SUFFIX)) 126 ((rpcport = BASE_PORT_PREFIX + index * 100 + RPC_PORT_SUFFIX)) 127 fi 128 ((restport = index * 100 + REST_PORT)) 129 130 LOG_LEVEL=main:info,*:error,consensus:error,state:info,provider:info 131 132 nohup ${BIN_NAME} start \ 133 --chain-id ${CHAIN_ID} \ 134 --home cache/node${index}/fbchaind \ 135 --p2p.laddr tcp://${IP}:${p2pport} \ 136 --rpc.laddr tcp://${IP}:${rpcport} \ 137 --rest.laddr tcp://${IP}:${restport} \ 138 --log_level ${LOG_LEVEL} \ 139 --enable-gid \ 140 --append-pid=true \ 141 --p2p.addr_book_strict=false \ 142 --enable-preruntx=${PRERUN} \ 143 ${fbchaind_opts} \ 144 > cache/${node_name}.log 2>&1 & 145 } 146 147 function add_val() { 148 index=$1 149 node_name=val${index} 150 seed_addr=$(fbchaind tendermint show-node-id --home cache/node0/fbchaind)@${IP}:${seedp2pport} 151 echo "add val >>> "$node_name 152 153 fbchaind_opts="--p2p.allow_duplicate_ip --p2p.pex=false --p2p.addr_book_strict=false --consensus.timeout_commit 600ms --upload-delta=false --elapsed DeliverTxs=0,Round=1,CommitRound=1,Produce=1 --consensus-role=v${index} --p2p.seeds ${seed_addr} " 154 155 start_node $index $node_name $fbchaind_opts 156 } 157 158 function add_seed() { 159 index=0 160 node_name=val${index} 161 echo "add seed >>> "$node_name 162 163 fbchaind_opts="--p2p.seed_mode=true --p2p.allow_duplicate_ip --p2p.pex=false --p2p.addr_book_strict=false --consensus.timeout_commit 600ms --upload-delta=false --elapsed DeliverTxs=0,Round=1,CommitRound=1,Produce=1 --consensus-role=v$index " 164 165 start_node $index $node_name $fbchaind_opts 166 } 167 168 function add_rpc() { 169 index=$1 170 node_name=rpc${index} 171 echo "add rpc >>> "$node_name 172 173 seed_addr=$(fbchaind tendermint show-node-id --home cache/node0/fbchaind)@${IP}:${seedp2pport} 174 175 fbchaind_opts="--p2p.seeds ${seed_addr} " 176 start_node $index $node_name $fbchaind_opts 177 } 178 179 function case_prepare() { 180 # Prepare 4 validators and 2 rpc nodes 181 version1=$1 182 183 killbyname_gracefully ${BIN_NAME} 184 killbyname_gracefully "run.sh" 185 killbyname_gracefully "./client" 186 187 bash testnet.sh -i 188 build_fbchain $version1 189 bash testnet.sh -s -n 4 190 bash addnewnode.sh -n 4 191 bash addnewnode.sh -n 5 192 } 193 194 function clean_in_the_end() { 195 echo "Clean after running cases ..." 196 killbyname_gracefully ${BIN_NAME} 197 killbyname_gracefully "run.sh" 198 killbyname_gracefully "./client" 199 200 rm -rf ./cache 201 echo "Clean finished!" 202 } 203 204 function caseopt() { 205 echo "Perform caseopt()" 206 version1=$1 207 version2=$2 208 209 case_1 $version1 $version2 210 case_2 $version1 $version2 211 case_3 $version1 $version2 212 echo "All cases finished!" 213 214 clean_in_the_end 215 } 216 217 function case_1() { 218 # Upgrade 1 rpc node , then upgrade 1 validator node. 219 echo "[][][][][][][][][][][][][][][][][][]" 220 echo "[][][][][] case_1 [][][][][]" 221 echo "[][][][][][][][][][][][][][][][][][]" 222 223 version1=$1 224 version2=$2 225 226 # pre 227 case_prepare $version1 228 # extend opts below.... 229 230 #STEP sleep 231 sleep 20 232 233 check_block_all 234 235 #STEP send tx 236 send_tx 237 238 #STEP sleep 239 sleep 30 240 241 #STEP kill rpc 242 killbyname_gracefully "cache/node4/fbchaind" 243 sleep 2 244 245 #STEP BUILD version2 246 build_fbchain $version2 247 248 #STEP add rpc 249 add_rpc 4 250 251 #STEP sleep 252 sleep 30 253 254 #STEP CHECK BLOCK ALL 255 check_block rpc4 tx 256 257 #STEP kill 25% v 258 killbyname_gracefully "cache/node3/fbchaind" 259 sleep 3 260 261 #STEP add v 262 add_val 3 263 sleep 30 264 265 #STEP CHECK val 266 check_block val3 tx 267 } 268 269 function case_2() { 270 # Upgrade 25% validator, then upgrade rest of the validators ,and then upgrade all the rpc nodes. 271 version1=$1 272 version2=$2 273 echo "[][][][][][][][][][][][][][][][][][]" 274 echo "[][][][][] case_2 [][][][][]" 275 echo "[][][][][][][][][][][][][][][][][][]" 276 277 # pre 278 case_prepare $version1 279 280 #STEP sleep 281 sleep 20 282 283 #STEP check block ,all 284 check_block_all 285 286 #STEP send tx 287 send_tx 288 sleep 30 289 290 #STEP BUILD version2 291 build_fbchain $version2 292 293 #STEP upgrade 25% v 294 killbyname_gracefully "cache/node3/fbchaind" 295 sleep 3 296 add_val 3 297 sleep 20 298 299 #STEP check block 300 check_block val3 tx 301 302 #STEP upgrade 100% v 303 killbyname_gracefully "cache/node2/fbchaind" 304 sleep 3 305 add_val 2 306 307 killbyname_gracefully "cache/node1/fbchaind" 308 sleep 3 309 add_val 1 310 311 killbyname_gracefully "cache/node0/fbchaind" 312 sleep 3 313 add_seed 314 315 sleep 30 316 317 #STEP check block 318 check_block val2 tx 319 check_block val1 tx 320 check_block val0 tx 321 322 #STEP upgrade 100% rpc 323 #STEP kill rpc 324 killbyname_gracefully "cache/node4/fbchaind" 325 killbyname_gracefully "cache/node5/fbchaind" 326 sleep 3 327 328 #STEP add rpc 329 add_rpc 4 330 add_rpc 5 331 332 #STEP check block 333 sleep 30 334 check_block rpc4 tx 335 check_block rpc5 tx 336 } 337 338 function case_3() { 339 # Upgrade all the validators,then upgrade 1 RPC 340 version1=$1 341 version2=$2 342 echo "[][][][][][][][][][][][][][][][][][]" 343 echo "[][][][][] case_3 [][][][][]" 344 echo "[][][][][][][][][][][][][][][][][][]" 345 346 # pre 347 case_prepare $version1 348 349 #STEP sleep 350 sleep 20 351 352 #STEP check block val 353 check_block_all 354 355 #STEP send tx 356 send_tx 357 sleep 30 358 359 #STEP BUILD version2 360 build_fbchain $version2 361 362 #STEP upgrade 100% v 363 killbyname_gracefully "cache/node3/fbchaind" 364 killbyname_gracefully "cache/node2/fbchaind" 365 killbyname_gracefully "cache/node1/fbchaind" 366 killbyname_gracefully "cache/node0/fbchaind" 367 sleep 3 368 369 add_seed 370 add_val 1 371 add_val 2 372 add_val 3 373 sleep 20 374 375 #STEP check block val 376 check_block val0 tx 377 check_block val1 tx 378 check_block val2 tx 379 check_block val3 tx 380 381 #STEP upgrade 1 rpc 382 killbyname_gracefully "cache/node5/fbchaind" 383 add_rpc 5 384 sleep 10 385 386 #STEP check block rpc 387 check_block rpc5 tx 388 389 } 390 391 if [ -z ${IP} ]; then 392 IP="127.0.0.1" 393 fi 394 395 ### send two params , the first is the old version of fbc, the second is the newer version. 396 exc_version1=$1 397 exc_version2=$2 398 caseopt $exc_version1 $exc_version2