github.com/turingchain2020/turingchain@v1.1.21/build/system-fork-test.sh (about) 1 #!/usr/bin/env bash 2 # shellcheck disable=SC2178 3 set +e 4 5 PWD=$(cd "$(dirname "$0")" && pwd) 6 export PATH="$PWD:$PATH" 7 8 NODE3="${1}_turingchain_1" 9 CLI="docker exec ${NODE3} /root/turingchain-cli" 10 11 NODE2="${1}_chain32_1" 12 CLI2="docker exec ${NODE2} /root/turingchain-cli" 13 14 NODE1="${1}_chain31_1" 15 CLI3="docker exec ${NODE1} /root/turingchain-cli" 16 17 NODE4="${1}_chain30_1" 18 CLI4="docker exec ${NODE4} /root/turingchain-cli" 19 20 NODE5="${1}_chain29_1" 21 CLI5="docker exec ${NODE5} /root/turingchain-cli" 22 23 NODE6="${1}_chain28_1" 24 CLI6="docker exec ${NODE6} /root/turingchain-cli" 25 26 containers=("${NODE1}" "${NODE2}" "${NODE3}" "${NODE4}" "${NODE5}" "${NODE6}") 27 forkContainers=("${CLI3}" "${CLI2}" "${CLI}" "${CLI4}" "${CLI5}" "${CLI6}") 28 29 export COMPOSE_PROJECT_NAME="$1" 30 31 sedfix="" 32 if [ "$(uname)" == "Darwin" ]; then 33 sedfix=".bak" 34 fi 35 36 DAPP="" 37 if [ -n "${2}" ]; then 38 DAPP=$2 39 fi 40 41 DAPP_TEST_FILE="" 42 43 if [ -n "${DAPP}" ]; then 44 testfile="fork-test.sh" 45 if [ -e "$testfile" ]; then 46 # shellcheck source=/dev/null 47 source "${testfile}" 48 DAPP_TEST_FILE="$testfile" 49 fi 50 51 DAPP_COMPOSE_FILE="docker-compose-${DAPP}.yml" 52 if [ -e "$DAPP_COMPOSE_FILE" ]; then 53 export COMPOSE_FILE="docker-compose.yml:${DAPP_COMPOSE_FILE}" 54 55 fi 56 57 fi 58 59 system_coins_file="system/coins/fork-test.sh" 60 # shellcheck source=/dev/null 61 source "${system_coins_file}" 62 63 echo "=========== # env setting =============" 64 echo "DAPP=$DAPP" 65 echo "DAPP_TEST_FILE=$DAPP_TEST_FILE" 66 echo "COMPOSE_FILE=$COMPOSE_FILE" 67 echo "COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME" 68 echo "CLI=$CLI" 69 70 function base_init() { 71 # update test environment 72 sed -i $sedfix 's/^Title.*/Title="local"/g' turingchain.toml 73 sed -i $sedfix 's/^TestNet=.*/TestNet=true/g' turingchain.toml 74 75 # p2p 76 sed -i $sedfix 's/^seeds=.*/seeds=["turingchain:13802","chain32:13802","chain31:13802","chain30:13802","chain29:13802","chain28:13802"]/g' turingchain.toml 77 sed -i $sedfix 's/^enable=.*/enable=true/g' turingchain.toml 78 sed -i $sedfix 's/^isSeed=.*/isSeed=true/g' turingchain.toml 79 sed -i $sedfix 's/^innerSeedEnable=.*/innerSeedEnable=false/g' turingchain.toml 80 sed -i $sedfix 's/^useGithub=.*/useGithub=false/g' turingchain.toml 81 82 # rpc 83 sed -i $sedfix 's/^jrpcBindAddr=.*/jrpcBindAddr="0.0.0.0:9671"/g' turingchain.toml 84 sed -i $sedfix 's/^grpcBindAddr=.*/grpcBindAddr="0.0.0.0:9672"/g' turingchain.toml 85 sed -i $sedfix 's/^whitelist=.*/whitelist=["localhost","127.0.0.1","0.0.0.0"]/g' turingchain.toml 86 87 # wallet 88 sed -i $sedfix 's/^minerdisable=.*/minerdisable=false/g' turingchain.toml 89 90 } 91 92 function start() { 93 # docker-compose ps 94 docker-compose ps 95 96 # remove exsit container 97 docker-compose down 98 99 # create and run docker-compose container 100 # docker-compose -f docker-compose.yml -f docker-compose-para.yml up --build -d 101 docker-compose up --build -d 102 103 local SLEEP=30 104 echo "=========== sleep ${SLEEP}s =============" 105 sleep ${SLEEP} 106 107 docker-compose ps 108 109 # query node run status 110 111 ${CLI} block last_header 112 ${CLI} net info 113 114 ${CLI} net peer 115 peersCount=$(${CLI} net peer | jq '.[] | length') 116 echo "${peersCount}" 117 if [ "${peersCount}" -lt 2 ]; then 118 sleep 20 119 peersCount=$(${CLI} net peer | jq '.[] | length') 120 echo "${peersCount}" 121 if [ "${peersCount}" -lt 2 ]; then 122 echo "peers error" 123 exit 1 124 fi 125 fi 126 127 #echo "=========== # create seed for wallet =============" 128 #seed=$(${CLI} seed generate -l 0 | jq ".seed") 129 #if [ -z "${seed}" ]; then 130 # echo "create seed error" 131 # exit 1 132 #fi 133 134 echo "=========== # save seed to wallet =============" 135 result=$(${CLI} seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib" | jq ".isok") 136 if [ "${result}" = "false" ]; then 137 echo "save seed to wallet error seed, result: ${result}" 138 exit 1 139 fi 140 141 sleep 1 142 143 echo "=========== # unlock wallet =============" 144 result=$(${CLI} wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 145 if [ "${result}" = "false" ]; then 146 exit 1 147 fi 148 149 sleep 1 150 151 echo "=========== # import private key returnAddr =============" 152 result=$(${CLI} account import_key -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944 -l returnAddr | jq ".label") 153 echo "${result}" 154 if [ -z "${result}" ]; then 155 exit 1 156 fi 157 158 sleep 1 159 160 echo "=========== # import private key mining =============" 161 result=$(${CLI} account import_key -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01 -l minerAddr | jq ".label") 162 echo "${result}" 163 if [ -z "${result}" ]; then 164 exit 1 165 fi 166 167 sleep 1 168 echo "=========== # close auto mining =============" 169 result=$(${CLI} wallet auto_mine -f 0 | jq ".isok") 170 if [ "${result}" = "false" ]; then 171 exit 1 172 fi 173 174 ## 2nd mining 175 echo "=========== # save seed to wallet =============" 176 result=$(${CLI4} seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib" | jq ".isok") 177 if [ "${result}" = "false" ]; then 178 echo "save seed to wallet error seed, result: ${result}" 179 exit 1 180 fi 181 182 sleep 1 183 184 echo "=========== # unlock wallet =============" 185 result=$(${CLI4} wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 186 if [ "${result}" = "false" ]; then 187 exit 1 188 fi 189 190 sleep 1 191 192 echo "=========== # import private key returnAddr =============" 193 result=$(${CLI4} account import_key -k 2AFF1981291355322C7A6308D46A9C9BA311AA21D94F36B43FC6A6021A1334CF -l returnAddr | jq ".label") 194 echo "${result}" 195 if [ -z "${result}" ]; then 196 exit 1 197 fi 198 199 sleep 1 200 201 echo "=========== # import private key mining =============" 202 result=$(${CLI4} account import_key -k 2116459C0EC8ED01AA0EEAE35CAC5C96F94473F7816F114873291217303F6989 -l minerAddr | jq ".label") 203 echo "${result}" 204 if [ -z "${result}" ]; then 205 exit 1 206 fi 207 208 sleep 1 209 echo "=========== # close auto mining =============" 210 result=$(${CLI4} wallet auto_mine -f 0 | jq ".isok") 211 if [ "${result}" = "false" ]; then 212 exit 1 213 fi 214 215 block_wait "${CLI}" 1 216 217 echo "=========== check genesis hash ========== " 218 ${CLI} block hash -t 0 219 res=$(${CLI} block hash -t 0 | jq ".hash") 220 count=$(echo "$res" | grep -c "0x67c58d6ba9175313f0468ae4e0ddec946549af7748037c2fdd5d54298afd20b6") 221 if [ "${count}" != 1 ]; then 222 echo "genesis hash error!" 223 exit 1 224 fi 225 226 echo "=========== query height ========== " 227 ${CLI} block last_header 228 result=$(${CLI} block last_header | jq ".height") 229 if [ "${result}" -lt 1 ]; then 230 exit 1 231 fi 232 233 ${CLI} wallet status 234 ${CLI} account list 235 ${CLI} mempool list 236 } 237 238 function dapp_run() { 239 if [ -e "$DAPP_TEST_FILE" ]; then 240 ${DAPP} "${CLI}" "${1}" 241 fi 242 243 } 244 245 function optDockerfun() { 246 ############################################# 247 #1 第一种分叉构造:首先两条链进行共同挖矿,然后再分 248 # 别进行挖矿,即两条链上发生分叉时候的交易是不同的。 249 ############################################# 250 forkType1 251 ############################################# 252 #2 第二种分叉构造:包括第一组docker,第二组docker, 253 # 以及公共节点docker,首先共同挖矿,然后停掉第二组 254 # docker,备份公共节点docker数据库,在公共节点docker 255 # 上创建交易,签名交易,记录签名,发送,然后关掉第一组 256 # docker,然后将公共节点docker数据库恢复到备份状态, 257 # 然后启动第二组docker,然后发送刚刚记录签名的交易。 258 # 最后启动全部节点共同挖矿 259 ############################################# 260 forkType2 261 } 262 263 function forkType1() { 264 echo "=========== 开始进行类型1分叉测试 ========== " 265 base_init 266 dapp_run forkInit 267 268 start 269 optDockerPart1 270 ############################################# 271 #此处根据具体需求加入;如从钱包中转入某个具体合约账户 272 #1 初始化交易余额 273 dapp_run forkConfig 274 275 ############################################# 276 optDockerPart2 277 ############################################# 278 #此处根据具体需求加入在一条测试链中发送测试数据 279 #2 构造第一条链中交易 280 dapp_run forkAGroupRun 281 282 ############################################# 283 optDockerPart3 284 ############################################# 285 #此处根据具体需求加入在第二条测试链中发送测试数据 286 #3 构造第二条链中交易 287 dapp_run forkBGroupRun 288 289 ############################################# 290 optDockerPart4 291 loopCount=30 #循环次数,每次循环休眠时间100s 292 checkBlockHashfun $loopCount 293 294 ############################################# 295 #此处根据具体需求加入结果检查 296 #4 检查交易结果 297 dapp_run forkCheckRst 298 299 ############################################# 300 echo "=========== 类型1分叉测试结束 ========== " 301 } 302 303 function forkType2() { 304 echo "=========== 开始进行类型2分叉测试 ========== " 305 base_init 306 dapp_run fork2Init 307 308 start 309 310 optDockerPart1 311 ############################################# 312 #此处根据具体需求加入;如从钱包中转入某个具体合约账户 313 #1 初始化交易余额 314 initCoinsAccount 315 dapp_run fork2Config 316 317 ############################################# 318 type2_optDockerPart2 319 ############################################# 320 #此处根据具体需求加入在一条测试链中发送测试数据 321 #2 构造第一条链中交易 322 genFirstChainCoinstx 323 dapp_run fork2AGroupRun 324 ############################################# 325 type2_optDockerPart3 326 ############################################# 327 #此处根据具体需求加入在第二条测试链中发送测试数据 328 #3 构造第二条链中交易 329 genSecondChainCoinstx 330 dapp_run fork2BGroupRun 331 332 ############################################# 333 type2_optDockerPart4 334 loopCount=30 #循环次数,每次循环休眠时间100s 335 checkBlockHashfun $loopCount 336 ############################################# 337 #此处根据具体需求加入结果检查 338 #4 检查交易结果 339 checkCoinsResult 340 dapp_run fork2CheckRst 341 ############################################# 342 echo "=========== 类型2分叉测试结束 ========== " 343 } 344 345 function optDockerPart1() { 346 echo "====== 区块生成中 ======" 347 #sleep 100 348 block_wait_timeout "${CLI}" 10 100 349 350 loopCount=20 351 for ((i = 0; i < loopCount; i++)); do 352 name="${CLI}" 353 time=2 354 needCount=6 355 peersCount "${name}" $time $needCount 356 peerStatus=$? 357 if [ $peerStatus -eq 1 ]; then 358 name="${CLI4}" 359 peersCount "${name}" $time $needCount 360 peerStatus=$? 361 if [ $peerStatus -eq 0 ]; then 362 break 363 fi 364 else 365 break 366 fi 367 #检查是否超过了最大检测次数 368 if [ $i -ge $((loopCount - 1)) ]; then 369 echo "====== peers not enough ======" 370 exit 1 371 fi 372 done 373 374 return 1 375 376 } 377 378 function optDockerPart2() { 379 checkMineHeight 380 status=$? 381 if [ $status -eq 0 ]; then 382 echo "====== All peers is the same height ======" 383 else 384 echo "====== All peers is the different height, syn blockchain fail======" 385 exit 1 386 fi 387 388 echo "===================================" 389 echo "====== 第一步:第一组docker挖矿======" 390 echo "===================================" 391 392 echo "======停止第二组docker ======" 393 docker pause "${NODE4}" "${NODE5}" "${NODE6}" 394 395 echo "======开启第一组docker节点挖矿======" 396 sleep 3 397 result=$($CLI wallet auto_mine -f 1 | jq ".isok") 398 if [ "${result}" = "false" ]; then 399 echo "start wallet2 mine fail" 400 exit 1 401 fi 402 403 name=$CLI 404 time=60 405 needCount=3 406 407 peersCount "${name}" $time $needCount 408 peerStatus=$? 409 if [ $peerStatus -eq 1 ]; then 410 echo "====== peers not enough ======" 411 exit 1 412 fi 413 414 } 415 416 function optDockerPart3() { 417 echo "======第一组docker节点挖矿中======" 418 block_wait_timeout "${CLI}" 5 100 419 echo "======停止第一组docker节点挖矿======" 420 result=$($CLI wallet auto_mine -f 0 | jq ".isok") 421 if [ "${result}" = "false" ]; then 422 echo "stop wallet2 mine fail" 423 exit 1 424 fi 425 426 echo "====== 第一组内部同步中 ======" 427 names[0]="${NODE3}" 428 names[1]="${NODE2}" 429 names[2]="${NODE1}" 430 syn_block_timeout "${CLI}" 3 50 "${names[@]}" 431 432 echo "=======================================" 433 echo "======== 第二步:第二组docker挖矿 =======" 434 echo "=======================================" 435 436 echo "======停止第一组docker======" 437 docker pause "${NODE1}" "${NODE2}" "${NODE3}" 438 439 echo "======sleep 5s======" 440 sleep 5 441 442 echo "======启动第二组docker======" 443 docker unpause "${NODE4}" "${NODE5}" "${NODE6}" 444 445 echo "======sleep 20s======" 446 sleep 5 447 result=$($CLI4 wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 448 if [ "${result}" = "false" ]; then 449 echo "wallet1 unlock fail" 450 exit 1 451 fi 452 453 name="${CLI4}" 454 time=60 455 needCount=3 456 457 peersCount "${name}" $time $needCount 458 peerStatus=$? 459 if [ $peerStatus -eq 1 ]; then 460 echo "====== peers not enough ======" 461 exit 1 462 fi 463 464 echo "======开启第二组docker节点挖矿======" 465 sleep 1 466 result=$($CLI4 wallet auto_mine -f 1 | jq ".isok") 467 if [ "${result}" = "false" ]; then 468 echo "start wallet1 mine fail" 469 exit 1 470 fi 471 472 names[0]="${NODE4}" 473 names[1]="${NODE5}" 474 names[2]="${NODE6}" 475 syn_block_timeout "${CLI4}" 2 100 "${names[@]}" 476 477 } 478 479 function optDockerPart4() { 480 echo "======第二组docker节点挖矿中======" 481 block_wait_timeout "${CLI4}" 3 50 482 echo "====== 第二组内部同步中 ======" 483 names[0]="${NODE4}" 484 names[1]="${NODE5}" 485 names[2]="${NODE6}" 486 syn_block_timeout "${CLI4}" 3 50 "${names[@]}" 487 488 echo "=======================================" 489 echo "====== 第三步:两组docker共同挖矿 =======" 490 echo "=======================================" 491 492 echo "======启动第一组docker======" 493 docker unpause "${NODE1}" "${NODE2}" "${NODE3}" 494 495 echo "======sleep 20s======" 496 sleep 5 497 result=$($CLI wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 498 if [ "${result}" = "false" ]; then 499 echo "wallet2 unlock fail" 500 exit 1 501 fi 502 echo "======开启第一组docker节点挖矿======" 503 sleep 1 504 result=$($CLI wallet auto_mine -f 1 | jq ".isok") 505 if [ "${result}" = "false" ]; then 506 echo "start wallet2 mine fail" 507 exit 1 508 fi 509 510 echo "======两组docker节点共同挖矿中======" 511 block_wait_timeout "${CLI}" 5 100 512 } 513 514 function copyData() { 515 name="${NODE3}" 516 sleep 1 517 docker exec "${name}" mkdir beifen 518 sleep 1 519 docker exec "${name}" cp -r datadir beifen 520 sleep 1 521 } 522 523 function restoreData() { 524 name="${NODE3}" 525 sleep 1 526 docker exec "${name}" rm -rf datadir 527 sleep 1 528 docker exec "${name}" cp -r beifen/datadir ./ 529 sleep 1 530 } 531 532 function type2_optDockerPart2() { 533 checkMineHeight 534 status=$? 535 if [ $status -eq 0 ]; then 536 echo "====== All peers is the same height ======" 537 else 538 echo "====== All peers is the different height, syn blockchain fail======" 539 exit 1 540 fi 541 542 echo "=============== 备份公共节点数据 ==============" 543 copyData 544 545 echo "===================================" 546 echo "====== 第一步:第一组docker挖矿======" 547 echo "===================================" 548 549 echo "======停止第二组docker ======" 550 docker pause "${NODE4}" "${NODE5}" "${NODE6}" 551 552 echo "======开启第一组docker节点挖矿======" 553 sleep 3 554 result=$($CLI wallet auto_mine -f 1 | jq ".isok") 555 if [ "${result}" = "false" ]; then 556 echo "start wallet2 mine fail" 557 exit 1 558 fi 559 560 name=$CLI 561 time=60 562 needCount=3 563 564 peersCount "${name}" $time $needCount 565 peerStatus=$? 566 if [ $peerStatus -eq 1 ]; then 567 echo "====== peers not enough ======" 568 exit 1 569 fi 570 571 } 572 573 function type2_optDockerPart3() { 574 echo "======第一组docker节点挖矿中======" 575 block_wait_timeout "${CLI}" 5 100 576 echo "======停止第一组docker节点挖矿======" 577 result=$($CLI wallet auto_mine -f 0 | jq ".isok") 578 if [ "${result}" = "false" ]; then 579 echo "stop wallet2 mine fail" 580 exit 1 581 fi 582 583 echo "====== 第一组内部同步中 ======" 584 names[0]="${NODE3}" 585 names[1]="${NODE2}" 586 names[2]="${NODE1}" 587 syn_block_timeout "${CLI}" 3 50 "${names[@]}" 588 589 echo "=======================================" 590 echo "======== 第二步:第二组docker挖矿 =======" 591 echo "=======================================" 592 593 echo "======停止第一组中除公共节点的docker======" 594 docker pause "${NODE1}" "${NODE2}" 595 596 echo "=============== 恢复公共节点数据 ==============" 597 restoreData 598 docker pause "${NODE3}" 599 600 echo "======sleep 5s======" 601 sleep 5 602 603 echo "======启动第二组docker======" 604 docker unpause "${NODE3}" "${NODE4}" "${NODE5}" "${NODE6}" 605 606 name="${CLI}" 607 time=60 608 needCount=4 609 610 peersCount "${name}" $time $needCount 611 peerStatus=$? 612 if [ $peerStatus -eq 1 ]; then 613 echo "====== peers not enough ======" 614 exit 1 615 fi 616 617 echo "======sleep 20s======" 618 sleep 20 619 echo "======开启第二组docker节点挖矿======" 620 621 result=$($CLI wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 622 if [ "${result}" = "false" ]; then 623 echo "wallet1 unlock fail" 624 exit 1 625 fi 626 627 sleep 1 628 result=$($CLI wallet auto_mine -f 1 | jq ".isok") 629 if [ "${result}" = "false" ]; then 630 echo "start wallet1 mine fail" 631 exit 1 632 fi 633 634 sleep 1 635 result=$($CLI4 wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 636 if [ "${result}" = "false" ]; then 637 echo "wallet2 unlock fail" 638 exit 1 639 fi 640 641 sleep 1 642 result=$($CLI4 wallet auto_mine -f 1 | jq ".isok") 643 if [ "${result}" = "false" ]; then 644 echo "start wallet2 mine fail" 645 exit 1 646 fi 647 648 names[0]="${NODE3}" 649 names[1]="${NODE4}" 650 names[2]="${NODE5}" 651 names[3]="${NODE6}" 652 syn_block_timeout "${CLI}" 2 100 "${names[@]}" 653 654 } 655 656 function type2_optDockerPart4() { 657 echo "======第二组docker节点挖矿中======" 658 block_wait_timeout "${CLI}" 3 50 659 echo "====== 第二组内部同步中 ======" 660 names[0]="${NODE4}" 661 names[1]="${NODE5}" 662 names[2]="${NODE6}" 663 names[3]="${NODE3}" 664 syn_block_timeout "${CLI}" 3 50 "${names[@]}" 665 666 echo "=======================================" 667 echo "====== 第三步:两组docker共同挖矿 =======" 668 echo "=======================================" 669 670 echo "======启动第一组docker======" 671 docker unpause "${NODE1}" "${NODE2}" 672 673 echo "======两组docker节点共同挖矿中======" 674 block_wait_timeout "${CLI}" 5 100 675 } 676 677 function checkMineHeight() { 678 result=$($CLI4 wallet auto_mine -f 0 | jq ".isok") 679 if [ "${result}" = "false" ]; then 680 echo "stop wallet1 mine fail" 681 return 1 682 fi 683 sleep 1 684 result=$($CLI wallet auto_mine -f 0 | jq ".isok") 685 if [ "${result}" = "false" ]; then 686 echo "stop wallet2 mine fail" 687 return 1 688 fi 689 690 echo "====== stop all wallet mine success ======" 691 692 echo "====== syn blockchain ======" 693 syn_block_timeout "${CLI}" 5 50 "${containers[@]}" 694 695 height=0 696 height1=$($CLI4 block last_header | jq ".height") 697 sleep 1 698 height2=$($CLI block last_header | jq ".height") 699 if [ "${height2}" -ge "${height1}" ]; then 700 height=$height2 701 printf '当前最大高度 %s \n' "${height}" 702 else 703 height=$height1 704 printf '当前最大高度 %s \n' "${height}" 705 fi 706 707 if [ "${height}" -eq 0 ]; then 708 echo "获取当前最大高度失败" 709 return 1 710 fi 711 loopCount=20 712 for ((k = 0; k < ${#forkContainers[*]}; k++)); do 713 for ((j = 0; j < loopCount; j++)); do 714 height1=$(${forkContainers[$k]} block last_header | jq ".height") 715 if [ "${height1}" -gt "${height}" ]; then #如果大于说明区块还没有完全产生完,替换期望高度 716 height=$height1 717 printf '查询 %s 目前区块最高高度为第 %s \n' "${containers[$k]}" "${height}" 718 elif [ "${height1}" -eq "${height}" ]; then #找到目标高度 719 break 720 else 721 printf '查询 %s 第 %d 次,当前高度 %d, 需要高度%d, 同步中,sleep 60s 后查询\n' "${containers[$k]}" $j "${height1}" "${height}" 722 sleep 60 723 fi 724 #检查是否超过了最大检测次数 725 if [ $j -ge $((loopCount - 1)) ]; then 726 echo "====== syn blockchain fail======" 727 return 1 728 fi 729 done 730 done 731 732 return 0 733 } 734 735 function peersCount() { 736 name=$1 737 time=$2 738 needCount=$3 739 740 for ((i = 0; i < time; i++)); do 741 peersCount=$($name net peer | jq '.[] | length') 742 printf '查询节点 %s ,所需节点数 %d ,当前节点数 %s \n' "${name}" "${needCount}" "${peersCount}" 743 if [ "${peersCount}" = "$needCount" ]; then 744 echo "============= 符合节点数要求 =============" 745 return 0 746 else 747 echo "============= 休眠 30s 继续查询 =============" 748 sleep 30 749 fi 750 done 751 752 return 1 753 } 754 755 function checkBlockHashfun() { 756 echo "====== syn blockchain ======" 757 syn_block_timeout "${CLI}" 10 50 "${containers[@]}" 758 759 height=0 760 hash="" 761 height1=$($CLI block last_header | jq ".height") 762 sleep 1 763 height2=$($CLI4 block last_header | jq ".height") 764 if [ "${height2}" -ge "${height1}" ]; then 765 height=$height2 766 printf "主链为 $CLI 当前最大高度 %d \\n" "${height}" 767 sleep 1 768 hash=$($CLI block hash -t "${height}" | jq ".hash") 769 else 770 height=$height1 771 printf "主链为 $CLI4 当前最大高度 %d \\n" "${height}" 772 sleep 1 773 hash=$($CLI4 block hash -t "${height}" | jq ".hash") 774 fi 775 776 for ((j = 0; j < $1; j++)); do 777 for ((k = 0; k < ${#forkContainers[*]}; k++)); do 778 sleep 1 779 height0[$k]=$(${forkContainers[$k]} block last_header | jq ".height") 780 if [ "${height0[$k]}" -ge "${height}" ]; then 781 sleep 1 782 hash0[$k]=$(${forkContainers[$k]} block hash -t "${height}" | jq ".hash") 783 else 784 hash0[$k]="${forkContainers[$k]}" 785 fi 786 done 787 788 if [ "${hash0[0]}" = "${hash}" ] && [ "${hash0[1]}" = "${hash}" ] && [ "${hash0[2]}" = "${hash}" ] && [ "${hash0[3]}" = "${hash}" ] && [ "${hash0[4]}" = "${hash}" ] && [ "${hash0[5]}" = "${hash}" ]; then 789 echo "syn blockchain success break" 790 break 791 else 792 if [ "${hash0[1]}" = "${hash0[0]}" ] && [ "${hash0[2]}" = "${hash0[0]}" ] && [ "${hash0[3]}" = "${hash0[0]}" ] && [ "${hash0[4]}" = "${hash0[0]}" ] && [ "${hash0[5]}" = "${hash0[0]}" ]; then 793 echo "syn blockchain success break" 794 break 795 fi 796 fi 797 peersCount=0 798 peersCount=$(${forkContainers[0]} net peer | jq '.[] | length') 799 printf '第 %d 次,未查询到网络同步,当前节点数 %d 个,100s后查询\n' $j "${peersCount}" 800 sleep 100 801 #检查是否超过了最大检测次数 802 var=$(($1 - 1)) 803 if [ $j -ge "${var}" ]; then 804 echo "====== syn blockchain fail======" 805 exit 1 806 fi 807 done 808 echo "====== syn blockchain success======" 809 } 810 811 # $1 name 812 # $2 txHash 813 function txQuery() { 814 name=$1 815 txHash=$2 816 result=$($name tx query -s "${txHash}" | jq -r ".receipt.tyname") 817 if [ "${result}" = "ExecOk" ]; then 818 return 0 819 fi 820 return 1 821 } 822 823 function block_wait_timeout() { 824 if [ "$#" -lt 3 ]; then 825 echo "wrong block_wait params" 826 exit 1 827 fi 828 cur_height=$(${1} block last_header | jq ".height") 829 expect=$((cur_height + ${2})) 830 count=0 831 while true; do 832 new_height=$(${1} block last_header | jq ".height") 833 if [ "${new_height}" -ge "${expect}" ]; then 834 break 835 fi 836 count=$((count + 1)) 837 sleep 1 838 if [ $count -ge "${3}" ]; then 839 echo "====== block wait timeout ======" 840 break 841 fi 842 done 843 echo "wait new block $count s" 844 } 845 846 function syn_block_timeout() { 847 #${1} name 848 #${2} minHeight 849 #${3} timeout 850 #${4} names 851 852 names=${4} 853 854 if [ "$#" -lt 3 ]; then 855 echo "wrong block_wait params" 856 exit 1 857 fi 858 cur_height=$(${1} block last_header | jq ".height") 859 expect=$((cur_height + ${2})) 860 count=0 861 while true; do 862 new_height=$(${1} block last_header | jq ".height") 863 if [ "${new_height}" -lt "${expect}" ]; then 864 count=$((count + 1)) 865 sleep 1 866 else 867 isSyn="true" 868 for ((k = 0; k < ${#names[@]}; k++)); do 869 sync_status=$(docker exec "${names[$k]}" /root/turingchain-cli net is_sync) 870 if [ "${sync_status}" = "false" ]; then 871 isSyn="false" 872 break 873 fi 874 count=$((count + 1)) 875 sleep 1 876 done 877 if [ "${isSyn}" = "true" ]; then 878 break 879 fi 880 fi 881 882 if [ $count -ge $(($3 + 1)) ]; then 883 echo "====== syn block wait timeout ======" 884 break 885 fi 886 887 done 888 echo "wait block $count s" 889 } 890 891 function block_wait() { 892 if [ "$#" -lt 2 ]; then 893 echo "wrong block_wait params" 894 exit 1 895 fi 896 cur_height=$(${1} block last_header | jq ".height") 897 expect=$((cur_height + ${2})) 898 count=0 899 while true; do 900 new_height=$(${1} block last_header | jq ".height") 901 if [ "${new_height}" -ge "${expect}" ]; then 902 break 903 fi 904 count=$((count + 1)) 905 sleep 1 906 done 907 echo "wait new block $count s" 908 } 909 910 optDockerfun